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