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 virtual void InstructionSelect();
63 /// getI32Imm - Return a target constant of type i32 with the specified
65 inline SDValue getI32Imm(unsigned Imm) {
66 return CurDAG->getTargetConstant(Imm, MVT::i32);
69 SDNode *Select(SDNode *N);
71 bool SelectShifterOperandReg(SDNode *Op, SDValue N, SDValue &A,
72 SDValue &B, SDValue &C);
73 bool SelectAddrMode2(SDNode *Op, SDValue N, SDValue &Base,
74 SDValue &Offset, SDValue &Opc);
75 bool SelectAddrMode2Offset(SDNode *Op, SDValue N,
76 SDValue &Offset, SDValue &Opc);
77 bool SelectAddrMode3(SDNode *Op, SDValue N, SDValue &Base,
78 SDValue &Offset, SDValue &Opc);
79 bool SelectAddrMode3Offset(SDNode *Op, SDValue N,
80 SDValue &Offset, SDValue &Opc);
81 bool SelectAddrMode4(SDNode *Op, SDValue N, SDValue &Addr,
83 bool SelectAddrMode5(SDNode *Op, SDValue N, SDValue &Base,
85 bool SelectAddrMode6(SDNode *Op, SDValue N, SDValue &Addr, SDValue &Update,
86 SDValue &Opc, SDValue &Align);
88 bool SelectAddrModePC(SDNode *Op, SDValue N, SDValue &Offset,
91 bool SelectThumbAddrModeRR(SDNode *Op, SDValue N, SDValue &Base,
93 bool SelectThumbAddrModeRI5(SDNode *Op, SDValue N, unsigned Scale,
94 SDValue &Base, SDValue &OffImm,
96 bool SelectThumbAddrModeS1(SDNode *Op, SDValue N, SDValue &Base,
97 SDValue &OffImm, SDValue &Offset);
98 bool SelectThumbAddrModeS2(SDNode *Op, SDValue N, SDValue &Base,
99 SDValue &OffImm, SDValue &Offset);
100 bool SelectThumbAddrModeS4(SDNode *Op, SDValue N, SDValue &Base,
101 SDValue &OffImm, SDValue &Offset);
102 bool SelectThumbAddrModeSP(SDNode *Op, SDValue N, SDValue &Base,
105 bool SelectT2ShifterOperandReg(SDNode *Op, SDValue N,
106 SDValue &BaseReg, SDValue &Opc);
107 bool SelectT2AddrModeImm12(SDNode *Op, SDValue N, SDValue &Base,
109 bool SelectT2AddrModeImm8(SDNode *Op, SDValue N, SDValue &Base,
111 bool SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
113 bool SelectT2AddrModeImm8s4(SDNode *Op, SDValue N, SDValue &Base,
115 bool SelectT2AddrModeSoReg(SDNode *Op, SDValue N, SDValue &Base,
116 SDValue &OffReg, SDValue &ShImm);
118 // Include the pieces autogenerated from the target description.
119 #include "ARMGenDAGISel.inc"
122 /// SelectARMIndexedLoad - Indexed (pre/post inc/dec) load matching code for
124 SDNode *SelectARMIndexedLoad(SDNode *N);
125 SDNode *SelectT2IndexedLoad(SDNode *N);
127 /// SelectDYN_ALLOC - Select dynamic alloc for Thumb.
128 SDNode *SelectDYN_ALLOC(SDNode *N);
130 /// SelectVLD - Select NEON load intrinsics. NumVecs should
131 /// be 2, 3 or 4. The opcode arrays specify the instructions used for
132 /// loads of D registers and even subregs and odd subregs of Q registers.
133 /// For NumVecs == 2, QOpcodes1 is not used.
134 SDNode *SelectVLD(SDNode *N, unsigned NumVecs, unsigned *DOpcodes,
135 unsigned *QOpcodes0, unsigned *QOpcodes1);
137 /// SelectVST - Select NEON store intrinsics. NumVecs should
138 /// be 2, 3 or 4. The opcode arrays specify the instructions used for
139 /// stores of D registers and even subregs and odd subregs of Q registers.
140 /// For NumVecs == 2, QOpcodes1 is not used.
141 SDNode *SelectVST(SDNode *N, unsigned NumVecs, unsigned *DOpcodes,
142 unsigned *QOpcodes0, unsigned *QOpcodes1);
144 /// SelectVLDSTLane - Select NEON load/store lane intrinsics. NumVecs should
145 /// be 2, 3 or 4. The opcode arrays specify the instructions used for
146 /// load/store of D registers and even subregs and odd subregs of Q registers.
147 SDNode *SelectVLDSTLane(SDNode *N, bool IsLoad, unsigned NumVecs,
148 unsigned *DOpcodes, unsigned *QOpcodes0,
149 unsigned *QOpcodes1);
151 /// SelectV6T2BitfieldExtractOp - Select SBFX/UBFX instructions for ARM.
152 SDNode *SelectV6T2BitfieldExtractOp(SDNode *N, unsigned Opc);
154 /// SelectCMOVOp - Select CMOV instructions for ARM.
155 SDNode *SelectCMOVOp(SDNode *N);
156 SDNode *SelectT2CMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
157 ARMCC::CondCodes CCVal, SDValue CCR,
159 SDNode *SelectARMCMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
160 ARMCC::CondCodes CCVal, SDValue CCR,
162 SDNode *SelectT2CMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
163 ARMCC::CondCodes CCVal, SDValue CCR,
165 SDNode *SelectARMCMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
166 ARMCC::CondCodes CCVal, SDValue CCR,
169 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
170 /// inline asm expressions.
171 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
173 std::vector<SDValue> &OutOps);
175 /// PairDRegs - Insert a pair of double registers into an implicit def to
176 /// form a quad register.
177 SDNode *PairDRegs(EVT VT, SDValue V0, SDValue V1);
181 /// isInt32Immediate - This method tests to see if the node is a 32-bit constant
182 /// operand. If so Imm will receive the 32-bit value.
183 static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
184 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
185 Imm = cast<ConstantSDNode>(N)->getZExtValue();
191 // isInt32Immediate - This method tests to see if a constant operand.
192 // If so Imm will receive the 32 bit value.
193 static bool isInt32Immediate(SDValue N, unsigned &Imm) {
194 return isInt32Immediate(N.getNode(), Imm);
197 // isOpcWithIntImmediate - This method tests to see if the node is a specific
198 // opcode and that it has a immediate integer right operand.
199 // If so Imm will receive the 32 bit value.
200 static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
201 return N->getOpcode() == Opc &&
202 isInt32Immediate(N->getOperand(1).getNode(), Imm);
206 void ARMDAGToDAGISel::InstructionSelect() {
208 CurDAG->RemoveDeadNodes();
211 bool ARMDAGToDAGISel::SelectShifterOperandReg(SDNode *Op,
216 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
218 // Don't match base register only case. That is matched to a separate
219 // lower complexity pattern with explicit register operand.
220 if (ShOpcVal == ARM_AM::no_shift) return false;
222 BaseReg = N.getOperand(0);
223 unsigned ShImmVal = 0;
224 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
225 ShReg = CurDAG->getRegister(0, MVT::i32);
226 ShImmVal = RHS->getZExtValue() & 31;
228 ShReg = N.getOperand(1);
230 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
235 bool ARMDAGToDAGISel::SelectAddrMode2(SDNode *Op, SDValue N,
236 SDValue &Base, SDValue &Offset,
238 if (N.getOpcode() == ISD::MUL) {
239 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
240 // X * [3,5,9] -> X + X * [2,4,8] etc.
241 int RHSC = (int)RHS->getZExtValue();
244 ARM_AM::AddrOpc AddSub = ARM_AM::add;
246 AddSub = ARM_AM::sub;
249 if (isPowerOf2_32(RHSC)) {
250 unsigned ShAmt = Log2_32(RHSC);
251 Base = Offset = N.getOperand(0);
252 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt,
261 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB) {
263 if (N.getOpcode() == ISD::FrameIndex) {
264 int FI = cast<FrameIndexSDNode>(N)->getIndex();
265 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
266 } else if (N.getOpcode() == ARMISD::Wrapper &&
267 !(Subtarget->useMovt() &&
268 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
269 Base = N.getOperand(0);
271 Offset = CurDAG->getRegister(0, MVT::i32);
272 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::add, 0,
278 // Match simple R +/- imm12 operands.
279 if (N.getOpcode() == ISD::ADD)
280 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
281 int RHSC = (int)RHS->getZExtValue();
282 if ((RHSC >= 0 && RHSC < 0x1000) ||
283 (RHSC < 0 && RHSC > -0x1000)) { // 12 bits.
284 Base = N.getOperand(0);
285 if (Base.getOpcode() == ISD::FrameIndex) {
286 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
287 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
289 Offset = CurDAG->getRegister(0, MVT::i32);
291 ARM_AM::AddrOpc AddSub = ARM_AM::add;
293 AddSub = ARM_AM::sub;
296 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, RHSC,
303 // Otherwise this is R +/- [possibly shifted] R.
304 ARM_AM::AddrOpc AddSub = N.getOpcode() == ISD::ADD ? ARM_AM::add:ARM_AM::sub;
305 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(1));
308 Base = N.getOperand(0);
309 Offset = N.getOperand(1);
311 if (ShOpcVal != ARM_AM::no_shift) {
312 // Check to see if the RHS of the shift is a constant, if not, we can't fold
314 if (ConstantSDNode *Sh =
315 dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) {
316 ShAmt = Sh->getZExtValue();
317 Offset = N.getOperand(1).getOperand(0);
319 ShOpcVal = ARM_AM::no_shift;
323 // Try matching (R shl C) + (R).
324 if (N.getOpcode() == ISD::ADD && ShOpcVal == ARM_AM::no_shift) {
325 ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0));
326 if (ShOpcVal != ARM_AM::no_shift) {
327 // Check to see if the RHS of the shift is a constant, if not, we can't
329 if (ConstantSDNode *Sh =
330 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
331 ShAmt = Sh->getZExtValue();
332 Offset = N.getOperand(0).getOperand(0);
333 Base = N.getOperand(1);
335 ShOpcVal = ARM_AM::no_shift;
340 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
345 bool ARMDAGToDAGISel::SelectAddrMode2Offset(SDNode *Op, SDValue N,
346 SDValue &Offset, SDValue &Opc) {
347 unsigned Opcode = Op->getOpcode();
348 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
349 ? cast<LoadSDNode>(Op)->getAddressingMode()
350 : cast<StoreSDNode>(Op)->getAddressingMode();
351 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
352 ? ARM_AM::add : ARM_AM::sub;
353 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
354 int Val = (int)C->getZExtValue();
355 if (Val >= 0 && Val < 0x1000) { // 12 bits.
356 Offset = CurDAG->getRegister(0, MVT::i32);
357 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, Val,
365 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
367 if (ShOpcVal != ARM_AM::no_shift) {
368 // Check to see if the RHS of the shift is a constant, if not, we can't fold
370 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
371 ShAmt = Sh->getZExtValue();
372 Offset = N.getOperand(0);
374 ShOpcVal = ARM_AM::no_shift;
378 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
384 bool ARMDAGToDAGISel::SelectAddrMode3(SDNode *Op, SDValue N,
385 SDValue &Base, SDValue &Offset,
387 if (N.getOpcode() == ISD::SUB) {
388 // X - C is canonicalize to X + -C, no need to handle it here.
389 Base = N.getOperand(0);
390 Offset = N.getOperand(1);
391 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::sub, 0),MVT::i32);
395 if (N.getOpcode() != ISD::ADD) {
397 if (N.getOpcode() == ISD::FrameIndex) {
398 int FI = cast<FrameIndexSDNode>(N)->getIndex();
399 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
401 Offset = CurDAG->getRegister(0, MVT::i32);
402 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0),MVT::i32);
406 // If the RHS is +/- imm8, fold into addr mode.
407 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
408 int RHSC = (int)RHS->getZExtValue();
409 if ((RHSC >= 0 && RHSC < 256) ||
410 (RHSC < 0 && RHSC > -256)) { // note -256 itself isn't allowed.
411 Base = N.getOperand(0);
412 if (Base.getOpcode() == ISD::FrameIndex) {
413 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
414 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
416 Offset = CurDAG->getRegister(0, MVT::i32);
418 ARM_AM::AddrOpc AddSub = ARM_AM::add;
420 AddSub = ARM_AM::sub;
423 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, RHSC),MVT::i32);
428 Base = N.getOperand(0);
429 Offset = N.getOperand(1);
430 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0), MVT::i32);
434 bool ARMDAGToDAGISel::SelectAddrMode3Offset(SDNode *Op, SDValue N,
435 SDValue &Offset, SDValue &Opc) {
436 unsigned Opcode = Op->getOpcode();
437 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
438 ? cast<LoadSDNode>(Op)->getAddressingMode()
439 : cast<StoreSDNode>(Op)->getAddressingMode();
440 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
441 ? ARM_AM::add : ARM_AM::sub;
442 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
443 int Val = (int)C->getZExtValue();
444 if (Val >= 0 && Val < 256) {
445 Offset = CurDAG->getRegister(0, MVT::i32);
446 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, Val), MVT::i32);
452 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, 0), MVT::i32);
456 bool ARMDAGToDAGISel::SelectAddrMode4(SDNode *Op, SDValue N,
457 SDValue &Addr, SDValue &Mode) {
459 Mode = CurDAG->getTargetConstant(0, MVT::i32);
463 bool ARMDAGToDAGISel::SelectAddrMode5(SDNode *Op, SDValue N,
464 SDValue &Base, SDValue &Offset) {
465 if (N.getOpcode() != ISD::ADD) {
467 if (N.getOpcode() == ISD::FrameIndex) {
468 int FI = cast<FrameIndexSDNode>(N)->getIndex();
469 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
470 } else if (N.getOpcode() == ARMISD::Wrapper &&
471 !(Subtarget->useMovt() &&
472 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
473 Base = N.getOperand(0);
475 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
480 // If the RHS is +/- imm8, fold into addr mode.
481 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
482 int RHSC = (int)RHS->getZExtValue();
483 if ((RHSC & 3) == 0) { // The constant is implicitly multiplied by 4.
485 if ((RHSC >= 0 && RHSC < 256) ||
486 (RHSC < 0 && RHSC > -256)) { // note -256 itself isn't allowed.
487 Base = N.getOperand(0);
488 if (Base.getOpcode() == ISD::FrameIndex) {
489 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
490 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
493 ARM_AM::AddrOpc AddSub = ARM_AM::add;
495 AddSub = ARM_AM::sub;
498 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(AddSub, RHSC),
506 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
511 bool ARMDAGToDAGISel::SelectAddrMode6(SDNode *Op, SDValue N,
512 SDValue &Addr, SDValue &Update,
513 SDValue &Opc, SDValue &Align) {
515 // Default to no writeback.
516 Update = CurDAG->getRegister(0, MVT::i32);
517 Opc = CurDAG->getTargetConstant(ARM_AM::getAM6Opc(false), MVT::i32);
518 // Default to no alignment.
519 Align = CurDAG->getTargetConstant(0, MVT::i32);
523 bool ARMDAGToDAGISel::SelectAddrModePC(SDNode *Op, SDValue N,
524 SDValue &Offset, SDValue &Label) {
525 if (N.getOpcode() == ARMISD::PIC_ADD && N.hasOneUse()) {
526 Offset = N.getOperand(0);
527 SDValue N1 = N.getOperand(1);
528 Label = CurDAG->getTargetConstant(cast<ConstantSDNode>(N1)->getZExtValue(),
535 bool ARMDAGToDAGISel::SelectThumbAddrModeRR(SDNode *Op, SDValue N,
536 SDValue &Base, SDValue &Offset){
537 // FIXME dl should come from the parent load or store, not the address
538 DebugLoc dl = Op->getDebugLoc();
539 if (N.getOpcode() != ISD::ADD) {
540 ConstantSDNode *NC = dyn_cast<ConstantSDNode>(N);
541 if (!NC || NC->getZExtValue() != 0)
548 Base = N.getOperand(0);
549 Offset = N.getOperand(1);
554 ARMDAGToDAGISel::SelectThumbAddrModeRI5(SDNode *Op, SDValue N,
555 unsigned Scale, SDValue &Base,
556 SDValue &OffImm, SDValue &Offset) {
558 SDValue TmpBase, TmpOffImm;
559 if (SelectThumbAddrModeSP(Op, N, TmpBase, TmpOffImm))
560 return false; // We want to select tLDRspi / tSTRspi instead.
561 if (N.getOpcode() == ARMISD::Wrapper &&
562 N.getOperand(0).getOpcode() == ISD::TargetConstantPool)
563 return false; // We want to select tLDRpci instead.
566 if (N.getOpcode() != ISD::ADD) {
567 if (N.getOpcode() == ARMISD::Wrapper &&
568 !(Subtarget->useMovt() &&
569 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
570 Base = N.getOperand(0);
574 Offset = CurDAG->getRegister(0, MVT::i32);
575 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
579 // Thumb does not have [sp, r] address mode.
580 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
581 RegisterSDNode *RHSR = dyn_cast<RegisterSDNode>(N.getOperand(1));
582 if ((LHSR && LHSR->getReg() == ARM::SP) ||
583 (RHSR && RHSR->getReg() == ARM::SP)) {
585 Offset = CurDAG->getRegister(0, MVT::i32);
586 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
590 // If the RHS is + imm5 * scale, fold into addr mode.
591 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
592 int RHSC = (int)RHS->getZExtValue();
593 if ((RHSC & (Scale-1)) == 0) { // The constant is implicitly multiplied.
595 if (RHSC >= 0 && RHSC < 32) {
596 Base = N.getOperand(0);
597 Offset = CurDAG->getRegister(0, MVT::i32);
598 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
604 Base = N.getOperand(0);
605 Offset = N.getOperand(1);
606 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
610 bool ARMDAGToDAGISel::SelectThumbAddrModeS1(SDNode *Op, SDValue N,
611 SDValue &Base, SDValue &OffImm,
613 return SelectThumbAddrModeRI5(Op, N, 1, Base, OffImm, Offset);
616 bool ARMDAGToDAGISel::SelectThumbAddrModeS2(SDNode *Op, SDValue N,
617 SDValue &Base, SDValue &OffImm,
619 return SelectThumbAddrModeRI5(Op, N, 2, Base, OffImm, Offset);
622 bool ARMDAGToDAGISel::SelectThumbAddrModeS4(SDNode *Op, SDValue N,
623 SDValue &Base, SDValue &OffImm,
625 return SelectThumbAddrModeRI5(Op, N, 4, Base, OffImm, Offset);
628 bool ARMDAGToDAGISel::SelectThumbAddrModeSP(SDNode *Op, SDValue N,
629 SDValue &Base, SDValue &OffImm) {
630 if (N.getOpcode() == ISD::FrameIndex) {
631 int FI = cast<FrameIndexSDNode>(N)->getIndex();
632 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
633 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
637 if (N.getOpcode() != ISD::ADD)
640 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
641 if (N.getOperand(0).getOpcode() == ISD::FrameIndex ||
642 (LHSR && LHSR->getReg() == ARM::SP)) {
643 // If the RHS is + imm8 * scale, fold into addr mode.
644 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
645 int RHSC = (int)RHS->getZExtValue();
646 if ((RHSC & 3) == 0) { // The constant is implicitly multiplied.
648 if (RHSC >= 0 && RHSC < 256) {
649 Base = N.getOperand(0);
650 if (Base.getOpcode() == ISD::FrameIndex) {
651 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
652 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
654 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
664 bool ARMDAGToDAGISel::SelectT2ShifterOperandReg(SDNode *Op, SDValue N,
667 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
669 // Don't match base register only case. That is matched to a separate
670 // lower complexity pattern with explicit register operand.
671 if (ShOpcVal == ARM_AM::no_shift) return false;
673 BaseReg = N.getOperand(0);
674 unsigned ShImmVal = 0;
675 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
676 ShImmVal = RHS->getZExtValue() & 31;
677 Opc = getI32Imm(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal));
684 bool ARMDAGToDAGISel::SelectT2AddrModeImm12(SDNode *Op, SDValue N,
685 SDValue &Base, SDValue &OffImm) {
686 // Match simple R + imm12 operands.
689 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB) {
690 if (N.getOpcode() == ISD::FrameIndex) {
691 // Match frame index...
692 int FI = cast<FrameIndexSDNode>(N)->getIndex();
693 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
694 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
696 } else if (N.getOpcode() == ARMISD::Wrapper &&
697 !(Subtarget->useMovt() &&
698 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
699 Base = N.getOperand(0);
700 if (Base.getOpcode() == ISD::TargetConstantPool)
701 return false; // We want to select t2LDRpci instead.
704 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
708 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
709 if (SelectT2AddrModeImm8(Op, N, Base, OffImm))
710 // Let t2LDRi8 handle (R - imm8).
713 int RHSC = (int)RHS->getZExtValue();
714 if (N.getOpcode() == ISD::SUB)
717 if (RHSC >= 0 && RHSC < 0x1000) { // 12 bits (unsigned)
718 Base = N.getOperand(0);
719 if (Base.getOpcode() == ISD::FrameIndex) {
720 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
721 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
723 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
730 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
734 bool ARMDAGToDAGISel::SelectT2AddrModeImm8(SDNode *Op, SDValue N,
735 SDValue &Base, SDValue &OffImm) {
736 // Match simple R - imm8 operands.
737 if (N.getOpcode() == ISD::ADD || N.getOpcode() == ISD::SUB) {
738 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
739 int RHSC = (int)RHS->getSExtValue();
740 if (N.getOpcode() == ISD::SUB)
743 if ((RHSC >= -255) && (RHSC < 0)) { // 8 bits (always negative)
744 Base = N.getOperand(0);
745 if (Base.getOpcode() == ISD::FrameIndex) {
746 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
747 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
749 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
758 bool ARMDAGToDAGISel::SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
760 unsigned Opcode = Op->getOpcode();
761 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
762 ? cast<LoadSDNode>(Op)->getAddressingMode()
763 : cast<StoreSDNode>(Op)->getAddressingMode();
764 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N)) {
765 int RHSC = (int)RHS->getZExtValue();
766 if (RHSC >= 0 && RHSC < 0x100) { // 8 bits.
767 OffImm = ((AM == ISD::PRE_INC) || (AM == ISD::POST_INC))
768 ? CurDAG->getTargetConstant(RHSC, MVT::i32)
769 : CurDAG->getTargetConstant(-RHSC, MVT::i32);
777 bool ARMDAGToDAGISel::SelectT2AddrModeImm8s4(SDNode *Op, SDValue N,
778 SDValue &Base, SDValue &OffImm) {
779 if (N.getOpcode() == ISD::ADD) {
780 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
781 int RHSC = (int)RHS->getZExtValue();
782 if (((RHSC & 0x3) == 0) &&
783 ((RHSC >= 0 && RHSC < 0x400) || (RHSC < 0 && RHSC > -0x400))) { // 8 bits.
784 Base = N.getOperand(0);
785 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
789 } else if (N.getOpcode() == ISD::SUB) {
790 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
791 int RHSC = (int)RHS->getZExtValue();
792 if (((RHSC & 0x3) == 0) && (RHSC >= 0 && RHSC < 0x400)) { // 8 bits.
793 Base = N.getOperand(0);
794 OffImm = CurDAG->getTargetConstant(-RHSC, MVT::i32);
803 bool ARMDAGToDAGISel::SelectT2AddrModeSoReg(SDNode *Op, SDValue N,
805 SDValue &OffReg, SDValue &ShImm) {
806 // (R - imm8) should be handled by t2LDRi8. The rest are handled by t2LDRi12.
807 if (N.getOpcode() != ISD::ADD)
810 // Leave (R + imm12) for t2LDRi12, (R - imm8) for t2LDRi8.
811 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
812 int RHSC = (int)RHS->getZExtValue();
813 if (RHSC >= 0 && RHSC < 0x1000) // 12 bits (unsigned)
815 else if (RHSC < 0 && RHSC >= -255) // 8 bits
819 // Look for (R + R) or (R + (R << [1,2,3])).
821 Base = N.getOperand(0);
822 OffReg = N.getOperand(1);
824 // Swap if it is ((R << c) + R).
825 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(OffReg);
826 if (ShOpcVal != ARM_AM::lsl) {
827 ShOpcVal = ARM_AM::getShiftOpcForNode(Base);
828 if (ShOpcVal == ARM_AM::lsl)
829 std::swap(Base, OffReg);
832 if (ShOpcVal == ARM_AM::lsl) {
833 // Check to see if the RHS of the shift is a constant, if not, we can't fold
835 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(OffReg.getOperand(1))) {
836 ShAmt = Sh->getZExtValue();
839 ShOpcVal = ARM_AM::no_shift;
841 OffReg = OffReg.getOperand(0);
843 ShOpcVal = ARM_AM::no_shift;
847 ShImm = CurDAG->getTargetConstant(ShAmt, MVT::i32);
852 //===--------------------------------------------------------------------===//
854 /// getAL - Returns a ARMCC::AL immediate node.
855 static inline SDValue getAL(SelectionDAG *CurDAG) {
856 return CurDAG->getTargetConstant((uint64_t)ARMCC::AL, MVT::i32);
859 SDNode *ARMDAGToDAGISel::SelectARMIndexedLoad(SDNode *N) {
860 LoadSDNode *LD = cast<LoadSDNode>(N);
861 ISD::MemIndexedMode AM = LD->getAddressingMode();
862 if (AM == ISD::UNINDEXED)
865 EVT LoadedVT = LD->getMemoryVT();
866 SDValue Offset, AMOpc;
867 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
870 if (LoadedVT == MVT::i32 &&
871 SelectAddrMode2Offset(N, LD->getOffset(), Offset, AMOpc)) {
872 Opcode = isPre ? ARM::LDR_PRE : ARM::LDR_POST;
874 } else if (LoadedVT == MVT::i16 &&
875 SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
877 Opcode = (LD->getExtensionType() == ISD::SEXTLOAD)
878 ? (isPre ? ARM::LDRSH_PRE : ARM::LDRSH_POST)
879 : (isPre ? ARM::LDRH_PRE : ARM::LDRH_POST);
880 } else if (LoadedVT == MVT::i8 || LoadedVT == MVT::i1) {
881 if (LD->getExtensionType() == ISD::SEXTLOAD) {
882 if (SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
884 Opcode = isPre ? ARM::LDRSB_PRE : ARM::LDRSB_POST;
887 if (SelectAddrMode2Offset(N, LD->getOffset(), Offset, AMOpc)) {
889 Opcode = isPre ? ARM::LDRB_PRE : ARM::LDRB_POST;
895 SDValue Chain = LD->getChain();
896 SDValue Base = LD->getBasePtr();
897 SDValue Ops[]= { Base, Offset, AMOpc, getAL(CurDAG),
898 CurDAG->getRegister(0, MVT::i32), Chain };
899 return CurDAG->getMachineNode(Opcode, N->getDebugLoc(), MVT::i32, MVT::i32,
906 SDNode *ARMDAGToDAGISel::SelectT2IndexedLoad(SDNode *N) {
907 LoadSDNode *LD = cast<LoadSDNode>(N);
908 ISD::MemIndexedMode AM = LD->getAddressingMode();
909 if (AM == ISD::UNINDEXED)
912 EVT LoadedVT = LD->getMemoryVT();
913 bool isSExtLd = LD->getExtensionType() == ISD::SEXTLOAD;
915 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
918 if (SelectT2AddrModeImm8Offset(N, LD->getOffset(), Offset)) {
919 switch (LoadedVT.getSimpleVT().SimpleTy) {
921 Opcode = isPre ? ARM::t2LDR_PRE : ARM::t2LDR_POST;
925 Opcode = isPre ? ARM::t2LDRSH_PRE : ARM::t2LDRSH_POST;
927 Opcode = isPre ? ARM::t2LDRH_PRE : ARM::t2LDRH_POST;
932 Opcode = isPre ? ARM::t2LDRSB_PRE : ARM::t2LDRSB_POST;
934 Opcode = isPre ? ARM::t2LDRB_PRE : ARM::t2LDRB_POST;
943 SDValue Chain = LD->getChain();
944 SDValue Base = LD->getBasePtr();
945 SDValue Ops[]= { Base, Offset, getAL(CurDAG),
946 CurDAG->getRegister(0, MVT::i32), Chain };
947 return CurDAG->getMachineNode(Opcode, N->getDebugLoc(), MVT::i32, MVT::i32,
954 SDNode *ARMDAGToDAGISel::SelectDYN_ALLOC(SDNode *N) {
955 DebugLoc dl = N->getDebugLoc();
956 EVT VT = N->getValueType(0);
957 SDValue Chain = N->getOperand(0);
958 SDValue Size = N->getOperand(1);
959 SDValue Align = N->getOperand(2);
960 SDValue SP = CurDAG->getRegister(ARM::SP, MVT::i32);
961 int32_t AlignVal = cast<ConstantSDNode>(Align)->getSExtValue();
963 // We need to align the stack. Use Thumb1 tAND which is the only thumb
964 // instruction that can read and write SP. This matches to a pseudo
965 // instruction that has a chain to ensure the result is written back to
966 // the stack pointer.
967 SP = SDValue(CurDAG->getMachineNode(ARM::tANDsp, dl, VT, SP, Align), 0);
969 bool isC = isa<ConstantSDNode>(Size);
970 uint32_t C = isC ? cast<ConstantSDNode>(Size)->getZExtValue() : ~0UL;
971 // Handle the most common case for both Thumb1 and Thumb2:
972 // tSUBspi - immediate is between 0 ... 508 inclusive.
973 if (C <= 508 && ((C & 3) == 0))
974 // FIXME: tSUBspi encode scale 4 implicitly.
975 return CurDAG->SelectNodeTo(N, ARM::tSUBspi_, VT, MVT::Other, SP,
976 CurDAG->getTargetConstant(C/4, MVT::i32),
979 if (Subtarget->isThumb1Only()) {
980 // Use tADDspr since Thumb1 does not have a sub r, sp, r. ARMISelLowering
981 // should have negated the size operand already. FIXME: We can't insert
982 // new target independent node at this stage so we are forced to negate
983 // it earlier. Is there a better solution?
984 return CurDAG->SelectNodeTo(N, ARM::tADDspr_, VT, MVT::Other, SP, Size,
986 } else if (Subtarget->isThumb2()) {
987 if (isC && Predicate_t2_so_imm(Size.getNode())) {
989 SDValue Ops[] = { SP, CurDAG->getTargetConstant(C, MVT::i32), Chain };
990 return CurDAG->SelectNodeTo(N, ARM::t2SUBrSPi_, VT, MVT::Other, Ops, 3);
991 } else if (isC && Predicate_imm0_4095(Size.getNode())) {
993 SDValue Ops[] = { SP, CurDAG->getTargetConstant(C, MVT::i32), Chain };
994 return CurDAG->SelectNodeTo(N, ARM::t2SUBrSPi12_, VT, MVT::Other, Ops, 3);
997 SDValue Ops[] = { SP, Size,
998 getI32Imm(ARM_AM::getSORegOpc(ARM_AM::lsl,0)), Chain };
999 return CurDAG->SelectNodeTo(N, ARM::t2SUBrSPs_, VT, MVT::Other, Ops, 4);
1003 // FIXME: Add ADD / SUB sp instructions for ARM.
1007 /// PairDRegs - Insert a pair of double registers into an implicit def to
1008 /// form a quad register.
1009 SDNode *ARMDAGToDAGISel::PairDRegs(EVT VT, SDValue V0, SDValue V1) {
1010 DebugLoc dl = V0.getNode()->getDebugLoc();
1012 SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0);
1013 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::DSUBREG_0, MVT::i32);
1014 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::DSUBREG_1, MVT::i32);
1015 SDNode *Pair = CurDAG->getMachineNode(TargetOpcode::INSERT_SUBREG, dl,
1016 VT, Undef, V0, SubReg0);
1017 return CurDAG->getMachineNode(TargetOpcode::INSERT_SUBREG, dl,
1018 VT, SDValue(Pair, 0), V1, SubReg1);
1021 /// GetNEONSubregVT - Given a type for a 128-bit NEON vector, return the type
1022 /// for a 64-bit subregister of the vector.
1023 static EVT GetNEONSubregVT(EVT VT) {
1024 switch (VT.getSimpleVT().SimpleTy) {
1025 default: llvm_unreachable("unhandled NEON type");
1026 case MVT::v16i8: return MVT::v8i8;
1027 case MVT::v8i16: return MVT::v4i16;
1028 case MVT::v4f32: return MVT::v2f32;
1029 case MVT::v4i32: return MVT::v2i32;
1030 case MVT::v2i64: return MVT::v1i64;
1034 SDNode *ARMDAGToDAGISel::SelectVLD(SDNode *N, unsigned NumVecs,
1035 unsigned *DOpcodes, unsigned *QOpcodes0,
1036 unsigned *QOpcodes1) {
1037 assert(NumVecs >=2 && NumVecs <= 4 && "VLD NumVecs out-of-range");
1038 DebugLoc dl = N->getDebugLoc();
1040 SDValue MemAddr, MemUpdate, MemOpc, Align;
1041 if (!SelectAddrMode6(N, N->getOperand(2), MemAddr, MemUpdate, MemOpc, Align))
1044 SDValue Chain = N->getOperand(0);
1045 EVT VT = N->getValueType(0);
1046 bool is64BitVector = VT.is64BitVector();
1048 unsigned OpcodeIndex;
1049 switch (VT.getSimpleVT().SimpleTy) {
1050 default: llvm_unreachable("unhandled vld type");
1051 // Double-register operations:
1052 case MVT::v8i8: OpcodeIndex = 0; break;
1053 case MVT::v4i16: OpcodeIndex = 1; break;
1055 case MVT::v2i32: OpcodeIndex = 2; break;
1056 case MVT::v1i64: OpcodeIndex = 3; break;
1057 // Quad-register operations:
1058 case MVT::v16i8: OpcodeIndex = 0; break;
1059 case MVT::v8i16: OpcodeIndex = 1; break;
1061 case MVT::v4i32: OpcodeIndex = 2; break;
1064 SDValue Pred = CurDAG->getTargetConstant(14, MVT::i32);
1065 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1066 if (is64BitVector) {
1067 unsigned Opc = DOpcodes[OpcodeIndex];
1068 const SDValue Ops[] = { MemAddr, MemUpdate, MemOpc, Align,
1069 Pred, PredReg, Chain };
1070 std::vector<EVT> ResTys(NumVecs, VT);
1071 ResTys.push_back(MVT::Other);
1072 return CurDAG->getMachineNode(Opc, dl, ResTys, Ops, 7);
1075 EVT RegVT = GetNEONSubregVT(VT);
1077 // Quad registers are directly supported for VLD2,
1078 // loading 2 pairs of D regs.
1079 unsigned Opc = QOpcodes0[OpcodeIndex];
1080 const SDValue Ops[] = { MemAddr, MemUpdate, MemOpc, Align,
1081 Pred, PredReg, Chain };
1082 std::vector<EVT> ResTys(4, VT);
1083 ResTys.push_back(MVT::Other);
1084 SDNode *VLd = CurDAG->getMachineNode(Opc, dl, ResTys, Ops, 7);
1085 Chain = SDValue(VLd, 4);
1087 // Combine the even and odd subregs to produce the result.
1088 for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
1089 SDNode *Q = PairDRegs(VT, SDValue(VLd, 2*Vec), SDValue(VLd, 2*Vec+1));
1090 ReplaceUses(SDValue(N, Vec), SDValue(Q, 0));
1093 // Otherwise, quad registers are loaded with two separate instructions,
1094 // where one loads the even registers and the other loads the odd registers.
1096 // Enable writeback to the address register.
1097 MemOpc = CurDAG->getTargetConstant(ARM_AM::getAM6Opc(true), MVT::i32);
1099 std::vector<EVT> ResTys(NumVecs, RegVT);
1100 ResTys.push_back(MemAddr.getValueType());
1101 ResTys.push_back(MVT::Other);
1103 // Load the even subregs.
1104 unsigned Opc = QOpcodes0[OpcodeIndex];
1105 const SDValue OpsA[] = { MemAddr, MemUpdate, MemOpc, Align,
1106 Pred, PredReg, Chain };
1107 SDNode *VLdA = CurDAG->getMachineNode(Opc, dl, ResTys, OpsA, 7);
1108 Chain = SDValue(VLdA, NumVecs+1);
1110 // Load the odd subregs.
1111 Opc = QOpcodes1[OpcodeIndex];
1112 const SDValue OpsB[] = { SDValue(VLdA, NumVecs), MemUpdate, MemOpc,
1113 Align, Pred, PredReg, Chain };
1114 SDNode *VLdB = CurDAG->getMachineNode(Opc, dl, ResTys, OpsB, 7);
1115 Chain = SDValue(VLdB, NumVecs+1);
1117 // Combine the even and odd subregs to produce the result.
1118 for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
1119 SDNode *Q = PairDRegs(VT, SDValue(VLdA, Vec), SDValue(VLdB, Vec));
1120 ReplaceUses(SDValue(N, Vec), SDValue(Q, 0));
1123 ReplaceUses(SDValue(N, NumVecs), Chain);
1127 SDNode *ARMDAGToDAGISel::SelectVST(SDNode *N, unsigned NumVecs,
1128 unsigned *DOpcodes, unsigned *QOpcodes0,
1129 unsigned *QOpcodes1) {
1130 assert(NumVecs >=2 && NumVecs <= 4 && "VST NumVecs out-of-range");
1131 DebugLoc dl = N->getDebugLoc();
1133 SDValue MemAddr, MemUpdate, MemOpc, Align;
1134 if (!SelectAddrMode6(N, N->getOperand(2), MemAddr, MemUpdate, MemOpc, Align))
1137 SDValue Chain = N->getOperand(0);
1138 EVT VT = N->getOperand(3).getValueType();
1139 bool is64BitVector = VT.is64BitVector();
1141 unsigned OpcodeIndex;
1142 switch (VT.getSimpleVT().SimpleTy) {
1143 default: llvm_unreachable("unhandled vst type");
1144 // Double-register operations:
1145 case MVT::v8i8: OpcodeIndex = 0; break;
1146 case MVT::v4i16: OpcodeIndex = 1; break;
1148 case MVT::v2i32: OpcodeIndex = 2; break;
1149 case MVT::v1i64: OpcodeIndex = 3; break;
1150 // Quad-register operations:
1151 case MVT::v16i8: OpcodeIndex = 0; break;
1152 case MVT::v8i16: OpcodeIndex = 1; break;
1154 case MVT::v4i32: OpcodeIndex = 2; break;
1157 SDValue Pred = CurDAG->getTargetConstant(14, MVT::i32);
1158 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1160 SmallVector<SDValue, 8> Ops;
1161 Ops.push_back(MemAddr);
1162 Ops.push_back(MemUpdate);
1163 Ops.push_back(MemOpc);
1164 Ops.push_back(Align);
1166 if (is64BitVector) {
1167 unsigned Opc = DOpcodes[OpcodeIndex];
1168 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1169 Ops.push_back(N->getOperand(Vec+3));
1170 Ops.push_back(Pred);
1171 Ops.push_back(PredReg);
1172 Ops.push_back(Chain);
1173 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), NumVecs+7);
1176 EVT RegVT = GetNEONSubregVT(VT);
1178 // Quad registers are directly supported for VST2,
1179 // storing 2 pairs of D regs.
1180 unsigned Opc = QOpcodes0[OpcodeIndex];
1181 for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
1182 Ops.push_back(CurDAG->getTargetExtractSubreg(ARM::DSUBREG_0, dl, RegVT,
1183 N->getOperand(Vec+3)));
1184 Ops.push_back(CurDAG->getTargetExtractSubreg(ARM::DSUBREG_1, dl, RegVT,
1185 N->getOperand(Vec+3)));
1187 Ops.push_back(Pred);
1188 Ops.push_back(PredReg);
1189 Ops.push_back(Chain);
1190 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 11);
1193 // Otherwise, quad registers are stored with two separate instructions,
1194 // where one stores the even registers and the other stores the odd registers.
1196 // Enable writeback to the address register.
1197 MemOpc = CurDAG->getTargetConstant(ARM_AM::getAM6Opc(true), MVT::i32);
1199 // Store the even subregs.
1200 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1201 Ops.push_back(CurDAG->getTargetExtractSubreg(ARM::DSUBREG_0, dl, RegVT,
1202 N->getOperand(Vec+3)));
1203 Ops.push_back(Pred);
1204 Ops.push_back(PredReg);
1205 Ops.push_back(Chain);
1206 unsigned Opc = QOpcodes0[OpcodeIndex];
1207 SDNode *VStA = CurDAG->getMachineNode(Opc, dl, MemAddr.getValueType(),
1208 MVT::Other, Ops.data(), NumVecs+7);
1209 Chain = SDValue(VStA, 1);
1211 // Store the odd subregs.
1212 Ops[0] = SDValue(VStA, 0); // MemAddr
1213 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1214 Ops[Vec+4] = CurDAG->getTargetExtractSubreg(ARM::DSUBREG_1, dl, RegVT,
1215 N->getOperand(Vec+3));
1216 Ops[NumVecs+4] = Pred;
1217 Ops[NumVecs+5] = PredReg;
1218 Ops[NumVecs+6] = Chain;
1219 Opc = QOpcodes1[OpcodeIndex];
1220 SDNode *VStB = CurDAG->getMachineNode(Opc, dl, MemAddr.getValueType(),
1221 MVT::Other, Ops.data(), NumVecs+7);
1222 Chain = SDValue(VStB, 1);
1223 ReplaceUses(SDValue(N, 0), Chain);
1227 SDNode *ARMDAGToDAGISel::SelectVLDSTLane(SDNode *N, bool IsLoad,
1228 unsigned NumVecs, unsigned *DOpcodes,
1229 unsigned *QOpcodes0,
1230 unsigned *QOpcodes1) {
1231 assert(NumVecs >=2 && NumVecs <= 4 && "VLDSTLane NumVecs out-of-range");
1232 DebugLoc dl = N->getDebugLoc();
1234 SDValue MemAddr, MemUpdate, MemOpc, Align;
1235 if (!SelectAddrMode6(N, N->getOperand(2), MemAddr, MemUpdate, MemOpc, Align))
1238 SDValue Chain = N->getOperand(0);
1240 cast<ConstantSDNode>(N->getOperand(NumVecs+3))->getZExtValue();
1241 EVT VT = IsLoad ? N->getValueType(0) : N->getOperand(3).getValueType();
1242 bool is64BitVector = VT.is64BitVector();
1244 // Quad registers are handled by load/store of subregs. Find the subreg info.
1245 unsigned NumElts = 0;
1248 if (!is64BitVector) {
1249 RegVT = GetNEONSubregVT(VT);
1250 NumElts = RegVT.getVectorNumElements();
1251 SubregIdx = (Lane < NumElts) ? ARM::DSUBREG_0 : ARM::DSUBREG_1;
1254 unsigned OpcodeIndex;
1255 switch (VT.getSimpleVT().SimpleTy) {
1256 default: llvm_unreachable("unhandled vld/vst lane type");
1257 // Double-register operations:
1258 case MVT::v8i8: OpcodeIndex = 0; break;
1259 case MVT::v4i16: OpcodeIndex = 1; break;
1261 case MVT::v2i32: OpcodeIndex = 2; break;
1262 // Quad-register operations:
1263 case MVT::v8i16: OpcodeIndex = 0; break;
1265 case MVT::v4i32: OpcodeIndex = 1; break;
1268 SDValue Pred = CurDAG->getTargetConstant(14, MVT::i32);
1269 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1271 SmallVector<SDValue, 9> Ops;
1272 Ops.push_back(MemAddr);
1273 Ops.push_back(MemUpdate);
1274 Ops.push_back(MemOpc);
1275 Ops.push_back(Align);
1278 if (is64BitVector) {
1279 Opc = DOpcodes[OpcodeIndex];
1280 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1281 Ops.push_back(N->getOperand(Vec+3));
1283 // Check if this is loading the even or odd subreg of a Q register.
1284 if (Lane < NumElts) {
1285 Opc = QOpcodes0[OpcodeIndex];
1288 Opc = QOpcodes1[OpcodeIndex];
1290 // Extract the subregs of the input vector.
1291 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1292 Ops.push_back(CurDAG->getTargetExtractSubreg(SubregIdx, dl, RegVT,
1293 N->getOperand(Vec+3)));
1295 Ops.push_back(getI32Imm(Lane));
1296 Ops.push_back(Pred);
1297 Ops.push_back(PredReg);
1298 Ops.push_back(Chain);
1301 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), NumVecs+8);
1303 std::vector<EVT> ResTys(NumVecs, RegVT);
1304 ResTys.push_back(MVT::Other);
1306 CurDAG->getMachineNode(Opc, dl, ResTys, Ops.data(), NumVecs+8);
1307 // For a 64-bit vector load to D registers, nothing more needs to be done.
1311 // For 128-bit vectors, take the 64-bit results of the load and insert them
1312 // as subregs into the result.
1313 for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
1314 SDValue QuadVec = CurDAG->getTargetInsertSubreg(SubregIdx, dl, VT,
1315 N->getOperand(Vec+3),
1316 SDValue(VLdLn, Vec));
1317 ReplaceUses(SDValue(N, Vec), QuadVec);
1320 Chain = SDValue(VLdLn, NumVecs);
1321 ReplaceUses(SDValue(N, NumVecs), Chain);
1325 SDNode *ARMDAGToDAGISel::SelectV6T2BitfieldExtractOp(SDNode *N,
1327 if (!Subtarget->hasV6T2Ops())
1330 unsigned Shl_imm = 0;
1331 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SHL, Shl_imm)) {
1332 assert(Shl_imm > 0 && Shl_imm < 32 && "bad amount in shift node!");
1333 unsigned Srl_imm = 0;
1334 if (isInt32Immediate(N->getOperand(1), Srl_imm)) {
1335 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
1336 unsigned Width = 32 - Srl_imm;
1337 int LSB = Srl_imm - Shl_imm;
1340 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1341 SDValue Ops[] = { N->getOperand(0).getOperand(0),
1342 CurDAG->getTargetConstant(LSB, MVT::i32),
1343 CurDAG->getTargetConstant(Width, MVT::i32),
1344 getAL(CurDAG), Reg0 };
1345 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
1351 SDNode *ARMDAGToDAGISel::
1352 SelectT2CMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
1353 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1356 if (SelectT2ShifterOperandReg(N, TrueVal, CPTmp0, CPTmp1)) {
1357 unsigned SOVal = cast<ConstantSDNode>(CPTmp1)->getZExtValue();
1358 unsigned SOShOp = ARM_AM::getSORegShOp(SOVal);
1361 case ARM_AM::lsl: Opc = ARM::t2MOVCClsl; break;
1362 case ARM_AM::lsr: Opc = ARM::t2MOVCClsr; break;
1363 case ARM_AM::asr: Opc = ARM::t2MOVCCasr; break;
1364 case ARM_AM::ror: Opc = ARM::t2MOVCCror; break;
1366 llvm_unreachable("Unknown so_reg opcode!");
1370 CurDAG->getTargetConstant(ARM_AM::getSORegOffset(SOVal), MVT::i32);
1371 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1372 SDValue Ops[] = { FalseVal, CPTmp0, SOShImm, CC, CCR, InFlag };
1373 return CurDAG->SelectNodeTo(N, Opc, MVT::i32,Ops, 6);
1378 SDNode *ARMDAGToDAGISel::
1379 SelectARMCMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
1380 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1384 if (SelectShifterOperandReg(N, TrueVal, CPTmp0, CPTmp1, CPTmp2)) {
1385 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1386 SDValue Ops[] = { FalseVal, CPTmp0, CPTmp1, CPTmp2, CC, CCR, InFlag };
1387 return CurDAG->SelectNodeTo(N, ARM::MOVCCs, MVT::i32, Ops, 7);
1392 SDNode *ARMDAGToDAGISel::
1393 SelectT2CMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
1394 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1395 ConstantSDNode *T = dyn_cast<ConstantSDNode>(TrueVal);
1399 if (Predicate_t2_so_imm(TrueVal.getNode())) {
1400 SDValue True = CurDAG->getTargetConstant(T->getZExtValue(), MVT::i32);
1401 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1402 SDValue Ops[] = { FalseVal, True, CC, CCR, InFlag };
1403 return CurDAG->SelectNodeTo(N,
1404 ARM::t2MOVCCi, MVT::i32, Ops, 5);
1409 SDNode *ARMDAGToDAGISel::
1410 SelectARMCMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
1411 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1412 ConstantSDNode *T = dyn_cast<ConstantSDNode>(TrueVal);
1416 if (Predicate_so_imm(TrueVal.getNode())) {
1417 SDValue True = CurDAG->getTargetConstant(T->getZExtValue(), MVT::i32);
1418 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1419 SDValue Ops[] = { FalseVal, True, CC, CCR, InFlag };
1420 return CurDAG->SelectNodeTo(N,
1421 ARM::MOVCCi, MVT::i32, Ops, 5);
1426 SDNode *ARMDAGToDAGISel::SelectCMOVOp(SDNode *N) {
1427 EVT VT = N->getValueType(0);
1428 SDValue FalseVal = N->getOperand(0);
1429 SDValue TrueVal = N->getOperand(1);
1430 SDValue CC = N->getOperand(2);
1431 SDValue CCR = N->getOperand(3);
1432 SDValue InFlag = N->getOperand(4);
1433 assert(CC.getOpcode() == ISD::Constant);
1434 assert(CCR.getOpcode() == ISD::Register);
1435 ARMCC::CondCodes CCVal =
1436 (ARMCC::CondCodes)cast<ConstantSDNode>(CC)->getZExtValue();
1438 if (!Subtarget->isThumb1Only() && VT == MVT::i32) {
1439 // Pattern: (ARMcmov:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
1440 // Emits: (MOVCCs:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
1441 // Pattern complexity = 18 cost = 1 size = 0
1445 if (Subtarget->isThumb()) {
1446 SDNode *Res = SelectT2CMOVShiftOp(N, FalseVal, TrueVal,
1447 CCVal, CCR, InFlag);
1449 Res = SelectT2CMOVShiftOp(N, TrueVal, FalseVal,
1450 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1454 SDNode *Res = SelectARMCMOVShiftOp(N, FalseVal, TrueVal,
1455 CCVal, CCR, InFlag);
1457 Res = SelectARMCMOVShiftOp(N, TrueVal, FalseVal,
1458 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1463 // Pattern: (ARMcmov:i32 GPR:i32:$false,
1464 // (imm:i32)<<P:Predicate_so_imm>>:$true,
1466 // Emits: (MOVCCi:i32 GPR:i32:$false,
1467 // (so_imm:i32 (imm:i32):$true), (imm:i32):$cc)
1468 // Pattern complexity = 10 cost = 1 size = 0
1469 if (Subtarget->isThumb()) {
1470 SDNode *Res = SelectT2CMOVSoImmOp(N, FalseVal, TrueVal,
1471 CCVal, CCR, InFlag);
1473 Res = SelectT2CMOVSoImmOp(N, TrueVal, FalseVal,
1474 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1478 SDNode *Res = SelectARMCMOVSoImmOp(N, FalseVal, TrueVal,
1479 CCVal, CCR, InFlag);
1481 Res = SelectARMCMOVSoImmOp(N, TrueVal, FalseVal,
1482 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1488 // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1489 // Emits: (MOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1490 // Pattern complexity = 6 cost = 1 size = 0
1492 // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1493 // Emits: (tMOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1494 // Pattern complexity = 6 cost = 11 size = 0
1496 // Also FCPYScc and FCPYDcc.
1497 SDValue Tmp2 = CurDAG->getTargetConstant(CCVal, MVT::i32);
1498 SDValue Ops[] = { FalseVal, TrueVal, Tmp2, CCR, InFlag };
1500 switch (VT.getSimpleVT().SimpleTy) {
1501 default: assert(false && "Illegal conditional move type!");
1504 Opc = Subtarget->isThumb()
1505 ? (Subtarget->hasThumb2() ? ARM::t2MOVCCr : ARM::tMOVCCr_pseudo)
1515 return CurDAG->SelectNodeTo(N, Opc, VT, Ops, 5);
1518 SDNode *ARMDAGToDAGISel::Select(SDNode *N) {
1519 DebugLoc dl = N->getDebugLoc();
1521 if (N->isMachineOpcode())
1522 return NULL; // Already selected.
1524 switch (N->getOpcode()) {
1526 case ISD::Constant: {
1527 unsigned Val = cast<ConstantSDNode>(N)->getZExtValue();
1529 if (Subtarget->hasThumb2())
1530 // Thumb2-aware targets have the MOVT instruction, so all immediates can
1531 // be done with MOV + MOVT, at worst.
1534 if (Subtarget->isThumb()) {
1535 UseCP = (Val > 255 && // MOV
1536 ~Val > 255 && // MOV + MVN
1537 !ARM_AM::isThumbImmShiftedVal(Val)); // MOV + LSL
1539 UseCP = (ARM_AM::getSOImmVal(Val) == -1 && // MOV
1540 ARM_AM::getSOImmVal(~Val) == -1 && // MVN
1541 !ARM_AM::isSOImmTwoPartVal(Val)); // two instrs.
1546 CurDAG->getTargetConstantPool(ConstantInt::get(
1547 Type::getInt32Ty(*CurDAG->getContext()), Val),
1548 TLI.getPointerTy());
1551 if (Subtarget->isThumb1Only()) {
1552 SDValue Pred = CurDAG->getTargetConstant(14, MVT::i32);
1553 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1554 SDValue Ops[] = { CPIdx, Pred, PredReg, CurDAG->getEntryNode() };
1555 ResNode = CurDAG->getMachineNode(ARM::tLDRcp, dl, MVT::i32, MVT::Other,
1560 CurDAG->getRegister(0, MVT::i32),
1561 CurDAG->getTargetConstant(0, MVT::i32),
1563 CurDAG->getRegister(0, MVT::i32),
1564 CurDAG->getEntryNode()
1566 ResNode=CurDAG->getMachineNode(ARM::LDRcp, dl, MVT::i32, MVT::Other,
1569 ReplaceUses(SDValue(N, 0), SDValue(ResNode, 0));
1573 // Other cases are autogenerated.
1576 case ISD::FrameIndex: {
1577 // Selects to ADDri FI, 0 which in turn will become ADDri SP, imm.
1578 int FI = cast<FrameIndexSDNode>(N)->getIndex();
1579 SDValue TFI = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
1580 if (Subtarget->isThumb1Only()) {
1581 return CurDAG->SelectNodeTo(N, ARM::tADDrSPi, MVT::i32, TFI,
1582 CurDAG->getTargetConstant(0, MVT::i32));
1584 unsigned Opc = ((Subtarget->isThumb() && Subtarget->hasThumb2()) ?
1585 ARM::t2ADDri : ARM::ADDri);
1586 SDValue Ops[] = { TFI, CurDAG->getTargetConstant(0, MVT::i32),
1587 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1588 CurDAG->getRegister(0, MVT::i32) };
1589 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
1592 case ARMISD::DYN_ALLOC:
1593 return SelectDYN_ALLOC(N);
1595 if (SDNode *I = SelectV6T2BitfieldExtractOp(N,
1596 Subtarget->isThumb() ? ARM::t2UBFX : ARM::UBFX))
1600 if (SDNode *I = SelectV6T2BitfieldExtractOp(N,
1601 Subtarget->isThumb() ? ARM::t2SBFX : ARM::SBFX))
1605 if (Subtarget->isThumb1Only())
1607 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1608 unsigned RHSV = C->getZExtValue();
1610 if (isPowerOf2_32(RHSV-1)) { // 2^n+1?
1611 unsigned ShImm = Log2_32(RHSV-1);
1614 SDValue V = N->getOperand(0);
1615 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
1616 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, MVT::i32);
1617 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1618 if (Subtarget->isThumb()) {
1619 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
1620 return CurDAG->SelectNodeTo(N, ARM::t2ADDrs, MVT::i32, Ops, 6);
1622 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
1623 return CurDAG->SelectNodeTo(N, ARM::ADDrs, MVT::i32, Ops, 7);
1626 if (isPowerOf2_32(RHSV+1)) { // 2^n-1?
1627 unsigned ShImm = Log2_32(RHSV+1);
1630 SDValue V = N->getOperand(0);
1631 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
1632 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, MVT::i32);
1633 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1634 if (Subtarget->isThumb()) {
1635 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG), Reg0 };
1636 return CurDAG->SelectNodeTo(N, ARM::t2RSBrs, MVT::i32, Ops, 5);
1638 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
1639 return CurDAG->SelectNodeTo(N, ARM::RSBrs, MVT::i32, Ops, 7);
1645 // (and (or x, c2), c1) and top 16-bits of c1 and c2 match, lower 16-bits
1646 // of c1 are 0xffff, and lower 16-bit of c2 are 0. That is, the top 16-bits
1647 // are entirely contributed by c2 and lower 16-bits are entirely contributed
1648 // by x. That's equal to (or (and x, 0xffff), (and c1, 0xffff0000)).
1649 // Select it to: "movt x, ((c1 & 0xffff) >> 16)
1650 EVT VT = N->getValueType(0);
1653 unsigned Opc = (Subtarget->isThumb() && Subtarget->hasThumb2())
1655 : (Subtarget->hasV6T2Ops() ? ARM::MOVTi16 : 0);
1658 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
1659 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1662 if (N0.getOpcode() == ISD::OR && N0.getNode()->hasOneUse()) {
1663 SDValue N2 = N0.getOperand(1);
1664 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1667 unsigned N1CVal = N1C->getZExtValue();
1668 unsigned N2CVal = N2C->getZExtValue();
1669 if ((N1CVal & 0xffff0000U) == (N2CVal & 0xffff0000U) &&
1670 (N1CVal & 0xffffU) == 0xffffU &&
1671 (N2CVal & 0xffffU) == 0x0U) {
1672 SDValue Imm16 = CurDAG->getTargetConstant((N2CVal & 0xFFFF0000U) >> 16,
1674 SDValue Ops[] = { N0.getOperand(0), Imm16,
1675 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
1676 return CurDAG->getMachineNode(Opc, dl, VT, Ops, 4);
1681 case ARMISD::VMOVRRD:
1682 return CurDAG->getMachineNode(ARM::VMOVRRD, dl, MVT::i32, MVT::i32,
1683 N->getOperand(0), getAL(CurDAG),
1684 CurDAG->getRegister(0, MVT::i32));
1685 case ISD::UMUL_LOHI: {
1686 if (Subtarget->isThumb1Only())
1688 if (Subtarget->isThumb()) {
1689 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
1690 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1691 CurDAG->getRegister(0, MVT::i32) };
1692 return CurDAG->getMachineNode(ARM::t2UMULL, dl, MVT::i32, MVT::i32, Ops,4);
1694 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
1695 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1696 CurDAG->getRegister(0, MVT::i32) };
1697 return CurDAG->getMachineNode(ARM::UMULL, dl, MVT::i32, MVT::i32, Ops, 5);
1700 case ISD::SMUL_LOHI: {
1701 if (Subtarget->isThumb1Only())
1703 if (Subtarget->isThumb()) {
1704 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
1705 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
1706 return CurDAG->getMachineNode(ARM::t2SMULL, dl, MVT::i32, MVT::i32, Ops,4);
1708 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
1709 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1710 CurDAG->getRegister(0, MVT::i32) };
1711 return CurDAG->getMachineNode(ARM::SMULL, dl, MVT::i32, MVT::i32, Ops, 5);
1715 SDNode *ResNode = 0;
1716 if (Subtarget->isThumb() && Subtarget->hasThumb2())
1717 ResNode = SelectT2IndexedLoad(N);
1719 ResNode = SelectARMIndexedLoad(N);
1722 // Other cases are autogenerated.
1725 case ARMISD::BRCOND: {
1726 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
1727 // Emits: (Bcc:void (bb:Other):$dst, (imm:i32):$cc)
1728 // Pattern complexity = 6 cost = 1 size = 0
1730 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
1731 // Emits: (tBcc:void (bb:Other):$dst, (imm:i32):$cc)
1732 // Pattern complexity = 6 cost = 1 size = 0
1734 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
1735 // Emits: (t2Bcc:void (bb:Other):$dst, (imm:i32):$cc)
1736 // Pattern complexity = 6 cost = 1 size = 0
1738 unsigned Opc = Subtarget->isThumb() ?
1739 ((Subtarget->hasThumb2()) ? ARM::t2Bcc : ARM::tBcc) : ARM::Bcc;
1740 SDValue Chain = N->getOperand(0);
1741 SDValue N1 = N->getOperand(1);
1742 SDValue N2 = N->getOperand(2);
1743 SDValue N3 = N->getOperand(3);
1744 SDValue InFlag = N->getOperand(4);
1745 assert(N1.getOpcode() == ISD::BasicBlock);
1746 assert(N2.getOpcode() == ISD::Constant);
1747 assert(N3.getOpcode() == ISD::Register);
1749 SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
1750 cast<ConstantSDNode>(N2)->getZExtValue()),
1752 SDValue Ops[] = { N1, Tmp2, N3, Chain, InFlag };
1753 SDNode *ResNode = CurDAG->getMachineNode(Opc, dl, MVT::Other,
1755 Chain = SDValue(ResNode, 0);
1756 if (N->getNumValues() == 2) {
1757 InFlag = SDValue(ResNode, 1);
1758 ReplaceUses(SDValue(N, 1), InFlag);
1760 ReplaceUses(SDValue(N, 0),
1761 SDValue(Chain.getNode(), Chain.getResNo()));
1765 return SelectCMOVOp(N);
1766 case ARMISD::CNEG: {
1767 EVT VT = N->getValueType(0);
1768 SDValue N0 = N->getOperand(0);
1769 SDValue N1 = N->getOperand(1);
1770 SDValue N2 = N->getOperand(2);
1771 SDValue N3 = N->getOperand(3);
1772 SDValue InFlag = N->getOperand(4);
1773 assert(N2.getOpcode() == ISD::Constant);
1774 assert(N3.getOpcode() == ISD::Register);
1776 SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
1777 cast<ConstantSDNode>(N2)->getZExtValue()),
1779 SDValue Ops[] = { N0, N1, Tmp2, N3, InFlag };
1781 switch (VT.getSimpleVT().SimpleTy) {
1782 default: assert(false && "Illegal conditional move type!");
1791 return CurDAG->SelectNodeTo(N, Opc, VT, Ops, 5);
1794 case ARMISD::VZIP: {
1796 EVT VT = N->getValueType(0);
1797 switch (VT.getSimpleVT().SimpleTy) {
1798 default: return NULL;
1799 case MVT::v8i8: Opc = ARM::VZIPd8; break;
1800 case MVT::v4i16: Opc = ARM::VZIPd16; break;
1802 case MVT::v2i32: Opc = ARM::VZIPd32; break;
1803 case MVT::v16i8: Opc = ARM::VZIPq8; break;
1804 case MVT::v8i16: Opc = ARM::VZIPq16; break;
1806 case MVT::v4i32: Opc = ARM::VZIPq32; break;
1808 SDValue Pred = CurDAG->getTargetConstant(14, MVT::i32);
1809 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1810 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
1811 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
1813 case ARMISD::VUZP: {
1815 EVT VT = N->getValueType(0);
1816 switch (VT.getSimpleVT().SimpleTy) {
1817 default: return NULL;
1818 case MVT::v8i8: Opc = ARM::VUZPd8; break;
1819 case MVT::v4i16: Opc = ARM::VUZPd16; break;
1821 case MVT::v2i32: Opc = ARM::VUZPd32; break;
1822 case MVT::v16i8: Opc = ARM::VUZPq8; break;
1823 case MVT::v8i16: Opc = ARM::VUZPq16; break;
1825 case MVT::v4i32: Opc = ARM::VUZPq32; break;
1827 SDValue Pred = CurDAG->getTargetConstant(14, MVT::i32);
1828 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1829 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
1830 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
1832 case ARMISD::VTRN: {
1834 EVT VT = N->getValueType(0);
1835 switch (VT.getSimpleVT().SimpleTy) {
1836 default: return NULL;
1837 case MVT::v8i8: Opc = ARM::VTRNd8; break;
1838 case MVT::v4i16: Opc = ARM::VTRNd16; break;
1840 case MVT::v2i32: Opc = ARM::VTRNd32; break;
1841 case MVT::v16i8: Opc = ARM::VTRNq8; break;
1842 case MVT::v8i16: Opc = ARM::VTRNq16; break;
1844 case MVT::v4i32: Opc = ARM::VTRNq32; break;
1846 SDValue Pred = CurDAG->getTargetConstant(14, MVT::i32);
1847 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1848 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
1849 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
1852 case ISD::INTRINSIC_VOID:
1853 case ISD::INTRINSIC_W_CHAIN: {
1854 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
1859 case Intrinsic::arm_neon_vld2: {
1860 unsigned DOpcodes[] = { ARM::VLD2d8, ARM::VLD2d16,
1861 ARM::VLD2d32, ARM::VLD2d64 };
1862 unsigned QOpcodes[] = { ARM::VLD2q8, ARM::VLD2q16, ARM::VLD2q32 };
1863 return SelectVLD(N, 2, DOpcodes, QOpcodes, 0);
1866 case Intrinsic::arm_neon_vld3: {
1867 unsigned DOpcodes[] = { ARM::VLD3d8, ARM::VLD3d16,
1868 ARM::VLD3d32, ARM::VLD3d64 };
1869 unsigned QOpcodes0[] = { ARM::VLD3q8a, ARM::VLD3q16a, ARM::VLD3q32a };
1870 unsigned QOpcodes1[] = { ARM::VLD3q8b, ARM::VLD3q16b, ARM::VLD3q32b };
1871 return SelectVLD(N, 3, DOpcodes, QOpcodes0, QOpcodes1);
1874 case Intrinsic::arm_neon_vld4: {
1875 unsigned DOpcodes[] = { ARM::VLD4d8, ARM::VLD4d16,
1876 ARM::VLD4d32, ARM::VLD4d64 };
1877 unsigned QOpcodes0[] = { ARM::VLD4q8a, ARM::VLD4q16a, ARM::VLD4q32a };
1878 unsigned QOpcodes1[] = { ARM::VLD4q8b, ARM::VLD4q16b, ARM::VLD4q32b };
1879 return SelectVLD(N, 4, DOpcodes, QOpcodes0, QOpcodes1);
1882 case Intrinsic::arm_neon_vld2lane: {
1883 unsigned DOpcodes[] = { ARM::VLD2LNd8, ARM::VLD2LNd16, ARM::VLD2LNd32 };
1884 unsigned QOpcodes0[] = { ARM::VLD2LNq16a, ARM::VLD2LNq32a };
1885 unsigned QOpcodes1[] = { ARM::VLD2LNq16b, ARM::VLD2LNq32b };
1886 return SelectVLDSTLane(N, true, 2, DOpcodes, QOpcodes0, QOpcodes1);
1889 case Intrinsic::arm_neon_vld3lane: {
1890 unsigned DOpcodes[] = { ARM::VLD3LNd8, ARM::VLD3LNd16, ARM::VLD3LNd32 };
1891 unsigned QOpcodes0[] = { ARM::VLD3LNq16a, ARM::VLD3LNq32a };
1892 unsigned QOpcodes1[] = { ARM::VLD3LNq16b, ARM::VLD3LNq32b };
1893 return SelectVLDSTLane(N, true, 3, DOpcodes, QOpcodes0, QOpcodes1);
1896 case Intrinsic::arm_neon_vld4lane: {
1897 unsigned DOpcodes[] = { ARM::VLD4LNd8, ARM::VLD4LNd16, ARM::VLD4LNd32 };
1898 unsigned QOpcodes0[] = { ARM::VLD4LNq16a, ARM::VLD4LNq32a };
1899 unsigned QOpcodes1[] = { ARM::VLD4LNq16b, ARM::VLD4LNq32b };
1900 return SelectVLDSTLane(N, true, 4, DOpcodes, QOpcodes0, QOpcodes1);
1903 case Intrinsic::arm_neon_vst2: {
1904 unsigned DOpcodes[] = { ARM::VST2d8, ARM::VST2d16,
1905 ARM::VST2d32, ARM::VST2d64 };
1906 unsigned QOpcodes[] = { ARM::VST2q8, ARM::VST2q16, ARM::VST2q32 };
1907 return SelectVST(N, 2, DOpcodes, QOpcodes, 0);
1910 case Intrinsic::arm_neon_vst3: {
1911 unsigned DOpcodes[] = { ARM::VST3d8, ARM::VST3d16,
1912 ARM::VST3d32, ARM::VST3d64 };
1913 unsigned QOpcodes0[] = { ARM::VST3q8a, ARM::VST3q16a, ARM::VST3q32a };
1914 unsigned QOpcodes1[] = { ARM::VST3q8b, ARM::VST3q16b, ARM::VST3q32b };
1915 return SelectVST(N, 3, DOpcodes, QOpcodes0, QOpcodes1);
1918 case Intrinsic::arm_neon_vst4: {
1919 unsigned DOpcodes[] = { ARM::VST4d8, ARM::VST4d16,
1920 ARM::VST4d32, ARM::VST4d64 };
1921 unsigned QOpcodes0[] = { ARM::VST4q8a, ARM::VST4q16a, ARM::VST4q32a };
1922 unsigned QOpcodes1[] = { ARM::VST4q8b, ARM::VST4q16b, ARM::VST4q32b };
1923 return SelectVST(N, 4, DOpcodes, QOpcodes0, QOpcodes1);
1926 case Intrinsic::arm_neon_vst2lane: {
1927 unsigned DOpcodes[] = { ARM::VST2LNd8, ARM::VST2LNd16, ARM::VST2LNd32 };
1928 unsigned QOpcodes0[] = { ARM::VST2LNq16a, ARM::VST2LNq32a };
1929 unsigned QOpcodes1[] = { ARM::VST2LNq16b, ARM::VST2LNq32b };
1930 return SelectVLDSTLane(N, false, 2, DOpcodes, QOpcodes0, QOpcodes1);
1933 case Intrinsic::arm_neon_vst3lane: {
1934 unsigned DOpcodes[] = { ARM::VST3LNd8, ARM::VST3LNd16, ARM::VST3LNd32 };
1935 unsigned QOpcodes0[] = { ARM::VST3LNq16a, ARM::VST3LNq32a };
1936 unsigned QOpcodes1[] = { ARM::VST3LNq16b, ARM::VST3LNq32b };
1937 return SelectVLDSTLane(N, false, 3, DOpcodes, QOpcodes0, QOpcodes1);
1940 case Intrinsic::arm_neon_vst4lane: {
1941 unsigned DOpcodes[] = { ARM::VST4LNd8, ARM::VST4LNd16, ARM::VST4LNd32 };
1942 unsigned QOpcodes0[] = { ARM::VST4LNq16a, ARM::VST4LNq32a };
1943 unsigned QOpcodes1[] = { ARM::VST4LNq16b, ARM::VST4LNq32b };
1944 return SelectVLDSTLane(N, false, 4, DOpcodes, QOpcodes0, QOpcodes1);
1950 return SelectCode(N);
1953 bool ARMDAGToDAGISel::
1954 SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
1955 std::vector<SDValue> &OutOps) {
1956 assert(ConstraintCode == 'm' && "unexpected asm memory constraint");
1957 // Require the address to be in a register. That is safe for all ARM
1958 // variants and it is hard to do anything much smarter without knowing
1959 // how the operand is used.
1960 OutOps.push_back(Op);
1964 /// createARMISelDag - This pass converts a legalized DAG into a
1965 /// ARM-specific DAG, ready for instruction scheduling.
1967 FunctionPass *llvm::createARMISelDag(ARMBaseTargetMachine &TM,
1968 CodeGenOpt::Level OptLevel) {
1969 return new ARMDAGToDAGISel(TM, OptLevel);