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