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