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 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "arm-isel"
16 #include "ARMBaseInstrInfo.h"
17 #include "ARMAddressingModes.h"
18 #include "ARMTargetMachine.h"
19 #include "llvm/CallingConv.h"
20 #include "llvm/Constants.h"
21 #include "llvm/DerivedTypes.h"
22 #include "llvm/Function.h"
23 #include "llvm/Intrinsics.h"
24 #include "llvm/LLVMContext.h"
25 #include "llvm/CodeGen/MachineFrameInfo.h"
26 #include "llvm/CodeGen/MachineFunction.h"
27 #include "llvm/CodeGen/MachineInstrBuilder.h"
28 #include "llvm/CodeGen/SelectionDAG.h"
29 #include "llvm/CodeGen/SelectionDAGISel.h"
30 #include "llvm/Target/TargetLowering.h"
31 #include "llvm/Target/TargetOptions.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/Compiler.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/raw_ostream.h"
41 DisableShifterOp("disable-shifter-op", cl::Hidden,
42 cl::desc("Disable isel of shifter-op"),
46 CheckVMLxHazard("check-vmlx-hazard", cl::Hidden,
47 cl::desc("Check fp vmla / vmls hazard at isel time"),
50 //===--------------------------------------------------------------------===//
51 /// ARMDAGToDAGISel - ARM specific code to select ARM machine
52 /// instructions for SelectionDAG operations.
57 AM2_BASE, // Simple AM2 (+-imm12)
58 AM2_SHOP // Shifter-op AM2
61 class ARMDAGToDAGISel : public SelectionDAGISel {
62 ARMBaseTargetMachine &TM;
63 const ARMBaseInstrInfo *TII;
65 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
66 /// make the right decision when generating code for different targets.
67 const ARMSubtarget *Subtarget;
70 explicit ARMDAGToDAGISel(ARMBaseTargetMachine &tm,
71 CodeGenOpt::Level OptLevel)
72 : SelectionDAGISel(tm, OptLevel), TM(tm),
73 TII(static_cast<const ARMBaseInstrInfo*>(TM.getInstrInfo())),
74 Subtarget(&TM.getSubtarget<ARMSubtarget>()) {
77 virtual const char *getPassName() const {
78 return "ARM Instruction Selection";
81 /// getI32Imm - Return a target constant of type i32 with the specified
83 inline SDValue getI32Imm(unsigned Imm) {
84 return CurDAG->getTargetConstant(Imm, MVT::i32);
87 SDNode *Select(SDNode *N);
90 bool hasNoVMLxHazardUse(SDNode *N) const;
91 bool isShifterOpProfitable(const SDValue &Shift,
92 ARM_AM::ShiftOpc ShOpcVal, unsigned ShAmt);
93 bool SelectShifterOperandReg(SDValue N, SDValue &A,
94 SDValue &B, SDValue &C);
95 bool SelectShiftShifterOperandReg(SDValue N, SDValue &A,
96 SDValue &B, SDValue &C);
97 bool SelectAddrModeImm12(SDValue N, SDValue &Base, SDValue &OffImm);
98 bool SelectLdStSOReg(SDValue N, SDValue &Base, SDValue &Offset, SDValue &Opc);
100 AddrMode2Type SelectAddrMode2Worker(SDValue N, SDValue &Base,
101 SDValue &Offset, SDValue &Opc);
102 bool SelectAddrMode2Base(SDValue N, SDValue &Base, SDValue &Offset,
104 return SelectAddrMode2Worker(N, Base, Offset, Opc) == AM2_BASE;
107 bool SelectAddrMode2ShOp(SDValue N, SDValue &Base, SDValue &Offset,
109 return SelectAddrMode2Worker(N, Base, Offset, Opc) == AM2_SHOP;
112 bool SelectAddrMode2(SDValue N, SDValue &Base, SDValue &Offset,
114 SelectAddrMode2Worker(N, Base, Offset, Opc);
115 // return SelectAddrMode2ShOp(N, Base, Offset, Opc);
116 // This always matches one way or another.
120 bool SelectAddrMode2Offset(SDNode *Op, SDValue N,
121 SDValue &Offset, SDValue &Opc);
122 bool SelectAddrMode3(SDValue N, SDValue &Base,
123 SDValue &Offset, SDValue &Opc);
124 bool SelectAddrMode3Offset(SDNode *Op, SDValue N,
125 SDValue &Offset, SDValue &Opc);
126 bool SelectAddrMode5(SDValue N, SDValue &Base,
128 bool SelectAddrMode6(SDNode *Parent, SDValue N, SDValue &Addr,SDValue &Align);
130 bool SelectAddrModePC(SDValue N, SDValue &Offset, SDValue &Label);
132 // Thumb Addressing Modes:
133 bool SelectThumbAddrModeRR(SDValue N, SDValue &Base, SDValue &Offset);
134 bool SelectThumbAddrModeRI(SDValue N, SDValue &Base, SDValue &Offset,
136 bool SelectThumbAddrModeRI5S1(SDValue N, SDValue &Base, SDValue &Offset);
137 bool SelectThumbAddrModeRI5S2(SDValue N, SDValue &Base, SDValue &Offset);
138 bool SelectThumbAddrModeRI5S4(SDValue N, SDValue &Base, SDValue &Offset);
139 bool SelectThumbAddrModeImm5S(SDValue N, unsigned Scale, SDValue &Base,
141 bool SelectThumbAddrModeImm5S1(SDValue N, SDValue &Base,
143 bool SelectThumbAddrModeImm5S2(SDValue N, SDValue &Base,
145 bool SelectThumbAddrModeImm5S4(SDValue N, SDValue &Base,
147 bool SelectThumbAddrModeSP(SDValue N, SDValue &Base, SDValue &OffImm);
149 // Thumb 2 Addressing Modes:
150 bool SelectT2ShifterOperandReg(SDValue N,
151 SDValue &BaseReg, SDValue &Opc);
152 bool SelectT2AddrModeImm12(SDValue N, SDValue &Base, SDValue &OffImm);
153 bool SelectT2AddrModeImm8(SDValue N, SDValue &Base,
155 bool SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
157 bool SelectT2AddrModeSoReg(SDValue N, SDValue &Base,
158 SDValue &OffReg, SDValue &ShImm);
160 inline bool is_so_imm(unsigned Imm) const {
161 return ARM_AM::getSOImmVal(Imm) != -1;
164 inline bool is_so_imm_not(unsigned Imm) const {
165 return ARM_AM::getSOImmVal(~Imm) != -1;
168 inline bool is_t2_so_imm(unsigned Imm) const {
169 return ARM_AM::getT2SOImmVal(Imm) != -1;
172 inline bool is_t2_so_imm_not(unsigned Imm) const {
173 return ARM_AM::getT2SOImmVal(~Imm) != -1;
176 inline bool Pred_so_imm(SDNode *inN) const {
177 ConstantSDNode *N = cast<ConstantSDNode>(inN);
178 return is_so_imm(N->getZExtValue());
181 inline bool Pred_t2_so_imm(SDNode *inN) const {
182 ConstantSDNode *N = cast<ConstantSDNode>(inN);
183 return is_t2_so_imm(N->getZExtValue());
186 // Include the pieces autogenerated from the target description.
187 #include "ARMGenDAGISel.inc"
190 /// SelectARMIndexedLoad - Indexed (pre/post inc/dec) load matching code for
192 SDNode *SelectARMIndexedLoad(SDNode *N);
193 SDNode *SelectT2IndexedLoad(SDNode *N);
195 /// SelectVLD - Select NEON load intrinsics. NumVecs should be
196 /// 1, 2, 3 or 4. The opcode arrays specify the instructions used for
197 /// loads of D registers and even subregs and odd subregs of Q registers.
198 /// For NumVecs <= 2, QOpcodes1 is not used.
199 SDNode *SelectVLD(SDNode *N, unsigned NumVecs, unsigned *DOpcodes,
200 unsigned *QOpcodes0, unsigned *QOpcodes1);
202 /// SelectVST - Select NEON store intrinsics. NumVecs should
203 /// be 1, 2, 3 or 4. The opcode arrays specify the instructions used for
204 /// stores of D registers and even subregs and odd subregs of Q registers.
205 /// For NumVecs <= 2, QOpcodes1 is not used.
206 SDNode *SelectVST(SDNode *N, unsigned NumVecs, unsigned *DOpcodes,
207 unsigned *QOpcodes0, unsigned *QOpcodes1);
209 /// SelectVLDSTLane - Select NEON load/store lane intrinsics. NumVecs should
210 /// be 2, 3 or 4. The opcode arrays specify the instructions used for
211 /// load/store of D registers and Q registers.
212 SDNode *SelectVLDSTLane(SDNode *N, bool IsLoad, unsigned NumVecs,
213 unsigned *DOpcodes, unsigned *QOpcodes);
215 /// SelectVLDDup - Select NEON load-duplicate intrinsics. NumVecs
216 /// should be 2, 3 or 4. The opcode array specifies the instructions used
217 /// for loading D registers. (Q registers are not supported.)
218 SDNode *SelectVLDDup(SDNode *N, unsigned NumVecs, unsigned *Opcodes);
220 /// SelectVTBL - Select NEON VTBL and VTBX intrinsics. NumVecs should be 2,
221 /// 3 or 4. These are custom-selected so that a REG_SEQUENCE can be
222 /// generated to force the table registers to be consecutive.
223 SDNode *SelectVTBL(SDNode *N, bool IsExt, unsigned NumVecs, unsigned Opc);
225 /// SelectV6T2BitfieldExtractOp - Select SBFX/UBFX instructions for ARM.
226 SDNode *SelectV6T2BitfieldExtractOp(SDNode *N, bool isSigned);
228 /// SelectCMOVOp - Select CMOV instructions for ARM.
229 SDNode *SelectCMOVOp(SDNode *N);
230 SDNode *SelectT2CMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
231 ARMCC::CondCodes CCVal, SDValue CCR,
233 SDNode *SelectARMCMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
234 ARMCC::CondCodes CCVal, SDValue CCR,
236 SDNode *SelectT2CMOVImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
237 ARMCC::CondCodes CCVal, SDValue CCR,
239 SDNode *SelectARMCMOVImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
240 ARMCC::CondCodes CCVal, SDValue CCR,
243 SDNode *SelectConcatVector(SDNode *N);
245 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
246 /// inline asm expressions.
247 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
249 std::vector<SDValue> &OutOps);
251 // Form pairs of consecutive S, D, or Q registers.
252 SDNode *PairSRegs(EVT VT, SDValue V0, SDValue V1);
253 SDNode *PairDRegs(EVT VT, SDValue V0, SDValue V1);
254 SDNode *PairQRegs(EVT VT, SDValue V0, SDValue V1);
256 // Form sequences of 4 consecutive S, D, or Q registers.
257 SDNode *QuadSRegs(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
258 SDNode *QuadDRegs(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
259 SDNode *QuadQRegs(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
261 // Get the alignment operand for a NEON VLD or VST instruction.
262 SDValue GetVLDSTAlign(SDValue Align, unsigned NumVecs, bool is64BitVector);
266 /// isInt32Immediate - This method tests to see if the node is a 32-bit constant
267 /// operand. If so Imm will receive the 32-bit value.
268 static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
269 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
270 Imm = cast<ConstantSDNode>(N)->getZExtValue();
276 // isInt32Immediate - This method tests to see if a constant operand.
277 // If so Imm will receive the 32 bit value.
278 static bool isInt32Immediate(SDValue N, unsigned &Imm) {
279 return isInt32Immediate(N.getNode(), Imm);
282 // isOpcWithIntImmediate - This method tests to see if the node is a specific
283 // opcode and that it has a immediate integer right operand.
284 // If so Imm will receive the 32 bit value.
285 static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
286 return N->getOpcode() == Opc &&
287 isInt32Immediate(N->getOperand(1).getNode(), Imm);
290 /// \brief Check whether a particular node is a constant value representable as
291 /// (N * Scale) where (N in [\arg RangeMin, \arg RangeMax).
293 /// \param ScaledConstant [out] - On success, the pre-scaled constant value.
294 static bool isScaledConstantInRange(SDValue Node, unsigned Scale,
295 int RangeMin, int RangeMax,
296 int &ScaledConstant) {
297 assert(Scale && "Invalid scale!");
299 // Check that this is a constant.
300 const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Node);
304 ScaledConstant = (int) C->getZExtValue();
305 if ((ScaledConstant % Scale) != 0)
308 ScaledConstant /= Scale;
309 return ScaledConstant >= RangeMin && ScaledConstant < RangeMax;
312 /// hasNoVMLxHazardUse - Return true if it's desirable to select a FP MLA / MLS
313 /// node. VFP / NEON fp VMLA / VMLS instructions have special RAW hazards (at
314 /// least on current ARM implementations) which should be avoidded.
315 bool ARMDAGToDAGISel::hasNoVMLxHazardUse(SDNode *N) const {
316 if (OptLevel == CodeGenOpt::None)
319 if (!CheckVMLxHazard)
322 if (!Subtarget->isCortexA8() && !Subtarget->isCortexA9())
328 SDNode *Use = *N->use_begin();
329 if (Use->getOpcode() == ISD::CopyToReg)
331 if (Use->isMachineOpcode()) {
332 const TargetInstrDesc &TID = TII->get(Use->getMachineOpcode());
335 unsigned Opcode = TID.getOpcode();
336 if (Opcode == ARM::VMOVRS || Opcode == ARM::VMOVRRD)
338 // vmlx feeding into another vmlx. We actually want to unfold
339 // the use later in the MLxExpansion pass. e.g.
341 // vmla (stall 8 cycles)
346 // This adds up to about 18 - 19 cycles.
349 // vmul (stall 4 cycles)
350 // vadd adds up to about 14 cycles.
351 return TII->isFpMLxInstruction(Opcode);
357 bool ARMDAGToDAGISel::isShifterOpProfitable(const SDValue &Shift,
358 ARM_AM::ShiftOpc ShOpcVal,
360 if (!Subtarget->isCortexA9())
362 if (Shift.hasOneUse())
365 return ShOpcVal == ARM_AM::lsl && ShAmt == 2;
368 bool ARMDAGToDAGISel::SelectShifterOperandReg(SDValue N,
372 if (DisableShifterOp)
375 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
377 // Don't match base register only case. That is matched to a separate
378 // lower complexity pattern with explicit register operand.
379 if (ShOpcVal == ARM_AM::no_shift) return false;
381 BaseReg = N.getOperand(0);
382 unsigned ShImmVal = 0;
383 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
384 ShReg = CurDAG->getRegister(0, MVT::i32);
385 ShImmVal = RHS->getZExtValue() & 31;
387 ShReg = N.getOperand(1);
388 if (!isShifterOpProfitable(N, ShOpcVal, ShImmVal))
391 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
396 bool ARMDAGToDAGISel::SelectShiftShifterOperandReg(SDValue N,
400 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
402 // Don't match base register only case. That is matched to a separate
403 // lower complexity pattern with explicit register operand.
404 if (ShOpcVal == ARM_AM::no_shift) return false;
406 BaseReg = N.getOperand(0);
407 unsigned ShImmVal = 0;
408 // Do not check isShifterOpProfitable. This must return true.
409 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
410 ShReg = CurDAG->getRegister(0, MVT::i32);
411 ShImmVal = RHS->getZExtValue() & 31;
413 ShReg = N.getOperand(1);
415 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
420 bool ARMDAGToDAGISel::SelectAddrModeImm12(SDValue N,
423 // Match simple R + imm12 operands.
426 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB) {
427 if (N.getOpcode() == ISD::FrameIndex) {
428 // Match frame index...
429 int FI = cast<FrameIndexSDNode>(N)->getIndex();
430 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
431 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
433 } else if (N.getOpcode() == ARMISD::Wrapper &&
434 !(Subtarget->useMovt() &&
435 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
436 Base = N.getOperand(0);
439 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
443 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
444 int RHSC = (int)RHS->getZExtValue();
445 if (N.getOpcode() == ISD::SUB)
448 if (RHSC >= 0 && RHSC < 0x1000) { // 12 bits (unsigned)
449 Base = N.getOperand(0);
450 if (Base.getOpcode() == ISD::FrameIndex) {
451 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
452 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
454 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
461 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
467 bool ARMDAGToDAGISel::SelectLdStSOReg(SDValue N, SDValue &Base, SDValue &Offset,
469 if (N.getOpcode() == ISD::MUL &&
470 (!Subtarget->isCortexA9() || N.hasOneUse())) {
471 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
472 // X * [3,5,9] -> X + X * [2,4,8] etc.
473 int RHSC = (int)RHS->getZExtValue();
476 ARM_AM::AddrOpc AddSub = ARM_AM::add;
478 AddSub = ARM_AM::sub;
481 if (isPowerOf2_32(RHSC)) {
482 unsigned ShAmt = Log2_32(RHSC);
483 Base = Offset = N.getOperand(0);
484 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt,
493 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB)
496 // Leave simple R +/- imm12 operands for LDRi12
497 if (N.getOpcode() == ISD::ADD) {
499 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/1,
500 -0x1000+1, 0x1000, RHSC)) // 12 bits.
504 if (Subtarget->isCortexA9() && !N.hasOneUse())
505 // Compute R +/- (R << N) and reuse it.
508 // Otherwise this is R +/- [possibly shifted] R.
509 ARM_AM::AddrOpc AddSub = N.getOpcode() == ISD::ADD ? ARM_AM::add:ARM_AM::sub;
510 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(1));
513 Base = N.getOperand(0);
514 Offset = N.getOperand(1);
516 if (ShOpcVal != ARM_AM::no_shift) {
517 // Check to see if the RHS of the shift is a constant, if not, we can't fold
519 if (ConstantSDNode *Sh =
520 dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) {
521 ShAmt = Sh->getZExtValue();
522 if (isShifterOpProfitable(Offset, ShOpcVal, ShAmt))
523 Offset = N.getOperand(1).getOperand(0);
526 ShOpcVal = ARM_AM::no_shift;
529 ShOpcVal = ARM_AM::no_shift;
533 // Try matching (R shl C) + (R).
534 if (N.getOpcode() == ISD::ADD && ShOpcVal == ARM_AM::no_shift &&
535 !(Subtarget->isCortexA9() || N.getOperand(0).hasOneUse())) {
536 ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0));
537 if (ShOpcVal != ARM_AM::no_shift) {
538 // Check to see if the RHS of the shift is a constant, if not, we can't
540 if (ConstantSDNode *Sh =
541 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
542 ShAmt = Sh->getZExtValue();
543 if (!Subtarget->isCortexA9() ||
545 isShifterOpProfitable(N.getOperand(0), ShOpcVal, ShAmt))) {
546 Offset = N.getOperand(0).getOperand(0);
547 Base = N.getOperand(1);
550 ShOpcVal = ARM_AM::no_shift;
553 ShOpcVal = ARM_AM::no_shift;
558 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
568 AddrMode2Type ARMDAGToDAGISel::SelectAddrMode2Worker(SDValue N,
572 if (N.getOpcode() == ISD::MUL &&
573 (!Subtarget->isCortexA9() || N.hasOneUse())) {
574 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
575 // X * [3,5,9] -> X + X * [2,4,8] etc.
576 int RHSC = (int)RHS->getZExtValue();
579 ARM_AM::AddrOpc AddSub = ARM_AM::add;
581 AddSub = ARM_AM::sub;
584 if (isPowerOf2_32(RHSC)) {
585 unsigned ShAmt = Log2_32(RHSC);
586 Base = Offset = N.getOperand(0);
587 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt,
596 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB) {
598 if (N.getOpcode() == ISD::FrameIndex) {
599 int FI = cast<FrameIndexSDNode>(N)->getIndex();
600 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
601 } else if (N.getOpcode() == ARMISD::Wrapper &&
602 !(Subtarget->useMovt() &&
603 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
604 Base = N.getOperand(0);
606 Offset = CurDAG->getRegister(0, MVT::i32);
607 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::add, 0,
613 // Match simple R +/- imm12 operands.
614 if (N.getOpcode() == ISD::ADD) {
616 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/1,
617 -0x1000+1, 0x1000, RHSC)) { // 12 bits.
618 Base = N.getOperand(0);
619 if (Base.getOpcode() == ISD::FrameIndex) {
620 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
621 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
623 Offset = CurDAG->getRegister(0, MVT::i32);
625 ARM_AM::AddrOpc AddSub = ARM_AM::add;
627 AddSub = ARM_AM::sub;
630 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, RHSC,
637 if (Subtarget->isCortexA9() && !N.hasOneUse()) {
638 // Compute R +/- (R << N) and reuse it.
640 Offset = CurDAG->getRegister(0, MVT::i32);
641 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::add, 0,
647 // Otherwise this is R +/- [possibly shifted] R.
648 ARM_AM::AddrOpc AddSub = N.getOpcode() == ISD::ADD ? ARM_AM::add:ARM_AM::sub;
649 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(1));
652 Base = N.getOperand(0);
653 Offset = N.getOperand(1);
655 if (ShOpcVal != ARM_AM::no_shift) {
656 // Check to see if the RHS of the shift is a constant, if not, we can't fold
658 if (ConstantSDNode *Sh =
659 dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) {
660 ShAmt = Sh->getZExtValue();
661 if (isShifterOpProfitable(Offset, ShOpcVal, ShAmt))
662 Offset = N.getOperand(1).getOperand(0);
665 ShOpcVal = ARM_AM::no_shift;
668 ShOpcVal = ARM_AM::no_shift;
672 // Try matching (R shl C) + (R).
673 if (N.getOpcode() == ISD::ADD && ShOpcVal == ARM_AM::no_shift &&
674 !(Subtarget->isCortexA9() || N.getOperand(0).hasOneUse())) {
675 ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0));
676 if (ShOpcVal != ARM_AM::no_shift) {
677 // Check to see if the RHS of the shift is a constant, if not, we can't
679 if (ConstantSDNode *Sh =
680 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
681 ShAmt = Sh->getZExtValue();
682 if (!Subtarget->isCortexA9() ||
684 isShifterOpProfitable(N.getOperand(0), ShOpcVal, ShAmt))) {
685 Offset = N.getOperand(0).getOperand(0);
686 Base = N.getOperand(1);
689 ShOpcVal = ARM_AM::no_shift;
692 ShOpcVal = ARM_AM::no_shift;
697 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
702 bool ARMDAGToDAGISel::SelectAddrMode2Offset(SDNode *Op, SDValue N,
703 SDValue &Offset, SDValue &Opc) {
704 unsigned Opcode = Op->getOpcode();
705 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
706 ? cast<LoadSDNode>(Op)->getAddressingMode()
707 : cast<StoreSDNode>(Op)->getAddressingMode();
708 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
709 ? ARM_AM::add : ARM_AM::sub;
711 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x1000, Val)) { // 12 bits.
712 Offset = CurDAG->getRegister(0, MVT::i32);
713 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, Val,
720 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
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 fold
725 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
726 ShAmt = Sh->getZExtValue();
727 if (isShifterOpProfitable(N, ShOpcVal, ShAmt))
728 Offset = N.getOperand(0);
731 ShOpcVal = ARM_AM::no_shift;
734 ShOpcVal = ARM_AM::no_shift;
738 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
744 bool ARMDAGToDAGISel::SelectAddrMode3(SDValue N,
745 SDValue &Base, SDValue &Offset,
747 if (N.getOpcode() == ISD::SUB) {
748 // X - C is canonicalize to X + -C, no need to handle it here.
749 Base = N.getOperand(0);
750 Offset = N.getOperand(1);
751 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::sub, 0),MVT::i32);
755 if (N.getOpcode() != ISD::ADD) {
757 if (N.getOpcode() == ISD::FrameIndex) {
758 int FI = cast<FrameIndexSDNode>(N)->getIndex();
759 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
761 Offset = CurDAG->getRegister(0, MVT::i32);
762 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0),MVT::i32);
766 // If the RHS is +/- imm8, fold into addr mode.
768 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/1,
769 -256 + 1, 256, RHSC)) { // 8 bits.
770 Base = N.getOperand(0);
771 if (Base.getOpcode() == ISD::FrameIndex) {
772 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
773 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
775 Offset = CurDAG->getRegister(0, MVT::i32);
777 ARM_AM::AddrOpc AddSub = ARM_AM::add;
779 AddSub = ARM_AM::sub;
782 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, RHSC),MVT::i32);
786 Base = N.getOperand(0);
787 Offset = N.getOperand(1);
788 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0), MVT::i32);
792 bool ARMDAGToDAGISel::SelectAddrMode3Offset(SDNode *Op, SDValue N,
793 SDValue &Offset, SDValue &Opc) {
794 unsigned Opcode = Op->getOpcode();
795 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
796 ? cast<LoadSDNode>(Op)->getAddressingMode()
797 : cast<StoreSDNode>(Op)->getAddressingMode();
798 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
799 ? ARM_AM::add : ARM_AM::sub;
801 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 256, Val)) { // 12 bits.
802 Offset = CurDAG->getRegister(0, MVT::i32);
803 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, Val), MVT::i32);
808 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, 0), MVT::i32);
812 bool ARMDAGToDAGISel::SelectAddrMode5(SDValue N,
813 SDValue &Base, SDValue &Offset) {
814 if (N.getOpcode() != ISD::ADD) {
816 if (N.getOpcode() == ISD::FrameIndex) {
817 int FI = cast<FrameIndexSDNode>(N)->getIndex();
818 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
819 } else if (N.getOpcode() == ARMISD::Wrapper &&
820 !(Subtarget->useMovt() &&
821 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
822 Base = N.getOperand(0);
824 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
829 // If the RHS is +/- imm8, fold into addr mode.
831 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/4,
832 -256 + 1, 256, RHSC)) {
833 Base = N.getOperand(0);
834 if (Base.getOpcode() == ISD::FrameIndex) {
835 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
836 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
839 ARM_AM::AddrOpc AddSub = ARM_AM::add;
841 AddSub = ARM_AM::sub;
844 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(AddSub, RHSC),
850 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
855 bool ARMDAGToDAGISel::SelectAddrMode6(SDNode *Parent, SDValue N, SDValue &Addr,
859 unsigned Alignment = 0;
860 if (LSBaseSDNode *LSN = dyn_cast<LSBaseSDNode>(Parent)) {
861 // This case occurs only for VLD1-lane/dup and VST1-lane instructions.
862 // The maximum alignment is equal to the memory size being referenced.
863 unsigned LSNAlign = LSN->getAlignment();
864 unsigned MemSize = LSN->getMemoryVT().getSizeInBits() / 8;
865 if (LSNAlign > MemSize && MemSize > 1)
868 // All other uses of addrmode6 are for intrinsics. For now just record
869 // the raw alignment value; it will be refined later based on the legal
870 // alignment operands for the intrinsic.
871 Alignment = cast<MemIntrinsicSDNode>(Parent)->getAlignment();
874 Align = CurDAG->getTargetConstant(Alignment, MVT::i32);
878 bool ARMDAGToDAGISel::SelectAddrModePC(SDValue N,
879 SDValue &Offset, SDValue &Label) {
880 if (N.getOpcode() == ARMISD::PIC_ADD && N.hasOneUse()) {
881 Offset = N.getOperand(0);
882 SDValue N1 = N.getOperand(1);
883 Label = CurDAG->getTargetConstant(cast<ConstantSDNode>(N1)->getZExtValue(),
892 //===----------------------------------------------------------------------===//
893 // Thumb Addressing Modes
894 //===----------------------------------------------------------------------===//
896 bool ARMDAGToDAGISel::SelectThumbAddrModeRR(SDValue N,
897 SDValue &Base, SDValue &Offset){
898 // FIXME dl should come from the parent load or store, not the address
899 if (N.getOpcode() != ISD::ADD) {
900 ConstantSDNode *NC = dyn_cast<ConstantSDNode>(N);
901 if (!NC || !NC->isNullValue())
908 Base = N.getOperand(0);
909 Offset = N.getOperand(1);
914 ARMDAGToDAGISel::SelectThumbAddrModeRI(SDValue N, SDValue &Base,
915 SDValue &Offset, unsigned Scale) {
917 SDValue TmpBase, TmpOffImm;
918 if (SelectThumbAddrModeSP(N, TmpBase, TmpOffImm))
919 return false; // We want to select tLDRspi / tSTRspi instead.
921 if (N.getOpcode() == ARMISD::Wrapper &&
922 N.getOperand(0).getOpcode() == ISD::TargetConstantPool)
923 return false; // We want to select tLDRpci instead.
926 if (N.getOpcode() != ISD::ADD)
929 // Thumb does not have [sp, r] address mode.
930 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
931 RegisterSDNode *RHSR = dyn_cast<RegisterSDNode>(N.getOperand(1));
932 if ((LHSR && LHSR->getReg() == ARM::SP) ||
933 (RHSR && RHSR->getReg() == ARM::SP))
936 // FIXME: Why do we explicitly check for a match here and then return false?
937 // Presumably to allow something else to match, but shouldn't this be
940 if (isScaledConstantInRange(N.getOperand(1), Scale, 0, 32, RHSC))
943 Base = N.getOperand(0);
944 Offset = N.getOperand(1);
949 ARMDAGToDAGISel::SelectThumbAddrModeRI5S1(SDValue N,
952 return SelectThumbAddrModeRI(N, Base, Offset, 1);
956 ARMDAGToDAGISel::SelectThumbAddrModeRI5S2(SDValue N,
959 return SelectThumbAddrModeRI(N, Base, Offset, 2);
963 ARMDAGToDAGISel::SelectThumbAddrModeRI5S4(SDValue N,
966 return SelectThumbAddrModeRI(N, Base, Offset, 4);
970 ARMDAGToDAGISel::SelectThumbAddrModeImm5S(SDValue N, unsigned Scale,
971 SDValue &Base, SDValue &OffImm) {
973 SDValue TmpBase, TmpOffImm;
974 if (SelectThumbAddrModeSP(N, TmpBase, TmpOffImm))
975 return false; // We want to select tLDRspi / tSTRspi instead.
977 if (N.getOpcode() == ARMISD::Wrapper &&
978 N.getOperand(0).getOpcode() == ISD::TargetConstantPool)
979 return false; // We want to select tLDRpci instead.
982 if (N.getOpcode() != ISD::ADD) {
983 if (N.getOpcode() == ARMISD::Wrapper &&
984 !(Subtarget->useMovt() &&
985 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
986 Base = N.getOperand(0);
991 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
995 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
996 RegisterSDNode *RHSR = dyn_cast<RegisterSDNode>(N.getOperand(1));
997 if ((LHSR && LHSR->getReg() == ARM::SP) ||
998 (RHSR && RHSR->getReg() == ARM::SP)) {
999 ConstantSDNode *LHS = dyn_cast<ConstantSDNode>(N.getOperand(0));
1000 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1));
1001 unsigned LHSC = LHS ? LHS->getZExtValue() : 0;
1002 unsigned RHSC = RHS ? RHS->getZExtValue() : 0;
1004 // Thumb does not have [sp, #imm5] address mode for non-zero imm5.
1005 if (LHSC != 0 || RHSC != 0) return false;
1008 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
1012 // If the RHS is + imm5 * scale, fold into addr mode.
1014 if (isScaledConstantInRange(N.getOperand(1), Scale, 0, 32, RHSC)) {
1015 Base = N.getOperand(0);
1016 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
1020 Base = N.getOperand(0);
1021 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
1026 ARMDAGToDAGISel::SelectThumbAddrModeImm5S4(SDValue N, SDValue &Base,
1028 return SelectThumbAddrModeImm5S(N, 4, Base, OffImm);
1032 ARMDAGToDAGISel::SelectThumbAddrModeImm5S2(SDValue N, SDValue &Base,
1034 return SelectThumbAddrModeImm5S(N, 2, Base, OffImm);
1038 ARMDAGToDAGISel::SelectThumbAddrModeImm5S1(SDValue N, SDValue &Base,
1040 return SelectThumbAddrModeImm5S(N, 1, Base, OffImm);
1043 bool ARMDAGToDAGISel::SelectThumbAddrModeSP(SDValue N,
1044 SDValue &Base, SDValue &OffImm) {
1045 if (N.getOpcode() == ISD::FrameIndex) {
1046 int FI = cast<FrameIndexSDNode>(N)->getIndex();
1047 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
1048 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
1052 if (N.getOpcode() != ISD::ADD)
1055 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
1056 if (N.getOperand(0).getOpcode() == ISD::FrameIndex ||
1057 (LHSR && LHSR->getReg() == ARM::SP)) {
1058 // If the RHS is + imm8 * scale, fold into addr mode.
1060 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/4, 0, 256, RHSC)) {
1061 Base = N.getOperand(0);
1062 if (Base.getOpcode() == ISD::FrameIndex) {
1063 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
1064 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
1066 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
1075 //===----------------------------------------------------------------------===//
1076 // Thumb 2 Addressing Modes
1077 //===----------------------------------------------------------------------===//
1080 bool ARMDAGToDAGISel::SelectT2ShifterOperandReg(SDValue N, SDValue &BaseReg,
1082 if (DisableShifterOp)
1085 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
1087 // Don't match base register only case. That is matched to a separate
1088 // lower complexity pattern with explicit register operand.
1089 if (ShOpcVal == ARM_AM::no_shift) return false;
1091 BaseReg = N.getOperand(0);
1092 unsigned ShImmVal = 0;
1093 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1094 ShImmVal = RHS->getZExtValue() & 31;
1095 Opc = getI32Imm(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal));
1102 bool ARMDAGToDAGISel::SelectT2AddrModeImm12(SDValue N,
1103 SDValue &Base, SDValue &OffImm) {
1104 // Match simple R + imm12 operands.
1107 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB) {
1108 if (N.getOpcode() == ISD::FrameIndex) {
1109 // Match frame index...
1110 int FI = cast<FrameIndexSDNode>(N)->getIndex();
1111 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
1112 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
1114 } else if (N.getOpcode() == ARMISD::Wrapper &&
1115 !(Subtarget->useMovt() &&
1116 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
1117 Base = N.getOperand(0);
1118 if (Base.getOpcode() == ISD::TargetConstantPool)
1119 return false; // We want to select t2LDRpci instead.
1122 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
1126 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1127 if (SelectT2AddrModeImm8(N, Base, OffImm))
1128 // Let t2LDRi8 handle (R - imm8).
1131 int RHSC = (int)RHS->getZExtValue();
1132 if (N.getOpcode() == ISD::SUB)
1135 if (RHSC >= 0 && RHSC < 0x1000) { // 12 bits (unsigned)
1136 Base = N.getOperand(0);
1137 if (Base.getOpcode() == ISD::FrameIndex) {
1138 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
1139 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
1141 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
1148 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
1152 bool ARMDAGToDAGISel::SelectT2AddrModeImm8(SDValue N,
1153 SDValue &Base, SDValue &OffImm) {
1154 // Match simple R - imm8 operands.
1155 if (N.getOpcode() == ISD::ADD || N.getOpcode() == ISD::SUB) {
1156 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1157 int RHSC = (int)RHS->getSExtValue();
1158 if (N.getOpcode() == ISD::SUB)
1161 if ((RHSC >= -255) && (RHSC < 0)) { // 8 bits (always negative)
1162 Base = N.getOperand(0);
1163 if (Base.getOpcode() == ISD::FrameIndex) {
1164 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
1165 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
1167 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
1176 bool ARMDAGToDAGISel::SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
1178 unsigned Opcode = Op->getOpcode();
1179 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
1180 ? cast<LoadSDNode>(Op)->getAddressingMode()
1181 : cast<StoreSDNode>(Op)->getAddressingMode();
1183 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x100, RHSC)) { // 8 bits.
1184 OffImm = ((AM == ISD::PRE_INC) || (AM == ISD::POST_INC))
1185 ? CurDAG->getTargetConstant(RHSC, MVT::i32)
1186 : CurDAG->getTargetConstant(-RHSC, MVT::i32);
1193 bool ARMDAGToDAGISel::SelectT2AddrModeSoReg(SDValue N,
1195 SDValue &OffReg, SDValue &ShImm) {
1196 // (R - imm8) should be handled by t2LDRi8. The rest are handled by t2LDRi12.
1197 if (N.getOpcode() != ISD::ADD)
1200 // Leave (R + imm12) for t2LDRi12, (R - imm8) for t2LDRi8.
1201 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1202 int RHSC = (int)RHS->getZExtValue();
1203 if (RHSC >= 0 && RHSC < 0x1000) // 12 bits (unsigned)
1205 else if (RHSC < 0 && RHSC >= -255) // 8 bits
1209 if (Subtarget->isCortexA9() && !N.hasOneUse()) {
1210 // Compute R + (R << [1,2,3]) and reuse it.
1215 // Look for (R + R) or (R + (R << [1,2,3])).
1217 Base = N.getOperand(0);
1218 OffReg = N.getOperand(1);
1220 // Swap if it is ((R << c) + R).
1221 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(OffReg);
1222 if (ShOpcVal != ARM_AM::lsl) {
1223 ShOpcVal = ARM_AM::getShiftOpcForNode(Base);
1224 if (ShOpcVal == ARM_AM::lsl)
1225 std::swap(Base, OffReg);
1228 if (ShOpcVal == ARM_AM::lsl) {
1229 // Check to see if the RHS of the shift is a constant, if not, we can't fold
1231 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(OffReg.getOperand(1))) {
1232 ShAmt = Sh->getZExtValue();
1233 if (ShAmt < 4 && isShifterOpProfitable(OffReg, ShOpcVal, ShAmt))
1234 OffReg = OffReg.getOperand(0);
1237 ShOpcVal = ARM_AM::no_shift;
1240 ShOpcVal = ARM_AM::no_shift;
1244 ShImm = CurDAG->getTargetConstant(ShAmt, MVT::i32);
1249 //===--------------------------------------------------------------------===//
1251 /// getAL - Returns a ARMCC::AL immediate node.
1252 static inline SDValue getAL(SelectionDAG *CurDAG) {
1253 return CurDAG->getTargetConstant((uint64_t)ARMCC::AL, MVT::i32);
1256 SDNode *ARMDAGToDAGISel::SelectARMIndexedLoad(SDNode *N) {
1257 LoadSDNode *LD = cast<LoadSDNode>(N);
1258 ISD::MemIndexedMode AM = LD->getAddressingMode();
1259 if (AM == ISD::UNINDEXED)
1262 EVT LoadedVT = LD->getMemoryVT();
1263 SDValue Offset, AMOpc;
1264 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
1265 unsigned Opcode = 0;
1267 if (LoadedVT == MVT::i32 &&
1268 SelectAddrMode2Offset(N, LD->getOffset(), Offset, AMOpc)) {
1269 Opcode = isPre ? ARM::LDR_PRE : ARM::LDR_POST;
1271 } else if (LoadedVT == MVT::i16 &&
1272 SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
1274 Opcode = (LD->getExtensionType() == ISD::SEXTLOAD)
1275 ? (isPre ? ARM::LDRSH_PRE : ARM::LDRSH_POST)
1276 : (isPre ? ARM::LDRH_PRE : ARM::LDRH_POST);
1277 } else if (LoadedVT == MVT::i8 || LoadedVT == MVT::i1) {
1278 if (LD->getExtensionType() == ISD::SEXTLOAD) {
1279 if (SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
1281 Opcode = isPre ? ARM::LDRSB_PRE : ARM::LDRSB_POST;
1284 if (SelectAddrMode2Offset(N, LD->getOffset(), Offset, AMOpc)) {
1286 Opcode = isPre ? ARM::LDRB_PRE : ARM::LDRB_POST;
1292 SDValue Chain = LD->getChain();
1293 SDValue Base = LD->getBasePtr();
1294 SDValue Ops[]= { Base, Offset, AMOpc, getAL(CurDAG),
1295 CurDAG->getRegister(0, MVT::i32), Chain };
1296 return CurDAG->getMachineNode(Opcode, N->getDebugLoc(), MVT::i32, MVT::i32,
1297 MVT::Other, Ops, 6);
1303 SDNode *ARMDAGToDAGISel::SelectT2IndexedLoad(SDNode *N) {
1304 LoadSDNode *LD = cast<LoadSDNode>(N);
1305 ISD::MemIndexedMode AM = LD->getAddressingMode();
1306 if (AM == ISD::UNINDEXED)
1309 EVT LoadedVT = LD->getMemoryVT();
1310 bool isSExtLd = LD->getExtensionType() == ISD::SEXTLOAD;
1312 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
1313 unsigned Opcode = 0;
1315 if (SelectT2AddrModeImm8Offset(N, LD->getOffset(), Offset)) {
1316 switch (LoadedVT.getSimpleVT().SimpleTy) {
1318 Opcode = isPre ? ARM::t2LDR_PRE : ARM::t2LDR_POST;
1322 Opcode = isPre ? ARM::t2LDRSH_PRE : ARM::t2LDRSH_POST;
1324 Opcode = isPre ? ARM::t2LDRH_PRE : ARM::t2LDRH_POST;
1329 Opcode = isPre ? ARM::t2LDRSB_PRE : ARM::t2LDRSB_POST;
1331 Opcode = isPre ? ARM::t2LDRB_PRE : ARM::t2LDRB_POST;
1340 SDValue Chain = LD->getChain();
1341 SDValue Base = LD->getBasePtr();
1342 SDValue Ops[]= { Base, Offset, getAL(CurDAG),
1343 CurDAG->getRegister(0, MVT::i32), Chain };
1344 return CurDAG->getMachineNode(Opcode, N->getDebugLoc(), MVT::i32, MVT::i32,
1345 MVT::Other, Ops, 5);
1351 /// PairSRegs - Form a D register from a pair of S registers.
1353 SDNode *ARMDAGToDAGISel::PairSRegs(EVT VT, SDValue V0, SDValue V1) {
1354 DebugLoc dl = V0.getNode()->getDebugLoc();
1355 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, MVT::i32);
1356 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, MVT::i32);
1357 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
1358 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
1361 /// PairDRegs - Form a quad register from a pair of D registers.
1363 SDNode *ARMDAGToDAGISel::PairDRegs(EVT VT, SDValue V0, SDValue V1) {
1364 DebugLoc dl = V0.getNode()->getDebugLoc();
1365 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
1366 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
1367 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
1368 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
1371 /// PairQRegs - Form 4 consecutive D registers from a pair of Q registers.
1373 SDNode *ARMDAGToDAGISel::PairQRegs(EVT VT, SDValue V0, SDValue V1) {
1374 DebugLoc dl = V0.getNode()->getDebugLoc();
1375 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, MVT::i32);
1376 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, MVT::i32);
1377 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
1378 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
1381 /// QuadSRegs - Form 4 consecutive S registers.
1383 SDNode *ARMDAGToDAGISel::QuadSRegs(EVT VT, SDValue V0, SDValue V1,
1384 SDValue V2, SDValue V3) {
1385 DebugLoc dl = V0.getNode()->getDebugLoc();
1386 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, MVT::i32);
1387 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, MVT::i32);
1388 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::ssub_2, MVT::i32);
1389 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::ssub_3, MVT::i32);
1390 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1, V2, SubReg2, V3, SubReg3 };
1391 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 8);
1394 /// QuadDRegs - Form 4 consecutive D registers.
1396 SDNode *ARMDAGToDAGISel::QuadDRegs(EVT VT, SDValue V0, SDValue V1,
1397 SDValue V2, SDValue V3) {
1398 DebugLoc dl = V0.getNode()->getDebugLoc();
1399 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
1400 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
1401 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::dsub_2, MVT::i32);
1402 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::dsub_3, MVT::i32);
1403 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1, V2, SubReg2, V3, SubReg3 };
1404 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 8);
1407 /// QuadQRegs - Form 4 consecutive Q registers.
1409 SDNode *ARMDAGToDAGISel::QuadQRegs(EVT VT, SDValue V0, SDValue V1,
1410 SDValue V2, SDValue V3) {
1411 DebugLoc dl = V0.getNode()->getDebugLoc();
1412 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, MVT::i32);
1413 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, MVT::i32);
1414 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::qsub_2, MVT::i32);
1415 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::qsub_3, MVT::i32);
1416 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1, V2, SubReg2, V3, SubReg3 };
1417 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 8);
1420 /// GetVLDSTAlign - Get the alignment (in bytes) for the alignment operand
1421 /// of a NEON VLD or VST instruction. The supported values depend on the
1422 /// number of registers being loaded.
1423 SDValue ARMDAGToDAGISel::GetVLDSTAlign(SDValue Align, unsigned NumVecs,
1424 bool is64BitVector) {
1425 unsigned NumRegs = NumVecs;
1426 if (!is64BitVector && NumVecs < 3)
1429 unsigned Alignment = cast<ConstantSDNode>(Align)->getZExtValue();
1430 if (Alignment >= 32 && NumRegs == 4)
1432 else if (Alignment >= 16 && (NumRegs == 2 || NumRegs == 4))
1434 else if (Alignment >= 8)
1439 return CurDAG->getTargetConstant(Alignment, MVT::i32);
1442 SDNode *ARMDAGToDAGISel::SelectVLD(SDNode *N, unsigned NumVecs,
1443 unsigned *DOpcodes, unsigned *QOpcodes0,
1444 unsigned *QOpcodes1) {
1445 assert(NumVecs >= 1 && NumVecs <= 4 && "VLD NumVecs out-of-range");
1446 DebugLoc dl = N->getDebugLoc();
1448 SDValue MemAddr, Align;
1449 if (!SelectAddrMode6(N, N->getOperand(2), MemAddr, Align))
1452 SDValue Chain = N->getOperand(0);
1453 EVT VT = N->getValueType(0);
1454 bool is64BitVector = VT.is64BitVector();
1455 Align = GetVLDSTAlign(Align, NumVecs, is64BitVector);
1457 unsigned OpcodeIndex;
1458 switch (VT.getSimpleVT().SimpleTy) {
1459 default: llvm_unreachable("unhandled vld type");
1460 // Double-register operations:
1461 case MVT::v8i8: OpcodeIndex = 0; break;
1462 case MVT::v4i16: OpcodeIndex = 1; break;
1464 case MVT::v2i32: OpcodeIndex = 2; break;
1465 case MVT::v1i64: OpcodeIndex = 3; break;
1466 // Quad-register operations:
1467 case MVT::v16i8: OpcodeIndex = 0; break;
1468 case MVT::v8i16: OpcodeIndex = 1; break;
1470 case MVT::v4i32: OpcodeIndex = 2; break;
1471 case MVT::v2i64: OpcodeIndex = 3;
1472 assert(NumVecs == 1 && "v2i64 type only supported for VLD1");
1480 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
1483 ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts);
1486 SDValue Pred = getAL(CurDAG);
1487 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1489 if (is64BitVector) {
1490 const SDValue Ops[] = { MemAddr, Align, Pred, Reg0, Chain };
1491 SDNode *VLd = CurDAG->getMachineNode(DOpcodes[OpcodeIndex], dl,
1492 ResTy, MVT::Other, Ops, 5);
1496 SuperReg = SDValue(VLd, 0);
1497 assert(ARM::dsub_7 == ARM::dsub_0+7 && "Unexpected subreg numbering");
1498 for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
1499 SDValue D = CurDAG->getTargetExtractSubreg(ARM::dsub_0+Vec,
1501 ReplaceUses(SDValue(N, Vec), D);
1503 ReplaceUses(SDValue(N, NumVecs), SDValue(VLd, 1));
1508 // Quad registers are directly supported for VLD1 and VLD2,
1509 // loading pairs of D regs.
1510 const SDValue Ops[] = { MemAddr, Align, Pred, Reg0, Chain };
1511 SDNode *VLd = CurDAG->getMachineNode(QOpcodes0[OpcodeIndex], dl,
1512 ResTy, MVT::Other, Ops, 5);
1516 SuperReg = SDValue(VLd, 0);
1517 Chain = SDValue(VLd, 1);
1520 // Otherwise, quad registers are loaded with two separate instructions,
1521 // where one loads the even registers and the other loads the odd registers.
1522 EVT AddrTy = MemAddr.getValueType();
1524 // Load the even subregs.
1526 SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, ResTy), 0);
1527 const SDValue OpsA[] = { MemAddr, Align, Reg0, ImplDef, Pred, Reg0, Chain };
1528 SDNode *VLdA = CurDAG->getMachineNode(QOpcodes0[OpcodeIndex], dl,
1529 ResTy, AddrTy, MVT::Other, OpsA, 7);
1530 Chain = SDValue(VLdA, 2);
1532 // Load the odd subregs.
1533 const SDValue OpsB[] = { SDValue(VLdA, 1), Align, SDValue(VLdA, 0),
1534 Pred, Reg0, Chain };
1535 SDNode *VLdB = CurDAG->getMachineNode(QOpcodes1[OpcodeIndex], dl,
1536 ResTy, MVT::Other, OpsB, 6);
1537 SuperReg = SDValue(VLdB, 0);
1538 Chain = SDValue(VLdB, 1);
1541 // Extract out the Q registers.
1542 assert(ARM::qsub_3 == ARM::qsub_0+3 && "Unexpected subreg numbering");
1543 for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
1544 SDValue Q = CurDAG->getTargetExtractSubreg(ARM::qsub_0+Vec,
1546 ReplaceUses(SDValue(N, Vec), Q);
1548 ReplaceUses(SDValue(N, NumVecs), Chain);
1552 SDNode *ARMDAGToDAGISel::SelectVST(SDNode *N, unsigned NumVecs,
1553 unsigned *DOpcodes, unsigned *QOpcodes0,
1554 unsigned *QOpcodes1) {
1555 assert(NumVecs >= 1 && NumVecs <= 4 && "VST NumVecs out-of-range");
1556 DebugLoc dl = N->getDebugLoc();
1558 SDValue MemAddr, Align;
1559 if (!SelectAddrMode6(N, N->getOperand(2), MemAddr, Align))
1562 SDValue Chain = N->getOperand(0);
1563 EVT VT = N->getOperand(3).getValueType();
1564 bool is64BitVector = VT.is64BitVector();
1565 Align = GetVLDSTAlign(Align, NumVecs, is64BitVector);
1567 unsigned OpcodeIndex;
1568 switch (VT.getSimpleVT().SimpleTy) {
1569 default: llvm_unreachable("unhandled vst type");
1570 // Double-register operations:
1571 case MVT::v8i8: OpcodeIndex = 0; break;
1572 case MVT::v4i16: OpcodeIndex = 1; break;
1574 case MVT::v2i32: OpcodeIndex = 2; break;
1575 case MVT::v1i64: OpcodeIndex = 3; break;
1576 // Quad-register operations:
1577 case MVT::v16i8: OpcodeIndex = 0; break;
1578 case MVT::v8i16: OpcodeIndex = 1; break;
1580 case MVT::v4i32: OpcodeIndex = 2; break;
1581 case MVT::v2i64: OpcodeIndex = 3;
1582 assert(NumVecs == 1 && "v2i64 type only supported for VST1");
1586 SDValue Pred = getAL(CurDAG);
1587 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1589 if (is64BitVector) {
1592 SrcReg = N->getOperand(3);
1594 SDValue V0 = N->getOperand(0+3);
1595 SDValue V1 = N->getOperand(1+3);
1597 // Form a REG_SEQUENCE to force register allocation.
1599 SrcReg = SDValue(PairDRegs(MVT::v2i64, V0, V1), 0);
1601 SDValue V2 = N->getOperand(2+3);
1602 // If it's a vst3, form a quad D-register and leave the last part as
1604 SDValue V3 = (NumVecs == 3)
1605 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
1606 : N->getOperand(3+3);
1607 SrcReg = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1610 const SDValue Ops[] = { MemAddr, Align, SrcReg, Pred, Reg0, Chain };
1611 return CurDAG->getMachineNode(DOpcodes[OpcodeIndex], dl,
1612 MVT::Other, Ops, 6);
1616 // Quad registers are directly supported for VST1 and VST2.
1619 SrcReg = N->getOperand(3);
1621 // Form a QQ register.
1622 SDValue Q0 = N->getOperand(3);
1623 SDValue Q1 = N->getOperand(4);
1624 SrcReg = SDValue(PairQRegs(MVT::v4i64, Q0, Q1), 0);
1626 const SDValue Ops[] = { MemAddr, Align, SrcReg, Pred, Reg0, Chain };
1627 return CurDAG->getMachineNode(QOpcodes0[OpcodeIndex], dl,
1628 MVT::Other, Ops, 6);
1631 // Otherwise, quad registers are stored with two separate instructions,
1632 // where one stores the even registers and the other stores the odd registers.
1634 // Form the QQQQ REG_SEQUENCE.
1635 SDValue V0 = N->getOperand(0+3);
1636 SDValue V1 = N->getOperand(1+3);
1637 SDValue V2 = N->getOperand(2+3);
1638 SDValue V3 = (NumVecs == 3)
1639 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
1640 : N->getOperand(3+3);
1641 SDValue RegSeq = SDValue(QuadQRegs(MVT::v8i64, V0, V1, V2, V3), 0);
1643 // Store the even D registers.
1644 const SDValue OpsA[] = { MemAddr, Align, Reg0, RegSeq, Pred, Reg0, Chain };
1645 SDNode *VStA = CurDAG->getMachineNode(QOpcodes0[OpcodeIndex], dl,
1646 MemAddr.getValueType(),
1647 MVT::Other, OpsA, 7);
1648 Chain = SDValue(VStA, 1);
1650 // Store the odd D registers.
1651 const SDValue OpsB[] = { SDValue(VStA, 0), Align, RegSeq, Pred, Reg0, Chain };
1652 SDNode *VStB = CurDAG->getMachineNode(QOpcodes1[OpcodeIndex], dl,
1653 MVT::Other, OpsB, 6);
1654 Chain = SDValue(VStB, 0);
1655 ReplaceUses(SDValue(N, 0), Chain);
1659 SDNode *ARMDAGToDAGISel::SelectVLDSTLane(SDNode *N, bool IsLoad,
1660 unsigned NumVecs, unsigned *DOpcodes,
1661 unsigned *QOpcodes) {
1662 assert(NumVecs >=2 && NumVecs <= 4 && "VLDSTLane NumVecs out-of-range");
1663 DebugLoc dl = N->getDebugLoc();
1665 SDValue MemAddr, Align;
1666 if (!SelectAddrMode6(N, N->getOperand(2), MemAddr, Align))
1669 SDValue Chain = N->getOperand(0);
1671 cast<ConstantSDNode>(N->getOperand(NumVecs+3))->getZExtValue();
1672 EVT VT = IsLoad ? N->getValueType(0) : N->getOperand(3).getValueType();
1673 bool is64BitVector = VT.is64BitVector();
1675 unsigned Alignment = 0;
1677 Alignment = cast<ConstantSDNode>(Align)->getZExtValue();
1678 unsigned NumBytes = NumVecs * VT.getVectorElementType().getSizeInBits()/8;
1679 if (Alignment > NumBytes)
1680 Alignment = NumBytes;
1681 if (Alignment < 8 && Alignment < NumBytes)
1683 // Alignment must be a power of two; make sure of that.
1684 Alignment = (Alignment & -Alignment);
1688 Align = CurDAG->getTargetConstant(Alignment, MVT::i32);
1690 unsigned OpcodeIndex;
1691 switch (VT.getSimpleVT().SimpleTy) {
1692 default: llvm_unreachable("unhandled vld/vst lane type");
1693 // Double-register operations:
1694 case MVT::v8i8: OpcodeIndex = 0; break;
1695 case MVT::v4i16: OpcodeIndex = 1; break;
1697 case MVT::v2i32: OpcodeIndex = 2; break;
1698 // Quad-register operations:
1699 case MVT::v8i16: OpcodeIndex = 0; break;
1701 case MVT::v4i32: OpcodeIndex = 1; break;
1704 SDValue Pred = getAL(CurDAG);
1705 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1707 SmallVector<SDValue, 7> Ops;
1708 Ops.push_back(MemAddr);
1709 Ops.push_back(Align);
1711 unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
1712 QOpcodes[OpcodeIndex]);
1715 SDValue V0 = N->getOperand(0+3);
1716 SDValue V1 = N->getOperand(1+3);
1719 SuperReg = SDValue(PairDRegs(MVT::v2i64, V0, V1), 0);
1721 SuperReg = SDValue(PairQRegs(MVT::v4i64, V0, V1), 0);
1723 SDValue V2 = N->getOperand(2+3);
1724 SDValue V3 = (NumVecs == 3)
1725 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
1726 : N->getOperand(3+3);
1728 SuperReg = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1730 SuperReg = SDValue(QuadQRegs(MVT::v8i64, V0, V1, V2, V3), 0);
1732 Ops.push_back(SuperReg);
1733 Ops.push_back(getI32Imm(Lane));
1734 Ops.push_back(Pred);
1735 Ops.push_back(Reg0);
1736 Ops.push_back(Chain);
1739 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 7);
1742 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
1745 ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts);
1747 SDNode *VLdLn = CurDAG->getMachineNode(Opc, dl, ResTy, MVT::Other,
1749 SuperReg = SDValue(VLdLn, 0);
1750 Chain = SDValue(VLdLn, 1);
1752 // Extract the subregisters.
1753 assert(ARM::dsub_7 == ARM::dsub_0+7 && "Unexpected subreg numbering");
1754 assert(ARM::qsub_3 == ARM::qsub_0+3 && "Unexpected subreg numbering");
1755 unsigned SubIdx = is64BitVector ? ARM::dsub_0 : ARM::qsub_0;
1756 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1757 ReplaceUses(SDValue(N, Vec),
1758 CurDAG->getTargetExtractSubreg(SubIdx+Vec, dl, VT, SuperReg));
1759 ReplaceUses(SDValue(N, NumVecs), Chain);
1763 SDNode *ARMDAGToDAGISel::SelectVLDDup(SDNode *N, unsigned NumVecs,
1764 unsigned *Opcodes) {
1765 assert(NumVecs >=2 && NumVecs <= 4 && "VLDDup NumVecs out-of-range");
1766 DebugLoc dl = N->getDebugLoc();
1768 SDValue MemAddr, Align;
1769 if (!SelectAddrMode6(N, N->getOperand(1), MemAddr, Align))
1772 SDValue Chain = N->getOperand(0);
1773 EVT VT = N->getValueType(0);
1775 unsigned Alignment = 0;
1777 Alignment = cast<ConstantSDNode>(Align)->getZExtValue();
1778 unsigned NumBytes = NumVecs * VT.getVectorElementType().getSizeInBits()/8;
1779 if (Alignment > NumBytes)
1780 Alignment = NumBytes;
1781 if (Alignment < 8 && Alignment < NumBytes)
1783 // Alignment must be a power of two; make sure of that.
1784 Alignment = (Alignment & -Alignment);
1788 Align = CurDAG->getTargetConstant(Alignment, MVT::i32);
1790 unsigned OpcodeIndex;
1791 switch (VT.getSimpleVT().SimpleTy) {
1792 default: llvm_unreachable("unhandled vld-dup type");
1793 case MVT::v8i8: OpcodeIndex = 0; break;
1794 case MVT::v4i16: OpcodeIndex = 1; break;
1796 case MVT::v2i32: OpcodeIndex = 2; break;
1799 SDValue Pred = getAL(CurDAG);
1800 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1802 unsigned Opc = Opcodes[OpcodeIndex];
1803 const SDValue Ops[] = { MemAddr, Align, Pred, Reg0, Chain };
1805 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
1806 EVT ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts);
1807 SDNode *VLdDup = CurDAG->getMachineNode(Opc, dl, ResTy, MVT::Other, Ops, 5);
1808 SuperReg = SDValue(VLdDup, 0);
1809 Chain = SDValue(VLdDup, 1);
1811 // Extract the subregisters.
1812 assert(ARM::dsub_7 == ARM::dsub_0+7 && "Unexpected subreg numbering");
1813 unsigned SubIdx = ARM::dsub_0;
1814 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1815 ReplaceUses(SDValue(N, Vec),
1816 CurDAG->getTargetExtractSubreg(SubIdx+Vec, dl, VT, SuperReg));
1817 ReplaceUses(SDValue(N, NumVecs), Chain);
1821 SDNode *ARMDAGToDAGISel::SelectVTBL(SDNode *N, bool IsExt, unsigned NumVecs,
1823 assert(NumVecs >= 2 && NumVecs <= 4 && "VTBL NumVecs out-of-range");
1824 DebugLoc dl = N->getDebugLoc();
1825 EVT VT = N->getValueType(0);
1826 unsigned FirstTblReg = IsExt ? 2 : 1;
1828 // Form a REG_SEQUENCE to force register allocation.
1830 SDValue V0 = N->getOperand(FirstTblReg + 0);
1831 SDValue V1 = N->getOperand(FirstTblReg + 1);
1833 RegSeq = SDValue(PairDRegs(MVT::v16i8, V0, V1), 0);
1835 SDValue V2 = N->getOperand(FirstTblReg + 2);
1836 // If it's a vtbl3, form a quad D-register and leave the last part as
1838 SDValue V3 = (NumVecs == 3)
1839 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
1840 : N->getOperand(FirstTblReg + 3);
1841 RegSeq = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1844 SmallVector<SDValue, 6> Ops;
1846 Ops.push_back(N->getOperand(1));
1847 Ops.push_back(RegSeq);
1848 Ops.push_back(N->getOperand(FirstTblReg + NumVecs));
1849 Ops.push_back(getAL(CurDAG)); // predicate
1850 Ops.push_back(CurDAG->getRegister(0, MVT::i32)); // predicate register
1851 return CurDAG->getMachineNode(Opc, dl, VT, Ops.data(), Ops.size());
1854 SDNode *ARMDAGToDAGISel::SelectV6T2BitfieldExtractOp(SDNode *N,
1856 if (!Subtarget->hasV6T2Ops())
1859 unsigned Opc = isSigned ? (Subtarget->isThumb() ? ARM::t2SBFX : ARM::SBFX)
1860 : (Subtarget->isThumb() ? ARM::t2UBFX : ARM::UBFX);
1863 // For unsigned extracts, check for a shift right and mask
1864 unsigned And_imm = 0;
1865 if (N->getOpcode() == ISD::AND) {
1866 if (isOpcWithIntImmediate(N, ISD::AND, And_imm)) {
1868 // The immediate is a mask of the low bits iff imm & (imm+1) == 0
1869 if (And_imm & (And_imm + 1))
1872 unsigned Srl_imm = 0;
1873 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRL,
1875 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
1877 unsigned Width = CountTrailingOnes_32(And_imm);
1878 unsigned LSB = Srl_imm;
1879 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1880 SDValue Ops[] = { N->getOperand(0).getOperand(0),
1881 CurDAG->getTargetConstant(LSB, MVT::i32),
1882 CurDAG->getTargetConstant(Width, MVT::i32),
1883 getAL(CurDAG), Reg0 };
1884 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
1890 // Otherwise, we're looking for a shift of a shift
1891 unsigned Shl_imm = 0;
1892 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SHL, Shl_imm)) {
1893 assert(Shl_imm > 0 && Shl_imm < 32 && "bad amount in shift node!");
1894 unsigned Srl_imm = 0;
1895 if (isInt32Immediate(N->getOperand(1), Srl_imm)) {
1896 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
1897 unsigned Width = 32 - Srl_imm;
1898 int LSB = Srl_imm - Shl_imm;
1901 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1902 SDValue Ops[] = { N->getOperand(0).getOperand(0),
1903 CurDAG->getTargetConstant(LSB, MVT::i32),
1904 CurDAG->getTargetConstant(Width, MVT::i32),
1905 getAL(CurDAG), Reg0 };
1906 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
1912 SDNode *ARMDAGToDAGISel::
1913 SelectT2CMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
1914 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1917 if (SelectT2ShifterOperandReg(TrueVal, CPTmp0, CPTmp1)) {
1918 unsigned SOVal = cast<ConstantSDNode>(CPTmp1)->getZExtValue();
1919 unsigned SOShOp = ARM_AM::getSORegShOp(SOVal);
1922 case ARM_AM::lsl: Opc = ARM::t2MOVCClsl; break;
1923 case ARM_AM::lsr: Opc = ARM::t2MOVCClsr; break;
1924 case ARM_AM::asr: Opc = ARM::t2MOVCCasr; break;
1925 case ARM_AM::ror: Opc = ARM::t2MOVCCror; break;
1927 llvm_unreachable("Unknown so_reg opcode!");
1931 CurDAG->getTargetConstant(ARM_AM::getSORegOffset(SOVal), MVT::i32);
1932 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1933 SDValue Ops[] = { FalseVal, CPTmp0, SOShImm, CC, CCR, InFlag };
1934 return CurDAG->SelectNodeTo(N, Opc, MVT::i32,Ops, 6);
1939 SDNode *ARMDAGToDAGISel::
1940 SelectARMCMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
1941 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1945 if (SelectShifterOperandReg(TrueVal, CPTmp0, CPTmp1, CPTmp2)) {
1946 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1947 SDValue Ops[] = { FalseVal, CPTmp0, CPTmp1, CPTmp2, CC, CCR, InFlag };
1948 return CurDAG->SelectNodeTo(N, ARM::MOVCCs, MVT::i32, Ops, 7);
1953 SDNode *ARMDAGToDAGISel::
1954 SelectT2CMOVImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
1955 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1956 ConstantSDNode *T = dyn_cast<ConstantSDNode>(TrueVal);
1961 unsigned TrueImm = T->getZExtValue();
1962 if (is_t2_so_imm(TrueImm)) {
1963 Opc = ARM::t2MOVCCi;
1964 } else if (TrueImm <= 0xffff) {
1965 Opc = ARM::t2MOVCCi16;
1966 } else if (is_t2_so_imm_not(TrueImm)) {
1968 Opc = ARM::t2MVNCCi;
1969 } else if (TrueVal.getNode()->hasOneUse() && Subtarget->hasV6T2Ops()) {
1971 Opc = ARM::t2MOVCCi32imm;
1975 SDValue True = CurDAG->getTargetConstant(TrueImm, MVT::i32);
1976 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1977 SDValue Ops[] = { FalseVal, True, CC, CCR, InFlag };
1978 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
1984 SDNode *ARMDAGToDAGISel::
1985 SelectARMCMOVImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
1986 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1987 ConstantSDNode *T = dyn_cast<ConstantSDNode>(TrueVal);
1992 unsigned TrueImm = T->getZExtValue();
1993 bool isSoImm = is_so_imm(TrueImm);
1996 } else if (Subtarget->hasV6T2Ops() && TrueImm <= 0xffff) {
1997 Opc = ARM::MOVCCi16;
1998 } else if (is_so_imm_not(TrueImm)) {
2001 } else if (TrueVal.getNode()->hasOneUse() &&
2002 (Subtarget->hasV6T2Ops() || ARM_AM::isSOImmTwoPartVal(TrueImm))) {
2004 Opc = ARM::MOVCCi32imm;
2008 SDValue True = CurDAG->getTargetConstant(TrueImm, MVT::i32);
2009 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
2010 SDValue Ops[] = { FalseVal, True, CC, CCR, InFlag };
2011 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
2017 SDNode *ARMDAGToDAGISel::SelectCMOVOp(SDNode *N) {
2018 EVT VT = N->getValueType(0);
2019 SDValue FalseVal = N->getOperand(0);
2020 SDValue TrueVal = N->getOperand(1);
2021 SDValue CC = N->getOperand(2);
2022 SDValue CCR = N->getOperand(3);
2023 SDValue InFlag = N->getOperand(4);
2024 assert(CC.getOpcode() == ISD::Constant);
2025 assert(CCR.getOpcode() == ISD::Register);
2026 ARMCC::CondCodes CCVal =
2027 (ARMCC::CondCodes)cast<ConstantSDNode>(CC)->getZExtValue();
2029 if (!Subtarget->isThumb1Only() && VT == MVT::i32) {
2030 // Pattern: (ARMcmov:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
2031 // Emits: (MOVCCs:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
2032 // Pattern complexity = 18 cost = 1 size = 0
2036 if (Subtarget->isThumb()) {
2037 SDNode *Res = SelectT2CMOVShiftOp(N, FalseVal, TrueVal,
2038 CCVal, CCR, InFlag);
2040 Res = SelectT2CMOVShiftOp(N, TrueVal, FalseVal,
2041 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
2045 SDNode *Res = SelectARMCMOVShiftOp(N, FalseVal, TrueVal,
2046 CCVal, CCR, InFlag);
2048 Res = SelectARMCMOVShiftOp(N, TrueVal, FalseVal,
2049 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
2054 // Pattern: (ARMcmov:i32 GPR:i32:$false,
2055 // (imm:i32)<<P:Pred_so_imm>>:$true,
2057 // Emits: (MOVCCi:i32 GPR:i32:$false,
2058 // (so_imm:i32 (imm:i32):$true), (imm:i32):$cc)
2059 // Pattern complexity = 10 cost = 1 size = 0
2060 if (Subtarget->isThumb()) {
2061 SDNode *Res = SelectT2CMOVImmOp(N, FalseVal, TrueVal,
2062 CCVal, CCR, InFlag);
2064 Res = SelectT2CMOVImmOp(N, TrueVal, FalseVal,
2065 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
2069 SDNode *Res = SelectARMCMOVImmOp(N, FalseVal, TrueVal,
2070 CCVal, CCR, InFlag);
2072 Res = SelectARMCMOVImmOp(N, TrueVal, FalseVal,
2073 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
2079 // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
2080 // Emits: (MOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
2081 // Pattern complexity = 6 cost = 1 size = 0
2083 // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
2084 // Emits: (tMOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
2085 // Pattern complexity = 6 cost = 11 size = 0
2087 // Also FCPYScc and FCPYDcc.
2088 SDValue Tmp2 = CurDAG->getTargetConstant(CCVal, MVT::i32);
2089 SDValue Ops[] = { FalseVal, TrueVal, Tmp2, CCR, InFlag };
2091 switch (VT.getSimpleVT().SimpleTy) {
2092 default: assert(false && "Illegal conditional move type!");
2095 Opc = Subtarget->isThumb()
2096 ? (Subtarget->hasThumb2() ? ARM::t2MOVCCr : ARM::tMOVCCr_pseudo)
2106 return CurDAG->SelectNodeTo(N, Opc, VT, Ops, 5);
2109 SDNode *ARMDAGToDAGISel::SelectConcatVector(SDNode *N) {
2110 // The only time a CONCAT_VECTORS operation can have legal types is when
2111 // two 64-bit vectors are concatenated to a 128-bit vector.
2112 EVT VT = N->getValueType(0);
2113 if (!VT.is128BitVector() || N->getNumOperands() != 2)
2114 llvm_unreachable("unexpected CONCAT_VECTORS");
2115 return PairDRegs(VT, N->getOperand(0), N->getOperand(1));
2118 SDNode *ARMDAGToDAGISel::Select(SDNode *N) {
2119 DebugLoc dl = N->getDebugLoc();
2121 if (N->isMachineOpcode())
2122 return NULL; // Already selected.
2124 switch (N->getOpcode()) {
2126 case ISD::Constant: {
2127 unsigned Val = cast<ConstantSDNode>(N)->getZExtValue();
2129 if (Subtarget->hasThumb2())
2130 // Thumb2-aware targets have the MOVT instruction, so all immediates can
2131 // be done with MOV + MOVT, at worst.
2134 if (Subtarget->isThumb()) {
2135 UseCP = (Val > 255 && // MOV
2136 ~Val > 255 && // MOV + MVN
2137 !ARM_AM::isThumbImmShiftedVal(Val)); // MOV + LSL
2139 UseCP = (ARM_AM::getSOImmVal(Val) == -1 && // MOV
2140 ARM_AM::getSOImmVal(~Val) == -1 && // MVN
2141 !ARM_AM::isSOImmTwoPartVal(Val)); // two instrs.
2146 CurDAG->getTargetConstantPool(ConstantInt::get(
2147 Type::getInt32Ty(*CurDAG->getContext()), Val),
2148 TLI.getPointerTy());
2151 if (Subtarget->isThumb1Only()) {
2152 SDValue Pred = getAL(CurDAG);
2153 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
2154 SDValue Ops[] = { CPIdx, Pred, PredReg, CurDAG->getEntryNode() };
2155 ResNode = CurDAG->getMachineNode(ARM::tLDRpci, dl, MVT::i32, MVT::Other,
2160 CurDAG->getTargetConstant(0, MVT::i32),
2162 CurDAG->getRegister(0, MVT::i32),
2163 CurDAG->getEntryNode()
2165 ResNode=CurDAG->getMachineNode(ARM::LDRcp, dl, MVT::i32, MVT::Other,
2168 ReplaceUses(SDValue(N, 0), SDValue(ResNode, 0));
2172 // Other cases are autogenerated.
2175 case ISD::FrameIndex: {
2176 // Selects to ADDri FI, 0 which in turn will become ADDri SP, imm.
2177 int FI = cast<FrameIndexSDNode>(N)->getIndex();
2178 SDValue TFI = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
2179 if (Subtarget->isThumb1Only()) {
2180 return CurDAG->SelectNodeTo(N, ARM::tADDrSPi, MVT::i32, TFI,
2181 CurDAG->getTargetConstant(0, MVT::i32));
2183 unsigned Opc = ((Subtarget->isThumb() && Subtarget->hasThumb2()) ?
2184 ARM::t2ADDri : ARM::ADDri);
2185 SDValue Ops[] = { TFI, CurDAG->getTargetConstant(0, MVT::i32),
2186 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
2187 CurDAG->getRegister(0, MVT::i32) };
2188 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
2192 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, false))
2196 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, true))
2200 if (Subtarget->isThumb1Only())
2202 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
2203 unsigned RHSV = C->getZExtValue();
2205 if (isPowerOf2_32(RHSV-1)) { // 2^n+1?
2206 unsigned ShImm = Log2_32(RHSV-1);
2209 SDValue V = N->getOperand(0);
2210 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
2211 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, MVT::i32);
2212 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2213 if (Subtarget->isThumb()) {
2214 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
2215 return CurDAG->SelectNodeTo(N, ARM::t2ADDrs, MVT::i32, Ops, 6);
2217 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
2218 return CurDAG->SelectNodeTo(N, ARM::ADDrs, MVT::i32, Ops, 7);
2221 if (isPowerOf2_32(RHSV+1)) { // 2^n-1?
2222 unsigned ShImm = Log2_32(RHSV+1);
2225 SDValue V = N->getOperand(0);
2226 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
2227 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, MVT::i32);
2228 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2229 if (Subtarget->isThumb()) {
2230 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
2231 return CurDAG->SelectNodeTo(N, ARM::t2RSBrs, MVT::i32, Ops, 6);
2233 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
2234 return CurDAG->SelectNodeTo(N, ARM::RSBrs, MVT::i32, Ops, 7);
2240 // Check for unsigned bitfield extract
2241 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, false))
2244 // (and (or x, c2), c1) and top 16-bits of c1 and c2 match, lower 16-bits
2245 // of c1 are 0xffff, and lower 16-bit of c2 are 0. That is, the top 16-bits
2246 // are entirely contributed by c2 and lower 16-bits are entirely contributed
2247 // by x. That's equal to (or (and x, 0xffff), (and c1, 0xffff0000)).
2248 // Select it to: "movt x, ((c1 & 0xffff) >> 16)
2249 EVT VT = N->getValueType(0);
2252 unsigned Opc = (Subtarget->isThumb() && Subtarget->hasThumb2())
2254 : (Subtarget->hasV6T2Ops() ? ARM::MOVTi16 : 0);
2257 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
2258 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2261 if (N0.getOpcode() == ISD::OR && N0.getNode()->hasOneUse()) {
2262 SDValue N2 = N0.getOperand(1);
2263 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
2266 unsigned N1CVal = N1C->getZExtValue();
2267 unsigned N2CVal = N2C->getZExtValue();
2268 if ((N1CVal & 0xffff0000U) == (N2CVal & 0xffff0000U) &&
2269 (N1CVal & 0xffffU) == 0xffffU &&
2270 (N2CVal & 0xffffU) == 0x0U) {
2271 SDValue Imm16 = CurDAG->getTargetConstant((N2CVal & 0xFFFF0000U) >> 16,
2273 SDValue Ops[] = { N0.getOperand(0), Imm16,
2274 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
2275 return CurDAG->getMachineNode(Opc, dl, VT, Ops, 4);
2280 case ARMISD::VMOVRRD:
2281 return CurDAG->getMachineNode(ARM::VMOVRRD, dl, MVT::i32, MVT::i32,
2282 N->getOperand(0), getAL(CurDAG),
2283 CurDAG->getRegister(0, MVT::i32));
2284 case ISD::UMUL_LOHI: {
2285 if (Subtarget->isThumb1Only())
2287 if (Subtarget->isThumb()) {
2288 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
2289 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
2290 CurDAG->getRegister(0, MVT::i32) };
2291 return CurDAG->getMachineNode(ARM::t2UMULL, dl, MVT::i32, MVT::i32,Ops,4);
2293 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
2294 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
2295 CurDAG->getRegister(0, MVT::i32) };
2296 return CurDAG->getMachineNode(Subtarget->hasV6Ops() ?
2297 ARM::UMULL : ARM::UMULLv5,
2298 dl, MVT::i32, MVT::i32, Ops, 5);
2301 case ISD::SMUL_LOHI: {
2302 if (Subtarget->isThumb1Only())
2304 if (Subtarget->isThumb()) {
2305 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
2306 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
2307 return CurDAG->getMachineNode(ARM::t2SMULL, dl, MVT::i32, MVT::i32,Ops,4);
2309 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
2310 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
2311 CurDAG->getRegister(0, MVT::i32) };
2312 return CurDAG->getMachineNode(Subtarget->hasV6Ops() ?
2313 ARM::SMULL : ARM::SMULLv5,
2314 dl, MVT::i32, MVT::i32, Ops, 5);
2318 SDNode *ResNode = 0;
2319 if (Subtarget->isThumb() && Subtarget->hasThumb2())
2320 ResNode = SelectT2IndexedLoad(N);
2322 ResNode = SelectARMIndexedLoad(N);
2325 // Other cases are autogenerated.
2328 case ARMISD::BRCOND: {
2329 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
2330 // Emits: (Bcc:void (bb:Other):$dst, (imm:i32):$cc)
2331 // Pattern complexity = 6 cost = 1 size = 0
2333 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
2334 // Emits: (tBcc:void (bb:Other):$dst, (imm:i32):$cc)
2335 // Pattern complexity = 6 cost = 1 size = 0
2337 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
2338 // Emits: (t2Bcc:void (bb:Other):$dst, (imm:i32):$cc)
2339 // Pattern complexity = 6 cost = 1 size = 0
2341 unsigned Opc = Subtarget->isThumb() ?
2342 ((Subtarget->hasThumb2()) ? ARM::t2Bcc : ARM::tBcc) : ARM::Bcc;
2343 SDValue Chain = N->getOperand(0);
2344 SDValue N1 = N->getOperand(1);
2345 SDValue N2 = N->getOperand(2);
2346 SDValue N3 = N->getOperand(3);
2347 SDValue InFlag = N->getOperand(4);
2348 assert(N1.getOpcode() == ISD::BasicBlock);
2349 assert(N2.getOpcode() == ISD::Constant);
2350 assert(N3.getOpcode() == ISD::Register);
2352 SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
2353 cast<ConstantSDNode>(N2)->getZExtValue()),
2355 SDValue Ops[] = { N1, Tmp2, N3, Chain, InFlag };
2356 SDNode *ResNode = CurDAG->getMachineNode(Opc, dl, MVT::Other,
2358 Chain = SDValue(ResNode, 0);
2359 if (N->getNumValues() == 2) {
2360 InFlag = SDValue(ResNode, 1);
2361 ReplaceUses(SDValue(N, 1), InFlag);
2363 ReplaceUses(SDValue(N, 0),
2364 SDValue(Chain.getNode(), Chain.getResNo()));
2368 return SelectCMOVOp(N);
2369 case ARMISD::CNEG: {
2370 EVT VT = N->getValueType(0);
2371 SDValue N0 = N->getOperand(0);
2372 SDValue N1 = N->getOperand(1);
2373 SDValue N2 = N->getOperand(2);
2374 SDValue N3 = N->getOperand(3);
2375 SDValue InFlag = N->getOperand(4);
2376 assert(N2.getOpcode() == ISD::Constant);
2377 assert(N3.getOpcode() == ISD::Register);
2379 SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
2380 cast<ConstantSDNode>(N2)->getZExtValue()),
2382 SDValue Ops[] = { N0, N1, Tmp2, N3, InFlag };
2384 switch (VT.getSimpleVT().SimpleTy) {
2385 default: assert(false && "Illegal conditional move type!");
2394 return CurDAG->SelectNodeTo(N, Opc, VT, Ops, 5);
2397 case ARMISD::VZIP: {
2399 EVT VT = N->getValueType(0);
2400 switch (VT.getSimpleVT().SimpleTy) {
2401 default: return NULL;
2402 case MVT::v8i8: Opc = ARM::VZIPd8; break;
2403 case MVT::v4i16: Opc = ARM::VZIPd16; break;
2405 case MVT::v2i32: Opc = ARM::VZIPd32; break;
2406 case MVT::v16i8: Opc = ARM::VZIPq8; break;
2407 case MVT::v8i16: Opc = ARM::VZIPq16; break;
2409 case MVT::v4i32: Opc = ARM::VZIPq32; break;
2411 SDValue Pred = getAL(CurDAG);
2412 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
2413 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
2414 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
2416 case ARMISD::VUZP: {
2418 EVT VT = N->getValueType(0);
2419 switch (VT.getSimpleVT().SimpleTy) {
2420 default: return NULL;
2421 case MVT::v8i8: Opc = ARM::VUZPd8; break;
2422 case MVT::v4i16: Opc = ARM::VUZPd16; break;
2424 case MVT::v2i32: Opc = ARM::VUZPd32; break;
2425 case MVT::v16i8: Opc = ARM::VUZPq8; break;
2426 case MVT::v8i16: Opc = ARM::VUZPq16; break;
2428 case MVT::v4i32: Opc = ARM::VUZPq32; break;
2430 SDValue Pred = getAL(CurDAG);
2431 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
2432 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
2433 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
2435 case ARMISD::VTRN: {
2437 EVT VT = N->getValueType(0);
2438 switch (VT.getSimpleVT().SimpleTy) {
2439 default: return NULL;
2440 case MVT::v8i8: Opc = ARM::VTRNd8; break;
2441 case MVT::v4i16: Opc = ARM::VTRNd16; break;
2443 case MVT::v2i32: Opc = ARM::VTRNd32; break;
2444 case MVT::v16i8: Opc = ARM::VTRNq8; break;
2445 case MVT::v8i16: Opc = ARM::VTRNq16; break;
2447 case MVT::v4i32: Opc = ARM::VTRNq32; break;
2449 SDValue Pred = getAL(CurDAG);
2450 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
2451 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
2452 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
2454 case ARMISD::BUILD_VECTOR: {
2455 EVT VecVT = N->getValueType(0);
2456 EVT EltVT = VecVT.getVectorElementType();
2457 unsigned NumElts = VecVT.getVectorNumElements();
2458 if (EltVT == MVT::f64) {
2459 assert(NumElts == 2 && "unexpected type for BUILD_VECTOR");
2460 return PairDRegs(VecVT, N->getOperand(0), N->getOperand(1));
2462 assert(EltVT == MVT::f32 && "unexpected type for BUILD_VECTOR");
2464 return PairSRegs(VecVT, N->getOperand(0), N->getOperand(1));
2465 assert(NumElts == 4 && "unexpected type for BUILD_VECTOR");
2466 return QuadSRegs(VecVT, N->getOperand(0), N->getOperand(1),
2467 N->getOperand(2), N->getOperand(3));
2470 case ARMISD::VLD2DUP: {
2471 unsigned Opcodes[] = { ARM::VLD2DUPd8Pseudo, ARM::VLD2DUPd16Pseudo,
2472 ARM::VLD2DUPd32Pseudo };
2473 return SelectVLDDup(N, 2, Opcodes);
2476 case ARMISD::VLD3DUP: {
2477 unsigned Opcodes[] = { ARM::VLD3DUPd8Pseudo, ARM::VLD3DUPd16Pseudo,
2478 ARM::VLD3DUPd32Pseudo };
2479 return SelectVLDDup(N, 3, Opcodes);
2482 case ARMISD::VLD4DUP: {
2483 unsigned Opcodes[] = { ARM::VLD4DUPd8Pseudo, ARM::VLD4DUPd16Pseudo,
2484 ARM::VLD4DUPd32Pseudo };
2485 return SelectVLDDup(N, 4, Opcodes);
2488 case ISD::INTRINSIC_VOID:
2489 case ISD::INTRINSIC_W_CHAIN: {
2490 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
2495 case Intrinsic::arm_neon_vld1: {
2496 unsigned DOpcodes[] = { ARM::VLD1d8, ARM::VLD1d16,
2497 ARM::VLD1d32, ARM::VLD1d64 };
2498 unsigned QOpcodes[] = { ARM::VLD1q8Pseudo, ARM::VLD1q16Pseudo,
2499 ARM::VLD1q32Pseudo, ARM::VLD1q64Pseudo };
2500 return SelectVLD(N, 1, DOpcodes, QOpcodes, 0);
2503 case Intrinsic::arm_neon_vld2: {
2504 unsigned DOpcodes[] = { ARM::VLD2d8Pseudo, ARM::VLD2d16Pseudo,
2505 ARM::VLD2d32Pseudo, ARM::VLD1q64Pseudo };
2506 unsigned QOpcodes[] = { ARM::VLD2q8Pseudo, ARM::VLD2q16Pseudo,
2507 ARM::VLD2q32Pseudo };
2508 return SelectVLD(N, 2, DOpcodes, QOpcodes, 0);
2511 case Intrinsic::arm_neon_vld3: {
2512 unsigned DOpcodes[] = { ARM::VLD3d8Pseudo, ARM::VLD3d16Pseudo,
2513 ARM::VLD3d32Pseudo, ARM::VLD1d64TPseudo };
2514 unsigned QOpcodes0[] = { ARM::VLD3q8Pseudo_UPD,
2515 ARM::VLD3q16Pseudo_UPD,
2516 ARM::VLD3q32Pseudo_UPD };
2517 unsigned QOpcodes1[] = { ARM::VLD3q8oddPseudo,
2518 ARM::VLD3q16oddPseudo,
2519 ARM::VLD3q32oddPseudo };
2520 return SelectVLD(N, 3, DOpcodes, QOpcodes0, QOpcodes1);
2523 case Intrinsic::arm_neon_vld4: {
2524 unsigned DOpcodes[] = { ARM::VLD4d8Pseudo, ARM::VLD4d16Pseudo,
2525 ARM::VLD4d32Pseudo, ARM::VLD1d64QPseudo };
2526 unsigned QOpcodes0[] = { ARM::VLD4q8Pseudo_UPD,
2527 ARM::VLD4q16Pseudo_UPD,
2528 ARM::VLD4q32Pseudo_UPD };
2529 unsigned QOpcodes1[] = { ARM::VLD4q8oddPseudo,
2530 ARM::VLD4q16oddPseudo,
2531 ARM::VLD4q32oddPseudo };
2532 return SelectVLD(N, 4, DOpcodes, QOpcodes0, QOpcodes1);
2535 case Intrinsic::arm_neon_vld2lane: {
2536 unsigned DOpcodes[] = { ARM::VLD2LNd8Pseudo, ARM::VLD2LNd16Pseudo,
2537 ARM::VLD2LNd32Pseudo };
2538 unsigned QOpcodes[] = { ARM::VLD2LNq16Pseudo, ARM::VLD2LNq32Pseudo };
2539 return SelectVLDSTLane(N, true, 2, DOpcodes, QOpcodes);
2542 case Intrinsic::arm_neon_vld3lane: {
2543 unsigned DOpcodes[] = { ARM::VLD3LNd8Pseudo, ARM::VLD3LNd16Pseudo,
2544 ARM::VLD3LNd32Pseudo };
2545 unsigned QOpcodes[] = { ARM::VLD3LNq16Pseudo, ARM::VLD3LNq32Pseudo };
2546 return SelectVLDSTLane(N, true, 3, DOpcodes, QOpcodes);
2549 case Intrinsic::arm_neon_vld4lane: {
2550 unsigned DOpcodes[] = { ARM::VLD4LNd8Pseudo, ARM::VLD4LNd16Pseudo,
2551 ARM::VLD4LNd32Pseudo };
2552 unsigned QOpcodes[] = { ARM::VLD4LNq16Pseudo, ARM::VLD4LNq32Pseudo };
2553 return SelectVLDSTLane(N, true, 4, DOpcodes, QOpcodes);
2556 case Intrinsic::arm_neon_vst1: {
2557 unsigned DOpcodes[] = { ARM::VST1d8, ARM::VST1d16,
2558 ARM::VST1d32, ARM::VST1d64 };
2559 unsigned QOpcodes[] = { ARM::VST1q8Pseudo, ARM::VST1q16Pseudo,
2560 ARM::VST1q32Pseudo, ARM::VST1q64Pseudo };
2561 return SelectVST(N, 1, DOpcodes, QOpcodes, 0);
2564 case Intrinsic::arm_neon_vst2: {
2565 unsigned DOpcodes[] = { ARM::VST2d8Pseudo, ARM::VST2d16Pseudo,
2566 ARM::VST2d32Pseudo, ARM::VST1q64Pseudo };
2567 unsigned QOpcodes[] = { ARM::VST2q8Pseudo, ARM::VST2q16Pseudo,
2568 ARM::VST2q32Pseudo };
2569 return SelectVST(N, 2, DOpcodes, QOpcodes, 0);
2572 case Intrinsic::arm_neon_vst3: {
2573 unsigned DOpcodes[] = { ARM::VST3d8Pseudo, ARM::VST3d16Pseudo,
2574 ARM::VST3d32Pseudo, ARM::VST1d64TPseudo };
2575 unsigned QOpcodes0[] = { ARM::VST3q8Pseudo_UPD,
2576 ARM::VST3q16Pseudo_UPD,
2577 ARM::VST3q32Pseudo_UPD };
2578 unsigned QOpcodes1[] = { ARM::VST3q8oddPseudo,
2579 ARM::VST3q16oddPseudo,
2580 ARM::VST3q32oddPseudo };
2581 return SelectVST(N, 3, DOpcodes, QOpcodes0, QOpcodes1);
2584 case Intrinsic::arm_neon_vst4: {
2585 unsigned DOpcodes[] = { ARM::VST4d8Pseudo, ARM::VST4d16Pseudo,
2586 ARM::VST4d32Pseudo, ARM::VST1d64QPseudo };
2587 unsigned QOpcodes0[] = { ARM::VST4q8Pseudo_UPD,
2588 ARM::VST4q16Pseudo_UPD,
2589 ARM::VST4q32Pseudo_UPD };
2590 unsigned QOpcodes1[] = { ARM::VST4q8oddPseudo,
2591 ARM::VST4q16oddPseudo,
2592 ARM::VST4q32oddPseudo };
2593 return SelectVST(N, 4, DOpcodes, QOpcodes0, QOpcodes1);
2596 case Intrinsic::arm_neon_vst2lane: {
2597 unsigned DOpcodes[] = { ARM::VST2LNd8Pseudo, ARM::VST2LNd16Pseudo,
2598 ARM::VST2LNd32Pseudo };
2599 unsigned QOpcodes[] = { ARM::VST2LNq16Pseudo, ARM::VST2LNq32Pseudo };
2600 return SelectVLDSTLane(N, false, 2, DOpcodes, QOpcodes);
2603 case Intrinsic::arm_neon_vst3lane: {
2604 unsigned DOpcodes[] = { ARM::VST3LNd8Pseudo, ARM::VST3LNd16Pseudo,
2605 ARM::VST3LNd32Pseudo };
2606 unsigned QOpcodes[] = { ARM::VST3LNq16Pseudo, ARM::VST3LNq32Pseudo };
2607 return SelectVLDSTLane(N, false, 3, DOpcodes, QOpcodes);
2610 case Intrinsic::arm_neon_vst4lane: {
2611 unsigned DOpcodes[] = { ARM::VST4LNd8Pseudo, ARM::VST4LNd16Pseudo,
2612 ARM::VST4LNd32Pseudo };
2613 unsigned QOpcodes[] = { ARM::VST4LNq16Pseudo, ARM::VST4LNq32Pseudo };
2614 return SelectVLDSTLane(N, false, 4, DOpcodes, QOpcodes);
2620 case ISD::INTRINSIC_WO_CHAIN: {
2621 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
2626 case Intrinsic::arm_neon_vtbl2:
2627 return SelectVTBL(N, false, 2, ARM::VTBL2Pseudo);
2628 case Intrinsic::arm_neon_vtbl3:
2629 return SelectVTBL(N, false, 3, ARM::VTBL3Pseudo);
2630 case Intrinsic::arm_neon_vtbl4:
2631 return SelectVTBL(N, false, 4, ARM::VTBL4Pseudo);
2633 case Intrinsic::arm_neon_vtbx2:
2634 return SelectVTBL(N, true, 2, ARM::VTBX2Pseudo);
2635 case Intrinsic::arm_neon_vtbx3:
2636 return SelectVTBL(N, true, 3, ARM::VTBX3Pseudo);
2637 case Intrinsic::arm_neon_vtbx4:
2638 return SelectVTBL(N, true, 4, ARM::VTBX4Pseudo);
2643 case ISD::CONCAT_VECTORS:
2644 return SelectConcatVector(N);
2647 return SelectCode(N);
2650 bool ARMDAGToDAGISel::
2651 SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
2652 std::vector<SDValue> &OutOps) {
2653 assert(ConstraintCode == 'm' && "unexpected asm memory constraint");
2654 // Require the address to be in a register. That is safe for all ARM
2655 // variants and it is hard to do anything much smarter without knowing
2656 // how the operand is used.
2657 OutOps.push_back(Op);
2661 /// createARMISelDag - This pass converts a legalized DAG into a
2662 /// ARM-specific DAG, ready for instruction scheduling.
2664 FunctionPass *llvm::createARMISelDag(ARMBaseTargetMachine &TM,
2665 CodeGenOpt::Level OptLevel) {
2666 return new ARMDAGToDAGISel(TM, OptLevel);