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