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