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