Remove non-DebugLoc forms of CopyToReg and CopyFromReg.
[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 "llvm/CallingConv.h"
24 #include "llvm/Constants.h"
25 #include "llvm/Instruction.h"
26 #include "llvm/Intrinsics.h"
27 #include "llvm/GlobalValue.h"
28 #include "llvm/CodeGen/MachineBasicBlock.h"
29 #include "llvm/CodeGen/MachineFrameInfo.h"
30 #include "llvm/CodeGen/MachineFunction.h"
31 #include "llvm/CodeGen/MachineInstrBuilder.h"
32 #include "llvm/CodeGen/MachineRegisterInfo.h"
33 #include "llvm/CodeGen/SelectionDAG.h"
34 #include "llvm/Target/TargetOptions.h"
35 #include "llvm/ADT/VectorExtras.h"
36 #include "llvm/Support/MathExtras.h"
37 using namespace llvm;
38
39 ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
40     : TargetLowering(TM), ARMPCLabelIndex(0) {
41   Subtarget = &TM.getSubtarget<ARMSubtarget>();
42
43   if (Subtarget->isTargetDarwin()) {
44     // Uses VFP for Thumb libfuncs if available.
45     if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
46       // Single-precision floating-point arithmetic.
47       setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
48       setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
49       setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
50       setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
51
52       // Double-precision floating-point arithmetic.
53       setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
54       setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
55       setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
56       setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
57
58       // Single-precision comparisons.
59       setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
60       setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
61       setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
62       setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
63       setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
64       setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
65       setLibcallName(RTLIB::UO_F32,  "__unordsf2vfp");
66       setLibcallName(RTLIB::O_F32,   "__unordsf2vfp");
67
68       setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
69       setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
70       setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
71       setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
72       setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
73       setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
74       setCmpLibcallCC(RTLIB::UO_F32,  ISD::SETNE);
75       setCmpLibcallCC(RTLIB::O_F32,   ISD::SETEQ);
76
77       // Double-precision comparisons.
78       setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
79       setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
80       setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
81       setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
82       setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
83       setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
84       setLibcallName(RTLIB::UO_F64,  "__unorddf2vfp");
85       setLibcallName(RTLIB::O_F64,   "__unorddf2vfp");
86
87       setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
88       setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
89       setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
90       setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
91       setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
92       setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
93       setCmpLibcallCC(RTLIB::UO_F64,  ISD::SETNE);
94       setCmpLibcallCC(RTLIB::O_F64,   ISD::SETEQ);
95
96       // Floating-point to integer conversions.
97       // i64 conversions are done via library routines even when generating VFP
98       // instructions, so use the same ones.
99       setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
100       setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
101       setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
102       setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
103
104       // Conversions between floating types.
105       setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
106       setLibcallName(RTLIB::FPEXT_F32_F64,   "__extendsfdf2vfp");
107
108       // Integer to floating-point conversions.
109       // i64 conversions are done via library routines even when generating VFP
110       // instructions, so use the same ones.
111       // FIXME: There appears to be some naming inconsistency in ARM libgcc: e.g.
112       // __floatunsidf vs. __floatunssidfvfp.
113       setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
114       setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
115       setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
116       setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
117     }
118   }
119
120   addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
121   if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb()) {
122     addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
123     addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
124     
125     setTruncStoreAction(MVT::f64, MVT::f32, Expand);
126   }
127   computeRegisterProperties();
128
129   // ARM does not have f32 extending load.
130   setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
131
132   // ARM does not have i1 sign extending load.
133   setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
134
135   // ARM supports all 4 flavors of integer indexed load / store.
136   for (unsigned im = (unsigned)ISD::PRE_INC;
137        im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
138     setIndexedLoadAction(im,  MVT::i1,  Legal);
139     setIndexedLoadAction(im,  MVT::i8,  Legal);
140     setIndexedLoadAction(im,  MVT::i16, Legal);
141     setIndexedLoadAction(im,  MVT::i32, Legal);
142     setIndexedStoreAction(im, MVT::i1,  Legal);
143     setIndexedStoreAction(im, MVT::i8,  Legal);
144     setIndexedStoreAction(im, MVT::i16, Legal);
145     setIndexedStoreAction(im, MVT::i32, Legal);
146   }
147
148   // i64 operation support.
149   if (Subtarget->isThumb()) {
150     setOperationAction(ISD::MUL,     MVT::i64, Expand);
151     setOperationAction(ISD::MULHU,   MVT::i32, Expand);
152     setOperationAction(ISD::MULHS,   MVT::i32, Expand);
153     setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
154     setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
155   } else {
156     setOperationAction(ISD::MUL,     MVT::i64, Expand);
157     setOperationAction(ISD::MULHU,   MVT::i32, Expand);
158     if (!Subtarget->hasV6Ops())
159       setOperationAction(ISD::MULHS, MVT::i32, Expand);
160   }
161   setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
162   setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
163   setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
164   setOperationAction(ISD::SRL,       MVT::i64, Custom);
165   setOperationAction(ISD::SRA,       MVT::i64, Custom);
166
167   // ARM does not have ROTL.
168   setOperationAction(ISD::ROTL,  MVT::i32, Expand);
169   setOperationAction(ISD::CTTZ , MVT::i32, Expand);
170   setOperationAction(ISD::CTPOP, MVT::i32, Expand);
171   if (!Subtarget->hasV5TOps() || Subtarget->isThumb())
172     setOperationAction(ISD::CTLZ, MVT::i32, Expand);
173
174   // Only ARMv6 has BSWAP.
175   if (!Subtarget->hasV6Ops())
176     setOperationAction(ISD::BSWAP, MVT::i32, Expand);
177
178   // These are expanded into libcalls.
179   setOperationAction(ISD::SDIV,  MVT::i32, Expand);
180   setOperationAction(ISD::UDIV,  MVT::i32, Expand);
181   setOperationAction(ISD::SREM,  MVT::i32, Expand);
182   setOperationAction(ISD::UREM,  MVT::i32, Expand);
183   setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
184   setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
185   
186   // Support label based line numbers.
187   setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
188   setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
189
190   setOperationAction(ISD::RET,           MVT::Other, Custom);
191   setOperationAction(ISD::GlobalAddress, MVT::i32,   Custom);
192   setOperationAction(ISD::ConstantPool,  MVT::i32,   Custom);
193   setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
194   setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
195
196   // Use the default implementation.
197   setOperationAction(ISD::VASTART           , MVT::Other, Custom);
198   setOperationAction(ISD::VAARG             , MVT::Other, Expand);
199   setOperationAction(ISD::VACOPY            , MVT::Other, Expand);
200   setOperationAction(ISD::VAEND             , MVT::Other, Expand);
201   setOperationAction(ISD::STACKSAVE,          MVT::Other, Expand); 
202   setOperationAction(ISD::STACKRESTORE,       MVT::Other, Expand);
203   setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32  , Expand);
204   setOperationAction(ISD::MEMBARRIER        , MVT::Other, Expand);
205
206   if (!Subtarget->hasV6Ops()) {
207     setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
208     setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8,  Expand);
209   }
210   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
211
212   if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb())
213     // Turn f64->i64 into FMRRD, i64 -> f64 to FMDRR iff target supports vfp2.
214     setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
215
216   // We want to custom lower some of our intrinsics.
217   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
218
219   setOperationAction(ISD::SETCC    , MVT::i32, Expand);
220   setOperationAction(ISD::SETCC    , MVT::f32, Expand);
221   setOperationAction(ISD::SETCC    , MVT::f64, Expand);
222   setOperationAction(ISD::SELECT   , MVT::i32, Expand);
223   setOperationAction(ISD::SELECT   , MVT::f32, Expand);
224   setOperationAction(ISD::SELECT   , MVT::f64, Expand);
225   setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
226   setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
227   setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
228
229   setOperationAction(ISD::BRCOND   , MVT::Other, Expand);
230   setOperationAction(ISD::BR_CC    , MVT::i32,   Custom);
231   setOperationAction(ISD::BR_CC    , MVT::f32,   Custom);
232   setOperationAction(ISD::BR_CC    , MVT::f64,   Custom);
233   setOperationAction(ISD::BR_JT    , MVT::Other, Custom);
234
235   // We don't support sin/cos/fmod/copysign/pow
236   setOperationAction(ISD::FSIN     , MVT::f64, Expand);
237   setOperationAction(ISD::FSIN     , MVT::f32, Expand);
238   setOperationAction(ISD::FCOS     , MVT::f32, Expand);
239   setOperationAction(ISD::FCOS     , MVT::f64, Expand);
240   setOperationAction(ISD::FREM     , MVT::f64, Expand);
241   setOperationAction(ISD::FREM     , MVT::f32, Expand);
242   if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb()) {
243     setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
244     setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
245   }
246   setOperationAction(ISD::FPOW     , MVT::f64, Expand);
247   setOperationAction(ISD::FPOW     , MVT::f32, Expand);
248   
249   // int <-> fp are custom expanded into bit_convert + ARMISD ops.
250   if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb()) {
251     setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
252     setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
253     setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
254     setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
255   }
256
257   // We have target-specific dag combine patterns for the following nodes:
258   // ARMISD::FMRRD  - No need to call setTargetDAGCombine
259   
260   setStackPointerRegisterToSaveRestore(ARM::SP);
261   setSchedulingPreference(SchedulingForRegPressure);
262   setIfCvtBlockSizeLimit(Subtarget->isThumb() ? 0 : 10);
263   setIfCvtDupBlockSizeLimit(Subtarget->isThumb() ? 0 : 2);
264
265   maxStoresPerMemcpy = 1;   //// temporary - rewrite interface to use type
266 }
267
268
269 const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
270   switch (Opcode) {
271   default: return 0;
272   case ARMISD::Wrapper:       return "ARMISD::Wrapper";
273   case ARMISD::WrapperJT:     return "ARMISD::WrapperJT";
274   case ARMISD::CALL:          return "ARMISD::CALL";
275   case ARMISD::CALL_PRED:     return "ARMISD::CALL_PRED";
276   case ARMISD::CALL_NOLINK:   return "ARMISD::CALL_NOLINK";
277   case ARMISD::tCALL:         return "ARMISD::tCALL";
278   case ARMISD::BRCOND:        return "ARMISD::BRCOND";
279   case ARMISD::BR_JT:         return "ARMISD::BR_JT";
280   case ARMISD::RET_FLAG:      return "ARMISD::RET_FLAG";
281   case ARMISD::PIC_ADD:       return "ARMISD::PIC_ADD";
282   case ARMISD::CMP:           return "ARMISD::CMP";
283   case ARMISD::CMPNZ:         return "ARMISD::CMPNZ";
284   case ARMISD::CMPFP:         return "ARMISD::CMPFP";
285   case ARMISD::CMPFPw0:       return "ARMISD::CMPFPw0";
286   case ARMISD::FMSTAT:        return "ARMISD::FMSTAT";
287   case ARMISD::CMOV:          return "ARMISD::CMOV";
288   case ARMISD::CNEG:          return "ARMISD::CNEG";
289     
290   case ARMISD::FTOSI:         return "ARMISD::FTOSI";
291   case ARMISD::FTOUI:         return "ARMISD::FTOUI";
292   case ARMISD::SITOF:         return "ARMISD::SITOF";
293   case ARMISD::UITOF:         return "ARMISD::UITOF";
294
295   case ARMISD::SRL_FLAG:      return "ARMISD::SRL_FLAG";
296   case ARMISD::SRA_FLAG:      return "ARMISD::SRA_FLAG";
297   case ARMISD::RRX:           return "ARMISD::RRX";
298       
299   case ARMISD::FMRRD:         return "ARMISD::FMRRD";
300   case ARMISD::FMDRR:         return "ARMISD::FMDRR";
301
302   case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
303   }
304 }
305
306 //===----------------------------------------------------------------------===//
307 // Lowering Code
308 //===----------------------------------------------------------------------===//
309
310
311 /// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
312 static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
313   switch (CC) {
314   default: assert(0 && "Unknown condition code!");
315   case ISD::SETNE:  return ARMCC::NE;
316   case ISD::SETEQ:  return ARMCC::EQ;
317   case ISD::SETGT:  return ARMCC::GT;
318   case ISD::SETGE:  return ARMCC::GE;
319   case ISD::SETLT:  return ARMCC::LT;
320   case ISD::SETLE:  return ARMCC::LE;
321   case ISD::SETUGT: return ARMCC::HI;
322   case ISD::SETUGE: return ARMCC::HS;
323   case ISD::SETULT: return ARMCC::LO;
324   case ISD::SETULE: return ARMCC::LS;
325   }
326 }
327
328 /// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC. It
329 /// returns true if the operands should be inverted to form the proper
330 /// comparison.
331 static bool FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
332                         ARMCC::CondCodes &CondCode2) {
333   bool Invert = false;
334   CondCode2 = ARMCC::AL;
335   switch (CC) {
336   default: assert(0 && "Unknown FP condition!");
337   case ISD::SETEQ:
338   case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
339   case ISD::SETGT:
340   case ISD::SETOGT: CondCode = ARMCC::GT; break;
341   case ISD::SETGE:
342   case ISD::SETOGE: CondCode = ARMCC::GE; break;
343   case ISD::SETOLT: CondCode = ARMCC::MI; break;
344   case ISD::SETOLE: CondCode = ARMCC::GT; Invert = true; break;
345   case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
346   case ISD::SETO:   CondCode = ARMCC::VC; break;
347   case ISD::SETUO:  CondCode = ARMCC::VS; break;
348   case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
349   case ISD::SETUGT: CondCode = ARMCC::HI; break;
350   case ISD::SETUGE: CondCode = ARMCC::PL; break;
351   case ISD::SETLT:
352   case ISD::SETULT: CondCode = ARMCC::LT; break;
353   case ISD::SETLE:
354   case ISD::SETULE: CondCode = ARMCC::LE; break;
355   case ISD::SETNE:
356   case ISD::SETUNE: CondCode = ARMCC::NE; break;
357   }
358   return Invert;
359 }
360
361 static void
362 HowToPassArgument(MVT ObjectVT, unsigned NumGPRs,
363                   unsigned StackOffset, unsigned &NeededGPRs,
364                   unsigned &NeededStackSize, unsigned &GPRPad,
365                   unsigned &StackPad, ISD::ArgFlagsTy Flags) {
366   NeededStackSize = 0;
367   NeededGPRs = 0;
368   StackPad = 0;
369   GPRPad = 0;
370   unsigned align = Flags.getOrigAlign();
371   GPRPad = NumGPRs % ((align + 3)/4);
372   StackPad = StackOffset % align;
373   unsigned firstGPR = NumGPRs + GPRPad;
374   switch (ObjectVT.getSimpleVT()) {
375   default: assert(0 && "Unhandled argument type!");
376   case MVT::i32:
377   case MVT::f32:
378     if (firstGPR < 4)
379       NeededGPRs = 1;
380     else
381       NeededStackSize = 4;
382     break;
383   case MVT::i64:
384   case MVT::f64:
385     if (firstGPR < 3)
386       NeededGPRs = 2;
387     else if (firstGPR == 3) {
388       NeededGPRs = 1;
389       NeededStackSize = 4;
390     } else
391       NeededStackSize = 8;
392   }
393 }
394
395 /// LowerCALL - Lowering a ISD::CALL node into a callseq_start <-
396 /// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
397 /// nodes.
398 SDValue ARMTargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
399   CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
400   MVT RetVT = TheCall->getRetValType(0);
401   SDValue Chain    = TheCall->getChain();
402   assert((TheCall->getCallingConv() == CallingConv::C ||
403           TheCall->getCallingConv() == CallingConv::Fast) &&
404          "unknown calling convention");
405   SDValue Callee   = TheCall->getCallee();
406   unsigned NumOps   = TheCall->getNumArgs();
407   DebugLoc dl       = TheCall->getDebugLoc();
408   unsigned ArgOffset = 0;   // Frame mechanisms handle retaddr slot
409   unsigned NumGPRs = 0;     // GPRs used for parameter passing.
410
411   // Count how many bytes are to be pushed on the stack.
412   unsigned NumBytes = 0;
413
414   // Add up all the space actually used.
415   for (unsigned i = 0; i < NumOps; ++i) {
416     unsigned ObjSize;
417     unsigned ObjGPRs;
418     unsigned StackPad;
419     unsigned GPRPad;
420     MVT ObjectVT = TheCall->getArg(i).getValueType();
421     ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
422     HowToPassArgument(ObjectVT, NumGPRs, NumBytes, ObjGPRs, ObjSize,
423                       GPRPad, StackPad, Flags);
424     NumBytes += ObjSize + StackPad;
425     NumGPRs += ObjGPRs + GPRPad;
426   }
427
428   // Adjust the stack pointer for the new arguments...
429   // These operations are automatically eliminated by the prolog/epilog pass
430   Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
431
432   SDValue StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
433
434   static const unsigned GPRArgRegs[] = {
435     ARM::R0, ARM::R1, ARM::R2, ARM::R3
436   };
437
438   NumGPRs = 0;
439   std::vector<std::pair<unsigned, SDValue> > RegsToPass;
440   std::vector<SDValue> MemOpChains;
441   for (unsigned i = 0; i != NumOps; ++i) {
442     SDValue Arg = TheCall->getArg(i);
443     ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
444     MVT ArgVT = Arg.getValueType();
445
446     unsigned ObjSize;
447     unsigned ObjGPRs;
448     unsigned GPRPad;
449     unsigned StackPad;
450     HowToPassArgument(ArgVT, NumGPRs, ArgOffset, ObjGPRs,
451                       ObjSize, GPRPad, StackPad, Flags);
452     NumGPRs += GPRPad;
453     ArgOffset += StackPad;
454     if (ObjGPRs > 0) {
455       switch (ArgVT.getSimpleVT()) {
456       default: assert(0 && "Unexpected ValueType for argument!");
457       case MVT::i32:
458         RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Arg));
459         break;
460       case MVT::f32:
461         RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs],
462                              DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Arg)));
463         break;
464       case MVT::i64: {
465         SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Arg,
466                                    DAG.getConstant(0, getPointerTy()));
467         SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Arg,
468                                    DAG.getConstant(1, getPointerTy()));
469         RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Lo));
470         if (ObjGPRs == 2)
471           RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs+1], Hi));
472         else {
473           SDValue PtrOff= DAG.getConstant(ArgOffset, StackPtr.getValueType());
474           PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
475           MemOpChains.push_back(DAG.getStore(Chain, dl, Hi, PtrOff, NULL, 0));
476         }
477         break;
478       }
479       case MVT::f64: {
480         SDValue Cvt = DAG.getNode(ARMISD::FMRRD, dl,
481                                     DAG.getVTList(MVT::i32, MVT::i32),
482                                     &Arg, 1);
483         RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs], Cvt));
484         if (ObjGPRs == 2)
485           RegsToPass.push_back(std::make_pair(GPRArgRegs[NumGPRs+1],
486                                               Cvt.getValue(1)));
487         else {
488           SDValue PtrOff= DAG.getConstant(ArgOffset, StackPtr.getValueType());
489           PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
490           MemOpChains.push_back(DAG.getStore(Chain, dl, Cvt.getValue(1), PtrOff,
491                                              NULL, 0));
492         }
493         break;
494       }
495       }
496     } else {
497       assert(ObjSize != 0);
498       SDValue PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
499       PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
500       MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff, NULL, 0));
501     }
502
503     NumGPRs += ObjGPRs;
504     ArgOffset += ObjSize;
505   }
506
507   if (!MemOpChains.empty())
508     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
509                         &MemOpChains[0], MemOpChains.size());
510
511   // Build a sequence of copy-to-reg nodes chained together with token chain
512   // and flag operands which copy the outgoing args into the appropriate regs.
513   SDValue InFlag;
514   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
515     Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, 
516                              RegsToPass[i].second, InFlag);
517     InFlag = Chain.getValue(1);
518   }
519
520   // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
521   // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
522   // node so that legalize doesn't hack it.
523   bool isDirect = false;
524   bool isARMFunc = false;
525   bool isLocalARMFunc = false;
526   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
527     GlobalValue *GV = G->getGlobal();
528     isDirect = true;
529     bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
530                   GV->hasLinkOnceLinkage());
531     bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
532                    getTargetMachine().getRelocationModel() != Reloc::Static;
533     isARMFunc = !Subtarget->isThumb() || isStub;
534     // ARM call to a local ARM function is predicable.
535     isLocalARMFunc = !Subtarget->isThumb() && !isExt;
536     // tBX takes a register source operand.
537     if (isARMFunc && Subtarget->isThumb() && !Subtarget->hasV5TOps()) {
538       ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
539                                                            ARMCP::CPStub, 4);
540       SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 2);
541       CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
542       Callee = DAG.getLoad(getPointerTy(), dl, 
543                            DAG.getEntryNode(), CPAddr, NULL, 0); 
544       SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
545       Callee = DAG.getNode(ARMISD::PIC_ADD, dl, 
546                            getPointerTy(), Callee, PICLabel);
547    } else
548       Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
549   } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
550     isDirect = true;
551     bool isStub = Subtarget->isTargetDarwin() &&
552                   getTargetMachine().getRelocationModel() != Reloc::Static;
553     isARMFunc = !Subtarget->isThumb() || isStub;
554     // tBX takes a register source operand.
555     const char *Sym = S->getSymbol();
556     if (isARMFunc && Subtarget->isThumb() && !Subtarget->hasV5TOps()) {
557       ARMConstantPoolValue *CPV = new ARMConstantPoolValue(Sym, ARMPCLabelIndex,
558                                                            ARMCP::CPStub, 4);
559       SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 2);
560       CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
561       Callee = DAG.getLoad(getPointerTy(), dl,
562                            DAG.getEntryNode(), CPAddr, NULL, 0); 
563       SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
564       Callee = DAG.getNode(ARMISD::PIC_ADD, dl, 
565                            getPointerTy(), Callee, PICLabel);
566     } else
567       Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
568   }
569
570   // FIXME: handle tail calls differently.
571   unsigned CallOpc;
572   if (Subtarget->isThumb()) {
573     if (!Subtarget->hasV5TOps() && (!isDirect || isARMFunc))
574       CallOpc = ARMISD::CALL_NOLINK;
575     else
576       CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
577   } else {
578     CallOpc = (isDirect || Subtarget->hasV5TOps())
579       ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
580       : ARMISD::CALL_NOLINK;
581   }
582   if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb()) {
583     // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
584     Chain = DAG.getCopyToReg(Chain, dl, ARM::LR,
585                              DAG.getNode(ISD::UNDEF, MVT::i32), InFlag);
586     InFlag = Chain.getValue(1);
587   }
588
589   std::vector<SDValue> Ops;
590   Ops.push_back(Chain);
591   Ops.push_back(Callee);
592
593   // Add argument registers to the end of the list so that they are known live
594   // into the call.
595   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
596     Ops.push_back(DAG.getRegister(RegsToPass[i].first,
597                                   RegsToPass[i].second.getValueType()));
598
599   if (InFlag.getNode())
600     Ops.push_back(InFlag);
601   // Returns a chain and a flag for retval copy to use.
602   Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
603                       &Ops[0], Ops.size());
604   InFlag = Chain.getValue(1);
605
606   Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
607                              DAG.getIntPtrConstant(0, true), InFlag);
608   if (RetVT != MVT::Other)
609     InFlag = Chain.getValue(1);
610
611   std::vector<SDValue> ResultVals;
612
613   // If the call has results, copy the values out of the ret val registers.
614   switch (RetVT.getSimpleVT()) {
615   default: assert(0 && "Unexpected ret value!");
616   case MVT::Other:
617     break;
618   case MVT::i32:
619     Chain = DAG.getCopyFromReg(Chain, dl, ARM::R0, 
620                                MVT::i32, InFlag).getValue(1);
621     ResultVals.push_back(Chain.getValue(0));
622     if (TheCall->getNumRetVals() > 1 &&
623         TheCall->getRetValType(1) == MVT::i32) {
624       // Returns a i64 value.
625       Chain = DAG.getCopyFromReg(Chain, dl, ARM::R1, MVT::i32,
626                                  Chain.getValue(2)).getValue(1);
627       ResultVals.push_back(Chain.getValue(0));
628     }
629     break;
630   case MVT::f32:
631     Chain = DAG.getCopyFromReg(Chain, dl, ARM::R0, 
632                                MVT::i32, InFlag).getValue(1);
633     ResultVals.push_back(DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32,
634                                      Chain.getValue(0)));
635     break;
636   case MVT::f64: {
637     SDValue Lo = DAG.getCopyFromReg(Chain, dl, ARM::R0, MVT::i32, InFlag);
638     SDValue Hi = DAG.getCopyFromReg(Lo, dl, ARM::R1, MVT::i32, Lo.getValue(2));
639     ResultVals.push_back(DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi));
640     break;
641   }
642   }
643
644   if (ResultVals.empty())
645     return Chain;
646
647   ResultVals.push_back(Chain);
648   SDValue Res = DAG.getMergeValues(&ResultVals[0], ResultVals.size(), dl);
649   return Res.getValue(Op.getResNo());
650 }
651
652 static SDValue LowerRET(SDValue Op, SelectionDAG &DAG) {
653   SDValue Copy;
654   SDValue Chain = Op.getOperand(0);
655   DebugLoc dl = Op.getDebugLoc();
656   switch(Op.getNumOperands()) {
657   default:
658     assert(0 && "Do not know how to return this many arguments!");
659     abort();
660   case 1: {
661     SDValue LR = DAG.getRegister(ARM::LR, MVT::i32);
662     return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Chain);
663   }
664   case 3:
665     Op = Op.getOperand(1);
666     if (Op.getValueType() == MVT::f32) {
667       Op = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
668     } else if (Op.getValueType() == MVT::f64) {
669       // Legalize ret f64 -> ret 2 x i32.  We always have fmrrd if f64 is
670       // available.
671       Op = DAG.getNode(ARMISD::FMRRD, DAG.getVTList(MVT::i32, MVT::i32), &Op,1);
672       SDValue Sign = DAG.getConstant(0, MVT::i32);
673       return DAG.getNode(ISD::RET, MVT::Other, Chain, Op, Sign, 
674                          Op.getValue(1), Sign);
675     }
676     Copy = DAG.getCopyToReg(Chain, dl, ARM::R0, Op, SDValue());
677     if (DAG.getMachineFunction().getRegInfo().liveout_empty())
678       DAG.getMachineFunction().getRegInfo().addLiveOut(ARM::R0);
679     break;
680   case 5:
681     Copy = DAG.getCopyToReg(Chain, dl, ARM::R1, Op.getOperand(3), SDValue());
682     Copy = DAG.getCopyToReg(Copy, dl, ARM::R0, Op.getOperand(1), 
683                             Copy.getValue(1));
684     // If we haven't noted the R0+R1 are live out, do so now.
685     if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
686       DAG.getMachineFunction().getRegInfo().addLiveOut(ARM::R0);
687       DAG.getMachineFunction().getRegInfo().addLiveOut(ARM::R1);
688     }
689     break;
690   case 9:  // i128 -> 4 regs
691     Copy = DAG.getCopyToReg(Chain, dl, ARM::R3, Op.getOperand(7), SDValue());
692     Copy = DAG.getCopyToReg(Copy , dl, ARM::R2, Op.getOperand(5),
693                             Copy.getValue(1));
694     Copy = DAG.getCopyToReg(Copy , dl, ARM::R1, Op.getOperand(3),
695                             Copy.getValue(1));
696     Copy = DAG.getCopyToReg(Copy , dl, ARM::R0, Op.getOperand(1),
697                             Copy.getValue(1));
698     // If we haven't noted the R0+R1 are live out, do so now.
699     if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
700       DAG.getMachineFunction().getRegInfo().addLiveOut(ARM::R0);
701       DAG.getMachineFunction().getRegInfo().addLiveOut(ARM::R1);
702       DAG.getMachineFunction().getRegInfo().addLiveOut(ARM::R2);
703       DAG.getMachineFunction().getRegInfo().addLiveOut(ARM::R3);
704     }
705     break;
706       
707   }
708
709   //We must use RET_FLAG instead of BRIND because BRIND doesn't have a flag
710   return DAG.getNode(ARMISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
711 }
712
713 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as 
714 // their target countpart wrapped in the ARMISD::Wrapper node. Suppose N is
715 // one of the above mentioned nodes. It has to be wrapped because otherwise
716 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
717 // be used to form addressing mode. These wrapped nodes will be selected
718 // into MOVi.
719 static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
720   MVT PtrVT = Op.getValueType();
721   ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
722   SDValue Res;
723   if (CP->isMachineConstantPoolEntry())
724     Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
725                                     CP->getAlignment());
726   else
727     Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
728                                     CP->getAlignment());
729   return DAG.getNode(ARMISD::Wrapper, MVT::i32, Res);
730 }
731
732 // Lower ISD::GlobalTLSAddress using the "general dynamic" model
733 SDValue
734 ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
735                                                  SelectionDAG &DAG) {
736   DebugLoc dl = GA->getDebugLoc();
737   MVT PtrVT = getPointerTy();
738   unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
739   ARMConstantPoolValue *CPV =
740     new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue,
741                              PCAdj, "tlsgd", true);
742   SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 2);
743   Argument = DAG.getNode(ARMISD::Wrapper, MVT::i32, Argument);
744   Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument, NULL, 0);
745   SDValue Chain = Argument.getValue(1);
746
747   SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
748   Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
749
750   // call __tls_get_addr.
751   ArgListTy Args;
752   ArgListEntry Entry;
753   Entry.Node = Argument;
754   Entry.Ty = (const Type *) Type::Int32Ty;
755   Args.push_back(Entry);
756   // FIXME: is there useful debug info available here?
757   std::pair<SDValue, SDValue> CallResult =
758     LowerCallTo(Chain, (const Type *) Type::Int32Ty, false, false, false, false,
759                 CallingConv::C, false,
760                 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
761   return CallResult.first;
762 }
763
764 // Lower ISD::GlobalTLSAddress using the "initial exec" or
765 // "local exec" model.
766 SDValue
767 ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
768                                             SelectionDAG &DAG) {
769   GlobalValue *GV = GA->getGlobal();
770   DebugLoc dl = GA->getDebugLoc();
771   SDValue Offset;
772   SDValue Chain = DAG.getEntryNode();
773   MVT PtrVT = getPointerTy();
774   // Get the Thread Pointer
775   SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
776
777   if (GV->isDeclaration()){
778     // initial exec model
779     unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
780     ARMConstantPoolValue *CPV =
781       new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex, ARMCP::CPValue,
782                                PCAdj, "gottpoff", true);
783     Offset = DAG.getTargetConstantPool(CPV, PtrVT, 2);
784     Offset = DAG.getNode(ARMISD::Wrapper, MVT::i32, Offset);
785     Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
786     Chain = Offset.getValue(1);
787
788     SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
789     Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
790
791     Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
792   } else {
793     // local exec model
794     ARMConstantPoolValue *CPV =
795       new ARMConstantPoolValue(GV, ARMCP::CPValue, "tpoff");
796     Offset = DAG.getTargetConstantPool(CPV, PtrVT, 2);
797     Offset = DAG.getNode(ARMISD::Wrapper, MVT::i32, Offset);
798     Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
799   }
800
801   // The address of the thread local variable is the add of the thread
802   // pointer with the offset of the variable.
803   return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
804 }
805
806 SDValue
807 ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
808   // TODO: implement the "local dynamic" model
809   assert(Subtarget->isTargetELF() &&
810          "TLS not implemented for non-ELF targets");
811   GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
812   // If the relocation model is PIC, use the "General Dynamic" TLS Model,
813   // otherwise use the "Local Exec" TLS Model
814   if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
815     return LowerToTLSGeneralDynamicModel(GA, DAG);
816   else
817     return LowerToTLSExecModels(GA, DAG);
818 }
819
820 SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
821                                                    SelectionDAG &DAG) {
822   MVT PtrVT = getPointerTy();
823   DebugLoc dl = Op.getDebugLoc();
824   GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
825   Reloc::Model RelocM = getTargetMachine().getRelocationModel();
826   if (RelocM == Reloc::PIC_) {
827     bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
828     ARMConstantPoolValue *CPV =
829       new ARMConstantPoolValue(GV, ARMCP::CPValue, UseGOTOFF ? "GOTOFF":"GOT");
830     SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2);
831     CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
832     SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), 
833                                  CPAddr, NULL, 0);
834     SDValue Chain = Result.getValue(1);
835     SDValue GOT = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PtrVT);
836     Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
837     if (!UseGOTOFF)
838       Result = DAG.getLoad(PtrVT, dl, Chain, Result, NULL, 0);
839     return Result;
840   } else {
841     SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 2);
842     CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
843     return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
844   }
845 }
846
847 /// GVIsIndirectSymbol - true if the GV will be accessed via an indirect symbol
848 /// even in non-static mode.
849 static bool GVIsIndirectSymbol(GlobalValue *GV, Reloc::Model RelocM) {
850   // If symbol visibility is hidden, the extra load is not needed if
851   // the symbol is definitely defined in the current translation unit.
852   bool isDecl = GV->isDeclaration() && !GV->hasNotBeenReadFromBitcode();
853   if (GV->hasHiddenVisibility() && (!isDecl && !GV->hasCommonLinkage()))
854     return false;
855   return RelocM != Reloc::Static && (isDecl || GV->mayBeOverridden());
856 }
857
858 SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
859                                                       SelectionDAG &DAG) {
860   MVT PtrVT = getPointerTy();
861   DebugLoc dl = Op.getDebugLoc();
862   GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
863   Reloc::Model RelocM = getTargetMachine().getRelocationModel();
864   bool IsIndirect = GVIsIndirectSymbol(GV, RelocM);
865   SDValue CPAddr;
866   if (RelocM == Reloc::Static)
867     CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 2);
868   else {
869     unsigned PCAdj = (RelocM != Reloc::PIC_)
870       ? 0 : (Subtarget->isThumb() ? 4 : 8);
871     ARMCP::ARMCPKind Kind = IsIndirect ? ARMCP::CPNonLazyPtr
872       : ARMCP::CPValue;
873     ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMPCLabelIndex,
874                                                          Kind, PCAdj);
875     CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2);
876   }
877   CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
878
879   SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
880   SDValue Chain = Result.getValue(1);
881
882   if (RelocM == Reloc::PIC_) {
883     SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
884     Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
885   }
886   if (IsIndirect)
887     Result = DAG.getLoad(PtrVT, dl, Chain, Result, NULL, 0);
888
889   return Result;
890 }
891
892 SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
893                                                       SelectionDAG &DAG){
894   assert(Subtarget->isTargetELF() &&
895          "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
896   MVT PtrVT = getPointerTy();
897   DebugLoc dl = Op.getDebugLoc();
898   unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
899   ARMConstantPoolValue *CPV = new ARMConstantPoolValue("_GLOBAL_OFFSET_TABLE_",
900                                                        ARMPCLabelIndex,
901                                                        ARMCP::CPValue, PCAdj);
902   SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 2);
903   CPAddr = DAG.getNode(ARMISD::Wrapper, MVT::i32, CPAddr);
904   SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
905   SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
906   return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
907 }
908
909 static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
910   MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
911   unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
912   switch (IntNo) {
913   default: return SDValue();    // Don't custom lower most intrinsics.
914   case Intrinsic::arm_thread_pointer:
915       return DAG.getNode(ARMISD::THREAD_POINTER, PtrVT);
916   }
917 }
918
919 static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
920                               unsigned VarArgsFrameIndex) {
921   // vastart just stores the address of the VarArgsFrameIndex slot into the
922   // memory location argument.
923   DebugLoc dl = Op.getDebugLoc();
924   MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
925   SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
926   const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
927   return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
928 }
929
930 static SDValue LowerFORMAL_ARGUMENT(SDValue Op, SelectionDAG &DAG,
931                                       unsigned ArgNo, unsigned &NumGPRs,
932                                       unsigned &ArgOffset, DebugLoc dl) {
933   MachineFunction &MF = DAG.getMachineFunction();
934   MVT ObjectVT = Op.getValue(ArgNo).getValueType();
935   SDValue Root = Op.getOperand(0);
936   MachineRegisterInfo &RegInfo = MF.getRegInfo();
937
938   static const unsigned GPRArgRegs[] = {
939     ARM::R0, ARM::R1, ARM::R2, ARM::R3
940   };
941
942   unsigned ObjSize;
943   unsigned ObjGPRs;
944   unsigned GPRPad;
945   unsigned StackPad;
946   ISD::ArgFlagsTy Flags =
947     cast<ARG_FLAGSSDNode>(Op.getOperand(ArgNo + 3))->getArgFlags();
948   HowToPassArgument(ObjectVT, NumGPRs, ArgOffset, ObjGPRs,
949                     ObjSize, GPRPad, StackPad, Flags);
950   NumGPRs += GPRPad;
951   ArgOffset += StackPad;
952
953   SDValue ArgValue;
954   if (ObjGPRs == 1) {
955     unsigned VReg = RegInfo.createVirtualRegister(&ARM::GPRRegClass);
956     RegInfo.addLiveIn(GPRArgRegs[NumGPRs], VReg);
957     ArgValue = DAG.getCopyFromReg(Root, dl, VReg, MVT::i32);
958     if (ObjectVT == MVT::f32)
959       ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, ArgValue);
960   } else if (ObjGPRs == 2) {
961     unsigned VReg = RegInfo.createVirtualRegister(&ARM::GPRRegClass);
962     RegInfo.addLiveIn(GPRArgRegs[NumGPRs], VReg);
963     ArgValue = DAG.getCopyFromReg(Root, dl, VReg, MVT::i32);
964
965     VReg = RegInfo.createVirtualRegister(&ARM::GPRRegClass);
966     RegInfo.addLiveIn(GPRArgRegs[NumGPRs+1], VReg);
967     SDValue ArgValue2 = DAG.getCopyFromReg(Root, dl, VReg, MVT::i32);
968
969     assert(ObjectVT != MVT::i64 && "i64 should already be lowered");
970     ArgValue = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, ArgValue, ArgValue2);
971   }
972   NumGPRs += ObjGPRs;
973
974   if (ObjSize) {
975     MachineFrameInfo *MFI = MF.getFrameInfo();
976     int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
977     SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
978     if (ObjGPRs == 0)
979       ArgValue = DAG.getLoad(ObjectVT, dl, Root, FIN, NULL, 0);
980     else {
981       SDValue ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN, NULL, 0);
982       assert(ObjectVT != MVT::i64 && "i64 should already be lowered");
983       ArgValue = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, ArgValue, ArgValue2);
984     }
985
986     ArgOffset += ObjSize;   // Move on to the next argument.
987   }
988
989   return ArgValue;
990 }
991
992 SDValue
993 ARMTargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
994   std::vector<SDValue> ArgValues;
995   SDValue Root = Op.getOperand(0);
996   DebugLoc dl = Op.getDebugLoc();
997   unsigned ArgOffset = 0;   // Frame mechanisms handle retaddr slot
998   unsigned NumGPRs = 0;     // GPRs used for parameter passing.
999
1000   unsigned NumArgs = Op.getNode()->getNumValues()-1;
1001   for (unsigned ArgNo = 0; ArgNo < NumArgs; ++ArgNo)
1002     ArgValues.push_back(LowerFORMAL_ARGUMENT(Op, DAG, ArgNo,
1003                                              NumGPRs, ArgOffset, dl));
1004
1005   bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
1006   if (isVarArg) {
1007     static const unsigned GPRArgRegs[] = {
1008       ARM::R0, ARM::R1, ARM::R2, ARM::R3
1009     };
1010
1011     MachineFunction &MF = DAG.getMachineFunction();
1012     MachineRegisterInfo &RegInfo = MF.getRegInfo();
1013     MachineFrameInfo *MFI = MF.getFrameInfo();
1014     ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1015     unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1016     unsigned VARegSize = (4 - NumGPRs) * 4;
1017     unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
1018     if (VARegSaveSize) {
1019       // If this function is vararg, store any remaining integer argument regs
1020       // to their spots on the stack so that they may be loaded by deferencing
1021       // the result of va_next.
1022       AFI->setVarArgsRegSaveSize(VARegSaveSize);
1023       VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
1024                                                  VARegSaveSize - VARegSize);
1025       SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
1026
1027       SmallVector<SDValue, 4> MemOps;
1028       for (; NumGPRs < 4; ++NumGPRs) {
1029         unsigned VReg = RegInfo.createVirtualRegister(&ARM::GPRRegClass);
1030         RegInfo.addLiveIn(GPRArgRegs[NumGPRs], VReg);
1031         SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::i32);
1032         SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
1033         MemOps.push_back(Store);
1034         FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
1035                           DAG.getConstant(4, getPointerTy()));
1036       }
1037       if (!MemOps.empty())
1038         Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1039                            &MemOps[0], MemOps.size());
1040     } else
1041       // This will point to the next argument passed via stack.
1042       VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1043   }
1044
1045   ArgValues.push_back(Root);
1046
1047   // Return the new list of results.
1048   return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
1049                      &ArgValues[0], ArgValues.size());
1050 }
1051
1052 /// isFloatingPointZero - Return true if this is +0.0.
1053 static bool isFloatingPointZero(SDValue Op) {
1054   if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
1055     return CFP->getValueAPF().isPosZero();
1056   else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
1057     // Maybe this has already been legalized into the constant pool?
1058     if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
1059       SDValue WrapperOp = Op.getOperand(1).getOperand(0);
1060       if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1061         if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
1062           return CFP->getValueAPF().isPosZero();
1063     }
1064   }
1065   return false;
1066 }
1067
1068 static bool isLegalCmpImmediate(unsigned C, bool isThumb) {
1069   return ( isThumb && (C & ~255U) == 0) ||
1070          (!isThumb && ARM_AM::getSOImmVal(C) != -1);
1071 }
1072
1073 /// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1074 /// the given operands.
1075 static SDValue getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
1076                            SDValue &ARMCC, SelectionDAG &DAG, bool isThumb) {
1077   if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
1078     unsigned C = RHSC->getZExtValue();
1079     if (!isLegalCmpImmediate(C, isThumb)) {
1080       // Constant does not fit, try adjusting it by one?
1081       switch (CC) {
1082       default: break;
1083       case ISD::SETLT:
1084       case ISD::SETGE:
1085         if (isLegalCmpImmediate(C-1, isThumb)) {
1086           CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
1087           RHS = DAG.getConstant(C-1, MVT::i32);
1088         }
1089         break;
1090       case ISD::SETULT:
1091       case ISD::SETUGE:
1092         if (C > 0 && isLegalCmpImmediate(C-1, isThumb)) {
1093           CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
1094           RHS = DAG.getConstant(C-1, MVT::i32);
1095         }
1096         break;
1097       case ISD::SETLE:
1098       case ISD::SETGT:
1099         if (isLegalCmpImmediate(C+1, isThumb)) {
1100           CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
1101           RHS = DAG.getConstant(C+1, MVT::i32);
1102         }
1103         break;
1104       case ISD::SETULE:
1105       case ISD::SETUGT:
1106         if (C < 0xffffffff && isLegalCmpImmediate(C+1, isThumb)) {
1107           CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
1108           RHS = DAG.getConstant(C+1, MVT::i32);
1109         }
1110         break;
1111       }
1112     }
1113   }
1114
1115   ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
1116   ARMISD::NodeType CompareType;
1117   switch (CondCode) {
1118   default:
1119     CompareType = ARMISD::CMP;
1120     break;
1121   case ARMCC::EQ:
1122   case ARMCC::NE:
1123   case ARMCC::MI:
1124   case ARMCC::PL:
1125     // Uses only N and Z Flags
1126     CompareType = ARMISD::CMPNZ;
1127     break;
1128   }
1129   ARMCC = DAG.getConstant(CondCode, MVT::i32);
1130   return DAG.getNode(CompareType, MVT::Flag, LHS, RHS);
1131 }
1132
1133 /// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
1134 static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG) {
1135   SDValue Cmp;
1136   if (!isFloatingPointZero(RHS))
1137     Cmp = DAG.getNode(ARMISD::CMPFP, MVT::Flag, LHS, RHS);
1138   else
1139     Cmp = DAG.getNode(ARMISD::CMPFPw0, MVT::Flag, LHS);
1140   return DAG.getNode(ARMISD::FMSTAT, MVT::Flag, Cmp);
1141 }
1142
1143 static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
1144                                 const ARMSubtarget *ST) {
1145   MVT VT = Op.getValueType();
1146   SDValue LHS = Op.getOperand(0);
1147   SDValue RHS = Op.getOperand(1);
1148   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1149   SDValue TrueVal = Op.getOperand(2);
1150   SDValue FalseVal = Op.getOperand(3);
1151
1152   if (LHS.getValueType() == MVT::i32) {
1153     SDValue ARMCC;
1154     SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1155     SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb());
1156     return DAG.getNode(ARMISD::CMOV, VT, FalseVal, TrueVal, ARMCC, CCR, Cmp);
1157   }
1158
1159   ARMCC::CondCodes CondCode, CondCode2;
1160   if (FPCCToARMCC(CC, CondCode, CondCode2))
1161     std::swap(TrueVal, FalseVal);
1162
1163   SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1164   SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1165   SDValue Cmp = getVFPCmp(LHS, RHS, DAG);
1166   SDValue Result = DAG.getNode(ARMISD::CMOV, VT, FalseVal, TrueVal,
1167                                  ARMCC, CCR, Cmp);
1168   if (CondCode2 != ARMCC::AL) {
1169     SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
1170     // FIXME: Needs another CMP because flag can have but one use.
1171     SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG);
1172     Result = DAG.getNode(ARMISD::CMOV, VT, Result, TrueVal, ARMCC2, CCR, Cmp2);
1173   }
1174   return Result;
1175 }
1176
1177 static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
1178                             const ARMSubtarget *ST) {
1179   SDValue  Chain = Op.getOperand(0);
1180   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1181   SDValue    LHS = Op.getOperand(2);
1182   SDValue    RHS = Op.getOperand(3);
1183   SDValue   Dest = Op.getOperand(4);
1184
1185   if (LHS.getValueType() == MVT::i32) {
1186     SDValue ARMCC;
1187     SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1188     SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb());
1189     return DAG.getNode(ARMISD::BRCOND, MVT::Other, Chain, Dest, ARMCC, CCR,Cmp);
1190   }
1191
1192   assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
1193   ARMCC::CondCodes CondCode, CondCode2;
1194   if (FPCCToARMCC(CC, CondCode, CondCode2))
1195     // Swap the LHS/RHS of the comparison if needed.
1196     std::swap(LHS, RHS);
1197   
1198   SDValue Cmp = getVFPCmp(LHS, RHS, DAG);
1199   SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1200   SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1201   SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
1202   SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
1203   SDValue Res = DAG.getNode(ARMISD::BRCOND, VTList, Ops, 5);
1204   if (CondCode2 != ARMCC::AL) {
1205     ARMCC = DAG.getConstant(CondCode2, MVT::i32);
1206     SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
1207     Res = DAG.getNode(ARMISD::BRCOND, VTList, Ops, 5);
1208   }
1209   return Res;
1210 }
1211
1212 SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) {
1213   SDValue Chain = Op.getOperand(0);
1214   SDValue Table = Op.getOperand(1);
1215   SDValue Index = Op.getOperand(2);
1216   DebugLoc dl = Op.getDebugLoc();
1217
1218   MVT PTy = getPointerTy();
1219   JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1220   ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
1221   SDValue UId =  DAG.getConstant(AFI->createJumpTableUId(), PTy);
1222   SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
1223   Table = DAG.getNode(ARMISD::WrapperJT, MVT::i32, JTI, UId);
1224   Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
1225   SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
1226   bool isPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
1227   Addr = DAG.getLoad(isPIC ? (MVT)MVT::i32 : PTy, dl,
1228                      Chain, Addr, NULL, 0);
1229   Chain = Addr.getValue(1);
1230   if (isPIC)
1231     Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
1232   return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
1233 }
1234
1235 static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
1236   unsigned Opc =
1237     Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
1238   Op = DAG.getNode(Opc, MVT::f32, Op.getOperand(0));
1239   return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
1240 }
1241
1242 static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
1243   MVT VT = Op.getValueType();
1244   unsigned Opc =
1245     Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1246
1247   Op = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Op.getOperand(0));
1248   return DAG.getNode(Opc, VT, Op);
1249 }
1250
1251 static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
1252   // Implement fcopysign with a fabs and a conditional fneg.
1253   SDValue Tmp0 = Op.getOperand(0);
1254   SDValue Tmp1 = Op.getOperand(1);
1255   MVT VT = Op.getValueType();
1256   MVT SrcVT = Tmp1.getValueType();
1257   SDValue AbsVal = DAG.getNode(ISD::FABS, VT, Tmp0);
1258   SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG);
1259   SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
1260   SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1261   return DAG.getNode(ARMISD::CNEG, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
1262 }
1263
1264 SDValue
1265 ARMTargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
1266                                            SDValue Chain,
1267                                            SDValue Dst, SDValue Src,
1268                                            SDValue Size, unsigned Align,
1269                                            bool AlwaysInline,
1270                                          const Value *DstSV, uint64_t DstSVOff,
1271                                          const Value *SrcSV, uint64_t SrcSVOff){
1272   // Do repeated 4-byte loads and stores. To be improved.
1273   // This requires 4-byte alignment.
1274   if ((Align & 3) != 0)
1275     return SDValue();
1276   // This requires the copy size to be a constant, preferrably
1277   // within a subtarget-specific limit.
1278   ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
1279   if (!ConstantSize)
1280     return SDValue();
1281   uint64_t SizeVal = ConstantSize->getZExtValue();
1282   if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
1283     return SDValue();
1284
1285   unsigned BytesLeft = SizeVal & 3;
1286   unsigned NumMemOps = SizeVal >> 2;
1287   unsigned EmittedNumMemOps = 0;
1288   MVT VT = MVT::i32;
1289   unsigned VTSize = 4;
1290   unsigned i = 0;
1291   const unsigned MAX_LOADS_IN_LDM = 6;
1292   SDValue TFOps[MAX_LOADS_IN_LDM];
1293   SDValue Loads[MAX_LOADS_IN_LDM];
1294   uint64_t SrcOff = 0, DstOff = 0;
1295
1296   // Emit up to MAX_LOADS_IN_LDM loads, then a TokenFactor barrier, then the
1297   // same number of stores.  The loads and stores will get combined into
1298   // ldm/stm later on.
1299   while (EmittedNumMemOps < NumMemOps) {
1300     for (i = 0;
1301          i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
1302       Loads[i] = DAG.getLoad(VT, dl, Chain,
1303                              DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
1304                                          DAG.getConstant(SrcOff, MVT::i32)),
1305                              SrcSV, SrcSVOff + SrcOff);
1306       TFOps[i] = Loads[i].getValue(1);
1307       SrcOff += VTSize;
1308     }
1309     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
1310
1311     for (i = 0;
1312          i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
1313       TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
1314                            DAG.getNode(ISD::ADD, dl, MVT::i32, Dst, 
1315                                        DAG.getConstant(DstOff, MVT::i32)),
1316                            DstSV, DstSVOff + DstOff);
1317       DstOff += VTSize;
1318     }
1319     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
1320
1321     EmittedNumMemOps += i;
1322   }
1323
1324   if (BytesLeft == 0) 
1325     return Chain;
1326
1327   // Issue loads / stores for the trailing (1 - 3) bytes.
1328   unsigned BytesLeftSave = BytesLeft;
1329   i = 0;
1330   while (BytesLeft) {
1331     if (BytesLeft >= 2) {
1332       VT = MVT::i16;
1333       VTSize = 2;
1334     } else {
1335       VT = MVT::i8;
1336       VTSize = 1;
1337     }
1338
1339     Loads[i] = DAG.getLoad(VT, dl, Chain,
1340                            DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
1341                                        DAG.getConstant(SrcOff, MVT::i32)),
1342                            SrcSV, SrcSVOff + SrcOff);
1343     TFOps[i] = Loads[i].getValue(1);
1344     ++i;
1345     SrcOff += VTSize;
1346     BytesLeft -= VTSize;
1347   }
1348   Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
1349
1350   i = 0;
1351   BytesLeft = BytesLeftSave;
1352   while (BytesLeft) {
1353     if (BytesLeft >= 2) {
1354       VT = MVT::i16;
1355       VTSize = 2;
1356     } else {
1357       VT = MVT::i8;
1358       VTSize = 1;
1359     }
1360
1361     TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
1362                             DAG.getNode(ISD::ADD, dl, MVT::i32, Dst, 
1363                                         DAG.getConstant(DstOff, MVT::i32)),
1364                             DstSV, DstSVOff + DstOff);
1365     ++i;
1366     DstOff += VTSize;
1367     BytesLeft -= VTSize;
1368   }
1369   return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
1370 }
1371
1372 static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
1373   SDValue Op = N->getOperand(0);
1374   if (N->getValueType(0) == MVT::f64) {
1375     // Turn i64->f64 into FMDRR.
1376     SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
1377                              DAG.getConstant(0, MVT::i32));
1378     SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
1379                              DAG.getConstant(1, MVT::i32));
1380     return DAG.getNode(ARMISD::FMDRR, MVT::f64, Lo, Hi);
1381   }
1382   
1383   // Turn f64->i64 into FMRRD.
1384   SDValue Cvt = DAG.getNode(ARMISD::FMRRD, DAG.getVTList(MVT::i32, MVT::i32),
1385                             &Op, 1);
1386   
1387   // Merge the pieces into a single i64 value.
1388   return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Cvt, Cvt.getValue(1));
1389 }
1390
1391 static SDValue ExpandSRx(SDNode *N, SelectionDAG &DAG, const ARMSubtarget *ST) {
1392   assert(N->getValueType(0) == MVT::i64 &&
1393          (N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
1394          "Unknown shift to lower!");
1395
1396   // We only lower SRA, SRL of 1 here, all others use generic lowering.
1397   if (!isa<ConstantSDNode>(N->getOperand(1)) ||
1398       cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
1399     return SDValue();
1400   
1401   // If we are in thumb mode, we don't have RRX.
1402   if (ST->isThumb()) return SDValue();
1403   
1404   // Okay, we have a 64-bit SRA or SRL of 1.  Lower this to an RRX expr.
1405   SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(0),
1406                              DAG.getConstant(0, MVT::i32));
1407   SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(0),
1408                              DAG.getConstant(1, MVT::i32));
1409   
1410   // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
1411   // captures the result into a carry flag.
1412   unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
1413   Hi = DAG.getNode(Opc, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
1414   
1415   // The low part is an ARMISD::RRX operand, which shifts the carry in.
1416   Lo = DAG.getNode(ARMISD::RRX, MVT::i32, Lo, Hi.getValue(1));
1417   
1418   // Merge the pieces into a single i64 value.
1419  return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1420 }
1421
1422
1423 SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
1424   switch (Op.getOpcode()) {
1425   default: assert(0 && "Don't know how to custom lower this!"); abort();
1426   case ISD::ConstantPool:  return LowerConstantPool(Op, DAG);
1427   case ISD::GlobalAddress:
1428     return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
1429       LowerGlobalAddressELF(Op, DAG);
1430   case ISD::GlobalTLSAddress:   return LowerGlobalTLSAddress(Op, DAG);
1431   case ISD::CALL:          return LowerCALL(Op, DAG);
1432   case ISD::RET:           return LowerRET(Op, DAG);
1433   case ISD::SELECT_CC:     return LowerSELECT_CC(Op, DAG, Subtarget);
1434   case ISD::BR_CC:         return LowerBR_CC(Op, DAG, Subtarget);
1435   case ISD::BR_JT:         return LowerBR_JT(Op, DAG);
1436   case ISD::VASTART:       return LowerVASTART(Op, DAG, VarArgsFrameIndex);
1437   case ISD::SINT_TO_FP:
1438   case ISD::UINT_TO_FP:    return LowerINT_TO_FP(Op, DAG);
1439   case ISD::FP_TO_SINT:
1440   case ISD::FP_TO_UINT:    return LowerFP_TO_INT(Op, DAG);
1441   case ISD::FCOPYSIGN:     return LowerFCOPYSIGN(Op, DAG);
1442   case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
1443   case ISD::RETURNADDR:    break;
1444   case ISD::FRAMEADDR:     break;
1445   case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
1446   case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
1447   case ISD::BIT_CONVERT:   return ExpandBIT_CONVERT(Op.getNode(), DAG);
1448   case ISD::SRL:
1449   case ISD::SRA:           return ExpandSRx(Op.getNode(), DAG,Subtarget);
1450   }
1451   return SDValue();
1452 }
1453
1454
1455 /// ReplaceNodeResults - Replace the results of node with an illegal result
1456 /// type with new values built out of custom code.
1457 ///
1458 void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
1459                                            SmallVectorImpl<SDValue>&Results,
1460                                            SelectionDAG &DAG) {
1461   switch (N->getOpcode()) {
1462   default:
1463     assert(0 && "Don't know how to custom expand this!");
1464     return;
1465   case ISD::BIT_CONVERT:
1466     Results.push_back(ExpandBIT_CONVERT(N, DAG));
1467     return;
1468   case ISD::SRL:
1469   case ISD::SRA: {
1470     SDValue Res = ExpandSRx(N, DAG, Subtarget);
1471     if (Res.getNode())
1472       Results.push_back(Res);
1473     return;
1474   }
1475   }
1476 }
1477   
1478
1479 //===----------------------------------------------------------------------===//
1480 //                           ARM Scheduler Hooks
1481 //===----------------------------------------------------------------------===//
1482
1483 MachineBasicBlock *
1484 ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
1485                                            MachineBasicBlock *BB) {
1486   const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1487   switch (MI->getOpcode()) {
1488   default: assert(false && "Unexpected instr type to insert");
1489   case ARM::tMOVCCr: {
1490     // To "insert" a SELECT_CC instruction, we actually have to insert the
1491     // diamond control-flow pattern.  The incoming instruction knows the
1492     // destination vreg to set, the condition code register to branch on, the
1493     // true/false values to select between, and a branch opcode to use.
1494     const BasicBlock *LLVM_BB = BB->getBasicBlock();
1495     MachineFunction::iterator It = BB;
1496     ++It;
1497
1498     //  thisMBB:
1499     //  ...
1500     //   TrueVal = ...
1501     //   cmpTY ccX, r1, r2
1502     //   bCC copy1MBB
1503     //   fallthrough --> copy0MBB
1504     MachineBasicBlock *thisMBB  = BB;
1505     MachineFunction *F = BB->getParent();
1506     MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1507     MachineBasicBlock *sinkMBB  = F->CreateMachineBasicBlock(LLVM_BB);
1508     BuildMI(BB, TII->get(ARM::tBcc)).addMBB(sinkMBB)
1509       .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
1510     F->insert(It, copy0MBB);
1511     F->insert(It, sinkMBB);
1512     // Update machine-CFG edges by first adding all successors of the current
1513     // block to the new block which will contain the Phi node for the select.
1514     for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1515         e = BB->succ_end(); i != e; ++i)
1516       sinkMBB->addSuccessor(*i);
1517     // Next, remove all successors of the current block, and add the true
1518     // and fallthrough blocks as its successors.
1519     while(!BB->succ_empty())
1520       BB->removeSuccessor(BB->succ_begin());
1521     BB->addSuccessor(copy0MBB);
1522     BB->addSuccessor(sinkMBB);
1523
1524     //  copy0MBB:
1525     //   %FalseValue = ...
1526     //   # fallthrough to sinkMBB
1527     BB = copy0MBB;
1528
1529     // Update machine-CFG edges
1530     BB->addSuccessor(sinkMBB);
1531
1532     //  sinkMBB:
1533     //   %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1534     //  ...
1535     BB = sinkMBB;
1536     BuildMI(BB, TII->get(ARM::PHI), MI->getOperand(0).getReg())
1537       .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1538       .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1539
1540     F->DeleteMachineInstr(MI);   // The pseudo instruction is gone now.
1541     return BB;
1542   }
1543   }
1544 }
1545
1546 //===----------------------------------------------------------------------===//
1547 //                           ARM Optimization Hooks
1548 //===----------------------------------------------------------------------===//
1549
1550 /// PerformFMRRDCombine - Target-specific dag combine xforms for ARMISD::FMRRD.
1551 static SDValue PerformFMRRDCombine(SDNode *N, 
1552                                      TargetLowering::DAGCombinerInfo &DCI) {
1553   // fmrrd(fmdrr x, y) -> x,y
1554   SDValue InDouble = N->getOperand(0);
1555   if (InDouble.getOpcode() == ARMISD::FMDRR)
1556     return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
1557   return SDValue();
1558 }
1559
1560 SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
1561                                                DAGCombinerInfo &DCI) const {
1562   switch (N->getOpcode()) {
1563   default: break;
1564   case ARMISD::FMRRD: return PerformFMRRDCombine(N, DCI);
1565   }
1566   
1567   return SDValue();
1568 }
1569
1570
1571 /// isLegalAddressImmediate - Return true if the integer value can be used
1572 /// as the offset of the target addressing mode for load / store of the
1573 /// given type.
1574 static bool isLegalAddressImmediate(int64_t V, MVT VT,
1575                                     const ARMSubtarget *Subtarget) {
1576   if (V == 0)
1577     return true;
1578
1579   if (Subtarget->isThumb()) {
1580     if (V < 0)
1581       return false;
1582
1583     unsigned Scale = 1;
1584     switch (VT.getSimpleVT()) {
1585     default: return false;
1586     case MVT::i1:
1587     case MVT::i8:
1588       // Scale == 1;
1589       break;
1590     case MVT::i16:
1591       // Scale == 2;
1592       Scale = 2;
1593       break;
1594     case MVT::i32:
1595       // Scale == 4;
1596       Scale = 4;
1597       break;
1598     }
1599
1600     if ((V & (Scale - 1)) != 0)
1601       return false;
1602     V /= Scale;
1603     return V == (V & ((1LL << 5) - 1));
1604   }
1605
1606   if (V < 0)
1607     V = - V;
1608   switch (VT.getSimpleVT()) {
1609   default: return false;
1610   case MVT::i1:
1611   case MVT::i8:
1612   case MVT::i32:
1613     // +- imm12
1614     return V == (V & ((1LL << 12) - 1));
1615   case MVT::i16:
1616     // +- imm8
1617     return V == (V & ((1LL << 8) - 1));
1618   case MVT::f32:
1619   case MVT::f64:
1620     if (!Subtarget->hasVFP2())
1621       return false;
1622     if ((V & 3) != 0)
1623       return false;
1624     V >>= 2;
1625     return V == (V & ((1LL << 8) - 1));
1626   }
1627 }
1628
1629 /// isLegalAddressingMode - Return true if the addressing mode represented
1630 /// by AM is legal for this target, for a load/store of the specified type.
1631 bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM, 
1632                                               const Type *Ty) const {
1633   if (!isLegalAddressImmediate(AM.BaseOffs, getValueType(Ty, true), Subtarget))
1634     return false;
1635   
1636   // Can never fold addr of global into load/store.
1637   if (AM.BaseGV) 
1638     return false;
1639   
1640   switch (AM.Scale) {
1641   case 0:  // no scale reg, must be "r+i" or "r", or "i".
1642     break;
1643   case 1:
1644     if (Subtarget->isThumb())
1645       return false;
1646     // FALL THROUGH.
1647   default:
1648     // ARM doesn't support any R+R*scale+imm addr modes.
1649     if (AM.BaseOffs)
1650       return false;
1651     
1652     int Scale = AM.Scale;
1653     switch (getValueType(Ty).getSimpleVT()) {
1654     default: return false;
1655     case MVT::i1:
1656     case MVT::i8:
1657     case MVT::i32:
1658     case MVT::i64:
1659       // This assumes i64 is legalized to a pair of i32. If not (i.e.
1660       // ldrd / strd are used, then its address mode is same as i16.
1661       // r + r
1662       if (Scale < 0) Scale = -Scale;
1663       if (Scale == 1)
1664         return true;
1665       // r + r << imm
1666       return isPowerOf2_32(Scale & ~1);
1667     case MVT::i16:
1668       // r + r
1669       if (((unsigned)AM.HasBaseReg + Scale) <= 2)
1670         return true;
1671       return false;
1672       
1673     case MVT::isVoid:
1674       // Note, we allow "void" uses (basically, uses that aren't loads or
1675       // stores), because arm allows folding a scale into many arithmetic
1676       // operations.  This should be made more precise and revisited later.
1677       
1678       // Allow r << imm, but the imm has to be a multiple of two.
1679       if (AM.Scale & 1) return false;
1680       return isPowerOf2_32(AM.Scale);
1681     }
1682     break;
1683   }
1684   return true;
1685 }
1686
1687
1688 static bool getIndexedAddressParts(SDNode *Ptr, MVT VT,
1689                                    bool isSEXTLoad, SDValue &Base,
1690                                    SDValue &Offset, bool &isInc,
1691                                    SelectionDAG &DAG) {
1692   if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
1693     return false;
1694
1695   if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
1696     // AddressingMode 3
1697     Base = Ptr->getOperand(0);
1698     if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
1699       int RHSC = (int)RHS->getZExtValue();
1700       if (RHSC < 0 && RHSC > -256) {
1701         isInc = false;
1702         Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
1703         return true;
1704       }
1705     }
1706     isInc = (Ptr->getOpcode() == ISD::ADD);
1707     Offset = Ptr->getOperand(1);
1708     return true;
1709   } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
1710     // AddressingMode 2
1711     if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
1712       int RHSC = (int)RHS->getZExtValue();
1713       if (RHSC < 0 && RHSC > -0x1000) {
1714         isInc = false;
1715         Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
1716         Base = Ptr->getOperand(0);
1717         return true;
1718       }
1719     }
1720
1721     if (Ptr->getOpcode() == ISD::ADD) {
1722       isInc = true;
1723       ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
1724       if (ShOpcVal != ARM_AM::no_shift) {
1725         Base = Ptr->getOperand(1);
1726         Offset = Ptr->getOperand(0);
1727       } else {
1728         Base = Ptr->getOperand(0);
1729         Offset = Ptr->getOperand(1);
1730       }
1731       return true;
1732     }
1733
1734     isInc = (Ptr->getOpcode() == ISD::ADD);
1735     Base = Ptr->getOperand(0);
1736     Offset = Ptr->getOperand(1);
1737     return true;
1738   }
1739
1740   // FIXME: Use FLDM / FSTM to emulate indexed FP load / store.
1741   return false;
1742 }
1743
1744 /// getPreIndexedAddressParts - returns true by value, base pointer and
1745 /// offset pointer and addressing mode by reference if the node's address
1746 /// can be legally represented as pre-indexed load / store address.
1747 bool
1748 ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
1749                                              SDValue &Offset,
1750                                              ISD::MemIndexedMode &AM,
1751                                              SelectionDAG &DAG) const {
1752   if (Subtarget->isThumb())
1753     return false;
1754
1755   MVT VT;
1756   SDValue Ptr;
1757   bool isSEXTLoad = false;
1758   if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1759     Ptr = LD->getBasePtr();
1760     VT  = LD->getMemoryVT();
1761     isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
1762   } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
1763     Ptr = ST->getBasePtr();
1764     VT  = ST->getMemoryVT();
1765   } else
1766     return false;
1767
1768   bool isInc;
1769   bool isLegal = getIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base, Offset,
1770                                         isInc, DAG);
1771   if (isLegal) {
1772     AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
1773     return true;
1774   }
1775   return false;
1776 }
1777
1778 /// getPostIndexedAddressParts - returns true by value, base pointer and
1779 /// offset pointer and addressing mode by reference if this node can be
1780 /// combined with a load / store to form a post-indexed load / store.
1781 bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
1782                                                    SDValue &Base,
1783                                                    SDValue &Offset,
1784                                                    ISD::MemIndexedMode &AM,
1785                                                    SelectionDAG &DAG) const {
1786   if (Subtarget->isThumb())
1787     return false;
1788
1789   MVT VT;
1790   SDValue Ptr;
1791   bool isSEXTLoad = false;
1792   if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1793     VT  = LD->getMemoryVT();
1794     isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
1795   } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
1796     VT  = ST->getMemoryVT();
1797   } else
1798     return false;
1799
1800   bool isInc;
1801   bool isLegal = getIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
1802                                         isInc, DAG);
1803   if (isLegal) {
1804     AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
1805     return true;
1806   }
1807   return false;
1808 }
1809
1810 void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
1811                                                        const APInt &Mask,
1812                                                        APInt &KnownZero, 
1813                                                        APInt &KnownOne,
1814                                                        const SelectionDAG &DAG,
1815                                                        unsigned Depth) const {
1816   KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
1817   switch (Op.getOpcode()) {
1818   default: break;
1819   case ARMISD::CMOV: {
1820     // Bits are known zero/one if known on the LHS and RHS.
1821     DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1822     if (KnownZero == 0 && KnownOne == 0) return;
1823
1824     APInt KnownZeroRHS, KnownOneRHS;
1825     DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
1826                           KnownZeroRHS, KnownOneRHS, Depth+1);
1827     KnownZero &= KnownZeroRHS;
1828     KnownOne  &= KnownOneRHS;
1829     return;
1830   }
1831   }
1832 }
1833
1834 //===----------------------------------------------------------------------===//
1835 //                           ARM Inline Assembly Support
1836 //===----------------------------------------------------------------------===//
1837
1838 /// getConstraintType - Given a constraint letter, return the type of
1839 /// constraint it is for this target.
1840 ARMTargetLowering::ConstraintType
1841 ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
1842   if (Constraint.size() == 1) {
1843     switch (Constraint[0]) {
1844     default:  break;
1845     case 'l': return C_RegisterClass;
1846     case 'w': return C_RegisterClass;
1847     }
1848   }
1849   return TargetLowering::getConstraintType(Constraint);
1850 }
1851
1852 std::pair<unsigned, const TargetRegisterClass*> 
1853 ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
1854                                                 MVT VT) const {
1855   if (Constraint.size() == 1) {
1856     // GCC RS6000 Constraint Letters
1857     switch (Constraint[0]) {
1858     case 'l':
1859     // FIXME: in thumb mode, 'l' is only low-regs.
1860     // FALL THROUGH.
1861     case 'r':
1862       return std::make_pair(0U, ARM::GPRRegisterClass);
1863     case 'w':
1864       if (VT == MVT::f32)
1865         return std::make_pair(0U, ARM::SPRRegisterClass);
1866       if (VT == MVT::f64)
1867         return std::make_pair(0U, ARM::DPRRegisterClass);
1868       break;
1869     }
1870   }
1871   return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1872 }
1873
1874 std::vector<unsigned> ARMTargetLowering::
1875 getRegClassForInlineAsmConstraint(const std::string &Constraint,
1876                                   MVT VT) const {
1877   if (Constraint.size() != 1)
1878     return std::vector<unsigned>();
1879
1880   switch (Constraint[0]) {      // GCC ARM Constraint Letters
1881   default: break;
1882   case 'l':
1883   case 'r':
1884     return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
1885                                  ARM::R4, ARM::R5, ARM::R6, ARM::R7,
1886                                  ARM::R8, ARM::R9, ARM::R10, ARM::R11,
1887                                  ARM::R12, ARM::LR, 0);
1888   case 'w':
1889     if (VT == MVT::f32)
1890       return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
1891                                    ARM::S4, ARM::S5, ARM::S6, ARM::S7,
1892                                    ARM::S8, ARM::S9, ARM::S10, ARM::S11,
1893                                    ARM::S12,ARM::S13,ARM::S14,ARM::S15,
1894                                    ARM::S16,ARM::S17,ARM::S18,ARM::S19,
1895                                    ARM::S20,ARM::S21,ARM::S22,ARM::S23,
1896                                    ARM::S24,ARM::S25,ARM::S26,ARM::S27,
1897                                    ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
1898     if (VT == MVT::f64)
1899       return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
1900                                    ARM::D4, ARM::D5, ARM::D6, ARM::D7,
1901                                    ARM::D8, ARM::D9, ARM::D10,ARM::D11,
1902                                    ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
1903       break;
1904   }
1905
1906   return std::vector<unsigned>();
1907 }