This patch combines several changes from Evan Cheng for rdar://8659675.
[oota-llvm.git] / lib / Target / ARM / ARMISelDAGToDAG.cpp
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 "ARMBaseInstrInfo.h"
17 #include "ARMAddressingModes.h"
18 #include "ARMTargetMachine.h"
19 #include "llvm/CallingConv.h"
20 #include "llvm/Constants.h"
21 #include "llvm/DerivedTypes.h"
22 #include "llvm/Function.h"
23 #include "llvm/Intrinsics.h"
24 #include "llvm/LLVMContext.h"
25 #include "llvm/CodeGen/MachineFrameInfo.h"
26 #include "llvm/CodeGen/MachineFunction.h"
27 #include "llvm/CodeGen/MachineInstrBuilder.h"
28 #include "llvm/CodeGen/SelectionDAG.h"
29 #include "llvm/CodeGen/SelectionDAGISel.h"
30 #include "llvm/Target/TargetLowering.h"
31 #include "llvm/Target/TargetOptions.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/Compiler.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/raw_ostream.h"
37
38 using namespace llvm;
39
40 static cl::opt<bool>
41 DisableShifterOp("disable-shifter-op", cl::Hidden,
42   cl::desc("Disable isel of shifter-op"),
43   cl::init(false));
44
45 static cl::opt<bool>
46 CheckVMLxHazard("check-vmlx-hazard", cl::Hidden,
47   cl::desc("Check fp vmla / vmls hazard at isel time"),
48   cl::init(true));
49
50 //===--------------------------------------------------------------------===//
51 /// ARMDAGToDAGISel - ARM specific code to select ARM machine
52 /// instructions for SelectionDAG operations.
53 ///
54 namespace {
55
56 enum AddrMode2Type {
57   AM2_BASE, // Simple AM2 (+-imm12)
58   AM2_SHOP  // Shifter-op AM2
59 };
60
61 class ARMDAGToDAGISel : public SelectionDAGISel {
62   ARMBaseTargetMachine &TM;
63   const ARMBaseInstrInfo *TII;
64
65   /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
66   /// make the right decision when generating code for different targets.
67   const ARMSubtarget *Subtarget;
68
69 public:
70   explicit ARMDAGToDAGISel(ARMBaseTargetMachine &tm,
71                            CodeGenOpt::Level OptLevel)
72     : SelectionDAGISel(tm, OptLevel), TM(tm),
73       TII(static_cast<const ARMBaseInstrInfo*>(TM.getInstrInfo())),
74       Subtarget(&TM.getSubtarget<ARMSubtarget>()) {
75   }
76
77   virtual const char *getPassName() const {
78     return "ARM Instruction Selection";
79   }
80
81   /// getI32Imm - Return a target constant of type i32 with the specified
82   /// value.
83   inline SDValue getI32Imm(unsigned Imm) {
84     return CurDAG->getTargetConstant(Imm, MVT::i32);
85   }
86
87   SDNode *Select(SDNode *N);
88
89
90   bool hasNoVMLxHazardUse(SDNode *N) const;
91   bool isShifterOpProfitable(const SDValue &Shift,
92                              ARM_AM::ShiftOpc ShOpcVal, unsigned ShAmt);
93   bool SelectShifterOperandReg(SDValue N, SDValue &A,
94                                SDValue &B, SDValue &C,
95                                bool CheckProfitability = true);
96   bool SelectShiftShifterOperandReg(SDValue N, SDValue &A,
97                                     SDValue &B, SDValue &C) {
98     // Don't apply the profitability check
99     return SelectShifterOperandReg(N, A, B, C, false);
100   }
101
102   bool SelectAddrModeImm12(SDValue N, SDValue &Base, SDValue &OffImm);
103   bool SelectLdStSOReg(SDValue N, SDValue &Base, SDValue &Offset, SDValue &Opc);
104
105   AddrMode2Type SelectAddrMode2Worker(SDValue N, SDValue &Base,
106                                       SDValue &Offset, SDValue &Opc);
107   bool SelectAddrMode2Base(SDValue N, SDValue &Base, SDValue &Offset,
108                            SDValue &Opc) {
109     return SelectAddrMode2Worker(N, Base, Offset, Opc) == AM2_BASE;
110   }
111
112   bool SelectAddrMode2ShOp(SDValue N, SDValue &Base, SDValue &Offset,
113                            SDValue &Opc) {
114     return SelectAddrMode2Worker(N, Base, Offset, Opc) == AM2_SHOP;
115   }
116
117   bool SelectAddrMode2(SDValue N, SDValue &Base, SDValue &Offset,
118                        SDValue &Opc) {
119     SelectAddrMode2Worker(N, Base, Offset, Opc);
120 //    return SelectAddrMode2ShOp(N, Base, Offset, Opc);
121     // This always matches one way or another.
122     return true;
123   }
124
125   bool SelectAddrMode2Offset(SDNode *Op, SDValue N,
126                              SDValue &Offset, SDValue &Opc);
127   bool SelectAddrMode3(SDValue N, SDValue &Base,
128                        SDValue &Offset, SDValue &Opc);
129   bool SelectAddrMode3Offset(SDNode *Op, SDValue N,
130                              SDValue &Offset, SDValue &Opc);
131   bool SelectAddrMode5(SDValue N, SDValue &Base,
132                        SDValue &Offset);
133   bool SelectAddrMode6(SDNode *Parent, SDValue N, SDValue &Addr,SDValue &Align);
134   bool SelectAddrMode6Offset(SDNode *Op, SDValue N, SDValue &Offset);
135
136   bool SelectAddrModePC(SDValue N, SDValue &Offset, SDValue &Label);
137
138   // Thumb Addressing Modes:
139   bool SelectThumbAddrModeRR(SDValue N, SDValue &Base, SDValue &Offset);
140   bool SelectThumbAddrModeRI(SDValue N, SDValue &Base, SDValue &Offset,
141                              unsigned Scale);
142   bool SelectThumbAddrModeRI5S1(SDValue N, SDValue &Base, SDValue &Offset);
143   bool SelectThumbAddrModeRI5S2(SDValue N, SDValue &Base, SDValue &Offset);
144   bool SelectThumbAddrModeRI5S4(SDValue N, SDValue &Base, SDValue &Offset);
145   bool SelectThumbAddrModeImm5S(SDValue N, unsigned Scale, SDValue &Base,
146                                 SDValue &OffImm);
147   bool SelectThumbAddrModeImm5S1(SDValue N, SDValue &Base,
148                                  SDValue &OffImm);
149   bool SelectThumbAddrModeImm5S2(SDValue N, SDValue &Base,
150                                  SDValue &OffImm);
151   bool SelectThumbAddrModeImm5S4(SDValue N, SDValue &Base,
152                                  SDValue &OffImm);
153   bool SelectThumbAddrModeSP(SDValue N, SDValue &Base, SDValue &OffImm);
154
155   // Thumb 2 Addressing Modes:
156   bool SelectT2ShifterOperandReg(SDValue N,
157                                  SDValue &BaseReg, SDValue &Opc);
158   bool SelectT2AddrModeImm12(SDValue N, SDValue &Base, SDValue &OffImm);
159   bool SelectT2AddrModeImm8(SDValue N, SDValue &Base,
160                             SDValue &OffImm);
161   bool SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
162                                  SDValue &OffImm);
163   bool SelectT2AddrModeSoReg(SDValue N, SDValue &Base,
164                              SDValue &OffReg, SDValue &ShImm);
165
166   inline bool is_so_imm(unsigned Imm) const {
167     return ARM_AM::getSOImmVal(Imm) != -1;
168   }
169
170   inline bool is_so_imm_not(unsigned Imm) const {
171     return ARM_AM::getSOImmVal(~Imm) != -1;
172   }
173
174   inline bool is_t2_so_imm(unsigned Imm) const {
175     return ARM_AM::getT2SOImmVal(Imm) != -1;
176   }
177
178   inline bool is_t2_so_imm_not(unsigned Imm) const {
179     return ARM_AM::getT2SOImmVal(~Imm) != -1;
180   }
181
182   inline bool Pred_so_imm(SDNode *inN) const {
183     ConstantSDNode *N = cast<ConstantSDNode>(inN);
184     return is_so_imm(N->getZExtValue());
185   }
186
187   inline bool Pred_t2_so_imm(SDNode *inN) const {
188     ConstantSDNode *N = cast<ConstantSDNode>(inN);
189     return is_t2_so_imm(N->getZExtValue());
190   }
191
192   // Include the pieces autogenerated from the target description.
193 #include "ARMGenDAGISel.inc"
194
195 private:
196   /// SelectARMIndexedLoad - Indexed (pre/post inc/dec) load matching code for
197   /// ARM.
198   SDNode *SelectARMIndexedLoad(SDNode *N);
199   SDNode *SelectT2IndexedLoad(SDNode *N);
200
201   /// SelectVLD - Select NEON load intrinsics.  NumVecs should be
202   /// 1, 2, 3 or 4.  The opcode arrays specify the instructions used for
203   /// loads of D registers and even subregs and odd subregs of Q registers.
204   /// For NumVecs <= 2, QOpcodes1 is not used.
205   SDNode *SelectVLD(SDNode *N, bool isUpdating, unsigned NumVecs,
206                     unsigned *DOpcodes,
207                     unsigned *QOpcodes0, unsigned *QOpcodes1);
208
209   /// SelectVST - Select NEON store intrinsics.  NumVecs should
210   /// be 1, 2, 3 or 4.  The opcode arrays specify the instructions used for
211   /// stores of D registers and even subregs and odd subregs of Q registers.
212   /// For NumVecs <= 2, QOpcodes1 is not used.
213   SDNode *SelectVST(SDNode *N, bool isUpdating, unsigned NumVecs,
214                     unsigned *DOpcodes,
215                     unsigned *QOpcodes0, unsigned *QOpcodes1);
216
217   /// SelectVLDSTLane - Select NEON load/store lane intrinsics.  NumVecs should
218   /// be 2, 3 or 4.  The opcode arrays specify the instructions used for
219   /// load/store of D registers and Q registers.
220   SDNode *SelectVLDSTLane(SDNode *N, bool IsLoad,
221                           bool isUpdating, unsigned NumVecs,
222                           unsigned *DOpcodes, unsigned *QOpcodes);
223
224   /// SelectVLDDup - Select NEON load-duplicate intrinsics.  NumVecs
225   /// should be 2, 3 or 4.  The opcode array specifies the instructions used
226   /// for loading D registers.  (Q registers are not supported.)
227   SDNode *SelectVLDDup(SDNode *N, bool isUpdating, unsigned NumVecs,
228                        unsigned *Opcodes);
229
230   /// SelectVTBL - Select NEON VTBL and VTBX intrinsics.  NumVecs should be 2,
231   /// 3 or 4.  These are custom-selected so that a REG_SEQUENCE can be
232   /// generated to force the table registers to be consecutive.
233   SDNode *SelectVTBL(SDNode *N, bool IsExt, unsigned NumVecs, unsigned Opc);
234
235   /// SelectV6T2BitfieldExtractOp - Select SBFX/UBFX instructions for ARM.
236   SDNode *SelectV6T2BitfieldExtractOp(SDNode *N, bool isSigned);
237
238   /// SelectCMOVOp - Select CMOV instructions for ARM.
239   SDNode *SelectCMOVOp(SDNode *N);
240   SDNode *SelectT2CMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
241                               ARMCC::CondCodes CCVal, SDValue CCR,
242                               SDValue InFlag);
243   SDNode *SelectARMCMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
244                                ARMCC::CondCodes CCVal, SDValue CCR,
245                                SDValue InFlag);
246   SDNode *SelectT2CMOVImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
247                               ARMCC::CondCodes CCVal, SDValue CCR,
248                               SDValue InFlag);
249   SDNode *SelectARMCMOVImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
250                                ARMCC::CondCodes CCVal, SDValue CCR,
251                                SDValue InFlag);
252
253   SDNode *SelectConcatVector(SDNode *N);
254
255   /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
256   /// inline asm expressions.
257   virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
258                                             char ConstraintCode,
259                                             std::vector<SDValue> &OutOps);
260
261   // Form pairs of consecutive S, D, or Q registers.
262   SDNode *PairSRegs(EVT VT, SDValue V0, SDValue V1);
263   SDNode *PairDRegs(EVT VT, SDValue V0, SDValue V1);
264   SDNode *PairQRegs(EVT VT, SDValue V0, SDValue V1);
265
266   // Form sequences of 4 consecutive S, D, or Q registers.
267   SDNode *QuadSRegs(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
268   SDNode *QuadDRegs(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
269   SDNode *QuadQRegs(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
270
271   // Get the alignment operand for a NEON VLD or VST instruction.
272   SDValue GetVLDSTAlign(SDValue Align, unsigned NumVecs, bool is64BitVector);
273 };
274 }
275
276 /// isInt32Immediate - This method tests to see if the node is a 32-bit constant
277 /// operand. If so Imm will receive the 32-bit value.
278 static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
279   if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
280     Imm = cast<ConstantSDNode>(N)->getZExtValue();
281     return true;
282   }
283   return false;
284 }
285
286 // isInt32Immediate - This method tests to see if a constant operand.
287 // If so Imm will receive the 32 bit value.
288 static bool isInt32Immediate(SDValue N, unsigned &Imm) {
289   return isInt32Immediate(N.getNode(), Imm);
290 }
291
292 // isOpcWithIntImmediate - This method tests to see if the node is a specific
293 // opcode and that it has a immediate integer right operand.
294 // If so Imm will receive the 32 bit value.
295 static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
296   return N->getOpcode() == Opc &&
297          isInt32Immediate(N->getOperand(1).getNode(), Imm);
298 }
299
300 /// \brief Check whether a particular node is a constant value representable as
301 /// (N * Scale) where (N in [\arg RangeMin, \arg RangeMax).
302 ///
303 /// \param ScaledConstant [out] - On success, the pre-scaled constant value.
304 static bool isScaledConstantInRange(SDValue Node, unsigned Scale,
305                                     int RangeMin, int RangeMax,
306                                     int &ScaledConstant) {
307   assert(Scale && "Invalid scale!");
308
309   // Check that this is a constant.
310   const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Node);
311   if (!C)
312     return false;
313
314   ScaledConstant = (int) C->getZExtValue();
315   if ((ScaledConstant % Scale) != 0)
316     return false;
317
318   ScaledConstant /= Scale;
319   return ScaledConstant >= RangeMin && ScaledConstant < RangeMax;
320 }
321
322 /// hasNoVMLxHazardUse - Return true if it's desirable to select a FP MLA / MLS
323 /// node. VFP / NEON fp VMLA / VMLS instructions have special RAW hazards (at
324 /// least on current ARM implementations) which should be avoidded.
325 bool ARMDAGToDAGISel::hasNoVMLxHazardUse(SDNode *N) const {
326   if (OptLevel == CodeGenOpt::None)
327     return true;
328
329   if (!CheckVMLxHazard)
330     return true;
331
332   if (!Subtarget->isCortexA8() && !Subtarget->isCortexA9())
333     return true;
334
335   if (!N->hasOneUse())
336     return false;
337
338   SDNode *Use = *N->use_begin();
339   if (Use->getOpcode() == ISD::CopyToReg)
340     return true;
341   if (Use->isMachineOpcode()) {
342     const TargetInstrDesc &TID = TII->get(Use->getMachineOpcode());
343     if (TID.mayStore())
344       return true;
345     unsigned Opcode = TID.getOpcode();
346     if (Opcode == ARM::VMOVRS || Opcode == ARM::VMOVRRD)
347       return true;
348     // vmlx feeding into another vmlx. We actually want to unfold
349     // the use later in the MLxExpansion pass. e.g.
350     // vmla
351     // vmla (stall 8 cycles)
352     //
353     // vmul (5 cycles)
354     // vadd (5 cycles)
355     // vmla
356     // This adds up to about 18 - 19 cycles.
357     //
358     // vmla
359     // vmul (stall 4 cycles)
360     // vadd adds up to about 14 cycles.
361     return TII->isFpMLxInstruction(Opcode);
362   }
363
364   return false;
365 }
366
367 bool ARMDAGToDAGISel::isShifterOpProfitable(const SDValue &Shift,
368                                             ARM_AM::ShiftOpc ShOpcVal,
369                                             unsigned ShAmt) {
370   if (!Subtarget->isCortexA9())
371     return true;
372   if (Shift.hasOneUse())
373     return true;
374   // R << 2 is free.
375   return ShOpcVal == ARM_AM::lsl && ShAmt == 2;
376 }
377
378 bool ARMDAGToDAGISel::SelectShifterOperandReg(SDValue N,
379                                               SDValue &BaseReg,
380                                               SDValue &ShReg,
381                                               SDValue &Opc,
382                                               bool CheckProfitability) {
383   if (DisableShifterOp)
384     return false;
385
386   ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
387
388   // Don't match base register only case. That is matched to a separate
389   // lower complexity pattern with explicit register operand.
390   if (ShOpcVal == ARM_AM::no_shift) return false;
391
392   BaseReg = N.getOperand(0);
393   unsigned ShImmVal = 0;
394   if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
395     ShReg = CurDAG->getRegister(0, MVT::i32);
396     ShImmVal = RHS->getZExtValue() & 31;
397   } else {
398     ShReg = N.getOperand(1);
399     if (CheckProfitability && !isShifterOpProfitable(N, ShOpcVal, ShImmVal))
400       return false;
401   }
402   Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
403                                   MVT::i32);
404   return true;
405 }
406
407 bool ARMDAGToDAGISel::SelectAddrModeImm12(SDValue N,
408                                           SDValue &Base,
409                                           SDValue &OffImm) {
410   // Match simple R + imm12 operands.
411
412   // Base only.
413   if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
414       !CurDAG->isBaseWithConstantOffset(N)) {
415     if (N.getOpcode() == ISD::FrameIndex) {
416       // Match frame index.
417       int FI = cast<FrameIndexSDNode>(N)->getIndex();
418       Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
419       OffImm  = CurDAG->getTargetConstant(0, MVT::i32);
420       return true;
421     }
422
423     if (N.getOpcode() == ARMISD::Wrapper &&
424         !(Subtarget->useMovt() &&
425                      N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
426       Base = N.getOperand(0);
427     } else
428       Base = N;
429     OffImm  = CurDAG->getTargetConstant(0, MVT::i32);
430     return true;
431   }
432
433   if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
434     int RHSC = (int)RHS->getZExtValue();
435     if (N.getOpcode() == ISD::SUB)
436       RHSC = -RHSC;
437
438     if (RHSC >= 0 && RHSC < 0x1000) { // 12 bits (unsigned)
439       Base   = N.getOperand(0);
440       if (Base.getOpcode() == ISD::FrameIndex) {
441         int FI = cast<FrameIndexSDNode>(Base)->getIndex();
442         Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
443       }
444       OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
445       return true;
446     }
447   }
448
449   // Base only.
450   Base = N;
451   OffImm  = CurDAG->getTargetConstant(0, MVT::i32);
452   return true;
453 }
454
455
456
457 bool ARMDAGToDAGISel::SelectLdStSOReg(SDValue N, SDValue &Base, SDValue &Offset,
458                                       SDValue &Opc) {
459   if (N.getOpcode() == ISD::MUL &&
460       (!Subtarget->isCortexA9() || N.hasOneUse())) {
461     if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
462       // X * [3,5,9] -> X + X * [2,4,8] etc.
463       int RHSC = (int)RHS->getZExtValue();
464       if (RHSC & 1) {
465         RHSC = RHSC & ~1;
466         ARM_AM::AddrOpc AddSub = ARM_AM::add;
467         if (RHSC < 0) {
468           AddSub = ARM_AM::sub;
469           RHSC = - RHSC;
470         }
471         if (isPowerOf2_32(RHSC)) {
472           unsigned ShAmt = Log2_32(RHSC);
473           Base = Offset = N.getOperand(0);
474           Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt,
475                                                             ARM_AM::lsl),
476                                           MVT::i32);
477           return true;
478         }
479       }
480     }
481   }
482
483   if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
484       // ISD::OR that is equivalent to an ISD::ADD.
485       !CurDAG->isBaseWithConstantOffset(N))
486     return false;
487
488   // Leave simple R +/- imm12 operands for LDRi12
489   if (N.getOpcode() == ISD::ADD || N.getOpcode() == ISD::OR) {
490     int RHSC;
491     if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/1,
492                                 -0x1000+1, 0x1000, RHSC)) // 12 bits.
493       return false;
494   }
495
496   if (Subtarget->isCortexA9() && !N.hasOneUse())
497     // Compute R +/- (R << N) and reuse it.
498     return false;
499
500   // Otherwise this is R +/- [possibly shifted] R.
501   ARM_AM::AddrOpc AddSub = N.getOpcode() == ISD::SUB ? ARM_AM::sub:ARM_AM::add;
502   ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(1));
503   unsigned ShAmt = 0;
504
505   Base   = N.getOperand(0);
506   Offset = N.getOperand(1);
507
508   if (ShOpcVal != ARM_AM::no_shift) {
509     // Check to see if the RHS of the shift is a constant, if not, we can't fold
510     // it.
511     if (ConstantSDNode *Sh =
512            dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) {
513       ShAmt = Sh->getZExtValue();
514       if (isShifterOpProfitable(Offset, ShOpcVal, ShAmt))
515         Offset = N.getOperand(1).getOperand(0);
516       else {
517         ShAmt = 0;
518         ShOpcVal = ARM_AM::no_shift;
519       }
520     } else {
521       ShOpcVal = ARM_AM::no_shift;
522     }
523   }
524
525   // Try matching (R shl C) + (R).
526   if (N.getOpcode() != ISD::SUB && ShOpcVal == ARM_AM::no_shift &&
527       !(Subtarget->isCortexA9() || N.getOperand(0).hasOneUse())) {
528     ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0));
529     if (ShOpcVal != ARM_AM::no_shift) {
530       // Check to see if the RHS of the shift is a constant, if not, we can't
531       // fold it.
532       if (ConstantSDNode *Sh =
533           dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
534         ShAmt = Sh->getZExtValue();
535         if (!Subtarget->isCortexA9() ||
536             (N.hasOneUse() &&
537              isShifterOpProfitable(N.getOperand(0), ShOpcVal, ShAmt))) {
538           Offset = N.getOperand(0).getOperand(0);
539           Base = N.getOperand(1);
540         } else {
541           ShAmt = 0;
542           ShOpcVal = ARM_AM::no_shift;
543         }
544       } else {
545         ShOpcVal = ARM_AM::no_shift;
546       }
547     }
548   }
549
550   Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
551                                   MVT::i32);
552   return true;
553 }
554
555
556
557
558 //-----
559
560 AddrMode2Type ARMDAGToDAGISel::SelectAddrMode2Worker(SDValue N,
561                                                      SDValue &Base,
562                                                      SDValue &Offset,
563                                                      SDValue &Opc) {
564   if (N.getOpcode() == ISD::MUL &&
565       (!Subtarget->isCortexA9() || N.hasOneUse())) {
566     if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
567       // X * [3,5,9] -> X + X * [2,4,8] etc.
568       int RHSC = (int)RHS->getZExtValue();
569       if (RHSC & 1) {
570         RHSC = RHSC & ~1;
571         ARM_AM::AddrOpc AddSub = ARM_AM::add;
572         if (RHSC < 0) {
573           AddSub = ARM_AM::sub;
574           RHSC = - RHSC;
575         }
576         if (isPowerOf2_32(RHSC)) {
577           unsigned ShAmt = Log2_32(RHSC);
578           Base = Offset = N.getOperand(0);
579           Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt,
580                                                             ARM_AM::lsl),
581                                           MVT::i32);
582           return AM2_SHOP;
583         }
584       }
585     }
586   }
587
588   if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
589       // ISD::OR that is equivalent to an ADD.
590       !CurDAG->isBaseWithConstantOffset(N)) {
591     Base = N;
592     if (N.getOpcode() == ISD::FrameIndex) {
593       int FI = cast<FrameIndexSDNode>(N)->getIndex();
594       Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
595     } else if (N.getOpcode() == ARMISD::Wrapper &&
596                !(Subtarget->useMovt() &&
597                  N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
598       Base = N.getOperand(0);
599     }
600     Offset = CurDAG->getRegister(0, MVT::i32);
601     Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::add, 0,
602                                                       ARM_AM::no_shift),
603                                     MVT::i32);
604     return AM2_BASE;
605   }
606
607   // Match simple R +/- imm12 operands.
608   if (N.getOpcode() != ISD::SUB) {
609     int RHSC;
610     if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/1,
611                                 -0x1000+1, 0x1000, RHSC)) { // 12 bits.
612       Base = N.getOperand(0);
613       if (Base.getOpcode() == ISD::FrameIndex) {
614         int FI = cast<FrameIndexSDNode>(Base)->getIndex();
615         Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
616       }
617       Offset = CurDAG->getRegister(0, MVT::i32);
618
619       ARM_AM::AddrOpc AddSub = ARM_AM::add;
620       if (RHSC < 0) {
621         AddSub = ARM_AM::sub;
622         RHSC = - RHSC;
623       }
624       Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, RHSC,
625                                                         ARM_AM::no_shift),
626                                       MVT::i32);
627       return AM2_BASE;
628     }
629   }
630
631   if (Subtarget->isCortexA9() && !N.hasOneUse()) {
632     // Compute R +/- (R << N) and reuse it.
633     Base = N;
634     Offset = CurDAG->getRegister(0, MVT::i32);
635     Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::add, 0,
636                                                       ARM_AM::no_shift),
637                                     MVT::i32);
638     return AM2_BASE;
639   }
640
641   // Otherwise this is R +/- [possibly shifted] R.
642   ARM_AM::AddrOpc AddSub = N.getOpcode() != ISD::SUB ? ARM_AM::add:ARM_AM::sub;
643   ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(1));
644   unsigned ShAmt = 0;
645
646   Base   = N.getOperand(0);
647   Offset = N.getOperand(1);
648
649   if (ShOpcVal != ARM_AM::no_shift) {
650     // Check to see if the RHS of the shift is a constant, if not, we can't fold
651     // it.
652     if (ConstantSDNode *Sh =
653            dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) {
654       ShAmt = Sh->getZExtValue();
655       if (isShifterOpProfitable(Offset, ShOpcVal, ShAmt))
656         Offset = N.getOperand(1).getOperand(0);
657       else {
658         ShAmt = 0;
659         ShOpcVal = ARM_AM::no_shift;
660       }
661     } else {
662       ShOpcVal = ARM_AM::no_shift;
663     }
664   }
665
666   // Try matching (R shl C) + (R).
667   if (N.getOpcode() != ISD::SUB && ShOpcVal == ARM_AM::no_shift &&
668       !(Subtarget->isCortexA9() || N.getOperand(0).hasOneUse())) {
669     ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0));
670     if (ShOpcVal != ARM_AM::no_shift) {
671       // Check to see if the RHS of the shift is a constant, if not, we can't
672       // fold it.
673       if (ConstantSDNode *Sh =
674           dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
675         ShAmt = Sh->getZExtValue();
676         if (!Subtarget->isCortexA9() ||
677             (N.hasOneUse() &&
678              isShifterOpProfitable(N.getOperand(0), ShOpcVal, ShAmt))) {
679           Offset = N.getOperand(0).getOperand(0);
680           Base = N.getOperand(1);
681         } else {
682           ShAmt = 0;
683           ShOpcVal = ARM_AM::no_shift;
684         }
685       } else {
686         ShOpcVal = ARM_AM::no_shift;
687       }
688     }
689   }
690
691   Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
692                                   MVT::i32);
693   return AM2_SHOP;
694 }
695
696 bool ARMDAGToDAGISel::SelectAddrMode2Offset(SDNode *Op, SDValue N,
697                                             SDValue &Offset, SDValue &Opc) {
698   unsigned Opcode = Op->getOpcode();
699   ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
700     ? cast<LoadSDNode>(Op)->getAddressingMode()
701     : cast<StoreSDNode>(Op)->getAddressingMode();
702   ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
703     ? ARM_AM::add : ARM_AM::sub;
704   int Val;
705   if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x1000, Val)) { // 12 bits.
706     Offset = CurDAG->getRegister(0, MVT::i32);
707     Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, Val,
708                                                       ARM_AM::no_shift),
709                                     MVT::i32);
710     return true;
711   }
712
713   Offset = N;
714   ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
715   unsigned ShAmt = 0;
716   if (ShOpcVal != ARM_AM::no_shift) {
717     // Check to see if the RHS of the shift is a constant, if not, we can't fold
718     // it.
719     if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
720       ShAmt = Sh->getZExtValue();
721       if (isShifterOpProfitable(N, ShOpcVal, ShAmt))
722         Offset = N.getOperand(0);
723       else {
724         ShAmt = 0;
725         ShOpcVal = ARM_AM::no_shift;
726       }
727     } else {
728       ShOpcVal = ARM_AM::no_shift;
729     }
730   }
731
732   Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
733                                   MVT::i32);
734   return true;
735 }
736
737
738 bool ARMDAGToDAGISel::SelectAddrMode3(SDValue N,
739                                       SDValue &Base, SDValue &Offset,
740                                       SDValue &Opc) {
741   if (N.getOpcode() == ISD::SUB) {
742     // X - C  is canonicalize to X + -C, no need to handle it here.
743     Base = N.getOperand(0);
744     Offset = N.getOperand(1);
745     Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::sub, 0),MVT::i32);
746     return true;
747   }
748
749   if (!CurDAG->isBaseWithConstantOffset(N)) {
750     Base = N;
751     if (N.getOpcode() == ISD::FrameIndex) {
752       int FI = cast<FrameIndexSDNode>(N)->getIndex();
753       Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
754     }
755     Offset = CurDAG->getRegister(0, MVT::i32);
756     Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0),MVT::i32);
757     return true;
758   }
759
760   // If the RHS is +/- imm8, fold into addr mode.
761   int RHSC;
762   if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/1,
763                               -256 + 1, 256, RHSC)) { // 8 bits.
764     Base = N.getOperand(0);
765     if (Base.getOpcode() == ISD::FrameIndex) {
766       int FI = cast<FrameIndexSDNode>(Base)->getIndex();
767       Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
768     }
769     Offset = CurDAG->getRegister(0, MVT::i32);
770
771     ARM_AM::AddrOpc AddSub = ARM_AM::add;
772     if (RHSC < 0) {
773       AddSub = ARM_AM::sub;
774       RHSC = -RHSC;
775     }
776     Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, RHSC),MVT::i32);
777     return true;
778   }
779
780   Base = N.getOperand(0);
781   Offset = N.getOperand(1);
782   Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0), MVT::i32);
783   return true;
784 }
785
786 bool ARMDAGToDAGISel::SelectAddrMode3Offset(SDNode *Op, SDValue N,
787                                             SDValue &Offset, SDValue &Opc) {
788   unsigned Opcode = Op->getOpcode();
789   ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
790     ? cast<LoadSDNode>(Op)->getAddressingMode()
791     : cast<StoreSDNode>(Op)->getAddressingMode();
792   ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
793     ? ARM_AM::add : ARM_AM::sub;
794   int Val;
795   if (isScaledConstantInRange(N, /*Scale=*/1, 0, 256, Val)) { // 12 bits.
796     Offset = CurDAG->getRegister(0, MVT::i32);
797     Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, Val), MVT::i32);
798     return true;
799   }
800
801   Offset = N;
802   Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, 0), MVT::i32);
803   return true;
804 }
805
806 bool ARMDAGToDAGISel::SelectAddrMode5(SDValue N,
807                                       SDValue &Base, SDValue &Offset) {
808   if (!CurDAG->isBaseWithConstantOffset(N)) {
809     Base = N;
810     if (N.getOpcode() == ISD::FrameIndex) {
811       int FI = cast<FrameIndexSDNode>(N)->getIndex();
812       Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
813     } else if (N.getOpcode() == ARMISD::Wrapper &&
814                !(Subtarget->useMovt() &&
815                  N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
816       Base = N.getOperand(0);
817     }
818     Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
819                                        MVT::i32);
820     return true;
821   }
822
823   // If the RHS is +/- imm8, fold into addr mode.
824   int RHSC;
825   if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/4,
826                               -256 + 1, 256, RHSC)) {
827     Base = N.getOperand(0);
828     if (Base.getOpcode() == ISD::FrameIndex) {
829       int FI = cast<FrameIndexSDNode>(Base)->getIndex();
830       Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
831     }
832
833     ARM_AM::AddrOpc AddSub = ARM_AM::add;
834     if (RHSC < 0) {
835       AddSub = ARM_AM::sub;
836       RHSC = -RHSC;
837     }
838     Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(AddSub, RHSC),
839                                        MVT::i32);
840     return true;
841   }
842
843   Base = N;
844   Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
845                                      MVT::i32);
846   return true;
847 }
848
849 bool ARMDAGToDAGISel::SelectAddrMode6(SDNode *Parent, SDValue N, SDValue &Addr,
850                                       SDValue &Align) {
851   Addr = N;
852
853   unsigned Alignment = 0;
854   if (LSBaseSDNode *LSN = dyn_cast<LSBaseSDNode>(Parent)) {
855     // This case occurs only for VLD1-lane/dup and VST1-lane instructions.
856     // The maximum alignment is equal to the memory size being referenced.
857     unsigned LSNAlign = LSN->getAlignment();
858     unsigned MemSize = LSN->getMemoryVT().getSizeInBits() / 8;
859     if (LSNAlign > MemSize && MemSize > 1)
860       Alignment = MemSize;
861   } else {
862     // All other uses of addrmode6 are for intrinsics.  For now just record
863     // the raw alignment value; it will be refined later based on the legal
864     // alignment operands for the intrinsic.
865     Alignment = cast<MemIntrinsicSDNode>(Parent)->getAlignment();
866   }
867
868   Align = CurDAG->getTargetConstant(Alignment, MVT::i32);
869   return true;
870 }
871
872 bool ARMDAGToDAGISel::SelectAddrMode6Offset(SDNode *Op, SDValue N,
873                                             SDValue &Offset) {
874   LSBaseSDNode *LdSt = cast<LSBaseSDNode>(Op);
875   ISD::MemIndexedMode AM = LdSt->getAddressingMode();
876   if (AM != ISD::POST_INC)
877     return false;
878   Offset = N;
879   if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(N)) {
880     if (NC->getZExtValue() * 8 == LdSt->getMemoryVT().getSizeInBits())
881       Offset = CurDAG->getRegister(0, MVT::i32);
882   }
883   return true;
884 }
885
886 bool ARMDAGToDAGISel::SelectAddrModePC(SDValue N,
887                                        SDValue &Offset, SDValue &Label) {
888   if (N.getOpcode() == ARMISD::PIC_ADD && N.hasOneUse()) {
889     Offset = N.getOperand(0);
890     SDValue N1 = N.getOperand(1);
891     Label = CurDAG->getTargetConstant(cast<ConstantSDNode>(N1)->getZExtValue(),
892                                       MVT::i32);
893     return true;
894   }
895
896   return false;
897 }
898
899
900 //===----------------------------------------------------------------------===//
901 //                         Thumb Addressing Modes
902 //===----------------------------------------------------------------------===//
903
904 bool ARMDAGToDAGISel::SelectThumbAddrModeRR(SDValue N,
905                                             SDValue &Base, SDValue &Offset){
906   if (N.getOpcode() != ISD::ADD && !CurDAG->isBaseWithConstantOffset(N)) {
907     ConstantSDNode *NC = dyn_cast<ConstantSDNode>(N);
908     if (!NC || !NC->isNullValue())
909       return false;
910
911     Base = Offset = N;
912     return true;
913   }
914
915   Base = N.getOperand(0);
916   Offset = N.getOperand(1);
917   return true;
918 }
919
920 bool
921 ARMDAGToDAGISel::SelectThumbAddrModeRI(SDValue N, SDValue &Base,
922                                        SDValue &Offset, unsigned Scale) {
923   if (Scale == 4) {
924     SDValue TmpBase, TmpOffImm;
925     if (SelectThumbAddrModeSP(N, TmpBase, TmpOffImm))
926       return false;  // We want to select tLDRspi / tSTRspi instead.
927
928     if (N.getOpcode() == ARMISD::Wrapper &&
929         N.getOperand(0).getOpcode() == ISD::TargetConstantPool)
930       return false;  // We want to select tLDRpci instead.
931   }
932
933   if (!CurDAG->isBaseWithConstantOffset(N))
934     return false;
935
936   // Thumb does not have [sp, r] address mode.
937   RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
938   RegisterSDNode *RHSR = dyn_cast<RegisterSDNode>(N.getOperand(1));
939   if ((LHSR && LHSR->getReg() == ARM::SP) ||
940       (RHSR && RHSR->getReg() == ARM::SP))
941     return false;
942
943   // FIXME: Why do we explicitly check for a match here and then return false?
944   // Presumably to allow something else to match, but shouldn't this be
945   // documented?
946   int RHSC;
947   if (isScaledConstantInRange(N.getOperand(1), Scale, 0, 32, RHSC))
948     return false;
949
950   Base = N.getOperand(0);
951   Offset = N.getOperand(1);
952   return true;
953 }
954
955 bool
956 ARMDAGToDAGISel::SelectThumbAddrModeRI5S1(SDValue N,
957                                           SDValue &Base,
958                                           SDValue &Offset) {
959   return SelectThumbAddrModeRI(N, Base, Offset, 1);
960 }
961
962 bool
963 ARMDAGToDAGISel::SelectThumbAddrModeRI5S2(SDValue N,
964                                           SDValue &Base,
965                                           SDValue &Offset) {
966   return SelectThumbAddrModeRI(N, Base, Offset, 2);
967 }
968
969 bool
970 ARMDAGToDAGISel::SelectThumbAddrModeRI5S4(SDValue N,
971                                           SDValue &Base,
972                                           SDValue &Offset) {
973   return SelectThumbAddrModeRI(N, Base, Offset, 4);
974 }
975
976 bool
977 ARMDAGToDAGISel::SelectThumbAddrModeImm5S(SDValue N, unsigned Scale,
978                                           SDValue &Base, SDValue &OffImm) {
979   if (Scale == 4) {
980     SDValue TmpBase, TmpOffImm;
981     if (SelectThumbAddrModeSP(N, TmpBase, TmpOffImm))
982       return false;  // We want to select tLDRspi / tSTRspi instead.
983
984     if (N.getOpcode() == ARMISD::Wrapper &&
985         N.getOperand(0).getOpcode() == ISD::TargetConstantPool)
986       return false;  // We want to select tLDRpci instead.
987   }
988
989   if (!CurDAG->isBaseWithConstantOffset(N)) {
990     if (N.getOpcode() == ARMISD::Wrapper &&
991         !(Subtarget->useMovt() &&
992           N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
993       Base = N.getOperand(0);
994     } else {
995       Base = N;
996     }
997
998     OffImm = CurDAG->getTargetConstant(0, MVT::i32);
999     return true;
1000   }
1001
1002   RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
1003   RegisterSDNode *RHSR = dyn_cast<RegisterSDNode>(N.getOperand(1));
1004   if ((LHSR && LHSR->getReg() == ARM::SP) ||
1005       (RHSR && RHSR->getReg() == ARM::SP)) {
1006     ConstantSDNode *LHS = dyn_cast<ConstantSDNode>(N.getOperand(0));
1007     ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1));
1008     unsigned LHSC = LHS ? LHS->getZExtValue() : 0;
1009     unsigned RHSC = RHS ? RHS->getZExtValue() : 0;
1010
1011     // Thumb does not have [sp, #imm5] address mode for non-zero imm5.
1012     if (LHSC != 0 || RHSC != 0) return false;
1013
1014     Base = N;
1015     OffImm = CurDAG->getTargetConstant(0, MVT::i32);
1016     return true;
1017   }
1018
1019   // If the RHS is + imm5 * scale, fold into addr mode.
1020   int RHSC;
1021   if (isScaledConstantInRange(N.getOperand(1), Scale, 0, 32, RHSC)) {
1022     Base = N.getOperand(0);
1023     OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
1024     return true;
1025   }
1026
1027   Base = N.getOperand(0);
1028   OffImm = CurDAG->getTargetConstant(0, MVT::i32);
1029   return true;
1030 }
1031
1032 bool
1033 ARMDAGToDAGISel::SelectThumbAddrModeImm5S4(SDValue N, SDValue &Base,
1034                                            SDValue &OffImm) {
1035   return SelectThumbAddrModeImm5S(N, 4, Base, OffImm);
1036 }
1037
1038 bool
1039 ARMDAGToDAGISel::SelectThumbAddrModeImm5S2(SDValue N, SDValue &Base,
1040                                            SDValue &OffImm) {
1041   return SelectThumbAddrModeImm5S(N, 2, Base, OffImm);
1042 }
1043
1044 bool
1045 ARMDAGToDAGISel::SelectThumbAddrModeImm5S1(SDValue N, SDValue &Base,
1046                                            SDValue &OffImm) {
1047   return SelectThumbAddrModeImm5S(N, 1, Base, OffImm);
1048 }
1049
1050 bool ARMDAGToDAGISel::SelectThumbAddrModeSP(SDValue N,
1051                                             SDValue &Base, SDValue &OffImm) {
1052   if (N.getOpcode() == ISD::FrameIndex) {
1053     int FI = cast<FrameIndexSDNode>(N)->getIndex();
1054     Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
1055     OffImm = CurDAG->getTargetConstant(0, MVT::i32);
1056     return true;
1057   }
1058
1059   if (!CurDAG->isBaseWithConstantOffset(N))
1060     return false;
1061
1062   RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
1063   if (N.getOperand(0).getOpcode() == ISD::FrameIndex ||
1064       (LHSR && LHSR->getReg() == ARM::SP)) {
1065     // If the RHS is + imm8 * scale, fold into addr mode.
1066     int RHSC;
1067     if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/4, 0, 256, RHSC)) {
1068       Base = N.getOperand(0);
1069       if (Base.getOpcode() == ISD::FrameIndex) {
1070         int FI = cast<FrameIndexSDNode>(Base)->getIndex();
1071         Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
1072       }
1073       OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
1074       return true;
1075     }
1076   }
1077
1078   return false;
1079 }
1080
1081
1082 //===----------------------------------------------------------------------===//
1083 //                        Thumb 2 Addressing Modes
1084 //===----------------------------------------------------------------------===//
1085
1086
1087 bool ARMDAGToDAGISel::SelectT2ShifterOperandReg(SDValue N, SDValue &BaseReg,
1088                                                 SDValue &Opc) {
1089   if (DisableShifterOp)
1090     return false;
1091
1092   ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
1093
1094   // Don't match base register only case. That is matched to a separate
1095   // lower complexity pattern with explicit register operand.
1096   if (ShOpcVal == ARM_AM::no_shift) return false;
1097
1098   BaseReg = N.getOperand(0);
1099   unsigned ShImmVal = 0;
1100   if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1101     ShImmVal = RHS->getZExtValue() & 31;
1102     Opc = getI32Imm(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal));
1103     return true;
1104   }
1105
1106   return false;
1107 }
1108
1109 bool ARMDAGToDAGISel::SelectT2AddrModeImm12(SDValue N,
1110                                             SDValue &Base, SDValue &OffImm) {
1111   // Match simple R + imm12 operands.
1112
1113   // Base only.
1114   if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
1115       !CurDAG->isBaseWithConstantOffset(N)) {
1116     if (N.getOpcode() == ISD::FrameIndex) {
1117       // Match frame index.
1118       int FI = cast<FrameIndexSDNode>(N)->getIndex();
1119       Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
1120       OffImm  = CurDAG->getTargetConstant(0, MVT::i32);
1121       return true;
1122     }
1123
1124     if (N.getOpcode() == ARMISD::Wrapper &&
1125                !(Subtarget->useMovt() &&
1126                  N.getOperand(0).getOpcode() == ISD::TargetGlobalAddress)) {
1127       Base = N.getOperand(0);
1128       if (Base.getOpcode() == ISD::TargetConstantPool)
1129         return false;  // We want to select t2LDRpci instead.
1130     } else
1131       Base = N;
1132     OffImm  = CurDAG->getTargetConstant(0, MVT::i32);
1133     return true;
1134   }
1135
1136   if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1137     if (SelectT2AddrModeImm8(N, Base, OffImm))
1138       // Let t2LDRi8 handle (R - imm8).
1139       return false;
1140
1141     int RHSC = (int)RHS->getZExtValue();
1142     if (N.getOpcode() == ISD::SUB)
1143       RHSC = -RHSC;
1144
1145     if (RHSC >= 0 && RHSC < 0x1000) { // 12 bits (unsigned)
1146       Base   = N.getOperand(0);
1147       if (Base.getOpcode() == ISD::FrameIndex) {
1148         int FI = cast<FrameIndexSDNode>(Base)->getIndex();
1149         Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
1150       }
1151       OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
1152       return true;
1153     }
1154   }
1155
1156   // Base only.
1157   Base = N;
1158   OffImm  = CurDAG->getTargetConstant(0, MVT::i32);
1159   return true;
1160 }
1161
1162 bool ARMDAGToDAGISel::SelectT2AddrModeImm8(SDValue N,
1163                                            SDValue &Base, SDValue &OffImm) {
1164   // Match simple R - imm8 operands.
1165   if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
1166       !CurDAG->isBaseWithConstantOffset(N))
1167     return false;
1168
1169   if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1170     int RHSC = (int)RHS->getSExtValue();
1171     if (N.getOpcode() == ISD::SUB)
1172       RHSC = -RHSC;
1173
1174     if ((RHSC >= -255) && (RHSC < 0)) { // 8 bits (always negative)
1175       Base = N.getOperand(0);
1176       if (Base.getOpcode() == ISD::FrameIndex) {
1177         int FI = cast<FrameIndexSDNode>(Base)->getIndex();
1178         Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
1179       }
1180       OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
1181       return true;
1182     }
1183   }
1184
1185   return false;
1186 }
1187
1188 bool ARMDAGToDAGISel::SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
1189                                                  SDValue &OffImm){
1190   unsigned Opcode = Op->getOpcode();
1191   ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
1192     ? cast<LoadSDNode>(Op)->getAddressingMode()
1193     : cast<StoreSDNode>(Op)->getAddressingMode();
1194   int RHSC;
1195   if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x100, RHSC)) { // 8 bits.
1196     OffImm = ((AM == ISD::PRE_INC) || (AM == ISD::POST_INC))
1197       ? CurDAG->getTargetConstant(RHSC, MVT::i32)
1198       : CurDAG->getTargetConstant(-RHSC, MVT::i32);
1199     return true;
1200   }
1201
1202   return false;
1203 }
1204
1205 bool ARMDAGToDAGISel::SelectT2AddrModeSoReg(SDValue N,
1206                                             SDValue &Base,
1207                                             SDValue &OffReg, SDValue &ShImm) {
1208   // (R - imm8) should be handled by t2LDRi8. The rest are handled by t2LDRi12.
1209   if (N.getOpcode() != ISD::ADD && !CurDAG->isBaseWithConstantOffset(N))
1210     return false;
1211
1212   // Leave (R + imm12) for t2LDRi12, (R - imm8) for t2LDRi8.
1213   if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1214     int RHSC = (int)RHS->getZExtValue();
1215     if (RHSC >= 0 && RHSC < 0x1000) // 12 bits (unsigned)
1216       return false;
1217     else if (RHSC < 0 && RHSC >= -255) // 8 bits
1218       return false;
1219   }
1220
1221   if (Subtarget->isCortexA9() && !N.hasOneUse()) {
1222     // Compute R + (R << [1,2,3]) and reuse it.
1223     Base = N;
1224     return false;
1225   }
1226
1227   // Look for (R + R) or (R + (R << [1,2,3])).
1228   unsigned ShAmt = 0;
1229   Base   = N.getOperand(0);
1230   OffReg = N.getOperand(1);
1231
1232   // Swap if it is ((R << c) + R).
1233   ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(OffReg);
1234   if (ShOpcVal != ARM_AM::lsl) {
1235     ShOpcVal = ARM_AM::getShiftOpcForNode(Base);
1236     if (ShOpcVal == ARM_AM::lsl)
1237       std::swap(Base, OffReg);
1238   }
1239
1240   if (ShOpcVal == ARM_AM::lsl) {
1241     // Check to see if the RHS of the shift is a constant, if not, we can't fold
1242     // it.
1243     if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(OffReg.getOperand(1))) {
1244       ShAmt = Sh->getZExtValue();
1245       if (ShAmt < 4 && isShifterOpProfitable(OffReg, ShOpcVal, ShAmt))
1246         OffReg = OffReg.getOperand(0);
1247       else {
1248         ShAmt = 0;
1249         ShOpcVal = ARM_AM::no_shift;
1250       }
1251     } else {
1252       ShOpcVal = ARM_AM::no_shift;
1253     }
1254   }
1255
1256   ShImm = CurDAG->getTargetConstant(ShAmt, MVT::i32);
1257
1258   return true;
1259 }
1260
1261 //===--------------------------------------------------------------------===//
1262
1263 /// getAL - Returns a ARMCC::AL immediate node.
1264 static inline SDValue getAL(SelectionDAG *CurDAG) {
1265   return CurDAG->getTargetConstant((uint64_t)ARMCC::AL, MVT::i32);
1266 }
1267
1268 SDNode *ARMDAGToDAGISel::SelectARMIndexedLoad(SDNode *N) {
1269   LoadSDNode *LD = cast<LoadSDNode>(N);
1270   ISD::MemIndexedMode AM = LD->getAddressingMode();
1271   if (AM == ISD::UNINDEXED)
1272     return NULL;
1273
1274   EVT LoadedVT = LD->getMemoryVT();
1275   SDValue Offset, AMOpc;
1276   bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
1277   unsigned Opcode = 0;
1278   bool Match = false;
1279   if (LoadedVT == MVT::i32 &&
1280       SelectAddrMode2Offset(N, LD->getOffset(), Offset, AMOpc)) {
1281     Opcode = isPre ? ARM::LDR_PRE : ARM::LDR_POST;
1282     Match = true;
1283   } else if (LoadedVT == MVT::i16 &&
1284              SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
1285     Match = true;
1286     Opcode = (LD->getExtensionType() == ISD::SEXTLOAD)
1287       ? (isPre ? ARM::LDRSH_PRE : ARM::LDRSH_POST)
1288       : (isPre ? ARM::LDRH_PRE : ARM::LDRH_POST);
1289   } else if (LoadedVT == MVT::i8 || LoadedVT == MVT::i1) {
1290     if (LD->getExtensionType() == ISD::SEXTLOAD) {
1291       if (SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
1292         Match = true;
1293         Opcode = isPre ? ARM::LDRSB_PRE : ARM::LDRSB_POST;
1294       }
1295     } else {
1296       if (SelectAddrMode2Offset(N, LD->getOffset(), Offset, AMOpc)) {
1297         Match = true;
1298         Opcode = isPre ? ARM::LDRB_PRE : ARM::LDRB_POST;
1299       }
1300     }
1301   }
1302
1303   if (Match) {
1304     SDValue Chain = LD->getChain();
1305     SDValue Base = LD->getBasePtr();
1306     SDValue Ops[]= { Base, Offset, AMOpc, getAL(CurDAG),
1307                      CurDAG->getRegister(0, MVT::i32), Chain };
1308     return CurDAG->getMachineNode(Opcode, N->getDebugLoc(), MVT::i32, MVT::i32,
1309                                   MVT::Other, Ops, 6);
1310   }
1311
1312   return NULL;
1313 }
1314
1315 SDNode *ARMDAGToDAGISel::SelectT2IndexedLoad(SDNode *N) {
1316   LoadSDNode *LD = cast<LoadSDNode>(N);
1317   ISD::MemIndexedMode AM = LD->getAddressingMode();
1318   if (AM == ISD::UNINDEXED)
1319     return NULL;
1320
1321   EVT LoadedVT = LD->getMemoryVT();
1322   bool isSExtLd = LD->getExtensionType() == ISD::SEXTLOAD;
1323   SDValue Offset;
1324   bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
1325   unsigned Opcode = 0;
1326   bool Match = false;
1327   if (SelectT2AddrModeImm8Offset(N, LD->getOffset(), Offset)) {
1328     switch (LoadedVT.getSimpleVT().SimpleTy) {
1329     case MVT::i32:
1330       Opcode = isPre ? ARM::t2LDR_PRE : ARM::t2LDR_POST;
1331       break;
1332     case MVT::i16:
1333       if (isSExtLd)
1334         Opcode = isPre ? ARM::t2LDRSH_PRE : ARM::t2LDRSH_POST;
1335       else
1336         Opcode = isPre ? ARM::t2LDRH_PRE : ARM::t2LDRH_POST;
1337       break;
1338     case MVT::i8:
1339     case MVT::i1:
1340       if (isSExtLd)
1341         Opcode = isPre ? ARM::t2LDRSB_PRE : ARM::t2LDRSB_POST;
1342       else
1343         Opcode = isPre ? ARM::t2LDRB_PRE : ARM::t2LDRB_POST;
1344       break;
1345     default:
1346       return NULL;
1347     }
1348     Match = true;
1349   }
1350
1351   if (Match) {
1352     SDValue Chain = LD->getChain();
1353     SDValue Base = LD->getBasePtr();
1354     SDValue Ops[]= { Base, Offset, getAL(CurDAG),
1355                      CurDAG->getRegister(0, MVT::i32), Chain };
1356     return CurDAG->getMachineNode(Opcode, N->getDebugLoc(), MVT::i32, MVT::i32,
1357                                   MVT::Other, Ops, 5);
1358   }
1359
1360   return NULL;
1361 }
1362
1363 /// PairSRegs - Form a D register from a pair of S registers.
1364 ///
1365 SDNode *ARMDAGToDAGISel::PairSRegs(EVT VT, SDValue V0, SDValue V1) {
1366   DebugLoc dl = V0.getNode()->getDebugLoc();
1367   SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, MVT::i32);
1368   SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, MVT::i32);
1369   const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
1370   return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
1371 }
1372
1373 /// PairDRegs - Form a quad register from a pair of D registers.
1374 ///
1375 SDNode *ARMDAGToDAGISel::PairDRegs(EVT VT, SDValue V0, SDValue V1) {
1376   DebugLoc dl = V0.getNode()->getDebugLoc();
1377   SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
1378   SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
1379   const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
1380   return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
1381 }
1382
1383 /// PairQRegs - Form 4 consecutive D registers from a pair of Q registers.
1384 ///
1385 SDNode *ARMDAGToDAGISel::PairQRegs(EVT VT, SDValue V0, SDValue V1) {
1386   DebugLoc dl = V0.getNode()->getDebugLoc();
1387   SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, MVT::i32);
1388   SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, MVT::i32);
1389   const SDValue Ops[] = { V0, SubReg0, V1, SubReg1 };
1390   return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 4);
1391 }
1392
1393 /// QuadSRegs - Form 4 consecutive S registers.
1394 ///
1395 SDNode *ARMDAGToDAGISel::QuadSRegs(EVT VT, SDValue V0, SDValue V1,
1396                                    SDValue V2, SDValue V3) {
1397   DebugLoc dl = V0.getNode()->getDebugLoc();
1398   SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, MVT::i32);
1399   SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, MVT::i32);
1400   SDValue SubReg2 = CurDAG->getTargetConstant(ARM::ssub_2, MVT::i32);
1401   SDValue SubReg3 = CurDAG->getTargetConstant(ARM::ssub_3, MVT::i32);
1402   const SDValue Ops[] = { V0, SubReg0, V1, SubReg1, V2, SubReg2, V3, SubReg3 };
1403   return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 8);
1404 }
1405
1406 /// QuadDRegs - Form 4 consecutive D registers.
1407 ///
1408 SDNode *ARMDAGToDAGISel::QuadDRegs(EVT VT, SDValue V0, SDValue V1,
1409                                    SDValue V2, SDValue V3) {
1410   DebugLoc dl = V0.getNode()->getDebugLoc();
1411   SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, MVT::i32);
1412   SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, MVT::i32);
1413   SDValue SubReg2 = CurDAG->getTargetConstant(ARM::dsub_2, MVT::i32);
1414   SDValue SubReg3 = CurDAG->getTargetConstant(ARM::dsub_3, MVT::i32);
1415   const SDValue Ops[] = { V0, SubReg0, V1, SubReg1, V2, SubReg2, V3, SubReg3 };
1416   return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 8);
1417 }
1418
1419 /// QuadQRegs - Form 4 consecutive Q registers.
1420 ///
1421 SDNode *ARMDAGToDAGISel::QuadQRegs(EVT VT, SDValue V0, SDValue V1,
1422                                    SDValue V2, SDValue V3) {
1423   DebugLoc dl = V0.getNode()->getDebugLoc();
1424   SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, MVT::i32);
1425   SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, MVT::i32);
1426   SDValue SubReg2 = CurDAG->getTargetConstant(ARM::qsub_2, MVT::i32);
1427   SDValue SubReg3 = CurDAG->getTargetConstant(ARM::qsub_3, MVT::i32);
1428   const SDValue Ops[] = { V0, SubReg0, V1, SubReg1, V2, SubReg2, V3, SubReg3 };
1429   return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops, 8);
1430 }
1431
1432 /// GetVLDSTAlign - Get the alignment (in bytes) for the alignment operand
1433 /// of a NEON VLD or VST instruction.  The supported values depend on the
1434 /// number of registers being loaded.
1435 SDValue ARMDAGToDAGISel::GetVLDSTAlign(SDValue Align, unsigned NumVecs,
1436                                        bool is64BitVector) {
1437   unsigned NumRegs = NumVecs;
1438   if (!is64BitVector && NumVecs < 3)
1439     NumRegs *= 2;
1440
1441   unsigned Alignment = cast<ConstantSDNode>(Align)->getZExtValue();
1442   if (Alignment >= 32 && NumRegs == 4)
1443     Alignment = 32;
1444   else if (Alignment >= 16 && (NumRegs == 2 || NumRegs == 4))
1445     Alignment = 16;
1446   else if (Alignment >= 8)
1447     Alignment = 8;
1448   else
1449     Alignment = 0;
1450
1451   return CurDAG->getTargetConstant(Alignment, MVT::i32);
1452 }
1453
1454 SDNode *ARMDAGToDAGISel::SelectVLD(SDNode *N, bool isUpdating, unsigned NumVecs,
1455                                    unsigned *DOpcodes, unsigned *QOpcodes0,
1456                                    unsigned *QOpcodes1) {
1457   assert(NumVecs >= 1 && NumVecs <= 4 && "VLD NumVecs out-of-range");
1458   DebugLoc dl = N->getDebugLoc();
1459
1460   SDValue MemAddr, Align;
1461   unsigned AddrOpIdx = isUpdating ? 1 : 2;
1462   if (!SelectAddrMode6(N, N->getOperand(AddrOpIdx), MemAddr, Align))
1463     return NULL;
1464
1465   SDValue Chain = N->getOperand(0);
1466   EVT VT = N->getValueType(0);
1467   bool is64BitVector = VT.is64BitVector();
1468   Align = GetVLDSTAlign(Align, NumVecs, is64BitVector);
1469
1470   unsigned OpcodeIndex;
1471   switch (VT.getSimpleVT().SimpleTy) {
1472   default: llvm_unreachable("unhandled vld type");
1473     // Double-register operations:
1474   case MVT::v8i8:  OpcodeIndex = 0; break;
1475   case MVT::v4i16: OpcodeIndex = 1; break;
1476   case MVT::v2f32:
1477   case MVT::v2i32: OpcodeIndex = 2; break;
1478   case MVT::v1i64: OpcodeIndex = 3; break;
1479     // Quad-register operations:
1480   case MVT::v16i8: OpcodeIndex = 0; break;
1481   case MVT::v8i16: OpcodeIndex = 1; break;
1482   case MVT::v4f32:
1483   case MVT::v4i32: OpcodeIndex = 2; break;
1484   case MVT::v2i64: OpcodeIndex = 3;
1485     assert(NumVecs == 1 && "v2i64 type only supported for VLD1");
1486     break;
1487   }
1488
1489   EVT ResTy;
1490   if (NumVecs == 1)
1491     ResTy = VT;
1492   else {
1493     unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
1494     if (!is64BitVector)
1495       ResTyElts *= 2;
1496     ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts);
1497   }
1498   std::vector<EVT> ResTys;
1499   ResTys.push_back(ResTy);
1500   if (isUpdating)
1501     ResTys.push_back(MVT::i32);
1502   ResTys.push_back(MVT::Other);
1503
1504   SDValue Pred = getAL(CurDAG);
1505   SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1506   SDNode *VLd;
1507   SmallVector<SDValue, 7> Ops;
1508
1509   // Double registers and VLD1/VLD2 quad registers are directly supported.
1510   if (is64BitVector || NumVecs <= 2) {
1511     unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
1512                     QOpcodes0[OpcodeIndex]);
1513     Ops.push_back(MemAddr);
1514     Ops.push_back(Align);
1515     if (isUpdating) {
1516       SDValue Inc = N->getOperand(AddrOpIdx + 1);
1517       Ops.push_back(isa<ConstantSDNode>(Inc.getNode()) ? Reg0 : Inc);
1518     }
1519     Ops.push_back(Pred);
1520     Ops.push_back(Reg0);
1521     Ops.push_back(Chain);
1522     VLd = CurDAG->getMachineNode(Opc, dl, ResTys, Ops.data(), Ops.size());
1523
1524   } else {
1525     // Otherwise, quad registers are loaded with two separate instructions,
1526     // where one loads the even registers and the other loads the odd registers.
1527     EVT AddrTy = MemAddr.getValueType();
1528
1529     // Load the even subregs.  This is always an updating load, so that it
1530     // provides the address to the second load for the odd subregs.
1531     SDValue ImplDef =
1532       SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, ResTy), 0);
1533     const SDValue OpsA[] = { MemAddr, Align, Reg0, ImplDef, Pred, Reg0, Chain };
1534     SDNode *VLdA = CurDAG->getMachineNode(QOpcodes0[OpcodeIndex], dl,
1535                                           ResTy, AddrTy, MVT::Other, OpsA, 7);
1536     Chain = SDValue(VLdA, 2);
1537
1538     // Load the odd subregs.
1539     Ops.push_back(SDValue(VLdA, 1));
1540     Ops.push_back(Align);
1541     if (isUpdating) {
1542       SDValue Inc = N->getOperand(AddrOpIdx + 1);
1543       assert(isa<ConstantSDNode>(Inc.getNode()) &&
1544              "only constant post-increment update allowed for VLD3/4");
1545       (void)Inc;
1546       Ops.push_back(Reg0);
1547     }
1548     Ops.push_back(SDValue(VLdA, 0));
1549     Ops.push_back(Pred);
1550     Ops.push_back(Reg0);
1551     Ops.push_back(Chain);
1552     VLd = CurDAG->getMachineNode(QOpcodes1[OpcodeIndex], dl, ResTys,
1553                                  Ops.data(), Ops.size());
1554   }
1555
1556   // Transfer memoperands.
1557   MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1558   MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
1559   cast<MachineSDNode>(VLd)->setMemRefs(MemOp, MemOp + 1);
1560
1561   if (NumVecs == 1)
1562     return VLd;
1563
1564   // Extract out the subregisters.
1565   SDValue SuperReg = SDValue(VLd, 0);
1566   assert(ARM::dsub_7 == ARM::dsub_0+7 &&
1567          ARM::qsub_3 == ARM::qsub_0+3 && "Unexpected subreg numbering");
1568   unsigned Sub0 = (is64BitVector ? ARM::dsub_0 : ARM::qsub_0);
1569   for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1570     ReplaceUses(SDValue(N, Vec),
1571                 CurDAG->getTargetExtractSubreg(Sub0 + Vec, dl, VT, SuperReg));
1572   ReplaceUses(SDValue(N, NumVecs), SDValue(VLd, 1));
1573   if (isUpdating)
1574     ReplaceUses(SDValue(N, NumVecs + 1), SDValue(VLd, 2));
1575   return NULL;
1576 }
1577
1578 SDNode *ARMDAGToDAGISel::SelectVST(SDNode *N, bool isUpdating, unsigned NumVecs,
1579                                    unsigned *DOpcodes, unsigned *QOpcodes0,
1580                                    unsigned *QOpcodes1) {
1581   assert(NumVecs >= 1 && NumVecs <= 4 && "VST NumVecs out-of-range");
1582   DebugLoc dl = N->getDebugLoc();
1583
1584   SDValue MemAddr, Align;
1585   unsigned AddrOpIdx = isUpdating ? 1 : 2;
1586   unsigned Vec0Idx = 3; // AddrOpIdx + (isUpdating ? 2 : 1)
1587   if (!SelectAddrMode6(N, N->getOperand(AddrOpIdx), MemAddr, Align))
1588     return NULL;
1589
1590   MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1591   MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
1592
1593   SDValue Chain = N->getOperand(0);
1594   EVT VT = N->getOperand(Vec0Idx).getValueType();
1595   bool is64BitVector = VT.is64BitVector();
1596   Align = GetVLDSTAlign(Align, NumVecs, is64BitVector);
1597
1598   unsigned OpcodeIndex;
1599   switch (VT.getSimpleVT().SimpleTy) {
1600   default: llvm_unreachable("unhandled vst type");
1601     // Double-register operations:
1602   case MVT::v8i8:  OpcodeIndex = 0; break;
1603   case MVT::v4i16: OpcodeIndex = 1; break;
1604   case MVT::v2f32:
1605   case MVT::v2i32: OpcodeIndex = 2; break;
1606   case MVT::v1i64: OpcodeIndex = 3; break;
1607     // Quad-register operations:
1608   case MVT::v16i8: OpcodeIndex = 0; break;
1609   case MVT::v8i16: OpcodeIndex = 1; break;
1610   case MVT::v4f32:
1611   case MVT::v4i32: OpcodeIndex = 2; break;
1612   case MVT::v2i64: OpcodeIndex = 3;
1613     assert(NumVecs == 1 && "v2i64 type only supported for VST1");
1614     break;
1615   }
1616
1617   std::vector<EVT> ResTys;
1618   if (isUpdating)
1619     ResTys.push_back(MVT::i32);
1620   ResTys.push_back(MVT::Other);
1621
1622   SDValue Pred = getAL(CurDAG);
1623   SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1624   SmallVector<SDValue, 7> Ops;
1625
1626   // Double registers and VST1/VST2 quad registers are directly supported.
1627   if (is64BitVector || NumVecs <= 2) {
1628     SDValue SrcReg;
1629     if (NumVecs == 1) {
1630       SrcReg = N->getOperand(Vec0Idx);
1631     } else if (is64BitVector) {
1632       // Form a REG_SEQUENCE to force register allocation.
1633       SDValue V0 = N->getOperand(Vec0Idx + 0);
1634       SDValue V1 = N->getOperand(Vec0Idx + 1);
1635       if (NumVecs == 2)
1636         SrcReg = SDValue(PairDRegs(MVT::v2i64, V0, V1), 0);
1637       else {
1638         SDValue V2 = N->getOperand(Vec0Idx + 2);
1639         // If it's a vst3, form a quad D-register and leave the last part as
1640         // an undef.
1641         SDValue V3 = (NumVecs == 3)
1642           ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
1643           : N->getOperand(Vec0Idx + 3);
1644         SrcReg = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1645       }
1646     } else {
1647       // Form a QQ register.
1648       SDValue Q0 = N->getOperand(Vec0Idx);
1649       SDValue Q1 = N->getOperand(Vec0Idx + 1);
1650       SrcReg = SDValue(PairQRegs(MVT::v4i64, Q0, Q1), 0);
1651     }
1652
1653     unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
1654                     QOpcodes0[OpcodeIndex]);
1655     Ops.push_back(MemAddr);
1656     Ops.push_back(Align);
1657     if (isUpdating) {
1658       SDValue Inc = N->getOperand(AddrOpIdx + 1);
1659       Ops.push_back(isa<ConstantSDNode>(Inc.getNode()) ? Reg0 : Inc);
1660     }
1661     Ops.push_back(SrcReg);
1662     Ops.push_back(Pred);
1663     Ops.push_back(Reg0);
1664     Ops.push_back(Chain);
1665     SDNode *VSt =
1666       CurDAG->getMachineNode(Opc, dl, ResTys, Ops.data(), Ops.size());
1667
1668     // Transfer memoperands.
1669     cast<MachineSDNode>(VSt)->setMemRefs(MemOp, MemOp + 1);
1670
1671     return VSt;
1672   }
1673
1674   // Otherwise, quad registers are stored with two separate instructions,
1675   // where one stores the even registers and the other stores the odd registers.
1676
1677   // Form the QQQQ REG_SEQUENCE.
1678   SDValue V0 = N->getOperand(Vec0Idx + 0);
1679   SDValue V1 = N->getOperand(Vec0Idx + 1);
1680   SDValue V2 = N->getOperand(Vec0Idx + 2);
1681   SDValue V3 = (NumVecs == 3)
1682     ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
1683     : N->getOperand(Vec0Idx + 3);
1684   SDValue RegSeq = SDValue(QuadQRegs(MVT::v8i64, V0, V1, V2, V3), 0);
1685
1686   // Store the even D registers.  This is always an updating store, so that it
1687   // provides the address to the second store for the odd subregs.
1688   const SDValue OpsA[] = { MemAddr, Align, Reg0, RegSeq, Pred, Reg0, Chain };
1689   SDNode *VStA = CurDAG->getMachineNode(QOpcodes0[OpcodeIndex], dl,
1690                                         MemAddr.getValueType(),
1691                                         MVT::Other, OpsA, 7);
1692   cast<MachineSDNode>(VStA)->setMemRefs(MemOp, MemOp + 1);
1693   Chain = SDValue(VStA, 1);
1694
1695   // Store the odd D registers.
1696   Ops.push_back(SDValue(VStA, 0));
1697   Ops.push_back(Align);
1698   if (isUpdating) {
1699     SDValue Inc = N->getOperand(AddrOpIdx + 1);
1700     assert(isa<ConstantSDNode>(Inc.getNode()) &&
1701            "only constant post-increment update allowed for VST3/4");
1702     (void)Inc;
1703     Ops.push_back(Reg0);
1704   }
1705   Ops.push_back(RegSeq);
1706   Ops.push_back(Pred);
1707   Ops.push_back(Reg0);
1708   Ops.push_back(Chain);
1709   SDNode *VStB = CurDAG->getMachineNode(QOpcodes1[OpcodeIndex], dl, ResTys,
1710                                         Ops.data(), Ops.size());
1711   cast<MachineSDNode>(VStB)->setMemRefs(MemOp, MemOp + 1);
1712   return VStB;
1713 }
1714
1715 SDNode *ARMDAGToDAGISel::SelectVLDSTLane(SDNode *N, bool IsLoad,
1716                                          bool isUpdating, unsigned NumVecs,
1717                                          unsigned *DOpcodes,
1718                                          unsigned *QOpcodes) {
1719   assert(NumVecs >=2 && NumVecs <= 4 && "VLDSTLane NumVecs out-of-range");
1720   DebugLoc dl = N->getDebugLoc();
1721
1722   SDValue MemAddr, Align;
1723   unsigned AddrOpIdx = isUpdating ? 1 : 2;
1724   unsigned Vec0Idx = 3; // AddrOpIdx + (isUpdating ? 2 : 1)
1725   if (!SelectAddrMode6(N, N->getOperand(AddrOpIdx), MemAddr, Align))
1726     return NULL;
1727
1728   MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1729   MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
1730
1731   SDValue Chain = N->getOperand(0);
1732   unsigned Lane =
1733     cast<ConstantSDNode>(N->getOperand(Vec0Idx + NumVecs))->getZExtValue();
1734   EVT VT = N->getOperand(Vec0Idx).getValueType();
1735   bool is64BitVector = VT.is64BitVector();
1736
1737   unsigned Alignment = 0;
1738   if (NumVecs != 3) {
1739     Alignment = cast<ConstantSDNode>(Align)->getZExtValue();
1740     unsigned NumBytes = NumVecs * VT.getVectorElementType().getSizeInBits()/8;
1741     if (Alignment > NumBytes)
1742       Alignment = NumBytes;
1743     if (Alignment < 8 && Alignment < NumBytes)
1744       Alignment = 0;
1745     // Alignment must be a power of two; make sure of that.
1746     Alignment = (Alignment & -Alignment);
1747     if (Alignment == 1)
1748       Alignment = 0;
1749   }
1750   Align = CurDAG->getTargetConstant(Alignment, MVT::i32);
1751
1752   unsigned OpcodeIndex;
1753   switch (VT.getSimpleVT().SimpleTy) {
1754   default: llvm_unreachable("unhandled vld/vst lane type");
1755     // Double-register operations:
1756   case MVT::v8i8:  OpcodeIndex = 0; break;
1757   case MVT::v4i16: OpcodeIndex = 1; break;
1758   case MVT::v2f32:
1759   case MVT::v2i32: OpcodeIndex = 2; break;
1760     // Quad-register operations:
1761   case MVT::v8i16: OpcodeIndex = 0; break;
1762   case MVT::v4f32:
1763   case MVT::v4i32: OpcodeIndex = 1; break;
1764   }
1765
1766   std::vector<EVT> ResTys;
1767   if (IsLoad) {
1768     unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
1769     if (!is64BitVector)
1770       ResTyElts *= 2;
1771     ResTys.push_back(EVT::getVectorVT(*CurDAG->getContext(),
1772                                       MVT::i64, ResTyElts));
1773   }
1774   if (isUpdating)
1775     ResTys.push_back(MVT::i32);
1776   ResTys.push_back(MVT::Other);
1777
1778   SDValue Pred = getAL(CurDAG);
1779   SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1780
1781   SmallVector<SDValue, 8> Ops;
1782   Ops.push_back(MemAddr);
1783   Ops.push_back(Align);
1784   if (isUpdating) {
1785     SDValue Inc = N->getOperand(AddrOpIdx + 1);
1786     Ops.push_back(isa<ConstantSDNode>(Inc.getNode()) ? Reg0 : Inc);
1787   }
1788
1789   SDValue SuperReg;
1790   SDValue V0 = N->getOperand(Vec0Idx + 0);
1791   SDValue V1 = N->getOperand(Vec0Idx + 1);
1792   if (NumVecs == 2) {
1793     if (is64BitVector)
1794       SuperReg = SDValue(PairDRegs(MVT::v2i64, V0, V1), 0);
1795     else
1796       SuperReg = SDValue(PairQRegs(MVT::v4i64, V0, V1), 0);
1797   } else {
1798     SDValue V2 = N->getOperand(Vec0Idx + 2);
1799     SDValue V3 = (NumVecs == 3)
1800       ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
1801       : N->getOperand(Vec0Idx + 3);
1802     if (is64BitVector)
1803       SuperReg = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1804     else
1805       SuperReg = SDValue(QuadQRegs(MVT::v8i64, V0, V1, V2, V3), 0);
1806   }
1807   Ops.push_back(SuperReg);
1808   Ops.push_back(getI32Imm(Lane));
1809   Ops.push_back(Pred);
1810   Ops.push_back(Reg0);
1811   Ops.push_back(Chain);
1812
1813   unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
1814                                   QOpcodes[OpcodeIndex]);
1815   SDNode *VLdLn = CurDAG->getMachineNode(Opc, dl, ResTys,
1816                                          Ops.data(), Ops.size());
1817   cast<MachineSDNode>(VLdLn)->setMemRefs(MemOp, MemOp + 1);
1818   if (!IsLoad)
1819     return VLdLn;
1820
1821   // Extract the subregisters.
1822   SuperReg = SDValue(VLdLn, 0);
1823   assert(ARM::dsub_7 == ARM::dsub_0+7 &&
1824          ARM::qsub_3 == ARM::qsub_0+3 && "Unexpected subreg numbering");
1825   unsigned Sub0 = is64BitVector ? ARM::dsub_0 : ARM::qsub_0;
1826   for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1827     ReplaceUses(SDValue(N, Vec),
1828                 CurDAG->getTargetExtractSubreg(Sub0 + Vec, dl, VT, SuperReg));
1829   ReplaceUses(SDValue(N, NumVecs), SDValue(VLdLn, 1));
1830   if (isUpdating)
1831     ReplaceUses(SDValue(N, NumVecs + 1), SDValue(VLdLn, 2));
1832   return NULL;
1833 }
1834
1835 SDNode *ARMDAGToDAGISel::SelectVLDDup(SDNode *N, bool isUpdating,
1836                                       unsigned NumVecs, unsigned *Opcodes) {
1837   assert(NumVecs >=2 && NumVecs <= 4 && "VLDDup NumVecs out-of-range");
1838   DebugLoc dl = N->getDebugLoc();
1839
1840   SDValue MemAddr, Align;
1841   if (!SelectAddrMode6(N, N->getOperand(1), MemAddr, Align))
1842     return NULL;
1843
1844   MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1845   MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
1846
1847   SDValue Chain = N->getOperand(0);
1848   EVT VT = N->getValueType(0);
1849
1850   unsigned Alignment = 0;
1851   if (NumVecs != 3) {
1852     Alignment = cast<ConstantSDNode>(Align)->getZExtValue();
1853     unsigned NumBytes = NumVecs * VT.getVectorElementType().getSizeInBits()/8;
1854     if (Alignment > NumBytes)
1855       Alignment = NumBytes;
1856     if (Alignment < 8 && Alignment < NumBytes)
1857       Alignment = 0;
1858     // Alignment must be a power of two; make sure of that.
1859     Alignment = (Alignment & -Alignment);
1860     if (Alignment == 1)
1861       Alignment = 0;
1862   }
1863   Align = CurDAG->getTargetConstant(Alignment, MVT::i32);
1864
1865   unsigned OpcodeIndex;
1866   switch (VT.getSimpleVT().SimpleTy) {
1867   default: llvm_unreachable("unhandled vld-dup type");
1868   case MVT::v8i8:  OpcodeIndex = 0; break;
1869   case MVT::v4i16: OpcodeIndex = 1; break;
1870   case MVT::v2f32:
1871   case MVT::v2i32: OpcodeIndex = 2; break;
1872   }
1873
1874   SDValue Pred = getAL(CurDAG);
1875   SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1876   SDValue SuperReg;
1877   unsigned Opc = Opcodes[OpcodeIndex];
1878   SmallVector<SDValue, 6> Ops;
1879   Ops.push_back(MemAddr);
1880   Ops.push_back(Align);
1881   if (isUpdating) {
1882     SDValue Inc = N->getOperand(2);
1883     Ops.push_back(isa<ConstantSDNode>(Inc.getNode()) ? Reg0 : Inc);
1884   }
1885   Ops.push_back(Pred);
1886   Ops.push_back(Reg0);
1887   Ops.push_back(Chain);
1888
1889   unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
1890   std::vector<EVT> ResTys;
1891   ResTys.push_back(EVT::getVectorVT(*CurDAG->getContext(), MVT::i64,ResTyElts));
1892   if (isUpdating)
1893     ResTys.push_back(MVT::i32);
1894   ResTys.push_back(MVT::Other);
1895   SDNode *VLdDup =
1896     CurDAG->getMachineNode(Opc, dl, ResTys, Ops.data(), Ops.size());
1897   cast<MachineSDNode>(VLdDup)->setMemRefs(MemOp, MemOp + 1);
1898   SuperReg = SDValue(VLdDup, 0);
1899
1900   // Extract the subregisters.
1901   assert(ARM::dsub_7 == ARM::dsub_0+7 && "Unexpected subreg numbering");
1902   unsigned SubIdx = ARM::dsub_0;
1903   for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1904     ReplaceUses(SDValue(N, Vec),
1905                 CurDAG->getTargetExtractSubreg(SubIdx+Vec, dl, VT, SuperReg));
1906   ReplaceUses(SDValue(N, NumVecs), SDValue(VLdDup, 1));
1907   if (isUpdating)
1908     ReplaceUses(SDValue(N, NumVecs + 1), SDValue(VLdDup, 2));
1909   return NULL;
1910 }
1911
1912 SDNode *ARMDAGToDAGISel::SelectVTBL(SDNode *N, bool IsExt, unsigned NumVecs,
1913                                     unsigned Opc) {
1914   assert(NumVecs >= 2 && NumVecs <= 4 && "VTBL NumVecs out-of-range");
1915   DebugLoc dl = N->getDebugLoc();
1916   EVT VT = N->getValueType(0);
1917   unsigned FirstTblReg = IsExt ? 2 : 1;
1918
1919   // Form a REG_SEQUENCE to force register allocation.
1920   SDValue RegSeq;
1921   SDValue V0 = N->getOperand(FirstTblReg + 0);
1922   SDValue V1 = N->getOperand(FirstTblReg + 1);
1923   if (NumVecs == 2)
1924     RegSeq = SDValue(PairDRegs(MVT::v16i8, V0, V1), 0);
1925   else {
1926     SDValue V2 = N->getOperand(FirstTblReg + 2);
1927     // If it's a vtbl3, form a quad D-register and leave the last part as
1928     // an undef.
1929     SDValue V3 = (NumVecs == 3)
1930       ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
1931       : N->getOperand(FirstTblReg + 3);
1932     RegSeq = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1933   }
1934
1935   SmallVector<SDValue, 6> Ops;
1936   if (IsExt)
1937     Ops.push_back(N->getOperand(1));
1938   Ops.push_back(RegSeq);
1939   Ops.push_back(N->getOperand(FirstTblReg + NumVecs));
1940   Ops.push_back(getAL(CurDAG)); // predicate
1941   Ops.push_back(CurDAG->getRegister(0, MVT::i32)); // predicate register
1942   return CurDAG->getMachineNode(Opc, dl, VT, Ops.data(), Ops.size());
1943 }
1944
1945 SDNode *ARMDAGToDAGISel::SelectV6T2BitfieldExtractOp(SDNode *N,
1946                                                      bool isSigned) {
1947   if (!Subtarget->hasV6T2Ops())
1948     return NULL;
1949
1950   unsigned Opc = isSigned ? (Subtarget->isThumb() ? ARM::t2SBFX : ARM::SBFX)
1951     : (Subtarget->isThumb() ? ARM::t2UBFX : ARM::UBFX);
1952
1953
1954   // For unsigned extracts, check for a shift right and mask
1955   unsigned And_imm = 0;
1956   if (N->getOpcode() == ISD::AND) {
1957     if (isOpcWithIntImmediate(N, ISD::AND, And_imm)) {
1958
1959       // The immediate is a mask of the low bits iff imm & (imm+1) == 0
1960       if (And_imm & (And_imm + 1))
1961         return NULL;
1962
1963       unsigned Srl_imm = 0;
1964       if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRL,
1965                                 Srl_imm)) {
1966         assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
1967
1968         unsigned Width = CountTrailingOnes_32(And_imm);
1969         unsigned LSB = Srl_imm;
1970         SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1971         SDValue Ops[] = { N->getOperand(0).getOperand(0),
1972                           CurDAG->getTargetConstant(LSB, MVT::i32),
1973                           CurDAG->getTargetConstant(Width, MVT::i32),
1974           getAL(CurDAG), Reg0 };
1975         return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
1976       }
1977     }
1978     return NULL;
1979   }
1980
1981   // Otherwise, we're looking for a shift of a shift
1982   unsigned Shl_imm = 0;
1983   if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SHL, Shl_imm)) {
1984     assert(Shl_imm > 0 && Shl_imm < 32 && "bad amount in shift node!");
1985     unsigned Srl_imm = 0;
1986     if (isInt32Immediate(N->getOperand(1), Srl_imm)) {
1987       assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
1988       unsigned Width = 32 - Srl_imm;
1989       int LSB = Srl_imm - Shl_imm;
1990       if (LSB < 0)
1991         return NULL;
1992       SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1993       SDValue Ops[] = { N->getOperand(0).getOperand(0),
1994                         CurDAG->getTargetConstant(LSB, MVT::i32),
1995                         CurDAG->getTargetConstant(Width, MVT::i32),
1996                         getAL(CurDAG), Reg0 };
1997       return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
1998     }
1999   }
2000   return NULL;
2001 }
2002
2003 SDNode *ARMDAGToDAGISel::
2004 SelectT2CMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
2005                     ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
2006   SDValue CPTmp0;
2007   SDValue CPTmp1;
2008   if (SelectT2ShifterOperandReg(TrueVal, CPTmp0, CPTmp1)) {
2009     unsigned SOVal = cast<ConstantSDNode>(CPTmp1)->getZExtValue();
2010     unsigned SOShOp = ARM_AM::getSORegShOp(SOVal);
2011     unsigned Opc = 0;
2012     switch (SOShOp) {
2013     case ARM_AM::lsl: Opc = ARM::t2MOVCClsl; break;
2014     case ARM_AM::lsr: Opc = ARM::t2MOVCClsr; break;
2015     case ARM_AM::asr: Opc = ARM::t2MOVCCasr; break;
2016     case ARM_AM::ror: Opc = ARM::t2MOVCCror; break;
2017     default:
2018       llvm_unreachable("Unknown so_reg opcode!");
2019       break;
2020     }
2021     SDValue SOShImm =
2022       CurDAG->getTargetConstant(ARM_AM::getSORegOffset(SOVal), MVT::i32);
2023     SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
2024     SDValue Ops[] = { FalseVal, CPTmp0, SOShImm, CC, CCR, InFlag };
2025     return CurDAG->SelectNodeTo(N, Opc, MVT::i32,Ops, 6);
2026   }
2027   return 0;
2028 }
2029
2030 SDNode *ARMDAGToDAGISel::
2031 SelectARMCMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
2032                      ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
2033   SDValue CPTmp0;
2034   SDValue CPTmp1;
2035   SDValue CPTmp2;
2036   if (SelectShifterOperandReg(TrueVal, CPTmp0, CPTmp1, CPTmp2)) {
2037     SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
2038     SDValue Ops[] = { FalseVal, CPTmp0, CPTmp1, CPTmp2, CC, CCR, InFlag };
2039     return CurDAG->SelectNodeTo(N, ARM::MOVCCs, MVT::i32, Ops, 7);
2040   }
2041   return 0;
2042 }
2043
2044 SDNode *ARMDAGToDAGISel::
2045 SelectT2CMOVImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
2046                   ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
2047   ConstantSDNode *T = dyn_cast<ConstantSDNode>(TrueVal);
2048   if (!T)
2049     return 0;
2050
2051   unsigned Opc = 0;
2052   unsigned TrueImm = T->getZExtValue();
2053   if (is_t2_so_imm(TrueImm)) {
2054     Opc = ARM::t2MOVCCi;
2055   } else if (TrueImm <= 0xffff) {
2056     Opc = ARM::t2MOVCCi16;
2057   } else if (is_t2_so_imm_not(TrueImm)) {
2058     TrueImm = ~TrueImm;
2059     Opc = ARM::t2MVNCCi;
2060   } else if (TrueVal.getNode()->hasOneUse() && Subtarget->hasV6T2Ops()) {
2061     // Large immediate.
2062     Opc = ARM::t2MOVCCi32imm;
2063   }
2064
2065   if (Opc) {
2066     SDValue True = CurDAG->getTargetConstant(TrueImm, MVT::i32);
2067     SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
2068     SDValue Ops[] = { FalseVal, True, CC, CCR, InFlag };
2069     return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
2070   }
2071
2072   return 0;
2073 }
2074
2075 SDNode *ARMDAGToDAGISel::
2076 SelectARMCMOVImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
2077                    ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
2078   ConstantSDNode *T = dyn_cast<ConstantSDNode>(TrueVal);
2079   if (!T)
2080     return 0;
2081
2082   unsigned Opc = 0;
2083   unsigned TrueImm = T->getZExtValue();
2084   bool isSoImm = is_so_imm(TrueImm);
2085   if (isSoImm) {
2086     Opc = ARM::MOVCCi;
2087   } else if (Subtarget->hasV6T2Ops() && TrueImm <= 0xffff) {
2088     Opc = ARM::MOVCCi16;
2089   } else if (is_so_imm_not(TrueImm)) {
2090     TrueImm = ~TrueImm;
2091     Opc = ARM::MVNCCi;
2092   } else if (TrueVal.getNode()->hasOneUse() &&
2093              (Subtarget->hasV6T2Ops() || ARM_AM::isSOImmTwoPartVal(TrueImm))) {
2094     // Large immediate.
2095     Opc = ARM::MOVCCi32imm;
2096   }
2097
2098   if (Opc) {
2099     SDValue True = CurDAG->getTargetConstant(TrueImm, MVT::i32);
2100     SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
2101     SDValue Ops[] = { FalseVal, True, CC, CCR, InFlag };
2102     return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
2103   }
2104
2105   return 0;
2106 }
2107
2108 SDNode *ARMDAGToDAGISel::SelectCMOVOp(SDNode *N) {
2109   EVT VT = N->getValueType(0);
2110   SDValue FalseVal = N->getOperand(0);
2111   SDValue TrueVal  = N->getOperand(1);
2112   SDValue CC = N->getOperand(2);
2113   SDValue CCR = N->getOperand(3);
2114   SDValue InFlag = N->getOperand(4);
2115   assert(CC.getOpcode() == ISD::Constant);
2116   assert(CCR.getOpcode() == ISD::Register);
2117   ARMCC::CondCodes CCVal =
2118     (ARMCC::CondCodes)cast<ConstantSDNode>(CC)->getZExtValue();
2119
2120   if (!Subtarget->isThumb1Only() && VT == MVT::i32) {
2121     // Pattern: (ARMcmov:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
2122     // Emits: (MOVCCs:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
2123     // Pattern complexity = 18  cost = 1  size = 0
2124     SDValue CPTmp0;
2125     SDValue CPTmp1;
2126     SDValue CPTmp2;
2127     if (Subtarget->isThumb()) {
2128       SDNode *Res = SelectT2CMOVShiftOp(N, FalseVal, TrueVal,
2129                                         CCVal, CCR, InFlag);
2130       if (!Res)
2131         Res = SelectT2CMOVShiftOp(N, TrueVal, FalseVal,
2132                                ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
2133       if (Res)
2134         return Res;
2135     } else {
2136       SDNode *Res = SelectARMCMOVShiftOp(N, FalseVal, TrueVal,
2137                                          CCVal, CCR, InFlag);
2138       if (!Res)
2139         Res = SelectARMCMOVShiftOp(N, TrueVal, FalseVal,
2140                                ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
2141       if (Res)
2142         return Res;
2143     }
2144
2145     // Pattern: (ARMcmov:i32 GPR:i32:$false,
2146     //             (imm:i32)<<P:Pred_so_imm>>:$true,
2147     //             (imm:i32):$cc)
2148     // Emits: (MOVCCi:i32 GPR:i32:$false,
2149     //           (so_imm:i32 (imm:i32):$true), (imm:i32):$cc)
2150     // Pattern complexity = 10  cost = 1  size = 0
2151     if (Subtarget->isThumb()) {
2152       SDNode *Res = SelectT2CMOVImmOp(N, FalseVal, TrueVal,
2153                                         CCVal, CCR, InFlag);
2154       if (!Res)
2155         Res = SelectT2CMOVImmOp(N, TrueVal, FalseVal,
2156                                ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
2157       if (Res)
2158         return Res;
2159     } else {
2160       SDNode *Res = SelectARMCMOVImmOp(N, FalseVal, TrueVal,
2161                                          CCVal, CCR, InFlag);
2162       if (!Res)
2163         Res = SelectARMCMOVImmOp(N, TrueVal, FalseVal,
2164                                ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
2165       if (Res)
2166         return Res;
2167     }
2168   }
2169
2170   // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
2171   // Emits: (MOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
2172   // Pattern complexity = 6  cost = 1  size = 0
2173   //
2174   // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
2175   // Emits: (tMOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
2176   // Pattern complexity = 6  cost = 11  size = 0
2177   //
2178   // Also VMOVScc and VMOVDcc.
2179   SDValue Tmp2 = CurDAG->getTargetConstant(CCVal, MVT::i32);
2180   SDValue Ops[] = { FalseVal, TrueVal, Tmp2, CCR, InFlag };
2181   unsigned Opc = 0;
2182   switch (VT.getSimpleVT().SimpleTy) {
2183   default: assert(false && "Illegal conditional move type!");
2184     break;
2185   case MVT::i32:
2186     Opc = Subtarget->isThumb()
2187       ? (Subtarget->hasThumb2() ? ARM::t2MOVCCr : ARM::tMOVCCr_pseudo)
2188       : ARM::MOVCCr;
2189     break;
2190   case MVT::f32:
2191     Opc = ARM::VMOVScc;
2192     break;
2193   case MVT::f64:
2194     Opc = ARM::VMOVDcc;
2195     break;
2196   }
2197   return CurDAG->SelectNodeTo(N, Opc, VT, Ops, 5);
2198 }
2199
2200 SDNode *ARMDAGToDAGISel::SelectConcatVector(SDNode *N) {
2201   // The only time a CONCAT_VECTORS operation can have legal types is when
2202   // two 64-bit vectors are concatenated to a 128-bit vector.
2203   EVT VT = N->getValueType(0);
2204   if (!VT.is128BitVector() || N->getNumOperands() != 2)
2205     llvm_unreachable("unexpected CONCAT_VECTORS");
2206   return PairDRegs(VT, N->getOperand(0), N->getOperand(1));
2207 }
2208
2209 SDNode *ARMDAGToDAGISel::Select(SDNode *N) {
2210   DebugLoc dl = N->getDebugLoc();
2211
2212   if (N->isMachineOpcode())
2213     return NULL;   // Already selected.
2214
2215   switch (N->getOpcode()) {
2216   default: break;
2217   case ISD::Constant: {
2218     unsigned Val = cast<ConstantSDNode>(N)->getZExtValue();
2219     bool UseCP = true;
2220     if (Subtarget->hasThumb2())
2221       // Thumb2-aware targets have the MOVT instruction, so all immediates can
2222       // be done with MOV + MOVT, at worst.
2223       UseCP = 0;
2224     else {
2225       if (Subtarget->isThumb()) {
2226         UseCP = (Val > 255 &&                          // MOV
2227                  ~Val > 255 &&                         // MOV + MVN
2228                  !ARM_AM::isThumbImmShiftedVal(Val));  // MOV + LSL
2229       } else
2230         UseCP = (ARM_AM::getSOImmVal(Val) == -1 &&     // MOV
2231                  ARM_AM::getSOImmVal(~Val) == -1 &&    // MVN
2232                  !ARM_AM::isSOImmTwoPartVal(Val));     // two instrs.
2233     }
2234
2235     if (UseCP) {
2236       SDValue CPIdx =
2237         CurDAG->getTargetConstantPool(ConstantInt::get(
2238                                   Type::getInt32Ty(*CurDAG->getContext()), Val),
2239                                       TLI.getPointerTy());
2240
2241       SDNode *ResNode;
2242       if (Subtarget->isThumb1Only()) {
2243         SDValue Pred = getAL(CurDAG);
2244         SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
2245         SDValue Ops[] = { CPIdx, Pred, PredReg, CurDAG->getEntryNode() };
2246         ResNode = CurDAG->getMachineNode(ARM::tLDRpci, dl, MVT::i32, MVT::Other,
2247                                          Ops, 4);
2248       } else {
2249         SDValue Ops[] = {
2250           CPIdx,
2251           CurDAG->getTargetConstant(0, MVT::i32),
2252           getAL(CurDAG),
2253           CurDAG->getRegister(0, MVT::i32),
2254           CurDAG->getEntryNode()
2255         };
2256         ResNode=CurDAG->getMachineNode(ARM::LDRcp, dl, MVT::i32, MVT::Other,
2257                                        Ops, 5);
2258       }
2259       ReplaceUses(SDValue(N, 0), SDValue(ResNode, 0));
2260       return NULL;
2261     }
2262
2263     // Other cases are autogenerated.
2264     break;
2265   }
2266   case ISD::FrameIndex: {
2267     // Selects to ADDri FI, 0 which in turn will become ADDri SP, imm.
2268     int FI = cast<FrameIndexSDNode>(N)->getIndex();
2269     SDValue TFI = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
2270     if (Subtarget->isThumb1Only()) {
2271       return CurDAG->SelectNodeTo(N, ARM::tADDrSPi, MVT::i32, TFI,
2272                                   CurDAG->getTargetConstant(0, MVT::i32));
2273     } else {
2274       unsigned Opc = ((Subtarget->isThumb() && Subtarget->hasThumb2()) ?
2275                       ARM::t2ADDri : ARM::ADDri);
2276       SDValue Ops[] = { TFI, CurDAG->getTargetConstant(0, MVT::i32),
2277                         getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
2278                         CurDAG->getRegister(0, MVT::i32) };
2279       return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
2280     }
2281   }
2282   case ISD::SRL:
2283     if (SDNode *I = SelectV6T2BitfieldExtractOp(N, false))
2284       return I;
2285     break;
2286   case ISD::SRA:
2287     if (SDNode *I = SelectV6T2BitfieldExtractOp(N, true))
2288       return I;
2289     break;
2290   case ISD::MUL:
2291     if (Subtarget->isThumb1Only())
2292       break;
2293     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
2294       unsigned RHSV = C->getZExtValue();
2295       if (!RHSV) break;
2296       if (isPowerOf2_32(RHSV-1)) {  // 2^n+1?
2297         unsigned ShImm = Log2_32(RHSV-1);
2298         if (ShImm >= 32)
2299           break;
2300         SDValue V = N->getOperand(0);
2301         ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
2302         SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, MVT::i32);
2303         SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2304         if (Subtarget->isThumb()) {
2305           SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
2306           return CurDAG->SelectNodeTo(N, ARM::t2ADDrs, MVT::i32, Ops, 6);
2307         } else {
2308           SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
2309           return CurDAG->SelectNodeTo(N, ARM::ADDrs, MVT::i32, Ops, 7);
2310         }
2311       }
2312       if (isPowerOf2_32(RHSV+1)) {  // 2^n-1?
2313         unsigned ShImm = Log2_32(RHSV+1);
2314         if (ShImm >= 32)
2315           break;
2316         SDValue V = N->getOperand(0);
2317         ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
2318         SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, MVT::i32);
2319         SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2320         if (Subtarget->isThumb()) {
2321           SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
2322           return CurDAG->SelectNodeTo(N, ARM::t2RSBrs, MVT::i32, Ops, 6);
2323         } else {
2324           SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
2325           return CurDAG->SelectNodeTo(N, ARM::RSBrs, MVT::i32, Ops, 7);
2326         }
2327       }
2328     }
2329     break;
2330   case ISD::AND: {
2331     // Check for unsigned bitfield extract
2332     if (SDNode *I = SelectV6T2BitfieldExtractOp(N, false))
2333       return I;
2334
2335     // (and (or x, c2), c1) and top 16-bits of c1 and c2 match, lower 16-bits
2336     // of c1 are 0xffff, and lower 16-bit of c2 are 0. That is, the top 16-bits
2337     // are entirely contributed by c2 and lower 16-bits are entirely contributed
2338     // by x. That's equal to (or (and x, 0xffff), (and c1, 0xffff0000)).
2339     // Select it to: "movt x, ((c1 & 0xffff) >> 16)
2340     EVT VT = N->getValueType(0);
2341     if (VT != MVT::i32)
2342       break;
2343     unsigned Opc = (Subtarget->isThumb() && Subtarget->hasThumb2())
2344       ? ARM::t2MOVTi16
2345       : (Subtarget->hasV6T2Ops() ? ARM::MOVTi16 : 0);
2346     if (!Opc)
2347       break;
2348     SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
2349     ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2350     if (!N1C)
2351       break;
2352     if (N0.getOpcode() == ISD::OR && N0.getNode()->hasOneUse()) {
2353       SDValue N2 = N0.getOperand(1);
2354       ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
2355       if (!N2C)
2356         break;
2357       unsigned N1CVal = N1C->getZExtValue();
2358       unsigned N2CVal = N2C->getZExtValue();
2359       if ((N1CVal & 0xffff0000U) == (N2CVal & 0xffff0000U) &&
2360           (N1CVal & 0xffffU) == 0xffffU &&
2361           (N2CVal & 0xffffU) == 0x0U) {
2362         SDValue Imm16 = CurDAG->getTargetConstant((N2CVal & 0xFFFF0000U) >> 16,
2363                                                   MVT::i32);
2364         SDValue Ops[] = { N0.getOperand(0), Imm16,
2365                           getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
2366         return CurDAG->getMachineNode(Opc, dl, VT, Ops, 4);
2367       }
2368     }
2369     break;
2370   }
2371   case ARMISD::VMOVRRD:
2372     return CurDAG->getMachineNode(ARM::VMOVRRD, dl, MVT::i32, MVT::i32,
2373                                   N->getOperand(0), getAL(CurDAG),
2374                                   CurDAG->getRegister(0, MVT::i32));
2375   case ISD::UMUL_LOHI: {
2376     if (Subtarget->isThumb1Only())
2377       break;
2378     if (Subtarget->isThumb()) {
2379       SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
2380                         getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
2381                         CurDAG->getRegister(0, MVT::i32) };
2382       return CurDAG->getMachineNode(ARM::t2UMULL, dl, MVT::i32, MVT::i32,Ops,4);
2383     } else {
2384       SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
2385                         getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
2386                         CurDAG->getRegister(0, MVT::i32) };
2387       return CurDAG->getMachineNode(Subtarget->hasV6Ops() ?
2388                                     ARM::UMULL : ARM::UMULLv5,
2389                                     dl, MVT::i32, MVT::i32, Ops, 5);
2390     }
2391   }
2392   case ISD::SMUL_LOHI: {
2393     if (Subtarget->isThumb1Only())
2394       break;
2395     if (Subtarget->isThumb()) {
2396       SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
2397                         getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
2398       return CurDAG->getMachineNode(ARM::t2SMULL, dl, MVT::i32, MVT::i32,Ops,4);
2399     } else {
2400       SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
2401                         getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
2402                         CurDAG->getRegister(0, MVT::i32) };
2403       return CurDAG->getMachineNode(Subtarget->hasV6Ops() ?
2404                                     ARM::SMULL : ARM::SMULLv5,
2405                                     dl, MVT::i32, MVT::i32, Ops, 5);
2406     }
2407   }
2408   case ISD::LOAD: {
2409     SDNode *ResNode = 0;
2410     if (Subtarget->isThumb() && Subtarget->hasThumb2())
2411       ResNode = SelectT2IndexedLoad(N);
2412     else
2413       ResNode = SelectARMIndexedLoad(N);
2414     if (ResNode)
2415       return ResNode;
2416     // Other cases are autogenerated.
2417     break;
2418   }
2419   case ARMISD::BRCOND: {
2420     // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
2421     // Emits: (Bcc:void (bb:Other):$dst, (imm:i32):$cc)
2422     // Pattern complexity = 6  cost = 1  size = 0
2423
2424     // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
2425     // Emits: (tBcc:void (bb:Other):$dst, (imm:i32):$cc)
2426     // Pattern complexity = 6  cost = 1  size = 0
2427
2428     // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
2429     // Emits: (t2Bcc:void (bb:Other):$dst, (imm:i32):$cc)
2430     // Pattern complexity = 6  cost = 1  size = 0
2431
2432     unsigned Opc = Subtarget->isThumb() ?
2433       ((Subtarget->hasThumb2()) ? ARM::t2Bcc : ARM::tBcc) : ARM::Bcc;
2434     SDValue Chain = N->getOperand(0);
2435     SDValue N1 = N->getOperand(1);
2436     SDValue N2 = N->getOperand(2);
2437     SDValue N3 = N->getOperand(3);
2438     SDValue InFlag = N->getOperand(4);
2439     assert(N1.getOpcode() == ISD::BasicBlock);
2440     assert(N2.getOpcode() == ISD::Constant);
2441     assert(N3.getOpcode() == ISD::Register);
2442
2443     SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
2444                                cast<ConstantSDNode>(N2)->getZExtValue()),
2445                                MVT::i32);
2446     SDValue Ops[] = { N1, Tmp2, N3, Chain, InFlag };
2447     SDNode *ResNode = CurDAG->getMachineNode(Opc, dl, MVT::Other,
2448                                              MVT::Glue, Ops, 5);
2449     Chain = SDValue(ResNode, 0);
2450     if (N->getNumValues() == 2) {
2451       InFlag = SDValue(ResNode, 1);
2452       ReplaceUses(SDValue(N, 1), InFlag);
2453     }
2454     ReplaceUses(SDValue(N, 0),
2455                 SDValue(Chain.getNode(), Chain.getResNo()));
2456     return NULL;
2457   }
2458   case ARMISD::CMOV:
2459     return SelectCMOVOp(N);
2460   case ARMISD::VZIP: {
2461     unsigned Opc = 0;
2462     EVT VT = N->getValueType(0);
2463     switch (VT.getSimpleVT().SimpleTy) {
2464     default: return NULL;
2465     case MVT::v8i8:  Opc = ARM::VZIPd8; break;
2466     case MVT::v4i16: Opc = ARM::VZIPd16; break;
2467     case MVT::v2f32:
2468     case MVT::v2i32: Opc = ARM::VZIPd32; break;
2469     case MVT::v16i8: Opc = ARM::VZIPq8; break;
2470     case MVT::v8i16: Opc = ARM::VZIPq16; break;
2471     case MVT::v4f32:
2472     case MVT::v4i32: Opc = ARM::VZIPq32; break;
2473     }
2474     SDValue Pred = getAL(CurDAG);
2475     SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
2476     SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
2477     return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
2478   }
2479   case ARMISD::VUZP: {
2480     unsigned Opc = 0;
2481     EVT VT = N->getValueType(0);
2482     switch (VT.getSimpleVT().SimpleTy) {
2483     default: return NULL;
2484     case MVT::v8i8:  Opc = ARM::VUZPd8; break;
2485     case MVT::v4i16: Opc = ARM::VUZPd16; break;
2486     case MVT::v2f32:
2487     case MVT::v2i32: Opc = ARM::VUZPd32; break;
2488     case MVT::v16i8: Opc = ARM::VUZPq8; break;
2489     case MVT::v8i16: Opc = ARM::VUZPq16; break;
2490     case MVT::v4f32:
2491     case MVT::v4i32: Opc = ARM::VUZPq32; break;
2492     }
2493     SDValue Pred = getAL(CurDAG);
2494     SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
2495     SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
2496     return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
2497   }
2498   case ARMISD::VTRN: {
2499     unsigned Opc = 0;
2500     EVT VT = N->getValueType(0);
2501     switch (VT.getSimpleVT().SimpleTy) {
2502     default: return NULL;
2503     case MVT::v8i8:  Opc = ARM::VTRNd8; break;
2504     case MVT::v4i16: Opc = ARM::VTRNd16; break;
2505     case MVT::v2f32:
2506     case MVT::v2i32: Opc = ARM::VTRNd32; break;
2507     case MVT::v16i8: Opc = ARM::VTRNq8; break;
2508     case MVT::v8i16: Opc = ARM::VTRNq16; break;
2509     case MVT::v4f32:
2510     case MVT::v4i32: Opc = ARM::VTRNq32; break;
2511     }
2512     SDValue Pred = getAL(CurDAG);
2513     SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
2514     SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
2515     return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
2516   }
2517   case ARMISD::BUILD_VECTOR: {
2518     EVT VecVT = N->getValueType(0);
2519     EVT EltVT = VecVT.getVectorElementType();
2520     unsigned NumElts = VecVT.getVectorNumElements();
2521     if (EltVT == MVT::f64) {
2522       assert(NumElts == 2 && "unexpected type for BUILD_VECTOR");
2523       return PairDRegs(VecVT, N->getOperand(0), N->getOperand(1));
2524     }
2525     assert(EltVT == MVT::f32 && "unexpected type for BUILD_VECTOR");
2526     if (NumElts == 2)
2527       return PairSRegs(VecVT, N->getOperand(0), N->getOperand(1));
2528     assert(NumElts == 4 && "unexpected type for BUILD_VECTOR");
2529     return QuadSRegs(VecVT, N->getOperand(0), N->getOperand(1),
2530                      N->getOperand(2), N->getOperand(3));
2531   }
2532
2533   case ARMISD::VLD2DUP: {
2534     unsigned Opcodes[] = { ARM::VLD2DUPd8Pseudo, ARM::VLD2DUPd16Pseudo,
2535                            ARM::VLD2DUPd32Pseudo };
2536     return SelectVLDDup(N, false, 2, Opcodes);
2537   }
2538
2539   case ARMISD::VLD3DUP: {
2540     unsigned Opcodes[] = { ARM::VLD3DUPd8Pseudo, ARM::VLD3DUPd16Pseudo,
2541                            ARM::VLD3DUPd32Pseudo };
2542     return SelectVLDDup(N, false, 3, Opcodes);
2543   }
2544
2545   case ARMISD::VLD4DUP: {
2546     unsigned Opcodes[] = { ARM::VLD4DUPd8Pseudo, ARM::VLD4DUPd16Pseudo,
2547                            ARM::VLD4DUPd32Pseudo };
2548     return SelectVLDDup(N, false, 4, Opcodes);
2549   }
2550
2551   case ARMISD::VLD2DUP_UPD: {
2552     unsigned Opcodes[] = { ARM::VLD2DUPd8Pseudo_UPD, ARM::VLD2DUPd16Pseudo_UPD,
2553                            ARM::VLD2DUPd32Pseudo_UPD };
2554     return SelectVLDDup(N, true, 2, Opcodes);
2555   }
2556
2557   case ARMISD::VLD3DUP_UPD: {
2558     unsigned Opcodes[] = { ARM::VLD3DUPd8Pseudo_UPD, ARM::VLD3DUPd16Pseudo_UPD,
2559                            ARM::VLD3DUPd32Pseudo_UPD };
2560     return SelectVLDDup(N, true, 3, Opcodes);
2561   }
2562
2563   case ARMISD::VLD4DUP_UPD: {
2564     unsigned Opcodes[] = { ARM::VLD4DUPd8Pseudo_UPD, ARM::VLD4DUPd16Pseudo_UPD,
2565                            ARM::VLD4DUPd32Pseudo_UPD };
2566     return SelectVLDDup(N, true, 4, Opcodes);
2567   }
2568
2569   case ARMISD::VLD1_UPD: {
2570     unsigned DOpcodes[] = { ARM::VLD1d8_UPD, ARM::VLD1d16_UPD,
2571                             ARM::VLD1d32_UPD, ARM::VLD1d64_UPD };
2572     unsigned QOpcodes[] = { ARM::VLD1q8Pseudo_UPD, ARM::VLD1q16Pseudo_UPD,
2573                             ARM::VLD1q32Pseudo_UPD, ARM::VLD1q64Pseudo_UPD };
2574     return SelectVLD(N, true, 1, DOpcodes, QOpcodes, 0);
2575   }
2576
2577   case ARMISD::VLD2_UPD: {
2578     unsigned DOpcodes[] = { ARM::VLD2d8Pseudo_UPD, ARM::VLD2d16Pseudo_UPD,
2579                             ARM::VLD2d32Pseudo_UPD, ARM::VLD1q64Pseudo_UPD };
2580     unsigned QOpcodes[] = { ARM::VLD2q8Pseudo_UPD, ARM::VLD2q16Pseudo_UPD,
2581                             ARM::VLD2q32Pseudo_UPD };
2582     return SelectVLD(N, true, 2, DOpcodes, QOpcodes, 0);
2583   }
2584
2585   case ARMISD::VLD3_UPD: {
2586     unsigned DOpcodes[] = { ARM::VLD3d8Pseudo_UPD, ARM::VLD3d16Pseudo_UPD,
2587                             ARM::VLD3d32Pseudo_UPD, ARM::VLD1d64TPseudo_UPD };
2588     unsigned QOpcodes0[] = { ARM::VLD3q8Pseudo_UPD,
2589                              ARM::VLD3q16Pseudo_UPD,
2590                              ARM::VLD3q32Pseudo_UPD };
2591     unsigned QOpcodes1[] = { ARM::VLD3q8oddPseudo_UPD,
2592                              ARM::VLD3q16oddPseudo_UPD,
2593                              ARM::VLD3q32oddPseudo_UPD };
2594     return SelectVLD(N, true, 3, DOpcodes, QOpcodes0, QOpcodes1);
2595   }
2596
2597   case ARMISD::VLD4_UPD: {
2598     unsigned DOpcodes[] = { ARM::VLD4d8Pseudo_UPD, ARM::VLD4d16Pseudo_UPD,
2599                             ARM::VLD4d32Pseudo_UPD, ARM::VLD1d64QPseudo_UPD };
2600     unsigned QOpcodes0[] = { ARM::VLD4q8Pseudo_UPD,
2601                              ARM::VLD4q16Pseudo_UPD,
2602                              ARM::VLD4q32Pseudo_UPD };
2603     unsigned QOpcodes1[] = { ARM::VLD4q8oddPseudo_UPD,
2604                              ARM::VLD4q16oddPseudo_UPD,
2605                              ARM::VLD4q32oddPseudo_UPD };
2606     return SelectVLD(N, true, 4, DOpcodes, QOpcodes0, QOpcodes1);
2607   }
2608
2609   case ARMISD::VLD2LN_UPD: {
2610     unsigned DOpcodes[] = { ARM::VLD2LNd8Pseudo_UPD, ARM::VLD2LNd16Pseudo_UPD,
2611                             ARM::VLD2LNd32Pseudo_UPD };
2612     unsigned QOpcodes[] = { ARM::VLD2LNq16Pseudo_UPD,
2613                             ARM::VLD2LNq32Pseudo_UPD };
2614     return SelectVLDSTLane(N, true, true, 2, DOpcodes, QOpcodes);
2615   }
2616
2617   case ARMISD::VLD3LN_UPD: {
2618     unsigned DOpcodes[] = { ARM::VLD3LNd8Pseudo_UPD, ARM::VLD3LNd16Pseudo_UPD,
2619                             ARM::VLD3LNd32Pseudo_UPD };
2620     unsigned QOpcodes[] = { ARM::VLD3LNq16Pseudo_UPD,
2621                             ARM::VLD3LNq32Pseudo_UPD };
2622     return SelectVLDSTLane(N, true, true, 3, DOpcodes, QOpcodes);
2623   }
2624
2625   case ARMISD::VLD4LN_UPD: {
2626     unsigned DOpcodes[] = { ARM::VLD4LNd8Pseudo_UPD, ARM::VLD4LNd16Pseudo_UPD,
2627                             ARM::VLD4LNd32Pseudo_UPD };
2628     unsigned QOpcodes[] = { ARM::VLD4LNq16Pseudo_UPD,
2629                             ARM::VLD4LNq32Pseudo_UPD };
2630     return SelectVLDSTLane(N, true, true, 4, DOpcodes, QOpcodes);
2631   }
2632
2633   case ARMISD::VST1_UPD: {
2634     unsigned DOpcodes[] = { ARM::VST1d8_UPD, ARM::VST1d16_UPD,
2635                             ARM::VST1d32_UPD, ARM::VST1d64_UPD };
2636     unsigned QOpcodes[] = { ARM::VST1q8Pseudo_UPD, ARM::VST1q16Pseudo_UPD,
2637                             ARM::VST1q32Pseudo_UPD, ARM::VST1q64Pseudo_UPD };
2638     return SelectVST(N, true, 1, DOpcodes, QOpcodes, 0);
2639   }
2640
2641   case ARMISD::VST2_UPD: {
2642     unsigned DOpcodes[] = { ARM::VST2d8Pseudo_UPD, ARM::VST2d16Pseudo_UPD,
2643                             ARM::VST2d32Pseudo_UPD, ARM::VST1q64Pseudo_UPD };
2644     unsigned QOpcodes[] = { ARM::VST2q8Pseudo_UPD, ARM::VST2q16Pseudo_UPD,
2645                             ARM::VST2q32Pseudo_UPD };
2646     return SelectVST(N, true, 2, DOpcodes, QOpcodes, 0);
2647   }
2648
2649   case ARMISD::VST3_UPD: {
2650     unsigned DOpcodes[] = { ARM::VST3d8Pseudo_UPD, ARM::VST3d16Pseudo_UPD,
2651                             ARM::VST3d32Pseudo_UPD, ARM::VST1d64TPseudo_UPD };
2652     unsigned QOpcodes0[] = { ARM::VST3q8Pseudo_UPD,
2653                              ARM::VST3q16Pseudo_UPD,
2654                              ARM::VST3q32Pseudo_UPD };
2655     unsigned QOpcodes1[] = { ARM::VST3q8oddPseudo_UPD,
2656                              ARM::VST3q16oddPseudo_UPD,
2657                              ARM::VST3q32oddPseudo_UPD };
2658     return SelectVST(N, true, 3, DOpcodes, QOpcodes0, QOpcodes1);
2659   }
2660
2661   case ARMISD::VST4_UPD: {
2662     unsigned DOpcodes[] = { ARM::VST4d8Pseudo_UPD, ARM::VST4d16Pseudo_UPD,
2663                             ARM::VST4d32Pseudo_UPD, ARM::VST1d64QPseudo_UPD };
2664     unsigned QOpcodes0[] = { ARM::VST4q8Pseudo_UPD,
2665                              ARM::VST4q16Pseudo_UPD,
2666                              ARM::VST4q32Pseudo_UPD };
2667     unsigned QOpcodes1[] = { ARM::VST4q8oddPseudo_UPD,
2668                              ARM::VST4q16oddPseudo_UPD,
2669                              ARM::VST4q32oddPseudo_UPD };
2670     return SelectVST(N, true, 4, DOpcodes, QOpcodes0, QOpcodes1);
2671   }
2672
2673   case ARMISD::VST2LN_UPD: {
2674     unsigned DOpcodes[] = { ARM::VST2LNd8Pseudo_UPD, ARM::VST2LNd16Pseudo_UPD,
2675                             ARM::VST2LNd32Pseudo_UPD };
2676     unsigned QOpcodes[] = { ARM::VST2LNq16Pseudo_UPD,
2677                             ARM::VST2LNq32Pseudo_UPD };
2678     return SelectVLDSTLane(N, false, true, 2, DOpcodes, QOpcodes);
2679   }
2680
2681   case ARMISD::VST3LN_UPD: {
2682     unsigned DOpcodes[] = { ARM::VST3LNd8Pseudo_UPD, ARM::VST3LNd16Pseudo_UPD,
2683                             ARM::VST3LNd32Pseudo_UPD };
2684     unsigned QOpcodes[] = { ARM::VST3LNq16Pseudo_UPD,
2685                             ARM::VST3LNq32Pseudo_UPD };
2686     return SelectVLDSTLane(N, false, true, 3, DOpcodes, QOpcodes);
2687   }
2688
2689   case ARMISD::VST4LN_UPD: {
2690     unsigned DOpcodes[] = { ARM::VST4LNd8Pseudo_UPD, ARM::VST4LNd16Pseudo_UPD,
2691                             ARM::VST4LNd32Pseudo_UPD };
2692     unsigned QOpcodes[] = { ARM::VST4LNq16Pseudo_UPD,
2693                             ARM::VST4LNq32Pseudo_UPD };
2694     return SelectVLDSTLane(N, false, true, 4, DOpcodes, QOpcodes);
2695   }
2696
2697   case ISD::INTRINSIC_VOID:
2698   case ISD::INTRINSIC_W_CHAIN: {
2699     unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
2700     switch (IntNo) {
2701     default:
2702       break;
2703
2704     case Intrinsic::arm_neon_vld1: {
2705       unsigned DOpcodes[] = { ARM::VLD1d8, ARM::VLD1d16,
2706                               ARM::VLD1d32, ARM::VLD1d64 };
2707       unsigned QOpcodes[] = { ARM::VLD1q8Pseudo, ARM::VLD1q16Pseudo,
2708                               ARM::VLD1q32Pseudo, ARM::VLD1q64Pseudo };
2709       return SelectVLD(N, false, 1, DOpcodes, QOpcodes, 0);
2710     }
2711
2712     case Intrinsic::arm_neon_vld2: {
2713       unsigned DOpcodes[] = { ARM::VLD2d8Pseudo, ARM::VLD2d16Pseudo,
2714                               ARM::VLD2d32Pseudo, ARM::VLD1q64Pseudo };
2715       unsigned QOpcodes[] = { ARM::VLD2q8Pseudo, ARM::VLD2q16Pseudo,
2716                               ARM::VLD2q32Pseudo };
2717       return SelectVLD(N, false, 2, DOpcodes, QOpcodes, 0);
2718     }
2719
2720     case Intrinsic::arm_neon_vld3: {
2721       unsigned DOpcodes[] = { ARM::VLD3d8Pseudo, ARM::VLD3d16Pseudo,
2722                               ARM::VLD3d32Pseudo, ARM::VLD1d64TPseudo };
2723       unsigned QOpcodes0[] = { ARM::VLD3q8Pseudo_UPD,
2724                                ARM::VLD3q16Pseudo_UPD,
2725                                ARM::VLD3q32Pseudo_UPD };
2726       unsigned QOpcodes1[] = { ARM::VLD3q8oddPseudo,
2727                                ARM::VLD3q16oddPseudo,
2728                                ARM::VLD3q32oddPseudo };
2729       return SelectVLD(N, false, 3, DOpcodes, QOpcodes0, QOpcodes1);
2730     }
2731
2732     case Intrinsic::arm_neon_vld4: {
2733       unsigned DOpcodes[] = { ARM::VLD4d8Pseudo, ARM::VLD4d16Pseudo,
2734                               ARM::VLD4d32Pseudo, ARM::VLD1d64QPseudo };
2735       unsigned QOpcodes0[] = { ARM::VLD4q8Pseudo_UPD,
2736                                ARM::VLD4q16Pseudo_UPD,
2737                                ARM::VLD4q32Pseudo_UPD };
2738       unsigned QOpcodes1[] = { ARM::VLD4q8oddPseudo,
2739                                ARM::VLD4q16oddPseudo,
2740                                ARM::VLD4q32oddPseudo };
2741       return SelectVLD(N, false, 4, DOpcodes, QOpcodes0, QOpcodes1);
2742     }
2743
2744     case Intrinsic::arm_neon_vld2lane: {
2745       unsigned DOpcodes[] = { ARM::VLD2LNd8Pseudo, ARM::VLD2LNd16Pseudo,
2746                               ARM::VLD2LNd32Pseudo };
2747       unsigned QOpcodes[] = { ARM::VLD2LNq16Pseudo, ARM::VLD2LNq32Pseudo };
2748       return SelectVLDSTLane(N, true, false, 2, DOpcodes, QOpcodes);
2749     }
2750
2751     case Intrinsic::arm_neon_vld3lane: {
2752       unsigned DOpcodes[] = { ARM::VLD3LNd8Pseudo, ARM::VLD3LNd16Pseudo,
2753                               ARM::VLD3LNd32Pseudo };
2754       unsigned QOpcodes[] = { ARM::VLD3LNq16Pseudo, ARM::VLD3LNq32Pseudo };
2755       return SelectVLDSTLane(N, true, false, 3, DOpcodes, QOpcodes);
2756     }
2757
2758     case Intrinsic::arm_neon_vld4lane: {
2759       unsigned DOpcodes[] = { ARM::VLD4LNd8Pseudo, ARM::VLD4LNd16Pseudo,
2760                               ARM::VLD4LNd32Pseudo };
2761       unsigned QOpcodes[] = { ARM::VLD4LNq16Pseudo, ARM::VLD4LNq32Pseudo };
2762       return SelectVLDSTLane(N, true, false, 4, DOpcodes, QOpcodes);
2763     }
2764
2765     case Intrinsic::arm_neon_vst1: {
2766       unsigned DOpcodes[] = { ARM::VST1d8, ARM::VST1d16,
2767                               ARM::VST1d32, ARM::VST1d64 };
2768       unsigned QOpcodes[] = { ARM::VST1q8Pseudo, ARM::VST1q16Pseudo,
2769                               ARM::VST1q32Pseudo, ARM::VST1q64Pseudo };
2770       return SelectVST(N, false, 1, DOpcodes, QOpcodes, 0);
2771     }
2772
2773     case Intrinsic::arm_neon_vst2: {
2774       unsigned DOpcodes[] = { ARM::VST2d8Pseudo, ARM::VST2d16Pseudo,
2775                               ARM::VST2d32Pseudo, ARM::VST1q64Pseudo };
2776       unsigned QOpcodes[] = { ARM::VST2q8Pseudo, ARM::VST2q16Pseudo,
2777                               ARM::VST2q32Pseudo };
2778       return SelectVST(N, false, 2, DOpcodes, QOpcodes, 0);
2779     }
2780
2781     case Intrinsic::arm_neon_vst3: {
2782       unsigned DOpcodes[] = { ARM::VST3d8Pseudo, ARM::VST3d16Pseudo,
2783                               ARM::VST3d32Pseudo, ARM::VST1d64TPseudo };
2784       unsigned QOpcodes0[] = { ARM::VST3q8Pseudo_UPD,
2785                                ARM::VST3q16Pseudo_UPD,
2786                                ARM::VST3q32Pseudo_UPD };
2787       unsigned QOpcodes1[] = { ARM::VST3q8oddPseudo,
2788                                ARM::VST3q16oddPseudo,
2789                                ARM::VST3q32oddPseudo };
2790       return SelectVST(N, false, 3, DOpcodes, QOpcodes0, QOpcodes1);
2791     }
2792
2793     case Intrinsic::arm_neon_vst4: {
2794       unsigned DOpcodes[] = { ARM::VST4d8Pseudo, ARM::VST4d16Pseudo,
2795                               ARM::VST4d32Pseudo, ARM::VST1d64QPseudo };
2796       unsigned QOpcodes0[] = { ARM::VST4q8Pseudo_UPD,
2797                                ARM::VST4q16Pseudo_UPD,
2798                                ARM::VST4q32Pseudo_UPD };
2799       unsigned QOpcodes1[] = { ARM::VST4q8oddPseudo,
2800                                ARM::VST4q16oddPseudo,
2801                                ARM::VST4q32oddPseudo };
2802       return SelectVST(N, false, 4, DOpcodes, QOpcodes0, QOpcodes1);
2803     }
2804
2805     case Intrinsic::arm_neon_vst2lane: {
2806       unsigned DOpcodes[] = { ARM::VST2LNd8Pseudo, ARM::VST2LNd16Pseudo,
2807                               ARM::VST2LNd32Pseudo };
2808       unsigned QOpcodes[] = { ARM::VST2LNq16Pseudo, ARM::VST2LNq32Pseudo };
2809       return SelectVLDSTLane(N, false, false, 2, DOpcodes, QOpcodes);
2810     }
2811
2812     case Intrinsic::arm_neon_vst3lane: {
2813       unsigned DOpcodes[] = { ARM::VST3LNd8Pseudo, ARM::VST3LNd16Pseudo,
2814                               ARM::VST3LNd32Pseudo };
2815       unsigned QOpcodes[] = { ARM::VST3LNq16Pseudo, ARM::VST3LNq32Pseudo };
2816       return SelectVLDSTLane(N, false, false, 3, DOpcodes, QOpcodes);
2817     }
2818
2819     case Intrinsic::arm_neon_vst4lane: {
2820       unsigned DOpcodes[] = { ARM::VST4LNd8Pseudo, ARM::VST4LNd16Pseudo,
2821                               ARM::VST4LNd32Pseudo };
2822       unsigned QOpcodes[] = { ARM::VST4LNq16Pseudo, ARM::VST4LNq32Pseudo };
2823       return SelectVLDSTLane(N, false, false, 4, DOpcodes, QOpcodes);
2824     }
2825     }
2826     break;
2827   }
2828
2829   case ISD::INTRINSIC_WO_CHAIN: {
2830     unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
2831     switch (IntNo) {
2832     default:
2833       break;
2834
2835     case Intrinsic::arm_neon_vtbl2:
2836       return SelectVTBL(N, false, 2, ARM::VTBL2Pseudo);
2837     case Intrinsic::arm_neon_vtbl3:
2838       return SelectVTBL(N, false, 3, ARM::VTBL3Pseudo);
2839     case Intrinsic::arm_neon_vtbl4:
2840       return SelectVTBL(N, false, 4, ARM::VTBL4Pseudo);
2841
2842     case Intrinsic::arm_neon_vtbx2:
2843       return SelectVTBL(N, true, 2, ARM::VTBX2Pseudo);
2844     case Intrinsic::arm_neon_vtbx3:
2845       return SelectVTBL(N, true, 3, ARM::VTBX3Pseudo);
2846     case Intrinsic::arm_neon_vtbx4:
2847       return SelectVTBL(N, true, 4, ARM::VTBX4Pseudo);
2848     }
2849     break;
2850   }
2851
2852   case ARMISD::VTBL1: {
2853     DebugLoc dl = N->getDebugLoc();
2854     EVT VT = N->getValueType(0);
2855     SmallVector<SDValue, 6> Ops;
2856
2857     Ops.push_back(N->getOperand(0));
2858     Ops.push_back(N->getOperand(1));
2859     Ops.push_back(getAL(CurDAG));                    // Predicate
2860     Ops.push_back(CurDAG->getRegister(0, MVT::i32)); // Predicate Register
2861     return CurDAG->getMachineNode(ARM::VTBL1, dl, VT, Ops.data(), Ops.size());
2862   }
2863   case ARMISD::VTBL2: {
2864     DebugLoc dl = N->getDebugLoc();
2865     EVT VT = N->getValueType(0);
2866
2867     // Form a REG_SEQUENCE to force register allocation.
2868     SDValue V0 = N->getOperand(0);
2869     SDValue V1 = N->getOperand(1);
2870     SDValue RegSeq = SDValue(PairDRegs(MVT::v16i8, V0, V1), 0);
2871
2872     SmallVector<SDValue, 6> Ops;
2873     Ops.push_back(RegSeq);
2874     Ops.push_back(N->getOperand(2));
2875     Ops.push_back(getAL(CurDAG));                    // Predicate
2876     Ops.push_back(CurDAG->getRegister(0, MVT::i32)); // Predicate Register
2877     return CurDAG->getMachineNode(ARM::VTBL2Pseudo, dl, VT,
2878                                   Ops.data(), Ops.size());
2879   }
2880
2881   case ISD::CONCAT_VECTORS:
2882     return SelectConcatVector(N);
2883   }
2884
2885   return SelectCode(N);
2886 }
2887
2888 bool ARMDAGToDAGISel::
2889 SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
2890                              std::vector<SDValue> &OutOps) {
2891   assert(ConstraintCode == 'm' && "unexpected asm memory constraint");
2892   // Require the address to be in a register.  That is safe for all ARM
2893   // variants and it is hard to do anything much smarter without knowing
2894   // how the operand is used.
2895   OutOps.push_back(Op);
2896   return false;
2897 }
2898
2899 /// createARMISelDag - This pass converts a legalized DAG into a
2900 /// ARM-specific DAG, ready for instruction scheduling.
2901 ///
2902 FunctionPass *llvm::createARMISelDag(ARMBaseTargetMachine &TM,
2903                                      CodeGenOpt::Level OptLevel) {
2904   return new ARMDAGToDAGISel(TM, OptLevel);
2905 }