Add predicate operand to NEON instructions. Fix lots (but not all) 80 col violations...
[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 #include "ARM.h"
15 #include "ARMAddressingModes.h"
16 #include "ARMISelLowering.h"
17 #include "ARMTargetMachine.h"
18 #include "llvm/CallingConv.h"
19 #include "llvm/Constants.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Function.h"
22 #include "llvm/Intrinsics.h"
23 #include "llvm/LLVMContext.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/MachineFunction.h"
26 #include "llvm/CodeGen/MachineInstrBuilder.h"
27 #include "llvm/CodeGen/SelectionDAG.h"
28 #include "llvm/CodeGen/SelectionDAGISel.h"
29 #include "llvm/Target/TargetLowering.h"
30 #include "llvm/Target/TargetOptions.h"
31 #include "llvm/Support/Compiler.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/raw_ostream.h"
35
36 using namespace llvm;
37
38 //===--------------------------------------------------------------------===//
39 /// ARMDAGToDAGISel - ARM specific code to select ARM machine
40 /// instructions for SelectionDAG operations.
41 ///
42 namespace {
43 class ARMDAGToDAGISel : public SelectionDAGISel {
44   ARMBaseTargetMachine &TM;
45
46   /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
47   /// make the right decision when generating code for different targets.
48   const ARMSubtarget *Subtarget;
49
50 public:
51   explicit ARMDAGToDAGISel(ARMBaseTargetMachine &tm,
52                            CodeGenOpt::Level OptLevel)
53     : SelectionDAGISel(tm, OptLevel), TM(tm),
54     Subtarget(&TM.getSubtarget<ARMSubtarget>()) {
55   }
56
57   virtual const char *getPassName() const {
58     return "ARM Instruction Selection";
59   }
60
61   /// getI32Imm - Return a target constant of type i32 with the specified
62   /// value.
63   inline SDValue getI32Imm(unsigned Imm) {
64     return CurDAG->getTargetConstant(Imm, MVT::i32);
65   }
66
67   SDNode *Select(SDValue Op);
68   virtual void InstructionSelect();
69   bool SelectShifterOperandReg(SDValue Op, SDValue N, SDValue &A,
70                                SDValue &B, SDValue &C);
71   bool SelectAddrMode2(SDValue Op, SDValue N, SDValue &Base,
72                        SDValue &Offset, SDValue &Opc);
73   bool SelectAddrMode2Offset(SDValue Op, SDValue N,
74                              SDValue &Offset, SDValue &Opc);
75   bool SelectAddrMode3(SDValue Op, SDValue N, SDValue &Base,
76                        SDValue &Offset, SDValue &Opc);
77   bool SelectAddrMode3Offset(SDValue Op, SDValue N,
78                              SDValue &Offset, SDValue &Opc);
79   bool SelectAddrMode4(SDValue Op, SDValue N, SDValue &Addr,
80                        SDValue &Mode);
81   bool SelectAddrMode5(SDValue Op, SDValue N, SDValue &Base,
82                        SDValue &Offset);
83   bool SelectAddrMode6(SDValue Op, SDValue N, SDValue &Addr, SDValue &Update,
84                        SDValue &Opc, SDValue &Align);
85
86   bool SelectAddrModePC(SDValue Op, SDValue N, SDValue &Offset,
87                         SDValue &Label);
88
89   bool SelectThumbAddrModeRR(SDValue Op, SDValue N, SDValue &Base,
90                              SDValue &Offset);
91   bool SelectThumbAddrModeRI5(SDValue Op, SDValue N, unsigned Scale,
92                               SDValue &Base, SDValue &OffImm,
93                               SDValue &Offset);
94   bool SelectThumbAddrModeS1(SDValue Op, SDValue N, SDValue &Base,
95                              SDValue &OffImm, SDValue &Offset);
96   bool SelectThumbAddrModeS2(SDValue Op, SDValue N, SDValue &Base,
97                              SDValue &OffImm, SDValue &Offset);
98   bool SelectThumbAddrModeS4(SDValue Op, SDValue N, SDValue &Base,
99                              SDValue &OffImm, SDValue &Offset);
100   bool SelectThumbAddrModeSP(SDValue Op, SDValue N, SDValue &Base,
101                              SDValue &OffImm);
102
103   bool SelectT2ShifterOperandReg(SDValue Op, SDValue N,
104                                  SDValue &BaseReg, SDValue &Opc);
105   bool SelectT2AddrModeImm12(SDValue Op, SDValue N, SDValue &Base,
106                              SDValue &OffImm);
107   bool SelectT2AddrModeImm8(SDValue Op, SDValue N, SDValue &Base,
108                             SDValue &OffImm);
109   bool SelectT2AddrModeImm8Offset(SDValue Op, SDValue N,
110                                  SDValue &OffImm);
111   bool SelectT2AddrModeImm8s4(SDValue Op, SDValue N, SDValue &Base,
112                               SDValue &OffImm);
113   bool SelectT2AddrModeSoReg(SDValue Op, SDValue N, SDValue &Base,
114                              SDValue &OffReg, SDValue &ShImm);
115
116   // Include the pieces autogenerated from the target description.
117 #include "ARMGenDAGISel.inc"
118
119 private:
120   /// SelectARMIndexedLoad - Indexed (pre/post inc/dec) load matching code for
121   /// ARM.
122   SDNode *SelectARMIndexedLoad(SDValue Op);
123   SDNode *SelectT2IndexedLoad(SDValue Op);
124
125   /// SelectDYN_ALLOC - Select dynamic alloc for Thumb.
126   SDNode *SelectDYN_ALLOC(SDValue Op);
127
128   /// SelectVLD - Select NEON load intrinsics.  NumVecs should
129   /// be 2, 3 or 4.  The opcode arrays specify the instructions used for
130   /// loads of D registers and even subregs and odd subregs of Q registers.
131   /// For NumVecs == 2, QOpcodes1 is not used.
132   SDNode *SelectVLD(SDValue Op, unsigned NumVecs, unsigned *DOpcodes,
133                     unsigned *QOpcodes0, unsigned *QOpcodes1);
134
135   /// SelectVST - Select NEON store intrinsics.  NumVecs should
136   /// be 2, 3 or 4.  The opcode arrays specify the instructions used for
137   /// stores of D registers and even subregs and odd subregs of Q registers.
138   /// For NumVecs == 2, QOpcodes1 is not used.
139   SDNode *SelectVST(SDValue Op, unsigned NumVecs, unsigned *DOpcodes,
140                     unsigned *QOpcodes0, unsigned *QOpcodes1);
141
142   /// SelectVLDSTLane - Select NEON load/store lane intrinsics.  NumVecs should
143   /// be 2, 3 or 4.  The opcode arrays specify the instructions used for
144   /// load/store of D registers and even subregs and odd subregs of Q registers.
145   SDNode *SelectVLDSTLane(SDValue Op, bool IsLoad, unsigned NumVecs,
146                           unsigned *DOpcodes, unsigned *QOpcodes0,
147                           unsigned *QOpcodes1);
148
149   /// SelectV6T2BitfieldExtractOp - Select SBFX/UBFX instructions for ARM.
150   SDNode *SelectV6T2BitfieldExtractOp(SDValue Op, unsigned Opc);
151
152   /// SelectCMOVOp - Select CMOV instructions for ARM.
153   SDNode *SelectCMOVOp(SDValue Op);
154   SDNode *SelectT2CMOVShiftOp(SDValue Op, SDValue FalseVal, SDValue TrueVal,
155                               ARMCC::CondCodes CCVal, SDValue CCR,
156                               SDValue InFlag);
157   SDNode *SelectARMCMOVShiftOp(SDValue Op, SDValue FalseVal, SDValue TrueVal,
158                                ARMCC::CondCodes CCVal, SDValue CCR,
159                                SDValue InFlag);
160   SDNode *SelectT2CMOVSoImmOp(SDValue Op, SDValue FalseVal, SDValue TrueVal,
161                               ARMCC::CondCodes CCVal, SDValue CCR,
162                               SDValue InFlag);
163   SDNode *SelectARMCMOVSoImmOp(SDValue Op, SDValue FalseVal, SDValue TrueVal,
164                                ARMCC::CondCodes CCVal, SDValue CCR,
165                                SDValue InFlag);
166
167   /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
168   /// inline asm expressions.
169   virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
170                                             char ConstraintCode,
171                                             std::vector<SDValue> &OutOps);
172
173   /// PairDRegs - Insert a pair of double registers into an implicit def to
174   /// form a quad register.
175   SDNode *PairDRegs(EVT VT, SDValue V0, SDValue V1);
176 };
177 }
178
179 /// isInt32Immediate - This method tests to see if the node is a 32-bit constant
180 /// operand. If so Imm will receive the 32-bit value.
181 static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
182   if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
183     Imm = cast<ConstantSDNode>(N)->getZExtValue();
184     return true;
185   }
186   return false;
187 }
188
189 // isInt32Immediate - This method tests to see if a constant operand.
190 // If so Imm will receive the 32 bit value.
191 static bool isInt32Immediate(SDValue N, unsigned &Imm) {
192   return isInt32Immediate(N.getNode(), Imm);
193 }
194
195 // isOpcWithIntImmediate - This method tests to see if the node is a specific
196 // opcode and that it has a immediate integer right operand.
197 // If so Imm will receive the 32 bit value.
198 static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
199   return N->getOpcode() == Opc &&
200          isInt32Immediate(N->getOperand(1).getNode(), Imm);
201 }
202
203
204 void ARMDAGToDAGISel::InstructionSelect() {
205   SelectRoot(*CurDAG);
206   CurDAG->RemoveDeadNodes();
207 }
208
209 bool ARMDAGToDAGISel::SelectShifterOperandReg(SDValue Op,
210                                               SDValue N,
211                                               SDValue &BaseReg,
212                                               SDValue &ShReg,
213                                               SDValue &Opc) {
214   ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
215
216   // Don't match base register only case. That is matched to a separate
217   // lower complexity pattern with explicit register operand.
218   if (ShOpcVal == ARM_AM::no_shift) return false;
219
220   BaseReg = N.getOperand(0);
221   unsigned ShImmVal = 0;
222   if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
223     ShReg = CurDAG->getRegister(0, MVT::i32);
224     ShImmVal = RHS->getZExtValue() & 31;
225   } else {
226     ShReg = N.getOperand(1);
227   }
228   Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
229                                   MVT::i32);
230   return true;
231 }
232
233 bool ARMDAGToDAGISel::SelectAddrMode2(SDValue Op, SDValue N,
234                                       SDValue &Base, SDValue &Offset,
235                                       SDValue &Opc) {
236   if (N.getOpcode() == ISD::MUL) {
237     if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
238       // X * [3,5,9] -> X + X * [2,4,8] etc.
239       int RHSC = (int)RHS->getZExtValue();
240       if (RHSC & 1) {
241         RHSC = RHSC & ~1;
242         ARM_AM::AddrOpc AddSub = ARM_AM::add;
243         if (RHSC < 0) {
244           AddSub = ARM_AM::sub;
245           RHSC = - RHSC;
246         }
247         if (isPowerOf2_32(RHSC)) {
248           unsigned ShAmt = Log2_32(RHSC);
249           Base = Offset = N.getOperand(0);
250           Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt,
251                                                             ARM_AM::lsl),
252                                           MVT::i32);
253           return true;
254         }
255       }
256     }
257   }
258
259   if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB) {
260     Base = N;
261     if (N.getOpcode() == ISD::FrameIndex) {
262       int FI = cast<FrameIndexSDNode>(N)->getIndex();
263       Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
264     } else if (N.getOpcode() == ARMISD::Wrapper) {
265       Base = N.getOperand(0);
266     }
267     Offset = CurDAG->getRegister(0, MVT::i32);
268     Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::add, 0,
269                                                       ARM_AM::no_shift),
270                                     MVT::i32);
271     return true;
272   }
273
274   // Match simple R +/- imm12 operands.
275   if (N.getOpcode() == ISD::ADD)
276     if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
277       int RHSC = (int)RHS->getZExtValue();
278       if ((RHSC >= 0 && RHSC < 0x1000) ||
279           (RHSC < 0 && RHSC > -0x1000)) { // 12 bits.
280         Base = N.getOperand(0);
281         if (Base.getOpcode() == ISD::FrameIndex) {
282           int FI = cast<FrameIndexSDNode>(Base)->getIndex();
283           Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
284         }
285         Offset = CurDAG->getRegister(0, MVT::i32);
286
287         ARM_AM::AddrOpc AddSub = ARM_AM::add;
288         if (RHSC < 0) {
289           AddSub = ARM_AM::sub;
290           RHSC = - RHSC;
291         }
292         Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, RHSC,
293                                                           ARM_AM::no_shift),
294                                         MVT::i32);
295         return true;
296       }
297     }
298
299   // Otherwise this is R +/- [possibly shifted] R.
300   ARM_AM::AddrOpc AddSub = N.getOpcode() == ISD::ADD ? ARM_AM::add:ARM_AM::sub;
301   ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(1));
302   unsigned ShAmt = 0;
303
304   Base   = N.getOperand(0);
305   Offset = N.getOperand(1);
306
307   if (ShOpcVal != ARM_AM::no_shift) {
308     // Check to see if the RHS of the shift is a constant, if not, we can't fold
309     // it.
310     if (ConstantSDNode *Sh =
311            dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) {
312       ShAmt = Sh->getZExtValue();
313       Offset = N.getOperand(1).getOperand(0);
314     } else {
315       ShOpcVal = ARM_AM::no_shift;
316     }
317   }
318
319   // Try matching (R shl C) + (R).
320   if (N.getOpcode() == ISD::ADD && ShOpcVal == ARM_AM::no_shift) {
321     ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0));
322     if (ShOpcVal != ARM_AM::no_shift) {
323       // Check to see if the RHS of the shift is a constant, if not, we can't
324       // fold it.
325       if (ConstantSDNode *Sh =
326           dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
327         ShAmt = Sh->getZExtValue();
328         Offset = N.getOperand(0).getOperand(0);
329         Base = N.getOperand(1);
330       } else {
331         ShOpcVal = ARM_AM::no_shift;
332       }
333     }
334   }
335
336   Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
337                                   MVT::i32);
338   return true;
339 }
340
341 bool ARMDAGToDAGISel::SelectAddrMode2Offset(SDValue Op, SDValue N,
342                                             SDValue &Offset, SDValue &Opc) {
343   unsigned Opcode = Op.getOpcode();
344   ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
345     ? cast<LoadSDNode>(Op)->getAddressingMode()
346     : cast<StoreSDNode>(Op)->getAddressingMode();
347   ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
348     ? ARM_AM::add : ARM_AM::sub;
349   if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
350     int Val = (int)C->getZExtValue();
351     if (Val >= 0 && Val < 0x1000) { // 12 bits.
352       Offset = CurDAG->getRegister(0, MVT::i32);
353       Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, Val,
354                                                         ARM_AM::no_shift),
355                                       MVT::i32);
356       return true;
357     }
358   }
359
360   Offset = N;
361   ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
362   unsigned ShAmt = 0;
363   if (ShOpcVal != ARM_AM::no_shift) {
364     // Check to see if the RHS of the shift is a constant, if not, we can't fold
365     // it.
366     if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
367       ShAmt = Sh->getZExtValue();
368       Offset = N.getOperand(0);
369     } else {
370       ShOpcVal = ARM_AM::no_shift;
371     }
372   }
373
374   Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
375                                   MVT::i32);
376   return true;
377 }
378
379
380 bool ARMDAGToDAGISel::SelectAddrMode3(SDValue Op, SDValue N,
381                                       SDValue &Base, SDValue &Offset,
382                                       SDValue &Opc) {
383   if (N.getOpcode() == ISD::SUB) {
384     // X - C  is canonicalize to X + -C, no need to handle it here.
385     Base = N.getOperand(0);
386     Offset = N.getOperand(1);
387     Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::sub, 0),MVT::i32);
388     return true;
389   }
390
391   if (N.getOpcode() != ISD::ADD) {
392     Base = N;
393     if (N.getOpcode() == ISD::FrameIndex) {
394       int FI = cast<FrameIndexSDNode>(N)->getIndex();
395       Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
396     }
397     Offset = CurDAG->getRegister(0, MVT::i32);
398     Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0),MVT::i32);
399     return true;
400   }
401
402   // If the RHS is +/- imm8, fold into addr mode.
403   if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
404     int RHSC = (int)RHS->getZExtValue();
405     if ((RHSC >= 0 && RHSC < 256) ||
406         (RHSC < 0 && RHSC > -256)) { // note -256 itself isn't allowed.
407       Base = N.getOperand(0);
408       if (Base.getOpcode() == ISD::FrameIndex) {
409         int FI = cast<FrameIndexSDNode>(Base)->getIndex();
410         Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
411       }
412       Offset = CurDAG->getRegister(0, MVT::i32);
413
414       ARM_AM::AddrOpc AddSub = ARM_AM::add;
415       if (RHSC < 0) {
416         AddSub = ARM_AM::sub;
417         RHSC = - RHSC;
418       }
419       Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, RHSC),MVT::i32);
420       return true;
421     }
422   }
423
424   Base = N.getOperand(0);
425   Offset = N.getOperand(1);
426   Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0), MVT::i32);
427   return true;
428 }
429
430 bool ARMDAGToDAGISel::SelectAddrMode3Offset(SDValue Op, SDValue N,
431                                             SDValue &Offset, SDValue &Opc) {
432   unsigned Opcode = Op.getOpcode();
433   ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
434     ? cast<LoadSDNode>(Op)->getAddressingMode()
435     : cast<StoreSDNode>(Op)->getAddressingMode();
436   ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
437     ? ARM_AM::add : ARM_AM::sub;
438   if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) {
439     int Val = (int)C->getZExtValue();
440     if (Val >= 0 && Val < 256) {
441       Offset = CurDAG->getRegister(0, MVT::i32);
442       Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, Val), MVT::i32);
443       return true;
444     }
445   }
446
447   Offset = N;
448   Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, 0), MVT::i32);
449   return true;
450 }
451
452 bool ARMDAGToDAGISel::SelectAddrMode4(SDValue Op, SDValue N,
453                                       SDValue &Addr, SDValue &Mode) {
454   Addr = N;
455   Mode = CurDAG->getTargetConstant(0, MVT::i32);
456   return true;
457 }
458
459 bool ARMDAGToDAGISel::SelectAddrMode5(SDValue Op, SDValue N,
460                                       SDValue &Base, SDValue &Offset) {
461   if (N.getOpcode() != ISD::ADD) {
462     Base = N;
463     if (N.getOpcode() == ISD::FrameIndex) {
464       int FI = cast<FrameIndexSDNode>(N)->getIndex();
465       Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
466     } else if (N.getOpcode() == ARMISD::Wrapper) {
467       Base = N.getOperand(0);
468     }
469     Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
470                                        MVT::i32);
471     return true;
472   }
473
474   // If the RHS is +/- imm8, fold into addr mode.
475   if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
476     int RHSC = (int)RHS->getZExtValue();
477     if ((RHSC & 3) == 0) {  // The constant is implicitly multiplied by 4.
478       RHSC >>= 2;
479       if ((RHSC >= 0 && RHSC < 256) ||
480           (RHSC < 0 && RHSC > -256)) { // note -256 itself isn't allowed.
481         Base = N.getOperand(0);
482         if (Base.getOpcode() == ISD::FrameIndex) {
483           int FI = cast<FrameIndexSDNode>(Base)->getIndex();
484           Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
485         }
486
487         ARM_AM::AddrOpc AddSub = ARM_AM::add;
488         if (RHSC < 0) {
489           AddSub = ARM_AM::sub;
490           RHSC = - RHSC;
491         }
492         Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(AddSub, RHSC),
493                                            MVT::i32);
494         return true;
495       }
496     }
497   }
498
499   Base = N;
500   Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
501                                      MVT::i32);
502   return true;
503 }
504
505 bool ARMDAGToDAGISel::SelectAddrMode6(SDValue Op, SDValue N,
506                                       SDValue &Addr, SDValue &Update,
507                                       SDValue &Opc, SDValue &Align) {
508   Addr = N;
509   // Default to no writeback.
510   Update = CurDAG->getRegister(0, MVT::i32);
511   Opc = CurDAG->getTargetConstant(ARM_AM::getAM6Opc(false), MVT::i32);
512   // Default to no alignment.
513   Align = CurDAG->getTargetConstant(0, MVT::i32);
514   return true;
515 }
516
517 bool ARMDAGToDAGISel::SelectAddrModePC(SDValue Op, SDValue N,
518                                        SDValue &Offset, SDValue &Label) {
519   if (N.getOpcode() == ARMISD::PIC_ADD && N.hasOneUse()) {
520     Offset = N.getOperand(0);
521     SDValue N1 = N.getOperand(1);
522     Label  = CurDAG->getTargetConstant(cast<ConstantSDNode>(N1)->getZExtValue(),
523                                        MVT::i32);
524     return true;
525   }
526   return false;
527 }
528
529 bool ARMDAGToDAGISel::SelectThumbAddrModeRR(SDValue Op, SDValue N,
530                                             SDValue &Base, SDValue &Offset){
531   // FIXME dl should come from the parent load or store, not the address
532   DebugLoc dl = Op.getDebugLoc();
533   if (N.getOpcode() != ISD::ADD) {
534     ConstantSDNode *NC = dyn_cast<ConstantSDNode>(N);
535     if (!NC || NC->getZExtValue() != 0)
536       return false;
537
538     Base = Offset = N;
539     return true;
540   }
541
542   Base = N.getOperand(0);
543   Offset = N.getOperand(1);
544   return true;
545 }
546
547 bool
548 ARMDAGToDAGISel::SelectThumbAddrModeRI5(SDValue Op, SDValue N,
549                                         unsigned Scale, SDValue &Base,
550                                         SDValue &OffImm, SDValue &Offset) {
551   if (Scale == 4) {
552     SDValue TmpBase, TmpOffImm;
553     if (SelectThumbAddrModeSP(Op, N, TmpBase, TmpOffImm))
554       return false;  // We want to select tLDRspi / tSTRspi instead.
555     if (N.getOpcode() == ARMISD::Wrapper &&
556         N.getOperand(0).getOpcode() == ISD::TargetConstantPool)
557       return false;  // We want to select tLDRpci instead.
558   }
559
560   if (N.getOpcode() != ISD::ADD) {
561     Base = (N.getOpcode() == ARMISD::Wrapper) ? N.getOperand(0) : N;
562     Offset = CurDAG->getRegister(0, MVT::i32);
563     OffImm = CurDAG->getTargetConstant(0, MVT::i32);
564     return true;
565   }
566
567   // Thumb does not have [sp, r] address mode.
568   RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
569   RegisterSDNode *RHSR = dyn_cast<RegisterSDNode>(N.getOperand(1));
570   if ((LHSR && LHSR->getReg() == ARM::SP) ||
571       (RHSR && RHSR->getReg() == ARM::SP)) {
572     Base = N;
573     Offset = CurDAG->getRegister(0, MVT::i32);
574     OffImm = CurDAG->getTargetConstant(0, MVT::i32);
575     return true;
576   }
577
578   // If the RHS is + imm5 * scale, fold into addr mode.
579   if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
580     int RHSC = (int)RHS->getZExtValue();
581     if ((RHSC & (Scale-1)) == 0) {  // The constant is implicitly multiplied.
582       RHSC /= Scale;
583       if (RHSC >= 0 && RHSC < 32) {
584         Base = N.getOperand(0);
585         Offset = CurDAG->getRegister(0, MVT::i32);
586         OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
587         return true;
588       }
589     }
590   }
591
592   Base = N.getOperand(0);
593   Offset = N.getOperand(1);
594   OffImm = CurDAG->getTargetConstant(0, MVT::i32);
595   return true;
596 }
597
598 bool ARMDAGToDAGISel::SelectThumbAddrModeS1(SDValue Op, SDValue N,
599                                             SDValue &Base, SDValue &OffImm,
600                                             SDValue &Offset) {
601   return SelectThumbAddrModeRI5(Op, N, 1, Base, OffImm, Offset);
602 }
603
604 bool ARMDAGToDAGISel::SelectThumbAddrModeS2(SDValue Op, SDValue N,
605                                             SDValue &Base, SDValue &OffImm,
606                                             SDValue &Offset) {
607   return SelectThumbAddrModeRI5(Op, N, 2, Base, OffImm, Offset);
608 }
609
610 bool ARMDAGToDAGISel::SelectThumbAddrModeS4(SDValue Op, SDValue N,
611                                             SDValue &Base, SDValue &OffImm,
612                                             SDValue &Offset) {
613   return SelectThumbAddrModeRI5(Op, N, 4, Base, OffImm, Offset);
614 }
615
616 bool ARMDAGToDAGISel::SelectThumbAddrModeSP(SDValue Op, SDValue N,
617                                            SDValue &Base, SDValue &OffImm) {
618   if (N.getOpcode() == ISD::FrameIndex) {
619     int FI = cast<FrameIndexSDNode>(N)->getIndex();
620     Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
621     OffImm = CurDAG->getTargetConstant(0, MVT::i32);
622     return true;
623   }
624
625   if (N.getOpcode() != ISD::ADD)
626     return false;
627
628   RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
629   if (N.getOperand(0).getOpcode() == ISD::FrameIndex ||
630       (LHSR && LHSR->getReg() == ARM::SP)) {
631     // If the RHS is + imm8 * scale, fold into addr mode.
632     if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
633       int RHSC = (int)RHS->getZExtValue();
634       if ((RHSC & 3) == 0) {  // The constant is implicitly multiplied.
635         RHSC >>= 2;
636         if (RHSC >= 0 && RHSC < 256) {
637           Base = N.getOperand(0);
638           if (Base.getOpcode() == ISD::FrameIndex) {
639             int FI = cast<FrameIndexSDNode>(Base)->getIndex();
640             Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
641           }
642           OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
643           return true;
644         }
645       }
646     }
647   }
648
649   return false;
650 }
651
652 bool ARMDAGToDAGISel::SelectT2ShifterOperandReg(SDValue Op, SDValue N,
653                                                 SDValue &BaseReg,
654                                                 SDValue &Opc) {
655   ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N);
656
657   // Don't match base register only case. That is matched to a separate
658   // lower complexity pattern with explicit register operand.
659   if (ShOpcVal == ARM_AM::no_shift) return false;
660
661   BaseReg = N.getOperand(0);
662   unsigned ShImmVal = 0;
663   if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
664     ShImmVal = RHS->getZExtValue() & 31;
665     Opc = getI32Imm(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal));
666     return true;
667   }
668
669   return false;
670 }
671
672 bool ARMDAGToDAGISel::SelectT2AddrModeImm12(SDValue Op, SDValue N,
673                                             SDValue &Base, SDValue &OffImm) {
674   // Match simple R + imm12 operands.
675
676   // Base only.
677   if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB) {
678     if (N.getOpcode() == ISD::FrameIndex) {
679       // Match frame index...
680       int FI = cast<FrameIndexSDNode>(N)->getIndex();
681       Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
682       OffImm  = CurDAG->getTargetConstant(0, MVT::i32);
683       return true;
684     } else if (N.getOpcode() == ARMISD::Wrapper) {
685       Base = N.getOperand(0);
686       if (Base.getOpcode() == ISD::TargetConstantPool)
687         return false;  // We want to select t2LDRpci instead.
688     } else
689       Base = N;
690     OffImm  = CurDAG->getTargetConstant(0, MVT::i32);
691     return true;
692   }
693
694   if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
695     if (SelectT2AddrModeImm8(Op, N, Base, OffImm))
696       // Let t2LDRi8 handle (R - imm8).
697       return false;
698
699     int RHSC = (int)RHS->getZExtValue();
700     if (N.getOpcode() == ISD::SUB)
701       RHSC = -RHSC;
702
703     if (RHSC >= 0 && RHSC < 0x1000) { // 12 bits (unsigned)
704       Base   = N.getOperand(0);
705       if (Base.getOpcode() == ISD::FrameIndex) {
706         int FI = cast<FrameIndexSDNode>(Base)->getIndex();
707         Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
708       }
709       OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
710       return true;
711     }
712   }
713
714   // Base only.
715   Base = N;
716   OffImm  = CurDAG->getTargetConstant(0, MVT::i32);
717   return true;
718 }
719
720 bool ARMDAGToDAGISel::SelectT2AddrModeImm8(SDValue Op, SDValue N,
721                                            SDValue &Base, SDValue &OffImm) {
722   // Match simple R - imm8 operands.
723   if (N.getOpcode() == ISD::ADD || N.getOpcode() == ISD::SUB) {
724     if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
725       int RHSC = (int)RHS->getSExtValue();
726       if (N.getOpcode() == ISD::SUB)
727         RHSC = -RHSC;
728
729       if ((RHSC >= -255) && (RHSC < 0)) { // 8 bits (always negative)
730         Base = N.getOperand(0);
731         if (Base.getOpcode() == ISD::FrameIndex) {
732           int FI = cast<FrameIndexSDNode>(Base)->getIndex();
733           Base = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
734         }
735         OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
736         return true;
737       }
738     }
739   }
740
741   return false;
742 }
743
744 bool ARMDAGToDAGISel::SelectT2AddrModeImm8Offset(SDValue Op, SDValue N,
745                                                  SDValue &OffImm){
746   unsigned Opcode = Op.getOpcode();
747   ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
748     ? cast<LoadSDNode>(Op)->getAddressingMode()
749     : cast<StoreSDNode>(Op)->getAddressingMode();
750   if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N)) {
751     int RHSC = (int)RHS->getZExtValue();
752     if (RHSC >= 0 && RHSC < 0x100) { // 8 bits.
753       OffImm = ((AM == ISD::PRE_INC) || (AM == ISD::POST_INC))
754         ? CurDAG->getTargetConstant(RHSC, MVT::i32)
755         : CurDAG->getTargetConstant(-RHSC, MVT::i32);
756       return true;
757     }
758   }
759
760   return false;
761 }
762
763 bool ARMDAGToDAGISel::SelectT2AddrModeImm8s4(SDValue Op, SDValue N,
764                                              SDValue &Base, SDValue &OffImm) {
765   if (N.getOpcode() == ISD::ADD) {
766     if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
767       int RHSC = (int)RHS->getZExtValue();
768       if (((RHSC & 0x3) == 0) &&
769           ((RHSC >= 0 && RHSC < 0x400) || (RHSC < 0 && RHSC > -0x400))) { // 8 bits.
770         Base   = N.getOperand(0);
771         OffImm = CurDAG->getTargetConstant(RHSC, MVT::i32);
772         return true;
773       }
774     }
775   } else if (N.getOpcode() == ISD::SUB) {
776     if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
777       int RHSC = (int)RHS->getZExtValue();
778       if (((RHSC & 0x3) == 0) && (RHSC >= 0 && RHSC < 0x400)) { // 8 bits.
779         Base   = N.getOperand(0);
780         OffImm = CurDAG->getTargetConstant(-RHSC, MVT::i32);
781         return true;
782       }
783     }
784   }
785
786   return false;
787 }
788
789 bool ARMDAGToDAGISel::SelectT2AddrModeSoReg(SDValue Op, SDValue N,
790                                             SDValue &Base,
791                                             SDValue &OffReg, SDValue &ShImm) {
792   // (R - imm8) should be handled by t2LDRi8. The rest are handled by t2LDRi12.
793   if (N.getOpcode() != ISD::ADD)
794     return false;
795
796   // Leave (R + imm12) for t2LDRi12, (R - imm8) for t2LDRi8.
797   if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
798     int RHSC = (int)RHS->getZExtValue();
799     if (RHSC >= 0 && RHSC < 0x1000) // 12 bits (unsigned)
800       return false;
801     else if (RHSC < 0 && RHSC >= -255) // 8 bits
802       return false;
803   }
804
805   // Look for (R + R) or (R + (R << [1,2,3])).
806   unsigned ShAmt = 0;
807   Base   = N.getOperand(0);
808   OffReg = N.getOperand(1);
809
810   // Swap if it is ((R << c) + R).
811   ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(OffReg);
812   if (ShOpcVal != ARM_AM::lsl) {
813     ShOpcVal = ARM_AM::getShiftOpcForNode(Base);
814     if (ShOpcVal == ARM_AM::lsl)
815       std::swap(Base, OffReg);
816   }
817
818   if (ShOpcVal == ARM_AM::lsl) {
819     // Check to see if the RHS of the shift is a constant, if not, we can't fold
820     // it.
821     if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(OffReg.getOperand(1))) {
822       ShAmt = Sh->getZExtValue();
823       if (ShAmt >= 4) {
824         ShAmt = 0;
825         ShOpcVal = ARM_AM::no_shift;
826       } else
827         OffReg = OffReg.getOperand(0);
828     } else {
829       ShOpcVal = ARM_AM::no_shift;
830     }
831   }
832
833   ShImm = CurDAG->getTargetConstant(ShAmt, MVT::i32);
834
835   return true;
836 }
837
838 //===--------------------------------------------------------------------===//
839
840 /// getAL - Returns a ARMCC::AL immediate node.
841 static inline SDValue getAL(SelectionDAG *CurDAG) {
842   return CurDAG->getTargetConstant((uint64_t)ARMCC::AL, MVT::i32);
843 }
844
845 SDNode *ARMDAGToDAGISel::SelectARMIndexedLoad(SDValue Op) {
846   LoadSDNode *LD = cast<LoadSDNode>(Op);
847   ISD::MemIndexedMode AM = LD->getAddressingMode();
848   if (AM == ISD::UNINDEXED)
849     return NULL;
850
851   EVT LoadedVT = LD->getMemoryVT();
852   SDValue Offset, AMOpc;
853   bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
854   unsigned Opcode = 0;
855   bool Match = false;
856   if (LoadedVT == MVT::i32 &&
857       SelectAddrMode2Offset(Op, LD->getOffset(), Offset, AMOpc)) {
858     Opcode = isPre ? ARM::LDR_PRE : ARM::LDR_POST;
859     Match = true;
860   } else if (LoadedVT == MVT::i16 &&
861              SelectAddrMode3Offset(Op, LD->getOffset(), Offset, AMOpc)) {
862     Match = true;
863     Opcode = (LD->getExtensionType() == ISD::SEXTLOAD)
864       ? (isPre ? ARM::LDRSH_PRE : ARM::LDRSH_POST)
865       : (isPre ? ARM::LDRH_PRE : ARM::LDRH_POST);
866   } else if (LoadedVT == MVT::i8 || LoadedVT == MVT::i1) {
867     if (LD->getExtensionType() == ISD::SEXTLOAD) {
868       if (SelectAddrMode3Offset(Op, LD->getOffset(), Offset, AMOpc)) {
869         Match = true;
870         Opcode = isPre ? ARM::LDRSB_PRE : ARM::LDRSB_POST;
871       }
872     } else {
873       if (SelectAddrMode2Offset(Op, LD->getOffset(), Offset, AMOpc)) {
874         Match = true;
875         Opcode = isPre ? ARM::LDRB_PRE : ARM::LDRB_POST;
876       }
877     }
878   }
879
880   if (Match) {
881     SDValue Chain = LD->getChain();
882     SDValue Base = LD->getBasePtr();
883     SDValue Ops[]= { Base, Offset, AMOpc, getAL(CurDAG),
884                      CurDAG->getRegister(0, MVT::i32), Chain };
885     return CurDAG->getMachineNode(Opcode, Op.getDebugLoc(), MVT::i32, MVT::i32,
886                                   MVT::Other, Ops, 6);
887   }
888
889   return NULL;
890 }
891
892 SDNode *ARMDAGToDAGISel::SelectT2IndexedLoad(SDValue Op) {
893   LoadSDNode *LD = cast<LoadSDNode>(Op);
894   ISD::MemIndexedMode AM = LD->getAddressingMode();
895   if (AM == ISD::UNINDEXED)
896     return NULL;
897
898   EVT LoadedVT = LD->getMemoryVT();
899   bool isSExtLd = LD->getExtensionType() == ISD::SEXTLOAD;
900   SDValue Offset;
901   bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
902   unsigned Opcode = 0;
903   bool Match = false;
904   if (SelectT2AddrModeImm8Offset(Op, LD->getOffset(), Offset)) {
905     switch (LoadedVT.getSimpleVT().SimpleTy) {
906     case MVT::i32:
907       Opcode = isPre ? ARM::t2LDR_PRE : ARM::t2LDR_POST;
908       break;
909     case MVT::i16:
910       if (isSExtLd)
911         Opcode = isPre ? ARM::t2LDRSH_PRE : ARM::t2LDRSH_POST;
912       else
913         Opcode = isPre ? ARM::t2LDRH_PRE : ARM::t2LDRH_POST;
914       break;
915     case MVT::i8:
916     case MVT::i1:
917       if (isSExtLd)
918         Opcode = isPre ? ARM::t2LDRSB_PRE : ARM::t2LDRSB_POST;
919       else
920         Opcode = isPre ? ARM::t2LDRB_PRE : ARM::t2LDRB_POST;
921       break;
922     default:
923       return NULL;
924     }
925     Match = true;
926   }
927
928   if (Match) {
929     SDValue Chain = LD->getChain();
930     SDValue Base = LD->getBasePtr();
931     SDValue Ops[]= { Base, Offset, getAL(CurDAG),
932                      CurDAG->getRegister(0, MVT::i32), Chain };
933     return CurDAG->getMachineNode(Opcode, Op.getDebugLoc(), MVT::i32, MVT::i32,
934                                   MVT::Other, Ops, 5);
935   }
936
937   return NULL;
938 }
939
940 SDNode *ARMDAGToDAGISel::SelectDYN_ALLOC(SDValue Op) {
941   SDNode *N = Op.getNode();
942   DebugLoc dl = N->getDebugLoc();
943   EVT VT = Op.getValueType();
944   SDValue Chain = Op.getOperand(0);
945   SDValue Size = Op.getOperand(1);
946   SDValue Align = Op.getOperand(2);
947   SDValue SP = CurDAG->getRegister(ARM::SP, MVT::i32);
948   int32_t AlignVal = cast<ConstantSDNode>(Align)->getSExtValue();
949   if (AlignVal < 0)
950     // We need to align the stack. Use Thumb1 tAND which is the only thumb
951     // instruction that can read and write SP. This matches to a pseudo
952     // instruction that has a chain to ensure the result is written back to
953     // the stack pointer.
954     SP = SDValue(CurDAG->getMachineNode(ARM::tANDsp, dl, VT, SP, Align), 0);
955
956   bool isC = isa<ConstantSDNode>(Size);
957   uint32_t C = isC ? cast<ConstantSDNode>(Size)->getZExtValue() : ~0UL;
958   // Handle the most common case for both Thumb1 and Thumb2:
959   // tSUBspi - immediate is between 0 ... 508 inclusive.
960   if (C <= 508 && ((C & 3) == 0))
961     // FIXME: tSUBspi encode scale 4 implicitly.
962     return CurDAG->SelectNodeTo(N, ARM::tSUBspi_, VT, MVT::Other, SP,
963                                 CurDAG->getTargetConstant(C/4, MVT::i32),
964                                 Chain);
965
966   if (Subtarget->isThumb1Only()) {
967     // Use tADDspr since Thumb1 does not have a sub r, sp, r. ARMISelLowering
968     // should have negated the size operand already. FIXME: We can't insert
969     // new target independent node at this stage so we are forced to negate
970     // it earlier. Is there a better solution?
971     return CurDAG->SelectNodeTo(N, ARM::tADDspr_, VT, MVT::Other, SP, Size,
972                                 Chain);
973   } else if (Subtarget->isThumb2()) {
974     if (isC && Predicate_t2_so_imm(Size.getNode())) {
975       // t2SUBrSPi
976       SDValue Ops[] = { SP, CurDAG->getTargetConstant(C, MVT::i32), Chain };
977       return CurDAG->SelectNodeTo(N, ARM::t2SUBrSPi_, VT, MVT::Other, Ops, 3);
978     } else if (isC && Predicate_imm0_4095(Size.getNode())) {
979       // t2SUBrSPi12
980       SDValue Ops[] = { SP, CurDAG->getTargetConstant(C, MVT::i32), Chain };
981       return CurDAG->SelectNodeTo(N, ARM::t2SUBrSPi12_, VT, MVT::Other, Ops, 3);
982     } else {
983       // t2SUBrSPs
984       SDValue Ops[] = { SP, Size,
985                         getI32Imm(ARM_AM::getSORegOpc(ARM_AM::lsl,0)), Chain };
986       return CurDAG->SelectNodeTo(N, ARM::t2SUBrSPs_, VT, MVT::Other, Ops, 4);
987     }
988   }
989
990   // FIXME: Add ADD / SUB sp instructions for ARM.
991   return 0;
992 }
993
994 /// PairDRegs - Insert a pair of double registers into an implicit def to
995 /// form a quad register.
996 SDNode *ARMDAGToDAGISel::PairDRegs(EVT VT, SDValue V0, SDValue V1) {
997   DebugLoc dl = V0.getNode()->getDebugLoc();
998   SDValue Undef =
999     SDValue(CurDAG->getMachineNode(TargetInstrInfo::IMPLICIT_DEF, dl, VT), 0);
1000   SDValue SubReg0 = CurDAG->getTargetConstant(ARM::DSUBREG_0, MVT::i32);
1001   SDValue SubReg1 = CurDAG->getTargetConstant(ARM::DSUBREG_1, MVT::i32);
1002   SDNode *Pair = CurDAG->getMachineNode(TargetInstrInfo::INSERT_SUBREG, dl,
1003                                         VT, Undef, V0, SubReg0);
1004   return CurDAG->getMachineNode(TargetInstrInfo::INSERT_SUBREG, dl,
1005                                 VT, SDValue(Pair, 0), V1, SubReg1);
1006 }
1007
1008 /// GetNEONSubregVT - Given a type for a 128-bit NEON vector, return the type
1009 /// for a 64-bit subregister of the vector.
1010 static EVT GetNEONSubregVT(EVT VT) {
1011   switch (VT.getSimpleVT().SimpleTy) {
1012   default: llvm_unreachable("unhandled NEON type");
1013   case MVT::v16i8: return MVT::v8i8;
1014   case MVT::v8i16: return MVT::v4i16;
1015   case MVT::v4f32: return MVT::v2f32;
1016   case MVT::v4i32: return MVT::v2i32;
1017   case MVT::v2i64: return MVT::v1i64;
1018   }
1019 }
1020
1021 SDNode *ARMDAGToDAGISel::SelectVLD(SDValue Op, unsigned NumVecs,
1022                                    unsigned *DOpcodes, unsigned *QOpcodes0,
1023                                    unsigned *QOpcodes1) {
1024   assert(NumVecs >=2 && NumVecs <= 4 && "VLD NumVecs out-of-range");
1025   SDNode *N = Op.getNode();
1026   DebugLoc dl = N->getDebugLoc();
1027
1028   SDValue MemAddr, MemUpdate, MemOpc, Align;
1029   if (!SelectAddrMode6(Op, N->getOperand(2), MemAddr, MemUpdate, MemOpc, Align))
1030     return NULL;
1031
1032   SDValue Chain = N->getOperand(0);
1033   EVT VT = N->getValueType(0);
1034   bool is64BitVector = VT.is64BitVector();
1035
1036   unsigned OpcodeIndex;
1037   switch (VT.getSimpleVT().SimpleTy) {
1038   default: llvm_unreachable("unhandled vld type");
1039     // Double-register operations:
1040   case MVT::v8i8:  OpcodeIndex = 0; break;
1041   case MVT::v4i16: OpcodeIndex = 1; break;
1042   case MVT::v2f32:
1043   case MVT::v2i32: OpcodeIndex = 2; break;
1044   case MVT::v1i64: OpcodeIndex = 3; break;
1045     // Quad-register operations:
1046   case MVT::v16i8: OpcodeIndex = 0; break;
1047   case MVT::v8i16: OpcodeIndex = 1; break;
1048   case MVT::v4f32:
1049   case MVT::v4i32: OpcodeIndex = 2; break;
1050   }
1051
1052   SDValue Pred = CurDAG->getTargetConstant(14, MVT::i32);
1053   SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1054   if (is64BitVector) {
1055     unsigned Opc = DOpcodes[OpcodeIndex];
1056     const SDValue Ops[] = { MemAddr, MemUpdate, MemOpc, Align,
1057                             Pred, PredReg, Chain };
1058     std::vector<EVT> ResTys(NumVecs, VT);
1059     ResTys.push_back(MVT::Other);
1060     return CurDAG->getMachineNode(Opc, dl, ResTys, Ops, 7);
1061   }
1062
1063   EVT RegVT = GetNEONSubregVT(VT);
1064   if (NumVecs == 2) {
1065     // Quad registers are directly supported for VLD2,
1066     // loading 2 pairs of D regs.
1067     unsigned Opc = QOpcodes0[OpcodeIndex];
1068     const SDValue Ops[] = { MemAddr, MemUpdate, MemOpc, Align,
1069                             Pred, PredReg, Chain };
1070     std::vector<EVT> ResTys(4, VT);
1071     ResTys.push_back(MVT::Other);
1072     SDNode *VLd = CurDAG->getMachineNode(Opc, dl, ResTys, Ops, 7);
1073     Chain = SDValue(VLd, 4);
1074
1075     // Combine the even and odd subregs to produce the result.
1076     for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
1077       SDNode *Q = PairDRegs(VT, SDValue(VLd, 2*Vec), SDValue(VLd, 2*Vec+1));
1078       ReplaceUses(SDValue(N, Vec), SDValue(Q, 0));
1079     }
1080   } else {
1081     // Otherwise, quad registers are loaded with two separate instructions,
1082     // where one loads the even registers and the other loads the odd registers.
1083
1084     // Enable writeback to the address register.
1085     MemOpc = CurDAG->getTargetConstant(ARM_AM::getAM6Opc(true), MVT::i32);
1086
1087     std::vector<EVT> ResTys(NumVecs, RegVT);
1088     ResTys.push_back(MemAddr.getValueType());
1089     ResTys.push_back(MVT::Other);
1090
1091     // Load the even subregs.
1092     unsigned Opc = QOpcodes0[OpcodeIndex];
1093     const SDValue OpsA[] = { MemAddr, MemUpdate, MemOpc, Align,
1094                              Pred, PredReg, Chain };
1095     SDNode *VLdA = CurDAG->getMachineNode(Opc, dl, ResTys, OpsA, 7);
1096     Chain = SDValue(VLdA, NumVecs+1);
1097
1098     // Load the odd subregs.
1099     Opc = QOpcodes1[OpcodeIndex];
1100     const SDValue OpsB[] = { SDValue(VLdA, NumVecs), MemUpdate, MemOpc,
1101                              Align, Pred, PredReg, Chain };
1102     SDNode *VLdB = CurDAG->getMachineNode(Opc, dl, ResTys, OpsB, 7);
1103     Chain = SDValue(VLdB, NumVecs+1);
1104
1105     // Combine the even and odd subregs to produce the result.
1106     for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
1107       SDNode *Q = PairDRegs(VT, SDValue(VLdA, Vec), SDValue(VLdB, Vec));
1108       ReplaceUses(SDValue(N, Vec), SDValue(Q, 0));
1109     }
1110   }
1111   ReplaceUses(SDValue(N, NumVecs), Chain);
1112   return NULL;
1113 }
1114
1115 SDNode *ARMDAGToDAGISel::SelectVST(SDValue Op, unsigned NumVecs,
1116                                    unsigned *DOpcodes, unsigned *QOpcodes0,
1117                                    unsigned *QOpcodes1) {
1118   assert(NumVecs >=2 && NumVecs <= 4 && "VST NumVecs out-of-range");
1119   SDNode *N = Op.getNode();
1120   DebugLoc dl = N->getDebugLoc();
1121
1122   SDValue MemAddr, MemUpdate, MemOpc, Align;
1123   if (!SelectAddrMode6(Op, N->getOperand(2), MemAddr, MemUpdate, MemOpc, Align))
1124     return NULL;
1125
1126   SDValue Chain = N->getOperand(0);
1127   EVT VT = N->getOperand(3).getValueType();
1128   bool is64BitVector = VT.is64BitVector();
1129
1130   unsigned OpcodeIndex;
1131   switch (VT.getSimpleVT().SimpleTy) {
1132   default: llvm_unreachable("unhandled vst type");
1133     // Double-register operations:
1134   case MVT::v8i8:  OpcodeIndex = 0; break;
1135   case MVT::v4i16: OpcodeIndex = 1; break;
1136   case MVT::v2f32:
1137   case MVT::v2i32: OpcodeIndex = 2; break;
1138   case MVT::v1i64: OpcodeIndex = 3; break;
1139     // Quad-register operations:
1140   case MVT::v16i8: OpcodeIndex = 0; break;
1141   case MVT::v8i16: OpcodeIndex = 1; break;
1142   case MVT::v4f32:
1143   case MVT::v4i32: OpcodeIndex = 2; break;
1144   }
1145
1146   SDValue Pred = CurDAG->getTargetConstant(14, MVT::i32);
1147   SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1148
1149   SmallVector<SDValue, 8> Ops;
1150   Ops.push_back(MemAddr);
1151   Ops.push_back(MemUpdate);
1152   Ops.push_back(MemOpc);
1153   Ops.push_back(Align);
1154
1155   if (is64BitVector) {
1156     unsigned Opc = DOpcodes[OpcodeIndex];
1157     for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1158       Ops.push_back(N->getOperand(Vec+3));
1159     Ops.push_back(Pred);
1160     Ops.push_back(PredReg);
1161     Ops.push_back(Chain);
1162     return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), NumVecs+7);
1163   }
1164
1165   EVT RegVT = GetNEONSubregVT(VT);
1166   if (NumVecs == 2) {
1167     // Quad registers are directly supported for VST2,
1168     // storing 2 pairs of D regs.
1169     unsigned Opc = QOpcodes0[OpcodeIndex];
1170     for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
1171       Ops.push_back(CurDAG->getTargetExtractSubreg(ARM::DSUBREG_0, dl, RegVT,
1172                                                    N->getOperand(Vec+3)));
1173       Ops.push_back(CurDAG->getTargetExtractSubreg(ARM::DSUBREG_1, dl, RegVT,
1174                                                    N->getOperand(Vec+3)));
1175     }
1176     Ops.push_back(Pred);
1177     Ops.push_back(PredReg);
1178     Ops.push_back(Chain);
1179     return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), 11);
1180   }
1181
1182   // Otherwise, quad registers are stored with two separate instructions,
1183   // where one stores the even registers and the other stores the odd registers.
1184
1185   // Enable writeback to the address register.
1186   MemOpc = CurDAG->getTargetConstant(ARM_AM::getAM6Opc(true), MVT::i32);
1187
1188   // Store the even subregs.
1189   for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1190     Ops.push_back(CurDAG->getTargetExtractSubreg(ARM::DSUBREG_0, dl, RegVT,
1191                                                  N->getOperand(Vec+3)));
1192   Ops.push_back(Pred);
1193   Ops.push_back(PredReg);
1194   Ops.push_back(Chain);
1195   unsigned Opc = QOpcodes0[OpcodeIndex];
1196   SDNode *VStA = CurDAG->getMachineNode(Opc, dl, MemAddr.getValueType(),
1197                                         MVT::Other, Ops.data(), NumVecs+7);
1198   Chain = SDValue(VStA, 1);
1199
1200   // Store the odd subregs.
1201   Ops[0] = SDValue(VStA, 0); // MemAddr
1202   for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1203     Ops[Vec+4] = CurDAG->getTargetExtractSubreg(ARM::DSUBREG_1, dl, RegVT,
1204                                                 N->getOperand(Vec+3));
1205   Ops[NumVecs+4] = Pred;
1206   Ops[NumVecs+5] = PredReg;
1207   Ops[NumVecs+6] = Chain;
1208   Opc = QOpcodes1[OpcodeIndex];
1209   SDNode *VStB = CurDAG->getMachineNode(Opc, dl, MemAddr.getValueType(),
1210                                         MVT::Other, Ops.data(), NumVecs+7);
1211   Chain = SDValue(VStB, 1);
1212   ReplaceUses(SDValue(N, 0), Chain);
1213   return NULL;
1214 }
1215
1216 SDNode *ARMDAGToDAGISel::SelectVLDSTLane(SDValue Op, bool IsLoad,
1217                                          unsigned NumVecs, unsigned *DOpcodes,
1218                                          unsigned *QOpcodes0,
1219                                          unsigned *QOpcodes1) {
1220   assert(NumVecs >=2 && NumVecs <= 4 && "VLDSTLane NumVecs out-of-range");
1221   SDNode *N = Op.getNode();
1222   DebugLoc dl = N->getDebugLoc();
1223
1224   SDValue MemAddr, MemUpdate, MemOpc, Align;
1225   if (!SelectAddrMode6(Op, N->getOperand(2), MemAddr, MemUpdate, MemOpc, Align))
1226     return NULL;
1227
1228   SDValue Chain = N->getOperand(0);
1229   unsigned Lane =
1230     cast<ConstantSDNode>(N->getOperand(NumVecs+3))->getZExtValue();
1231   EVT VT = IsLoad ? N->getValueType(0) : N->getOperand(3).getValueType();
1232   bool is64BitVector = VT.is64BitVector();
1233
1234   // Quad registers are handled by load/store of subregs. Find the subreg info.
1235   unsigned NumElts = 0;
1236   int SubregIdx = 0;
1237   EVT RegVT = VT;
1238   if (!is64BitVector) {
1239     RegVT = GetNEONSubregVT(VT);
1240     NumElts = RegVT.getVectorNumElements();
1241     SubregIdx = (Lane < NumElts) ? ARM::DSUBREG_0 : ARM::DSUBREG_1;
1242   }
1243
1244   unsigned OpcodeIndex;
1245   switch (VT.getSimpleVT().SimpleTy) {
1246   default: llvm_unreachable("unhandled vld/vst lane type");
1247     // Double-register operations:
1248   case MVT::v8i8:  OpcodeIndex = 0; break;
1249   case MVT::v4i16: OpcodeIndex = 1; break;
1250   case MVT::v2f32:
1251   case MVT::v2i32: OpcodeIndex = 2; break;
1252     // Quad-register operations:
1253   case MVT::v8i16: OpcodeIndex = 0; break;
1254   case MVT::v4f32:
1255   case MVT::v4i32: OpcodeIndex = 1; break;
1256   }
1257
1258   SDValue Pred = CurDAG->getTargetConstant(14, MVT::i32);
1259   SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1260
1261   SmallVector<SDValue, 9> Ops;
1262   Ops.push_back(MemAddr);
1263   Ops.push_back(MemUpdate);
1264   Ops.push_back(MemOpc);
1265   Ops.push_back(Align);
1266
1267   unsigned Opc = 0;
1268   if (is64BitVector) {
1269     Opc = DOpcodes[OpcodeIndex];
1270     for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1271       Ops.push_back(N->getOperand(Vec+3));
1272   } else {
1273     // Check if this is loading the even or odd subreg of a Q register.
1274     if (Lane < NumElts) {
1275       Opc = QOpcodes0[OpcodeIndex];
1276     } else {
1277       Lane -= NumElts;
1278       Opc = QOpcodes1[OpcodeIndex];
1279     }
1280     // Extract the subregs of the input vector.
1281     for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1282       Ops.push_back(CurDAG->getTargetExtractSubreg(SubregIdx, dl, RegVT,
1283                                                    N->getOperand(Vec+3)));
1284   }
1285   Ops.push_back(getI32Imm(Lane));
1286   Ops.push_back(Pred);
1287   Ops.push_back(PredReg);
1288   Ops.push_back(Chain);
1289
1290   if (!IsLoad)
1291     return CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops.data(), NumVecs+7);
1292
1293   std::vector<EVT> ResTys(NumVecs, RegVT);
1294   ResTys.push_back(MVT::Other);
1295   SDNode *VLdLn =
1296     CurDAG->getMachineNode(Opc, dl, ResTys, Ops.data(), NumVecs+7);
1297   // For a 64-bit vector load to D registers, nothing more needs to be done.
1298   if (is64BitVector)
1299     return VLdLn;
1300
1301   // For 128-bit vectors, take the 64-bit results of the load and insert them
1302   // as subregs into the result.
1303   for (unsigned Vec = 0; Vec < NumVecs; ++Vec) {
1304     SDValue QuadVec = CurDAG->getTargetInsertSubreg(SubregIdx, dl, VT,
1305                                                     N->getOperand(Vec+3),
1306                                                     SDValue(VLdLn, Vec));
1307     ReplaceUses(SDValue(N, Vec), QuadVec);
1308   }
1309
1310   Chain = SDValue(VLdLn, NumVecs);
1311   ReplaceUses(SDValue(N, NumVecs), Chain);
1312   return NULL;
1313 }
1314
1315 SDNode *ARMDAGToDAGISel::SelectV6T2BitfieldExtractOp(SDValue Op,
1316                                                      unsigned Opc) {
1317   if (!Subtarget->hasV6T2Ops())
1318     return NULL;
1319
1320   unsigned Shl_imm = 0;
1321   if (isOpcWithIntImmediate(Op.getOperand(0).getNode(), ISD::SHL, Shl_imm)) {
1322     assert(Shl_imm > 0 && Shl_imm < 32 && "bad amount in shift node!");
1323     unsigned Srl_imm = 0;
1324     if (isInt32Immediate(Op.getOperand(1), Srl_imm)) {
1325       assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
1326       unsigned Width = 32 - Srl_imm;
1327       int LSB = Srl_imm - Shl_imm;
1328       if (LSB < 0)
1329         return NULL;
1330       SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1331       SDValue Ops[] = { Op.getOperand(0).getOperand(0),
1332                         CurDAG->getTargetConstant(LSB, MVT::i32),
1333                         CurDAG->getTargetConstant(Width, MVT::i32),
1334                         getAL(CurDAG), Reg0 };
1335       return CurDAG->SelectNodeTo(Op.getNode(), Opc, MVT::i32, Ops, 5);
1336     }
1337   }
1338   return NULL;
1339 }
1340
1341 SDNode *ARMDAGToDAGISel::
1342 SelectT2CMOVShiftOp(SDValue Op, SDValue FalseVal, SDValue TrueVal,
1343                     ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1344   SDValue CPTmp0;
1345   SDValue CPTmp1;
1346   if (SelectT2ShifterOperandReg(Op, TrueVal, CPTmp0, CPTmp1)) {
1347     unsigned SOVal = cast<ConstantSDNode>(CPTmp1)->getZExtValue();
1348     unsigned SOShOp = ARM_AM::getSORegShOp(SOVal);
1349     unsigned Opc = 0;
1350     switch (SOShOp) {
1351     case ARM_AM::lsl: Opc = ARM::t2MOVCClsl; break;
1352     case ARM_AM::lsr: Opc = ARM::t2MOVCClsr; break;
1353     case ARM_AM::asr: Opc = ARM::t2MOVCCasr; break;
1354     case ARM_AM::ror: Opc = ARM::t2MOVCCror; break;
1355     default:
1356       llvm_unreachable("Unknown so_reg opcode!");
1357       break;
1358     }
1359     SDValue SOShImm =
1360       CurDAG->getTargetConstant(ARM_AM::getSORegOffset(SOVal), MVT::i32);
1361     SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1362     SDValue Ops[] = { FalseVal, CPTmp0, SOShImm, CC, CCR, InFlag };
1363     return CurDAG->SelectNodeTo(Op.getNode(), Opc, MVT::i32,Ops, 6);
1364   }
1365   return 0;
1366 }
1367
1368 SDNode *ARMDAGToDAGISel::
1369 SelectARMCMOVShiftOp(SDValue Op, SDValue FalseVal, SDValue TrueVal,
1370                      ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1371   SDValue CPTmp0;
1372   SDValue CPTmp1;
1373   SDValue CPTmp2;
1374   if (SelectShifterOperandReg(Op, TrueVal, CPTmp0, CPTmp1, CPTmp2)) {
1375     SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1376     SDValue Ops[] = { FalseVal, CPTmp0, CPTmp1, CPTmp2, CC, CCR, InFlag };
1377     return CurDAG->SelectNodeTo(Op.getNode(), ARM::MOVCCs, MVT::i32, Ops, 7);
1378   }
1379   return 0;
1380 }
1381
1382 SDNode *ARMDAGToDAGISel::
1383 SelectT2CMOVSoImmOp(SDValue Op, SDValue FalseVal, SDValue TrueVal,
1384                     ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1385   ConstantSDNode *T = dyn_cast<ConstantSDNode>(TrueVal);
1386   if (!T)
1387     return 0;
1388
1389   if (Predicate_t2_so_imm(TrueVal.getNode())) {
1390     SDValue True = CurDAG->getTargetConstant(T->getZExtValue(), MVT::i32);
1391     SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1392     SDValue Ops[] = { FalseVal, True, CC, CCR, InFlag };
1393     return CurDAG->SelectNodeTo(Op.getNode(),
1394                                 ARM::t2MOVCCi, MVT::i32, Ops, 5);
1395   }
1396   return 0;
1397 }
1398
1399 SDNode *ARMDAGToDAGISel::
1400 SelectARMCMOVSoImmOp(SDValue Op, SDValue FalseVal, SDValue TrueVal,
1401                      ARMCC::CondCodes CCVal, SDValue CCR, SDValue InFlag) {
1402   ConstantSDNode *T = dyn_cast<ConstantSDNode>(TrueVal);
1403   if (!T)
1404     return 0;
1405
1406   if (Predicate_so_imm(TrueVal.getNode())) {
1407     SDValue True = CurDAG->getTargetConstant(T->getZExtValue(), MVT::i32);
1408     SDValue CC = CurDAG->getTargetConstant(CCVal, MVT::i32);
1409     SDValue Ops[] = { FalseVal, True, CC, CCR, InFlag };
1410     return CurDAG->SelectNodeTo(Op.getNode(),
1411                                 ARM::MOVCCi, MVT::i32, Ops, 5);
1412   }
1413   return 0;
1414 }
1415
1416 SDNode *ARMDAGToDAGISel::SelectCMOVOp(SDValue Op) {
1417   EVT VT = Op.getValueType();
1418   SDValue FalseVal = Op.getOperand(0);
1419   SDValue TrueVal  = Op.getOperand(1);
1420   SDValue CC = Op.getOperand(2);
1421   SDValue CCR = Op.getOperand(3);
1422   SDValue InFlag = Op.getOperand(4);
1423   assert(CC.getOpcode() == ISD::Constant);
1424   assert(CCR.getOpcode() == ISD::Register);
1425   ARMCC::CondCodes CCVal =
1426     (ARMCC::CondCodes)cast<ConstantSDNode>(CC)->getZExtValue();
1427
1428   if (!Subtarget->isThumb1Only() && VT == MVT::i32) {
1429     // Pattern: (ARMcmov:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
1430     // Emits: (MOVCCs:i32 GPR:i32:$false, so_reg:i32:$true, (imm:i32):$cc)
1431     // Pattern complexity = 18  cost = 1  size = 0
1432     SDValue CPTmp0;
1433     SDValue CPTmp1;
1434     SDValue CPTmp2;
1435     if (Subtarget->isThumb()) {
1436       SDNode *Res = SelectT2CMOVShiftOp(Op, FalseVal, TrueVal,
1437                                         CCVal, CCR, InFlag);
1438       if (!Res)
1439         Res = SelectT2CMOVShiftOp(Op, TrueVal, FalseVal,
1440                                ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1441       if (Res)
1442         return Res;
1443     } else {
1444       SDNode *Res = SelectARMCMOVShiftOp(Op, FalseVal, TrueVal,
1445                                          CCVal, CCR, InFlag);
1446       if (!Res)
1447         Res = SelectARMCMOVShiftOp(Op, TrueVal, FalseVal,
1448                                ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1449       if (Res)
1450         return Res;
1451     }
1452
1453     // Pattern: (ARMcmov:i32 GPR:i32:$false,
1454     //             (imm:i32)<<P:Predicate_so_imm>>:$true,
1455     //             (imm:i32):$cc)
1456     // Emits: (MOVCCi:i32 GPR:i32:$false,
1457     //           (so_imm:i32 (imm:i32):$true), (imm:i32):$cc)
1458     // Pattern complexity = 10  cost = 1  size = 0
1459     if (Subtarget->isThumb()) {
1460       SDNode *Res = SelectT2CMOVSoImmOp(Op, FalseVal, TrueVal,
1461                                         CCVal, CCR, InFlag);
1462       if (!Res)
1463         Res = SelectT2CMOVSoImmOp(Op, TrueVal, FalseVal,
1464                                ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1465       if (Res)
1466         return Res;
1467     } else {
1468       SDNode *Res = SelectARMCMOVSoImmOp(Op, FalseVal, TrueVal,
1469                                          CCVal, CCR, InFlag);
1470       if (!Res)
1471         Res = SelectARMCMOVSoImmOp(Op, TrueVal, FalseVal,
1472                                ARMCC::getOppositeCondition(CCVal), CCR, InFlag);
1473       if (Res)
1474         return Res;
1475     }
1476   }
1477
1478   // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1479   // Emits: (MOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1480   // Pattern complexity = 6  cost = 1  size = 0
1481   //
1482   // Pattern: (ARMcmov:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1483   // Emits: (tMOVCCr:i32 GPR:i32:$false, GPR:i32:$true, (imm:i32):$cc)
1484   // Pattern complexity = 6  cost = 11  size = 0
1485   //
1486   // Also FCPYScc and FCPYDcc.
1487   SDValue Tmp2 = CurDAG->getTargetConstant(CCVal, MVT::i32);
1488   SDValue Ops[] = { FalseVal, TrueVal, Tmp2, CCR, InFlag };
1489   unsigned Opc = 0;
1490   switch (VT.getSimpleVT().SimpleTy) {
1491   default: assert(false && "Illegal conditional move type!");
1492     break;
1493   case MVT::i32:
1494     Opc = Subtarget->isThumb()
1495       ? (Subtarget->hasThumb2() ? ARM::t2MOVCCr : ARM::tMOVCCr_pseudo)
1496       : ARM::MOVCCr;
1497     break;
1498   case MVT::f32:
1499     Opc = ARM::VMOVScc;
1500     break;
1501   case MVT::f64:
1502     Opc = ARM::VMOVDcc;
1503     break;
1504   }
1505   return CurDAG->SelectNodeTo(Op.getNode(), Opc, VT, Ops, 5);
1506 }
1507
1508 SDNode *ARMDAGToDAGISel::Select(SDValue Op) {
1509   SDNode *N = Op.getNode();
1510   DebugLoc dl = N->getDebugLoc();
1511
1512   if (N->isMachineOpcode())
1513     return NULL;   // Already selected.
1514
1515   switch (N->getOpcode()) {
1516   default: break;
1517   case ISD::Constant: {
1518     unsigned Val = cast<ConstantSDNode>(N)->getZExtValue();
1519     bool UseCP = true;
1520     if (Subtarget->hasThumb2())
1521       // Thumb2-aware targets have the MOVT instruction, so all immediates can
1522       // be done with MOV + MOVT, at worst.
1523       UseCP = 0;
1524     else {
1525       if (Subtarget->isThumb()) {
1526         UseCP = (Val > 255 &&                          // MOV
1527                  ~Val > 255 &&                         // MOV + MVN
1528                  !ARM_AM::isThumbImmShiftedVal(Val));  // MOV + LSL
1529       } else
1530         UseCP = (ARM_AM::getSOImmVal(Val) == -1 &&     // MOV
1531                  ARM_AM::getSOImmVal(~Val) == -1 &&    // MVN
1532                  !ARM_AM::isSOImmTwoPartVal(Val));     // two instrs.
1533     }
1534
1535     if (UseCP) {
1536       SDValue CPIdx =
1537         CurDAG->getTargetConstantPool(ConstantInt::get(
1538                                   Type::getInt32Ty(*CurDAG->getContext()), Val),
1539                                       TLI.getPointerTy());
1540
1541       SDNode *ResNode;
1542       if (Subtarget->isThumb1Only()) {
1543         SDValue Pred = CurDAG->getTargetConstant(14, MVT::i32);
1544         SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1545         SDValue Ops[] = { CPIdx, Pred, PredReg, CurDAG->getEntryNode() };
1546         ResNode = CurDAG->getMachineNode(ARM::tLDRcp, dl, MVT::i32, MVT::Other,
1547                                          Ops, 4);
1548       } else {
1549         SDValue Ops[] = {
1550           CPIdx,
1551           CurDAG->getRegister(0, MVT::i32),
1552           CurDAG->getTargetConstant(0, MVT::i32),
1553           getAL(CurDAG),
1554           CurDAG->getRegister(0, MVT::i32),
1555           CurDAG->getEntryNode()
1556         };
1557         ResNode=CurDAG->getMachineNode(ARM::LDRcp, dl, MVT::i32, MVT::Other,
1558                                        Ops, 6);
1559       }
1560       ReplaceUses(Op, SDValue(ResNode, 0));
1561       return NULL;
1562     }
1563
1564     // Other cases are autogenerated.
1565     break;
1566   }
1567   case ISD::FrameIndex: {
1568     // Selects to ADDri FI, 0 which in turn will become ADDri SP, imm.
1569     int FI = cast<FrameIndexSDNode>(N)->getIndex();
1570     SDValue TFI = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
1571     if (Subtarget->isThumb1Only()) {
1572       return CurDAG->SelectNodeTo(N, ARM::tADDrSPi, MVT::i32, TFI,
1573                                   CurDAG->getTargetConstant(0, MVT::i32));
1574     } else {
1575       unsigned Opc = ((Subtarget->isThumb() && Subtarget->hasThumb2()) ?
1576                       ARM::t2ADDri : ARM::ADDri);
1577       SDValue Ops[] = { TFI, CurDAG->getTargetConstant(0, MVT::i32),
1578                         getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1579                         CurDAG->getRegister(0, MVT::i32) };
1580       return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops, 5);
1581     }
1582   }
1583   case ARMISD::DYN_ALLOC:
1584     return SelectDYN_ALLOC(Op);
1585   case ISD::SRL:
1586     if (SDNode *I = SelectV6T2BitfieldExtractOp(Op,
1587                       Subtarget->isThumb() ? ARM::t2UBFX : ARM::UBFX))
1588       return I;
1589     break;
1590   case ISD::SRA:
1591     if (SDNode *I = SelectV6T2BitfieldExtractOp(Op,
1592                       Subtarget->isThumb() ? ARM::t2SBFX : ARM::SBFX))
1593       return I;
1594     break;
1595   case ISD::MUL:
1596     if (Subtarget->isThumb1Only())
1597       break;
1598     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1599       unsigned RHSV = C->getZExtValue();
1600       if (!RHSV) break;
1601       if (isPowerOf2_32(RHSV-1)) {  // 2^n+1?
1602         unsigned ShImm = Log2_32(RHSV-1);
1603         if (ShImm >= 32)
1604           break;
1605         SDValue V = Op.getOperand(0);
1606         ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
1607         SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, MVT::i32);
1608         SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1609         if (Subtarget->isThumb()) {
1610           SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
1611           return CurDAG->SelectNodeTo(N, ARM::t2ADDrs, MVT::i32, Ops, 6);
1612         } else {
1613           SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
1614           return CurDAG->SelectNodeTo(N, ARM::ADDrs, MVT::i32, Ops, 7);
1615         }
1616       }
1617       if (isPowerOf2_32(RHSV+1)) {  // 2^n-1?
1618         unsigned ShImm = Log2_32(RHSV+1);
1619         if (ShImm >= 32)
1620           break;
1621         SDValue V = Op.getOperand(0);
1622         ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
1623         SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, MVT::i32);
1624         SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1625         if (Subtarget->isThumb()) {
1626           SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG), Reg0 };
1627           return CurDAG->SelectNodeTo(N, ARM::t2RSBrs, MVT::i32, Ops, 5);
1628         } else {
1629           SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG), Reg0, Reg0 };
1630           return CurDAG->SelectNodeTo(N, ARM::RSBrs, MVT::i32, Ops, 7);
1631         }
1632       }
1633     }
1634     break;
1635   case ISD::AND: {
1636     // (and (or x, c2), c1) and top 16-bits of c1 and c2 match, lower 16-bits
1637     // of c1 are 0xffff, and lower 16-bit of c2 are 0. That is, the top 16-bits
1638     // are entirely contributed by c2 and lower 16-bits are entirely contributed
1639     // by x. That's equal to (or (and x, 0xffff), (and c1, 0xffff0000)).
1640     // Select it to: "movt x, ((c1 & 0xffff) >> 16)
1641     EVT VT = Op.getValueType();
1642     if (VT != MVT::i32)
1643       break;
1644     unsigned Opc = (Subtarget->isThumb() && Subtarget->hasThumb2())
1645       ? ARM::t2MOVTi16
1646       : (Subtarget->hasV6T2Ops() ? ARM::MOVTi16 : 0);
1647     if (!Opc)
1648       break;
1649     SDValue N0 = Op.getOperand(0), N1 = Op.getOperand(1);
1650     ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1651     if (!N1C)
1652       break;
1653     if (N0.getOpcode() == ISD::OR && N0.getNode()->hasOneUse()) {
1654       SDValue N2 = N0.getOperand(1);
1655       ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1656       if (!N2C)
1657         break;
1658       unsigned N1CVal = N1C->getZExtValue();
1659       unsigned N2CVal = N2C->getZExtValue();
1660       if ((N1CVal & 0xffff0000U) == (N2CVal & 0xffff0000U) &&
1661           (N1CVal & 0xffffU) == 0xffffU &&
1662           (N2CVal & 0xffffU) == 0x0U) {
1663         SDValue Imm16 = CurDAG->getTargetConstant((N2CVal & 0xFFFF0000U) >> 16,
1664                                                   MVT::i32);
1665         SDValue Ops[] = { N0.getOperand(0), Imm16,
1666                           getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
1667         return CurDAG->getMachineNode(Opc, dl, VT, Ops, 4);
1668       }
1669     }
1670     break;
1671   }
1672   case ARMISD::VMOVRRD:
1673     return CurDAG->getMachineNode(ARM::VMOVRRD, dl, MVT::i32, MVT::i32,
1674                                   Op.getOperand(0), getAL(CurDAG),
1675                                   CurDAG->getRegister(0, MVT::i32));
1676   case ISD::UMUL_LOHI: {
1677     if (Subtarget->isThumb1Only())
1678       break;
1679     if (Subtarget->isThumb()) {
1680       SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1),
1681                         getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1682                         CurDAG->getRegister(0, MVT::i32) };
1683       return CurDAG->getMachineNode(ARM::t2UMULL, dl, MVT::i32, MVT::i32, Ops,4);
1684     } else {
1685       SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1),
1686                         getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1687                         CurDAG->getRegister(0, MVT::i32) };
1688       return CurDAG->getMachineNode(ARM::UMULL, dl, MVT::i32, MVT::i32, Ops, 5);
1689     }
1690   }
1691   case ISD::SMUL_LOHI: {
1692     if (Subtarget->isThumb1Only())
1693       break;
1694     if (Subtarget->isThumb()) {
1695       SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1),
1696                         getAL(CurDAG), CurDAG->getRegister(0, MVT::i32) };
1697       return CurDAG->getMachineNode(ARM::t2SMULL, dl, MVT::i32, MVT::i32, Ops,4);
1698     } else {
1699       SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1),
1700                         getAL(CurDAG), CurDAG->getRegister(0, MVT::i32),
1701                         CurDAG->getRegister(0, MVT::i32) };
1702       return CurDAG->getMachineNode(ARM::SMULL, dl, MVT::i32, MVT::i32, Ops, 5);
1703     }
1704   }
1705   case ISD::LOAD: {
1706     SDNode *ResNode = 0;
1707     if (Subtarget->isThumb() && Subtarget->hasThumb2())
1708       ResNode = SelectT2IndexedLoad(Op);
1709     else
1710       ResNode = SelectARMIndexedLoad(Op);
1711     if (ResNode)
1712       return ResNode;
1713     // Other cases are autogenerated.
1714     break;
1715   }
1716   case ARMISD::BRCOND: {
1717     // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
1718     // Emits: (Bcc:void (bb:Other):$dst, (imm:i32):$cc)
1719     // Pattern complexity = 6  cost = 1  size = 0
1720
1721     // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
1722     // Emits: (tBcc:void (bb:Other):$dst, (imm:i32):$cc)
1723     // Pattern complexity = 6  cost = 1  size = 0
1724
1725     // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
1726     // Emits: (t2Bcc:void (bb:Other):$dst, (imm:i32):$cc)
1727     // Pattern complexity = 6  cost = 1  size = 0
1728
1729     unsigned Opc = Subtarget->isThumb() ?
1730       ((Subtarget->hasThumb2()) ? ARM::t2Bcc : ARM::tBcc) : ARM::Bcc;
1731     SDValue Chain = Op.getOperand(0);
1732     SDValue N1 = Op.getOperand(1);
1733     SDValue N2 = Op.getOperand(2);
1734     SDValue N3 = Op.getOperand(3);
1735     SDValue InFlag = Op.getOperand(4);
1736     assert(N1.getOpcode() == ISD::BasicBlock);
1737     assert(N2.getOpcode() == ISD::Constant);
1738     assert(N3.getOpcode() == ISD::Register);
1739
1740     SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
1741                                cast<ConstantSDNode>(N2)->getZExtValue()),
1742                                MVT::i32);
1743     SDValue Ops[] = { N1, Tmp2, N3, Chain, InFlag };
1744     SDNode *ResNode = CurDAG->getMachineNode(Opc, dl, MVT::Other,
1745                                              MVT::Flag, Ops, 5);
1746     Chain = SDValue(ResNode, 0);
1747     if (Op.getNode()->getNumValues() == 2) {
1748       InFlag = SDValue(ResNode, 1);
1749       ReplaceUses(SDValue(Op.getNode(), 1), InFlag);
1750     }
1751     ReplaceUses(SDValue(Op.getNode(), 0),
1752                 SDValue(Chain.getNode(), Chain.getResNo()));
1753     return NULL;
1754   }
1755   case ARMISD::CMOV:
1756     return SelectCMOVOp(Op);
1757   case ARMISD::CNEG: {
1758     EVT VT = Op.getValueType();
1759     SDValue N0 = Op.getOperand(0);
1760     SDValue N1 = Op.getOperand(1);
1761     SDValue N2 = Op.getOperand(2);
1762     SDValue N3 = Op.getOperand(3);
1763     SDValue InFlag = Op.getOperand(4);
1764     assert(N2.getOpcode() == ISD::Constant);
1765     assert(N3.getOpcode() == ISD::Register);
1766
1767     SDValue Tmp2 = CurDAG->getTargetConstant(((unsigned)
1768                                cast<ConstantSDNode>(N2)->getZExtValue()),
1769                                MVT::i32);
1770     SDValue Ops[] = { N0, N1, Tmp2, N3, InFlag };
1771     unsigned Opc = 0;
1772     switch (VT.getSimpleVT().SimpleTy) {
1773     default: assert(false && "Illegal conditional move type!");
1774       break;
1775     case MVT::f32:
1776       Opc = ARM::VNEGScc;
1777       break;
1778     case MVT::f64:
1779       Opc = ARM::VNEGDcc;
1780       break;
1781     }
1782     return CurDAG->SelectNodeTo(Op.getNode(), Opc, VT, Ops, 5);
1783   }
1784
1785   case ARMISD::VZIP: {
1786     unsigned Opc = 0;
1787     EVT VT = N->getValueType(0);
1788     switch (VT.getSimpleVT().SimpleTy) {
1789     default: return NULL;
1790     case MVT::v8i8:  Opc = ARM::VZIPd8; break;
1791     case MVT::v4i16: Opc = ARM::VZIPd16; break;
1792     case MVT::v2f32:
1793     case MVT::v2i32: Opc = ARM::VZIPd32; break;
1794     case MVT::v16i8: Opc = ARM::VZIPq8; break;
1795     case MVT::v8i16: Opc = ARM::VZIPq16; break;
1796     case MVT::v4f32:
1797     case MVT::v4i32: Opc = ARM::VZIPq32; break;
1798     }
1799     SDValue Pred = CurDAG->getTargetConstant(14, MVT::i32);
1800     SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1801     SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
1802     return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
1803   }
1804   case ARMISD::VUZP: {
1805     unsigned Opc = 0;
1806     EVT VT = N->getValueType(0);
1807     switch (VT.getSimpleVT().SimpleTy) {
1808     default: return NULL;
1809     case MVT::v8i8:  Opc = ARM::VUZPd8; break;
1810     case MVT::v4i16: Opc = ARM::VUZPd16; break;
1811     case MVT::v2f32:
1812     case MVT::v2i32: Opc = ARM::VUZPd32; break;
1813     case MVT::v16i8: Opc = ARM::VUZPq8; break;
1814     case MVT::v8i16: Opc = ARM::VUZPq16; break;
1815     case MVT::v4f32:
1816     case MVT::v4i32: Opc = ARM::VUZPq32; break;
1817     }
1818     SDValue Pred = CurDAG->getTargetConstant(14, MVT::i32);
1819     SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1820     SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
1821     return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
1822   }
1823   case ARMISD::VTRN: {
1824     unsigned Opc = 0;
1825     EVT VT = N->getValueType(0);
1826     switch (VT.getSimpleVT().SimpleTy) {
1827     default: return NULL;
1828     case MVT::v8i8:  Opc = ARM::VTRNd8; break;
1829     case MVT::v4i16: Opc = ARM::VTRNd16; break;
1830     case MVT::v2f32:
1831     case MVT::v2i32: Opc = ARM::VTRNd32; break;
1832     case MVT::v16i8: Opc = ARM::VTRNq8; break;
1833     case MVT::v8i16: Opc = ARM::VTRNq16; break;
1834     case MVT::v4f32:
1835     case MVT::v4i32: Opc = ARM::VTRNq32; break;
1836     }
1837     SDValue Pred = CurDAG->getTargetConstant(14, MVT::i32);
1838     SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
1839     SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
1840     return CurDAG->getMachineNode(Opc, dl, VT, VT, Ops, 4);
1841   }
1842
1843   case ISD::INTRINSIC_VOID:
1844   case ISD::INTRINSIC_W_CHAIN: {
1845     unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
1846     switch (IntNo) {
1847     default:
1848       break;
1849
1850     case Intrinsic::arm_neon_vld2: {
1851       unsigned DOpcodes[] = { ARM::VLD2d8, ARM::VLD2d16,
1852                               ARM::VLD2d32, ARM::VLD2d64 };
1853       unsigned QOpcodes[] = { ARM::VLD2q8, ARM::VLD2q16, ARM::VLD2q32 };
1854       return SelectVLD(Op, 2, DOpcodes, QOpcodes, 0);
1855     }
1856
1857     case Intrinsic::arm_neon_vld3: {
1858       unsigned DOpcodes[] = { ARM::VLD3d8, ARM::VLD3d16,
1859                               ARM::VLD3d32, ARM::VLD3d64 };
1860       unsigned QOpcodes0[] = { ARM::VLD3q8a, ARM::VLD3q16a, ARM::VLD3q32a };
1861       unsigned QOpcodes1[] = { ARM::VLD3q8b, ARM::VLD3q16b, ARM::VLD3q32b };
1862       return SelectVLD(Op, 3, DOpcodes, QOpcodes0, QOpcodes1);
1863     }
1864
1865     case Intrinsic::arm_neon_vld4: {
1866       unsigned DOpcodes[] = { ARM::VLD4d8, ARM::VLD4d16,
1867                               ARM::VLD4d32, ARM::VLD4d64 };
1868       unsigned QOpcodes0[] = { ARM::VLD4q8a, ARM::VLD4q16a, ARM::VLD4q32a };
1869       unsigned QOpcodes1[] = { ARM::VLD4q8b, ARM::VLD4q16b, ARM::VLD4q32b };
1870       return SelectVLD(Op, 4, DOpcodes, QOpcodes0, QOpcodes1);
1871     }
1872
1873     case Intrinsic::arm_neon_vld2lane: {
1874       unsigned DOpcodes[] = { ARM::VLD2LNd8, ARM::VLD2LNd16, ARM::VLD2LNd32 };
1875       unsigned QOpcodes0[] = { ARM::VLD2LNq16a, ARM::VLD2LNq32a };
1876       unsigned QOpcodes1[] = { ARM::VLD2LNq16b, ARM::VLD2LNq32b };
1877       return SelectVLDSTLane(Op, true, 2, DOpcodes, QOpcodes0, QOpcodes1);
1878     }
1879
1880     case Intrinsic::arm_neon_vld3lane: {
1881       unsigned DOpcodes[] = { ARM::VLD3LNd8, ARM::VLD3LNd16, ARM::VLD3LNd32 };
1882       unsigned QOpcodes0[] = { ARM::VLD3LNq16a, ARM::VLD3LNq32a };
1883       unsigned QOpcodes1[] = { ARM::VLD3LNq16b, ARM::VLD3LNq32b };
1884       return SelectVLDSTLane(Op, true, 3, DOpcodes, QOpcodes0, QOpcodes1);
1885     }
1886
1887     case Intrinsic::arm_neon_vld4lane: {
1888       unsigned DOpcodes[] = { ARM::VLD4LNd8, ARM::VLD4LNd16, ARM::VLD4LNd32 };
1889       unsigned QOpcodes0[] = { ARM::VLD4LNq16a, ARM::VLD4LNq32a };
1890       unsigned QOpcodes1[] = { ARM::VLD4LNq16b, ARM::VLD4LNq32b };
1891       return SelectVLDSTLane(Op, true, 4, DOpcodes, QOpcodes0, QOpcodes1);
1892     }
1893
1894     case Intrinsic::arm_neon_vst2: {
1895       unsigned DOpcodes[] = { ARM::VST2d8, ARM::VST2d16,
1896                               ARM::VST2d32, ARM::VST2d64 };
1897       unsigned QOpcodes[] = { ARM::VST2q8, ARM::VST2q16, ARM::VST2q32 };
1898       return SelectVST(Op, 2, DOpcodes, QOpcodes, 0);
1899     }
1900
1901     case Intrinsic::arm_neon_vst3: {
1902       unsigned DOpcodes[] = { ARM::VST3d8, ARM::VST3d16,
1903                               ARM::VST3d32, ARM::VST3d64 };
1904       unsigned QOpcodes0[] = { ARM::VST3q8a, ARM::VST3q16a, ARM::VST3q32a };
1905       unsigned QOpcodes1[] = { ARM::VST3q8b, ARM::VST3q16b, ARM::VST3q32b };
1906       return SelectVST(Op, 3, DOpcodes, QOpcodes0, QOpcodes1);
1907     }
1908
1909     case Intrinsic::arm_neon_vst4: {
1910       unsigned DOpcodes[] = { ARM::VST4d8, ARM::VST4d16,
1911                               ARM::VST4d32, ARM::VST4d64 };
1912       unsigned QOpcodes0[] = { ARM::VST4q8a, ARM::VST4q16a, ARM::VST4q32a };
1913       unsigned QOpcodes1[] = { ARM::VST4q8b, ARM::VST4q16b, ARM::VST4q32b };
1914       return SelectVST(Op, 4, DOpcodes, QOpcodes0, QOpcodes1);
1915     }
1916
1917     case Intrinsic::arm_neon_vst2lane: {
1918       unsigned DOpcodes[] = { ARM::VST2LNd8, ARM::VST2LNd16, ARM::VST2LNd32 };
1919       unsigned QOpcodes0[] = { ARM::VST2LNq16a, ARM::VST2LNq32a };
1920       unsigned QOpcodes1[] = { ARM::VST2LNq16b, ARM::VST2LNq32b };
1921       return SelectVLDSTLane(Op, false, 2, DOpcodes, QOpcodes0, QOpcodes1);
1922     }
1923
1924     case Intrinsic::arm_neon_vst3lane: {
1925       unsigned DOpcodes[] = { ARM::VST3LNd8, ARM::VST3LNd16, ARM::VST3LNd32 };
1926       unsigned QOpcodes0[] = { ARM::VST3LNq16a, ARM::VST3LNq32a };
1927       unsigned QOpcodes1[] = { ARM::VST3LNq16b, ARM::VST3LNq32b };
1928       return SelectVLDSTLane(Op, false, 3, DOpcodes, QOpcodes0, QOpcodes1);
1929     }
1930
1931     case Intrinsic::arm_neon_vst4lane: {
1932       unsigned DOpcodes[] = { ARM::VST4LNd8, ARM::VST4LNd16, ARM::VST4LNd32 };
1933       unsigned QOpcodes0[] = { ARM::VST4LNq16a, ARM::VST4LNq32a };
1934       unsigned QOpcodes1[] = { ARM::VST4LNq16b, ARM::VST4LNq32b };
1935       return SelectVLDSTLane(Op, false, 4, DOpcodes, QOpcodes0, QOpcodes1);
1936     }
1937     }
1938   }
1939   }
1940
1941   return SelectCode(Op);
1942 }
1943
1944 bool ARMDAGToDAGISel::
1945 SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
1946                              std::vector<SDValue> &OutOps) {
1947   assert(ConstraintCode == 'm' && "unexpected asm memory constraint");
1948   // Require the address to be in a register.  That is safe for all ARM
1949   // variants and it is hard to do anything much smarter without knowing
1950   // how the operand is used.
1951   OutOps.push_back(Op);
1952   return false;
1953 }
1954
1955 /// createARMISelDag - This pass converts a legalized DAG into a
1956 /// ARM-specific DAG, ready for instruction scheduling.
1957 ///
1958 FunctionPass *llvm::createARMISelDag(ARMBaseTargetMachine &TM,
1959                                      CodeGenOpt::Level OptLevel) {
1960   return new ARMDAGToDAGISel(TM, OptLevel);
1961 }