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 const char *getPassName() const override {
74 return "ARM Instruction Selection";
77 void PreprocessISelDAG() override;
79 /// getI32Imm - Return a target constant of type i32 with the specified
81 inline SDValue getI32Imm(unsigned Imm, const SDLoc &dl) {
82 return CurDAG->getTargetConstant(Imm, dl, MVT::i32);
85 void Select(SDNode *N) override;
87 bool hasNoVMLxHazardUse(SDNode *N) const;
88 bool isShifterOpProfitable(const SDValue &Shift,
89 ARM_AM::ShiftOpc ShOpcVal, unsigned ShAmt);
90 bool SelectRegShifterOperand(SDValue N, SDValue &A,
91 SDValue &B, SDValue &C,
92 bool CheckProfitability = true);
93 bool SelectImmShifterOperand(SDValue N, SDValue &A,
94 SDValue &B, bool CheckProfitability = true);
95 bool SelectShiftRegShifterOperand(SDValue N, SDValue &A,
96 SDValue &B, SDValue &C) {
97 // Don't apply the profitability check
98 return SelectRegShifterOperand(N, A, B, C, false);
100 bool SelectShiftImmShifterOperand(SDValue N, SDValue &A,
102 // Don't apply the profitability check
103 return SelectImmShifterOperand(N, A, B, false);
106 bool SelectAddrModeImm12(SDValue N, SDValue &Base, SDValue &OffImm);
107 bool SelectLdStSOReg(SDValue N, SDValue &Base, SDValue &Offset, SDValue &Opc);
109 AddrMode2Type SelectAddrMode2Worker(SDValue N, SDValue &Base,
110 SDValue &Offset, SDValue &Opc);
111 bool SelectAddrMode2Base(SDValue N, SDValue &Base, SDValue &Offset,
113 return SelectAddrMode2Worker(N, Base, Offset, Opc) == AM2_BASE;
116 bool SelectAddrMode2ShOp(SDValue N, SDValue &Base, SDValue &Offset,
118 return SelectAddrMode2Worker(N, Base, Offset, Opc) == AM2_SHOP;
121 bool SelectAddrMode2(SDValue N, SDValue &Base, SDValue &Offset,
123 SelectAddrMode2Worker(N, Base, Offset, Opc);
124 // return SelectAddrMode2ShOp(N, Base, Offset, Opc);
125 // This always matches one way or another.
129 bool SelectCMOVPred(SDValue N, SDValue &Pred, SDValue &Reg) {
130 const ConstantSDNode *CN = cast<ConstantSDNode>(N);
131 Pred = CurDAG->getTargetConstant(CN->getZExtValue(), SDLoc(N), MVT::i32);
132 Reg = CurDAG->getRegister(ARM::CPSR, MVT::i32);
136 bool SelectAddrMode2OffsetReg(SDNode *Op, SDValue N,
137 SDValue &Offset, SDValue &Opc);
138 bool SelectAddrMode2OffsetImm(SDNode *Op, SDValue N,
139 SDValue &Offset, SDValue &Opc);
140 bool SelectAddrMode2OffsetImmPre(SDNode *Op, SDValue N,
141 SDValue &Offset, SDValue &Opc);
142 bool SelectAddrOffsetNone(SDValue N, SDValue &Base);
143 bool SelectAddrMode3(SDValue N, SDValue &Base,
144 SDValue &Offset, SDValue &Opc);
145 bool SelectAddrMode3Offset(SDNode *Op, SDValue N,
146 SDValue &Offset, SDValue &Opc);
147 bool SelectAddrMode5(SDValue N, SDValue &Base,
149 bool SelectAddrMode6(SDNode *Parent, SDValue N, SDValue &Addr,SDValue &Align);
150 bool SelectAddrMode6Offset(SDNode *Op, SDValue N, SDValue &Offset);
152 bool SelectAddrModePC(SDValue N, SDValue &Offset, SDValue &Label);
154 // Thumb Addressing Modes:
155 bool SelectThumbAddrModeRR(SDValue N, SDValue &Base, SDValue &Offset);
156 bool SelectThumbAddrModeImm5S(SDValue N, unsigned Scale, SDValue &Base,
158 bool SelectThumbAddrModeImm5S1(SDValue N, SDValue &Base,
160 bool SelectThumbAddrModeImm5S2(SDValue N, SDValue &Base,
162 bool SelectThumbAddrModeImm5S4(SDValue N, SDValue &Base,
164 bool SelectThumbAddrModeSP(SDValue N, SDValue &Base, SDValue &OffImm);
166 // Thumb 2 Addressing Modes:
167 bool SelectT2AddrModeImm12(SDValue N, SDValue &Base, SDValue &OffImm);
168 bool SelectT2AddrModeImm8(SDValue N, SDValue &Base,
170 bool SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
172 bool SelectT2AddrModeSoReg(SDValue N, SDValue &Base,
173 SDValue &OffReg, SDValue &ShImm);
174 bool SelectT2AddrModeExclusive(SDValue N, SDValue &Base, SDValue &OffImm);
176 inline bool is_so_imm(unsigned Imm) const {
177 return ARM_AM::getSOImmVal(Imm) != -1;
180 inline bool is_so_imm_not(unsigned Imm) const {
181 return ARM_AM::getSOImmVal(~Imm) != -1;
184 inline bool is_t2_so_imm(unsigned Imm) const {
185 return ARM_AM::getT2SOImmVal(Imm) != -1;
188 inline bool is_t2_so_imm_not(unsigned Imm) const {
189 return ARM_AM::getT2SOImmVal(~Imm) != -1;
192 // Include the pieces autogenerated from the target description.
193 #include "ARMGenDAGISel.inc"
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 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 *Opcodes);
230 /// SelectVTBL - Select NEON VTBL and VTBX intrinsics. NumVecs should be 2,
231 /// 3 or 4. These are custom-selected so that a REG_SEQUENCE can be
232 /// generated to force the table registers to be consecutive.
233 void SelectVTBL(SDNode *N, bool IsExt, unsigned NumVecs, unsigned Opc);
235 /// Try to select SBFX/UBFX instructions for ARM.
236 bool tryV6T2BitfieldExtractOp(SDNode *N, bool isSigned);
238 // Select special operations if node forms integer ABS pattern
239 bool tryABSOp(SDNode *N);
241 bool tryReadRegister(SDNode *N);
242 bool tryWriteRegister(SDNode *N);
244 bool tryInlineAsm(SDNode *N);
246 void SelectConcatVector(SDNode *N);
248 bool trySMLAWSMULW(SDNode *N);
250 void SelectCMP_SWAP(SDNode *N);
252 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
253 /// inline asm expressions.
254 bool SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
255 std::vector<SDValue> &OutOps) override;
257 // Form pairs of consecutive R, S, D, or Q registers.
258 SDNode *createGPRPairNode(EVT VT, SDValue V0, SDValue V1);
259 SDNode *createSRegPairNode(EVT VT, SDValue V0, SDValue V1);
260 SDNode *createDRegPairNode(EVT VT, SDValue V0, SDValue V1);
261 SDNode *createQRegPairNode(EVT VT, SDValue V0, SDValue V1);
263 // Form sequences of 4 consecutive S, D, or Q registers.
264 SDNode *createQuadSRegsNode(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
265 SDNode *createQuadDRegsNode(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
266 SDNode *createQuadQRegsNode(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
268 // Get the alignment operand for a NEON VLD or VST instruction.
269 SDValue GetVLDSTAlign(SDValue Align, const SDLoc &dl, unsigned NumVecs,
272 /// Returns the number of instructions required to materialize the given
273 /// constant in a register, or 3 if a literal pool load is needed.
274 unsigned ConstantMaterializationCost(unsigned Val) const;
276 /// Checks if N is a multiplication by a constant where we can extract out a
277 /// power of two from the constant so that it can be used in a shift, but only
278 /// if it simplifies the materialization of the constant. Returns true if it
279 /// is, and assigns to PowerOfTwo the power of two that should be extracted
280 /// out and to NewMulConst the new constant to be multiplied by.
281 bool canExtractShiftFromMul(const SDValue &N, unsigned MaxShift,
282 unsigned &PowerOfTwo, SDValue &NewMulConst) const;
284 /// Replace N with M in CurDAG, in a way that also ensures that M gets
285 /// selected when N would have been selected.
286 void replaceDAGValue(const SDValue &N, SDValue M);
290 /// isInt32Immediate - This method tests to see if the node is a 32-bit constant
291 /// operand. If so Imm will receive the 32-bit value.
292 static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
293 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
294 Imm = cast<ConstantSDNode>(N)->getZExtValue();
300 // isInt32Immediate - This method tests to see if a constant operand.
301 // If so Imm will receive the 32 bit value.
302 static bool isInt32Immediate(SDValue N, unsigned &Imm) {
303 return isInt32Immediate(N.getNode(), Imm);
306 // isOpcWithIntImmediate - This method tests to see if the node is a specific
307 // opcode and that it has a immediate integer right operand.
308 // If so Imm will receive the 32 bit value.
309 static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
310 return N->getOpcode() == Opc &&
311 isInt32Immediate(N->getOperand(1).getNode(), Imm);
314 /// \brief Check whether a particular node is a constant value representable as
315 /// (N * Scale) where (N in [\p RangeMin, \p RangeMax).
317 /// \param ScaledConstant [out] - On success, the pre-scaled constant value.
318 static bool isScaledConstantInRange(SDValue Node, int Scale,
319 int RangeMin, int RangeMax,
320 int &ScaledConstant) {
321 assert(Scale > 0 && "Invalid scale!");
323 // Check that this is a constant.
324 const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Node);
328 ScaledConstant = (int) C->getZExtValue();
329 if ((ScaledConstant % Scale) != 0)
332 ScaledConstant /= Scale;
333 return ScaledConstant >= RangeMin && ScaledConstant < RangeMax;
336 void ARMDAGToDAGISel::PreprocessISelDAG() {
337 if (!Subtarget->hasV6T2Ops())
340 bool isThumb2 = Subtarget->isThumb();
341 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
342 E = CurDAG->allnodes_end(); I != E; ) {
343 SDNode *N = &*I++; // Preincrement iterator to avoid invalidation issues.
345 if (N->getOpcode() != ISD::ADD)
348 // Look for (add X1, (and (srl X2, c1), c2)) where c2 is constant with
349 // leading zeros, followed by consecutive set bits, followed by 1 or 2
350 // trailing zeros, e.g. 1020.
351 // Transform the expression to
352 // (add X1, (shl (and (srl X2, c1), (c2>>tz)), tz)) where tz is the number
353 // of trailing zeros of c2. The left shift would be folded as an shifter
354 // operand of 'add' and the 'and' and 'srl' would become a bits extraction
357 SDValue N0 = N->getOperand(0);
358 SDValue N1 = N->getOperand(1);
359 unsigned And_imm = 0;
360 if (!isOpcWithIntImmediate(N1.getNode(), ISD::AND, And_imm)) {
361 if (isOpcWithIntImmediate(N0.getNode(), ISD::AND, And_imm))
367 // Check if the AND mask is an immediate of the form: 000.....1111111100
368 unsigned TZ = countTrailingZeros(And_imm);
369 if (TZ != 1 && TZ != 2)
370 // Be conservative here. Shifter operands aren't always free. e.g. On
371 // Swift, left shifter operand of 1 / 2 for free but others are not.
373 // ubfx r3, r1, #16, #8
374 // ldr.w r3, [r0, r3, lsl #2]
377 // and.w r2, r9, r1, lsr #14
381 if (And_imm & (And_imm + 1))
384 // Look for (and (srl X, c1), c2).
385 SDValue Srl = N1.getOperand(0);
386 unsigned Srl_imm = 0;
387 if (!isOpcWithIntImmediate(Srl.getNode(), ISD::SRL, Srl_imm) ||
391 // Make sure first operand is not a shifter operand which would prevent
392 // folding of the left shift.
397 if (SelectImmShifterOperand(N0, CPTmp0, CPTmp1))
400 if (SelectImmShifterOperand(N0, CPTmp0, CPTmp1) ||
401 SelectRegShifterOperand(N0, CPTmp0, CPTmp1, CPTmp2))
405 // Now make the transformation.
406 Srl = CurDAG->getNode(ISD::SRL, SDLoc(Srl), MVT::i32,
408 CurDAG->getConstant(Srl_imm + TZ, SDLoc(Srl),
410 N1 = CurDAG->getNode(ISD::AND, SDLoc(N1), MVT::i32,
412 CurDAG->getConstant(And_imm, SDLoc(Srl), MVT::i32));
413 N1 = CurDAG->getNode(ISD::SHL, SDLoc(N1), MVT::i32,
414 N1, CurDAG->getConstant(TZ, SDLoc(Srl), MVT::i32));
415 CurDAG->UpdateNodeOperands(N, N0, N1);
419 /// hasNoVMLxHazardUse - Return true if it's desirable to select a FP MLA / MLS
420 /// node. VFP / NEON fp VMLA / VMLS instructions have special RAW hazards (at
421 /// least on current ARM implementations) which should be avoidded.
422 bool ARMDAGToDAGISel::hasNoVMLxHazardUse(SDNode *N) const {
423 if (OptLevel == CodeGenOpt::None)
426 if (!Subtarget->hasVMLxHazards())
432 SDNode *Use = *N->use_begin();
433 if (Use->getOpcode() == ISD::CopyToReg)
435 if (Use->isMachineOpcode()) {
436 const ARMBaseInstrInfo *TII = static_cast<const ARMBaseInstrInfo *>(
437 CurDAG->getSubtarget().getInstrInfo());
439 const MCInstrDesc &MCID = TII->get(Use->getMachineOpcode());
442 unsigned Opcode = MCID.getOpcode();
443 if (Opcode == ARM::VMOVRS || Opcode == ARM::VMOVRRD)
445 // vmlx feeding into another vmlx. We actually want to unfold
446 // the use later in the MLxExpansion pass. e.g.
448 // vmla (stall 8 cycles)
453 // This adds up to about 18 - 19 cycles.
456 // vmul (stall 4 cycles)
457 // vadd adds up to about 14 cycles.
458 return TII->isFpMLxInstruction(Opcode);
464 bool ARMDAGToDAGISel::isShifterOpProfitable(const SDValue &Shift,
465 ARM_AM::ShiftOpc ShOpcVal,
467 if (!Subtarget->isLikeA9() && !Subtarget->isSwift())
469 if (Shift.hasOneUse())
472 return ShOpcVal == ARM_AM::lsl &&
473 (ShAmt == 2 || (Subtarget->isSwift() && ShAmt == 1));
476 unsigned ARMDAGToDAGISel::ConstantMaterializationCost(unsigned Val) const {
477 if (Subtarget->isThumb()) {
478 if (Val <= 255) return 1; // MOV
479 if (Subtarget->hasV6T2Ops() && Val <= 0xffff) return 1; // MOVW
480 if (Val <= 510) return 2; // MOV + ADDi8
481 if (~Val <= 255) return 2; // MOV + MVN
482 if (ARM_AM::isThumbImmShiftedVal(Val)) return 2; // MOV + LSL
484 if (ARM_AM::getSOImmVal(Val) != -1) return 1; // MOV
485 if (ARM_AM::getSOImmVal(~Val) != -1) return 1; // MVN
486 if (Subtarget->hasV6T2Ops() && Val <= 0xffff) return 1; // MOVW
487 if (ARM_AM::isSOImmTwoPartVal(Val)) return 2; // two instrs
489 if (Subtarget->useMovt(*MF)) return 2; // MOVW + MOVT
490 return 3; // Literal pool load
493 bool ARMDAGToDAGISel::canExtractShiftFromMul(const SDValue &N,
495 unsigned &PowerOfTwo,
496 SDValue &NewMulConst) const {
497 assert(N.getOpcode() == ISD::MUL);
498 assert(MaxShift > 0);
500 // If the multiply is used in more than one place then changing the constant
501 // will make other uses incorrect, so don't.
502 if (!N.hasOneUse()) return false;
503 // Check if the multiply is by a constant
504 ConstantSDNode *MulConst = dyn_cast<ConstantSDNode>(N.getOperand(1));
505 if (!MulConst) return false;
506 // If the constant is used in more than one place then modifying it will mean
507 // we need to materialize two constants instead of one, which is a bad idea.
508 if (!MulConst->hasOneUse()) return false;
509 unsigned MulConstVal = MulConst->getZExtValue();
510 if (MulConstVal == 0) return false;
512 // Find the largest power of 2 that MulConstVal is a multiple of
513 PowerOfTwo = MaxShift;
514 while ((MulConstVal % (1 << PowerOfTwo)) != 0) {
516 if (PowerOfTwo == 0) return false;
519 // Only optimise if the new cost is better
520 unsigned NewMulConstVal = MulConstVal / (1 << PowerOfTwo);
521 NewMulConst = CurDAG->getConstant(NewMulConstVal, SDLoc(N), MVT::i32);
522 unsigned OldCost = ConstantMaterializationCost(MulConstVal);
523 unsigned NewCost = ConstantMaterializationCost(NewMulConstVal);
524 return NewCost < OldCost;
527 void ARMDAGToDAGISel::replaceDAGValue(const SDValue &N, SDValue M) {
528 CurDAG->RepositionNode(N.getNode()->getIterator(), M.getNode());
529 CurDAG->ReplaceAllUsesWith(N, M);
532 bool ARMDAGToDAGISel::SelectImmShifterOperand(SDValue N,
535 bool CheckProfitability) {
536 if (DisableShifterOp)
539 // If N is a multiply-by-constant and it's profitable to extract a shift and
540 // use it in a shifted operand do so.
541 if (N.getOpcode() == ISD::MUL) {
542 unsigned PowerOfTwo = 0;
544 if (canExtractShiftFromMul(N, 31, PowerOfTwo, NewMulConst)) {
545 HandleSDNode Handle(N);
546 replaceDAGValue(N.getOperand(1), NewMulConst);
547 BaseReg = Handle.getValue();
548 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ARM_AM::lsl,
555 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOpcode());
557 // Don't match base register only case. That is matched to a separate
558 // lower complexity pattern with explicit register operand.
559 if (ShOpcVal == ARM_AM::no_shift) return false;
561 BaseReg = N.getOperand(0);
562 unsigned ShImmVal = 0;
563 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1));
564 if (!RHS) return false;
565 ShImmVal = RHS->getZExtValue() & 31;
566 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
571 bool ARMDAGToDAGISel::SelectRegShifterOperand(SDValue N,
575 bool CheckProfitability) {
576 if (DisableShifterOp)
579 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOpcode());
581 // Don't match base register only case. That is matched to a separate
582 // lower complexity pattern with explicit register operand.
583 if (ShOpcVal == ARM_AM::no_shift) return false;
585 BaseReg = N.getOperand(0);
586 unsigned ShImmVal = 0;
587 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1));
588 if (RHS) return false;
590 ShReg = N.getOperand(1);
591 if (CheckProfitability && !isShifterOpProfitable(N, ShOpcVal, ShImmVal))
593 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
599 bool ARMDAGToDAGISel::SelectAddrModeImm12(SDValue N,
602 // Match simple R + imm12 operands.
605 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
606 !CurDAG->isBaseWithConstantOffset(N)) {
607 if (N.getOpcode() == ISD::FrameIndex) {
608 // Match frame index.
609 int FI = cast<FrameIndexSDNode>(N)->getIndex();
610 Base = CurDAG->getTargetFrameIndex(
611 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
612 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
616 if (N.getOpcode() == ARMISD::Wrapper &&
617 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress &&
618 N.getOperand(0).getOpcode() != ISD::TargetExternalSymbol &&
619 N.getOperand(0).getOpcode() != ISD::TargetGlobalTLSAddress) {
620 Base = N.getOperand(0);
623 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
627 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
628 int RHSC = (int)RHS->getSExtValue();
629 if (N.getOpcode() == ISD::SUB)
632 if (RHSC > -0x1000 && RHSC < 0x1000) { // 12 bits
633 Base = N.getOperand(0);
634 if (Base.getOpcode() == ISD::FrameIndex) {
635 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
636 Base = CurDAG->getTargetFrameIndex(
637 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
639 OffImm = CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i32);
646 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
652 bool ARMDAGToDAGISel::SelectLdStSOReg(SDValue N, SDValue &Base, SDValue &Offset,
654 if (N.getOpcode() == ISD::MUL &&
655 ((!Subtarget->isLikeA9() && !Subtarget->isSwift()) || N.hasOneUse())) {
656 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
657 // X * [3,5,9] -> X + X * [2,4,8] etc.
658 int RHSC = (int)RHS->getZExtValue();
661 ARM_AM::AddrOpc AddSub = ARM_AM::add;
663 AddSub = ARM_AM::sub;
666 if (isPowerOf2_32(RHSC)) {
667 unsigned ShAmt = Log2_32(RHSC);
668 Base = Offset = N.getOperand(0);
669 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt,
678 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
679 // ISD::OR that is equivalent to an ISD::ADD.
680 !CurDAG->isBaseWithConstantOffset(N))
683 // Leave simple R +/- imm12 operands for LDRi12
684 if (N.getOpcode() == ISD::ADD || N.getOpcode() == ISD::OR) {
686 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/1,
687 -0x1000+1, 0x1000, RHSC)) // 12 bits.
691 // Otherwise this is R +/- [possibly shifted] R.
692 ARM_AM::AddrOpc AddSub = N.getOpcode() == ISD::SUB ? ARM_AM::sub:ARM_AM::add;
693 ARM_AM::ShiftOpc ShOpcVal =
694 ARM_AM::getShiftOpcForNode(N.getOperand(1).getOpcode());
697 Base = N.getOperand(0);
698 Offset = N.getOperand(1);
700 if (ShOpcVal != ARM_AM::no_shift) {
701 // Check to see if the RHS of the shift is a constant, if not, we can't fold
703 if (ConstantSDNode *Sh =
704 dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) {
705 ShAmt = Sh->getZExtValue();
706 if (isShifterOpProfitable(Offset, ShOpcVal, ShAmt))
707 Offset = N.getOperand(1).getOperand(0);
710 ShOpcVal = ARM_AM::no_shift;
713 ShOpcVal = ARM_AM::no_shift;
717 // Try matching (R shl C) + (R).
718 if (N.getOpcode() != ISD::SUB && ShOpcVal == ARM_AM::no_shift &&
719 !(Subtarget->isLikeA9() || Subtarget->isSwift() ||
720 N.getOperand(0).hasOneUse())) {
721 ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0).getOpcode());
722 if (ShOpcVal != ARM_AM::no_shift) {
723 // Check to see if the RHS of the shift is a constant, if not, we can't
725 if (ConstantSDNode *Sh =
726 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
727 ShAmt = Sh->getZExtValue();
728 if (isShifterOpProfitable(N.getOperand(0), ShOpcVal, ShAmt)) {
729 Offset = N.getOperand(0).getOperand(0);
730 Base = N.getOperand(1);
733 ShOpcVal = ARM_AM::no_shift;
736 ShOpcVal = ARM_AM::no_shift;
741 // If Offset is a multiply-by-constant and it's profitable to extract a shift
742 // and use it in a shifted operand do so.
743 if (Offset.getOpcode() == ISD::MUL && N.hasOneUse()) {
744 unsigned PowerOfTwo = 0;
746 if (canExtractShiftFromMul(Offset, 31, PowerOfTwo, NewMulConst)) {
747 replaceDAGValue(Offset.getOperand(1), NewMulConst);
749 ShOpcVal = ARM_AM::lsl;
753 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
761 AddrMode2Type ARMDAGToDAGISel::SelectAddrMode2Worker(SDValue N,
765 if (N.getOpcode() == ISD::MUL &&
766 (!(Subtarget->isLikeA9() || Subtarget->isSwift()) || N.hasOneUse())) {
767 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
768 // X * [3,5,9] -> X + X * [2,4,8] etc.
769 int RHSC = (int)RHS->getZExtValue();
772 ARM_AM::AddrOpc AddSub = ARM_AM::add;
774 AddSub = ARM_AM::sub;
777 if (isPowerOf2_32(RHSC)) {
778 unsigned ShAmt = Log2_32(RHSC);
779 Base = Offset = N.getOperand(0);
780 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt,
789 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
790 // ISD::OR that is equivalent to an ADD.
791 !CurDAG->isBaseWithConstantOffset(N)) {
793 if (N.getOpcode() == ISD::FrameIndex) {
794 int FI = cast<FrameIndexSDNode>(N)->getIndex();
795 Base = CurDAG->getTargetFrameIndex(
796 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
797 } else if (N.getOpcode() == ARMISD::Wrapper &&
798 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress &&
799 N.getOperand(0).getOpcode() != ISD::TargetExternalSymbol &&
800 N.getOperand(0).getOpcode() != ISD::TargetGlobalTLSAddress) {
801 Base = N.getOperand(0);
803 Offset = CurDAG->getRegister(0, MVT::i32);
804 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::add, 0,
810 // Match simple R +/- imm12 operands.
811 if (N.getOpcode() != ISD::SUB) {
813 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/1,
814 -0x1000+1, 0x1000, RHSC)) { // 12 bits.
815 Base = N.getOperand(0);
816 if (Base.getOpcode() == ISD::FrameIndex) {
817 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
818 Base = CurDAG->getTargetFrameIndex(
819 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
821 Offset = CurDAG->getRegister(0, MVT::i32);
823 ARM_AM::AddrOpc AddSub = ARM_AM::add;
825 AddSub = ARM_AM::sub;
828 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, RHSC,
835 if ((Subtarget->isLikeA9() || Subtarget->isSwift()) && !N.hasOneUse()) {
836 // Compute R +/- (R << N) and reuse it.
838 Offset = CurDAG->getRegister(0, MVT::i32);
839 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::add, 0,
845 // Otherwise this is R +/- [possibly shifted] R.
846 ARM_AM::AddrOpc AddSub = N.getOpcode() != ISD::SUB ? ARM_AM::add:ARM_AM::sub;
847 ARM_AM::ShiftOpc ShOpcVal =
848 ARM_AM::getShiftOpcForNode(N.getOperand(1).getOpcode());
851 Base = N.getOperand(0);
852 Offset = N.getOperand(1);
854 if (ShOpcVal != ARM_AM::no_shift) {
855 // Check to see if the RHS of the shift is a constant, if not, we can't fold
857 if (ConstantSDNode *Sh =
858 dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) {
859 ShAmt = Sh->getZExtValue();
860 if (isShifterOpProfitable(Offset, ShOpcVal, ShAmt))
861 Offset = N.getOperand(1).getOperand(0);
864 ShOpcVal = ARM_AM::no_shift;
867 ShOpcVal = ARM_AM::no_shift;
871 // Try matching (R shl C) + (R).
872 if (N.getOpcode() != ISD::SUB && ShOpcVal == ARM_AM::no_shift &&
873 !(Subtarget->isLikeA9() || Subtarget->isSwift() ||
874 N.getOperand(0).hasOneUse())) {
875 ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0).getOpcode());
876 if (ShOpcVal != ARM_AM::no_shift) {
877 // Check to see if the RHS of the shift is a constant, if not, we can't
879 if (ConstantSDNode *Sh =
880 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
881 ShAmt = Sh->getZExtValue();
882 if (isShifterOpProfitable(N.getOperand(0), ShOpcVal, ShAmt)) {
883 Offset = N.getOperand(0).getOperand(0);
884 Base = N.getOperand(1);
887 ShOpcVal = ARM_AM::no_shift;
890 ShOpcVal = ARM_AM::no_shift;
895 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
900 bool ARMDAGToDAGISel::SelectAddrMode2OffsetReg(SDNode *Op, SDValue N,
901 SDValue &Offset, SDValue &Opc) {
902 unsigned Opcode = Op->getOpcode();
903 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
904 ? cast<LoadSDNode>(Op)->getAddressingMode()
905 : cast<StoreSDNode>(Op)->getAddressingMode();
906 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
907 ? ARM_AM::add : ARM_AM::sub;
909 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x1000, Val))
913 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOpcode());
915 if (ShOpcVal != ARM_AM::no_shift) {
916 // Check to see if the RHS of the shift is a constant, if not, we can't fold
918 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
919 ShAmt = Sh->getZExtValue();
920 if (isShifterOpProfitable(N, ShOpcVal, ShAmt))
921 Offset = N.getOperand(0);
924 ShOpcVal = ARM_AM::no_shift;
927 ShOpcVal = ARM_AM::no_shift;
931 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
936 bool ARMDAGToDAGISel::SelectAddrMode2OffsetImmPre(SDNode *Op, SDValue N,
937 SDValue &Offset, SDValue &Opc) {
938 unsigned Opcode = Op->getOpcode();
939 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
940 ? cast<LoadSDNode>(Op)->getAddressingMode()
941 : cast<StoreSDNode>(Op)->getAddressingMode();
942 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
943 ? ARM_AM::add : ARM_AM::sub;
945 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x1000, Val)) { // 12 bits.
946 if (AddSub == ARM_AM::sub) Val *= -1;
947 Offset = CurDAG->getRegister(0, MVT::i32);
948 Opc = CurDAG->getTargetConstant(Val, SDLoc(Op), MVT::i32);
956 bool ARMDAGToDAGISel::SelectAddrMode2OffsetImm(SDNode *Op, SDValue N,
957 SDValue &Offset, SDValue &Opc) {
958 unsigned Opcode = Op->getOpcode();
959 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
960 ? cast<LoadSDNode>(Op)->getAddressingMode()
961 : cast<StoreSDNode>(Op)->getAddressingMode();
962 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
963 ? ARM_AM::add : ARM_AM::sub;
965 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x1000, Val)) { // 12 bits.
966 Offset = CurDAG->getRegister(0, MVT::i32);
967 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, Val,
969 SDLoc(Op), MVT::i32);
976 bool ARMDAGToDAGISel::SelectAddrOffsetNone(SDValue N, SDValue &Base) {
981 bool ARMDAGToDAGISel::SelectAddrMode3(SDValue N,
982 SDValue &Base, SDValue &Offset,
984 if (N.getOpcode() == ISD::SUB) {
985 // X - C is canonicalize to X + -C, no need to handle it here.
986 Base = N.getOperand(0);
987 Offset = N.getOperand(1);
988 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::sub, 0), SDLoc(N),
993 if (!CurDAG->isBaseWithConstantOffset(N)) {
995 if (N.getOpcode() == ISD::FrameIndex) {
996 int FI = cast<FrameIndexSDNode>(N)->getIndex();
997 Base = CurDAG->getTargetFrameIndex(
998 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1000 Offset = CurDAG->getRegister(0, MVT::i32);
1001 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0), SDLoc(N),
1006 // If the RHS is +/- imm8, fold into addr mode.
1008 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/1,
1009 -256 + 1, 256, RHSC)) { // 8 bits.
1010 Base = N.getOperand(0);
1011 if (Base.getOpcode() == ISD::FrameIndex) {
1012 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
1013 Base = CurDAG->getTargetFrameIndex(
1014 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1016 Offset = CurDAG->getRegister(0, MVT::i32);
1018 ARM_AM::AddrOpc AddSub = ARM_AM::add;
1020 AddSub = ARM_AM::sub;
1023 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, RHSC), SDLoc(N),
1028 Base = N.getOperand(0);
1029 Offset = N.getOperand(1);
1030 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0), SDLoc(N),
1035 bool ARMDAGToDAGISel::SelectAddrMode3Offset(SDNode *Op, SDValue N,
1036 SDValue &Offset, SDValue &Opc) {
1037 unsigned Opcode = Op->getOpcode();
1038 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
1039 ? cast<LoadSDNode>(Op)->getAddressingMode()
1040 : cast<StoreSDNode>(Op)->getAddressingMode();
1041 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
1042 ? ARM_AM::add : ARM_AM::sub;
1044 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 256, Val)) { // 12 bits.
1045 Offset = CurDAG->getRegister(0, MVT::i32);
1046 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, Val), SDLoc(Op),
1052 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, 0), SDLoc(Op),
1057 bool ARMDAGToDAGISel::SelectAddrMode5(SDValue N,
1058 SDValue &Base, SDValue &Offset) {
1059 if (!CurDAG->isBaseWithConstantOffset(N)) {
1061 if (N.getOpcode() == ISD::FrameIndex) {
1062 int FI = cast<FrameIndexSDNode>(N)->getIndex();
1063 Base = CurDAG->getTargetFrameIndex(
1064 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1065 } else if (N.getOpcode() == ARMISD::Wrapper &&
1066 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress &&
1067 N.getOperand(0).getOpcode() != ISD::TargetExternalSymbol &&
1068 N.getOperand(0).getOpcode() != ISD::TargetGlobalTLSAddress) {
1069 Base = N.getOperand(0);
1071 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
1072 SDLoc(N), MVT::i32);
1076 // If the RHS is +/- imm8, fold into addr mode.
1078 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/4,
1079 -256 + 1, 256, RHSC)) {
1080 Base = N.getOperand(0);
1081 if (Base.getOpcode() == ISD::FrameIndex) {
1082 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
1083 Base = CurDAG->getTargetFrameIndex(
1084 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1087 ARM_AM::AddrOpc AddSub = ARM_AM::add;
1089 AddSub = ARM_AM::sub;
1092 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(AddSub, RHSC),
1093 SDLoc(N), MVT::i32);
1098 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
1099 SDLoc(N), MVT::i32);
1103 bool ARMDAGToDAGISel::SelectAddrMode6(SDNode *Parent, SDValue N, SDValue &Addr,
1107 unsigned Alignment = 0;
1109 MemSDNode *MemN = cast<MemSDNode>(Parent);
1111 if (isa<LSBaseSDNode>(MemN) ||
1112 ((MemN->getOpcode() == ARMISD::VST1_UPD ||
1113 MemN->getOpcode() == ARMISD::VLD1_UPD) &&
1114 MemN->getConstantOperandVal(MemN->getNumOperands() - 1) == 1)) {
1115 // This case occurs only for VLD1-lane/dup and VST1-lane instructions.
1116 // The maximum alignment is equal to the memory size being referenced.
1117 unsigned MMOAlign = MemN->getAlignment();
1118 unsigned MemSize = MemN->getMemoryVT().getSizeInBits() / 8;
1119 if (MMOAlign >= MemSize && MemSize > 1)
1120 Alignment = MemSize;
1122 // All other uses of addrmode6 are for intrinsics. For now just record
1123 // the raw alignment value; it will be refined later based on the legal
1124 // alignment operands for the intrinsic.
1125 Alignment = MemN->getAlignment();
1128 Align = CurDAG->getTargetConstant(Alignment, SDLoc(N), MVT::i32);
1132 bool ARMDAGToDAGISel::SelectAddrMode6Offset(SDNode *Op, SDValue N,
1134 LSBaseSDNode *LdSt = cast<LSBaseSDNode>(Op);
1135 ISD::MemIndexedMode AM = LdSt->getAddressingMode();
1136 if (AM != ISD::POST_INC)
1139 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(N)) {
1140 if (NC->getZExtValue() * 8 == LdSt->getMemoryVT().getSizeInBits())
1141 Offset = CurDAG->getRegister(0, MVT::i32);
1146 bool ARMDAGToDAGISel::SelectAddrModePC(SDValue N,
1147 SDValue &Offset, SDValue &Label) {
1148 if (N.getOpcode() == ARMISD::PIC_ADD && N.hasOneUse()) {
1149 Offset = N.getOperand(0);
1150 SDValue N1 = N.getOperand(1);
1151 Label = CurDAG->getTargetConstant(cast<ConstantSDNode>(N1)->getZExtValue(),
1152 SDLoc(N), MVT::i32);
1160 //===----------------------------------------------------------------------===//
1161 // Thumb Addressing Modes
1162 //===----------------------------------------------------------------------===//
1164 bool ARMDAGToDAGISel::SelectThumbAddrModeRR(SDValue N,
1165 SDValue &Base, SDValue &Offset){
1166 if (N.getOpcode() != ISD::ADD && !CurDAG->isBaseWithConstantOffset(N)) {
1167 ConstantSDNode *NC = dyn_cast<ConstantSDNode>(N);
1168 if (!NC || !NC->isNullValue())
1175 Base = N.getOperand(0);
1176 Offset = N.getOperand(1);
1181 ARMDAGToDAGISel::SelectThumbAddrModeImm5S(SDValue N, unsigned Scale,
1182 SDValue &Base, SDValue &OffImm) {
1183 if (!CurDAG->isBaseWithConstantOffset(N)) {
1184 if (N.getOpcode() == ISD::ADD) {
1185 return false; // We want to select register offset instead
1186 } else if (N.getOpcode() == ARMISD::Wrapper &&
1187 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress &&
1188 N.getOperand(0).getOpcode() != ISD::TargetExternalSymbol &&
1189 N.getOperand(0).getOpcode() != ISD::TargetGlobalTLSAddress) {
1190 Base = N.getOperand(0);
1195 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
1199 // If the RHS is + imm5 * scale, fold into addr mode.
1201 if (isScaledConstantInRange(N.getOperand(1), Scale, 0, 32, RHSC)) {
1202 Base = N.getOperand(0);
1203 OffImm = CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i32);
1207 // Offset is too large, so use register offset instead.
1212 ARMDAGToDAGISel::SelectThumbAddrModeImm5S4(SDValue N, SDValue &Base,
1214 return SelectThumbAddrModeImm5S(N, 4, Base, OffImm);
1218 ARMDAGToDAGISel::SelectThumbAddrModeImm5S2(SDValue N, SDValue &Base,
1220 return SelectThumbAddrModeImm5S(N, 2, Base, OffImm);
1224 ARMDAGToDAGISel::SelectThumbAddrModeImm5S1(SDValue N, SDValue &Base,
1226 return SelectThumbAddrModeImm5S(N, 1, Base, OffImm);
1229 bool ARMDAGToDAGISel::SelectThumbAddrModeSP(SDValue N,
1230 SDValue &Base, SDValue &OffImm) {
1231 if (N.getOpcode() == ISD::FrameIndex) {
1232 int FI = cast<FrameIndexSDNode>(N)->getIndex();
1233 // Only multiples of 4 are allowed for the offset, so the frame object
1234 // alignment must be at least 4.
1235 MachineFrameInfo *MFI = MF->getFrameInfo();
1236 if (MFI->getObjectAlignment(FI) < 4)
1237 MFI->setObjectAlignment(FI, 4);
1238 Base = CurDAG->getTargetFrameIndex(
1239 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1240 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
1244 if (!CurDAG->isBaseWithConstantOffset(N))
1247 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
1248 if (N.getOperand(0).getOpcode() == ISD::FrameIndex ||
1249 (LHSR && LHSR->getReg() == ARM::SP)) {
1250 // If the RHS is + imm8 * scale, fold into addr mode.
1252 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/4, 0, 256, RHSC)) {
1253 Base = N.getOperand(0);
1254 if (Base.getOpcode() == ISD::FrameIndex) {
1255 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
1256 // For LHS+RHS to result in an offset that's a multiple of 4 the object
1257 // indexed by the LHS must be 4-byte aligned.
1258 MachineFrameInfo *MFI = MF->getFrameInfo();
1259 if (MFI->getObjectAlignment(FI) < 4)
1260 MFI->setObjectAlignment(FI, 4);
1261 Base = CurDAG->getTargetFrameIndex(
1262 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1264 OffImm = CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i32);
1273 //===----------------------------------------------------------------------===//
1274 // Thumb 2 Addressing Modes
1275 //===----------------------------------------------------------------------===//
1278 bool ARMDAGToDAGISel::SelectT2AddrModeImm12(SDValue N,
1279 SDValue &Base, SDValue &OffImm) {
1280 // Match simple R + imm12 operands.
1283 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
1284 !CurDAG->isBaseWithConstantOffset(N)) {
1285 if (N.getOpcode() == ISD::FrameIndex) {
1286 // Match frame index.
1287 int FI = cast<FrameIndexSDNode>(N)->getIndex();
1288 Base = CurDAG->getTargetFrameIndex(
1289 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1290 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
1294 if (N.getOpcode() == ARMISD::Wrapper &&
1295 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress &&
1296 N.getOperand(0).getOpcode() != ISD::TargetExternalSymbol &&
1297 N.getOperand(0).getOpcode() != ISD::TargetGlobalTLSAddress) {
1298 Base = N.getOperand(0);
1299 if (Base.getOpcode() == ISD::TargetConstantPool)
1300 return false; // We want to select t2LDRpci instead.
1303 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
1307 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1308 if (SelectT2AddrModeImm8(N, Base, OffImm))
1309 // Let t2LDRi8 handle (R - imm8).
1312 int RHSC = (int)RHS->getZExtValue();
1313 if (N.getOpcode() == ISD::SUB)
1316 if (RHSC >= 0 && RHSC < 0x1000) { // 12 bits (unsigned)
1317 Base = N.getOperand(0);
1318 if (Base.getOpcode() == ISD::FrameIndex) {
1319 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
1320 Base = CurDAG->getTargetFrameIndex(
1321 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1323 OffImm = CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i32);
1330 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
1334 bool ARMDAGToDAGISel::SelectT2AddrModeImm8(SDValue N,
1335 SDValue &Base, SDValue &OffImm) {
1336 // Match simple R - imm8 operands.
1337 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
1338 !CurDAG->isBaseWithConstantOffset(N))
1341 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1342 int RHSC = (int)RHS->getSExtValue();
1343 if (N.getOpcode() == ISD::SUB)
1346 if ((RHSC >= -255) && (RHSC < 0)) { // 8 bits (always negative)
1347 Base = N.getOperand(0);
1348 if (Base.getOpcode() == ISD::FrameIndex) {
1349 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
1350 Base = CurDAG->getTargetFrameIndex(
1351 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1353 OffImm = CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i32);
1361 bool ARMDAGToDAGISel::SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
1363 unsigned Opcode = Op->getOpcode();
1364 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
1365 ? cast<LoadSDNode>(Op)->getAddressingMode()
1366 : cast<StoreSDNode>(Op)->getAddressingMode();
1368 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x100, RHSC)) { // 8 bits.
1369 OffImm = ((AM == ISD::PRE_INC) || (AM == ISD::POST_INC))
1370 ? CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i32)
1371 : CurDAG->getTargetConstant(-RHSC, SDLoc(N), MVT::i32);
1378 bool ARMDAGToDAGISel::SelectT2AddrModeSoReg(SDValue N,
1380 SDValue &OffReg, SDValue &ShImm) {
1381 // (R - imm8) should be handled by t2LDRi8. The rest are handled by t2LDRi12.
1382 if (N.getOpcode() != ISD::ADD && !CurDAG->isBaseWithConstantOffset(N))
1385 // Leave (R + imm12) for t2LDRi12, (R - imm8) for t2LDRi8.
1386 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1387 int RHSC = (int)RHS->getZExtValue();
1388 if (RHSC >= 0 && RHSC < 0x1000) // 12 bits (unsigned)
1390 else if (RHSC < 0 && RHSC >= -255) // 8 bits
1394 // Look for (R + R) or (R + (R << [1,2,3])).
1396 Base = N.getOperand(0);
1397 OffReg = N.getOperand(1);
1399 // Swap if it is ((R << c) + R).
1400 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(OffReg.getOpcode());
1401 if (ShOpcVal != ARM_AM::lsl) {
1402 ShOpcVal = ARM_AM::getShiftOpcForNode(Base.getOpcode());
1403 if (ShOpcVal == ARM_AM::lsl)
1404 std::swap(Base, OffReg);
1407 if (ShOpcVal == ARM_AM::lsl) {
1408 // Check to see if the RHS of the shift is a constant, if not, we can't fold
1410 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(OffReg.getOperand(1))) {
1411 ShAmt = Sh->getZExtValue();
1412 if (ShAmt < 4 && isShifterOpProfitable(OffReg, ShOpcVal, ShAmt))
1413 OffReg = OffReg.getOperand(0);
1420 // If OffReg is a multiply-by-constant and it's profitable to extract a shift
1421 // and use it in a shifted operand do so.
1422 if (OffReg.getOpcode() == ISD::MUL && N.hasOneUse()) {
1423 unsigned PowerOfTwo = 0;
1424 SDValue NewMulConst;
1425 if (canExtractShiftFromMul(OffReg, 3, PowerOfTwo, NewMulConst)) {
1426 replaceDAGValue(OffReg.getOperand(1), NewMulConst);
1431 ShImm = CurDAG->getTargetConstant(ShAmt, SDLoc(N), MVT::i32);
1436 bool ARMDAGToDAGISel::SelectT2AddrModeExclusive(SDValue N, SDValue &Base,
1438 // This *must* succeed since it's used for the irreplaceable ldrex and strex
1441 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
1443 if (N.getOpcode() != ISD::ADD || !CurDAG->isBaseWithConstantOffset(N))
1446 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1));
1450 uint32_t RHSC = (int)RHS->getZExtValue();
1451 if (RHSC > 1020 || RHSC % 4 != 0)
1454 Base = N.getOperand(0);
1455 if (Base.getOpcode() == ISD::FrameIndex) {
1456 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
1457 Base = CurDAG->getTargetFrameIndex(
1458 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1461 OffImm = CurDAG->getTargetConstant(RHSC/4, SDLoc(N), MVT::i32);
1465 //===--------------------------------------------------------------------===//
1467 /// getAL - Returns a ARMCC::AL immediate node.
1468 static inline SDValue getAL(SelectionDAG *CurDAG, const SDLoc &dl) {
1469 return CurDAG->getTargetConstant((uint64_t)ARMCC::AL, dl, MVT::i32);
1472 bool ARMDAGToDAGISel::tryARMIndexedLoad(SDNode *N) {
1473 LoadSDNode *LD = cast<LoadSDNode>(N);
1474 ISD::MemIndexedMode AM = LD->getAddressingMode();
1475 if (AM == ISD::UNINDEXED)
1478 EVT LoadedVT = LD->getMemoryVT();
1479 SDValue Offset, AMOpc;
1480 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
1481 unsigned Opcode = 0;
1483 if (LoadedVT == MVT::i32 && isPre &&
1484 SelectAddrMode2OffsetImmPre(N, LD->getOffset(), Offset, AMOpc)) {
1485 Opcode = ARM::LDR_PRE_IMM;
1487 } else if (LoadedVT == MVT::i32 && !isPre &&
1488 SelectAddrMode2OffsetImm(N, LD->getOffset(), Offset, AMOpc)) {
1489 Opcode = ARM::LDR_POST_IMM;
1491 } else if (LoadedVT == MVT::i32 &&
1492 SelectAddrMode2OffsetReg(N, LD->getOffset(), Offset, AMOpc)) {
1493 Opcode = isPre ? ARM::LDR_PRE_REG : ARM::LDR_POST_REG;
1496 } else if (LoadedVT == MVT::i16 &&
1497 SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
1499 Opcode = (LD->getExtensionType() == ISD::SEXTLOAD)
1500 ? (isPre ? ARM::LDRSH_PRE : ARM::LDRSH_POST)
1501 : (isPre ? ARM::LDRH_PRE : ARM::LDRH_POST);
1502 } else if (LoadedVT == MVT::i8 || LoadedVT == MVT::i1) {
1503 if (LD->getExtensionType() == ISD::SEXTLOAD) {
1504 if (SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
1506 Opcode = isPre ? ARM::LDRSB_PRE : ARM::LDRSB_POST;
1510 SelectAddrMode2OffsetImmPre(N, LD->getOffset(), Offset, AMOpc)) {
1512 Opcode = ARM::LDRB_PRE_IMM;
1513 } else if (!isPre &&
1514 SelectAddrMode2OffsetImm(N, LD->getOffset(), Offset, AMOpc)) {
1516 Opcode = ARM::LDRB_POST_IMM;
1517 } else if (SelectAddrMode2OffsetReg(N, LD->getOffset(), Offset, AMOpc)) {
1519 Opcode = isPre ? ARM::LDRB_PRE_REG : ARM::LDRB_POST_REG;
1525 if (Opcode == ARM::LDR_PRE_IMM || Opcode == ARM::LDRB_PRE_IMM) {
1526 SDValue Chain = LD->getChain();
1527 SDValue Base = LD->getBasePtr();
1528 SDValue Ops[]= { Base, AMOpc, getAL(CurDAG, SDLoc(N)),
1529 CurDAG->getRegister(0, MVT::i32), Chain };
1530 ReplaceNode(N, CurDAG->getMachineNode(Opcode, SDLoc(N), MVT::i32,
1531 MVT::i32, MVT::Other, Ops));
1534 SDValue Chain = LD->getChain();
1535 SDValue Base = LD->getBasePtr();
1536 SDValue Ops[]= { Base, Offset, AMOpc, getAL(CurDAG, SDLoc(N)),
1537 CurDAG->getRegister(0, MVT::i32), Chain };
1538 ReplaceNode(N, CurDAG->getMachineNode(Opcode, SDLoc(N), MVT::i32,
1539 MVT::i32, MVT::Other, Ops));
1547 bool ARMDAGToDAGISel::tryT1IndexedLoad(SDNode *N) {
1548 LoadSDNode *LD = cast<LoadSDNode>(N);
1549 EVT LoadedVT = LD->getMemoryVT();
1550 ISD::MemIndexedMode AM = LD->getAddressingMode();
1551 if (AM == ISD::UNINDEXED || LD->getExtensionType() != ISD::NON_EXTLOAD ||
1552 AM != ISD::POST_INC || LoadedVT.getSimpleVT().SimpleTy != MVT::i32)
1555 auto *COffs = dyn_cast<ConstantSDNode>(LD->getOffset());
1556 if (!COffs || COffs->getZExtValue() != 4)
1559 // A T1 post-indexed load is just a single register LDM: LDM r0!, {r1}.
1560 // The encoding of LDM is not how the rest of ISel expects a post-inc load to
1561 // look however, so we use a pseudo here and switch it for a tLDMIA_UPD after
1563 SDValue Chain = LD->getChain();
1564 SDValue Base = LD->getBasePtr();
1565 SDValue Ops[]= { Base, getAL(CurDAG, SDLoc(N)),
1566 CurDAG->getRegister(0, MVT::i32), Chain };
1567 ReplaceNode(N, CurDAG->getMachineNode(ARM::tLDR_postidx, SDLoc(N), MVT::i32, MVT::i32,
1572 bool ARMDAGToDAGISel::tryT2IndexedLoad(SDNode *N) {
1573 LoadSDNode *LD = cast<LoadSDNode>(N);
1574 ISD::MemIndexedMode AM = LD->getAddressingMode();
1575 if (AM == ISD::UNINDEXED)
1578 EVT LoadedVT = LD->getMemoryVT();
1579 bool isSExtLd = LD->getExtensionType() == ISD::SEXTLOAD;
1581 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
1582 unsigned Opcode = 0;
1584 if (SelectT2AddrModeImm8Offset(N, LD->getOffset(), Offset)) {
1585 switch (LoadedVT.getSimpleVT().SimpleTy) {
1587 Opcode = isPre ? ARM::t2LDR_PRE : ARM::t2LDR_POST;
1591 Opcode = isPre ? ARM::t2LDRSH_PRE : ARM::t2LDRSH_POST;
1593 Opcode = isPre ? ARM::t2LDRH_PRE : ARM::t2LDRH_POST;
1598 Opcode = isPre ? ARM::t2LDRSB_PRE : ARM::t2LDRSB_POST;
1600 Opcode = isPre ? ARM::t2LDRB_PRE : ARM::t2LDRB_POST;
1609 SDValue Chain = LD->getChain();
1610 SDValue Base = LD->getBasePtr();
1611 SDValue Ops[]= { Base, Offset, getAL(CurDAG, SDLoc(N)),
1612 CurDAG->getRegister(0, MVT::i32), Chain };
1613 ReplaceNode(N, CurDAG->getMachineNode(Opcode, SDLoc(N), MVT::i32, MVT::i32,
1621 /// \brief Form a GPRPair pseudo register from a pair of GPR regs.
1622 SDNode *ARMDAGToDAGISel::createGPRPairNode(EVT VT, SDValue V0, SDValue V1) {
1623 SDLoc dl(V0.getNode());
1625 CurDAG->getTargetConstant(ARM::GPRPairRegClassID, dl, MVT::i32);
1626 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::gsub_0, dl, MVT::i32);
1627 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::gsub_1, dl, MVT::i32);
1628 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1 };
1629 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
1632 /// \brief Form a D register from a pair of S registers.
1633 SDNode *ARMDAGToDAGISel::createSRegPairNode(EVT VT, SDValue V0, SDValue V1) {
1634 SDLoc dl(V0.getNode());
1636 CurDAG->getTargetConstant(ARM::DPR_VFP2RegClassID, dl, MVT::i32);
1637 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, dl, MVT::i32);
1638 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, dl, MVT::i32);
1639 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1 };
1640 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
1643 /// \brief Form a quad register from a pair of D registers.
1644 SDNode *ARMDAGToDAGISel::createDRegPairNode(EVT VT, SDValue V0, SDValue V1) {
1645 SDLoc dl(V0.getNode());
1646 SDValue RegClass = CurDAG->getTargetConstant(ARM::QPRRegClassID, dl,
1648 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, dl, MVT::i32);
1649 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, dl, MVT::i32);
1650 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1 };
1651 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
1654 /// \brief Form 4 consecutive D registers from a pair of Q registers.
1655 SDNode *ARMDAGToDAGISel::createQRegPairNode(EVT VT, SDValue V0, SDValue V1) {
1656 SDLoc dl(V0.getNode());
1657 SDValue RegClass = CurDAG->getTargetConstant(ARM::QQPRRegClassID, dl,
1659 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, dl, MVT::i32);
1660 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, dl, MVT::i32);
1661 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1 };
1662 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
1665 /// \brief Form 4 consecutive S registers.
1666 SDNode *ARMDAGToDAGISel::createQuadSRegsNode(EVT VT, SDValue V0, SDValue V1,
1667 SDValue V2, SDValue V3) {
1668 SDLoc dl(V0.getNode());
1670 CurDAG->getTargetConstant(ARM::QPR_VFP2RegClassID, dl, MVT::i32);
1671 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, dl, MVT::i32);
1672 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, dl, MVT::i32);
1673 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::ssub_2, dl, MVT::i32);
1674 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::ssub_3, dl, MVT::i32);
1675 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1,
1676 V2, SubReg2, V3, SubReg3 };
1677 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
1680 /// \brief Form 4 consecutive D registers.
1681 SDNode *ARMDAGToDAGISel::createQuadDRegsNode(EVT VT, SDValue V0, SDValue V1,
1682 SDValue V2, SDValue V3) {
1683 SDLoc dl(V0.getNode());
1684 SDValue RegClass = CurDAG->getTargetConstant(ARM::QQPRRegClassID, dl,
1686 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, dl, MVT::i32);
1687 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, dl, MVT::i32);
1688 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::dsub_2, dl, MVT::i32);
1689 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::dsub_3, dl, MVT::i32);
1690 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1,
1691 V2, SubReg2, V3, SubReg3 };
1692 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
1695 /// \brief Form 4 consecutive Q registers.
1696 SDNode *ARMDAGToDAGISel::createQuadQRegsNode(EVT VT, SDValue V0, SDValue V1,
1697 SDValue V2, SDValue V3) {
1698 SDLoc dl(V0.getNode());
1699 SDValue RegClass = CurDAG->getTargetConstant(ARM::QQQQPRRegClassID, dl,
1701 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, dl, MVT::i32);
1702 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, dl, MVT::i32);
1703 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::qsub_2, dl, MVT::i32);
1704 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::qsub_3, dl, MVT::i32);
1705 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1,
1706 V2, SubReg2, V3, SubReg3 };
1707 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
1710 /// GetVLDSTAlign - Get the alignment (in bytes) for the alignment operand
1711 /// of a NEON VLD or VST instruction. The supported values depend on the
1712 /// number of registers being loaded.
1713 SDValue ARMDAGToDAGISel::GetVLDSTAlign(SDValue Align, const SDLoc &dl,
1714 unsigned NumVecs, bool is64BitVector) {
1715 unsigned NumRegs = NumVecs;
1716 if (!is64BitVector && NumVecs < 3)
1719 unsigned Alignment = cast<ConstantSDNode>(Align)->getZExtValue();
1720 if (Alignment >= 32 && NumRegs == 4)
1722 else if (Alignment >= 16 && (NumRegs == 2 || NumRegs == 4))
1724 else if (Alignment >= 8)
1729 return CurDAG->getTargetConstant(Alignment, dl, MVT::i32);
1732 static bool isVLDfixed(unsigned Opc)
1735 default: return false;
1736 case ARM::VLD1d8wb_fixed : return true;
1737 case ARM::VLD1d16wb_fixed : return true;
1738 case ARM::VLD1d64Qwb_fixed : return true;
1739 case ARM::VLD1d32wb_fixed : return true;
1740 case ARM::VLD1d64wb_fixed : return true;
1741 case ARM::VLD1d64TPseudoWB_fixed : return true;
1742 case ARM::VLD1d64QPseudoWB_fixed : return true;
1743 case ARM::VLD1q8wb_fixed : return true;
1744 case ARM::VLD1q16wb_fixed : return true;
1745 case ARM::VLD1q32wb_fixed : return true;
1746 case ARM::VLD1q64wb_fixed : return true;
1747 case ARM::VLD2d8wb_fixed : return true;
1748 case ARM::VLD2d16wb_fixed : return true;
1749 case ARM::VLD2d32wb_fixed : return true;
1750 case ARM::VLD2q8PseudoWB_fixed : return true;
1751 case ARM::VLD2q16PseudoWB_fixed : return true;
1752 case ARM::VLD2q32PseudoWB_fixed : return true;
1753 case ARM::VLD2DUPd8wb_fixed : return true;
1754 case ARM::VLD2DUPd16wb_fixed : return true;
1755 case ARM::VLD2DUPd32wb_fixed : return true;
1759 static bool isVSTfixed(unsigned Opc)
1762 default: return false;
1763 case ARM::VST1d8wb_fixed : return true;
1764 case ARM::VST1d16wb_fixed : return true;
1765 case ARM::VST1d32wb_fixed : return true;
1766 case ARM::VST1d64wb_fixed : return true;
1767 case ARM::VST1q8wb_fixed : return true;
1768 case ARM::VST1q16wb_fixed : return true;
1769 case ARM::VST1q32wb_fixed : return true;
1770 case ARM::VST1q64wb_fixed : return true;
1771 case ARM::VST1d64TPseudoWB_fixed : return true;
1772 case ARM::VST1d64QPseudoWB_fixed : return true;
1773 case ARM::VST2d8wb_fixed : return true;
1774 case ARM::VST2d16wb_fixed : return true;
1775 case ARM::VST2d32wb_fixed : return true;
1776 case ARM::VST2q8PseudoWB_fixed : return true;
1777 case ARM::VST2q16PseudoWB_fixed : return true;
1778 case ARM::VST2q32PseudoWB_fixed : return true;
1782 // Get the register stride update opcode of a VLD/VST instruction that
1783 // is otherwise equivalent to the given fixed stride updating instruction.
1784 static unsigned getVLDSTRegisterUpdateOpcode(unsigned Opc) {
1785 assert((isVLDfixed(Opc) || isVSTfixed(Opc))
1786 && "Incorrect fixed stride updating instruction.");
1789 case ARM::VLD1d8wb_fixed: return ARM::VLD1d8wb_register;
1790 case ARM::VLD1d16wb_fixed: return ARM::VLD1d16wb_register;
1791 case ARM::VLD1d32wb_fixed: return ARM::VLD1d32wb_register;
1792 case ARM::VLD1d64wb_fixed: return ARM::VLD1d64wb_register;
1793 case ARM::VLD1q8wb_fixed: return ARM::VLD1q8wb_register;
1794 case ARM::VLD1q16wb_fixed: return ARM::VLD1q16wb_register;
1795 case ARM::VLD1q32wb_fixed: return ARM::VLD1q32wb_register;
1796 case ARM::VLD1q64wb_fixed: return ARM::VLD1q64wb_register;
1797 case ARM::VLD1d64Twb_fixed: return ARM::VLD1d64Twb_register;
1798 case ARM::VLD1d64Qwb_fixed: return ARM::VLD1d64Qwb_register;
1799 case ARM::VLD1d64TPseudoWB_fixed: return ARM::VLD1d64TPseudoWB_register;
1800 case ARM::VLD1d64QPseudoWB_fixed: return ARM::VLD1d64QPseudoWB_register;
1802 case ARM::VST1d8wb_fixed: return ARM::VST1d8wb_register;
1803 case ARM::VST1d16wb_fixed: return ARM::VST1d16wb_register;
1804 case ARM::VST1d32wb_fixed: return ARM::VST1d32wb_register;
1805 case ARM::VST1d64wb_fixed: return ARM::VST1d64wb_register;
1806 case ARM::VST1q8wb_fixed: return ARM::VST1q8wb_register;
1807 case ARM::VST1q16wb_fixed: return ARM::VST1q16wb_register;
1808 case ARM::VST1q32wb_fixed: return ARM::VST1q32wb_register;
1809 case ARM::VST1q64wb_fixed: return ARM::VST1q64wb_register;
1810 case ARM::VST1d64TPseudoWB_fixed: return ARM::VST1d64TPseudoWB_register;
1811 case ARM::VST1d64QPseudoWB_fixed: return ARM::VST1d64QPseudoWB_register;
1813 case ARM::VLD2d8wb_fixed: return ARM::VLD2d8wb_register;
1814 case ARM::VLD2d16wb_fixed: return ARM::VLD2d16wb_register;
1815 case ARM::VLD2d32wb_fixed: return ARM::VLD2d32wb_register;
1816 case ARM::VLD2q8PseudoWB_fixed: return ARM::VLD2q8PseudoWB_register;
1817 case ARM::VLD2q16PseudoWB_fixed: return ARM::VLD2q16PseudoWB_register;
1818 case ARM::VLD2q32PseudoWB_fixed: return ARM::VLD2q32PseudoWB_register;
1820 case ARM::VST2d8wb_fixed: return ARM::VST2d8wb_register;
1821 case ARM::VST2d16wb_fixed: return ARM::VST2d16wb_register;
1822 case ARM::VST2d32wb_fixed: return ARM::VST2d32wb_register;
1823 case ARM::VST2q8PseudoWB_fixed: return ARM::VST2q8PseudoWB_register;
1824 case ARM::VST2q16PseudoWB_fixed: return ARM::VST2q16PseudoWB_register;
1825 case ARM::VST2q32PseudoWB_fixed: return ARM::VST2q32PseudoWB_register;
1827 case ARM::VLD2DUPd8wb_fixed: return ARM::VLD2DUPd8wb_register;
1828 case ARM::VLD2DUPd16wb_fixed: return ARM::VLD2DUPd16wb_register;
1829 case ARM::VLD2DUPd32wb_fixed: return ARM::VLD2DUPd32wb_register;
1831 return Opc; // If not one we handle, return it unchanged.
1834 void ARMDAGToDAGISel::SelectVLD(SDNode *N, bool isUpdating, unsigned NumVecs,
1835 const uint16_t *DOpcodes,
1836 const uint16_t *QOpcodes0,
1837 const uint16_t *QOpcodes1) {
1838 assert(NumVecs >= 1 && NumVecs <= 4 && "VLD NumVecs out-of-range");
1841 SDValue MemAddr, Align;
1842 unsigned AddrOpIdx = isUpdating ? 1 : 2;
1843 if (!SelectAddrMode6(N, N->getOperand(AddrOpIdx), MemAddr, Align))
1846 SDValue Chain = N->getOperand(0);
1847 EVT VT = N->getValueType(0);
1848 bool is64BitVector = VT.is64BitVector();
1849 Align = GetVLDSTAlign(Align, dl, NumVecs, is64BitVector);
1851 unsigned OpcodeIndex;
1852 switch (VT.getSimpleVT().SimpleTy) {
1853 default: llvm_unreachable("unhandled vld type");
1854 // Double-register operations:
1855 case MVT::v8i8: OpcodeIndex = 0; break;
1856 case MVT::v4i16: OpcodeIndex = 1; break;
1858 case MVT::v2i32: OpcodeIndex = 2; break;
1859 case MVT::v1i64: OpcodeIndex = 3; break;
1860 // Quad-register operations:
1861 case MVT::v16i8: OpcodeIndex = 0; break;
1862 case MVT::v8i16: OpcodeIndex = 1; break;
1864 case MVT::v4i32: OpcodeIndex = 2; break;
1866 case MVT::v2i64: OpcodeIndex = 3;
1867 assert(NumVecs == 1 && "v2i64 type only supported for VLD1");
1875 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
1878 ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts);
1880 std::vector<EVT> ResTys;
1881 ResTys.push_back(ResTy);
1883 ResTys.push_back(MVT::i32);
1884 ResTys.push_back(MVT::Other);
1886 SDValue Pred = getAL(CurDAG, dl);
1887 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1889 SmallVector<SDValue, 7> Ops;
1891 // Double registers and VLD1/VLD2 quad registers are directly supported.
1892 if (is64BitVector || NumVecs <= 2) {
1893 unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
1894 QOpcodes0[OpcodeIndex]);
1895 Ops.push_back(MemAddr);
1896 Ops.push_back(Align);
1898 SDValue Inc = N->getOperand(AddrOpIdx + 1);
1899 // FIXME: VLD1/VLD2 fixed increment doesn't need Reg0. Remove the reg0
1900 // case entirely when the rest are updated to that form, too.
1901 if ((NumVecs <= 2) && !isa<ConstantSDNode>(Inc.getNode()))
1902 Opc = getVLDSTRegisterUpdateOpcode(Opc);
1903 // FIXME: We use a VLD1 for v1i64 even if the pseudo says vld2/3/4, so
1904 // check for that explicitly too. Horribly hacky, but temporary.
1905 if ((NumVecs > 2 && !isVLDfixed(Opc)) ||
1906 !isa<ConstantSDNode>(Inc.getNode()))
1907 Ops.push_back(isa<ConstantSDNode>(Inc.getNode()) ? Reg0 : Inc);
1909 Ops.push_back(Pred);
1910 Ops.push_back(Reg0);
1911 Ops.push_back(Chain);
1912 VLd = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
1915 // Otherwise, quad registers are loaded with two separate instructions,
1916 // where one loads the even registers and the other loads the odd registers.
1917 EVT AddrTy = MemAddr.getValueType();
1919 // Load the even subregs. This is always an updating load, so that it
1920 // provides the address to the second load for the odd subregs.
1922 SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, ResTy), 0);
1923 const SDValue OpsA[] = { MemAddr, Align, Reg0, ImplDef, Pred, Reg0, Chain };
1924 SDNode *VLdA = CurDAG->getMachineNode(QOpcodes0[OpcodeIndex], dl,
1925 ResTy, AddrTy, MVT::Other, OpsA);
1926 Chain = SDValue(VLdA, 2);
1928 // Load the odd subregs.
1929 Ops.push_back(SDValue(VLdA, 1));
1930 Ops.push_back(Align);
1932 SDValue Inc = N->getOperand(AddrOpIdx + 1);
1933 assert(isa<ConstantSDNode>(Inc.getNode()) &&
1934 "only constant post-increment update allowed for VLD3/4");
1936 Ops.push_back(Reg0);
1938 Ops.push_back(SDValue(VLdA, 0));
1939 Ops.push_back(Pred);
1940 Ops.push_back(Reg0);
1941 Ops.push_back(Chain);
1942 VLd = CurDAG->getMachineNode(QOpcodes1[OpcodeIndex], dl, ResTys, Ops);
1945 // Transfer memoperands.
1946 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1947 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
1948 cast<MachineSDNode>(VLd)->setMemRefs(MemOp, MemOp + 1);
1951 ReplaceNode(N, VLd);
1955 // Extract out the subregisters.
1956 SDValue SuperReg = SDValue(VLd, 0);
1957 static_assert(ARM::dsub_7 == ARM::dsub_0 + 7 &&
1958 ARM::qsub_3 == ARM::qsub_0 + 3,
1959 "Unexpected subreg numbering");
1960 unsigned Sub0 = (is64BitVector ? ARM::dsub_0 : ARM::qsub_0);
1961 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1962 ReplaceUses(SDValue(N, Vec),
1963 CurDAG->getTargetExtractSubreg(Sub0 + Vec, dl, VT, SuperReg));
1964 ReplaceUses(SDValue(N, NumVecs), SDValue(VLd, 1));
1966 ReplaceUses(SDValue(N, NumVecs + 1), SDValue(VLd, 2));
1967 CurDAG->RemoveDeadNode(N);
1970 void ARMDAGToDAGISel::SelectVST(SDNode *N, bool isUpdating, unsigned NumVecs,
1971 const uint16_t *DOpcodes,
1972 const uint16_t *QOpcodes0,
1973 const uint16_t *QOpcodes1) {
1974 assert(NumVecs >= 1 && NumVecs <= 4 && "VST NumVecs out-of-range");
1977 SDValue MemAddr, Align;
1978 unsigned AddrOpIdx = isUpdating ? 1 : 2;
1979 unsigned Vec0Idx = 3; // AddrOpIdx + (isUpdating ? 2 : 1)
1980 if (!SelectAddrMode6(N, N->getOperand(AddrOpIdx), MemAddr, Align))
1983 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1984 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
1986 SDValue Chain = N->getOperand(0);
1987 EVT VT = N->getOperand(Vec0Idx).getValueType();
1988 bool is64BitVector = VT.is64BitVector();
1989 Align = GetVLDSTAlign(Align, dl, NumVecs, is64BitVector);
1991 unsigned OpcodeIndex;
1992 switch (VT.getSimpleVT().SimpleTy) {
1993 default: llvm_unreachable("unhandled vst type");
1994 // Double-register operations:
1995 case MVT::v8i8: OpcodeIndex = 0; break;
1996 case MVT::v4i16: OpcodeIndex = 1; break;
1998 case MVT::v2i32: OpcodeIndex = 2; break;
1999 case MVT::v1i64: OpcodeIndex = 3; break;
2000 // Quad-register operations:
2001 case MVT::v16i8: OpcodeIndex = 0; break;
2002 case MVT::v8i16: OpcodeIndex = 1; break;
2004 case MVT::v4i32: OpcodeIndex = 2; break;
2006 case MVT::v2i64: OpcodeIndex = 3;
2007 assert(NumVecs == 1 && "v2i64 type only supported for VST1");
2011 std::vector<EVT> ResTys;
2013 ResTys.push_back(MVT::i32);
2014 ResTys.push_back(MVT::Other);
2016 SDValue Pred = getAL(CurDAG, dl);
2017 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2018 SmallVector<SDValue, 7> Ops;
2020 // Double registers and VST1/VST2 quad registers are directly supported.
2021 if (is64BitVector || NumVecs <= 2) {
2024 SrcReg = N->getOperand(Vec0Idx);
2025 } else if (is64BitVector) {
2026 // Form a REG_SEQUENCE to force register allocation.
2027 SDValue V0 = N->getOperand(Vec0Idx + 0);
2028 SDValue V1 = N->getOperand(Vec0Idx + 1);
2030 SrcReg = SDValue(createDRegPairNode(MVT::v2i64, V0, V1), 0);
2032 SDValue V2 = N->getOperand(Vec0Idx + 2);
2033 // If it's a vst3, form a quad D-register and leave the last part as
2035 SDValue V3 = (NumVecs == 3)
2036 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
2037 : N->getOperand(Vec0Idx + 3);
2038 SrcReg = SDValue(createQuadDRegsNode(MVT::v4i64, V0, V1, V2, V3), 0);
2041 // Form a QQ register.
2042 SDValue Q0 = N->getOperand(Vec0Idx);
2043 SDValue Q1 = N->getOperand(Vec0Idx + 1);
2044 SrcReg = SDValue(createQRegPairNode(MVT::v4i64, Q0, Q1), 0);
2047 unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
2048 QOpcodes0[OpcodeIndex]);
2049 Ops.push_back(MemAddr);
2050 Ops.push_back(Align);
2052 SDValue Inc = N->getOperand(AddrOpIdx + 1);
2053 // FIXME: VST1/VST2 fixed increment doesn't need Reg0. Remove the reg0
2054 // case entirely when the rest are updated to that form, too.
2055 if (NumVecs <= 2 && !isa<ConstantSDNode>(Inc.getNode()))
2056 Opc = getVLDSTRegisterUpdateOpcode(Opc);
2057 // FIXME: We use a VST1 for v1i64 even if the pseudo says vld2/3/4, so
2058 // check for that explicitly too. Horribly hacky, but temporary.
2059 if (!isa<ConstantSDNode>(Inc.getNode()))
2061 else if (NumVecs > 2 && !isVSTfixed(Opc))
2062 Ops.push_back(Reg0);
2064 Ops.push_back(SrcReg);
2065 Ops.push_back(Pred);
2066 Ops.push_back(Reg0);
2067 Ops.push_back(Chain);
2068 SDNode *VSt = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
2070 // Transfer memoperands.
2071 cast<MachineSDNode>(VSt)->setMemRefs(MemOp, MemOp + 1);
2073 ReplaceNode(N, VSt);
2077 // Otherwise, quad registers are stored with two separate instructions,
2078 // where one stores the even registers and the other stores the odd registers.
2080 // Form the QQQQ REG_SEQUENCE.
2081 SDValue V0 = N->getOperand(Vec0Idx + 0);
2082 SDValue V1 = N->getOperand(Vec0Idx + 1);
2083 SDValue V2 = N->getOperand(Vec0Idx + 2);
2084 SDValue V3 = (NumVecs == 3)
2085 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
2086 : N->getOperand(Vec0Idx + 3);
2087 SDValue RegSeq = SDValue(createQuadQRegsNode(MVT::v8i64, V0, V1, V2, V3), 0);
2089 // Store the even D registers. This is always an updating store, so that it
2090 // provides the address to the second store for the odd subregs.
2091 const SDValue OpsA[] = { MemAddr, Align, Reg0, RegSeq, Pred, Reg0, Chain };
2092 SDNode *VStA = CurDAG->getMachineNode(QOpcodes0[OpcodeIndex], dl,
2093 MemAddr.getValueType(),
2095 cast<MachineSDNode>(VStA)->setMemRefs(MemOp, MemOp + 1);
2096 Chain = SDValue(VStA, 1);
2098 // Store the odd D registers.
2099 Ops.push_back(SDValue(VStA, 0));
2100 Ops.push_back(Align);
2102 SDValue Inc = N->getOperand(AddrOpIdx + 1);
2103 assert(isa<ConstantSDNode>(Inc.getNode()) &&
2104 "only constant post-increment update allowed for VST3/4");
2106 Ops.push_back(Reg0);
2108 Ops.push_back(RegSeq);
2109 Ops.push_back(Pred);
2110 Ops.push_back(Reg0);
2111 Ops.push_back(Chain);
2112 SDNode *VStB = CurDAG->getMachineNode(QOpcodes1[OpcodeIndex], dl, ResTys,
2114 cast<MachineSDNode>(VStB)->setMemRefs(MemOp, MemOp + 1);
2115 ReplaceNode(N, VStB);
2118 void ARMDAGToDAGISel::SelectVLDSTLane(SDNode *N, bool IsLoad, bool isUpdating,
2120 const uint16_t *DOpcodes,
2121 const uint16_t *QOpcodes) {
2122 assert(NumVecs >=2 && NumVecs <= 4 && "VLDSTLane NumVecs out-of-range");
2125 SDValue MemAddr, Align;
2126 unsigned AddrOpIdx = isUpdating ? 1 : 2;
2127 unsigned Vec0Idx = 3; // AddrOpIdx + (isUpdating ? 2 : 1)
2128 if (!SelectAddrMode6(N, N->getOperand(AddrOpIdx), MemAddr, Align))
2131 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2132 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
2134 SDValue Chain = N->getOperand(0);
2136 cast<ConstantSDNode>(N->getOperand(Vec0Idx + NumVecs))->getZExtValue();
2137 EVT VT = N->getOperand(Vec0Idx).getValueType();
2138 bool is64BitVector = VT.is64BitVector();
2140 unsigned Alignment = 0;
2142 Alignment = cast<ConstantSDNode>(Align)->getZExtValue();
2143 unsigned NumBytes = NumVecs * VT.getVectorElementType().getSizeInBits()/8;
2144 if (Alignment > NumBytes)
2145 Alignment = NumBytes;
2146 if (Alignment < 8 && Alignment < NumBytes)
2148 // Alignment must be a power of two; make sure of that.
2149 Alignment = (Alignment & -Alignment);
2153 Align = CurDAG->getTargetConstant(Alignment, dl, MVT::i32);
2155 unsigned OpcodeIndex;
2156 switch (VT.getSimpleVT().SimpleTy) {
2157 default: llvm_unreachable("unhandled vld/vst lane type");
2158 // Double-register operations:
2159 case MVT::v8i8: OpcodeIndex = 0; break;
2160 case MVT::v4i16: OpcodeIndex = 1; break;
2162 case MVT::v2i32: OpcodeIndex = 2; break;
2163 // Quad-register operations:
2164 case MVT::v8i16: OpcodeIndex = 0; break;
2166 case MVT::v4i32: OpcodeIndex = 1; break;
2169 std::vector<EVT> ResTys;
2171 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
2174 ResTys.push_back(EVT::getVectorVT(*CurDAG->getContext(),
2175 MVT::i64, ResTyElts));
2178 ResTys.push_back(MVT::i32);
2179 ResTys.push_back(MVT::Other);
2181 SDValue Pred = getAL(CurDAG, dl);
2182 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2184 SmallVector<SDValue, 8> Ops;
2185 Ops.push_back(MemAddr);
2186 Ops.push_back(Align);
2188 SDValue Inc = N->getOperand(AddrOpIdx + 1);
2189 Ops.push_back(isa<ConstantSDNode>(Inc.getNode()) ? Reg0 : Inc);
2193 SDValue V0 = N->getOperand(Vec0Idx + 0);
2194 SDValue V1 = N->getOperand(Vec0Idx + 1);
2197 SuperReg = SDValue(createDRegPairNode(MVT::v2i64, V0, V1), 0);
2199 SuperReg = SDValue(createQRegPairNode(MVT::v4i64, V0, V1), 0);
2201 SDValue V2 = N->getOperand(Vec0Idx + 2);
2202 SDValue V3 = (NumVecs == 3)
2203 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
2204 : N->getOperand(Vec0Idx + 3);
2206 SuperReg = SDValue(createQuadDRegsNode(MVT::v4i64, V0, V1, V2, V3), 0);
2208 SuperReg = SDValue(createQuadQRegsNode(MVT::v8i64, V0, V1, V2, V3), 0);
2210 Ops.push_back(SuperReg);
2211 Ops.push_back(getI32Imm(Lane, dl));
2212 Ops.push_back(Pred);
2213 Ops.push_back(Reg0);
2214 Ops.push_back(Chain);
2216 unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
2217 QOpcodes[OpcodeIndex]);
2218 SDNode *VLdLn = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
2219 cast<MachineSDNode>(VLdLn)->setMemRefs(MemOp, MemOp + 1);
2221 ReplaceNode(N, VLdLn);
2225 // Extract the subregisters.
2226 SuperReg = SDValue(VLdLn, 0);
2227 static_assert(ARM::dsub_7 == ARM::dsub_0 + 7 &&
2228 ARM::qsub_3 == ARM::qsub_0 + 3,
2229 "Unexpected subreg numbering");
2230 unsigned Sub0 = is64BitVector ? ARM::dsub_0 : ARM::qsub_0;
2231 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
2232 ReplaceUses(SDValue(N, Vec),
2233 CurDAG->getTargetExtractSubreg(Sub0 + Vec, dl, VT, SuperReg));
2234 ReplaceUses(SDValue(N, NumVecs), SDValue(VLdLn, 1));
2236 ReplaceUses(SDValue(N, NumVecs + 1), SDValue(VLdLn, 2));
2237 CurDAG->RemoveDeadNode(N);
2240 void ARMDAGToDAGISel::SelectVLDDup(SDNode *N, bool isUpdating, unsigned NumVecs,
2241 const uint16_t *Opcodes) {
2242 assert(NumVecs >=2 && NumVecs <= 4 && "VLDDup NumVecs out-of-range");
2245 SDValue MemAddr, Align;
2246 if (!SelectAddrMode6(N, N->getOperand(1), MemAddr, Align))
2249 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2250 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
2252 SDValue Chain = N->getOperand(0);
2253 EVT VT = N->getValueType(0);
2255 unsigned Alignment = 0;
2257 Alignment = cast<ConstantSDNode>(Align)->getZExtValue();
2258 unsigned NumBytes = NumVecs * VT.getVectorElementType().getSizeInBits()/8;
2259 if (Alignment > NumBytes)
2260 Alignment = NumBytes;
2261 if (Alignment < 8 && Alignment < NumBytes)
2263 // Alignment must be a power of two; make sure of that.
2264 Alignment = (Alignment & -Alignment);
2268 Align = CurDAG->getTargetConstant(Alignment, dl, MVT::i32);
2270 unsigned OpcodeIndex;
2271 switch (VT.getSimpleVT().SimpleTy) {
2272 default: llvm_unreachable("unhandled vld-dup type");
2273 case MVT::v8i8: OpcodeIndex = 0; break;
2274 case MVT::v4i16: OpcodeIndex = 1; break;
2276 case MVT::v2i32: OpcodeIndex = 2; break;
2279 SDValue Pred = getAL(CurDAG, dl);
2280 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2282 unsigned Opc = Opcodes[OpcodeIndex];
2283 SmallVector<SDValue, 6> Ops;
2284 Ops.push_back(MemAddr);
2285 Ops.push_back(Align);
2287 // fixed-stride update instructions don't have an explicit writeback
2288 // operand. It's implicit in the opcode itself.
2289 SDValue Inc = N->getOperand(2);
2290 if (!isa<ConstantSDNode>(Inc.getNode()))
2292 // FIXME: VLD3 and VLD4 haven't been updated to that form yet.
2293 else if (NumVecs > 2)
2294 Ops.push_back(Reg0);
2296 Ops.push_back(Pred);
2297 Ops.push_back(Reg0);
2298 Ops.push_back(Chain);
2300 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
2301 std::vector<EVT> ResTys;
2302 ResTys.push_back(EVT::getVectorVT(*CurDAG->getContext(), MVT::i64,ResTyElts));
2304 ResTys.push_back(MVT::i32);
2305 ResTys.push_back(MVT::Other);
2306 SDNode *VLdDup = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
2307 cast<MachineSDNode>(VLdDup)->setMemRefs(MemOp, MemOp + 1);
2308 SuperReg = SDValue(VLdDup, 0);
2310 // Extract the subregisters.
2311 static_assert(ARM::dsub_7 == ARM::dsub_0 + 7, "Unexpected subreg numbering");
2312 unsigned SubIdx = ARM::dsub_0;
2313 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
2314 ReplaceUses(SDValue(N, Vec),
2315 CurDAG->getTargetExtractSubreg(SubIdx+Vec, dl, VT, SuperReg));
2316 ReplaceUses(SDValue(N, NumVecs), SDValue(VLdDup, 1));
2318 ReplaceUses(SDValue(N, NumVecs + 1), SDValue(VLdDup, 2));
2319 CurDAG->RemoveDeadNode(N);
2322 void ARMDAGToDAGISel::SelectVTBL(SDNode *N, bool IsExt, unsigned NumVecs,
2324 assert(NumVecs >= 2 && NumVecs <= 4 && "VTBL NumVecs out-of-range");
2326 EVT VT = N->getValueType(0);
2327 unsigned FirstTblReg = IsExt ? 2 : 1;
2329 // Form a REG_SEQUENCE to force register allocation.
2331 SDValue V0 = N->getOperand(FirstTblReg + 0);
2332 SDValue V1 = N->getOperand(FirstTblReg + 1);
2334 RegSeq = SDValue(createDRegPairNode(MVT::v16i8, V0, V1), 0);
2336 SDValue V2 = N->getOperand(FirstTblReg + 2);
2337 // If it's a vtbl3, form a quad D-register and leave the last part as
2339 SDValue V3 = (NumVecs == 3)
2340 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
2341 : N->getOperand(FirstTblReg + 3);
2342 RegSeq = SDValue(createQuadDRegsNode(MVT::v4i64, V0, V1, V2, V3), 0);
2345 SmallVector<SDValue, 6> Ops;
2347 Ops.push_back(N->getOperand(1));
2348 Ops.push_back(RegSeq);
2349 Ops.push_back(N->getOperand(FirstTblReg + NumVecs));
2350 Ops.push_back(getAL(CurDAG, dl)); // predicate
2351 Ops.push_back(CurDAG->getRegister(0, MVT::i32)); // predicate register
2352 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, VT, Ops));
2355 bool ARMDAGToDAGISel::tryV6T2BitfieldExtractOp(SDNode *N, bool isSigned) {
2356 if (!Subtarget->hasV6T2Ops())
2359 unsigned Opc = isSigned
2360 ? (Subtarget->isThumb() ? ARM::t2SBFX : ARM::SBFX)
2361 : (Subtarget->isThumb() ? ARM::t2UBFX : ARM::UBFX);
2364 // For unsigned extracts, check for a shift right and mask
2365 unsigned And_imm = 0;
2366 if (N->getOpcode() == ISD::AND) {
2367 if (isOpcWithIntImmediate(N, ISD::AND, And_imm)) {
2369 // The immediate is a mask of the low bits iff imm & (imm+1) == 0
2370 if (And_imm & (And_imm + 1))
2373 unsigned Srl_imm = 0;
2374 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRL,
2376 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
2378 // Note: The width operand is encoded as width-1.
2379 unsigned Width = countTrailingOnes(And_imm) - 1;
2380 unsigned LSB = Srl_imm;
2382 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2384 if ((LSB + Width + 1) == N->getValueType(0).getSizeInBits()) {
2385 // It's cheaper to use a right shift to extract the top bits.
2386 if (Subtarget->isThumb()) {
2387 Opc = isSigned ? ARM::t2ASRri : ARM::t2LSRri;
2388 SDValue Ops[] = { N->getOperand(0).getOperand(0),
2389 CurDAG->getTargetConstant(LSB, dl, MVT::i32),
2390 getAL(CurDAG, dl), Reg0, Reg0 };
2391 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
2395 // ARM models shift instructions as MOVsi with shifter operand.
2396 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(ISD::SRL);
2398 CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, LSB), dl,
2400 SDValue Ops[] = { N->getOperand(0).getOperand(0), ShOpc,
2401 getAL(CurDAG, dl), Reg0, Reg0 };
2402 CurDAG->SelectNodeTo(N, ARM::MOVsi, MVT::i32, Ops);
2406 SDValue Ops[] = { N->getOperand(0).getOperand(0),
2407 CurDAG->getTargetConstant(LSB, dl, MVT::i32),
2408 CurDAG->getTargetConstant(Width, dl, MVT::i32),
2409 getAL(CurDAG, dl), Reg0 };
2410 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
2417 // Otherwise, we're looking for a shift of a shift
2418 unsigned Shl_imm = 0;
2419 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SHL, Shl_imm)) {
2420 assert(Shl_imm > 0 && Shl_imm < 32 && "bad amount in shift node!");
2421 unsigned Srl_imm = 0;
2422 if (isInt32Immediate(N->getOperand(1), Srl_imm)) {
2423 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
2424 // Note: The width operand is encoded as width-1.
2425 unsigned Width = 32 - Srl_imm - 1;
2426 int LSB = Srl_imm - Shl_imm;
2429 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2430 SDValue Ops[] = { N->getOperand(0).getOperand(0),
2431 CurDAG->getTargetConstant(LSB, dl, MVT::i32),
2432 CurDAG->getTargetConstant(Width, dl, MVT::i32),
2433 getAL(CurDAG, dl), Reg0 };
2434 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
2439 // Or we are looking for a shift of an and, with a mask operand
2440 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, And_imm) &&
2441 isShiftedMask_32(And_imm)) {
2442 unsigned Srl_imm = 0;
2443 unsigned LSB = countTrailingZeros(And_imm);
2444 // Shift must be the same as the ands lsb
2445 if (isInt32Immediate(N->getOperand(1), Srl_imm) && Srl_imm == LSB) {
2446 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
2447 unsigned MSB = 31 - countLeadingZeros(And_imm);
2448 // Note: The width operand is encoded as width-1.
2449 unsigned Width = MSB - LSB;
2450 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2451 SDValue Ops[] = { N->getOperand(0).getOperand(0),
2452 CurDAG->getTargetConstant(Srl_imm, dl, MVT::i32),
2453 CurDAG->getTargetConstant(Width, dl, MVT::i32),
2454 getAL(CurDAG, dl), Reg0 };
2455 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
2460 if (N->getOpcode() == ISD::SIGN_EXTEND_INREG) {
2461 unsigned Width = cast<VTSDNode>(N->getOperand(1))->getVT().getSizeInBits();
2463 if (!isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRL, LSB) &&
2464 !isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRA, LSB))
2467 if (LSB + Width > 32)
2470 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2471 SDValue Ops[] = { N->getOperand(0).getOperand(0),
2472 CurDAG->getTargetConstant(LSB, dl, MVT::i32),
2473 CurDAG->getTargetConstant(Width - 1, dl, MVT::i32),
2474 getAL(CurDAG, dl), Reg0 };
2475 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
2482 /// Target-specific DAG combining for ISD::XOR.
2483 /// Target-independent combining lowers SELECT_CC nodes of the form
2484 /// select_cc setg[ge] X, 0, X, -X
2485 /// select_cc setgt X, -1, X, -X
2486 /// select_cc setl[te] X, 0, -X, X
2487 /// select_cc setlt X, 1, -X, X
2488 /// which represent Integer ABS into:
2489 /// Y = sra (X, size(X)-1); xor (add (X, Y), Y)
2490 /// ARM instruction selection detects the latter and matches it to
2491 /// ARM::ABS or ARM::t2ABS machine node.
2492 bool ARMDAGToDAGISel::tryABSOp(SDNode *N){
2493 SDValue XORSrc0 = N->getOperand(0);
2494 SDValue XORSrc1 = N->getOperand(1);
2495 EVT VT = N->getValueType(0);
2497 if (Subtarget->isThumb1Only())
2500 if (XORSrc0.getOpcode() != ISD::ADD || XORSrc1.getOpcode() != ISD::SRA)
2503 SDValue ADDSrc0 = XORSrc0.getOperand(0);
2504 SDValue ADDSrc1 = XORSrc0.getOperand(1);
2505 SDValue SRASrc0 = XORSrc1.getOperand(0);
2506 SDValue SRASrc1 = XORSrc1.getOperand(1);
2507 ConstantSDNode *SRAConstant = dyn_cast<ConstantSDNode>(SRASrc1);
2508 EVT XType = SRASrc0.getValueType();
2509 unsigned Size = XType.getSizeInBits() - 1;
2511 if (ADDSrc1 == XORSrc1 && ADDSrc0 == SRASrc0 &&
2512 XType.isInteger() && SRAConstant != nullptr &&
2513 Size == SRAConstant->getZExtValue()) {
2514 unsigned Opcode = Subtarget->isThumb2() ? ARM::t2ABS : ARM::ABS;
2515 CurDAG->SelectNodeTo(N, Opcode, VT, ADDSrc0);
2522 static bool SearchSignedMulShort(SDValue SignExt, unsigned *Opc, SDValue &Src1,
2524 // For SM*WB, we need to some form of sext.
2525 // For SM*WT, we need to search for (sra X, 16)
2526 // Src1 then gets set to X.
2527 if ((SignExt.getOpcode() == ISD::SIGN_EXTEND ||
2528 SignExt.getOpcode() == ISD::SIGN_EXTEND_INREG ||
2529 SignExt.getOpcode() == ISD::AssertSext) &&
2530 SignExt.getValueType() == MVT::i32) {
2532 *Opc = Accumulate ? ARM::SMLAWB : ARM::SMULWB;
2533 Src1 = SignExt.getOperand(0);
2537 if (SignExt.getOpcode() != ISD::SRA)
2540 ConstantSDNode *SRASrc1 = dyn_cast<ConstantSDNode>(SignExt.getOperand(1));
2541 if (!SRASrc1 || SRASrc1->getZExtValue() != 16)
2544 SDValue Op0 = SignExt.getOperand(0);
2546 // The sign extend operand for SM*WB could be generated by a shl and ashr.
2547 if (Op0.getOpcode() == ISD::SHL) {
2549 ConstantSDNode *SHLSrc1 = dyn_cast<ConstantSDNode>(SHL.getOperand(1));
2550 if (!SHLSrc1 || SHLSrc1->getZExtValue() != 16)
2553 *Opc = Accumulate ? ARM::SMLAWB : ARM::SMULWB;
2554 Src1 = Op0.getOperand(0);
2557 *Opc = Accumulate ? ARM::SMLAWT : ARM::SMULWT;
2558 Src1 = SignExt.getOperand(0);
2562 static bool SearchSignedMulLong(SDValue OR, unsigned *Opc, SDValue &Src0,
2563 SDValue &Src1, bool Accumulate) {
2564 // First we look for:
2565 // (add (or (srl ?, 16), (shl ?, 16)))
2566 if (OR.getOpcode() != ISD::OR)
2569 SDValue SRL = OR.getOperand(0);
2570 SDValue SHL = OR.getOperand(1);
2572 if (SRL.getOpcode() != ISD::SRL || SHL.getOpcode() != ISD::SHL) {
2573 SRL = OR.getOperand(1);
2574 SHL = OR.getOperand(0);
2575 if (SRL.getOpcode() != ISD::SRL || SHL.getOpcode() != ISD::SHL)
2579 ConstantSDNode *SRLSrc1 = dyn_cast<ConstantSDNode>(SRL.getOperand(1));
2580 ConstantSDNode *SHLSrc1 = dyn_cast<ConstantSDNode>(SHL.getOperand(1));
2581 if (!SRLSrc1 || !SHLSrc1 || SRLSrc1->getZExtValue() != 16 ||
2582 SHLSrc1->getZExtValue() != 16)
2585 // The first operands to the shifts need to be the two results from the
2586 // same smul_lohi node.
2587 if ((SRL.getOperand(0).getNode() != SHL.getOperand(0).getNode()) ||
2588 SRL.getOperand(0).getOpcode() != ISD::SMUL_LOHI)
2591 SDNode *SMULLOHI = SRL.getOperand(0).getNode();
2592 if (SRL.getOperand(0) != SDValue(SMULLOHI, 0) ||
2593 SHL.getOperand(0) != SDValue(SMULLOHI, 1))
2597 // (add (or (srl (smul_lohi ?, ?), 16), (shl (smul_lohi ?, ?), 16)))
2598 // For SMLAW[B|T] smul_lohi will take a 32-bit and a 16-bit arguments.
2599 // For SMLAWB the 16-bit value will signed extended somehow.
2600 // For SMLAWT only the SRA is required.
2602 // Check both sides of SMUL_LOHI
2603 if (SearchSignedMulShort(SMULLOHI->getOperand(0), Opc, Src1, Accumulate)) {
2604 Src0 = SMULLOHI->getOperand(1);
2605 } else if (SearchSignedMulShort(SMULLOHI->getOperand(1), Opc, Src1,
2607 Src0 = SMULLOHI->getOperand(0);
2614 bool ARMDAGToDAGISel::trySMLAWSMULW(SDNode *N) {
2616 SDValue Src0 = N->getOperand(0);
2617 SDValue Src1 = N->getOperand(1);
2621 if (N->getOpcode() == ISD::ADD) {
2622 if (Src0.getOpcode() != ISD::OR && Src1.getOpcode() != ISD::OR)
2626 if (SearchSignedMulLong(Src0, &Opc, A, B, true)) {
2628 } else if (SearchSignedMulLong(Src1, &Opc, A, B, true)) {
2636 SDValue Ops[] = { A, B, Acc, getAL(CurDAG, dl),
2637 CurDAG->getRegister(0, MVT::i32) };
2638 CurDAG->SelectNodeTo(N, Opc, MVT::i32, MVT::Other, Ops);
2640 } else if (N->getOpcode() == ISD::OR &&
2641 SearchSignedMulLong(SDValue(N, 0), &Opc, A, B, false)) {
2645 SDValue Ops[] = { A, B, getAL(CurDAG, dl),
2646 CurDAG->getRegister(0, MVT::i32)};
2647 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
2653 /// We've got special pseudo-instructions for these
2654 void ARMDAGToDAGISel::SelectCMP_SWAP(SDNode *N) {
2656 EVT MemTy = cast<MemSDNode>(N)->getMemoryVT();
2657 if (MemTy == MVT::i8)
2658 Opcode = ARM::CMP_SWAP_8;
2659 else if (MemTy == MVT::i16)
2660 Opcode = ARM::CMP_SWAP_16;
2661 else if (MemTy == MVT::i32)
2662 Opcode = ARM::CMP_SWAP_32;
2664 llvm_unreachable("Unknown AtomicCmpSwap type");
2666 SDValue Ops[] = {N->getOperand(1), N->getOperand(2), N->getOperand(3),
2668 SDNode *CmpSwap = CurDAG->getMachineNode(
2670 CurDAG->getVTList(MVT::i32, MVT::i32, MVT::Other), Ops);
2672 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2673 MemOp[0] = cast<MemSDNode>(N)->getMemOperand();
2674 cast<MachineSDNode>(CmpSwap)->setMemRefs(MemOp, MemOp + 1);
2676 ReplaceUses(SDValue(N, 0), SDValue(CmpSwap, 0));
2677 ReplaceUses(SDValue(N, 1), SDValue(CmpSwap, 2));
2678 CurDAG->RemoveDeadNode(N);
2681 void ARMDAGToDAGISel::SelectConcatVector(SDNode *N) {
2682 // The only time a CONCAT_VECTORS operation can have legal types is when
2683 // two 64-bit vectors are concatenated to a 128-bit vector.
2684 EVT VT = N->getValueType(0);
2685 if (!VT.is128BitVector() || N->getNumOperands() != 2)
2686 llvm_unreachable("unexpected CONCAT_VECTORS");
2687 ReplaceNode(N, createDRegPairNode(VT, N->getOperand(0), N->getOperand(1)));
2690 void ARMDAGToDAGISel::Select(SDNode *N) {
2693 if (N->isMachineOpcode()) {
2695 return; // Already selected.
2698 switch (N->getOpcode()) {
2702 if (trySMLAWSMULW(N))
2705 case ISD::WRITE_REGISTER:
2706 if (tryWriteRegister(N))
2709 case ISD::READ_REGISTER:
2710 if (tryReadRegister(N))
2713 case ISD::INLINEASM:
2714 if (tryInlineAsm(N))
2718 // Select special operations if XOR node forms integer ABS pattern
2721 // Other cases are autogenerated.
2723 case ISD::Constant: {
2724 unsigned Val = cast<ConstantSDNode>(N)->getZExtValue();
2725 // If we can't materialize the constant we need to use a literal pool
2726 if (ConstantMaterializationCost(Val) > 2) {
2727 SDValue CPIdx = CurDAG->getTargetConstantPool(
2728 ConstantInt::get(Type::getInt32Ty(*CurDAG->getContext()), Val),
2729 TLI->getPointerTy(CurDAG->getDataLayout()));
2732 if (Subtarget->isThumb()) {
2733 SDValue Pred = getAL(CurDAG, dl);
2734 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
2735 SDValue Ops[] = { CPIdx, Pred, PredReg, CurDAG->getEntryNode() };
2736 ResNode = CurDAG->getMachineNode(ARM::tLDRpci, dl, MVT::i32, MVT::Other,
2741 CurDAG->getTargetConstant(0, dl, MVT::i32),
2743 CurDAG->getRegister(0, MVT::i32),
2744 CurDAG->getEntryNode()
2746 ResNode = CurDAG->getMachineNode(ARM::LDRcp, dl, MVT::i32, MVT::Other,
2749 ReplaceNode(N, ResNode);
2753 // Other cases are autogenerated.
2756 case ISD::FrameIndex: {
2757 // Selects to ADDri FI, 0 which in turn will become ADDri SP, imm.
2758 int FI = cast<FrameIndexSDNode>(N)->getIndex();
2759 SDValue TFI = CurDAG->getTargetFrameIndex(
2760 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
2761 if (Subtarget->isThumb1Only()) {
2762 // Set the alignment of the frame object to 4, to avoid having to generate
2763 // more than one ADD
2764 MachineFrameInfo *MFI = MF->getFrameInfo();
2765 if (MFI->getObjectAlignment(FI) < 4)
2766 MFI->setObjectAlignment(FI, 4);
2767 CurDAG->SelectNodeTo(N, ARM::tADDframe, MVT::i32, TFI,
2768 CurDAG->getTargetConstant(0, dl, MVT::i32));
2771 unsigned Opc = ((Subtarget->isThumb() && Subtarget->hasThumb2()) ?
2772 ARM::t2ADDri : ARM::ADDri);
2773 SDValue Ops[] = { TFI, CurDAG->getTargetConstant(0, dl, MVT::i32),
2774 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32),
2775 CurDAG->getRegister(0, MVT::i32) };
2776 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
2781 if (tryV6T2BitfieldExtractOp(N, false))
2784 case ISD::SIGN_EXTEND_INREG:
2786 if (tryV6T2BitfieldExtractOp(N, true))
2790 if (Subtarget->isThumb1Only())
2792 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
2793 unsigned RHSV = C->getZExtValue();
2795 if (isPowerOf2_32(RHSV-1)) { // 2^n+1?
2796 unsigned ShImm = Log2_32(RHSV-1);
2799 SDValue V = N->getOperand(0);
2800 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
2801 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, dl, MVT::i32);
2802 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2803 if (Subtarget->isThumb()) {
2804 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG, dl), Reg0, Reg0 };
2805 CurDAG->SelectNodeTo(N, ARM::t2ADDrs, MVT::i32, Ops);
2808 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG, dl), Reg0,
2810 CurDAG->SelectNodeTo(N, ARM::ADDrsi, MVT::i32, Ops);
2814 if (isPowerOf2_32(RHSV+1)) { // 2^n-1?
2815 unsigned ShImm = Log2_32(RHSV+1);
2818 SDValue V = N->getOperand(0);
2819 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
2820 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, dl, MVT::i32);
2821 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2822 if (Subtarget->isThumb()) {
2823 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG, dl), Reg0, Reg0 };
2824 CurDAG->SelectNodeTo(N, ARM::t2RSBrs, MVT::i32, Ops);
2827 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG, dl), Reg0,
2829 CurDAG->SelectNodeTo(N, ARM::RSBrsi, MVT::i32, Ops);
2836 // Check for unsigned bitfield extract
2837 if (tryV6T2BitfieldExtractOp(N, false))
2840 // If an immediate is used in an AND node, it is possible that the immediate
2841 // can be more optimally materialized when negated. If this is the case we
2842 // can negate the immediate and use a BIC instead.
2843 auto *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1));
2844 if (N1C && N1C->hasOneUse() && Subtarget->isThumb()) {
2845 uint32_t Imm = (uint32_t) N1C->getZExtValue();
2847 // In Thumb2 mode, an AND can take a 12-bit immediate. If this
2848 // immediate can be negated and fit in the immediate operand of
2849 // a t2BIC, don't do any manual transform here as this can be
2850 // handled by the generic ISel machinery.
2851 bool PreferImmediateEncoding =
2852 Subtarget->hasThumb2() && (is_t2_so_imm(Imm) || is_t2_so_imm_not(Imm));
2853 if (!PreferImmediateEncoding &&
2854 ConstantMaterializationCost(Imm) >
2855 ConstantMaterializationCost(~Imm)) {
2856 // The current immediate costs more to materialize than a negated
2857 // immediate, so negate the immediate and use a BIC.
2859 CurDAG->getConstant(~N1C->getZExtValue(), dl, MVT::i32);
2860 // If the new constant didn't exist before, reposition it in the topological
2861 // ordering so it is just before N. Otherwise, don't touch its location.
2862 if (NewImm->getNodeId() == -1)
2863 CurDAG->RepositionNode(N->getIterator(), NewImm.getNode());
2865 if (!Subtarget->hasThumb2()) {
2866 SDValue Ops[] = {CurDAG->getRegister(ARM::CPSR, MVT::i32),
2867 N->getOperand(0), NewImm, getAL(CurDAG, dl),
2868 CurDAG->getRegister(0, MVT::i32)};
2869 ReplaceNode(N, CurDAG->getMachineNode(ARM::tBIC, dl, MVT::i32, Ops));
2872 SDValue Ops[] = {N->getOperand(0), NewImm, getAL(CurDAG, dl),
2873 CurDAG->getRegister(0, MVT::i32),
2874 CurDAG->getRegister(0, MVT::i32)};
2876 CurDAG->getMachineNode(ARM::t2BICrr, dl, MVT::i32, Ops));
2882 // (and (or x, c2), c1) and top 16-bits of c1 and c2 match, lower 16-bits
2883 // of c1 are 0xffff, and lower 16-bit of c2 are 0. That is, the top 16-bits
2884 // are entirely contributed by c2 and lower 16-bits are entirely contributed
2885 // by x. That's equal to (or (and x, 0xffff), (and c1, 0xffff0000)).
2886 // Select it to: "movt x, ((c1 & 0xffff) >> 16)
2887 EVT VT = N->getValueType(0);
2890 unsigned Opc = (Subtarget->isThumb() && Subtarget->hasThumb2())
2892 : (Subtarget->hasV6T2Ops() ? ARM::MOVTi16 : 0);
2895 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
2896 N1C = dyn_cast<ConstantSDNode>(N1);
2899 if (N0.getOpcode() == ISD::OR && N0.getNode()->hasOneUse()) {
2900 SDValue N2 = N0.getOperand(1);
2901 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
2904 unsigned N1CVal = N1C->getZExtValue();
2905 unsigned N2CVal = N2C->getZExtValue();
2906 if ((N1CVal & 0xffff0000U) == (N2CVal & 0xffff0000U) &&
2907 (N1CVal & 0xffffU) == 0xffffU &&
2908 (N2CVal & 0xffffU) == 0x0U) {
2909 SDValue Imm16 = CurDAG->getTargetConstant((N2CVal & 0xFFFF0000U) >> 16,
2911 SDValue Ops[] = { N0.getOperand(0), Imm16,
2912 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32) };
2913 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, VT, Ops));
2919 case ARMISD::VMOVRRD:
2920 ReplaceNode(N, CurDAG->getMachineNode(ARM::VMOVRRD, dl, MVT::i32, MVT::i32,
2921 N->getOperand(0), getAL(CurDAG, dl),
2922 CurDAG->getRegister(0, MVT::i32)));
2924 case ISD::UMUL_LOHI: {
2925 if (Subtarget->isThumb1Only())
2927 if (Subtarget->isThumb()) {
2928 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
2929 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32) };
2931 N, CurDAG->getMachineNode(ARM::t2UMULL, dl, MVT::i32, MVT::i32, Ops));
2934 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
2935 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32),
2936 CurDAG->getRegister(0, MVT::i32) };
2937 ReplaceNode(N, CurDAG->getMachineNode(
2938 Subtarget->hasV6Ops() ? ARM::UMULL : ARM::UMULLv5, dl,
2939 MVT::i32, MVT::i32, Ops));
2943 case ISD::SMUL_LOHI: {
2944 if (Subtarget->isThumb1Only())
2946 if (Subtarget->isThumb()) {
2947 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
2948 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32) };
2950 N, CurDAG->getMachineNode(ARM::t2SMULL, dl, MVT::i32, MVT::i32, Ops));
2953 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
2954 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32),
2955 CurDAG->getRegister(0, MVT::i32) };
2956 ReplaceNode(N, CurDAG->getMachineNode(
2957 Subtarget->hasV6Ops() ? ARM::SMULL : ARM::SMULLv5, dl,
2958 MVT::i32, MVT::i32, Ops));
2962 case ARMISD::UMAAL: {
2963 unsigned Opc = Subtarget->isThumb() ? ARM::t2UMAAL : ARM::UMAAL;
2964 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
2965 N->getOperand(2), N->getOperand(3),
2967 CurDAG->getRegister(0, MVT::i32) };
2968 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, MVT::i32, MVT::i32, Ops));
2971 case ARMISD::UMLAL:{
2972 // UMAAL is similar to UMLAL but it adds two 32-bit values to the
2973 // 64-bit multiplication result.
2974 if (Subtarget->hasV6Ops() && N->getOperand(2).getOpcode() == ARMISD::ADDC &&
2975 N->getOperand(3).getOpcode() == ARMISD::ADDE) {
2977 SDValue Addc = N->getOperand(2);
2978 SDValue Adde = N->getOperand(3);
2980 if (Adde.getOperand(2).getNode() == Addc.getNode()) {
2982 ConstantSDNode *Op0 = dyn_cast<ConstantSDNode>(Adde.getOperand(0));
2983 ConstantSDNode *Op1 = dyn_cast<ConstantSDNode>(Adde.getOperand(1));
2985 if (Op0 && Op1 && Op0->getZExtValue() == 0 && Op1->getZExtValue() == 0)
2987 // Select UMAAL instead: UMAAL RdLo, RdHi, Rn, Rm
2988 // RdLo = one operand to be added, lower 32-bits of res
2989 // RdHi = other operand to be added, upper 32-bits of res
2990 // Rn = first multiply operand
2991 // Rm = second multiply operand
2992 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
2993 Addc.getOperand(0), Addc.getOperand(1),
2995 CurDAG->getRegister(0, MVT::i32) };
2996 unsigned opc = Subtarget->isThumb() ? ARM::t2UMAAL : ARM::UMAAL;
2997 CurDAG->SelectNodeTo(N, opc, MVT::i32, MVT::i32, Ops);
3003 if (Subtarget->isThumb()) {
3004 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2),
3005 N->getOperand(3), getAL(CurDAG, dl),
3006 CurDAG->getRegister(0, MVT::i32)};
3008 N, CurDAG->getMachineNode(ARM::t2UMLAL, dl, MVT::i32, MVT::i32, Ops));
3011 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2),
3012 N->getOperand(3), getAL(CurDAG, dl),
3013 CurDAG->getRegister(0, MVT::i32),
3014 CurDAG->getRegister(0, MVT::i32) };
3015 ReplaceNode(N, CurDAG->getMachineNode(
3016 Subtarget->hasV6Ops() ? ARM::UMLAL : ARM::UMLALv5, dl,
3017 MVT::i32, MVT::i32, Ops));
3021 case ARMISD::SMLAL:{
3022 if (Subtarget->isThumb()) {
3023 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2),
3024 N->getOperand(3), getAL(CurDAG, dl),
3025 CurDAG->getRegister(0, MVT::i32)};
3027 N, CurDAG->getMachineNode(ARM::t2SMLAL, dl, MVT::i32, MVT::i32, Ops));
3030 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2),
3031 N->getOperand(3), getAL(CurDAG, dl),
3032 CurDAG->getRegister(0, MVT::i32),
3033 CurDAG->getRegister(0, MVT::i32) };
3034 ReplaceNode(N, CurDAG->getMachineNode(
3035 Subtarget->hasV6Ops() ? ARM::SMLAL : ARM::SMLALv5, dl,
3036 MVT::i32, MVT::i32, Ops));
3041 if (Subtarget->isThumb() && Subtarget->hasThumb2()) {
3042 if (tryT2IndexedLoad(N))
3044 } else if (Subtarget->isThumb()) {
3045 if (tryT1IndexedLoad(N))
3047 } else if (tryARMIndexedLoad(N))
3049 // Other cases are autogenerated.
3052 case ARMISD::BRCOND: {
3053 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
3054 // Emits: (Bcc:void (bb:Other):$dst, (imm:i32):$cc)
3055 // Pattern complexity = 6 cost = 1 size = 0
3057 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
3058 // Emits: (tBcc:void (bb:Other):$dst, (imm:i32):$cc)
3059 // Pattern complexity = 6 cost = 1 size = 0
3061 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
3062 // Emits: (t2Bcc:void (bb:Other):$dst, (imm:i32):$cc)
3063 // Pattern complexity = 6 cost = 1 size = 0
3065 unsigned Opc = Subtarget->isThumb() ?
3066 ((Subtarget->hasThumb2()) ? ARM::t2Bcc : ARM::tBcc) : ARM::Bcc;
3067 SDValue Chain = N->getOperand(0);
3068 SDValue N1 = N->getOperand(1);
3069 SDValue N2 = N->getOperand(2);
3070 SDValue N3 = N->getOperand(3);
3071 SDValue InFlag = N->getOperand(4);
3072 assert(N1.getOpcode() == ISD::BasicBlock);
3073 assert(N2.getOpcode() == ISD::Constant);
3074 assert(N3.getOpcode() == ISD::Register);
3076 SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
3077 cast<ConstantSDNode>(N2)->getZExtValue()), dl,
3079 SDValue Ops[] = { N1, Tmp2, N3, Chain, InFlag };
3080 SDNode *ResNode = CurDAG->getMachineNode(Opc, dl, MVT::Other,
3082 Chain = SDValue(ResNode, 0);
3083 if (N->getNumValues() == 2) {
3084 InFlag = SDValue(ResNode, 1);
3085 ReplaceUses(SDValue(N, 1), InFlag);
3087 ReplaceUses(SDValue(N, 0),
3088 SDValue(Chain.getNode(), Chain.getResNo()));
3089 CurDAG->RemoveDeadNode(N);
3092 case ARMISD::VZIP: {
3094 EVT VT = N->getValueType(0);
3095 switch (VT.getSimpleVT().SimpleTy) {
3097 case MVT::v8i8: Opc = ARM::VZIPd8; break;
3098 case MVT::v4i16: Opc = ARM::VZIPd16; break;
3100 // vzip.32 Dd, Dm is a pseudo-instruction expanded to vtrn.32 Dd, Dm.
3101 case MVT::v2i32: Opc = ARM::VTRNd32; break;
3102 case MVT::v16i8: Opc = ARM::VZIPq8; break;
3103 case MVT::v8i16: Opc = ARM::VZIPq16; break;
3105 case MVT::v4i32: Opc = ARM::VZIPq32; break;
3107 SDValue Pred = getAL(CurDAG, dl);
3108 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
3109 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
3110 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, VT, VT, Ops));
3113 case ARMISD::VUZP: {
3115 EVT VT = N->getValueType(0);
3116 switch (VT.getSimpleVT().SimpleTy) {
3118 case MVT::v8i8: Opc = ARM::VUZPd8; break;
3119 case MVT::v4i16: Opc = ARM::VUZPd16; break;
3121 // vuzp.32 Dd, Dm is a pseudo-instruction expanded to vtrn.32 Dd, Dm.
3122 case MVT::v2i32: Opc = ARM::VTRNd32; break;
3123 case MVT::v16i8: Opc = ARM::VUZPq8; break;
3124 case MVT::v8i16: Opc = ARM::VUZPq16; break;
3126 case MVT::v4i32: Opc = ARM::VUZPq32; break;
3128 SDValue Pred = getAL(CurDAG, dl);
3129 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
3130 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
3131 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, VT, VT, Ops));
3134 case ARMISD::VTRN: {
3136 EVT VT = N->getValueType(0);
3137 switch (VT.getSimpleVT().SimpleTy) {
3139 case MVT::v8i8: Opc = ARM::VTRNd8; break;
3140 case MVT::v4i16: Opc = ARM::VTRNd16; break;
3142 case MVT::v2i32: Opc = ARM::VTRNd32; break;
3143 case MVT::v16i8: Opc = ARM::VTRNq8; break;
3144 case MVT::v8i16: Opc = ARM::VTRNq16; break;
3146 case MVT::v4i32: Opc = ARM::VTRNq32; 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::BUILD_VECTOR: {
3155 EVT VecVT = N->getValueType(0);
3156 EVT EltVT = VecVT.getVectorElementType();
3157 unsigned NumElts = VecVT.getVectorNumElements();
3158 if (EltVT == MVT::f64) {
3159 assert(NumElts == 2 && "unexpected type for BUILD_VECTOR");
3161 N, createDRegPairNode(VecVT, N->getOperand(0), N->getOperand(1)));
3164 assert(EltVT == MVT::f32 && "unexpected type for BUILD_VECTOR");
3167 N, createSRegPairNode(VecVT, N->getOperand(0), N->getOperand(1)));
3170 assert(NumElts == 4 && "unexpected type for BUILD_VECTOR");
3172 createQuadSRegsNode(VecVT, N->getOperand(0), N->getOperand(1),
3173 N->getOperand(2), N->getOperand(3)));
3177 case ARMISD::VLD2DUP: {
3178 static const uint16_t Opcodes[] = { ARM::VLD2DUPd8, ARM::VLD2DUPd16,
3180 SelectVLDDup(N, false, 2, Opcodes);
3184 case ARMISD::VLD3DUP: {
3185 static const uint16_t Opcodes[] = { ARM::VLD3DUPd8Pseudo,
3186 ARM::VLD3DUPd16Pseudo,
3187 ARM::VLD3DUPd32Pseudo };
3188 SelectVLDDup(N, false, 3, Opcodes);
3192 case ARMISD::VLD4DUP: {
3193 static const uint16_t Opcodes[] = { ARM::VLD4DUPd8Pseudo,
3194 ARM::VLD4DUPd16Pseudo,
3195 ARM::VLD4DUPd32Pseudo };
3196 SelectVLDDup(N, false, 4, Opcodes);
3200 case ARMISD::VLD2DUP_UPD: {
3201 static const uint16_t Opcodes[] = { ARM::VLD2DUPd8wb_fixed,
3202 ARM::VLD2DUPd16wb_fixed,
3203 ARM::VLD2DUPd32wb_fixed };
3204 SelectVLDDup(N, true, 2, Opcodes);
3208 case ARMISD::VLD3DUP_UPD: {
3209 static const uint16_t Opcodes[] = { ARM::VLD3DUPd8Pseudo_UPD,
3210 ARM::VLD3DUPd16Pseudo_UPD,
3211 ARM::VLD3DUPd32Pseudo_UPD };
3212 SelectVLDDup(N, true, 3, Opcodes);
3216 case ARMISD::VLD4DUP_UPD: {
3217 static const uint16_t Opcodes[] = { ARM::VLD4DUPd8Pseudo_UPD,
3218 ARM::VLD4DUPd16Pseudo_UPD,
3219 ARM::VLD4DUPd32Pseudo_UPD };
3220 SelectVLDDup(N, true, 4, Opcodes);
3224 case ARMISD::VLD1_UPD: {
3225 static const uint16_t DOpcodes[] = { ARM::VLD1d8wb_fixed,
3226 ARM::VLD1d16wb_fixed,
3227 ARM::VLD1d32wb_fixed,
3228 ARM::VLD1d64wb_fixed };
3229 static const uint16_t QOpcodes[] = { ARM::VLD1q8wb_fixed,
3230 ARM::VLD1q16wb_fixed,
3231 ARM::VLD1q32wb_fixed,
3232 ARM::VLD1q64wb_fixed };
3233 SelectVLD(N, true, 1, DOpcodes, QOpcodes, nullptr);
3237 case ARMISD::VLD2_UPD: {
3238 static const uint16_t DOpcodes[] = { ARM::VLD2d8wb_fixed,
3239 ARM::VLD2d16wb_fixed,
3240 ARM::VLD2d32wb_fixed,
3241 ARM::VLD1q64wb_fixed};
3242 static const uint16_t QOpcodes[] = { ARM::VLD2q8PseudoWB_fixed,
3243 ARM::VLD2q16PseudoWB_fixed,
3244 ARM::VLD2q32PseudoWB_fixed };
3245 SelectVLD(N, true, 2, DOpcodes, QOpcodes, nullptr);
3249 case ARMISD::VLD3_UPD: {
3250 static const uint16_t DOpcodes[] = { ARM::VLD3d8Pseudo_UPD,
3251 ARM::VLD3d16Pseudo_UPD,
3252 ARM::VLD3d32Pseudo_UPD,
3253 ARM::VLD1d64TPseudoWB_fixed};
3254 static const uint16_t QOpcodes0[] = { ARM::VLD3q8Pseudo_UPD,
3255 ARM::VLD3q16Pseudo_UPD,
3256 ARM::VLD3q32Pseudo_UPD };
3257 static const uint16_t QOpcodes1[] = { ARM::VLD3q8oddPseudo_UPD,
3258 ARM::VLD3q16oddPseudo_UPD,
3259 ARM::VLD3q32oddPseudo_UPD };
3260 SelectVLD(N, true, 3, DOpcodes, QOpcodes0, QOpcodes1);
3264 case ARMISD::VLD4_UPD: {
3265 static const uint16_t DOpcodes[] = { ARM::VLD4d8Pseudo_UPD,
3266 ARM::VLD4d16Pseudo_UPD,
3267 ARM::VLD4d32Pseudo_UPD,
3268 ARM::VLD1d64QPseudoWB_fixed};
3269 static const uint16_t QOpcodes0[] = { ARM::VLD4q8Pseudo_UPD,
3270 ARM::VLD4q16Pseudo_UPD,
3271 ARM::VLD4q32Pseudo_UPD };
3272 static const uint16_t QOpcodes1[] = { ARM::VLD4q8oddPseudo_UPD,
3273 ARM::VLD4q16oddPseudo_UPD,
3274 ARM::VLD4q32oddPseudo_UPD };
3275 SelectVLD(N, true, 4, DOpcodes, QOpcodes0, QOpcodes1);
3279 case ARMISD::VLD2LN_UPD: {
3280 static const uint16_t DOpcodes[] = { ARM::VLD2LNd8Pseudo_UPD,
3281 ARM::VLD2LNd16Pseudo_UPD,
3282 ARM::VLD2LNd32Pseudo_UPD };
3283 static const uint16_t QOpcodes[] = { ARM::VLD2LNq16Pseudo_UPD,
3284 ARM::VLD2LNq32Pseudo_UPD };
3285 SelectVLDSTLane(N, true, true, 2, DOpcodes, QOpcodes);
3289 case ARMISD::VLD3LN_UPD: {
3290 static const uint16_t DOpcodes[] = { ARM::VLD3LNd8Pseudo_UPD,
3291 ARM::VLD3LNd16Pseudo_UPD,
3292 ARM::VLD3LNd32Pseudo_UPD };
3293 static const uint16_t QOpcodes[] = { ARM::VLD3LNq16Pseudo_UPD,
3294 ARM::VLD3LNq32Pseudo_UPD };
3295 SelectVLDSTLane(N, true, true, 3, DOpcodes, QOpcodes);
3299 case ARMISD::VLD4LN_UPD: {
3300 static const uint16_t DOpcodes[] = { ARM::VLD4LNd8Pseudo_UPD,
3301 ARM::VLD4LNd16Pseudo_UPD,
3302 ARM::VLD4LNd32Pseudo_UPD };
3303 static const uint16_t QOpcodes[] = { ARM::VLD4LNq16Pseudo_UPD,
3304 ARM::VLD4LNq32Pseudo_UPD };
3305 SelectVLDSTLane(N, true, true, 4, DOpcodes, QOpcodes);
3309 case ARMISD::VST1_UPD: {
3310 static const uint16_t DOpcodes[] = { ARM::VST1d8wb_fixed,
3311 ARM::VST1d16wb_fixed,
3312 ARM::VST1d32wb_fixed,
3313 ARM::VST1d64wb_fixed };
3314 static const uint16_t QOpcodes[] = { ARM::VST1q8wb_fixed,
3315 ARM::VST1q16wb_fixed,
3316 ARM::VST1q32wb_fixed,
3317 ARM::VST1q64wb_fixed };
3318 SelectVST(N, true, 1, DOpcodes, QOpcodes, nullptr);
3322 case ARMISD::VST2_UPD: {
3323 static const uint16_t DOpcodes[] = { ARM::VST2d8wb_fixed,
3324 ARM::VST2d16wb_fixed,
3325 ARM::VST2d32wb_fixed,
3326 ARM::VST1q64wb_fixed};
3327 static const uint16_t QOpcodes[] = { ARM::VST2q8PseudoWB_fixed,
3328 ARM::VST2q16PseudoWB_fixed,
3329 ARM::VST2q32PseudoWB_fixed };
3330 SelectVST(N, true, 2, DOpcodes, QOpcodes, nullptr);
3334 case ARMISD::VST3_UPD: {
3335 static const uint16_t DOpcodes[] = { ARM::VST3d8Pseudo_UPD,
3336 ARM::VST3d16Pseudo_UPD,
3337 ARM::VST3d32Pseudo_UPD,
3338 ARM::VST1d64TPseudoWB_fixed};
3339 static const uint16_t QOpcodes0[] = { ARM::VST3q8Pseudo_UPD,
3340 ARM::VST3q16Pseudo_UPD,
3341 ARM::VST3q32Pseudo_UPD };
3342 static const uint16_t QOpcodes1[] = { ARM::VST3q8oddPseudo_UPD,
3343 ARM::VST3q16oddPseudo_UPD,
3344 ARM::VST3q32oddPseudo_UPD };
3345 SelectVST(N, true, 3, DOpcodes, QOpcodes0, QOpcodes1);
3349 case ARMISD::VST4_UPD: {
3350 static const uint16_t DOpcodes[] = { ARM::VST4d8Pseudo_UPD,
3351 ARM::VST4d16Pseudo_UPD,
3352 ARM::VST4d32Pseudo_UPD,
3353 ARM::VST1d64QPseudoWB_fixed};
3354 static const uint16_t QOpcodes0[] = { ARM::VST4q8Pseudo_UPD,
3355 ARM::VST4q16Pseudo_UPD,
3356 ARM::VST4q32Pseudo_UPD };
3357 static const uint16_t QOpcodes1[] = { ARM::VST4q8oddPseudo_UPD,
3358 ARM::VST4q16oddPseudo_UPD,
3359 ARM::VST4q32oddPseudo_UPD };
3360 SelectVST(N, true, 4, DOpcodes, QOpcodes0, QOpcodes1);
3364 case ARMISD::VST2LN_UPD: {
3365 static const uint16_t DOpcodes[] = { ARM::VST2LNd8Pseudo_UPD,
3366 ARM::VST2LNd16Pseudo_UPD,
3367 ARM::VST2LNd32Pseudo_UPD };
3368 static const uint16_t QOpcodes[] = { ARM::VST2LNq16Pseudo_UPD,
3369 ARM::VST2LNq32Pseudo_UPD };
3370 SelectVLDSTLane(N, false, true, 2, DOpcodes, QOpcodes);
3374 case ARMISD::VST3LN_UPD: {
3375 static const uint16_t DOpcodes[] = { ARM::VST3LNd8Pseudo_UPD,
3376 ARM::VST3LNd16Pseudo_UPD,
3377 ARM::VST3LNd32Pseudo_UPD };
3378 static const uint16_t QOpcodes[] = { ARM::VST3LNq16Pseudo_UPD,
3379 ARM::VST3LNq32Pseudo_UPD };
3380 SelectVLDSTLane(N, false, true, 3, DOpcodes, QOpcodes);
3384 case ARMISD::VST4LN_UPD: {
3385 static const uint16_t DOpcodes[] = { ARM::VST4LNd8Pseudo_UPD,
3386 ARM::VST4LNd16Pseudo_UPD,
3387 ARM::VST4LNd32Pseudo_UPD };
3388 static const uint16_t QOpcodes[] = { ARM::VST4LNq16Pseudo_UPD,
3389 ARM::VST4LNq32Pseudo_UPD };
3390 SelectVLDSTLane(N, false, true, 4, DOpcodes, QOpcodes);
3394 case ISD::INTRINSIC_VOID:
3395 case ISD::INTRINSIC_W_CHAIN: {
3396 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
3401 case Intrinsic::arm_mrrc:
3402 case Intrinsic::arm_mrrc2: {
3404 SDValue Chain = N->getOperand(0);
3407 if (Subtarget->isThumb())
3408 Opc = (IntNo == Intrinsic::arm_mrrc ? ARM::t2MRRC : ARM::t2MRRC2);
3410 Opc = (IntNo == Intrinsic::arm_mrrc ? ARM::MRRC : ARM::MRRC2);
3412 SmallVector<SDValue, 5> Ops;
3413 Ops.push_back(getI32Imm(cast<ConstantSDNode>(N->getOperand(2))->getZExtValue(), dl)); /* coproc */
3414 Ops.push_back(getI32Imm(cast<ConstantSDNode>(N->getOperand(3))->getZExtValue(), dl)); /* opc */
3415 Ops.push_back(getI32Imm(cast<ConstantSDNode>(N->getOperand(4))->getZExtValue(), dl)); /* CRm */
3417 // The mrrc2 instruction in ARM doesn't allow predicates, the top 4 bits of the encoded
3418 // instruction will always be '1111' but it is possible in assembly language to specify
3419 // AL as a predicate to mrrc2 but it doesn't make any difference to the encoded instruction.
3420 if (Opc != ARM::MRRC2) {
3421 Ops.push_back(getAL(CurDAG, dl));
3422 Ops.push_back(CurDAG->getRegister(0, MVT::i32));
3425 Ops.push_back(Chain);
3427 // Writes to two registers.
3428 const EVT RetType[] = {MVT::i32, MVT::i32, MVT::Other};
3430 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, RetType, Ops));
3433 case Intrinsic::arm_ldaexd:
3434 case Intrinsic::arm_ldrexd: {
3436 SDValue Chain = N->getOperand(0);
3437 SDValue MemAddr = N->getOperand(2);
3438 bool isThumb = Subtarget->isThumb() && Subtarget->hasV8MBaselineOps();
3440 bool IsAcquire = IntNo == Intrinsic::arm_ldaexd;
3441 unsigned NewOpc = isThumb ? (IsAcquire ? ARM::t2LDAEXD : ARM::t2LDREXD)
3442 : (IsAcquire ? ARM::LDAEXD : ARM::LDREXD);
3444 // arm_ldrexd returns a i64 value in {i32, i32}
3445 std::vector<EVT> ResTys;
3447 ResTys.push_back(MVT::i32);
3448 ResTys.push_back(MVT::i32);
3450 ResTys.push_back(MVT::Untyped);
3451 ResTys.push_back(MVT::Other);
3453 // Place arguments in the right order.
3454 SDValue Ops[] = {MemAddr, getAL(CurDAG, dl),
3455 CurDAG->getRegister(0, MVT::i32), Chain};
3456 SDNode *Ld = CurDAG->getMachineNode(NewOpc, dl, ResTys, Ops);
3457 // Transfer memoperands.
3458 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
3459 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
3460 cast<MachineSDNode>(Ld)->setMemRefs(MemOp, MemOp + 1);
3463 SDValue OutChain = isThumb ? SDValue(Ld, 2) : SDValue(Ld, 1);
3464 if (!SDValue(N, 0).use_empty()) {
3467 Result = SDValue(Ld, 0);
3470 CurDAG->getTargetConstant(ARM::gsub_0, dl, MVT::i32);
3471 SDNode *ResNode = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
3472 dl, MVT::i32, SDValue(Ld, 0), SubRegIdx);
3473 Result = SDValue(ResNode,0);
3475 ReplaceUses(SDValue(N, 0), Result);
3477 if (!SDValue(N, 1).use_empty()) {
3480 Result = SDValue(Ld, 1);
3483 CurDAG->getTargetConstant(ARM::gsub_1, dl, MVT::i32);
3484 SDNode *ResNode = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
3485 dl, MVT::i32, SDValue(Ld, 0), SubRegIdx);
3486 Result = SDValue(ResNode,0);
3488 ReplaceUses(SDValue(N, 1), Result);
3490 ReplaceUses(SDValue(N, 2), OutChain);
3491 CurDAG->RemoveDeadNode(N);
3494 case Intrinsic::arm_stlexd:
3495 case Intrinsic::arm_strexd: {
3497 SDValue Chain = N->getOperand(0);
3498 SDValue Val0 = N->getOperand(2);
3499 SDValue Val1 = N->getOperand(3);
3500 SDValue MemAddr = N->getOperand(4);
3502 // Store exclusive double return a i32 value which is the return status
3503 // of the issued store.
3504 const EVT ResTys[] = {MVT::i32, MVT::Other};
3506 bool isThumb = Subtarget->isThumb() && Subtarget->hasThumb2();
3507 // Place arguments in the right order.
3508 SmallVector<SDValue, 7> Ops;
3510 Ops.push_back(Val0);
3511 Ops.push_back(Val1);
3513 // arm_strexd uses GPRPair.
3514 Ops.push_back(SDValue(createGPRPairNode(MVT::Untyped, Val0, Val1), 0));
3515 Ops.push_back(MemAddr);
3516 Ops.push_back(getAL(CurDAG, dl));
3517 Ops.push_back(CurDAG->getRegister(0, MVT::i32));
3518 Ops.push_back(Chain);
3520 bool IsRelease = IntNo == Intrinsic::arm_stlexd;
3521 unsigned NewOpc = isThumb ? (IsRelease ? ARM::t2STLEXD : ARM::t2STREXD)
3522 : (IsRelease ? ARM::STLEXD : ARM::STREXD);
3524 SDNode *St = CurDAG->getMachineNode(NewOpc, dl, ResTys, Ops);
3525 // Transfer memoperands.
3526 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
3527 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
3528 cast<MachineSDNode>(St)->setMemRefs(MemOp, MemOp + 1);
3534 case Intrinsic::arm_neon_vld1: {
3535 static const uint16_t DOpcodes[] = { ARM::VLD1d8, ARM::VLD1d16,
3536 ARM::VLD1d32, ARM::VLD1d64 };
3537 static const uint16_t QOpcodes[] = { ARM::VLD1q8, ARM::VLD1q16,
3538 ARM::VLD1q32, ARM::VLD1q64};
3539 SelectVLD(N, false, 1, DOpcodes, QOpcodes, nullptr);
3543 case Intrinsic::arm_neon_vld2: {
3544 static const uint16_t DOpcodes[] = { ARM::VLD2d8, ARM::VLD2d16,
3545 ARM::VLD2d32, ARM::VLD1q64 };
3546 static const uint16_t QOpcodes[] = { ARM::VLD2q8Pseudo, ARM::VLD2q16Pseudo,
3547 ARM::VLD2q32Pseudo };
3548 SelectVLD(N, false, 2, DOpcodes, QOpcodes, nullptr);
3552 case Intrinsic::arm_neon_vld3: {
3553 static const uint16_t DOpcodes[] = { ARM::VLD3d8Pseudo,
3556 ARM::VLD1d64TPseudo };
3557 static const uint16_t QOpcodes0[] = { ARM::VLD3q8Pseudo_UPD,
3558 ARM::VLD3q16Pseudo_UPD,
3559 ARM::VLD3q32Pseudo_UPD };
3560 static const uint16_t QOpcodes1[] = { ARM::VLD3q8oddPseudo,
3561 ARM::VLD3q16oddPseudo,
3562 ARM::VLD3q32oddPseudo };
3563 SelectVLD(N, false, 3, DOpcodes, QOpcodes0, QOpcodes1);
3567 case Intrinsic::arm_neon_vld4: {
3568 static const uint16_t DOpcodes[] = { ARM::VLD4d8Pseudo,
3571 ARM::VLD1d64QPseudo };
3572 static const uint16_t QOpcodes0[] = { ARM::VLD4q8Pseudo_UPD,
3573 ARM::VLD4q16Pseudo_UPD,
3574 ARM::VLD4q32Pseudo_UPD };
3575 static const uint16_t QOpcodes1[] = { ARM::VLD4q8oddPseudo,
3576 ARM::VLD4q16oddPseudo,
3577 ARM::VLD4q32oddPseudo };
3578 SelectVLD(N, false, 4, DOpcodes, QOpcodes0, QOpcodes1);
3582 case Intrinsic::arm_neon_vld2lane: {
3583 static const uint16_t DOpcodes[] = { ARM::VLD2LNd8Pseudo,
3584 ARM::VLD2LNd16Pseudo,
3585 ARM::VLD2LNd32Pseudo };
3586 static const uint16_t QOpcodes[] = { ARM::VLD2LNq16Pseudo,
3587 ARM::VLD2LNq32Pseudo };
3588 SelectVLDSTLane(N, true, false, 2, DOpcodes, QOpcodes);
3592 case Intrinsic::arm_neon_vld3lane: {
3593 static const uint16_t DOpcodes[] = { ARM::VLD3LNd8Pseudo,
3594 ARM::VLD3LNd16Pseudo,
3595 ARM::VLD3LNd32Pseudo };
3596 static const uint16_t QOpcodes[] = { ARM::VLD3LNq16Pseudo,
3597 ARM::VLD3LNq32Pseudo };
3598 SelectVLDSTLane(N, true, false, 3, DOpcodes, QOpcodes);
3602 case Intrinsic::arm_neon_vld4lane: {
3603 static const uint16_t DOpcodes[] = { ARM::VLD4LNd8Pseudo,
3604 ARM::VLD4LNd16Pseudo,
3605 ARM::VLD4LNd32Pseudo };
3606 static const uint16_t QOpcodes[] = { ARM::VLD4LNq16Pseudo,
3607 ARM::VLD4LNq32Pseudo };
3608 SelectVLDSTLane(N, true, false, 4, DOpcodes, QOpcodes);
3612 case Intrinsic::arm_neon_vst1: {
3613 static const uint16_t DOpcodes[] = { ARM::VST1d8, ARM::VST1d16,
3614 ARM::VST1d32, ARM::VST1d64 };
3615 static const uint16_t QOpcodes[] = { ARM::VST1q8, ARM::VST1q16,
3616 ARM::VST1q32, ARM::VST1q64 };
3617 SelectVST(N, false, 1, DOpcodes, QOpcodes, nullptr);
3621 case Intrinsic::arm_neon_vst2: {
3622 static const uint16_t DOpcodes[] = { ARM::VST2d8, ARM::VST2d16,
3623 ARM::VST2d32, ARM::VST1q64 };
3624 static const uint16_t QOpcodes[] = { ARM::VST2q8Pseudo, ARM::VST2q16Pseudo,
3625 ARM::VST2q32Pseudo };
3626 SelectVST(N, false, 2, DOpcodes, QOpcodes, nullptr);
3630 case Intrinsic::arm_neon_vst3: {
3631 static const uint16_t DOpcodes[] = { ARM::VST3d8Pseudo,
3634 ARM::VST1d64TPseudo };
3635 static const uint16_t QOpcodes0[] = { ARM::VST3q8Pseudo_UPD,
3636 ARM::VST3q16Pseudo_UPD,
3637 ARM::VST3q32Pseudo_UPD };
3638 static const uint16_t QOpcodes1[] = { ARM::VST3q8oddPseudo,
3639 ARM::VST3q16oddPseudo,
3640 ARM::VST3q32oddPseudo };
3641 SelectVST(N, false, 3, DOpcodes, QOpcodes0, QOpcodes1);
3645 case Intrinsic::arm_neon_vst4: {
3646 static const uint16_t DOpcodes[] = { ARM::VST4d8Pseudo,
3649 ARM::VST1d64QPseudo };
3650 static const uint16_t QOpcodes0[] = { ARM::VST4q8Pseudo_UPD,
3651 ARM::VST4q16Pseudo_UPD,
3652 ARM::VST4q32Pseudo_UPD };
3653 static const uint16_t QOpcodes1[] = { ARM::VST4q8oddPseudo,
3654 ARM::VST4q16oddPseudo,
3655 ARM::VST4q32oddPseudo };
3656 SelectVST(N, false, 4, DOpcodes, QOpcodes0, QOpcodes1);
3660 case Intrinsic::arm_neon_vst2lane: {
3661 static const uint16_t DOpcodes[] = { ARM::VST2LNd8Pseudo,
3662 ARM::VST2LNd16Pseudo,
3663 ARM::VST2LNd32Pseudo };
3664 static const uint16_t QOpcodes[] = { ARM::VST2LNq16Pseudo,
3665 ARM::VST2LNq32Pseudo };
3666 SelectVLDSTLane(N, false, false, 2, DOpcodes, QOpcodes);
3670 case Intrinsic::arm_neon_vst3lane: {
3671 static const uint16_t DOpcodes[] = { ARM::VST3LNd8Pseudo,
3672 ARM::VST3LNd16Pseudo,
3673 ARM::VST3LNd32Pseudo };
3674 static const uint16_t QOpcodes[] = { ARM::VST3LNq16Pseudo,
3675 ARM::VST3LNq32Pseudo };
3676 SelectVLDSTLane(N, false, false, 3, DOpcodes, QOpcodes);
3680 case Intrinsic::arm_neon_vst4lane: {
3681 static const uint16_t DOpcodes[] = { ARM::VST4LNd8Pseudo,
3682 ARM::VST4LNd16Pseudo,
3683 ARM::VST4LNd32Pseudo };
3684 static const uint16_t QOpcodes[] = { ARM::VST4LNq16Pseudo,
3685 ARM::VST4LNq32Pseudo };
3686 SelectVLDSTLane(N, false, false, 4, DOpcodes, QOpcodes);
3693 case ISD::INTRINSIC_WO_CHAIN: {
3694 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3699 case Intrinsic::arm_neon_vtbl2:
3700 SelectVTBL(N, false, 2, ARM::VTBL2);
3702 case Intrinsic::arm_neon_vtbl3:
3703 SelectVTBL(N, false, 3, ARM::VTBL3Pseudo);
3705 case Intrinsic::arm_neon_vtbl4:
3706 SelectVTBL(N, false, 4, ARM::VTBL4Pseudo);
3709 case Intrinsic::arm_neon_vtbx2:
3710 SelectVTBL(N, true, 2, ARM::VTBX2);
3712 case Intrinsic::arm_neon_vtbx3:
3713 SelectVTBL(N, true, 3, ARM::VTBX3Pseudo);
3715 case Intrinsic::arm_neon_vtbx4:
3716 SelectVTBL(N, true, 4, ARM::VTBX4Pseudo);
3722 case ARMISD::VTBL1: {
3724 EVT VT = N->getValueType(0);
3725 SDValue Ops[] = {N->getOperand(0), N->getOperand(1),
3726 getAL(CurDAG, dl), // Predicate
3727 CurDAG->getRegister(0, MVT::i32)}; // Predicate Register
3728 ReplaceNode(N, CurDAG->getMachineNode(ARM::VTBL1, dl, VT, Ops));
3731 case ARMISD::VTBL2: {
3733 EVT VT = N->getValueType(0);
3735 // Form a REG_SEQUENCE to force register allocation.
3736 SDValue V0 = N->getOperand(0);
3737 SDValue V1 = N->getOperand(1);
3738 SDValue RegSeq = SDValue(createDRegPairNode(MVT::v16i8, V0, V1), 0);
3740 SDValue Ops[] = {RegSeq, N->getOperand(2), getAL(CurDAG, dl), // Predicate
3741 CurDAG->getRegister(0, MVT::i32)}; // Predicate Register
3742 ReplaceNode(N, CurDAG->getMachineNode(ARM::VTBL2, dl, VT, Ops));
3746 case ISD::CONCAT_VECTORS:
3747 SelectConcatVector(N);
3750 case ISD::ATOMIC_CMP_SWAP:
3758 // Inspect a register string of the form
3759 // cp<coprocessor>:<opc1>:c<CRn>:c<CRm>:<opc2> (32bit) or
3760 // cp<coprocessor>:<opc1>:c<CRm> (64bit) inspect the fields of the string
3761 // and obtain the integer operands from them, adding these operands to the
3763 static void getIntOperandsFromRegisterString(StringRef RegString,
3764 SelectionDAG *CurDAG,
3766 std::vector<SDValue> &Ops) {
3767 SmallVector<StringRef, 5> Fields;
3768 RegString.split(Fields, ':');
3770 if (Fields.size() > 1) {
3771 bool AllIntFields = true;
3773 for (StringRef Field : Fields) {
3774 // Need to trim out leading 'cp' characters and get the integer field.
3776 AllIntFields &= !Field.trim("CPcp").getAsInteger(10, IntField);
3777 Ops.push_back(CurDAG->getTargetConstant(IntField, DL, MVT::i32));
3780 assert(AllIntFields &&
3781 "Unexpected non-integer value in special register string.");
3785 // Maps a Banked Register string to its mask value. The mask value returned is
3786 // for use in the MRSbanked / MSRbanked instruction nodes as the Banked Register
3787 // mask operand, which expresses which register is to be used, e.g. r8, and in
3788 // which mode it is to be used, e.g. usr. Returns -1 to signify that the string
3790 static inline int getBankedRegisterMask(StringRef RegString) {
3791 return StringSwitch<int>(RegString.lower())
3792 .Case("r8_usr", 0x00)
3793 .Case("r9_usr", 0x01)
3794 .Case("r10_usr", 0x02)
3795 .Case("r11_usr", 0x03)
3796 .Case("r12_usr", 0x04)
3797 .Case("sp_usr", 0x05)
3798 .Case("lr_usr", 0x06)
3799 .Case("r8_fiq", 0x08)
3800 .Case("r9_fiq", 0x09)
3801 .Case("r10_fiq", 0x0a)
3802 .Case("r11_fiq", 0x0b)
3803 .Case("r12_fiq", 0x0c)
3804 .Case("sp_fiq", 0x0d)
3805 .Case("lr_fiq", 0x0e)
3806 .Case("lr_irq", 0x10)
3807 .Case("sp_irq", 0x11)
3808 .Case("lr_svc", 0x12)
3809 .Case("sp_svc", 0x13)
3810 .Case("lr_abt", 0x14)
3811 .Case("sp_abt", 0x15)
3812 .Case("lr_und", 0x16)
3813 .Case("sp_und", 0x17)
3814 .Case("lr_mon", 0x1c)
3815 .Case("sp_mon", 0x1d)
3816 .Case("elr_hyp", 0x1e)
3817 .Case("sp_hyp", 0x1f)
3818 .Case("spsr_fiq", 0x2e)
3819 .Case("spsr_irq", 0x30)
3820 .Case("spsr_svc", 0x32)
3821 .Case("spsr_abt", 0x34)
3822 .Case("spsr_und", 0x36)
3823 .Case("spsr_mon", 0x3c)
3824 .Case("spsr_hyp", 0x3e)
3828 // Maps a MClass special register string to its value for use in the
3829 // t2MRS_M / t2MSR_M instruction nodes as the SYSm value operand.
3830 // Returns -1 to signify that the string was invalid.
3831 static inline int getMClassRegisterSYSmValueMask(StringRef RegString) {
3832 return StringSwitch<int>(RegString.lower())
3842 .Case("primask", 0x10)
3843 .Case("basepri", 0x11)
3844 .Case("basepri_max", 0x12)
3845 .Case("faultmask", 0x13)
3846 .Case("control", 0x14)
3847 .Case("msplim", 0x0a)
3848 .Case("psplim", 0x0b)
3853 // The flags here are common to those allowed for apsr in the A class cores and
3854 // those allowed for the special registers in the M class cores. Returns a
3855 // value representing which flags were present, -1 if invalid.
3856 static inline int getMClassFlagsMask(StringRef Flags, bool hasDSP) {
3858 return 0x2 | (int)hasDSP;
3860 return StringSwitch<int>(Flags)
3863 .Case("nzcvqg", 0x3)
3867 static int getMClassRegisterMask(StringRef Reg, StringRef Flags, bool IsRead,
3868 const ARMSubtarget *Subtarget) {
3869 // Ensure that the register (without flags) was a valid M Class special
3871 int SYSmvalue = getMClassRegisterSYSmValueMask(Reg);
3872 if (SYSmvalue == -1)
3875 // basepri, basepri_max and faultmask are only valid for V7m.
3876 if (!Subtarget->hasV7Ops() && SYSmvalue >= 0x11 && SYSmvalue <= 0x13)
3879 if (Subtarget->has8MSecExt() && Flags.lower() == "ns") {
3884 if (!Subtarget->has8MSecExt() &&
3885 (SYSmvalue == 0xa || SYSmvalue == 0xb || SYSmvalue > 0x14))
3888 if (!Subtarget->hasV8MMainlineOps() &&
3889 (SYSmvalue == 0x8a || SYSmvalue == 0x8b || SYSmvalue == 0x91 ||
3893 // If it was a read then we won't be expecting flags and so at this point
3894 // we can return the mask.
3902 // We know we are now handling a write so need to get the mask for the flags.
3903 int Mask = getMClassFlagsMask(Flags, Subtarget->hasDSP());
3905 // Only apsr, iapsr, eapsr, xpsr can have flags. The other register values
3906 // shouldn't have flags present.
3907 if ((SYSmvalue < 0x4 && Mask == -1) || (SYSmvalue > 0x4 && !Flags.empty()))
3910 // The _g and _nzcvqg versions are only valid if the DSP extension is
3912 if (!Subtarget->hasDSP() && (Mask & 0x1))
3915 // The register was valid so need to put the mask in the correct place
3916 // (the flags need to be in bits 11-10) and combine with the SYSmvalue to
3917 // construct the operand for the instruction node.
3918 if (SYSmvalue < 0x4)
3919 return SYSmvalue | Mask << 10;
3924 static int getARClassRegisterMask(StringRef Reg, StringRef Flags) {
3925 // The mask operand contains the special register (R Bit) in bit 4, whether
3926 // the register is spsr (R bit is 1) or one of cpsr/apsr (R bit is 0), and
3927 // bits 3-0 contains the fields to be accessed in the special register, set by
3928 // the flags provided with the register.
3930 if (Reg == "apsr") {
3931 // The flags permitted for apsr are the same flags that are allowed in
3932 // M class registers. We get the flag value and then shift the flags into
3933 // the correct place to combine with the mask.
3934 Mask = getMClassFlagsMask(Flags, true);
3940 if (Reg != "cpsr" && Reg != "spsr") {
3944 // This is the same as if the flags were "fc"
3945 if (Flags.empty() || Flags == "all")
3948 // Inspect the supplied flags string and set the bits in the mask for
3949 // the relevant and valid flags allowed for cpsr and spsr.
3950 for (char Flag : Flags) {
3969 // This avoids allowing strings where the same flag bit appears twice.
3970 if (!FlagVal || (Mask & FlagVal))
3975 // If the register is spsr then we need to set the R bit.
3982 // Lower the read_register intrinsic to ARM specific DAG nodes
3983 // using the supplied metadata string to select the instruction node to use
3984 // and the registers/masks to construct as operands for the node.
3985 bool ARMDAGToDAGISel::tryReadRegister(SDNode *N){
3986 const MDNodeSDNode *MD = dyn_cast<MDNodeSDNode>(N->getOperand(1));
3987 const MDString *RegString = dyn_cast<MDString>(MD->getMD()->getOperand(0));
3988 bool IsThumb2 = Subtarget->isThumb2();
3991 std::vector<SDValue> Ops;
3992 getIntOperandsFromRegisterString(RegString->getString(), CurDAG, DL, Ops);
3995 // If the special register string was constructed of fields (as defined
3996 // in the ACLE) then need to lower to MRC node (32 bit) or
3997 // MRRC node(64 bit), we can make the distinction based on the number of
3998 // operands we have.
4000 SmallVector<EVT, 3> ResTypes;
4001 if (Ops.size() == 5){
4002 Opcode = IsThumb2 ? ARM::t2MRC : ARM::MRC;
4003 ResTypes.append({ MVT::i32, MVT::Other });
4005 assert(Ops.size() == 3 &&
4006 "Invalid number of fields in special register string.");
4007 Opcode = IsThumb2 ? ARM::t2MRRC : ARM::MRRC;
4008 ResTypes.append({ MVT::i32, MVT::i32, MVT::Other });
4011 Ops.push_back(getAL(CurDAG, DL));
4012 Ops.push_back(CurDAG->getRegister(0, MVT::i32));
4013 Ops.push_back(N->getOperand(0));
4014 ReplaceNode(N, CurDAG->getMachineNode(Opcode, DL, ResTypes, Ops));
4018 std::string SpecialReg = RegString->getString().lower();
4020 int BankedReg = getBankedRegisterMask(SpecialReg);
4021 if (BankedReg != -1) {
4022 Ops = { CurDAG->getTargetConstant(BankedReg, DL, MVT::i32),
4023 getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
4026 N, CurDAG->getMachineNode(IsThumb2 ? ARM::t2MRSbanked : ARM::MRSbanked,
4027 DL, MVT::i32, MVT::Other, Ops));
4031 // The VFP registers are read by creating SelectionDAG nodes with opcodes
4032 // corresponding to the register that is being read from. So we switch on the
4033 // string to find which opcode we need to use.
4034 unsigned Opcode = StringSwitch<unsigned>(SpecialReg)
4035 .Case("fpscr", ARM::VMRS)
4036 .Case("fpexc", ARM::VMRS_FPEXC)
4037 .Case("fpsid", ARM::VMRS_FPSID)
4038 .Case("mvfr0", ARM::VMRS_MVFR0)
4039 .Case("mvfr1", ARM::VMRS_MVFR1)
4040 .Case("mvfr2", ARM::VMRS_MVFR2)
4041 .Case("fpinst", ARM::VMRS_FPINST)
4042 .Case("fpinst2", ARM::VMRS_FPINST2)
4045 // If an opcode was found then we can lower the read to a VFP instruction.
4047 if (!Subtarget->hasVFP2())
4049 if (Opcode == ARM::VMRS_MVFR2 && !Subtarget->hasFPARMv8())
4052 Ops = { getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
4055 CurDAG->getMachineNode(Opcode, DL, MVT::i32, MVT::Other, Ops));
4059 // If the target is M Class then need to validate that the register string
4060 // is an acceptable value, so check that a mask can be constructed from the
4062 if (Subtarget->isMClass()) {
4063 StringRef Flags = "", Reg = SpecialReg;
4064 if (Reg.endswith("_ns")) {
4066 Reg = Reg.drop_back(3);
4069 int SYSmValue = getMClassRegisterMask(Reg, Flags, true, Subtarget);
4070 if (SYSmValue == -1)
4073 SDValue Ops[] = { CurDAG->getTargetConstant(SYSmValue, DL, MVT::i32),
4074 getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
4077 N, CurDAG->getMachineNode(ARM::t2MRS_M, DL, MVT::i32, MVT::Other, Ops));
4081 // Here we know the target is not M Class so we need to check if it is one
4082 // of the remaining possible values which are apsr, cpsr or spsr.
4083 if (SpecialReg == "apsr" || SpecialReg == "cpsr") {
4084 Ops = { getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
4086 ReplaceNode(N, CurDAG->getMachineNode(IsThumb2 ? ARM::t2MRS_AR : ARM::MRS,
4087 DL, MVT::i32, MVT::Other, Ops));
4091 if (SpecialReg == "spsr") {
4092 Ops = { getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
4095 N, CurDAG->getMachineNode(IsThumb2 ? ARM::t2MRSsys_AR : ARM::MRSsys, DL,
4096 MVT::i32, MVT::Other, Ops));
4103 // Lower the write_register intrinsic to ARM specific DAG nodes
4104 // using the supplied metadata string to select the instruction node to use
4105 // and the registers/masks to use in the nodes
4106 bool ARMDAGToDAGISel::tryWriteRegister(SDNode *N){
4107 const MDNodeSDNode *MD = dyn_cast<MDNodeSDNode>(N->getOperand(1));
4108 const MDString *RegString = dyn_cast<MDString>(MD->getMD()->getOperand(0));
4109 bool IsThumb2 = Subtarget->isThumb2();
4112 std::vector<SDValue> Ops;
4113 getIntOperandsFromRegisterString(RegString->getString(), CurDAG, DL, Ops);
4116 // If the special register string was constructed of fields (as defined
4117 // in the ACLE) then need to lower to MCR node (32 bit) or
4118 // MCRR node(64 bit), we can make the distinction based on the number of
4119 // operands we have.
4121 if (Ops.size() == 5) {
4122 Opcode = IsThumb2 ? ARM::t2MCR : ARM::MCR;
4123 Ops.insert(Ops.begin()+2, N->getOperand(2));
4125 assert(Ops.size() == 3 &&
4126 "Invalid number of fields in special register string.");
4127 Opcode = IsThumb2 ? ARM::t2MCRR : ARM::MCRR;
4128 SDValue WriteValue[] = { N->getOperand(2), N->getOperand(3) };
4129 Ops.insert(Ops.begin()+2, WriteValue, WriteValue+2);
4132 Ops.push_back(getAL(CurDAG, DL));
4133 Ops.push_back(CurDAG->getRegister(0, MVT::i32));
4134 Ops.push_back(N->getOperand(0));
4136 ReplaceNode(N, CurDAG->getMachineNode(Opcode, DL, MVT::Other, Ops));
4140 std::string SpecialReg = RegString->getString().lower();
4141 int BankedReg = getBankedRegisterMask(SpecialReg);
4142 if (BankedReg != -1) {
4143 Ops = { CurDAG->getTargetConstant(BankedReg, DL, MVT::i32), N->getOperand(2),
4144 getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
4147 N, CurDAG->getMachineNode(IsThumb2 ? ARM::t2MSRbanked : ARM::MSRbanked,
4148 DL, MVT::Other, Ops));
4152 // The VFP registers are written to by creating SelectionDAG nodes with
4153 // opcodes corresponding to the register that is being written. So we switch
4154 // on the string to find which opcode we need to use.
4155 unsigned Opcode = StringSwitch<unsigned>(SpecialReg)
4156 .Case("fpscr", ARM::VMSR)
4157 .Case("fpexc", ARM::VMSR_FPEXC)
4158 .Case("fpsid", ARM::VMSR_FPSID)
4159 .Case("fpinst", ARM::VMSR_FPINST)
4160 .Case("fpinst2", ARM::VMSR_FPINST2)
4164 if (!Subtarget->hasVFP2())
4166 Ops = { N->getOperand(2), getAL(CurDAG, DL),
4167 CurDAG->getRegister(0, MVT::i32), N->getOperand(0) };
4168 ReplaceNode(N, CurDAG->getMachineNode(Opcode, DL, MVT::Other, Ops));
4172 std::pair<StringRef, StringRef> Fields;
4173 Fields = StringRef(SpecialReg).rsplit('_');
4174 std::string Reg = Fields.first.str();
4175 StringRef Flags = Fields.second;
4177 // If the target was M Class then need to validate the special register value
4178 // and retrieve the mask for use in the instruction node.
4179 if (Subtarget->isMClass()) {
4180 // basepri_max gets split so need to correct Reg and Flags.
4181 if (SpecialReg == "basepri_max") {
4185 int SYSmValue = getMClassRegisterMask(Reg, Flags, false, Subtarget);
4186 if (SYSmValue == -1)
4189 SDValue Ops[] = { CurDAG->getTargetConstant(SYSmValue, DL, MVT::i32),
4190 N->getOperand(2), getAL(CurDAG, DL),
4191 CurDAG->getRegister(0, MVT::i32), N->getOperand(0) };
4192 ReplaceNode(N, CurDAG->getMachineNode(ARM::t2MSR_M, DL, MVT::Other, Ops));
4196 // We then check to see if a valid mask can be constructed for one of the
4197 // register string values permitted for the A and R class cores. These values
4198 // are apsr, spsr and cpsr; these are also valid on older cores.
4199 int Mask = getARClassRegisterMask(Reg, Flags);
4201 Ops = { CurDAG->getTargetConstant(Mask, DL, MVT::i32), N->getOperand(2),
4202 getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
4204 ReplaceNode(N, CurDAG->getMachineNode(IsThumb2 ? ARM::t2MSR_AR : ARM::MSR,
4205 DL, MVT::Other, Ops));
4212 bool ARMDAGToDAGISel::tryInlineAsm(SDNode *N){
4213 std::vector<SDValue> AsmNodeOperands;
4214 unsigned Flag, Kind;
4215 bool Changed = false;
4216 unsigned NumOps = N->getNumOperands();
4218 // Normally, i64 data is bounded to two arbitrary GRPs for "%r" constraint.
4219 // However, some instrstions (e.g. ldrexd/strexd in ARM mode) require
4220 // (even/even+1) GPRs and use %n and %Hn to refer to the individual regs
4221 // respectively. Since there is no constraint to explicitly specify a
4222 // reg pair, we use GPRPair reg class for "%r" for 64-bit data. For Thumb,
4223 // the 64-bit data may be referred by H, Q, R modifiers, so we still pack
4224 // them into a GPRPair.
4227 SDValue Glue = N->getGluedNode() ? N->getOperand(NumOps-1)
4228 : SDValue(nullptr,0);
4230 SmallVector<bool, 8> OpChanged;
4231 // Glue node will be appended late.
4232 for(unsigned i = 0, e = N->getGluedNode() ? NumOps - 1 : NumOps; i < e; ++i) {
4233 SDValue op = N->getOperand(i);
4234 AsmNodeOperands.push_back(op);
4236 if (i < InlineAsm::Op_FirstOperand)
4239 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(i))) {
4240 Flag = C->getZExtValue();
4241 Kind = InlineAsm::getKind(Flag);
4246 // Immediate operands to inline asm in the SelectionDAG are modeled with
4247 // two operands. The first is a constant of value InlineAsm::Kind_Imm, and
4248 // the second is a constant with the value of the immediate. If we get here
4249 // and we have a Kind_Imm, skip the next operand, and continue.
4250 if (Kind == InlineAsm::Kind_Imm) {
4251 SDValue op = N->getOperand(++i);
4252 AsmNodeOperands.push_back(op);
4256 unsigned NumRegs = InlineAsm::getNumOperandRegisters(Flag);
4258 OpChanged.push_back(false);
4260 unsigned DefIdx = 0;
4261 bool IsTiedToChangedOp = false;
4262 // If it's a use that is tied with a previous def, it has no
4263 // reg class constraint.
4264 if (Changed && InlineAsm::isUseOperandTiedToDef(Flag, DefIdx))
4265 IsTiedToChangedOp = OpChanged[DefIdx];
4267 // Memory operands to inline asm in the SelectionDAG are modeled with two
4268 // operands: a constant of value InlineAsm::Kind_Mem followed by the input
4269 // operand. If we get here and we have a Kind_Mem, skip the next operand (so
4270 // it doesn't get misinterpreted), and continue. We do this here because
4271 // it's important to update the OpChanged array correctly before moving on.
4272 if (Kind == InlineAsm::Kind_Mem) {
4273 SDValue op = N->getOperand(++i);
4274 AsmNodeOperands.push_back(op);
4278 if (Kind != InlineAsm::Kind_RegUse && Kind != InlineAsm::Kind_RegDef
4279 && Kind != InlineAsm::Kind_RegDefEarlyClobber)
4283 bool HasRC = InlineAsm::hasRegClassConstraint(Flag, RC);
4284 if ((!IsTiedToChangedOp && (!HasRC || RC != ARM::GPRRegClassID))
4288 assert((i+2 < NumOps) && "Invalid number of operands in inline asm");
4289 SDValue V0 = N->getOperand(i+1);
4290 SDValue V1 = N->getOperand(i+2);
4291 unsigned Reg0 = cast<RegisterSDNode>(V0)->getReg();
4292 unsigned Reg1 = cast<RegisterSDNode>(V1)->getReg();
4294 MachineRegisterInfo &MRI = MF->getRegInfo();
4296 if (Kind == InlineAsm::Kind_RegDef ||
4297 Kind == InlineAsm::Kind_RegDefEarlyClobber) {
4298 // Replace the two GPRs with 1 GPRPair and copy values from GPRPair to
4299 // the original GPRs.
4301 unsigned GPVR = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
4302 PairedReg = CurDAG->getRegister(GPVR, MVT::Untyped);
4303 SDValue Chain = SDValue(N,0);
4305 SDNode *GU = N->getGluedUser();
4306 SDValue RegCopy = CurDAG->getCopyFromReg(Chain, dl, GPVR, MVT::Untyped,
4309 // Extract values from a GPRPair reg and copy to the original GPR reg.
4310 SDValue Sub0 = CurDAG->getTargetExtractSubreg(ARM::gsub_0, dl, MVT::i32,
4312 SDValue Sub1 = CurDAG->getTargetExtractSubreg(ARM::gsub_1, dl, MVT::i32,
4314 SDValue T0 = CurDAG->getCopyToReg(Sub0, dl, Reg0, Sub0,
4315 RegCopy.getValue(1));
4316 SDValue T1 = CurDAG->getCopyToReg(Sub1, dl, Reg1, Sub1, T0.getValue(1));
4318 // Update the original glue user.
4319 std::vector<SDValue> Ops(GU->op_begin(), GU->op_end()-1);
4320 Ops.push_back(T1.getValue(1));
4321 CurDAG->UpdateNodeOperands(GU, Ops);
4324 // For Kind == InlineAsm::Kind_RegUse, we first copy two GPRs into a
4325 // GPRPair and then pass the GPRPair to the inline asm.
4326 SDValue Chain = AsmNodeOperands[InlineAsm::Op_InputChain];
4328 // As REG_SEQ doesn't take RegisterSDNode, we copy them first.
4329 SDValue T0 = CurDAG->getCopyFromReg(Chain, dl, Reg0, MVT::i32,
4331 SDValue T1 = CurDAG->getCopyFromReg(Chain, dl, Reg1, MVT::i32,
4333 SDValue Pair = SDValue(createGPRPairNode(MVT::Untyped, T0, T1), 0);
4335 // Copy REG_SEQ into a GPRPair-typed VR and replace the original two
4336 // i32 VRs of inline asm with it.
4337 unsigned GPVR = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
4338 PairedReg = CurDAG->getRegister(GPVR, MVT::Untyped);
4339 Chain = CurDAG->getCopyToReg(T1, dl, GPVR, Pair, T1.getValue(1));
4341 AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
4342 Glue = Chain.getValue(1);
4347 if(PairedReg.getNode()) {
4348 OpChanged[OpChanged.size() -1 ] = true;
4349 Flag = InlineAsm::getFlagWord(Kind, 1 /* RegNum*/);
4350 if (IsTiedToChangedOp)
4351 Flag = InlineAsm::getFlagWordForMatchingOp(Flag, DefIdx);
4353 Flag = InlineAsm::getFlagWordForRegClass(Flag, ARM::GPRPairRegClassID);
4354 // Replace the current flag.
4355 AsmNodeOperands[AsmNodeOperands.size() -1] = CurDAG->getTargetConstant(
4356 Flag, dl, MVT::i32);
4357 // Add the new register node and skip the original two GPRs.
4358 AsmNodeOperands.push_back(PairedReg);
4359 // Skip the next two GPRs.
4365 AsmNodeOperands.push_back(Glue);
4369 SDValue New = CurDAG->getNode(ISD::INLINEASM, SDLoc(N),
4370 CurDAG->getVTList(MVT::Other, MVT::Glue), AsmNodeOperands);
4372 ReplaceNode(N, New.getNode());
4377 bool ARMDAGToDAGISel::
4378 SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
4379 std::vector<SDValue> &OutOps) {
4380 switch(ConstraintID) {
4382 llvm_unreachable("Unexpected asm memory constraint");
4383 case InlineAsm::Constraint_i:
4384 // FIXME: It seems strange that 'i' is needed here since it's supposed to
4385 // be an immediate and not a memory constraint.
4387 case InlineAsm::Constraint_m:
4388 case InlineAsm::Constraint_o:
4389 case InlineAsm::Constraint_Q:
4390 case InlineAsm::Constraint_Um:
4391 case InlineAsm::Constraint_Un:
4392 case InlineAsm::Constraint_Uq:
4393 case InlineAsm::Constraint_Us:
4394 case InlineAsm::Constraint_Ut:
4395 case InlineAsm::Constraint_Uv:
4396 case InlineAsm::Constraint_Uy:
4397 // Require the address to be in a register. That is safe for all ARM
4398 // variants and it is hard to do anything much smarter without knowing
4399 // how the operand is used.
4400 OutOps.push_back(Op);
4406 /// createARMISelDag - This pass converts a legalized DAG into a
4407 /// ARM-specific DAG, ready for instruction scheduling.
4409 FunctionPass *llvm::createARMISelDag(ARMBaseTargetMachine &TM,
4410 CodeGenOpt::Level OptLevel) {
4411 return new ARMDAGToDAGISel(TM, OptLevel);