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(SDValue N, SDValue &A,
76 SDValue &B, SDValue &C);
77 bool SelectAddrMode2(SDValue N, SDValue &Base,
78 SDValue &Offset, SDValue &Opc);
79 bool SelectAddrMode2Offset(SDNode *Op, SDValue N,
80 SDValue &Offset, SDValue &Opc);
81 bool SelectAddrMode3(SDValue N, SDValue &Base,
82 SDValue &Offset, SDValue &Opc);
83 bool SelectAddrMode3Offset(SDNode *Op, SDValue N,
84 SDValue &Offset, SDValue &Opc);
85 bool SelectAddrMode4(SDValue N, SDValue &Addr, SDValue &Mode);
86 bool SelectAddrMode5(SDValue N, SDValue &Base,
88 bool SelectAddrMode6(SDValue N, SDValue &Addr, SDValue &Align);
90 bool SelectAddrModePC(SDValue N, SDValue &Offset,
93 bool SelectThumbAddrModeRR(SDValue N, SDValue &Base, SDValue &Offset);
94 bool SelectThumbAddrModeRI5(SDValue N, unsigned Scale,
95 SDValue &Base, SDValue &OffImm,
97 bool SelectThumbAddrModeS1(SDValue N, SDValue &Base,
98 SDValue &OffImm, SDValue &Offset);
99 bool SelectThumbAddrModeS2(SDValue N, SDValue &Base,
100 SDValue &OffImm, SDValue &Offset);
101 bool SelectThumbAddrModeS4(SDValue N, SDValue &Base,
102 SDValue &OffImm, SDValue &Offset);
103 bool SelectThumbAddrModeSP(SDValue N, SDValue &Base, SDValue &OffImm);
105 bool SelectT2ShifterOperandReg(SDValue N,
106 SDValue &BaseReg, SDValue &Opc);
107 bool SelectT2AddrModeImm12(SDValue N, SDValue &Base, SDValue &OffImm);
108 bool SelectT2AddrModeImm8(SDValue N, SDValue &Base,
110 bool SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
112 bool SelectT2AddrModeSoReg(SDValue N, SDValue &Base,
113 SDValue &OffReg, SDValue &ShImm);
115 inline bool Pred_so_imm(SDNode *inN) const {
116 ConstantSDNode *N = cast<ConstantSDNode>(inN);
117 return ARM_AM::getSOImmVal(N->getZExtValue()) != -1;
120 inline bool Pred_t2_so_imm(SDNode *inN) const {
121 ConstantSDNode *N = cast<ConstantSDNode>(inN);
122 return ARM_AM::getT2SOImmVal(N->getZExtValue()) != -1;
125 // Include the pieces autogenerated from the target description.
126 #include "ARMGenDAGISel.inc"
129 /// SelectARMIndexedLoad - Indexed (pre/post inc/dec) load matching code for
131 SDNode *SelectARMIndexedLoad(SDNode *N);
132 SDNode *SelectT2IndexedLoad(SDNode *N);
134 /// SelectVLD - Select NEON load intrinsics. NumVecs should be
135 /// 1, 2, 3 or 4. The opcode arrays specify the instructions used for
136 /// loads of D registers and even subregs and odd subregs of Q registers.
137 /// For NumVecs <= 2, QOpcodes1 is not used.
138 SDNode *SelectVLD(SDNode *N, unsigned NumVecs, unsigned *DOpcodes,
139 unsigned *QOpcodes0, unsigned *QOpcodes1);
141 /// SelectVST - Select NEON store intrinsics. NumVecs should
142 /// be 1, 2, 3 or 4. The opcode arrays specify the instructions used for
143 /// stores of D registers and even subregs and odd subregs of Q registers.
144 /// For NumVecs <= 2, QOpcodes1 is not used.
145 SDNode *SelectVST(SDNode *N, unsigned NumVecs, unsigned *DOpcodes,
146 unsigned *QOpcodes0, unsigned *QOpcodes1);
148 /// SelectVLDSTLane - Select NEON load/store lane intrinsics. NumVecs should
149 /// be 2, 3 or 4. The opcode arrays specify the instructions used for
150 /// load/store of D registers and Q registers.
151 SDNode *SelectVLDSTLane(SDNode *N, bool IsLoad, unsigned NumVecs,
152 unsigned *DOpcodes, unsigned *QOpcodes);
154 /// SelectVTBL - Select NEON VTBL and VTBX intrinsics. NumVecs should be 2,
155 /// 3 or 4. These are custom-selected so that a REG_SEQUENCE can be
156 /// generated to force the table registers to be consecutive.
157 SDNode *SelectVTBL(SDNode *N, bool IsExt, unsigned NumVecs, unsigned Opc);
159 /// SelectV6T2BitfieldExtractOp - Select SBFX/UBFX instructions for ARM.
160 SDNode *SelectV6T2BitfieldExtractOp(SDNode *N, bool isSigned);
162 /// SelectCMOVOp - Select CMOV instructions for ARM.
163 SDNode *SelectCMOVOp(SDNode *N);
164 SDNode *SelectT2CMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
165 ARMCC::CondCodes CCVal, SDValue CCR,
167 SDNode *SelectARMCMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
168 ARMCC::CondCodes CCVal, SDValue CCR,
170 SDNode *SelectT2CMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
171 ARMCC::CondCodes CCVal, SDValue CCR,
173 SDNode *SelectARMCMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
174 ARMCC::CondCodes CCVal, SDValue CCR,
177 SDNode *SelectConcatVector(SDNode *N);
179 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
180 /// inline asm expressions.
181 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
183 std::vector<SDValue> &OutOps);
185 // Form pairs of consecutive S, D, or Q registers.
186 SDNode *PairSRegs(EVT VT, SDValue V0, SDValue V1);
187 SDNode *PairDRegs(EVT VT, SDValue V0, SDValue V1);
188 SDNode *PairQRegs(EVT VT, SDValue V0, SDValue V1);
190 // Form sequences of 4 consecutive S, D, or Q registers.
191 SDNode *QuadSRegs(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
192 SDNode *QuadDRegs(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
193 SDNode *QuadQRegs(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
197 /// isInt32Immediate - This method tests to see if the node is a 32-bit constant
198 /// operand. If so Imm will receive the 32-bit value.
199 static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
200 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
201 Imm = cast<ConstantSDNode>(N)->getZExtValue();
207 // isInt32Immediate - This method tests to see if a constant operand.
208 // If so Imm will receive the 32 bit value.
209 static bool isInt32Immediate(SDValue N, unsigned &Imm) {
210 return isInt32Immediate(N.getNode(), Imm);
213 // isOpcWithIntImmediate - This method tests to see if the node is a specific
214 // opcode and that it has a immediate integer right operand.
215 // If so Imm will receive the 32 bit value.
216 static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
217 return N->getOpcode() == Opc &&
218 isInt32Immediate(N->getOperand(1).getNode(), Imm);
222 bool ARMDAGToDAGISel::SelectShifterOperandReg(SDValue N,
226 if (DisableShifterOp)
229 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
231 // Don't match base register only case. That is matched to a separate
232 // lower complexity pattern with explicit register operand.
233 if (ShOpcVal == ARM_AM::no_shift) return false;
235 BaseReg = N.getOperand(0);
236 unsigned ShImmVal = 0;
237 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
238 ShReg = CurDAG->getRegister(0, MVT::i32);
239 ShImmVal = RHS->getZExtValue() & 31;
241 ShReg = N.getOperand(1);
243 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
248 bool ARMDAGToDAGISel::SelectAddrMode2(SDValue N,
249 SDValue &Base, SDValue &Offset,
251 if (N.getOpcode() == ISD::MUL) {
252 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
253 // X * [3,5,9] -> X + X * [2,4,8] etc.
254 int RHSC = (int)RHS->getZExtValue();
257 ARM_AM::AddrOpc AddSub = ARM_AM::add;
259 AddSub = ARM_AM::sub;
262 if (isPowerOf2_32(RHSC)) {
263 unsigned ShAmt = Log2_32(RHSC);
264 Base = Offset = N.getOperand(0);
265 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt,
274 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB) {
276 if (N.getOpcode() == ISD::FrameIndex) {
277 int FI = cast<FrameIndexSDNode>(N)->getIndex();
278 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
279 } else if (N.getOpcode() == ARMISD::Wrapper &&
280 !(Subtarget->useMovt() &&
281 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
282 Base = N.getOperand(0);
284 Offset = CurDAG->getRegister(0, MVT::i32);
285 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::add, 0,
291 // Match simple R +/- imm12 operands.
292 if (N.getOpcode() == ISD::ADD)
293 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
294 int RHSC = (int)RHS->getZExtValue();
295 if ((RHSC >= 0 && RHSC < 0x1000) ||
296 (RHSC < 0 && RHSC > -0x1000)) { // 12 bits.
297 Base = N.getOperand(0);
298 if (Base.getOpcode() == ISD::FrameIndex) {
299 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
300 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
302 Offset = CurDAG->getRegister(0, MVT::i32);
304 ARM_AM::AddrOpc AddSub = ARM_AM::add;
306 AddSub = ARM_AM::sub;
309 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, RHSC,
316 // Otherwise this is R +/- [possibly shifted] R.
317 ARM_AM::AddrOpc AddSub = N.getOpcode() == ISD::ADD ? ARM_AM::add:ARM_AM::sub;
318 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(1));
321 Base = N.getOperand(0);
322 Offset = N.getOperand(1);
324 if (ShOpcVal != ARM_AM::no_shift) {
325 // Check to see if the RHS of the shift is a constant, if not, we can't fold
327 if (ConstantSDNode *Sh =
328 dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) {
329 ShAmt = Sh->getZExtValue();
330 Offset = N.getOperand(1).getOperand(0);
332 ShOpcVal = ARM_AM::no_shift;
336 // Try matching (R shl C) + (R).
337 if (N.getOpcode() == ISD::ADD && ShOpcVal == ARM_AM::no_shift) {
338 ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0));
339 if (ShOpcVal != ARM_AM::no_shift) {
340 // Check to see if the RHS of the shift is a constant, if not, we can't
342 if (ConstantSDNode *Sh =
343 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
344 ShAmt = Sh->getZExtValue();
345 Offset = N.getOperand(0).getOperand(0);
346 Base = N.getOperand(1);
348 ShOpcVal = ARM_AM::no_shift;
353 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
358 bool ARMDAGToDAGISel::SelectAddrMode2Offset(SDNode *Op, SDValue N,
359 SDValue &Offset, SDValue &Opc) {
360 unsigned Opcode = Op->getOpcode();
361 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
362 ? cast<LoadSDNode>(Op)->getAddressingMode()
363 : cast<StoreSDNode>(Op)->getAddressingMode();
364 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
365 ? ARM_AM::add : ARM_AM::sub;
366 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
367 int Val = (int)C->getZExtValue();
368 if (Val >= 0 && Val < 0x1000) { // 12 bits.
369 Offset = CurDAG->getRegister(0, MVT::i32);
370 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, Val,
378 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
380 if (ShOpcVal != ARM_AM::no_shift) {
381 // Check to see if the RHS of the shift is a constant, if not, we can't fold
383 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
384 ShAmt = Sh->getZExtValue();
385 Offset = N.getOperand(0);
387 ShOpcVal = ARM_AM::no_shift;
391 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
397 bool ARMDAGToDAGISel::SelectAddrMode3(SDValue N,
398 SDValue &Base, SDValue &Offset,
400 if (N.getOpcode() == ISD::SUB) {
401 // X - C is canonicalize to X + -C, no need to handle it here.
402 Base = N.getOperand(0);
403 Offset = N.getOperand(1);
404 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::sub, 0),MVT::i32);
408 if (N.getOpcode() != ISD::ADD) {
410 if (N.getOpcode() == ISD::FrameIndex) {
411 int FI = cast<FrameIndexSDNode>(N)->getIndex();
412 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
414 Offset = CurDAG->getRegister(0, MVT::i32);
415 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0),MVT::i32);
419 // If the RHS is +/- imm8, fold into addr mode.
420 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
421 int RHSC = (int)RHS->getZExtValue();
422 if ((RHSC >= 0 && RHSC < 256) ||
423 (RHSC < 0 && RHSC > -256)) { // note -256 itself isn't allowed.
424 Base = N.getOperand(0);
425 if (Base.getOpcode() == ISD::FrameIndex) {
426 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
427 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
429 Offset = CurDAG->getRegister(0, MVT::i32);
431 ARM_AM::AddrOpc AddSub = ARM_AM::add;
433 AddSub = ARM_AM::sub;
436 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, RHSC),MVT::i32);
441 Base = N.getOperand(0);
442 Offset = N.getOperand(1);
443 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0), MVT::i32);
447 bool ARMDAGToDAGISel::SelectAddrMode3Offset(SDNode *Op, SDValue N,
448 SDValue &Offset, SDValue &Opc) {
449 unsigned Opcode = Op->getOpcode();
450 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
451 ? cast<LoadSDNode>(Op)->getAddressingMode()
452 : cast<StoreSDNode>(Op)->getAddressingMode();
453 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
454 ? ARM_AM::add : ARM_AM::sub;
455 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
456 int Val = (int)C->getZExtValue();
457 if (Val >= 0 && Val < 256) {
458 Offset = CurDAG->getRegister(0, MVT::i32);
459 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, Val), MVT::i32);
465 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, 0), MVT::i32);
469 bool ARMDAGToDAGISel::SelectAddrMode4(SDValue N, SDValue &Addr, SDValue &Mode) {
471 Mode = CurDAG->getTargetConstant(ARM_AM::getAM4ModeImm(ARM_AM::ia), MVT::i32);
475 bool ARMDAGToDAGISel::SelectAddrMode5(SDValue N,
476 SDValue &Base, SDValue &Offset) {
477 if (N.getOpcode() != ISD::ADD) {
479 if (N.getOpcode() == ISD::FrameIndex) {
480 int FI = cast<FrameIndexSDNode>(N)->getIndex();
481 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
482 } else if (N.getOpcode() == ARMISD::Wrapper &&
483 !(Subtarget->useMovt() &&
484 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
485 Base = N.getOperand(0);
487 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
492 // If the RHS is +/- imm8, fold into addr mode.
493 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
494 int RHSC = (int)RHS->getZExtValue();
495 if ((RHSC & 3) == 0) { // The constant is implicitly multiplied by 4.
497 if ((RHSC >= 0 && RHSC < 256) ||
498 (RHSC < 0 && RHSC > -256)) { // note -256 itself isn't allowed.
499 Base = N.getOperand(0);
500 if (Base.getOpcode() == ISD::FrameIndex) {
501 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
502 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
505 ARM_AM::AddrOpc AddSub = ARM_AM::add;
507 AddSub = ARM_AM::sub;
510 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(AddSub, RHSC),
518 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
523 bool ARMDAGToDAGISel::SelectAddrMode6(SDValue N, SDValue &Addr, SDValue &Align){
525 // Default to no alignment.
526 Align = CurDAG->getTargetConstant(0, MVT::i32);
530 bool ARMDAGToDAGISel::SelectAddrModePC(SDValue N,
531 SDValue &Offset, SDValue &Label) {
532 if (N.getOpcode() == ARMISD::PIC_ADD && N.hasOneUse()) {
533 Offset = N.getOperand(0);
534 SDValue N1 = N.getOperand(1);
535 Label = CurDAG->getTargetConstant(cast<ConstantSDNode>(N1)->getZExtValue(),
542 bool ARMDAGToDAGISel::SelectThumbAddrModeRR(SDValue N,
543 SDValue &Base, SDValue &Offset){
544 // FIXME dl should come from the parent load or store, not the address
545 if (N.getOpcode() != ISD::ADD) {
546 ConstantSDNode *NC = dyn_cast<ConstantSDNode>(N);
547 if (!NC || !NC->isNullValue())
554 Base = N.getOperand(0);
555 Offset = N.getOperand(1);
560 ARMDAGToDAGISel::SelectThumbAddrModeRI5(SDValue N,
561 unsigned Scale, SDValue &Base,
562 SDValue &OffImm, SDValue &Offset) {
564 SDValue TmpBase, TmpOffImm;
565 if (SelectThumbAddrModeSP(N, TmpBase, TmpOffImm))
566 return false; // We want to select tLDRspi / tSTRspi instead.
567 if (N.getOpcode() == ARMISD::Wrapper &&
568 N.getOperand(0).getOpcode() == ISD::TargetConstantPool)
569 return false; // We want to select tLDRpci instead.
572 if (N.getOpcode() != ISD::ADD) {
573 if (N.getOpcode() == ARMISD::Wrapper &&
574 !(Subtarget->useMovt() &&
575 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
576 Base = N.getOperand(0);
580 Offset = CurDAG->getRegister(0, MVT::i32);
581 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
585 // Thumb does not have [sp, r] address mode.
586 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
587 RegisterSDNode *RHSR = dyn_cast<RegisterSDNode>(N.getOperand(1));
588 if ((LHSR && LHSR->getReg() == ARM::SP) ||
589 (RHSR && RHSR->getReg() == ARM::SP)) {
591 Offset = CurDAG->getRegister(0, MVT::i32);
592 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
596 // If the RHS is + imm5 * scale, fold into addr mode.
597 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
598 int RHSC = (int)RHS->getZExtValue();
599 if ((RHSC & (Scale-1)) == 0) { // The constant is implicitly multiplied.
601 if (RHSC >= 0 && RHSC < 32) {
602 Base = N.getOperand(0);
603 Offset = CurDAG->getRegister(0, MVT::i32);
604 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
610 Base = N.getOperand(0);
611 Offset = N.getOperand(1);
612 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
616 bool ARMDAGToDAGISel::SelectThumbAddrModeS1(SDValue N,
617 SDValue &Base, SDValue &OffImm,
619 return SelectThumbAddrModeRI5(N, 1, Base, OffImm, Offset);
622 bool ARMDAGToDAGISel::SelectThumbAddrModeS2(SDValue N,
623 SDValue &Base, SDValue &OffImm,
625 return SelectThumbAddrModeRI5(N, 2, Base, OffImm, Offset);
628 bool ARMDAGToDAGISel::SelectThumbAddrModeS4(SDValue N,
629 SDValue &Base, SDValue &OffImm,
631 return SelectThumbAddrModeRI5(N, 4, Base, OffImm, Offset);
634 bool ARMDAGToDAGISel::SelectThumbAddrModeSP(SDValue N,
635 SDValue &Base, SDValue &OffImm) {
636 if (N.getOpcode() == ISD::FrameIndex) {
637 int FI = cast<FrameIndexSDNode>(N)->getIndex();
638 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
639 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
643 if (N.getOpcode() != ISD::ADD)
646 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
647 if (N.getOperand(0).getOpcode() == ISD::FrameIndex ||
648 (LHSR && LHSR->getReg() == ARM::SP)) {
649 // If the RHS is + imm8 * scale, fold into addr mode.
650 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
651 int RHSC = (int)RHS->getZExtValue();
652 if ((RHSC & 3) == 0) { // The constant is implicitly multiplied.
654 if (RHSC >= 0 && RHSC < 256) {
655 Base = N.getOperand(0);
656 if (Base.getOpcode() == ISD::FrameIndex) {
657 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
658 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
660 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
670 bool ARMDAGToDAGISel::SelectT2ShifterOperandReg(SDValue N, SDValue &BaseReg,
672 if (DisableShifterOp)
675 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
677 // Don't match base register only case. That is matched to a separate
678 // lower complexity pattern with explicit register operand.
679 if (ShOpcVal == ARM_AM::no_shift) return false;
681 BaseReg = N.getOperand(0);
682 unsigned ShImmVal = 0;
683 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
684 ShImmVal = RHS->getZExtValue() & 31;
685 Opc = getI32Imm(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal));
692 bool ARMDAGToDAGISel::SelectT2AddrModeImm12(SDValue N,
693 SDValue &Base, SDValue &OffImm) {
694 // Match simple R + imm12 operands.
697 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB) {
698 if (N.getOpcode() == ISD::FrameIndex) {
699 // Match frame index...
700 int FI = cast<FrameIndexSDNode>(N)->getIndex();
701 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
702 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
704 } else if (N.getOpcode() == ARMISD::Wrapper &&
705 !(Subtarget->useMovt() &&
706 N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
707 Base = N.getOperand(0);
708 if (Base.getOpcode() == ISD::TargetConstantPool)
709 return false; // We want to select t2LDRpci instead.
712 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
716 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
717 if (SelectT2AddrModeImm8(N, Base, OffImm))
718 // Let t2LDRi8 handle (R - imm8).
721 int RHSC = (int)RHS->getZExtValue();
722 if (N.getOpcode() == ISD::SUB)
725 if (RHSC >= 0 && RHSC < 0x1000) { // 12 bits (unsigned)
726 Base = N.getOperand(0);
727 if (Base.getOpcode() == ISD::FrameIndex) {
728 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
729 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
731 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
738 OffImm = CurDAG->getTargetConstant(0, MVT::i32);
742 bool ARMDAGToDAGISel::SelectT2AddrModeImm8(SDValue N,
743 SDValue &Base, SDValue &OffImm) {
744 // Match simple R - imm8 operands.
745 if (N.getOpcode() == ISD::ADD || N.getOpcode() == ISD::SUB) {
746 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
747 int RHSC = (int)RHS->getSExtValue();
748 if (N.getOpcode() == ISD::SUB)
751 if ((RHSC >= -255) && (RHSC < 0)) { // 8 bits (always negative)
752 Base = N.getOperand(0);
753 if (Base.getOpcode() == ISD::FrameIndex) {
754 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
755 Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
757 OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
766 bool ARMDAGToDAGISel::SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
768 unsigned Opcode = Op->getOpcode();
769 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
770 ? cast<LoadSDNode>(Op)->getAddressingMode()
771 : cast<StoreSDNode>(Op)->getAddressingMode();
772 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N)) {
773 int RHSC = (int)RHS->getZExtValue();
774 if (RHSC >= 0 && RHSC < 0x100) { // 8 bits.
775 OffImm = ((AM == ISD::PRE_INC) || (AM == ISD::POST_INC))
776 ? CurDAG->getTargetConstant(RHSC, MVT::i32)
777 : CurDAG->getTargetConstant(-RHSC, MVT::i32);
785 bool ARMDAGToDAGISel::SelectT2AddrModeSoReg(SDValue N,
787 SDValue &OffReg, SDValue &ShImm) {
788 // (R - imm8) should be handled by t2LDRi8. The rest are handled by t2LDRi12.
789 if (N.getOpcode() != ISD::ADD)
792 // Leave (R + imm12) for t2LDRi12, (R - imm8) for t2LDRi8.
793 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
794 int RHSC = (int)RHS->getZExtValue();
795 if (RHSC >= 0 && RHSC < 0x1000) // 12 bits (unsigned)
797 else if (RHSC < 0 && RHSC >= -255) // 8 bits
801 // Look for (R + R) or (R + (R << [1,2,3])).
803 Base = N.getOperand(0);
804 OffReg = N.getOperand(1);
806 // Swap if it is ((R << c) + R).
807 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(OffReg);
808 if (ShOpcVal != ARM_AM::lsl) {
809 ShOpcVal = ARM_AM::getShiftOpcForNode(Base);
810 if (ShOpcVal == ARM_AM::lsl)
811 std::swap(Base, OffReg);
814 if (ShOpcVal == ARM_AM::lsl) {
815 // Check to see if the RHS of the shift is a constant, if not, we can't fold
817 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(OffReg.getOperand(1))) {
818 ShAmt = Sh->getZExtValue();
821 ShOpcVal = ARM_AM::no_shift;
823 OffReg = OffReg.getOperand(0);
825 ShOpcVal = ARM_AM::no_shift;
829 ShImm = CurDAG->getTargetConstant(ShAmt, MVT::i32);
834 //===--------------------------------------------------------------------===//
836 /// getAL - Returns a ARMCC::AL immediate node.
837 static inline SDValue getAL(SelectionDAG *CurDAG) {
838 return CurDAG->getTargetConstant((uint64_t)ARMCC::AL, MVT::i32);
841 SDNode *ARMDAGToDAGISel::SelectARMIndexedLoad(SDNode *N) {
842 LoadSDNode *LD = cast<LoadSDNode>(N);
843 ISD::MemIndexedMode AM = LD->getAddressingMode();
844 if (AM == ISD::UNINDEXED)
847 EVT LoadedVT = LD->getMemoryVT();
848 SDValue Offset, AMOpc;
849 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
852 if (LoadedVT == MVT::i32 &&
853 SelectAddrMode2Offset(N, LD->getOffset(), Offset, AMOpc)) {
854 Opcode = isPre ? ARM::LDR_PRE : ARM::LDR_POST;
856 } else if (LoadedVT == MVT::i16 &&
857 SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
859 Opcode = (LD->getExtensionType() == ISD::SEXTLOAD)
860 ? (isPre ? ARM::LDRSH_PRE : ARM::LDRSH_POST)
861 : (isPre ? ARM::LDRH_PRE : ARM::LDRH_POST);
862 } else if (LoadedVT == MVT::i8 || LoadedVT == MVT::i1) {
863 if (LD->getExtensionType() == ISD::SEXTLOAD) {
864 if (SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
866 Opcode = isPre ? ARM::LDRSB_PRE : ARM::LDRSB_POST;
869 if (SelectAddrMode2Offset(N, LD->getOffset(), Offset, AMOpc)) {
871 Opcode = isPre ? ARM::LDRB_PRE : ARM::LDRB_POST;
877 SDValue Chain = LD->getChain();
878 SDValue Base = LD->getBasePtr();
879 SDValue Ops[]= { Base, Offset, AMOpc, getAL(CurDAG),
880 CurDAG->getRegister(0, MVT::i32), Chain };
881 return CurDAG->getMachineNode(Opcode, N->getDebugLoc(), MVT::i32, MVT::i32,
888 SDNode *ARMDAGToDAGISel::SelectT2IndexedLoad(SDNode *N) {
889 LoadSDNode *LD = cast<LoadSDNode>(N);
890 ISD::MemIndexedMode AM = LD->getAddressingMode();
891 if (AM == ISD::UNINDEXED)
894 EVT LoadedVT = LD->getMemoryVT();
895 bool isSExtLd = LD->getExtensionType() == ISD::SEXTLOAD;
897 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
900 if (SelectT2AddrModeImm8Offset(N, LD->getOffset(), Offset)) {
901 switch (LoadedVT.getSimpleVT().SimpleTy) {
903 Opcode = isPre ? ARM::t2LDR_PRE : ARM::t2LDR_POST;
907 Opcode = isPre ? ARM::t2LDRSH_PRE : ARM::t2LDRSH_POST;
909 Opcode = isPre ? ARM::t2LDRH_PRE : ARM::t2LDRH_POST;
914 Opcode = isPre ? ARM::t2LDRSB_PRE : ARM::t2LDRSB_POST;
916 Opcode = isPre ? ARM::t2LDRB_PRE : ARM::t2LDRB_POST;
925 SDValue Chain = LD->getChain();
926 SDValue Base = LD->getBasePtr();
927 SDValue Ops[]= { Base, Offset, getAL(CurDAG),
928 CurDAG->getRegister(0, MVT::i32), Chain };
929 return CurDAG->getMachineNode(Opcode, N->getDebugLoc(), MVT::i32, MVT::i32,
936 /// PairSRegs - Form a D register from a pair of S registers.
938 SDNode *ARMDAGToDAGISel::PairSRegs(EVT VT, SDValue V0, SDValue V1) {
939 DebugLoc dl = V0.getNode()->getDebugLoc();
940 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, MVT::i32);
941 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, MVT::i32);
942 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
943 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
946 /// PairDRegs - Form a quad register from a pair of D registers.
948 SDNode *ARMDAGToDAGISel::PairDRegs(EVT VT, SDValue V0, SDValue V1) {
949 DebugLoc dl = V0.getNode()->getDebugLoc();
950 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
951 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
952 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
953 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
956 /// PairQRegs - Form 4 consecutive D registers from a pair of Q registers.
958 SDNode *ARMDAGToDAGISel::PairQRegs(EVT VT, SDValue V0, SDValue V1) {
959 DebugLoc dl = V0.getNode()->getDebugLoc();
960 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, MVT::i32);
961 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, MVT::i32);
962 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
963 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
966 /// QuadSRegs - Form 4 consecutive S registers.
968 SDNode *ARMDAGToDAGISel::QuadSRegs(EVT VT, SDValue V0, SDValue V1,
969 SDValue V2, SDValue V3) {
970 DebugLoc dl = V0.getNode()->getDebugLoc();
971 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, MVT::i32);
972 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, MVT::i32);
973 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::ssub_2, MVT::i32);
974 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::ssub_3, MVT::i32);
975 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1, V2, SubReg2, V3, SubReg3 };
976 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 8);
979 /// QuadDRegs - Form 4 consecutive D registers.
981 SDNode *ARMDAGToDAGISel::QuadDRegs(EVT VT, SDValue V0, SDValue V1,
982 SDValue V2, SDValue V3) {
983 DebugLoc dl = V0.getNode()->getDebugLoc();
984 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
985 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
986 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::dsub_2, MVT::i32);
987 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::dsub_3, MVT::i32);
988 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1, V2, SubReg2, V3, SubReg3 };
989 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 8);
992 /// QuadQRegs - Form 4 consecutive Q registers.
994 SDNode *ARMDAGToDAGISel::QuadQRegs(EVT VT, SDValue V0, SDValue V1,
995 SDValue V2, SDValue V3) {
996 DebugLoc dl = V0.getNode()->getDebugLoc();
997 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, MVT::i32);
998 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, MVT::i32);
999 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::qsub_2, MVT::i32);
1000 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::qsub_3, MVT::i32);
1001 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1, V2, SubReg2, V3, SubReg3 };
1002 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 8);
1005 /// GetVLDSTAlign - Get the alignment (in bytes) for the alignment operand
1006 /// of a NEON VLD or VST instruction. The supported values depend on the
1007 /// number of registers being loaded.
1008 static unsigned GetVLDSTAlign(SDNode *N, unsigned NumVecs, bool is64BitVector) {
1009 unsigned NumRegs = NumVecs;
1010 if (!is64BitVector && NumVecs < 3)
1013 unsigned Alignment = cast<MemIntrinsicSDNode>(N)->getAlignment();
1014 if (Alignment >= 32 && NumRegs == 4)
1016 if (Alignment >= 16 && (NumRegs == 2 || NumRegs == 4))
1023 SDNode *ARMDAGToDAGISel::SelectVLD(SDNode *N, unsigned NumVecs,
1024 unsigned *DOpcodes, unsigned *QOpcodes0,
1025 unsigned *QOpcodes1) {
1026 assert(NumVecs >= 1 && NumVecs <= 4 && "VLD NumVecs out-of-range");
1027 DebugLoc dl = N->getDebugLoc();
1029 SDValue MemAddr, Align;
1030 if (!SelectAddrMode6(N->getOperand(2), MemAddr, Align))
1033 SDValue Chain = N->getOperand(0);
1034 EVT VT = N->getValueType(0);
1035 bool is64BitVector = VT.is64BitVector();
1037 unsigned Alignment = GetVLDSTAlign(N, NumVecs, is64BitVector);
1038 Align = CurDAG->getTargetConstant(Alignment, MVT::i32);
1040 unsigned OpcodeIndex;
1041 switch (VT.getSimpleVT().SimpleTy) {
1042 default: llvm_unreachable("unhandled vld type");
1043 // Double-register operations:
1044 case MVT::v8i8: OpcodeIndex = 0; break;
1045 case MVT::v4i16: OpcodeIndex = 1; break;
1047 case MVT::v2i32: OpcodeIndex = 2; break;
1048 case MVT::v1i64: OpcodeIndex = 3; break;
1049 // Quad-register operations:
1050 case MVT::v16i8: OpcodeIndex = 0; break;
1051 case MVT::v8i16: OpcodeIndex = 1; break;
1053 case MVT::v4i32: OpcodeIndex = 2; break;
1054 case MVT::v2i64: OpcodeIndex = 3;
1055 assert(NumVecs == 1 && "v2i64 type only supported for VLD1");
1063 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
1066 ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts);
1069 SDValue Pred = getAL(CurDAG);
1070 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1072 if (is64BitVector) {
1073 unsigned Opc = DOpcodes[OpcodeIndex];
1074 const SDValue Ops[] = { MemAddr, Align, Pred, Reg0, Chain };
1075 SDNode *VLd = CurDAG->getMachineNode(Opc, dl, ResTy, MVT::Other, Ops, 5);
1079 SuperReg = SDValue(VLd, 0);
1080 assert(ARM::dsub_7 == ARM::dsub_0+7 && "Unexpected subreg numbering");
1081 for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
1082 SDValue D = CurDAG->getTargetExtractSubreg(ARM::dsub_0+Vec,
1084 ReplaceUses(SDValue(N, Vec), D);
1086 ReplaceUses(SDValue(N, NumVecs), SDValue(VLd, 1));
1091 // Quad registers are directly supported for VLD1 and VLD2,
1092 // loading pairs of D regs.
1093 unsigned Opc = QOpcodes0[OpcodeIndex];
1094 const SDValue Ops[] = { MemAddr, Align, Pred, Reg0, Chain };
1095 SDNode *VLd = CurDAG->getMachineNode(Opc, dl, ResTy, MVT::Other, Ops, 5);
1099 SuperReg = SDValue(VLd, 0);
1100 Chain = SDValue(VLd, 1);
1103 // Otherwise, quad registers are loaded with two separate instructions,
1104 // where one loads the even registers and the other loads the odd registers.
1105 EVT AddrTy = MemAddr.getValueType();
1107 // Load the even subregs.
1108 unsigned Opc = QOpcodes0[OpcodeIndex];
1110 SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, ResTy), 0);
1111 const SDValue OpsA[] = { MemAddr, Align, Reg0, ImplDef, Pred, Reg0, Chain };
1113 CurDAG->getMachineNode(Opc, dl, ResTy, AddrTy, MVT::Other, OpsA, 7);
1114 Chain = SDValue(VLdA, 2);
1116 // Load the odd subregs.
1117 Opc = QOpcodes1[OpcodeIndex];
1118 const SDValue OpsB[] = { SDValue(VLdA, 1), Align, Reg0, SDValue(VLdA, 0),
1119 Pred, Reg0, Chain };
1121 CurDAG->getMachineNode(Opc, dl, ResTy, AddrTy, MVT::Other, OpsB, 7);
1122 SuperReg = SDValue(VLdB, 0);
1123 Chain = SDValue(VLdB, 2);
1126 // Extract out the Q registers.
1127 assert(ARM::qsub_3 == ARM::qsub_0+3 && "Unexpected subreg numbering");
1128 for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
1129 SDValue Q = CurDAG->getTargetExtractSubreg(ARM::qsub_0+Vec,
1131 ReplaceUses(SDValue(N, Vec), Q);
1133 ReplaceUses(SDValue(N, NumVecs), Chain);
1137 SDNode *ARMDAGToDAGISel::SelectVST(SDNode *N, unsigned NumVecs,
1138 unsigned *DOpcodes, unsigned *QOpcodes0,
1139 unsigned *QOpcodes1) {
1140 assert(NumVecs >= 1 && NumVecs <= 4 && "VST NumVecs out-of-range");
1141 DebugLoc dl = N->getDebugLoc();
1143 SDValue MemAddr, Align;
1144 if (!SelectAddrMode6(N->getOperand(2), MemAddr, Align))
1147 SDValue Chain = N->getOperand(0);
1148 EVT VT = N->getOperand(3).getValueType();
1149 bool is64BitVector = VT.is64BitVector();
1151 unsigned Alignment = GetVLDSTAlign(N, NumVecs, is64BitVector);
1152 Align = CurDAG->getTargetConstant(Alignment, MVT::i32);
1154 unsigned OpcodeIndex;
1155 switch (VT.getSimpleVT().SimpleTy) {
1156 default: llvm_unreachable("unhandled vst type");
1157 // Double-register operations:
1158 case MVT::v8i8: OpcodeIndex = 0; break;
1159 case MVT::v4i16: OpcodeIndex = 1; break;
1161 case MVT::v2i32: OpcodeIndex = 2; break;
1162 case MVT::v1i64: OpcodeIndex = 3; break;
1163 // Quad-register operations:
1164 case MVT::v16i8: OpcodeIndex = 0; break;
1165 case MVT::v8i16: OpcodeIndex = 1; break;
1167 case MVT::v4i32: OpcodeIndex = 2; break;
1168 case MVT::v2i64: OpcodeIndex = 3;
1169 assert(NumVecs == 1 && "v2i64 type only supported for VST1");
1173 SDValue Pred = getAL(CurDAG);
1174 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1176 SmallVector<SDValue, 7> Ops;
1177 Ops.push_back(MemAddr);
1178 Ops.push_back(Align);
1180 if (is64BitVector) {
1182 Ops.push_back(N->getOperand(3));
1185 SDValue V0 = N->getOperand(0+3);
1186 SDValue V1 = N->getOperand(1+3);
1188 // Form a REG_SEQUENCE to force register allocation.
1190 RegSeq = SDValue(PairDRegs(MVT::v2i64, V0, V1), 0);
1192 SDValue V2 = N->getOperand(2+3);
1193 // If it's a vld3, form a quad D-register and leave the last part as
1195 SDValue V3 = (NumVecs == 3)
1196 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
1197 : N->getOperand(3+3);
1198 RegSeq = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1200 Ops.push_back(RegSeq);
1202 Ops.push_back(Pred);
1203 Ops.push_back(Reg0); // predicate register
1204 Ops.push_back(Chain);
1205 unsigned Opc = DOpcodes[OpcodeIndex];
1206 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 6);
1210 // Quad registers are directly supported for VST1 and VST2.
1211 unsigned Opc = QOpcodes0[OpcodeIndex];
1213 Ops.push_back(N->getOperand(3));
1215 // Form a QQ register.
1216 SDValue Q0 = N->getOperand(3);
1217 SDValue Q1 = N->getOperand(4);
1218 Ops.push_back(SDValue(PairQRegs(MVT::v4i64, Q0, Q1), 0));
1220 Ops.push_back(Pred);
1221 Ops.push_back(Reg0); // predicate register
1222 Ops.push_back(Chain);
1223 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 6);
1226 // Otherwise, quad registers are stored with two separate instructions,
1227 // where one stores the even registers and the other stores the odd registers.
1229 // Form the QQQQ REG_SEQUENCE.
1230 SDValue V0 = N->getOperand(0+3);
1231 SDValue V1 = N->getOperand(1+3);
1232 SDValue V2 = N->getOperand(2+3);
1233 SDValue V3 = (NumVecs == 3)
1234 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
1235 : N->getOperand(3+3);
1236 SDValue RegSeq = SDValue(QuadQRegs(MVT::v8i64, V0, V1, V2, V3), 0);
1238 // Store the even D registers.
1239 Ops.push_back(Reg0); // post-access address offset
1240 Ops.push_back(RegSeq);
1241 Ops.push_back(Pred);
1242 Ops.push_back(Reg0); // predicate register
1243 Ops.push_back(Chain);
1244 unsigned Opc = QOpcodes0[OpcodeIndex];
1245 SDNode *VStA = CurDAG->getMachineNode(Opc, dl, MemAddr.getValueType(),
1246 MVT::Other, Ops.data(), 7);
1247 Chain = SDValue(VStA, 1);
1249 // Store the odd D registers.
1250 Ops[0] = SDValue(VStA, 0); // MemAddr
1252 Opc = QOpcodes1[OpcodeIndex];
1253 SDNode *VStB = CurDAG->getMachineNode(Opc, dl, MemAddr.getValueType(),
1254 MVT::Other, Ops.data(), 7);
1255 Chain = SDValue(VStB, 1);
1256 ReplaceUses(SDValue(N, 0), Chain);
1260 SDNode *ARMDAGToDAGISel::SelectVLDSTLane(SDNode *N, bool IsLoad,
1261 unsigned NumVecs, unsigned *DOpcodes,
1262 unsigned *QOpcodes) {
1263 assert(NumVecs >=2 && NumVecs <= 4 && "VLDSTLane NumVecs out-of-range");
1264 DebugLoc dl = N->getDebugLoc();
1266 SDValue MemAddr, Align;
1267 if (!SelectAddrMode6(N->getOperand(2), MemAddr, Align))
1270 SDValue Chain = N->getOperand(0);
1272 cast<ConstantSDNode>(N->getOperand(NumVecs+3))->getZExtValue();
1273 EVT VT = IsLoad ? N->getValueType(0) : N->getOperand(3).getValueType();
1274 bool is64BitVector = VT.is64BitVector();
1276 unsigned OpcodeIndex;
1277 switch (VT.getSimpleVT().SimpleTy) {
1278 default: llvm_unreachable("unhandled vld/vst lane type");
1279 // Double-register operations:
1280 case MVT::v8i8: OpcodeIndex = 0; break;
1281 case MVT::v4i16: OpcodeIndex = 1; break;
1283 case MVT::v2i32: OpcodeIndex = 2; break;
1284 // Quad-register operations:
1285 case MVT::v8i16: OpcodeIndex = 0; break;
1287 case MVT::v4i32: OpcodeIndex = 1; break;
1290 SDValue Pred = getAL(CurDAG);
1291 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1293 SmallVector<SDValue, 7> Ops;
1294 Ops.push_back(MemAddr);
1295 Ops.push_back(Align);
1297 unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
1298 QOpcodes[OpcodeIndex]);
1301 SDValue V0 = N->getOperand(0+3);
1302 SDValue V1 = N->getOperand(1+3);
1305 SuperReg = SDValue(PairDRegs(MVT::v2i64, V0, V1), 0);
1307 SuperReg = SDValue(PairQRegs(MVT::v4i64, V0, V1), 0);
1309 SDValue V2 = N->getOperand(2+3);
1310 SDValue V3 = (NumVecs == 3)
1311 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
1312 : N->getOperand(3+3);
1314 SuperReg = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1316 SuperReg = SDValue(QuadQRegs(MVT::v8i64, V0, V1, V2, V3), 0);
1318 Ops.push_back(SuperReg);
1319 Ops.push_back(getI32Imm(Lane));
1320 Ops.push_back(Pred);
1321 Ops.push_back(Reg0);
1322 Ops.push_back(Chain);
1325 return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 7);
1328 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
1331 ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts);
1333 SDNode *VLdLn = CurDAG->getMachineNode(Opc, dl, ResTy, MVT::Other,
1335 SuperReg = SDValue(VLdLn, 0);
1336 Chain = SDValue(VLdLn, 1);
1338 // Extract the subregisters.
1339 assert(ARM::dsub_7 == ARM::dsub_0+7 && "Unexpected subreg numbering");
1340 assert(ARM::qsub_3 == ARM::qsub_0+3 && "Unexpected subreg numbering");
1341 unsigned SubIdx = is64BitVector ? ARM::dsub_0 : ARM::qsub_0;
1342 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1343 ReplaceUses(SDValue(N, Vec),
1344 CurDAG->getTargetExtractSubreg(SubIdx+Vec, dl, VT, SuperReg));
1345 ReplaceUses(SDValue(N, NumVecs), Chain);
1349 SDNode *ARMDAGToDAGISel::SelectVTBL(SDNode *N, bool IsExt, unsigned NumVecs,
1351 assert(NumVecs >= 2 && NumVecs <= 4 && "VTBL NumVecs out-of-range");
1352 DebugLoc dl = N->getDebugLoc();
1353 EVT VT = N->getValueType(0);
1354 unsigned FirstTblReg = IsExt ? 2 : 1;
1356 // Form a REG_SEQUENCE to force register allocation.
1358 SDValue V0 = N->getOperand(FirstTblReg + 0);
1359 SDValue V1 = N->getOperand(FirstTblReg + 1);
1361 RegSeq = SDValue(PairDRegs(MVT::v16i8, V0, V1), 0);
1363 SDValue V2 = N->getOperand(FirstTblReg + 2);
1364 // If it's a vtbl3, form a quad D-register and leave the last part as
1366 SDValue V3 = (NumVecs == 3)
1367 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
1368 : N->getOperand(FirstTblReg + 3);
1369 RegSeq = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1372 SmallVector<SDValue, 6> Ops;
1374 Ops.push_back(N->getOperand(1));
1375 Ops.push_back(RegSeq);
1376 Ops.push_back(N->getOperand(FirstTblReg + NumVecs));
1377 Ops.push_back(getAL(CurDAG)); // predicate
1378 Ops.push_back(CurDAG->getRegister(0, MVT::i32)); // predicate register
1379 return CurDAG->getMachineNode(Opc, dl, VT, Ops.data(), Ops.size());
1382 SDNode *ARMDAGToDAGISel::SelectV6T2BitfieldExtractOp(SDNode *N,
1384 if (!Subtarget->hasV6T2Ops())
1387 unsigned Opc = isSigned ? (Subtarget->isThumb() ? ARM::t2SBFX : ARM::SBFX)
1388 : (Subtarget->isThumb() ? ARM::t2UBFX : ARM::UBFX);
1391 // For unsigned extracts, check for a shift right and mask
1392 unsigned And_imm = 0;
1393 if (N->getOpcode() == ISD::AND) {
1394 if (isOpcWithIntImmediate(N, ISD::AND, And_imm)) {
1396 // The immediate is a mask of the low bits iff imm & (imm+1) == 0
1397 if (And_imm & (And_imm + 1))
1400 unsigned Srl_imm = 0;
1401 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRL,
1403 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
1405 unsigned Width = CountTrailingOnes_32(And_imm);
1406 unsigned LSB = Srl_imm;
1407 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1408 SDValue Ops[] = { N->getOperand(0).getOperand(0),
1409 CurDAG->getTargetConstant(LSB, MVT::i32),
1410 CurDAG->getTargetConstant(Width, MVT::i32),
1411 getAL(CurDAG), Reg0 };
1412 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
1418 // Otherwise, we're looking for a shift of a shift
1419 unsigned Shl_imm = 0;
1420 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SHL, Shl_imm)) {
1421 assert(Shl_imm > 0 && Shl_imm < 32 && "bad amount in shift node!");
1422 unsigned Srl_imm = 0;
1423 if (isInt32Immediate(N->getOperand(1), Srl_imm)) {
1424 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
1425 unsigned Width = 32 - Srl_imm;
1426 int LSB = Srl_imm - Shl_imm;
1429 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1430 SDValue Ops[] = { N->getOperand(0).getOperand(0),
1431 CurDAG->getTargetConstant(LSB, MVT::i32),
1432 CurDAG->getTargetConstant(Width, MVT::i32),
1433 getAL(CurDAG), Reg0 };
1434 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
1440 SDNode *ARMDAGToDAGISel::
1441 SelectT2CMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
1442 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1445 if (SelectT2ShifterOperandReg(TrueVal, CPTmp0, CPTmp1)) {
1446 unsigned SOVal = cast<ConstantSDNode>(CPTmp1)->getZExtValue();
1447 unsigned SOShOp = ARM_AM::getSORegShOp(SOVal);
1450 case ARM_AM::lsl: Opc = ARM::t2MOVCClsl; break;
1451 case ARM_AM::lsr: Opc = ARM::t2MOVCClsr; break;
1452 case ARM_AM::asr: Opc = ARM::t2MOVCCasr; break;
1453 case ARM_AM::ror: Opc = ARM::t2MOVCCror; break;
1455 llvm_unreachable("Unknown so_reg opcode!");
1459 CurDAG->getTargetConstant(ARM_AM::getSORegOffset(SOVal), MVT::i32);
1460 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1461 SDValue Ops[] = { FalseVal, CPTmp0, SOShImm, CC, CCR, InFlag };
1462 return CurDAG->SelectNodeTo(N, Opc, MVT::i32,Ops, 6);
1467 SDNode *ARMDAGToDAGISel::
1468 SelectARMCMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
1469 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1473 if (SelectShifterOperandReg(TrueVal, CPTmp0, CPTmp1, CPTmp2)) {
1474 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1475 SDValue Ops[] = { FalseVal, CPTmp0, CPTmp1, CPTmp2, CC, CCR, InFlag };
1476 return CurDAG->SelectNodeTo(N, ARM::MOVCCs, MVT::i32, Ops, 7);
1481 SDNode *ARMDAGToDAGISel::
1482 SelectT2CMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
1483 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1484 ConstantSDNode *T = dyn_cast<ConstantSDNode>(TrueVal);
1488 if (Pred_t2_so_imm(TrueVal.getNode())) {
1489 SDValue True = CurDAG->getTargetConstant(T->getZExtValue(), MVT::i32);
1490 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1491 SDValue Ops[] = { FalseVal, True, CC, CCR, InFlag };
1492 return CurDAG->SelectNodeTo(N,
1493 ARM::t2MOVCCi, MVT::i32, Ops, 5);
1498 SDNode *ARMDAGToDAGISel::
1499 SelectARMCMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
1500 ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1501 ConstantSDNode *T = dyn_cast<ConstantSDNode>(TrueVal);
1505 if (Pred_so_imm(TrueVal.getNode())) {
1506 SDValue True = CurDAG->getTargetConstant(T->getZExtValue(), MVT::i32);
1507 SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1508 SDValue Ops[] = { FalseVal, True, CC, CCR, InFlag };
1509 return CurDAG->SelectNodeTo(N,
1510 ARM::MOVCCi, MVT::i32, Ops, 5);
1515 SDNode *ARMDAGToDAGISel::SelectCMOVOp(SDNode *N) {
1516 EVT VT = N->getValueType(0);
1517 SDValue FalseVal = N->getOperand(0);
1518 SDValue TrueVal = N->getOperand(1);
1519 SDValue CC = N->getOperand(2);
1520 SDValue CCR = N->getOperand(3);
1521 SDValue InFlag = N->getOperand(4);
1522 assert(CC.getOpcode() == ISD::Constant);
1523 assert(CCR.getOpcode() == ISD::Register);
1524 ARMCC::CondCodes CCVal =
1525 (ARMCC::CondCodes)cast<ConstantSDNode>(CC)->getZExtValue();
1527 if (!Subtarget->isThumb1Only() && VT == MVT::i32) {
1528 // Pattern: (ARMcmov:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
1529 // Emits: (MOVCCs:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
1530 // Pattern complexity = 18 cost = 1 size = 0
1534 if (Subtarget->isThumb()) {
1535 SDNode *Res = SelectT2CMOVShiftOp(N, FalseVal, TrueVal,
1536 CCVal, CCR, InFlag);
1538 Res = SelectT2CMOVShiftOp(N, TrueVal, FalseVal,
1539 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1543 SDNode *Res = SelectARMCMOVShiftOp(N, FalseVal, TrueVal,
1544 CCVal, CCR, InFlag);
1546 Res = SelectARMCMOVShiftOp(N, TrueVal, FalseVal,
1547 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1552 // Pattern: (ARMcmov:i32 GPR:i32:$false,
1553 // (imm:i32)<<P:Pred_so_imm>>:$true,
1555 // Emits: (MOVCCi:i32 GPR:i32:$false,
1556 // (so_imm:i32 (imm:i32):$true), (imm:i32):$cc)
1557 // Pattern complexity = 10 cost = 1 size = 0
1558 if (Subtarget->isThumb()) {
1559 SDNode *Res = SelectT2CMOVSoImmOp(N, FalseVal, TrueVal,
1560 CCVal, CCR, InFlag);
1562 Res = SelectT2CMOVSoImmOp(N, TrueVal, FalseVal,
1563 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1567 SDNode *Res = SelectARMCMOVSoImmOp(N, FalseVal, TrueVal,
1568 CCVal, CCR, InFlag);
1570 Res = SelectARMCMOVSoImmOp(N, TrueVal, FalseVal,
1571 ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1577 // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1578 // Emits: (MOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1579 // Pattern complexity = 6 cost = 1 size = 0
1581 // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1582 // Emits: (tMOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1583 // Pattern complexity = 6 cost = 11 size = 0
1585 // Also FCPYScc and FCPYDcc.
1586 SDValue Tmp2 = CurDAG->getTargetConstant(CCVal, MVT::i32);
1587 SDValue Ops[] = { FalseVal, TrueVal, Tmp2, CCR, InFlag };
1589 switch (VT.getSimpleVT().SimpleTy) {
1590 default: assert(false && "Illegal conditional move type!");
1593 Opc = Subtarget->isThumb()
1594 ? (Subtarget->hasThumb2() ? ARM::t2MOVCCr : ARM::tMOVCCr_pseudo)
1604 return CurDAG->SelectNodeTo(N, Opc, VT, Ops, 5);
1607 SDNode *ARMDAGToDAGISel::SelectConcatVector(SDNode *N) {
1608 // The only time a CONCAT_VECTORS operation can have legal types is when
1609 // two 64-bit vectors are concatenated to a 128-bit vector.
1610 EVT VT = N->getValueType(0);
1611 if (!VT.is128BitVector() || N->getNumOperands() != 2)
1612 llvm_unreachable("unexpected CONCAT_VECTORS");
1613 DebugLoc dl = N->getDebugLoc();
1614 SDValue V0 = N->getOperand(0);
1615 SDValue V1 = N->getOperand(1);
1616 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
1617 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
1618 const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
1619 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
1622 SDNode *ARMDAGToDAGISel::Select(SDNode *N) {
1623 DebugLoc dl = N->getDebugLoc();
1625 if (N->isMachineOpcode())
1626 return NULL; // Already selected.
1628 switch (N->getOpcode()) {
1630 case ISD::Constant: {
1631 unsigned Val = cast<ConstantSDNode>(N)->getZExtValue();
1633 if (Subtarget->hasThumb2())
1634 // Thumb2-aware targets have the MOVT instruction, so all immediates can
1635 // be done with MOV + MOVT, at worst.
1638 if (Subtarget->isThumb()) {
1639 UseCP = (Val > 255 && // MOV
1640 ~Val > 255 && // MOV + MVN
1641 !ARM_AM::isThumbImmShiftedVal(Val)); // MOV + LSL
1643 UseCP = (ARM_AM::getSOImmVal(Val) == -1 && // MOV
1644 ARM_AM::getSOImmVal(~Val) == -1 && // MVN
1645 !ARM_AM::isSOImmTwoPartVal(Val)); // two instrs.
1650 CurDAG->getTargetConstantPool(ConstantInt::get(
1651 Type::getInt32Ty(*CurDAG->getContext()), Val),
1652 TLI.getPointerTy());
1655 if (Subtarget->isThumb1Only()) {
1656 SDValue Pred = getAL(CurDAG);
1657 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1658 SDValue Ops[] = { CPIdx, Pred, PredReg, CurDAG->getEntryNode() };
1659 ResNode = CurDAG->getMachineNode(ARM::tLDRcp, dl, MVT::i32, MVT::Other,
1664 CurDAG->getRegister(0, MVT::i32),
1665 CurDAG->getTargetConstant(0, MVT::i32),
1667 CurDAG->getRegister(0, MVT::i32),
1668 CurDAG->getEntryNode()
1670 ResNode=CurDAG->getMachineNode(ARM::LDRcp, dl, MVT::i32, MVT::Other,
1673 ReplaceUses(SDValue(N, 0), SDValue(ResNode, 0));
1677 // Other cases are autogenerated.
1680 case ISD::FrameIndex: {
1681 // Selects to ADDri FI, 0 which in turn will become ADDri SP, imm.
1682 int FI = cast<FrameIndexSDNode>(N)->getIndex();
1683 SDValue TFI = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
1684 if (Subtarget->isThumb1Only()) {
1685 return CurDAG->SelectNodeTo(N, ARM::tADDrSPi, MVT::i32, TFI,
1686 CurDAG->getTargetConstant(0, MVT::i32));
1688 unsigned Opc = ((Subtarget->isThumb() && Subtarget->hasThumb2()) ?
1689 ARM::t2ADDri : ARM::ADDri);
1690 SDValue Ops[] = { TFI, CurDAG->getTargetConstant(0, MVT::i32),
1691 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1692 CurDAG->getRegister(0, MVT::i32) };
1693 return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
1697 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, false))
1701 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, true))
1705 if (Subtarget->isThumb1Only())
1707 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1708 unsigned RHSV = C->getZExtValue();
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::t2ADDrs, MVT::i32, Ops, 6);
1722 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
1723 return CurDAG->SelectNodeTo(N, ARM::ADDrs, MVT::i32, Ops, 7);
1726 if (isPowerOf2_32(RHSV+1)) { // 2^n-1?
1727 unsigned ShImm = Log2_32(RHSV+1);
1730 SDValue V = N->getOperand(0);
1731 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
1732 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, MVT::i32);
1733 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1734 if (Subtarget->isThumb()) {
1735 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
1736 return CurDAG->SelectNodeTo(N, ARM::t2RSBrs, MVT::i32, Ops, 6);
1738 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
1739 return CurDAG->SelectNodeTo(N, ARM::RSBrs, MVT::i32, Ops, 7);
1745 // Check for unsigned bitfield extract
1746 if (SDNode *I = SelectV6T2BitfieldExtractOp(N, false))
1749 // (and (or x, c2), c1) and top 16-bits of c1 and c2 match, lower 16-bits
1750 // of c1 are 0xffff, and lower 16-bit of c2 are 0. That is, the top 16-bits
1751 // are entirely contributed by c2 and lower 16-bits are entirely contributed
1752 // by x. That's equal to (or (and x, 0xffff), (and c1, 0xffff0000)).
1753 // Select it to: "movt x, ((c1 & 0xffff) >> 16)
1754 EVT VT = N->getValueType(0);
1757 unsigned Opc = (Subtarget->isThumb() && Subtarget->hasThumb2())
1759 : (Subtarget->hasV6T2Ops() ? ARM::MOVTi16 : 0);
1762 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
1763 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1766 if (N0.getOpcode() == ISD::OR && N0.getNode()->hasOneUse()) {
1767 SDValue N2 = N0.getOperand(1);
1768 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1771 unsigned N1CVal = N1C->getZExtValue();
1772 unsigned N2CVal = N2C->getZExtValue();
1773 if ((N1CVal & 0xffff0000U) == (N2CVal & 0xffff0000U) &&
1774 (N1CVal & 0xffffU) == 0xffffU &&
1775 (N2CVal & 0xffffU) == 0x0U) {
1776 SDValue Imm16 = CurDAG->getTargetConstant((N2CVal & 0xFFFF0000U) >> 16,
1778 SDValue Ops[] = { N0.getOperand(0), Imm16,
1779 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
1780 return CurDAG->getMachineNode(Opc, dl, VT, Ops, 4);
1785 case ARMISD::VMOVRRD:
1786 return CurDAG->getMachineNode(ARM::VMOVRRD, dl, MVT::i32, MVT::i32,
1787 N->getOperand(0), getAL(CurDAG),
1788 CurDAG->getRegister(0, MVT::i32));
1789 case ISD::UMUL_LOHI: {
1790 if (Subtarget->isThumb1Only())
1792 if (Subtarget->isThumb()) {
1793 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
1794 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1795 CurDAG->getRegister(0, MVT::i32) };
1796 return CurDAG->getMachineNode(ARM::t2UMULL, dl, MVT::i32, MVT::i32,Ops,4);
1798 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
1799 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1800 CurDAG->getRegister(0, MVT::i32) };
1801 return CurDAG->getMachineNode(ARM::UMULL, dl, MVT::i32, MVT::i32, Ops, 5);
1804 case ISD::SMUL_LOHI: {
1805 if (Subtarget->isThumb1Only())
1807 if (Subtarget->isThumb()) {
1808 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
1809 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
1810 return CurDAG->getMachineNode(ARM::t2SMULL, dl, MVT::i32, MVT::i32,Ops,4);
1812 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
1813 getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1814 CurDAG->getRegister(0, MVT::i32) };
1815 return CurDAG->getMachineNode(ARM::SMULL, dl, MVT::i32, MVT::i32, Ops, 5);
1819 SDNode *ResNode = 0;
1820 if (Subtarget->isThumb() && Subtarget->hasThumb2())
1821 ResNode = SelectT2IndexedLoad(N);
1823 ResNode = SelectARMIndexedLoad(N);
1826 // Other cases are autogenerated.
1829 case ARMISD::BRCOND: {
1830 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
1831 // Emits: (Bcc:void (bb:Other):$dst, (imm:i32):$cc)
1832 // Pattern complexity = 6 cost = 1 size = 0
1834 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
1835 // Emits: (tBcc:void (bb:Other):$dst, (imm:i32):$cc)
1836 // Pattern complexity = 6 cost = 1 size = 0
1838 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
1839 // Emits: (t2Bcc:void (bb:Other):$dst, (imm:i32):$cc)
1840 // Pattern complexity = 6 cost = 1 size = 0
1842 unsigned Opc = Subtarget->isThumb() ?
1843 ((Subtarget->hasThumb2()) ? ARM::t2Bcc : ARM::tBcc) : ARM::Bcc;
1844 SDValue Chain = N->getOperand(0);
1845 SDValue N1 = N->getOperand(1);
1846 SDValue N2 = N->getOperand(2);
1847 SDValue N3 = N->getOperand(3);
1848 SDValue InFlag = N->getOperand(4);
1849 assert(N1.getOpcode() == ISD::BasicBlock);
1850 assert(N2.getOpcode() == ISD::Constant);
1851 assert(N3.getOpcode() == ISD::Register);
1853 SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
1854 cast<ConstantSDNode>(N2)->getZExtValue()),
1856 SDValue Ops[] = { N1, Tmp2, N3, Chain, InFlag };
1857 SDNode *ResNode = CurDAG->getMachineNode(Opc, dl, MVT::Other,
1859 Chain = SDValue(ResNode, 0);
1860 if (N->getNumValues() == 2) {
1861 InFlag = SDValue(ResNode, 1);
1862 ReplaceUses(SDValue(N, 1), InFlag);
1864 ReplaceUses(SDValue(N, 0),
1865 SDValue(Chain.getNode(), Chain.getResNo()));
1869 return SelectCMOVOp(N);
1870 case ARMISD::CNEG: {
1871 EVT VT = N->getValueType(0);
1872 SDValue N0 = N->getOperand(0);
1873 SDValue N1 = N->getOperand(1);
1874 SDValue N2 = N->getOperand(2);
1875 SDValue N3 = N->getOperand(3);
1876 SDValue InFlag = N->getOperand(4);
1877 assert(N2.getOpcode() == ISD::Constant);
1878 assert(N3.getOpcode() == ISD::Register);
1880 SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
1881 cast<ConstantSDNode>(N2)->getZExtValue()),
1883 SDValue Ops[] = { N0, N1, Tmp2, N3, InFlag };
1885 switch (VT.getSimpleVT().SimpleTy) {
1886 default: assert(false && "Illegal conditional move type!");
1895 return CurDAG->SelectNodeTo(N, Opc, VT, Ops, 5);
1898 case ARMISD::VZIP: {
1900 EVT VT = N->getValueType(0);
1901 switch (VT.getSimpleVT().SimpleTy) {
1902 default: return NULL;
1903 case MVT::v8i8: Opc = ARM::VZIPd8; break;
1904 case MVT::v4i16: Opc = ARM::VZIPd16; break;
1906 case MVT::v2i32: Opc = ARM::VZIPd32; break;
1907 case MVT::v16i8: Opc = ARM::VZIPq8; break;
1908 case MVT::v8i16: Opc = ARM::VZIPq16; break;
1910 case MVT::v4i32: Opc = ARM::VZIPq32; break;
1912 SDValue Pred = getAL(CurDAG);
1913 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1914 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
1915 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
1917 case ARMISD::VUZP: {
1919 EVT VT = N->getValueType(0);
1920 switch (VT.getSimpleVT().SimpleTy) {
1921 default: return NULL;
1922 case MVT::v8i8: Opc = ARM::VUZPd8; break;
1923 case MVT::v4i16: Opc = ARM::VUZPd16; break;
1925 case MVT::v2i32: Opc = ARM::VUZPd32; break;
1926 case MVT::v16i8: Opc = ARM::VUZPq8; break;
1927 case MVT::v8i16: Opc = ARM::VUZPq16; break;
1929 case MVT::v4i32: Opc = ARM::VUZPq32; break;
1931 SDValue Pred = getAL(CurDAG);
1932 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1933 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
1934 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
1936 case ARMISD::VTRN: {
1938 EVT VT = N->getValueType(0);
1939 switch (VT.getSimpleVT().SimpleTy) {
1940 default: return NULL;
1941 case MVT::v8i8: Opc = ARM::VTRNd8; break;
1942 case MVT::v4i16: Opc = ARM::VTRNd16; break;
1944 case MVT::v2i32: Opc = ARM::VTRNd32; break;
1945 case MVT::v16i8: Opc = ARM::VTRNq8; break;
1946 case MVT::v8i16: Opc = ARM::VTRNq16; break;
1948 case MVT::v4i32: Opc = ARM::VTRNq32; break;
1950 SDValue Pred = getAL(CurDAG);
1951 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1952 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
1953 return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
1955 case ARMISD::BUILD_VECTOR: {
1956 EVT VecVT = N->getValueType(0);
1957 EVT EltVT = VecVT.getVectorElementType();
1958 unsigned NumElts = VecVT.getVectorNumElements();
1959 if (EltVT.getSimpleVT() == MVT::f64) {
1960 assert(NumElts == 2 && "unexpected type for BUILD_VECTOR");
1961 return PairDRegs(VecVT, N->getOperand(0), N->getOperand(1));
1963 assert(EltVT.getSimpleVT() == MVT::f32 &&
1964 "unexpected type for BUILD_VECTOR");
1966 return PairSRegs(VecVT, N->getOperand(0), N->getOperand(1));
1967 assert(NumElts == 4 && "unexpected type for BUILD_VECTOR");
1968 return QuadSRegs(VecVT, N->getOperand(0), N->getOperand(1),
1969 N->getOperand(2), N->getOperand(3));
1972 case ISD::INTRINSIC_VOID:
1973 case ISD::INTRINSIC_W_CHAIN: {
1974 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
1979 case Intrinsic::arm_neon_vld1: {
1980 unsigned DOpcodes[] = { ARM::VLD1d8, ARM::VLD1d16,
1981 ARM::VLD1d32, ARM::VLD1d64 };
1982 unsigned QOpcodes[] = { ARM::VLD1q8Pseudo, ARM::VLD1q16Pseudo,
1983 ARM::VLD1q32Pseudo, ARM::VLD1q64Pseudo };
1984 return SelectVLD(N, 1, DOpcodes, QOpcodes, 0);
1987 case Intrinsic::arm_neon_vld2: {
1988 unsigned DOpcodes[] = { ARM::VLD2d8Pseudo, ARM::VLD2d16Pseudo,
1989 ARM::VLD2d32Pseudo, ARM::VLD1q64Pseudo };
1990 unsigned QOpcodes[] = { ARM::VLD2q8Pseudo, ARM::VLD2q16Pseudo,
1991 ARM::VLD2q32Pseudo };
1992 return SelectVLD(N, 2, DOpcodes, QOpcodes, 0);
1995 case Intrinsic::arm_neon_vld3: {
1996 unsigned DOpcodes[] = { ARM::VLD3d8Pseudo, ARM::VLD3d16Pseudo,
1997 ARM::VLD3d32Pseudo, ARM::VLD1d64TPseudo };
1998 unsigned QOpcodes0[] = { ARM::VLD3q8Pseudo_UPD,
1999 ARM::VLD3q16Pseudo_UPD,
2000 ARM::VLD3q32Pseudo_UPD };
2001 unsigned QOpcodes1[] = { ARM::VLD3q8oddPseudo_UPD,
2002 ARM::VLD3q16oddPseudo_UPD,
2003 ARM::VLD3q32oddPseudo_UPD };
2004 return SelectVLD(N, 3, DOpcodes, QOpcodes0, QOpcodes1);
2007 case Intrinsic::arm_neon_vld4: {
2008 unsigned DOpcodes[] = { ARM::VLD4d8Pseudo, ARM::VLD4d16Pseudo,
2009 ARM::VLD4d32Pseudo, ARM::VLD1d64QPseudo };
2010 unsigned QOpcodes0[] = { ARM::VLD4q8Pseudo_UPD,
2011 ARM::VLD4q16Pseudo_UPD,
2012 ARM::VLD4q32Pseudo_UPD };
2013 unsigned QOpcodes1[] = { ARM::VLD4q8oddPseudo_UPD,
2014 ARM::VLD4q16oddPseudo_UPD,
2015 ARM::VLD4q32oddPseudo_UPD };
2016 return SelectVLD(N, 4, DOpcodes, QOpcodes0, QOpcodes1);
2019 case Intrinsic::arm_neon_vld2lane: {
2020 unsigned DOpcodes[] = { ARM::VLD2LNd8Pseudo, ARM::VLD2LNd16Pseudo,
2021 ARM::VLD2LNd32Pseudo };
2022 unsigned QOpcodes[] = { ARM::VLD2LNq16Pseudo, ARM::VLD2LNq32Pseudo };
2023 return SelectVLDSTLane(N, true, 2, DOpcodes, QOpcodes);
2026 case Intrinsic::arm_neon_vld3lane: {
2027 unsigned DOpcodes[] = { ARM::VLD3LNd8Pseudo, ARM::VLD3LNd16Pseudo,
2028 ARM::VLD3LNd32Pseudo };
2029 unsigned QOpcodes[] = { ARM::VLD3LNq16Pseudo, ARM::VLD3LNq32Pseudo };
2030 return SelectVLDSTLane(N, true, 3, DOpcodes, QOpcodes);
2033 case Intrinsic::arm_neon_vld4lane: {
2034 unsigned DOpcodes[] = { ARM::VLD4LNd8Pseudo, ARM::VLD4LNd16Pseudo,
2035 ARM::VLD4LNd32Pseudo };
2036 unsigned QOpcodes[] = { ARM::VLD4LNq16Pseudo, ARM::VLD4LNq32Pseudo };
2037 return SelectVLDSTLane(N, true, 4, DOpcodes, QOpcodes);
2040 case Intrinsic::arm_neon_vst1: {
2041 unsigned DOpcodes[] = { ARM::VST1d8, ARM::VST1d16,
2042 ARM::VST1d32, ARM::VST1d64 };
2043 unsigned QOpcodes[] = { ARM::VST1q8Pseudo, ARM::VST1q16Pseudo,
2044 ARM::VST1q32Pseudo, ARM::VST1q64Pseudo };
2045 return SelectVST(N, 1, DOpcodes, QOpcodes, 0);
2048 case Intrinsic::arm_neon_vst2: {
2049 unsigned DOpcodes[] = { ARM::VST2d8Pseudo, ARM::VST2d16Pseudo,
2050 ARM::VST2d32Pseudo, ARM::VST1q64Pseudo };
2051 unsigned QOpcodes[] = { ARM::VST2q8Pseudo, ARM::VST2q16Pseudo,
2052 ARM::VST2q32Pseudo };
2053 return SelectVST(N, 2, DOpcodes, QOpcodes, 0);
2056 case Intrinsic::arm_neon_vst3: {
2057 unsigned DOpcodes[] = { ARM::VST3d8Pseudo, ARM::VST3d16Pseudo,
2058 ARM::VST3d32Pseudo, ARM::VST1d64TPseudo };
2059 unsigned QOpcodes0[] = { ARM::VST3q8Pseudo_UPD,
2060 ARM::VST3q16Pseudo_UPD,
2061 ARM::VST3q32Pseudo_UPD };
2062 unsigned QOpcodes1[] = { ARM::VST3q8oddPseudo_UPD,
2063 ARM::VST3q16oddPseudo_UPD,
2064 ARM::VST3q32oddPseudo_UPD };
2065 return SelectVST(N, 3, DOpcodes, QOpcodes0, QOpcodes1);
2068 case Intrinsic::arm_neon_vst4: {
2069 unsigned DOpcodes[] = { ARM::VST4d8Pseudo, ARM::VST4d16Pseudo,
2070 ARM::VST4d32Pseudo, ARM::VST1d64QPseudo };
2071 unsigned QOpcodes0[] = { ARM::VST4q8Pseudo_UPD,
2072 ARM::VST4q16Pseudo_UPD,
2073 ARM::VST4q32Pseudo_UPD };
2074 unsigned QOpcodes1[] = { ARM::VST4q8oddPseudo_UPD,
2075 ARM::VST4q16oddPseudo_UPD,
2076 ARM::VST4q32oddPseudo_UPD };
2077 return SelectVST(N, 4, DOpcodes, QOpcodes0, QOpcodes1);
2080 case Intrinsic::arm_neon_vst2lane: {
2081 unsigned DOpcodes[] = { ARM::VST2LNd8Pseudo, ARM::VST2LNd16Pseudo,
2082 ARM::VST2LNd32Pseudo };
2083 unsigned QOpcodes[] = { ARM::VST2LNq16Pseudo, ARM::VST2LNq32Pseudo };
2084 return SelectVLDSTLane(N, false, 2, DOpcodes, QOpcodes);
2087 case Intrinsic::arm_neon_vst3lane: {
2088 unsigned DOpcodes[] = { ARM::VST3LNd8Pseudo, ARM::VST3LNd16Pseudo,
2089 ARM::VST3LNd32Pseudo };
2090 unsigned QOpcodes[] = { ARM::VST3LNq16Pseudo, ARM::VST3LNq32Pseudo };
2091 return SelectVLDSTLane(N, false, 3, DOpcodes, QOpcodes);
2094 case Intrinsic::arm_neon_vst4lane: {
2095 unsigned DOpcodes[] = { ARM::VST4LNd8Pseudo, ARM::VST4LNd16Pseudo,
2096 ARM::VST4LNd32Pseudo };
2097 unsigned QOpcodes[] = { ARM::VST4LNq16Pseudo, ARM::VST4LNq32Pseudo };
2098 return SelectVLDSTLane(N, false, 4, DOpcodes, QOpcodes);
2104 case ISD::INTRINSIC_WO_CHAIN: {
2105 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
2110 case Intrinsic::arm_neon_vtbl2:
2111 return SelectVTBL(N, false, 2, ARM::VTBL2Pseudo);
2112 case Intrinsic::arm_neon_vtbl3:
2113 return SelectVTBL(N, false, 3, ARM::VTBL3Pseudo);
2114 case Intrinsic::arm_neon_vtbl4:
2115 return SelectVTBL(N, false, 4, ARM::VTBL4Pseudo);
2117 case Intrinsic::arm_neon_vtbx2:
2118 return SelectVTBL(N, true, 2, ARM::VTBX2Pseudo);
2119 case Intrinsic::arm_neon_vtbx3:
2120 return SelectVTBL(N, true, 3, ARM::VTBX3Pseudo);
2121 case Intrinsic::arm_neon_vtbx4:
2122 return SelectVTBL(N, true, 4, ARM::VTBX4Pseudo);
2127 case ISD::CONCAT_VECTORS:
2128 return SelectConcatVector(N);
2131 return SelectCode(N);
2134 bool ARMDAGToDAGISel::
2135 SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
2136 std::vector<SDValue> &OutOps) {
2137 assert(ConstraintCode == 'm' && "unexpected asm memory constraint");
2138 // Require the address to be in a register. That is safe for all ARM
2139 // variants and it is hard to do anything much smarter without knowing
2140 // how the operand is used.
2141 OutOps.push_back(Op);
2145 /// createARMISelDag - This pass converts a legalized DAG into a
2146 /// ARM-specific DAG, ready for instruction scheduling.
2148 FunctionPass *llvm::createARMISelDag(ARMBaseTargetMachine &TM,
2149 CodeGenOpt::Level OptLevel) {
2150 return new ARMDAGToDAGISel(TM, OptLevel);