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