1 //===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the interfaces that ARM uses to lower LLVM code into a
13 //===----------------------------------------------------------------------===//
16 #include "ARMAddressingModes.h"
17 #include "ARMConstantPoolValue.h"
18 #include "ARMISelLowering.h"
19 #include "ARMMachineFunctionInfo.h"
20 #include "ARMPerfectShuffle.h"
21 #include "ARMRegisterInfo.h"
22 #include "ARMSubtarget.h"
23 #include "ARMTargetMachine.h"
24 #include "ARMTargetObjectFile.h"
25 #include "llvm/CallingConv.h"
26 #include "llvm/Constants.h"
27 #include "llvm/Function.h"
28 #include "llvm/Instruction.h"
29 #include "llvm/Intrinsics.h"
30 #include "llvm/GlobalValue.h"
31 #include "llvm/CodeGen/CallingConvLower.h"
32 #include "llvm/CodeGen/MachineBasicBlock.h"
33 #include "llvm/CodeGen/MachineFrameInfo.h"
34 #include "llvm/CodeGen/MachineFunction.h"
35 #include "llvm/CodeGen/MachineInstrBuilder.h"
36 #include "llvm/CodeGen/MachineRegisterInfo.h"
37 #include "llvm/CodeGen/PseudoSourceValue.h"
38 #include "llvm/CodeGen/SelectionDAG.h"
39 #include "llvm/Target/TargetOptions.h"
40 #include "llvm/ADT/VectorExtras.h"
41 #include "llvm/Support/ErrorHandling.h"
42 #include "llvm/Support/MathExtras.h"
46 static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
47 CCValAssign::LocInfo &LocInfo,
48 ISD::ArgFlagsTy &ArgFlags,
50 static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
51 CCValAssign::LocInfo &LocInfo,
52 ISD::ArgFlagsTy &ArgFlags,
54 static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
55 CCValAssign::LocInfo &LocInfo,
56 ISD::ArgFlagsTy &ArgFlags,
58 static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
59 CCValAssign::LocInfo &LocInfo,
60 ISD::ArgFlagsTy &ArgFlags,
63 void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
64 EVT PromotedBitwiseVT) {
65 if (VT != PromotedLdStVT) {
66 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
67 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
68 PromotedLdStVT.getSimpleVT());
70 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
71 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
72 PromotedLdStVT.getSimpleVT());
75 EVT ElemTy = VT.getVectorElementType();
76 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
77 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
78 if (ElemTy == MVT::i8 || ElemTy == MVT::i16)
79 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
80 if (ElemTy != MVT::i32) {
81 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
82 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
83 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
84 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
86 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
87 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
88 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Custom);
89 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Expand);
91 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
92 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
93 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
96 // Promote all bit-wise operations.
97 if (VT.isInteger() && VT != PromotedBitwiseVT) {
98 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
99 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
100 PromotedBitwiseVT.getSimpleVT());
101 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
102 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
103 PromotedBitwiseVT.getSimpleVT());
104 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
105 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
106 PromotedBitwiseVT.getSimpleVT());
109 // Neon does not support vector divide/remainder operations.
110 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
111 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
112 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
113 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
114 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
115 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
118 void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
119 addRegisterClass(VT, ARM::DPRRegisterClass);
120 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
123 void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
124 addRegisterClass(VT, ARM::QPRRegisterClass);
125 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
128 static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
129 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
130 return new TargetLoweringObjectFileMachO();
131 return new ARMElfTargetObjectFile();
134 ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
135 : TargetLowering(TM, createTLOF(TM)), ARMPCLabelIndex(0) {
136 Subtarget = &TM.getSubtarget<ARMSubtarget>();
138 if (Subtarget->isTargetDarwin()) {
139 // Uses VFP for Thumb libfuncs if available.
140 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
141 // Single-precision floating-point arithmetic.
142 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
143 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
144 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
145 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
147 // Double-precision floating-point arithmetic.
148 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
149 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
150 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
151 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
153 // Single-precision comparisons.
154 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
155 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
156 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
157 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
158 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
159 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
160 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
161 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
163 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
164 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
165 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
166 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
167 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
168 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
169 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
170 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
172 // Double-precision comparisons.
173 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
174 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
175 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
176 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
177 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
178 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
179 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
180 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
182 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
183 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
184 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
185 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
186 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
187 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
188 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
189 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
191 // Floating-point to integer conversions.
192 // i64 conversions are done via library routines even when generating VFP
193 // instructions, so use the same ones.
194 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
195 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
196 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
197 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
199 // Conversions between floating types.
200 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
201 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
203 // Integer to floating-point conversions.
204 // i64 conversions are done via library routines even when generating VFP
205 // instructions, so use the same ones.
206 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
207 // e.g., __floatunsidf vs. __floatunssidfvfp.
208 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
209 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
210 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
211 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
215 // These libcalls are not available in 32-bit.
216 setLibcallName(RTLIB::SHL_I128, 0);
217 setLibcallName(RTLIB::SRL_I128, 0);
218 setLibcallName(RTLIB::SRA_I128, 0);
220 // Libcalls should use the AAPCS base standard ABI, even if hard float
221 // is in effect, as per the ARM RTABI specification, section 4.1.2.
222 if (Subtarget->isAAPCS_ABI()) {
223 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
224 setLibcallCallingConv(static_cast<RTLIB::Libcall>(i),
225 CallingConv::ARM_AAPCS);
229 if (Subtarget->isThumb1Only())
230 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
232 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
233 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
234 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
235 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
237 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
240 if (Subtarget->hasNEON()) {
241 addDRTypeForNEON(MVT::v2f32);
242 addDRTypeForNEON(MVT::v8i8);
243 addDRTypeForNEON(MVT::v4i16);
244 addDRTypeForNEON(MVT::v2i32);
245 addDRTypeForNEON(MVT::v1i64);
247 addQRTypeForNEON(MVT::v4f32);
248 addQRTypeForNEON(MVT::v2f64);
249 addQRTypeForNEON(MVT::v16i8);
250 addQRTypeForNEON(MVT::v8i16);
251 addQRTypeForNEON(MVT::v4i32);
252 addQRTypeForNEON(MVT::v2i64);
254 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
255 // neither Neon nor VFP support any arithmetic operations on it.
256 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
257 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
258 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
259 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
260 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
261 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
262 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
263 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
264 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
265 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
266 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
267 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
268 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
269 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
270 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
271 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
272 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
273 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
274 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
275 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
276 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
277 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
278 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
279 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
281 // Neon does not support some operations on v1i64 and v2i64 types.
282 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
283 setOperationAction(ISD::MUL, MVT::v2i64, Expand);
284 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
285 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
287 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
288 setTargetDAGCombine(ISD::SHL);
289 setTargetDAGCombine(ISD::SRL);
290 setTargetDAGCombine(ISD::SRA);
291 setTargetDAGCombine(ISD::SIGN_EXTEND);
292 setTargetDAGCombine(ISD::ZERO_EXTEND);
293 setTargetDAGCombine(ISD::ANY_EXTEND);
296 computeRegisterProperties();
298 // ARM does not have f32 extending load.
299 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
301 // ARM does not have i1 sign extending load.
302 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
304 // ARM supports all 4 flavors of integer indexed load / store.
305 if (!Subtarget->isThumb1Only()) {
306 for (unsigned im = (unsigned)ISD::PRE_INC;
307 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
308 setIndexedLoadAction(im, MVT::i1, Legal);
309 setIndexedLoadAction(im, MVT::i8, Legal);
310 setIndexedLoadAction(im, MVT::i16, Legal);
311 setIndexedLoadAction(im, MVT::i32, Legal);
312 setIndexedStoreAction(im, MVT::i1, Legal);
313 setIndexedStoreAction(im, MVT::i8, Legal);
314 setIndexedStoreAction(im, MVT::i16, Legal);
315 setIndexedStoreAction(im, MVT::i32, Legal);
319 // i64 operation support.
320 if (Subtarget->isThumb1Only()) {
321 setOperationAction(ISD::MUL, MVT::i64, Expand);
322 setOperationAction(ISD::MULHU, MVT::i32, Expand);
323 setOperationAction(ISD::MULHS, MVT::i32, Expand);
324 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
325 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
327 setOperationAction(ISD::MUL, MVT::i64, Expand);
328 setOperationAction(ISD::MULHU, MVT::i32, Expand);
329 if (!Subtarget->hasV6Ops())
330 setOperationAction(ISD::MULHS, MVT::i32, Expand);
332 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
333 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
334 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
335 setOperationAction(ISD::SRL, MVT::i64, Custom);
336 setOperationAction(ISD::SRA, MVT::i64, Custom);
338 // ARM does not have ROTL.
339 setOperationAction(ISD::ROTL, MVT::i32, Expand);
340 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
341 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
342 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
343 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
345 // Only ARMv6 has BSWAP.
346 if (!Subtarget->hasV6Ops())
347 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
349 // These are expanded into libcalls.
350 setOperationAction(ISD::SDIV, MVT::i32, Expand);
351 setOperationAction(ISD::UDIV, MVT::i32, Expand);
352 setOperationAction(ISD::SREM, MVT::i32, Expand);
353 setOperationAction(ISD::UREM, MVT::i32, Expand);
354 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
355 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
357 // Support label based line numbers.
358 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
359 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
361 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
362 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
363 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
364 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
366 // Use the default implementation.
367 setOperationAction(ISD::VASTART, MVT::Other, Custom);
368 setOperationAction(ISD::VAARG, MVT::Other, Expand);
369 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
370 setOperationAction(ISD::VAEND, MVT::Other, Expand);
371 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
372 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
373 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
374 // FIXME: Shouldn't need this, since no register is used, but the legalizer
375 // doesn't yet know how to not do that for SjLj.
376 setExceptionSelectorRegister(ARM::R0);
377 if (Subtarget->isThumb())
378 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
380 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
381 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
383 if (!Subtarget->hasV6Ops() && !Subtarget->isThumb2()) {
384 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
385 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
387 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
389 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only())
390 // Turn f64->i64 into FMRRD, i64 -> f64 to FMDRR iff target supports vfp2.
391 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
393 // We want to custom lower some of our intrinsics.
394 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
395 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
396 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
398 setOperationAction(ISD::SETCC, MVT::i32, Expand);
399 setOperationAction(ISD::SETCC, MVT::f32, Expand);
400 setOperationAction(ISD::SETCC, MVT::f64, Expand);
401 setOperationAction(ISD::SELECT, MVT::i32, Expand);
402 setOperationAction(ISD::SELECT, MVT::f32, Expand);
403 setOperationAction(ISD::SELECT, MVT::f64, Expand);
404 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
405 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
406 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
408 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
409 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
410 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
411 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
412 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
414 // We don't support sin/cos/fmod/copysign/pow
415 setOperationAction(ISD::FSIN, MVT::f64, Expand);
416 setOperationAction(ISD::FSIN, MVT::f32, Expand);
417 setOperationAction(ISD::FCOS, MVT::f32, Expand);
418 setOperationAction(ISD::FCOS, MVT::f64, Expand);
419 setOperationAction(ISD::FREM, MVT::f64, Expand);
420 setOperationAction(ISD::FREM, MVT::f32, Expand);
421 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
422 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
423 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
425 setOperationAction(ISD::FPOW, MVT::f64, Expand);
426 setOperationAction(ISD::FPOW, MVT::f32, Expand);
428 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
429 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
430 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
431 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
432 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
433 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
436 // We have target-specific dag combine patterns for the following nodes:
437 // ARMISD::FMRRD - No need to call setTargetDAGCombine
438 setTargetDAGCombine(ISD::ADD);
439 setTargetDAGCombine(ISD::SUB);
441 setStackPointerRegisterToSaveRestore(ARM::SP);
442 setSchedulingPreference(SchedulingForRegPressure);
444 // FIXME: If-converter should use instruction latency to determine
445 // profitability rather than relying on fixed limits.
446 if (Subtarget->getCPUString() == "generic") {
447 // Generic (and overly aggressive) if-conversion limits.
448 setIfCvtBlockSizeLimit(10);
449 setIfCvtDupBlockSizeLimit(2);
450 } else if (Subtarget->hasV6Ops()) {
451 setIfCvtBlockSizeLimit(2);
452 setIfCvtDupBlockSizeLimit(1);
454 setIfCvtBlockSizeLimit(3);
455 setIfCvtDupBlockSizeLimit(2);
458 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
459 // Do not enable CodePlacementOpt for now: it currently runs after the
460 // ARMConstantIslandPass and messes up branch relaxation and placement
461 // of constant islands.
462 // benefitFromCodePlacementOpt = true;
465 const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
468 case ARMISD::Wrapper: return "ARMISD::Wrapper";
469 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
470 case ARMISD::CALL: return "ARMISD::CALL";
471 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
472 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
473 case ARMISD::tCALL: return "ARMISD::tCALL";
474 case ARMISD::BRCOND: return "ARMISD::BRCOND";
475 case ARMISD::BR_JT: return "ARMISD::BR_JT";
476 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
477 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
478 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
479 case ARMISD::CMP: return "ARMISD::CMP";
480 case ARMISD::CMPZ: return "ARMISD::CMPZ";
481 case ARMISD::CMPFP: return "ARMISD::CMPFP";
482 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
483 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
484 case ARMISD::CMOV: return "ARMISD::CMOV";
485 case ARMISD::CNEG: return "ARMISD::CNEG";
487 case ARMISD::FTOSI: return "ARMISD::FTOSI";
488 case ARMISD::FTOUI: return "ARMISD::FTOUI";
489 case ARMISD::SITOF: return "ARMISD::SITOF";
490 case ARMISD::UITOF: return "ARMISD::UITOF";
492 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
493 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
494 case ARMISD::RRX: return "ARMISD::RRX";
496 case ARMISD::FMRRD: return "ARMISD::FMRRD";
497 case ARMISD::FMDRR: return "ARMISD::FMDRR";
499 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
501 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
503 case ARMISD::VCEQ: return "ARMISD::VCEQ";
504 case ARMISD::VCGE: return "ARMISD::VCGE";
505 case ARMISD::VCGEU: return "ARMISD::VCGEU";
506 case ARMISD::VCGT: return "ARMISD::VCGT";
507 case ARMISD::VCGTU: return "ARMISD::VCGTU";
508 case ARMISD::VTST: return "ARMISD::VTST";
510 case ARMISD::VSHL: return "ARMISD::VSHL";
511 case ARMISD::VSHRs: return "ARMISD::VSHRs";
512 case ARMISD::VSHRu: return "ARMISD::VSHRu";
513 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
514 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
515 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
516 case ARMISD::VSHRN: return "ARMISD::VSHRN";
517 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
518 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
519 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
520 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
521 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
522 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
523 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
524 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
525 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
526 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
527 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
528 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
529 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
530 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
531 case ARMISD::VDUP: return "ARMISD::VDUP";
532 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
533 case ARMISD::VEXT: return "ARMISD::VEXT";
534 case ARMISD::VREV64: return "ARMISD::VREV64";
535 case ARMISD::VREV32: return "ARMISD::VREV32";
536 case ARMISD::VREV16: return "ARMISD::VREV16";
537 case ARMISD::VZIP: return "ARMISD::VZIP";
538 case ARMISD::VUZP: return "ARMISD::VUZP";
539 case ARMISD::VTRN: return "ARMISD::VTRN";
543 /// getFunctionAlignment - Return the Log2 alignment of this function.
544 unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
545 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 1 : 2;
548 //===----------------------------------------------------------------------===//
550 //===----------------------------------------------------------------------===//
552 /// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
553 static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
555 default: llvm_unreachable("Unknown condition code!");
556 case ISD::SETNE: return ARMCC::NE;
557 case ISD::SETEQ: return ARMCC::EQ;
558 case ISD::SETGT: return ARMCC::GT;
559 case ISD::SETGE: return ARMCC::GE;
560 case ISD::SETLT: return ARMCC::LT;
561 case ISD::SETLE: return ARMCC::LE;
562 case ISD::SETUGT: return ARMCC::HI;
563 case ISD::SETUGE: return ARMCC::HS;
564 case ISD::SETULT: return ARMCC::LO;
565 case ISD::SETULE: return ARMCC::LS;
569 /// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
570 static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
571 ARMCC::CondCodes &CondCode2) {
572 CondCode2 = ARMCC::AL;
574 default: llvm_unreachable("Unknown FP condition!");
576 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
578 case ISD::SETOGT: CondCode = ARMCC::GT; break;
580 case ISD::SETOGE: CondCode = ARMCC::GE; break;
581 case ISD::SETOLT: CondCode = ARMCC::MI; break;
582 case ISD::SETOLE: CondCode = ARMCC::LS; break;
583 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
584 case ISD::SETO: CondCode = ARMCC::VC; break;
585 case ISD::SETUO: CondCode = ARMCC::VS; break;
586 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
587 case ISD::SETUGT: CondCode = ARMCC::HI; break;
588 case ISD::SETUGE: CondCode = ARMCC::PL; break;
590 case ISD::SETULT: CondCode = ARMCC::LT; break;
592 case ISD::SETULE: CondCode = ARMCC::LE; break;
594 case ISD::SETUNE: CondCode = ARMCC::NE; break;
598 //===----------------------------------------------------------------------===//
599 // Calling Convention Implementation
600 //===----------------------------------------------------------------------===//
602 #include "ARMGenCallingConv.inc"
604 // APCS f64 is in register pairs, possibly split to stack
605 static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
606 CCValAssign::LocInfo &LocInfo,
607 CCState &State, bool CanFail) {
608 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
610 // Try to get the first register.
611 if (unsigned Reg = State.AllocateReg(RegList, 4))
612 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
614 // For the 2nd half of a v2f64, do not fail.
618 // Put the whole thing on the stack.
619 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
620 State.AllocateStack(8, 4),
625 // Try to get the second register.
626 if (unsigned Reg = State.AllocateReg(RegList, 4))
627 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
629 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
630 State.AllocateStack(4, 4),
635 static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
636 CCValAssign::LocInfo &LocInfo,
637 ISD::ArgFlagsTy &ArgFlags,
639 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
641 if (LocVT == MVT::v2f64 &&
642 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
644 return true; // we handled it
647 // AAPCS f64 is in aligned register pairs
648 static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
649 CCValAssign::LocInfo &LocInfo,
650 CCState &State, bool CanFail) {
651 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
652 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
654 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
656 // For the 2nd half of a v2f64, do not just fail.
660 // Put the whole thing on the stack.
661 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
662 State.AllocateStack(8, 8),
668 for (i = 0; i < 2; ++i)
669 if (HiRegList[i] == Reg)
672 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
673 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
678 static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
679 CCValAssign::LocInfo &LocInfo,
680 ISD::ArgFlagsTy &ArgFlags,
682 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
684 if (LocVT == MVT::v2f64 &&
685 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
687 return true; // we handled it
690 static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
691 CCValAssign::LocInfo &LocInfo, CCState &State) {
692 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
693 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
695 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
697 return false; // we didn't handle it
700 for (i = 0; i < 2; ++i)
701 if (HiRegList[i] == Reg)
704 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
705 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
710 static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
711 CCValAssign::LocInfo &LocInfo,
712 ISD::ArgFlagsTy &ArgFlags,
714 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
716 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
718 return true; // we handled it
721 static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
722 CCValAssign::LocInfo &LocInfo,
723 ISD::ArgFlagsTy &ArgFlags,
725 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
729 /// CCAssignFnForNode - Selects the correct CCAssignFn for a the
730 /// given CallingConvention value.
731 CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
733 bool isVarArg) const {
736 llvm_unreachable("Unsupported calling convention");
738 case CallingConv::Fast:
739 // Use target triple & subtarget features to do actual dispatch.
740 if (Subtarget->isAAPCS_ABI()) {
741 if (Subtarget->hasVFP2() &&
742 FloatABIType == FloatABI::Hard && !isVarArg)
743 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
745 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
747 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
748 case CallingConv::ARM_AAPCS_VFP:
749 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
750 case CallingConv::ARM_AAPCS:
751 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
752 case CallingConv::ARM_APCS:
753 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
757 /// LowerCallResult - Lower the result values of a call into the
758 /// appropriate copies out of appropriate physical registers.
760 ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
761 CallingConv::ID CallConv, bool isVarArg,
762 const SmallVectorImpl<ISD::InputArg> &Ins,
763 DebugLoc dl, SelectionDAG &DAG,
764 SmallVectorImpl<SDValue> &InVals) {
766 // Assign locations to each value returned by this call.
767 SmallVector<CCValAssign, 16> RVLocs;
768 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
769 RVLocs, *DAG.getContext());
770 CCInfo.AnalyzeCallResult(Ins,
771 CCAssignFnForNode(CallConv, /* Return*/ true,
774 // Copy all of the result registers out of their specified physreg.
775 for (unsigned i = 0; i != RVLocs.size(); ++i) {
776 CCValAssign VA = RVLocs[i];
779 if (VA.needsCustom()) {
780 // Handle f64 or half of a v2f64.
781 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
783 Chain = Lo.getValue(1);
784 InFlag = Lo.getValue(2);
785 VA = RVLocs[++i]; // skip ahead to next loc
786 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
788 Chain = Hi.getValue(1);
789 InFlag = Hi.getValue(2);
790 Val = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
792 if (VA.getLocVT() == MVT::v2f64) {
793 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
794 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
795 DAG.getConstant(0, MVT::i32));
797 VA = RVLocs[++i]; // skip ahead to next loc
798 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
799 Chain = Lo.getValue(1);
800 InFlag = Lo.getValue(2);
801 VA = RVLocs[++i]; // skip ahead to next loc
802 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
803 Chain = Hi.getValue(1);
804 InFlag = Hi.getValue(2);
805 Val = DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
806 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
807 DAG.getConstant(1, MVT::i32));
810 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
812 Chain = Val.getValue(1);
813 InFlag = Val.getValue(2);
816 switch (VA.getLocInfo()) {
817 default: llvm_unreachable("Unknown loc info!");
818 case CCValAssign::Full: break;
819 case CCValAssign::BCvt:
820 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
824 InVals.push_back(Val);
830 /// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
831 /// by "Src" to address "Dst" of size "Size". Alignment information is
832 /// specified by the specific parameter attribute. The copy will be passed as
833 /// a byval function parameter.
834 /// Sometimes what we are copying is the end of a larger object, the part that
835 /// does not fit in registers.
837 CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
838 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
840 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
841 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
842 /*AlwaysInline=*/false, NULL, 0, NULL, 0);
845 /// LowerMemOpCallTo - Store the argument to the stack.
847 ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
848 SDValue StackPtr, SDValue Arg,
849 DebugLoc dl, SelectionDAG &DAG,
850 const CCValAssign &VA,
851 ISD::ArgFlagsTy Flags) {
852 unsigned LocMemOffset = VA.getLocMemOffset();
853 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
854 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
855 if (Flags.isByVal()) {
856 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
858 return DAG.getStore(Chain, dl, Arg, PtrOff,
859 PseudoSourceValue::getStack(), LocMemOffset);
862 void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
863 SDValue Chain, SDValue &Arg,
864 RegsToPassVector &RegsToPass,
865 CCValAssign &VA, CCValAssign &NextVA,
867 SmallVector<SDValue, 8> &MemOpChains,
868 ISD::ArgFlagsTy Flags) {
870 SDValue fmrrd = DAG.getNode(ARMISD::FMRRD, dl,
871 DAG.getVTList(MVT::i32, MVT::i32), Arg);
872 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
874 if (NextVA.isRegLoc())
875 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
877 assert(NextVA.isMemLoc());
878 if (StackPtr.getNode() == 0)
879 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
881 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
887 /// LowerCall - Lowering a call into a callseq_start <-
888 /// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
891 ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
892 CallingConv::ID CallConv, bool isVarArg,
894 const SmallVectorImpl<ISD::OutputArg> &Outs,
895 const SmallVectorImpl<ISD::InputArg> &Ins,
896 DebugLoc dl, SelectionDAG &DAG,
897 SmallVectorImpl<SDValue> &InVals) {
899 // Analyze operands of the call, assigning locations to each operand.
900 SmallVector<CCValAssign, 16> ArgLocs;
901 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
903 CCInfo.AnalyzeCallOperands(Outs,
904 CCAssignFnForNode(CallConv, /* Return*/ false,
907 // Get a count of how many bytes are to be pushed on the stack.
908 unsigned NumBytes = CCInfo.getNextStackOffset();
910 // Adjust the stack pointer for the new arguments...
911 // These operations are automatically eliminated by the prolog/epilog pass
912 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
914 SDValue StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
916 RegsToPassVector RegsToPass;
917 SmallVector<SDValue, 8> MemOpChains;
919 // Walk the register/memloc assignments, inserting copies/loads. In the case
920 // of tail call optimization, arguments are handled later.
921 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
924 CCValAssign &VA = ArgLocs[i];
925 SDValue Arg = Outs[realArgIdx].Val;
926 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
928 // Promote the value if needed.
929 switch (VA.getLocInfo()) {
930 default: llvm_unreachable("Unknown loc info!");
931 case CCValAssign::Full: break;
932 case CCValAssign::SExt:
933 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
935 case CCValAssign::ZExt:
936 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
938 case CCValAssign::AExt:
939 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
941 case CCValAssign::BCvt:
942 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
946 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
947 if (VA.needsCustom()) {
948 if (VA.getLocVT() == MVT::v2f64) {
949 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
950 DAG.getConstant(0, MVT::i32));
951 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
952 DAG.getConstant(1, MVT::i32));
954 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
955 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
957 VA = ArgLocs[++i]; // skip ahead to next loc
959 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
960 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
962 assert(VA.isMemLoc());
963 if (StackPtr.getNode() == 0)
964 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
966 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
967 dl, DAG, VA, Flags));
970 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
971 StackPtr, MemOpChains, Flags);
973 } else if (VA.isRegLoc()) {
974 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
976 assert(VA.isMemLoc());
977 if (StackPtr.getNode() == 0)
978 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
980 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
981 dl, DAG, VA, Flags));
985 if (!MemOpChains.empty())
986 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
987 &MemOpChains[0], MemOpChains.size());
989 // Build a sequence of copy-to-reg nodes chained together with token chain
990 // and flag operands which copy the outgoing args into the appropriate regs.
992 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
993 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
994 RegsToPass[i].second, InFlag);
995 InFlag = Chain.getValue(1);
998 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
999 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1000 // node so that legalize doesn't hack it.
1001 bool isDirect = false;
1002 bool isARMFunc = false;
1003 bool isLocalARMFunc = false;
1004 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1005 GlobalValue *GV = G->getGlobal();
1007 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
1008 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
1009 getTargetMachine().getRelocationModel() != Reloc::Static;
1010 isARMFunc = !Subtarget->isThumb() || isStub;
1011 // ARM call to a local ARM function is predicable.
1012 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
1013 // tBX takes a register source operand.
1014 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
1015 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1018 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1019 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1020 Callee = DAG.getLoad(getPointerTy(), dl,
1021 DAG.getEntryNode(), CPAddr, NULL, 0);
1022 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
1023 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
1024 getPointerTy(), Callee, PICLabel);
1026 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
1027 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1029 bool isStub = Subtarget->isTargetDarwin() &&
1030 getTargetMachine().getRelocationModel() != Reloc::Static;
1031 isARMFunc = !Subtarget->isThumb() || isStub;
1032 // tBX takes a register source operand.
1033 const char *Sym = S->getSymbol();
1034 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
1035 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1036 Sym, ARMPCLabelIndex, 4);
1037 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1038 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1039 Callee = DAG.getLoad(getPointerTy(), dl,
1040 DAG.getEntryNode(), CPAddr, NULL, 0);
1041 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
1042 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
1043 getPointerTy(), Callee, PICLabel);
1045 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
1048 // FIXME: handle tail calls differently.
1050 if (Subtarget->isThumb()) {
1051 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
1052 CallOpc = ARMISD::CALL_NOLINK;
1054 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1056 CallOpc = (isDirect || Subtarget->hasV5TOps())
1057 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1058 : ARMISD::CALL_NOLINK;
1060 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
1061 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
1062 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
1063 InFlag = Chain.getValue(1);
1066 std::vector<SDValue> Ops;
1067 Ops.push_back(Chain);
1068 Ops.push_back(Callee);
1070 // Add argument registers to the end of the list so that they are known live
1072 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1073 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1074 RegsToPass[i].second.getValueType()));
1076 if (InFlag.getNode())
1077 Ops.push_back(InFlag);
1078 // Returns a chain and a flag for retval copy to use.
1079 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
1080 &Ops[0], Ops.size());
1081 InFlag = Chain.getValue(1);
1083 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1084 DAG.getIntPtrConstant(0, true), InFlag);
1086 InFlag = Chain.getValue(1);
1088 // Handle result values, copying them out of physregs into vregs that we
1090 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1095 ARMTargetLowering::LowerReturn(SDValue Chain,
1096 CallingConv::ID CallConv, bool isVarArg,
1097 const SmallVectorImpl<ISD::OutputArg> &Outs,
1098 DebugLoc dl, SelectionDAG &DAG) {
1100 // CCValAssign - represent the assignment of the return value to a location.
1101 SmallVector<CCValAssign, 16> RVLocs;
1103 // CCState - Info about the registers and stack slots.
1104 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1107 // Analyze outgoing return values.
1108 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1111 // If this is the first return lowered for this function, add
1112 // the regs to the liveout set for the function.
1113 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1114 for (unsigned i = 0; i != RVLocs.size(); ++i)
1115 if (RVLocs[i].isRegLoc())
1116 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
1121 // Copy the result values into the output registers.
1122 for (unsigned i = 0, realRVLocIdx = 0;
1124 ++i, ++realRVLocIdx) {
1125 CCValAssign &VA = RVLocs[i];
1126 assert(VA.isRegLoc() && "Can only return in registers!");
1128 SDValue Arg = Outs[realRVLocIdx].Val;
1130 switch (VA.getLocInfo()) {
1131 default: llvm_unreachable("Unknown loc info!");
1132 case CCValAssign::Full: break;
1133 case CCValAssign::BCvt:
1134 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1138 if (VA.needsCustom()) {
1139 if (VA.getLocVT() == MVT::v2f64) {
1140 // Extract the first half and return it in two registers.
1141 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1142 DAG.getConstant(0, MVT::i32));
1143 SDValue HalfGPRs = DAG.getNode(ARMISD::FMRRD, dl,
1144 DAG.getVTList(MVT::i32, MVT::i32), Half);
1146 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1147 Flag = Chain.getValue(1);
1148 VA = RVLocs[++i]; // skip ahead to next loc
1149 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1150 HalfGPRs.getValue(1), Flag);
1151 Flag = Chain.getValue(1);
1152 VA = RVLocs[++i]; // skip ahead to next loc
1154 // Extract the 2nd half and fall through to handle it as an f64 value.
1155 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1156 DAG.getConstant(1, MVT::i32));
1158 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1160 SDValue fmrrd = DAG.getNode(ARMISD::FMRRD, dl,
1161 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
1162 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
1163 Flag = Chain.getValue(1);
1164 VA = RVLocs[++i]; // skip ahead to next loc
1165 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1168 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1170 // Guarantee that all emitted copies are
1171 // stuck together, avoiding something bad.
1172 Flag = Chain.getValue(1);
1177 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
1179 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
1184 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1185 // their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1186 // one of the above mentioned nodes. It has to be wrapped because otherwise
1187 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1188 // be used to form addressing mode. These wrapped nodes will be selected
1190 static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
1191 EVT PtrVT = Op.getValueType();
1192 // FIXME there is no actual debug info here
1193 DebugLoc dl = Op.getDebugLoc();
1194 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
1196 if (CP->isMachineConstantPoolEntry())
1197 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1198 CP->getAlignment());
1200 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1201 CP->getAlignment());
1202 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
1205 // Lower ISD::GlobalTLSAddress using the "general dynamic" model
1207 ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1208 SelectionDAG &DAG) {
1209 DebugLoc dl = GA->getDebugLoc();
1210 EVT PtrVT = getPointerTy();
1211 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1212 ARMConstantPoolValue *CPV =
1213 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
1214 ARMCP::CPValue, PCAdj, "tlsgd", true);
1215 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1216 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
1217 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument, NULL, 0);
1218 SDValue Chain = Argument.getValue(1);
1220 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
1221 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
1223 // call __tls_get_addr.
1226 Entry.Node = Argument;
1227 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
1228 Args.push_back(Entry);
1229 // FIXME: is there useful debug info available here?
1230 std::pair<SDValue, SDValue> CallResult =
1231 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1232 false, false, false, false,
1233 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
1234 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
1235 return CallResult.first;
1238 // Lower ISD::GlobalTLSAddress using the "initial exec" or
1239 // "local exec" model.
1241 ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
1242 SelectionDAG &DAG) {
1243 GlobalValue *GV = GA->getGlobal();
1244 DebugLoc dl = GA->getDebugLoc();
1246 SDValue Chain = DAG.getEntryNode();
1247 EVT PtrVT = getPointerTy();
1248 // Get the Thread Pointer
1249 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1251 if (GV->isDeclaration()) {
1252 // initial exec model
1253 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1254 ARMConstantPoolValue *CPV =
1255 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
1256 ARMCP::CPValue, PCAdj, "gottpoff", true);
1257 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1258 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
1259 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
1260 Chain = Offset.getValue(1);
1262 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
1263 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
1265 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
1268 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
1269 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1270 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
1271 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset, NULL, 0);
1274 // The address of the thread local variable is the add of the thread
1275 // pointer with the offset of the variable.
1276 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
1280 ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
1281 // TODO: implement the "local dynamic" model
1282 assert(Subtarget->isTargetELF() &&
1283 "TLS not implemented for non-ELF targets");
1284 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1285 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1286 // otherwise use the "Local Exec" TLS Model
1287 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1288 return LowerToTLSGeneralDynamicModel(GA, DAG);
1290 return LowerToTLSExecModels(GA, DAG);
1293 SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
1294 SelectionDAG &DAG) {
1295 EVT PtrVT = getPointerTy();
1296 DebugLoc dl = Op.getDebugLoc();
1297 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1298 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1299 if (RelocM == Reloc::PIC_) {
1300 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
1301 ARMConstantPoolValue *CPV =
1302 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
1303 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1304 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1305 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
1307 SDValue Chain = Result.getValue(1);
1308 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
1309 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
1311 Result = DAG.getLoad(PtrVT, dl, Chain, Result, NULL, 0);
1314 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
1315 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1316 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
1320 SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
1321 SelectionDAG &DAG) {
1322 EVT PtrVT = getPointerTy();
1323 DebugLoc dl = Op.getDebugLoc();
1324 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1325 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1327 if (RelocM == Reloc::Static)
1328 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
1330 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1331 ARMConstantPoolValue *CPV =
1332 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
1333 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1335 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1337 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
1338 SDValue Chain = Result.getValue(1);
1340 if (RelocM == Reloc::PIC_) {
1341 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
1342 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1345 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
1346 Result = DAG.getLoad(PtrVT, dl, Chain, Result, NULL, 0);
1351 SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
1353 assert(Subtarget->isTargetELF() &&
1354 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
1355 EVT PtrVT = getPointerTy();
1356 DebugLoc dl = Op.getDebugLoc();
1357 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
1358 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1359 "_GLOBAL_OFFSET_TABLE_",
1360 ARMPCLabelIndex, PCAdj);
1361 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1362 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1363 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
1364 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
1365 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1368 static SDValue LowerNeonVLDIntrinsic(SDValue Op, SelectionDAG &DAG,
1370 SDNode *Node = Op.getNode();
1371 EVT VT = Node->getValueType(0);
1373 // No expansion needed for 64-bit vectors.
1374 if (VT.is64BitVector())
1377 // FIXME: We need to expand VLD3 and VLD4 of 128-bit vectors into separate
1378 // operations to load the even and odd registers.
1382 static SDValue LowerNeonVSTIntrinsic(SDValue Op, SelectionDAG &DAG,
1384 SDNode *Node = Op.getNode();
1385 EVT VT = Node->getOperand(3).getValueType();
1387 // No expansion needed for 64-bit vectors.
1388 if (VT.is64BitVector())
1391 // FIXME: We need to expand VST3 and VST4 of 128-bit vectors into separate
1392 // operations to store the even and odd registers.
1396 static SDValue LowerNeonVLDLaneIntrinsic(SDValue Op, SelectionDAG &DAG,
1398 SDNode *Node = Op.getNode();
1399 EVT VT = Node->getValueType(0);
1401 if (!VT.is64BitVector())
1402 return SDValue(); // unimplemented
1404 // Change the lane number operand to be a TargetConstant; otherwise it
1405 // will be legalized into a register.
1406 ConstantSDNode *Lane = dyn_cast<ConstantSDNode>(Node->getOperand(NumVecs+3));
1408 assert(false && "vld lane number must be a constant");
1411 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
1412 Ops[NumVecs+3] = DAG.getTargetConstant(Lane->getZExtValue(), MVT::i32);
1413 return DAG.UpdateNodeOperands(Op, &Ops[0], Ops.size());
1416 static SDValue LowerNeonVSTLaneIntrinsic(SDValue Op, SelectionDAG &DAG,
1418 SDNode *Node = Op.getNode();
1419 EVT VT = Node->getOperand(3).getValueType();
1421 if (!VT.is64BitVector())
1422 return SDValue(); // unimplemented
1424 // Change the lane number operand to be a TargetConstant; otherwise it
1425 // will be legalized into a register.
1426 ConstantSDNode *Lane = dyn_cast<ConstantSDNode>(Node->getOperand(NumVecs+3));
1428 assert(false && "vst lane number must be a constant");
1431 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
1432 Ops[NumVecs+3] = DAG.getTargetConstant(Lane->getZExtValue(), MVT::i32);
1433 return DAG.UpdateNodeOperands(Op, &Ops[0], Ops.size());
1437 ARMTargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
1438 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1440 case Intrinsic::arm_neon_vld3:
1441 return LowerNeonVLDIntrinsic(Op, DAG, 3);
1442 case Intrinsic::arm_neon_vld4:
1443 return LowerNeonVLDIntrinsic(Op, DAG, 4);
1444 case Intrinsic::arm_neon_vld2lane:
1445 return LowerNeonVLDLaneIntrinsic(Op, DAG, 2);
1446 case Intrinsic::arm_neon_vld3lane:
1447 return LowerNeonVLDLaneIntrinsic(Op, DAG, 3);
1448 case Intrinsic::arm_neon_vld4lane:
1449 return LowerNeonVLDLaneIntrinsic(Op, DAG, 4);
1450 case Intrinsic::arm_neon_vst3:
1451 return LowerNeonVSTIntrinsic(Op, DAG, 3);
1452 case Intrinsic::arm_neon_vst4:
1453 return LowerNeonVSTIntrinsic(Op, DAG, 4);
1454 case Intrinsic::arm_neon_vst2lane:
1455 return LowerNeonVSTLaneIntrinsic(Op, DAG, 2);
1456 case Intrinsic::arm_neon_vst3lane:
1457 return LowerNeonVSTLaneIntrinsic(Op, DAG, 3);
1458 case Intrinsic::arm_neon_vst4lane:
1459 return LowerNeonVSTLaneIntrinsic(Op, DAG, 4);
1460 default: return SDValue(); // Don't custom lower most intrinsics.
1465 ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
1466 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1467 DebugLoc dl = Op.getDebugLoc();
1469 default: return SDValue(); // Don't custom lower most intrinsics.
1470 case Intrinsic::arm_thread_pointer: {
1471 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1472 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1474 case Intrinsic::eh_sjlj_lsda: {
1475 MachineFunction &MF = DAG.getMachineFunction();
1476 EVT PtrVT = getPointerTy();
1477 DebugLoc dl = Op.getDebugLoc();
1478 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1480 unsigned PCAdj = (RelocM != Reloc::PIC_)
1481 ? 0 : (Subtarget->isThumb() ? 4 : 8);
1482 ARMConstantPoolValue *CPV =
1483 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1484 ARMCP::CPLSDA, PCAdj);
1485 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1486 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1488 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr, NULL, 0);
1489 SDValue Chain = Result.getValue(1);
1491 if (RelocM == Reloc::PIC_) {
1492 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex++, MVT::i32);
1493 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1497 case Intrinsic::eh_sjlj_setjmp:
1498 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(1));
1502 static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
1503 unsigned VarArgsFrameIndex) {
1504 // vastart just stores the address of the VarArgsFrameIndex slot into the
1505 // memory location argument.
1506 DebugLoc dl = Op.getDebugLoc();
1507 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1508 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
1509 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
1510 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
1514 ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
1515 SDNode *Node = Op.getNode();
1516 DebugLoc dl = Node->getDebugLoc();
1517 EVT VT = Node->getValueType(0);
1518 SDValue Chain = Op.getOperand(0);
1519 SDValue Size = Op.getOperand(1);
1520 SDValue Align = Op.getOperand(2);
1522 // Chain the dynamic stack allocation so that it doesn't modify the stack
1523 // pointer when other instructions are using the stack.
1524 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1526 unsigned AlignVal = cast<ConstantSDNode>(Align)->getZExtValue();
1527 unsigned StackAlign = getTargetMachine().getFrameInfo()->getStackAlignment();
1528 if (AlignVal > StackAlign)
1529 // Do this now since selection pass cannot introduce new target
1530 // independent node.
1531 Align = DAG.getConstant(-(uint64_t)AlignVal, VT);
1533 // In Thumb1 mode, there isn't a "sub r, sp, r" instruction, we will end up
1534 // using a "add r, sp, r" instead. Negate the size now so we don't have to
1535 // do even more horrible hack later.
1536 MachineFunction &MF = DAG.getMachineFunction();
1537 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1538 if (AFI->isThumb1OnlyFunction()) {
1540 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Size);
1542 uint32_t Val = C->getZExtValue();
1543 if (Val <= 508 && ((Val & 3) == 0))
1547 Size = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, VT), Size);
1550 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
1551 SDValue Ops1[] = { Chain, Size, Align };
1552 SDValue Res = DAG.getNode(ARMISD::DYN_ALLOC, dl, VTList, Ops1, 3);
1553 Chain = Res.getValue(1);
1554 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1555 DAG.getIntPtrConstant(0, true), SDValue());
1556 SDValue Ops2[] = { Res, Chain };
1557 return DAG.getMergeValues(Ops2, 2, dl);
1561 ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1562 SDValue &Root, SelectionDAG &DAG,
1564 MachineFunction &MF = DAG.getMachineFunction();
1565 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1567 TargetRegisterClass *RC;
1568 if (AFI->isThumb1OnlyFunction())
1569 RC = ARM::tGPRRegisterClass;
1571 RC = ARM::GPRRegisterClass;
1573 // Transform the arguments stored in physical registers into virtual ones.
1574 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
1575 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
1578 if (NextVA.isMemLoc()) {
1579 unsigned ArgSize = NextVA.getLocVT().getSizeInBits()/8;
1580 MachineFrameInfo *MFI = MF.getFrameInfo();
1581 int FI = MFI->CreateFixedObject(ArgSize, NextVA.getLocMemOffset());
1583 // Create load node to retrieve arguments from the stack.
1584 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1585 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN, NULL, 0);
1587 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
1588 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
1591 return DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, ArgValue, ArgValue2);
1595 ARMTargetLowering::LowerFormalArguments(SDValue Chain,
1596 CallingConv::ID CallConv, bool isVarArg,
1597 const SmallVectorImpl<ISD::InputArg>
1599 DebugLoc dl, SelectionDAG &DAG,
1600 SmallVectorImpl<SDValue> &InVals) {
1602 MachineFunction &MF = DAG.getMachineFunction();
1603 MachineFrameInfo *MFI = MF.getFrameInfo();
1605 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1607 // Assign locations to all of the incoming arguments.
1608 SmallVector<CCValAssign, 16> ArgLocs;
1609 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1611 CCInfo.AnalyzeFormalArguments(Ins,
1612 CCAssignFnForNode(CallConv, /* Return*/ false,
1615 SmallVector<SDValue, 16> ArgValues;
1617 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1618 CCValAssign &VA = ArgLocs[i];
1620 // Arguments stored in registers.
1621 if (VA.isRegLoc()) {
1622 EVT RegVT = VA.getLocVT();
1625 if (VA.needsCustom()) {
1626 // f64 and vector types are split up into multiple registers or
1627 // combinations of registers and stack slots.
1630 if (VA.getLocVT() == MVT::v2f64) {
1631 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
1633 VA = ArgLocs[++i]; // skip ahead to next loc
1634 SDValue ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
1636 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1637 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
1638 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
1639 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
1640 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
1642 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
1645 TargetRegisterClass *RC;
1647 if (RegVT == MVT::f32)
1648 RC = ARM::SPRRegisterClass;
1649 else if (RegVT == MVT::f64)
1650 RC = ARM::DPRRegisterClass;
1651 else if (RegVT == MVT::v2f64)
1652 RC = ARM::QPRRegisterClass;
1653 else if (RegVT == MVT::i32)
1654 RC = (AFI->isThumb1OnlyFunction() ?
1655 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
1657 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
1659 // Transform the arguments in physical registers into virtual ones.
1660 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
1661 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
1664 // If this is an 8 or 16-bit value, it is really passed promoted
1665 // to 32 bits. Insert an assert[sz]ext to capture this, then
1666 // truncate to the right size.
1667 switch (VA.getLocInfo()) {
1668 default: llvm_unreachable("Unknown loc info!");
1669 case CCValAssign::Full: break;
1670 case CCValAssign::BCvt:
1671 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1673 case CCValAssign::SExt:
1674 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1675 DAG.getValueType(VA.getValVT()));
1676 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1678 case CCValAssign::ZExt:
1679 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1680 DAG.getValueType(VA.getValVT()));
1681 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1685 InVals.push_back(ArgValue);
1687 } else { // VA.isRegLoc()
1690 assert(VA.isMemLoc());
1691 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
1693 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
1694 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset());
1696 // Create load nodes to retrieve arguments from the stack.
1697 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1698 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN, NULL, 0));
1704 static const unsigned GPRArgRegs[] = {
1705 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1708 unsigned NumGPRs = CCInfo.getFirstUnallocated
1709 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
1711 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1712 unsigned VARegSize = (4 - NumGPRs) * 4;
1713 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
1714 unsigned ArgOffset = 0;
1715 if (VARegSaveSize) {
1716 // If this function is vararg, store any remaining integer argument regs
1717 // to their spots on the stack so that they may be loaded by deferencing
1718 // the result of va_next.
1719 AFI->setVarArgsRegSaveSize(VARegSaveSize);
1720 ArgOffset = CCInfo.getNextStackOffset();
1721 VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
1722 VARegSaveSize - VARegSize);
1723 SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
1725 SmallVector<SDValue, 4> MemOps;
1726 for (; NumGPRs < 4; ++NumGPRs) {
1727 TargetRegisterClass *RC;
1728 if (AFI->isThumb1OnlyFunction())
1729 RC = ARM::tGPRRegisterClass;
1731 RC = ARM::GPRRegisterClass;
1733 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
1734 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
1735 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
1736 MemOps.push_back(Store);
1737 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
1738 DAG.getConstant(4, getPointerTy()));
1740 if (!MemOps.empty())
1741 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1742 &MemOps[0], MemOps.size());
1744 // This will point to the next argument passed via stack.
1745 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1751 /// isFloatingPointZero - Return true if this is +0.0.
1752 static bool isFloatingPointZero(SDValue Op) {
1753 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
1754 return CFP->getValueAPF().isPosZero();
1755 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
1756 // Maybe this has already been legalized into the constant pool?
1757 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
1758 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
1759 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1760 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
1761 return CFP->getValueAPF().isPosZero();
1767 static bool isLegalCmpImmediate(unsigned C, bool isThumb1Only) {
1768 return ( isThumb1Only && (C & ~255U) == 0) ||
1769 (!isThumb1Only && ARM_AM::getSOImmVal(C) != -1);
1772 /// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1773 /// the given operands.
1774 static SDValue getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
1775 SDValue &ARMCC, SelectionDAG &DAG, bool isThumb1Only,
1777 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
1778 unsigned C = RHSC->getZExtValue();
1779 if (!isLegalCmpImmediate(C, isThumb1Only)) {
1780 // Constant does not fit, try adjusting it by one?
1785 if (isLegalCmpImmediate(C-1, isThumb1Only)) {
1786 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
1787 RHS = DAG.getConstant(C-1, MVT::i32);
1792 if (C > 0 && isLegalCmpImmediate(C-1, isThumb1Only)) {
1793 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
1794 RHS = DAG.getConstant(C-1, MVT::i32);
1799 if (isLegalCmpImmediate(C+1, isThumb1Only)) {
1800 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
1801 RHS = DAG.getConstant(C+1, MVT::i32);
1806 if (C < 0xffffffff && isLegalCmpImmediate(C+1, isThumb1Only)) {
1807 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
1808 RHS = DAG.getConstant(C+1, MVT::i32);
1815 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
1816 ARMISD::NodeType CompareType;
1819 CompareType = ARMISD::CMP;
1824 CompareType = ARMISD::CMPZ;
1827 ARMCC = DAG.getConstant(CondCode, MVT::i32);
1828 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
1831 /// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
1832 static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
1835 if (!isFloatingPointZero(RHS))
1836 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
1838 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
1839 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
1842 static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
1843 const ARMSubtarget *ST) {
1844 EVT VT = Op.getValueType();
1845 SDValue LHS = Op.getOperand(0);
1846 SDValue RHS = Op.getOperand(1);
1847 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1848 SDValue TrueVal = Op.getOperand(2);
1849 SDValue FalseVal = Op.getOperand(3);
1850 DebugLoc dl = Op.getDebugLoc();
1852 if (LHS.getValueType() == MVT::i32) {
1854 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1855 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb1Only(), dl);
1856 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
1859 ARMCC::CondCodes CondCode, CondCode2;
1860 FPCCToARMCC(CC, CondCode, CondCode2);
1862 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1863 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1864 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
1865 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
1867 if (CondCode2 != ARMCC::AL) {
1868 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
1869 // FIXME: Needs another CMP because flag can have but one use.
1870 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
1871 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
1872 Result, TrueVal, ARMCC2, CCR, Cmp2);
1877 static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
1878 const ARMSubtarget *ST) {
1879 SDValue Chain = Op.getOperand(0);
1880 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1881 SDValue LHS = Op.getOperand(2);
1882 SDValue RHS = Op.getOperand(3);
1883 SDValue Dest = Op.getOperand(4);
1884 DebugLoc dl = Op.getDebugLoc();
1886 if (LHS.getValueType() == MVT::i32) {
1888 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1889 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, ST->isThumb1Only(), dl);
1890 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
1891 Chain, Dest, ARMCC, CCR,Cmp);
1894 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
1895 ARMCC::CondCodes CondCode, CondCode2;
1896 FPCCToARMCC(CC, CondCode, CondCode2);
1898 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
1899 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1900 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1901 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
1902 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
1903 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
1904 if (CondCode2 != ARMCC::AL) {
1905 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
1906 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
1907 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
1912 SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) {
1913 SDValue Chain = Op.getOperand(0);
1914 SDValue Table = Op.getOperand(1);
1915 SDValue Index = Op.getOperand(2);
1916 DebugLoc dl = Op.getDebugLoc();
1918 EVT PTy = getPointerTy();
1919 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1920 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
1921 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
1922 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
1923 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
1924 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
1925 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
1926 if (Subtarget->isThumb2()) {
1927 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
1928 // which does another jump to the destination. This also makes it easier
1929 // to translate it to TBB / TBH later.
1930 // FIXME: This might not work if the function is extremely large.
1931 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
1932 Addr, Op.getOperand(2), JTI, UId);
1934 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1935 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr, NULL, 0);
1936 Chain = Addr.getValue(1);
1937 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
1938 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
1940 Addr = DAG.getLoad(PTy, dl, Chain, Addr, NULL, 0);
1941 Chain = Addr.getValue(1);
1942 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
1946 static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
1947 DebugLoc dl = Op.getDebugLoc();
1949 Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
1950 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
1951 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
1954 static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
1955 EVT VT = Op.getValueType();
1956 DebugLoc dl = Op.getDebugLoc();
1958 Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1960 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
1961 return DAG.getNode(Opc, dl, VT, Op);
1964 static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
1965 // Implement fcopysign with a fabs and a conditional fneg.
1966 SDValue Tmp0 = Op.getOperand(0);
1967 SDValue Tmp1 = Op.getOperand(1);
1968 DebugLoc dl = Op.getDebugLoc();
1969 EVT VT = Op.getValueType();
1970 EVT SrcVT = Tmp1.getValueType();
1971 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
1972 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
1973 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
1974 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1975 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
1978 SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
1979 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1980 MFI->setFrameAddressIsTaken(true);
1981 EVT VT = Op.getValueType();
1982 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
1983 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1984 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
1985 ? ARM::R7 : ARM::R11;
1986 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
1988 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
1993 ARMTargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
1995 SDValue Dst, SDValue Src,
1996 SDValue Size, unsigned Align,
1998 const Value *DstSV, uint64_t DstSVOff,
1999 const Value *SrcSV, uint64_t SrcSVOff){
2000 // Do repeated 4-byte loads and stores. To be improved.
2001 // This requires 4-byte alignment.
2002 if ((Align & 3) != 0)
2004 // This requires the copy size to be a constant, preferrably
2005 // within a subtarget-specific limit.
2006 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2009 uint64_t SizeVal = ConstantSize->getZExtValue();
2010 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
2013 unsigned BytesLeft = SizeVal & 3;
2014 unsigned NumMemOps = SizeVal >> 2;
2015 unsigned EmittedNumMemOps = 0;
2017 unsigned VTSize = 4;
2019 const unsigned MAX_LOADS_IN_LDM = 6;
2020 SDValue TFOps[MAX_LOADS_IN_LDM];
2021 SDValue Loads[MAX_LOADS_IN_LDM];
2022 uint64_t SrcOff = 0, DstOff = 0;
2024 // Emit up to MAX_LOADS_IN_LDM loads, then a TokenFactor barrier, then the
2025 // same number of stores. The loads and stores will get combined into
2026 // ldm/stm later on.
2027 while (EmittedNumMemOps < NumMemOps) {
2029 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
2030 Loads[i] = DAG.getLoad(VT, dl, Chain,
2031 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
2032 DAG.getConstant(SrcOff, MVT::i32)),
2033 SrcSV, SrcSVOff + SrcOff);
2034 TFOps[i] = Loads[i].getValue(1);
2037 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
2040 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
2041 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
2042 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
2043 DAG.getConstant(DstOff, MVT::i32)),
2044 DstSV, DstSVOff + DstOff);
2047 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
2049 EmittedNumMemOps += i;
2055 // Issue loads / stores for the trailing (1 - 3) bytes.
2056 unsigned BytesLeftSave = BytesLeft;
2059 if (BytesLeft >= 2) {
2067 Loads[i] = DAG.getLoad(VT, dl, Chain,
2068 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
2069 DAG.getConstant(SrcOff, MVT::i32)),
2070 SrcSV, SrcSVOff + SrcOff);
2071 TFOps[i] = Loads[i].getValue(1);
2074 BytesLeft -= VTSize;
2076 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
2079 BytesLeft = BytesLeftSave;
2081 if (BytesLeft >= 2) {
2089 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
2090 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
2091 DAG.getConstant(DstOff, MVT::i32)),
2092 DstSV, DstSVOff + DstOff);
2095 BytesLeft -= VTSize;
2097 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
2100 static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
2101 SDValue Op = N->getOperand(0);
2102 DebugLoc dl = N->getDebugLoc();
2103 if (N->getValueType(0) == MVT::f64) {
2104 // Turn i64->f64 into FMDRR.
2105 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2106 DAG.getConstant(0, MVT::i32));
2107 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2108 DAG.getConstant(1, MVT::i32));
2109 return DAG.getNode(ARMISD::FMDRR, dl, MVT::f64, Lo, Hi);
2112 // Turn f64->i64 into FMRRD.
2113 SDValue Cvt = DAG.getNode(ARMISD::FMRRD, dl,
2114 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
2116 // Merge the pieces into a single i64 value.
2117 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
2120 /// getZeroVector - Returns a vector of specified type with all zero elements.
2122 static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
2123 assert(VT.isVector() && "Expected a vector type");
2125 // Zero vectors are used to represent vector negation and in those cases
2126 // will be implemented with the NEON VNEG instruction. However, VNEG does
2127 // not support i64 elements, so sometimes the zero vectors will need to be
2128 // explicitly constructed. For those cases, and potentially other uses in
2129 // the future, always build zero vectors as <16 x i8> or <8 x i8> bitcasted
2130 // to their dest type. This ensures they get CSE'd.
2132 SDValue Cst = DAG.getTargetConstant(0, MVT::i8);
2133 SmallVector<SDValue, 8> Ops;
2136 if (VT.getSizeInBits() == 64) {
2137 Ops.assign(8, Cst); TVT = MVT::v8i8;
2139 Ops.assign(16, Cst); TVT = MVT::v16i8;
2141 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
2143 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2146 /// getOnesVector - Returns a vector of specified type with all bits set.
2148 static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
2149 assert(VT.isVector() && "Expected a vector type");
2151 // Always build ones vectors as <16 x i32> or <8 x i32> bitcasted to their
2152 // dest type. This ensures they get CSE'd.
2154 SDValue Cst = DAG.getTargetConstant(0xFF, MVT::i8);
2155 SmallVector<SDValue, 8> Ops;
2158 if (VT.getSizeInBits() == 64) {
2159 Ops.assign(8, Cst); TVT = MVT::v8i8;
2161 Ops.assign(16, Cst); TVT = MVT::v16i8;
2163 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
2165 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2168 static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2169 const ARMSubtarget *ST) {
2170 EVT VT = N->getValueType(0);
2171 DebugLoc dl = N->getDebugLoc();
2173 // Lower vector shifts on NEON to use VSHL.
2174 if (VT.isVector()) {
2175 assert(ST->hasNEON() && "unexpected vector shift");
2177 // Left shifts translate directly to the vshiftu intrinsic.
2178 if (N->getOpcode() == ISD::SHL)
2179 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
2180 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
2181 N->getOperand(0), N->getOperand(1));
2183 assert((N->getOpcode() == ISD::SRA ||
2184 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2186 // NEON uses the same intrinsics for both left and right shifts. For
2187 // right shifts, the shift amounts are negative, so negate the vector of
2189 EVT ShiftVT = N->getOperand(1).getValueType();
2190 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2191 getZeroVector(ShiftVT, DAG, dl),
2193 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2194 Intrinsic::arm_neon_vshifts :
2195 Intrinsic::arm_neon_vshiftu);
2196 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
2197 DAG.getConstant(vshiftInt, MVT::i32),
2198 N->getOperand(0), NegatedCount);
2201 // We can get here for a node like i32 = ISD::SHL i32, i64
2205 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
2206 "Unknown shift to lower!");
2208 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2209 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
2210 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
2213 // If we are in thumb mode, we don't have RRX.
2214 if (ST->isThumb1Only()) return SDValue();
2216 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
2217 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2218 DAG.getConstant(0, MVT::i32));
2219 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2220 DAG.getConstant(1, MVT::i32));
2222 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2223 // captures the result into a carry flag.
2224 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
2225 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
2227 // The low part is an ARMISD::RRX operand, which shifts the carry in.
2228 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
2230 // Merge the pieces into a single i64 value.
2231 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
2234 static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2235 SDValue TmpOp0, TmpOp1;
2236 bool Invert = false;
2240 SDValue Op0 = Op.getOperand(0);
2241 SDValue Op1 = Op.getOperand(1);
2242 SDValue CC = Op.getOperand(2);
2243 EVT VT = Op.getValueType();
2244 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2245 DebugLoc dl = Op.getDebugLoc();
2247 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2248 switch (SetCCOpcode) {
2249 default: llvm_unreachable("Illegal FP comparison"); break;
2251 case ISD::SETNE: Invert = true; // Fallthrough
2253 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2255 case ISD::SETLT: Swap = true; // Fallthrough
2257 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2259 case ISD::SETLE: Swap = true; // Fallthrough
2261 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2262 case ISD::SETUGE: Swap = true; // Fallthrough
2263 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2264 case ISD::SETUGT: Swap = true; // Fallthrough
2265 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2266 case ISD::SETUEQ: Invert = true; // Fallthrough
2268 // Expand this to (OLT | OGT).
2272 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2273 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2275 case ISD::SETUO: Invert = true; // Fallthrough
2277 // Expand this to (OLT | OGE).
2281 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2282 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2286 // Integer comparisons.
2287 switch (SetCCOpcode) {
2288 default: llvm_unreachable("Illegal integer comparison"); break;
2289 case ISD::SETNE: Invert = true;
2290 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2291 case ISD::SETLT: Swap = true;
2292 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2293 case ISD::SETLE: Swap = true;
2294 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2295 case ISD::SETULT: Swap = true;
2296 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2297 case ISD::SETULE: Swap = true;
2298 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2301 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
2302 if (Opc == ARMISD::VCEQ) {
2305 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2307 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2310 // Ignore bitconvert.
2311 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2312 AndOp = AndOp.getOperand(0);
2314 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2316 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2317 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2324 std::swap(Op0, Op1);
2326 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2329 Result = DAG.getNOT(dl, Result, VT);
2334 /// isVMOVSplat - Check if the specified splat value corresponds to an immediate
2335 /// VMOV instruction, and if so, return the constant being splatted.
2336 static SDValue isVMOVSplat(uint64_t SplatBits, uint64_t SplatUndef,
2337 unsigned SplatBitSize, SelectionDAG &DAG) {
2338 switch (SplatBitSize) {
2340 // Any 1-byte value is OK.
2341 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
2342 return DAG.getTargetConstant(SplatBits, MVT::i8);
2345 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
2346 if ((SplatBits & ~0xff) == 0 ||
2347 (SplatBits & ~0xff00) == 0)
2348 return DAG.getTargetConstant(SplatBits, MVT::i16);
2352 // NEON's 32-bit VMOV supports splat values where:
2353 // * only one byte is nonzero, or
2354 // * the least significant byte is 0xff and the second byte is nonzero, or
2355 // * the least significant 2 bytes are 0xff and the third is nonzero.
2356 if ((SplatBits & ~0xff) == 0 ||
2357 (SplatBits & ~0xff00) == 0 ||
2358 (SplatBits & ~0xff0000) == 0 ||
2359 (SplatBits & ~0xff000000) == 0)
2360 return DAG.getTargetConstant(SplatBits, MVT::i32);
2362 if ((SplatBits & ~0xffff) == 0 &&
2363 ((SplatBits | SplatUndef) & 0xff) == 0xff)
2364 return DAG.getTargetConstant(SplatBits | 0xff, MVT::i32);
2366 if ((SplatBits & ~0xffffff) == 0 &&
2367 ((SplatBits | SplatUndef) & 0xffff) == 0xffff)
2368 return DAG.getTargetConstant(SplatBits | 0xffff, MVT::i32);
2370 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2371 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2372 // VMOV.I32. A (very) minor optimization would be to replicate the value
2373 // and fall through here to test for a valid 64-bit splat. But, then the
2374 // caller would also need to check and handle the change in size.
2378 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
2379 uint64_t BitMask = 0xff;
2381 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
2382 if (((SplatBits | SplatUndef) & BitMask) == BitMask)
2384 else if ((SplatBits & BitMask) != 0)
2388 return DAG.getTargetConstant(Val, MVT::i64);
2392 llvm_unreachable("unexpected size for isVMOVSplat");
2399 /// getVMOVImm - If this is a build_vector of constants which can be
2400 /// formed by using a VMOV instruction of the specified element size,
2401 /// return the constant being splatted. The ByteSize field indicates the
2402 /// number of bytes of each element [1248].
2403 SDValue ARM::getVMOVImm(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
2404 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2405 APInt SplatBits, SplatUndef;
2406 unsigned SplatBitSize;
2408 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2409 HasAnyUndefs, ByteSize * 8))
2412 if (SplatBitSize > ByteSize * 8)
2415 return isVMOVSplat(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
2419 static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
2420 bool &ReverseVEXT, unsigned &Imm) {
2421 unsigned NumElts = VT.getVectorNumElements();
2422 ReverseVEXT = false;
2425 // If this is a VEXT shuffle, the immediate value is the index of the first
2426 // element. The other shuffle indices must be the successive elements after
2428 unsigned ExpectedElt = Imm;
2429 for (unsigned i = 1; i < NumElts; ++i) {
2430 // Increment the expected index. If it wraps around, it may still be
2431 // a VEXT but the source vectors must be swapped.
2433 if (ExpectedElt == NumElts * 2) {
2438 if (ExpectedElt != static_cast<unsigned>(M[i]))
2442 // Adjust the index value if the source operands will be swapped.
2449 /// isVREVMask - Check if a vector shuffle corresponds to a VREV
2450 /// instruction with the specified blocksize. (The order of the elements
2451 /// within each block of the vector is reversed.)
2452 static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
2453 unsigned BlockSize) {
2454 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
2455 "Only possible block sizes for VREV are: 16, 32, 64");
2457 unsigned NumElts = VT.getVectorNumElements();
2458 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2459 unsigned BlockElts = M[0] + 1;
2461 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
2464 for (unsigned i = 0; i < NumElts; ++i) {
2465 if ((unsigned) M[i] !=
2466 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
2473 static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
2474 unsigned &WhichResult) {
2475 unsigned NumElts = VT.getVectorNumElements();
2476 WhichResult = (M[0] == 0 ? 0 : 1);
2477 for (unsigned i = 0; i < NumElts; i += 2) {
2478 if ((unsigned) M[i] != i + WhichResult ||
2479 (unsigned) M[i+1] != i + NumElts + WhichResult)
2485 static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
2486 unsigned &WhichResult) {
2487 unsigned NumElts = VT.getVectorNumElements();
2488 WhichResult = (M[0] == 0 ? 0 : 1);
2489 for (unsigned i = 0; i != NumElts; ++i) {
2490 if ((unsigned) M[i] != 2 * i + WhichResult)
2494 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2495 if (VT.is64BitVector() && VT.getVectorElementType().getSizeInBits() == 32)
2501 static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
2502 unsigned &WhichResult) {
2503 unsigned NumElts = VT.getVectorNumElements();
2504 WhichResult = (M[0] == 0 ? 0 : 1);
2505 unsigned Idx = WhichResult * NumElts / 2;
2506 for (unsigned i = 0; i != NumElts; i += 2) {
2507 if ((unsigned) M[i] != Idx ||
2508 (unsigned) M[i+1] != Idx + NumElts)
2513 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2514 if (VT.is64BitVector() && VT.getVectorElementType().getSizeInBits() == 32)
2520 static SDValue BuildSplat(SDValue Val, EVT VT, SelectionDAG &DAG, DebugLoc dl) {
2521 // Canonicalize all-zeros and all-ones vectors.
2522 ConstantSDNode *ConstVal = cast<ConstantSDNode>(Val.getNode());
2523 if (ConstVal->isNullValue())
2524 return getZeroVector(VT, DAG, dl);
2525 if (ConstVal->isAllOnesValue())
2526 return getOnesVector(VT, DAG, dl);
2529 if (VT.is64BitVector()) {
2530 switch (Val.getValueType().getSizeInBits()) {
2531 case 8: CanonicalVT = MVT::v8i8; break;
2532 case 16: CanonicalVT = MVT::v4i16; break;
2533 case 32: CanonicalVT = MVT::v2i32; break;
2534 case 64: CanonicalVT = MVT::v1i64; break;
2535 default: llvm_unreachable("unexpected splat element type"); break;
2538 assert(VT.is128BitVector() && "unknown splat vector size");
2539 switch (Val.getValueType().getSizeInBits()) {
2540 case 8: CanonicalVT = MVT::v16i8; break;
2541 case 16: CanonicalVT = MVT::v8i16; break;
2542 case 32: CanonicalVT = MVT::v4i32; break;
2543 case 64: CanonicalVT = MVT::v2i64; break;
2544 default: llvm_unreachable("unexpected splat element type"); break;
2548 // Build a canonical splat for this value.
2549 SmallVector<SDValue, 8> Ops;
2550 Ops.assign(CanonicalVT.getVectorNumElements(), Val);
2551 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
2553 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
2556 // If this is a case we can't handle, return null and let the default
2557 // expansion code take care of it.
2558 static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
2559 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
2560 DebugLoc dl = Op.getDebugLoc();
2561 EVT VT = Op.getValueType();
2563 APInt SplatBits, SplatUndef;
2564 unsigned SplatBitSize;
2566 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
2567 if (SplatBitSize <= 64) {
2568 SDValue Val = isVMOVSplat(SplatBits.getZExtValue(),
2569 SplatUndef.getZExtValue(), SplatBitSize, DAG);
2571 return BuildSplat(Val, VT, DAG, dl);
2575 // If there are only 2 elements in a 128-bit vector, insert them into an
2576 // undef vector. This handles the common case for 128-bit vector argument
2577 // passing, where the insertions should be translated to subreg accesses
2578 // with no real instructions.
2579 if (VT.is128BitVector() && Op.getNumOperands() == 2) {
2580 SDValue Val = DAG.getUNDEF(VT);
2581 SDValue Op0 = Op.getOperand(0);
2582 SDValue Op1 = Op.getOperand(1);
2583 if (Op0.getOpcode() != ISD::UNDEF)
2584 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op0,
2585 DAG.getIntPtrConstant(0));
2586 if (Op1.getOpcode() != ISD::UNDEF)
2587 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op1,
2588 DAG.getIntPtrConstant(1));
2595 /// isShuffleMaskLegal - Targets can use this to indicate that they only
2596 /// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2597 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2598 /// are assumed to be legal.
2600 ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
2602 if (VT.getVectorNumElements() == 4 &&
2603 (VT.is128BitVector() || VT.is64BitVector())) {
2604 unsigned PFIndexes[4];
2605 for (unsigned i = 0; i != 4; ++i) {
2609 PFIndexes[i] = M[i];
2612 // Compute the index in the perfect shuffle table.
2613 unsigned PFTableIndex =
2614 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2615 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2616 unsigned Cost = (PFEntry >> 30);
2623 unsigned Imm, WhichResult;
2625 return (ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
2626 isVREVMask(M, VT, 64) ||
2627 isVREVMask(M, VT, 32) ||
2628 isVREVMask(M, VT, 16) ||
2629 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
2630 isVTRNMask(M, VT, WhichResult) ||
2631 isVUZPMask(M, VT, WhichResult) ||
2632 isVZIPMask(M, VT, WhichResult));
2635 /// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
2636 /// the specified operations to build the shuffle.
2637 static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
2638 SDValue RHS, SelectionDAG &DAG,
2640 unsigned OpNum = (PFEntry >> 26) & 0x0F;
2641 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
2642 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
2645 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
2654 OP_VUZPL, // VUZP, left result
2655 OP_VUZPR, // VUZP, right result
2656 OP_VZIPL, // VZIP, left result
2657 OP_VZIPR, // VZIP, right result
2658 OP_VTRNL, // VTRN, left result
2659 OP_VTRNR // VTRN, right result
2662 if (OpNum == OP_COPY) {
2663 if (LHSID == (1*9+2)*9+3) return LHS;
2664 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
2668 SDValue OpLHS, OpRHS;
2669 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
2670 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
2671 EVT VT = OpLHS.getValueType();
2674 default: llvm_unreachable("Unknown shuffle opcode!");
2676 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
2681 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
2682 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
2686 return DAG.getNode(ARMISD::VEXT, dl, VT,
2688 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
2691 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
2692 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
2695 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
2696 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
2699 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2700 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
2704 static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
2705 SDValue V1 = Op.getOperand(0);
2706 SDValue V2 = Op.getOperand(1);
2707 DebugLoc dl = Op.getDebugLoc();
2708 EVT VT = Op.getValueType();
2709 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
2710 SmallVector<int, 8> ShuffleMask;
2712 // Convert shuffles that are directly supported on NEON to target-specific
2713 // DAG nodes, instead of keeping them as shuffles and matching them again
2714 // during code selection. This is more efficient and avoids the possibility
2715 // of inconsistencies between legalization and selection.
2716 // FIXME: floating-point vectors should be canonicalized to integer vectors
2717 // of the same time so that they get CSEd properly.
2718 SVN->getMask(ShuffleMask);
2720 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
2721 int Lane = SVN->getSplatIndex();
2722 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
2723 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
2725 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
2726 DAG.getConstant(Lane, MVT::i32));
2731 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
2734 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
2735 DAG.getConstant(Imm, MVT::i32));
2738 if (isVREVMask(ShuffleMask, VT, 64))
2739 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
2740 if (isVREVMask(ShuffleMask, VT, 32))
2741 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
2742 if (isVREVMask(ShuffleMask, VT, 16))
2743 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
2745 // Check for Neon shuffles that modify both input vectors in place.
2746 // If both results are used, i.e., if there are two shuffles with the same
2747 // source operands and with masks corresponding to both results of one of
2748 // these operations, DAG memoization will ensure that a single node is
2749 // used for both shuffles.
2750 unsigned WhichResult;
2751 if (isVTRNMask(ShuffleMask, VT, WhichResult))
2752 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2753 V1, V2).getValue(WhichResult);
2754 if (isVUZPMask(ShuffleMask, VT, WhichResult))
2755 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
2756 V1, V2).getValue(WhichResult);
2757 if (isVZIPMask(ShuffleMask, VT, WhichResult))
2758 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
2759 V1, V2).getValue(WhichResult);
2761 // If the shuffle is not directly supported and it has 4 elements, use
2762 // the PerfectShuffle-generated table to synthesize it from other shuffles.
2763 if (VT.getVectorNumElements() == 4 &&
2764 (VT.is128BitVector() || VT.is64BitVector())) {
2765 unsigned PFIndexes[4];
2766 for (unsigned i = 0; i != 4; ++i) {
2767 if (ShuffleMask[i] < 0)
2770 PFIndexes[i] = ShuffleMask[i];
2773 // Compute the index in the perfect shuffle table.
2774 unsigned PFTableIndex =
2775 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2777 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2778 unsigned Cost = (PFEntry >> 30);
2781 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
2787 static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
2788 EVT VT = Op.getValueType();
2789 DebugLoc dl = Op.getDebugLoc();
2790 SDValue Vec = Op.getOperand(0);
2791 SDValue Lane = Op.getOperand(1);
2793 // FIXME: This is invalid for 8 and 16-bit elements - the information about
2794 // sign / zero extension is lost!
2795 Op = DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
2796 Op = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Op, DAG.getValueType(VT));
2798 if (VT.bitsLT(MVT::i32))
2799 Op = DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
2800 else if (VT.bitsGT(MVT::i32))
2801 Op = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Op);
2806 static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
2807 // The only time a CONCAT_VECTORS operation can have legal types is when
2808 // two 64-bit vectors are concatenated to a 128-bit vector.
2809 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
2810 "unexpected CONCAT_VECTORS");
2811 DebugLoc dl = Op.getDebugLoc();
2812 SDValue Val = DAG.getUNDEF(MVT::v2f64);
2813 SDValue Op0 = Op.getOperand(0);
2814 SDValue Op1 = Op.getOperand(1);
2815 if (Op0.getOpcode() != ISD::UNDEF)
2816 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2817 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
2818 DAG.getIntPtrConstant(0));
2819 if (Op1.getOpcode() != ISD::UNDEF)
2820 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2821 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
2822 DAG.getIntPtrConstant(1));
2823 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
2826 SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
2827 switch (Op.getOpcode()) {
2828 default: llvm_unreachable("Don't know how to custom lower this!");
2829 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
2830 case ISD::GlobalAddress:
2831 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
2832 LowerGlobalAddressELF(Op, DAG);
2833 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
2834 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, Subtarget);
2835 case ISD::BR_CC: return LowerBR_CC(Op, DAG, Subtarget);
2836 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
2837 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
2838 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
2839 case ISD::SINT_TO_FP:
2840 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
2841 case ISD::FP_TO_SINT:
2842 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
2843 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
2844 case ISD::RETURNADDR: break;
2845 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
2846 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
2847 case ISD::INTRINSIC_VOID:
2848 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
2849 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
2850 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
2853 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
2854 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
2855 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2856 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
2857 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
2858 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
2863 /// ReplaceNodeResults - Replace the results of node with an illegal result
2864 /// type with new values built out of custom code.
2865 void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
2866 SmallVectorImpl<SDValue>&Results,
2867 SelectionDAG &DAG) {
2868 switch (N->getOpcode()) {
2870 llvm_unreachable("Don't know how to custom expand this!");
2872 case ISD::BIT_CONVERT:
2873 Results.push_back(ExpandBIT_CONVERT(N, DAG));
2877 SDValue Res = LowerShift(N, DAG, Subtarget);
2879 Results.push_back(Res);
2885 //===----------------------------------------------------------------------===//
2886 // ARM Scheduler Hooks
2887 //===----------------------------------------------------------------------===//
2890 ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
2891 MachineBasicBlock *BB,
2892 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
2893 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2894 DebugLoc dl = MI->getDebugLoc();
2895 switch (MI->getOpcode()) {
2897 llvm_unreachable("Unexpected instr type to insert");
2898 case ARM::tMOVCCr_pseudo: {
2899 // To "insert" a SELECT_CC instruction, we actually have to insert the
2900 // diamond control-flow pattern. The incoming instruction knows the
2901 // destination vreg to set, the condition code register to branch on, the
2902 // true/false values to select between, and a branch opcode to use.
2903 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2904 MachineFunction::iterator It = BB;
2910 // cmpTY ccX, r1, r2
2912 // fallthrough --> copy0MBB
2913 MachineBasicBlock *thisMBB = BB;
2914 MachineFunction *F = BB->getParent();
2915 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
2916 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
2917 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
2918 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
2919 F->insert(It, copy0MBB);
2920 F->insert(It, sinkMBB);
2921 // Update machine-CFG edges by first adding all successors of the current
2922 // block to the new block which will contain the Phi node for the select.
2923 // Also inform sdisel of the edge changes.
2924 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
2925 E = BB->succ_end(); I != E; ++I) {
2926 EM->insert(std::make_pair(*I, sinkMBB));
2927 sinkMBB->addSuccessor(*I);
2929 // Next, remove all successors of the current block, and add the true
2930 // and fallthrough blocks as its successors.
2931 while (!BB->succ_empty())
2932 BB->removeSuccessor(BB->succ_begin());
2933 BB->addSuccessor(copy0MBB);
2934 BB->addSuccessor(sinkMBB);
2937 // %FalseValue = ...
2938 // # fallthrough to sinkMBB
2941 // Update machine-CFG edges
2942 BB->addSuccessor(sinkMBB);
2945 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2948 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
2949 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
2950 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
2952 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
2959 case ARM::t2SUBrSPi_:
2960 case ARM::t2SUBrSPi12_:
2961 case ARM::t2SUBrSPs_: {
2962 MachineFunction *MF = BB->getParent();
2963 unsigned DstReg = MI->getOperand(0).getReg();
2964 unsigned SrcReg = MI->getOperand(1).getReg();
2965 bool DstIsDead = MI->getOperand(0).isDead();
2966 bool SrcIsKill = MI->getOperand(1).isKill();
2968 if (SrcReg != ARM::SP) {
2969 // Copy the source to SP from virtual register.
2970 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
2971 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
2972 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
2973 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
2974 .addReg(SrcReg, getKillRegState(SrcIsKill));
2978 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
2979 switch (MI->getOpcode()) {
2981 llvm_unreachable("Unexpected pseudo instruction!");
2987 OpOpc = ARM::tADDspr;
2990 OpOpc = ARM::tSUBspi;
2992 case ARM::t2SUBrSPi_:
2993 OpOpc = ARM::t2SUBrSPi;
2994 NeedPred = true; NeedCC = true;
2996 case ARM::t2SUBrSPi12_:
2997 OpOpc = ARM::t2SUBrSPi12;
3000 case ARM::t2SUBrSPs_:
3001 OpOpc = ARM::t2SUBrSPs;
3002 NeedPred = true; NeedCC = true; NeedOp3 = true;
3005 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
3006 if (OpOpc == ARM::tAND)
3007 AddDefaultT1CC(MIB);
3008 MIB.addReg(ARM::SP);
3009 MIB.addOperand(MI->getOperand(2));
3011 MIB.addOperand(MI->getOperand(3));
3013 AddDefaultPred(MIB);
3017 // Copy the result from SP to virtual register.
3018 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
3019 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3020 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
3021 BuildMI(BB, dl, TII->get(CopyOpc))
3022 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
3024 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
3030 //===----------------------------------------------------------------------===//
3031 // ARM Optimization Hooks
3032 //===----------------------------------------------------------------------===//
3035 SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
3036 TargetLowering::DAGCombinerInfo &DCI) {
3037 SelectionDAG &DAG = DCI.DAG;
3038 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3039 EVT VT = N->getValueType(0);
3040 unsigned Opc = N->getOpcode();
3041 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
3042 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
3043 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
3044 ISD::CondCode CC = ISD::SETCC_INVALID;
3047 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
3049 SDValue CCOp = Slct.getOperand(0);
3050 if (CCOp.getOpcode() == ISD::SETCC)
3051 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
3054 bool DoXform = false;
3056 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
3059 if (LHS.getOpcode() == ISD::Constant &&
3060 cast<ConstantSDNode>(LHS)->isNullValue()) {
3062 } else if (CC != ISD::SETCC_INVALID &&
3063 RHS.getOpcode() == ISD::Constant &&
3064 cast<ConstantSDNode>(RHS)->isNullValue()) {
3065 std::swap(LHS, RHS);
3066 SDValue Op0 = Slct.getOperand(0);
3067 EVT OpVT = isSlctCC ? Op0.getValueType() :
3068 Op0.getOperand(0).getValueType();
3069 bool isInt = OpVT.isInteger();
3070 CC = ISD::getSetCCInverse(CC, isInt);
3072 if (!TLI.isCondCodeLegal(CC, OpVT))
3073 return SDValue(); // Inverse operator isn't legal.
3080 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
3082 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
3083 Slct.getOperand(0), Slct.getOperand(1), CC);
3084 SDValue CCOp = Slct.getOperand(0);
3086 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
3087 CCOp.getOperand(0), CCOp.getOperand(1), CC);
3088 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
3089 CCOp, OtherOp, Result);
3094 /// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
3095 static SDValue PerformADDCombine(SDNode *N,
3096 TargetLowering::DAGCombinerInfo &DCI) {
3097 // added by evan in r37685 with no testcase.
3098 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
3100 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
3101 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
3102 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
3103 if (Result.getNode()) return Result;
3105 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3106 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3107 if (Result.getNode()) return Result;
3113 /// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
3114 static SDValue PerformSUBCombine(SDNode *N,
3115 TargetLowering::DAGCombinerInfo &DCI) {
3116 // added by evan in r37685 with no testcase.
3117 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
3119 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
3120 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3121 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3122 if (Result.getNode()) return Result;
3129 /// PerformFMRRDCombine - Target-specific dag combine xforms for ARMISD::FMRRD.
3130 static SDValue PerformFMRRDCombine(SDNode *N,
3131 TargetLowering::DAGCombinerInfo &DCI) {
3132 // fmrrd(fmdrr x, y) -> x,y
3133 SDValue InDouble = N->getOperand(0);
3134 if (InDouble.getOpcode() == ARMISD::FMDRR)
3135 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
3139 /// getVShiftImm - Check if this is a valid build_vector for the immediate
3140 /// operand of a vector shift operation, where all the elements of the
3141 /// build_vector must have the same constant integer value.
3142 static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
3143 // Ignore bit_converts.
3144 while (Op.getOpcode() == ISD::BIT_CONVERT)
3145 Op = Op.getOperand(0);
3146 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3147 APInt SplatBits, SplatUndef;
3148 unsigned SplatBitSize;
3150 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
3151 HasAnyUndefs, ElementBits) ||
3152 SplatBitSize > ElementBits)
3154 Cnt = SplatBits.getSExtValue();
3158 /// isVShiftLImm - Check if this is a valid build_vector for the immediate
3159 /// operand of a vector shift left operation. That value must be in the range:
3160 /// 0 <= Value < ElementBits for a left shift; or
3161 /// 0 <= Value <= ElementBits for a long left shift.
3162 static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
3163 assert(VT.isVector() && "vector shift count is not a vector type");
3164 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3165 if (! getVShiftImm(Op, ElementBits, Cnt))
3167 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
3170 /// isVShiftRImm - Check if this is a valid build_vector for the immediate
3171 /// operand of a vector shift right operation. For a shift opcode, the value
3172 /// is positive, but for an intrinsic the value count must be negative. The
3173 /// absolute value must be in the range:
3174 /// 1 <= |Value| <= ElementBits for a right shift; or
3175 /// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
3176 static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
3178 assert(VT.isVector() && "vector shift count is not a vector type");
3179 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3180 if (! getVShiftImm(Op, ElementBits, Cnt))
3184 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
3187 /// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
3188 static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
3189 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3192 // Don't do anything for most intrinsics.
3195 // Vector shifts: check for immediate versions and lower them.
3196 // Note: This is done during DAG combining instead of DAG legalizing because
3197 // the build_vectors for 64-bit vector element shift counts are generally
3198 // not legal, and it is hard to see their values after they get legalized to
3199 // loads from a constant pool.
3200 case Intrinsic::arm_neon_vshifts:
3201 case Intrinsic::arm_neon_vshiftu:
3202 case Intrinsic::arm_neon_vshiftls:
3203 case Intrinsic::arm_neon_vshiftlu:
3204 case Intrinsic::arm_neon_vshiftn:
3205 case Intrinsic::arm_neon_vrshifts:
3206 case Intrinsic::arm_neon_vrshiftu:
3207 case Intrinsic::arm_neon_vrshiftn:
3208 case Intrinsic::arm_neon_vqshifts:
3209 case Intrinsic::arm_neon_vqshiftu:
3210 case Intrinsic::arm_neon_vqshiftsu:
3211 case Intrinsic::arm_neon_vqshiftns:
3212 case Intrinsic::arm_neon_vqshiftnu:
3213 case Intrinsic::arm_neon_vqshiftnsu:
3214 case Intrinsic::arm_neon_vqrshiftns:
3215 case Intrinsic::arm_neon_vqrshiftnu:
3216 case Intrinsic::arm_neon_vqrshiftnsu: {
3217 EVT VT = N->getOperand(1).getValueType();
3219 unsigned VShiftOpc = 0;
3222 case Intrinsic::arm_neon_vshifts:
3223 case Intrinsic::arm_neon_vshiftu:
3224 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
3225 VShiftOpc = ARMISD::VSHL;
3228 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
3229 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
3230 ARMISD::VSHRs : ARMISD::VSHRu);
3235 case Intrinsic::arm_neon_vshiftls:
3236 case Intrinsic::arm_neon_vshiftlu:
3237 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
3239 llvm_unreachable("invalid shift count for vshll intrinsic");
3241 case Intrinsic::arm_neon_vrshifts:
3242 case Intrinsic::arm_neon_vrshiftu:
3243 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
3247 case Intrinsic::arm_neon_vqshifts:
3248 case Intrinsic::arm_neon_vqshiftu:
3249 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3253 case Intrinsic::arm_neon_vqshiftsu:
3254 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3256 llvm_unreachable("invalid shift count for vqshlu intrinsic");
3258 case Intrinsic::arm_neon_vshiftn:
3259 case Intrinsic::arm_neon_vrshiftn:
3260 case Intrinsic::arm_neon_vqshiftns:
3261 case Intrinsic::arm_neon_vqshiftnu:
3262 case Intrinsic::arm_neon_vqshiftnsu:
3263 case Intrinsic::arm_neon_vqrshiftns:
3264 case Intrinsic::arm_neon_vqrshiftnu:
3265 case Intrinsic::arm_neon_vqrshiftnsu:
3266 // Narrowing shifts require an immediate right shift.
3267 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
3269 llvm_unreachable("invalid shift count for narrowing vector shift intrinsic");
3272 llvm_unreachable("unhandled vector shift");
3276 case Intrinsic::arm_neon_vshifts:
3277 case Intrinsic::arm_neon_vshiftu:
3278 // Opcode already set above.
3280 case Intrinsic::arm_neon_vshiftls:
3281 case Intrinsic::arm_neon_vshiftlu:
3282 if (Cnt == VT.getVectorElementType().getSizeInBits())
3283 VShiftOpc = ARMISD::VSHLLi;
3285 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
3286 ARMISD::VSHLLs : ARMISD::VSHLLu);
3288 case Intrinsic::arm_neon_vshiftn:
3289 VShiftOpc = ARMISD::VSHRN; break;
3290 case Intrinsic::arm_neon_vrshifts:
3291 VShiftOpc = ARMISD::VRSHRs; break;
3292 case Intrinsic::arm_neon_vrshiftu:
3293 VShiftOpc = ARMISD::VRSHRu; break;
3294 case Intrinsic::arm_neon_vrshiftn:
3295 VShiftOpc = ARMISD::VRSHRN; break;
3296 case Intrinsic::arm_neon_vqshifts:
3297 VShiftOpc = ARMISD::VQSHLs; break;
3298 case Intrinsic::arm_neon_vqshiftu:
3299 VShiftOpc = ARMISD::VQSHLu; break;
3300 case Intrinsic::arm_neon_vqshiftsu:
3301 VShiftOpc = ARMISD::VQSHLsu; break;
3302 case Intrinsic::arm_neon_vqshiftns:
3303 VShiftOpc = ARMISD::VQSHRNs; break;
3304 case Intrinsic::arm_neon_vqshiftnu:
3305 VShiftOpc = ARMISD::VQSHRNu; break;
3306 case Intrinsic::arm_neon_vqshiftnsu:
3307 VShiftOpc = ARMISD::VQSHRNsu; break;
3308 case Intrinsic::arm_neon_vqrshiftns:
3309 VShiftOpc = ARMISD::VQRSHRNs; break;
3310 case Intrinsic::arm_neon_vqrshiftnu:
3311 VShiftOpc = ARMISD::VQRSHRNu; break;
3312 case Intrinsic::arm_neon_vqrshiftnsu:
3313 VShiftOpc = ARMISD::VQRSHRNsu; break;
3316 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
3317 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
3320 case Intrinsic::arm_neon_vshiftins: {
3321 EVT VT = N->getOperand(1).getValueType();
3323 unsigned VShiftOpc = 0;
3325 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
3326 VShiftOpc = ARMISD::VSLI;
3327 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
3328 VShiftOpc = ARMISD::VSRI;
3330 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
3333 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
3334 N->getOperand(1), N->getOperand(2),
3335 DAG.getConstant(Cnt, MVT::i32));
3338 case Intrinsic::arm_neon_vqrshifts:
3339 case Intrinsic::arm_neon_vqrshiftu:
3340 // No immediate versions of these to check for.
3347 /// PerformShiftCombine - Checks for immediate versions of vector shifts and
3348 /// lowers them. As with the vector shift intrinsics, this is done during DAG
3349 /// combining instead of DAG legalizing because the build_vectors for 64-bit
3350 /// vector element shift counts are generally not legal, and it is hard to see
3351 /// their values after they get legalized to loads from a constant pool.
3352 static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
3353 const ARMSubtarget *ST) {
3354 EVT VT = N->getValueType(0);
3356 // Nothing to be done for scalar shifts.
3357 if (! VT.isVector())
3360 assert(ST->hasNEON() && "unexpected vector shift");
3363 switch (N->getOpcode()) {
3364 default: llvm_unreachable("unexpected shift opcode");
3367 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
3368 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
3369 DAG.getConstant(Cnt, MVT::i32));
3374 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
3375 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
3376 ARMISD::VSHRs : ARMISD::VSHRu);
3377 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
3378 DAG.getConstant(Cnt, MVT::i32));
3384 /// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
3385 /// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
3386 static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
3387 const ARMSubtarget *ST) {
3388 SDValue N0 = N->getOperand(0);
3390 // Check for sign- and zero-extensions of vector extract operations of 8-
3391 // and 16-bit vector elements. NEON supports these directly. They are
3392 // handled during DAG combining because type legalization will promote them
3393 // to 32-bit types and it is messy to recognize the operations after that.
3394 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
3395 SDValue Vec = N0.getOperand(0);
3396 SDValue Lane = N0.getOperand(1);
3397 EVT VT = N->getValueType(0);
3398 EVT EltVT = N0.getValueType();
3399 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3401 if (VT == MVT::i32 &&
3402 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
3403 TLI.isTypeLegal(Vec.getValueType())) {
3406 switch (N->getOpcode()) {
3407 default: llvm_unreachable("unexpected opcode");
3408 case ISD::SIGN_EXTEND:
3409 Opc = ARMISD::VGETLANEs;
3411 case ISD::ZERO_EXTEND:
3412 case ISD::ANY_EXTEND:
3413 Opc = ARMISD::VGETLANEu;
3416 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
3423 SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
3424 DAGCombinerInfo &DCI) const {
3425 switch (N->getOpcode()) {
3427 case ISD::ADD: return PerformADDCombine(N, DCI);
3428 case ISD::SUB: return PerformSUBCombine(N, DCI);
3429 case ARMISD::FMRRD: return PerformFMRRDCombine(N, DCI);
3430 case ISD::INTRINSIC_WO_CHAIN:
3431 return PerformIntrinsicCombine(N, DCI.DAG);
3435 return PerformShiftCombine(N, DCI.DAG, Subtarget);
3436 case ISD::SIGN_EXTEND:
3437 case ISD::ZERO_EXTEND:
3438 case ISD::ANY_EXTEND:
3439 return PerformExtendCombine(N, DCI.DAG, Subtarget);
3444 bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
3445 if (!Subtarget->hasV6Ops())
3446 // Pre-v6 does not support unaligned mem access.
3448 else if (!Subtarget->hasV6Ops()) {
3449 // v6 may or may not support unaligned mem access.
3450 if (!Subtarget->isTargetDarwin())
3454 switch (VT.getSimpleVT().SimpleTy) {
3461 // FIXME: VLD1 etc with standard alignment is legal.
3465 static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
3470 switch (VT.getSimpleVT().SimpleTy) {
3471 default: return false;
3486 if ((V & (Scale - 1)) != 0)
3489 return V == (V & ((1LL << 5) - 1));
3492 static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
3493 const ARMSubtarget *Subtarget) {
3500 switch (VT.getSimpleVT().SimpleTy) {
3501 default: return false;
3506 // + imm12 or - imm8
3508 return V == (V & ((1LL << 8) - 1));
3509 return V == (V & ((1LL << 12) - 1));
3512 // Same as ARM mode. FIXME: NEON?
3513 if (!Subtarget->hasVFP2())
3518 return V == (V & ((1LL << 8) - 1));
3522 /// isLegalAddressImmediate - Return true if the integer value can be used
3523 /// as the offset of the target addressing mode for load / store of the
3525 static bool isLegalAddressImmediate(int64_t V, EVT VT,
3526 const ARMSubtarget *Subtarget) {
3533 if (Subtarget->isThumb1Only())
3534 return isLegalT1AddressImmediate(V, VT);
3535 else if (Subtarget->isThumb2())
3536 return isLegalT2AddressImmediate(V, VT, Subtarget);
3541 switch (VT.getSimpleVT().SimpleTy) {
3542 default: return false;
3547 return V == (V & ((1LL << 12) - 1));
3550 return V == (V & ((1LL << 8) - 1));
3553 if (!Subtarget->hasVFP2()) // FIXME: NEON?
3558 return V == (V & ((1LL << 8) - 1));
3562 bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
3564 int Scale = AM.Scale;
3568 switch (VT.getSimpleVT().SimpleTy) {
3569 default: return false;
3578 return Scale == 2 || Scale == 4 || Scale == 8;
3581 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
3585 // Note, we allow "void" uses (basically, uses that aren't loads or
3586 // stores), because arm allows folding a scale into many arithmetic
3587 // operations. This should be made more precise and revisited later.
3589 // Allow r << imm, but the imm has to be a multiple of two.
3590 if (Scale & 1) return false;
3591 return isPowerOf2_32(Scale);
3595 /// isLegalAddressingMode - Return true if the addressing mode represented
3596 /// by AM is legal for this target, for a load/store of the specified type.
3597 bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
3598 const Type *Ty) const {
3599 EVT VT = getValueType(Ty, true);
3600 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
3603 // Can never fold addr of global into load/store.
3608 case 0: // no scale reg, must be "r+i" or "r", or "i".
3611 if (Subtarget->isThumb1Only())
3615 // ARM doesn't support any R+R*scale+imm addr modes.
3622 if (Subtarget->isThumb2())
3623 return isLegalT2ScaledAddressingMode(AM, VT);
3625 int Scale = AM.Scale;
3626 switch (VT.getSimpleVT().SimpleTy) {
3627 default: return false;
3631 if (Scale < 0) Scale = -Scale;
3635 return isPowerOf2_32(Scale & ~1);
3639 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
3644 // Note, we allow "void" uses (basically, uses that aren't loads or
3645 // stores), because arm allows folding a scale into many arithmetic
3646 // operations. This should be made more precise and revisited later.
3648 // Allow r << imm, but the imm has to be a multiple of two.
3649 if (Scale & 1) return false;
3650 return isPowerOf2_32(Scale);
3657 static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
3658 bool isSEXTLoad, SDValue &Base,
3659 SDValue &Offset, bool &isInc,
3660 SelectionDAG &DAG) {
3661 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3664 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
3666 Base = Ptr->getOperand(0);
3667 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
3668 int RHSC = (int)RHS->getZExtValue();
3669 if (RHSC < 0 && RHSC > -256) {
3670 assert(Ptr->getOpcode() == ISD::ADD);
3672 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3676 isInc = (Ptr->getOpcode() == ISD::ADD);
3677 Offset = Ptr->getOperand(1);
3679 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
3681 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
3682 int RHSC = (int)RHS->getZExtValue();
3683 if (RHSC < 0 && RHSC > -0x1000) {
3684 assert(Ptr->getOpcode() == ISD::ADD);
3686 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3687 Base = Ptr->getOperand(0);
3692 if (Ptr->getOpcode() == ISD::ADD) {
3694 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
3695 if (ShOpcVal != ARM_AM::no_shift) {
3696 Base = Ptr->getOperand(1);
3697 Offset = Ptr->getOperand(0);
3699 Base = Ptr->getOperand(0);
3700 Offset = Ptr->getOperand(1);
3705 isInc = (Ptr->getOpcode() == ISD::ADD);
3706 Base = Ptr->getOperand(0);
3707 Offset = Ptr->getOperand(1);
3711 // FIXME: Use FLDM / FSTM to emulate indexed FP load / store.
3715 static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
3716 bool isSEXTLoad, SDValue &Base,
3717 SDValue &Offset, bool &isInc,
3718 SelectionDAG &DAG) {
3719 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3722 Base = Ptr->getOperand(0);
3723 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
3724 int RHSC = (int)RHS->getZExtValue();
3725 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
3726 assert(Ptr->getOpcode() == ISD::ADD);
3728 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3730 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
3731 isInc = Ptr->getOpcode() == ISD::ADD;
3732 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
3740 /// getPreIndexedAddressParts - returns true by value, base pointer and
3741 /// offset pointer and addressing mode by reference if the node's address
3742 /// can be legally represented as pre-indexed load / store address.
3744 ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
3746 ISD::MemIndexedMode &AM,
3747 SelectionDAG &DAG) const {
3748 if (Subtarget->isThumb1Only())
3753 bool isSEXTLoad = false;
3754 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
3755 Ptr = LD->getBasePtr();
3756 VT = LD->getMemoryVT();
3757 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3758 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
3759 Ptr = ST->getBasePtr();
3760 VT = ST->getMemoryVT();
3765 bool isLegal = false;
3766 if (Subtarget->isThumb2())
3767 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
3768 Offset, isInc, DAG);
3770 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
3771 Offset, isInc, DAG);
3775 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
3779 /// getPostIndexedAddressParts - returns true by value, base pointer and
3780 /// offset pointer and addressing mode by reference if this node can be
3781 /// combined with a load / store to form a post-indexed load / store.
3782 bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
3785 ISD::MemIndexedMode &AM,
3786 SelectionDAG &DAG) const {
3787 if (Subtarget->isThumb1Only())
3792 bool isSEXTLoad = false;
3793 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
3794 VT = LD->getMemoryVT();
3795 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
3796 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
3797 VT = ST->getMemoryVT();
3802 bool isLegal = false;
3803 if (Subtarget->isThumb2())
3804 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
3807 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
3812 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
3816 void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
3820 const SelectionDAG &DAG,
3821 unsigned Depth) const {
3822 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
3823 switch (Op.getOpcode()) {
3825 case ARMISD::CMOV: {
3826 // Bits are known zero/one if known on the LHS and RHS.
3827 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
3828 if (KnownZero == 0 && KnownOne == 0) return;
3830 APInt KnownZeroRHS, KnownOneRHS;
3831 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
3832 KnownZeroRHS, KnownOneRHS, Depth+1);
3833 KnownZero &= KnownZeroRHS;
3834 KnownOne &= KnownOneRHS;
3840 //===----------------------------------------------------------------------===//
3841 // ARM Inline Assembly Support
3842 //===----------------------------------------------------------------------===//
3844 /// getConstraintType - Given a constraint letter, return the type of
3845 /// constraint it is for this target.
3846 ARMTargetLowering::ConstraintType
3847 ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
3848 if (Constraint.size() == 1) {
3849 switch (Constraint[0]) {
3851 case 'l': return C_RegisterClass;
3852 case 'w': return C_RegisterClass;
3855 return TargetLowering::getConstraintType(Constraint);
3858 std::pair<unsigned, const TargetRegisterClass*>
3859 ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
3861 if (Constraint.size() == 1) {
3862 // GCC RS6000 Constraint Letters
3863 switch (Constraint[0]) {
3865 if (Subtarget->isThumb1Only())
3866 return std::make_pair(0U, ARM::tGPRRegisterClass);
3868 return std::make_pair(0U, ARM::GPRRegisterClass);
3870 return std::make_pair(0U, ARM::GPRRegisterClass);
3873 return std::make_pair(0U, ARM::SPRRegisterClass);
3875 return std::make_pair(0U, ARM::DPRRegisterClass);
3879 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3882 std::vector<unsigned> ARMTargetLowering::
3883 getRegClassForInlineAsmConstraint(const std::string &Constraint,
3885 if (Constraint.size() != 1)
3886 return std::vector<unsigned>();
3888 switch (Constraint[0]) { // GCC ARM Constraint Letters
3891 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
3892 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
3895 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
3896 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
3897 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
3898 ARM::R12, ARM::LR, 0);
3901 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
3902 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
3903 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
3904 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
3905 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
3906 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
3907 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
3908 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
3910 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
3911 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
3912 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
3913 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
3917 return std::vector<unsigned>();
3920 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3921 /// vector. If it is invalid, don't add anything to Ops.
3922 void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3925 std::vector<SDValue>&Ops,
3926 SelectionDAG &DAG) const {
3927 SDValue Result(0, 0);
3929 switch (Constraint) {
3931 case 'I': case 'J': case 'K': case 'L':
3932 case 'M': case 'N': case 'O':
3933 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
3937 int64_t CVal64 = C->getSExtValue();
3938 int CVal = (int) CVal64;
3939 // None of these constraints allow values larger than 32 bits. Check
3940 // that the value fits in an int.
3944 switch (Constraint) {
3946 if (Subtarget->isThumb1Only()) {
3947 // This must be a constant between 0 and 255, for ADD
3949 if (CVal >= 0 && CVal <= 255)
3951 } else if (Subtarget->isThumb2()) {
3952 // A constant that can be used as an immediate value in a
3953 // data-processing instruction.
3954 if (ARM_AM::getT2SOImmVal(CVal) != -1)
3957 // A constant that can be used as an immediate value in a
3958 // data-processing instruction.
3959 if (ARM_AM::getSOImmVal(CVal) != -1)
3965 if (Subtarget->isThumb()) { // FIXME thumb2
3966 // This must be a constant between -255 and -1, for negated ADD
3967 // immediates. This can be used in GCC with an "n" modifier that
3968 // prints the negated value, for use with SUB instructions. It is
3969 // not useful otherwise but is implemented for compatibility.
3970 if (CVal >= -255 && CVal <= -1)
3973 // This must be a constant between -4095 and 4095. It is not clear
3974 // what this constraint is intended for. Implemented for
3975 // compatibility with GCC.
3976 if (CVal >= -4095 && CVal <= 4095)
3982 if (Subtarget->isThumb1Only()) {
3983 // A 32-bit value where only one byte has a nonzero value. Exclude
3984 // zero to match GCC. This constraint is used by GCC internally for
3985 // constants that can be loaded with a move/shift combination.
3986 // It is not useful otherwise but is implemented for compatibility.
3987 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
3989 } else if (Subtarget->isThumb2()) {
3990 // A constant whose bitwise inverse can be used as an immediate
3991 // value in a data-processing instruction. This can be used in GCC
3992 // with a "B" modifier that prints the inverted value, for use with
3993 // BIC and MVN instructions. It is not useful otherwise but is
3994 // implemented for compatibility.
3995 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
3998 // A constant whose bitwise inverse can be used as an immediate
3999 // value in a data-processing instruction. This can be used in GCC
4000 // with a "B" modifier that prints the inverted value, for use with
4001 // BIC and MVN instructions. It is not useful otherwise but is
4002 // implemented for compatibility.
4003 if (ARM_AM::getSOImmVal(~CVal) != -1)
4009 if (Subtarget->isThumb1Only()) {
4010 // This must be a constant between -7 and 7,
4011 // for 3-operand ADD/SUB immediate instructions.
4012 if (CVal >= -7 && CVal < 7)
4014 } else if (Subtarget->isThumb2()) {
4015 // A constant whose negation can be used as an immediate value in a
4016 // data-processing instruction. This can be used in GCC with an "n"
4017 // modifier that prints the negated value, for use with SUB
4018 // instructions. It is not useful otherwise but is implemented for
4020 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
4023 // A constant whose negation can be used as an immediate value in a
4024 // data-processing instruction. This can be used in GCC with an "n"
4025 // modifier that prints the negated value, for use with SUB
4026 // instructions. It is not useful otherwise but is implemented for
4028 if (ARM_AM::getSOImmVal(-CVal) != -1)
4034 if (Subtarget->isThumb()) { // FIXME thumb2
4035 // This must be a multiple of 4 between 0 and 1020, for
4036 // ADD sp + immediate.
4037 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
4040 // A power of two or a constant between 0 and 32. This is used in
4041 // GCC for the shift amount on shifted register operands, but it is
4042 // useful in general for any shift amounts.
4043 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
4049 if (Subtarget->isThumb()) { // FIXME thumb2
4050 // This must be a constant between 0 and 31, for shift amounts.
4051 if (CVal >= 0 && CVal <= 31)
4057 if (Subtarget->isThumb()) { // FIXME thumb2
4058 // This must be a multiple of 4 between -508 and 508, for
4059 // ADD/SUB sp = sp + immediate.
4060 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
4065 Result = DAG.getTargetConstant(CVal, Op.getValueType());
4069 if (Result.getNode()) {
4070 Ops.push_back(Result);
4073 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,