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