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/GlobalValue.h"
29 #include "llvm/Instruction.h"
30 #include "llvm/Intrinsics.h"
31 #include "llvm/Type.h"
32 #include "llvm/CodeGen/CallingConvLower.h"
33 #include "llvm/CodeGen/MachineBasicBlock.h"
34 #include "llvm/CodeGen/MachineFrameInfo.h"
35 #include "llvm/CodeGen/MachineFunction.h"
36 #include "llvm/CodeGen/MachineInstrBuilder.h"
37 #include "llvm/CodeGen/MachineRegisterInfo.h"
38 #include "llvm/CodeGen/PseudoSourceValue.h"
39 #include "llvm/CodeGen/SelectionDAG.h"
40 #include "llvm/Target/TargetOptions.h"
41 #include "llvm/ADT/VectorExtras.h"
42 #include "llvm/Support/CommandLine.h"
43 #include "llvm/Support/ErrorHandling.h"
44 #include "llvm/Support/MathExtras.h"
45 #include "llvm/Support/raw_ostream.h"
49 static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
50 CCValAssign::LocInfo &LocInfo,
51 ISD::ArgFlagsTy &ArgFlags,
53 static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
54 CCValAssign::LocInfo &LocInfo,
55 ISD::ArgFlagsTy &ArgFlags,
57 static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
58 CCValAssign::LocInfo &LocInfo,
59 ISD::ArgFlagsTy &ArgFlags,
61 static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
62 CCValAssign::LocInfo &LocInfo,
63 ISD::ArgFlagsTy &ArgFlags,
66 void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
67 EVT PromotedBitwiseVT) {
68 if (VT != PromotedLdStVT) {
69 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
70 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
71 PromotedLdStVT.getSimpleVT());
73 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
74 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
75 PromotedLdStVT.getSimpleVT());
78 EVT ElemTy = VT.getVectorElementType();
79 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
80 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
81 if (ElemTy == MVT::i8 || ElemTy == MVT::i16)
82 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
83 if (ElemTy != MVT::i32) {
84 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
85 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
86 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
87 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
89 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
90 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
91 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Custom);
92 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Expand);
94 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
95 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
96 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
99 // Promote all bit-wise operations.
100 if (VT.isInteger() && VT != PromotedBitwiseVT) {
101 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
102 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
103 PromotedBitwiseVT.getSimpleVT());
104 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
105 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
106 PromotedBitwiseVT.getSimpleVT());
107 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
108 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
109 PromotedBitwiseVT.getSimpleVT());
112 // Neon does not support vector divide/remainder operations.
113 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
114 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
115 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
116 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
117 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
118 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
121 void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
122 addRegisterClass(VT, ARM::DPRRegisterClass);
123 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
126 void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
127 addRegisterClass(VT, ARM::QPRRegisterClass);
128 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
131 static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
132 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
133 return new TargetLoweringObjectFileMachO();
134 return new ARMElfTargetObjectFile();
137 ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
138 : TargetLowering(TM, createTLOF(TM)) {
139 Subtarget = &TM.getSubtarget<ARMSubtarget>();
141 if (Subtarget->isTargetDarwin()) {
142 // Uses VFP for Thumb libfuncs if available.
143 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
144 // Single-precision floating-point arithmetic.
145 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
146 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
147 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
148 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
150 // Double-precision floating-point arithmetic.
151 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
152 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
153 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
154 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
156 // Single-precision comparisons.
157 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
158 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
159 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
160 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
161 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
162 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
163 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
164 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
166 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
167 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
168 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
169 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
170 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
171 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
172 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
173 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
175 // Double-precision comparisons.
176 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
177 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
178 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
179 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
180 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
181 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
182 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
183 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
185 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
186 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
187 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
188 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
189 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
190 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
191 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
192 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
194 // Floating-point to integer conversions.
195 // i64 conversions are done via library routines even when generating VFP
196 // instructions, so use the same ones.
197 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
198 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
199 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
200 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
202 // Conversions between floating types.
203 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
204 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
206 // Integer to floating-point conversions.
207 // i64 conversions are done via library routines even when generating VFP
208 // instructions, so use the same ones.
209 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
210 // e.g., __floatunsidf vs. __floatunssidfvfp.
211 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
212 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
213 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
214 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
218 // These libcalls are not available in 32-bit.
219 setLibcallName(RTLIB::SHL_I128, 0);
220 setLibcallName(RTLIB::SRL_I128, 0);
221 setLibcallName(RTLIB::SRA_I128, 0);
223 // Libcalls should use the AAPCS base standard ABI, even if hard float
224 // is in effect, as per the ARM RTABI specification, section 4.1.2.
225 if (Subtarget->isAAPCS_ABI()) {
226 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
227 setLibcallCallingConv(static_cast<RTLIB::Libcall>(i),
228 CallingConv::ARM_AAPCS);
232 if (Subtarget->isThumb1Only())
233 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
235 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
236 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
237 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
238 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
240 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
243 if (Subtarget->hasNEON()) {
244 addDRTypeForNEON(MVT::v2f32);
245 addDRTypeForNEON(MVT::v8i8);
246 addDRTypeForNEON(MVT::v4i16);
247 addDRTypeForNEON(MVT::v2i32);
248 addDRTypeForNEON(MVT::v1i64);
250 addQRTypeForNEON(MVT::v4f32);
251 addQRTypeForNEON(MVT::v2f64);
252 addQRTypeForNEON(MVT::v16i8);
253 addQRTypeForNEON(MVT::v8i16);
254 addQRTypeForNEON(MVT::v4i32);
255 addQRTypeForNEON(MVT::v2i64);
257 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
258 // neither Neon nor VFP support any arithmetic operations on it.
259 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
260 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
261 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
262 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
263 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
264 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
265 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
266 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
267 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
268 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
269 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
270 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
271 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
272 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
273 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
274 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
275 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
276 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
277 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
278 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
279 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
280 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
281 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
282 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
284 // Neon does not support some operations on v1i64 and v2i64 types.
285 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
286 setOperationAction(ISD::MUL, MVT::v2i64, Expand);
287 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
288 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
290 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
291 setTargetDAGCombine(ISD::SHL);
292 setTargetDAGCombine(ISD::SRL);
293 setTargetDAGCombine(ISD::SRA);
294 setTargetDAGCombine(ISD::SIGN_EXTEND);
295 setTargetDAGCombine(ISD::ZERO_EXTEND);
296 setTargetDAGCombine(ISD::ANY_EXTEND);
299 computeRegisterProperties();
301 // ARM does not have f32 extending load.
302 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
304 // ARM does not have i1 sign extending load.
305 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
307 // ARM supports all 4 flavors of integer indexed load / store.
308 if (!Subtarget->isThumb1Only()) {
309 for (unsigned im = (unsigned)ISD::PRE_INC;
310 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
311 setIndexedLoadAction(im, MVT::i1, Legal);
312 setIndexedLoadAction(im, MVT::i8, Legal);
313 setIndexedLoadAction(im, MVT::i16, Legal);
314 setIndexedLoadAction(im, MVT::i32, Legal);
315 setIndexedStoreAction(im, MVT::i1, Legal);
316 setIndexedStoreAction(im, MVT::i8, Legal);
317 setIndexedStoreAction(im, MVT::i16, Legal);
318 setIndexedStoreAction(im, MVT::i32, Legal);
322 // i64 operation support.
323 if (Subtarget->isThumb1Only()) {
324 setOperationAction(ISD::MUL, MVT::i64, Expand);
325 setOperationAction(ISD::MULHU, MVT::i32, Expand);
326 setOperationAction(ISD::MULHS, MVT::i32, Expand);
327 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
328 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
330 setOperationAction(ISD::MUL, MVT::i64, Expand);
331 setOperationAction(ISD::MULHU, MVT::i32, Expand);
332 if (!Subtarget->hasV6Ops())
333 setOperationAction(ISD::MULHS, MVT::i32, Expand);
335 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
336 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
337 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
338 setOperationAction(ISD::SRL, MVT::i64, Custom);
339 setOperationAction(ISD::SRA, MVT::i64, Custom);
341 // ARM does not have ROTL.
342 setOperationAction(ISD::ROTL, MVT::i32, Expand);
343 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
344 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
345 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
346 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
348 // Only ARMv6 has BSWAP.
349 if (!Subtarget->hasV6Ops())
350 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
352 // These are expanded into libcalls.
353 setOperationAction(ISD::SDIV, MVT::i32, Expand);
354 setOperationAction(ISD::UDIV, MVT::i32, Expand);
355 setOperationAction(ISD::SREM, MVT::i32, Expand);
356 setOperationAction(ISD::UREM, MVT::i32, Expand);
357 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
358 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
360 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
361 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
362 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
363 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
364 setOperationAction(ISD::BlockAddress, 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, Custom);
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 VMOVRRD, i64 -> f64 to VMOVDRR 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);
396 setOperationAction(ISD::SETCC, MVT::i32, Expand);
397 setOperationAction(ISD::SETCC, MVT::f32, Expand);
398 setOperationAction(ISD::SETCC, MVT::f64, Expand);
399 setOperationAction(ISD::SELECT, MVT::i32, Expand);
400 setOperationAction(ISD::SELECT, MVT::f32, Expand);
401 setOperationAction(ISD::SELECT, MVT::f64, Expand);
402 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
403 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
404 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
406 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
407 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
408 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
409 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
410 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
412 // We don't support sin/cos/fmod/copysign/pow
413 setOperationAction(ISD::FSIN, MVT::f64, Expand);
414 setOperationAction(ISD::FSIN, MVT::f32, Expand);
415 setOperationAction(ISD::FCOS, MVT::f32, Expand);
416 setOperationAction(ISD::FCOS, MVT::f64, Expand);
417 setOperationAction(ISD::FREM, MVT::f64, Expand);
418 setOperationAction(ISD::FREM, MVT::f32, Expand);
419 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
420 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
421 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
423 setOperationAction(ISD::FPOW, MVT::f64, Expand);
424 setOperationAction(ISD::FPOW, MVT::f32, Expand);
426 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
427 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
428 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
429 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
430 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
431 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
434 // We have target-specific dag combine patterns for the following nodes:
435 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
436 setTargetDAGCombine(ISD::ADD);
437 setTargetDAGCombine(ISD::SUB);
439 setStackPointerRegisterToSaveRestore(ARM::SP);
440 setSchedulingPreference(SchedulingForRegPressure);
442 // FIXME: If-converter should use instruction latency to determine
443 // profitability rather than relying on fixed limits.
444 if (Subtarget->getCPUString() == "generic") {
445 // Generic (and overly aggressive) if-conversion limits.
446 setIfCvtBlockSizeLimit(10);
447 setIfCvtDupBlockSizeLimit(2);
448 } else if (Subtarget->hasV6Ops()) {
449 setIfCvtBlockSizeLimit(2);
450 setIfCvtDupBlockSizeLimit(1);
452 setIfCvtBlockSizeLimit(3);
453 setIfCvtDupBlockSizeLimit(2);
456 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
457 // Do not enable CodePlacementOpt for now: it currently runs after the
458 // ARMConstantIslandPass and messes up branch relaxation and placement
459 // of constant islands.
460 // benefitFromCodePlacementOpt = true;
463 const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
466 case ARMISD::Wrapper: return "ARMISD::Wrapper";
467 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
468 case ARMISD::CALL: return "ARMISD::CALL";
469 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
470 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
471 case ARMISD::tCALL: return "ARMISD::tCALL";
472 case ARMISD::BRCOND: return "ARMISD::BRCOND";
473 case ARMISD::BR_JT: return "ARMISD::BR_JT";
474 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
475 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
476 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
477 case ARMISD::CMP: return "ARMISD::CMP";
478 case ARMISD::CMPZ: return "ARMISD::CMPZ";
479 case ARMISD::CMPFP: return "ARMISD::CMPFP";
480 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
481 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
482 case ARMISD::CMOV: return "ARMISD::CMOV";
483 case ARMISD::CNEG: return "ARMISD::CNEG";
485 case ARMISD::FTOSI: return "ARMISD::FTOSI";
486 case ARMISD::FTOUI: return "ARMISD::FTOUI";
487 case ARMISD::SITOF: return "ARMISD::SITOF";
488 case ARMISD::UITOF: return "ARMISD::UITOF";
490 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
491 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
492 case ARMISD::RRX: return "ARMISD::RRX";
494 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
495 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
497 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
498 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
500 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
502 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
504 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
505 case ARMISD::SYNCBARRIER: return "ARMISD::SYNCBARRIER";
507 case ARMISD::VCEQ: return "ARMISD::VCEQ";
508 case ARMISD::VCGE: return "ARMISD::VCGE";
509 case ARMISD::VCGEU: return "ARMISD::VCGEU";
510 case ARMISD::VCGT: return "ARMISD::VCGT";
511 case ARMISD::VCGTU: return "ARMISD::VCGTU";
512 case ARMISD::VTST: return "ARMISD::VTST";
514 case ARMISD::VSHL: return "ARMISD::VSHL";
515 case ARMISD::VSHRs: return "ARMISD::VSHRs";
516 case ARMISD::VSHRu: return "ARMISD::VSHRu";
517 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
518 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
519 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
520 case ARMISD::VSHRN: return "ARMISD::VSHRN";
521 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
522 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
523 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
524 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
525 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
526 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
527 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
528 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
529 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
530 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
531 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
532 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
533 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
534 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
535 case ARMISD::VDUP: return "ARMISD::VDUP";
536 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
537 case ARMISD::VEXT: return "ARMISD::VEXT";
538 case ARMISD::VREV64: return "ARMISD::VREV64";
539 case ARMISD::VREV32: return "ARMISD::VREV32";
540 case ARMISD::VREV16: return "ARMISD::VREV16";
541 case ARMISD::VZIP: return "ARMISD::VZIP";
542 case ARMISD::VUZP: return "ARMISD::VUZP";
543 case ARMISD::VTRN: return "ARMISD::VTRN";
547 /// getFunctionAlignment - Return the Log2 alignment of this function.
548 unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
549 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 0 : 1;
552 //===----------------------------------------------------------------------===//
554 //===----------------------------------------------------------------------===//
556 /// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
557 static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
559 default: llvm_unreachable("Unknown condition code!");
560 case ISD::SETNE: return ARMCC::NE;
561 case ISD::SETEQ: return ARMCC::EQ;
562 case ISD::SETGT: return ARMCC::GT;
563 case ISD::SETGE: return ARMCC::GE;
564 case ISD::SETLT: return ARMCC::LT;
565 case ISD::SETLE: return ARMCC::LE;
566 case ISD::SETUGT: return ARMCC::HI;
567 case ISD::SETUGE: return ARMCC::HS;
568 case ISD::SETULT: return ARMCC::LO;
569 case ISD::SETULE: return ARMCC::LS;
573 /// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
574 static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
575 ARMCC::CondCodes &CondCode2) {
576 CondCode2 = ARMCC::AL;
578 default: llvm_unreachable("Unknown FP condition!");
580 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
582 case ISD::SETOGT: CondCode = ARMCC::GT; break;
584 case ISD::SETOGE: CondCode = ARMCC::GE; break;
585 case ISD::SETOLT: CondCode = ARMCC::MI; break;
586 case ISD::SETOLE: CondCode = ARMCC::LS; break;
587 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
588 case ISD::SETO: CondCode = ARMCC::VC; break;
589 case ISD::SETUO: CondCode = ARMCC::VS; break;
590 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
591 case ISD::SETUGT: CondCode = ARMCC::HI; break;
592 case ISD::SETUGE: CondCode = ARMCC::PL; break;
594 case ISD::SETULT: CondCode = ARMCC::LT; break;
596 case ISD::SETULE: CondCode = ARMCC::LE; break;
598 case ISD::SETUNE: CondCode = ARMCC::NE; break;
602 //===----------------------------------------------------------------------===//
603 // Calling Convention Implementation
604 //===----------------------------------------------------------------------===//
606 #include "ARMGenCallingConv.inc"
608 // APCS f64 is in register pairs, possibly split to stack
609 static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
610 CCValAssign::LocInfo &LocInfo,
611 CCState &State, bool CanFail) {
612 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
614 // Try to get the first register.
615 if (unsigned Reg = State.AllocateReg(RegList, 4))
616 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
618 // For the 2nd half of a v2f64, do not fail.
622 // Put the whole thing on the stack.
623 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
624 State.AllocateStack(8, 4),
629 // Try to get the second register.
630 if (unsigned Reg = State.AllocateReg(RegList, 4))
631 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
633 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
634 State.AllocateStack(4, 4),
639 static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
640 CCValAssign::LocInfo &LocInfo,
641 ISD::ArgFlagsTy &ArgFlags,
643 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
645 if (LocVT == MVT::v2f64 &&
646 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
648 return true; // we handled it
651 // AAPCS f64 is in aligned register pairs
652 static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
653 CCValAssign::LocInfo &LocInfo,
654 CCState &State, bool CanFail) {
655 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
656 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
658 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
660 // For the 2nd half of a v2f64, do not just fail.
664 // Put the whole thing on the stack.
665 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
666 State.AllocateStack(8, 8),
672 for (i = 0; i < 2; ++i)
673 if (HiRegList[i] == Reg)
676 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
677 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
682 static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
683 CCValAssign::LocInfo &LocInfo,
684 ISD::ArgFlagsTy &ArgFlags,
686 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
688 if (LocVT == MVT::v2f64 &&
689 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
691 return true; // we handled it
694 static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
695 CCValAssign::LocInfo &LocInfo, CCState &State) {
696 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
697 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
699 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
701 return false; // we didn't handle it
704 for (i = 0; i < 2; ++i)
705 if (HiRegList[i] == Reg)
708 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
709 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
714 static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
715 CCValAssign::LocInfo &LocInfo,
716 ISD::ArgFlagsTy &ArgFlags,
718 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
720 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
722 return true; // we handled it
725 static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
726 CCValAssign::LocInfo &LocInfo,
727 ISD::ArgFlagsTy &ArgFlags,
729 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
733 /// CCAssignFnForNode - Selects the correct CCAssignFn for a the
734 /// given CallingConvention value.
735 CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
737 bool isVarArg) const {
740 llvm_unreachable("Unsupported calling convention");
742 case CallingConv::Fast:
743 // Use target triple & subtarget features to do actual dispatch.
744 if (Subtarget->isAAPCS_ABI()) {
745 if (Subtarget->hasVFP2() &&
746 FloatABIType == FloatABI::Hard && !isVarArg)
747 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
749 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
751 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
752 case CallingConv::ARM_AAPCS_VFP:
753 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
754 case CallingConv::ARM_AAPCS:
755 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
756 case CallingConv::ARM_APCS:
757 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
761 /// LowerCallResult - Lower the result values of a call into the
762 /// appropriate copies out of appropriate physical registers.
764 ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
765 CallingConv::ID CallConv, bool isVarArg,
766 const SmallVectorImpl<ISD::InputArg> &Ins,
767 DebugLoc dl, SelectionDAG &DAG,
768 SmallVectorImpl<SDValue> &InVals) {
770 // Assign locations to each value returned by this call.
771 SmallVector<CCValAssign, 16> RVLocs;
772 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
773 RVLocs, *DAG.getContext());
774 CCInfo.AnalyzeCallResult(Ins,
775 CCAssignFnForNode(CallConv, /* Return*/ true,
778 // Copy all of the result registers out of their specified physreg.
779 for (unsigned i = 0; i != RVLocs.size(); ++i) {
780 CCValAssign VA = RVLocs[i];
783 if (VA.needsCustom()) {
784 // Handle f64 or half of a v2f64.
785 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
787 Chain = Lo.getValue(1);
788 InFlag = Lo.getValue(2);
789 VA = RVLocs[++i]; // skip ahead to next loc
790 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
792 Chain = Hi.getValue(1);
793 InFlag = Hi.getValue(2);
794 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
796 if (VA.getLocVT() == MVT::v2f64) {
797 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
798 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
799 DAG.getConstant(0, MVT::i32));
801 VA = RVLocs[++i]; // skip ahead to next loc
802 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
803 Chain = Lo.getValue(1);
804 InFlag = Lo.getValue(2);
805 VA = RVLocs[++i]; // skip ahead to next loc
806 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
807 Chain = Hi.getValue(1);
808 InFlag = Hi.getValue(2);
809 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
810 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
811 DAG.getConstant(1, MVT::i32));
814 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
816 Chain = Val.getValue(1);
817 InFlag = Val.getValue(2);
820 switch (VA.getLocInfo()) {
821 default: llvm_unreachable("Unknown loc info!");
822 case CCValAssign::Full: break;
823 case CCValAssign::BCvt:
824 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
828 InVals.push_back(Val);
834 /// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
835 /// by "Src" to address "Dst" of size "Size". Alignment information is
836 /// specified by the specific parameter attribute. The copy will be passed as
837 /// a byval function parameter.
838 /// Sometimes what we are copying is the end of a larger object, the part that
839 /// does not fit in registers.
841 CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
842 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
844 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
845 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
846 /*AlwaysInline=*/false, NULL, 0, NULL, 0);
849 /// LowerMemOpCallTo - Store the argument to the stack.
851 ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
852 SDValue StackPtr, SDValue Arg,
853 DebugLoc dl, SelectionDAG &DAG,
854 const CCValAssign &VA,
855 ISD::ArgFlagsTy Flags) {
856 unsigned LocMemOffset = VA.getLocMemOffset();
857 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
858 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
859 if (Flags.isByVal()) {
860 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
862 return DAG.getStore(Chain, dl, Arg, PtrOff,
863 PseudoSourceValue::getStack(), LocMemOffset);
866 void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
867 SDValue Chain, SDValue &Arg,
868 RegsToPassVector &RegsToPass,
869 CCValAssign &VA, CCValAssign &NextVA,
871 SmallVector<SDValue, 8> &MemOpChains,
872 ISD::ArgFlagsTy Flags) {
874 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
875 DAG.getVTList(MVT::i32, MVT::i32), Arg);
876 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
878 if (NextVA.isRegLoc())
879 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
881 assert(NextVA.isMemLoc());
882 if (StackPtr.getNode() == 0)
883 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
885 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
891 /// LowerCall - Lowering a call into a callseq_start <-
892 /// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
895 ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
896 CallingConv::ID CallConv, bool isVarArg,
898 const SmallVectorImpl<ISD::OutputArg> &Outs,
899 const SmallVectorImpl<ISD::InputArg> &Ins,
900 DebugLoc dl, SelectionDAG &DAG,
901 SmallVectorImpl<SDValue> &InVals) {
903 // Analyze operands of the call, assigning locations to each operand.
904 SmallVector<CCValAssign, 16> ArgLocs;
905 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
907 CCInfo.AnalyzeCallOperands(Outs,
908 CCAssignFnForNode(CallConv, /* Return*/ false,
911 // Get a count of how many bytes are to be pushed on the stack.
912 unsigned NumBytes = CCInfo.getNextStackOffset();
914 // Adjust the stack pointer for the new arguments...
915 // These operations are automatically eliminated by the prolog/epilog pass
916 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
918 SDValue StackPtr = DAG.getRegister(ARM::SP, MVT::i32);
920 RegsToPassVector RegsToPass;
921 SmallVector<SDValue, 8> MemOpChains;
923 // Walk the register/memloc assignments, inserting copies/loads. In the case
924 // of tail call optimization, arguments are handled later.
925 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
928 CCValAssign &VA = ArgLocs[i];
929 SDValue Arg = Outs[realArgIdx].Val;
930 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
932 // Promote the value if needed.
933 switch (VA.getLocInfo()) {
934 default: llvm_unreachable("Unknown loc info!");
935 case CCValAssign::Full: break;
936 case CCValAssign::SExt:
937 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
939 case CCValAssign::ZExt:
940 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
942 case CCValAssign::AExt:
943 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
945 case CCValAssign::BCvt:
946 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
950 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
951 if (VA.needsCustom()) {
952 if (VA.getLocVT() == MVT::v2f64) {
953 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
954 DAG.getConstant(0, MVT::i32));
955 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
956 DAG.getConstant(1, MVT::i32));
958 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
959 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
961 VA = ArgLocs[++i]; // skip ahead to next loc
963 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
964 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
966 assert(VA.isMemLoc());
967 if (StackPtr.getNode() == 0)
968 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
970 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
971 dl, DAG, VA, Flags));
974 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
975 StackPtr, MemOpChains, Flags);
977 } else if (VA.isRegLoc()) {
978 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
980 assert(VA.isMemLoc());
981 if (StackPtr.getNode() == 0)
982 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
984 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
985 dl, DAG, VA, Flags));
989 if (!MemOpChains.empty())
990 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
991 &MemOpChains[0], MemOpChains.size());
993 // Build a sequence of copy-to-reg nodes chained together with token chain
994 // and flag operands which copy the outgoing args into the appropriate regs.
996 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
997 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
998 RegsToPass[i].second, InFlag);
999 InFlag = Chain.getValue(1);
1002 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1003 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1004 // node so that legalize doesn't hack it.
1005 bool isDirect = false;
1006 bool isARMFunc = false;
1007 bool isLocalARMFunc = false;
1008 MachineFunction &MF = DAG.getMachineFunction();
1009 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1010 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1011 GlobalValue *GV = G->getGlobal();
1013 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
1014 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
1015 getTargetMachine().getRelocationModel() != Reloc::Static;
1016 isARMFunc = !Subtarget->isThumb() || isStub;
1017 // ARM call to a local ARM function is predicable.
1018 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
1019 // tBX takes a register source operand.
1020 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
1021 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1022 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1025 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1026 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1027 Callee = DAG.getLoad(getPointerTy(), dl,
1028 DAG.getEntryNode(), CPAddr,
1029 PseudoSourceValue::getConstantPool(), 0);
1030 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
1031 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
1032 getPointerTy(), Callee, PICLabel);
1034 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
1035 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1037 bool isStub = Subtarget->isTargetDarwin() &&
1038 getTargetMachine().getRelocationModel() != Reloc::Static;
1039 isARMFunc = !Subtarget->isThumb() || isStub;
1040 // tBX takes a register source operand.
1041 const char *Sym = S->getSymbol();
1042 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
1043 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1044 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1045 Sym, ARMPCLabelIndex, 4);
1046 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1047 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1048 Callee = DAG.getLoad(getPointerTy(), dl,
1049 DAG.getEntryNode(), CPAddr,
1050 PseudoSourceValue::getConstantPool(), 0);
1051 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
1052 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
1053 getPointerTy(), Callee, PICLabel);
1055 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
1058 // FIXME: handle tail calls differently.
1060 if (Subtarget->isThumb()) {
1061 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
1062 CallOpc = ARMISD::CALL_NOLINK;
1064 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1066 CallOpc = (isDirect || Subtarget->hasV5TOps())
1067 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1068 : ARMISD::CALL_NOLINK;
1070 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
1071 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
1072 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
1073 InFlag = Chain.getValue(1);
1076 std::vector<SDValue> Ops;
1077 Ops.push_back(Chain);
1078 Ops.push_back(Callee);
1080 // Add argument registers to the end of the list so that they are known live
1082 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1083 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1084 RegsToPass[i].second.getValueType()));
1086 if (InFlag.getNode())
1087 Ops.push_back(InFlag);
1088 // Returns a chain and a flag for retval copy to use.
1089 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
1090 &Ops[0], Ops.size());
1091 InFlag = Chain.getValue(1);
1093 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1094 DAG.getIntPtrConstant(0, true), InFlag);
1096 InFlag = Chain.getValue(1);
1098 // Handle result values, copying them out of physregs into vregs that we
1100 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1105 ARMTargetLowering::LowerReturn(SDValue Chain,
1106 CallingConv::ID CallConv, bool isVarArg,
1107 const SmallVectorImpl<ISD::OutputArg> &Outs,
1108 DebugLoc dl, SelectionDAG &DAG) {
1110 // CCValAssign - represent the assignment of the return value to a location.
1111 SmallVector<CCValAssign, 16> RVLocs;
1113 // CCState - Info about the registers and stack slots.
1114 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1117 // Analyze outgoing return values.
1118 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1121 // If this is the first return lowered for this function, add
1122 // the regs to the liveout set for the function.
1123 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1124 for (unsigned i = 0; i != RVLocs.size(); ++i)
1125 if (RVLocs[i].isRegLoc())
1126 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
1131 // Copy the result values into the output registers.
1132 for (unsigned i = 0, realRVLocIdx = 0;
1134 ++i, ++realRVLocIdx) {
1135 CCValAssign &VA = RVLocs[i];
1136 assert(VA.isRegLoc() && "Can only return in registers!");
1138 SDValue Arg = Outs[realRVLocIdx].Val;
1140 switch (VA.getLocInfo()) {
1141 default: llvm_unreachable("Unknown loc info!");
1142 case CCValAssign::Full: break;
1143 case CCValAssign::BCvt:
1144 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1148 if (VA.needsCustom()) {
1149 if (VA.getLocVT() == MVT::v2f64) {
1150 // Extract the first half and return it in two registers.
1151 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1152 DAG.getConstant(0, MVT::i32));
1153 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
1154 DAG.getVTList(MVT::i32, MVT::i32), Half);
1156 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1157 Flag = Chain.getValue(1);
1158 VA = RVLocs[++i]; // skip ahead to next loc
1159 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1160 HalfGPRs.getValue(1), Flag);
1161 Flag = Chain.getValue(1);
1162 VA = RVLocs[++i]; // skip ahead to next loc
1164 // Extract the 2nd half and fall through to handle it as an f64 value.
1165 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1166 DAG.getConstant(1, MVT::i32));
1168 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1170 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
1171 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
1172 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
1173 Flag = Chain.getValue(1);
1174 VA = RVLocs[++i]; // skip ahead to next loc
1175 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1178 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1180 // Guarantee that all emitted copies are
1181 // stuck together, avoiding something bad.
1182 Flag = Chain.getValue(1);
1187 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
1189 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
1194 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1195 // their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1196 // one of the above mentioned nodes. It has to be wrapped because otherwise
1197 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1198 // be used to form addressing mode. These wrapped nodes will be selected
1200 static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
1201 EVT PtrVT = Op.getValueType();
1202 // FIXME there is no actual debug info here
1203 DebugLoc dl = Op.getDebugLoc();
1204 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
1206 if (CP->isMachineConstantPoolEntry())
1207 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1208 CP->getAlignment());
1210 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1211 CP->getAlignment());
1212 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
1215 SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) {
1216 MachineFunction &MF = DAG.getMachineFunction();
1217 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1218 unsigned ARMPCLabelIndex = 0;
1219 DebugLoc DL = Op.getDebugLoc();
1220 EVT PtrVT = getPointerTy();
1221 BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1222 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1224 if (RelocM == Reloc::Static) {
1225 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1227 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
1228 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1229 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1230 ARMCP::CPBlockAddress,
1232 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1234 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1235 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
1236 PseudoSourceValue::getConstantPool(), 0);
1237 if (RelocM == Reloc::Static)
1239 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
1240 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
1243 // Lower ISD::GlobalTLSAddress using the "general dynamic" model
1245 ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1246 SelectionDAG &DAG) {
1247 DebugLoc dl = GA->getDebugLoc();
1248 EVT PtrVT = getPointerTy();
1249 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1250 MachineFunction &MF = DAG.getMachineFunction();
1251 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1252 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1253 ARMConstantPoolValue *CPV =
1254 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
1255 ARMCP::CPValue, PCAdj, "tlsgd", true);
1256 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1257 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
1258 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
1259 PseudoSourceValue::getConstantPool(), 0);
1260 SDValue Chain = Argument.getValue(1);
1262 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
1263 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
1265 // call __tls_get_addr.
1268 Entry.Node = Argument;
1269 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
1270 Args.push_back(Entry);
1271 // FIXME: is there useful debug info available here?
1272 std::pair<SDValue, SDValue> CallResult =
1273 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1274 false, false, false, false,
1275 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
1276 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
1277 return CallResult.first;
1280 // Lower ISD::GlobalTLSAddress using the "initial exec" or
1281 // "local exec" model.
1283 ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
1284 SelectionDAG &DAG) {
1285 GlobalValue *GV = GA->getGlobal();
1286 DebugLoc dl = GA->getDebugLoc();
1288 SDValue Chain = DAG.getEntryNode();
1289 EVT PtrVT = getPointerTy();
1290 // Get the Thread Pointer
1291 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1293 if (GV->isDeclaration()) {
1294 MachineFunction &MF = DAG.getMachineFunction();
1295 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1296 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1297 // Initial exec model.
1298 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1299 ARMConstantPoolValue *CPV =
1300 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
1301 ARMCP::CPValue, PCAdj, "gottpoff", true);
1302 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1303 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
1304 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
1305 PseudoSourceValue::getConstantPool(), 0);
1306 Chain = Offset.getValue(1);
1308 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
1309 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
1311 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
1312 PseudoSourceValue::getConstantPool(), 0);
1315 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
1316 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1317 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
1318 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
1319 PseudoSourceValue::getConstantPool(), 0);
1322 // The address of the thread local variable is the add of the thread
1323 // pointer with the offset of the variable.
1324 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
1328 ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
1329 // TODO: implement the "local dynamic" model
1330 assert(Subtarget->isTargetELF() &&
1331 "TLS not implemented for non-ELF targets");
1332 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1333 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1334 // otherwise use the "Local Exec" TLS Model
1335 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1336 return LowerToTLSGeneralDynamicModel(GA, DAG);
1338 return LowerToTLSExecModels(GA, DAG);
1341 SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
1342 SelectionDAG &DAG) {
1343 EVT PtrVT = getPointerTy();
1344 DebugLoc dl = Op.getDebugLoc();
1345 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1346 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1347 if (RelocM == Reloc::PIC_) {
1348 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
1349 ARMConstantPoolValue *CPV =
1350 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
1351 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1352 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1353 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
1355 PseudoSourceValue::getConstantPool(), 0);
1356 SDValue Chain = Result.getValue(1);
1357 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
1358 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
1360 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
1361 PseudoSourceValue::getGOT(), 0);
1364 // If we have T2 ops, we can materialize the address directly via movt/movw
1365 // pair. This is always cheaper.
1366 if (Subtarget->useMovt()) {
1367 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
1368 DAG.getTargetGlobalAddress(GV, PtrVT));
1370 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
1371 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1372 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1373 PseudoSourceValue::getConstantPool(), 0);
1378 SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
1379 SelectionDAG &DAG) {
1380 MachineFunction &MF = DAG.getMachineFunction();
1381 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1382 unsigned ARMPCLabelIndex = 0;
1383 EVT PtrVT = getPointerTy();
1384 DebugLoc dl = Op.getDebugLoc();
1385 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1386 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1388 if (RelocM == Reloc::Static)
1389 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
1391 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1392 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1393 ARMConstantPoolValue *CPV =
1394 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
1395 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1397 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1399 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1400 PseudoSourceValue::getConstantPool(), 0);
1401 SDValue Chain = Result.getValue(1);
1403 if (RelocM == Reloc::PIC_) {
1404 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
1405 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1408 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
1409 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
1410 PseudoSourceValue::getGOT(), 0);
1415 SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
1417 assert(Subtarget->isTargetELF() &&
1418 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
1419 MachineFunction &MF = DAG.getMachineFunction();
1420 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1421 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1422 EVT PtrVT = getPointerTy();
1423 DebugLoc dl = Op.getDebugLoc();
1424 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
1425 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1426 "_GLOBAL_OFFSET_TABLE_",
1427 ARMPCLabelIndex, PCAdj);
1428 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1429 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1430 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1431 PseudoSourceValue::getConstantPool(), 0);
1432 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
1433 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1437 ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
1438 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1439 DebugLoc dl = Op.getDebugLoc();
1441 default: return SDValue(); // Don't custom lower most intrinsics.
1442 case Intrinsic::arm_thread_pointer: {
1443 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1444 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1446 case Intrinsic::eh_sjlj_lsda: {
1447 MachineFunction &MF = DAG.getMachineFunction();
1448 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1449 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1450 EVT PtrVT = getPointerTy();
1451 DebugLoc dl = Op.getDebugLoc();
1452 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1454 unsigned PCAdj = (RelocM != Reloc::PIC_)
1455 ? 0 : (Subtarget->isThumb() ? 4 : 8);
1456 ARMConstantPoolValue *CPV =
1457 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1458 ARMCP::CPLSDA, PCAdj);
1459 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1460 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1462 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1463 PseudoSourceValue::getConstantPool(), 0);
1464 SDValue Chain = Result.getValue(1);
1466 if (RelocM == Reloc::PIC_) {
1467 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
1468 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1472 case Intrinsic::eh_sjlj_setjmp:
1473 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(1));
1477 static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) {
1478 DebugLoc dl = Op.getDebugLoc();
1479 SDValue Op5 = Op.getOperand(5);
1481 unsigned isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue();
1482 if (isDeviceBarrier) {
1483 Res = DAG.getNode(ARMISD::SYNCBARRIER, dl, MVT::Other,
1486 Res = DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other,
1492 static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
1493 unsigned VarArgsFrameIndex) {
1494 // vastart just stores the address of the VarArgsFrameIndex slot into the
1495 // memory location argument.
1496 DebugLoc dl = Op.getDebugLoc();
1497 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1498 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
1499 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
1500 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
1504 ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
1505 SDNode *Node = Op.getNode();
1506 DebugLoc dl = Node->getDebugLoc();
1507 EVT VT = Node->getValueType(0);
1508 SDValue Chain = Op.getOperand(0);
1509 SDValue Size = Op.getOperand(1);
1510 SDValue Align = Op.getOperand(2);
1512 // Chain the dynamic stack allocation so that it doesn't modify the stack
1513 // pointer when other instructions are using the stack.
1514 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1516 unsigned AlignVal = cast<ConstantSDNode>(Align)->getZExtValue();
1517 unsigned StackAlign = getTargetMachine().getFrameInfo()->getStackAlignment();
1518 if (AlignVal > StackAlign)
1519 // Do this now since selection pass cannot introduce new target
1520 // independent node.
1521 Align = DAG.getConstant(-(uint64_t)AlignVal, VT);
1523 // In Thumb1 mode, there isn't a "sub r, sp, r" instruction, we will end up
1524 // using a "add r, sp, r" instead. Negate the size now so we don't have to
1525 // do even more horrible hack later.
1526 MachineFunction &MF = DAG.getMachineFunction();
1527 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1528 if (AFI->isThumb1OnlyFunction()) {
1530 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Size);
1532 uint32_t Val = C->getZExtValue();
1533 if (Val <= 508 && ((Val & 3) == 0))
1537 Size = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, VT), Size);
1540 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
1541 SDValue Ops1[] = { Chain, Size, Align };
1542 SDValue Res = DAG.getNode(ARMISD::DYN_ALLOC, dl, VTList, Ops1, 3);
1543 Chain = Res.getValue(1);
1544 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1545 DAG.getIntPtrConstant(0, true), SDValue());
1546 SDValue Ops2[] = { Res, Chain };
1547 return DAG.getMergeValues(Ops2, 2, dl);
1551 ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1552 SDValue &Root, SelectionDAG &DAG,
1554 MachineFunction &MF = DAG.getMachineFunction();
1555 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1557 TargetRegisterClass *RC;
1558 if (AFI->isThumb1OnlyFunction())
1559 RC = ARM::tGPRRegisterClass;
1561 RC = ARM::GPRRegisterClass;
1563 // Transform the arguments stored in physical registers into virtual ones.
1564 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
1565 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
1568 if (NextVA.isMemLoc()) {
1569 unsigned ArgSize = NextVA.getLocVT().getSizeInBits()/8;
1570 MachineFrameInfo *MFI = MF.getFrameInfo();
1571 int FI = MFI->CreateFixedObject(ArgSize, NextVA.getLocMemOffset(),
1574 // Create load node to retrieve arguments from the stack.
1575 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1576 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
1577 PseudoSourceValue::getFixedStack(FI), 0);
1579 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
1580 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
1583 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
1587 ARMTargetLowering::LowerFormalArguments(SDValue Chain,
1588 CallingConv::ID CallConv, bool isVarArg,
1589 const SmallVectorImpl<ISD::InputArg>
1591 DebugLoc dl, SelectionDAG &DAG,
1592 SmallVectorImpl<SDValue> &InVals) {
1594 MachineFunction &MF = DAG.getMachineFunction();
1595 MachineFrameInfo *MFI = MF.getFrameInfo();
1597 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1599 // Assign locations to all of the incoming arguments.
1600 SmallVector<CCValAssign, 16> ArgLocs;
1601 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1603 CCInfo.AnalyzeFormalArguments(Ins,
1604 CCAssignFnForNode(CallConv, /* Return*/ false,
1607 SmallVector<SDValue, 16> ArgValues;
1609 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1610 CCValAssign &VA = ArgLocs[i];
1612 // Arguments stored in registers.
1613 if (VA.isRegLoc()) {
1614 EVT RegVT = VA.getLocVT();
1617 if (VA.needsCustom()) {
1618 // f64 and vector types are split up into multiple registers or
1619 // combinations of registers and stack slots.
1622 if (VA.getLocVT() == MVT::v2f64) {
1623 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
1625 VA = ArgLocs[++i]; // skip ahead to next loc
1626 SDValue ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
1628 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1629 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
1630 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
1631 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
1632 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
1634 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
1637 TargetRegisterClass *RC;
1639 if (RegVT == MVT::f32)
1640 RC = ARM::SPRRegisterClass;
1641 else if (RegVT == MVT::f64)
1642 RC = ARM::DPRRegisterClass;
1643 else if (RegVT == MVT::v2f64)
1644 RC = ARM::QPRRegisterClass;
1645 else if (RegVT == MVT::i32)
1646 RC = (AFI->isThumb1OnlyFunction() ?
1647 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
1649 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
1651 // Transform the arguments in physical registers into virtual ones.
1652 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
1653 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
1656 // If this is an 8 or 16-bit value, it is really passed promoted
1657 // to 32 bits. Insert an assert[sz]ext to capture this, then
1658 // truncate to the right size.
1659 switch (VA.getLocInfo()) {
1660 default: llvm_unreachable("Unknown loc info!");
1661 case CCValAssign::Full: break;
1662 case CCValAssign::BCvt:
1663 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1665 case CCValAssign::SExt:
1666 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1667 DAG.getValueType(VA.getValVT()));
1668 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1670 case CCValAssign::ZExt:
1671 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1672 DAG.getValueType(VA.getValVT()));
1673 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1677 InVals.push_back(ArgValue);
1679 } else { // VA.isRegLoc()
1682 assert(VA.isMemLoc());
1683 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
1685 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
1686 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
1689 // Create load nodes to retrieve arguments from the stack.
1690 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1691 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
1692 PseudoSourceValue::getFixedStack(FI), 0));
1698 static const unsigned GPRArgRegs[] = {
1699 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1702 unsigned NumGPRs = CCInfo.getFirstUnallocated
1703 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
1705 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1706 unsigned VARegSize = (4 - NumGPRs) * 4;
1707 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
1708 unsigned ArgOffset = CCInfo.getNextStackOffset();
1709 if (VARegSaveSize) {
1710 // If this function is vararg, store any remaining integer argument regs
1711 // to their spots on the stack so that they may be loaded by deferencing
1712 // the result of va_next.
1713 AFI->setVarArgsRegSaveSize(VARegSaveSize);
1714 VarArgsFrameIndex = MFI->CreateFixedObject(VARegSaveSize, ArgOffset +
1715 VARegSaveSize - VARegSize,
1717 SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
1719 SmallVector<SDValue, 4> MemOps;
1720 for (; NumGPRs < 4; ++NumGPRs) {
1721 TargetRegisterClass *RC;
1722 if (AFI->isThumb1OnlyFunction())
1723 RC = ARM::tGPRRegisterClass;
1725 RC = ARM::GPRRegisterClass;
1727 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
1728 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
1729 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
1730 PseudoSourceValue::getFixedStack(VarArgsFrameIndex), 0);
1731 MemOps.push_back(Store);
1732 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
1733 DAG.getConstant(4, getPointerTy()));
1735 if (!MemOps.empty())
1736 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1737 &MemOps[0], MemOps.size());
1739 // This will point to the next argument passed via stack.
1740 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset, true, false);
1746 /// isFloatingPointZero - Return true if this is +0.0.
1747 static bool isFloatingPointZero(SDValue Op) {
1748 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
1749 return CFP->getValueAPF().isPosZero();
1750 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
1751 // Maybe this has already been legalized into the constant pool?
1752 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
1753 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
1754 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1755 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
1756 return CFP->getValueAPF().isPosZero();
1762 /// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1763 /// the given operands.
1765 ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
1766 SDValue &ARMCC, SelectionDAG &DAG, DebugLoc dl) {
1767 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
1768 unsigned C = RHSC->getZExtValue();
1769 if (!isLegalICmpImmediate(C)) {
1770 // Constant does not fit, try adjusting it by one?
1775 if (isLegalICmpImmediate(C-1)) {
1776 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
1777 RHS = DAG.getConstant(C-1, MVT::i32);
1782 if (C > 0 && isLegalICmpImmediate(C-1)) {
1783 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
1784 RHS = DAG.getConstant(C-1, MVT::i32);
1789 if (isLegalICmpImmediate(C+1)) {
1790 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
1791 RHS = DAG.getConstant(C+1, MVT::i32);
1796 if (C < 0xffffffff && isLegalICmpImmediate(C+1)) {
1797 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
1798 RHS = DAG.getConstant(C+1, MVT::i32);
1805 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
1806 ARMISD::NodeType CompareType;
1809 CompareType = ARMISD::CMP;
1814 CompareType = ARMISD::CMPZ;
1817 ARMCC = DAG.getConstant(CondCode, MVT::i32);
1818 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
1821 /// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
1822 static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
1825 if (!isFloatingPointZero(RHS))
1826 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
1828 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
1829 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
1832 SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
1833 EVT VT = Op.getValueType();
1834 SDValue LHS = Op.getOperand(0);
1835 SDValue RHS = Op.getOperand(1);
1836 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1837 SDValue TrueVal = Op.getOperand(2);
1838 SDValue FalseVal = Op.getOperand(3);
1839 DebugLoc dl = Op.getDebugLoc();
1841 if (LHS.getValueType() == MVT::i32) {
1843 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1844 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
1845 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
1848 ARMCC::CondCodes CondCode, CondCode2;
1849 FPCCToARMCC(CC, CondCode, CondCode2);
1851 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1852 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1853 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
1854 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
1856 if (CondCode2 != ARMCC::AL) {
1857 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
1858 // FIXME: Needs another CMP because flag can have but one use.
1859 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
1860 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
1861 Result, TrueVal, ARMCC2, CCR, Cmp2);
1866 SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
1867 SDValue Chain = Op.getOperand(0);
1868 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1869 SDValue LHS = Op.getOperand(2);
1870 SDValue RHS = Op.getOperand(3);
1871 SDValue Dest = Op.getOperand(4);
1872 DebugLoc dl = Op.getDebugLoc();
1874 if (LHS.getValueType() == MVT::i32) {
1876 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1877 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
1878 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
1879 Chain, Dest, ARMCC, CCR,Cmp);
1882 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
1883 ARMCC::CondCodes CondCode, CondCode2;
1884 FPCCToARMCC(CC, CondCode, CondCode2);
1886 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
1887 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1888 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1889 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
1890 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
1891 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
1892 if (CondCode2 != ARMCC::AL) {
1893 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
1894 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
1895 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
1900 SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) {
1901 SDValue Chain = Op.getOperand(0);
1902 SDValue Table = Op.getOperand(1);
1903 SDValue Index = Op.getOperand(2);
1904 DebugLoc dl = Op.getDebugLoc();
1906 EVT PTy = getPointerTy();
1907 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
1908 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
1909 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
1910 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
1911 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
1912 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
1913 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
1914 if (Subtarget->isThumb2()) {
1915 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
1916 // which does another jump to the destination. This also makes it easier
1917 // to translate it to TBB / TBH later.
1918 // FIXME: This might not work if the function is extremely large.
1919 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
1920 Addr, Op.getOperand(2), JTI, UId);
1922 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1923 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
1924 PseudoSourceValue::getJumpTable(), 0);
1925 Chain = Addr.getValue(1);
1926 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
1927 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
1929 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
1930 PseudoSourceValue::getJumpTable(), 0);
1931 Chain = Addr.getValue(1);
1932 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
1936 static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
1937 DebugLoc dl = Op.getDebugLoc();
1939 Op.getOpcode() == ISD::FP_TO_SINT ? ARMISD::FTOSI : ARMISD::FTOUI;
1940 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
1941 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
1944 static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
1945 EVT VT = Op.getValueType();
1946 DebugLoc dl = Op.getDebugLoc();
1948 Op.getOpcode() == ISD::SINT_TO_FP ? ARMISD::SITOF : ARMISD::UITOF;
1950 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
1951 return DAG.getNode(Opc, dl, VT, Op);
1954 static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
1955 // Implement fcopysign with a fabs and a conditional fneg.
1956 SDValue Tmp0 = Op.getOperand(0);
1957 SDValue Tmp1 = Op.getOperand(1);
1958 DebugLoc dl = Op.getDebugLoc();
1959 EVT VT = Op.getValueType();
1960 EVT SrcVT = Tmp1.getValueType();
1961 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
1962 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
1963 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
1964 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1965 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
1968 SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
1969 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1970 MFI->setFrameAddressIsTaken(true);
1971 EVT VT = Op.getValueType();
1972 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
1973 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1974 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
1975 ? ARM::R7 : ARM::R11;
1976 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
1978 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
1983 ARMTargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
1985 SDValue Dst, SDValue Src,
1986 SDValue Size, unsigned Align,
1988 const Value *DstSV, uint64_t DstSVOff,
1989 const Value *SrcSV, uint64_t SrcSVOff){
1990 // Do repeated 4-byte loads and stores. To be improved.
1991 // This requires 4-byte alignment.
1992 if ((Align & 3) != 0)
1994 // This requires the copy size to be a constant, preferrably
1995 // within a subtarget-specific limit.
1996 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
1999 uint64_t SizeVal = ConstantSize->getZExtValue();
2000 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
2003 unsigned BytesLeft = SizeVal & 3;
2004 unsigned NumMemOps = SizeVal >> 2;
2005 unsigned EmittedNumMemOps = 0;
2007 unsigned VTSize = 4;
2009 const unsigned MAX_LOADS_IN_LDM = 6;
2010 SDValue TFOps[MAX_LOADS_IN_LDM];
2011 SDValue Loads[MAX_LOADS_IN_LDM];
2012 uint64_t SrcOff = 0, DstOff = 0;
2014 // Emit up to MAX_LOADS_IN_LDM loads, then a TokenFactor barrier, then the
2015 // same number of stores. The loads and stores will get combined into
2016 // ldm/stm later on.
2017 while (EmittedNumMemOps < NumMemOps) {
2019 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
2020 Loads[i] = DAG.getLoad(VT, dl, Chain,
2021 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
2022 DAG.getConstant(SrcOff, MVT::i32)),
2023 SrcSV, SrcSVOff + SrcOff);
2024 TFOps[i] = Loads[i].getValue(1);
2027 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
2030 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
2031 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
2032 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
2033 DAG.getConstant(DstOff, MVT::i32)),
2034 DstSV, DstSVOff + DstOff);
2037 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
2039 EmittedNumMemOps += i;
2045 // Issue loads / stores for the trailing (1 - 3) bytes.
2046 unsigned BytesLeftSave = BytesLeft;
2049 if (BytesLeft >= 2) {
2057 Loads[i] = DAG.getLoad(VT, dl, Chain,
2058 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
2059 DAG.getConstant(SrcOff, MVT::i32)),
2060 SrcSV, SrcSVOff + SrcOff);
2061 TFOps[i] = Loads[i].getValue(1);
2064 BytesLeft -= VTSize;
2066 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
2069 BytesLeft = BytesLeftSave;
2071 if (BytesLeft >= 2) {
2079 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
2080 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
2081 DAG.getConstant(DstOff, MVT::i32)),
2082 DstSV, DstSVOff + DstOff);
2085 BytesLeft -= VTSize;
2087 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
2090 static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
2091 SDValue Op = N->getOperand(0);
2092 DebugLoc dl = N->getDebugLoc();
2093 if (N->getValueType(0) == MVT::f64) {
2094 // Turn i64->f64 into VMOVDRR.
2095 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2096 DAG.getConstant(0, MVT::i32));
2097 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2098 DAG.getConstant(1, MVT::i32));
2099 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
2102 // Turn f64->i64 into VMOVRRD.
2103 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
2104 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
2106 // Merge the pieces into a single i64 value.
2107 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
2110 /// getZeroVector - Returns a vector of specified type with all zero elements.
2112 static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
2113 assert(VT.isVector() && "Expected a vector type");
2115 // Zero vectors are used to represent vector negation and in those cases
2116 // will be implemented with the NEON VNEG instruction. However, VNEG does
2117 // not support i64 elements, so sometimes the zero vectors will need to be
2118 // explicitly constructed. For those cases, and potentially other uses in
2119 // the future, always build zero vectors as <16 x i8> or <8 x i8> bitcasted
2120 // to their dest type. This ensures they get CSE'd.
2122 SDValue Cst = DAG.getTargetConstant(0, MVT::i8);
2123 SmallVector<SDValue, 8> Ops;
2126 if (VT.getSizeInBits() == 64) {
2127 Ops.assign(8, Cst); TVT = MVT::v8i8;
2129 Ops.assign(16, Cst); TVT = MVT::v16i8;
2131 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
2133 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2136 /// getOnesVector - Returns a vector of specified type with all bits set.
2138 static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
2139 assert(VT.isVector() && "Expected a vector type");
2141 // Always build ones vectors as <16 x i8> or <8 x i8> bitcasted to their
2142 // dest type. This ensures they get CSE'd.
2144 SDValue Cst = DAG.getTargetConstant(0xFF, MVT::i8);
2145 SmallVector<SDValue, 8> Ops;
2148 if (VT.getSizeInBits() == 64) {
2149 Ops.assign(8, Cst); TVT = MVT::v8i8;
2151 Ops.assign(16, Cst); TVT = MVT::v16i8;
2153 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
2155 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2158 /// LowerShiftRightParts - Lower SRA_PARTS, which returns two
2159 /// i32 values and take a 2 x i32 value to shift plus a shift amount.
2160 SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op, SelectionDAG &DAG) {
2161 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2162 EVT VT = Op.getValueType();
2163 unsigned VTBits = VT.getSizeInBits();
2164 DebugLoc dl = Op.getDebugLoc();
2165 SDValue ShOpLo = Op.getOperand(0);
2166 SDValue ShOpHi = Op.getOperand(1);
2167 SDValue ShAmt = Op.getOperand(2);
2169 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
2171 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
2173 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2174 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2175 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
2176 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2177 DAG.getConstant(VTBits, MVT::i32));
2178 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
2179 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2180 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
2182 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2183 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
2185 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
2186 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC,
2189 SDValue Ops[2] = { Lo, Hi };
2190 return DAG.getMergeValues(Ops, 2, dl);
2193 /// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
2194 /// i32 values and take a 2 x i32 value to shift plus a shift amount.
2195 SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op, SelectionDAG &DAG) {
2196 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2197 EVT VT = Op.getValueType();
2198 unsigned VTBits = VT.getSizeInBits();
2199 DebugLoc dl = Op.getDebugLoc();
2200 SDValue ShOpLo = Op.getOperand(0);
2201 SDValue ShOpHi = Op.getOperand(1);
2202 SDValue ShAmt = Op.getOperand(2);
2205 assert(Op.getOpcode() == ISD::SHL_PARTS);
2206 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2207 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2208 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
2209 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2210 DAG.getConstant(VTBits, MVT::i32));
2211 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
2212 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
2214 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2215 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2216 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
2218 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
2219 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMCC,
2222 SDValue Ops[2] = { Lo, Hi };
2223 return DAG.getMergeValues(Ops, 2, dl);
2226 static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2227 const ARMSubtarget *ST) {
2228 EVT VT = N->getValueType(0);
2229 DebugLoc dl = N->getDebugLoc();
2231 // Lower vector shifts on NEON to use VSHL.
2232 if (VT.isVector()) {
2233 assert(ST->hasNEON() && "unexpected vector shift");
2235 // Left shifts translate directly to the vshiftu intrinsic.
2236 if (N->getOpcode() == ISD::SHL)
2237 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
2238 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
2239 N->getOperand(0), N->getOperand(1));
2241 assert((N->getOpcode() == ISD::SRA ||
2242 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2244 // NEON uses the same intrinsics for both left and right shifts. For
2245 // right shifts, the shift amounts are negative, so negate the vector of
2247 EVT ShiftVT = N->getOperand(1).getValueType();
2248 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2249 getZeroVector(ShiftVT, DAG, dl),
2251 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2252 Intrinsic::arm_neon_vshifts :
2253 Intrinsic::arm_neon_vshiftu);
2254 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
2255 DAG.getConstant(vshiftInt, MVT::i32),
2256 N->getOperand(0), NegatedCount);
2259 // We can get here for a node like i32 = ISD::SHL i32, i64
2263 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
2264 "Unknown shift to lower!");
2266 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2267 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
2268 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
2271 // If we are in thumb mode, we don't have RRX.
2272 if (ST->isThumb1Only()) return SDValue();
2274 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
2275 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2276 DAG.getConstant(0, MVT::i32));
2277 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2278 DAG.getConstant(1, MVT::i32));
2280 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2281 // captures the result into a carry flag.
2282 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
2283 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
2285 // The low part is an ARMISD::RRX operand, which shifts the carry in.
2286 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
2288 // Merge the pieces into a single i64 value.
2289 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
2292 static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2293 SDValue TmpOp0, TmpOp1;
2294 bool Invert = false;
2298 SDValue Op0 = Op.getOperand(0);
2299 SDValue Op1 = Op.getOperand(1);
2300 SDValue CC = Op.getOperand(2);
2301 EVT VT = Op.getValueType();
2302 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2303 DebugLoc dl = Op.getDebugLoc();
2305 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2306 switch (SetCCOpcode) {
2307 default: llvm_unreachable("Illegal FP comparison"); break;
2309 case ISD::SETNE: Invert = true; // Fallthrough
2311 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2313 case ISD::SETLT: Swap = true; // Fallthrough
2315 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2317 case ISD::SETLE: Swap = true; // Fallthrough
2319 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2320 case ISD::SETUGE: Swap = true; // Fallthrough
2321 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2322 case ISD::SETUGT: Swap = true; // Fallthrough
2323 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2324 case ISD::SETUEQ: Invert = true; // Fallthrough
2326 // Expand this to (OLT | OGT).
2330 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2331 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2333 case ISD::SETUO: Invert = true; // Fallthrough
2335 // Expand this to (OLT | OGE).
2339 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2340 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2344 // Integer comparisons.
2345 switch (SetCCOpcode) {
2346 default: llvm_unreachable("Illegal integer comparison"); break;
2347 case ISD::SETNE: Invert = true;
2348 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2349 case ISD::SETLT: Swap = true;
2350 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2351 case ISD::SETLE: Swap = true;
2352 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2353 case ISD::SETULT: Swap = true;
2354 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2355 case ISD::SETULE: Swap = true;
2356 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2359 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
2360 if (Opc == ARMISD::VCEQ) {
2363 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2365 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2368 // Ignore bitconvert.
2369 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2370 AndOp = AndOp.getOperand(0);
2372 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2374 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2375 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2382 std::swap(Op0, Op1);
2384 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2387 Result = DAG.getNOT(dl, Result, VT);
2392 /// isVMOVSplat - Check if the specified splat value corresponds to an immediate
2393 /// VMOV instruction, and if so, return the constant being splatted.
2394 static SDValue isVMOVSplat(uint64_t SplatBits, uint64_t SplatUndef,
2395 unsigned SplatBitSize, SelectionDAG &DAG) {
2396 switch (SplatBitSize) {
2398 // Any 1-byte value is OK.
2399 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
2400 return DAG.getTargetConstant(SplatBits, MVT::i8);
2403 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
2404 if ((SplatBits & ~0xff) == 0 ||
2405 (SplatBits & ~0xff00) == 0)
2406 return DAG.getTargetConstant(SplatBits, MVT::i16);
2410 // NEON's 32-bit VMOV supports splat values where:
2411 // * only one byte is nonzero, or
2412 // * the least significant byte is 0xff and the second byte is nonzero, or
2413 // * the least significant 2 bytes are 0xff and the third is nonzero.
2414 if ((SplatBits & ~0xff) == 0 ||
2415 (SplatBits & ~0xff00) == 0 ||
2416 (SplatBits & ~0xff0000) == 0 ||
2417 (SplatBits & ~0xff000000) == 0)
2418 return DAG.getTargetConstant(SplatBits, MVT::i32);
2420 if ((SplatBits & ~0xffff) == 0 &&
2421 ((SplatBits | SplatUndef) & 0xff) == 0xff)
2422 return DAG.getTargetConstant(SplatBits | 0xff, MVT::i32);
2424 if ((SplatBits & ~0xffffff) == 0 &&
2425 ((SplatBits | SplatUndef) & 0xffff) == 0xffff)
2426 return DAG.getTargetConstant(SplatBits | 0xffff, MVT::i32);
2428 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2429 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2430 // VMOV.I32. A (very) minor optimization would be to replicate the value
2431 // and fall through here to test for a valid 64-bit splat. But, then the
2432 // caller would also need to check and handle the change in size.
2436 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
2437 uint64_t BitMask = 0xff;
2439 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
2440 if (((SplatBits | SplatUndef) & BitMask) == BitMask)
2442 else if ((SplatBits & BitMask) != 0)
2446 return DAG.getTargetConstant(Val, MVT::i64);
2450 llvm_unreachable("unexpected size for isVMOVSplat");
2457 /// getVMOVImm - If this is a build_vector of constants which can be
2458 /// formed by using a VMOV instruction of the specified element size,
2459 /// return the constant being splatted. The ByteSize field indicates the
2460 /// number of bytes of each element [1248].
2461 SDValue ARM::getVMOVImm(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
2462 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2463 APInt SplatBits, SplatUndef;
2464 unsigned SplatBitSize;
2466 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2467 HasAnyUndefs, ByteSize * 8))
2470 if (SplatBitSize > ByteSize * 8)
2473 return isVMOVSplat(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
2477 static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
2478 bool &ReverseVEXT, unsigned &Imm) {
2479 unsigned NumElts = VT.getVectorNumElements();
2480 ReverseVEXT = false;
2483 // If this is a VEXT shuffle, the immediate value is the index of the first
2484 // element. The other shuffle indices must be the successive elements after
2486 unsigned ExpectedElt = Imm;
2487 for (unsigned i = 1; i < NumElts; ++i) {
2488 // Increment the expected index. If it wraps around, it may still be
2489 // a VEXT but the source vectors must be swapped.
2491 if (ExpectedElt == NumElts * 2) {
2496 if (ExpectedElt != static_cast<unsigned>(M[i]))
2500 // Adjust the index value if the source operands will be swapped.
2507 /// isVREVMask - Check if a vector shuffle corresponds to a VREV
2508 /// instruction with the specified blocksize. (The order of the elements
2509 /// within each block of the vector is reversed.)
2510 static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
2511 unsigned BlockSize) {
2512 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
2513 "Only possible block sizes for VREV are: 16, 32, 64");
2515 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2519 unsigned NumElts = VT.getVectorNumElements();
2520 unsigned BlockElts = M[0] + 1;
2522 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
2525 for (unsigned i = 0; i < NumElts; ++i) {
2526 if ((unsigned) M[i] !=
2527 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
2534 static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
2535 unsigned &WhichResult) {
2536 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2540 unsigned NumElts = VT.getVectorNumElements();
2541 WhichResult = (M[0] == 0 ? 0 : 1);
2542 for (unsigned i = 0; i < NumElts; i += 2) {
2543 if ((unsigned) M[i] != i + WhichResult ||
2544 (unsigned) M[i+1] != i + NumElts + WhichResult)
2550 /// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
2551 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2552 /// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
2553 static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2554 unsigned &WhichResult) {
2555 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2559 unsigned NumElts = VT.getVectorNumElements();
2560 WhichResult = (M[0] == 0 ? 0 : 1);
2561 for (unsigned i = 0; i < NumElts; i += 2) {
2562 if ((unsigned) M[i] != i + WhichResult ||
2563 (unsigned) M[i+1] != i + WhichResult)
2569 static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
2570 unsigned &WhichResult) {
2571 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2575 unsigned NumElts = VT.getVectorNumElements();
2576 WhichResult = (M[0] == 0 ? 0 : 1);
2577 for (unsigned i = 0; i != NumElts; ++i) {
2578 if ((unsigned) M[i] != 2 * i + WhichResult)
2582 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2583 if (VT.is64BitVector() && EltSz == 32)
2589 /// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
2590 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2591 /// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
2592 static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2593 unsigned &WhichResult) {
2594 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2598 unsigned Half = VT.getVectorNumElements() / 2;
2599 WhichResult = (M[0] == 0 ? 0 : 1);
2600 for (unsigned j = 0; j != 2; ++j) {
2601 unsigned Idx = WhichResult;
2602 for (unsigned i = 0; i != Half; ++i) {
2603 if ((unsigned) M[i + j * Half] != Idx)
2609 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2610 if (VT.is64BitVector() && EltSz == 32)
2616 static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
2617 unsigned &WhichResult) {
2618 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2622 unsigned NumElts = VT.getVectorNumElements();
2623 WhichResult = (M[0] == 0 ? 0 : 1);
2624 unsigned Idx = WhichResult * NumElts / 2;
2625 for (unsigned i = 0; i != NumElts; i += 2) {
2626 if ((unsigned) M[i] != Idx ||
2627 (unsigned) M[i+1] != Idx + NumElts)
2632 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2633 if (VT.is64BitVector() && EltSz == 32)
2639 /// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
2640 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2641 /// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
2642 static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2643 unsigned &WhichResult) {
2644 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2648 unsigned NumElts = VT.getVectorNumElements();
2649 WhichResult = (M[0] == 0 ? 0 : 1);
2650 unsigned Idx = WhichResult * NumElts / 2;
2651 for (unsigned i = 0; i != NumElts; i += 2) {
2652 if ((unsigned) M[i] != Idx ||
2653 (unsigned) M[i+1] != Idx)
2658 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2659 if (VT.is64BitVector() && EltSz == 32)
2666 static SDValue BuildSplat(SDValue Val, EVT VT, SelectionDAG &DAG, DebugLoc dl) {
2667 // Canonicalize all-zeros and all-ones vectors.
2668 ConstantSDNode *ConstVal = cast<ConstantSDNode>(Val.getNode());
2669 if (ConstVal->isNullValue())
2670 return getZeroVector(VT, DAG, dl);
2671 if (ConstVal->isAllOnesValue())
2672 return getOnesVector(VT, DAG, dl);
2675 if (VT.is64BitVector()) {
2676 switch (Val.getValueType().getSizeInBits()) {
2677 case 8: CanonicalVT = MVT::v8i8; break;
2678 case 16: CanonicalVT = MVT::v4i16; break;
2679 case 32: CanonicalVT = MVT::v2i32; break;
2680 case 64: CanonicalVT = MVT::v1i64; break;
2681 default: llvm_unreachable("unexpected splat element type"); break;
2684 assert(VT.is128BitVector() && "unknown splat vector size");
2685 switch (Val.getValueType().getSizeInBits()) {
2686 case 8: CanonicalVT = MVT::v16i8; break;
2687 case 16: CanonicalVT = MVT::v8i16; break;
2688 case 32: CanonicalVT = MVT::v4i32; break;
2689 case 64: CanonicalVT = MVT::v2i64; break;
2690 default: llvm_unreachable("unexpected splat element type"); break;
2694 // Build a canonical splat for this value.
2695 SmallVector<SDValue, 8> Ops;
2696 Ops.assign(CanonicalVT.getVectorNumElements(), Val);
2697 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
2699 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
2702 // If this is a case we can't handle, return null and let the default
2703 // expansion code take care of it.
2704 static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
2705 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
2706 DebugLoc dl = Op.getDebugLoc();
2707 EVT VT = Op.getValueType();
2709 APInt SplatBits, SplatUndef;
2710 unsigned SplatBitSize;
2712 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
2713 if (SplatBitSize <= 64) {
2714 SDValue Val = isVMOVSplat(SplatBits.getZExtValue(),
2715 SplatUndef.getZExtValue(), SplatBitSize, DAG);
2717 return BuildSplat(Val, VT, DAG, dl);
2721 // If there are only 2 elements in a 128-bit vector, insert them into an
2722 // undef vector. This handles the common case for 128-bit vector argument
2723 // passing, where the insertions should be translated to subreg accesses
2724 // with no real instructions.
2725 if (VT.is128BitVector() && Op.getNumOperands() == 2) {
2726 SDValue Val = DAG.getUNDEF(VT);
2727 SDValue Op0 = Op.getOperand(0);
2728 SDValue Op1 = Op.getOperand(1);
2729 if (Op0.getOpcode() != ISD::UNDEF)
2730 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op0,
2731 DAG.getIntPtrConstant(0));
2732 if (Op1.getOpcode() != ISD::UNDEF)
2733 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op1,
2734 DAG.getIntPtrConstant(1));
2741 /// isShuffleMaskLegal - Targets can use this to indicate that they only
2742 /// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2743 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2744 /// are assumed to be legal.
2746 ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
2748 if (VT.getVectorNumElements() == 4 &&
2749 (VT.is128BitVector() || VT.is64BitVector())) {
2750 unsigned PFIndexes[4];
2751 for (unsigned i = 0; i != 4; ++i) {
2755 PFIndexes[i] = M[i];
2758 // Compute the index in the perfect shuffle table.
2759 unsigned PFTableIndex =
2760 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2761 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2762 unsigned Cost = (PFEntry >> 30);
2769 unsigned Imm, WhichResult;
2771 return (ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
2772 isVREVMask(M, VT, 64) ||
2773 isVREVMask(M, VT, 32) ||
2774 isVREVMask(M, VT, 16) ||
2775 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
2776 isVTRNMask(M, VT, WhichResult) ||
2777 isVUZPMask(M, VT, WhichResult) ||
2778 isVZIPMask(M, VT, WhichResult) ||
2779 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
2780 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
2781 isVZIP_v_undef_Mask(M, VT, WhichResult));
2784 /// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
2785 /// the specified operations to build the shuffle.
2786 static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
2787 SDValue RHS, SelectionDAG &DAG,
2789 unsigned OpNum = (PFEntry >> 26) & 0x0F;
2790 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
2791 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
2794 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
2803 OP_VUZPL, // VUZP, left result
2804 OP_VUZPR, // VUZP, right result
2805 OP_VZIPL, // VZIP, left result
2806 OP_VZIPR, // VZIP, right result
2807 OP_VTRNL, // VTRN, left result
2808 OP_VTRNR // VTRN, right result
2811 if (OpNum == OP_COPY) {
2812 if (LHSID == (1*9+2)*9+3) return LHS;
2813 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
2817 SDValue OpLHS, OpRHS;
2818 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
2819 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
2820 EVT VT = OpLHS.getValueType();
2823 default: llvm_unreachable("Unknown shuffle opcode!");
2825 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
2830 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
2831 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
2835 return DAG.getNode(ARMISD::VEXT, dl, VT,
2837 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
2840 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
2841 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
2844 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
2845 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
2848 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2849 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
2853 static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
2854 SDValue V1 = Op.getOperand(0);
2855 SDValue V2 = Op.getOperand(1);
2856 DebugLoc dl = Op.getDebugLoc();
2857 EVT VT = Op.getValueType();
2858 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
2859 SmallVector<int, 8> ShuffleMask;
2861 // Convert shuffles that are directly supported on NEON to target-specific
2862 // DAG nodes, instead of keeping them as shuffles and matching them again
2863 // during code selection. This is more efficient and avoids the possibility
2864 // of inconsistencies between legalization and selection.
2865 // FIXME: floating-point vectors should be canonicalized to integer vectors
2866 // of the same time so that they get CSEd properly.
2867 SVN->getMask(ShuffleMask);
2869 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
2870 int Lane = SVN->getSplatIndex();
2871 // If this is undef splat, generate it via "just" vdup, if possible.
2872 if (Lane == -1) Lane = 0;
2874 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
2875 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
2877 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
2878 DAG.getConstant(Lane, MVT::i32));
2883 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
2886 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
2887 DAG.getConstant(Imm, MVT::i32));
2890 if (isVREVMask(ShuffleMask, VT, 64))
2891 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
2892 if (isVREVMask(ShuffleMask, VT, 32))
2893 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
2894 if (isVREVMask(ShuffleMask, VT, 16))
2895 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
2897 // Check for Neon shuffles that modify both input vectors in place.
2898 // If both results are used, i.e., if there are two shuffles with the same
2899 // source operands and with masks corresponding to both results of one of
2900 // these operations, DAG memoization will ensure that a single node is
2901 // used for both shuffles.
2902 unsigned WhichResult;
2903 if (isVTRNMask(ShuffleMask, VT, WhichResult))
2904 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2905 V1, V2).getValue(WhichResult);
2906 if (isVUZPMask(ShuffleMask, VT, WhichResult))
2907 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
2908 V1, V2).getValue(WhichResult);
2909 if (isVZIPMask(ShuffleMask, VT, WhichResult))
2910 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
2911 V1, V2).getValue(WhichResult);
2913 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
2914 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2915 V1, V1).getValue(WhichResult);
2916 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
2917 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
2918 V1, V1).getValue(WhichResult);
2919 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
2920 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
2921 V1, V1).getValue(WhichResult);
2923 // If the shuffle is not directly supported and it has 4 elements, use
2924 // the PerfectShuffle-generated table to synthesize it from other shuffles.
2925 if (VT.getVectorNumElements() == 4 &&
2926 (VT.is128BitVector() || VT.is64BitVector())) {
2927 unsigned PFIndexes[4];
2928 for (unsigned i = 0; i != 4; ++i) {
2929 if (ShuffleMask[i] < 0)
2932 PFIndexes[i] = ShuffleMask[i];
2935 // Compute the index in the perfect shuffle table.
2936 unsigned PFTableIndex =
2937 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2939 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2940 unsigned Cost = (PFEntry >> 30);
2943 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
2949 static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
2950 EVT VT = Op.getValueType();
2951 DebugLoc dl = Op.getDebugLoc();
2952 SDValue Vec = Op.getOperand(0);
2953 SDValue Lane = Op.getOperand(1);
2954 assert(VT == MVT::i32 &&
2955 Vec.getValueType().getVectorElementType().getSizeInBits() < 32 &&
2956 "unexpected type for custom-lowering vector extract");
2957 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
2960 static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
2961 // The only time a CONCAT_VECTORS operation can have legal types is when
2962 // two 64-bit vectors are concatenated to a 128-bit vector.
2963 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
2964 "unexpected CONCAT_VECTORS");
2965 DebugLoc dl = Op.getDebugLoc();
2966 SDValue Val = DAG.getUNDEF(MVT::v2f64);
2967 SDValue Op0 = Op.getOperand(0);
2968 SDValue Op1 = Op.getOperand(1);
2969 if (Op0.getOpcode() != ISD::UNDEF)
2970 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2971 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
2972 DAG.getIntPtrConstant(0));
2973 if (Op1.getOpcode() != ISD::UNDEF)
2974 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
2975 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
2976 DAG.getIntPtrConstant(1));
2977 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
2980 SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
2981 switch (Op.getOpcode()) {
2982 default: llvm_unreachable("Don't know how to custom lower this!");
2983 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
2984 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
2985 case ISD::GlobalAddress:
2986 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
2987 LowerGlobalAddressELF(Op, DAG);
2988 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
2989 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
2990 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
2991 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
2992 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
2993 case ISD::VASTART: return LowerVASTART(Op, DAG, VarArgsFrameIndex);
2994 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG);
2995 case ISD::SINT_TO_FP:
2996 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
2997 case ISD::FP_TO_SINT:
2998 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
2999 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
3000 case ISD::RETURNADDR: break;
3001 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
3002 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
3003 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
3004 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
3007 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
3008 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
3009 case ISD::SRL_PARTS:
3010 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
3011 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
3012 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3013 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
3014 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
3015 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
3020 /// ReplaceNodeResults - Replace the results of node with an illegal result
3021 /// type with new values built out of custom code.
3022 void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
3023 SmallVectorImpl<SDValue>&Results,
3024 SelectionDAG &DAG) {
3025 switch (N->getOpcode()) {
3027 llvm_unreachable("Don't know how to custom expand this!");
3029 case ISD::BIT_CONVERT:
3030 Results.push_back(ExpandBIT_CONVERT(N, DAG));
3034 SDValue Res = LowerShift(N, DAG, Subtarget);
3036 Results.push_back(Res);
3042 //===----------------------------------------------------------------------===//
3043 // ARM Scheduler Hooks
3044 //===----------------------------------------------------------------------===//
3047 ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
3048 MachineBasicBlock *BB,
3049 unsigned Size) const {
3050 unsigned dest = MI->getOperand(0).getReg();
3051 unsigned ptr = MI->getOperand(1).getReg();
3052 unsigned oldval = MI->getOperand(2).getReg();
3053 unsigned newval = MI->getOperand(3).getReg();
3054 unsigned scratch = BB->getParent()->getRegInfo()
3055 .createVirtualRegister(ARM::GPRRegisterClass);
3056 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3057 DebugLoc dl = MI->getDebugLoc();
3059 unsigned ldrOpc, strOpc;
3061 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
3062 case 1: ldrOpc = ARM::LDREXB; strOpc = ARM::STREXB; break;
3063 case 2: ldrOpc = ARM::LDREXH; strOpc = ARM::STREXH; break;
3064 case 4: ldrOpc = ARM::LDREX; strOpc = ARM::STREX; break;
3067 MachineFunction *MF = BB->getParent();
3068 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3069 MachineFunction::iterator It = BB;
3070 ++It; // insert the new blocks after the current block
3072 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3073 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3074 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3075 MF->insert(It, loop1MBB);
3076 MF->insert(It, loop2MBB);
3077 MF->insert(It, exitMBB);
3078 exitMBB->transferSuccessors(BB);
3082 // fallthrough --> loop1MBB
3083 BB->addSuccessor(loop1MBB);
3086 // ldrex dest, [ptr]
3090 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
3091 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::CMPrr))
3092 .addReg(dest).addReg(oldval));
3093 BuildMI(BB, dl, TII->get(ARM::Bcc)).addMBB(exitMBB).addImm(ARMCC::NE)
3095 BB->addSuccessor(loop2MBB);
3096 BB->addSuccessor(exitMBB);
3099 // strex scratch, newval, [ptr]
3103 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
3105 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::CMPri))
3106 .addReg(scratch).addImm(0));
3107 BuildMI(BB, dl, TII->get(ARM::Bcc)).addMBB(loop1MBB).addImm(ARMCC::NE)
3109 BB->addSuccessor(loop1MBB);
3110 BB->addSuccessor(exitMBB);
3119 ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
3120 unsigned Size, unsigned BinOpcode) const {
3122 raw_string_ostream Msg(msg);
3123 Msg << "Cannot yet emit: ";
3125 llvm_report_error(Msg.str());
3129 ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
3130 MachineBasicBlock *BB,
3131 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
3132 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3133 DebugLoc dl = MI->getDebugLoc();
3134 switch (MI->getOpcode()) {
3137 llvm_unreachable("Unexpected instr type to insert");
3139 case ARM::ATOMIC_LOAD_ADD_I8: return EmitAtomicBinary(MI, BB, 1, ARM::ADDrr);
3140 case ARM::ATOMIC_LOAD_ADD_I16: return EmitAtomicBinary(MI, BB, 2, ARM::ADDrr);
3141 case ARM::ATOMIC_LOAD_ADD_I32: return EmitAtomicBinary(MI, BB, 4, ARM::ADDrr);
3143 case ARM::ATOMIC_LOAD_AND_I8: return EmitAtomicBinary(MI, BB, 1, ARM::ANDrr);
3144 case ARM::ATOMIC_LOAD_AND_I16: return EmitAtomicBinary(MI, BB, 2, ARM::ANDrr);
3145 case ARM::ATOMIC_LOAD_AND_I32: return EmitAtomicBinary(MI, BB, 4, ARM::ANDrr);
3147 case ARM::ATOMIC_LOAD_OR_I8: return EmitAtomicBinary(MI, BB, 1, ARM::ORRrr);
3148 case ARM::ATOMIC_LOAD_OR_I16: return EmitAtomicBinary(MI, BB, 2, ARM::ORRrr);
3149 case ARM::ATOMIC_LOAD_OR_I32: return EmitAtomicBinary(MI, BB, 4, ARM::ORRrr);
3151 case ARM::ATOMIC_LOAD_XOR_I8: return EmitAtomicBinary(MI, BB, 1, ARM::EORrr);
3152 case ARM::ATOMIC_LOAD_XOR_I16: return EmitAtomicBinary(MI, BB, 2, ARM::EORrr);
3153 case ARM::ATOMIC_LOAD_XOR_I32: return EmitAtomicBinary(MI, BB, 4, ARM::EORrr);
3155 case ARM::ATOMIC_LOAD_NAND_I8: return EmitAtomicBinary(MI, BB, 1, ARM::BICrr);
3156 case ARM::ATOMIC_LOAD_NAND_I16:return EmitAtomicBinary(MI, BB, 2, ARM::BICrr);
3157 case ARM::ATOMIC_LOAD_NAND_I32:return EmitAtomicBinary(MI, BB, 4, ARM::BICrr);
3159 case ARM::ATOMIC_LOAD_SUB_I8: return EmitAtomicBinary(MI, BB, 1, ARM::SUBrr);
3160 case ARM::ATOMIC_LOAD_SUB_I16: return EmitAtomicBinary(MI, BB, 2, ARM::SUBrr);
3161 case ARM::ATOMIC_LOAD_SUB_I32: return EmitAtomicBinary(MI, BB, 4, ARM::SUBrr);
3163 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
3164 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
3165 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
3167 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
3168 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
3169 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
3171 case ARM::tMOVCCr_pseudo: {
3172 // To "insert" a SELECT_CC instruction, we actually have to insert the
3173 // diamond control-flow pattern. The incoming instruction knows the
3174 // destination vreg to set, the condition code register to branch on, the
3175 // true/false values to select between, and a branch opcode to use.
3176 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3177 MachineFunction::iterator It = BB;
3183 // cmpTY ccX, r1, r2
3185 // fallthrough --> copy0MBB
3186 MachineBasicBlock *thisMBB = BB;
3187 MachineFunction *F = BB->getParent();
3188 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3189 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
3190 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
3191 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
3192 F->insert(It, copy0MBB);
3193 F->insert(It, sinkMBB);
3194 // Update machine-CFG edges by first adding all successors of the current
3195 // block to the new block which will contain the Phi node for the select.
3196 // Also inform sdisel of the edge changes.
3197 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
3198 E = BB->succ_end(); I != E; ++I) {
3199 EM->insert(std::make_pair(*I, sinkMBB));
3200 sinkMBB->addSuccessor(*I);
3202 // Next, remove all successors of the current block, and add the true
3203 // and fallthrough blocks as its successors.
3204 while (!BB->succ_empty())
3205 BB->removeSuccessor(BB->succ_begin());
3206 BB->addSuccessor(copy0MBB);
3207 BB->addSuccessor(sinkMBB);
3210 // %FalseValue = ...
3211 // # fallthrough to sinkMBB
3214 // Update machine-CFG edges
3215 BB->addSuccessor(sinkMBB);
3218 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
3221 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
3222 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
3223 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
3225 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
3232 case ARM::t2SUBrSPi_:
3233 case ARM::t2SUBrSPi12_:
3234 case ARM::t2SUBrSPs_: {
3235 MachineFunction *MF = BB->getParent();
3236 unsigned DstReg = MI->getOperand(0).getReg();
3237 unsigned SrcReg = MI->getOperand(1).getReg();
3238 bool DstIsDead = MI->getOperand(0).isDead();
3239 bool SrcIsKill = MI->getOperand(1).isKill();
3241 if (SrcReg != ARM::SP) {
3242 // Copy the source to SP from virtual register.
3243 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
3244 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3245 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
3246 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
3247 .addReg(SrcReg, getKillRegState(SrcIsKill));
3251 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
3252 switch (MI->getOpcode()) {
3254 llvm_unreachable("Unexpected pseudo instruction!");
3260 OpOpc = ARM::tADDspr;
3263 OpOpc = ARM::tSUBspi;
3265 case ARM::t2SUBrSPi_:
3266 OpOpc = ARM::t2SUBrSPi;
3267 NeedPred = true; NeedCC = true;
3269 case ARM::t2SUBrSPi12_:
3270 OpOpc = ARM::t2SUBrSPi12;
3273 case ARM::t2SUBrSPs_:
3274 OpOpc = ARM::t2SUBrSPs;
3275 NeedPred = true; NeedCC = true; NeedOp3 = true;
3278 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
3279 if (OpOpc == ARM::tAND)
3280 AddDefaultT1CC(MIB);
3281 MIB.addReg(ARM::SP);
3282 MIB.addOperand(MI->getOperand(2));
3284 MIB.addOperand(MI->getOperand(3));
3286 AddDefaultPred(MIB);
3290 // Copy the result from SP to virtual register.
3291 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
3292 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3293 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
3294 BuildMI(BB, dl, TII->get(CopyOpc))
3295 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
3297 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
3303 //===----------------------------------------------------------------------===//
3304 // ARM Optimization Hooks
3305 //===----------------------------------------------------------------------===//
3308 SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
3309 TargetLowering::DAGCombinerInfo &DCI) {
3310 SelectionDAG &DAG = DCI.DAG;
3311 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3312 EVT VT = N->getValueType(0);
3313 unsigned Opc = N->getOpcode();
3314 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
3315 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
3316 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
3317 ISD::CondCode CC = ISD::SETCC_INVALID;
3320 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
3322 SDValue CCOp = Slct.getOperand(0);
3323 if (CCOp.getOpcode() == ISD::SETCC)
3324 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
3327 bool DoXform = false;
3329 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
3332 if (LHS.getOpcode() == ISD::Constant &&
3333 cast<ConstantSDNode>(LHS)->isNullValue()) {
3335 } else if (CC != ISD::SETCC_INVALID &&
3336 RHS.getOpcode() == ISD::Constant &&
3337 cast<ConstantSDNode>(RHS)->isNullValue()) {
3338 std::swap(LHS, RHS);
3339 SDValue Op0 = Slct.getOperand(0);
3340 EVT OpVT = isSlctCC ? Op0.getValueType() :
3341 Op0.getOperand(0).getValueType();
3342 bool isInt = OpVT.isInteger();
3343 CC = ISD::getSetCCInverse(CC, isInt);
3345 if (!TLI.isCondCodeLegal(CC, OpVT))
3346 return SDValue(); // Inverse operator isn't legal.
3353 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
3355 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
3356 Slct.getOperand(0), Slct.getOperand(1), CC);
3357 SDValue CCOp = Slct.getOperand(0);
3359 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
3360 CCOp.getOperand(0), CCOp.getOperand(1), CC);
3361 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
3362 CCOp, OtherOp, Result);
3367 /// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
3368 static SDValue PerformADDCombine(SDNode *N,
3369 TargetLowering::DAGCombinerInfo &DCI) {
3370 // added by evan in r37685 with no testcase.
3371 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
3373 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
3374 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
3375 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
3376 if (Result.getNode()) return Result;
3378 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3379 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3380 if (Result.getNode()) return Result;
3386 /// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
3387 static SDValue PerformSUBCombine(SDNode *N,
3388 TargetLowering::DAGCombinerInfo &DCI) {
3389 // added by evan in r37685 with no testcase.
3390 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
3392 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
3393 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3394 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3395 if (Result.getNode()) return Result;
3401 /// PerformVMOVRRDCombine - Target-specific dag combine xforms for ARMISD::VMOVRRD.
3402 static SDValue PerformVMOVRRDCombine(SDNode *N,
3403 TargetLowering::DAGCombinerInfo &DCI) {
3404 // fmrrd(fmdrr x, y) -> x,y
3405 SDValue InDouble = N->getOperand(0);
3406 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
3407 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
3411 /// getVShiftImm - Check if this is a valid build_vector for the immediate
3412 /// operand of a vector shift operation, where all the elements of the
3413 /// build_vector must have the same constant integer value.
3414 static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
3415 // Ignore bit_converts.
3416 while (Op.getOpcode() == ISD::BIT_CONVERT)
3417 Op = Op.getOperand(0);
3418 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3419 APInt SplatBits, SplatUndef;
3420 unsigned SplatBitSize;
3422 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
3423 HasAnyUndefs, ElementBits) ||
3424 SplatBitSize > ElementBits)
3426 Cnt = SplatBits.getSExtValue();
3430 /// isVShiftLImm - Check if this is a valid build_vector for the immediate
3431 /// operand of a vector shift left operation. That value must be in the range:
3432 /// 0 <= Value < ElementBits for a left shift; or
3433 /// 0 <= Value <= ElementBits for a long left shift.
3434 static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
3435 assert(VT.isVector() && "vector shift count is not a vector type");
3436 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3437 if (! getVShiftImm(Op, ElementBits, Cnt))
3439 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
3442 /// isVShiftRImm - Check if this is a valid build_vector for the immediate
3443 /// operand of a vector shift right operation. For a shift opcode, the value
3444 /// is positive, but for an intrinsic the value count must be negative. The
3445 /// absolute value must be in the range:
3446 /// 1 <= |Value| <= ElementBits for a right shift; or
3447 /// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
3448 static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
3450 assert(VT.isVector() && "vector shift count is not a vector type");
3451 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3452 if (! getVShiftImm(Op, ElementBits, Cnt))
3456 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
3459 /// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
3460 static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
3461 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3464 // Don't do anything for most intrinsics.
3467 // Vector shifts: check for immediate versions and lower them.
3468 // Note: This is done during DAG combining instead of DAG legalizing because
3469 // the build_vectors for 64-bit vector element shift counts are generally
3470 // not legal, and it is hard to see their values after they get legalized to
3471 // loads from a constant pool.
3472 case Intrinsic::arm_neon_vshifts:
3473 case Intrinsic::arm_neon_vshiftu:
3474 case Intrinsic::arm_neon_vshiftls:
3475 case Intrinsic::arm_neon_vshiftlu:
3476 case Intrinsic::arm_neon_vshiftn:
3477 case Intrinsic::arm_neon_vrshifts:
3478 case Intrinsic::arm_neon_vrshiftu:
3479 case Intrinsic::arm_neon_vrshiftn:
3480 case Intrinsic::arm_neon_vqshifts:
3481 case Intrinsic::arm_neon_vqshiftu:
3482 case Intrinsic::arm_neon_vqshiftsu:
3483 case Intrinsic::arm_neon_vqshiftns:
3484 case Intrinsic::arm_neon_vqshiftnu:
3485 case Intrinsic::arm_neon_vqshiftnsu:
3486 case Intrinsic::arm_neon_vqrshiftns:
3487 case Intrinsic::arm_neon_vqrshiftnu:
3488 case Intrinsic::arm_neon_vqrshiftnsu: {
3489 EVT VT = N->getOperand(1).getValueType();
3491 unsigned VShiftOpc = 0;
3494 case Intrinsic::arm_neon_vshifts:
3495 case Intrinsic::arm_neon_vshiftu:
3496 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
3497 VShiftOpc = ARMISD::VSHL;
3500 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
3501 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
3502 ARMISD::VSHRs : ARMISD::VSHRu);
3507 case Intrinsic::arm_neon_vshiftls:
3508 case Intrinsic::arm_neon_vshiftlu:
3509 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
3511 llvm_unreachable("invalid shift count for vshll intrinsic");
3513 case Intrinsic::arm_neon_vrshifts:
3514 case Intrinsic::arm_neon_vrshiftu:
3515 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
3519 case Intrinsic::arm_neon_vqshifts:
3520 case Intrinsic::arm_neon_vqshiftu:
3521 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3525 case Intrinsic::arm_neon_vqshiftsu:
3526 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3528 llvm_unreachable("invalid shift count for vqshlu intrinsic");
3530 case Intrinsic::arm_neon_vshiftn:
3531 case Intrinsic::arm_neon_vrshiftn:
3532 case Intrinsic::arm_neon_vqshiftns:
3533 case Intrinsic::arm_neon_vqshiftnu:
3534 case Intrinsic::arm_neon_vqshiftnsu:
3535 case Intrinsic::arm_neon_vqrshiftns:
3536 case Intrinsic::arm_neon_vqrshiftnu:
3537 case Intrinsic::arm_neon_vqrshiftnsu:
3538 // Narrowing shifts require an immediate right shift.
3539 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
3541 llvm_unreachable("invalid shift count for narrowing vector shift intrinsic");
3544 llvm_unreachable("unhandled vector shift");
3548 case Intrinsic::arm_neon_vshifts:
3549 case Intrinsic::arm_neon_vshiftu:
3550 // Opcode already set above.
3552 case Intrinsic::arm_neon_vshiftls:
3553 case Intrinsic::arm_neon_vshiftlu:
3554 if (Cnt == VT.getVectorElementType().getSizeInBits())
3555 VShiftOpc = ARMISD::VSHLLi;
3557 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
3558 ARMISD::VSHLLs : ARMISD::VSHLLu);
3560 case Intrinsic::arm_neon_vshiftn:
3561 VShiftOpc = ARMISD::VSHRN; break;
3562 case Intrinsic::arm_neon_vrshifts:
3563 VShiftOpc = ARMISD::VRSHRs; break;
3564 case Intrinsic::arm_neon_vrshiftu:
3565 VShiftOpc = ARMISD::VRSHRu; break;
3566 case Intrinsic::arm_neon_vrshiftn:
3567 VShiftOpc = ARMISD::VRSHRN; break;
3568 case Intrinsic::arm_neon_vqshifts:
3569 VShiftOpc = ARMISD::VQSHLs; break;
3570 case Intrinsic::arm_neon_vqshiftu:
3571 VShiftOpc = ARMISD::VQSHLu; break;
3572 case Intrinsic::arm_neon_vqshiftsu:
3573 VShiftOpc = ARMISD::VQSHLsu; break;
3574 case Intrinsic::arm_neon_vqshiftns:
3575 VShiftOpc = ARMISD::VQSHRNs; break;
3576 case Intrinsic::arm_neon_vqshiftnu:
3577 VShiftOpc = ARMISD::VQSHRNu; break;
3578 case Intrinsic::arm_neon_vqshiftnsu:
3579 VShiftOpc = ARMISD::VQSHRNsu; break;
3580 case Intrinsic::arm_neon_vqrshiftns:
3581 VShiftOpc = ARMISD::VQRSHRNs; break;
3582 case Intrinsic::arm_neon_vqrshiftnu:
3583 VShiftOpc = ARMISD::VQRSHRNu; break;
3584 case Intrinsic::arm_neon_vqrshiftnsu:
3585 VShiftOpc = ARMISD::VQRSHRNsu; break;
3588 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
3589 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
3592 case Intrinsic::arm_neon_vshiftins: {
3593 EVT VT = N->getOperand(1).getValueType();
3595 unsigned VShiftOpc = 0;
3597 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
3598 VShiftOpc = ARMISD::VSLI;
3599 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
3600 VShiftOpc = ARMISD::VSRI;
3602 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
3605 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
3606 N->getOperand(1), N->getOperand(2),
3607 DAG.getConstant(Cnt, MVT::i32));
3610 case Intrinsic::arm_neon_vqrshifts:
3611 case Intrinsic::arm_neon_vqrshiftu:
3612 // No immediate versions of these to check for.
3619 /// PerformShiftCombine - Checks for immediate versions of vector shifts and
3620 /// lowers them. As with the vector shift intrinsics, this is done during DAG
3621 /// combining instead of DAG legalizing because the build_vectors for 64-bit
3622 /// vector element shift counts are generally not legal, and it is hard to see
3623 /// their values after they get legalized to loads from a constant pool.
3624 static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
3625 const ARMSubtarget *ST) {
3626 EVT VT = N->getValueType(0);
3628 // Nothing to be done for scalar shifts.
3629 if (! VT.isVector())
3632 assert(ST->hasNEON() && "unexpected vector shift");
3635 switch (N->getOpcode()) {
3636 default: llvm_unreachable("unexpected shift opcode");
3639 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
3640 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
3641 DAG.getConstant(Cnt, MVT::i32));
3646 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
3647 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
3648 ARMISD::VSHRs : ARMISD::VSHRu);
3649 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
3650 DAG.getConstant(Cnt, MVT::i32));
3656 /// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
3657 /// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
3658 static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
3659 const ARMSubtarget *ST) {
3660 SDValue N0 = N->getOperand(0);
3662 // Check for sign- and zero-extensions of vector extract operations of 8-
3663 // and 16-bit vector elements. NEON supports these directly. They are
3664 // handled during DAG combining because type legalization will promote them
3665 // to 32-bit types and it is messy to recognize the operations after that.
3666 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
3667 SDValue Vec = N0.getOperand(0);
3668 SDValue Lane = N0.getOperand(1);
3669 EVT VT = N->getValueType(0);
3670 EVT EltVT = N0.getValueType();
3671 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3673 if (VT == MVT::i32 &&
3674 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
3675 TLI.isTypeLegal(Vec.getValueType())) {
3678 switch (N->getOpcode()) {
3679 default: llvm_unreachable("unexpected opcode");
3680 case ISD::SIGN_EXTEND:
3681 Opc = ARMISD::VGETLANEs;
3683 case ISD::ZERO_EXTEND:
3684 case ISD::ANY_EXTEND:
3685 Opc = ARMISD::VGETLANEu;
3688 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
3695 SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
3696 DAGCombinerInfo &DCI) const {
3697 switch (N->getOpcode()) {
3699 case ISD::ADD: return PerformADDCombine(N, DCI);
3700 case ISD::SUB: return PerformSUBCombine(N, DCI);
3701 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
3702 case ISD::INTRINSIC_WO_CHAIN:
3703 return PerformIntrinsicCombine(N, DCI.DAG);
3707 return PerformShiftCombine(N, DCI.DAG, Subtarget);
3708 case ISD::SIGN_EXTEND:
3709 case ISD::ZERO_EXTEND:
3710 case ISD::ANY_EXTEND:
3711 return PerformExtendCombine(N, DCI.DAG, Subtarget);
3716 bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
3717 if (!Subtarget->hasV6Ops())
3718 // Pre-v6 does not support unaligned mem access.
3720 else if (!Subtarget->hasV6Ops()) {
3721 // v6 may or may not support unaligned mem access.
3722 if (!Subtarget->isTargetDarwin())
3726 switch (VT.getSimpleVT().SimpleTy) {
3733 // FIXME: VLD1 etc with standard alignment is legal.
3737 static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
3742 switch (VT.getSimpleVT().SimpleTy) {
3743 default: return false;
3758 if ((V & (Scale - 1)) != 0)
3761 return V == (V & ((1LL << 5) - 1));
3764 static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
3765 const ARMSubtarget *Subtarget) {
3772 switch (VT.getSimpleVT().SimpleTy) {
3773 default: return false;
3778 // + imm12 or - imm8
3780 return V == (V & ((1LL << 8) - 1));
3781 return V == (V & ((1LL << 12) - 1));
3784 // Same as ARM mode. FIXME: NEON?
3785 if (!Subtarget->hasVFP2())
3790 return V == (V & ((1LL << 8) - 1));
3794 /// isLegalAddressImmediate - Return true if the integer value can be used
3795 /// as the offset of the target addressing mode for load / store of the
3797 static bool isLegalAddressImmediate(int64_t V, EVT VT,
3798 const ARMSubtarget *Subtarget) {
3805 if (Subtarget->isThumb1Only())
3806 return isLegalT1AddressImmediate(V, VT);
3807 else if (Subtarget->isThumb2())
3808 return isLegalT2AddressImmediate(V, VT, Subtarget);
3813 switch (VT.getSimpleVT().SimpleTy) {
3814 default: return false;
3819 return V == (V & ((1LL << 12) - 1));
3822 return V == (V & ((1LL << 8) - 1));
3825 if (!Subtarget->hasVFP2()) // FIXME: NEON?
3830 return V == (V & ((1LL << 8) - 1));
3834 bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
3836 int Scale = AM.Scale;
3840 switch (VT.getSimpleVT().SimpleTy) {
3841 default: return false;
3850 return Scale == 2 || Scale == 4 || Scale == 8;
3853 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
3857 // Note, we allow "void" uses (basically, uses that aren't loads or
3858 // stores), because arm allows folding a scale into many arithmetic
3859 // operations. This should be made more precise and revisited later.
3861 // Allow r << imm, but the imm has to be a multiple of two.
3862 if (Scale & 1) return false;
3863 return isPowerOf2_32(Scale);
3867 /// isLegalAddressingMode - Return true if the addressing mode represented
3868 /// by AM is legal for this target, for a load/store of the specified type.
3869 bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
3870 const Type *Ty) const {
3871 EVT VT = getValueType(Ty, true);
3872 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
3875 // Can never fold addr of global into load/store.
3880 case 0: // no scale reg, must be "r+i" or "r", or "i".
3883 if (Subtarget->isThumb1Only())
3887 // ARM doesn't support any R+R*scale+imm addr modes.
3894 if (Subtarget->isThumb2())
3895 return isLegalT2ScaledAddressingMode(AM, VT);
3897 int Scale = AM.Scale;
3898 switch (VT.getSimpleVT().SimpleTy) {
3899 default: return false;
3903 if (Scale < 0) Scale = -Scale;
3907 return isPowerOf2_32(Scale & ~1);
3911 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
3916 // Note, we allow "void" uses (basically, uses that aren't loads or
3917 // stores), because arm allows folding a scale into many arithmetic
3918 // operations. This should be made more precise and revisited later.
3920 // Allow r << imm, but the imm has to be a multiple of two.
3921 if (Scale & 1) return false;
3922 return isPowerOf2_32(Scale);
3929 /// isLegalICmpImmediate - Return true if the specified immediate is legal
3930 /// icmp immediate, that is the target has icmp instructions which can compare
3931 /// a register against the immediate without having to materialize the
3932 /// immediate into a register.
3933 bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
3934 if (!Subtarget->isThumb())
3935 return ARM_AM::getSOImmVal(Imm) != -1;
3936 if (Subtarget->isThumb2())
3937 return ARM_AM::getT2SOImmVal(Imm) != -1;
3938 return Imm >= 0 && Imm <= 255;
3941 static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
3942 bool isSEXTLoad, SDValue &Base,
3943 SDValue &Offset, bool &isInc,
3944 SelectionDAG &DAG) {
3945 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
3948 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
3950 Base = Ptr->getOperand(0);
3951 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
3952 int RHSC = (int)RHS->getZExtValue();
3953 if (RHSC < 0 && RHSC > -256) {
3954 assert(Ptr->getOpcode() == ISD::ADD);
3956 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3960 isInc = (Ptr->getOpcode() == ISD::ADD);
3961 Offset = Ptr->getOperand(1);
3963 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
3965 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
3966 int RHSC = (int)RHS->getZExtValue();
3967 if (RHSC < 0 && RHSC > -0x1000) {
3968 assert(Ptr->getOpcode() == ISD::ADD);
3970 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
3971 Base = Ptr->getOperand(0);
3976 if (Ptr->getOpcode() == ISD::ADD) {
3978 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
3979 if (ShOpcVal != ARM_AM::no_shift) {
3980 Base = Ptr->getOperand(1);
3981 Offset = Ptr->getOperand(0);
3983 Base = Ptr->getOperand(0);
3984 Offset = Ptr->getOperand(1);
3989 isInc = (Ptr->getOpcode() == ISD::ADD);
3990 Base = Ptr->getOperand(0);
3991 Offset = Ptr->getOperand(1);
3995 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
3999 static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
4000 bool isSEXTLoad, SDValue &Base,
4001 SDValue &Offset, bool &isInc,
4002 SelectionDAG &DAG) {
4003 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4006 Base = Ptr->getOperand(0);
4007 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
4008 int RHSC = (int)RHS->getZExtValue();
4009 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
4010 assert(Ptr->getOpcode() == ISD::ADD);
4012 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4014 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
4015 isInc = Ptr->getOpcode() == ISD::ADD;
4016 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
4024 /// getPreIndexedAddressParts - returns true by value, base pointer and
4025 /// offset pointer and addressing mode by reference if the node's address
4026 /// can be legally represented as pre-indexed load / store address.
4028 ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
4030 ISD::MemIndexedMode &AM,
4031 SelectionDAG &DAG) const {
4032 if (Subtarget->isThumb1Only())
4037 bool isSEXTLoad = false;
4038 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4039 Ptr = LD->getBasePtr();
4040 VT = LD->getMemoryVT();
4041 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4042 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
4043 Ptr = ST->getBasePtr();
4044 VT = ST->getMemoryVT();
4049 bool isLegal = false;
4050 if (Subtarget->isThumb2())
4051 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
4052 Offset, isInc, DAG);
4054 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
4055 Offset, isInc, DAG);
4059 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
4063 /// getPostIndexedAddressParts - returns true by value, base pointer and
4064 /// offset pointer and addressing mode by reference if this node can be
4065 /// combined with a load / store to form a post-indexed load / store.
4066 bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
4069 ISD::MemIndexedMode &AM,
4070 SelectionDAG &DAG) const {
4071 if (Subtarget->isThumb1Only())
4076 bool isSEXTLoad = false;
4077 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4078 VT = LD->getMemoryVT();
4079 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4080 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
4081 VT = ST->getMemoryVT();
4086 bool isLegal = false;
4087 if (Subtarget->isThumb2())
4088 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
4091 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
4096 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
4100 void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
4104 const SelectionDAG &DAG,
4105 unsigned Depth) const {
4106 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
4107 switch (Op.getOpcode()) {
4109 case ARMISD::CMOV: {
4110 // Bits are known zero/one if known on the LHS and RHS.
4111 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
4112 if (KnownZero == 0 && KnownOne == 0) return;
4114 APInt KnownZeroRHS, KnownOneRHS;
4115 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
4116 KnownZeroRHS, KnownOneRHS, Depth+1);
4117 KnownZero &= KnownZeroRHS;
4118 KnownOne &= KnownOneRHS;
4124 //===----------------------------------------------------------------------===//
4125 // ARM Inline Assembly Support
4126 //===----------------------------------------------------------------------===//
4128 /// getConstraintType - Given a constraint letter, return the type of
4129 /// constraint it is for this target.
4130 ARMTargetLowering::ConstraintType
4131 ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
4132 if (Constraint.size() == 1) {
4133 switch (Constraint[0]) {
4135 case 'l': return C_RegisterClass;
4136 case 'w': return C_RegisterClass;
4139 return TargetLowering::getConstraintType(Constraint);
4142 std::pair<unsigned, const TargetRegisterClass*>
4143 ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
4145 if (Constraint.size() == 1) {
4146 // GCC RS6000 Constraint Letters
4147 switch (Constraint[0]) {
4149 if (Subtarget->isThumb1Only())
4150 return std::make_pair(0U, ARM::tGPRRegisterClass);
4152 return std::make_pair(0U, ARM::GPRRegisterClass);
4154 return std::make_pair(0U, ARM::GPRRegisterClass);
4157 return std::make_pair(0U, ARM::SPRRegisterClass);
4159 return std::make_pair(0U, ARM::DPRRegisterClass);
4160 if (VT.getSizeInBits() == 128)
4161 return std::make_pair(0U, ARM::QPRRegisterClass);
4165 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
4168 std::vector<unsigned> ARMTargetLowering::
4169 getRegClassForInlineAsmConstraint(const std::string &Constraint,
4171 if (Constraint.size() != 1)
4172 return std::vector<unsigned>();
4174 switch (Constraint[0]) { // GCC ARM Constraint Letters
4177 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
4178 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
4181 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
4182 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
4183 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
4184 ARM::R12, ARM::LR, 0);
4187 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
4188 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
4189 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
4190 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
4191 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
4192 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
4193 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
4194 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
4196 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
4197 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
4198 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
4199 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
4200 if (VT.getSizeInBits() == 128)
4201 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
4202 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
4206 return std::vector<unsigned>();
4209 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
4210 /// vector. If it is invalid, don't add anything to Ops.
4211 void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
4214 std::vector<SDValue>&Ops,
4215 SelectionDAG &DAG) const {
4216 SDValue Result(0, 0);
4218 switch (Constraint) {
4220 case 'I': case 'J': case 'K': case 'L':
4221 case 'M': case 'N': case 'O':
4222 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
4226 int64_t CVal64 = C->getSExtValue();
4227 int CVal = (int) CVal64;
4228 // None of these constraints allow values larger than 32 bits. Check
4229 // that the value fits in an int.
4233 switch (Constraint) {
4235 if (Subtarget->isThumb1Only()) {
4236 // This must be a constant between 0 and 255, for ADD
4238 if (CVal >= 0 && CVal <= 255)
4240 } else if (Subtarget->isThumb2()) {
4241 // A constant that can be used as an immediate value in a
4242 // data-processing instruction.
4243 if (ARM_AM::getT2SOImmVal(CVal) != -1)
4246 // A constant that can be used as an immediate value in a
4247 // data-processing instruction.
4248 if (ARM_AM::getSOImmVal(CVal) != -1)
4254 if (Subtarget->isThumb()) { // FIXME thumb2
4255 // This must be a constant between -255 and -1, for negated ADD
4256 // immediates. This can be used in GCC with an "n" modifier that
4257 // prints the negated value, for use with SUB instructions. It is
4258 // not useful otherwise but is implemented for compatibility.
4259 if (CVal >= -255 && CVal <= -1)
4262 // This must be a constant between -4095 and 4095. It is not clear
4263 // what this constraint is intended for. Implemented for
4264 // compatibility with GCC.
4265 if (CVal >= -4095 && CVal <= 4095)
4271 if (Subtarget->isThumb1Only()) {
4272 // A 32-bit value where only one byte has a nonzero value. Exclude
4273 // zero to match GCC. This constraint is used by GCC internally for
4274 // constants that can be loaded with a move/shift combination.
4275 // It is not useful otherwise but is implemented for compatibility.
4276 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
4278 } else if (Subtarget->isThumb2()) {
4279 // A constant whose bitwise inverse can be used as an immediate
4280 // value in a data-processing instruction. This can be used in GCC
4281 // with a "B" modifier that prints the inverted value, for use with
4282 // BIC and MVN instructions. It is not useful otherwise but is
4283 // implemented for compatibility.
4284 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
4287 // A constant whose bitwise inverse can be used as an immediate
4288 // value in a data-processing instruction. This can be used in GCC
4289 // with a "B" modifier that prints the inverted value, for use with
4290 // BIC and MVN instructions. It is not useful otherwise but is
4291 // implemented for compatibility.
4292 if (ARM_AM::getSOImmVal(~CVal) != -1)
4298 if (Subtarget->isThumb1Only()) {
4299 // This must be a constant between -7 and 7,
4300 // for 3-operand ADD/SUB immediate instructions.
4301 if (CVal >= -7 && CVal < 7)
4303 } else if (Subtarget->isThumb2()) {
4304 // A constant whose negation can be used as an immediate value in a
4305 // data-processing instruction. This can be used in GCC with an "n"
4306 // modifier that prints the negated value, for use with SUB
4307 // instructions. It is not useful otherwise but is implemented for
4309 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
4312 // A constant whose negation can be used as an immediate value in a
4313 // data-processing instruction. This can be used in GCC with an "n"
4314 // modifier that prints the negated value, for use with SUB
4315 // instructions. It is not useful otherwise but is implemented for
4317 if (ARM_AM::getSOImmVal(-CVal) != -1)
4323 if (Subtarget->isThumb()) { // FIXME thumb2
4324 // This must be a multiple of 4 between 0 and 1020, for
4325 // ADD sp + immediate.
4326 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
4329 // A power of two or a constant between 0 and 32. This is used in
4330 // GCC for the shift amount on shifted register operands, but it is
4331 // useful in general for any shift amounts.
4332 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
4338 if (Subtarget->isThumb()) { // FIXME thumb2
4339 // This must be a constant between 0 and 31, for shift amounts.
4340 if (CVal >= 0 && CVal <= 31)
4346 if (Subtarget->isThumb()) { // FIXME thumb2
4347 // This must be a multiple of 4 between -508 and 508, for
4348 // ADD/SUB sp = sp + immediate.
4349 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
4354 Result = DAG.getTargetConstant(CVal, Op.getValueType());
4358 if (Result.getNode()) {
4359 Ops.push_back(Result);
4362 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
4367 ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
4368 // The ARM target isn't yet aware of offsets.
4372 int ARM::getVFPf32Imm(const APFloat &FPImm) {
4373 APInt Imm = FPImm.bitcastToAPInt();
4374 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
4375 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
4376 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
4378 // We can handle 4 bits of mantissa.
4379 // mantissa = (16+UInt(e:f:g:h))/16.
4380 if (Mantissa & 0x7ffff)
4383 if ((Mantissa & 0xf) != Mantissa)
4386 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4387 if (Exp < -3 || Exp > 4)
4389 Exp = ((Exp+3) & 0x7) ^ 4;
4391 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4394 int ARM::getVFPf64Imm(const APFloat &FPImm) {
4395 APInt Imm = FPImm.bitcastToAPInt();
4396 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
4397 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
4398 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
4400 // We can handle 4 bits of mantissa.
4401 // mantissa = (16+UInt(e:f:g:h))/16.
4402 if (Mantissa & 0xffffffffffffLL)
4405 if ((Mantissa & 0xf) != Mantissa)
4408 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4409 if (Exp < -3 || Exp > 4)
4411 Exp = ((Exp+3) & 0x7) ^ 4;
4413 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4416 /// isFPImmLegal - Returns true if the target can instruction select the
4417 /// specified FP immediate natively. If false, the legalizer will
4418 /// materialize the FP immediate as a load from a constant pool.
4419 bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
4420 if (!Subtarget->hasVFP3())
4423 return ARM::getVFPf32Imm(Imm) != -1;
4425 return ARM::getVFPf64Imm(Imm) != -1;