Lower CONCAT_VECTOR during legalization instead of matching it during isel.
[oota-llvm.git] / lib / Target / ARM / ARMISelLowering.cpp
1 //===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
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 the interfaces that ARM uses to lower LLVM code into a
11 // selection DAG.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "ARM.h"
16 #include "ARMAddressingModes.h"
17 #include "ARMConstantPoolValue.h"
18 #include "ARMISelLowering.h"
19 #include "ARMMachineFunctionInfo.h"
20 #include "ARMRegisterInfo.h"
21 #include "ARMSubtarget.h"
22 #include "ARMTargetMachine.h"
23 #include "ARMTargetObjectFile.h"
24 #include "llvm/CallingConv.h"
25 #include "llvm/Constants.h"
26 #include "llvm/Function.h"
27 #include "llvm/Instruction.h"
28 #include "llvm/Intrinsics.h"
29 #include "llvm/GlobalValue.h"
30 #include "llvm/CodeGen/CallingConvLower.h"
31 #include "llvm/CodeGen/MachineBasicBlock.h"
32 #include "llvm/CodeGen/MachineFrameInfo.h"
33 #include "llvm/CodeGen/MachineFunction.h"
34 #include "llvm/CodeGen/MachineInstrBuilder.h"
35 #include "llvm/CodeGen/MachineRegisterInfo.h"
36 #include "llvm/CodeGen/PseudoSourceValue.h"
37 #include "llvm/CodeGen/SelectionDAG.h"
38 #include "llvm/Target/TargetOptions.h"
39 #include "llvm/ADT/VectorExtras.h"
40 #include "llvm/Support/ErrorHandling.h"
41 #include "llvm/Support/MathExtras.h"
42 using namespace llvm;
43
44 static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
45                                    CCValAssign::LocInfo &LocInfo,
46                                    ISD::ArgFlagsTy &ArgFlags,
47                                    CCState &State);
48 static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
49                                     CCValAssign::LocInfo &LocInfo,
50                                     ISD::ArgFlagsTy &ArgFlags,
51                                     CCState &State);
52 static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
53                                       CCValAssign::LocInfo &LocInfo,
54                                       ISD::ArgFlagsTy &ArgFlags,
55                                       CCState &State);
56 static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
57                                        CCValAssign::LocInfo &LocInfo,
58                                        ISD::ArgFlagsTy &ArgFlags,
59                                        CCState &State);
60
61 void ARMTargetLowering::addTypeForNEON(MVT VT, MVT PromotedLdStVT,
62                                        MVT PromotedBitwiseVT) {
63   if (VT != PromotedLdStVT) {
64     setOperationAction(ISD::LOAD, VT, Promote);
65     AddPromotedToType (ISD::LOAD, VT, PromotedLdStVT);
66
67     setOperationAction(ISD::STORE, VT, Promote);
68     AddPromotedToType (ISD::STORE, VT, PromotedLdStVT);
69   }
70
71   MVT ElemTy = VT.getVectorElementType();
72   if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
73     setOperationAction(ISD::VSETCC, VT, Custom);
74   if (ElemTy == MVT::i8 || ElemTy == MVT::i16)
75     setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
76   setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
77   setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
78   setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
79   setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
80   if (VT.isInteger()) {
81     setOperationAction(ISD::SHL, VT, Custom);
82     setOperationAction(ISD::SRA, VT, Custom);
83     setOperationAction(ISD::SRL, VT, Custom);
84   }
85
86   // Promote all bit-wise operations.
87   if (VT.isInteger() && VT != PromotedBitwiseVT) {
88     setOperationAction(ISD::AND, VT, Promote);
89     AddPromotedToType (ISD::AND, VT, PromotedBitwiseVT);
90     setOperationAction(ISD::OR,  VT, Promote);
91     AddPromotedToType (ISD::OR,  VT, PromotedBitwiseVT);
92     setOperationAction(ISD::XOR, VT, Promote);
93     AddPromotedToType (ISD::XOR, VT, PromotedBitwiseVT);
94   }
95 }
96
97 void ARMTargetLowering::addDRTypeForNEON(MVT VT) {
98   addRegisterClass(VT, ARM::DPRRegisterClass);
99   addTypeForNEON(VT, MVT::f64, MVT::v2i32);
100 }
101
102 void ARMTargetLowering::addQRTypeForNEON(MVT VT) {
103   addRegisterClass(VT, ARM::QPRRegisterClass);
104   addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
105 }
106
107 static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
108   if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
109     return new TargetLoweringObjectFileMachO();
110   return new ARMElfTargetObjectFile();
111 }
112
113 ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
114     : TargetLowering(TM, createTLOF(TM)), ARMPCLabelIndex(0) {
115   Subtarget = &TM.getSubtarget<ARMSubtarget>();
116
117   if (Subtarget->isTargetDarwin()) {
118     // Uses VFP for Thumb libfuncs if available.
119     if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
120       // Single-precision floating-point arithmetic.
121       setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
122       setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
123       setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
124       setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
125
126       // Double-precision floating-point arithmetic.
127       setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
128       setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
129       setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
130       setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
131
132       // Single-precision comparisons.
133       setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
134       setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
135       setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
136       setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
137       setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
138       setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
139       setLibcallName(RTLIB::UO_F32,  "__unordsf2vfp");
140       setLibcallName(RTLIB::O_F32,   "__unordsf2vfp");
141
142       setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
143       setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
144       setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
145       setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
146       setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
147       setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
148       setCmpLibcallCC(RTLIB::UO_F32,  ISD::SETNE);
149       setCmpLibcallCC(RTLIB::O_F32,   ISD::SETEQ);
150
151       // Double-precision comparisons.
152       setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
153       setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
154       setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
155       setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
156       setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
157       setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
158       setLibcallName(RTLIB::UO_F64,  "__unorddf2vfp");
159       setLibcallName(RTLIB::O_F64,   "__unorddf2vfp");
160
161       setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
162       setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
163       setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
164       setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
165       setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
166       setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
167       setCmpLibcallCC(RTLIB::UO_F64,  ISD::SETNE);
168       setCmpLibcallCC(RTLIB::O_F64,   ISD::SETEQ);
169
170       // Floating-point to integer conversions.
171       // i64 conversions are done via library routines even when generating VFP
172       // instructions, so use the same ones.
173       setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
174       setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
175       setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
176       setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
177
178       // Conversions between floating types.
179       setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
180       setLibcallName(RTLIB::FPEXT_F32_F64,   "__extendsfdf2vfp");
181
182       // Integer to floating-point conversions.
183       // i64 conversions are done via library routines even when generating VFP
184       // instructions, so use the same ones.
185       // FIXME: There appears to be some naming inconsistency in ARM libgcc:
186       // e.g., __floatunsidf vs. __floatunssidfvfp.
187       setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
188       setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
189       setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
190       setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
191     }
192   }
193
194   // These libcalls are not available in 32-bit.
195   setLibcallName(RTLIB::SHL_I128, 0);
196   setLibcallName(RTLIB::SRL_I128, 0);
197   setLibcallName(RTLIB::SRA_I128, 0);
198
199   if (Subtarget->isThumb1Only())
200     addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
201   else
202     addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
203   if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
204     addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
205     addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
206
207     setTruncStoreAction(MVT::f64, MVT::f32, Expand);
208   }
209
210   if (Subtarget->hasNEON()) {
211     addDRTypeForNEON(MVT::v2f32);
212     addDRTypeForNEON(MVT::v8i8);
213     addDRTypeForNEON(MVT::v4i16);
214     addDRTypeForNEON(MVT::v2i32);
215     addDRTypeForNEON(MVT::v1i64);
216
217     addQRTypeForNEON(MVT::v4f32);
218     addQRTypeForNEON(MVT::v2f64);
219     addQRTypeForNEON(MVT::v16i8);
220     addQRTypeForNEON(MVT::v8i16);
221     addQRTypeForNEON(MVT::v4i32);
222     addQRTypeForNEON(MVT::v2i64);
223
224     setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
225     setTargetDAGCombine(ISD::SHL);
226     setTargetDAGCombine(ISD::SRL);
227     setTargetDAGCombine(ISD::SRA);
228     setTargetDAGCombine(ISD::SIGN_EXTEND);
229     setTargetDAGCombine(ISD::ZERO_EXTEND);
230     setTargetDAGCombine(ISD::ANY_EXTEND);
231   }
232
233   computeRegisterProperties();
234
235   // ARM does not have f32 extending load.
236   setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
237
238   // ARM does not have i1 sign extending load.
239   setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
240
241   // ARM supports all 4 flavors of integer indexed load / store.
242   if (!Subtarget->isThumb1Only()) {
243     for (unsigned im = (unsigned)ISD::PRE_INC;
244          im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
245       setIndexedLoadAction(im,  MVT::i1,  Legal);
246       setIndexedLoadAction(im,  MVT::i8,  Legal);
247       setIndexedLoadAction(im,  MVT::i16, Legal);
248       setIndexedLoadAction(im,  MVT::i32, Legal);
249       setIndexedStoreAction(im, MVT::i1,  Legal);
250       setIndexedStoreAction(im, MVT::i8,  Legal);
251       setIndexedStoreAction(im, MVT::i16, Legal);
252       setIndexedStoreAction(im, MVT::i32, Legal);
253     }
254   }
255
256   // i64 operation support.
257   if (Subtarget->isThumb1Only()) {
258     setOperationAction(ISD::MUL,     MVT::i64, Expand);
259     setOperationAction(ISD::MULHU,   MVT::i32, Expand);
260     setOperationAction(ISD::MULHS,   MVT::i32, Expand);
261     setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
262     setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
263   } else {
264     setOperationAction(ISD::MUL,     MVT::i64, Expand);
265     setOperationAction(ISD::MULHU,   MVT::i32, Expand);
266     if (!Subtarget->hasV6Ops())
267       setOperationAction(ISD::MULHS, MVT::i32, Expand);
268   }
269   setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
270   setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
271   setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
272   setOperationAction(ISD::SRL,       MVT::i64, Custom);
273   setOperationAction(ISD::SRA,       MVT::i64, Custom);
274
275   // ARM does not have ROTL.
276   setOperationAction(ISD::ROTL,  MVT::i32, Expand);
277   setOperationAction(ISD::CTTZ,  MVT::i32, Expand);
278   setOperationAction(ISD::CTPOP, MVT::i32, Expand);
279   if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
280     setOperationAction(ISD::CTLZ, MVT::i32, Expand);
281
282   // Only ARMv6 has BSWAP.
283   if (!Subtarget->hasV6Ops())
284     setOperationAction(ISD::BSWAP, MVT::i32, Expand);
285
286   // These are expanded into libcalls.
287   setOperationAction(ISD::SDIV,  MVT::i32, Expand);
288   setOperationAction(ISD::UDIV,  MVT::i32, Expand);
289   setOperationAction(ISD::SREM,  MVT::i32, Expand);
290   setOperationAction(ISD::UREM,  MVT::i32, Expand);
291   setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
292   setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
293
294   // Support label based line numbers.
295   setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
296   setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
297
298   setOperationAction(ISD::RET,           MVT::Other, Custom);
299   setOperationAction(ISD::GlobalAddress, MVT::i32,   Custom);
300   setOperationAction(ISD::ConstantPool,  MVT::i32,   Custom);
301   setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
302   setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
303
304   // Use the default implementation.
305   setOperationAction(ISD::VASTART,            MVT::Other, Custom);
306   setOperationAction(ISD::VAARG,              MVT::Other, Expand);
307   setOperationAction(ISD::VACOPY,             MVT::Other, Expand);
308   setOperationAction(ISD::VAEND,              MVT::Other, Expand);
309   setOperationAction(ISD::STACKSAVE,          MVT::Other, Expand);
310   setOperationAction(ISD::STACKRESTORE,       MVT::Other, Expand);
311   setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32,   Expand);
312   setOperationAction(ISD::MEMBARRIER,         MVT::Other, Expand);
313
314   if (!Subtarget->hasV6Ops() && !Subtarget->isThumb2()) {
315     setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
316     setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8,  Expand);
317   }
318   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
319
320   if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only())
321     // Turn f64->i64 into FMRRD, i64 -> f64 to FMDRR iff target supports vfp2.
322     setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
323
324   // We want to custom lower some of our intrinsics.
325   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
326
327   setOperationAction(ISD::SETCC,     MVT::i32, Expand);
328   setOperationAction(ISD::SETCC,     MVT::f32, Expand);
329   setOperationAction(ISD::SETCC,     MVT::f64, Expand);
330   setOperationAction(ISD::SELECT,    MVT::i32, Expand);
331   setOperationAction(ISD::SELECT,    MVT::f32, Expand);
332   setOperationAction(ISD::SELECT,    MVT::f64, Expand);
333   setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
334   setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
335   setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
336
337   setOperationAction(ISD::BRCOND,    MVT::Other, Expand);
338   setOperationAction(ISD::BR_CC,     MVT::i32,   Custom);
339   setOperationAction(ISD::BR_CC,     MVT::f32,   Custom);
340   setOperationAction(ISD::BR_CC,     MVT::f64,   Custom);
341   setOperationAction(ISD::BR_JT,     MVT::Other, Custom);
342
343   // We don't support sin/cos/fmod/copysign/pow
344   setOperationAction(ISD::FSIN,      MVT::f64, Expand);
345   setOperationAction(ISD::FSIN,      MVT::f32, Expand);
346   setOperationAction(ISD::FCOS,      MVT::f32, Expand);
347   setOperationAction(ISD::FCOS,      MVT::f64, Expand);
348   setOperationAction(ISD::FREM,      MVT::f64, Expand);
349   setOperationAction(ISD::FREM,      MVT::f32, Expand);
350   if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
351     setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
352     setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
353   }
354   setOperationAction(ISD::FPOW,      MVT::f64, Expand);
355   setOperationAction(ISD::FPOW,      MVT::f32, Expand);
356
357   // int <-> fp are custom expanded into bit_convert + ARMISD ops.
358   if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
359     setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
360     setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
361     setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
362     setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
363   }
364
365   // We have target-specific dag combine patterns for the following nodes:
366   // ARMISD::FMRRD  - No need to call setTargetDAGCombine
367   setTargetDAGCombine(ISD::ADD);
368   setTargetDAGCombine(ISD::SUB);
369
370   setStackPointerRegisterToSaveRestore(ARM::SP);
371   setSchedulingPreference(SchedulingForRegPressure);
372   setIfCvtBlockSizeLimit(Subtarget->isThumb() ? 0 : 10);
373   setIfCvtDupBlockSizeLimit(Subtarget->isThumb() ? 0 : 2);
374
375   if (!Subtarget->isThumb()) {
376     // Use branch latency information to determine if-conversion limits.
377     // FIXME: If-converter should use instruction latency of the branch being
378     // eliminated to compute the threshold. For ARMv6, the branch "latency"
379     // varies depending on whether it's dynamically or statically predicted
380     // and on whether the destination is in the prefetch buffer.
381     const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
382     const InstrItineraryData &InstrItins = Subtarget->getInstrItineraryData();
383     unsigned Latency= InstrItins.getLatency(TII->get(ARM::Bcc).getSchedClass());
384     if (Latency > 1) {
385       setIfCvtBlockSizeLimit(Latency-1);
386       if (Latency > 2)
387         setIfCvtDupBlockSizeLimit(Latency-2);
388     } else {
389       setIfCvtBlockSizeLimit(10);
390       setIfCvtDupBlockSizeLimit(2);
391     }
392   }
393
394   maxStoresPerMemcpy = 1;   //// temporary - rewrite interface to use type
395   // Do not enable CodePlacementOpt for now: it currently runs after the
396   // ARMConstantIslandPass and messes up branch relaxation and placement
397   // of constant islands.
398   // benefitFromCodePlacementOpt = true;
399 }
400
401 const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
402   switch (Opcode) {
403   default: return 0;
404   case ARMISD::Wrapper:       return "ARMISD::Wrapper";
405   case ARMISD::WrapperJT:     return "ARMISD::WrapperJT";
406   case ARMISD::CALL:          return "ARMISD::CALL";
407   case ARMISD::CALL_PRED:     return "ARMISD::CALL_PRED";
408   case ARMISD::CALL_NOLINK:   return "ARMISD::CALL_NOLINK";
409   case ARMISD::tCALL:         return "ARMISD::tCALL";
410   case ARMISD::BRCOND:        return "ARMISD::BRCOND";
411   case ARMISD::BR_JT:         return "ARMISD::BR_JT";
412   case ARMISD::BR2_JT:        return "ARMISD::BR2_JT";
413   case ARMISD::RET_FLAG:      return "ARMISD::RET_FLAG";
414   case ARMISD::PIC_ADD:       return "ARMISD::PIC_ADD";
415   case ARMISD::CMP:           return "ARMISD::CMP";
416   case ARMISD::CMPZ:          return "ARMISD::CMPZ";
417   case ARMISD::CMPFP:         return "ARMISD::CMPFP";
418   case ARMISD::CMPFPw0:       return "ARMISD::CMPFPw0";
419   case ARMISD::FMSTAT:        return "ARMISD::FMSTAT";
420   case ARMISD::CMOV:          return "ARMISD::CMOV";
421   case ARMISD::CNEG:          return "ARMISD::CNEG";
422
423   case ARMISD::FTOSI:         return "ARMISD::FTOSI";
424   case ARMISD::FTOUI:         return "ARMISD::FTOUI";
425   case ARMISD::SITOF:         return "ARMISD::SITOF";
426   case ARMISD::UITOF:         return "ARMISD::UITOF";
427
428   case ARMISD::SRL_FLAG:      return "ARMISD::SRL_FLAG";
429   case ARMISD::SRA_FLAG:      return "ARMISD::SRA_FLAG";
430   case ARMISD::RRX:           return "ARMISD::RRX";
431
432   case ARMISD::FMRRD:         return "ARMISD::FMRRD";
433   case ARMISD::FMDRR:         return "ARMISD::FMDRR";
434
435   case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
436
437   case ARMISD::VCEQ:          return "ARMISD::VCEQ";
438   case ARMISD::VCGE:          return "ARMISD::VCGE";
439   case ARMISD::VCGEU:         return "ARMISD::VCGEU";
440   case ARMISD::VCGT:          return "ARMISD::VCGT";
441   case ARMISD::VCGTU:         return "ARMISD::VCGTU";
442   case ARMISD::VTST:          return "ARMISD::VTST";
443
444   case ARMISD::VSHL:          return "ARMISD::VSHL";
445   case ARMISD::VSHRs:         return "ARMISD::VSHRs";
446   case ARMISD::VSHRu:         return "ARMISD::VSHRu";
447   case ARMISD::VSHLLs:        return "ARMISD::VSHLLs";
448   case ARMISD::VSHLLu:        return "ARMISD::VSHLLu";
449   case ARMISD::VSHLLi:        return "ARMISD::VSHLLi";
450   case ARMISD::VSHRN:         return "ARMISD::VSHRN";
451   case ARMISD::VRSHRs:        return "ARMISD::VRSHRs";
452   case ARMISD::VRSHRu:        return "ARMISD::VRSHRu";
453   case ARMISD::VRSHRN:        return "ARMISD::VRSHRN";
454   case ARMISD::VQSHLs:        return "ARMISD::VQSHLs";
455   case ARMISD::VQSHLu:        return "ARMISD::VQSHLu";
456   case ARMISD::VQSHLsu:       return "ARMISD::VQSHLsu";
457   case ARMISD::VQSHRNs:       return "ARMISD::VQSHRNs";
458   case ARMISD::VQSHRNu:       return "ARMISD::VQSHRNu";
459   case ARMISD::VQSHRNsu:      return "ARMISD::VQSHRNsu";
460   case ARMISD::VQRSHRNs:      return "ARMISD::VQRSHRNs";
461   case ARMISD::VQRSHRNu:      return "ARMISD::VQRSHRNu";
462   case ARMISD::VQRSHRNsu:     return "ARMISD::VQRSHRNsu";
463   case ARMISD::VGETLANEu:     return "ARMISD::VGETLANEu";
464   case ARMISD::VGETLANEs:     return "ARMISD::VGETLANEs";
465   case ARMISD::VDUPLANEQ:     return "ARMISD::VDUPLANEQ";
466   }
467 }
468
469 /// getFunctionAlignment - Return the Log2 alignment of this function.
470 unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
471   return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 1 : 2;
472 }
473
474 //===----------------------------------------------------------------------===//
475 // Lowering Code
476 //===----------------------------------------------------------------------===//
477
478 /// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
479 static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
480   switch (CC) {
481   default: llvm_unreachable("Unknown condition code!");
482   case ISD::SETNE:  return ARMCC::NE;
483   case ISD::SETEQ:  return ARMCC::EQ;
484   case ISD::SETGT:  return ARMCC::GT;
485   case ISD::SETGE:  return ARMCC::GE;
486   case ISD::SETLT:  return ARMCC::LT;
487   case ISD::SETLE:  return ARMCC::LE;
488   case ISD::SETUGT: return ARMCC::HI;
489   case ISD::SETUGE: return ARMCC::HS;
490   case ISD::SETULT: return ARMCC::LO;
491   case ISD::SETULE: return ARMCC::LS;
492   }
493 }
494
495 /// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC. It
496 /// returns true if the operands should be inverted to form the proper
497 /// comparison.
498 static bool FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
499                         ARMCC::CondCodes &CondCode2) {
500   bool Invert = false;
501   CondCode2 = ARMCC::AL;
502   switch (CC) {
503   default: llvm_unreachable("Unknown FP condition!");
504   case ISD::SETEQ:
505   case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
506   case ISD::SETGT:
507   case ISD::SETOGT: CondCode = ARMCC::GT; break;
508   case ISD::SETGE:
509   case ISD::SETOGE: CondCode = ARMCC::GE; break;
510   case ISD::SETOLT: CondCode = ARMCC::MI; break;
511   case ISD::SETOLE: CondCode = ARMCC::GT; Invert = true; break;
512   case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
513   case ISD::SETO:   CondCode = ARMCC::VC; break;
514   case ISD::SETUO:  CondCode = ARMCC::VS; break;
515   case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
516   case ISD::SETUGT: CondCode = ARMCC::HI; break;
517   case ISD::SETUGE: CondCode = ARMCC::PL; break;
518   case ISD::SETLT:
519   case ISD::SETULT: CondCode = ARMCC::LT; break;
520   case ISD::SETLE:
521   case ISD::SETULE: CondCode = ARMCC::LE; break;
522   case ISD::SETNE:
523   case ISD::SETUNE: CondCode = ARMCC::NE; break;
524   }
525   return Invert;
526 }
527
528 //===----------------------------------------------------------------------===//
529 //                      Calling Convention Implementation
530 //
531 //  The lower operations present on calling convention works on this order:
532 //      LowerCALL (virt regs --> phys regs, virt regs --> stack)
533 //      LowerFORMAL_ARGUMENTS (phys --> virt regs, stack --> virt regs)
534 //      LowerRET (virt regs --> phys regs)
535 //      LowerCALL (phys regs --> virt regs)
536 //
537 //===----------------------------------------------------------------------===//
538
539 #include "ARMGenCallingConv.inc"
540
541 // APCS f64 is in register pairs, possibly split to stack
542 static bool f64AssignAPCS(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
543                           CCValAssign::LocInfo &LocInfo,
544                           CCState &State, bool CanFail) {
545   static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
546
547   // Try to get the first register.
548   if (unsigned Reg = State.AllocateReg(RegList, 4))
549     State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
550   else {
551     // For the 2nd half of a v2f64, do not fail.
552     if (CanFail)
553       return false;
554
555     // Put the whole thing on the stack.
556     State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
557                                            State.AllocateStack(8, 4),
558                                            LocVT, LocInfo));
559     return true;
560   }
561
562   // Try to get the second register.
563   if (unsigned Reg = State.AllocateReg(RegList, 4))
564     State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
565   else
566     State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
567                                            State.AllocateStack(4, 4),
568                                            LocVT, LocInfo));
569   return true;
570 }
571
572 static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
573                                    CCValAssign::LocInfo &LocInfo,
574                                    ISD::ArgFlagsTy &ArgFlags,
575                                    CCState &State) {
576   if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
577     return false;
578   if (LocVT == MVT::v2f64 &&
579       !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
580     return false;
581   return true;  // we handled it
582 }
583
584 // AAPCS f64 is in aligned register pairs
585 static bool f64AssignAAPCS(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
586                            CCValAssign::LocInfo &LocInfo,
587                            CCState &State, bool CanFail) {
588   static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
589   static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
590
591   unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
592   if (Reg == 0) {
593     // For the 2nd half of a v2f64, do not just fail.
594     if (CanFail)
595       return false;
596
597     // Put the whole thing on the stack.
598     State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
599                                            State.AllocateStack(8, 8),
600                                            LocVT, LocInfo));
601     return true;
602   }
603
604   unsigned i;
605   for (i = 0; i < 2; ++i)
606     if (HiRegList[i] == Reg)
607       break;
608
609   State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
610   State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
611                                          LocVT, LocInfo));
612   return true;
613 }
614
615 static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
616                                     CCValAssign::LocInfo &LocInfo,
617                                     ISD::ArgFlagsTy &ArgFlags,
618                                     CCState &State) {
619   if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
620     return false;
621   if (LocVT == MVT::v2f64 &&
622       !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
623     return false;
624   return true;  // we handled it
625 }
626
627 static bool f64RetAssign(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
628                          CCValAssign::LocInfo &LocInfo, CCState &State) {
629   static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
630   static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
631
632   unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
633   if (Reg == 0)
634     return false; // we didn't handle it
635
636   unsigned i;
637   for (i = 0; i < 2; ++i)
638     if (HiRegList[i] == Reg)
639       break;
640
641   State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
642   State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
643                                          LocVT, LocInfo));
644   return true;
645 }
646
647 static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
648                                       CCValAssign::LocInfo &LocInfo,
649                                       ISD::ArgFlagsTy &ArgFlags,
650                                       CCState &State) {
651   if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
652     return false;
653   if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
654     return false;
655   return true;  // we handled it
656 }
657
658 static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
659                                        CCValAssign::LocInfo &LocInfo,
660                                        ISD::ArgFlagsTy &ArgFlags,
661                                        CCState &State) {
662   return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
663                                    State);
664 }
665
666 /// CCAssignFnForNode - Selects the correct CCAssignFn for a the
667 /// given CallingConvention value.
668 CCAssignFn *ARMTargetLowering::CCAssignFnForNode(unsigned CC,
669                                                  bool Return) const {
670   switch (CC) {
671   default:
672    llvm_unreachable("Unsupported calling convention");
673   case CallingConv::C:
674   case CallingConv::Fast:
675    // Use target triple & subtarget features to do actual dispatch.
676    if (Subtarget->isAAPCS_ABI()) {
677      if (Subtarget->hasVFP2() &&
678          FloatABIType == FloatABI::Hard)
679        return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
680      else
681        return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
682    } else
683      return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
684   case CallingConv::ARM_AAPCS_VFP:
685    return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
686   case CallingConv::ARM_AAPCS:
687    return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
688   case CallingConv::ARM_APCS:
689    return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
690   }
691 }
692
693 /// LowerCallResult - Lower the result values of an ISD::CALL into the
694 /// appropriate copies out of appropriate physical registers.  This assumes that
695 /// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
696 /// being lowered.  The returns a SDNode with the same number of values as the
697 /// ISD::CALL.
698 SDNode *ARMTargetLowering::
699 LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
700                 unsigned CallingConv, SelectionDAG &DAG) {
701
702   DebugLoc dl = TheCall->getDebugLoc();
703   // Assign locations to each value returned by this call.
704   SmallVector<CCValAssign, 16> RVLocs;
705   bool isVarArg = TheCall->isVarArg();
706   CCState CCInfo(CallingConv, isVarArg, getTargetMachine(),
707                  RVLocs, *DAG.getContext());
708   CCInfo.AnalyzeCallResult(TheCall,
709                            CCAssignFnForNode(CallingConv, /* Return*/ true));
710
711   SmallVector<SDValue, 8> ResultVals;
712
713   // Copy all of the result registers out of their specified physreg.
714   for (unsigned i = 0; i != RVLocs.size(); ++i) {
715     CCValAssign VA = RVLocs[i];
716
717     SDValue Val;
718     if (VA.needsCustom()) {
719       // Handle f64 or half of a v2f64.
720       SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
721                                       InFlag);
722       Chain = Lo.getValue(1);
723       InFlag = Lo.getValue(2);
724       VA = RVLocs[++i]; // skip ahead to next loc
725       SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
726                                       InFlag);
727       Chain = Hi.getValue(1);
728       InFlag = Hi.getValue(2);
729       Val = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
730
731       if (VA.getLocVT() == MVT::v2f64) {
732         SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
733         Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
734                           DAG.getConstant(0, MVT::i32));
735
736         VA = RVLocs[++i]; // skip ahead to next loc
737         Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
738         Chain = Lo.getValue(1);
739         InFlag = Lo.getValue(2);
740         VA = RVLocs[++i]; // skip ahead to next loc
741         Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
742         Chain = Hi.getValue(1);
743         InFlag = Hi.getValue(2);
744         Val = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
745         Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
746                           DAG.getConstant(1, MVT::i32));
747       }
748     } else {
749       Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
750                                InFlag);
751       Chain = Val.getValue(1);
752       InFlag = Val.getValue(2);
753     }
754
755     switch (VA.getLocInfo()) {
756     default: llvm_unreachable("Unknown loc info!");
757     case CCValAssign::Full: break;
758     case CCValAssign::BCvt:
759       Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
760       break;
761     }
762
763     ResultVals.push_back(Val);
764   }
765
766   // Merge everything together with a MERGE_VALUES node.
767   ResultVals.push_back(Chain);
768   return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
769                      &ResultVals[0], ResultVals.size()).getNode();
770 }
771
772 /// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
773 /// by "Src" to address "Dst" of size "Size".  Alignment information is
774 /// specified by the specific parameter attribute.  The copy will be passed as
775 /// a byval function parameter.
776 /// Sometimes what we are copying is the end of a larger object, the part that
777 /// does not fit in registers.
778 static SDValue
779 CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
780                           ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
781                           DebugLoc dl) {
782   SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
783   return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
784                        /*AlwaysInline=*/false, NULL, 0, NULL, 0);
785 }
786
787 /// LowerMemOpCallTo - Store the argument to the stack.
788 SDValue
789 ARMTargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG,
790                                     const SDValue &StackPtr,
791                                     const CCValAssign &VA, SDValue Chain,
792                                     SDValue Arg, ISD::ArgFlagsTy Flags) {
793   DebugLoc dl = TheCall->getDebugLoc();
794   unsigned LocMemOffset = VA.getLocMemOffset();
795   SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
796   PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
797   if (Flags.isByVal()) {
798     return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
799   }
800   return DAG.getStore(Chain, dl, Arg, PtrOff,
801                       PseudoSourceValue::getStack(), LocMemOffset);
802 }
803
804 void ARMTargetLowering::PassF64ArgInRegs(CallSDNode *TheCall, SelectionDAG &DAG,
805                                          SDValue Chain, SDValue &Arg,
806                                          RegsToPassVector &RegsToPass,
807                                          CCValAssign &VA, CCValAssign &NextVA,
808                                          SDValue &StackPtr,
809                                          SmallVector<SDValue, 8> &MemOpChains,
810                                          ISD::ArgFlagsTy Flags) {
811   DebugLoc dl = TheCall->getDebugLoc();
812
813   SDValue fmrrd = DAG.getNode(ARMISD::FMRRD, dl,
814                               DAG.getVTList(MVT::i32, MVT::i32), Arg);
815   RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
816
817   if (NextVA.isRegLoc())
818     RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
819   else {
820     assert(NextVA.isMemLoc());
821     if (StackPtr.getNode() == 0)
822       StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
823
824     MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, NextVA,
825                                            Chain, fmrrd.getValue(1), Flags));
826   }
827 }
828
829 /// LowerCALL - Lowering a ISD::CALL node into a callseq_start <-
830 /// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
831 /// nodes.
832 SDValue ARMTargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
833   CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
834   MVT RetVT           = TheCall->getRetValType(0);
835   SDValue Chain       = TheCall->getChain();
836   unsigned CC         = TheCall->getCallingConv();
837   bool isVarArg       = TheCall->isVarArg();
838   SDValue Callee      = TheCall->getCallee();
839   DebugLoc dl         = TheCall->getDebugLoc();
840
841   // Analyze operands of the call, assigning locations to each operand.
842   SmallVector<CCValAssign, 16> ArgLocs;
843   CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, *DAG.getContext());
844   CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC, /* Return*/ false));
845
846   // Get a count of how many bytes are to be pushed on the stack.
847   unsigned NumBytes = CCInfo.getNextStackOffset();
848
849   // Adjust the stack pointer for the new arguments...
850   // These operations are automatically eliminated by the prolog/epilog pass
851   Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
852
853   SDValue StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
854
855   RegsToPassVector RegsToPass;
856   SmallVector<SDValue, 8> MemOpChains;
857
858   // Walk the register/memloc assignments, inserting copies/loads.  In the case
859   // of tail call optimization, arguments are handled later.
860   for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
861        i != e;
862        ++i, ++realArgIdx) {
863     CCValAssign &VA = ArgLocs[i];
864     SDValue Arg = TheCall->getArg(realArgIdx);
865     ISD::ArgFlagsTy Flags = TheCall->getArgFlags(realArgIdx);
866
867     // Promote the value if needed.
868     switch (VA.getLocInfo()) {
869     default: llvm_unreachable("Unknown loc info!");
870     case CCValAssign::Full: break;
871     case CCValAssign::SExt:
872       Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
873       break;
874     case CCValAssign::ZExt:
875       Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
876       break;
877     case CCValAssign::AExt:
878       Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
879       break;
880     case CCValAssign::BCvt:
881       Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
882       break;
883     }
884
885     // f64 and v2f64 are passed in i32 pairs and must be split into pieces
886     if (VA.needsCustom()) {
887       if (VA.getLocVT() == MVT::v2f64) {
888         SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
889                                   DAG.getConstant(0, MVT::i32));
890         SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
891                                   DAG.getConstant(1, MVT::i32));
892
893         PassF64ArgInRegs(TheCall, DAG, Chain, Op0, RegsToPass,
894                          VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
895
896         VA = ArgLocs[++i]; // skip ahead to next loc
897         if (VA.isRegLoc()) {
898           PassF64ArgInRegs(TheCall, DAG, Chain, Op1, RegsToPass,
899                            VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
900         } else {
901           assert(VA.isMemLoc());
902           if (StackPtr.getNode() == 0)
903             StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
904
905           MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
906                                                  Chain, Op1, Flags));
907         }
908       } else {
909         PassF64ArgInRegs(TheCall, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
910                          StackPtr, MemOpChains, Flags);
911       }
912     } else if (VA.isRegLoc()) {
913       RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
914     } else {
915       assert(VA.isMemLoc());
916       if (StackPtr.getNode() == 0)
917         StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
918
919       MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
920                                              Chain, Arg, Flags));
921     }
922   }
923
924   if (!MemOpChains.empty())
925     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
926                         &MemOpChains[0], MemOpChains.size());
927
928   // Build a sequence of copy-to-reg nodes chained together with token chain
929   // and flag operands which copy the outgoing args into the appropriate regs.
930   SDValue InFlag;
931   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
932     Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
933                              RegsToPass[i].second, InFlag);
934     InFlag = Chain.getValue(1);
935   }
936
937   // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
938   // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
939   // node so that legalize doesn't hack it.
940   bool isDirect = false;
941   bool isARMFunc = false;
942   bool isLocalARMFunc = false;
943   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
944     GlobalValue *GV = G->getGlobal();
945     isDirect = true;
946     bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
947     bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
948                    getTargetMachine().getRelocationModel() != Reloc::Static;
949     isARMFunc = !Subtarget->isThumb() || isStub;
950     // ARM call to a local ARM function is predicable.
951     isLocalARMFunc = !Subtarget->isThumb() && !isExt;
952     // tBX takes a register source operand.
953     if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
954       ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
955                                                            ARMCP::CPStub, 4);
956       SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
957       CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
958       Callee = DAG.getLoad(getPointerTy(), dl,
959                            DAG.getEntryNode(), CPAddr, NULL, 0);
960       SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
961       Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
962                            getPointerTy(), Callee, PICLabel);
963    } else
964       Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
965   } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
966     isDirect = true;
967     bool isStub = Subtarget->isTargetDarwin() &&
968                   getTargetMachine().getRelocationModel() != Reloc::Static;
969     isARMFunc = !Subtarget->isThumb() || isStub;
970     // tBX takes a register source operand.
971     const char *Sym = S->getSymbol();
972     if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
973       ARMConstantPoolValue *CPV = new ARMConstantPoolValue(Sym, ARMPCLabelIndex,
974                                                            ARMCP::CPStub, 4);
975       SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
976       CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
977       Callee = DAG.getLoad(getPointerTy(), dl,
978                            DAG.getEntryNode(), CPAddr, NULL, 0);
979       SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
980       Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
981                            getPointerTy(), Callee, PICLabel);
982     } else
983       Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
984   }
985
986   // FIXME: handle tail calls differently.
987   unsigned CallOpc;
988   if (Subtarget->isThumb()) {
989     if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
990       CallOpc = ARMISD::CALL_NOLINK;
991     else
992       CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
993   } else {
994     CallOpc = (isDirect || Subtarget->hasV5TOps())
995       ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
996       : ARMISD::CALL_NOLINK;
997   }
998   if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
999     // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
1000     Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
1001     InFlag = Chain.getValue(1);
1002   }
1003
1004   std::vector<SDValue> Ops;
1005   Ops.push_back(Chain);
1006   Ops.push_back(Callee);
1007
1008   // Add argument registers to the end of the list so that they are known live
1009   // into the call.
1010   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1011     Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1012                                   RegsToPass[i].second.getValueType()));
1013
1014   if (InFlag.getNode())
1015     Ops.push_back(InFlag);
1016   // Returns a chain and a flag for retval copy to use.
1017   Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
1018                       &Ops[0], Ops.size());
1019   InFlag = Chain.getValue(1);
1020
1021   Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1022                              DAG.getIntPtrConstant(0, true), InFlag);
1023   if (RetVT != MVT::Other)
1024     InFlag = Chain.getValue(1);
1025
1026   // Handle result values, copying them out of physregs into vregs that we
1027   // return.
1028   return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
1029                                  Op.getResNo());
1030 }
1031
1032 SDValue ARMTargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
1033   // The chain is always operand #0
1034   SDValue Chain = Op.getOperand(0);
1035   DebugLoc dl = Op.getDebugLoc();
1036
1037   // CCValAssign - represent the assignment of the return value to a location.
1038   SmallVector<CCValAssign, 16> RVLocs;
1039   unsigned CC   = DAG.getMachineFunction().getFunction()->getCallingConv();
1040   bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
1041
1042   // CCState - Info about the registers and stack slots.
1043   CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs, *DAG.getContext());
1044
1045   // Analyze return values of ISD::RET.
1046   CCInfo.AnalyzeReturn(Op.getNode(), CCAssignFnForNode(CC, /* Return */ true));
1047
1048   // If this is the first return lowered for this function, add
1049   // the regs to the liveout set for the function.
1050   if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1051     for (unsigned i = 0; i != RVLocs.size(); ++i)
1052       if (RVLocs[i].isRegLoc())
1053         DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
1054   }
1055
1056   SDValue Flag;
1057
1058   // Copy the result values into the output registers.
1059   for (unsigned i = 0, realRVLocIdx = 0;
1060        i != RVLocs.size();
1061        ++i, ++realRVLocIdx) {
1062     CCValAssign &VA = RVLocs[i];
1063     assert(VA.isRegLoc() && "Can only return in registers!");
1064
1065     // ISD::RET => ret chain, (regnum1,val1), ...
1066     // So i*2+1 index only the regnums
1067     SDValue Arg = Op.getOperand(realRVLocIdx*2+1);
1068
1069     switch (VA.getLocInfo()) {
1070     default: llvm_unreachable("Unknown loc info!");
1071     case CCValAssign::Full: break;
1072     case CCValAssign::BCvt:
1073       Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1074       break;
1075     }
1076
1077     if (VA.needsCustom()) {
1078       if (VA.getLocVT() == MVT::v2f64) {
1079         // Extract the first half and return it in two registers.
1080         SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1081                                    DAG.getConstant(0, MVT::i32));
1082         SDValue HalfGPRs = DAG.getNode(ARMISD::FMRRD, dl,
1083                                        DAG.getVTList(MVT::i32, MVT::i32), Half);
1084
1085         Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1086         Flag = Chain.getValue(1);
1087         VA = RVLocs[++i]; // skip ahead to next loc
1088         Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1089                                  HalfGPRs.getValue(1), Flag);
1090         Flag = Chain.getValue(1);
1091         VA = RVLocs[++i]; // skip ahead to next loc
1092
1093         // Extract the 2nd half and fall through to handle it as an f64 value.
1094         Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1095                           DAG.getConstant(1, MVT::i32));
1096       }
1097       // Legalize ret f64 -> ret 2 x i32.  We always have fmrrd if f64 is
1098       // available.
1099       SDValue fmrrd = DAG.getNode(ARMISD::FMRRD, dl,
1100                                   DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
1101       Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
1102       Flag = Chain.getValue(1);
1103       VA = RVLocs[++i]; // skip ahead to next loc
1104       Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1105                                Flag);
1106     } else
1107       Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1108
1109     // Guarantee that all emitted copies are
1110     // stuck together, avoiding something bad.
1111     Flag = Chain.getValue(1);
1112   }
1113
1114   SDValue result;
1115   if (Flag.getNode())
1116     result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
1117   else // Return Void
1118     result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
1119
1120   return result;
1121 }
1122
1123 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1124 // their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1125 // one of the above mentioned nodes. It has to be wrapped because otherwise
1126 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1127 // be used to form addressing mode. These wrapped nodes will be selected
1128 // into MOVi.
1129 static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
1130   MVT PtrVT = Op.getValueType();
1131   // FIXME there is no actual debug info here
1132   DebugLoc dl = Op.getDebugLoc();
1133   ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
1134   SDValue Res;
1135   if (CP->isMachineConstantPoolEntry())
1136     Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1137                                     CP->getAlignment());
1138   else
1139     Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1140                                     CP->getAlignment());
1141   return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
1142 }
1143
1144 // Lower ISD::GlobalTLSAddress using the "general dynamic" model
1145 SDValue
1146 ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1147                                                  SelectionDAG &DAG) {
1148   DebugLoc dl = GA->getDebugLoc();
1149   MVT PtrVT = getPointerTy();
1150   unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1151   ARMConstantPoolValue *CPV =
1152     new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue,
1153                              PCAdj, "tlsgd", true);
1154   SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1155   Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
1156   Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument, NULL, 0);
1157   SDValue Chain = Argument.getValue(1);
1158
1159   SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
1160   Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
1161
1162   // call __tls_get_addr.
1163   ArgListTy Args;
1164   ArgListEntry Entry;
1165   Entry.Node = Argument;
1166   Entry.Ty = (const Type *) Type::Int32Ty;
1167   Args.push_back(Entry);
1168   // FIXME: is there useful debug info available here?
1169   std::pair<SDValue, SDValue> CallResult =
1170     LowerCallTo(Chain, (const Type *) Type::Int32Ty, false, false, false, false,
1171                 0, CallingConv::C, false,
1172                 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
1173   return CallResult.first;
1174 }
1175
1176 // Lower ISD::GlobalTLSAddress using the "initial exec" or
1177 // "local exec" model.
1178 SDValue
1179 ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
1180                                         SelectionDAG &DAG) {
1181   GlobalValue *GV = GA->getGlobal();
1182   DebugLoc dl = GA->getDebugLoc();
1183   SDValue Offset;
1184   SDValue Chain = DAG.getEntryNode();
1185   MVT PtrVT = getPointerTy();
1186   // Get the Thread Pointer
1187   SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1188
1189   if (GV->isDeclaration()) {
1190     // initial exec model
1191     unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1192     ARMConstantPoolValue *CPV =
1193       new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue,
1194                                PCAdj, "gottpoff", true);
1195     Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1196     Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
1197     Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
1198     Chain = Offset.getValue(1);
1199
1200     SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
1201     Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
1202
1203     Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
1204   } else {
1205     // local exec model
1206     ARMConstantPoolValue *CPV =
1207       new ARMConstantPoolValue(GV, ARMCP::CPValue, "tpoff");
1208     Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1209     Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
1210     Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
1211   }
1212
1213   // The address of the thread local variable is the add of the thread
1214   // pointer with the offset of the variable.
1215   return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
1216 }
1217
1218 SDValue
1219 ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
1220   // TODO: implement the "local dynamic" model
1221   assert(Subtarget->isTargetELF() &&
1222          "TLS not implemented for non-ELF targets");
1223   GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1224   // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1225   // otherwise use the "Local Exec" TLS Model
1226   if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1227     return LowerToTLSGeneralDynamicModel(GA, DAG);
1228   else
1229     return LowerToTLSExecModels(GA, DAG);
1230 }
1231
1232 SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
1233                                                  SelectionDAG &DAG) {
1234   MVT PtrVT = getPointerTy();
1235   DebugLoc dl = Op.getDebugLoc();
1236   GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1237   Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1238   if (RelocM == Reloc::PIC_) {
1239     bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
1240     ARMConstantPoolValue *CPV =
1241       new ARMConstantPoolValue(GV, ARMCP::CPValue, UseGOTOFF ? "GOTOFF":"GOT");
1242     SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1243     CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1244     SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
1245                                  CPAddr, NULL, 0);
1246     SDValue Chain = Result.getValue(1);
1247     SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
1248     Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
1249     if (!UseGOTOFF)
1250       Result = DAG.getLoad(PtrVT, dl, Chain, Result, NULL, 0);
1251     return Result;
1252   } else {
1253     SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
1254     CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1255     return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
1256   }
1257 }
1258
1259 /// GVIsIndirectSymbol - true if the GV will be accessed via an indirect symbol
1260 /// even in non-static mode.
1261 static bool GVIsIndirectSymbol(GlobalValue *GV, Reloc::Model RelocM) {
1262   // If symbol visibility is hidden, the extra load is not needed if
1263   // the symbol is definitely defined in the current translation unit.
1264   bool isDecl = GV->isDeclaration() || GV->hasAvailableExternallyLinkage();
1265   if (GV->hasHiddenVisibility() && (!isDecl && !GV->hasCommonLinkage()))
1266     return false;
1267   return RelocM != Reloc::Static && (isDecl || GV->isWeakForLinker());
1268 }
1269
1270 SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
1271                                                     SelectionDAG &DAG) {
1272   MVT PtrVT = getPointerTy();
1273   DebugLoc dl = Op.getDebugLoc();
1274   GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1275   Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1276   bool IsIndirect = GVIsIndirectSymbol(GV, RelocM);
1277   SDValue CPAddr;
1278   if (RelocM == Reloc::Static)
1279     CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
1280   else {
1281     unsigned PCAdj = (RelocM != Reloc::PIC_)
1282       ? 0 : (Subtarget->isThumb() ? 4 : 8);
1283     ARMCP::ARMCPKind Kind = IsIndirect ? ARMCP::CPNonLazyPtr
1284       : ARMCP::CPValue;
1285     ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
1286                                                          Kind, PCAdj);
1287     CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1288   }
1289   CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1290
1291   SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
1292   SDValue Chain = Result.getValue(1);
1293
1294   if (RelocM == Reloc::PIC_) {
1295     SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
1296     Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1297   }
1298   if (IsIndirect)
1299     Result = DAG.getLoad(PtrVT, dl, Chain, Result, NULL, 0);
1300
1301   return Result;
1302 }
1303
1304 SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
1305                                                     SelectionDAG &DAG){
1306   assert(Subtarget->isTargetELF() &&
1307          "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
1308   MVT PtrVT = getPointerTy();
1309   DebugLoc dl = Op.getDebugLoc();
1310   unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
1311   ARMConstantPoolValue *CPV = new ARMConstantPoolValue("_GLOBAL_OFFSET_TABLE_",
1312                                                        ARMPCLabelIndex,
1313                                                        ARMCP::CPValue, PCAdj);
1314   SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1315   CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1316   SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
1317   SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
1318   return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1319 }
1320
1321 SDValue
1322 ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
1323   MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1324   unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1325   DebugLoc dl = Op.getDebugLoc();
1326   switch (IntNo) {
1327   default: return SDValue();    // Don't custom lower most intrinsics.
1328   case Intrinsic::arm_thread_pointer:
1329       return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1330   case Intrinsic::eh_sjlj_setjmp:
1331       SDValue Res = DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32,
1332                          Op.getOperand(1));
1333       return Res;
1334   }
1335 }
1336
1337 static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
1338                             unsigned VarArgsFrameIndex) {
1339   // vastart just stores the address of the VarArgsFrameIndex slot into the
1340   // memory location argument.
1341   DebugLoc dl = Op.getDebugLoc();
1342   MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1343   SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
1344   const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
1345   return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
1346 }
1347
1348 SDValue
1349 ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1350                                         SDValue &Root, SelectionDAG &DAG,
1351                                         DebugLoc dl) {
1352   MachineFunction &MF = DAG.getMachineFunction();
1353   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1354
1355   TargetRegisterClass *RC;
1356   if (AFI->isThumb1OnlyFunction())
1357     RC = ARM::tGPRRegisterClass;
1358   else
1359     RC = ARM::GPRRegisterClass;
1360
1361   // Transform the arguments stored in physical registers into virtual ones.
1362   unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
1363   SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
1364
1365   SDValue ArgValue2;
1366   if (NextVA.isMemLoc()) {
1367     unsigned ArgSize = NextVA.getLocVT().getSizeInBits()/8;
1368     MachineFrameInfo *MFI = MF.getFrameInfo();
1369     int FI = MFI->CreateFixedObject(ArgSize, NextVA.getLocMemOffset());
1370
1371     // Create load node to retrieve arguments from the stack.
1372     SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1373     ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN, NULL, 0);
1374   } else {
1375     Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
1376     ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
1377   }
1378
1379   return DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, ArgValue, ArgValue2);
1380 }
1381
1382 SDValue
1383 ARMTargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
1384   MachineFunction &MF = DAG.getMachineFunction();
1385   MachineFrameInfo *MFI = MF.getFrameInfo();
1386
1387   SDValue Root = Op.getOperand(0);
1388   DebugLoc dl = Op.getDebugLoc();
1389   bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
1390   unsigned CC = MF.getFunction()->getCallingConv();
1391   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1392
1393   // Assign locations to all of the incoming arguments.
1394   SmallVector<CCValAssign, 16> ArgLocs;
1395   CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, *DAG.getContext());
1396   CCInfo.AnalyzeFormalArguments(Op.getNode(),
1397                                 CCAssignFnForNode(CC, /* Return*/ false));
1398
1399   SmallVector<SDValue, 16> ArgValues;
1400
1401   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1402     CCValAssign &VA = ArgLocs[i];
1403
1404     // Arguments stored in registers.
1405     if (VA.isRegLoc()) {
1406       MVT RegVT = VA.getLocVT();
1407
1408       SDValue ArgValue;
1409       if (VA.needsCustom()) {
1410         // f64 and vector types are split up into multiple registers or
1411         // combinations of registers and stack slots.
1412         RegVT = MVT::i32;
1413
1414         if (VA.getLocVT() == MVT::v2f64) {
1415           SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
1416                                                    Root, DAG, dl);
1417           VA = ArgLocs[++i]; // skip ahead to next loc
1418           SDValue ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
1419                                                    Root, DAG, dl);
1420           ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1421           ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
1422                                  ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
1423           ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
1424                                  ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
1425         } else
1426           ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Root, DAG, dl);
1427
1428       } else {
1429         TargetRegisterClass *RC;
1430         if (FloatABIType == FloatABI::Hard && RegVT == MVT::f32)
1431           RC = ARM::SPRRegisterClass;
1432         else if (FloatABIType == FloatABI::Hard && RegVT == MVT::f64)
1433           RC = ARM::DPRRegisterClass;
1434         else if (AFI->isThumb1OnlyFunction())
1435           RC = ARM::tGPRRegisterClass;
1436         else
1437           RC = ARM::GPRRegisterClass;
1438
1439         assert((RegVT == MVT::i32 || RegVT == MVT::f32 ||
1440                 (FloatABIType == FloatABI::Hard && RegVT == MVT::f64)) &&
1441                "RegVT not supported by FORMAL_ARGUMENTS Lowering");
1442
1443         // Transform the arguments in physical registers into virtual ones.
1444         unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
1445         ArgValue = DAG.getCopyFromReg(Root, dl, Reg, RegVT);
1446       }
1447
1448       // If this is an 8 or 16-bit value, it is really passed promoted
1449       // to 32 bits.  Insert an assert[sz]ext to capture this, then
1450       // truncate to the right size.
1451       switch (VA.getLocInfo()) {
1452       default: llvm_unreachable("Unknown loc info!");
1453       case CCValAssign::Full: break;
1454       case CCValAssign::BCvt:
1455         ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1456         break;
1457       case CCValAssign::SExt:
1458         ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1459                                DAG.getValueType(VA.getValVT()));
1460         ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1461         break;
1462       case CCValAssign::ZExt:
1463         ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1464                                DAG.getValueType(VA.getValVT()));
1465         ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1466         break;
1467       }
1468
1469       ArgValues.push_back(ArgValue);
1470
1471     } else { // VA.isRegLoc()
1472
1473       // sanity check
1474       assert(VA.isMemLoc());
1475       assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
1476
1477       unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
1478       int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset());
1479
1480       // Create load nodes to retrieve arguments from the stack.
1481       SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1482       ArgValues.push_back(DAG.getLoad(VA.getValVT(), dl, Root, FIN, NULL, 0));
1483     }
1484   }
1485
1486   // varargs
1487   if (isVarArg) {
1488     static const unsigned GPRArgRegs[] = {
1489       ARM::R0, ARM::R1, ARM::R2, ARM::R3
1490     };
1491
1492     unsigned NumGPRs = CCInfo.getFirstUnallocated
1493       (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
1494
1495     unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1496     unsigned VARegSize = (4 - NumGPRs) * 4;
1497     unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
1498     unsigned ArgOffset = 0;
1499     if (VARegSaveSize) {
1500       // If this function is vararg, store any remaining integer argument regs
1501       // to their spots on the stack so that they may be loaded by deferencing
1502       // the result of va_next.
1503       AFI->setVarArgsRegSaveSize(VARegSaveSize);
1504       ArgOffset = CCInfo.getNextStackOffset();
1505       VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
1506                                                  VARegSaveSize - VARegSize);
1507       SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
1508
1509       SmallVector<SDValue, 4> MemOps;
1510       for (; NumGPRs < 4; ++NumGPRs) {
1511         TargetRegisterClass *RC;
1512         if (AFI->isThumb1OnlyFunction())
1513           RC = ARM::tGPRRegisterClass;
1514         else
1515           RC = ARM::GPRRegisterClass;
1516
1517         unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
1518         SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::i32);
1519         SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
1520         MemOps.push_back(Store);
1521         FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
1522                           DAG.getConstant(4, getPointerTy()));
1523       }
1524       if (!MemOps.empty())
1525         Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1526                            &MemOps[0], MemOps.size());
1527     } else
1528       // This will point to the next argument passed via stack.
1529       VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1530   }
1531
1532   ArgValues.push_back(Root);
1533
1534   // Return the new list of results.
1535   return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
1536                      &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
1537 }
1538
1539 /// isFloatingPointZero - Return true if this is +0.0.
1540 static bool isFloatingPointZero(SDValue Op) {
1541   if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
1542     return CFP->getValueAPF().isPosZero();
1543   else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
1544     // Maybe this has already been legalized into the constant pool?
1545     if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
1546       SDValue WrapperOp = Op.getOperand(1).getOperand(0);
1547       if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1548         if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
1549           return CFP->getValueAPF().isPosZero();
1550     }
1551   }
1552   return false;
1553 }
1554
1555 static bool isLegalCmpImmediate(unsigned C, bool isThumb1Only) {
1556   return ( isThumb1Only && (C & ~255U) == 0) ||
1557          (!isThumb1Only && ARM_AM::getSOImmVal(C) != -1);
1558 }
1559
1560 /// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1561 /// the given operands.
1562 static SDValue getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
1563                          SDValue &ARMCC, SelectionDAG &DAG, bool isThumb1Only,
1564                          DebugLoc dl) {
1565   if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
1566     unsigned C = RHSC->getZExtValue();
1567     if (!isLegalCmpImmediate(C, isThumb1Only)) {
1568       // Constant does not fit, try adjusting it by one?
1569       switch (CC) {
1570       default: break;
1571       case ISD::SETLT:
1572       case ISD::SETGE:
1573         if (isLegalCmpImmediate(C-1, isThumb1Only)) {
1574           CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
1575           RHS = DAG.getConstant(C-1, MVT::i32);
1576         }
1577         break;
1578       case ISD::SETULT:
1579       case ISD::SETUGE:
1580         if (C > 0 && isLegalCmpImmediate(C-1, isThumb1Only)) {
1581           CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
1582           RHS = DAG.getConstant(C-1, MVT::i32);
1583         }
1584         break;
1585       case ISD::SETLE:
1586       case ISD::SETGT:
1587         if (isLegalCmpImmediate(C+1, isThumb1Only)) {
1588           CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
1589           RHS = DAG.getConstant(C+1, MVT::i32);
1590         }
1591         break;
1592       case ISD::SETULE:
1593       case ISD::SETUGT:
1594         if (C < 0xffffffff && isLegalCmpImmediate(C+1, isThumb1Only)) {
1595           CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
1596           RHS = DAG.getConstant(C+1, MVT::i32);
1597         }
1598         break;
1599       }
1600     }
1601   }
1602
1603   ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
1604   ARMISD::NodeType CompareType;
1605   switch (CondCode) {
1606   default:
1607     CompareType = ARMISD::CMP;
1608     break;
1609   case ARMCC::EQ:
1610   case ARMCC::NE:
1611     // Uses only Z Flag
1612     CompareType = ARMISD::CMPZ;
1613     break;
1614   }
1615   ARMCC = DAG.getConstant(CondCode, MVT::i32);
1616   return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
1617 }
1618
1619 /// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
1620 static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
1621                          DebugLoc dl) {
1622   SDValue Cmp;
1623   if (!isFloatingPointZero(RHS))
1624     Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
1625   else
1626     Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
1627   return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
1628 }
1629
1630 static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
1631                               const ARMSubtarget *ST) {
1632   MVT VT = Op.getValueType();
1633   SDValue LHS = Op.getOperand(0);
1634   SDValue RHS = Op.getOperand(1);
1635   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1636   SDValue TrueVal = Op.getOperand(2);
1637   SDValue FalseVal = Op.getOperand(3);
1638   DebugLoc dl = Op.getDebugLoc();
1639
1640   if (LHS.getValueType() == MVT::i32) {
1641     SDValue ARMCC;
1642     SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1643     SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb1Only(), dl);
1644     return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
1645   }
1646
1647   ARMCC::CondCodes CondCode, CondCode2;
1648   if (FPCCToARMCC(CC, CondCode, CondCode2))
1649     std::swap(TrueVal, FalseVal);
1650
1651   SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1652   SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1653   SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
1654   SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
1655                                  ARMCC, CCR, Cmp);
1656   if (CondCode2 != ARMCC::AL) {
1657     SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
1658     // FIXME: Needs another CMP because flag can have but one use.
1659     SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
1660     Result = DAG.getNode(ARMISD::CMOV, dl, VT,
1661                          Result, TrueVal, ARMCC2, CCR, Cmp2);
1662   }
1663   return Result;
1664 }
1665
1666 static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
1667                           const ARMSubtarget *ST) {
1668   SDValue  Chain = Op.getOperand(0);
1669   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1670   SDValue    LHS = Op.getOperand(2);
1671   SDValue    RHS = Op.getOperand(3);
1672   SDValue   Dest = Op.getOperand(4);
1673   DebugLoc dl = Op.getDebugLoc();
1674
1675   if (LHS.getValueType() == MVT::i32) {
1676     SDValue ARMCC;
1677     SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1678     SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb1Only(), dl);
1679     return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
1680                        Chain, Dest, ARMCC, CCR,Cmp);
1681   }
1682
1683   assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
1684   ARMCC::CondCodes CondCode, CondCode2;
1685   if (FPCCToARMCC(CC, CondCode, CondCode2))
1686     // Swap the LHS/RHS of the comparison if needed.
1687     std::swap(LHS, RHS);
1688
1689   SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
1690   SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1691   SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1692   SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
1693   SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
1694   SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
1695   if (CondCode2 != ARMCC::AL) {
1696     ARMCC = DAG.getConstant(CondCode2, MVT::i32);
1697     SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
1698     Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
1699   }
1700   return Res;
1701 }
1702
1703 SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) {
1704   SDValue Chain = Op.getOperand(0);
1705   SDValue Table = Op.getOperand(1);
1706   SDValue Index = Op.getOperand(2);
1707   DebugLoc dl = Op.getDebugLoc();
1708
1709   MVT PTy = getPointerTy();
1710   JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1711   ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
1712   SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
1713   SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
1714   Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
1715   Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
1716   SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
1717   if (Subtarget->isThumb2()) {
1718     // Thumb2 uses a two-level jump. That is, it jumps into the jump table
1719     // which does another jump to the destination. This also makes it easier
1720     // to translate it to TBB / TBH later.
1721     // FIXME: This might not work if the function is extremely large.
1722     return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
1723                        Addr, Op.getOperand(2), JTI, UId);
1724   }
1725   if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1726     Addr = DAG.getLoad((MVT)MVT::i32, dl, Chain, Addr, NULL, 0);
1727     Chain = Addr.getValue(1);
1728     Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
1729     return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
1730   } else {
1731     Addr = DAG.getLoad(PTy, dl, Chain, Addr, NULL, 0);
1732     Chain = Addr.getValue(1);
1733     return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
1734   }
1735 }
1736
1737 static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
1738   DebugLoc dl = Op.getDebugLoc();
1739   unsigned Opc =
1740     Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
1741   Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
1742   return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
1743 }
1744
1745 static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
1746   MVT VT = Op.getValueType();
1747   DebugLoc dl = Op.getDebugLoc();
1748   unsigned Opc =
1749     Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1750
1751   Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
1752   return DAG.getNode(Opc, dl, VT, Op);
1753 }
1754
1755 static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
1756   // Implement fcopysign with a fabs and a conditional fneg.
1757   SDValue Tmp0 = Op.getOperand(0);
1758   SDValue Tmp1 = Op.getOperand(1);
1759   DebugLoc dl = Op.getDebugLoc();
1760   MVT VT = Op.getValueType();
1761   MVT SrcVT = Tmp1.getValueType();
1762   SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
1763   SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
1764   SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
1765   SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1766   return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
1767 }
1768
1769 SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
1770   MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1771   MFI->setFrameAddressIsTaken(true);
1772   MVT VT = Op.getValueType();
1773   DebugLoc dl = Op.getDebugLoc();  // FIXME probably not meaningful
1774   unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1775   unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
1776     ? ARM::R7 : ARM::R11;
1777   SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
1778   while (Depth--)
1779     FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
1780   return FrameAddr;
1781 }
1782
1783 SDValue
1784 ARMTargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
1785                                            SDValue Chain,
1786                                            SDValue Dst, SDValue Src,
1787                                            SDValue Size, unsigned Align,
1788                                            bool AlwaysInline,
1789                                          const Value *DstSV, uint64_t DstSVOff,
1790                                          const Value *SrcSV, uint64_t SrcSVOff){
1791   // Do repeated 4-byte loads and stores. To be improved.
1792   // This requires 4-byte alignment.
1793   if ((Align & 3) != 0)
1794     return SDValue();
1795   // This requires the copy size to be a constant, preferrably
1796   // within a subtarget-specific limit.
1797   ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
1798   if (!ConstantSize)
1799     return SDValue();
1800   uint64_t SizeVal = ConstantSize->getZExtValue();
1801   if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
1802     return SDValue();
1803
1804   unsigned BytesLeft = SizeVal & 3;
1805   unsigned NumMemOps = SizeVal >> 2;
1806   unsigned EmittedNumMemOps = 0;
1807   MVT VT = MVT::i32;
1808   unsigned VTSize = 4;
1809   unsigned i = 0;
1810   const unsigned MAX_LOADS_IN_LDM = 6;
1811   SDValue TFOps[MAX_LOADS_IN_LDM];
1812   SDValue Loads[MAX_LOADS_IN_LDM];
1813   uint64_t SrcOff = 0, DstOff = 0;
1814
1815   // Emit up to MAX_LOADS_IN_LDM loads, then a TokenFactor barrier, then the
1816   // same number of stores.  The loads and stores will get combined into
1817   // ldm/stm later on.
1818   while (EmittedNumMemOps < NumMemOps) {
1819     for (i = 0;
1820          i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
1821       Loads[i] = DAG.getLoad(VT, dl, Chain,
1822                              DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
1823                                          DAG.getConstant(SrcOff, MVT::i32)),
1824                              SrcSV, SrcSVOff + SrcOff);
1825       TFOps[i] = Loads[i].getValue(1);
1826       SrcOff += VTSize;
1827     }
1828     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
1829
1830     for (i = 0;
1831          i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
1832       TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
1833                            DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
1834                                        DAG.getConstant(DstOff, MVT::i32)),
1835                            DstSV, DstSVOff + DstOff);
1836       DstOff += VTSize;
1837     }
1838     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
1839
1840     EmittedNumMemOps += i;
1841   }
1842
1843   if (BytesLeft == 0)
1844     return Chain;
1845
1846   // Issue loads / stores for the trailing (1 - 3) bytes.
1847   unsigned BytesLeftSave = BytesLeft;
1848   i = 0;
1849   while (BytesLeft) {
1850     if (BytesLeft >= 2) {
1851       VT = MVT::i16;
1852       VTSize = 2;
1853     } else {
1854       VT = MVT::i8;
1855       VTSize = 1;
1856     }
1857
1858     Loads[i] = DAG.getLoad(VT, dl, Chain,
1859                            DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
1860                                        DAG.getConstant(SrcOff, MVT::i32)),
1861                            SrcSV, SrcSVOff + SrcOff);
1862     TFOps[i] = Loads[i].getValue(1);
1863     ++i;
1864     SrcOff += VTSize;
1865     BytesLeft -= VTSize;
1866   }
1867   Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
1868
1869   i = 0;
1870   BytesLeft = BytesLeftSave;
1871   while (BytesLeft) {
1872     if (BytesLeft >= 2) {
1873       VT = MVT::i16;
1874       VTSize = 2;
1875     } else {
1876       VT = MVT::i8;
1877       VTSize = 1;
1878     }
1879
1880     TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
1881                             DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
1882                                         DAG.getConstant(DstOff, MVT::i32)),
1883                             DstSV, DstSVOff + DstOff);
1884     ++i;
1885     DstOff += VTSize;
1886     BytesLeft -= VTSize;
1887   }
1888   return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
1889 }
1890
1891 static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
1892   SDValue Op = N->getOperand(0);
1893   DebugLoc dl = N->getDebugLoc();
1894   if (N->getValueType(0) == MVT::f64) {
1895     // Turn i64->f64 into FMDRR.
1896     SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
1897                              DAG.getConstant(0, MVT::i32));
1898     SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
1899                              DAG.getConstant(1, MVT::i32));
1900     return DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
1901   }
1902
1903   // Turn f64->i64 into FMRRD.
1904   SDValue Cvt = DAG.getNode(ARMISD::FMRRD, dl,
1905                             DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
1906
1907   // Merge the pieces into a single i64 value.
1908   return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
1909 }
1910
1911 /// getZeroVector - Returns a vector of specified type with all zero elements.
1912 ///
1913 static SDValue getZeroVector(MVT VT, SelectionDAG &DAG, DebugLoc dl) {
1914   assert(VT.isVector() && "Expected a vector type");
1915
1916   // Zero vectors are used to represent vector negation and in those cases
1917   // will be implemented with the NEON VNEG instruction.  However, VNEG does
1918   // not support i64 elements, so sometimes the zero vectors will need to be
1919   // explicitly constructed.  For those cases, and potentially other uses in
1920   // the future, always build zero vectors as <4 x i32> or <2 x i32> bitcasted
1921   // to their dest type.  This ensures they get CSE'd.
1922   SDValue Vec;
1923   SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
1924   if (VT.getSizeInBits() == 64)
1925     Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
1926   else
1927     Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
1928
1929   return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
1930 }
1931
1932 /// getOnesVector - Returns a vector of specified type with all bits set.
1933 ///
1934 static SDValue getOnesVector(MVT VT, SelectionDAG &DAG, DebugLoc dl) {
1935   assert(VT.isVector() && "Expected a vector type");
1936
1937   // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
1938   // type.  This ensures they get CSE'd.
1939   SDValue Vec;
1940   SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
1941   if (VT.getSizeInBits() == 64)
1942     Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
1943   else
1944     Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
1945
1946   return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
1947 }
1948
1949 static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
1950                           const ARMSubtarget *ST) {
1951   MVT VT = N->getValueType(0);
1952   DebugLoc dl = N->getDebugLoc();
1953
1954   // Lower vector shifts on NEON to use VSHL.
1955   if (VT.isVector()) {
1956     assert(ST->hasNEON() && "unexpected vector shift");
1957
1958     // Left shifts translate directly to the vshiftu intrinsic.
1959     if (N->getOpcode() == ISD::SHL)
1960       return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
1961                          DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
1962                          N->getOperand(0), N->getOperand(1));
1963
1964     assert((N->getOpcode() == ISD::SRA ||
1965             N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
1966
1967     // NEON uses the same intrinsics for both left and right shifts.  For
1968     // right shifts, the shift amounts are negative, so negate the vector of
1969     // shift amounts.
1970     MVT ShiftVT = N->getOperand(1).getValueType();
1971     SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
1972                                        getZeroVector(ShiftVT, DAG, dl),
1973                                        N->getOperand(1));
1974     Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
1975                                Intrinsic::arm_neon_vshifts :
1976                                Intrinsic::arm_neon_vshiftu);
1977     return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
1978                        DAG.getConstant(vshiftInt, MVT::i32),
1979                        N->getOperand(0), NegatedCount);
1980   }
1981
1982   assert(VT == MVT::i64 &&
1983          (N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
1984          "Unknown shift to lower!");
1985
1986   // We only lower SRA, SRL of 1 here, all others use generic lowering.
1987   if (!isa<ConstantSDNode>(N->getOperand(1)) ||
1988       cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
1989     return SDValue();
1990
1991   // If we are in thumb mode, we don't have RRX.
1992   if (ST->isThumb1Only()) return SDValue();
1993
1994   // Okay, we have a 64-bit SRA or SRL of 1.  Lower this to an RRX expr.
1995   SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
1996                              DAG.getConstant(0, MVT::i32));
1997   SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
1998                              DAG.getConstant(1, MVT::i32));
1999
2000   // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2001   // captures the result into a carry flag.
2002   unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
2003   Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
2004
2005   // The low part is an ARMISD::RRX operand, which shifts the carry in.
2006   Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
2007
2008   // Merge the pieces into a single i64 value.
2009  return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
2010 }
2011
2012 static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2013   SDValue TmpOp0, TmpOp1;
2014   bool Invert = false;
2015   bool Swap = false;
2016   unsigned Opc = 0;
2017
2018   SDValue Op0 = Op.getOperand(0);
2019   SDValue Op1 = Op.getOperand(1);
2020   SDValue CC = Op.getOperand(2);
2021   MVT VT = Op.getValueType();
2022   ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2023   DebugLoc dl = Op.getDebugLoc();
2024
2025   if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2026     switch (SetCCOpcode) {
2027     default: llvm_unreachable("Illegal FP comparison"); break;
2028     case ISD::SETUNE:
2029     case ISD::SETNE:  Invert = true; // Fallthrough
2030     case ISD::SETOEQ:
2031     case ISD::SETEQ:  Opc = ARMISD::VCEQ; break;
2032     case ISD::SETOLT:
2033     case ISD::SETLT: Swap = true; // Fallthrough
2034     case ISD::SETOGT:
2035     case ISD::SETGT:  Opc = ARMISD::VCGT; break;
2036     case ISD::SETOLE:
2037     case ISD::SETLE:  Swap = true; // Fallthrough
2038     case ISD::SETOGE:
2039     case ISD::SETGE: Opc = ARMISD::VCGE; break;
2040     case ISD::SETUGE: Swap = true; // Fallthrough
2041     case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2042     case ISD::SETUGT: Swap = true; // Fallthrough
2043     case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2044     case ISD::SETUEQ: Invert = true; // Fallthrough
2045     case ISD::SETONE:
2046       // Expand this to (OLT | OGT).
2047       TmpOp0 = Op0;
2048       TmpOp1 = Op1;
2049       Opc = ISD::OR;
2050       Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2051       Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2052       break;
2053     case ISD::SETUO: Invert = true; // Fallthrough
2054     case ISD::SETO:
2055       // Expand this to (OLT | OGE).
2056       TmpOp0 = Op0;
2057       TmpOp1 = Op1;
2058       Opc = ISD::OR;
2059       Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2060       Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2061       break;
2062     }
2063   } else {
2064     // Integer comparisons.
2065     switch (SetCCOpcode) {
2066     default: llvm_unreachable("Illegal integer comparison"); break;
2067     case ISD::SETNE:  Invert = true;
2068     case ISD::SETEQ:  Opc = ARMISD::VCEQ; break;
2069     case ISD::SETLT:  Swap = true;
2070     case ISD::SETGT:  Opc = ARMISD::VCGT; break;
2071     case ISD::SETLE:  Swap = true;
2072     case ISD::SETGE:  Opc = ARMISD::VCGE; break;
2073     case ISD::SETULT: Swap = true;
2074     case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2075     case ISD::SETULE: Swap = true;
2076     case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2077     }
2078
2079     // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
2080     if (Opc == ARMISD::VCEQ) {
2081
2082       SDValue AndOp;
2083       if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2084         AndOp = Op0;
2085       else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2086         AndOp = Op1;
2087
2088       // Ignore bitconvert.
2089       if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2090         AndOp = AndOp.getOperand(0);
2091
2092       if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2093         Opc = ARMISD::VTST;
2094         Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2095         Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2096         Invert = !Invert;
2097       }
2098     }
2099   }
2100
2101   if (Swap)
2102     std::swap(Op0, Op1);
2103
2104   SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2105
2106   if (Invert)
2107     Result = DAG.getNOT(dl, Result, VT);
2108
2109   return Result;
2110 }
2111
2112 /// isVMOVSplat - Check if the specified splat value corresponds to an immediate
2113 /// VMOV instruction, and if so, return the constant being splatted.
2114 static SDValue isVMOVSplat(uint64_t SplatBits, uint64_t SplatUndef,
2115                            unsigned SplatBitSize, SelectionDAG &DAG) {
2116   switch (SplatBitSize) {
2117   case 8:
2118     // Any 1-byte value is OK.
2119     assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
2120     return DAG.getTargetConstant(SplatBits, MVT::i8);
2121
2122   case 16:
2123     // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
2124     if ((SplatBits & ~0xff) == 0 ||
2125         (SplatBits & ~0xff00) == 0)
2126       return DAG.getTargetConstant(SplatBits, MVT::i16);
2127     break;
2128
2129   case 32:
2130     // NEON's 32-bit VMOV supports splat values where:
2131     // * only one byte is nonzero, or
2132     // * the least significant byte is 0xff and the second byte is nonzero, or
2133     // * the least significant 2 bytes are 0xff and the third is nonzero.
2134     if ((SplatBits & ~0xff) == 0 ||
2135         (SplatBits & ~0xff00) == 0 ||
2136         (SplatBits & ~0xff0000) == 0 ||
2137         (SplatBits & ~0xff000000) == 0)
2138       return DAG.getTargetConstant(SplatBits, MVT::i32);
2139
2140     if ((SplatBits & ~0xffff) == 0 &&
2141         ((SplatBits | SplatUndef) & 0xff) == 0xff)
2142       return DAG.getTargetConstant(SplatBits | 0xff, MVT::i32);
2143
2144     if ((SplatBits & ~0xffffff) == 0 &&
2145         ((SplatBits | SplatUndef) & 0xffff) == 0xffff)
2146       return DAG.getTargetConstant(SplatBits | 0xffff, MVT::i32);
2147
2148     // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2149     // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2150     // VMOV.I32.  A (very) minor optimization would be to replicate the value
2151     // and fall through here to test for a valid 64-bit splat.  But, then the
2152     // caller would also need to check and handle the change in size.
2153     break;
2154
2155   case 64: {
2156     // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
2157     uint64_t BitMask = 0xff;
2158     uint64_t Val = 0;
2159     for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
2160       if (((SplatBits | SplatUndef) & BitMask) == BitMask)
2161         Val |= BitMask;
2162       else if ((SplatBits & BitMask) != 0)
2163         return SDValue();
2164       BitMask <<= 8;
2165     }
2166     return DAG.getTargetConstant(Val, MVT::i64);
2167   }
2168
2169   default:
2170     llvm_unreachable("unexpected size for isVMOVSplat");
2171     break;
2172   }
2173
2174   return SDValue();
2175 }
2176
2177 /// getVMOVImm - If this is a build_vector of constants which can be
2178 /// formed by using a VMOV instruction of the specified element size,
2179 /// return the constant being splatted.  The ByteSize field indicates the
2180 /// number of bytes of each element [1248].
2181 SDValue ARM::getVMOVImm(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
2182   BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2183   APInt SplatBits, SplatUndef;
2184   unsigned SplatBitSize;
2185   bool HasAnyUndefs;
2186   if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2187                                       HasAnyUndefs, ByteSize * 8))
2188     return SDValue();
2189
2190   if (SplatBitSize > ByteSize * 8)
2191     return SDValue();
2192
2193   return isVMOVSplat(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
2194                      SplatBitSize, DAG);
2195 }
2196
2197 /// isVREVMask - Check if a vector shuffle corresponds to a VREV
2198 /// instruction with the specified blocksize.  (The order of the elements
2199 /// within each block of the vector is reversed.)
2200 bool ARM::isVREVMask(ShuffleVectorSDNode *N, unsigned BlockSize) {
2201   assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
2202          "Only possible block sizes for VREV are: 16, 32, 64");
2203
2204   MVT VT = N->getValueType(0);
2205   unsigned NumElts = VT.getVectorNumElements();
2206   unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2207   unsigned BlockElts = N->getMaskElt(0) + 1;
2208
2209   if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
2210     return false;
2211
2212   for (unsigned i = 0; i < NumElts; ++i) {
2213     if ((unsigned) N->getMaskElt(i) !=
2214         (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
2215       return false;
2216   }
2217
2218   return true;
2219 }
2220
2221 static SDValue BuildSplat(SDValue Val, MVT VT, SelectionDAG &DAG, DebugLoc dl) {
2222   // Canonicalize all-zeros and all-ones vectors.
2223   ConstantSDNode *ConstVal = dyn_cast<ConstantSDNode>(Val.getNode());
2224   if (ConstVal->isNullValue())
2225     return getZeroVector(VT, DAG, dl);
2226   if (ConstVal->isAllOnesValue())
2227     return getOnesVector(VT, DAG, dl);
2228
2229   MVT CanonicalVT;
2230   if (VT.is64BitVector()) {
2231     switch (Val.getValueType().getSizeInBits()) {
2232     case 8:  CanonicalVT = MVT::v8i8; break;
2233     case 16: CanonicalVT = MVT::v4i16; break;
2234     case 32: CanonicalVT = MVT::v2i32; break;
2235     case 64: CanonicalVT = MVT::v1i64; break;
2236     default: llvm_unreachable("unexpected splat element type"); break;
2237     }
2238   } else {
2239     assert(VT.is128BitVector() && "unknown splat vector size");
2240     switch (Val.getValueType().getSizeInBits()) {
2241     case 8:  CanonicalVT = MVT::v16i8; break;
2242     case 16: CanonicalVT = MVT::v8i16; break;
2243     case 32: CanonicalVT = MVT::v4i32; break;
2244     case 64: CanonicalVT = MVT::v2i64; break;
2245     default: llvm_unreachable("unexpected splat element type"); break;
2246     }
2247   }
2248
2249   // Build a canonical splat for this value.
2250   SmallVector<SDValue, 8> Ops;
2251   Ops.assign(CanonicalVT.getVectorNumElements(), Val);
2252   SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
2253                             Ops.size());
2254   return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
2255 }
2256
2257 // If this is a case we can't handle, return null and let the default
2258 // expansion code take care of it.
2259 static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
2260   BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
2261   assert(BVN != 0 && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR");
2262   DebugLoc dl = Op.getDebugLoc();
2263   MVT VT = Op.getValueType();
2264
2265   APInt SplatBits, SplatUndef;
2266   unsigned SplatBitSize;
2267   bool HasAnyUndefs;
2268   if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
2269     SDValue Val = isVMOVSplat(SplatBits.getZExtValue(),
2270                               SplatUndef.getZExtValue(), SplatBitSize, DAG);
2271     if (Val.getNode())
2272       return BuildSplat(Val, VT, DAG, dl);
2273   }
2274
2275   // If there are only 2 elements in a 128-bit vector, insert them into an
2276   // undef vector.  This handles the common case for 128-bit vector argument
2277   // passing, where the insertions should be translated to subreg accesses
2278   // with no real instructions.
2279   if (VT.is128BitVector() && Op.getNumOperands() == 2) {
2280     SDValue Val = DAG.getUNDEF(VT);
2281     SDValue Op0 = Op.getOperand(0);
2282     SDValue Op1 = Op.getOperand(1);
2283     if (Op0.getOpcode() != ISD::UNDEF)
2284       Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op0,
2285                         DAG.getIntPtrConstant(0));
2286     if (Op1.getOpcode() != ISD::UNDEF)
2287       Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op1,
2288                         DAG.getIntPtrConstant(1));
2289     return Val;
2290   }
2291
2292   return SDValue();
2293 }
2294
2295 static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
2296   return Op;
2297 }
2298
2299 static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
2300   return Op;
2301 }
2302
2303 static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
2304   MVT VT = Op.getValueType();
2305   DebugLoc dl = Op.getDebugLoc();
2306   assert((VT == MVT::i8 || VT == MVT::i16) &&
2307          "unexpected type for custom-lowering vector extract");
2308   SDValue Vec = Op.getOperand(0);
2309   SDValue Lane = Op.getOperand(1);
2310   Op = DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
2311   Op = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Op, DAG.getValueType(VT));
2312   return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
2313 }
2314
2315 static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
2316   // The only time a CONCAT_VECTORS operation can have legal types is when
2317   // two 64-bit vectors are concatenated to a 128-bit vector.
2318   assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
2319          "unexpected CONCAT_VECTORS");
2320   DebugLoc dl = Op.getDebugLoc();
2321   SDValue Val = DAG.getUNDEF(MVT::v2f64);
2322   SDValue Op0 = Op.getOperand(0);
2323   SDValue Op1 = Op.getOperand(1);
2324   if (Op0.getOpcode() != ISD::UNDEF)
2325     Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2326                       DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
2327                       DAG.getIntPtrConstant(0));
2328   if (Op1.getOpcode() != ISD::UNDEF)
2329     Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2330                       DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
2331                       DAG.getIntPtrConstant(1));
2332   return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
2333 }
2334
2335 SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
2336   switch (Op.getOpcode()) {
2337   default: llvm_unreachable("Don't know how to custom lower this!");
2338   case ISD::ConstantPool:  return LowerConstantPool(Op, DAG);
2339   case ISD::GlobalAddress:
2340     return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
2341       LowerGlobalAddressELF(Op, DAG);
2342   case ISD::GlobalTLSAddress:   return LowerGlobalTLSAddress(Op, DAG);
2343   case ISD::CALL:          return LowerCALL(Op, DAG);
2344   case ISD::RET:           return LowerRET(Op, DAG);
2345   case ISD::SELECT_CC:     return LowerSELECT_CC(Op, DAG, Subtarget);
2346   case ISD::BR_CC:         return LowerBR_CC(Op, DAG, Subtarget);
2347   case ISD::BR_JT:         return LowerBR_JT(Op, DAG);
2348   case ISD::VASTART:       return LowerVASTART(Op, DAG, VarArgsFrameIndex);
2349   case ISD::SINT_TO_FP:
2350   case ISD::UINT_TO_FP:    return LowerINT_TO_FP(Op, DAG);
2351   case ISD::FP_TO_SINT:
2352   case ISD::FP_TO_UINT:    return LowerFP_TO_INT(Op, DAG);
2353   case ISD::FCOPYSIGN:     return LowerFCOPYSIGN(Op, DAG);
2354   case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
2355   case ISD::RETURNADDR:    break;
2356   case ISD::FRAMEADDR:     return LowerFRAMEADDR(Op, DAG);
2357   case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
2358   case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
2359   case ISD::BIT_CONVERT:   return ExpandBIT_CONVERT(Op.getNode(), DAG);
2360   case ISD::SHL:
2361   case ISD::SRL:
2362   case ISD::SRA:           return LowerShift(Op.getNode(), DAG, Subtarget);
2363   case ISD::VSETCC:        return LowerVSETCC(Op, DAG);
2364   case ISD::BUILD_VECTOR:  return LowerBUILD_VECTOR(Op, DAG);
2365   case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
2366   case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
2367   case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
2368   case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
2369   }
2370   return SDValue();
2371 }
2372
2373 /// ReplaceNodeResults - Replace the results of node with an illegal result
2374 /// type with new values built out of custom code.
2375 void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
2376                                            SmallVectorImpl<SDValue>&Results,
2377                                            SelectionDAG &DAG) {
2378   switch (N->getOpcode()) {
2379   default:
2380     llvm_unreachable("Don't know how to custom expand this!");
2381     return;
2382   case ISD::BIT_CONVERT:
2383     Results.push_back(ExpandBIT_CONVERT(N, DAG));
2384     return;
2385   case ISD::SRL:
2386   case ISD::SRA: {
2387     SDValue Res = LowerShift(N, DAG, Subtarget);
2388     if (Res.getNode())
2389       Results.push_back(Res);
2390     return;
2391   }
2392   }
2393 }
2394
2395 //===----------------------------------------------------------------------===//
2396 //                           ARM Scheduler Hooks
2397 //===----------------------------------------------------------------------===//
2398
2399 MachineBasicBlock *
2400 ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
2401                                                MachineBasicBlock *BB) const {
2402   const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2403   DebugLoc dl = MI->getDebugLoc();
2404   switch (MI->getOpcode()) {
2405   default: assert(false && "Unexpected instr type to insert");
2406   case ARM::tMOVCCr: {
2407     // To "insert" a SELECT_CC instruction, we actually have to insert the
2408     // diamond control-flow pattern.  The incoming instruction knows the
2409     // destination vreg to set, the condition code register to branch on, the
2410     // true/false values to select between, and a branch opcode to use.
2411     const BasicBlock *LLVM_BB = BB->getBasicBlock();
2412     MachineFunction::iterator It = BB;
2413     ++It;
2414
2415     //  thisMBB:
2416     //  ...
2417     //   TrueVal = ...
2418     //   cmpTY ccX, r1, r2
2419     //   bCC copy1MBB
2420     //   fallthrough --> copy0MBB
2421     MachineBasicBlock *thisMBB  = BB;
2422     MachineFunction *F = BB->getParent();
2423     MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
2424     MachineBasicBlock *sinkMBB  = F->CreateMachineBasicBlock(LLVM_BB);
2425     BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
2426       .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
2427     F->insert(It, copy0MBB);
2428     F->insert(It, sinkMBB);
2429     // Update machine-CFG edges by first adding all successors of the current
2430     // block to the new block which will contain the Phi node for the select.
2431     for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
2432         e = BB->succ_end(); i != e; ++i)
2433       sinkMBB->addSuccessor(*i);
2434     // Next, remove all successors of the current block, and add the true
2435     // and fallthrough blocks as its successors.
2436     while(!BB->succ_empty())
2437       BB->removeSuccessor(BB->succ_begin());
2438     BB->addSuccessor(copy0MBB);
2439     BB->addSuccessor(sinkMBB);
2440
2441     //  copy0MBB:
2442     //   %FalseValue = ...
2443     //   # fallthrough to sinkMBB
2444     BB = copy0MBB;
2445
2446     // Update machine-CFG edges
2447     BB->addSuccessor(sinkMBB);
2448
2449     //  sinkMBB:
2450     //   %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2451     //  ...
2452     BB = sinkMBB;
2453     BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
2454       .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
2455       .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
2456
2457     F->DeleteMachineInstr(MI);   // The pseudo instruction is gone now.
2458     return BB;
2459   }
2460   }
2461 }
2462
2463 //===----------------------------------------------------------------------===//
2464 //                           ARM Optimization Hooks
2465 //===----------------------------------------------------------------------===//
2466
2467 static
2468 SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
2469                             TargetLowering::DAGCombinerInfo &DCI) {
2470   SelectionDAG &DAG = DCI.DAG;
2471   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2472   MVT VT = N->getValueType(0);
2473   unsigned Opc = N->getOpcode();
2474   bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
2475   SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
2476   SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
2477   ISD::CondCode CC = ISD::SETCC_INVALID;
2478
2479   if (isSlctCC) {
2480     CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
2481   } else {
2482     SDValue CCOp = Slct.getOperand(0);
2483     if (CCOp.getOpcode() == ISD::SETCC)
2484       CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
2485   }
2486
2487   bool DoXform = false;
2488   bool InvCC = false;
2489   assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
2490           "Bad input!");
2491
2492   if (LHS.getOpcode() == ISD::Constant &&
2493       cast<ConstantSDNode>(LHS)->isNullValue()) {
2494     DoXform = true;
2495   } else if (CC != ISD::SETCC_INVALID &&
2496              RHS.getOpcode() == ISD::Constant &&
2497              cast<ConstantSDNode>(RHS)->isNullValue()) {
2498     std::swap(LHS, RHS);
2499     SDValue Op0 = Slct.getOperand(0);
2500     MVT OpVT = isSlctCC ? Op0.getValueType() :
2501                           Op0.getOperand(0).getValueType();
2502     bool isInt = OpVT.isInteger();
2503     CC = ISD::getSetCCInverse(CC, isInt);
2504
2505     if (!TLI.isCondCodeLegal(CC, OpVT))
2506       return SDValue();         // Inverse operator isn't legal.
2507
2508     DoXform = true;
2509     InvCC = true;
2510   }
2511
2512   if (DoXform) {
2513     SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
2514     if (isSlctCC)
2515       return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
2516                              Slct.getOperand(0), Slct.getOperand(1), CC);
2517     SDValue CCOp = Slct.getOperand(0);
2518     if (InvCC)
2519       CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
2520                           CCOp.getOperand(0), CCOp.getOperand(1), CC);
2521     return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
2522                        CCOp, OtherOp, Result);
2523   }
2524   return SDValue();
2525 }
2526
2527 /// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
2528 static SDValue PerformADDCombine(SDNode *N,
2529                                  TargetLowering::DAGCombinerInfo &DCI) {
2530   // added by evan in r37685 with no testcase.
2531   SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
2532
2533   // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
2534   if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
2535     SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
2536     if (Result.getNode()) return Result;
2537   }
2538   if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
2539     SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
2540     if (Result.getNode()) return Result;
2541   }
2542
2543   return SDValue();
2544 }
2545
2546 /// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
2547 static SDValue PerformSUBCombine(SDNode *N,
2548                                  TargetLowering::DAGCombinerInfo &DCI) {
2549   // added by evan in r37685 with no testcase.
2550   SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
2551
2552   // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
2553   if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
2554     SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
2555     if (Result.getNode()) return Result;
2556   }
2557
2558   return SDValue();
2559 }
2560
2561
2562 /// PerformFMRRDCombine - Target-specific dag combine xforms for ARMISD::FMRRD.
2563 static SDValue PerformFMRRDCombine(SDNode *N,
2564                                    TargetLowering::DAGCombinerInfo &DCI) {
2565   // fmrrd(fmdrr x, y) -> x,y
2566   SDValue InDouble = N->getOperand(0);
2567   if (InDouble.getOpcode() == ARMISD::FMDRR)
2568     return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
2569   return SDValue();
2570 }
2571
2572 /// getVShiftImm - Check if this is a valid build_vector for the immediate
2573 /// operand of a vector shift operation, where all the elements of the
2574 /// build_vector must have the same constant integer value.
2575 static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
2576   // Ignore bit_converts.
2577   while (Op.getOpcode() == ISD::BIT_CONVERT)
2578     Op = Op.getOperand(0);
2579   BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
2580   APInt SplatBits, SplatUndef;
2581   unsigned SplatBitSize;
2582   bool HasAnyUndefs;
2583   if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2584                                       HasAnyUndefs, ElementBits) ||
2585       SplatBitSize > ElementBits)
2586     return false;
2587   Cnt = SplatBits.getSExtValue();
2588   return true;
2589 }
2590
2591 /// isVShiftLImm - Check if this is a valid build_vector for the immediate
2592 /// operand of a vector shift left operation.  That value must be in the range:
2593 ///   0 <= Value < ElementBits for a left shift; or
2594 ///   0 <= Value <= ElementBits for a long left shift.
2595 static bool isVShiftLImm(SDValue Op, MVT VT, bool isLong, int64_t &Cnt) {
2596   assert(VT.isVector() && "vector shift count is not a vector type");
2597   unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
2598   if (! getVShiftImm(Op, ElementBits, Cnt))
2599     return false;
2600   return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
2601 }
2602
2603 /// isVShiftRImm - Check if this is a valid build_vector for the immediate
2604 /// operand of a vector shift right operation.  For a shift opcode, the value
2605 /// is positive, but for an intrinsic the value count must be negative. The
2606 /// absolute value must be in the range:
2607 ///   1 <= |Value| <= ElementBits for a right shift; or
2608 ///   1 <= |Value| <= ElementBits/2 for a narrow right shift.
2609 static bool isVShiftRImm(SDValue Op, MVT VT, bool isNarrow, bool isIntrinsic,
2610                          int64_t &Cnt) {
2611   assert(VT.isVector() && "vector shift count is not a vector type");
2612   unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
2613   if (! getVShiftImm(Op, ElementBits, Cnt))
2614     return false;
2615   if (isIntrinsic)
2616     Cnt = -Cnt;
2617   return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
2618 }
2619
2620 /// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
2621 static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
2622   unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
2623   switch (IntNo) {
2624   default:
2625     // Don't do anything for most intrinsics.
2626     break;
2627
2628   // Vector shifts: check for immediate versions and lower them.
2629   // Note: This is done during DAG combining instead of DAG legalizing because
2630   // the build_vectors for 64-bit vector element shift counts are generally
2631   // not legal, and it is hard to see their values after they get legalized to
2632   // loads from a constant pool.
2633   case Intrinsic::arm_neon_vshifts:
2634   case Intrinsic::arm_neon_vshiftu:
2635   case Intrinsic::arm_neon_vshiftls:
2636   case Intrinsic::arm_neon_vshiftlu:
2637   case Intrinsic::arm_neon_vshiftn:
2638   case Intrinsic::arm_neon_vrshifts:
2639   case Intrinsic::arm_neon_vrshiftu:
2640   case Intrinsic::arm_neon_vrshiftn:
2641   case Intrinsic::arm_neon_vqshifts:
2642   case Intrinsic::arm_neon_vqshiftu:
2643   case Intrinsic::arm_neon_vqshiftsu:
2644   case Intrinsic::arm_neon_vqshiftns:
2645   case Intrinsic::arm_neon_vqshiftnu:
2646   case Intrinsic::arm_neon_vqshiftnsu:
2647   case Intrinsic::arm_neon_vqrshiftns:
2648   case Intrinsic::arm_neon_vqrshiftnu:
2649   case Intrinsic::arm_neon_vqrshiftnsu: {
2650     MVT VT = N->getOperand(1).getValueType();
2651     int64_t Cnt;
2652     unsigned VShiftOpc = 0;
2653
2654     switch (IntNo) {
2655     case Intrinsic::arm_neon_vshifts:
2656     case Intrinsic::arm_neon_vshiftu:
2657       if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
2658         VShiftOpc = ARMISD::VSHL;
2659         break;
2660       }
2661       if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
2662         VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
2663                      ARMISD::VSHRs : ARMISD::VSHRu);
2664         break;
2665       }
2666       return SDValue();
2667
2668     case Intrinsic::arm_neon_vshiftls:
2669     case Intrinsic::arm_neon_vshiftlu:
2670       if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
2671         break;
2672       llvm_unreachable("invalid shift count for vshll intrinsic");
2673
2674     case Intrinsic::arm_neon_vrshifts:
2675     case Intrinsic::arm_neon_vrshiftu:
2676       if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
2677         break;
2678       return SDValue();
2679
2680     case Intrinsic::arm_neon_vqshifts:
2681     case Intrinsic::arm_neon_vqshiftu:
2682       if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
2683         break;
2684       return SDValue();
2685
2686     case Intrinsic::arm_neon_vqshiftsu:
2687       if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
2688         break;
2689       llvm_unreachable("invalid shift count for vqshlu intrinsic");
2690
2691     case Intrinsic::arm_neon_vshiftn:
2692     case Intrinsic::arm_neon_vrshiftn:
2693     case Intrinsic::arm_neon_vqshiftns:
2694     case Intrinsic::arm_neon_vqshiftnu:
2695     case Intrinsic::arm_neon_vqshiftnsu:
2696     case Intrinsic::arm_neon_vqrshiftns:
2697     case Intrinsic::arm_neon_vqrshiftnu:
2698     case Intrinsic::arm_neon_vqrshiftnsu:
2699       // Narrowing shifts require an immediate right shift.
2700       if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
2701         break;
2702       llvm_unreachable("invalid shift count for narrowing vector shift intrinsic");
2703
2704     default:
2705       llvm_unreachable("unhandled vector shift");
2706     }
2707
2708     switch (IntNo) {
2709     case Intrinsic::arm_neon_vshifts:
2710     case Intrinsic::arm_neon_vshiftu:
2711       // Opcode already set above.
2712       break;
2713     case Intrinsic::arm_neon_vshiftls:
2714     case Intrinsic::arm_neon_vshiftlu:
2715       if (Cnt == VT.getVectorElementType().getSizeInBits())
2716         VShiftOpc = ARMISD::VSHLLi;
2717       else
2718         VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
2719                      ARMISD::VSHLLs : ARMISD::VSHLLu);
2720       break;
2721     case Intrinsic::arm_neon_vshiftn:
2722       VShiftOpc = ARMISD::VSHRN; break;
2723     case Intrinsic::arm_neon_vrshifts:
2724       VShiftOpc = ARMISD::VRSHRs; break;
2725     case Intrinsic::arm_neon_vrshiftu:
2726       VShiftOpc = ARMISD::VRSHRu; break;
2727     case Intrinsic::arm_neon_vrshiftn:
2728       VShiftOpc = ARMISD::VRSHRN; break;
2729     case Intrinsic::arm_neon_vqshifts:
2730       VShiftOpc = ARMISD::VQSHLs; break;
2731     case Intrinsic::arm_neon_vqshiftu:
2732       VShiftOpc = ARMISD::VQSHLu; break;
2733     case Intrinsic::arm_neon_vqshiftsu:
2734       VShiftOpc = ARMISD::VQSHLsu; break;
2735     case Intrinsic::arm_neon_vqshiftns:
2736       VShiftOpc = ARMISD::VQSHRNs; break;
2737     case Intrinsic::arm_neon_vqshiftnu:
2738       VShiftOpc = ARMISD::VQSHRNu; break;
2739     case Intrinsic::arm_neon_vqshiftnsu:
2740       VShiftOpc = ARMISD::VQSHRNsu; break;
2741     case Intrinsic::arm_neon_vqrshiftns:
2742       VShiftOpc = ARMISD::VQRSHRNs; break;
2743     case Intrinsic::arm_neon_vqrshiftnu:
2744       VShiftOpc = ARMISD::VQRSHRNu; break;
2745     case Intrinsic::arm_neon_vqrshiftnsu:
2746       VShiftOpc = ARMISD::VQRSHRNsu; break;
2747     }
2748
2749     return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
2750                        N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
2751   }
2752
2753   case Intrinsic::arm_neon_vshiftins: {
2754     MVT VT = N->getOperand(1).getValueType();
2755     int64_t Cnt;
2756     unsigned VShiftOpc = 0;
2757
2758     if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
2759       VShiftOpc = ARMISD::VSLI;
2760     else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
2761       VShiftOpc = ARMISD::VSRI;
2762     else {
2763       llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
2764     }
2765
2766     return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
2767                        N->getOperand(1), N->getOperand(2),
2768                        DAG.getConstant(Cnt, MVT::i32));
2769   }
2770
2771   case Intrinsic::arm_neon_vqrshifts:
2772   case Intrinsic::arm_neon_vqrshiftu:
2773     // No immediate versions of these to check for.
2774     break;
2775   }
2776
2777   return SDValue();
2778 }
2779
2780 /// PerformShiftCombine - Checks for immediate versions of vector shifts and
2781 /// lowers them.  As with the vector shift intrinsics, this is done during DAG
2782 /// combining instead of DAG legalizing because the build_vectors for 64-bit
2783 /// vector element shift counts are generally not legal, and it is hard to see
2784 /// their values after they get legalized to loads from a constant pool.
2785 static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
2786                                    const ARMSubtarget *ST) {
2787   MVT VT = N->getValueType(0);
2788
2789   // Nothing to be done for scalar shifts.
2790   if (! VT.isVector())
2791     return SDValue();
2792
2793   assert(ST->hasNEON() && "unexpected vector shift");
2794   int64_t Cnt;
2795
2796   switch (N->getOpcode()) {
2797   default: llvm_unreachable("unexpected shift opcode");
2798
2799   case ISD::SHL:
2800     if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
2801       return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
2802                          DAG.getConstant(Cnt, MVT::i32));
2803     break;
2804
2805   case ISD::SRA:
2806   case ISD::SRL:
2807     if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
2808       unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
2809                             ARMISD::VSHRs : ARMISD::VSHRu);
2810       return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
2811                          DAG.getConstant(Cnt, MVT::i32));
2812     }
2813   }
2814   return SDValue();
2815 }
2816
2817 /// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
2818 /// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
2819 static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
2820                                     const ARMSubtarget *ST) {
2821   SDValue N0 = N->getOperand(0);
2822
2823   // Check for sign- and zero-extensions of vector extract operations of 8-
2824   // and 16-bit vector elements.  NEON supports these directly.  They are
2825   // handled during DAG combining because type legalization will promote them
2826   // to 32-bit types and it is messy to recognize the operations after that.
2827   if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
2828     SDValue Vec = N0.getOperand(0);
2829     SDValue Lane = N0.getOperand(1);
2830     MVT VT = N->getValueType(0);
2831     MVT EltVT = N0.getValueType();
2832     const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2833
2834     if (VT == MVT::i32 &&
2835         (EltVT == MVT::i8 || EltVT == MVT::i16) &&
2836         TLI.isTypeLegal(Vec.getValueType())) {
2837
2838       unsigned Opc = 0;
2839       switch (N->getOpcode()) {
2840       default: llvm_unreachable("unexpected opcode");
2841       case ISD::SIGN_EXTEND:
2842         Opc = ARMISD::VGETLANEs;
2843         break;
2844       case ISD::ZERO_EXTEND:
2845       case ISD::ANY_EXTEND:
2846         Opc = ARMISD::VGETLANEu;
2847         break;
2848       }
2849       return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
2850     }
2851   }
2852
2853   return SDValue();
2854 }
2855
2856 SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
2857                                              DAGCombinerInfo &DCI) const {
2858   switch (N->getOpcode()) {
2859   default: break;
2860   case ISD::ADD:      return PerformADDCombine(N, DCI);
2861   case ISD::SUB:      return PerformSUBCombine(N, DCI);
2862   case ARMISD::FMRRD: return PerformFMRRDCombine(N, DCI);
2863   case ISD::INTRINSIC_WO_CHAIN:
2864     return PerformIntrinsicCombine(N, DCI.DAG);
2865   case ISD::SHL:
2866   case ISD::SRA:
2867   case ISD::SRL:
2868     return PerformShiftCombine(N, DCI.DAG, Subtarget);
2869   case ISD::SIGN_EXTEND:
2870   case ISD::ZERO_EXTEND:
2871   case ISD::ANY_EXTEND:
2872     return PerformExtendCombine(N, DCI.DAG, Subtarget);
2873   }
2874   return SDValue();
2875 }
2876
2877 /// isLegalAddressImmediate - Return true if the integer value can be used
2878 /// as the offset of the target addressing mode for load / store of the
2879 /// given type.
2880 static bool isLegalAddressImmediate(int64_t V, MVT VT,
2881                                     const ARMSubtarget *Subtarget) {
2882   if (V == 0)
2883     return true;
2884
2885   if (!VT.isSimple())
2886     return false;
2887
2888   if (Subtarget->isThumb()) { // FIXME for thumb2
2889     if (V < 0)
2890       return false;
2891
2892     unsigned Scale = 1;
2893     switch (VT.getSimpleVT()) {
2894     default: return false;
2895     case MVT::i1:
2896     case MVT::i8:
2897       // Scale == 1;
2898       break;
2899     case MVT::i16:
2900       // Scale == 2;
2901       Scale = 2;
2902       break;
2903     case MVT::i32:
2904       // Scale == 4;
2905       Scale = 4;
2906       break;
2907     }
2908
2909     if ((V & (Scale - 1)) != 0)
2910       return false;
2911     V /= Scale;
2912     return V == (V & ((1LL << 5) - 1));
2913   }
2914
2915   if (V < 0)
2916     V = - V;
2917   switch (VT.getSimpleVT()) {
2918   default: return false;
2919   case MVT::i1:
2920   case MVT::i8:
2921   case MVT::i32:
2922     // +- imm12
2923     return V == (V & ((1LL << 12) - 1));
2924   case MVT::i16:
2925     // +- imm8
2926     return V == (V & ((1LL << 8) - 1));
2927   case MVT::f32:
2928   case MVT::f64:
2929     if (!Subtarget->hasVFP2())
2930       return false;
2931     if ((V & 3) != 0)
2932       return false;
2933     V >>= 2;
2934     return V == (V & ((1LL << 8) - 1));
2935   }
2936 }
2937
2938 /// isLegalAddressingMode - Return true if the addressing mode represented
2939 /// by AM is legal for this target, for a load/store of the specified type.
2940 bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
2941                                               const Type *Ty) const {
2942   MVT VT = getValueType(Ty, true);
2943   if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
2944     return false;
2945
2946   // Can never fold addr of global into load/store.
2947   if (AM.BaseGV)
2948     return false;
2949
2950   switch (AM.Scale) {
2951   case 0:  // no scale reg, must be "r+i" or "r", or "i".
2952     break;
2953   case 1:
2954     if (Subtarget->isThumb())  // FIXME for thumb2
2955       return false;
2956     // FALL THROUGH.
2957   default:
2958     // ARM doesn't support any R+R*scale+imm addr modes.
2959     if (AM.BaseOffs)
2960       return false;
2961
2962     if (!VT.isSimple())
2963       return false;
2964
2965     int Scale = AM.Scale;
2966     switch (VT.getSimpleVT()) {
2967     default: return false;
2968     case MVT::i1:
2969     case MVT::i8:
2970     case MVT::i32:
2971     case MVT::i64:
2972       // This assumes i64 is legalized to a pair of i32. If not (i.e.
2973       // ldrd / strd are used, then its address mode is same as i16.
2974       // r + r
2975       if (Scale < 0) Scale = -Scale;
2976       if (Scale == 1)
2977         return true;
2978       // r + r << imm
2979       return isPowerOf2_32(Scale & ~1);
2980     case MVT::i16:
2981       // r + r
2982       if (((unsigned)AM.HasBaseReg + Scale) <= 2)
2983         return true;
2984       return false;
2985
2986     case MVT::isVoid:
2987       // Note, we allow "void" uses (basically, uses that aren't loads or
2988       // stores), because arm allows folding a scale into many arithmetic
2989       // operations.  This should be made more precise and revisited later.
2990
2991       // Allow r << imm, but the imm has to be a multiple of two.
2992       if (AM.Scale & 1) return false;
2993       return isPowerOf2_32(AM.Scale);
2994     }
2995     break;
2996   }
2997   return true;
2998 }
2999
3000 static bool getARMIndexedAddressParts(SDNode *Ptr, MVT VT,
3001                                       bool isSEXTLoad, SDValue &Base,
3002                                       SDValue &Offset, bool &isInc,
3003                                       SelectionDAG &DAG) {
3004   if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3005     return false;
3006
3007   if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
3008     // AddressingMode 3
3009     Base = Ptr->getOperand(0);
3010     if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
3011       int RHSC = (int)RHS->getZExtValue();
3012       if (RHSC < 0 && RHSC > -256) {
3013         assert(Ptr->getOpcode() == ISD::ADD);
3014         isInc = false;
3015         Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3016         return true;
3017       }
3018     }
3019     isInc = (Ptr->getOpcode() == ISD::ADD);
3020     Offset = Ptr->getOperand(1);
3021     return true;
3022   } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
3023     // AddressingMode 2
3024     if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
3025       int RHSC = (int)RHS->getZExtValue();
3026       if (RHSC < 0 && RHSC > -0x1000) {
3027         assert(Ptr->getOpcode() == ISD::ADD);
3028         isInc = false;
3029         Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3030         Base = Ptr->getOperand(0);
3031         return true;
3032       }
3033     }
3034
3035     if (Ptr->getOpcode() == ISD::ADD) {
3036       isInc = true;
3037       ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
3038       if (ShOpcVal != ARM_AM::no_shift) {
3039         Base = Ptr->getOperand(1);
3040         Offset = Ptr->getOperand(0);
3041       } else {
3042         Base = Ptr->getOperand(0);
3043         Offset = Ptr->getOperand(1);
3044       }
3045       return true;
3046     }
3047
3048     isInc = (Ptr->getOpcode() == ISD::ADD);
3049     Base = Ptr->getOperand(0);
3050     Offset = Ptr->getOperand(1);
3051     return true;
3052   }
3053
3054   // FIXME: Use FLDM / FSTM to emulate indexed FP load / store.
3055   return false;
3056 }
3057
3058 static bool getT2IndexedAddressParts(SDNode *Ptr, MVT VT,
3059                                      bool isSEXTLoad, SDValue &Base,
3060                                      SDValue &Offset, bool &isInc,
3061                                      SelectionDAG &DAG) {
3062   if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3063     return false;
3064
3065   Base = Ptr->getOperand(0);
3066   if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
3067     int RHSC = (int)RHS->getZExtValue();
3068     if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
3069       assert(Ptr->getOpcode() == ISD::ADD);
3070       isInc = false;
3071       Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3072       return true;
3073     } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
3074       isInc = Ptr->getOpcode() == ISD::ADD;
3075       Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
3076       return true;
3077     }
3078   }
3079
3080   return false;
3081 }
3082
3083 /// getPreIndexedAddressParts - returns true by value, base pointer and
3084 /// offset pointer and addressing mode by reference if the node's address
3085 /// can be legally represented as pre-indexed load / store address.
3086 bool
3087 ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
3088                                              SDValue &Offset,
3089                                              ISD::MemIndexedMode &AM,
3090                                              SelectionDAG &DAG) const {
3091   if (Subtarget->isThumb1Only())
3092     return false;
3093
3094   MVT VT;
3095   SDValue Ptr;
3096   bool isSEXTLoad = false;
3097   if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
3098     Ptr = LD->getBasePtr();
3099     VT  = LD->getMemoryVT();
3100     isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3101   } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
3102     Ptr = ST->getBasePtr();
3103     VT  = ST->getMemoryVT();
3104   } else
3105     return false;
3106
3107   bool isInc;
3108   bool isLegal = false;
3109   if (Subtarget->isThumb() && Subtarget->hasThumb2())
3110     isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
3111                                        Offset, isInc, DAG);
3112   else 
3113     isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
3114                                         Offset, isInc, DAG);
3115   if (!isLegal)
3116     return false;
3117
3118   AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
3119   return true;
3120 }
3121
3122 /// getPostIndexedAddressParts - returns true by value, base pointer and
3123 /// offset pointer and addressing mode by reference if this node can be
3124 /// combined with a load / store to form a post-indexed load / store.
3125 bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
3126                                                    SDValue &Base,
3127                                                    SDValue &Offset,
3128                                                    ISD::MemIndexedMode &AM,
3129                                                    SelectionDAG &DAG) const {
3130   if (Subtarget->isThumb1Only())
3131     return false;
3132
3133   MVT VT;
3134   SDValue Ptr;
3135   bool isSEXTLoad = false;
3136   if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
3137     VT  = LD->getMemoryVT();
3138     isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3139   } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
3140     VT  = ST->getMemoryVT();
3141   } else
3142     return false;
3143
3144   bool isInc;
3145   bool isLegal = false;
3146   if (Subtarget->isThumb() && Subtarget->hasThumb2())
3147     isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
3148                                         isInc, DAG);
3149   else 
3150     isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
3151                                         isInc, DAG);
3152   if (!isLegal)
3153     return false;
3154
3155   AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
3156   return true;
3157 }
3158
3159 void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
3160                                                        const APInt &Mask,
3161                                                        APInt &KnownZero,
3162                                                        APInt &KnownOne,
3163                                                        const SelectionDAG &DAG,
3164                                                        unsigned Depth) const {
3165   KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
3166   switch (Op.getOpcode()) {
3167   default: break;
3168   case ARMISD::CMOV: {
3169     // Bits are known zero/one if known on the LHS and RHS.
3170     DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
3171     if (KnownZero == 0 && KnownOne == 0) return;
3172
3173     APInt KnownZeroRHS, KnownOneRHS;
3174     DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
3175                           KnownZeroRHS, KnownOneRHS, Depth+1);
3176     KnownZero &= KnownZeroRHS;
3177     KnownOne  &= KnownOneRHS;
3178     return;
3179   }
3180   }
3181 }
3182
3183 //===----------------------------------------------------------------------===//
3184 //                           ARM Inline Assembly Support
3185 //===----------------------------------------------------------------------===//
3186
3187 /// getConstraintType - Given a constraint letter, return the type of
3188 /// constraint it is for this target.
3189 ARMTargetLowering::ConstraintType
3190 ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
3191   if (Constraint.size() == 1) {
3192     switch (Constraint[0]) {
3193     default:  break;
3194     case 'l': return C_RegisterClass;
3195     case 'w': return C_RegisterClass;
3196     }
3197   }
3198   return TargetLowering::getConstraintType(Constraint);
3199 }
3200
3201 std::pair<unsigned, const TargetRegisterClass*>
3202 ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
3203                                                 MVT VT) const {
3204   if (Constraint.size() == 1) {
3205     // GCC RS6000 Constraint Letters
3206     switch (Constraint[0]) {
3207     case 'l':
3208       if (Subtarget->isThumb1Only())
3209         return std::make_pair(0U, ARM::tGPRRegisterClass);
3210       else
3211         return std::make_pair(0U, ARM::GPRRegisterClass);
3212     case 'r':
3213       return std::make_pair(0U, ARM::GPRRegisterClass);
3214     case 'w':
3215       if (VT == MVT::f32)
3216         return std::make_pair(0U, ARM::SPRRegisterClass);
3217       if (VT == MVT::f64)
3218         return std::make_pair(0U, ARM::DPRRegisterClass);
3219       break;
3220     }
3221   }
3222   return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3223 }
3224
3225 std::vector<unsigned> ARMTargetLowering::
3226 getRegClassForInlineAsmConstraint(const std::string &Constraint,
3227                                   MVT VT) const {
3228   if (Constraint.size() != 1)
3229     return std::vector<unsigned>();
3230
3231   switch (Constraint[0]) {      // GCC ARM Constraint Letters
3232   default: break;
3233   case 'l':
3234     return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
3235                                  ARM::R4, ARM::R5, ARM::R6, ARM::R7,
3236                                  0);
3237   case 'r':
3238     return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
3239                                  ARM::R4, ARM::R5, ARM::R6, ARM::R7,
3240                                  ARM::R8, ARM::R9, ARM::R10, ARM::R11,
3241                                  ARM::R12, ARM::LR, 0);
3242   case 'w':
3243     if (VT == MVT::f32)
3244       return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
3245                                    ARM::S4, ARM::S5, ARM::S6, ARM::S7,
3246                                    ARM::S8, ARM::S9, ARM::S10, ARM::S11,
3247                                    ARM::S12,ARM::S13,ARM::S14,ARM::S15,
3248                                    ARM::S16,ARM::S17,ARM::S18,ARM::S19,
3249                                    ARM::S20,ARM::S21,ARM::S22,ARM::S23,
3250                                    ARM::S24,ARM::S25,ARM::S26,ARM::S27,
3251                                    ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
3252     if (VT == MVT::f64)
3253       return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
3254                                    ARM::D4, ARM::D5, ARM::D6, ARM::D7,
3255                                    ARM::D8, ARM::D9, ARM::D10,ARM::D11,
3256                                    ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
3257       break;
3258   }
3259
3260   return std::vector<unsigned>();
3261 }
3262
3263 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3264 /// vector.  If it is invalid, don't add anything to Ops.
3265 void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3266                                                      char Constraint,
3267                                                      bool hasMemory,
3268                                                      std::vector<SDValue>&Ops,
3269                                                      SelectionDAG &DAG) const {
3270   SDValue Result(0, 0);
3271
3272   switch (Constraint) {
3273   default: break;
3274   case 'I': case 'J': case 'K': case 'L':
3275   case 'M': case 'N': case 'O':
3276     ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
3277     if (!C)
3278       return;
3279
3280     int64_t CVal64 = C->getSExtValue();
3281     int CVal = (int) CVal64;
3282     // None of these constraints allow values larger than 32 bits.  Check
3283     // that the value fits in an int.
3284     if (CVal != CVal64)
3285       return;
3286
3287     switch (Constraint) {
3288       case 'I':
3289         if (Subtarget->isThumb1Only()) {
3290           // This must be a constant between 0 and 255, for ADD
3291           // immediates.
3292           if (CVal >= 0 && CVal <= 255)
3293             break;
3294         } else if (Subtarget->isThumb2()) {
3295           // A constant that can be used as an immediate value in a
3296           // data-processing instruction.
3297           if (ARM_AM::getT2SOImmVal(CVal) != -1)
3298             break;
3299         } else {
3300           // A constant that can be used as an immediate value in a
3301           // data-processing instruction.
3302           if (ARM_AM::getSOImmVal(CVal) != -1)
3303             break;
3304         }
3305         return;
3306
3307       case 'J':
3308         if (Subtarget->isThumb()) {  // FIXME thumb2
3309           // This must be a constant between -255 and -1, for negated ADD
3310           // immediates. This can be used in GCC with an "n" modifier that
3311           // prints the negated value, for use with SUB instructions. It is
3312           // not useful otherwise but is implemented for compatibility.
3313           if (CVal >= -255 && CVal <= -1)
3314             break;
3315         } else {
3316           // This must be a constant between -4095 and 4095. It is not clear
3317           // what this constraint is intended for. Implemented for
3318           // compatibility with GCC.
3319           if (CVal >= -4095 && CVal <= 4095)
3320             break;
3321         }
3322         return;
3323
3324       case 'K':
3325         if (Subtarget->isThumb1Only()) {
3326           // A 32-bit value where only one byte has a nonzero value. Exclude
3327           // zero to match GCC. This constraint is used by GCC internally for
3328           // constants that can be loaded with a move/shift combination.
3329           // It is not useful otherwise but is implemented for compatibility.
3330           if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
3331             break;
3332         } else if (Subtarget->isThumb2()) {
3333           // A constant whose bitwise inverse can be used as an immediate
3334           // value in a data-processing instruction. This can be used in GCC
3335           // with a "B" modifier that prints the inverted value, for use with
3336           // BIC and MVN instructions. It is not useful otherwise but is
3337           // implemented for compatibility.
3338           if (ARM_AM::getT2SOImmVal(~CVal) != -1)
3339             break;
3340         } else {
3341           // A constant whose bitwise inverse can be used as an immediate
3342           // value in a data-processing instruction. This can be used in GCC
3343           // with a "B" modifier that prints the inverted value, for use with
3344           // BIC and MVN instructions. It is not useful otherwise but is
3345           // implemented for compatibility.
3346           if (ARM_AM::getSOImmVal(~CVal) != -1)
3347             break;
3348         }
3349         return;
3350
3351       case 'L':
3352         if (Subtarget->isThumb1Only()) {
3353           // This must be a constant between -7 and 7,
3354           // for 3-operand ADD/SUB immediate instructions.
3355           if (CVal >= -7 && CVal < 7)
3356             break;
3357         } else if (Subtarget->isThumb2()) {
3358           // A constant whose negation can be used as an immediate value in a
3359           // data-processing instruction. This can be used in GCC with an "n"
3360           // modifier that prints the negated value, for use with SUB
3361           // instructions. It is not useful otherwise but is implemented for
3362           // compatibility.
3363           if (ARM_AM::getT2SOImmVal(-CVal) != -1)
3364             break;
3365         } else {
3366           // A constant whose negation can be used as an immediate value in a
3367           // data-processing instruction. This can be used in GCC with an "n"
3368           // modifier that prints the negated value, for use with SUB
3369           // instructions. It is not useful otherwise but is implemented for
3370           // compatibility.
3371           if (ARM_AM::getSOImmVal(-CVal) != -1)
3372             break;
3373         }
3374         return;
3375
3376       case 'M':
3377         if (Subtarget->isThumb()) { // FIXME thumb2
3378           // This must be a multiple of 4 between 0 and 1020, for
3379           // ADD sp + immediate.
3380           if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
3381             break;
3382         } else {
3383           // A power of two or a constant between 0 and 32.  This is used in
3384           // GCC for the shift amount on shifted register operands, but it is
3385           // useful in general for any shift amounts.
3386           if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
3387             break;
3388         }
3389         return;
3390
3391       case 'N':
3392         if (Subtarget->isThumb()) {  // FIXME thumb2
3393           // This must be a constant between 0 and 31, for shift amounts.
3394           if (CVal >= 0 && CVal <= 31)
3395             break;
3396         }
3397         return;
3398
3399       case 'O':
3400         if (Subtarget->isThumb()) {  // FIXME thumb2
3401           // This must be a multiple of 4 between -508 and 508, for
3402           // ADD/SUB sp = sp + immediate.
3403           if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
3404             break;
3405         }
3406         return;
3407     }
3408     Result = DAG.getTargetConstant(CVal, Op.getValueType());
3409     break;
3410   }
3411
3412   if (Result.getNode()) {
3413     Ops.push_back(Result);
3414     return;
3415   }
3416   return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
3417                                                       Ops, DAG);
3418 }