ARMISelDAGToDAG.cpp revision 52a261b3c1391c5fec399ddeb3fc6ee9541e8790
1//===-- ARMISelDAGToDAG.cpp - A dag to dag inst selector for ARM ----------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines an instruction selector for the ARM target.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "arm-isel"
15#include "ARM.h"
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"
36
37using namespace llvm;
38
39static cl::opt<bool>
40DisableShifterOp("disable-shifter-op", cl::Hidden,
41  cl::desc("Disable isel of shifter-op"),
42  cl::init(false));
43
44//===--------------------------------------------------------------------===//
45/// ARMDAGToDAGISel - ARM specific code to select ARM machine
46/// instructions for SelectionDAG operations.
47///
48namespace {
49class ARMDAGToDAGISel : public SelectionDAGISel {
50  ARMBaseTargetMachine &TM;
51
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;
55
56public:
57  explicit ARMDAGToDAGISel(ARMBaseTargetMachine &tm,
58                           CodeGenOpt::Level OptLevel)
59    : SelectionDAGISel(tm, OptLevel), TM(tm),
60    Subtarget(&TM.getSubtarget<ARMSubtarget>()) {
61  }
62
63  virtual const char *getPassName() const {
64    return "ARM Instruction Selection";
65  }
66
67  /// getI32Imm - Return a target constant of type i32 with the specified
68  /// value.
69  inline SDValue getI32Imm(unsigned Imm) {
70    return CurDAG->getTargetConstant(Imm, MVT::i32);
71  }
72
73  SDNode *Select(SDNode *N);
74
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,
87                       SDValue &Offset);
88  bool SelectAddrMode6(SDValue N, SDValue &Addr, SDValue &Align);
89
90  bool SelectAddrModePC(SDValue N, SDValue &Offset,
91                        SDValue &Label);
92
93  bool SelectThumbAddrModeRR(SDValue N, SDValue &Base, SDValue &Offset);
94  bool SelectThumbAddrModeRI5(SDValue N, unsigned Scale,
95                              SDValue &Base, SDValue &OffImm,
96                              SDValue &Offset);
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);
104
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,
109                            SDValue &OffImm);
110  bool SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
111                                 SDValue &OffImm);
112  bool SelectT2AddrModeSoReg(SDValue N, SDValue &Base,
113                             SDValue &OffReg, SDValue &ShImm);
114
115  inline bool Pred_so_imm(SDNode *inN) const {
116    ConstantSDNode *N = cast<ConstantSDNode>(inN);
117    return ARM_AM::getSOImmVal(N->getZExtValue()) != -1;
118  }
119
120  inline bool Pred_t2_so_imm(SDNode *inN) const {
121    ConstantSDNode *N = cast<ConstantSDNode>(inN);
122    return ARM_AM::getT2SOImmVal(N->getZExtValue()) != -1;
123  }
124
125  // Include the pieces autogenerated from the target description.
126#include "ARMGenDAGISel.inc"
127
128private:
129  /// SelectARMIndexedLoad - Indexed (pre/post inc/dec) load matching code for
130  /// ARM.
131  SDNode *SelectARMIndexedLoad(SDNode *N);
132  SDNode *SelectT2IndexedLoad(SDNode *N);
133
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);
140
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);
147
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);
153
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);
158
159  /// SelectV6T2BitfieldExtractOp - Select SBFX/UBFX instructions for ARM.
160  SDNode *SelectV6T2BitfieldExtractOp(SDNode *N, bool isSigned);
161
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,
166                              SDValue InFlag);
167  SDNode *SelectARMCMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
168                               ARMCC::CondCodes CCVal, SDValue CCR,
169                               SDValue InFlag);
170  SDNode *SelectT2CMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
171                              ARMCC::CondCodes CCVal, SDValue CCR,
172                              SDValue InFlag);
173  SDNode *SelectARMCMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
174                               ARMCC::CondCodes CCVal, SDValue CCR,
175                               SDValue InFlag);
176
177  SDNode *SelectConcatVector(SDNode *N);
178
179  /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
180  /// inline asm expressions.
181  virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
182                                            char ConstraintCode,
183                                            std::vector<SDValue> &OutOps);
184
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);
189
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);
194};
195}
196
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.
199static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
200  if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
201    Imm = cast<ConstantSDNode>(N)->getZExtValue();
202    return true;
203  }
204  return false;
205}
206
207// isInt32Immediate - This method tests to see if a constant operand.
208// If so Imm will receive the 32 bit value.
209static bool isInt32Immediate(SDValue N, unsigned &Imm) {
210  return isInt32Immediate(N.getNode(), Imm);
211}
212
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.
216static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
217  return N->getOpcode() == Opc &&
218         isInt32Immediate(N->getOperand(1).getNode(), Imm);
219}
220
221
222bool ARMDAGToDAGISel::SelectShifterOperandReg(SDValue N,
223                                              SDValue &BaseReg,
224                                              SDValue &ShReg,
225                                              SDValue &Opc) {
226  if (DisableShifterOp)
227    return false;
228
229  ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
230
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;
234
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;
240  } else {
241    ShReg = N.getOperand(1);
242  }
243  Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
244                                  MVT::i32);
245  return true;
246}
247
248bool ARMDAGToDAGISel::SelectAddrMode2(SDValue N,
249                                      SDValue &Base, SDValue &Offset,
250                                      SDValue &Opc) {
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();
255      if (RHSC & 1) {
256        RHSC = RHSC & ~1;
257        ARM_AM::AddrOpc AddSub = ARM_AM::add;
258        if (RHSC < 0) {
259          AddSub = ARM_AM::sub;
260          RHSC = - RHSC;
261        }
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,
266                                                            ARM_AM::lsl),
267                                          MVT::i32);
268          return true;
269        }
270      }
271    }
272  }
273
274  if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB) {
275    Base = N;
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);
283    }
284    Offset = CurDAG->getRegister(0, MVT::i32);
285    Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::add, 0,
286                                                      ARM_AM::no_shift),
287                                    MVT::i32);
288    return true;
289  }
290
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());
301        }
302        Offset = CurDAG->getRegister(0, MVT::i32);
303
304        ARM_AM::AddrOpc AddSub = ARM_AM::add;
305        if (RHSC < 0) {
306          AddSub = ARM_AM::sub;
307          RHSC = - RHSC;
308        }
309        Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, RHSC,
310                                                          ARM_AM::no_shift),
311                                        MVT::i32);
312        return true;
313      }
314    }
315
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));
319  unsigned ShAmt = 0;
320
321  Base   = N.getOperand(0);
322  Offset = N.getOperand(1);
323
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
326    // it.
327    if (ConstantSDNode *Sh =
328           dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) {
329      ShAmt = Sh->getZExtValue();
330      Offset = N.getOperand(1).getOperand(0);
331    } else {
332      ShOpcVal = ARM_AM::no_shift;
333    }
334  }
335
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
341      // fold it.
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);
347      } else {
348        ShOpcVal = ARM_AM::no_shift;
349      }
350    }
351  }
352
353  Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
354                                  MVT::i32);
355  return true;
356}
357
358bool 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,
371                                                        ARM_AM::no_shift),
372                                      MVT::i32);
373      return true;
374    }
375  }
376
377  Offset = N;
378  ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
379  unsigned ShAmt = 0;
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
382    // it.
383    if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
384      ShAmt = Sh->getZExtValue();
385      Offset = N.getOperand(0);
386    } else {
387      ShOpcVal = ARM_AM::no_shift;
388    }
389  }
390
391  Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
392                                  MVT::i32);
393  return true;
394}
395
396
397bool ARMDAGToDAGISel::SelectAddrMode3(SDValue N,
398                                      SDValue &Base, SDValue &Offset,
399                                      SDValue &Opc) {
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);
405    return true;
406  }
407
408  if (N.getOpcode() != ISD::ADD) {
409    Base = N;
410    if (N.getOpcode() == ISD::FrameIndex) {
411      int FI = cast<FrameIndexSDNode>(N)->getIndex();
412      Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
413    }
414    Offset = CurDAG->getRegister(0, MVT::i32);
415    Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0),MVT::i32);
416    return true;
417  }
418
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());
428      }
429      Offset = CurDAG->getRegister(0, MVT::i32);
430
431      ARM_AM::AddrOpc AddSub = ARM_AM::add;
432      if (RHSC < 0) {
433        AddSub = ARM_AM::sub;
434        RHSC = - RHSC;
435      }
436      Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, RHSC),MVT::i32);
437      return true;
438    }
439  }
440
441  Base = N.getOperand(0);
442  Offset = N.getOperand(1);
443  Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0), MVT::i32);
444  return true;
445}
446
447bool 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);
460      return true;
461    }
462  }
463
464  Offset = N;
465  Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, 0), MVT::i32);
466  return true;
467}
468
469bool ARMDAGToDAGISel::SelectAddrMode4(SDValue N, SDValue &Addr, SDValue &Mode) {
470  Addr = N;
471  Mode = CurDAG->getTargetConstant(ARM_AM::getAM4ModeImm(ARM_AM::ia), MVT::i32);
472  return true;
473}
474
475bool ARMDAGToDAGISel::SelectAddrMode5(SDValue N,
476                                      SDValue &Base, SDValue &Offset) {
477  if (N.getOpcode() != ISD::ADD) {
478    Base = N;
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);
486    }
487    Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
488                                       MVT::i32);
489    return true;
490  }
491
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.
496      RHSC >>= 2;
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());
503        }
504
505        ARM_AM::AddrOpc AddSub = ARM_AM::add;
506        if (RHSC < 0) {
507          AddSub = ARM_AM::sub;
508          RHSC = - RHSC;
509        }
510        Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(AddSub, RHSC),
511                                           MVT::i32);
512        return true;
513      }
514    }
515  }
516
517  Base = N;
518  Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
519                                     MVT::i32);
520  return true;
521}
522
523bool ARMDAGToDAGISel::SelectAddrMode6(SDValue N, SDValue &Addr, SDValue &Align){
524  Addr = N;
525  // Default to no alignment.
526  Align = CurDAG->getTargetConstant(0, MVT::i32);
527  return true;
528}
529
530bool 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(),
536                                       MVT::i32);
537    return true;
538  }
539  return false;
540}
541
542bool 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())
548      return false;
549
550    Base = Offset = N;
551    return true;
552  }
553
554  Base = N.getOperand(0);
555  Offset = N.getOperand(1);
556  return true;
557}
558
559bool
560ARMDAGToDAGISel::SelectThumbAddrModeRI5(SDValue N,
561                                        unsigned Scale, SDValue &Base,
562                                        SDValue &OffImm, SDValue &Offset) {
563  if (Scale == 4) {
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.
570  }
571
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);
577    } else
578      Base = N;
579
580    Offset = CurDAG->getRegister(0, MVT::i32);
581    OffImm = CurDAG->getTargetConstant(0, MVT::i32);
582    return true;
583  }
584
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)) {
590    Base = N;
591    Offset = CurDAG->getRegister(0, MVT::i32);
592    OffImm = CurDAG->getTargetConstant(0, MVT::i32);
593    return true;
594  }
595
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.
600      RHSC /= Scale;
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);
605        return true;
606      }
607    }
608  }
609
610  Base = N.getOperand(0);
611  Offset = N.getOperand(1);
612  OffImm = CurDAG->getTargetConstant(0, MVT::i32);
613  return true;
614}
615
616bool ARMDAGToDAGISel::SelectThumbAddrModeS1(SDValue N,
617                                            SDValue &Base, SDValue &OffImm,
618                                            SDValue &Offset) {
619  return SelectThumbAddrModeRI5(N, 1, Base, OffImm, Offset);
620}
621
622bool ARMDAGToDAGISel::SelectThumbAddrModeS2(SDValue N,
623                                            SDValue &Base, SDValue &OffImm,
624                                            SDValue &Offset) {
625  return SelectThumbAddrModeRI5(N, 2, Base, OffImm, Offset);
626}
627
628bool ARMDAGToDAGISel::SelectThumbAddrModeS4(SDValue N,
629                                            SDValue &Base, SDValue &OffImm,
630                                            SDValue &Offset) {
631  return SelectThumbAddrModeRI5(N, 4, Base, OffImm, Offset);
632}
633
634bool 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);
640    return true;
641  }
642
643  if (N.getOpcode() != ISD::ADD)
644    return false;
645
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.
653        RHSC >>= 2;
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());
659          }
660          OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
661          return true;
662        }
663      }
664    }
665  }
666
667  return false;
668}
669
670bool ARMDAGToDAGISel::SelectT2ShifterOperandReg(SDValue N, SDValue &BaseReg,
671                                                SDValue &Opc) {
672  if (DisableShifterOp)
673    return false;
674
675  ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
676
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;
680
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));
686    return true;
687  }
688
689  return false;
690}
691
692bool ARMDAGToDAGISel::SelectT2AddrModeImm12(SDValue N,
693                                            SDValue &Base, SDValue &OffImm) {
694  // Match simple R + imm12 operands.
695
696  // Base only.
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);
703      return true;
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.
710    } else
711      Base = N;
712    OffImm  = CurDAG->getTargetConstant(0, MVT::i32);
713    return true;
714  }
715
716  if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
717    if (SelectT2AddrModeImm8(N, Base, OffImm))
718      // Let t2LDRi8 handle (R - imm8).
719      return false;
720
721    int RHSC = (int)RHS->getZExtValue();
722    if (N.getOpcode() == ISD::SUB)
723      RHSC = -RHSC;
724
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());
730      }
731      OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
732      return true;
733    }
734  }
735
736  // Base only.
737  Base = N;
738  OffImm  = CurDAG->getTargetConstant(0, MVT::i32);
739  return true;
740}
741
742bool 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)
749        RHSC = -RHSC;
750
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());
756        }
757        OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
758        return true;
759      }
760    }
761  }
762
763  return false;
764}
765
766bool ARMDAGToDAGISel::SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
767                                                 SDValue &OffImm){
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);
778      return true;
779    }
780  }
781
782  return false;
783}
784
785bool ARMDAGToDAGISel::SelectT2AddrModeSoReg(SDValue N,
786                                            SDValue &Base,
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)
790    return false;
791
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)
796      return false;
797    else if (RHSC < 0 && RHSC >= -255) // 8 bits
798      return false;
799  }
800
801  // Look for (R + R) or (R + (R << [1,2,3])).
802  unsigned ShAmt = 0;
803  Base   = N.getOperand(0);
804  OffReg = N.getOperand(1);
805
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);
812  }
813
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
816    // it.
817    if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(OffReg.getOperand(1))) {
818      ShAmt = Sh->getZExtValue();
819      if (ShAmt >= 4) {
820        ShAmt = 0;
821        ShOpcVal = ARM_AM::no_shift;
822      } else
823        OffReg = OffReg.getOperand(0);
824    } else {
825      ShOpcVal = ARM_AM::no_shift;
826    }
827  }
828
829  ShImm = CurDAG->getTargetConstant(ShAmt, MVT::i32);
830
831  return true;
832}
833
834//===--------------------------------------------------------------------===//
835
836/// getAL - Returns a ARMCC::AL immediate node.
837static inline SDValue getAL(SelectionDAG *CurDAG) {
838  return CurDAG->getTargetConstant((uint64_t)ARMCC::AL, MVT::i32);
839}
840
841SDNode *ARMDAGToDAGISel::SelectARMIndexedLoad(SDNode *N) {
842  LoadSDNode *LD = cast<LoadSDNode>(N);
843  ISD::MemIndexedMode AM = LD->getAddressingMode();
844  if (AM == ISD::UNINDEXED)
845    return NULL;
846
847  EVT LoadedVT = LD->getMemoryVT();
848  SDValue Offset, AMOpc;
849  bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
850  unsigned Opcode = 0;
851  bool Match = false;
852  if (LoadedVT == MVT::i32 &&
853      SelectAddrMode2Offset(N, LD->getOffset(), Offset, AMOpc)) {
854    Opcode = isPre ? ARM::LDR_PRE : ARM::LDR_POST;
855    Match = true;
856  } else if (LoadedVT == MVT::i16 &&
857             SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
858    Match = true;
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)) {
865        Match = true;
866        Opcode = isPre ? ARM::LDRSB_PRE : ARM::LDRSB_POST;
867      }
868    } else {
869      if (SelectAddrMode2Offset(N, LD->getOffset(), Offset, AMOpc)) {
870        Match = true;
871        Opcode = isPre ? ARM::LDRB_PRE : ARM::LDRB_POST;
872      }
873    }
874  }
875
876  if (Match) {
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,
882                                  MVT::Other, Ops, 6);
883  }
884
885  return NULL;
886}
887
888SDNode *ARMDAGToDAGISel::SelectT2IndexedLoad(SDNode *N) {
889  LoadSDNode *LD = cast<LoadSDNode>(N);
890  ISD::MemIndexedMode AM = LD->getAddressingMode();
891  if (AM == ISD::UNINDEXED)
892    return NULL;
893
894  EVT LoadedVT = LD->getMemoryVT();
895  bool isSExtLd = LD->getExtensionType() == ISD::SEXTLOAD;
896  SDValue Offset;
897  bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
898  unsigned Opcode = 0;
899  bool Match = false;
900  if (SelectT2AddrModeImm8Offset(N, LD->getOffset(), Offset)) {
901    switch (LoadedVT.getSimpleVT().SimpleTy) {
902    case MVT::i32:
903      Opcode = isPre ? ARM::t2LDR_PRE : ARM::t2LDR_POST;
904      break;
905    case MVT::i16:
906      if (isSExtLd)
907        Opcode = isPre ? ARM::t2LDRSH_PRE : ARM::t2LDRSH_POST;
908      else
909        Opcode = isPre ? ARM::t2LDRH_PRE : ARM::t2LDRH_POST;
910      break;
911    case MVT::i8:
912    case MVT::i1:
913      if (isSExtLd)
914        Opcode = isPre ? ARM::t2LDRSB_PRE : ARM::t2LDRSB_POST;
915      else
916        Opcode = isPre ? ARM::t2LDRB_PRE : ARM::t2LDRB_POST;
917      break;
918    default:
919      return NULL;
920    }
921    Match = true;
922  }
923
924  if (Match) {
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,
930                                  MVT::Other, Ops, 5);
931  }
932
933  return NULL;
934}
935
936/// PairSRegs - Form a D register from a pair of S registers.
937///
938SDNode *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);
944}
945
946/// PairDRegs - Form a quad register from a pair of D registers.
947///
948SDNode *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);
954}
955
956/// PairQRegs - Form 4 consecutive D registers from a pair of Q registers.
957///
958SDNode *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);
964}
965
966/// QuadSRegs - Form 4 consecutive S registers.
967///
968SDNode *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);
977}
978
979/// QuadDRegs - Form 4 consecutive D registers.
980///
981SDNode *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);
990}
991
992/// QuadQRegs - Form 4 consecutive Q registers.
993///
994SDNode *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);
1003}
1004
1005SDNode *ARMDAGToDAGISel::SelectVLD(SDNode *N, unsigned NumVecs,
1006                                   unsigned *DOpcodes, unsigned *QOpcodes0,
1007                                   unsigned *QOpcodes1) {
1008  assert(NumVecs >= 1 && NumVecs <= 4 && "VLD NumVecs out-of-range");
1009  DebugLoc dl = N->getDebugLoc();
1010
1011  SDValue MemAddr, Align;
1012  if (!SelectAddrMode6(N->getOperand(2), MemAddr, Align))
1013    return NULL;
1014
1015  SDValue Chain = N->getOperand(0);
1016  EVT VT = N->getValueType(0);
1017  bool is64BitVector = VT.is64BitVector();
1018
1019  unsigned OpcodeIndex;
1020  switch (VT.getSimpleVT().SimpleTy) {
1021  default: llvm_unreachable("unhandled vld type");
1022    // Double-register operations:
1023  case MVT::v8i8:  OpcodeIndex = 0; break;
1024  case MVT::v4i16: OpcodeIndex = 1; break;
1025  case MVT::v2f32:
1026  case MVT::v2i32: OpcodeIndex = 2; break;
1027  case MVT::v1i64: OpcodeIndex = 3; break;
1028    // Quad-register operations:
1029  case MVT::v16i8: OpcodeIndex = 0; break;
1030  case MVT::v8i16: OpcodeIndex = 1; break;
1031  case MVT::v4f32:
1032  case MVT::v4i32: OpcodeIndex = 2; break;
1033  case MVT::v2i64: OpcodeIndex = 3;
1034    assert(NumVecs == 1 && "v2i64 type only supported for VLD1");
1035    break;
1036  }
1037
1038  EVT ResTy;
1039  if (NumVecs == 1)
1040    ResTy = VT;
1041  else {
1042    unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
1043    if (!is64BitVector)
1044      ResTyElts *= 2;
1045    ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts);
1046  }
1047
1048  SDValue Pred = getAL(CurDAG);
1049  SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1050  SDValue SuperReg;
1051  if (is64BitVector) {
1052    unsigned Opc = DOpcodes[OpcodeIndex];
1053    const SDValue Ops[] = { MemAddr, Align, Pred, Reg0, Chain };
1054    SDNode *VLd = CurDAG->getMachineNode(Opc, dl, ResTy, MVT::Other, Ops, 5);
1055    if (NumVecs == 1)
1056      return VLd;
1057
1058    SuperReg = SDValue(VLd, 0);
1059    assert(ARM::dsub_7 == ARM::dsub_0+7 && "Unexpected subreg numbering");
1060    for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
1061      SDValue D = CurDAG->getTargetExtractSubreg(ARM::dsub_0+Vec,
1062                                                 dl, VT, SuperReg);
1063      ReplaceUses(SDValue(N, Vec), D);
1064    }
1065    ReplaceUses(SDValue(N, NumVecs), SDValue(VLd, 1));
1066    return NULL;
1067  }
1068
1069  if (NumVecs <= 2) {
1070    // Quad registers are directly supported for VLD1 and VLD2,
1071    // loading pairs of D regs.
1072    unsigned Opc = QOpcodes0[OpcodeIndex];
1073    const SDValue Ops[] = { MemAddr, Align, Pred, Reg0, Chain };
1074    SDNode *VLd = CurDAG->getMachineNode(Opc, dl, ResTy, MVT::Other, Ops, 5);
1075    if (NumVecs == 1)
1076      return VLd;
1077
1078    SuperReg = SDValue(VLd, 0);
1079    Chain = SDValue(VLd, 1);
1080
1081  } else {
1082    // Otherwise, quad registers are loaded with two separate instructions,
1083    // where one loads the even registers and the other loads the odd registers.
1084    EVT AddrTy = MemAddr.getValueType();
1085
1086    // Load the even subregs.
1087    unsigned Opc = QOpcodes0[OpcodeIndex];
1088    SDValue ImplDef =
1089      SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, ResTy), 0);
1090    const SDValue OpsA[] = { MemAddr, Align, Reg0, ImplDef, Pred, Reg0, Chain };
1091    SDNode *VLdA =
1092      CurDAG->getMachineNode(Opc, dl, ResTy, AddrTy, MVT::Other, OpsA, 7);
1093    Chain = SDValue(VLdA, 2);
1094
1095    // Load the odd subregs.
1096    Opc = QOpcodes1[OpcodeIndex];
1097    const SDValue OpsB[] = { SDValue(VLdA, 1), Align, Reg0, SDValue(VLdA, 0),
1098                             Pred, Reg0, Chain };
1099    SDNode *VLdB =
1100      CurDAG->getMachineNode(Opc, dl, ResTy, AddrTy, MVT::Other, OpsB, 7);
1101    SuperReg = SDValue(VLdB, 0);
1102    Chain = SDValue(VLdB, 2);
1103  }
1104
1105  // Extract out the Q registers.
1106  assert(ARM::qsub_3 == ARM::qsub_0+3 && "Unexpected subreg numbering");
1107  for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
1108    SDValue Q = CurDAG->getTargetExtractSubreg(ARM::qsub_0+Vec,
1109                                               dl, VT, SuperReg);
1110    ReplaceUses(SDValue(N, Vec), Q);
1111  }
1112  ReplaceUses(SDValue(N, NumVecs), Chain);
1113  return NULL;
1114}
1115
1116SDNode *ARMDAGToDAGISel::SelectVST(SDNode *N, unsigned NumVecs,
1117                                   unsigned *DOpcodes, unsigned *QOpcodes0,
1118                                   unsigned *QOpcodes1) {
1119  assert(NumVecs >= 1 && NumVecs <= 4 && "VST NumVecs out-of-range");
1120  DebugLoc dl = N->getDebugLoc();
1121
1122  SDValue MemAddr, Align;
1123  if (!SelectAddrMode6(N->getOperand(2), MemAddr, Align))
1124    return NULL;
1125
1126  SDValue Chain = N->getOperand(0);
1127  EVT VT = N->getOperand(3).getValueType();
1128  bool is64BitVector = VT.is64BitVector();
1129
1130  unsigned OpcodeIndex;
1131  switch (VT.getSimpleVT().SimpleTy) {
1132  default: llvm_unreachable("unhandled vst type");
1133    // Double-register operations:
1134  case MVT::v8i8:  OpcodeIndex = 0; break;
1135  case MVT::v4i16: OpcodeIndex = 1; break;
1136  case MVT::v2f32:
1137  case MVT::v2i32: OpcodeIndex = 2; break;
1138  case MVT::v1i64: OpcodeIndex = 3; break;
1139    // Quad-register operations:
1140  case MVT::v16i8: OpcodeIndex = 0; break;
1141  case MVT::v8i16: OpcodeIndex = 1; break;
1142  case MVT::v4f32:
1143  case MVT::v4i32: OpcodeIndex = 2; break;
1144  case MVT::v2i64: OpcodeIndex = 3;
1145    assert(NumVecs == 1 && "v2i64 type only supported for VST1");
1146    break;
1147  }
1148
1149  SDValue Pred = getAL(CurDAG);
1150  SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1151
1152  SmallVector<SDValue, 7> Ops;
1153  Ops.push_back(MemAddr);
1154  Ops.push_back(Align);
1155
1156  if (is64BitVector) {
1157    if (NumVecs == 1) {
1158      Ops.push_back(N->getOperand(3));
1159    } else {
1160      SDValue RegSeq;
1161      SDValue V0 = N->getOperand(0+3);
1162      SDValue V1 = N->getOperand(1+3);
1163
1164      // Form a REG_SEQUENCE to force register allocation.
1165      if (NumVecs == 2)
1166        RegSeq = SDValue(PairDRegs(MVT::v2i64, V0, V1), 0);
1167      else {
1168        SDValue V2 = N->getOperand(2+3);
1169        // If it's a vld3, form a quad D-register and leave the last part as
1170        // an undef.
1171        SDValue V3 = (NumVecs == 3)
1172          ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
1173          : N->getOperand(3+3);
1174        RegSeq = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1175      }
1176      Ops.push_back(RegSeq);
1177    }
1178    Ops.push_back(Pred);
1179    Ops.push_back(Reg0); // predicate register
1180    Ops.push_back(Chain);
1181    unsigned Opc = DOpcodes[OpcodeIndex];
1182    return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 6);
1183  }
1184
1185  if (NumVecs <= 2) {
1186    // Quad registers are directly supported for VST1 and VST2.
1187    unsigned Opc = QOpcodes0[OpcodeIndex];
1188    if (NumVecs == 1) {
1189      Ops.push_back(N->getOperand(3));
1190    } else {
1191      // Form a QQ register.
1192      SDValue Q0 = N->getOperand(3);
1193      SDValue Q1 = N->getOperand(4);
1194      Ops.push_back(SDValue(PairQRegs(MVT::v4i64, Q0, Q1), 0));
1195    }
1196    Ops.push_back(Pred);
1197    Ops.push_back(Reg0); // predicate register
1198    Ops.push_back(Chain);
1199    return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 6);
1200  }
1201
1202  // Otherwise, quad registers are stored with two separate instructions,
1203  // where one stores the even registers and the other stores the odd registers.
1204
1205  // Form the QQQQ REG_SEQUENCE.
1206  SDValue V0 = N->getOperand(0+3);
1207  SDValue V1 = N->getOperand(1+3);
1208  SDValue V2 = N->getOperand(2+3);
1209  SDValue V3 = (NumVecs == 3)
1210    ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
1211    : N->getOperand(3+3);
1212  SDValue RegSeq = SDValue(QuadQRegs(MVT::v8i64, V0, V1, V2, V3), 0);
1213
1214  // Store the even D registers.
1215  Ops.push_back(Reg0); // post-access address offset
1216  Ops.push_back(RegSeq);
1217  Ops.push_back(Pred);
1218  Ops.push_back(Reg0); // predicate register
1219  Ops.push_back(Chain);
1220  unsigned Opc = QOpcodes0[OpcodeIndex];
1221  SDNode *VStA = CurDAG->getMachineNode(Opc, dl, MemAddr.getValueType(),
1222                                        MVT::Other, Ops.data(), 7);
1223  Chain = SDValue(VStA, 1);
1224
1225  // Store the odd D registers.
1226  Ops[0] = SDValue(VStA, 0); // MemAddr
1227  Ops[6] = Chain;
1228  Opc = QOpcodes1[OpcodeIndex];
1229  SDNode *VStB = CurDAG->getMachineNode(Opc, dl, MemAddr.getValueType(),
1230                                        MVT::Other, Ops.data(), 7);
1231  Chain = SDValue(VStB, 1);
1232  ReplaceUses(SDValue(N, 0), Chain);
1233  return NULL;
1234}
1235
1236SDNode *ARMDAGToDAGISel::SelectVLDSTLane(SDNode *N, bool IsLoad,
1237                                         unsigned NumVecs, unsigned *DOpcodes,
1238                                         unsigned *QOpcodes) {
1239  assert(NumVecs >=2 && NumVecs <= 4 && "VLDSTLane NumVecs out-of-range");
1240  DebugLoc dl = N->getDebugLoc();
1241
1242  SDValue MemAddr, Align;
1243  if (!SelectAddrMode6(N->getOperand(2), MemAddr, Align))
1244    return NULL;
1245
1246  SDValue Chain = N->getOperand(0);
1247  unsigned Lane =
1248    cast<ConstantSDNode>(N->getOperand(NumVecs+3))->getZExtValue();
1249  EVT VT = IsLoad ? N->getValueType(0) : N->getOperand(3).getValueType();
1250  bool is64BitVector = VT.is64BitVector();
1251
1252  unsigned OpcodeIndex;
1253  switch (VT.getSimpleVT().SimpleTy) {
1254  default: llvm_unreachable("unhandled vld/vst lane type");
1255    // Double-register operations:
1256  case MVT::v8i8:  OpcodeIndex = 0; break;
1257  case MVT::v4i16: OpcodeIndex = 1; break;
1258  case MVT::v2f32:
1259  case MVT::v2i32: OpcodeIndex = 2; break;
1260    // Quad-register operations:
1261  case MVT::v8i16: OpcodeIndex = 0; break;
1262  case MVT::v4f32:
1263  case MVT::v4i32: OpcodeIndex = 1; break;
1264  }
1265
1266  SDValue Pred = getAL(CurDAG);
1267  SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1268
1269  SmallVector<SDValue, 7> Ops;
1270  Ops.push_back(MemAddr);
1271  Ops.push_back(Align);
1272
1273  unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
1274                                  QOpcodes[OpcodeIndex]);
1275
1276  SDValue SuperReg;
1277  SDValue V0 = N->getOperand(0+3);
1278  SDValue V1 = N->getOperand(1+3);
1279  if (NumVecs == 2) {
1280    if (is64BitVector)
1281      SuperReg = SDValue(PairDRegs(MVT::v2i64, V0, V1), 0);
1282    else
1283      SuperReg = SDValue(PairQRegs(MVT::v4i64, V0, V1), 0);
1284  } else {
1285    SDValue V2 = N->getOperand(2+3);
1286    SDValue V3 = (NumVecs == 3)
1287      ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
1288      : N->getOperand(3+3);
1289    if (is64BitVector)
1290      SuperReg = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1291    else
1292      SuperReg = SDValue(QuadQRegs(MVT::v8i64, V0, V1, V2, V3), 0);
1293  }
1294  Ops.push_back(SuperReg);
1295  Ops.push_back(getI32Imm(Lane));
1296  Ops.push_back(Pred);
1297  Ops.push_back(Reg0);
1298  Ops.push_back(Chain);
1299
1300  if (!IsLoad)
1301    return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 7);
1302
1303  EVT ResTy;
1304  unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
1305  if (!is64BitVector)
1306    ResTyElts *= 2;
1307  ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts);
1308
1309  SDNode *VLdLn = CurDAG->getMachineNode(Opc, dl, ResTy, MVT::Other,
1310                                         Ops.data(), 7);
1311  SuperReg = SDValue(VLdLn, 0);
1312  Chain = SDValue(VLdLn, 1);
1313
1314  // Extract the subregisters.
1315  assert(ARM::dsub_7 == ARM::dsub_0+7 && "Unexpected subreg numbering");
1316  assert(ARM::qsub_3 == ARM::qsub_0+3 && "Unexpected subreg numbering");
1317  unsigned SubIdx = is64BitVector ? ARM::dsub_0 : ARM::qsub_0;
1318  for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1319    ReplaceUses(SDValue(N, Vec),
1320                CurDAG->getTargetExtractSubreg(SubIdx+Vec, dl, VT, SuperReg));
1321  ReplaceUses(SDValue(N, NumVecs), Chain);
1322  return NULL;
1323}
1324
1325SDNode *ARMDAGToDAGISel::SelectVTBL(SDNode *N, bool IsExt, unsigned NumVecs,
1326                                    unsigned Opc) {
1327  assert(NumVecs >= 2 && NumVecs <= 4 && "VTBL NumVecs out-of-range");
1328  DebugLoc dl = N->getDebugLoc();
1329  EVT VT = N->getValueType(0);
1330  unsigned FirstTblReg = IsExt ? 2 : 1;
1331
1332  // Form a REG_SEQUENCE to force register allocation.
1333  SDValue RegSeq;
1334  SDValue V0 = N->getOperand(FirstTblReg + 0);
1335  SDValue V1 = N->getOperand(FirstTblReg + 1);
1336  if (NumVecs == 2)
1337    RegSeq = SDValue(PairDRegs(MVT::v16i8, V0, V1), 0);
1338  else {
1339    SDValue V2 = N->getOperand(FirstTblReg + 2);
1340    // If it's a vtbl3, form a quad D-register and leave the last part as
1341    // an undef.
1342    SDValue V3 = (NumVecs == 3)
1343      ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
1344      : N->getOperand(FirstTblReg + 3);
1345    RegSeq = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1346  }
1347
1348  SmallVector<SDValue, 6> Ops;
1349  if (IsExt)
1350    Ops.push_back(N->getOperand(1));
1351  Ops.push_back(RegSeq);
1352  Ops.push_back(N->getOperand(FirstTblReg + NumVecs));
1353  Ops.push_back(getAL(CurDAG)); // predicate
1354  Ops.push_back(CurDAG->getRegister(0, MVT::i32)); // predicate register
1355  return CurDAG->getMachineNode(Opc, dl, VT, Ops.data(), Ops.size());
1356}
1357
1358SDNode *ARMDAGToDAGISel::SelectV6T2BitfieldExtractOp(SDNode *N,
1359                                                     bool isSigned) {
1360  if (!Subtarget->hasV6T2Ops())
1361    return NULL;
1362
1363  unsigned Opc = isSigned ? (Subtarget->isThumb() ? ARM::t2SBFX : ARM::SBFX)
1364    : (Subtarget->isThumb() ? ARM::t2UBFX : ARM::UBFX);
1365
1366
1367  // For unsigned extracts, check for a shift right and mask
1368  unsigned And_imm = 0;
1369  if (N->getOpcode() == ISD::AND) {
1370    if (isOpcWithIntImmediate(N, ISD::AND, And_imm)) {
1371
1372      // The immediate is a mask of the low bits iff imm & (imm+1) == 0
1373      if (And_imm & (And_imm + 1))
1374        return NULL;
1375
1376      unsigned Srl_imm = 0;
1377      if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRL,
1378                                Srl_imm)) {
1379        assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
1380
1381        unsigned Width = CountTrailingOnes_32(And_imm);
1382        unsigned LSB = Srl_imm;
1383        SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1384        SDValue Ops[] = { N->getOperand(0).getOperand(0),
1385                          CurDAG->getTargetConstant(LSB, MVT::i32),
1386                          CurDAG->getTargetConstant(Width, MVT::i32),
1387          getAL(CurDAG), Reg0 };
1388        return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
1389      }
1390    }
1391    return NULL;
1392  }
1393
1394  // Otherwise, we're looking for a shift of a shift
1395  unsigned Shl_imm = 0;
1396  if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SHL, Shl_imm)) {
1397    assert(Shl_imm > 0 && Shl_imm < 32 && "bad amount in shift node!");
1398    unsigned Srl_imm = 0;
1399    if (isInt32Immediate(N->getOperand(1), Srl_imm)) {
1400      assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
1401      unsigned Width = 32 - Srl_imm;
1402      int LSB = Srl_imm - Shl_imm;
1403      if (LSB < 0)
1404        return NULL;
1405      SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1406      SDValue Ops[] = { N->getOperand(0).getOperand(0),
1407                        CurDAG->getTargetConstant(LSB, MVT::i32),
1408                        CurDAG->getTargetConstant(Width, MVT::i32),
1409                        getAL(CurDAG), Reg0 };
1410      return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
1411    }
1412  }
1413  return NULL;
1414}
1415
1416SDNode *ARMDAGToDAGISel::
1417SelectT2CMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
1418                    ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1419  SDValue CPTmp0;
1420  SDValue CPTmp1;
1421  if (SelectT2ShifterOperandReg(TrueVal, CPTmp0, CPTmp1)) {
1422    unsigned SOVal = cast<ConstantSDNode>(CPTmp1)->getZExtValue();
1423    unsigned SOShOp = ARM_AM::getSORegShOp(SOVal);
1424    unsigned Opc = 0;
1425    switch (SOShOp) {
1426    case ARM_AM::lsl: Opc = ARM::t2MOVCClsl; break;
1427    case ARM_AM::lsr: Opc = ARM::t2MOVCClsr; break;
1428    case ARM_AM::asr: Opc = ARM::t2MOVCCasr; break;
1429    case ARM_AM::ror: Opc = ARM::t2MOVCCror; break;
1430    default:
1431      llvm_unreachable("Unknown so_reg opcode!");
1432      break;
1433    }
1434    SDValue SOShImm =
1435      CurDAG->getTargetConstant(ARM_AM::getSORegOffset(SOVal), MVT::i32);
1436    SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1437    SDValue Ops[] = { FalseVal, CPTmp0, SOShImm, CC, CCR, InFlag };
1438    return CurDAG->SelectNodeTo(N, Opc, MVT::i32,Ops, 6);
1439  }
1440  return 0;
1441}
1442
1443SDNode *ARMDAGToDAGISel::
1444SelectARMCMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
1445                     ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1446  SDValue CPTmp0;
1447  SDValue CPTmp1;
1448  SDValue CPTmp2;
1449  if (SelectShifterOperandReg(TrueVal, CPTmp0, CPTmp1, CPTmp2)) {
1450    SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1451    SDValue Ops[] = { FalseVal, CPTmp0, CPTmp1, CPTmp2, CC, CCR, InFlag };
1452    return CurDAG->SelectNodeTo(N, ARM::MOVCCs, MVT::i32, Ops, 7);
1453  }
1454  return 0;
1455}
1456
1457SDNode *ARMDAGToDAGISel::
1458SelectT2CMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
1459                    ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1460  ConstantSDNode *T = dyn_cast<ConstantSDNode>(TrueVal);
1461  if (!T)
1462    return 0;
1463
1464  if (Pred_t2_so_imm(TrueVal.getNode())) {
1465    SDValue True = CurDAG->getTargetConstant(T->getZExtValue(), MVT::i32);
1466    SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1467    SDValue Ops[] = { FalseVal, True, CC, CCR, InFlag };
1468    return CurDAG->SelectNodeTo(N,
1469                                ARM::t2MOVCCi, MVT::i32, Ops, 5);
1470  }
1471  return 0;
1472}
1473
1474SDNode *ARMDAGToDAGISel::
1475SelectARMCMOVSoImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
1476                     ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1477  ConstantSDNode *T = dyn_cast<ConstantSDNode>(TrueVal);
1478  if (!T)
1479    return 0;
1480
1481  if (Pred_so_imm(TrueVal.getNode())) {
1482    SDValue True = CurDAG->getTargetConstant(T->getZExtValue(), MVT::i32);
1483    SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1484    SDValue Ops[] = { FalseVal, True, CC, CCR, InFlag };
1485    return CurDAG->SelectNodeTo(N,
1486                                ARM::MOVCCi, MVT::i32, Ops, 5);
1487  }
1488  return 0;
1489}
1490
1491SDNode *ARMDAGToDAGISel::SelectCMOVOp(SDNode *N) {
1492  EVT VT = N->getValueType(0);
1493  SDValue FalseVal = N->getOperand(0);
1494  SDValue TrueVal  = N->getOperand(1);
1495  SDValue CC = N->getOperand(2);
1496  SDValue CCR = N->getOperand(3);
1497  SDValue InFlag = N->getOperand(4);
1498  assert(CC.getOpcode() == ISD::Constant);
1499  assert(CCR.getOpcode() == ISD::Register);
1500  ARMCC::CondCodes CCVal =
1501    (ARMCC::CondCodes)cast<ConstantSDNode>(CC)->getZExtValue();
1502
1503  if (!Subtarget->isThumb1Only() && VT == MVT::i32) {
1504    // Pattern: (ARMcmov:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
1505    // Emits: (MOVCCs:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
1506    // Pattern complexity = 18  cost = 1  size = 0
1507    SDValue CPTmp0;
1508    SDValue CPTmp1;
1509    SDValue CPTmp2;
1510    if (Subtarget->isThumb()) {
1511      SDNode *Res = SelectT2CMOVShiftOp(N, FalseVal, TrueVal,
1512                                        CCVal, CCR, InFlag);
1513      if (!Res)
1514        Res = SelectT2CMOVShiftOp(N, TrueVal, FalseVal,
1515                               ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1516      if (Res)
1517        return Res;
1518    } else {
1519      SDNode *Res = SelectARMCMOVShiftOp(N, FalseVal, TrueVal,
1520                                         CCVal, CCR, InFlag);
1521      if (!Res)
1522        Res = SelectARMCMOVShiftOp(N, TrueVal, FalseVal,
1523                               ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1524      if (Res)
1525        return Res;
1526    }
1527
1528    // Pattern: (ARMcmov:i32 GPR:i32:$false,
1529    //             (imm:i32)<<P:Pred_so_imm>>:$true,
1530    //             (imm:i32):$cc)
1531    // Emits: (MOVCCi:i32 GPR:i32:$false,
1532    //           (so_imm:i32 (imm:i32):$true), (imm:i32):$cc)
1533    // Pattern complexity = 10  cost = 1  size = 0
1534    if (Subtarget->isThumb()) {
1535      SDNode *Res = SelectT2CMOVSoImmOp(N, FalseVal, TrueVal,
1536                                        CCVal, CCR, InFlag);
1537      if (!Res)
1538        Res = SelectT2CMOVSoImmOp(N, TrueVal, FalseVal,
1539                               ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1540      if (Res)
1541        return Res;
1542    } else {
1543      SDNode *Res = SelectARMCMOVSoImmOp(N, FalseVal, TrueVal,
1544                                         CCVal, CCR, InFlag);
1545      if (!Res)
1546        Res = SelectARMCMOVSoImmOp(N, TrueVal, FalseVal,
1547                               ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1548      if (Res)
1549        return Res;
1550    }
1551  }
1552
1553  // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1554  // Emits: (MOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1555  // Pattern complexity = 6  cost = 1  size = 0
1556  //
1557  // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1558  // Emits: (tMOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1559  // Pattern complexity = 6  cost = 11  size = 0
1560  //
1561  // Also FCPYScc and FCPYDcc.
1562  SDValue Tmp2 = CurDAG->getTargetConstant(CCVal, MVT::i32);
1563  SDValue Ops[] = { FalseVal, TrueVal, Tmp2, CCR, InFlag };
1564  unsigned Opc = 0;
1565  switch (VT.getSimpleVT().SimpleTy) {
1566  default: assert(false && "Illegal conditional move type!");
1567    break;
1568  case MVT::i32:
1569    Opc = Subtarget->isThumb()
1570      ? (Subtarget->hasThumb2() ? ARM::t2MOVCCr : ARM::tMOVCCr_pseudo)
1571      : ARM::MOVCCr;
1572    break;
1573  case MVT::f32:
1574    Opc = ARM::VMOVScc;
1575    break;
1576  case MVT::f64:
1577    Opc = ARM::VMOVDcc;
1578    break;
1579  }
1580  return CurDAG->SelectNodeTo(N, Opc, VT, Ops, 5);
1581}
1582
1583SDNode *ARMDAGToDAGISel::SelectConcatVector(SDNode *N) {
1584  // The only time a CONCAT_VECTORS operation can have legal types is when
1585  // two 64-bit vectors are concatenated to a 128-bit vector.
1586  EVT VT = N->getValueType(0);
1587  if (!VT.is128BitVector() || N->getNumOperands() != 2)
1588    llvm_unreachable("unexpected CONCAT_VECTORS");
1589  DebugLoc dl = N->getDebugLoc();
1590  SDValue V0 = N->getOperand(0);
1591  SDValue V1 = N->getOperand(1);
1592  SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
1593  SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
1594  const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
1595  return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
1596}
1597
1598SDNode *ARMDAGToDAGISel::Select(SDNode *N) {
1599  DebugLoc dl = N->getDebugLoc();
1600
1601  if (N->isMachineOpcode())
1602    return NULL;   // Already selected.
1603
1604  switch (N->getOpcode()) {
1605  default: break;
1606  case ISD::Constant: {
1607    unsigned Val = cast<ConstantSDNode>(N)->getZExtValue();
1608    bool UseCP = true;
1609    if (Subtarget->hasThumb2())
1610      // Thumb2-aware targets have the MOVT instruction, so all immediates can
1611      // be done with MOV + MOVT, at worst.
1612      UseCP = 0;
1613    else {
1614      if (Subtarget->isThumb()) {
1615        UseCP = (Val > 255 &&                          // MOV
1616                 ~Val > 255 &&                         // MOV + MVN
1617                 !ARM_AM::isThumbImmShiftedVal(Val));  // MOV + LSL
1618      } else
1619        UseCP = (ARM_AM::getSOImmVal(Val) == -1 &&     // MOV
1620                 ARM_AM::getSOImmVal(~Val) == -1 &&    // MVN
1621                 !ARM_AM::isSOImmTwoPartVal(Val));     // two instrs.
1622    }
1623
1624    if (UseCP) {
1625      SDValue CPIdx =
1626        CurDAG->getTargetConstantPool(ConstantInt::get(
1627                                  Type::getInt32Ty(*CurDAG->getContext()), Val),
1628                                      TLI.getPointerTy());
1629
1630      SDNode *ResNode;
1631      if (Subtarget->isThumb1Only()) {
1632        SDValue Pred = getAL(CurDAG);
1633        SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1634        SDValue Ops[] = { CPIdx, Pred, PredReg, CurDAG->getEntryNode() };
1635        ResNode = CurDAG->getMachineNode(ARM::tLDRcp, dl, MVT::i32, MVT::Other,
1636                                         Ops, 4);
1637      } else {
1638        SDValue Ops[] = {
1639          CPIdx,
1640          CurDAG->getRegister(0, MVT::i32),
1641          CurDAG->getTargetConstant(0, MVT::i32),
1642          getAL(CurDAG),
1643          CurDAG->getRegister(0, MVT::i32),
1644          CurDAG->getEntryNode()
1645        };
1646        ResNode=CurDAG->getMachineNode(ARM::LDRcp, dl, MVT::i32, MVT::Other,
1647                                       Ops, 6);
1648      }
1649      ReplaceUses(SDValue(N, 0), SDValue(ResNode, 0));
1650      return NULL;
1651    }
1652
1653    // Other cases are autogenerated.
1654    break;
1655  }
1656  case ISD::FrameIndex: {
1657    // Selects to ADDri FI, 0 which in turn will become ADDri SP, imm.
1658    int FI = cast<FrameIndexSDNode>(N)->getIndex();
1659    SDValue TFI = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
1660    if (Subtarget->isThumb1Only()) {
1661      return CurDAG->SelectNodeTo(N, ARM::tADDrSPi, MVT::i32, TFI,
1662                                  CurDAG->getTargetConstant(0, MVT::i32));
1663    } else {
1664      unsigned Opc = ((Subtarget->isThumb() && Subtarget->hasThumb2()) ?
1665                      ARM::t2ADDri : ARM::ADDri);
1666      SDValue Ops[] = { TFI, CurDAG->getTargetConstant(0, MVT::i32),
1667                        getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1668                        CurDAG->getRegister(0, MVT::i32) };
1669      return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
1670    }
1671  }
1672  case ISD::SRL:
1673    if (SDNode *I = SelectV6T2BitfieldExtractOp(N, false))
1674      return I;
1675    break;
1676  case ISD::SRA:
1677    if (SDNode *I = SelectV6T2BitfieldExtractOp(N, true))
1678      return I;
1679    break;
1680  case ISD::MUL:
1681    if (Subtarget->isThumb1Only())
1682      break;
1683    if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1684      unsigned RHSV = C->getZExtValue();
1685      if (!RHSV) break;
1686      if (isPowerOf2_32(RHSV-1)) {  // 2^n+1?
1687        unsigned ShImm = Log2_32(RHSV-1);
1688        if (ShImm >= 32)
1689          break;
1690        SDValue V = N->getOperand(0);
1691        ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
1692        SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, MVT::i32);
1693        SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1694        if (Subtarget->isThumb()) {
1695          SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
1696          return CurDAG->SelectNodeTo(N, ARM::t2ADDrs, MVT::i32, Ops, 6);
1697        } else {
1698          SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
1699          return CurDAG->SelectNodeTo(N, ARM::ADDrs, MVT::i32, Ops, 7);
1700        }
1701      }
1702      if (isPowerOf2_32(RHSV+1)) {  // 2^n-1?
1703        unsigned ShImm = Log2_32(RHSV+1);
1704        if (ShImm >= 32)
1705          break;
1706        SDValue V = N->getOperand(0);
1707        ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
1708        SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, MVT::i32);
1709        SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1710        if (Subtarget->isThumb()) {
1711          SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
1712          return CurDAG->SelectNodeTo(N, ARM::t2RSBrs, MVT::i32, Ops, 6);
1713        } else {
1714          SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
1715          return CurDAG->SelectNodeTo(N, ARM::RSBrs, MVT::i32, Ops, 7);
1716        }
1717      }
1718    }
1719    break;
1720  case ISD::AND: {
1721    // Check for unsigned bitfield extract
1722    if (SDNode *I = SelectV6T2BitfieldExtractOp(N, false))
1723      return I;
1724
1725    // (and (or x, c2), c1) and top 16-bits of c1 and c2 match, lower 16-bits
1726    // of c1 are 0xffff, and lower 16-bit of c2 are 0. That is, the top 16-bits
1727    // are entirely contributed by c2 and lower 16-bits are entirely contributed
1728    // by x. That's equal to (or (and x, 0xffff), (and c1, 0xffff0000)).
1729    // Select it to: "movt x, ((c1 & 0xffff) >> 16)
1730    EVT VT = N->getValueType(0);
1731    if (VT != MVT::i32)
1732      break;
1733    unsigned Opc = (Subtarget->isThumb() && Subtarget->hasThumb2())
1734      ? ARM::t2MOVTi16
1735      : (Subtarget->hasV6T2Ops() ? ARM::MOVTi16 : 0);
1736    if (!Opc)
1737      break;
1738    SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
1739    ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1740    if (!N1C)
1741      break;
1742    if (N0.getOpcode() == ISD::OR && N0.getNode()->hasOneUse()) {
1743      SDValue N2 = N0.getOperand(1);
1744      ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1745      if (!N2C)
1746        break;
1747      unsigned N1CVal = N1C->getZExtValue();
1748      unsigned N2CVal = N2C->getZExtValue();
1749      if ((N1CVal & 0xffff0000U) == (N2CVal & 0xffff0000U) &&
1750          (N1CVal & 0xffffU) == 0xffffU &&
1751          (N2CVal & 0xffffU) == 0x0U) {
1752        SDValue Imm16 = CurDAG->getTargetConstant((N2CVal & 0xFFFF0000U) >> 16,
1753                                                  MVT::i32);
1754        SDValue Ops[] = { N0.getOperand(0), Imm16,
1755                          getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
1756        return CurDAG->getMachineNode(Opc, dl, VT, Ops, 4);
1757      }
1758    }
1759    break;
1760  }
1761  case ARMISD::VMOVRRD:
1762    return CurDAG->getMachineNode(ARM::VMOVRRD, dl, MVT::i32, MVT::i32,
1763                                  N->getOperand(0), getAL(CurDAG),
1764                                  CurDAG->getRegister(0, MVT::i32));
1765  case ISD::UMUL_LOHI: {
1766    if (Subtarget->isThumb1Only())
1767      break;
1768    if (Subtarget->isThumb()) {
1769      SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
1770                        getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1771                        CurDAG->getRegister(0, MVT::i32) };
1772      return CurDAG->getMachineNode(ARM::t2UMULL, dl, MVT::i32, MVT::i32,Ops,4);
1773    } else {
1774      SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
1775                        getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1776                        CurDAG->getRegister(0, MVT::i32) };
1777      return CurDAG->getMachineNode(ARM::UMULL, dl, MVT::i32, MVT::i32, Ops, 5);
1778    }
1779  }
1780  case ISD::SMUL_LOHI: {
1781    if (Subtarget->isThumb1Only())
1782      break;
1783    if (Subtarget->isThumb()) {
1784      SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
1785                        getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
1786      return CurDAG->getMachineNode(ARM::t2SMULL, dl, MVT::i32, MVT::i32,Ops,4);
1787    } else {
1788      SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
1789                        getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1790                        CurDAG->getRegister(0, MVT::i32) };
1791      return CurDAG->getMachineNode(ARM::SMULL, dl, MVT::i32, MVT::i32, Ops, 5);
1792    }
1793  }
1794  case ISD::LOAD: {
1795    SDNode *ResNode = 0;
1796    if (Subtarget->isThumb() && Subtarget->hasThumb2())
1797      ResNode = SelectT2IndexedLoad(N);
1798    else
1799      ResNode = SelectARMIndexedLoad(N);
1800    if (ResNode)
1801      return ResNode;
1802    // Other cases are autogenerated.
1803    break;
1804  }
1805  case ARMISD::BRCOND: {
1806    // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
1807    // Emits: (Bcc:void (bb:Other):$dst, (imm:i32):$cc)
1808    // Pattern complexity = 6  cost = 1  size = 0
1809
1810    // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
1811    // Emits: (tBcc:void (bb:Other):$dst, (imm:i32):$cc)
1812    // Pattern complexity = 6  cost = 1  size = 0
1813
1814    // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
1815    // Emits: (t2Bcc:void (bb:Other):$dst, (imm:i32):$cc)
1816    // Pattern complexity = 6  cost = 1  size = 0
1817
1818    unsigned Opc = Subtarget->isThumb() ?
1819      ((Subtarget->hasThumb2()) ? ARM::t2Bcc : ARM::tBcc) : ARM::Bcc;
1820    SDValue Chain = N->getOperand(0);
1821    SDValue N1 = N->getOperand(1);
1822    SDValue N2 = N->getOperand(2);
1823    SDValue N3 = N->getOperand(3);
1824    SDValue InFlag = N->getOperand(4);
1825    assert(N1.getOpcode() == ISD::BasicBlock);
1826    assert(N2.getOpcode() == ISD::Constant);
1827    assert(N3.getOpcode() == ISD::Register);
1828
1829    SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
1830                               cast<ConstantSDNode>(N2)->getZExtValue()),
1831                               MVT::i32);
1832    SDValue Ops[] = { N1, Tmp2, N3, Chain, InFlag };
1833    SDNode *ResNode = CurDAG->getMachineNode(Opc, dl, MVT::Other,
1834                                             MVT::Flag, Ops, 5);
1835    Chain = SDValue(ResNode, 0);
1836    if (N->getNumValues() == 2) {
1837      InFlag = SDValue(ResNode, 1);
1838      ReplaceUses(SDValue(N, 1), InFlag);
1839    }
1840    ReplaceUses(SDValue(N, 0),
1841                SDValue(Chain.getNode(), Chain.getResNo()));
1842    return NULL;
1843  }
1844  case ARMISD::CMOV:
1845    return SelectCMOVOp(N);
1846  case ARMISD::CNEG: {
1847    EVT VT = N->getValueType(0);
1848    SDValue N0 = N->getOperand(0);
1849    SDValue N1 = N->getOperand(1);
1850    SDValue N2 = N->getOperand(2);
1851    SDValue N3 = N->getOperand(3);
1852    SDValue InFlag = N->getOperand(4);
1853    assert(N2.getOpcode() == ISD::Constant);
1854    assert(N3.getOpcode() == ISD::Register);
1855
1856    SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
1857                               cast<ConstantSDNode>(N2)->getZExtValue()),
1858                               MVT::i32);
1859    SDValue Ops[] = { N0, N1, Tmp2, N3, InFlag };
1860    unsigned Opc = 0;
1861    switch (VT.getSimpleVT().SimpleTy) {
1862    default: assert(false && "Illegal conditional move type!");
1863      break;
1864    case MVT::f32:
1865      Opc = ARM::VNEGScc;
1866      break;
1867    case MVT::f64:
1868      Opc = ARM::VNEGDcc;
1869      break;
1870    }
1871    return CurDAG->SelectNodeTo(N, Opc, VT, Ops, 5);
1872  }
1873
1874  case ARMISD::VZIP: {
1875    unsigned Opc = 0;
1876    EVT VT = N->getValueType(0);
1877    switch (VT.getSimpleVT().SimpleTy) {
1878    default: return NULL;
1879    case MVT::v8i8:  Opc = ARM::VZIPd8; break;
1880    case MVT::v4i16: Opc = ARM::VZIPd16; break;
1881    case MVT::v2f32:
1882    case MVT::v2i32: Opc = ARM::VZIPd32; break;
1883    case MVT::v16i8: Opc = ARM::VZIPq8; break;
1884    case MVT::v8i16: Opc = ARM::VZIPq16; break;
1885    case MVT::v4f32:
1886    case MVT::v4i32: Opc = ARM::VZIPq32; break;
1887    }
1888    SDValue Pred = getAL(CurDAG);
1889    SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1890    SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
1891    return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
1892  }
1893  case ARMISD::VUZP: {
1894    unsigned Opc = 0;
1895    EVT VT = N->getValueType(0);
1896    switch (VT.getSimpleVT().SimpleTy) {
1897    default: return NULL;
1898    case MVT::v8i8:  Opc = ARM::VUZPd8; break;
1899    case MVT::v4i16: Opc = ARM::VUZPd16; break;
1900    case MVT::v2f32:
1901    case MVT::v2i32: Opc = ARM::VUZPd32; break;
1902    case MVT::v16i8: Opc = ARM::VUZPq8; break;
1903    case MVT::v8i16: Opc = ARM::VUZPq16; break;
1904    case MVT::v4f32:
1905    case MVT::v4i32: Opc = ARM::VUZPq32; break;
1906    }
1907    SDValue Pred = getAL(CurDAG);
1908    SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1909    SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
1910    return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
1911  }
1912  case ARMISD::VTRN: {
1913    unsigned Opc = 0;
1914    EVT VT = N->getValueType(0);
1915    switch (VT.getSimpleVT().SimpleTy) {
1916    default: return NULL;
1917    case MVT::v8i8:  Opc = ARM::VTRNd8; break;
1918    case MVT::v4i16: Opc = ARM::VTRNd16; break;
1919    case MVT::v2f32:
1920    case MVT::v2i32: Opc = ARM::VTRNd32; break;
1921    case MVT::v16i8: Opc = ARM::VTRNq8; break;
1922    case MVT::v8i16: Opc = ARM::VTRNq16; break;
1923    case MVT::v4f32:
1924    case MVT::v4i32: Opc = ARM::VTRNq32; break;
1925    }
1926    SDValue Pred = getAL(CurDAG);
1927    SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1928    SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
1929    return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
1930  }
1931  case ARMISD::BUILD_VECTOR: {
1932    EVT VecVT = N->getValueType(0);
1933    EVT EltVT = VecVT.getVectorElementType();
1934    unsigned NumElts = VecVT.getVectorNumElements();
1935    if (EltVT.getSimpleVT() == MVT::f64) {
1936      assert(NumElts == 2 && "unexpected type for BUILD_VECTOR");
1937      return PairDRegs(VecVT, N->getOperand(0), N->getOperand(1));
1938    }
1939    assert(EltVT.getSimpleVT() == MVT::f32 &&
1940           "unexpected type for BUILD_VECTOR");
1941    if (NumElts == 2)
1942      return PairSRegs(VecVT, N->getOperand(0), N->getOperand(1));
1943    assert(NumElts == 4 && "unexpected type for BUILD_VECTOR");
1944    return QuadSRegs(VecVT, N->getOperand(0), N->getOperand(1),
1945                     N->getOperand(2), N->getOperand(3));
1946  }
1947
1948  case ISD::INTRINSIC_VOID:
1949  case ISD::INTRINSIC_W_CHAIN: {
1950    unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
1951    switch (IntNo) {
1952    default:
1953      break;
1954
1955    case Intrinsic::arm_neon_vld1: {
1956      unsigned DOpcodes[] = { ARM::VLD1d8, ARM::VLD1d16,
1957                              ARM::VLD1d32, ARM::VLD1d64 };
1958      unsigned QOpcodes[] = { ARM::VLD1q8Pseudo, ARM::VLD1q16Pseudo,
1959                              ARM::VLD1q32Pseudo, ARM::VLD1q64Pseudo };
1960      return SelectVLD(N, 1, DOpcodes, QOpcodes, 0);
1961    }
1962
1963    case Intrinsic::arm_neon_vld2: {
1964      unsigned DOpcodes[] = { ARM::VLD2d8Pseudo, ARM::VLD2d16Pseudo,
1965                              ARM::VLD2d32Pseudo, ARM::VLD1q64Pseudo };
1966      unsigned QOpcodes[] = { ARM::VLD2q8Pseudo, ARM::VLD2q16Pseudo,
1967                              ARM::VLD2q32Pseudo };
1968      return SelectVLD(N, 2, DOpcodes, QOpcodes, 0);
1969    }
1970
1971    case Intrinsic::arm_neon_vld3: {
1972      unsigned DOpcodes[] = { ARM::VLD3d8Pseudo, ARM::VLD3d16Pseudo,
1973                              ARM::VLD3d32Pseudo, ARM::VLD1d64TPseudo };
1974      unsigned QOpcodes0[] = { ARM::VLD3q8Pseudo_UPD,
1975                               ARM::VLD3q16Pseudo_UPD,
1976                               ARM::VLD3q32Pseudo_UPD };
1977      unsigned QOpcodes1[] = { ARM::VLD3q8oddPseudo_UPD,
1978                               ARM::VLD3q16oddPseudo_UPD,
1979                               ARM::VLD3q32oddPseudo_UPD };
1980      return SelectVLD(N, 3, DOpcodes, QOpcodes0, QOpcodes1);
1981    }
1982
1983    case Intrinsic::arm_neon_vld4: {
1984      unsigned DOpcodes[] = { ARM::VLD4d8Pseudo, ARM::VLD4d16Pseudo,
1985                              ARM::VLD4d32Pseudo, ARM::VLD1d64QPseudo };
1986      unsigned QOpcodes0[] = { ARM::VLD4q8Pseudo_UPD,
1987                               ARM::VLD4q16Pseudo_UPD,
1988                               ARM::VLD4q32Pseudo_UPD };
1989      unsigned QOpcodes1[] = { ARM::VLD4q8oddPseudo_UPD,
1990                               ARM::VLD4q16oddPseudo_UPD,
1991                               ARM::VLD4q32oddPseudo_UPD };
1992      return SelectVLD(N, 4, DOpcodes, QOpcodes0, QOpcodes1);
1993    }
1994
1995    case Intrinsic::arm_neon_vld2lane: {
1996      unsigned DOpcodes[] = { ARM::VLD2LNd8Pseudo, ARM::VLD2LNd16Pseudo,
1997                              ARM::VLD2LNd32Pseudo };
1998      unsigned QOpcodes[] = { ARM::VLD2LNq16Pseudo, ARM::VLD2LNq32Pseudo };
1999      return SelectVLDSTLane(N, true, 2, DOpcodes, QOpcodes);
2000    }
2001
2002    case Intrinsic::arm_neon_vld3lane: {
2003      unsigned DOpcodes[] = { ARM::VLD3LNd8Pseudo, ARM::VLD3LNd16Pseudo,
2004                              ARM::VLD3LNd32Pseudo };
2005      unsigned QOpcodes[] = { ARM::VLD3LNq16Pseudo, ARM::VLD3LNq32Pseudo };
2006      return SelectVLDSTLane(N, true, 3, DOpcodes, QOpcodes);
2007    }
2008
2009    case Intrinsic::arm_neon_vld4lane: {
2010      unsigned DOpcodes[] = { ARM::VLD4LNd8Pseudo, ARM::VLD4LNd16Pseudo,
2011                              ARM::VLD4LNd32Pseudo };
2012      unsigned QOpcodes[] = { ARM::VLD4LNq16Pseudo, ARM::VLD4LNq32Pseudo };
2013      return SelectVLDSTLane(N, true, 4, DOpcodes, QOpcodes);
2014    }
2015
2016    case Intrinsic::arm_neon_vst1: {
2017      unsigned DOpcodes[] = { ARM::VST1d8, ARM::VST1d16,
2018                              ARM::VST1d32, ARM::VST1d64 };
2019      unsigned QOpcodes[] = { ARM::VST1q8Pseudo, ARM::VST1q16Pseudo,
2020                              ARM::VST1q32Pseudo, ARM::VST1q64Pseudo };
2021      return SelectVST(N, 1, DOpcodes, QOpcodes, 0);
2022    }
2023
2024    case Intrinsic::arm_neon_vst2: {
2025      unsigned DOpcodes[] = { ARM::VST2d8Pseudo, ARM::VST2d16Pseudo,
2026                              ARM::VST2d32Pseudo, ARM::VST1q64Pseudo };
2027      unsigned QOpcodes[] = { ARM::VST2q8Pseudo, ARM::VST2q16Pseudo,
2028                              ARM::VST2q32Pseudo };
2029      return SelectVST(N, 2, DOpcodes, QOpcodes, 0);
2030    }
2031
2032    case Intrinsic::arm_neon_vst3: {
2033      unsigned DOpcodes[] = { ARM::VST3d8Pseudo, ARM::VST3d16Pseudo,
2034                              ARM::VST3d32Pseudo, ARM::VST1d64TPseudo };
2035      unsigned QOpcodes0[] = { ARM::VST3q8Pseudo_UPD,
2036                               ARM::VST3q16Pseudo_UPD,
2037                               ARM::VST3q32Pseudo_UPD };
2038      unsigned QOpcodes1[] = { ARM::VST3q8oddPseudo_UPD,
2039                               ARM::VST3q16oddPseudo_UPD,
2040                               ARM::VST3q32oddPseudo_UPD };
2041      return SelectVST(N, 3, DOpcodes, QOpcodes0, QOpcodes1);
2042    }
2043
2044    case Intrinsic::arm_neon_vst4: {
2045      unsigned DOpcodes[] = { ARM::VST4d8Pseudo, ARM::VST4d16Pseudo,
2046                              ARM::VST4d32Pseudo, ARM::VST1d64QPseudo };
2047      unsigned QOpcodes0[] = { ARM::VST4q8Pseudo_UPD,
2048                               ARM::VST4q16Pseudo_UPD,
2049                               ARM::VST4q32Pseudo_UPD };
2050      unsigned QOpcodes1[] = { ARM::VST4q8oddPseudo_UPD,
2051                               ARM::VST4q16oddPseudo_UPD,
2052                               ARM::VST4q32oddPseudo_UPD };
2053      return SelectVST(N, 4, DOpcodes, QOpcodes0, QOpcodes1);
2054    }
2055
2056    case Intrinsic::arm_neon_vst2lane: {
2057      unsigned DOpcodes[] = { ARM::VST2LNd8Pseudo, ARM::VST2LNd16Pseudo,
2058                              ARM::VST2LNd32Pseudo };
2059      unsigned QOpcodes[] = { ARM::VST2LNq16Pseudo, ARM::VST2LNq32Pseudo };
2060      return SelectVLDSTLane(N, false, 2, DOpcodes, QOpcodes);
2061    }
2062
2063    case Intrinsic::arm_neon_vst3lane: {
2064      unsigned DOpcodes[] = { ARM::VST3LNd8Pseudo, ARM::VST3LNd16Pseudo,
2065                              ARM::VST3LNd32Pseudo };
2066      unsigned QOpcodes[] = { ARM::VST3LNq16Pseudo, ARM::VST3LNq32Pseudo };
2067      return SelectVLDSTLane(N, false, 3, DOpcodes, QOpcodes);
2068    }
2069
2070    case Intrinsic::arm_neon_vst4lane: {
2071      unsigned DOpcodes[] = { ARM::VST4LNd8Pseudo, ARM::VST4LNd16Pseudo,
2072                              ARM::VST4LNd32Pseudo };
2073      unsigned QOpcodes[] = { ARM::VST4LNq16Pseudo, ARM::VST4LNq32Pseudo };
2074      return SelectVLDSTLane(N, false, 4, DOpcodes, QOpcodes);
2075    }
2076    }
2077    break;
2078  }
2079
2080  case ISD::INTRINSIC_WO_CHAIN: {
2081    unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
2082    switch (IntNo) {
2083    default:
2084      break;
2085
2086    case Intrinsic::arm_neon_vtbl2:
2087      return SelectVTBL(N, false, 2, ARM::VTBL2Pseudo);
2088    case Intrinsic::arm_neon_vtbl3:
2089      return SelectVTBL(N, false, 3, ARM::VTBL3Pseudo);
2090    case Intrinsic::arm_neon_vtbl4:
2091      return SelectVTBL(N, false, 4, ARM::VTBL4Pseudo);
2092
2093    case Intrinsic::arm_neon_vtbx2:
2094      return SelectVTBL(N, true, 2, ARM::VTBX2Pseudo);
2095    case Intrinsic::arm_neon_vtbx3:
2096      return SelectVTBL(N, true, 3, ARM::VTBX3Pseudo);
2097    case Intrinsic::arm_neon_vtbx4:
2098      return SelectVTBL(N, true, 4, ARM::VTBX4Pseudo);
2099    }
2100    break;
2101  }
2102
2103  case ISD::CONCAT_VECTORS:
2104    return SelectConcatVector(N);
2105  }
2106
2107  return SelectCode(N);
2108}
2109
2110bool ARMDAGToDAGISel::
2111SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
2112                             std::vector<SDValue> &OutOps) {
2113  assert(ConstraintCode == 'm' && "unexpected asm memory constraint");
2114  // Require the address to be in a register.  That is safe for all ARM
2115  // variants and it is hard to do anything much smarter without knowing
2116  // how the operand is used.
2117  OutOps.push_back(Op);
2118  return false;
2119}
2120
2121/// createARMISelDag - This pass converts a legalized DAG into a
2122/// ARM-specific DAG, ready for instruction scheduling.
2123///
2124FunctionPass *llvm::createARMISelDag(ARMBaseTargetMachine &TM,
2125                                     CodeGenOpt::Level OptLevel) {
2126  return new ARMDAGToDAGISel(TM, OptLevel);
2127}
2128