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/MC/MCSectionMachO.h"
41 #include "llvm/Target/TargetOptions.h"
42 #include "llvm/ADT/VectorExtras.h"
43 #include "llvm/Support/CommandLine.h"
44 #include "llvm/Support/ErrorHandling.h"
45 #include "llvm/Support/MathExtras.h"
46 #include "llvm/Support/raw_ostream.h"
51 EnableARMLongCalls("arm-long-calls", cl::Hidden,
52 cl::desc("Generate calls via indirect call instructions."),
55 static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
56 CCValAssign::LocInfo &LocInfo,
57 ISD::ArgFlagsTy &ArgFlags,
59 static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
60 CCValAssign::LocInfo &LocInfo,
61 ISD::ArgFlagsTy &ArgFlags,
63 static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
64 CCValAssign::LocInfo &LocInfo,
65 ISD::ArgFlagsTy &ArgFlags,
67 static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
68 CCValAssign::LocInfo &LocInfo,
69 ISD::ArgFlagsTy &ArgFlags,
72 void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
73 EVT PromotedBitwiseVT) {
74 if (VT != PromotedLdStVT) {
75 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
76 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
77 PromotedLdStVT.getSimpleVT());
79 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
80 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
81 PromotedLdStVT.getSimpleVT());
84 EVT ElemTy = VT.getVectorElementType();
85 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
86 setOperationAction(ISD::VSETCC, VT.getSimpleVT(), Custom);
87 if (ElemTy == MVT::i8 || ElemTy == MVT::i16)
88 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
89 if (ElemTy != MVT::i32) {
90 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
91 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
92 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
93 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
95 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
96 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
97 if (llvm::ModelWithRegSequence())
98 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
100 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Custom);
101 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Expand);
102 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
103 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
104 if (VT.isInteger()) {
105 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
106 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
107 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
110 // Promote all bit-wise operations.
111 if (VT.isInteger() && VT != PromotedBitwiseVT) {
112 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
113 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
114 PromotedBitwiseVT.getSimpleVT());
115 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
116 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
117 PromotedBitwiseVT.getSimpleVT());
118 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
119 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
120 PromotedBitwiseVT.getSimpleVT());
123 // Neon does not support vector divide/remainder operations.
124 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
125 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
126 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
127 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
128 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
129 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
132 void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
133 addRegisterClass(VT, ARM::DPRRegisterClass);
134 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
137 void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
138 addRegisterClass(VT, ARM::QPRRegisterClass);
139 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
142 static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
143 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
144 return new TargetLoweringObjectFileMachO();
146 return new ARMElfTargetObjectFile();
149 ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
150 : TargetLowering(TM, createTLOF(TM)) {
151 Subtarget = &TM.getSubtarget<ARMSubtarget>();
153 if (Subtarget->isTargetDarwin()) {
154 // Uses VFP for Thumb libfuncs if available.
155 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
156 // Single-precision floating-point arithmetic.
157 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
158 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
159 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
160 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
162 // Double-precision floating-point arithmetic.
163 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
164 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
165 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
166 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
168 // Single-precision comparisons.
169 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
170 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
171 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
172 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
173 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
174 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
175 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
176 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
178 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
179 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
180 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
181 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
182 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
183 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
184 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
185 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
187 // Double-precision comparisons.
188 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
189 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
190 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
191 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
192 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
193 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
194 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
195 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
197 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
198 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
199 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
200 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
201 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
202 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
203 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
204 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
206 // Floating-point to integer conversions.
207 // i64 conversions are done via library routines even when generating VFP
208 // instructions, so use the same ones.
209 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
210 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
211 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
212 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
214 // Conversions between floating types.
215 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
216 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
218 // Integer to floating-point conversions.
219 // i64 conversions are done via library routines even when generating VFP
220 // instructions, so use the same ones.
221 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
222 // e.g., __floatunsidf vs. __floatunssidfvfp.
223 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
224 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
225 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
226 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
230 // These libcalls are not available in 32-bit.
231 setLibcallName(RTLIB::SHL_I128, 0);
232 setLibcallName(RTLIB::SRL_I128, 0);
233 setLibcallName(RTLIB::SRA_I128, 0);
235 // Libcalls should use the AAPCS base standard ABI, even if hard float
236 // is in effect, as per the ARM RTABI specification, section 4.1.2.
237 if (Subtarget->isAAPCS_ABI()) {
238 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
239 setLibcallCallingConv(static_cast<RTLIB::Libcall>(i),
240 CallingConv::ARM_AAPCS);
244 if (Subtarget->isThumb1Only())
245 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
247 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
248 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
249 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
250 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
252 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
255 if (Subtarget->hasNEON()) {
256 addDRTypeForNEON(MVT::v2f32);
257 addDRTypeForNEON(MVT::v8i8);
258 addDRTypeForNEON(MVT::v4i16);
259 addDRTypeForNEON(MVT::v2i32);
260 addDRTypeForNEON(MVT::v1i64);
262 addQRTypeForNEON(MVT::v4f32);
263 addQRTypeForNEON(MVT::v2f64);
264 addQRTypeForNEON(MVT::v16i8);
265 addQRTypeForNEON(MVT::v8i16);
266 addQRTypeForNEON(MVT::v4i32);
267 addQRTypeForNEON(MVT::v2i64);
269 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
270 // neither Neon nor VFP support any arithmetic operations on it.
271 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
272 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
273 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
274 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
275 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
276 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
277 setOperationAction(ISD::VSETCC, MVT::v2f64, Expand);
278 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
279 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
280 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
281 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
282 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
283 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
284 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
285 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
286 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
287 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
288 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
289 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
290 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
291 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
292 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
293 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
294 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
296 // Neon does not support some operations on v1i64 and v2i64 types.
297 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
298 setOperationAction(ISD::MUL, MVT::v2i64, Expand);
299 setOperationAction(ISD::VSETCC, MVT::v1i64, Expand);
300 setOperationAction(ISD::VSETCC, MVT::v2i64, Expand);
302 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
303 setTargetDAGCombine(ISD::SHL);
304 setTargetDAGCombine(ISD::SRL);
305 setTargetDAGCombine(ISD::SRA);
306 setTargetDAGCombine(ISD::SIGN_EXTEND);
307 setTargetDAGCombine(ISD::ZERO_EXTEND);
308 setTargetDAGCombine(ISD::ANY_EXTEND);
309 setTargetDAGCombine(ISD::SELECT_CC);
312 computeRegisterProperties();
314 // ARM does not have f32 extending load.
315 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
317 // ARM does not have i1 sign extending load.
318 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
320 // ARM supports all 4 flavors of integer indexed load / store.
321 if (!Subtarget->isThumb1Only()) {
322 for (unsigned im = (unsigned)ISD::PRE_INC;
323 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
324 setIndexedLoadAction(im, MVT::i1, Legal);
325 setIndexedLoadAction(im, MVT::i8, Legal);
326 setIndexedLoadAction(im, MVT::i16, Legal);
327 setIndexedLoadAction(im, MVT::i32, Legal);
328 setIndexedStoreAction(im, MVT::i1, Legal);
329 setIndexedStoreAction(im, MVT::i8, Legal);
330 setIndexedStoreAction(im, MVT::i16, Legal);
331 setIndexedStoreAction(im, MVT::i32, Legal);
335 // i64 operation support.
336 if (Subtarget->isThumb1Only()) {
337 setOperationAction(ISD::MUL, MVT::i64, Expand);
338 setOperationAction(ISD::MULHU, MVT::i32, Expand);
339 setOperationAction(ISD::MULHS, MVT::i32, Expand);
340 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
341 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
343 setOperationAction(ISD::MUL, MVT::i64, Expand);
344 setOperationAction(ISD::MULHU, MVT::i32, Expand);
345 if (!Subtarget->hasV6Ops())
346 setOperationAction(ISD::MULHS, MVT::i32, Expand);
348 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
349 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
350 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
351 setOperationAction(ISD::SRL, MVT::i64, Custom);
352 setOperationAction(ISD::SRA, MVT::i64, Custom);
354 // ARM does not have ROTL.
355 setOperationAction(ISD::ROTL, MVT::i32, Expand);
356 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
357 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
358 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
359 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
361 // Only ARMv6 has BSWAP.
362 if (!Subtarget->hasV6Ops())
363 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
365 // These are expanded into libcalls.
366 if (!Subtarget->hasDivide()) {
367 // v7M has a hardware divider
368 setOperationAction(ISD::SDIV, MVT::i32, Expand);
369 setOperationAction(ISD::UDIV, MVT::i32, Expand);
371 setOperationAction(ISD::SREM, MVT::i32, Expand);
372 setOperationAction(ISD::UREM, MVT::i32, Expand);
373 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
374 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
376 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
377 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
378 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
379 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
380 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
382 setOperationAction(ISD::TRAP, MVT::Other, Legal);
384 // Use the default implementation.
385 setOperationAction(ISD::VASTART, MVT::Other, Custom);
386 setOperationAction(ISD::VAARG, MVT::Other, Expand);
387 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
388 setOperationAction(ISD::VAEND, MVT::Other, Expand);
389 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
390 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
391 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
392 // FIXME: Shouldn't need this, since no register is used, but the legalizer
393 // doesn't yet know how to not do that for SjLj.
394 setExceptionSelectorRegister(ARM::R0);
395 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
396 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
398 // If the subtarget does not have extract instructions, sign_extend_inreg
399 // needs to be expanded. Extract is available in ARM mode on v6 and up,
400 // and on most Thumb2 implementations.
401 if ((!Subtarget->isThumb() && !Subtarget->hasV6Ops())
402 || (Subtarget->isThumb2() && !Subtarget->hasT2ExtractPack())) {
403 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
404 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
406 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
408 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only())
409 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
410 // iff target supports vfp2.
411 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Custom);
413 // We want to custom lower some of our intrinsics.
414 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
416 setOperationAction(ISD::SETCC, MVT::i32, Expand);
417 setOperationAction(ISD::SETCC, MVT::f32, Expand);
418 setOperationAction(ISD::SETCC, MVT::f64, Expand);
419 setOperationAction(ISD::SELECT, MVT::i32, Expand);
420 setOperationAction(ISD::SELECT, MVT::f32, Expand);
421 setOperationAction(ISD::SELECT, MVT::f64, Expand);
422 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
423 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
424 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
426 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
427 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
428 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
429 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
430 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
432 // We don't support sin/cos/fmod/copysign/pow
433 setOperationAction(ISD::FSIN, MVT::f64, Expand);
434 setOperationAction(ISD::FSIN, MVT::f32, Expand);
435 setOperationAction(ISD::FCOS, MVT::f32, Expand);
436 setOperationAction(ISD::FCOS, MVT::f64, Expand);
437 setOperationAction(ISD::FREM, MVT::f64, Expand);
438 setOperationAction(ISD::FREM, MVT::f32, Expand);
439 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
440 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
441 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
443 setOperationAction(ISD::FPOW, MVT::f64, Expand);
444 setOperationAction(ISD::FPOW, MVT::f32, Expand);
446 // Various VFP goodness
447 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
448 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
449 if (Subtarget->hasVFP2()) {
450 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
451 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
452 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
453 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
455 // Special handling for half-precision FP.
456 if (!Subtarget->hasFP16()) {
457 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
458 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
462 // We have target-specific dag combine patterns for the following nodes:
463 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
464 setTargetDAGCombine(ISD::ADD);
465 setTargetDAGCombine(ISD::SUB);
466 setTargetDAGCombine(ISD::MUL);
468 setStackPointerRegisterToSaveRestore(ARM::SP);
470 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
471 setSchedulingPreference(Sched::RegPressure);
473 setSchedulingPreference(Sched::Hybrid);
475 // FIXME: If-converter should use instruction latency to determine
476 // profitability rather than relying on fixed limits.
477 if (Subtarget->getCPUString() == "generic") {
478 // Generic (and overly aggressive) if-conversion limits.
479 setIfCvtBlockSizeLimit(10);
480 setIfCvtDupBlockSizeLimit(2);
481 } else if (Subtarget->hasV7Ops()) {
482 setIfCvtBlockSizeLimit(3);
483 setIfCvtDupBlockSizeLimit(1);
484 } else if (Subtarget->hasV6Ops()) {
485 setIfCvtBlockSizeLimit(2);
486 setIfCvtDupBlockSizeLimit(1);
488 setIfCvtBlockSizeLimit(3);
489 setIfCvtDupBlockSizeLimit(2);
492 maxStoresPerMemcpy = 1; //// temporary - rewrite interface to use type
493 // Do not enable CodePlacementOpt for now: it currently runs after the
494 // ARMConstantIslandPass and messes up branch relaxation and placement
495 // of constant islands.
496 // benefitFromCodePlacementOpt = true;
499 const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
502 case ARMISD::Wrapper: return "ARMISD::Wrapper";
503 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
504 case ARMISD::CALL: return "ARMISD::CALL";
505 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
506 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
507 case ARMISD::tCALL: return "ARMISD::tCALL";
508 case ARMISD::BRCOND: return "ARMISD::BRCOND";
509 case ARMISD::BR_JT: return "ARMISD::BR_JT";
510 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
511 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
512 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
513 case ARMISD::CMP: return "ARMISD::CMP";
514 case ARMISD::CMPZ: return "ARMISD::CMPZ";
515 case ARMISD::CMPFP: return "ARMISD::CMPFP";
516 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
517 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
518 case ARMISD::CMOV: return "ARMISD::CMOV";
519 case ARMISD::CNEG: return "ARMISD::CNEG";
521 case ARMISD::RBIT: return "ARMISD::RBIT";
523 case ARMISD::FTOSI: return "ARMISD::FTOSI";
524 case ARMISD::FTOUI: return "ARMISD::FTOUI";
525 case ARMISD::SITOF: return "ARMISD::SITOF";
526 case ARMISD::UITOF: return "ARMISD::UITOF";
528 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
529 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
530 case ARMISD::RRX: return "ARMISD::RRX";
532 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
533 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
535 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
536 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
538 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
540 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
542 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
543 case ARMISD::SYNCBARRIER: return "ARMISD::SYNCBARRIER";
545 case ARMISD::VCEQ: return "ARMISD::VCEQ";
546 case ARMISD::VCGE: return "ARMISD::VCGE";
547 case ARMISD::VCGEU: return "ARMISD::VCGEU";
548 case ARMISD::VCGT: return "ARMISD::VCGT";
549 case ARMISD::VCGTU: return "ARMISD::VCGTU";
550 case ARMISD::VTST: return "ARMISD::VTST";
552 case ARMISD::VSHL: return "ARMISD::VSHL";
553 case ARMISD::VSHRs: return "ARMISD::VSHRs";
554 case ARMISD::VSHRu: return "ARMISD::VSHRu";
555 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
556 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
557 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
558 case ARMISD::VSHRN: return "ARMISD::VSHRN";
559 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
560 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
561 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
562 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
563 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
564 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
565 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
566 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
567 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
568 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
569 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
570 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
571 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
572 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
573 case ARMISD::VDUP: return "ARMISD::VDUP";
574 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
575 case ARMISD::VEXT: return "ARMISD::VEXT";
576 case ARMISD::VREV64: return "ARMISD::VREV64";
577 case ARMISD::VREV32: return "ARMISD::VREV32";
578 case ARMISD::VREV16: return "ARMISD::VREV16";
579 case ARMISD::VZIP: return "ARMISD::VZIP";
580 case ARMISD::VUZP: return "ARMISD::VUZP";
581 case ARMISD::VTRN: return "ARMISD::VTRN";
582 case ARMISD::FMAX: return "ARMISD::FMAX";
583 case ARMISD::FMIN: return "ARMISD::FMIN";
587 /// getRegClassFor - Return the register class that should be used for the
588 /// specified value type.
589 TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
590 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
591 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
592 // load / store 4 to 8 consecutive D registers.
593 if (Subtarget->hasNEON()) {
594 if (VT == MVT::v4i64)
595 return ARM::QQPRRegisterClass;
596 else if (VT == MVT::v8i64)
597 return ARM::QQQQPRRegisterClass;
599 return TargetLowering::getRegClassFor(VT);
602 /// getFunctionAlignment - Return the Log2 alignment of this function.
603 unsigned ARMTargetLowering::getFunctionAlignment(const Function *F) const {
604 return getTargetMachine().getSubtarget<ARMSubtarget>().isThumb() ? 0 : 1;
607 Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
608 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
609 EVT VT = N->getValueType(i);
610 if (VT.isFloatingPoint() || VT.isVector())
611 return Sched::Latency;
613 return Sched::RegPressure;
616 //===----------------------------------------------------------------------===//
618 //===----------------------------------------------------------------------===//
620 /// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
621 static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
623 default: llvm_unreachable("Unknown condition code!");
624 case ISD::SETNE: return ARMCC::NE;
625 case ISD::SETEQ: return ARMCC::EQ;
626 case ISD::SETGT: return ARMCC::GT;
627 case ISD::SETGE: return ARMCC::GE;
628 case ISD::SETLT: return ARMCC::LT;
629 case ISD::SETLE: return ARMCC::LE;
630 case ISD::SETUGT: return ARMCC::HI;
631 case ISD::SETUGE: return ARMCC::HS;
632 case ISD::SETULT: return ARMCC::LO;
633 case ISD::SETULE: return ARMCC::LS;
637 /// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
638 static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
639 ARMCC::CondCodes &CondCode2) {
640 CondCode2 = ARMCC::AL;
642 default: llvm_unreachable("Unknown FP condition!");
644 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
646 case ISD::SETOGT: CondCode = ARMCC::GT; break;
648 case ISD::SETOGE: CondCode = ARMCC::GE; break;
649 case ISD::SETOLT: CondCode = ARMCC::MI; break;
650 case ISD::SETOLE: CondCode = ARMCC::LS; break;
651 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
652 case ISD::SETO: CondCode = ARMCC::VC; break;
653 case ISD::SETUO: CondCode = ARMCC::VS; break;
654 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
655 case ISD::SETUGT: CondCode = ARMCC::HI; break;
656 case ISD::SETUGE: CondCode = ARMCC::PL; break;
658 case ISD::SETULT: CondCode = ARMCC::LT; break;
660 case ISD::SETULE: CondCode = ARMCC::LE; break;
662 case ISD::SETUNE: CondCode = ARMCC::NE; break;
666 //===----------------------------------------------------------------------===//
667 // Calling Convention Implementation
668 //===----------------------------------------------------------------------===//
670 #include "ARMGenCallingConv.inc"
672 // APCS f64 is in register pairs, possibly split to stack
673 static bool f64AssignAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
674 CCValAssign::LocInfo &LocInfo,
675 CCState &State, bool CanFail) {
676 static const unsigned RegList[] = { ARM::R0, ARM::R1, ARM::R2, ARM::R3 };
678 // Try to get the first register.
679 if (unsigned Reg = State.AllocateReg(RegList, 4))
680 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
682 // For the 2nd half of a v2f64, do not fail.
686 // Put the whole thing on the stack.
687 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
688 State.AllocateStack(8, 4),
693 // Try to get the second register.
694 if (unsigned Reg = State.AllocateReg(RegList, 4))
695 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
697 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
698 State.AllocateStack(4, 4),
703 static bool CC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
704 CCValAssign::LocInfo &LocInfo,
705 ISD::ArgFlagsTy &ArgFlags,
707 if (!f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
709 if (LocVT == MVT::v2f64 &&
710 !f64AssignAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
712 return true; // we handled it
715 // AAPCS f64 is in aligned register pairs
716 static bool f64AssignAAPCS(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
717 CCValAssign::LocInfo &LocInfo,
718 CCState &State, bool CanFail) {
719 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
720 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
722 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
724 // For the 2nd half of a v2f64, do not just fail.
728 // Put the whole thing on the stack.
729 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
730 State.AllocateStack(8, 8),
736 for (i = 0; i < 2; ++i)
737 if (HiRegList[i] == Reg)
740 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
741 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
746 static bool CC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
747 CCValAssign::LocInfo &LocInfo,
748 ISD::ArgFlagsTy &ArgFlags,
750 if (!f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, true))
752 if (LocVT == MVT::v2f64 &&
753 !f64AssignAAPCS(ValNo, ValVT, LocVT, LocInfo, State, false))
755 return true; // we handled it
758 static bool f64RetAssign(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
759 CCValAssign::LocInfo &LocInfo, CCState &State) {
760 static const unsigned HiRegList[] = { ARM::R0, ARM::R2 };
761 static const unsigned LoRegList[] = { ARM::R1, ARM::R3 };
763 unsigned Reg = State.AllocateReg(HiRegList, LoRegList, 2);
765 return false; // we didn't handle it
768 for (i = 0; i < 2; ++i)
769 if (HiRegList[i] == Reg)
772 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
773 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, LoRegList[i],
778 static bool RetCC_ARM_APCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
779 CCValAssign::LocInfo &LocInfo,
780 ISD::ArgFlagsTy &ArgFlags,
782 if (!f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
784 if (LocVT == MVT::v2f64 && !f64RetAssign(ValNo, ValVT, LocVT, LocInfo, State))
786 return true; // we handled it
789 static bool RetCC_ARM_AAPCS_Custom_f64(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
790 CCValAssign::LocInfo &LocInfo,
791 ISD::ArgFlagsTy &ArgFlags,
793 return RetCC_ARM_APCS_Custom_f64(ValNo, ValVT, LocVT, LocInfo, ArgFlags,
797 /// CCAssignFnForNode - Selects the correct CCAssignFn for a the
798 /// given CallingConvention value.
799 CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
801 bool isVarArg) const {
804 llvm_unreachable("Unsupported calling convention");
806 case CallingConv::Fast:
807 // Use target triple & subtarget features to do actual dispatch.
808 if (Subtarget->isAAPCS_ABI()) {
809 if (Subtarget->hasVFP2() &&
810 FloatABIType == FloatABI::Hard && !isVarArg)
811 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
813 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
815 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
816 case CallingConv::ARM_AAPCS_VFP:
817 return (Return ? RetCC_ARM_AAPCS_VFP: CC_ARM_AAPCS_VFP);
818 case CallingConv::ARM_AAPCS:
819 return (Return ? RetCC_ARM_AAPCS: CC_ARM_AAPCS);
820 case CallingConv::ARM_APCS:
821 return (Return ? RetCC_ARM_APCS: CC_ARM_APCS);
825 /// LowerCallResult - Lower the result values of a call into the
826 /// appropriate copies out of appropriate physical registers.
828 ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
829 CallingConv::ID CallConv, bool isVarArg,
830 const SmallVectorImpl<ISD::InputArg> &Ins,
831 DebugLoc dl, SelectionDAG &DAG,
832 SmallVectorImpl<SDValue> &InVals) const {
834 // Assign locations to each value returned by this call.
835 SmallVector<CCValAssign, 16> RVLocs;
836 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
837 RVLocs, *DAG.getContext());
838 CCInfo.AnalyzeCallResult(Ins,
839 CCAssignFnForNode(CallConv, /* Return*/ true,
842 // Copy all of the result registers out of their specified physreg.
843 for (unsigned i = 0; i != RVLocs.size(); ++i) {
844 CCValAssign VA = RVLocs[i];
847 if (VA.needsCustom()) {
848 // Handle f64 or half of a v2f64.
849 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
851 Chain = Lo.getValue(1);
852 InFlag = Lo.getValue(2);
853 VA = RVLocs[++i]; // skip ahead to next loc
854 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
856 Chain = Hi.getValue(1);
857 InFlag = Hi.getValue(2);
858 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
860 if (VA.getLocVT() == MVT::v2f64) {
861 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
862 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
863 DAG.getConstant(0, MVT::i32));
865 VA = RVLocs[++i]; // skip ahead to next loc
866 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
867 Chain = Lo.getValue(1);
868 InFlag = Lo.getValue(2);
869 VA = RVLocs[++i]; // skip ahead to next loc
870 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
871 Chain = Hi.getValue(1);
872 InFlag = Hi.getValue(2);
873 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
874 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
875 DAG.getConstant(1, MVT::i32));
878 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
880 Chain = Val.getValue(1);
881 InFlag = Val.getValue(2);
884 switch (VA.getLocInfo()) {
885 default: llvm_unreachable("Unknown loc info!");
886 case CCValAssign::Full: break;
887 case CCValAssign::BCvt:
888 Val = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), Val);
892 InVals.push_back(Val);
898 /// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
899 /// by "Src" to address "Dst" of size "Size". Alignment information is
900 /// specified by the specific parameter attribute. The copy will be passed as
901 /// a byval function parameter.
902 /// Sometimes what we are copying is the end of a larger object, the part that
903 /// does not fit in registers.
905 CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
906 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
908 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
909 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
910 /*isVolatile=*/false, /*AlwaysInline=*/false,
914 /// LowerMemOpCallTo - Store the argument to the stack.
916 ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
917 SDValue StackPtr, SDValue Arg,
918 DebugLoc dl, SelectionDAG &DAG,
919 const CCValAssign &VA,
920 ISD::ArgFlagsTy Flags) const {
921 unsigned LocMemOffset = VA.getLocMemOffset();
922 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
923 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
924 if (Flags.isByVal()) {
925 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
927 return DAG.getStore(Chain, dl, Arg, PtrOff,
928 PseudoSourceValue::getStack(), LocMemOffset,
932 void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
933 SDValue Chain, SDValue &Arg,
934 RegsToPassVector &RegsToPass,
935 CCValAssign &VA, CCValAssign &NextVA,
937 SmallVector<SDValue, 8> &MemOpChains,
938 ISD::ArgFlagsTy Flags) const {
940 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
941 DAG.getVTList(MVT::i32, MVT::i32), Arg);
942 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
944 if (NextVA.isRegLoc())
945 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
947 assert(NextVA.isMemLoc());
948 if (StackPtr.getNode() == 0)
949 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
951 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
957 /// LowerCall - Lowering a call into a callseq_start <-
958 /// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
961 ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
962 CallingConv::ID CallConv, bool isVarArg,
964 const SmallVectorImpl<ISD::OutputArg> &Outs,
965 const SmallVectorImpl<ISD::InputArg> &Ins,
966 DebugLoc dl, SelectionDAG &DAG,
967 SmallVectorImpl<SDValue> &InVals) const {
968 // ARM target does not yet support tail call optimization.
971 // Analyze operands of the call, assigning locations to each operand.
972 SmallVector<CCValAssign, 16> ArgLocs;
973 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
975 CCInfo.AnalyzeCallOperands(Outs,
976 CCAssignFnForNode(CallConv, /* Return*/ false,
979 // Get a count of how many bytes are to be pushed on the stack.
980 unsigned NumBytes = CCInfo.getNextStackOffset();
982 // Adjust the stack pointer for the new arguments...
983 // These operations are automatically eliminated by the prolog/epilog pass
984 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
986 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
988 RegsToPassVector RegsToPass;
989 SmallVector<SDValue, 8> MemOpChains;
991 // Walk the register/memloc assignments, inserting copies/loads. In the case
992 // of tail call optimization, arguments are handled later.
993 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
996 CCValAssign &VA = ArgLocs[i];
997 SDValue Arg = Outs[realArgIdx].Val;
998 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
1000 // Promote the value if needed.
1001 switch (VA.getLocInfo()) {
1002 default: llvm_unreachable("Unknown loc info!");
1003 case CCValAssign::Full: break;
1004 case CCValAssign::SExt:
1005 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1007 case CCValAssign::ZExt:
1008 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1010 case CCValAssign::AExt:
1011 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1013 case CCValAssign::BCvt:
1014 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1018 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
1019 if (VA.needsCustom()) {
1020 if (VA.getLocVT() == MVT::v2f64) {
1021 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1022 DAG.getConstant(0, MVT::i32));
1023 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1024 DAG.getConstant(1, MVT::i32));
1026 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
1027 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1029 VA = ArgLocs[++i]; // skip ahead to next loc
1030 if (VA.isRegLoc()) {
1031 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
1032 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1034 assert(VA.isMemLoc());
1036 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1037 dl, DAG, VA, Flags));
1040 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
1041 StackPtr, MemOpChains, Flags);
1043 } else if (VA.isRegLoc()) {
1044 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1046 assert(VA.isMemLoc());
1048 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1049 dl, DAG, VA, Flags));
1053 if (!MemOpChains.empty())
1054 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1055 &MemOpChains[0], MemOpChains.size());
1057 // Build a sequence of copy-to-reg nodes chained together with token chain
1058 // and flag operands which copy the outgoing args into the appropriate regs.
1060 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1061 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1062 RegsToPass[i].second, InFlag);
1063 InFlag = Chain.getValue(1);
1066 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1067 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1068 // node so that legalize doesn't hack it.
1069 bool isDirect = false;
1070 bool isARMFunc = false;
1071 bool isLocalARMFunc = false;
1072 MachineFunction &MF = DAG.getMachineFunction();
1073 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1075 if (EnableARMLongCalls) {
1076 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1077 && "long-calls with non-static relocation model!");
1078 // Handle a global address or an external symbol. If it's not one of
1079 // those, the target's already in a register, so we don't need to do
1081 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1082 const GlobalValue *GV = G->getGlobal();
1083 // Create a constant pool entry for the callee address
1084 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1085 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1088 // Get the address of the callee into a register
1089 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1090 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1091 Callee = DAG.getLoad(getPointerTy(), dl,
1092 DAG.getEntryNode(), CPAddr,
1093 PseudoSourceValue::getConstantPool(), 0,
1095 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1096 const char *Sym = S->getSymbol();
1098 // Create a constant pool entry for the callee address
1099 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1100 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1101 Sym, ARMPCLabelIndex, 0);
1102 // Get the address of the callee into a register
1103 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1104 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1105 Callee = DAG.getLoad(getPointerTy(), dl,
1106 DAG.getEntryNode(), CPAddr,
1107 PseudoSourceValue::getConstantPool(), 0,
1110 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1111 const GlobalValue *GV = G->getGlobal();
1113 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
1114 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
1115 getTargetMachine().getRelocationModel() != Reloc::Static;
1116 isARMFunc = !Subtarget->isThumb() || isStub;
1117 // ARM call to a local ARM function is predicable.
1118 isLocalARMFunc = !Subtarget->isThumb() && !isExt;
1119 // tBX takes a register source operand.
1120 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
1121 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1122 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1125 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1126 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1127 Callee = DAG.getLoad(getPointerTy(), dl,
1128 DAG.getEntryNode(), CPAddr,
1129 PseudoSourceValue::getConstantPool(), 0,
1131 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
1132 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
1133 getPointerTy(), Callee, PICLabel);
1135 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy());
1136 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1138 bool isStub = Subtarget->isTargetDarwin() &&
1139 getTargetMachine().getRelocationModel() != Reloc::Static;
1140 isARMFunc = !Subtarget->isThumb() || isStub;
1141 // tBX takes a register source operand.
1142 const char *Sym = S->getSymbol();
1143 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
1144 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1145 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1146 Sym, ARMPCLabelIndex, 4);
1147 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1148 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1149 Callee = DAG.getLoad(getPointerTy(), dl,
1150 DAG.getEntryNode(), CPAddr,
1151 PseudoSourceValue::getConstantPool(), 0,
1153 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
1154 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
1155 getPointerTy(), Callee, PICLabel);
1157 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
1160 // FIXME: handle tail calls differently.
1162 if (Subtarget->isThumb()) {
1163 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
1164 CallOpc = ARMISD::CALL_NOLINK;
1166 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1168 CallOpc = (isDirect || Subtarget->hasV5TOps())
1169 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1170 : ARMISD::CALL_NOLINK;
1172 if (CallOpc == ARMISD::CALL_NOLINK && !Subtarget->isThumb1Only()) {
1173 // implicit def LR - LR mustn't be allocated as GRP:$dst of CALL_NOLINK
1174 Chain = DAG.getCopyToReg(Chain, dl, ARM::LR, DAG.getUNDEF(MVT::i32),InFlag);
1175 InFlag = Chain.getValue(1);
1178 std::vector<SDValue> Ops;
1179 Ops.push_back(Chain);
1180 Ops.push_back(Callee);
1182 // Add argument registers to the end of the list so that they are known live
1184 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1185 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1186 RegsToPass[i].second.getValueType()));
1188 if (InFlag.getNode())
1189 Ops.push_back(InFlag);
1190 // Returns a chain and a flag for retval copy to use.
1191 Chain = DAG.getNode(CallOpc, dl, DAG.getVTList(MVT::Other, MVT::Flag),
1192 &Ops[0], Ops.size());
1193 InFlag = Chain.getValue(1);
1195 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1196 DAG.getIntPtrConstant(0, true), InFlag);
1198 InFlag = Chain.getValue(1);
1200 // Handle result values, copying them out of physregs into vregs that we
1202 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1207 ARMTargetLowering::LowerReturn(SDValue Chain,
1208 CallingConv::ID CallConv, bool isVarArg,
1209 const SmallVectorImpl<ISD::OutputArg> &Outs,
1210 DebugLoc dl, SelectionDAG &DAG) const {
1212 // CCValAssign - represent the assignment of the return value to a location.
1213 SmallVector<CCValAssign, 16> RVLocs;
1215 // CCState - Info about the registers and stack slots.
1216 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1219 // Analyze outgoing return values.
1220 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1223 // If this is the first return lowered for this function, add
1224 // the regs to the liveout set for the function.
1225 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1226 for (unsigned i = 0; i != RVLocs.size(); ++i)
1227 if (RVLocs[i].isRegLoc())
1228 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
1233 // Copy the result values into the output registers.
1234 for (unsigned i = 0, realRVLocIdx = 0;
1236 ++i, ++realRVLocIdx) {
1237 CCValAssign &VA = RVLocs[i];
1238 assert(VA.isRegLoc() && "Can only return in registers!");
1240 SDValue Arg = Outs[realRVLocIdx].Val;
1242 switch (VA.getLocInfo()) {
1243 default: llvm_unreachable("Unknown loc info!");
1244 case CCValAssign::Full: break;
1245 case CCValAssign::BCvt:
1246 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), Arg);
1250 if (VA.needsCustom()) {
1251 if (VA.getLocVT() == MVT::v2f64) {
1252 // Extract the first half and return it in two registers.
1253 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1254 DAG.getConstant(0, MVT::i32));
1255 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
1256 DAG.getVTList(MVT::i32, MVT::i32), Half);
1258 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1259 Flag = Chain.getValue(1);
1260 VA = RVLocs[++i]; // skip ahead to next loc
1261 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1262 HalfGPRs.getValue(1), Flag);
1263 Flag = Chain.getValue(1);
1264 VA = RVLocs[++i]; // skip ahead to next loc
1266 // Extract the 2nd half and fall through to handle it as an f64 value.
1267 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1268 DAG.getConstant(1, MVT::i32));
1270 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1272 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
1273 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
1274 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
1275 Flag = Chain.getValue(1);
1276 VA = RVLocs[++i]; // skip ahead to next loc
1277 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1280 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1282 // Guarantee that all emitted copies are
1283 // stuck together, avoiding something bad.
1284 Flag = Chain.getValue(1);
1289 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
1291 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
1296 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1297 // their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1298 // one of the above mentioned nodes. It has to be wrapped because otherwise
1299 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1300 // be used to form addressing mode. These wrapped nodes will be selected
1302 static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
1303 EVT PtrVT = Op.getValueType();
1304 // FIXME there is no actual debug info here
1305 DebugLoc dl = Op.getDebugLoc();
1306 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
1308 if (CP->isMachineConstantPoolEntry())
1309 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1310 CP->getAlignment());
1312 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1313 CP->getAlignment());
1314 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
1317 SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1318 SelectionDAG &DAG) const {
1319 MachineFunction &MF = DAG.getMachineFunction();
1320 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1321 unsigned ARMPCLabelIndex = 0;
1322 DebugLoc DL = Op.getDebugLoc();
1323 EVT PtrVT = getPointerTy();
1324 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1325 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1327 if (RelocM == Reloc::Static) {
1328 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1330 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
1331 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1332 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1333 ARMCP::CPBlockAddress,
1335 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1337 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1338 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
1339 PseudoSourceValue::getConstantPool(), 0,
1341 if (RelocM == Reloc::Static)
1343 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
1344 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
1347 // Lower ISD::GlobalTLSAddress using the "general dynamic" model
1349 ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1350 SelectionDAG &DAG) const {
1351 DebugLoc dl = GA->getDebugLoc();
1352 EVT PtrVT = getPointerTy();
1353 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1354 MachineFunction &MF = DAG.getMachineFunction();
1355 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1356 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1357 ARMConstantPoolValue *CPV =
1358 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
1359 ARMCP::CPValue, PCAdj, "tlsgd", true);
1360 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1361 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
1362 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
1363 PseudoSourceValue::getConstantPool(), 0,
1365 SDValue Chain = Argument.getValue(1);
1367 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
1368 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
1370 // call __tls_get_addr.
1373 Entry.Node = Argument;
1374 Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
1375 Args.push_back(Entry);
1376 // FIXME: is there useful debug info available here?
1377 std::pair<SDValue, SDValue> CallResult =
1378 LowerCallTo(Chain, (const Type *) Type::getInt32Ty(*DAG.getContext()),
1379 false, false, false, false,
1380 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
1381 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
1382 return CallResult.first;
1385 // Lower ISD::GlobalTLSAddress using the "initial exec" or
1386 // "local exec" model.
1388 ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
1389 SelectionDAG &DAG) const {
1390 const GlobalValue *GV = GA->getGlobal();
1391 DebugLoc dl = GA->getDebugLoc();
1393 SDValue Chain = DAG.getEntryNode();
1394 EVT PtrVT = getPointerTy();
1395 // Get the Thread Pointer
1396 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1398 if (GV->isDeclaration()) {
1399 MachineFunction &MF = DAG.getMachineFunction();
1400 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1401 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1402 // Initial exec model.
1403 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
1404 ARMConstantPoolValue *CPV =
1405 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
1406 ARMCP::CPValue, PCAdj, "gottpoff", true);
1407 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1408 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
1409 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
1410 PseudoSourceValue::getConstantPool(), 0,
1412 Chain = Offset.getValue(1);
1414 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
1415 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
1417 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
1418 PseudoSourceValue::getConstantPool(), 0,
1422 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, "tpoff");
1423 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1424 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
1425 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
1426 PseudoSourceValue::getConstantPool(), 0,
1430 // The address of the thread local variable is the add of the thread
1431 // pointer with the offset of the variable.
1432 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
1436 ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
1437 // TODO: implement the "local dynamic" model
1438 assert(Subtarget->isTargetELF() &&
1439 "TLS not implemented for non-ELF targets");
1440 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1441 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
1442 // otherwise use the "Local Exec" TLS Model
1443 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
1444 return LowerToTLSGeneralDynamicModel(GA, DAG);
1446 return LowerToTLSExecModels(GA, DAG);
1449 SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
1450 SelectionDAG &DAG) const {
1451 EVT PtrVT = getPointerTy();
1452 DebugLoc dl = Op.getDebugLoc();
1453 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1454 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1455 if (RelocM == Reloc::PIC_) {
1456 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
1457 ARMConstantPoolValue *CPV =
1458 new ARMConstantPoolValue(GV, UseGOTOFF ? "GOTOFF" : "GOT");
1459 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1460 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1461 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
1463 PseudoSourceValue::getConstantPool(), 0,
1465 SDValue Chain = Result.getValue(1);
1466 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
1467 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
1469 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
1470 PseudoSourceValue::getGOT(), 0,
1474 // If we have T2 ops, we can materialize the address directly via movt/movw
1475 // pair. This is always cheaper.
1476 if (Subtarget->useMovt()) {
1477 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
1478 DAG.getTargetGlobalAddress(GV, PtrVT));
1480 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
1481 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1482 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1483 PseudoSourceValue::getConstantPool(), 0,
1489 SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
1490 SelectionDAG &DAG) const {
1491 MachineFunction &MF = DAG.getMachineFunction();
1492 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1493 unsigned ARMPCLabelIndex = 0;
1494 EVT PtrVT = getPointerTy();
1495 DebugLoc dl = Op.getDebugLoc();
1496 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1497 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1499 if (RelocM == Reloc::Static)
1500 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
1502 ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1503 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
1504 ARMConstantPoolValue *CPV =
1505 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
1506 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1508 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1510 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1511 PseudoSourceValue::getConstantPool(), 0,
1513 SDValue Chain = Result.getValue(1);
1515 if (RelocM == Reloc::PIC_) {
1516 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
1517 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1520 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
1521 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
1522 PseudoSourceValue::getGOT(), 0,
1528 SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
1529 SelectionDAG &DAG) const {
1530 assert(Subtarget->isTargetELF() &&
1531 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
1532 MachineFunction &MF = DAG.getMachineFunction();
1533 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1534 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1535 EVT PtrVT = getPointerTy();
1536 DebugLoc dl = Op.getDebugLoc();
1537 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
1538 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1539 "_GLOBAL_OFFSET_TABLE_",
1540 ARMPCLabelIndex, PCAdj);
1541 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1542 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1543 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1544 PseudoSourceValue::getConstantPool(), 0,
1546 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
1547 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1551 ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
1552 const ARMSubtarget *Subtarget)
1554 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1555 DebugLoc dl = Op.getDebugLoc();
1557 default: return SDValue(); // Don't custom lower most intrinsics.
1558 case Intrinsic::arm_thread_pointer: {
1559 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1560 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
1562 case Intrinsic::eh_sjlj_lsda: {
1563 MachineFunction &MF = DAG.getMachineFunction();
1564 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1565 unsigned ARMPCLabelIndex = AFI->createConstPoolEntryUId();
1566 EVT PtrVT = getPointerTy();
1567 DebugLoc dl = Op.getDebugLoc();
1568 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1570 unsigned PCAdj = (RelocM != Reloc::PIC_)
1571 ? 0 : (Subtarget->isThumb() ? 4 : 8);
1572 ARMConstantPoolValue *CPV =
1573 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
1574 ARMCP::CPLSDA, PCAdj);
1575 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1576 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1578 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
1579 PseudoSourceValue::getConstantPool(), 0,
1581 SDValue Chain = Result.getValue(1);
1583 if (RelocM == Reloc::PIC_) {
1584 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
1585 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
1589 case Intrinsic::eh_sjlj_setjmp:
1590 SDValue Val = Subtarget->isThumb() ?
1591 DAG.getCopyFromReg(DAG.getEntryNode(), dl, ARM::SP, MVT::i32) :
1592 DAG.getConstant(0, MVT::i32);
1593 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(1),
1598 static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
1599 const ARMSubtarget *Subtarget) {
1600 DebugLoc dl = Op.getDebugLoc();
1601 SDValue Op5 = Op.getOperand(5);
1603 unsigned isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue();
1604 if (isDeviceBarrier) {
1605 if (Subtarget->hasV7Ops())
1606 Res = DAG.getNode(ARMISD::SYNCBARRIER, dl, MVT::Other, Op.getOperand(0));
1608 Res = DAG.getNode(ARMISD::SYNCBARRIER, dl, MVT::Other, Op.getOperand(0),
1609 DAG.getConstant(0, MVT::i32));
1611 if (Subtarget->hasV7Ops())
1612 Res = DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
1614 Res = DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
1615 DAG.getConstant(0, MVT::i32));
1620 static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
1621 MachineFunction &MF = DAG.getMachineFunction();
1622 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
1624 // vastart just stores the address of the VarArgsFrameIndex slot into the
1625 // memory location argument.
1626 DebugLoc dl = Op.getDebugLoc();
1627 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1628 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
1629 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
1630 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
1635 ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
1636 SelectionDAG &DAG) const {
1637 SDNode *Node = Op.getNode();
1638 DebugLoc dl = Node->getDebugLoc();
1639 EVT VT = Node->getValueType(0);
1640 SDValue Chain = Op.getOperand(0);
1641 SDValue Size = Op.getOperand(1);
1642 SDValue Align = Op.getOperand(2);
1644 // Chain the dynamic stack allocation so that it doesn't modify the stack
1645 // pointer when other instructions are using the stack.
1646 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1648 unsigned AlignVal = cast<ConstantSDNode>(Align)->getZExtValue();
1649 unsigned StackAlign = getTargetMachine().getFrameInfo()->getStackAlignment();
1650 if (AlignVal > StackAlign)
1651 // Do this now since selection pass cannot introduce new target
1652 // independent node.
1653 Align = DAG.getConstant(-(uint64_t)AlignVal, VT);
1655 // In Thumb1 mode, there isn't a "sub r, sp, r" instruction, we will end up
1656 // using a "add r, sp, r" instead. Negate the size now so we don't have to
1657 // do even more horrible hack later.
1658 MachineFunction &MF = DAG.getMachineFunction();
1659 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1660 if (AFI->isThumb1OnlyFunction()) {
1662 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Size);
1664 uint32_t Val = C->getZExtValue();
1665 if (Val <= 508 && ((Val & 3) == 0))
1669 Size = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, VT), Size);
1672 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
1673 SDValue Ops1[] = { Chain, Size, Align };
1674 SDValue Res = DAG.getNode(ARMISD::DYN_ALLOC, dl, VTList, Ops1, 3);
1675 Chain = Res.getValue(1);
1676 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1677 DAG.getIntPtrConstant(0, true), SDValue());
1678 SDValue Ops2[] = { Res, Chain };
1679 return DAG.getMergeValues(Ops2, 2, dl);
1683 ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
1684 SDValue &Root, SelectionDAG &DAG,
1685 DebugLoc dl) const {
1686 MachineFunction &MF = DAG.getMachineFunction();
1687 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1689 TargetRegisterClass *RC;
1690 if (AFI->isThumb1OnlyFunction())
1691 RC = ARM::tGPRRegisterClass;
1693 RC = ARM::GPRRegisterClass;
1695 // Transform the arguments stored in physical registers into virtual ones.
1696 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
1697 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
1700 if (NextVA.isMemLoc()) {
1701 MachineFrameInfo *MFI = MF.getFrameInfo();
1702 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true, false);
1704 // Create load node to retrieve arguments from the stack.
1705 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1706 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
1707 PseudoSourceValue::getFixedStack(FI), 0,
1710 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
1711 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
1714 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
1718 ARMTargetLowering::LowerFormalArguments(SDValue Chain,
1719 CallingConv::ID CallConv, bool isVarArg,
1720 const SmallVectorImpl<ISD::InputArg>
1722 DebugLoc dl, SelectionDAG &DAG,
1723 SmallVectorImpl<SDValue> &InVals)
1726 MachineFunction &MF = DAG.getMachineFunction();
1727 MachineFrameInfo *MFI = MF.getFrameInfo();
1729 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1731 // Assign locations to all of the incoming arguments.
1732 SmallVector<CCValAssign, 16> ArgLocs;
1733 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1735 CCInfo.AnalyzeFormalArguments(Ins,
1736 CCAssignFnForNode(CallConv, /* Return*/ false,
1739 SmallVector<SDValue, 16> ArgValues;
1741 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1742 CCValAssign &VA = ArgLocs[i];
1744 // Arguments stored in registers.
1745 if (VA.isRegLoc()) {
1746 EVT RegVT = VA.getLocVT();
1749 if (VA.needsCustom()) {
1750 // f64 and vector types are split up into multiple registers or
1751 // combinations of registers and stack slots.
1752 if (VA.getLocVT() == MVT::v2f64) {
1753 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
1755 VA = ArgLocs[++i]; // skip ahead to next loc
1757 if (VA.isMemLoc()) {
1758 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(),
1760 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1761 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
1762 PseudoSourceValue::getFixedStack(FI), 0,
1765 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
1768 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1769 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
1770 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
1771 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
1772 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
1774 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
1777 TargetRegisterClass *RC;
1779 if (RegVT == MVT::f32)
1780 RC = ARM::SPRRegisterClass;
1781 else if (RegVT == MVT::f64)
1782 RC = ARM::DPRRegisterClass;
1783 else if (RegVT == MVT::v2f64)
1784 RC = ARM::QPRRegisterClass;
1785 else if (RegVT == MVT::i32)
1786 RC = (AFI->isThumb1OnlyFunction() ?
1787 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
1789 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
1791 // Transform the arguments in physical registers into virtual ones.
1792 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
1793 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
1796 // If this is an 8 or 16-bit value, it is really passed promoted
1797 // to 32 bits. Insert an assert[sz]ext to capture this, then
1798 // truncate to the right size.
1799 switch (VA.getLocInfo()) {
1800 default: llvm_unreachable("Unknown loc info!");
1801 case CCValAssign::Full: break;
1802 case CCValAssign::BCvt:
1803 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1805 case CCValAssign::SExt:
1806 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
1807 DAG.getValueType(VA.getValVT()));
1808 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1810 case CCValAssign::ZExt:
1811 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
1812 DAG.getValueType(VA.getValVT()));
1813 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1817 InVals.push_back(ArgValue);
1819 } else { // VA.isRegLoc()
1822 assert(VA.isMemLoc());
1823 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
1825 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
1826 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
1829 // Create load nodes to retrieve arguments from the stack.
1830 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1831 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
1832 PseudoSourceValue::getFixedStack(FI), 0,
1839 static const unsigned GPRArgRegs[] = {
1840 ARM::R0, ARM::R1, ARM::R2, ARM::R3
1843 unsigned NumGPRs = CCInfo.getFirstUnallocated
1844 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
1846 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1847 unsigned VARegSize = (4 - NumGPRs) * 4;
1848 unsigned VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
1849 unsigned ArgOffset = CCInfo.getNextStackOffset();
1850 if (VARegSaveSize) {
1851 // If this function is vararg, store any remaining integer argument regs
1852 // to their spots on the stack so that they may be loaded by deferencing
1853 // the result of va_next.
1854 AFI->setVarArgsRegSaveSize(VARegSaveSize);
1855 AFI->setVarArgsFrameIndex(
1856 MFI->CreateFixedObject(VARegSaveSize,
1857 ArgOffset + VARegSaveSize - VARegSize,
1859 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
1862 SmallVector<SDValue, 4> MemOps;
1863 for (; NumGPRs < 4; ++NumGPRs) {
1864 TargetRegisterClass *RC;
1865 if (AFI->isThumb1OnlyFunction())
1866 RC = ARM::tGPRRegisterClass;
1868 RC = ARM::GPRRegisterClass;
1870 unsigned VReg = MF.addLiveIn(GPRArgRegs[NumGPRs], RC);
1871 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
1873 DAG.getStore(Val.getValue(1), dl, Val, FIN,
1874 PseudoSourceValue::getFixedStack(AFI->getVarArgsFrameIndex()), 0,
1876 MemOps.push_back(Store);
1877 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
1878 DAG.getConstant(4, getPointerTy()));
1880 if (!MemOps.empty())
1881 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1882 &MemOps[0], MemOps.size());
1884 // This will point to the next argument passed via stack.
1885 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset,
1892 /// isFloatingPointZero - Return true if this is +0.0.
1893 static bool isFloatingPointZero(SDValue Op) {
1894 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
1895 return CFP->getValueAPF().isPosZero();
1896 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
1897 // Maybe this has already been legalized into the constant pool?
1898 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
1899 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
1900 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
1901 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
1902 return CFP->getValueAPF().isPosZero();
1908 /// Returns appropriate ARM CMP (cmp) and corresponding condition code for
1909 /// the given operands.
1911 ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
1912 SDValue &ARMCC, SelectionDAG &DAG,
1913 DebugLoc dl) const {
1914 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
1915 unsigned C = RHSC->getZExtValue();
1916 if (!isLegalICmpImmediate(C)) {
1917 // Constant does not fit, try adjusting it by one?
1922 if (isLegalICmpImmediate(C-1)) {
1923 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
1924 RHS = DAG.getConstant(C-1, MVT::i32);
1929 if (C > 0 && isLegalICmpImmediate(C-1)) {
1930 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
1931 RHS = DAG.getConstant(C-1, MVT::i32);
1936 if (isLegalICmpImmediate(C+1)) {
1937 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
1938 RHS = DAG.getConstant(C+1, MVT::i32);
1943 if (C < 0xffffffff && isLegalICmpImmediate(C+1)) {
1944 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
1945 RHS = DAG.getConstant(C+1, MVT::i32);
1952 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
1953 ARMISD::NodeType CompareType;
1956 CompareType = ARMISD::CMP;
1961 CompareType = ARMISD::CMPZ;
1964 ARMCC = DAG.getConstant(CondCode, MVT::i32);
1965 return DAG.getNode(CompareType, dl, MVT::Flag, LHS, RHS);
1968 /// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
1969 static SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
1972 if (!isFloatingPointZero(RHS))
1973 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Flag, LHS, RHS);
1975 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Flag, LHS);
1976 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Flag, Cmp);
1979 SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
1980 EVT VT = Op.getValueType();
1981 SDValue LHS = Op.getOperand(0);
1982 SDValue RHS = Op.getOperand(1);
1983 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1984 SDValue TrueVal = Op.getOperand(2);
1985 SDValue FalseVal = Op.getOperand(3);
1986 DebugLoc dl = Op.getDebugLoc();
1988 if (LHS.getValueType() == MVT::i32) {
1990 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
1991 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
1992 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC, CCR,Cmp);
1995 ARMCC::CondCodes CondCode, CondCode2;
1996 FPCCToARMCC(CC, CondCode, CondCode2);
1998 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
1999 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2000 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
2001 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
2003 if (CondCode2 != ARMCC::AL) {
2004 SDValue ARMCC2 = DAG.getConstant(CondCode2, MVT::i32);
2005 // FIXME: Needs another CMP because flag can have but one use.
2006 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
2007 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
2008 Result, TrueVal, ARMCC2, CCR, Cmp2);
2013 SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2014 SDValue Chain = Op.getOperand(0);
2015 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2016 SDValue LHS = Op.getOperand(2);
2017 SDValue RHS = Op.getOperand(3);
2018 SDValue Dest = Op.getOperand(4);
2019 DebugLoc dl = Op.getDebugLoc();
2021 if (LHS.getValueType() == MVT::i32) {
2023 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2024 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMCC, DAG, dl);
2025 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2026 Chain, Dest, ARMCC, CCR,Cmp);
2029 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
2030 ARMCC::CondCodes CondCode, CondCode2;
2031 FPCCToARMCC(CC, CondCode, CondCode2);
2033 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
2034 SDValue ARMCC = DAG.getConstant(CondCode, MVT::i32);
2035 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2036 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Flag);
2037 SDValue Ops[] = { Chain, Dest, ARMCC, CCR, Cmp };
2038 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
2039 if (CondCode2 != ARMCC::AL) {
2040 ARMCC = DAG.getConstant(CondCode2, MVT::i32);
2041 SDValue Ops[] = { Res, Dest, ARMCC, CCR, Res.getValue(1) };
2042 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
2047 SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
2048 SDValue Chain = Op.getOperand(0);
2049 SDValue Table = Op.getOperand(1);
2050 SDValue Index = Op.getOperand(2);
2051 DebugLoc dl = Op.getDebugLoc();
2053 EVT PTy = getPointerTy();
2054 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2055 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
2056 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
2057 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
2058 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
2059 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2060 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
2061 if (Subtarget->isThumb2()) {
2062 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2063 // which does another jump to the destination. This also makes it easier
2064 // to translate it to TBB / TBH later.
2065 // FIXME: This might not work if the function is extremely large.
2066 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
2067 Addr, Op.getOperand(2), JTI, UId);
2069 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2070 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
2071 PseudoSourceValue::getJumpTable(), 0,
2073 Chain = Addr.getValue(1);
2074 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
2075 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
2077 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
2078 PseudoSourceValue::getJumpTable(), 0, false, false, 0);
2079 Chain = Addr.getValue(1);
2080 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
2084 static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2085 DebugLoc dl = Op.getDebugLoc();
2088 switch (Op.getOpcode()) {
2090 assert(0 && "Invalid opcode!");
2091 case ISD::FP_TO_SINT:
2092 Opc = ARMISD::FTOSI;
2094 case ISD::FP_TO_UINT:
2095 Opc = ARMISD::FTOUI;
2098 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
2099 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
2102 static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
2103 EVT VT = Op.getValueType();
2104 DebugLoc dl = Op.getDebugLoc();
2107 switch (Op.getOpcode()) {
2109 assert(0 && "Invalid opcode!");
2110 case ISD::SINT_TO_FP:
2111 Opc = ARMISD::SITOF;
2113 case ISD::UINT_TO_FP:
2114 Opc = ARMISD::UITOF;
2118 Op = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
2119 return DAG.getNode(Opc, dl, VT, Op);
2122 static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
2123 // Implement fcopysign with a fabs and a conditional fneg.
2124 SDValue Tmp0 = Op.getOperand(0);
2125 SDValue Tmp1 = Op.getOperand(1);
2126 DebugLoc dl = Op.getDebugLoc();
2127 EVT VT = Op.getValueType();
2128 EVT SrcVT = Tmp1.getValueType();
2129 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, VT, Tmp0);
2130 SDValue Cmp = getVFPCmp(Tmp1, DAG.getConstantFP(0.0, SrcVT), DAG, dl);
2131 SDValue ARMCC = DAG.getConstant(ARMCC::LT, MVT::i32);
2132 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2133 return DAG.getNode(ARMISD::CNEG, dl, VT, AbsVal, AbsVal, ARMCC, CCR, Cmp);
2136 SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
2137 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2138 MFI->setFrameAddressIsTaken(true);
2139 EVT VT = Op.getValueType();
2140 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
2141 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2142 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
2143 ? ARM::R7 : ARM::R11;
2144 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2146 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
2151 /// ExpandBIT_CONVERT - If the target supports VFP, this function is called to
2152 /// expand a bit convert where either the source or destination type is i64 to
2153 /// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
2154 /// operand type is illegal (e.g., v2f32 for a target that doesn't support
2155 /// vectors), since the legalizer won't know what to do with that.
2156 static SDValue ExpandBIT_CONVERT(SDNode *N, SelectionDAG &DAG) {
2157 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2158 DebugLoc dl = N->getDebugLoc();
2159 SDValue Op = N->getOperand(0);
2161 // This function is only supposed to be called for i64 types, either as the
2162 // source or destination of the bit convert.
2163 EVT SrcVT = Op.getValueType();
2164 EVT DstVT = N->getValueType(0);
2165 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
2166 "ExpandBIT_CONVERT called for non-i64 type");
2168 // Turn i64->f64 into VMOVDRR.
2169 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
2170 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2171 DAG.getConstant(0, MVT::i32));
2172 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
2173 DAG.getConstant(1, MVT::i32));
2174 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
2177 // Turn f64->i64 into VMOVRRD.
2178 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
2179 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
2180 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
2181 // Merge the pieces into a single i64 value.
2182 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
2188 /// getZeroVector - Returns a vector of specified type with all zero elements.
2190 static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
2191 assert(VT.isVector() && "Expected a vector type");
2193 // Zero vectors are used to represent vector negation and in those cases
2194 // will be implemented with the NEON VNEG instruction. However, VNEG does
2195 // not support i64 elements, so sometimes the zero vectors will need to be
2196 // explicitly constructed. For those cases, and potentially other uses in
2197 // the future, always build zero vectors as <16 x i8> or <8 x i8> bitcasted
2198 // to their dest type. This ensures they get CSE'd.
2200 SDValue Cst = DAG.getTargetConstant(0, MVT::i8);
2201 SmallVector<SDValue, 8> Ops;
2204 if (VT.getSizeInBits() == 64) {
2205 Ops.assign(8, Cst); TVT = MVT::v8i8;
2207 Ops.assign(16, Cst); TVT = MVT::v16i8;
2209 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
2211 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2214 /// getOnesVector - Returns a vector of specified type with all bits set.
2216 static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
2217 assert(VT.isVector() && "Expected a vector type");
2219 // Always build ones vectors as <16 x i8> or <8 x i8> bitcasted to their
2220 // dest type. This ensures they get CSE'd.
2222 SDValue Cst = DAG.getTargetConstant(0xFF, MVT::i8);
2223 SmallVector<SDValue, 8> Ops;
2226 if (VT.getSizeInBits() == 64) {
2227 Ops.assign(8, Cst); TVT = MVT::v8i8;
2229 Ops.assign(16, Cst); TVT = MVT::v16i8;
2231 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, TVT, &Ops[0], Ops.size());
2233 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
2236 /// LowerShiftRightParts - Lower SRA_PARTS, which returns two
2237 /// i32 values and take a 2 x i32 value to shift plus a shift amount.
2238 SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
2239 SelectionDAG &DAG) const {
2240 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2241 EVT VT = Op.getValueType();
2242 unsigned VTBits = VT.getSizeInBits();
2243 DebugLoc dl = Op.getDebugLoc();
2244 SDValue ShOpLo = Op.getOperand(0);
2245 SDValue ShOpHi = Op.getOperand(1);
2246 SDValue ShAmt = Op.getOperand(2);
2248 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
2250 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
2252 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2253 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2254 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
2255 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2256 DAG.getConstant(VTBits, MVT::i32));
2257 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
2258 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2259 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
2261 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2262 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
2264 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
2265 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMCC,
2268 SDValue Ops[2] = { Lo, Hi };
2269 return DAG.getMergeValues(Ops, 2, dl);
2272 /// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
2273 /// i32 values and take a 2 x i32 value to shift plus a shift amount.
2274 SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
2275 SelectionDAG &DAG) const {
2276 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2277 EVT VT = Op.getValueType();
2278 unsigned VTBits = VT.getSizeInBits();
2279 DebugLoc dl = Op.getDebugLoc();
2280 SDValue ShOpLo = Op.getOperand(0);
2281 SDValue ShOpHi = Op.getOperand(1);
2282 SDValue ShAmt = Op.getOperand(2);
2285 assert(Op.getOpcode() == ISD::SHL_PARTS);
2286 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2287 DAG.getConstant(VTBits, MVT::i32), ShAmt);
2288 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
2289 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2290 DAG.getConstant(VTBits, MVT::i32));
2291 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
2292 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
2294 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2295 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2296 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
2298 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
2299 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMCC,
2302 SDValue Ops[2] = { Lo, Hi };
2303 return DAG.getMergeValues(Ops, 2, dl);
2306 static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
2307 const ARMSubtarget *ST) {
2308 EVT VT = N->getValueType(0);
2309 DebugLoc dl = N->getDebugLoc();
2311 if (!ST->hasV6T2Ops())
2314 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
2315 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
2318 static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
2319 const ARMSubtarget *ST) {
2320 EVT VT = N->getValueType(0);
2321 DebugLoc dl = N->getDebugLoc();
2323 // Lower vector shifts on NEON to use VSHL.
2324 if (VT.isVector()) {
2325 assert(ST->hasNEON() && "unexpected vector shift");
2327 // Left shifts translate directly to the vshiftu intrinsic.
2328 if (N->getOpcode() == ISD::SHL)
2329 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
2330 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
2331 N->getOperand(0), N->getOperand(1));
2333 assert((N->getOpcode() == ISD::SRA ||
2334 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
2336 // NEON uses the same intrinsics for both left and right shifts. For
2337 // right shifts, the shift amounts are negative, so negate the vector of
2339 EVT ShiftVT = N->getOperand(1).getValueType();
2340 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
2341 getZeroVector(ShiftVT, DAG, dl),
2343 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
2344 Intrinsic::arm_neon_vshifts :
2345 Intrinsic::arm_neon_vshiftu);
2346 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
2347 DAG.getConstant(vshiftInt, MVT::i32),
2348 N->getOperand(0), NegatedCount);
2351 // We can get here for a node like i32 = ISD::SHL i32, i64
2355 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
2356 "Unknown shift to lower!");
2358 // We only lower SRA, SRL of 1 here, all others use generic lowering.
2359 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
2360 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
2363 // If we are in thumb mode, we don't have RRX.
2364 if (ST->isThumb1Only()) return SDValue();
2366 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
2367 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2368 DAG.getConstant(0, MVT::i32));
2369 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
2370 DAG.getConstant(1, MVT::i32));
2372 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
2373 // captures the result into a carry flag.
2374 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
2375 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Flag), &Hi, 1);
2377 // The low part is an ARMISD::RRX operand, which shifts the carry in.
2378 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
2380 // Merge the pieces into a single i64 value.
2381 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
2384 static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
2385 SDValue TmpOp0, TmpOp1;
2386 bool Invert = false;
2390 SDValue Op0 = Op.getOperand(0);
2391 SDValue Op1 = Op.getOperand(1);
2392 SDValue CC = Op.getOperand(2);
2393 EVT VT = Op.getValueType();
2394 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2395 DebugLoc dl = Op.getDebugLoc();
2397 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
2398 switch (SetCCOpcode) {
2399 default: llvm_unreachable("Illegal FP comparison"); break;
2401 case ISD::SETNE: Invert = true; // Fallthrough
2403 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2405 case ISD::SETLT: Swap = true; // Fallthrough
2407 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2409 case ISD::SETLE: Swap = true; // Fallthrough
2411 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2412 case ISD::SETUGE: Swap = true; // Fallthrough
2413 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
2414 case ISD::SETUGT: Swap = true; // Fallthrough
2415 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
2416 case ISD::SETUEQ: Invert = true; // Fallthrough
2418 // Expand this to (OLT | OGT).
2422 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2423 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
2425 case ISD::SETUO: Invert = true; // Fallthrough
2427 // Expand this to (OLT | OGE).
2431 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
2432 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
2436 // Integer comparisons.
2437 switch (SetCCOpcode) {
2438 default: llvm_unreachable("Illegal integer comparison"); break;
2439 case ISD::SETNE: Invert = true;
2440 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
2441 case ISD::SETLT: Swap = true;
2442 case ISD::SETGT: Opc = ARMISD::VCGT; break;
2443 case ISD::SETLE: Swap = true;
2444 case ISD::SETGE: Opc = ARMISD::VCGE; break;
2445 case ISD::SETULT: Swap = true;
2446 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
2447 case ISD::SETULE: Swap = true;
2448 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
2451 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
2452 if (Opc == ARMISD::VCEQ) {
2455 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
2457 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
2460 // Ignore bitconvert.
2461 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BIT_CONVERT)
2462 AndOp = AndOp.getOperand(0);
2464 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
2466 Op0 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(0));
2467 Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, VT, AndOp.getOperand(1));
2474 std::swap(Op0, Op1);
2476 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
2479 Result = DAG.getNOT(dl, Result, VT);
2484 /// isVMOVSplat - Check if the specified splat value corresponds to an immediate
2485 /// VMOV instruction, and if so, return the constant being splatted.
2486 static SDValue isVMOVSplat(uint64_t SplatBits, uint64_t SplatUndef,
2487 unsigned SplatBitSize, SelectionDAG &DAG) {
2488 switch (SplatBitSize) {
2490 // Any 1-byte value is OK.
2491 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
2492 return DAG.getTargetConstant(SplatBits, MVT::i8);
2495 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
2496 if ((SplatBits & ~0xff) == 0 ||
2497 (SplatBits & ~0xff00) == 0)
2498 return DAG.getTargetConstant(SplatBits, MVT::i16);
2502 // NEON's 32-bit VMOV supports splat values where:
2503 // * only one byte is nonzero, or
2504 // * the least significant byte is 0xff and the second byte is nonzero, or
2505 // * the least significant 2 bytes are 0xff and the third is nonzero.
2506 if ((SplatBits & ~0xff) == 0 ||
2507 (SplatBits & ~0xff00) == 0 ||
2508 (SplatBits & ~0xff0000) == 0 ||
2509 (SplatBits & ~0xff000000) == 0)
2510 return DAG.getTargetConstant(SplatBits, MVT::i32);
2512 if ((SplatBits & ~0xffff) == 0 &&
2513 ((SplatBits | SplatUndef) & 0xff) == 0xff)
2514 return DAG.getTargetConstant(SplatBits | 0xff, MVT::i32);
2516 if ((SplatBits & ~0xffffff) == 0 &&
2517 ((SplatBits | SplatUndef) & 0xffff) == 0xffff)
2518 return DAG.getTargetConstant(SplatBits | 0xffff, MVT::i32);
2520 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
2521 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
2522 // VMOV.I32. A (very) minor optimization would be to replicate the value
2523 // and fall through here to test for a valid 64-bit splat. But, then the
2524 // caller would also need to check and handle the change in size.
2528 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
2529 uint64_t BitMask = 0xff;
2531 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
2532 if (((SplatBits | SplatUndef) & BitMask) == BitMask)
2534 else if ((SplatBits & BitMask) != 0)
2538 return DAG.getTargetConstant(Val, MVT::i64);
2542 llvm_unreachable("unexpected size for isVMOVSplat");
2549 /// getVMOVImm - If this is a build_vector of constants which can be
2550 /// formed by using a VMOV instruction of the specified element size,
2551 /// return the constant being splatted. The ByteSize field indicates the
2552 /// number of bytes of each element [1248].
2553 SDValue ARM::getVMOVImm(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
2554 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N);
2555 APInt SplatBits, SplatUndef;
2556 unsigned SplatBitSize;
2558 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
2559 HasAnyUndefs, ByteSize * 8))
2562 if (SplatBitSize > ByteSize * 8)
2565 return isVMOVSplat(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
2569 static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
2570 bool &ReverseVEXT, unsigned &Imm) {
2571 unsigned NumElts = VT.getVectorNumElements();
2572 ReverseVEXT = false;
2575 // If this is a VEXT shuffle, the immediate value is the index of the first
2576 // element. The other shuffle indices must be the successive elements after
2578 unsigned ExpectedElt = Imm;
2579 for (unsigned i = 1; i < NumElts; ++i) {
2580 // Increment the expected index. If it wraps around, it may still be
2581 // a VEXT but the source vectors must be swapped.
2583 if (ExpectedElt == NumElts * 2) {
2588 if (ExpectedElt != static_cast<unsigned>(M[i]))
2592 // Adjust the index value if the source operands will be swapped.
2599 /// isVREVMask - Check if a vector shuffle corresponds to a VREV
2600 /// instruction with the specified blocksize. (The order of the elements
2601 /// within each block of the vector is reversed.)
2602 static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
2603 unsigned BlockSize) {
2604 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
2605 "Only possible block sizes for VREV are: 16, 32, 64");
2607 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2611 unsigned NumElts = VT.getVectorNumElements();
2612 unsigned BlockElts = M[0] + 1;
2614 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
2617 for (unsigned i = 0; i < NumElts; ++i) {
2618 if ((unsigned) M[i] !=
2619 (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
2626 static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
2627 unsigned &WhichResult) {
2628 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2632 unsigned NumElts = VT.getVectorNumElements();
2633 WhichResult = (M[0] == 0 ? 0 : 1);
2634 for (unsigned i = 0; i < NumElts; i += 2) {
2635 if ((unsigned) M[i] != i + WhichResult ||
2636 (unsigned) M[i+1] != i + NumElts + WhichResult)
2642 /// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
2643 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2644 /// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
2645 static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2646 unsigned &WhichResult) {
2647 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2651 unsigned NumElts = VT.getVectorNumElements();
2652 WhichResult = (M[0] == 0 ? 0 : 1);
2653 for (unsigned i = 0; i < NumElts; i += 2) {
2654 if ((unsigned) M[i] != i + WhichResult ||
2655 (unsigned) M[i+1] != i + WhichResult)
2661 static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
2662 unsigned &WhichResult) {
2663 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2667 unsigned NumElts = VT.getVectorNumElements();
2668 WhichResult = (M[0] == 0 ? 0 : 1);
2669 for (unsigned i = 0; i != NumElts; ++i) {
2670 if ((unsigned) M[i] != 2 * i + WhichResult)
2674 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2675 if (VT.is64BitVector() && EltSz == 32)
2681 /// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
2682 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2683 /// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
2684 static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2685 unsigned &WhichResult) {
2686 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2690 unsigned Half = VT.getVectorNumElements() / 2;
2691 WhichResult = (M[0] == 0 ? 0 : 1);
2692 for (unsigned j = 0; j != 2; ++j) {
2693 unsigned Idx = WhichResult;
2694 for (unsigned i = 0; i != Half; ++i) {
2695 if ((unsigned) M[i + j * Half] != Idx)
2701 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2702 if (VT.is64BitVector() && EltSz == 32)
2708 static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
2709 unsigned &WhichResult) {
2710 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2714 unsigned NumElts = VT.getVectorNumElements();
2715 WhichResult = (M[0] == 0 ? 0 : 1);
2716 unsigned Idx = WhichResult * NumElts / 2;
2717 for (unsigned i = 0; i != NumElts; i += 2) {
2718 if ((unsigned) M[i] != Idx ||
2719 (unsigned) M[i+1] != Idx + NumElts)
2724 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2725 if (VT.is64BitVector() && EltSz == 32)
2731 /// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
2732 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
2733 /// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
2734 static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
2735 unsigned &WhichResult) {
2736 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
2740 unsigned NumElts = VT.getVectorNumElements();
2741 WhichResult = (M[0] == 0 ? 0 : 1);
2742 unsigned Idx = WhichResult * NumElts / 2;
2743 for (unsigned i = 0; i != NumElts; i += 2) {
2744 if ((unsigned) M[i] != Idx ||
2745 (unsigned) M[i+1] != Idx)
2750 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
2751 if (VT.is64BitVector() && EltSz == 32)
2758 static SDValue BuildSplat(SDValue Val, EVT VT, SelectionDAG &DAG, DebugLoc dl) {
2759 // Canonicalize all-zeros and all-ones vectors.
2760 ConstantSDNode *ConstVal = cast<ConstantSDNode>(Val.getNode());
2761 if (ConstVal->isNullValue())
2762 return getZeroVector(VT, DAG, dl);
2763 if (ConstVal->isAllOnesValue())
2764 return getOnesVector(VT, DAG, dl);
2767 if (VT.is64BitVector()) {
2768 switch (Val.getValueType().getSizeInBits()) {
2769 case 8: CanonicalVT = MVT::v8i8; break;
2770 case 16: CanonicalVT = MVT::v4i16; break;
2771 case 32: CanonicalVT = MVT::v2i32; break;
2772 case 64: CanonicalVT = MVT::v1i64; break;
2773 default: llvm_unreachable("unexpected splat element type"); break;
2776 assert(VT.is128BitVector() && "unknown splat vector size");
2777 switch (Val.getValueType().getSizeInBits()) {
2778 case 8: CanonicalVT = MVT::v16i8; break;
2779 case 16: CanonicalVT = MVT::v8i16; break;
2780 case 32: CanonicalVT = MVT::v4i32; break;
2781 case 64: CanonicalVT = MVT::v2i64; break;
2782 default: llvm_unreachable("unexpected splat element type"); break;
2786 // Build a canonical splat for this value.
2787 SmallVector<SDValue, 8> Ops;
2788 Ops.assign(CanonicalVT.getVectorNumElements(), Val);
2789 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, &Ops[0],
2791 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Res);
2794 // If this is a case we can't handle, return null and let the default
2795 // expansion code take care of it.
2796 static SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
2797 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
2798 DebugLoc dl = Op.getDebugLoc();
2799 EVT VT = Op.getValueType();
2801 APInt SplatBits, SplatUndef;
2802 unsigned SplatBitSize;
2804 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
2805 if (SplatBitSize <= 64) {
2806 SDValue Val = isVMOVSplat(SplatBits.getZExtValue(),
2807 SplatUndef.getZExtValue(), SplatBitSize, DAG);
2809 return BuildSplat(Val, VT, DAG, dl);
2813 // If there are only 2 elements in a 128-bit vector, insert them into an
2814 // undef vector. This handles the common case for 128-bit vector argument
2815 // passing, where the insertions should be translated to subreg accesses
2816 // with no real instructions.
2817 if (VT.is128BitVector() && Op.getNumOperands() == 2) {
2818 SDValue Val = DAG.getUNDEF(VT);
2819 SDValue Op0 = Op.getOperand(0);
2820 SDValue Op1 = Op.getOperand(1);
2821 if (Op0.getOpcode() != ISD::UNDEF)
2822 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op0,
2823 DAG.getIntPtrConstant(0));
2824 if (Op1.getOpcode() != ISD::UNDEF)
2825 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Val, Op1,
2826 DAG.getIntPtrConstant(1));
2833 /// isShuffleMaskLegal - Targets can use this to indicate that they only
2834 /// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2835 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2836 /// are assumed to be legal.
2838 ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
2840 if (VT.getVectorNumElements() == 4 &&
2841 (VT.is128BitVector() || VT.is64BitVector())) {
2842 unsigned PFIndexes[4];
2843 for (unsigned i = 0; i != 4; ++i) {
2847 PFIndexes[i] = M[i];
2850 // Compute the index in the perfect shuffle table.
2851 unsigned PFTableIndex =
2852 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
2853 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
2854 unsigned Cost = (PFEntry >> 30);
2861 unsigned Imm, WhichResult;
2863 return (ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
2864 isVREVMask(M, VT, 64) ||
2865 isVREVMask(M, VT, 32) ||
2866 isVREVMask(M, VT, 16) ||
2867 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
2868 isVTRNMask(M, VT, WhichResult) ||
2869 isVUZPMask(M, VT, WhichResult) ||
2870 isVZIPMask(M, VT, WhichResult) ||
2871 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
2872 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
2873 isVZIP_v_undef_Mask(M, VT, WhichResult));
2876 /// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
2877 /// the specified operations to build the shuffle.
2878 static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
2879 SDValue RHS, SelectionDAG &DAG,
2881 unsigned OpNum = (PFEntry >> 26) & 0x0F;
2882 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
2883 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
2886 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
2895 OP_VUZPL, // VUZP, left result
2896 OP_VUZPR, // VUZP, right result
2897 OP_VZIPL, // VZIP, left result
2898 OP_VZIPR, // VZIP, right result
2899 OP_VTRNL, // VTRN, left result
2900 OP_VTRNR // VTRN, right result
2903 if (OpNum == OP_COPY) {
2904 if (LHSID == (1*9+2)*9+3) return LHS;
2905 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
2909 SDValue OpLHS, OpRHS;
2910 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
2911 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
2912 EVT VT = OpLHS.getValueType();
2915 default: llvm_unreachable("Unknown shuffle opcode!");
2917 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
2922 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
2923 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
2927 return DAG.getNode(ARMISD::VEXT, dl, VT,
2929 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
2932 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
2933 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
2936 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
2937 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
2940 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2941 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
2945 static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
2946 SDValue V1 = Op.getOperand(0);
2947 SDValue V2 = Op.getOperand(1);
2948 DebugLoc dl = Op.getDebugLoc();
2949 EVT VT = Op.getValueType();
2950 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
2951 SmallVector<int, 8> ShuffleMask;
2953 // Convert shuffles that are directly supported on NEON to target-specific
2954 // DAG nodes, instead of keeping them as shuffles and matching them again
2955 // during code selection. This is more efficient and avoids the possibility
2956 // of inconsistencies between legalization and selection.
2957 // FIXME: floating-point vectors should be canonicalized to integer vectors
2958 // of the same time so that they get CSEd properly.
2959 SVN->getMask(ShuffleMask);
2961 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
2962 int Lane = SVN->getSplatIndex();
2963 // If this is undef splat, generate it via "just" vdup, if possible.
2964 if (Lane == -1) Lane = 0;
2966 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
2967 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
2969 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
2970 DAG.getConstant(Lane, MVT::i32));
2975 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
2978 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
2979 DAG.getConstant(Imm, MVT::i32));
2982 if (isVREVMask(ShuffleMask, VT, 64))
2983 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
2984 if (isVREVMask(ShuffleMask, VT, 32))
2985 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
2986 if (isVREVMask(ShuffleMask, VT, 16))
2987 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
2989 // Check for Neon shuffles that modify both input vectors in place.
2990 // If both results are used, i.e., if there are two shuffles with the same
2991 // source operands and with masks corresponding to both results of one of
2992 // these operations, DAG memoization will ensure that a single node is
2993 // used for both shuffles.
2994 unsigned WhichResult;
2995 if (isVTRNMask(ShuffleMask, VT, WhichResult))
2996 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
2997 V1, V2).getValue(WhichResult);
2998 if (isVUZPMask(ShuffleMask, VT, WhichResult))
2999 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3000 V1, V2).getValue(WhichResult);
3001 if (isVZIPMask(ShuffleMask, VT, WhichResult))
3002 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3003 V1, V2).getValue(WhichResult);
3005 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
3006 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
3007 V1, V1).getValue(WhichResult);
3008 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3009 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
3010 V1, V1).getValue(WhichResult);
3011 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
3012 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
3013 V1, V1).getValue(WhichResult);
3015 // If the shuffle is not directly supported and it has 4 elements, use
3016 // the PerfectShuffle-generated table to synthesize it from other shuffles.
3017 if (VT.getVectorNumElements() == 4 &&
3018 (VT.is128BitVector() || VT.is64BitVector())) {
3019 unsigned PFIndexes[4];
3020 for (unsigned i = 0; i != 4; ++i) {
3021 if (ShuffleMask[i] < 0)
3024 PFIndexes[i] = ShuffleMask[i];
3027 // Compute the index in the perfect shuffle table.
3028 unsigned PFTableIndex =
3029 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
3031 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3032 unsigned Cost = (PFEntry >> 30);
3035 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
3038 // v2f64 and v2i64 shuffles are just register copies.
3039 if (VT == MVT::v2f64 || VT == MVT::v2i64) {
3040 // Do the expansion as f64 since i64 is not legal.
3041 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, V1);
3042 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, V2);
3043 SDValue Val = DAG.getUNDEF(MVT::v2f64);
3044 for (unsigned i = 0; i < 2; ++i) {
3045 if (ShuffleMask[i] < 0)
3047 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
3048 ShuffleMask[i] < 2 ? V1 : V2,
3049 DAG.getConstant(ShuffleMask[i] & 1, MVT::i32));
3050 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3051 Elt, DAG.getConstant(i, MVT::i32));
3053 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Val);
3059 static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
3060 EVT VT = Op.getValueType();
3061 DebugLoc dl = Op.getDebugLoc();
3062 SDValue Vec = Op.getOperand(0);
3063 SDValue Lane = Op.getOperand(1);
3064 assert(VT == MVT::i32 &&
3065 Vec.getValueType().getVectorElementType().getSizeInBits() < 32 &&
3066 "unexpected type for custom-lowering vector extract");
3067 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
3070 static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3071 // The only time a CONCAT_VECTORS operation can have legal types is when
3072 // two 64-bit vectors are concatenated to a 128-bit vector.
3073 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
3074 "unexpected CONCAT_VECTORS");
3075 DebugLoc dl = Op.getDebugLoc();
3076 SDValue Val = DAG.getUNDEF(MVT::v2f64);
3077 SDValue Op0 = Op.getOperand(0);
3078 SDValue Op1 = Op.getOperand(1);
3079 if (Op0.getOpcode() != ISD::UNDEF)
3080 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3081 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op0),
3082 DAG.getIntPtrConstant(0));
3083 if (Op1.getOpcode() != ISD::UNDEF)
3084 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
3085 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, Op1),
3086 DAG.getIntPtrConstant(1));
3087 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Val);
3090 SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
3091 switch (Op.getOpcode()) {
3092 default: llvm_unreachable("Don't know how to custom lower this!");
3093 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
3094 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
3095 case ISD::GlobalAddress:
3096 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
3097 LowerGlobalAddressELF(Op, DAG);
3098 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
3099 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
3100 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
3101 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
3102 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
3103 case ISD::VASTART: return LowerVASTART(Op, DAG);
3104 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
3105 case ISD::SINT_TO_FP:
3106 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
3107 case ISD::FP_TO_SINT:
3108 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
3109 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
3110 case ISD::RETURNADDR: break;
3111 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
3112 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
3113 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
3115 case ISD::BIT_CONVERT: return ExpandBIT_CONVERT(Op.getNode(), DAG);
3118 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
3119 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
3120 case ISD::SRL_PARTS:
3121 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
3122 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
3123 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
3124 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3125 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
3126 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
3127 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
3132 /// ReplaceNodeResults - Replace the results of node with an illegal result
3133 /// type with new values built out of custom code.
3134 void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
3135 SmallVectorImpl<SDValue>&Results,
3136 SelectionDAG &DAG) const {
3138 switch (N->getOpcode()) {
3140 llvm_unreachable("Don't know how to custom expand this!");
3142 case ISD::BIT_CONVERT:
3143 Res = ExpandBIT_CONVERT(N, DAG);
3147 Res = LowerShift(N, DAG, Subtarget);
3151 Results.push_back(Res);
3154 //===----------------------------------------------------------------------===//
3155 // ARM Scheduler Hooks
3156 //===----------------------------------------------------------------------===//
3159 ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
3160 MachineBasicBlock *BB,
3161 unsigned Size) const {
3162 unsigned dest = MI->getOperand(0).getReg();
3163 unsigned ptr = MI->getOperand(1).getReg();
3164 unsigned oldval = MI->getOperand(2).getReg();
3165 unsigned newval = MI->getOperand(3).getReg();
3166 unsigned scratch = BB->getParent()->getRegInfo()
3167 .createVirtualRegister(ARM::GPRRegisterClass);
3168 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3169 DebugLoc dl = MI->getDebugLoc();
3170 bool isThumb2 = Subtarget->isThumb2();
3172 unsigned ldrOpc, strOpc;
3174 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
3176 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
3177 strOpc = isThumb2 ? ARM::t2LDREXB : ARM::STREXB;
3180 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3181 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3184 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3185 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3189 MachineFunction *MF = BB->getParent();
3190 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3191 MachineFunction::iterator It = BB;
3192 ++It; // insert the new blocks after the current block
3194 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3195 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
3196 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3197 MF->insert(It, loop1MBB);
3198 MF->insert(It, loop2MBB);
3199 MF->insert(It, exitMBB);
3200 exitMBB->transferSuccessors(BB);
3204 // fallthrough --> loop1MBB
3205 BB->addSuccessor(loop1MBB);
3208 // ldrex dest, [ptr]
3212 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
3213 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
3214 .addReg(dest).addReg(oldval));
3215 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3216 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
3217 BB->addSuccessor(loop2MBB);
3218 BB->addSuccessor(exitMBB);
3221 // strex scratch, newval, [ptr]
3225 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval)
3227 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
3228 .addReg(scratch).addImm(0));
3229 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3230 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
3231 BB->addSuccessor(loop1MBB);
3232 BB->addSuccessor(exitMBB);
3238 MF->DeleteMachineInstr(MI); // The instruction is gone now.
3244 ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
3245 unsigned Size, unsigned BinOpcode) const {
3246 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
3247 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3249 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3250 MachineFunction *MF = BB->getParent();
3251 MachineFunction::iterator It = BB;
3254 unsigned dest = MI->getOperand(0).getReg();
3255 unsigned ptr = MI->getOperand(1).getReg();
3256 unsigned incr = MI->getOperand(2).getReg();
3257 DebugLoc dl = MI->getDebugLoc();
3259 bool isThumb2 = Subtarget->isThumb2();
3260 unsigned ldrOpc, strOpc;
3262 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
3264 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
3265 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
3268 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
3269 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
3272 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
3273 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
3277 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3278 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3279 MF->insert(It, loopMBB);
3280 MF->insert(It, exitMBB);
3281 exitMBB->transferSuccessors(BB);
3283 MachineRegisterInfo &RegInfo = MF->getRegInfo();
3284 unsigned scratch = RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3285 unsigned scratch2 = (!BinOpcode) ? incr :
3286 RegInfo.createVirtualRegister(ARM::GPRRegisterClass);
3290 // fallthrough --> loopMBB
3291 BB->addSuccessor(loopMBB);
3295 // <binop> scratch2, dest, incr
3296 // strex scratch, scratch2, ptr
3299 // fallthrough --> exitMBB
3301 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr));
3303 // operand order needs to go the other way for NAND
3304 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
3305 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3306 addReg(incr).addReg(dest)).addReg(0);
3308 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
3309 addReg(dest).addReg(incr)).addReg(0);
3312 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2)
3314 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
3315 .addReg(scratch).addImm(0));
3316 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
3317 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
3319 BB->addSuccessor(loopMBB);
3320 BB->addSuccessor(exitMBB);
3326 MF->DeleteMachineInstr(MI); // The instruction is gone now.
3332 ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
3333 MachineBasicBlock *BB) const {
3334 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
3335 DebugLoc dl = MI->getDebugLoc();
3336 bool isThumb2 = Subtarget->isThumb2();
3337 switch (MI->getOpcode()) {
3340 llvm_unreachable("Unexpected instr type to insert");
3342 case ARM::ATOMIC_LOAD_ADD_I8:
3343 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3344 case ARM::ATOMIC_LOAD_ADD_I16:
3345 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3346 case ARM::ATOMIC_LOAD_ADD_I32:
3347 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
3349 case ARM::ATOMIC_LOAD_AND_I8:
3350 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3351 case ARM::ATOMIC_LOAD_AND_I16:
3352 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3353 case ARM::ATOMIC_LOAD_AND_I32:
3354 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
3356 case ARM::ATOMIC_LOAD_OR_I8:
3357 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3358 case ARM::ATOMIC_LOAD_OR_I16:
3359 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3360 case ARM::ATOMIC_LOAD_OR_I32:
3361 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
3363 case ARM::ATOMIC_LOAD_XOR_I8:
3364 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3365 case ARM::ATOMIC_LOAD_XOR_I16:
3366 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3367 case ARM::ATOMIC_LOAD_XOR_I32:
3368 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
3370 case ARM::ATOMIC_LOAD_NAND_I8:
3371 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3372 case ARM::ATOMIC_LOAD_NAND_I16:
3373 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3374 case ARM::ATOMIC_LOAD_NAND_I32:
3375 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
3377 case ARM::ATOMIC_LOAD_SUB_I8:
3378 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3379 case ARM::ATOMIC_LOAD_SUB_I16:
3380 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3381 case ARM::ATOMIC_LOAD_SUB_I32:
3382 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
3384 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
3385 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
3386 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
3388 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
3389 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
3390 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
3392 case ARM::tMOVCCr_pseudo: {
3393 // To "insert" a SELECT_CC instruction, we actually have to insert the
3394 // diamond control-flow pattern. The incoming instruction knows the
3395 // destination vreg to set, the condition code register to branch on, the
3396 // true/false values to select between, and a branch opcode to use.
3397 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3398 MachineFunction::iterator It = BB;
3404 // cmpTY ccX, r1, r2
3406 // fallthrough --> copy0MBB
3407 MachineBasicBlock *thisMBB = BB;
3408 MachineFunction *F = BB->getParent();
3409 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3410 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
3411 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
3412 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
3413 F->insert(It, copy0MBB);
3414 F->insert(It, sinkMBB);
3415 // Update machine-CFG edges by first adding all successors of the current
3416 // block to the new block which will contain the Phi node for the select.
3417 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
3418 E = BB->succ_end(); I != E; ++I)
3419 sinkMBB->addSuccessor(*I);
3420 // Next, remove all successors of the current block, and add the true
3421 // and fallthrough blocks as its successors.
3422 while (!BB->succ_empty())
3423 BB->removeSuccessor(BB->succ_begin());
3424 BB->addSuccessor(copy0MBB);
3425 BB->addSuccessor(sinkMBB);
3428 // %FalseValue = ...
3429 // # fallthrough to sinkMBB
3432 // Update machine-CFG edges
3433 BB->addSuccessor(sinkMBB);
3436 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
3439 BuildMI(BB, dl, TII->get(ARM::PHI), MI->getOperand(0).getReg())
3440 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
3441 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
3443 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
3450 case ARM::t2SUBrSPi_:
3451 case ARM::t2SUBrSPi12_:
3452 case ARM::t2SUBrSPs_: {
3453 MachineFunction *MF = BB->getParent();
3454 unsigned DstReg = MI->getOperand(0).getReg();
3455 unsigned SrcReg = MI->getOperand(1).getReg();
3456 bool DstIsDead = MI->getOperand(0).isDead();
3457 bool SrcIsKill = MI->getOperand(1).isKill();
3459 if (SrcReg != ARM::SP) {
3460 // Copy the source to SP from virtual register.
3461 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(SrcReg);
3462 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3463 ? ARM::tMOVtgpr2gpr : ARM::tMOVgpr2gpr;
3464 BuildMI(BB, dl, TII->get(CopyOpc), ARM::SP)
3465 .addReg(SrcReg, getKillRegState(SrcIsKill));
3469 bool NeedPred = false, NeedCC = false, NeedOp3 = false;
3470 switch (MI->getOpcode()) {
3472 llvm_unreachable("Unexpected pseudo instruction!");
3478 OpOpc = ARM::tADDspr;
3481 OpOpc = ARM::tSUBspi;
3483 case ARM::t2SUBrSPi_:
3484 OpOpc = ARM::t2SUBrSPi;
3485 NeedPred = true; NeedCC = true;
3487 case ARM::t2SUBrSPi12_:
3488 OpOpc = ARM::t2SUBrSPi12;
3491 case ARM::t2SUBrSPs_:
3492 OpOpc = ARM::t2SUBrSPs;
3493 NeedPred = true; NeedCC = true; NeedOp3 = true;
3496 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(OpOpc), ARM::SP);
3497 if (OpOpc == ARM::tAND)
3498 AddDefaultT1CC(MIB);
3499 MIB.addReg(ARM::SP);
3500 MIB.addOperand(MI->getOperand(2));
3502 MIB.addOperand(MI->getOperand(3));
3504 AddDefaultPred(MIB);
3508 // Copy the result from SP to virtual register.
3509 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(DstReg);
3510 unsigned CopyOpc = (RC == ARM::tGPRRegisterClass)
3511 ? ARM::tMOVgpr2tgpr : ARM::tMOVgpr2gpr;
3512 BuildMI(BB, dl, TII->get(CopyOpc))
3513 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
3515 MF->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
3521 //===----------------------------------------------------------------------===//
3522 // ARM Optimization Hooks
3523 //===----------------------------------------------------------------------===//
3526 SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
3527 TargetLowering::DAGCombinerInfo &DCI) {
3528 SelectionDAG &DAG = DCI.DAG;
3529 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3530 EVT VT = N->getValueType(0);
3531 unsigned Opc = N->getOpcode();
3532 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
3533 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
3534 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
3535 ISD::CondCode CC = ISD::SETCC_INVALID;
3538 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
3540 SDValue CCOp = Slct.getOperand(0);
3541 if (CCOp.getOpcode() == ISD::SETCC)
3542 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
3545 bool DoXform = false;
3547 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
3550 if (LHS.getOpcode() == ISD::Constant &&
3551 cast<ConstantSDNode>(LHS)->isNullValue()) {
3553 } else if (CC != ISD::SETCC_INVALID &&
3554 RHS.getOpcode() == ISD::Constant &&
3555 cast<ConstantSDNode>(RHS)->isNullValue()) {
3556 std::swap(LHS, RHS);
3557 SDValue Op0 = Slct.getOperand(0);
3558 EVT OpVT = isSlctCC ? Op0.getValueType() :
3559 Op0.getOperand(0).getValueType();
3560 bool isInt = OpVT.isInteger();
3561 CC = ISD::getSetCCInverse(CC, isInt);
3563 if (!TLI.isCondCodeLegal(CC, OpVT))
3564 return SDValue(); // Inverse operator isn't legal.
3571 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
3573 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
3574 Slct.getOperand(0), Slct.getOperand(1), CC);
3575 SDValue CCOp = Slct.getOperand(0);
3577 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
3578 CCOp.getOperand(0), CCOp.getOperand(1), CC);
3579 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
3580 CCOp, OtherOp, Result);
3585 /// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
3586 static SDValue PerformADDCombine(SDNode *N,
3587 TargetLowering::DAGCombinerInfo &DCI) {
3588 // added by evan in r37685 with no testcase.
3589 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
3591 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
3592 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
3593 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
3594 if (Result.getNode()) return Result;
3596 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3597 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3598 if (Result.getNode()) return Result;
3604 /// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
3605 static SDValue PerformSUBCombine(SDNode *N,
3606 TargetLowering::DAGCombinerInfo &DCI) {
3607 // added by evan in r37685 with no testcase.
3608 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
3610 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
3611 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
3612 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
3613 if (Result.getNode()) return Result;
3619 static SDValue PerformMULCombine(SDNode *N,
3620 TargetLowering::DAGCombinerInfo &DCI,
3621 const ARMSubtarget *Subtarget) {
3622 SelectionDAG &DAG = DCI.DAG;
3624 if (Subtarget->isThumb1Only())
3627 if (DAG.getMachineFunction().
3628 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
3631 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
3634 EVT VT = N->getValueType(0);
3638 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
3642 uint64_t MulAmt = C->getZExtValue();
3643 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
3644 ShiftAmt = ShiftAmt & (32 - 1);
3645 SDValue V = N->getOperand(0);
3646 DebugLoc DL = N->getDebugLoc();
3649 MulAmt >>= ShiftAmt;
3650 if (isPowerOf2_32(MulAmt - 1)) {
3651 // (mul x, 2^N + 1) => (add (shl x, N), x)
3652 Res = DAG.getNode(ISD::ADD, DL, VT,
3653 V, DAG.getNode(ISD::SHL, DL, VT,
3654 V, DAG.getConstant(Log2_32(MulAmt-1),
3656 } else if (isPowerOf2_32(MulAmt + 1)) {
3657 // (mul x, 2^N - 1) => (sub (shl x, N), x)
3658 Res = DAG.getNode(ISD::SUB, DL, VT,
3659 DAG.getNode(ISD::SHL, DL, VT,
3660 V, DAG.getConstant(Log2_32(MulAmt+1),
3667 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
3668 DAG.getConstant(ShiftAmt, MVT::i32));
3670 // Do not add new nodes to DAG combiner worklist.
3671 DCI.CombineTo(N, Res, false);
3675 /// PerformVMOVRRDCombine - Target-specific dag combine xforms for
3676 /// ARMISD::VMOVRRD.
3677 static SDValue PerformVMOVRRDCombine(SDNode *N,
3678 TargetLowering::DAGCombinerInfo &DCI) {
3679 // fmrrd(fmdrr x, y) -> x,y
3680 SDValue InDouble = N->getOperand(0);
3681 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
3682 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
3686 /// getVShiftImm - Check if this is a valid build_vector for the immediate
3687 /// operand of a vector shift operation, where all the elements of the
3688 /// build_vector must have the same constant integer value.
3689 static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
3690 // Ignore bit_converts.
3691 while (Op.getOpcode() == ISD::BIT_CONVERT)
3692 Op = Op.getOperand(0);
3693 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3694 APInt SplatBits, SplatUndef;
3695 unsigned SplatBitSize;
3697 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
3698 HasAnyUndefs, ElementBits) ||
3699 SplatBitSize > ElementBits)
3701 Cnt = SplatBits.getSExtValue();
3705 /// isVShiftLImm - Check if this is a valid build_vector for the immediate
3706 /// operand of a vector shift left operation. That value must be in the range:
3707 /// 0 <= Value < ElementBits for a left shift; or
3708 /// 0 <= Value <= ElementBits for a long left shift.
3709 static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
3710 assert(VT.isVector() && "vector shift count is not a vector type");
3711 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3712 if (! getVShiftImm(Op, ElementBits, Cnt))
3714 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
3717 /// isVShiftRImm - Check if this is a valid build_vector for the immediate
3718 /// operand of a vector shift right operation. For a shift opcode, the value
3719 /// is positive, but for an intrinsic the value count must be negative. The
3720 /// absolute value must be in the range:
3721 /// 1 <= |Value| <= ElementBits for a right shift; or
3722 /// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
3723 static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
3725 assert(VT.isVector() && "vector shift count is not a vector type");
3726 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3727 if (! getVShiftImm(Op, ElementBits, Cnt))
3731 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
3734 /// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
3735 static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
3736 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3739 // Don't do anything for most intrinsics.
3742 // Vector shifts: check for immediate versions and lower them.
3743 // Note: This is done during DAG combining instead of DAG legalizing because
3744 // the build_vectors for 64-bit vector element shift counts are generally
3745 // not legal, and it is hard to see their values after they get legalized to
3746 // loads from a constant pool.
3747 case Intrinsic::arm_neon_vshifts:
3748 case Intrinsic::arm_neon_vshiftu:
3749 case Intrinsic::arm_neon_vshiftls:
3750 case Intrinsic::arm_neon_vshiftlu:
3751 case Intrinsic::arm_neon_vshiftn:
3752 case Intrinsic::arm_neon_vrshifts:
3753 case Intrinsic::arm_neon_vrshiftu:
3754 case Intrinsic::arm_neon_vrshiftn:
3755 case Intrinsic::arm_neon_vqshifts:
3756 case Intrinsic::arm_neon_vqshiftu:
3757 case Intrinsic::arm_neon_vqshiftsu:
3758 case Intrinsic::arm_neon_vqshiftns:
3759 case Intrinsic::arm_neon_vqshiftnu:
3760 case Intrinsic::arm_neon_vqshiftnsu:
3761 case Intrinsic::arm_neon_vqrshiftns:
3762 case Intrinsic::arm_neon_vqrshiftnu:
3763 case Intrinsic::arm_neon_vqrshiftnsu: {
3764 EVT VT = N->getOperand(1).getValueType();
3766 unsigned VShiftOpc = 0;
3769 case Intrinsic::arm_neon_vshifts:
3770 case Intrinsic::arm_neon_vshiftu:
3771 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
3772 VShiftOpc = ARMISD::VSHL;
3775 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
3776 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
3777 ARMISD::VSHRs : ARMISD::VSHRu);
3782 case Intrinsic::arm_neon_vshiftls:
3783 case Intrinsic::arm_neon_vshiftlu:
3784 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
3786 llvm_unreachable("invalid shift count for vshll intrinsic");
3788 case Intrinsic::arm_neon_vrshifts:
3789 case Intrinsic::arm_neon_vrshiftu:
3790 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
3794 case Intrinsic::arm_neon_vqshifts:
3795 case Intrinsic::arm_neon_vqshiftu:
3796 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3800 case Intrinsic::arm_neon_vqshiftsu:
3801 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
3803 llvm_unreachable("invalid shift count for vqshlu intrinsic");
3805 case Intrinsic::arm_neon_vshiftn:
3806 case Intrinsic::arm_neon_vrshiftn:
3807 case Intrinsic::arm_neon_vqshiftns:
3808 case Intrinsic::arm_neon_vqshiftnu:
3809 case Intrinsic::arm_neon_vqshiftnsu:
3810 case Intrinsic::arm_neon_vqrshiftns:
3811 case Intrinsic::arm_neon_vqrshiftnu:
3812 case Intrinsic::arm_neon_vqrshiftnsu:
3813 // Narrowing shifts require an immediate right shift.
3814 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
3816 llvm_unreachable("invalid shift count for narrowing vector shift intrinsic");
3819 llvm_unreachable("unhandled vector shift");
3823 case Intrinsic::arm_neon_vshifts:
3824 case Intrinsic::arm_neon_vshiftu:
3825 // Opcode already set above.
3827 case Intrinsic::arm_neon_vshiftls:
3828 case Intrinsic::arm_neon_vshiftlu:
3829 if (Cnt == VT.getVectorElementType().getSizeInBits())
3830 VShiftOpc = ARMISD::VSHLLi;
3832 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
3833 ARMISD::VSHLLs : ARMISD::VSHLLu);
3835 case Intrinsic::arm_neon_vshiftn:
3836 VShiftOpc = ARMISD::VSHRN; break;
3837 case Intrinsic::arm_neon_vrshifts:
3838 VShiftOpc = ARMISD::VRSHRs; break;
3839 case Intrinsic::arm_neon_vrshiftu:
3840 VShiftOpc = ARMISD::VRSHRu; break;
3841 case Intrinsic::arm_neon_vrshiftn:
3842 VShiftOpc = ARMISD::VRSHRN; break;
3843 case Intrinsic::arm_neon_vqshifts:
3844 VShiftOpc = ARMISD::VQSHLs; break;
3845 case Intrinsic::arm_neon_vqshiftu:
3846 VShiftOpc = ARMISD::VQSHLu; break;
3847 case Intrinsic::arm_neon_vqshiftsu:
3848 VShiftOpc = ARMISD::VQSHLsu; break;
3849 case Intrinsic::arm_neon_vqshiftns:
3850 VShiftOpc = ARMISD::VQSHRNs; break;
3851 case Intrinsic::arm_neon_vqshiftnu:
3852 VShiftOpc = ARMISD::VQSHRNu; break;
3853 case Intrinsic::arm_neon_vqshiftnsu:
3854 VShiftOpc = ARMISD::VQSHRNsu; break;
3855 case Intrinsic::arm_neon_vqrshiftns:
3856 VShiftOpc = ARMISD::VQRSHRNs; break;
3857 case Intrinsic::arm_neon_vqrshiftnu:
3858 VShiftOpc = ARMISD::VQRSHRNu; break;
3859 case Intrinsic::arm_neon_vqrshiftnsu:
3860 VShiftOpc = ARMISD::VQRSHRNsu; break;
3863 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
3864 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
3867 case Intrinsic::arm_neon_vshiftins: {
3868 EVT VT = N->getOperand(1).getValueType();
3870 unsigned VShiftOpc = 0;
3872 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
3873 VShiftOpc = ARMISD::VSLI;
3874 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
3875 VShiftOpc = ARMISD::VSRI;
3877 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
3880 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
3881 N->getOperand(1), N->getOperand(2),
3882 DAG.getConstant(Cnt, MVT::i32));
3885 case Intrinsic::arm_neon_vqrshifts:
3886 case Intrinsic::arm_neon_vqrshiftu:
3887 // No immediate versions of these to check for.
3894 /// PerformShiftCombine - Checks for immediate versions of vector shifts and
3895 /// lowers them. As with the vector shift intrinsics, this is done during DAG
3896 /// combining instead of DAG legalizing because the build_vectors for 64-bit
3897 /// vector element shift counts are generally not legal, and it is hard to see
3898 /// their values after they get legalized to loads from a constant pool.
3899 static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
3900 const ARMSubtarget *ST) {
3901 EVT VT = N->getValueType(0);
3903 // Nothing to be done for scalar shifts.
3904 if (! VT.isVector())
3907 assert(ST->hasNEON() && "unexpected vector shift");
3910 switch (N->getOpcode()) {
3911 default: llvm_unreachable("unexpected shift opcode");
3914 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
3915 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
3916 DAG.getConstant(Cnt, MVT::i32));
3921 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
3922 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
3923 ARMISD::VSHRs : ARMISD::VSHRu);
3924 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
3925 DAG.getConstant(Cnt, MVT::i32));
3931 /// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
3932 /// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
3933 static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
3934 const ARMSubtarget *ST) {
3935 SDValue N0 = N->getOperand(0);
3937 // Check for sign- and zero-extensions of vector extract operations of 8-
3938 // and 16-bit vector elements. NEON supports these directly. They are
3939 // handled during DAG combining because type legalization will promote them
3940 // to 32-bit types and it is messy to recognize the operations after that.
3941 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
3942 SDValue Vec = N0.getOperand(0);
3943 SDValue Lane = N0.getOperand(1);
3944 EVT VT = N->getValueType(0);
3945 EVT EltVT = N0.getValueType();
3946 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3948 if (VT == MVT::i32 &&
3949 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
3950 TLI.isTypeLegal(Vec.getValueType())) {
3953 switch (N->getOpcode()) {
3954 default: llvm_unreachable("unexpected opcode");
3955 case ISD::SIGN_EXTEND:
3956 Opc = ARMISD::VGETLANEs;
3958 case ISD::ZERO_EXTEND:
3959 case ISD::ANY_EXTEND:
3960 Opc = ARMISD::VGETLANEu;
3963 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
3970 /// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
3971 /// to match f32 max/min patterns to use NEON vmax/vmin instructions.
3972 static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
3973 const ARMSubtarget *ST) {
3974 // If the target supports NEON, try to use vmax/vmin instructions for f32
3975 // selects like "x < y ? x : y". Unless the FiniteOnlyFPMath option is set,
3976 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
3977 // a NaN; only do the transformation when it matches that behavior.
3979 // For now only do this when using NEON for FP operations; if using VFP, it
3980 // is not obvious that the benefit outweighs the cost of switching to the
3982 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
3983 N->getValueType(0) != MVT::f32)
3986 SDValue CondLHS = N->getOperand(0);
3987 SDValue CondRHS = N->getOperand(1);
3988 SDValue LHS = N->getOperand(2);
3989 SDValue RHS = N->getOperand(3);
3990 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
3992 unsigned Opcode = 0;
3994 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
3995 IsReversed = false; // x CC y ? x : y
3996 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
3997 IsReversed = true ; // x CC y ? y : x
4011 // If LHS is NaN, an ordered comparison will be false and the result will
4012 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
4013 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4014 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
4015 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4017 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
4018 // will return -0, so vmin can only be used for unsafe math or if one of
4019 // the operands is known to be nonzero.
4020 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
4022 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4024 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
4033 // If LHS is NaN, an ordered comparison will be false and the result will
4034 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
4035 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
4036 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
4037 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
4039 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
4040 // will return +0, so vmax can only be used for unsafe math or if one of
4041 // the operands is known to be nonzero.
4042 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
4044 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
4046 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
4052 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
4055 SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
4056 DAGCombinerInfo &DCI) const {
4057 switch (N->getOpcode()) {
4059 case ISD::ADD: return PerformADDCombine(N, DCI);
4060 case ISD::SUB: return PerformSUBCombine(N, DCI);
4061 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
4062 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
4063 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
4066 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
4067 case ISD::SIGN_EXTEND:
4068 case ISD::ZERO_EXTEND:
4069 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
4070 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
4075 bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
4076 if (!Subtarget->hasV6Ops())
4077 // Pre-v6 does not support unaligned mem access.
4080 // v6+ may or may not support unaligned mem access depending on the system
4082 // FIXME: This is pretty conservative. Should we provide cmdline option to
4083 // control the behaviour?
4084 if (!Subtarget->isTargetDarwin())
4088 switch (VT.getSimpleVT().SimpleTy) {
4095 // FIXME: VLD1 etc with standard alignment is legal.
4099 static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
4104 switch (VT.getSimpleVT().SimpleTy) {
4105 default: return false;
4120 if ((V & (Scale - 1)) != 0)
4123 return V == (V & ((1LL << 5) - 1));
4126 static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
4127 const ARMSubtarget *Subtarget) {
4134 switch (VT.getSimpleVT().SimpleTy) {
4135 default: return false;
4140 // + imm12 or - imm8
4142 return V == (V & ((1LL << 8) - 1));
4143 return V == (V & ((1LL << 12) - 1));
4146 // Same as ARM mode. FIXME: NEON?
4147 if (!Subtarget->hasVFP2())
4152 return V == (V & ((1LL << 8) - 1));
4156 /// isLegalAddressImmediate - Return true if the integer value can be used
4157 /// as the offset of the target addressing mode for load / store of the
4159 static bool isLegalAddressImmediate(int64_t V, EVT VT,
4160 const ARMSubtarget *Subtarget) {
4167 if (Subtarget->isThumb1Only())
4168 return isLegalT1AddressImmediate(V, VT);
4169 else if (Subtarget->isThumb2())
4170 return isLegalT2AddressImmediate(V, VT, Subtarget);
4175 switch (VT.getSimpleVT().SimpleTy) {
4176 default: return false;
4181 return V == (V & ((1LL << 12) - 1));
4184 return V == (V & ((1LL << 8) - 1));
4187 if (!Subtarget->hasVFP2()) // FIXME: NEON?
4192 return V == (V & ((1LL << 8) - 1));
4196 bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
4198 int Scale = AM.Scale;
4202 switch (VT.getSimpleVT().SimpleTy) {
4203 default: return false;
4212 return Scale == 2 || Scale == 4 || Scale == 8;
4215 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
4219 // Note, we allow "void" uses (basically, uses that aren't loads or
4220 // stores), because arm allows folding a scale into many arithmetic
4221 // operations. This should be made more precise and revisited later.
4223 // Allow r << imm, but the imm has to be a multiple of two.
4224 if (Scale & 1) return false;
4225 return isPowerOf2_32(Scale);
4229 /// isLegalAddressingMode - Return true if the addressing mode represented
4230 /// by AM is legal for this target, for a load/store of the specified type.
4231 bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
4232 const Type *Ty) const {
4233 EVT VT = getValueType(Ty, true);
4234 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
4237 // Can never fold addr of global into load/store.
4242 case 0: // no scale reg, must be "r+i" or "r", or "i".
4245 if (Subtarget->isThumb1Only())
4249 // ARM doesn't support any R+R*scale+imm addr modes.
4256 if (Subtarget->isThumb2())
4257 return isLegalT2ScaledAddressingMode(AM, VT);
4259 int Scale = AM.Scale;
4260 switch (VT.getSimpleVT().SimpleTy) {
4261 default: return false;
4265 if (Scale < 0) Scale = -Scale;
4269 return isPowerOf2_32(Scale & ~1);
4273 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
4278 // Note, we allow "void" uses (basically, uses that aren't loads or
4279 // stores), because arm allows folding a scale into many arithmetic
4280 // operations. This should be made more precise and revisited later.
4282 // Allow r << imm, but the imm has to be a multiple of two.
4283 if (Scale & 1) return false;
4284 return isPowerOf2_32(Scale);
4291 /// isLegalICmpImmediate - Return true if the specified immediate is legal
4292 /// icmp immediate, that is the target has icmp instructions which can compare
4293 /// a register against the immediate without having to materialize the
4294 /// immediate into a register.
4295 bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
4296 if (!Subtarget->isThumb())
4297 return ARM_AM::getSOImmVal(Imm) != -1;
4298 if (Subtarget->isThumb2())
4299 return ARM_AM::getT2SOImmVal(Imm) != -1;
4300 return Imm >= 0 && Imm <= 255;
4303 static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
4304 bool isSEXTLoad, SDValue &Base,
4305 SDValue &Offset, bool &isInc,
4306 SelectionDAG &DAG) {
4307 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4310 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
4312 Base = Ptr->getOperand(0);
4313 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
4314 int RHSC = (int)RHS->getZExtValue();
4315 if (RHSC < 0 && RHSC > -256) {
4316 assert(Ptr->getOpcode() == ISD::ADD);
4318 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4322 isInc = (Ptr->getOpcode() == ISD::ADD);
4323 Offset = Ptr->getOperand(1);
4325 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
4327 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
4328 int RHSC = (int)RHS->getZExtValue();
4329 if (RHSC < 0 && RHSC > -0x1000) {
4330 assert(Ptr->getOpcode() == ISD::ADD);
4332 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4333 Base = Ptr->getOperand(0);
4338 if (Ptr->getOpcode() == ISD::ADD) {
4340 ARM_AM::ShiftOpc ShOpcVal= ARM_AM::getShiftOpcForNode(Ptr->getOperand(0));
4341 if (ShOpcVal != ARM_AM::no_shift) {
4342 Base = Ptr->getOperand(1);
4343 Offset = Ptr->getOperand(0);
4345 Base = Ptr->getOperand(0);
4346 Offset = Ptr->getOperand(1);
4351 isInc = (Ptr->getOpcode() == ISD::ADD);
4352 Base = Ptr->getOperand(0);
4353 Offset = Ptr->getOperand(1);
4357 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
4361 static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
4362 bool isSEXTLoad, SDValue &Base,
4363 SDValue &Offset, bool &isInc,
4364 SelectionDAG &DAG) {
4365 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
4368 Base = Ptr->getOperand(0);
4369 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
4370 int RHSC = (int)RHS->getZExtValue();
4371 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
4372 assert(Ptr->getOpcode() == ISD::ADD);
4374 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
4376 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
4377 isInc = Ptr->getOpcode() == ISD::ADD;
4378 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
4386 /// getPreIndexedAddressParts - returns true by value, base pointer and
4387 /// offset pointer and addressing mode by reference if the node's address
4388 /// can be legally represented as pre-indexed load / store address.
4390 ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
4392 ISD::MemIndexedMode &AM,
4393 SelectionDAG &DAG) const {
4394 if (Subtarget->isThumb1Only())
4399 bool isSEXTLoad = false;
4400 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4401 Ptr = LD->getBasePtr();
4402 VT = LD->getMemoryVT();
4403 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4404 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
4405 Ptr = ST->getBasePtr();
4406 VT = ST->getMemoryVT();
4411 bool isLegal = false;
4412 if (Subtarget->isThumb2())
4413 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
4414 Offset, isInc, DAG);
4416 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
4417 Offset, isInc, DAG);
4421 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
4425 /// getPostIndexedAddressParts - returns true by value, base pointer and
4426 /// offset pointer and addressing mode by reference if this node can be
4427 /// combined with a load / store to form a post-indexed load / store.
4428 bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
4431 ISD::MemIndexedMode &AM,
4432 SelectionDAG &DAG) const {
4433 if (Subtarget->isThumb1Only())
4438 bool isSEXTLoad = false;
4439 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
4440 VT = LD->getMemoryVT();
4441 Ptr = LD->getBasePtr();
4442 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
4443 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
4444 VT = ST->getMemoryVT();
4445 Ptr = ST->getBasePtr();
4450 bool isLegal = false;
4451 if (Subtarget->isThumb2())
4452 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
4455 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
4461 // Swap base ptr and offset to catch more post-index load / store when
4462 // it's legal. In Thumb2 mode, offset must be an immediate.
4463 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
4464 !Subtarget->isThumb2())
4465 std::swap(Base, Offset);
4467 // Post-indexed load / store update the base pointer.
4472 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
4476 void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
4480 const SelectionDAG &DAG,
4481 unsigned Depth) const {
4482 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
4483 switch (Op.getOpcode()) {
4485 case ARMISD::CMOV: {
4486 // Bits are known zero/one if known on the LHS and RHS.
4487 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
4488 if (KnownZero == 0 && KnownOne == 0) return;
4490 APInt KnownZeroRHS, KnownOneRHS;
4491 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
4492 KnownZeroRHS, KnownOneRHS, Depth+1);
4493 KnownZero &= KnownZeroRHS;
4494 KnownOne &= KnownOneRHS;
4500 //===----------------------------------------------------------------------===//
4501 // ARM Inline Assembly Support
4502 //===----------------------------------------------------------------------===//
4504 /// getConstraintType - Given a constraint letter, return the type of
4505 /// constraint it is for this target.
4506 ARMTargetLowering::ConstraintType
4507 ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
4508 if (Constraint.size() == 1) {
4509 switch (Constraint[0]) {
4511 case 'l': return C_RegisterClass;
4512 case 'w': return C_RegisterClass;
4515 return TargetLowering::getConstraintType(Constraint);
4518 std::pair<unsigned, const TargetRegisterClass*>
4519 ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
4521 if (Constraint.size() == 1) {
4522 // GCC ARM Constraint Letters
4523 switch (Constraint[0]) {
4525 if (Subtarget->isThumb())
4526 return std::make_pair(0U, ARM::tGPRRegisterClass);
4528 return std::make_pair(0U, ARM::GPRRegisterClass);
4530 return std::make_pair(0U, ARM::GPRRegisterClass);
4533 return std::make_pair(0U, ARM::SPRRegisterClass);
4534 if (VT.getSizeInBits() == 64)
4535 return std::make_pair(0U, ARM::DPRRegisterClass);
4536 if (VT.getSizeInBits() == 128)
4537 return std::make_pair(0U, ARM::QPRRegisterClass);
4541 if (StringRef("{cc}").equals_lower(Constraint))
4542 return std::make_pair(0U, ARM::CCRRegisterClass);
4544 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
4547 std::vector<unsigned> ARMTargetLowering::
4548 getRegClassForInlineAsmConstraint(const std::string &Constraint,
4550 if (Constraint.size() != 1)
4551 return std::vector<unsigned>();
4553 switch (Constraint[0]) { // GCC ARM Constraint Letters
4556 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
4557 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
4560 return make_vector<unsigned>(ARM::R0, ARM::R1, ARM::R2, ARM::R3,
4561 ARM::R4, ARM::R5, ARM::R6, ARM::R7,
4562 ARM::R8, ARM::R9, ARM::R10, ARM::R11,
4563 ARM::R12, ARM::LR, 0);
4566 return make_vector<unsigned>(ARM::S0, ARM::S1, ARM::S2, ARM::S3,
4567 ARM::S4, ARM::S5, ARM::S6, ARM::S7,
4568 ARM::S8, ARM::S9, ARM::S10, ARM::S11,
4569 ARM::S12,ARM::S13,ARM::S14,ARM::S15,
4570 ARM::S16,ARM::S17,ARM::S18,ARM::S19,
4571 ARM::S20,ARM::S21,ARM::S22,ARM::S23,
4572 ARM::S24,ARM::S25,ARM::S26,ARM::S27,
4573 ARM::S28,ARM::S29,ARM::S30,ARM::S31, 0);
4574 if (VT.getSizeInBits() == 64)
4575 return make_vector<unsigned>(ARM::D0, ARM::D1, ARM::D2, ARM::D3,
4576 ARM::D4, ARM::D5, ARM::D6, ARM::D7,
4577 ARM::D8, ARM::D9, ARM::D10,ARM::D11,
4578 ARM::D12,ARM::D13,ARM::D14,ARM::D15, 0);
4579 if (VT.getSizeInBits() == 128)
4580 return make_vector<unsigned>(ARM::Q0, ARM::Q1, ARM::Q2, ARM::Q3,
4581 ARM::Q4, ARM::Q5, ARM::Q6, ARM::Q7, 0);
4585 return std::vector<unsigned>();
4588 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
4589 /// vector. If it is invalid, don't add anything to Ops.
4590 void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
4593 std::vector<SDValue>&Ops,
4594 SelectionDAG &DAG) const {
4595 SDValue Result(0, 0);
4597 switch (Constraint) {
4599 case 'I': case 'J': case 'K': case 'L':
4600 case 'M': case 'N': case 'O':
4601 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
4605 int64_t CVal64 = C->getSExtValue();
4606 int CVal = (int) CVal64;
4607 // None of these constraints allow values larger than 32 bits. Check
4608 // that the value fits in an int.
4612 switch (Constraint) {
4614 if (Subtarget->isThumb1Only()) {
4615 // This must be a constant between 0 and 255, for ADD
4617 if (CVal >= 0 && CVal <= 255)
4619 } else if (Subtarget->isThumb2()) {
4620 // A constant that can be used as an immediate value in a
4621 // data-processing instruction.
4622 if (ARM_AM::getT2SOImmVal(CVal) != -1)
4625 // A constant that can be used as an immediate value in a
4626 // data-processing instruction.
4627 if (ARM_AM::getSOImmVal(CVal) != -1)
4633 if (Subtarget->isThumb()) { // FIXME thumb2
4634 // This must be a constant between -255 and -1, for negated ADD
4635 // immediates. This can be used in GCC with an "n" modifier that
4636 // prints the negated value, for use with SUB instructions. It is
4637 // not useful otherwise but is implemented for compatibility.
4638 if (CVal >= -255 && CVal <= -1)
4641 // This must be a constant between -4095 and 4095. It is not clear
4642 // what this constraint is intended for. Implemented for
4643 // compatibility with GCC.
4644 if (CVal >= -4095 && CVal <= 4095)
4650 if (Subtarget->isThumb1Only()) {
4651 // A 32-bit value where only one byte has a nonzero value. Exclude
4652 // zero to match GCC. This constraint is used by GCC internally for
4653 // constants that can be loaded with a move/shift combination.
4654 // It is not useful otherwise but is implemented for compatibility.
4655 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
4657 } else if (Subtarget->isThumb2()) {
4658 // A constant whose bitwise inverse can be used as an immediate
4659 // value in a data-processing instruction. This can be used in GCC
4660 // with a "B" modifier that prints the inverted value, for use with
4661 // BIC and MVN instructions. It is not useful otherwise but is
4662 // implemented for compatibility.
4663 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
4666 // A constant whose bitwise inverse can be used as an immediate
4667 // value in a data-processing instruction. This can be used in GCC
4668 // with a "B" modifier that prints the inverted value, for use with
4669 // BIC and MVN instructions. It is not useful otherwise but is
4670 // implemented for compatibility.
4671 if (ARM_AM::getSOImmVal(~CVal) != -1)
4677 if (Subtarget->isThumb1Only()) {
4678 // This must be a constant between -7 and 7,
4679 // for 3-operand ADD/SUB immediate instructions.
4680 if (CVal >= -7 && CVal < 7)
4682 } else if (Subtarget->isThumb2()) {
4683 // A constant whose negation can be used as an immediate value in a
4684 // data-processing instruction. This can be used in GCC with an "n"
4685 // modifier that prints the negated value, for use with SUB
4686 // instructions. It is not useful otherwise but is implemented for
4688 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
4691 // A constant whose negation can be used as an immediate value in a
4692 // data-processing instruction. This can be used in GCC with an "n"
4693 // modifier that prints the negated value, for use with SUB
4694 // instructions. It is not useful otherwise but is implemented for
4696 if (ARM_AM::getSOImmVal(-CVal) != -1)
4702 if (Subtarget->isThumb()) { // FIXME thumb2
4703 // This must be a multiple of 4 between 0 and 1020, for
4704 // ADD sp + immediate.
4705 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
4708 // A power of two or a constant between 0 and 32. This is used in
4709 // GCC for the shift amount on shifted register operands, but it is
4710 // useful in general for any shift amounts.
4711 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
4717 if (Subtarget->isThumb()) { // FIXME thumb2
4718 // This must be a constant between 0 and 31, for shift amounts.
4719 if (CVal >= 0 && CVal <= 31)
4725 if (Subtarget->isThumb()) { // FIXME thumb2
4726 // This must be a multiple of 4 between -508 and 508, for
4727 // ADD/SUB sp = sp + immediate.
4728 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
4733 Result = DAG.getTargetConstant(CVal, Op.getValueType());
4737 if (Result.getNode()) {
4738 Ops.push_back(Result);
4741 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
4746 ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
4747 // The ARM target isn't yet aware of offsets.
4751 int ARM::getVFPf32Imm(const APFloat &FPImm) {
4752 APInt Imm = FPImm.bitcastToAPInt();
4753 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
4754 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
4755 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
4757 // We can handle 4 bits of mantissa.
4758 // mantissa = (16+UInt(e:f:g:h))/16.
4759 if (Mantissa & 0x7ffff)
4762 if ((Mantissa & 0xf) != Mantissa)
4765 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4766 if (Exp < -3 || Exp > 4)
4768 Exp = ((Exp+3) & 0x7) ^ 4;
4770 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4773 int ARM::getVFPf64Imm(const APFloat &FPImm) {
4774 APInt Imm = FPImm.bitcastToAPInt();
4775 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
4776 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
4777 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
4779 // We can handle 4 bits of mantissa.
4780 // mantissa = (16+UInt(e:f:g:h))/16.
4781 if (Mantissa & 0xffffffffffffLL)
4784 if ((Mantissa & 0xf) != Mantissa)
4787 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
4788 if (Exp < -3 || Exp > 4)
4790 Exp = ((Exp+3) & 0x7) ^ 4;
4792 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
4795 /// isFPImmLegal - Returns true if the target can instruction select the
4796 /// specified FP immediate natively. If false, the legalizer will
4797 /// materialize the FP immediate as a load from a constant pool.
4798 bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
4799 if (!Subtarget->hasVFP3())
4802 return ARM::getVFPf32Imm(Imm) != -1;
4804 return ARM::getVFPf64Imm(Imm) != -1;