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 "ARMAddressingModes.h"
16 #include "ARMISelLowering.h"
17 #include "ARMTargetMachine.h"
18 #include "llvm/CallingConv.h"
19 #include "llvm/Constants.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Function.h"
22 #include "llvm/Intrinsics.h"
23 #include "llvm/LLVMContext.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/MachineFunction.h"
26 #include "llvm/CodeGen/MachineInstrBuilder.h"
27 #include "llvm/CodeGen/SelectionDAG.h"
28 #include "llvm/CodeGen/SelectionDAGISel.h"
29 #include "llvm/Target/TargetLowering.h"
30 #include "llvm/Target/TargetOptions.h"
31 #include "llvm/Support/Compiler.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/raw_ostream.h"
38 //===--------------------------------------------------------------------===//
39 /// ARMDAGToDAGISel - ARM specific code to select ARM machine
40 /// instructions for SelectionDAG operations.
43 class ARMDAGToDAGISel : public SelectionDAGISel {
44 ARMBaseTargetMachine &TM;
46 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
47 /// make the right decision when generating code for different targets.
48 const ARMSubtarget *Subtarget;
51 explicit ARMDAGToDAGISel(ARMBaseTargetMachine &tm,
52 CodeGenOpt::Level OptLevel)
53 : SelectionDAGISel(tm, OptLevel), TM(tm),
54 Subtarget(&TM.getSubtarget<ARMSubtarget>()) {
57 virtual const char *getPassName() const {
58 return "ARM Instruction Selection";
61 /// getI32Imm - Return a target constant of type i32 with the specified
63 inline SDValue getI32Imm(unsigned Imm) {
64 return CurDAG->getTargetConstant(Imm, MVT::i32);
67 SDNode *Select(SDValue Op);
68 virtual void InstructionSelect();
69 bool SelectShifterOperandReg(SDValue Op, SDValue N, SDValue &A,
70 SDValue &B, SDValue &C);
71 bool SelectAddrMode2(SDValue Op, SDValue N, SDValue &Base,
72 SDValue &Offset, SDValue &Opc);
73 bool SelectAddrMode2Offset(SDValue Op, SDValue N,
74 SDValue &Offset, SDValue &Opc);
75 bool SelectAddrMode3(SDValue Op, SDValue N, SDValue &Base,
76 SDValue &Offset, SDValue &Opc);
77 bool SelectAddrMode3Offset(SDValue Op, SDValue N,
78 SDValue &Offset, SDValue &Opc);
79 bool SelectAddrMode4(SDValue Op, SDValue N, SDValue &Addr,
81 bool SelectAddrMode5(SDValue Op, SDValue N, SDValue &Base,
83 bool SelectAddrMode6(SDValue Op, SDValue N, SDValue &Addr, SDValue &Update,
84 SDValue &Opc, SDValue &Align);
86 bool SelectAddrModePC(SDValue Op, SDValue N, SDValue &Offset,
89 bool SelectThumbAddrModeRR(SDValue Op, SDValue N, SDValue &Base,
91 bool SelectThumbAddrModeRI5(SDValue Op, SDValue N, unsigned Scale,
92 SDValue &Base, SDValue &OffImm,
94 bool SelectThumbAddrModeS1(SDValue Op, SDValue N, SDValue &Base,
95 SDValue &OffImm, SDValue &Offset);
96 bool SelectThumbAddrModeS2(SDValue Op, SDValue N, SDValue &Base,
97 SDValue &OffImm, SDValue &Offset);
98 bool SelectThumbAddrModeS4(SDValue Op, SDValue N, SDValue &Base,
99 SDValue &OffImm, SDValue &Offset);
100 bool SelectThumbAddrModeSP(SDValue Op, SDValue N, SDValue &Base,
103 bool SelectT2ShifterOperandReg(SDValue Op, SDValue N,
104 SDValue &BaseReg, SDValue &Opc);
105 bool SelectT2AddrModeImm12(SDValue Op, SDValue N, SDValue &Base,
107 bool SelectT2AddrModeImm8(SDValue Op, SDValue N, SDValue &Base,
109 bool SelectT2AddrModeImm8Offset(SDValue Op, SDValue N,
111 bool SelectT2AddrModeImm8s4(SDValue Op, SDValue N, SDValue &Base,
113 bool SelectT2AddrModeSoReg(SDValue Op, SDValue N, SDValue &Base,
114 SDValue &OffReg, SDValue &ShImm);
116 // Include the pieces autogenerated from the target description.
117 #include "ARMGenDAGISel.inc"
120 /// SelectARMIndexedLoad - Indexed (pre/post inc/dec) load matching code for
122 SDNode *SelectARMIndexedLoad(SDValue Op);
123 SDNode *SelectT2IndexedLoad(SDValue Op);
125 /// SelectDYN_ALLOC - Select dynamic alloc for Thumb.
126 SDNode *SelectDYN_ALLOC(SDValue Op);
128 /// SelectVLD - Select NEON load intrinsics. NumVecs should
129 /// be 2, 3 or 4. The opcode arrays specify the instructions used for
130 /// loads of D registers and even subregs and odd subregs of Q registers.
131 /// For NumVecs == 2, QOpcodes1 is not used.
132 SDNode *SelectVLD(SDValue Op, unsigned NumVecs, unsigned *DOpcodes,
133 unsigned *QOpcodes0, unsigned *QOpcodes1);
135 /// SelectVST - Select NEON store intrinsics. NumVecs should
136 /// be 2, 3 or 4. The opcode arrays specify the instructions used for
137 /// stores of D registers and even subregs and odd subregs of Q registers.
138 /// For NumVecs == 2, QOpcodes1 is not used.
139 SDNode *SelectVST(SDValue Op, unsigned NumVecs, unsigned *DOpcodes,
140 unsigned *QOpcodes0, unsigned *QOpcodes1);
142 /// SelectVLDSTLane - Select NEON load/store lane intrinsics. NumVecs should
143 /// be 2, 3 or 4. The opcode arrays specify the instructions used for
144 /// load/store of D registers and even subregs and odd subregs of Q registers.
145 SDNode *SelectVLDSTLane(SDValue Op, bool IsLoad, unsigned NumVecs,
146 unsigned *DOpcodes, unsigned *QOpcodes0,
147 unsigned *QOpcodes1);
149 /// SelectV6T2BitfieldExtractOp - Select SBFX/UBFX instructions for ARM.
150 SDNode *SelectV6T2BitfieldExtractOp(SDValue Op, unsigned Opc);
152 /// SelectCMOVOp - Select CMOV instructions for ARM.
153 SDNode *SelectCMOVOp(SDValue Op);
154 SDNode *SelectT2CMOVShiftOp(SDValue Op, SDValue FalseVal, SDValue TrueVal,
155 ARMCC::CondCodes CCVal, SDValue CCR,
157 SDNode *SelectARMCMOVShiftOp(SDValue Op, SDValue FalseVal, SDValue TrueVal,
158 ARMCC::CondCodes CCVal, SDValue CCR,
160 SDNode *SelectT2CMOVSoImmOp(SDValue Op, SDValue FalseVal, SDValue TrueVal,
161 ARMCC::CondCodes CCVal, SDValue CCR,
163 SDNode *SelectARMCMOVSoImmOp(SDValue Op, SDValue FalseVal, SDValue TrueVal,
164 ARMCC::CondCodes CCVal, SDValue CCR,
167 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
168 /// inline asm expressions.
169 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
171 std::vector<SDValue> &OutOps);
173 /// PairDRegs - Insert a pair of double registers into an implicit def to
174 /// form a quad register.
175 SDNode *PairDRegs(EVT VT, SDValue V0, SDValue V1);
179 /// isInt32Immediate - This method tests to see if the node is a 32-bit constant
180 /// operand. If so Imm will receive the 32-bit value.
181 static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
182 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
183 Imm = cast<ConstantSDNode>(N)->getZExtValue();
189 // isInt32Immediate - This method tests to see if a constant operand.
190 // If so Imm will receive the 32 bit value.
191 static bool isInt32Immediate(SDValue N, unsigned &Imm) {
192 return isInt32Immediate(N.getNode(), Imm);
195 // isOpcWithIntImmediate - This method tests to see if the node is a specific
196 // opcode and that it has a immediate integer right operand.
197 // If so Imm will receive the 32 bit value.
198 static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
199 return N->getOpcode() == Opc &&
200 isInt32Immediate(N->getOperand(1).getNode(), Imm);
204 void ARMDAGToDAGISel::InstructionSelect() {
206 CurDAG->RemoveDeadNodes();
209 bool ARMDAGToDAGISel::SelectShifterOperandReg(SDValue Op,
214 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
216 // Don't match base register only case. That is matched to a separate
217 // lower complexity pattern with explicit register operand.
218 if (ShOpcVal == ARM_AM::no_shift) return false;
220 BaseReg = N.getOperand(0);
221 unsigned ShImmVal = 0;
222 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
223 ShReg = CurDAG->getRegister(0, MVT::i32);
224 ShImmVal = RHS->getZExtValue() & 31;
226 ShReg = N.getOperand(1);
228 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
233 bool ARMDAGToDAGISel::SelectAddrMode2(SDValue Op, SDValue N,
234 SDValue &Base, SDValue &Offset,
236 if (N.getOpcode() == ISD::MUL) {
237 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
238 // X * [3,5,9] -> X + X * [2,4,8] etc.
239 int RHSC = (int)RHS->getZExtValue();
242 ARM_AM::AddrOpc AddSub = ARM_AM::add;
244 AddSub = ARM_AM::sub;
247 if (isPowerOf2_32(RHSC)) {
248 unsigned ShAmt = Log2_32(RHSC);
249 Base = Offset = N.getOperand(0);
250 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt,
259 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB) {
261 if (N.getOpcode() == ISD::FrameIndex) {
262 int FI = cast<FrameIndexSDNode>(N)->getIndex();
263 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
264 } else if (N.getOpcode() == ARMISD::Wrapper) {
265 Base = N.getOperand(0);
267 Offset = CurDAG->getRegister(0, MVT::i32);
268 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::add, 0,
274 // Match simple R +/- imm12 operands.
275 if (N.getOpcode() == ISD::ADD)
276 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
277 int RHSC = (int)RHS->getZExtValue();
278 if ((RHSC >= 0 && RHSC < 0x1000) ||
279 (RHSC < 0 && RHSC > -0x1000)) { // 12 bits.
280 Base = N.getOperand(0);
281 if (Base.getOpcode() == ISD::FrameIndex) {
282 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
283 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
285 Offset = CurDAG->getRegister(0, MVT::i32);
287 ARM_AM::AddrOpc AddSub = ARM_AM::add;
289 AddSub = ARM_AM::sub;
292 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, RHSC,
299 // Otherwise this is R +/- [possibly shifted] R.
300 ARM_AM::AddrOpc AddSub = N.getOpcode() == ISD::ADD ? ARM_AM::add:ARM_AM::sub;
301 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(1));
304 Base = N.getOperand(0);
305 Offset = N.getOperand(1);
307 if (ShOpcVal != ARM_AM::no_shift) {
308 // Check to see if the RHS of the shift is a constant, if not, we can't fold
310 if (ConstantSDNode *Sh =
311 dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) {
312 ShAmt = Sh->getZExtValue();
313 Offset = N.getOperand(1).getOperand(0);
315 ShOpcVal = ARM_AM::no_shift;
319 // Try matching (R shl C) + (R).
320 if (N.getOpcode() == ISD::ADD && ShOpcVal == ARM_AM::no_shift) {
321 ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0));
322 if (ShOpcVal != ARM_AM::no_shift) {
323 // Check to see if the RHS of the shift is a constant, if not, we can't
325 if (ConstantSDNode *Sh =
326 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
327 ShAmt = Sh->getZExtValue();
328 Offset = N.getOperand(0).getOperand(0);
329 Base = N.getOperand(1);
331 ShOpcVal = ARM_AM::no_shift;
336 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
341 bool ARMDAGToDAGISel::SelectAddrMode2Offset(SDValue Op, SDValue N,
342 SDValue &Offset, SDValue &Opc) {
343 unsigned Opcode = Op.getOpcode();
344 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
345 ? cast<LoadSDNode>(Op)->getAddressingMode()
346 : cast<StoreSDNode>(Op)->getAddressingMode();
347 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
348 ? ARM_AM::add : ARM_AM::sub;
349 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
350 int Val = (int)C->getZExtValue();
351 if (Val >= 0 && Val < 0x1000) { // 12 bits.
352 Offset = CurDAG->getRegister(0, MVT::i32);
353 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, Val,
361 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
363 if (ShOpcVal != ARM_AM::no_shift) {
364 // Check to see if the RHS of the shift is a constant, if not, we can't fold
366 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
367 ShAmt = Sh->getZExtValue();
368 Offset = N.getOperand(0);
370 ShOpcVal = ARM_AM::no_shift;
374 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
380 bool ARMDAGToDAGISel::SelectAddrMode3(SDValue Op, SDValue N,
381 SDValue &Base, SDValue &Offset,
383 if (N.getOpcode() == ISD::SUB) {
384 // X - C is canonicalize to X + -C, no need to handle it here.
385 Base = N.getOperand(0);
386 Offset = N.getOperand(1);
387 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::sub, 0),MVT::i32);
391 if (N.getOpcode() != ISD::ADD) {
393 if (N.getOpcode() == ISD::FrameIndex) {
394 int FI = cast<FrameIndexSDNode>(N)->getIndex();
395 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
397 Offset = CurDAG->getRegister(0, MVT::i32);
398 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0),MVT::i32);
402 // If the RHS is +/- imm8, fold into addr mode.
403 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
404 int RHSC = (int)RHS->getZExtValue();
405 if ((RHSC >= 0 && RHSC < 256) ||
406 (RHSC < 0 && RHSC > -256)) { // note -256 itself isn't allowed.
407 Base = N.getOperand(0);
408 if (Base.getOpcode() == ISD::FrameIndex) {
409 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
410 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
412 Offset = CurDAG->getRegister(0, MVT::i32);
414 ARM_AM::AddrOpc AddSub = ARM_AM::add;
416 AddSub = ARM_AM::sub;
419 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, RHSC),MVT::i32);
424 Base = N.getOperand(0);
425 Offset = N.getOperand(1);
426 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0), MVT::i32);
430 bool ARMDAGToDAGISel::SelectAddrMode3Offset(SDValue Op, SDValue N,
431 SDValue &Offset, SDValue &Opc) {
432 unsigned Opcode = Op.getOpcode();
433 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
434 ? cast<LoadSDNode>(Op)->getAddressingMode()
435 : cast<StoreSDNode>(Op)->getAddressingMode();
436 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
437 ? ARM_AM::add : ARM_AM::sub;
438 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
439 int Val = (int)C->getZExtValue();
440 if (Val >= 0 && Val < 256) {
441 Offset = CurDAG->getRegister(0, MVT::i32);
442 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, Val), MVT::i32);
448 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, 0), MVT::i32);
452 bool ARMDAGToDAGISel::SelectAddrMode4(SDValue Op, SDValue N,
453 SDValue &Addr, SDValue &Mode) {
455 Mode = CurDAG->getTargetConstant(0, MVT::i32);
459 bool ARMDAGToDAGISel::SelectAddrMode5(SDValue Op, SDValue N,
460 SDValue &Base, SDValue &Offset) {
461 if (N.getOpcode() != ISD::ADD) {
463 if (N.getOpcode() == ISD::FrameIndex) {
464 int FI = cast<FrameIndexSDNode>(N)->getIndex();
465 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
466 } else if (N.getOpcode() == ARMISD::Wrapper) {
467 Base = N.getOperand(0);
469 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
474 // If the RHS is +/- imm8, fold into addr mode.
475 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
476 int RHSC = (int)RHS->getZExtValue();
477 if ((RHSC & 3) == 0) { // The constant is implicitly multiplied by 4.
479 if ((RHSC >= 0 && RHSC < 256) ||
480 (RHSC < 0 && RHSC > -256)) { // note -256 itself isn't allowed.
481 Base = N.getOperand(0);
482 if (Base.getOpcode() == ISD::FrameIndex) {
483 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
484 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
487 ARM_AM::AddrOpc AddSub = ARM_AM::add;
489 AddSub = ARM_AM::sub;
492 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(AddSub, RHSC),
500 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
505 bool ARMDAGToDAGISel::SelectAddrMode6(SDValue Op, SDValue N,
506 SDValue &Addr, SDValue &Update,
507 SDValue &Opc, SDValue &Align) {
509 // Default to no writeback.
510 Update = CurDAG->getRegister(0, MVT::i32);
511 Opc = CurDAG->getTargetConstant(ARM_AM::getAM6Opc(false), MVT::i32);
512 // Default to no alignment.
513 Align = CurDAG->getTargetConstant(0, MVT::i32);
517 bool ARMDAGToDAGISel::SelectAddrModePC(SDValue Op, SDValue N,
518 SDValue &Offset, SDValue &Label) {
519 if (N.getOpcode() == ARMISD::PIC_ADD && N.hasOneUse()) {
520 Offset = N.getOperand(0);
521 SDValue N1 = N.getOperand(1);
522 Label = CurDAG->getTargetConstant(cast<ConstantSDNode>(N1)->getZExtValue(),
529 bool ARMDAGToDAGISel::SelectThumbAddrModeRR(SDValue Op, SDValue N,
530 SDValue &Base, SDValue &Offset){
531 // FIXME dl should come from the parent load or store, not the address
532 DebugLoc dl = Op.getDebugLoc();
533 if (N.getOpcode() != ISD::ADD) {
534 ConstantSDNode *NC = dyn_cast<ConstantSDNode>(N);
535 if (!NC || NC->getZExtValue() != 0)
542 Base = N.getOperand(0);
543 Offset = N.getOperand(1);
548 ARMDAGToDAGISel::SelectThumbAddrModeRI5(SDValue Op, SDValue N,
549 unsigned Scale, SDValue &Base,
550 SDValue &OffImm, SDValue &Offset) {
552 SDValue TmpBase, TmpOffImm;
553 if (SelectThumbAddrModeSP(Op, N, TmpBase, TmpOffImm))
554 return false; // We want to select tLDRspi / tSTRspi instead.
555 if (N.getOpcode() == ARMISD::Wrapper &&
556 N.getOperand(0).getOpcode() == ISD::TargetConstantPool)
557 return false; // We want to select tLDRpci instead.
560 if (N.getOpcode() != ISD::ADD) {
561 Base = (N.getOpcode() == ARMISD::Wrapper) ? N.getOperand(0) : N;
562 Offset = CurDAG->getRegister(0, MVT::i32);
563 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
567 // Thumb does not have [sp, r] address mode.
568 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
569 RegisterSDNode *RHSR = dyn_cast<RegisterSDNode>(N.getOperand(1));
570 if ((LHSR && LHSR->getReg() == ARM::SP) ||
571 (RHSR && RHSR->getReg() == ARM::SP)) {
573 Offset = CurDAG->getRegister(0, MVT::i32);
574 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
578 // If the RHS is + imm5 * scale, fold into addr mode.
579 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
580 int RHSC = (int)RHS->getZExtValue();
581 if ((RHSC & (Scale-1)) == 0) { // The constant is implicitly multiplied.
583 if (RHSC >= 0 && RHSC < 32) {
584 Base = N.getOperand(0);
585 Offset = CurDAG->getRegister(0, MVT::i32);
586 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
592 Base = N.getOperand(0);
593 Offset = N.getOperand(1);
594 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
598 bool ARMDAGToDAGISel::SelectThumbAddrModeS1(SDValue Op, SDValue N,
599 SDValue &Base, SDValue &OffImm,
601 return SelectThumbAddrModeRI5(Op, N, 1, Base, OffImm, Offset);
604 bool ARMDAGToDAGISel::SelectThumbAddrModeS2(SDValue Op, SDValue N,
605 SDValue &Base, SDValue &OffImm,
607 return SelectThumbAddrModeRI5(Op, N, 2, Base, OffImm, Offset);
610 bool ARMDAGToDAGISel::SelectThumbAddrModeS4(SDValue Op, SDValue N,
611 SDValue &Base, SDValue &OffImm,
613 return SelectThumbAddrModeRI5(Op, N, 4, Base, OffImm, Offset);
616 bool ARMDAGToDAGISel::SelectThumbAddrModeSP(SDValue Op, SDValue N,
617 SDValue &Base, SDValue &OffImm) {
618 if (N.getOpcode() == ISD::FrameIndex) {
619 int FI = cast<FrameIndexSDNode>(N)->getIndex();
620 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
621 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
625 if (N.getOpcode() != ISD::ADD)
628 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
629 if (N.getOperand(0).getOpcode() == ISD::FrameIndex ||
630 (LHSR && LHSR->getReg() == ARM::SP)) {
631 // If the RHS is + imm8 * scale, fold into addr mode.
632 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
633 int RHSC = (int)RHS->getZExtValue();
634 if ((RHSC & 3) == 0) { // The constant is implicitly multiplied.
636 if (RHSC >= 0 && RHSC < 256) {
637 Base = N.getOperand(0);
638 if (Base.getOpcode() == ISD::FrameIndex) {
639 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
640 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
642 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
652 bool ARMDAGToDAGISel::SelectT2ShifterOperandReg(SDValue Op, SDValue N,
655 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
657 // Don't match base register only case. That is matched to a separate
658 // lower complexity pattern with explicit register operand.
659 if (ShOpcVal == ARM_AM::no_shift) return false;
661 BaseReg = N.getOperand(0);
662 unsigned ShImmVal = 0;
663 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
664 ShImmVal = RHS->getZExtValue() & 31;
665 Opc = getI32Imm(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal));
672 bool ARMDAGToDAGISel::SelectT2AddrModeImm12(SDValue Op, SDValue N,
673 SDValue &Base, SDValue &OffImm) {
674 // Match simple R + imm12 operands.
677 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB) {
678 if (N.getOpcode() == ISD::FrameIndex) {
679 // Match frame index...
680 int FI = cast<FrameIndexSDNode>(N)->getIndex();
681 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
682 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
684 } else if (N.getOpcode() == ARMISD::Wrapper) {
685 Base = N.getOperand(0);
686 if (Base.getOpcode() == ISD::TargetConstantPool)
687 return false; // We want to select t2LDRpci instead.
690 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
694 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
695 if (SelectT2AddrModeImm8(Op, N, Base, OffImm))
696 // Let t2LDRi8 handle (R - imm8).
699 int RHSC = (int)RHS->getZExtValue();
700 if (N.getOpcode() == ISD::SUB)
703 if (RHSC >= 0 && RHSC < 0x1000) { // 12 bits (unsigned)
704 Base = N.getOperand(0);
705 if (Base.getOpcode() == ISD::FrameIndex) {
706 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
707 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
709 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
716 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
720 bool ARMDAGToDAGISel::SelectT2AddrModeImm8(SDValue Op, SDValue N,
721 SDValue &Base, SDValue &OffImm) {
722 // Match simple R - imm8 operands.
723 if (N.getOpcode() == ISD::ADD || N.getOpcode() == ISD::SUB) {
724 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
725 int RHSC = (int)RHS->getSExtValue();
726 if (N.getOpcode() == ISD::SUB)
729 if ((RHSC >= -255) && (RHSC < 0)) { // 8 bits (always negative)
730 Base = N.getOperand(0);
731 if (Base.getOpcode() == ISD::FrameIndex) {
732 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
733 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
735 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
744 bool ARMDAGToDAGISel::SelectT2AddrModeImm8Offset(SDValue Op, SDValue N,
746 unsigned Opcode = Op.getOpcode();
747 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
748 ? cast<LoadSDNode>(Op)->getAddressingMode()
749 : cast<StoreSDNode>(Op)->getAddressingMode();
750 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N)) {
751 int RHSC = (int)RHS->getZExtValue();
752 if (RHSC >= 0 && RHSC < 0x100) { // 8 bits.
753 OffImm = ((AM == ISD::PRE_INC) || (AM == ISD::POST_INC))
754 ? CurDAG->getTargetConstant(RHSC, MVT::i32)
755 : CurDAG->getTargetConstant(-RHSC, MVT::i32);
763 bool ARMDAGToDAGISel::SelectT2AddrModeImm8s4(SDValue Op, SDValue N,
764 SDValue &Base, SDValue &OffImm) {
765 if (N.getOpcode() == ISD::ADD) {
766 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
767 int RHSC = (int)RHS->getZExtValue();
768 if (((RHSC & 0x3) == 0) &&
769 ((RHSC >= 0 && RHSC < 0x400) || (RHSC < 0 && RHSC > -0x400))) { // 8 bits.
770 Base = N.getOperand(0);
771 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
775 } else if (N.getOpcode() == ISD::SUB) {
776 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
777 int RHSC = (int)RHS->getZExtValue();
778 if (((RHSC & 0x3) == 0) && (RHSC >= 0 && RHSC < 0x400)) { // 8 bits.
779 Base = N.getOperand(0);
780 OffImm = CurDAG->getTargetConstant(-RHSC, MVT::i32);
789 bool ARMDAGToDAGISel::SelectT2AddrModeSoReg(SDValue Op, SDValue N,
791 SDValue &OffReg, SDValue &ShImm) {
792 // (R - imm8) should be handled by t2LDRi8. The rest are handled by t2LDRi12.
793 if (N.getOpcode() != ISD::ADD)
796 // Leave (R + imm12) for t2LDRi12, (R - imm8) for t2LDRi8.
797 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
798 int RHSC = (int)RHS->getZExtValue();
799 if (RHSC >= 0 && RHSC < 0x1000) // 12 bits (unsigned)
801 else if (RHSC < 0 && RHSC >= -255) // 8 bits
805 // Look for (R + R) or (R + (R << [1,2,3])).
807 Base = N.getOperand(0);
808 OffReg = N.getOperand(1);
810 // Swap if it is ((R << c) + R).
811 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(OffReg);
812 if (ShOpcVal != ARM_AM::lsl) {
813 ShOpcVal = ARM_AM::getShiftOpcForNode(Base);
814 if (ShOpcVal == ARM_AM::lsl)
815 std::swap(Base, OffReg);
818 if (ShOpcVal == ARM_AM::lsl) {
819 // Check to see if the RHS of the shift is a constant, if not, we can't fold
821 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(OffReg.getOperand(1))) {
822 ShAmt = Sh->getZExtValue();
825 ShOpcVal = ARM_AM::no_shift;
827 OffReg = OffReg.getOperand(0);
829 ShOpcVal = ARM_AM::no_shift;
833 ShImm = CurDAG->getTargetConstant(ShAmt, MVT::i32);
838 //===--------------------------------------------------------------------===//
840 /// getAL - Returns a ARMCC::AL immediate node.
841 static inline SDValue getAL(SelectionDAG *CurDAG) {
842 return CurDAG->getTargetConstant((uint64_t)ARMCC::AL, MVT::i32);
845 SDNode *ARMDAGToDAGISel::SelectARMIndexedLoad(SDValue Op) {
846 LoadSDNode *LD = cast<LoadSDNode>(Op);
847 ISD::MemIndexedMode AM = LD->getAddressingMode();
848 if (AM == ISD::UNINDEXED)
851 EVT LoadedVT = LD->getMemoryVT();
852 SDValue Offset, AMOpc;
853 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
856 if (LoadedVT == MVT::i32 &&
857 SelectAddrMode2Offset(Op, LD->getOffset(), Offset, AMOpc)) {
858 Opcode = isPre ? ARM::LDR_PRE : ARM::LDR_POST;
860 } else if (LoadedVT == MVT::i16 &&
861 SelectAddrMode3Offset(Op, LD->getOffset(), Offset, AMOpc)) {
863 Opcode = (LD->getExtensionType() == ISD::SEXTLOAD)
864 ? (isPre ? ARM::LDRSH_PRE : ARM::LDRSH_POST)
865 : (isPre ? ARM::LDRH_PRE : ARM::LDRH_POST);
866 } else if (LoadedVT == MVT::i8 || LoadedVT == MVT::i1) {
867 if (LD->getExtensionType() == ISD::SEXTLOAD) {
868 if (SelectAddrMode3Offset(Op, LD->getOffset(), Offset, AMOpc)) {
870 Opcode = isPre ? ARM::LDRSB_PRE : ARM::LDRSB_POST;
873 if (SelectAddrMode2Offset(Op, LD->getOffset(), Offset, AMOpc)) {
875 Opcode = isPre ? ARM::LDRB_PRE : ARM::LDRB_POST;
881 SDValue Chain = LD->getChain();
882 SDValue Base = LD->getBasePtr();
883 SDValue Ops[]= { Base, Offset, AMOpc, getAL(CurDAG),
884 CurDAG->getRegister(0, MVT::i32), Chain };
885 return CurDAG->getMachineNode(Opcode, Op.getDebugLoc(), MVT::i32, MVT::i32,
892 SDNode *ARMDAGToDAGISel::SelectT2IndexedLoad(SDValue Op) {
893 LoadSDNode *LD = cast<LoadSDNode>(Op);
894 ISD::MemIndexedMode AM = LD->getAddressingMode();
895 if (AM == ISD::UNINDEXED)
898 EVT LoadedVT = LD->getMemoryVT();
899 bool isSExtLd = LD->getExtensionType() == ISD::SEXTLOAD;
901 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
904 if (SelectT2AddrModeImm8Offset(Op, LD->getOffset(), Offset)) {
905 switch (LoadedVT.getSimpleVT().SimpleTy) {
907 Opcode = isPre ? ARM::t2LDR_PRE : ARM::t2LDR_POST;
911 Opcode = isPre ? ARM::t2LDRSH_PRE : ARM::t2LDRSH_POST;
913 Opcode = isPre ? ARM::t2LDRH_PRE : ARM::t2LDRH_POST;
918 Opcode = isPre ? ARM::t2LDRSB_PRE : ARM::t2LDRSB_POST;
920 Opcode = isPre ? ARM::t2LDRB_PRE : ARM::t2LDRB_POST;
929 SDValue Chain = LD->getChain();
930 SDValue Base = LD->getBasePtr();
931 SDValue Ops[]= { Base, Offset, getAL(CurDAG),
932 CurDAG->getRegister(0, MVT::i32), Chain };
933 return CurDAG->getMachineNode(Opcode, Op.getDebugLoc(), MVT::i32, MVT::i32,
940 SDNode *ARMDAGToDAGISel::SelectDYN_ALLOC(SDValue Op) {
941 SDNode *N = Op.getNode();
942 DebugLoc dl = N->getDebugLoc();
943 EVT VT = Op.getValueType();
944 SDValue Chain = Op.getOperand(0);
945 SDValue Size = Op.getOperand(1);
946 SDValue Align = Op.getOperand(2);
947 SDValue SP = CurDAG->getRegister(ARM::SP, MVT::i32);
948 int32_t AlignVal = cast<ConstantSDNode>(Align)->getSExtValue();
950 // We need to align the stack. Use Thumb1 tAND which is the only thumb
951 // instruction that can read and write SP. This matches to a pseudo
952 // instruction that has a chain to ensure the result is written back to
953 // the stack pointer.
954 SP = SDValue(CurDAG->getMachineNode(ARM::tANDsp, dl, VT, SP, Align), 0);
956 bool isC = isa<ConstantSDNode>(Size);
957 uint32_t C = isC ? cast<ConstantSDNode>(Size)->getZExtValue() : ~0UL;
958 // Handle the most common case for both Thumb1 and Thumb2:
959 // tSUBspi - immediate is between 0 ... 508 inclusive.
960 if (C <= 508 && ((C & 3) == 0))
961 // FIXME: tSUBspi encode scale 4 implicitly.
962 return CurDAG->SelectNodeTo(N, ARM::tSUBspi_, VT, MVT::Other, SP,
963 CurDAG->getTargetConstant(C/4, MVT::i32),
966 if (Subtarget->isThumb1Only()) {
967 // Use tADDspr since Thumb1 does not have a sub r, sp, r. ARMISelLowering
968 // should have negated the size operand already. FIXME: We can't insert
969 // new target independent node at this stage so we are forced to negate
970 // it earlier. Is there a better solution?
971 return CurDAG->SelectNodeTo(N, ARM::tADDspr_, VT, MVT::Other, SP, Size,
973 } else if (Subtarget->isThumb2()) {
974 if (isC && Predicate_t2_so_imm(Size.getNode())) {
976 SDValue Ops[] = { SP, CurDAG->getTargetConstant(C, MVT::i32), Chain };
977 return CurDAG->SelectNodeTo(N, ARM::t2SUBrSPi_, VT, MVT::Other, Ops, 3);
978 } else if (isC && Predicate_imm0_4095(Size.getNode())) {
980 SDValue Ops[] = { SP, CurDAG->getTargetConstant(C, MVT::i32), Chain };
981 return CurDAG->SelectNodeTo(N, ARM::t2SUBrSPi12_, VT, MVT::Other, Ops, 3);
984 SDValue Ops[] = { SP, Size,
985 getI32Imm(ARM_AM::getSORegOpc(ARM_AM::lsl,0)), Chain };
986 return CurDAG->SelectNodeTo(N, ARM::t2SUBrSPs_, VT, MVT::Other, Ops, 4);
990 // FIXME: Add ADD / SUB sp instructions for ARM.
994 /// PairDRegs - Insert a pair of double registers into an implicit def to
995 /// form a quad register.
996 SDNode *ARMDAGToDAGISel::PairDRegs(EVT VT, SDValue V0, SDValue V1) {
997 DebugLoc dl = V0.getNode()->getDebugLoc();
999 SDValue(CurDAG->getMachineNode(TargetInstrInfo::IMPLICIT_DEF, dl, VT), 0);
1000 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::DSUBREG_0, MVT::i32);
1001 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::DSUBREG_1, MVT::i32);
1002 SDNode *Pair = CurDAG->getMachineNode(TargetInstrInfo::INSERT_SUBREG, dl,
1003 VT, Undef, V0, SubReg0);
1004 return CurDAG->getMachineNode(TargetInstrInfo::INSERT_SUBREG, dl,
1005 VT, SDValue(Pair, 0), V1, SubReg1);
1008 /// GetNEONSubregVT - Given a type for a 128-bit NEON vector, return the type
1009 /// for a 64-bit subregister of the vector.
1010 static EVT GetNEONSubregVT(EVT VT) {
1011 switch (VT.getSimpleVT().SimpleTy) {
1012 default: llvm_unreachable("unhandled NEON type");
1013 case MVT::v16i8: return MVT::v8i8;
1014 case MVT::v8i16: return MVT::v4i16;
1015 case MVT::v4f32: return MVT::v2f32;
1016 case MVT::v4i32: return MVT::v2i32;
1017 case MVT::v2i64: return MVT::v1i64;
1021 SDNode *ARMDAGToDAGISel::SelectVLD(SDValue Op, unsigned NumVecs,
1022 unsigned *DOpcodes, unsigned *QOpcodes0,
1023 unsigned *QOpcodes1) {
1024 assert(NumVecs >=2 && NumVecs <= 4 && "VLD NumVecs out-of-range");
1025 SDNode *N = Op.getNode();
1026 DebugLoc dl = N->getDebugLoc();
1028 SDValue MemAddr, MemUpdate, MemOpc, Align;
1029 if (!SelectAddrMode6(Op, N->getOperand(2), MemAddr, MemUpdate, MemOpc, Align))
1032 SDValue Chain = N->getOperand(0);
1033 EVT VT = N->getValueType(0);
1034 bool is64BitVector = VT.is64BitVector();
1036 unsigned OpcodeIndex;
1037 switch (VT.getSimpleVT().SimpleTy) {
1038 default: llvm_unreachable("unhandled vld type");
1039 // Double-register operations:
1040 case MVT::v8i8: OpcodeIndex = 0; break;
1041 case MVT::v4i16: OpcodeIndex = 1; break;
1043 case MVT::v2i32: OpcodeIndex = 2; break;
1044 case MVT::v1i64: OpcodeIndex = 3; break;
1045 // Quad-register operations:
1046 case MVT::v16i8: OpcodeIndex = 0; break;
1047 case MVT::v8i16: OpcodeIndex = 1; break;
1049 case MVT::v4i32: OpcodeIndex = 2; break;
1052 SDValue Pred = CurDAG->getTargetConstant(14, MVT::i32);
1053 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1054 if (is64BitVector) {
1055 unsigned Opc = DOpcodes[OpcodeIndex];
1056 const SDValue Ops[] = { MemAddr, MemUpdate, MemOpc, Align,
1057 Pred, PredReg, Chain };
1058 std::vector<EVT> ResTys(NumVecs, VT);
1059 ResTys.push_back(MVT::Other);
1060 return CurDAG->getMachineNode(Opc, dl, ResTys, Ops, 7);
1063 EVT RegVT = GetNEONSubregVT(VT);
1065 // Quad registers are directly supported for VLD2,
1066 // loading 2 pairs of D regs.
1067 unsigned Opc = QOpcodes0[OpcodeIndex];
1068 const SDValue Ops[] = { MemAddr, MemUpdate, MemOpc, Align,
1069 Pred, PredReg, Chain };
1070 std::vector<EVT> ResTys(4, VT);
1071 ResTys.push_back(MVT::Other);
1072 SDNode *VLd = CurDAG->getMachineNode(Opc, dl, ResTys, Ops, 7);
1073 Chain = SDValue(VLd, 4);
1075 // Combine the even and odd subregs to produce the result.
1076 for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
1077 SDNode *Q = PairDRegs(VT, SDValue(VLd, 2*Vec), SDValue(VLd, 2*Vec+1));
1078 ReplaceUses(SDValue(N, Vec), SDValue(Q, 0));
1081 // Otherwise, quad registers are loaded with two separate instructions,
1082 // where one loads the even registers and the other loads the odd registers.
1084 // Enable writeback to the address register.
1085 MemOpc = CurDAG->getTargetConstant(ARM_AM::getAM6Opc(true), MVT::i32);
1087 std::vector<EVT> ResTys(NumVecs, RegVT);
1088 ResTys.push_back(MemAddr.getValueType());
1089 ResTys.push_back(MVT::Other);
1091 // Load the even subregs.
1092 unsigned Opc = QOpcodes0[OpcodeIndex];
1093 const SDValue OpsA[] = { MemAddr, MemUpdate, MemOpc, Align,
1094 Pred, PredReg, Chain };
1095 SDNode *VLdA = CurDAG->getMachineNode(Opc, dl, ResTys, OpsA, 7);
1096 Chain = SDValue(VLdA, NumVecs+1);
1098 // Load the odd subregs.
1099 Opc = QOpcodes1[OpcodeIndex];
1100 const SDValue OpsB[] = { SDValue(VLdA, NumVecs), MemUpdate, MemOpc,
1101 Align, Pred, PredReg, Chain };
1102 SDNode *VLdB = CurDAG->getMachineNode(Opc, dl, ResTys, OpsB, 7);
1103 Chain = SDValue(VLdB, NumVecs+1);
1105 // Combine the even and odd subregs to produce the result.
1106 for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
1107 SDNode *Q = PairDRegs(VT, SDValue(VLdA, Vec), SDValue(VLdB, Vec));
1108 ReplaceUses(SDValue(N, Vec), SDValue(Q, 0));
1111 ReplaceUses(SDValue(N, NumVecs), Chain);
1115 SDNode *ARMDAGToDAGISel::SelectVST(SDValue Op, unsigned NumVecs,
1116 unsigned *DOpcodes, unsigned *QOpcodes0,
1117 unsigned *QOpcodes1) {
1118 assert(NumVecs >=2 && NumVecs <= 4 && "VST NumVecs out-of-range");
1119 SDNode *N = Op.getNode();
1120 DebugLoc dl = N->getDebugLoc();
1122 SDValue MemAddr, MemUpdate, MemOpc, Align;
1123 if (!SelectAddrMode6(Op, N->getOperand(2), MemAddr, MemUpdate, MemOpc, Align))
1126 SDValue Chain = N->getOperand(0);
1127 EVT VT = N->getOperand(3).getValueType();
1128 bool is64BitVector = VT.is64BitVector();
1130 unsigned OpcodeIndex;
1131 switch (VT.getSimpleVT().SimpleTy) {
1132 default: llvm_unreachable("unhandled vst type");
1133 // Double-register operations:
1134 case MVT::v8i8: OpcodeIndex = 0; break;
1135 case MVT::v4i16: OpcodeIndex = 1; break;
1137 case MVT::v2i32: OpcodeIndex = 2; break;
1138 case MVT::v1i64: OpcodeIndex = 3; break;
1139 // Quad-register operations:
1140 case MVT::v16i8: OpcodeIndex = 0; break;
1141 case MVT::v8i16: OpcodeIndex = 1; break;
1143 case MVT::v4i32: OpcodeIndex = 2; break;
1146 SDValue Pred = CurDAG->getTargetConstant(14, MVT::i32);
1147 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1149 SmallVector<SDValue, 8> Ops;
1150 Ops.push_back(MemAddr);
1151 Ops.push_back(MemUpdate);
1152 Ops.push_back(MemOpc);
1153 Ops.push_back(Align);
1155 if (is64BitVector) {
1156 unsigned Opc = DOpcodes[OpcodeIndex];
1157 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1158 Ops.push_back(N->getOperand(Vec+3));
1159 Ops.push_back(Pred);
1160 Ops.push_back(PredReg);
1161 Ops.push_back(Chain);
1162 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), NumVecs+7);
1165 EVT RegVT = GetNEONSubregVT(VT);
1167 // Quad registers are directly supported for VST2,
1168 // storing 2 pairs of D regs.
1169 unsigned Opc = QOpcodes0[OpcodeIndex];
1170 for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
1171 Ops.push_back(CurDAG->getTargetExtractSubreg(ARM::DSUBREG_0, dl, RegVT,
1172 N->getOperand(Vec+3)));
1173 Ops.push_back(CurDAG->getTargetExtractSubreg(ARM::DSUBREG_1, dl, RegVT,
1174 N->getOperand(Vec+3)));
1176 Ops.push_back(Pred);
1177 Ops.push_back(PredReg);
1178 Ops.push_back(Chain);
1179 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 11);
1182 // Otherwise, quad registers are stored with two separate instructions,
1183 // where one stores the even registers and the other stores the odd registers.
1185 // Enable writeback to the address register.
1186 MemOpc = CurDAG->getTargetConstant(ARM_AM::getAM6Opc(true), MVT::i32);
1188 // Store the even subregs.
1189 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1190 Ops.push_back(CurDAG->getTargetExtractSubreg(ARM::DSUBREG_0, dl, RegVT,
1191 N->getOperand(Vec+3)));
1192 Ops.push_back(Pred);
1193 Ops.push_back(PredReg);
1194 Ops.push_back(Chain);
1195 unsigned Opc = QOpcodes0[OpcodeIndex];
1196 SDNode *VStA = CurDAG->getMachineNode(Opc, dl, MemAddr.getValueType(),
1197 MVT::Other, Ops.data(), NumVecs+7);
1198 Chain = SDValue(VStA, 1);
1200 // Store the odd subregs.
1201 Ops[0] = SDValue(VStA, 0); // MemAddr
1202 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1203 Ops[Vec+4] = CurDAG->getTargetExtractSubreg(ARM::DSUBREG_1, dl, RegVT,
1204 N->getOperand(Vec+3));
1205 Ops[NumVecs+4] = Pred;
1206 Ops[NumVecs+5] = PredReg;
1207 Ops[NumVecs+6] = Chain;
1208 Opc = QOpcodes1[OpcodeIndex];
1209 SDNode *VStB = CurDAG->getMachineNode(Opc, dl, MemAddr.getValueType(),
1210 MVT::Other, Ops.data(), NumVecs+7);
1211 Chain = SDValue(VStB, 1);
1212 ReplaceUses(SDValue(N, 0), Chain);
1216 SDNode *ARMDAGToDAGISel::SelectVLDSTLane(SDValue Op, bool IsLoad,
1217 unsigned NumVecs, unsigned *DOpcodes,
1218 unsigned *QOpcodes0,
1219 unsigned *QOpcodes1) {
1220 assert(NumVecs >=2 && NumVecs <= 4 && "VLDSTLane NumVecs out-of-range");
1221 SDNode *N = Op.getNode();
1222 DebugLoc dl = N->getDebugLoc();
1224 SDValue MemAddr, MemUpdate, MemOpc, Align;
1225 if (!SelectAddrMode6(Op, N->getOperand(2), MemAddr, MemUpdate, MemOpc, Align))
1228 SDValue Chain = N->getOperand(0);
1230 cast<ConstantSDNode>(N->getOperand(NumVecs+3))->getZExtValue();
1231 EVT VT = IsLoad ? N->getValueType(0) : N->getOperand(3).getValueType();
1232 bool is64BitVector = VT.is64BitVector();
1234 // Quad registers are handled by load/store of subregs. Find the subreg info.
1235 unsigned NumElts = 0;
1238 if (!is64BitVector) {
1239 RegVT = GetNEONSubregVT(VT);
1240 NumElts = RegVT.getVectorNumElements();
1241 SubregIdx = (Lane < NumElts) ? ARM::DSUBREG_0 : ARM::DSUBREG_1;
1244 unsigned OpcodeIndex;
1245 switch (VT.getSimpleVT().SimpleTy) {
1246 default: llvm_unreachable("unhandled vld/vst lane type");
1247 // Double-register operations:
1248 case MVT::v8i8: OpcodeIndex = 0; break;
1249 case MVT::v4i16: OpcodeIndex = 1; break;
1251 case MVT::v2i32: OpcodeIndex = 2; break;
1252 // Quad-register operations:
1253 case MVT::v8i16: OpcodeIndex = 0; break;
1255 case MVT::v4i32: OpcodeIndex = 1; break;
1258 SDValue Pred = CurDAG->getTargetConstant(14, MVT::i32);
1259 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1261 SmallVector<SDValue, 9> Ops;
1262 Ops.push_back(MemAddr);
1263 Ops.push_back(MemUpdate);
1264 Ops.push_back(MemOpc);
1265 Ops.push_back(Align);
1268 if (is64BitVector) {
1269 Opc = DOpcodes[OpcodeIndex];
1270 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1271 Ops.push_back(N->getOperand(Vec+3));
1273 // Check if this is loading the even or odd subreg of a Q register.
1274 if (Lane < NumElts) {
1275 Opc = QOpcodes0[OpcodeIndex];
1278 Opc = QOpcodes1[OpcodeIndex];
1280 // Extract the subregs of the input vector.
1281 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1282 Ops.push_back(CurDAG->getTargetExtractSubreg(SubregIdx, dl, RegVT,
1283 N->getOperand(Vec+3)));
1285 Ops.push_back(getI32Imm(Lane));
1286 Ops.push_back(Pred);
1287 Ops.push_back(PredReg);
1288 Ops.push_back(Chain);
1291 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), NumVecs+7);
1293 std::vector<EVT> ResTys(NumVecs, RegVT);
1294 ResTys.push_back(MVT::Other);
1296 CurDAG->getMachineNode(Opc, dl, ResTys, Ops.data(), NumVecs+7);
1297 // For a 64-bit vector load to D registers, nothing more needs to be done.
1301 // For 128-bit vectors, take the 64-bit results of the load and insert them
1302 // as subregs into the result.
1303 for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
1304 SDValue QuadVec = CurDAG->getTargetInsertSubreg(SubregIdx, dl, VT,
1305 N->getOperand(Vec+3),
1306 SDValue(VLdLn, Vec));
1307 ReplaceUses(SDValue(N, Vec), QuadVec);
1310 Chain = SDValue(VLdLn, NumVecs);
1311 ReplaceUses(SDValue(N, NumVecs), Chain);
1315 SDNode *ARMDAGToDAGISel::SelectV6T2BitfieldExtractOp(SDValue Op,
1317 if (!Subtarget->hasV6T2Ops())
1320 unsigned Shl_imm = 0;
1321 if (isOpcWithIntImmediate(Op.getOperand(0).getNode(), ISD::SHL, Shl_imm)) {
1322 assert(Shl_imm > 0 && Shl_imm < 32 && "bad amount in shift node!");
1323 unsigned Srl_imm = 0;
1324 if (isInt32Immediate(Op.getOperand(1), Srl_imm)) {
1325 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
1326 unsigned Width = 32 - Srl_imm;
1327 int LSB = Srl_imm - Shl_imm;
1330 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1331 SDValue Ops[] = { Op.getOperand(0).getOperand(0),
1332 CurDAG->getTargetConstant(LSB, MVT::i32),
1333 CurDAG->getTargetConstant(Width, MVT::i32),
1334 getAL(CurDAG), Reg0 };
1335 return CurDAG->SelectNodeTo(Op.getNode(), Opc, MVT::i32, Ops, 5);
1341 SDNode *ARMDAGToDAGISel::
1342 SelectT2CMOVShiftOp(SDValue Op, SDValue FalseVal, SDValue TrueVal,
1343 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1346 if (SelectT2ShifterOperandReg(Op, TrueVal, CPTmp0, CPTmp1)) {
1347 unsigned SOVal = cast<ConstantSDNode>(CPTmp1)->getZExtValue();
1348 unsigned SOShOp = ARM_AM::getSORegShOp(SOVal);
1351 case ARM_AM::lsl: Opc = ARM::t2MOVCClsl; break;
1352 case ARM_AM::lsr: Opc = ARM::t2MOVCClsr; break;
1353 case ARM_AM::asr: Opc = ARM::t2MOVCCasr; break;
1354 case ARM_AM::ror: Opc = ARM::t2MOVCCror; break;
1356 llvm_unreachable("Unknown so_reg opcode!");
1360 CurDAG->getTargetConstant(ARM_AM::getSORegOffset(SOVal), MVT::i32);
1361 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1362 SDValue Ops[] = { FalseVal, CPTmp0, SOShImm, CC, CCR, InFlag };
1363 return CurDAG->SelectNodeTo(Op.getNode(), Opc, MVT::i32,Ops, 6);
1368 SDNode *ARMDAGToDAGISel::
1369 SelectARMCMOVShiftOp(SDValue Op, SDValue FalseVal, SDValue TrueVal,
1370 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1374 if (SelectShifterOperandReg(Op, TrueVal, CPTmp0, CPTmp1, CPTmp2)) {
1375 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1376 SDValue Ops[] = { FalseVal, CPTmp0, CPTmp1, CPTmp2, CC, CCR, InFlag };
1377 return CurDAG->SelectNodeTo(Op.getNode(), ARM::MOVCCs, MVT::i32, Ops, 7);
1382 SDNode *ARMDAGToDAGISel::
1383 SelectT2CMOVSoImmOp(SDValue Op, SDValue FalseVal, SDValue TrueVal,
1384 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1385 ConstantSDNode *T = dyn_cast<ConstantSDNode>(TrueVal);
1389 if (Predicate_t2_so_imm(TrueVal.getNode())) {
1390 SDValue True = CurDAG->getTargetConstant(T->getZExtValue(), MVT::i32);
1391 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1392 SDValue Ops[] = { FalseVal, True, CC, CCR, InFlag };
1393 return CurDAG->SelectNodeTo(Op.getNode(),
1394 ARM::t2MOVCCi, MVT::i32, Ops, 5);
1399 SDNode *ARMDAGToDAGISel::
1400 SelectARMCMOVSoImmOp(SDValue Op, SDValue FalseVal, SDValue TrueVal,
1401 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1402 ConstantSDNode *T = dyn_cast<ConstantSDNode>(TrueVal);
1406 if (Predicate_so_imm(TrueVal.getNode())) {
1407 SDValue True = CurDAG->getTargetConstant(T->getZExtValue(), MVT::i32);
1408 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1409 SDValue Ops[] = { FalseVal, True, CC, CCR, InFlag };
1410 return CurDAG->SelectNodeTo(Op.getNode(),
1411 ARM::MOVCCi, MVT::i32, Ops, 5);
1416 SDNode *ARMDAGToDAGISel::SelectCMOVOp(SDValue Op) {
1417 EVT VT = Op.getValueType();
1418 SDValue FalseVal = Op.getOperand(0);
1419 SDValue TrueVal = Op.getOperand(1);
1420 SDValue CC = Op.getOperand(2);
1421 SDValue CCR = Op.getOperand(3);
1422 SDValue InFlag = Op.getOperand(4);
1423 assert(CC.getOpcode() == ISD::Constant);
1424 assert(CCR.getOpcode() == ISD::Register);
1425 ARMCC::CondCodes CCVal =
1426 (ARMCC::CondCodes)cast<ConstantSDNode>(CC)->getZExtValue();
1428 if (!Subtarget->isThumb1Only() && VT == MVT::i32) {
1429 // Pattern: (ARMcmov:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
1430 // Emits: (MOVCCs:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
1431 // Pattern complexity = 18 cost = 1 size = 0
1435 if (Subtarget->isThumb()) {
1436 SDNode *Res = SelectT2CMOVShiftOp(Op, FalseVal, TrueVal,
1437 CCVal, CCR, InFlag);
1439 Res = SelectT2CMOVShiftOp(Op, TrueVal, FalseVal,
1440 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1444 SDNode *Res = SelectARMCMOVShiftOp(Op, FalseVal, TrueVal,
1445 CCVal, CCR, InFlag);
1447 Res = SelectARMCMOVShiftOp(Op, TrueVal, FalseVal,
1448 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1453 // Pattern: (ARMcmov:i32 GPR:i32:$false,
1454 // (imm:i32)<<P:Predicate_so_imm>>:$true,
1456 // Emits: (MOVCCi:i32 GPR:i32:$false,
1457 // (so_imm:i32 (imm:i32):$true), (imm:i32):$cc)
1458 // Pattern complexity = 10 cost = 1 size = 0
1459 if (Subtarget->isThumb()) {
1460 SDNode *Res = SelectT2CMOVSoImmOp(Op, FalseVal, TrueVal,
1461 CCVal, CCR, InFlag);
1463 Res = SelectT2CMOVSoImmOp(Op, TrueVal, FalseVal,
1464 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1468 SDNode *Res = SelectARMCMOVSoImmOp(Op, FalseVal, TrueVal,
1469 CCVal, CCR, InFlag);
1471 Res = SelectARMCMOVSoImmOp(Op, TrueVal, FalseVal,
1472 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1478 // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1479 // Emits: (MOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1480 // Pattern complexity = 6 cost = 1 size = 0
1482 // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1483 // Emits: (tMOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1484 // Pattern complexity = 6 cost = 11 size = 0
1486 // Also FCPYScc and FCPYDcc.
1487 SDValue Tmp2 = CurDAG->getTargetConstant(CCVal, MVT::i32);
1488 SDValue Ops[] = { FalseVal, TrueVal, Tmp2, CCR, InFlag };
1490 switch (VT.getSimpleVT().SimpleTy) {
1491 default: assert(false && "Illegal conditional move type!");
1494 Opc = Subtarget->isThumb()
1495 ? (Subtarget->hasThumb2() ? ARM::t2MOVCCr : ARM::tMOVCCr_pseudo)
1505 return CurDAG->SelectNodeTo(Op.getNode(), Opc, VT, Ops, 5);
1508 SDNode *ARMDAGToDAGISel::Select(SDValue Op) {
1509 SDNode *N = Op.getNode();
1510 DebugLoc dl = N->getDebugLoc();
1512 if (N->isMachineOpcode())
1513 return NULL; // Already selected.
1515 switch (N->getOpcode()) {
1517 case ISD::Constant: {
1518 unsigned Val = cast<ConstantSDNode>(N)->getZExtValue();
1520 if (Subtarget->hasThumb2())
1521 // Thumb2-aware targets have the MOVT instruction, so all immediates can
1522 // be done with MOV + MOVT, at worst.
1525 if (Subtarget->isThumb()) {
1526 UseCP = (Val > 255 && // MOV
1527 ~Val > 255 && // MOV + MVN
1528 !ARM_AM::isThumbImmShiftedVal(Val)); // MOV + LSL
1530 UseCP = (ARM_AM::getSOImmVal(Val) == -1 && // MOV
1531 ARM_AM::getSOImmVal(~Val) == -1 && // MVN
1532 !ARM_AM::isSOImmTwoPartVal(Val)); // two instrs.
1537 CurDAG->getTargetConstantPool(ConstantInt::get(
1538 Type::getInt32Ty(*CurDAG->getContext()), Val),
1539 TLI.getPointerTy());
1542 if (Subtarget->isThumb1Only()) {
1543 SDValue Pred = CurDAG->getTargetConstant(14, MVT::i32);
1544 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1545 SDValue Ops[] = { CPIdx, Pred, PredReg, CurDAG->getEntryNode() };
1546 ResNode = CurDAG->getMachineNode(ARM::tLDRcp, dl, MVT::i32, MVT::Other,
1551 CurDAG->getRegister(0, MVT::i32),
1552 CurDAG->getTargetConstant(0, MVT::i32),
1554 CurDAG->getRegister(0, MVT::i32),
1555 CurDAG->getEntryNode()
1557 ResNode=CurDAG->getMachineNode(ARM::LDRcp, dl, MVT::i32, MVT::Other,
1560 ReplaceUses(Op, SDValue(ResNode, 0));
1564 // Other cases are autogenerated.
1567 case ISD::FrameIndex: {
1568 // Selects to ADDri FI, 0 which in turn will become ADDri SP, imm.
1569 int FI = cast<FrameIndexSDNode>(N)->getIndex();
1570 SDValue TFI = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
1571 if (Subtarget->isThumb1Only()) {
1572 return CurDAG->SelectNodeTo(N, ARM::tADDrSPi, MVT::i32, TFI,
1573 CurDAG->getTargetConstant(0, MVT::i32));
1575 unsigned Opc = ((Subtarget->isThumb() && Subtarget->hasThumb2()) ?
1576 ARM::t2ADDri : ARM::ADDri);
1577 SDValue Ops[] = { TFI, CurDAG->getTargetConstant(0, MVT::i32),
1578 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1579 CurDAG->getRegister(0, MVT::i32) };
1580 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
1583 case ARMISD::DYN_ALLOC:
1584 return SelectDYN_ALLOC(Op);
1586 if (SDNode *I = SelectV6T2BitfieldExtractOp(Op,
1587 Subtarget->isThumb() ? ARM::t2UBFX : ARM::UBFX))
1591 if (SDNode *I = SelectV6T2BitfieldExtractOp(Op,
1592 Subtarget->isThumb() ? ARM::t2SBFX : ARM::SBFX))
1596 if (Subtarget->isThumb1Only())
1598 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1599 unsigned RHSV = C->getZExtValue();
1601 if (isPowerOf2_32(RHSV-1)) { // 2^n+1?
1602 unsigned ShImm = Log2_32(RHSV-1);
1605 SDValue V = Op.getOperand(0);
1606 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
1607 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, MVT::i32);
1608 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1609 if (Subtarget->isThumb()) {
1610 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
1611 return CurDAG->SelectNodeTo(N, ARM::t2ADDrs, MVT::i32, Ops, 6);
1613 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
1614 return CurDAG->SelectNodeTo(N, ARM::ADDrs, MVT::i32, Ops, 7);
1617 if (isPowerOf2_32(RHSV+1)) { // 2^n-1?
1618 unsigned ShImm = Log2_32(RHSV+1);
1621 SDValue V = Op.getOperand(0);
1622 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
1623 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, MVT::i32);
1624 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1625 if (Subtarget->isThumb()) {
1626 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG), Reg0 };
1627 return CurDAG->SelectNodeTo(N, ARM::t2RSBrs, MVT::i32, Ops, 5);
1629 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
1630 return CurDAG->SelectNodeTo(N, ARM::RSBrs, MVT::i32, Ops, 7);
1636 // (and (or x, c2), c1) and top 16-bits of c1 and c2 match, lower 16-bits
1637 // of c1 are 0xffff, and lower 16-bit of c2 are 0. That is, the top 16-bits
1638 // are entirely contributed by c2 and lower 16-bits are entirely contributed
1639 // by x. That's equal to (or (and x, 0xffff), (and c1, 0xffff0000)).
1640 // Select it to: "movt x, ((c1 & 0xffff) >> 16)
1641 EVT VT = Op.getValueType();
1644 unsigned Opc = (Subtarget->isThumb() && Subtarget->hasThumb2())
1646 : (Subtarget->hasV6T2Ops() ? ARM::MOVTi16 : 0);
1649 SDValue N0 = Op.getOperand(0), N1 = Op.getOperand(1);
1650 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1653 if (N0.getOpcode() == ISD::OR && N0.getNode()->hasOneUse()) {
1654 SDValue N2 = N0.getOperand(1);
1655 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1658 unsigned N1CVal = N1C->getZExtValue();
1659 unsigned N2CVal = N2C->getZExtValue();
1660 if ((N1CVal & 0xffff0000U) == (N2CVal & 0xffff0000U) &&
1661 (N1CVal & 0xffffU) == 0xffffU &&
1662 (N2CVal & 0xffffU) == 0x0U) {
1663 SDValue Imm16 = CurDAG->getTargetConstant((N2CVal & 0xFFFF0000U) >> 16,
1665 SDValue Ops[] = { N0.getOperand(0), Imm16,
1666 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
1667 return CurDAG->getMachineNode(Opc, dl, VT, Ops, 4);
1672 case ARMISD::VMOVRRD:
1673 return CurDAG->getMachineNode(ARM::VMOVRRD, dl, MVT::i32, MVT::i32,
1674 Op.getOperand(0), getAL(CurDAG),
1675 CurDAG->getRegister(0, MVT::i32));
1676 case ISD::UMUL_LOHI: {
1677 if (Subtarget->isThumb1Only())
1679 if (Subtarget->isThumb()) {
1680 SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1),
1681 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1682 CurDAG->getRegister(0, MVT::i32) };
1683 return CurDAG->getMachineNode(ARM::t2UMULL, dl, MVT::i32, MVT::i32, Ops,4);
1685 SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1),
1686 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1687 CurDAG->getRegister(0, MVT::i32) };
1688 return CurDAG->getMachineNode(ARM::UMULL, dl, MVT::i32, MVT::i32, Ops, 5);
1691 case ISD::SMUL_LOHI: {
1692 if (Subtarget->isThumb1Only())
1694 if (Subtarget->isThumb()) {
1695 SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1),
1696 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
1697 return CurDAG->getMachineNode(ARM::t2SMULL, dl, MVT::i32, MVT::i32, Ops,4);
1699 SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1),
1700 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1701 CurDAG->getRegister(0, MVT::i32) };
1702 return CurDAG->getMachineNode(ARM::SMULL, dl, MVT::i32, MVT::i32, Ops, 5);
1706 SDNode *ResNode = 0;
1707 if (Subtarget->isThumb() && Subtarget->hasThumb2())
1708 ResNode = SelectT2IndexedLoad(Op);
1710 ResNode = SelectARMIndexedLoad(Op);
1713 // Other cases are autogenerated.
1716 case ARMISD::BRCOND: {
1717 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
1718 // Emits: (Bcc:void (bb:Other):$dst, (imm:i32):$cc)
1719 // Pattern complexity = 6 cost = 1 size = 0
1721 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
1722 // Emits: (tBcc:void (bb:Other):$dst, (imm:i32):$cc)
1723 // Pattern complexity = 6 cost = 1 size = 0
1725 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
1726 // Emits: (t2Bcc:void (bb:Other):$dst, (imm:i32):$cc)
1727 // Pattern complexity = 6 cost = 1 size = 0
1729 unsigned Opc = Subtarget->isThumb() ?
1730 ((Subtarget->hasThumb2()) ? ARM::t2Bcc : ARM::tBcc) : ARM::Bcc;
1731 SDValue Chain = Op.getOperand(0);
1732 SDValue N1 = Op.getOperand(1);
1733 SDValue N2 = Op.getOperand(2);
1734 SDValue N3 = Op.getOperand(3);
1735 SDValue InFlag = Op.getOperand(4);
1736 assert(N1.getOpcode() == ISD::BasicBlock);
1737 assert(N2.getOpcode() == ISD::Constant);
1738 assert(N3.getOpcode() == ISD::Register);
1740 SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
1741 cast<ConstantSDNode>(N2)->getZExtValue()),
1743 SDValue Ops[] = { N1, Tmp2, N3, Chain, InFlag };
1744 SDNode *ResNode = CurDAG->getMachineNode(Opc, dl, MVT::Other,
1746 Chain = SDValue(ResNode, 0);
1747 if (Op.getNode()->getNumValues() == 2) {
1748 InFlag = SDValue(ResNode, 1);
1749 ReplaceUses(SDValue(Op.getNode(), 1), InFlag);
1751 ReplaceUses(SDValue(Op.getNode(), 0),
1752 SDValue(Chain.getNode(), Chain.getResNo()));
1756 return SelectCMOVOp(Op);
1757 case ARMISD::CNEG: {
1758 EVT VT = Op.getValueType();
1759 SDValue N0 = Op.getOperand(0);
1760 SDValue N1 = Op.getOperand(1);
1761 SDValue N2 = Op.getOperand(2);
1762 SDValue N3 = Op.getOperand(3);
1763 SDValue InFlag = Op.getOperand(4);
1764 assert(N2.getOpcode() == ISD::Constant);
1765 assert(N3.getOpcode() == ISD::Register);
1767 SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
1768 cast<ConstantSDNode>(N2)->getZExtValue()),
1770 SDValue Ops[] = { N0, N1, Tmp2, N3, InFlag };
1772 switch (VT.getSimpleVT().SimpleTy) {
1773 default: assert(false && "Illegal conditional move type!");
1782 return CurDAG->SelectNodeTo(Op.getNode(), Opc, VT, Ops, 5);
1785 case ARMISD::VZIP: {
1787 EVT VT = N->getValueType(0);
1788 switch (VT.getSimpleVT().SimpleTy) {
1789 default: return NULL;
1790 case MVT::v8i8: Opc = ARM::VZIPd8; break;
1791 case MVT::v4i16: Opc = ARM::VZIPd16; break;
1793 case MVT::v2i32: Opc = ARM::VZIPd32; break;
1794 case MVT::v16i8: Opc = ARM::VZIPq8; break;
1795 case MVT::v8i16: Opc = ARM::VZIPq16; break;
1797 case MVT::v4i32: Opc = ARM::VZIPq32; break;
1799 SDValue Pred = CurDAG->getTargetConstant(14, MVT::i32);
1800 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1801 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
1802 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
1804 case ARMISD::VUZP: {
1806 EVT VT = N->getValueType(0);
1807 switch (VT.getSimpleVT().SimpleTy) {
1808 default: return NULL;
1809 case MVT::v8i8: Opc = ARM::VUZPd8; break;
1810 case MVT::v4i16: Opc = ARM::VUZPd16; break;
1812 case MVT::v2i32: Opc = ARM::VUZPd32; break;
1813 case MVT::v16i8: Opc = ARM::VUZPq8; break;
1814 case MVT::v8i16: Opc = ARM::VUZPq16; break;
1816 case MVT::v4i32: Opc = ARM::VUZPq32; break;
1818 SDValue Pred = CurDAG->getTargetConstant(14, MVT::i32);
1819 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1820 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
1821 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
1823 case ARMISD::VTRN: {
1825 EVT VT = N->getValueType(0);
1826 switch (VT.getSimpleVT().SimpleTy) {
1827 default: return NULL;
1828 case MVT::v8i8: Opc = ARM::VTRNd8; break;
1829 case MVT::v4i16: Opc = ARM::VTRNd16; break;
1831 case MVT::v2i32: Opc = ARM::VTRNd32; break;
1832 case MVT::v16i8: Opc = ARM::VTRNq8; break;
1833 case MVT::v8i16: Opc = ARM::VTRNq16; break;
1835 case MVT::v4i32: Opc = ARM::VTRNq32; break;
1837 SDValue Pred = CurDAG->getTargetConstant(14, MVT::i32);
1838 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1839 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
1840 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
1843 case ISD::INTRINSIC_VOID:
1844 case ISD::INTRINSIC_W_CHAIN: {
1845 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
1850 case Intrinsic::arm_neon_vld2: {
1851 unsigned DOpcodes[] = { ARM::VLD2d8, ARM::VLD2d16,
1852 ARM::VLD2d32, ARM::VLD2d64 };
1853 unsigned QOpcodes[] = { ARM::VLD2q8, ARM::VLD2q16, ARM::VLD2q32 };
1854 return SelectVLD(Op, 2, DOpcodes, QOpcodes, 0);
1857 case Intrinsic::arm_neon_vld3: {
1858 unsigned DOpcodes[] = { ARM::VLD3d8, ARM::VLD3d16,
1859 ARM::VLD3d32, ARM::VLD3d64 };
1860 unsigned QOpcodes0[] = { ARM::VLD3q8a, ARM::VLD3q16a, ARM::VLD3q32a };
1861 unsigned QOpcodes1[] = { ARM::VLD3q8b, ARM::VLD3q16b, ARM::VLD3q32b };
1862 return SelectVLD(Op, 3, DOpcodes, QOpcodes0, QOpcodes1);
1865 case Intrinsic::arm_neon_vld4: {
1866 unsigned DOpcodes[] = { ARM::VLD4d8, ARM::VLD4d16,
1867 ARM::VLD4d32, ARM::VLD4d64 };
1868 unsigned QOpcodes0[] = { ARM::VLD4q8a, ARM::VLD4q16a, ARM::VLD4q32a };
1869 unsigned QOpcodes1[] = { ARM::VLD4q8b, ARM::VLD4q16b, ARM::VLD4q32b };
1870 return SelectVLD(Op, 4, DOpcodes, QOpcodes0, QOpcodes1);
1873 case Intrinsic::arm_neon_vld2lane: {
1874 unsigned DOpcodes[] = { ARM::VLD2LNd8, ARM::VLD2LNd16, ARM::VLD2LNd32 };
1875 unsigned QOpcodes0[] = { ARM::VLD2LNq16a, ARM::VLD2LNq32a };
1876 unsigned QOpcodes1[] = { ARM::VLD2LNq16b, ARM::VLD2LNq32b };
1877 return SelectVLDSTLane(Op, true, 2, DOpcodes, QOpcodes0, QOpcodes1);
1880 case Intrinsic::arm_neon_vld3lane: {
1881 unsigned DOpcodes[] = { ARM::VLD3LNd8, ARM::VLD3LNd16, ARM::VLD3LNd32 };
1882 unsigned QOpcodes0[] = { ARM::VLD3LNq16a, ARM::VLD3LNq32a };
1883 unsigned QOpcodes1[] = { ARM::VLD3LNq16b, ARM::VLD3LNq32b };
1884 return SelectVLDSTLane(Op, true, 3, DOpcodes, QOpcodes0, QOpcodes1);
1887 case Intrinsic::arm_neon_vld4lane: {
1888 unsigned DOpcodes[] = { ARM::VLD4LNd8, ARM::VLD4LNd16, ARM::VLD4LNd32 };
1889 unsigned QOpcodes0[] = { ARM::VLD4LNq16a, ARM::VLD4LNq32a };
1890 unsigned QOpcodes1[] = { ARM::VLD4LNq16b, ARM::VLD4LNq32b };
1891 return SelectVLDSTLane(Op, true, 4, DOpcodes, QOpcodes0, QOpcodes1);
1894 case Intrinsic::arm_neon_vst2: {
1895 unsigned DOpcodes[] = { ARM::VST2d8, ARM::VST2d16,
1896 ARM::VST2d32, ARM::VST2d64 };
1897 unsigned QOpcodes[] = { ARM::VST2q8, ARM::VST2q16, ARM::VST2q32 };
1898 return SelectVST(Op, 2, DOpcodes, QOpcodes, 0);
1901 case Intrinsic::arm_neon_vst3: {
1902 unsigned DOpcodes[] = { ARM::VST3d8, ARM::VST3d16,
1903 ARM::VST3d32, ARM::VST3d64 };
1904 unsigned QOpcodes0[] = { ARM::VST3q8a, ARM::VST3q16a, ARM::VST3q32a };
1905 unsigned QOpcodes1[] = { ARM::VST3q8b, ARM::VST3q16b, ARM::VST3q32b };
1906 return SelectVST(Op, 3, DOpcodes, QOpcodes0, QOpcodes1);
1909 case Intrinsic::arm_neon_vst4: {
1910 unsigned DOpcodes[] = { ARM::VST4d8, ARM::VST4d16,
1911 ARM::VST4d32, ARM::VST4d64 };
1912 unsigned QOpcodes0[] = { ARM::VST4q8a, ARM::VST4q16a, ARM::VST4q32a };
1913 unsigned QOpcodes1[] = { ARM::VST4q8b, ARM::VST4q16b, ARM::VST4q32b };
1914 return SelectVST(Op, 4, DOpcodes, QOpcodes0, QOpcodes1);
1917 case Intrinsic::arm_neon_vst2lane: {
1918 unsigned DOpcodes[] = { ARM::VST2LNd8, ARM::VST2LNd16, ARM::VST2LNd32 };
1919 unsigned QOpcodes0[] = { ARM::VST2LNq16a, ARM::VST2LNq32a };
1920 unsigned QOpcodes1[] = { ARM::VST2LNq16b, ARM::VST2LNq32b };
1921 return SelectVLDSTLane(Op, false, 2, DOpcodes, QOpcodes0, QOpcodes1);
1924 case Intrinsic::arm_neon_vst3lane: {
1925 unsigned DOpcodes[] = { ARM::VST3LNd8, ARM::VST3LNd16, ARM::VST3LNd32 };
1926 unsigned QOpcodes0[] = { ARM::VST3LNq16a, ARM::VST3LNq32a };
1927 unsigned QOpcodes1[] = { ARM::VST3LNq16b, ARM::VST3LNq32b };
1928 return SelectVLDSTLane(Op, false, 3, DOpcodes, QOpcodes0, QOpcodes1);
1931 case Intrinsic::arm_neon_vst4lane: {
1932 unsigned DOpcodes[] = { ARM::VST4LNd8, ARM::VST4LNd16, ARM::VST4LNd32 };
1933 unsigned QOpcodes0[] = { ARM::VST4LNq16a, ARM::VST4LNq32a };
1934 unsigned QOpcodes1[] = { ARM::VST4LNq16b, ARM::VST4LNq32b };
1935 return SelectVLDSTLane(Op, false, 4, DOpcodes, QOpcodes0, QOpcodes1);
1941 return SelectCode(Op);
1944 bool ARMDAGToDAGISel::
1945 SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
1946 std::vector<SDValue> &OutOps) {
1947 assert(ConstraintCode == 'm' && "unexpected asm memory constraint");
1948 // Require the address to be in a register. That is safe for all ARM
1949 // variants and it is hard to do anything much smarter without knowing
1950 // how the operand is used.
1951 OutOps.push_back(Op);
1955 /// createARMISelDag - This pass converts a legalized DAG into a
1956 /// ARM-specific DAG, ready for instruction scheduling.
1958 FunctionPass *llvm::createARMISelDag(ARMBaseTargetMachine &TM,
1959 CodeGenOpt::Level OptLevel) {
1960 return new ARMDAGToDAGISel(TM, OptLevel);