1 //===-- ARMISelDAGToDAG.cpp - A dag to dag inst selector for ARM ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines an instruction selector for the ARM target.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "arm-isel"
16 #include "ARMAddressingModes.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/CommandLine.h"
32 #include "llvm/Support/Compiler.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/raw_ostream.h"
40 DisableShifterOp("disable-shifter-op", cl::Hidden,
41 cl::desc("Disable isel of shifter-op"),
44 //===--------------------------------------------------------------------===//
45 /// ARMDAGToDAGISel - ARM specific code to select ARM machine
46 /// instructions for SelectionDAG operations.
49 class ARMDAGToDAGISel : public SelectionDAGISel {
50 ARMBaseTargetMachine &TM;
52 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
53 /// make the right decision when generating code for different targets.
54 const ARMSubtarget *Subtarget;
57 explicit ARMDAGToDAGISel(ARMBaseTargetMachine &tm,
58 CodeGenOpt::Level OptLevel)
59 : SelectionDAGISel(tm, OptLevel), TM(tm),
60 Subtarget(&TM.getSubtarget<ARMSubtarget>()) {
63 virtual const char *getPassName() const {
64 return "ARM Instruction Selection";
67 /// getI32Imm - Return a target constant of type i32 with the specified
69 inline SDValue getI32Imm(unsigned Imm) {
70 return CurDAG->getTargetConstant(Imm, MVT::i32);
73 SDNode *Select(SDNode *N);
75 bool SelectShifterOperandReg(SDNode *Op, SDValue N, SDValue &A,
76 SDValue &B, SDValue &C);
77 bool SelectAddrMode2(SDNode *Op, SDValue N, SDValue &Base,
78 SDValue &Offset, SDValue &Opc);
79 bool SelectAddrMode2Offset(SDNode *Op, SDValue N,
80 SDValue &Offset, SDValue &Opc);
81 bool SelectAddrMode3(SDNode *Op, SDValue N, SDValue &Base,
82 SDValue &Offset, SDValue &Opc);
83 bool SelectAddrMode3Offset(SDNode *Op, SDValue N,
84 SDValue &Offset, SDValue &Opc);
85 bool SelectAddrMode4(SDNode *Op, SDValue N, SDValue &Addr,
87 bool SelectAddrMode5(SDNode *Op, SDValue N, SDValue &Base,
89 bool SelectAddrMode6(SDNode *Op, SDValue N, SDValue &Addr, SDValue &Align);
91 bool SelectAddrModePC(SDNode *Op, SDValue N, SDValue &Offset,
94 bool SelectThumbAddrModeRR(SDNode *Op, SDValue N, SDValue &Base,
96 bool SelectThumbAddrModeRI5(SDNode *Op, SDValue N, unsigned Scale,
97 SDValue &Base, SDValue &OffImm,
99 bool SelectThumbAddrModeS1(SDNode *Op, SDValue N, SDValue &Base,
100 SDValue &OffImm, SDValue &Offset);
101 bool SelectThumbAddrModeS2(SDNode *Op, SDValue N, SDValue &Base,
102 SDValue &OffImm, SDValue &Offset);
103 bool SelectThumbAddrModeS4(SDNode *Op, SDValue N, SDValue &Base,
104 SDValue &OffImm, SDValue &Offset);
105 bool SelectThumbAddrModeSP(SDNode *Op, SDValue N, SDValue &Base,
108 bool SelectT2ShifterOperandReg(SDNode *Op, SDValue N,
109 SDValue &BaseReg, SDValue &Opc);
110 bool SelectT2AddrModeImm12(SDNode *Op, SDValue N, SDValue &Base,
112 bool SelectT2AddrModeImm8(SDNode *Op, SDValue N, SDValue &Base,
114 bool SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
116 bool SelectT2AddrModeSoReg(SDNode *Op, SDValue N, SDValue &Base,
117 SDValue &OffReg, SDValue &ShImm);
119 inline bool Pred_so_imm(SDNode *inN) const {
120 ConstantSDNode *N = cast<ConstantSDNode>(inN);
121 return ARM_AM::getSOImmVal(N->getZExtValue()) != -1;
124 inline bool Pred_t2_so_imm(SDNode *inN) const {
125 ConstantSDNode *N = cast<ConstantSDNode>(inN);
126 return ARM_AM::getT2SOImmVal(N->getZExtValue()) != -1;
129 // Include the pieces autogenerated from the target description.
130 #include "ARMGenDAGISel.inc"
133 /// SelectARMIndexedLoad - Indexed (pre/post inc/dec) load matching code for
135 SDNode *SelectARMIndexedLoad(SDNode *N);
136 SDNode *SelectT2IndexedLoad(SDNode *N);
138 /// SelectVLD - Select NEON load intrinsics. NumVecs should be
139 /// 1, 2, 3 or 4. The opcode arrays specify the instructions used for
140 /// loads of D registers and even subregs and odd subregs of Q registers.
141 /// For NumVecs <= 2, QOpcodes1 is not used.
142 SDNode *SelectVLD(SDNode *N, unsigned NumVecs, unsigned *DOpcodes,
143 unsigned *QOpcodes0, unsigned *QOpcodes1);
145 /// SelectVST - Select NEON store intrinsics. NumVecs should
146 /// be 1, 2, 3 or 4. The opcode arrays specify the instructions used for
147 /// stores of D registers and even subregs and odd subregs of Q registers.
148 /// For NumVecs <= 2, QOpcodes1 is not used.
149 SDNode *SelectVST(SDNode *N, unsigned NumVecs, unsigned *DOpcodes,
150 unsigned *QOpcodes0, unsigned *QOpcodes1);
152 /// SelectVLDSTLane - Select NEON load/store lane intrinsics. NumVecs should
153 /// be 2, 3 or 4. The opcode arrays specify the instructions used for
154 /// load/store of D registers and Q registers.
155 SDNode *SelectVLDSTLane(SDNode *N, bool IsLoad, unsigned NumVecs,
156 unsigned *DOpcodes, unsigned *QOpcodes);
158 /// SelectVTBL - Select NEON VTBL and VTBX intrinsics. NumVecs should be 2,
159 /// 3 or 4. These are custom-selected so that a REG_SEQUENCE can be
160 /// generated to force the table registers to be consecutive.
161 SDNode *SelectVTBL(SDNode *N, bool IsExt, unsigned NumVecs, unsigned Opc);
163 /// SelectV6T2BitfieldExtractOp - Select SBFX/UBFX instructions for ARM.
164 SDNode *SelectV6T2BitfieldExtractOp(SDNode *N, bool isSigned);
166 /// SelectCMOVOp - Select CMOV instructions for ARM.
167 SDNode *SelectCMOVOp(SDNode *N);
168 SDNode *SelectT2CMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
169 ARMCC::CondCodes CCVal, SDValue CCR,
171 SDNode *SelectARMCMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
172 ARMCC::CondCodes CCVal, SDValue CCR,
174 SDNode *SelectT2CMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
175 ARMCC::CondCodes CCVal, SDValue CCR,
177 SDNode *SelectARMCMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
178 ARMCC::CondCodes CCVal, SDValue CCR,
181 SDNode *SelectConcatVector(SDNode *N);
183 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
184 /// inline asm expressions.
185 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
187 std::vector<SDValue> &OutOps);
189 // Form pairs of consecutive S, D, or Q registers.
190 SDNode *PairSRegs(EVT VT, SDValue V0, SDValue V1);
191 SDNode *PairDRegs(EVT VT, SDValue V0, SDValue V1);
192 SDNode *PairQRegs(EVT VT, SDValue V0, SDValue V1);
194 // Form sequences of 4 consecutive S, D, or Q registers.
195 SDNode *QuadSRegs(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
196 SDNode *QuadDRegs(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
197 SDNode *QuadQRegs(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
201 /// isInt32Immediate - This method tests to see if the node is a 32-bit constant
202 /// operand. If so Imm will receive the 32-bit value.
203 static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
204 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
205 Imm = cast<ConstantSDNode>(N)->getZExtValue();
211 // isInt32Immediate - This method tests to see if a constant operand.
212 // If so Imm will receive the 32 bit value.
213 static bool isInt32Immediate(SDValue N, unsigned &Imm) {
214 return isInt32Immediate(N.getNode(), Imm);
217 // isOpcWithIntImmediate - This method tests to see if the node is a specific
218 // opcode and that it has a immediate integer right operand.
219 // If so Imm will receive the 32 bit value.
220 static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
221 return N->getOpcode() == Opc &&
222 isInt32Immediate(N->getOperand(1).getNode(), Imm);
226 bool ARMDAGToDAGISel::SelectShifterOperandReg(SDNode *Op,
231 if (DisableShifterOp)
234 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
236 // Don't match base register only case. That is matched to a separate
237 // lower complexity pattern with explicit register operand.
238 if (ShOpcVal == ARM_AM::no_shift) return false;
240 BaseReg = N.getOperand(0);
241 unsigned ShImmVal = 0;
242 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
243 ShReg = CurDAG->getRegister(0, MVT::i32);
244 ShImmVal = RHS->getZExtValue() & 31;
246 ShReg = N.getOperand(1);
248 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
253 bool ARMDAGToDAGISel::SelectAddrMode2(SDNode *Op, SDValue N,
254 SDValue &Base, SDValue &Offset,
256 if (N.getOpcode() == ISD::MUL) {
257 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
258 // X * [3,5,9] -> X + X * [2,4,8] etc.
259 int RHSC = (int)RHS->getZExtValue();
262 ARM_AM::AddrOpc AddSub = ARM_AM::add;
264 AddSub = ARM_AM::sub;
267 if (isPowerOf2_32(RHSC)) {
268 unsigned ShAmt = Log2_32(RHSC);
269 Base = Offset = N.getOperand(0);
270 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt,
279 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB) {
281 if (N.getOpcode() == ISD::FrameIndex) {
282 int FI = cast<FrameIndexSDNode>(N)->getIndex();
283 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
284 } else if (N.getOpcode() == ARMISD::Wrapper &&
285 !(Subtarget->useMovt() &&
286 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
287 Base = N.getOperand(0);
289 Offset = CurDAG->getRegister(0, MVT::i32);
290 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::add, 0,
296 // Match simple R +/- imm12 operands.
297 if (N.getOpcode() == ISD::ADD)
298 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
299 int RHSC = (int)RHS->getZExtValue();
300 if ((RHSC >= 0 && RHSC < 0x1000) ||
301 (RHSC < 0 && RHSC > -0x1000)) { // 12 bits.
302 Base = N.getOperand(0);
303 if (Base.getOpcode() == ISD::FrameIndex) {
304 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
305 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
307 Offset = CurDAG->getRegister(0, MVT::i32);
309 ARM_AM::AddrOpc AddSub = ARM_AM::add;
311 AddSub = ARM_AM::sub;
314 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, RHSC,
321 // Otherwise this is R +/- [possibly shifted] R.
322 ARM_AM::AddrOpc AddSub = N.getOpcode() == ISD::ADD ? ARM_AM::add:ARM_AM::sub;
323 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(1));
326 Base = N.getOperand(0);
327 Offset = N.getOperand(1);
329 if (ShOpcVal != ARM_AM::no_shift) {
330 // Check to see if the RHS of the shift is a constant, if not, we can't fold
332 if (ConstantSDNode *Sh =
333 dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) {
334 ShAmt = Sh->getZExtValue();
335 Offset = N.getOperand(1).getOperand(0);
337 ShOpcVal = ARM_AM::no_shift;
341 // Try matching (R shl C) + (R).
342 if (N.getOpcode() == ISD::ADD && ShOpcVal == ARM_AM::no_shift) {
343 ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0));
344 if (ShOpcVal != ARM_AM::no_shift) {
345 // Check to see if the RHS of the shift is a constant, if not, we can't
347 if (ConstantSDNode *Sh =
348 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
349 ShAmt = Sh->getZExtValue();
350 Offset = N.getOperand(0).getOperand(0);
351 Base = N.getOperand(1);
353 ShOpcVal = ARM_AM::no_shift;
358 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
363 bool ARMDAGToDAGISel::SelectAddrMode2Offset(SDNode *Op, SDValue N,
364 SDValue &Offset, SDValue &Opc) {
365 unsigned Opcode = Op->getOpcode();
366 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
367 ? cast<LoadSDNode>(Op)->getAddressingMode()
368 : cast<StoreSDNode>(Op)->getAddressingMode();
369 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
370 ? ARM_AM::add : ARM_AM::sub;
371 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
372 int Val = (int)C->getZExtValue();
373 if (Val >= 0 && Val < 0x1000) { // 12 bits.
374 Offset = CurDAG->getRegister(0, MVT::i32);
375 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, Val,
383 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
385 if (ShOpcVal != ARM_AM::no_shift) {
386 // Check to see if the RHS of the shift is a constant, if not, we can't fold
388 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
389 ShAmt = Sh->getZExtValue();
390 Offset = N.getOperand(0);
392 ShOpcVal = ARM_AM::no_shift;
396 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
402 bool ARMDAGToDAGISel::SelectAddrMode3(SDNode *Op, SDValue N,
403 SDValue &Base, SDValue &Offset,
405 if (N.getOpcode() == ISD::SUB) {
406 // X - C is canonicalize to X + -C, no need to handle it here.
407 Base = N.getOperand(0);
408 Offset = N.getOperand(1);
409 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::sub, 0),MVT::i32);
413 if (N.getOpcode() != ISD::ADD) {
415 if (N.getOpcode() == ISD::FrameIndex) {
416 int FI = cast<FrameIndexSDNode>(N)->getIndex();
417 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
419 Offset = CurDAG->getRegister(0, MVT::i32);
420 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0),MVT::i32);
424 // If the RHS is +/- imm8, fold into addr mode.
425 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
426 int RHSC = (int)RHS->getZExtValue();
427 if ((RHSC >= 0 && RHSC < 256) ||
428 (RHSC < 0 && RHSC > -256)) { // note -256 itself isn't allowed.
429 Base = N.getOperand(0);
430 if (Base.getOpcode() == ISD::FrameIndex) {
431 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
432 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
434 Offset = CurDAG->getRegister(0, MVT::i32);
436 ARM_AM::AddrOpc AddSub = ARM_AM::add;
438 AddSub = ARM_AM::sub;
441 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, RHSC),MVT::i32);
446 Base = N.getOperand(0);
447 Offset = N.getOperand(1);
448 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0), MVT::i32);
452 bool ARMDAGToDAGISel::SelectAddrMode3Offset(SDNode *Op, SDValue N,
453 SDValue &Offset, SDValue &Opc) {
454 unsigned Opcode = Op->getOpcode();
455 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
456 ? cast<LoadSDNode>(Op)->getAddressingMode()
457 : cast<StoreSDNode>(Op)->getAddressingMode();
458 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
459 ? ARM_AM::add : ARM_AM::sub;
460 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
461 int Val = (int)C->getZExtValue();
462 if (Val >= 0 && Val < 256) {
463 Offset = CurDAG->getRegister(0, MVT::i32);
464 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, Val), MVT::i32);
470 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, 0), MVT::i32);
474 bool ARMDAGToDAGISel::SelectAddrMode4(SDNode *Op, SDValue N,
475 SDValue &Addr, SDValue &Mode) {
477 Mode = CurDAG->getTargetConstant(ARM_AM::getAM4ModeImm(ARM_AM::ia), MVT::i32);
481 bool ARMDAGToDAGISel::SelectAddrMode5(SDNode *Op, SDValue N,
482 SDValue &Base, SDValue &Offset) {
483 if (N.getOpcode() != ISD::ADD) {
485 if (N.getOpcode() == ISD::FrameIndex) {
486 int FI = cast<FrameIndexSDNode>(N)->getIndex();
487 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
488 } else if (N.getOpcode() == ARMISD::Wrapper &&
489 !(Subtarget->useMovt() &&
490 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
491 Base = N.getOperand(0);
493 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
498 // If the RHS is +/- imm8, fold into addr mode.
499 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
500 int RHSC = (int)RHS->getZExtValue();
501 if ((RHSC & 3) == 0) { // The constant is implicitly multiplied by 4.
503 if ((RHSC >= 0 && RHSC < 256) ||
504 (RHSC < 0 && RHSC > -256)) { // note -256 itself isn't allowed.
505 Base = N.getOperand(0);
506 if (Base.getOpcode() == ISD::FrameIndex) {
507 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
508 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
511 ARM_AM::AddrOpc AddSub = ARM_AM::add;
513 AddSub = ARM_AM::sub;
516 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(AddSub, RHSC),
524 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
529 bool ARMDAGToDAGISel::SelectAddrMode6(SDNode *Op, SDValue N,
530 SDValue &Addr, SDValue &Align) {
532 // Default to no alignment.
533 Align = CurDAG->getTargetConstant(0, MVT::i32);
537 bool ARMDAGToDAGISel::SelectAddrModePC(SDNode *Op, SDValue N,
538 SDValue &Offset, SDValue &Label) {
539 if (N.getOpcode() == ARMISD::PIC_ADD && N.hasOneUse()) {
540 Offset = N.getOperand(0);
541 SDValue N1 = N.getOperand(1);
542 Label = CurDAG->getTargetConstant(cast<ConstantSDNode>(N1)->getZExtValue(),
549 bool ARMDAGToDAGISel::SelectThumbAddrModeRR(SDNode *Op, SDValue N,
550 SDValue &Base, SDValue &Offset){
551 // FIXME dl should come from the parent load or store, not the address
552 if (N.getOpcode() != ISD::ADD) {
553 ConstantSDNode *NC = dyn_cast<ConstantSDNode>(N);
554 if (!NC || !NC->isNullValue())
561 Base = N.getOperand(0);
562 Offset = N.getOperand(1);
567 ARMDAGToDAGISel::SelectThumbAddrModeRI5(SDNode *Op, SDValue N,
568 unsigned Scale, SDValue &Base,
569 SDValue &OffImm, SDValue &Offset) {
571 SDValue TmpBase, TmpOffImm;
572 if (SelectThumbAddrModeSP(Op, N, TmpBase, TmpOffImm))
573 return false; // We want to select tLDRspi / tSTRspi instead.
574 if (N.getOpcode() == ARMISD::Wrapper &&
575 N.getOperand(0).getOpcode() == ISD::TargetConstantPool)
576 return false; // We want to select tLDRpci instead.
579 if (N.getOpcode() != ISD::ADD) {
580 if (N.getOpcode() == ARMISD::Wrapper &&
581 !(Subtarget->useMovt() &&
582 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
583 Base = N.getOperand(0);
587 Offset = CurDAG->getRegister(0, MVT::i32);
588 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
592 // Thumb does not have [sp, r] address mode.
593 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
594 RegisterSDNode *RHSR = dyn_cast<RegisterSDNode>(N.getOperand(1));
595 if ((LHSR && LHSR->getReg() == ARM::SP) ||
596 (RHSR && RHSR->getReg() == ARM::SP)) {
598 Offset = CurDAG->getRegister(0, MVT::i32);
599 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
603 // If the RHS is + imm5 * scale, fold into addr mode.
604 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
605 int RHSC = (int)RHS->getZExtValue();
606 if ((RHSC & (Scale-1)) == 0) { // The constant is implicitly multiplied.
608 if (RHSC >= 0 && RHSC < 32) {
609 Base = N.getOperand(0);
610 Offset = CurDAG->getRegister(0, MVT::i32);
611 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
617 Base = N.getOperand(0);
618 Offset = N.getOperand(1);
619 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
623 bool ARMDAGToDAGISel::SelectThumbAddrModeS1(SDNode *Op, SDValue N,
624 SDValue &Base, SDValue &OffImm,
626 return SelectThumbAddrModeRI5(Op, N, 1, Base, OffImm, Offset);
629 bool ARMDAGToDAGISel::SelectThumbAddrModeS2(SDNode *Op, SDValue N,
630 SDValue &Base, SDValue &OffImm,
632 return SelectThumbAddrModeRI5(Op, N, 2, Base, OffImm, Offset);
635 bool ARMDAGToDAGISel::SelectThumbAddrModeS4(SDNode *Op, SDValue N,
636 SDValue &Base, SDValue &OffImm,
638 return SelectThumbAddrModeRI5(Op, N, 4, Base, OffImm, Offset);
641 bool ARMDAGToDAGISel::SelectThumbAddrModeSP(SDNode *Op, SDValue N,
642 SDValue &Base, SDValue &OffImm) {
643 if (N.getOpcode() == ISD::FrameIndex) {
644 int FI = cast<FrameIndexSDNode>(N)->getIndex();
645 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
646 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
650 if (N.getOpcode() != ISD::ADD)
653 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
654 if (N.getOperand(0).getOpcode() == ISD::FrameIndex ||
655 (LHSR && LHSR->getReg() == ARM::SP)) {
656 // If the RHS is + imm8 * scale, fold into addr mode.
657 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
658 int RHSC = (int)RHS->getZExtValue();
659 if ((RHSC & 3) == 0) { // The constant is implicitly multiplied.
661 if (RHSC >= 0 && RHSC < 256) {
662 Base = N.getOperand(0);
663 if (Base.getOpcode() == ISD::FrameIndex) {
664 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
665 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
667 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
677 bool ARMDAGToDAGISel::SelectT2ShifterOperandReg(SDNode *Op, SDValue N,
680 if (DisableShifterOp)
683 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
685 // Don't match base register only case. That is matched to a separate
686 // lower complexity pattern with explicit register operand.
687 if (ShOpcVal == ARM_AM::no_shift) return false;
689 BaseReg = N.getOperand(0);
690 unsigned ShImmVal = 0;
691 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
692 ShImmVal = RHS->getZExtValue() & 31;
693 Opc = getI32Imm(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal));
700 bool ARMDAGToDAGISel::SelectT2AddrModeImm12(SDNode *Op, SDValue N,
701 SDValue &Base, SDValue &OffImm) {
702 // Match simple R + imm12 operands.
705 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB) {
706 if (N.getOpcode() == ISD::FrameIndex) {
707 // Match frame index...
708 int FI = cast<FrameIndexSDNode>(N)->getIndex();
709 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
710 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
712 } else if (N.getOpcode() == ARMISD::Wrapper &&
713 !(Subtarget->useMovt() &&
714 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
715 Base = N.getOperand(0);
716 if (Base.getOpcode() == ISD::TargetConstantPool)
717 return false; // We want to select t2LDRpci instead.
720 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
724 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
725 if (SelectT2AddrModeImm8(Op, N, Base, OffImm))
726 // Let t2LDRi8 handle (R - imm8).
729 int RHSC = (int)RHS->getZExtValue();
730 if (N.getOpcode() == ISD::SUB)
733 if (RHSC >= 0 && RHSC < 0x1000) { // 12 bits (unsigned)
734 Base = N.getOperand(0);
735 if (Base.getOpcode() == ISD::FrameIndex) {
736 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
737 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
739 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
746 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
750 bool ARMDAGToDAGISel::SelectT2AddrModeImm8(SDNode *Op, SDValue N,
751 SDValue &Base, SDValue &OffImm) {
752 // Match simple R - imm8 operands.
753 if (N.getOpcode() == ISD::ADD || N.getOpcode() == ISD::SUB) {
754 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
755 int RHSC = (int)RHS->getSExtValue();
756 if (N.getOpcode() == ISD::SUB)
759 if ((RHSC >= -255) && (RHSC < 0)) { // 8 bits (always negative)
760 Base = N.getOperand(0);
761 if (Base.getOpcode() == ISD::FrameIndex) {
762 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
763 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
765 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
774 bool ARMDAGToDAGISel::SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
776 unsigned Opcode = Op->getOpcode();
777 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
778 ? cast<LoadSDNode>(Op)->getAddressingMode()
779 : cast<StoreSDNode>(Op)->getAddressingMode();
780 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N)) {
781 int RHSC = (int)RHS->getZExtValue();
782 if (RHSC >= 0 && RHSC < 0x100) { // 8 bits.
783 OffImm = ((AM == ISD::PRE_INC) || (AM == ISD::POST_INC))
784 ? CurDAG->getTargetConstant(RHSC, MVT::i32)
785 : CurDAG->getTargetConstant(-RHSC, MVT::i32);
793 bool ARMDAGToDAGISel::SelectT2AddrModeSoReg(SDNode *Op, SDValue N,
795 SDValue &OffReg, SDValue &ShImm) {
796 // (R - imm8) should be handled by t2LDRi8. The rest are handled by t2LDRi12.
797 if (N.getOpcode() != ISD::ADD)
800 // Leave (R + imm12) for t2LDRi12, (R - imm8) for t2LDRi8.
801 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
802 int RHSC = (int)RHS->getZExtValue();
803 if (RHSC >= 0 && RHSC < 0x1000) // 12 bits (unsigned)
805 else if (RHSC < 0 && RHSC >= -255) // 8 bits
809 // Look for (R + R) or (R + (R << [1,2,3])).
811 Base = N.getOperand(0);
812 OffReg = N.getOperand(1);
814 // Swap if it is ((R << c) + R).
815 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(OffReg);
816 if (ShOpcVal != ARM_AM::lsl) {
817 ShOpcVal = ARM_AM::getShiftOpcForNode(Base);
818 if (ShOpcVal == ARM_AM::lsl)
819 std::swap(Base, OffReg);
822 if (ShOpcVal == ARM_AM::lsl) {
823 // Check to see if the RHS of the shift is a constant, if not, we can't fold
825 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(OffReg.getOperand(1))) {
826 ShAmt = Sh->getZExtValue();
829 ShOpcVal = ARM_AM::no_shift;
831 OffReg = OffReg.getOperand(0);
833 ShOpcVal = ARM_AM::no_shift;
837 ShImm = CurDAG->getTargetConstant(ShAmt, MVT::i32);
842 //===--------------------------------------------------------------------===//
844 /// getAL - Returns a ARMCC::AL immediate node.
845 static inline SDValue getAL(SelectionDAG *CurDAG) {
846 return CurDAG->getTargetConstant((uint64_t)ARMCC::AL, MVT::i32);
849 SDNode *ARMDAGToDAGISel::SelectARMIndexedLoad(SDNode *N) {
850 LoadSDNode *LD = cast<LoadSDNode>(N);
851 ISD::MemIndexedMode AM = LD->getAddressingMode();
852 if (AM == ISD::UNINDEXED)
855 EVT LoadedVT = LD->getMemoryVT();
856 SDValue Offset, AMOpc;
857 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
860 if (LoadedVT == MVT::i32 &&
861 SelectAddrMode2Offset(N, LD->getOffset(), Offset, AMOpc)) {
862 Opcode = isPre ? ARM::LDR_PRE : ARM::LDR_POST;
864 } else if (LoadedVT == MVT::i16 &&
865 SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
867 Opcode = (LD->getExtensionType() == ISD::SEXTLOAD)
868 ? (isPre ? ARM::LDRSH_PRE : ARM::LDRSH_POST)
869 : (isPre ? ARM::LDRH_PRE : ARM::LDRH_POST);
870 } else if (LoadedVT == MVT::i8 || LoadedVT == MVT::i1) {
871 if (LD->getExtensionType() == ISD::SEXTLOAD) {
872 if (SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
874 Opcode = isPre ? ARM::LDRSB_PRE : ARM::LDRSB_POST;
877 if (SelectAddrMode2Offset(N, LD->getOffset(), Offset, AMOpc)) {
879 Opcode = isPre ? ARM::LDRB_PRE : ARM::LDRB_POST;
885 SDValue Chain = LD->getChain();
886 SDValue Base = LD->getBasePtr();
887 SDValue Ops[]= { Base, Offset, AMOpc, getAL(CurDAG),
888 CurDAG->getRegister(0, MVT::i32), Chain };
889 return CurDAG->getMachineNode(Opcode, N->getDebugLoc(), MVT::i32, MVT::i32,
896 SDNode *ARMDAGToDAGISel::SelectT2IndexedLoad(SDNode *N) {
897 LoadSDNode *LD = cast<LoadSDNode>(N);
898 ISD::MemIndexedMode AM = LD->getAddressingMode();
899 if (AM == ISD::UNINDEXED)
902 EVT LoadedVT = LD->getMemoryVT();
903 bool isSExtLd = LD->getExtensionType() == ISD::SEXTLOAD;
905 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
908 if (SelectT2AddrModeImm8Offset(N, LD->getOffset(), Offset)) {
909 switch (LoadedVT.getSimpleVT().SimpleTy) {
911 Opcode = isPre ? ARM::t2LDR_PRE : ARM::t2LDR_POST;
915 Opcode = isPre ? ARM::t2LDRSH_PRE : ARM::t2LDRSH_POST;
917 Opcode = isPre ? ARM::t2LDRH_PRE : ARM::t2LDRH_POST;
922 Opcode = isPre ? ARM::t2LDRSB_PRE : ARM::t2LDRSB_POST;
924 Opcode = isPre ? ARM::t2LDRB_PRE : ARM::t2LDRB_POST;
933 SDValue Chain = LD->getChain();
934 SDValue Base = LD->getBasePtr();
935 SDValue Ops[]= { Base, Offset, getAL(CurDAG),
936 CurDAG->getRegister(0, MVT::i32), Chain };
937 return CurDAG->getMachineNode(Opcode, N->getDebugLoc(), MVT::i32, MVT::i32,
944 /// PairSRegs - Form a D register from a pair of S registers.
946 SDNode *ARMDAGToDAGISel::PairSRegs(EVT VT, SDValue V0, SDValue V1) {
947 DebugLoc dl = V0.getNode()->getDebugLoc();
948 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, MVT::i32);
949 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, MVT::i32);
950 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
951 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
954 /// PairDRegs - Form a quad register from a pair of D registers.
956 SDNode *ARMDAGToDAGISel::PairDRegs(EVT VT, SDValue V0, SDValue V1) {
957 DebugLoc dl = V0.getNode()->getDebugLoc();
958 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
959 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
960 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
961 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
964 /// PairQRegs - Form 4 consecutive D registers from a pair of Q registers.
966 SDNode *ARMDAGToDAGISel::PairQRegs(EVT VT, SDValue V0, SDValue V1) {
967 DebugLoc dl = V0.getNode()->getDebugLoc();
968 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, MVT::i32);
969 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, MVT::i32);
970 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
971 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
974 /// QuadSRegs - Form 4 consecutive S registers.
976 SDNode *ARMDAGToDAGISel::QuadSRegs(EVT VT, SDValue V0, SDValue V1,
977 SDValue V2, SDValue V3) {
978 DebugLoc dl = V0.getNode()->getDebugLoc();
979 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, MVT::i32);
980 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, MVT::i32);
981 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::ssub_2, MVT::i32);
982 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::ssub_3, MVT::i32);
983 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1, V2, SubReg2, V3, SubReg3 };
984 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 8);
987 /// QuadDRegs - Form 4 consecutive D registers.
989 SDNode *ARMDAGToDAGISel::QuadDRegs(EVT VT, SDValue V0, SDValue V1,
990 SDValue V2, SDValue V3) {
991 DebugLoc dl = V0.getNode()->getDebugLoc();
992 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
993 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
994 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::dsub_2, MVT::i32);
995 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::dsub_3, MVT::i32);
996 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1, V2, SubReg2, V3, SubReg3 };
997 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 8);
1000 /// QuadQRegs - Form 4 consecutive Q registers.
1002 SDNode *ARMDAGToDAGISel::QuadQRegs(EVT VT, SDValue V0, SDValue V1,
1003 SDValue V2, SDValue V3) {
1004 DebugLoc dl = V0.getNode()->getDebugLoc();
1005 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, MVT::i32);
1006 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, MVT::i32);
1007 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::qsub_2, MVT::i32);
1008 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::qsub_3, MVT::i32);
1009 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1, V2, SubReg2, V3, SubReg3 };
1010 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 8);
1013 SDNode *ARMDAGToDAGISel::SelectVLD(SDNode *N, unsigned NumVecs,
1014 unsigned *DOpcodes, unsigned *QOpcodes0,
1015 unsigned *QOpcodes1) {
1016 assert(NumVecs >= 1 && NumVecs <= 4 && "VLD NumVecs out-of-range");
1017 DebugLoc dl = N->getDebugLoc();
1019 SDValue MemAddr, Align;
1020 if (!SelectAddrMode6(N, N->getOperand(2), MemAddr, Align))
1023 SDValue Chain = N->getOperand(0);
1024 EVT VT = N->getValueType(0);
1025 bool is64BitVector = VT.is64BitVector();
1027 unsigned OpcodeIndex;
1028 switch (VT.getSimpleVT().SimpleTy) {
1029 default: llvm_unreachable("unhandled vld type");
1030 // Double-register operations:
1031 case MVT::v8i8: OpcodeIndex = 0; break;
1032 case MVT::v4i16: OpcodeIndex = 1; break;
1034 case MVT::v2i32: OpcodeIndex = 2; break;
1035 case MVT::v1i64: OpcodeIndex = 3; break;
1036 // Quad-register operations:
1037 case MVT::v16i8: OpcodeIndex = 0; break;
1038 case MVT::v8i16: OpcodeIndex = 1; break;
1040 case MVT::v4i32: OpcodeIndex = 2; break;
1041 case MVT::v2i64: OpcodeIndex = 3;
1042 assert(NumVecs == 1 && "v2i64 type only supported for VLD1");
1050 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
1053 ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts);
1056 SDValue Pred = getAL(CurDAG);
1057 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1059 if (is64BitVector) {
1060 unsigned Opc = DOpcodes[OpcodeIndex];
1061 const SDValue Ops[] = { MemAddr, Align, Pred, Reg0, Chain };
1062 SDNode *VLd = CurDAG->getMachineNode(Opc, dl, ResTy, MVT::Other, Ops, 5);
1066 SuperReg = SDValue(VLd, 0);
1067 assert(ARM::dsub_7 == ARM::dsub_0+7 && "Unexpected subreg numbering");
1068 for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
1069 SDValue D = CurDAG->getTargetExtractSubreg(ARM::dsub_0+Vec,
1071 ReplaceUses(SDValue(N, Vec), D);
1073 ReplaceUses(SDValue(N, NumVecs), SDValue(VLd, 1));
1078 // Quad registers are directly supported for VLD1 and VLD2,
1079 // loading pairs of D regs.
1080 unsigned Opc = QOpcodes0[OpcodeIndex];
1081 const SDValue Ops[] = { MemAddr, Align, Pred, Reg0, Chain };
1082 SDNode *VLd = CurDAG->getMachineNode(Opc, dl, ResTy, MVT::Other, Ops, 5);
1086 SuperReg = SDValue(VLd, 0);
1087 Chain = SDValue(VLd, 1);
1090 // Otherwise, quad registers are loaded with two separate instructions,
1091 // where one loads the even registers and the other loads the odd registers.
1092 EVT AddrTy = MemAddr.getValueType();
1094 // Load the even subregs.
1095 unsigned Opc = QOpcodes0[OpcodeIndex];
1097 SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, ResTy), 0);
1098 const SDValue OpsA[] = { MemAddr, Align, Reg0, ImplDef, Pred, Reg0, Chain };
1100 CurDAG->getMachineNode(Opc, dl, ResTy, AddrTy, MVT::Other, OpsA, 7);
1101 Chain = SDValue(VLdA, 2);
1103 // Load the odd subregs.
1104 Opc = QOpcodes1[OpcodeIndex];
1105 const SDValue OpsB[] = { SDValue(VLdA, 1), Align, Reg0, SDValue(VLdA, 0),
1106 Pred, Reg0, Chain };
1108 CurDAG->getMachineNode(Opc, dl, ResTy, AddrTy, MVT::Other, OpsB, 7);
1109 SuperReg = SDValue(VLdB, 0);
1110 Chain = SDValue(VLdB, 2);
1113 // Extract out the Q registers.
1114 assert(ARM::qsub_3 == ARM::qsub_0+3 && "Unexpected subreg numbering");
1115 for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
1116 SDValue Q = CurDAG->getTargetExtractSubreg(ARM::qsub_0+Vec,
1118 ReplaceUses(SDValue(N, Vec), Q);
1120 ReplaceUses(SDValue(N, NumVecs), Chain);
1124 SDNode *ARMDAGToDAGISel::SelectVST(SDNode *N, unsigned NumVecs,
1125 unsigned *DOpcodes, unsigned *QOpcodes0,
1126 unsigned *QOpcodes1) {
1127 assert(NumVecs >= 1 && NumVecs <= 4 && "VST NumVecs out-of-range");
1128 DebugLoc dl = N->getDebugLoc();
1130 SDValue MemAddr, Align;
1131 if (!SelectAddrMode6(N, N->getOperand(2), MemAddr, Align))
1134 SDValue Chain = N->getOperand(0);
1135 EVT VT = N->getOperand(3).getValueType();
1136 bool is64BitVector = VT.is64BitVector();
1138 unsigned OpcodeIndex;
1139 switch (VT.getSimpleVT().SimpleTy) {
1140 default: llvm_unreachable("unhandled vst type");
1141 // Double-register operations:
1142 case MVT::v8i8: OpcodeIndex = 0; break;
1143 case MVT::v4i16: OpcodeIndex = 1; break;
1145 case MVT::v2i32: OpcodeIndex = 2; break;
1146 case MVT::v1i64: OpcodeIndex = 3; break;
1147 // Quad-register operations:
1148 case MVT::v16i8: OpcodeIndex = 0; break;
1149 case MVT::v8i16: OpcodeIndex = 1; break;
1151 case MVT::v4i32: OpcodeIndex = 2; break;
1152 case MVT::v2i64: OpcodeIndex = 3;
1153 assert(NumVecs == 1 && "v2i64 type only supported for VST1");
1157 SDValue Pred = getAL(CurDAG);
1158 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1160 SmallVector<SDValue, 7> Ops;
1161 Ops.push_back(MemAddr);
1162 Ops.push_back(Align);
1164 if (is64BitVector) {
1166 Ops.push_back(N->getOperand(3));
1169 SDValue V0 = N->getOperand(0+3);
1170 SDValue V1 = N->getOperand(1+3);
1172 // Form a REG_SEQUENCE to force register allocation.
1174 RegSeq = SDValue(PairDRegs(MVT::v2i64, V0, V1), 0);
1176 SDValue V2 = N->getOperand(2+3);
1177 // If it's a vld3, form a quad D-register and leave the last part as
1179 SDValue V3 = (NumVecs == 3)
1180 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
1181 : N->getOperand(3+3);
1182 RegSeq = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1184 Ops.push_back(RegSeq);
1186 Ops.push_back(Pred);
1187 Ops.push_back(Reg0); // predicate register
1188 Ops.push_back(Chain);
1189 unsigned Opc = DOpcodes[OpcodeIndex];
1190 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 6);
1194 // Quad registers are directly supported for VST1 and VST2.
1195 unsigned Opc = QOpcodes0[OpcodeIndex];
1197 Ops.push_back(N->getOperand(3));
1199 // Form a QQ register.
1200 SDValue Q0 = N->getOperand(3);
1201 SDValue Q1 = N->getOperand(4);
1202 Ops.push_back(SDValue(PairQRegs(MVT::v4i64, Q0, Q1), 0));
1204 Ops.push_back(Pred);
1205 Ops.push_back(Reg0); // predicate register
1206 Ops.push_back(Chain);
1207 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 6);
1210 // Otherwise, quad registers are stored with two separate instructions,
1211 // where one stores the even registers and the other stores the odd registers.
1213 // Form the QQQQ REG_SEQUENCE.
1214 SDValue V0 = N->getOperand(0+3);
1215 SDValue V1 = N->getOperand(1+3);
1216 SDValue V2 = N->getOperand(2+3);
1217 SDValue V3 = (NumVecs == 3)
1218 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
1219 : N->getOperand(3+3);
1220 SDValue RegSeq = SDValue(QuadQRegs(MVT::v8i64, V0, V1, V2, V3), 0);
1222 // Store the even D registers.
1223 Ops.push_back(Reg0); // post-access address offset
1224 Ops.push_back(RegSeq);
1225 Ops.push_back(Pred);
1226 Ops.push_back(Reg0); // predicate register
1227 Ops.push_back(Chain);
1228 unsigned Opc = QOpcodes0[OpcodeIndex];
1229 SDNode *VStA = CurDAG->getMachineNode(Opc, dl, MemAddr.getValueType(),
1230 MVT::Other, Ops.data(), 7);
1231 Chain = SDValue(VStA, 1);
1233 // Store the odd D registers.
1234 Ops[0] = SDValue(VStA, 0); // MemAddr
1236 Opc = QOpcodes1[OpcodeIndex];
1237 SDNode *VStB = CurDAG->getMachineNode(Opc, dl, MemAddr.getValueType(),
1238 MVT::Other, Ops.data(), 7);
1239 Chain = SDValue(VStB, 1);
1240 ReplaceUses(SDValue(N, 0), Chain);
1244 SDNode *ARMDAGToDAGISel::SelectVLDSTLane(SDNode *N, bool IsLoad,
1245 unsigned NumVecs, unsigned *DOpcodes,
1246 unsigned *QOpcodes) {
1247 assert(NumVecs >=2 && NumVecs <= 4 && "VLDSTLane NumVecs out-of-range");
1248 DebugLoc dl = N->getDebugLoc();
1250 SDValue MemAddr, Align;
1251 if (!SelectAddrMode6(N, N->getOperand(2), MemAddr, Align))
1254 SDValue Chain = N->getOperand(0);
1256 cast<ConstantSDNode>(N->getOperand(NumVecs+3))->getZExtValue();
1257 EVT VT = IsLoad ? N->getValueType(0) : N->getOperand(3).getValueType();
1258 bool is64BitVector = VT.is64BitVector();
1260 unsigned OpcodeIndex;
1261 switch (VT.getSimpleVT().SimpleTy) {
1262 default: llvm_unreachable("unhandled vld/vst lane type");
1263 // Double-register operations:
1264 case MVT::v8i8: OpcodeIndex = 0; break;
1265 case MVT::v4i16: OpcodeIndex = 1; break;
1267 case MVT::v2i32: OpcodeIndex = 2; break;
1268 // Quad-register operations:
1269 case MVT::v8i16: OpcodeIndex = 0; break;
1271 case MVT::v4i32: OpcodeIndex = 1; break;
1274 SDValue Pred = getAL(CurDAG);
1275 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1277 SmallVector<SDValue, 7> Ops;
1278 Ops.push_back(MemAddr);
1279 Ops.push_back(Align);
1281 unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
1282 QOpcodes[OpcodeIndex]);
1285 SDValue V0 = N->getOperand(0+3);
1286 SDValue V1 = N->getOperand(1+3);
1289 SuperReg = SDValue(PairDRegs(MVT::v2i64, V0, V1), 0);
1291 SuperReg = SDValue(PairQRegs(MVT::v4i64, V0, V1), 0);
1293 SDValue V2 = N->getOperand(2+3);
1294 SDValue V3 = (NumVecs == 3)
1295 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
1296 : N->getOperand(3+3);
1298 SuperReg = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1300 SuperReg = SDValue(QuadQRegs(MVT::v8i64, V0, V1, V2, V3), 0);
1302 Ops.push_back(SuperReg);
1303 Ops.push_back(getI32Imm(Lane));
1304 Ops.push_back(Pred);
1305 Ops.push_back(Reg0);
1306 Ops.push_back(Chain);
1309 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 7);
1312 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
1315 ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts);
1317 SDNode *VLdLn = CurDAG->getMachineNode(Opc, dl, ResTy, MVT::Other,
1319 SuperReg = SDValue(VLdLn, 0);
1320 Chain = SDValue(VLdLn, 1);
1322 // Extract the subregisters.
1323 assert(ARM::dsub_7 == ARM::dsub_0+7 && "Unexpected subreg numbering");
1324 assert(ARM::qsub_3 == ARM::qsub_0+3 && "Unexpected subreg numbering");
1325 unsigned SubIdx = is64BitVector ? ARM::dsub_0 : ARM::qsub_0;
1326 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1327 ReplaceUses(SDValue(N, Vec),
1328 CurDAG->getTargetExtractSubreg(SubIdx+Vec, dl, VT, SuperReg));
1329 ReplaceUses(SDValue(N, NumVecs), Chain);
1333 SDNode *ARMDAGToDAGISel::SelectVTBL(SDNode *N, bool IsExt, unsigned NumVecs,
1335 assert(NumVecs >= 2 && NumVecs <= 4 && "VTBL NumVecs out-of-range");
1336 DebugLoc dl = N->getDebugLoc();
1337 EVT VT = N->getValueType(0);
1338 unsigned FirstTblReg = IsExt ? 2 : 1;
1340 // Form a REG_SEQUENCE to force register allocation.
1342 SDValue V0 = N->getOperand(FirstTblReg + 0);
1343 SDValue V1 = N->getOperand(FirstTblReg + 1);
1345 RegSeq = SDValue(PairDRegs(MVT::v16i8, V0, V1), 0);
1347 SDValue V2 = N->getOperand(FirstTblReg + 2);
1348 // If it's a vtbl3, form a quad D-register and leave the last part as
1350 SDValue V3 = (NumVecs == 3)
1351 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
1352 : N->getOperand(FirstTblReg + 3);
1353 RegSeq = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1356 SmallVector<SDValue, 6> Ops;
1358 Ops.push_back(N->getOperand(1));
1359 Ops.push_back(RegSeq);
1360 Ops.push_back(N->getOperand(FirstTblReg + NumVecs));
1361 Ops.push_back(getAL(CurDAG)); // predicate
1362 Ops.push_back(CurDAG->getRegister(0, MVT::i32)); // predicate register
1363 return CurDAG->getMachineNode(Opc, dl, VT, Ops.data(), Ops.size());
1366 SDNode *ARMDAGToDAGISel::SelectV6T2BitfieldExtractOp(SDNode *N,
1368 if (!Subtarget->hasV6T2Ops())
1371 unsigned Opc = isSigned ? (Subtarget->isThumb() ? ARM::t2SBFX : ARM::SBFX)
1372 : (Subtarget->isThumb() ? ARM::t2UBFX : ARM::UBFX);
1375 // For unsigned extracts, check for a shift right and mask
1376 unsigned And_imm = 0;
1377 if (N->getOpcode() == ISD::AND) {
1378 if (isOpcWithIntImmediate(N, ISD::AND, And_imm)) {
1380 // The immediate is a mask of the low bits iff imm & (imm+1) == 0
1381 if (And_imm & (And_imm + 1))
1384 unsigned Srl_imm = 0;
1385 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRL,
1387 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
1389 unsigned Width = CountTrailingOnes_32(And_imm);
1390 unsigned LSB = Srl_imm;
1391 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1392 SDValue Ops[] = { N->getOperand(0).getOperand(0),
1393 CurDAG->getTargetConstant(LSB, MVT::i32),
1394 CurDAG->getTargetConstant(Width, MVT::i32),
1395 getAL(CurDAG), Reg0 };
1396 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
1402 // Otherwise, we're looking for a shift of a shift
1403 unsigned Shl_imm = 0;
1404 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SHL, Shl_imm)) {
1405 assert(Shl_imm > 0 && Shl_imm < 32 && "bad amount in shift node!");
1406 unsigned Srl_imm = 0;
1407 if (isInt32Immediate(N->getOperand(1), Srl_imm)) {
1408 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
1409 unsigned Width = 32 - Srl_imm;
1410 int LSB = Srl_imm - Shl_imm;
1413 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1414 SDValue Ops[] = { N->getOperand(0).getOperand(0),
1415 CurDAG->getTargetConstant(LSB, MVT::i32),
1416 CurDAG->getTargetConstant(Width, MVT::i32),
1417 getAL(CurDAG), Reg0 };
1418 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
1424 SDNode *ARMDAGToDAGISel::
1425 SelectT2CMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
1426 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1429 if (SelectT2ShifterOperandReg(N, TrueVal, CPTmp0, CPTmp1)) {
1430 unsigned SOVal = cast<ConstantSDNode>(CPTmp1)->getZExtValue();
1431 unsigned SOShOp = ARM_AM::getSORegShOp(SOVal);
1434 case ARM_AM::lsl: Opc = ARM::t2MOVCClsl; break;
1435 case ARM_AM::lsr: Opc = ARM::t2MOVCClsr; break;
1436 case ARM_AM::asr: Opc = ARM::t2MOVCCasr; break;
1437 case ARM_AM::ror: Opc = ARM::t2MOVCCror; break;
1439 llvm_unreachable("Unknown so_reg opcode!");
1443 CurDAG->getTargetConstant(ARM_AM::getSORegOffset(SOVal), MVT::i32);
1444 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1445 SDValue Ops[] = { FalseVal, CPTmp0, SOShImm, CC, CCR, InFlag };
1446 return CurDAG->SelectNodeTo(N, Opc, MVT::i32,Ops, 6);
1451 SDNode *ARMDAGToDAGISel::
1452 SelectARMCMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
1453 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1457 if (SelectShifterOperandReg(N, TrueVal, CPTmp0, CPTmp1, CPTmp2)) {
1458 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1459 SDValue Ops[] = { FalseVal, CPTmp0, CPTmp1, CPTmp2, CC, CCR, InFlag };
1460 return CurDAG->SelectNodeTo(N, ARM::MOVCCs, MVT::i32, Ops, 7);
1465 SDNode *ARMDAGToDAGISel::
1466 SelectT2CMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
1467 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1468 ConstantSDNode *T = dyn_cast<ConstantSDNode>(TrueVal);
1472 if (Pred_t2_so_imm(TrueVal.getNode())) {
1473 SDValue True = CurDAG->getTargetConstant(T->getZExtValue(), MVT::i32);
1474 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1475 SDValue Ops[] = { FalseVal, True, CC, CCR, InFlag };
1476 return CurDAG->SelectNodeTo(N,
1477 ARM::t2MOVCCi, MVT::i32, Ops, 5);
1482 SDNode *ARMDAGToDAGISel::
1483 SelectARMCMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
1484 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1485 ConstantSDNode *T = dyn_cast<ConstantSDNode>(TrueVal);
1489 if (Pred_so_imm(TrueVal.getNode())) {
1490 SDValue True = CurDAG->getTargetConstant(T->getZExtValue(), MVT::i32);
1491 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1492 SDValue Ops[] = { FalseVal, True, CC, CCR, InFlag };
1493 return CurDAG->SelectNodeTo(N,
1494 ARM::MOVCCi, MVT::i32, Ops, 5);
1499 SDNode *ARMDAGToDAGISel::SelectCMOVOp(SDNode *N) {
1500 EVT VT = N->getValueType(0);
1501 SDValue FalseVal = N->getOperand(0);
1502 SDValue TrueVal = N->getOperand(1);
1503 SDValue CC = N->getOperand(2);
1504 SDValue CCR = N->getOperand(3);
1505 SDValue InFlag = N->getOperand(4);
1506 assert(CC.getOpcode() == ISD::Constant);
1507 assert(CCR.getOpcode() == ISD::Register);
1508 ARMCC::CondCodes CCVal =
1509 (ARMCC::CondCodes)cast<ConstantSDNode>(CC)->getZExtValue();
1511 if (!Subtarget->isThumb1Only() && VT == MVT::i32) {
1512 // Pattern: (ARMcmov:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
1513 // Emits: (MOVCCs:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
1514 // Pattern complexity = 18 cost = 1 size = 0
1518 if (Subtarget->isThumb()) {
1519 SDNode *Res = SelectT2CMOVShiftOp(N, FalseVal, TrueVal,
1520 CCVal, CCR, InFlag);
1522 Res = SelectT2CMOVShiftOp(N, TrueVal, FalseVal,
1523 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1527 SDNode *Res = SelectARMCMOVShiftOp(N, FalseVal, TrueVal,
1528 CCVal, CCR, InFlag);
1530 Res = SelectARMCMOVShiftOp(N, TrueVal, FalseVal,
1531 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1536 // Pattern: (ARMcmov:i32 GPR:i32:$false,
1537 // (imm:i32)<<P:Pred_so_imm>>:$true,
1539 // Emits: (MOVCCi:i32 GPR:i32:$false,
1540 // (so_imm:i32 (imm:i32):$true), (imm:i32):$cc)
1541 // Pattern complexity = 10 cost = 1 size = 0
1542 if (Subtarget->isThumb()) {
1543 SDNode *Res = SelectT2CMOVSoImmOp(N, FalseVal, TrueVal,
1544 CCVal, CCR, InFlag);
1546 Res = SelectT2CMOVSoImmOp(N, TrueVal, FalseVal,
1547 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1551 SDNode *Res = SelectARMCMOVSoImmOp(N, FalseVal, TrueVal,
1552 CCVal, CCR, InFlag);
1554 Res = SelectARMCMOVSoImmOp(N, TrueVal, FalseVal,
1555 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1561 // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1562 // Emits: (MOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1563 // Pattern complexity = 6 cost = 1 size = 0
1565 // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1566 // Emits: (tMOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1567 // Pattern complexity = 6 cost = 11 size = 0
1569 // Also FCPYScc and FCPYDcc.
1570 SDValue Tmp2 = CurDAG->getTargetConstant(CCVal, MVT::i32);
1571 SDValue Ops[] = { FalseVal, TrueVal, Tmp2, CCR, InFlag };
1573 switch (VT.getSimpleVT().SimpleTy) {
1574 default: assert(false && "Illegal conditional move type!");
1577 Opc = Subtarget->isThumb()
1578 ? (Subtarget->hasThumb2() ? ARM::t2MOVCCr : ARM::tMOVCCr_pseudo)
1588 return CurDAG->SelectNodeTo(N, Opc, VT, Ops, 5);
1591 SDNode *ARMDAGToDAGISel::SelectConcatVector(SDNode *N) {
1592 // The only time a CONCAT_VECTORS operation can have legal types is when
1593 // two 64-bit vectors are concatenated to a 128-bit vector.
1594 EVT VT = N->getValueType(0);
1595 if (!VT.is128BitVector() || N->getNumOperands() != 2)
1596 llvm_unreachable("unexpected CONCAT_VECTORS");
1597 DebugLoc dl = N->getDebugLoc();
1598 SDValue V0 = N->getOperand(0);
1599 SDValue V1 = N->getOperand(1);
1600 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
1601 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
1602 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
1603 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
1606 SDNode *ARMDAGToDAGISel::Select(SDNode *N) {
1607 DebugLoc dl = N->getDebugLoc();
1609 if (N->isMachineOpcode())
1610 return NULL; // Already selected.
1612 switch (N->getOpcode()) {
1614 case ISD::Constant: {
1615 unsigned Val = cast<ConstantSDNode>(N)->getZExtValue();
1617 if (Subtarget->hasThumb2())
1618 // Thumb2-aware targets have the MOVT instruction, so all immediates can
1619 // be done with MOV + MOVT, at worst.
1622 if (Subtarget->isThumb()) {
1623 UseCP = (Val > 255 && // MOV
1624 ~Val > 255 && // MOV + MVN
1625 !ARM_AM::isThumbImmShiftedVal(Val)); // MOV + LSL
1627 UseCP = (ARM_AM::getSOImmVal(Val) == -1 && // MOV
1628 ARM_AM::getSOImmVal(~Val) == -1 && // MVN
1629 !ARM_AM::isSOImmTwoPartVal(Val)); // two instrs.
1634 CurDAG->getTargetConstantPool(ConstantInt::get(
1635 Type::getInt32Ty(*CurDAG->getContext()), Val),
1636 TLI.getPointerTy());
1639 if (Subtarget->isThumb1Only()) {
1640 SDValue Pred = getAL(CurDAG);
1641 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1642 SDValue Ops[] = { CPIdx, Pred, PredReg, CurDAG->getEntryNode() };
1643 ResNode = CurDAG->getMachineNode(ARM::tLDRcp, dl, MVT::i32, MVT::Other,
1648 CurDAG->getRegister(0, MVT::i32),
1649 CurDAG->getTargetConstant(0, MVT::i32),
1651 CurDAG->getRegister(0, MVT::i32),
1652 CurDAG->getEntryNode()
1654 ResNode=CurDAG->getMachineNode(ARM::LDRcp, dl, MVT::i32, MVT::Other,
1657 ReplaceUses(SDValue(N, 0), SDValue(ResNode, 0));
1661 // Other cases are autogenerated.
1664 case ISD::FrameIndex: {
1665 // Selects to ADDri FI, 0 which in turn will become ADDri SP, imm.
1666 int FI = cast<FrameIndexSDNode>(N)->getIndex();
1667 SDValue TFI = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
1668 if (Subtarget->isThumb1Only()) {
1669 return CurDAG->SelectNodeTo(N, ARM::tADDrSPi, MVT::i32, TFI,
1670 CurDAG->getTargetConstant(0, MVT::i32));
1672 unsigned Opc = ((Subtarget->isThumb() && Subtarget->hasThumb2()) ?
1673 ARM::t2ADDri : ARM::ADDri);
1674 SDValue Ops[] = { TFI, CurDAG->getTargetConstant(0, MVT::i32),
1675 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1676 CurDAG->getRegister(0, MVT::i32) };
1677 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
1681 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, false))
1685 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, true))
1689 if (Subtarget->isThumb1Only())
1691 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1692 unsigned RHSV = C->getZExtValue();
1694 if (isPowerOf2_32(RHSV-1)) { // 2^n+1?
1695 unsigned ShImm = Log2_32(RHSV-1);
1698 SDValue V = N->getOperand(0);
1699 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
1700 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, MVT::i32);
1701 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1702 if (Subtarget->isThumb()) {
1703 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
1704 return CurDAG->SelectNodeTo(N, ARM::t2ADDrs, MVT::i32, Ops, 6);
1706 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
1707 return CurDAG->SelectNodeTo(N, ARM::ADDrs, MVT::i32, Ops, 7);
1710 if (isPowerOf2_32(RHSV+1)) { // 2^n-1?
1711 unsigned ShImm = Log2_32(RHSV+1);
1714 SDValue V = N->getOperand(0);
1715 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
1716 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, MVT::i32);
1717 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1718 if (Subtarget->isThumb()) {
1719 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
1720 return CurDAG->SelectNodeTo(N, ARM::t2RSBrs, MVT::i32, Ops, 6);
1722 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
1723 return CurDAG->SelectNodeTo(N, ARM::RSBrs, MVT::i32, Ops, 7);
1729 // Check for unsigned bitfield extract
1730 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, false))
1733 // (and (or x, c2), c1) and top 16-bits of c1 and c2 match, lower 16-bits
1734 // of c1 are 0xffff, and lower 16-bit of c2 are 0. That is, the top 16-bits
1735 // are entirely contributed by c2 and lower 16-bits are entirely contributed
1736 // by x. That's equal to (or (and x, 0xffff), (and c1, 0xffff0000)).
1737 // Select it to: "movt x, ((c1 & 0xffff) >> 16)
1738 EVT VT = N->getValueType(0);
1741 unsigned Opc = (Subtarget->isThumb() && Subtarget->hasThumb2())
1743 : (Subtarget->hasV6T2Ops() ? ARM::MOVTi16 : 0);
1746 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
1747 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1750 if (N0.getOpcode() == ISD::OR && N0.getNode()->hasOneUse()) {
1751 SDValue N2 = N0.getOperand(1);
1752 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1755 unsigned N1CVal = N1C->getZExtValue();
1756 unsigned N2CVal = N2C->getZExtValue();
1757 if ((N1CVal & 0xffff0000U) == (N2CVal & 0xffff0000U) &&
1758 (N1CVal & 0xffffU) == 0xffffU &&
1759 (N2CVal & 0xffffU) == 0x0U) {
1760 SDValue Imm16 = CurDAG->getTargetConstant((N2CVal & 0xFFFF0000U) >> 16,
1762 SDValue Ops[] = { N0.getOperand(0), Imm16,
1763 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
1764 return CurDAG->getMachineNode(Opc, dl, VT, Ops, 4);
1769 case ARMISD::VMOVRRD:
1770 return CurDAG->getMachineNode(ARM::VMOVRRD, dl, MVT::i32, MVT::i32,
1771 N->getOperand(0), getAL(CurDAG),
1772 CurDAG->getRegister(0, MVT::i32));
1773 case ISD::UMUL_LOHI: {
1774 if (Subtarget->isThumb1Only())
1776 if (Subtarget->isThumb()) {
1777 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
1778 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1779 CurDAG->getRegister(0, MVT::i32) };
1780 return CurDAG->getMachineNode(ARM::t2UMULL, dl, MVT::i32, MVT::i32,Ops,4);
1782 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
1783 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1784 CurDAG->getRegister(0, MVT::i32) };
1785 return CurDAG->getMachineNode(ARM::UMULL, dl, MVT::i32, MVT::i32, Ops, 5);
1788 case ISD::SMUL_LOHI: {
1789 if (Subtarget->isThumb1Only())
1791 if (Subtarget->isThumb()) {
1792 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
1793 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
1794 return CurDAG->getMachineNode(ARM::t2SMULL, dl, MVT::i32, MVT::i32,Ops,4);
1796 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
1797 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1798 CurDAG->getRegister(0, MVT::i32) };
1799 return CurDAG->getMachineNode(ARM::SMULL, dl, MVT::i32, MVT::i32, Ops, 5);
1803 SDNode *ResNode = 0;
1804 if (Subtarget->isThumb() && Subtarget->hasThumb2())
1805 ResNode = SelectT2IndexedLoad(N);
1807 ResNode = SelectARMIndexedLoad(N);
1810 // Other cases are autogenerated.
1813 case ARMISD::BRCOND: {
1814 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
1815 // Emits: (Bcc:void (bb:Other):$dst, (imm:i32):$cc)
1816 // Pattern complexity = 6 cost = 1 size = 0
1818 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
1819 // Emits: (tBcc:void (bb:Other):$dst, (imm:i32):$cc)
1820 // Pattern complexity = 6 cost = 1 size = 0
1822 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
1823 // Emits: (t2Bcc:void (bb:Other):$dst, (imm:i32):$cc)
1824 // Pattern complexity = 6 cost = 1 size = 0
1826 unsigned Opc = Subtarget->isThumb() ?
1827 ((Subtarget->hasThumb2()) ? ARM::t2Bcc : ARM::tBcc) : ARM::Bcc;
1828 SDValue Chain = N->getOperand(0);
1829 SDValue N1 = N->getOperand(1);
1830 SDValue N2 = N->getOperand(2);
1831 SDValue N3 = N->getOperand(3);
1832 SDValue InFlag = N->getOperand(4);
1833 assert(N1.getOpcode() == ISD::BasicBlock);
1834 assert(N2.getOpcode() == ISD::Constant);
1835 assert(N3.getOpcode() == ISD::Register);
1837 SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
1838 cast<ConstantSDNode>(N2)->getZExtValue()),
1840 SDValue Ops[] = { N1, Tmp2, N3, Chain, InFlag };
1841 SDNode *ResNode = CurDAG->getMachineNode(Opc, dl, MVT::Other,
1843 Chain = SDValue(ResNode, 0);
1844 if (N->getNumValues() == 2) {
1845 InFlag = SDValue(ResNode, 1);
1846 ReplaceUses(SDValue(N, 1), InFlag);
1848 ReplaceUses(SDValue(N, 0),
1849 SDValue(Chain.getNode(), Chain.getResNo()));
1853 return SelectCMOVOp(N);
1854 case ARMISD::CNEG: {
1855 EVT VT = N->getValueType(0);
1856 SDValue N0 = N->getOperand(0);
1857 SDValue N1 = N->getOperand(1);
1858 SDValue N2 = N->getOperand(2);
1859 SDValue N3 = N->getOperand(3);
1860 SDValue InFlag = N->getOperand(4);
1861 assert(N2.getOpcode() == ISD::Constant);
1862 assert(N3.getOpcode() == ISD::Register);
1864 SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
1865 cast<ConstantSDNode>(N2)->getZExtValue()),
1867 SDValue Ops[] = { N0, N1, Tmp2, N3, InFlag };
1869 switch (VT.getSimpleVT().SimpleTy) {
1870 default: assert(false && "Illegal conditional move type!");
1879 return CurDAG->SelectNodeTo(N, Opc, VT, Ops, 5);
1882 case ARMISD::VZIP: {
1884 EVT VT = N->getValueType(0);
1885 switch (VT.getSimpleVT().SimpleTy) {
1886 default: return NULL;
1887 case MVT::v8i8: Opc = ARM::VZIPd8; break;
1888 case MVT::v4i16: Opc = ARM::VZIPd16; break;
1890 case MVT::v2i32: Opc = ARM::VZIPd32; break;
1891 case MVT::v16i8: Opc = ARM::VZIPq8; break;
1892 case MVT::v8i16: Opc = ARM::VZIPq16; break;
1894 case MVT::v4i32: Opc = ARM::VZIPq32; break;
1896 SDValue Pred = getAL(CurDAG);
1897 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1898 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
1899 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
1901 case ARMISD::VUZP: {
1903 EVT VT = N->getValueType(0);
1904 switch (VT.getSimpleVT().SimpleTy) {
1905 default: return NULL;
1906 case MVT::v8i8: Opc = ARM::VUZPd8; break;
1907 case MVT::v4i16: Opc = ARM::VUZPd16; break;
1909 case MVT::v2i32: Opc = ARM::VUZPd32; break;
1910 case MVT::v16i8: Opc = ARM::VUZPq8; break;
1911 case MVT::v8i16: Opc = ARM::VUZPq16; break;
1913 case MVT::v4i32: Opc = ARM::VUZPq32; break;
1915 SDValue Pred = getAL(CurDAG);
1916 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1917 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
1918 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
1920 case ARMISD::VTRN: {
1922 EVT VT = N->getValueType(0);
1923 switch (VT.getSimpleVT().SimpleTy) {
1924 default: return NULL;
1925 case MVT::v8i8: Opc = ARM::VTRNd8; break;
1926 case MVT::v4i16: Opc = ARM::VTRNd16; break;
1928 case MVT::v2i32: Opc = ARM::VTRNd32; break;
1929 case MVT::v16i8: Opc = ARM::VTRNq8; break;
1930 case MVT::v8i16: Opc = ARM::VTRNq16; break;
1932 case MVT::v4i32: Opc = ARM::VTRNq32; break;
1934 SDValue Pred = getAL(CurDAG);
1935 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1936 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
1937 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
1939 case ARMISD::BUILD_VECTOR: {
1940 EVT VecVT = N->getValueType(0);
1941 EVT EltVT = VecVT.getVectorElementType();
1942 unsigned NumElts = VecVT.getVectorNumElements();
1943 if (EltVT.getSimpleVT() == MVT::f64) {
1944 assert(NumElts == 2 && "unexpected type for BUILD_VECTOR");
1945 return PairDRegs(VecVT, N->getOperand(0), N->getOperand(1));
1947 assert(EltVT.getSimpleVT() == MVT::f32 &&
1948 "unexpected type for BUILD_VECTOR");
1950 return PairSRegs(VecVT, N->getOperand(0), N->getOperand(1));
1951 assert(NumElts == 4 && "unexpected type for BUILD_VECTOR");
1952 return QuadSRegs(VecVT, N->getOperand(0), N->getOperand(1),
1953 N->getOperand(2), N->getOperand(3));
1956 case ISD::INTRINSIC_VOID:
1957 case ISD::INTRINSIC_W_CHAIN: {
1958 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
1963 case Intrinsic::arm_neon_vld1: {
1964 unsigned DOpcodes[] = { ARM::VLD1d8, ARM::VLD1d16,
1965 ARM::VLD1d32, ARM::VLD1d64 };
1966 unsigned QOpcodes[] = { ARM::VLD1q8Pseudo, ARM::VLD1q16Pseudo,
1967 ARM::VLD1q32Pseudo, ARM::VLD1q64Pseudo };
1968 return SelectVLD(N, 1, DOpcodes, QOpcodes, 0);
1971 case Intrinsic::arm_neon_vld2: {
1972 unsigned DOpcodes[] = { ARM::VLD2d8Pseudo, ARM::VLD2d16Pseudo,
1973 ARM::VLD2d32Pseudo, ARM::VLD1q64Pseudo };
1974 unsigned QOpcodes[] = { ARM::VLD2q8Pseudo, ARM::VLD2q16Pseudo,
1975 ARM::VLD2q32Pseudo };
1976 return SelectVLD(N, 2, DOpcodes, QOpcodes, 0);
1979 case Intrinsic::arm_neon_vld3: {
1980 unsigned DOpcodes[] = { ARM::VLD3d8Pseudo, ARM::VLD3d16Pseudo,
1981 ARM::VLD3d32Pseudo, ARM::VLD1d64TPseudo };
1982 unsigned QOpcodes0[] = { ARM::VLD3q8Pseudo_UPD,
1983 ARM::VLD3q16Pseudo_UPD,
1984 ARM::VLD3q32Pseudo_UPD };
1985 unsigned QOpcodes1[] = { ARM::VLD3q8oddPseudo_UPD,
1986 ARM::VLD3q16oddPseudo_UPD,
1987 ARM::VLD3q32oddPseudo_UPD };
1988 return SelectVLD(N, 3, DOpcodes, QOpcodes0, QOpcodes1);
1991 case Intrinsic::arm_neon_vld4: {
1992 unsigned DOpcodes[] = { ARM::VLD4d8Pseudo, ARM::VLD4d16Pseudo,
1993 ARM::VLD4d32Pseudo, ARM::VLD1d64QPseudo };
1994 unsigned QOpcodes0[] = { ARM::VLD4q8Pseudo_UPD,
1995 ARM::VLD4q16Pseudo_UPD,
1996 ARM::VLD4q32Pseudo_UPD };
1997 unsigned QOpcodes1[] = { ARM::VLD4q8oddPseudo_UPD,
1998 ARM::VLD4q16oddPseudo_UPD,
1999 ARM::VLD4q32oddPseudo_UPD };
2000 return SelectVLD(N, 4, DOpcodes, QOpcodes0, QOpcodes1);
2003 case Intrinsic::arm_neon_vld2lane: {
2004 unsigned DOpcodes[] = { ARM::VLD2LNd8Pseudo, ARM::VLD2LNd16Pseudo,
2005 ARM::VLD2LNd32Pseudo };
2006 unsigned QOpcodes[] = { ARM::VLD2LNq16Pseudo, ARM::VLD2LNq32Pseudo };
2007 return SelectVLDSTLane(N, true, 2, DOpcodes, QOpcodes);
2010 case Intrinsic::arm_neon_vld3lane: {
2011 unsigned DOpcodes[] = { ARM::VLD3LNd8Pseudo, ARM::VLD3LNd16Pseudo,
2012 ARM::VLD3LNd32Pseudo };
2013 unsigned QOpcodes[] = { ARM::VLD3LNq16Pseudo, ARM::VLD3LNq32Pseudo };
2014 return SelectVLDSTLane(N, true, 3, DOpcodes, QOpcodes);
2017 case Intrinsic::arm_neon_vld4lane: {
2018 unsigned DOpcodes[] = { ARM::VLD4LNd8Pseudo, ARM::VLD4LNd16Pseudo,
2019 ARM::VLD4LNd32Pseudo };
2020 unsigned QOpcodes[] = { ARM::VLD4LNq16Pseudo, ARM::VLD4LNq32Pseudo };
2021 return SelectVLDSTLane(N, true, 4, DOpcodes, QOpcodes);
2024 case Intrinsic::arm_neon_vst1: {
2025 unsigned DOpcodes[] = { ARM::VST1d8, ARM::VST1d16,
2026 ARM::VST1d32, ARM::VST1d64 };
2027 unsigned QOpcodes[] = { ARM::VST1q8Pseudo, ARM::VST1q16Pseudo,
2028 ARM::VST1q32Pseudo, ARM::VST1q64Pseudo };
2029 return SelectVST(N, 1, DOpcodes, QOpcodes, 0);
2032 case Intrinsic::arm_neon_vst2: {
2033 unsigned DOpcodes[] = { ARM::VST2d8Pseudo, ARM::VST2d16Pseudo,
2034 ARM::VST2d32Pseudo, ARM::VST1q64Pseudo };
2035 unsigned QOpcodes[] = { ARM::VST2q8Pseudo, ARM::VST2q16Pseudo,
2036 ARM::VST2q32Pseudo };
2037 return SelectVST(N, 2, DOpcodes, QOpcodes, 0);
2040 case Intrinsic::arm_neon_vst3: {
2041 unsigned DOpcodes[] = { ARM::VST3d8Pseudo, ARM::VST3d16Pseudo,
2042 ARM::VST3d32Pseudo, ARM::VST1d64TPseudo };
2043 unsigned QOpcodes0[] = { ARM::VST3q8Pseudo_UPD,
2044 ARM::VST3q16Pseudo_UPD,
2045 ARM::VST3q32Pseudo_UPD };
2046 unsigned QOpcodes1[] = { ARM::VST3q8oddPseudo_UPD,
2047 ARM::VST3q16oddPseudo_UPD,
2048 ARM::VST3q32oddPseudo_UPD };
2049 return SelectVST(N, 3, DOpcodes, QOpcodes0, QOpcodes1);
2052 case Intrinsic::arm_neon_vst4: {
2053 unsigned DOpcodes[] = { ARM::VST4d8Pseudo, ARM::VST4d16Pseudo,
2054 ARM::VST4d32Pseudo, ARM::VST1d64QPseudo };
2055 unsigned QOpcodes0[] = { ARM::VST4q8Pseudo_UPD,
2056 ARM::VST4q16Pseudo_UPD,
2057 ARM::VST4q32Pseudo_UPD };
2058 unsigned QOpcodes1[] = { ARM::VST4q8oddPseudo_UPD,
2059 ARM::VST4q16oddPseudo_UPD,
2060 ARM::VST4q32oddPseudo_UPD };
2061 return SelectVST(N, 4, DOpcodes, QOpcodes0, QOpcodes1);
2064 case Intrinsic::arm_neon_vst2lane: {
2065 unsigned DOpcodes[] = { ARM::VST2LNd8Pseudo, ARM::VST2LNd16Pseudo,
2066 ARM::VST2LNd32Pseudo };
2067 unsigned QOpcodes[] = { ARM::VST2LNq16Pseudo, ARM::VST2LNq32Pseudo };
2068 return SelectVLDSTLane(N, false, 2, DOpcodes, QOpcodes);
2071 case Intrinsic::arm_neon_vst3lane: {
2072 unsigned DOpcodes[] = { ARM::VST3LNd8Pseudo, ARM::VST3LNd16Pseudo,
2073 ARM::VST3LNd32Pseudo };
2074 unsigned QOpcodes[] = { ARM::VST3LNq16Pseudo, ARM::VST3LNq32Pseudo };
2075 return SelectVLDSTLane(N, false, 3, DOpcodes, QOpcodes);
2078 case Intrinsic::arm_neon_vst4lane: {
2079 unsigned DOpcodes[] = { ARM::VST4LNd8Pseudo, ARM::VST4LNd16Pseudo,
2080 ARM::VST4LNd32Pseudo };
2081 unsigned QOpcodes[] = { ARM::VST4LNq16Pseudo, ARM::VST4LNq32Pseudo };
2082 return SelectVLDSTLane(N, false, 4, DOpcodes, QOpcodes);
2088 case ISD::INTRINSIC_WO_CHAIN: {
2089 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
2094 case Intrinsic::arm_neon_vtbl2:
2095 return SelectVTBL(N, false, 2, ARM::VTBL2Pseudo);
2096 case Intrinsic::arm_neon_vtbl3:
2097 return SelectVTBL(N, false, 3, ARM::VTBL3Pseudo);
2098 case Intrinsic::arm_neon_vtbl4:
2099 return SelectVTBL(N, false, 4, ARM::VTBL4Pseudo);
2101 case Intrinsic::arm_neon_vtbx2:
2102 return SelectVTBL(N, true, 2, ARM::VTBX2Pseudo);
2103 case Intrinsic::arm_neon_vtbx3:
2104 return SelectVTBL(N, true, 3, ARM::VTBX3Pseudo);
2105 case Intrinsic::arm_neon_vtbx4:
2106 return SelectVTBL(N, true, 4, ARM::VTBX4Pseudo);
2111 case ISD::CONCAT_VECTORS:
2112 return SelectConcatVector(N);
2115 return SelectCode(N);
2118 bool ARMDAGToDAGISel::
2119 SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
2120 std::vector<SDValue> &OutOps) {
2121 assert(ConstraintCode == 'm' && "unexpected asm memory constraint");
2122 // Require the address to be in a register. That is safe for all ARM
2123 // variants and it is hard to do anything much smarter without knowing
2124 // how the operand is used.
2125 OutOps.push_back(Op);
2129 /// createARMISelDag - This pass converts a legalized DAG into a
2130 /// ARM-specific DAG, ready for instruction scheduling.
2132 FunctionPass *llvm::createARMISelDag(ARMBaseTargetMachine &TM,
2133 CodeGenOpt::Level OptLevel) {
2134 return new ARMDAGToDAGISel(TM, OptLevel);