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