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