ARM/ISel: Factor out isScaledConstantInRange() helper.
[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     unsigned Opc = DOpcodes[OpcodeIndex];
1491     const SDValue Ops[] = { MemAddr, Align, Pred, Reg0, Chain };
1492     SDNode *VLd = CurDAG->getMachineNode(Opc, dl, 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     unsigned Opc = QOpcodes0[OpcodeIndex];
1511     const SDValue Ops[] = { MemAddr, Align, Pred, Reg0, Chain };
1512     SDNode *VLd = CurDAG->getMachineNode(Opc, dl, 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     unsigned Opc = QOpcodes0[OpcodeIndex];
1526     SDValue ImplDef =
1527       SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, ResTy), 0);
1528     const SDValue OpsA[] = { MemAddr, Align, Reg0, ImplDef, Pred, Reg0, Chain };
1529     SDNode *VLdA =
1530       CurDAG->getMachineNode(Opc, dl, ResTy, AddrTy, MVT::Other, OpsA, 7);
1531     Chain = SDValue(VLdA, 2);
1532
1533     // Load the odd subregs.
1534     Opc = QOpcodes1[OpcodeIndex];
1535     const SDValue OpsB[] = { SDValue(VLdA, 1), Align, Reg0, SDValue(VLdA, 0),
1536                              Pred, Reg0, Chain };
1537     SDNode *VLdB =
1538       CurDAG->getMachineNode(Opc, dl, ResTy, AddrTy, MVT::Other, OpsB, 7);
1539     SuperReg = SDValue(VLdB, 0);
1540     Chain = SDValue(VLdB, 2);
1541   }
1542
1543   // Extract out the Q registers.
1544   assert(ARM::qsub_3 == ARM::qsub_0+3 && "Unexpected subreg numbering");
1545   for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
1546     SDValue Q = CurDAG->getTargetExtractSubreg(ARM::qsub_0+Vec,
1547                                                dl, VT, SuperReg);
1548     ReplaceUses(SDValue(N, Vec), Q);
1549   }
1550   ReplaceUses(SDValue(N, NumVecs), Chain);
1551   return NULL;
1552 }
1553
1554 SDNode *ARMDAGToDAGISel::SelectVST(SDNode *N, unsigned NumVecs,
1555                                    unsigned *DOpcodes, unsigned *QOpcodes0,
1556                                    unsigned *QOpcodes1) {
1557   assert(NumVecs >= 1 && NumVecs <= 4 && "VST NumVecs out-of-range");
1558   DebugLoc dl = N->getDebugLoc();
1559
1560   SDValue MemAddr, Align;
1561   if (!SelectAddrMode6(N, N->getOperand(2), MemAddr, Align))
1562     return NULL;
1563
1564   SDValue Chain = N->getOperand(0);
1565   EVT VT = N->getOperand(3).getValueType();
1566   bool is64BitVector = VT.is64BitVector();
1567   Align = GetVLDSTAlign(Align, NumVecs, is64BitVector);
1568
1569   unsigned OpcodeIndex;
1570   switch (VT.getSimpleVT().SimpleTy) {
1571   default: llvm_unreachable("unhandled vst type");
1572     // Double-register operations:
1573   case MVT::v8i8:  OpcodeIndex = 0; break;
1574   case MVT::v4i16: OpcodeIndex = 1; break;
1575   case MVT::v2f32:
1576   case MVT::v2i32: OpcodeIndex = 2; break;
1577   case MVT::v1i64: OpcodeIndex = 3; break;
1578     // Quad-register operations:
1579   case MVT::v16i8: OpcodeIndex = 0; break;
1580   case MVT::v8i16: OpcodeIndex = 1; break;
1581   case MVT::v4f32:
1582   case MVT::v4i32: OpcodeIndex = 2; break;
1583   case MVT::v2i64: OpcodeIndex = 3;
1584     assert(NumVecs == 1 && "v2i64 type only supported for VST1");
1585     break;
1586   }
1587
1588   SDValue Pred = getAL(CurDAG);
1589   SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1590
1591   SmallVector<SDValue, 7> Ops;
1592   Ops.push_back(MemAddr);
1593   Ops.push_back(Align);
1594
1595   if (is64BitVector) {
1596     if (NumVecs == 1) {
1597       Ops.push_back(N->getOperand(3));
1598     } else {
1599       SDValue RegSeq;
1600       SDValue V0 = N->getOperand(0+3);
1601       SDValue V1 = N->getOperand(1+3);
1602
1603       // Form a REG_SEQUENCE to force register allocation.
1604       if (NumVecs == 2)
1605         RegSeq = SDValue(PairDRegs(MVT::v2i64, V0, V1), 0);
1606       else {
1607         SDValue V2 = N->getOperand(2+3);
1608         // If it's a vld3, form a quad D-register and leave the last part as
1609         // an undef.
1610         SDValue V3 = (NumVecs == 3)
1611           ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
1612           : N->getOperand(3+3);
1613         RegSeq = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1614       }
1615       Ops.push_back(RegSeq);
1616     }
1617     Ops.push_back(Pred);
1618     Ops.push_back(Reg0); // predicate register
1619     Ops.push_back(Chain);
1620     unsigned Opc = DOpcodes[OpcodeIndex];
1621     return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 6);
1622   }
1623
1624   if (NumVecs <= 2) {
1625     // Quad registers are directly supported for VST1 and VST2.
1626     unsigned Opc = QOpcodes0[OpcodeIndex];
1627     if (NumVecs == 1) {
1628       Ops.push_back(N->getOperand(3));
1629     } else {
1630       // Form a QQ register.
1631       SDValue Q0 = N->getOperand(3);
1632       SDValue Q1 = N->getOperand(4);
1633       Ops.push_back(SDValue(PairQRegs(MVT::v4i64, Q0, Q1), 0));
1634     }
1635     Ops.push_back(Pred);
1636     Ops.push_back(Reg0); // predicate register
1637     Ops.push_back(Chain);
1638     return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 6);
1639   }
1640
1641   // Otherwise, quad registers are stored with two separate instructions,
1642   // where one stores the even registers and the other stores the odd registers.
1643
1644   // Form the QQQQ REG_SEQUENCE.
1645   SDValue V0 = N->getOperand(0+3);
1646   SDValue V1 = N->getOperand(1+3);
1647   SDValue V2 = N->getOperand(2+3);
1648   SDValue V3 = (NumVecs == 3)
1649     ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
1650     : N->getOperand(3+3);
1651   SDValue RegSeq = SDValue(QuadQRegs(MVT::v8i64, V0, V1, V2, V3), 0);
1652
1653   // Store the even D registers.
1654   Ops.push_back(Reg0); // post-access address offset
1655   Ops.push_back(RegSeq);
1656   Ops.push_back(Pred);
1657   Ops.push_back(Reg0); // predicate register
1658   Ops.push_back(Chain);
1659   unsigned Opc = QOpcodes0[OpcodeIndex];
1660   SDNode *VStA = CurDAG->getMachineNode(Opc, dl, MemAddr.getValueType(),
1661                                         MVT::Other, Ops.data(), 7);
1662   Chain = SDValue(VStA, 1);
1663
1664   // Store the odd D registers.
1665   Ops[0] = SDValue(VStA, 0); // MemAddr
1666   Ops[6] = Chain;
1667   Opc = QOpcodes1[OpcodeIndex];
1668   SDNode *VStB = CurDAG->getMachineNode(Opc, dl, MemAddr.getValueType(),
1669                                         MVT::Other, Ops.data(), 7);
1670   Chain = SDValue(VStB, 1);
1671   ReplaceUses(SDValue(N, 0), Chain);
1672   return NULL;
1673 }
1674
1675 SDNode *ARMDAGToDAGISel::SelectVLDSTLane(SDNode *N, bool IsLoad,
1676                                          unsigned NumVecs, unsigned *DOpcodes,
1677                                          unsigned *QOpcodes) {
1678   assert(NumVecs >=2 && NumVecs <= 4 && "VLDSTLane NumVecs out-of-range");
1679   DebugLoc dl = N->getDebugLoc();
1680
1681   SDValue MemAddr, Align;
1682   if (!SelectAddrMode6(N, N->getOperand(2), MemAddr, Align))
1683     return NULL;
1684
1685   SDValue Chain = N->getOperand(0);
1686   unsigned Lane =
1687     cast<ConstantSDNode>(N->getOperand(NumVecs+3))->getZExtValue();
1688   EVT VT = IsLoad ? N->getValueType(0) : N->getOperand(3).getValueType();
1689   bool is64BitVector = VT.is64BitVector();
1690
1691   unsigned Alignment = 0;
1692   if (NumVecs != 3) {
1693     Alignment = cast<ConstantSDNode>(Align)->getZExtValue();
1694     unsigned NumBytes = NumVecs * VT.getVectorElementType().getSizeInBits()/8;
1695     if (Alignment > NumBytes)
1696       Alignment = NumBytes;
1697     if (Alignment < 8 && Alignment < NumBytes)
1698       Alignment = 0;
1699     // Alignment must be a power of two; make sure of that.
1700     Alignment = (Alignment & -Alignment);
1701     if (Alignment == 1)
1702       Alignment = 0;
1703   }
1704   Align = CurDAG->getTargetConstant(Alignment, MVT::i32);
1705
1706   unsigned OpcodeIndex;
1707   switch (VT.getSimpleVT().SimpleTy) {
1708   default: llvm_unreachable("unhandled vld/vst lane type");
1709     // Double-register operations:
1710   case MVT::v8i8:  OpcodeIndex = 0; break;
1711   case MVT::v4i16: OpcodeIndex = 1; break;
1712   case MVT::v2f32:
1713   case MVT::v2i32: OpcodeIndex = 2; break;
1714     // Quad-register operations:
1715   case MVT::v8i16: OpcodeIndex = 0; break;
1716   case MVT::v4f32:
1717   case MVT::v4i32: OpcodeIndex = 1; break;
1718   }
1719
1720   SDValue Pred = getAL(CurDAG);
1721   SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1722
1723   SmallVector<SDValue, 7> Ops;
1724   Ops.push_back(MemAddr);
1725   Ops.push_back(Align);
1726
1727   unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
1728                                   QOpcodes[OpcodeIndex]);
1729
1730   SDValue SuperReg;
1731   SDValue V0 = N->getOperand(0+3);
1732   SDValue V1 = N->getOperand(1+3);
1733   if (NumVecs == 2) {
1734     if (is64BitVector)
1735       SuperReg = SDValue(PairDRegs(MVT::v2i64, V0, V1), 0);
1736     else
1737       SuperReg = SDValue(PairQRegs(MVT::v4i64, V0, V1), 0);
1738   } else {
1739     SDValue V2 = N->getOperand(2+3);
1740     SDValue V3 = (NumVecs == 3)
1741       ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
1742       : N->getOperand(3+3);
1743     if (is64BitVector)
1744       SuperReg = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1745     else
1746       SuperReg = SDValue(QuadQRegs(MVT::v8i64, V0, V1, V2, V3), 0);
1747   }
1748   Ops.push_back(SuperReg);
1749   Ops.push_back(getI32Imm(Lane));
1750   Ops.push_back(Pred);
1751   Ops.push_back(Reg0);
1752   Ops.push_back(Chain);
1753
1754   if (!IsLoad)
1755     return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 7);
1756
1757   EVT ResTy;
1758   unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
1759   if (!is64BitVector)
1760     ResTyElts *= 2;
1761   ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts);
1762
1763   SDNode *VLdLn = CurDAG->getMachineNode(Opc, dl, ResTy, MVT::Other,
1764                                          Ops.data(), 7);
1765   SuperReg = SDValue(VLdLn, 0);
1766   Chain = SDValue(VLdLn, 1);
1767
1768   // Extract the subregisters.
1769   assert(ARM::dsub_7 == ARM::dsub_0+7 && "Unexpected subreg numbering");
1770   assert(ARM::qsub_3 == ARM::qsub_0+3 && "Unexpected subreg numbering");
1771   unsigned SubIdx = is64BitVector ? ARM::dsub_0 : ARM::qsub_0;
1772   for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1773     ReplaceUses(SDValue(N, Vec),
1774                 CurDAG->getTargetExtractSubreg(SubIdx+Vec, dl, VT, SuperReg));
1775   ReplaceUses(SDValue(N, NumVecs), Chain);
1776   return NULL;
1777 }
1778
1779 SDNode *ARMDAGToDAGISel::SelectVLDDup(SDNode *N, unsigned NumVecs,
1780                                       unsigned *Opcodes) {
1781   assert(NumVecs >=2 && NumVecs <= 4 && "VLDDup NumVecs out-of-range");
1782   DebugLoc dl = N->getDebugLoc();
1783
1784   SDValue MemAddr, Align;
1785   if (!SelectAddrMode6(N, N->getOperand(1), MemAddr, Align))
1786     return NULL;
1787
1788   SDValue Chain = N->getOperand(0);
1789   EVT VT = N->getValueType(0);
1790
1791   unsigned Alignment = 0;
1792   if (NumVecs != 3) {
1793     Alignment = cast<ConstantSDNode>(Align)->getZExtValue();
1794     unsigned NumBytes = NumVecs * VT.getVectorElementType().getSizeInBits()/8;
1795     if (Alignment > NumBytes)
1796       Alignment = NumBytes;
1797     if (Alignment < 8 && Alignment < NumBytes)
1798       Alignment = 0;
1799     // Alignment must be a power of two; make sure of that.
1800     Alignment = (Alignment & -Alignment);
1801     if (Alignment == 1)
1802       Alignment = 0;
1803   }
1804   Align = CurDAG->getTargetConstant(Alignment, MVT::i32);
1805
1806   unsigned OpcodeIndex;
1807   switch (VT.getSimpleVT().SimpleTy) {
1808   default: llvm_unreachable("unhandled vld-dup type");
1809   case MVT::v8i8:  OpcodeIndex = 0; break;
1810   case MVT::v4i16: OpcodeIndex = 1; break;
1811   case MVT::v2f32:
1812   case MVT::v2i32: OpcodeIndex = 2; break;
1813   }
1814
1815   SDValue Pred = getAL(CurDAG);
1816   SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1817   SDValue SuperReg;
1818   unsigned Opc = Opcodes[OpcodeIndex];
1819   const SDValue Ops[] = { MemAddr, Align, Pred, Reg0, Chain };
1820
1821   unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
1822   EVT ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts);
1823   SDNode *VLdDup = CurDAG->getMachineNode(Opc, dl, ResTy, MVT::Other, Ops, 5);
1824   SuperReg = SDValue(VLdDup, 0);
1825   Chain = SDValue(VLdDup, 1);
1826
1827   // Extract the subregisters.
1828   assert(ARM::dsub_7 == ARM::dsub_0+7 && "Unexpected subreg numbering");
1829   unsigned SubIdx = ARM::dsub_0;
1830   for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1831     ReplaceUses(SDValue(N, Vec),
1832                 CurDAG->getTargetExtractSubreg(SubIdx+Vec, dl, VT, SuperReg));
1833   ReplaceUses(SDValue(N, NumVecs), Chain);
1834   return NULL;
1835 }
1836
1837 SDNode *ARMDAGToDAGISel::SelectVTBL(SDNode *N, bool IsExt, unsigned NumVecs,
1838                                     unsigned Opc) {
1839   assert(NumVecs >= 2 && NumVecs <= 4 && "VTBL NumVecs out-of-range");
1840   DebugLoc dl = N->getDebugLoc();
1841   EVT VT = N->getValueType(0);
1842   unsigned FirstTblReg = IsExt ? 2 : 1;
1843
1844   // Form a REG_SEQUENCE to force register allocation.
1845   SDValue RegSeq;
1846   SDValue V0 = N->getOperand(FirstTblReg + 0);
1847   SDValue V1 = N->getOperand(FirstTblReg + 1);
1848   if (NumVecs == 2)
1849     RegSeq = SDValue(PairDRegs(MVT::v16i8, V0, V1), 0);
1850   else {
1851     SDValue V2 = N->getOperand(FirstTblReg + 2);
1852     // If it's a vtbl3, form a quad D-register and leave the last part as
1853     // an undef.
1854     SDValue V3 = (NumVecs == 3)
1855       ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
1856       : N->getOperand(FirstTblReg + 3);
1857     RegSeq = SDValue(QuadDRegs(MVT::v4i64, V0, V1, V2, V3), 0);
1858   }
1859
1860   SmallVector<SDValue, 6> Ops;
1861   if (IsExt)
1862     Ops.push_back(N->getOperand(1));
1863   Ops.push_back(RegSeq);
1864   Ops.push_back(N->getOperand(FirstTblReg + NumVecs));
1865   Ops.push_back(getAL(CurDAG)); // predicate
1866   Ops.push_back(CurDAG->getRegister(0, MVT::i32)); // predicate register
1867   return CurDAG->getMachineNode(Opc, dl, VT, Ops.data(), Ops.size());
1868 }
1869
1870 SDNode *ARMDAGToDAGISel::SelectV6T2BitfieldExtractOp(SDNode *N,
1871                                                      bool isSigned) {
1872   if (!Subtarget->hasV6T2Ops())
1873     return NULL;
1874
1875   unsigned Opc = isSigned ? (Subtarget->isThumb() ? ARM::t2SBFX : ARM::SBFX)
1876     : (Subtarget->isThumb() ? ARM::t2UBFX : ARM::UBFX);
1877
1878
1879   // For unsigned extracts, check for a shift right and mask
1880   unsigned And_imm = 0;
1881   if (N->getOpcode() == ISD::AND) {
1882     if (isOpcWithIntImmediate(N, ISD::AND, And_imm)) {
1883
1884       // The immediate is a mask of the low bits iff imm & (imm+1) == 0
1885       if (And_imm & (And_imm + 1))
1886         return NULL;
1887
1888       unsigned Srl_imm = 0;
1889       if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRL,
1890                                 Srl_imm)) {
1891         assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
1892
1893         unsigned Width = CountTrailingOnes_32(And_imm);
1894         unsigned LSB = Srl_imm;
1895         SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1896         SDValue Ops[] = { N->getOperand(0).getOperand(0),
1897                           CurDAG->getTargetConstant(LSB, MVT::i32),
1898                           CurDAG->getTargetConstant(Width, MVT::i32),
1899           getAL(CurDAG), Reg0 };
1900         return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
1901       }
1902     }
1903     return NULL;
1904   }
1905
1906   // Otherwise, we're looking for a shift of a shift
1907   unsigned Shl_imm = 0;
1908   if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SHL, Shl_imm)) {
1909     assert(Shl_imm > 0 && Shl_imm < 32 && "bad amount in shift node!");
1910     unsigned Srl_imm = 0;
1911     if (isInt32Immediate(N->getOperand(1), Srl_imm)) {
1912       assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
1913       unsigned Width = 32 - Srl_imm;
1914       int LSB = Srl_imm - Shl_imm;
1915       if (LSB < 0)
1916         return NULL;
1917       SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1918       SDValue Ops[] = { N->getOperand(0).getOperand(0),
1919                         CurDAG->getTargetConstant(LSB, MVT::i32),
1920                         CurDAG->getTargetConstant(Width, MVT::i32),
1921                         getAL(CurDAG), Reg0 };
1922       return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
1923     }
1924   }
1925   return NULL;
1926 }
1927
1928 SDNode *ARMDAGToDAGISel::
1929 SelectT2CMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
1930                     ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1931   SDValue CPTmp0;
1932   SDValue CPTmp1;
1933   if (SelectT2ShifterOperandReg(TrueVal, CPTmp0, CPTmp1)) {
1934     unsigned SOVal = cast<ConstantSDNode>(CPTmp1)->getZExtValue();
1935     unsigned SOShOp = ARM_AM::getSORegShOp(SOVal);
1936     unsigned Opc = 0;
1937     switch (SOShOp) {
1938     case ARM_AM::lsl: Opc = ARM::t2MOVCClsl; break;
1939     case ARM_AM::lsr: Opc = ARM::t2MOVCClsr; break;
1940     case ARM_AM::asr: Opc = ARM::t2MOVCCasr; break;
1941     case ARM_AM::ror: Opc = ARM::t2MOVCCror; break;
1942     default:
1943       llvm_unreachable("Unknown so_reg opcode!");
1944       break;
1945     }
1946     SDValue SOShImm =
1947       CurDAG->getTargetConstant(ARM_AM::getSORegOffset(SOVal), MVT::i32);
1948     SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1949     SDValue Ops[] = { FalseVal, CPTmp0, SOShImm, CC, CCR, InFlag };
1950     return CurDAG->SelectNodeTo(N, Opc, MVT::i32,Ops, 6);
1951   }
1952   return 0;
1953 }
1954
1955 SDNode *ARMDAGToDAGISel::
1956 SelectARMCMOVShiftOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
1957                      ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1958   SDValue CPTmp0;
1959   SDValue CPTmp1;
1960   SDValue CPTmp2;
1961   if (SelectShifterOperandReg(TrueVal, CPTmp0, CPTmp1, CPTmp2)) {
1962     SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1963     SDValue Ops[] = { FalseVal, CPTmp0, CPTmp1, CPTmp2, CC, CCR, InFlag };
1964     return CurDAG->SelectNodeTo(N, ARM::MOVCCs, MVT::i32, Ops, 7);
1965   }
1966   return 0;
1967 }
1968
1969 SDNode *ARMDAGToDAGISel::
1970 SelectT2CMOVImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
1971                   ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1972   ConstantSDNode *T = dyn_cast<ConstantSDNode>(TrueVal);
1973   if (!T)
1974     return 0;
1975
1976   unsigned Opc = 0;
1977   unsigned TrueImm = T->getZExtValue();
1978   if (is_t2_so_imm(TrueImm)) {
1979     Opc = ARM::t2MOVCCi;
1980   } else if (TrueImm <= 0xffff) {
1981     Opc = ARM::t2MOVCCi16;
1982   } else if (is_t2_so_imm_not(TrueImm)) {
1983     TrueImm = ~TrueImm;
1984     Opc = ARM::t2MVNCCi;
1985   } else if (TrueVal.getNode()->hasOneUse() && Subtarget->hasV6T2Ops()) {
1986     // Large immediate.
1987     Opc = ARM::t2MOVCCi32imm;
1988   }
1989
1990   if (Opc) {
1991     SDValue True = CurDAG->getTargetConstant(TrueImm, MVT::i32);
1992     SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1993     SDValue Ops[] = { FalseVal, True, CC, CCR, InFlag };
1994     return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
1995   }
1996
1997   return 0;
1998 }
1999
2000 SDNode *ARMDAGToDAGISel::
2001 SelectARMCMOVImmOp(SDNode *N, SDValue FalseVal, SDValue TrueVal,
2002                    ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
2003   ConstantSDNode *T = dyn_cast<ConstantSDNode>(TrueVal);
2004   if (!T)
2005     return 0;
2006
2007   unsigned Opc = 0;
2008   unsigned TrueImm = T->getZExtValue();
2009   bool isSoImm = is_so_imm(TrueImm);
2010   if (isSoImm) {
2011     Opc = ARM::MOVCCi;
2012   } else if (Subtarget->hasV6T2Ops() && TrueImm <= 0xffff) {
2013     Opc = ARM::MOVCCi16;
2014   } else if (is_so_imm_not(TrueImm)) {
2015     TrueImm = ~TrueImm;
2016     Opc = ARM::MVNCCi;
2017   } else if (TrueVal.getNode()->hasOneUse() &&
2018              (Subtarget->hasV6T2Ops() || ARM_AM::isSOImmTwoPartVal(TrueImm))) {
2019     // Large immediate.
2020     Opc = ARM::MOVCCi32imm;
2021   }
2022
2023   if (Opc) {
2024     SDValue True = CurDAG->getTargetConstant(TrueImm, MVT::i32);
2025     SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
2026     SDValue Ops[] = { FalseVal, True, CC, CCR, InFlag };
2027     return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
2028   }
2029
2030   return 0;
2031 }
2032
2033 SDNode *ARMDAGToDAGISel::SelectCMOVOp(SDNode *N) {
2034   EVT VT = N->getValueType(0);
2035   SDValue FalseVal = N->getOperand(0);
2036   SDValue TrueVal  = N->getOperand(1);
2037   SDValue CC = N->getOperand(2);
2038   SDValue CCR = N->getOperand(3);
2039   SDValue InFlag = N->getOperand(4);
2040   assert(CC.getOpcode() == ISD::Constant);
2041   assert(CCR.getOpcode() == ISD::Register);
2042   ARMCC::CondCodes CCVal =
2043     (ARMCC::CondCodes)cast<ConstantSDNode>(CC)->getZExtValue();
2044
2045   if (!Subtarget->isThumb1Only() && VT == MVT::i32) {
2046     // Pattern: (ARMcmov:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
2047     // Emits: (MOVCCs:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
2048     // Pattern complexity = 18  cost = 1  size = 0
2049     SDValue CPTmp0;
2050     SDValue CPTmp1;
2051     SDValue CPTmp2;
2052     if (Subtarget->isThumb()) {
2053       SDNode *Res = SelectT2CMOVShiftOp(N, FalseVal, TrueVal,
2054                                         CCVal, CCR, InFlag);
2055       if (!Res)
2056         Res = SelectT2CMOVShiftOp(N, TrueVal, FalseVal,
2057                                ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
2058       if (Res)
2059         return Res;
2060     } else {
2061       SDNode *Res = SelectARMCMOVShiftOp(N, FalseVal, TrueVal,
2062                                          CCVal, CCR, InFlag);
2063       if (!Res)
2064         Res = SelectARMCMOVShiftOp(N, TrueVal, FalseVal,
2065                                ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
2066       if (Res)
2067         return Res;
2068     }
2069
2070     // Pattern: (ARMcmov:i32 GPR:i32:$false,
2071     //             (imm:i32)<<P:Pred_so_imm>>:$true,
2072     //             (imm:i32):$cc)
2073     // Emits: (MOVCCi:i32 GPR:i32:$false,
2074     //           (so_imm:i32 (imm:i32):$true), (imm:i32):$cc)
2075     // Pattern complexity = 10  cost = 1  size = 0
2076     if (Subtarget->isThumb()) {
2077       SDNode *Res = SelectT2CMOVImmOp(N, FalseVal, TrueVal,
2078                                         CCVal, CCR, InFlag);
2079       if (!Res)
2080         Res = SelectT2CMOVImmOp(N, TrueVal, FalseVal,
2081                                ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
2082       if (Res)
2083         return Res;
2084     } else {
2085       SDNode *Res = SelectARMCMOVImmOp(N, FalseVal, TrueVal,
2086                                          CCVal, CCR, InFlag);
2087       if (!Res)
2088         Res = SelectARMCMOVImmOp(N, TrueVal, FalseVal,
2089                                ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
2090       if (Res)
2091         return Res;
2092     }
2093   }
2094
2095   // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
2096   // Emits: (MOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
2097   // Pattern complexity = 6  cost = 1  size = 0
2098   //
2099   // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
2100   // Emits: (tMOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
2101   // Pattern complexity = 6  cost = 11  size = 0
2102   //
2103   // Also FCPYScc and FCPYDcc.
2104   SDValue Tmp2 = CurDAG->getTargetConstant(CCVal, MVT::i32);
2105   SDValue Ops[] = { FalseVal, TrueVal, Tmp2, CCR, InFlag };
2106   unsigned Opc = 0;
2107   switch (VT.getSimpleVT().SimpleTy) {
2108   default: assert(false && "Illegal conditional move type!");
2109     break;
2110   case MVT::i32:
2111     Opc = Subtarget->isThumb()
2112       ? (Subtarget->hasThumb2() ? ARM::t2MOVCCr : ARM::tMOVCCr_pseudo)
2113       : ARM::MOVCCr;
2114     break;
2115   case MVT::f32:
2116     Opc = ARM::VMOVScc;
2117     break;
2118   case MVT::f64:
2119     Opc = ARM::VMOVDcc;
2120     break;
2121   }
2122   return CurDAG->SelectNodeTo(N, Opc, VT, Ops, 5);
2123 }
2124
2125 SDNode *ARMDAGToDAGISel::SelectConcatVector(SDNode *N) {
2126   // The only time a CONCAT_VECTORS operation can have legal types is when
2127   // two 64-bit vectors are concatenated to a 128-bit vector.
2128   EVT VT = N->getValueType(0);
2129   if (!VT.is128BitVector() || N->getNumOperands() != 2)
2130     llvm_unreachable("unexpected CONCAT_VECTORS");
2131   return PairDRegs(VT, N->getOperand(0), N->getOperand(1));
2132 }
2133
2134 SDNode *ARMDAGToDAGISel::Select(SDNode *N) {
2135   DebugLoc dl = N->getDebugLoc();
2136
2137   if (N->isMachineOpcode())
2138     return NULL;   // Already selected.
2139
2140   switch (N->getOpcode()) {
2141   default: break;
2142   case ISD::Constant: {
2143     unsigned Val = cast<ConstantSDNode>(N)->getZExtValue();
2144     bool UseCP = true;
2145     if (Subtarget->hasThumb2())
2146       // Thumb2-aware targets have the MOVT instruction, so all immediates can
2147       // be done with MOV + MOVT, at worst.
2148       UseCP = 0;
2149     else {
2150       if (Subtarget->isThumb()) {
2151         UseCP = (Val > 255 &&                          // MOV
2152                  ~Val > 255 &&                         // MOV + MVN
2153                  !ARM_AM::isThumbImmShiftedVal(Val));  // MOV + LSL
2154       } else
2155         UseCP = (ARM_AM::getSOImmVal(Val) == -1 &&     // MOV
2156                  ARM_AM::getSOImmVal(~Val) == -1 &&    // MVN
2157                  !ARM_AM::isSOImmTwoPartVal(Val));     // two instrs.
2158     }
2159
2160     if (UseCP) {
2161       SDValue CPIdx =
2162         CurDAG->getTargetConstantPool(ConstantInt::get(
2163                                   Type::getInt32Ty(*CurDAG->getContext()), Val),
2164                                       TLI.getPointerTy());
2165
2166       SDNode *ResNode;
2167       if (Subtarget->isThumb1Only()) {
2168         SDValue Pred = getAL(CurDAG);
2169         SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
2170         SDValue Ops[] = { CPIdx, Pred, PredReg, CurDAG->getEntryNode() };
2171         ResNode = CurDAG->getMachineNode(ARM::tLDRpci, dl, MVT::i32, MVT::Other,
2172                                          Ops, 4);
2173       } else {
2174         SDValue Ops[] = {
2175           CPIdx,
2176           CurDAG->getTargetConstant(0, MVT::i32),
2177           getAL(CurDAG),
2178           CurDAG->getRegister(0, MVT::i32),
2179           CurDAG->getEntryNode()
2180         };
2181         ResNode=CurDAG->getMachineNode(ARM::LDRcp, dl, MVT::i32, MVT::Other,
2182                                        Ops, 5);
2183       }
2184       ReplaceUses(SDValue(N, 0), SDValue(ResNode, 0));
2185       return NULL;
2186     }
2187
2188     // Other cases are autogenerated.
2189     break;
2190   }
2191   case ISD::FrameIndex: {
2192     // Selects to ADDri FI, 0 which in turn will become ADDri SP, imm.
2193     int FI = cast<FrameIndexSDNode>(N)->getIndex();
2194     SDValue TFI = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
2195     if (Subtarget->isThumb1Only()) {
2196       return CurDAG->SelectNodeTo(N, ARM::tADDrSPi, MVT::i32, TFI,
2197                                   CurDAG->getTargetConstant(0, MVT::i32));
2198     } else {
2199       unsigned Opc = ((Subtarget->isThumb() && Subtarget->hasThumb2()) ?
2200                       ARM::t2ADDri : ARM::ADDri);
2201       SDValue Ops[] = { TFI, CurDAG->getTargetConstant(0, MVT::i32),
2202                         getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
2203                         CurDAG->getRegister(0, MVT::i32) };
2204       return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
2205     }
2206   }
2207   case ISD::SRL:
2208     if (SDNode *I = SelectV6T2BitfieldExtractOp(N, false))
2209       return I;
2210     break;
2211   case ISD::SRA:
2212     if (SDNode *I = SelectV6T2BitfieldExtractOp(N, true))
2213       return I;
2214     break;
2215   case ISD::MUL:
2216     if (Subtarget->isThumb1Only())
2217       break;
2218     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
2219       unsigned RHSV = C->getZExtValue();
2220       if (!RHSV) break;
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::t2ADDrs, MVT::i32, Ops, 6);
2232         } else {
2233           SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
2234           return CurDAG->SelectNodeTo(N, ARM::ADDrs, MVT::i32, Ops, 7);
2235         }
2236       }
2237       if (isPowerOf2_32(RHSV+1)) {  // 2^n-1?
2238         unsigned ShImm = Log2_32(RHSV+1);
2239         if (ShImm >= 32)
2240           break;
2241         SDValue V = N->getOperand(0);
2242         ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
2243         SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, MVT::i32);
2244         SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2245         if (Subtarget->isThumb()) {
2246           SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
2247           return CurDAG->SelectNodeTo(N, ARM::t2RSBrs, MVT::i32, Ops, 6);
2248         } else {
2249           SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
2250           return CurDAG->SelectNodeTo(N, ARM::RSBrs, MVT::i32, Ops, 7);
2251         }
2252       }
2253     }
2254     break;
2255   case ISD::AND: {
2256     // Check for unsigned bitfield extract
2257     if (SDNode *I = SelectV6T2BitfieldExtractOp(N, false))
2258       return I;
2259
2260     // (and (or x, c2), c1) and top 16-bits of c1 and c2 match, lower 16-bits
2261     // of c1 are 0xffff, and lower 16-bit of c2 are 0. That is, the top 16-bits
2262     // are entirely contributed by c2 and lower 16-bits are entirely contributed
2263     // by x. That's equal to (or (and x, 0xffff), (and c1, 0xffff0000)).
2264     // Select it to: "movt x, ((c1 & 0xffff) >> 16)
2265     EVT VT = N->getValueType(0);
2266     if (VT != MVT::i32)
2267       break;
2268     unsigned Opc = (Subtarget->isThumb() && Subtarget->hasThumb2())
2269       ? ARM::t2MOVTi16
2270       : (Subtarget->hasV6T2Ops() ? ARM::MOVTi16 : 0);
2271     if (!Opc)
2272       break;
2273     SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
2274     ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2275     if (!N1C)
2276       break;
2277     if (N0.getOpcode() == ISD::OR && N0.getNode()->hasOneUse()) {
2278       SDValue N2 = N0.getOperand(1);
2279       ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
2280       if (!N2C)
2281         break;
2282       unsigned N1CVal = N1C->getZExtValue();
2283       unsigned N2CVal = N2C->getZExtValue();
2284       if ((N1CVal & 0xffff0000U) == (N2CVal & 0xffff0000U) &&
2285           (N1CVal & 0xffffU) == 0xffffU &&
2286           (N2CVal & 0xffffU) == 0x0U) {
2287         SDValue Imm16 = CurDAG->getTargetConstant((N2CVal & 0xFFFF0000U) >> 16,
2288                                                   MVT::i32);
2289         SDValue Ops[] = { N0.getOperand(0), Imm16,
2290                           getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
2291         return CurDAG->getMachineNode(Opc, dl, VT, Ops, 4);
2292       }
2293     }
2294     break;
2295   }
2296   case ARMISD::VMOVRRD:
2297     return CurDAG->getMachineNode(ARM::VMOVRRD, dl, MVT::i32, MVT::i32,
2298                                   N->getOperand(0), getAL(CurDAG),
2299                                   CurDAG->getRegister(0, MVT::i32));
2300   case ISD::UMUL_LOHI: {
2301     if (Subtarget->isThumb1Only())
2302       break;
2303     if (Subtarget->isThumb()) {
2304       SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
2305                         getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
2306                         CurDAG->getRegister(0, MVT::i32) };
2307       return CurDAG->getMachineNode(ARM::t2UMULL, 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::UMULL : ARM::UMULLv5,
2314                                     dl, MVT::i32, MVT::i32, Ops, 5);
2315     }
2316   }
2317   case ISD::SMUL_LOHI: {
2318     if (Subtarget->isThumb1Only())
2319       break;
2320     if (Subtarget->isThumb()) {
2321       SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
2322                         getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
2323       return CurDAG->getMachineNode(ARM::t2SMULL, dl, MVT::i32, MVT::i32,Ops,4);
2324     } else {
2325       SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
2326                         getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
2327                         CurDAG->getRegister(0, MVT::i32) };
2328       return CurDAG->getMachineNode(Subtarget->hasV6Ops() ?
2329                                     ARM::SMULL : ARM::SMULLv5,
2330                                     dl, MVT::i32, MVT::i32, Ops, 5);
2331     }
2332   }
2333   case ISD::LOAD: {
2334     SDNode *ResNode = 0;
2335     if (Subtarget->isThumb() && Subtarget->hasThumb2())
2336       ResNode = SelectT2IndexedLoad(N);
2337     else
2338       ResNode = SelectARMIndexedLoad(N);
2339     if (ResNode)
2340       return ResNode;
2341     // Other cases are autogenerated.
2342     break;
2343   }
2344   case ARMISD::BRCOND: {
2345     // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
2346     // Emits: (Bcc:void (bb:Other):$dst, (imm:i32):$cc)
2347     // Pattern complexity = 6  cost = 1  size = 0
2348
2349     // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
2350     // Emits: (tBcc:void (bb:Other):$dst, (imm:i32):$cc)
2351     // Pattern complexity = 6  cost = 1  size = 0
2352
2353     // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
2354     // Emits: (t2Bcc:void (bb:Other):$dst, (imm:i32):$cc)
2355     // Pattern complexity = 6  cost = 1  size = 0
2356
2357     unsigned Opc = Subtarget->isThumb() ?
2358       ((Subtarget->hasThumb2()) ? ARM::t2Bcc : ARM::tBcc) : ARM::Bcc;
2359     SDValue Chain = N->getOperand(0);
2360     SDValue N1 = N->getOperand(1);
2361     SDValue N2 = N->getOperand(2);
2362     SDValue N3 = N->getOperand(3);
2363     SDValue InFlag = N->getOperand(4);
2364     assert(N1.getOpcode() == ISD::BasicBlock);
2365     assert(N2.getOpcode() == ISD::Constant);
2366     assert(N3.getOpcode() == ISD::Register);
2367
2368     SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
2369                                cast<ConstantSDNode>(N2)->getZExtValue()),
2370                                MVT::i32);
2371     SDValue Ops[] = { N1, Tmp2, N3, Chain, InFlag };
2372     SDNode *ResNode = CurDAG->getMachineNode(Opc, dl, MVT::Other,
2373                                              MVT::Glue, Ops, 5);
2374     Chain = SDValue(ResNode, 0);
2375     if (N->getNumValues() == 2) {
2376       InFlag = SDValue(ResNode, 1);
2377       ReplaceUses(SDValue(N, 1), InFlag);
2378     }
2379     ReplaceUses(SDValue(N, 0),
2380                 SDValue(Chain.getNode(), Chain.getResNo()));
2381     return NULL;
2382   }
2383   case ARMISD::CMOV:
2384     return SelectCMOVOp(N);
2385   case ARMISD::CNEG: {
2386     EVT VT = N->getValueType(0);
2387     SDValue N0 = N->getOperand(0);
2388     SDValue N1 = N->getOperand(1);
2389     SDValue N2 = N->getOperand(2);
2390     SDValue N3 = N->getOperand(3);
2391     SDValue InFlag = N->getOperand(4);
2392     assert(N2.getOpcode() == ISD::Constant);
2393     assert(N3.getOpcode() == ISD::Register);
2394
2395     SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
2396                                cast<ConstantSDNode>(N2)->getZExtValue()),
2397                                MVT::i32);
2398     SDValue Ops[] = { N0, N1, Tmp2, N3, InFlag };
2399     unsigned Opc = 0;
2400     switch (VT.getSimpleVT().SimpleTy) {
2401     default: assert(false && "Illegal conditional move type!");
2402       break;
2403     case MVT::f32:
2404       Opc = ARM::VNEGScc;
2405       break;
2406     case MVT::f64:
2407       Opc = ARM::VNEGDcc;
2408       break;
2409     }
2410     return CurDAG->SelectNodeTo(N, Opc, VT, Ops, 5);
2411   }
2412
2413   case ARMISD::VZIP: {
2414     unsigned Opc = 0;
2415     EVT VT = N->getValueType(0);
2416     switch (VT.getSimpleVT().SimpleTy) {
2417     default: return NULL;
2418     case MVT::v8i8:  Opc = ARM::VZIPd8; break;
2419     case MVT::v4i16: Opc = ARM::VZIPd16; break;
2420     case MVT::v2f32:
2421     case MVT::v2i32: Opc = ARM::VZIPd32; break;
2422     case MVT::v16i8: Opc = ARM::VZIPq8; break;
2423     case MVT::v8i16: Opc = ARM::VZIPq16; break;
2424     case MVT::v4f32:
2425     case MVT::v4i32: Opc = ARM::VZIPq32; break;
2426     }
2427     SDValue Pred = getAL(CurDAG);
2428     SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
2429     SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
2430     return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
2431   }
2432   case ARMISD::VUZP: {
2433     unsigned Opc = 0;
2434     EVT VT = N->getValueType(0);
2435     switch (VT.getSimpleVT().SimpleTy) {
2436     default: return NULL;
2437     case MVT::v8i8:  Opc = ARM::VUZPd8; break;
2438     case MVT::v4i16: Opc = ARM::VUZPd16; break;
2439     case MVT::v2f32:
2440     case MVT::v2i32: Opc = ARM::VUZPd32; break;
2441     case MVT::v16i8: Opc = ARM::VUZPq8; break;
2442     case MVT::v8i16: Opc = ARM::VUZPq16; break;
2443     case MVT::v4f32:
2444     case MVT::v4i32: Opc = ARM::VUZPq32; break;
2445     }
2446     SDValue Pred = getAL(CurDAG);
2447     SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
2448     SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
2449     return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
2450   }
2451   case ARMISD::VTRN: {
2452     unsigned Opc = 0;
2453     EVT VT = N->getValueType(0);
2454     switch (VT.getSimpleVT().SimpleTy) {
2455     default: return NULL;
2456     case MVT::v8i8:  Opc = ARM::VTRNd8; break;
2457     case MVT::v4i16: Opc = ARM::VTRNd16; break;
2458     case MVT::v2f32:
2459     case MVT::v2i32: Opc = ARM::VTRNd32; break;
2460     case MVT::v16i8: Opc = ARM::VTRNq8; break;
2461     case MVT::v8i16: Opc = ARM::VTRNq16; break;
2462     case MVT::v4f32:
2463     case MVT::v4i32: Opc = ARM::VTRNq32; break;
2464     }
2465     SDValue Pred = getAL(CurDAG);
2466     SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
2467     SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
2468     return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
2469   }
2470   case ARMISD::BUILD_VECTOR: {
2471     EVT VecVT = N->getValueType(0);
2472     EVT EltVT = VecVT.getVectorElementType();
2473     unsigned NumElts = VecVT.getVectorNumElements();
2474     if (EltVT == MVT::f64) {
2475       assert(NumElts == 2 && "unexpected type for BUILD_VECTOR");
2476       return PairDRegs(VecVT, N->getOperand(0), N->getOperand(1));
2477     }
2478     assert(EltVT == MVT::f32 && "unexpected type for BUILD_VECTOR");
2479     if (NumElts == 2)
2480       return PairSRegs(VecVT, N->getOperand(0), N->getOperand(1));
2481     assert(NumElts == 4 && "unexpected type for BUILD_VECTOR");
2482     return QuadSRegs(VecVT, N->getOperand(0), N->getOperand(1),
2483                      N->getOperand(2), N->getOperand(3));
2484   }
2485
2486   case ARMISD::VLD2DUP: {
2487     unsigned Opcodes[] = { ARM::VLD2DUPd8Pseudo, ARM::VLD2DUPd16Pseudo,
2488                            ARM::VLD2DUPd32Pseudo };
2489     return SelectVLDDup(N, 2, Opcodes);
2490   }
2491
2492   case ARMISD::VLD3DUP: {
2493     unsigned Opcodes[] = { ARM::VLD3DUPd8Pseudo, ARM::VLD3DUPd16Pseudo,
2494                            ARM::VLD3DUPd32Pseudo };
2495     return SelectVLDDup(N, 3, Opcodes);
2496   }
2497
2498   case ARMISD::VLD4DUP: {
2499     unsigned Opcodes[] = { ARM::VLD4DUPd8Pseudo, ARM::VLD4DUPd16Pseudo,
2500                            ARM::VLD4DUPd32Pseudo };
2501     return SelectVLDDup(N, 4, Opcodes);
2502   }
2503
2504   case ISD::INTRINSIC_VOID:
2505   case ISD::INTRINSIC_W_CHAIN: {
2506     unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
2507     switch (IntNo) {
2508     default:
2509       break;
2510
2511     case Intrinsic::arm_neon_vld1: {
2512       unsigned DOpcodes[] = { ARM::VLD1d8, ARM::VLD1d16,
2513                               ARM::VLD1d32, ARM::VLD1d64 };
2514       unsigned QOpcodes[] = { ARM::VLD1q8Pseudo, ARM::VLD1q16Pseudo,
2515                               ARM::VLD1q32Pseudo, ARM::VLD1q64Pseudo };
2516       return SelectVLD(N, 1, DOpcodes, QOpcodes, 0);
2517     }
2518
2519     case Intrinsic::arm_neon_vld2: {
2520       unsigned DOpcodes[] = { ARM::VLD2d8Pseudo, ARM::VLD2d16Pseudo,
2521                               ARM::VLD2d32Pseudo, ARM::VLD1q64Pseudo };
2522       unsigned QOpcodes[] = { ARM::VLD2q8Pseudo, ARM::VLD2q16Pseudo,
2523                               ARM::VLD2q32Pseudo };
2524       return SelectVLD(N, 2, DOpcodes, QOpcodes, 0);
2525     }
2526
2527     case Intrinsic::arm_neon_vld3: {
2528       unsigned DOpcodes[] = { ARM::VLD3d8Pseudo, ARM::VLD3d16Pseudo,
2529                               ARM::VLD3d32Pseudo, ARM::VLD1d64TPseudo };
2530       unsigned QOpcodes0[] = { ARM::VLD3q8Pseudo_UPD,
2531                                ARM::VLD3q16Pseudo_UPD,
2532                                ARM::VLD3q32Pseudo_UPD };
2533       unsigned QOpcodes1[] = { ARM::VLD3q8oddPseudo_UPD,
2534                                ARM::VLD3q16oddPseudo_UPD,
2535                                ARM::VLD3q32oddPseudo_UPD };
2536       return SelectVLD(N, 3, DOpcodes, QOpcodes0, QOpcodes1);
2537     }
2538
2539     case Intrinsic::arm_neon_vld4: {
2540       unsigned DOpcodes[] = { ARM::VLD4d8Pseudo, ARM::VLD4d16Pseudo,
2541                               ARM::VLD4d32Pseudo, ARM::VLD1d64QPseudo };
2542       unsigned QOpcodes0[] = { ARM::VLD4q8Pseudo_UPD,
2543                                ARM::VLD4q16Pseudo_UPD,
2544                                ARM::VLD4q32Pseudo_UPD };
2545       unsigned QOpcodes1[] = { ARM::VLD4q8oddPseudo_UPD,
2546                                ARM::VLD4q16oddPseudo_UPD,
2547                                ARM::VLD4q32oddPseudo_UPD };
2548       return SelectVLD(N, 4, DOpcodes, QOpcodes0, QOpcodes1);
2549     }
2550
2551     case Intrinsic::arm_neon_vld2lane: {
2552       unsigned DOpcodes[] = { ARM::VLD2LNd8Pseudo, ARM::VLD2LNd16Pseudo,
2553                               ARM::VLD2LNd32Pseudo };
2554       unsigned QOpcodes[] = { ARM::VLD2LNq16Pseudo, ARM::VLD2LNq32Pseudo };
2555       return SelectVLDSTLane(N, true, 2, DOpcodes, QOpcodes);
2556     }
2557
2558     case Intrinsic::arm_neon_vld3lane: {
2559       unsigned DOpcodes[] = { ARM::VLD3LNd8Pseudo, ARM::VLD3LNd16Pseudo,
2560                               ARM::VLD3LNd32Pseudo };
2561       unsigned QOpcodes[] = { ARM::VLD3LNq16Pseudo, ARM::VLD3LNq32Pseudo };
2562       return SelectVLDSTLane(N, true, 3, DOpcodes, QOpcodes);
2563     }
2564
2565     case Intrinsic::arm_neon_vld4lane: {
2566       unsigned DOpcodes[] = { ARM::VLD4LNd8Pseudo, ARM::VLD4LNd16Pseudo,
2567                               ARM::VLD4LNd32Pseudo };
2568       unsigned QOpcodes[] = { ARM::VLD4LNq16Pseudo, ARM::VLD4LNq32Pseudo };
2569       return SelectVLDSTLane(N, true, 4, DOpcodes, QOpcodes);
2570     }
2571
2572     case Intrinsic::arm_neon_vst1: {
2573       unsigned DOpcodes[] = { ARM::VST1d8, ARM::VST1d16,
2574                               ARM::VST1d32, ARM::VST1d64 };
2575       unsigned QOpcodes[] = { ARM::VST1q8Pseudo, ARM::VST1q16Pseudo,
2576                               ARM::VST1q32Pseudo, ARM::VST1q64Pseudo };
2577       return SelectVST(N, 1, DOpcodes, QOpcodes, 0);
2578     }
2579
2580     case Intrinsic::arm_neon_vst2: {
2581       unsigned DOpcodes[] = { ARM::VST2d8Pseudo, ARM::VST2d16Pseudo,
2582                               ARM::VST2d32Pseudo, ARM::VST1q64Pseudo };
2583       unsigned QOpcodes[] = { ARM::VST2q8Pseudo, ARM::VST2q16Pseudo,
2584                               ARM::VST2q32Pseudo };
2585       return SelectVST(N, 2, DOpcodes, QOpcodes, 0);
2586     }
2587
2588     case Intrinsic::arm_neon_vst3: {
2589       unsigned DOpcodes[] = { ARM::VST3d8Pseudo, ARM::VST3d16Pseudo,
2590                               ARM::VST3d32Pseudo, ARM::VST1d64TPseudo };
2591       unsigned QOpcodes0[] = { ARM::VST3q8Pseudo_UPD,
2592                                ARM::VST3q16Pseudo_UPD,
2593                                ARM::VST3q32Pseudo_UPD };
2594       unsigned QOpcodes1[] = { ARM::VST3q8oddPseudo_UPD,
2595                                ARM::VST3q16oddPseudo_UPD,
2596                                ARM::VST3q32oddPseudo_UPD };
2597       return SelectVST(N, 3, DOpcodes, QOpcodes0, QOpcodes1);
2598     }
2599
2600     case Intrinsic::arm_neon_vst4: {
2601       unsigned DOpcodes[] = { ARM::VST4d8Pseudo, ARM::VST4d16Pseudo,
2602                               ARM::VST4d32Pseudo, ARM::VST1d64QPseudo };
2603       unsigned QOpcodes0[] = { ARM::VST4q8Pseudo_UPD,
2604                                ARM::VST4q16Pseudo_UPD,
2605                                ARM::VST4q32Pseudo_UPD };
2606       unsigned QOpcodes1[] = { ARM::VST4q8oddPseudo_UPD,
2607                                ARM::VST4q16oddPseudo_UPD,
2608                                ARM::VST4q32oddPseudo_UPD };
2609       return SelectVST(N, 4, DOpcodes, QOpcodes0, QOpcodes1);
2610     }
2611
2612     case Intrinsic::arm_neon_vst2lane: {
2613       unsigned DOpcodes[] = { ARM::VST2LNd8Pseudo, ARM::VST2LNd16Pseudo,
2614                               ARM::VST2LNd32Pseudo };
2615       unsigned QOpcodes[] = { ARM::VST2LNq16Pseudo, ARM::VST2LNq32Pseudo };
2616       return SelectVLDSTLane(N, false, 2, DOpcodes, QOpcodes);
2617     }
2618
2619     case Intrinsic::arm_neon_vst3lane: {
2620       unsigned DOpcodes[] = { ARM::VST3LNd8Pseudo, ARM::VST3LNd16Pseudo,
2621                               ARM::VST3LNd32Pseudo };
2622       unsigned QOpcodes[] = { ARM::VST3LNq16Pseudo, ARM::VST3LNq32Pseudo };
2623       return SelectVLDSTLane(N, false, 3, DOpcodes, QOpcodes);
2624     }
2625
2626     case Intrinsic::arm_neon_vst4lane: {
2627       unsigned DOpcodes[] = { ARM::VST4LNd8Pseudo, ARM::VST4LNd16Pseudo,
2628                               ARM::VST4LNd32Pseudo };
2629       unsigned QOpcodes[] = { ARM::VST4LNq16Pseudo, ARM::VST4LNq32Pseudo };
2630       return SelectVLDSTLane(N, false, 4, DOpcodes, QOpcodes);
2631     }
2632     }
2633     break;
2634   }
2635
2636   case ISD::INTRINSIC_WO_CHAIN: {
2637     unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
2638     switch (IntNo) {
2639     default:
2640       break;
2641
2642     case Intrinsic::arm_neon_vtbl2:
2643       return SelectVTBL(N, false, 2, ARM::VTBL2Pseudo);
2644     case Intrinsic::arm_neon_vtbl3:
2645       return SelectVTBL(N, false, 3, ARM::VTBL3Pseudo);
2646     case Intrinsic::arm_neon_vtbl4:
2647       return SelectVTBL(N, false, 4, ARM::VTBL4Pseudo);
2648
2649     case Intrinsic::arm_neon_vtbx2:
2650       return SelectVTBL(N, true, 2, ARM::VTBX2Pseudo);
2651     case Intrinsic::arm_neon_vtbx3:
2652       return SelectVTBL(N, true, 3, ARM::VTBX3Pseudo);
2653     case Intrinsic::arm_neon_vtbx4:
2654       return SelectVTBL(N, true, 4, ARM::VTBX4Pseudo);
2655     }
2656     break;
2657   }
2658
2659   case ISD::CONCAT_VECTORS:
2660     return SelectConcatVector(N);
2661   }
2662
2663   return SelectCode(N);
2664 }
2665
2666 bool ARMDAGToDAGISel::
2667 SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
2668                              std::vector<SDValue> &OutOps) {
2669   assert(ConstraintCode == 'm' && "unexpected asm memory constraint");
2670   // Require the address to be in a register.  That is safe for all ARM
2671   // variants and it is hard to do anything much smarter without knowing
2672   // how the operand is used.
2673   OutOps.push_back(Op);
2674   return false;
2675 }
2676
2677 /// createARMISelDag - This pass converts a legalized DAG into a
2678 /// ARM-specific DAG, ready for instruction scheduling.
2679 ///
2680 FunctionPass *llvm::createARMISelDag(ARMBaseTargetMachine &TM,
2681                                      CodeGenOpt::Level OptLevel) {
2682   return new ARMDAGToDAGISel(TM, OptLevel);
2683 }