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