1 //===-- AArch64ISelLowering.cpp - AArch64 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 AArch64 uses to lower LLVM code into a
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "aarch64-isel"
17 #include "AArch64ISelLowering.h"
18 #include "AArch64MachineFunctionInfo.h"
19 #include "AArch64TargetMachine.h"
20 #include "AArch64TargetObjectFile.h"
21 #include "Utils/AArch64BaseInfo.h"
22 #include "llvm/CodeGen/Analysis.h"
23 #include "llvm/CodeGen/CallingConvLower.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/MachineInstrBuilder.h"
26 #include "llvm/CodeGen/MachineRegisterInfo.h"
27 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
28 #include "llvm/IR/CallingConv.h"
32 static TargetLoweringObjectFile *createTLOF(AArch64TargetMachine &TM) {
33 assert (TM.getSubtarget<AArch64Subtarget>().isTargetELF() &&
34 "unknown subtarget type");
35 return new AArch64ElfTargetObjectFile();
38 AArch64TargetLowering::AArch64TargetLowering(AArch64TargetMachine &TM)
39 : TargetLowering(TM, createTLOF(TM)), Itins(TM.getInstrItineraryData()) {
41 const AArch64Subtarget *Subtarget = &TM.getSubtarget<AArch64Subtarget>();
43 // SIMD compares set the entire lane's bits to 1
44 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
46 // Scalar register <-> type mapping
47 addRegisterClass(MVT::i32, &AArch64::GPR32RegClass);
48 addRegisterClass(MVT::i64, &AArch64::GPR64RegClass);
50 if (Subtarget->hasFPARMv8()) {
51 addRegisterClass(MVT::f16, &AArch64::FPR16RegClass);
52 addRegisterClass(MVT::f32, &AArch64::FPR32RegClass);
53 addRegisterClass(MVT::f64, &AArch64::FPR64RegClass);
54 addRegisterClass(MVT::f128, &AArch64::FPR128RegClass);
57 if (Subtarget->hasNEON()) {
59 addRegisterClass(MVT::v1i8, &AArch64::FPR8RegClass);
60 addRegisterClass(MVT::v1i16, &AArch64::FPR16RegClass);
61 addRegisterClass(MVT::v1i32, &AArch64::FPR32RegClass);
62 addRegisterClass(MVT::v1i64, &AArch64::FPR64RegClass);
63 addRegisterClass(MVT::v1f64, &AArch64::FPR64RegClass);
64 addRegisterClass(MVT::v8i8, &AArch64::FPR64RegClass);
65 addRegisterClass(MVT::v4i16, &AArch64::FPR64RegClass);
66 addRegisterClass(MVT::v2i32, &AArch64::FPR64RegClass);
67 addRegisterClass(MVT::v1i64, &AArch64::FPR64RegClass);
68 addRegisterClass(MVT::v2f32, &AArch64::FPR64RegClass);
69 addRegisterClass(MVT::v16i8, &AArch64::FPR128RegClass);
70 addRegisterClass(MVT::v8i16, &AArch64::FPR128RegClass);
71 addRegisterClass(MVT::v4i32, &AArch64::FPR128RegClass);
72 addRegisterClass(MVT::v2i64, &AArch64::FPR128RegClass);
73 addRegisterClass(MVT::v4f32, &AArch64::FPR128RegClass);
74 addRegisterClass(MVT::v2f64, &AArch64::FPR128RegClass);
77 computeRegisterProperties();
79 // We combine OR nodes for bitfield and NEON BSL operations.
80 setTargetDAGCombine(ISD::OR);
82 setTargetDAGCombine(ISD::AND);
83 setTargetDAGCombine(ISD::SRA);
84 setTargetDAGCombine(ISD::SRL);
85 setTargetDAGCombine(ISD::SHL);
87 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
88 setTargetDAGCombine(ISD::INTRINSIC_VOID);
89 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
91 // AArch64 does not have i1 loads, or much of anything for i1 really.
92 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
93 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
94 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
96 setStackPointerRegisterToSaveRestore(AArch64::XSP);
97 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
98 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
99 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
101 // We'll lower globals to wrappers for selection.
102 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
103 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
105 // A64 instructions have the comparison predicate attached to the user of the
106 // result, but having a separate comparison is valuable for matching.
107 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
108 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
109 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
110 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
112 setOperationAction(ISD::SELECT, MVT::i32, Custom);
113 setOperationAction(ISD::SELECT, MVT::i64, Custom);
114 setOperationAction(ISD::SELECT, MVT::f32, Custom);
115 setOperationAction(ISD::SELECT, MVT::f64, Custom);
117 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
118 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
119 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
120 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
122 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
124 setOperationAction(ISD::SETCC, MVT::i32, Custom);
125 setOperationAction(ISD::SETCC, MVT::i64, Custom);
126 setOperationAction(ISD::SETCC, MVT::f32, Custom);
127 setOperationAction(ISD::SETCC, MVT::f64, Custom);
129 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
130 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
131 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
133 setOperationAction(ISD::VASTART, MVT::Other, Custom);
134 setOperationAction(ISD::VACOPY, MVT::Other, Custom);
135 setOperationAction(ISD::VAEND, MVT::Other, Expand);
136 setOperationAction(ISD::VAARG, MVT::Other, Expand);
138 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
139 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
141 setOperationAction(ISD::ROTL, MVT::i32, Expand);
142 setOperationAction(ISD::ROTL, MVT::i64, Expand);
144 setOperationAction(ISD::UREM, MVT::i32, Expand);
145 setOperationAction(ISD::UREM, MVT::i64, Expand);
146 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
147 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
149 setOperationAction(ISD::SREM, MVT::i32, Expand);
150 setOperationAction(ISD::SREM, MVT::i64, Expand);
151 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
152 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
154 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
155 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
156 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
157 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
159 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
160 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
162 // Legal floating-point operations.
163 setOperationAction(ISD::FABS, MVT::f32, Legal);
164 setOperationAction(ISD::FABS, MVT::f64, Legal);
166 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
167 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
169 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
170 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
172 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
173 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
175 setOperationAction(ISD::FNEG, MVT::f32, Legal);
176 setOperationAction(ISD::FNEG, MVT::f64, Legal);
178 setOperationAction(ISD::FRINT, MVT::f32, Legal);
179 setOperationAction(ISD::FRINT, MVT::f64, Legal);
181 setOperationAction(ISD::FSQRT, MVT::f32, Legal);
182 setOperationAction(ISD::FSQRT, MVT::f64, Legal);
184 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
185 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
187 setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
188 setOperationAction(ISD::ConstantFP, MVT::f64, Legal);
189 setOperationAction(ISD::ConstantFP, MVT::f128, Legal);
191 // Illegal floating-point operations.
192 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
193 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
195 setOperationAction(ISD::FCOS, MVT::f32, Expand);
196 setOperationAction(ISD::FCOS, MVT::f64, Expand);
198 setOperationAction(ISD::FEXP, MVT::f32, Expand);
199 setOperationAction(ISD::FEXP, MVT::f64, Expand);
201 setOperationAction(ISD::FEXP2, MVT::f32, Expand);
202 setOperationAction(ISD::FEXP2, MVT::f64, Expand);
204 setOperationAction(ISD::FLOG, MVT::f32, Expand);
205 setOperationAction(ISD::FLOG, MVT::f64, Expand);
207 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
208 setOperationAction(ISD::FLOG2, MVT::f64, Expand);
210 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
211 setOperationAction(ISD::FLOG10, MVT::f64, Expand);
213 setOperationAction(ISD::FPOW, MVT::f32, Expand);
214 setOperationAction(ISD::FPOW, MVT::f64, Expand);
216 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
217 setOperationAction(ISD::FPOWI, MVT::f64, Expand);
219 setOperationAction(ISD::FREM, MVT::f32, Expand);
220 setOperationAction(ISD::FREM, MVT::f64, Expand);
222 setOperationAction(ISD::FSIN, MVT::f32, Expand);
223 setOperationAction(ISD::FSIN, MVT::f64, Expand);
225 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
226 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
228 // Virtually no operation on f128 is legal, but LLVM can't expand them when
229 // there's a valid register class, so we need custom operations in most cases.
230 setOperationAction(ISD::FABS, MVT::f128, Expand);
231 setOperationAction(ISD::FADD, MVT::f128, Custom);
232 setOperationAction(ISD::FCOPYSIGN, MVT::f128, Expand);
233 setOperationAction(ISD::FCOS, MVT::f128, Expand);
234 setOperationAction(ISD::FDIV, MVT::f128, Custom);
235 setOperationAction(ISD::FMA, MVT::f128, Expand);
236 setOperationAction(ISD::FMUL, MVT::f128, Custom);
237 setOperationAction(ISD::FNEG, MVT::f128, Expand);
238 setOperationAction(ISD::FP_EXTEND, MVT::f128, Expand);
239 setOperationAction(ISD::FP_ROUND, MVT::f128, Expand);
240 setOperationAction(ISD::FPOW, MVT::f128, Expand);
241 setOperationAction(ISD::FREM, MVT::f128, Expand);
242 setOperationAction(ISD::FRINT, MVT::f128, Expand);
243 setOperationAction(ISD::FSIN, MVT::f128, Expand);
244 setOperationAction(ISD::FSINCOS, MVT::f128, Expand);
245 setOperationAction(ISD::FSQRT, MVT::f128, Expand);
246 setOperationAction(ISD::FSUB, MVT::f128, Custom);
247 setOperationAction(ISD::FTRUNC, MVT::f128, Expand);
248 setOperationAction(ISD::SETCC, MVT::f128, Custom);
249 setOperationAction(ISD::BR_CC, MVT::f128, Custom);
250 setOperationAction(ISD::SELECT, MVT::f128, Expand);
251 setOperationAction(ISD::SELECT_CC, MVT::f128, Custom);
252 setOperationAction(ISD::FP_EXTEND, MVT::f128, Custom);
254 // Lowering for many of the conversions is actually specified by the non-f128
255 // type. The LowerXXX function will be trivial when f128 isn't involved.
256 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
257 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
258 setOperationAction(ISD::FP_TO_SINT, MVT::i128, Custom);
259 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
260 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
261 setOperationAction(ISD::FP_TO_UINT, MVT::i128, Custom);
262 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
263 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
264 setOperationAction(ISD::SINT_TO_FP, MVT::i128, Custom);
265 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
266 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
267 setOperationAction(ISD::UINT_TO_FP, MVT::i128, Custom);
268 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
269 setOperationAction(ISD::FP_ROUND, MVT::f64, Custom);
271 // This prevents LLVM trying to compress double constants into a floating
272 // constant-pool entry and trying to load from there. It's of doubtful benefit
273 // for A64: we'd need LDR followed by FCVT, I believe.
274 setLoadExtAction(ISD::EXTLOAD, MVT::f64, Expand);
275 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
276 setLoadExtAction(ISD::EXTLOAD, MVT::f16, Expand);
278 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
279 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
280 setTruncStoreAction(MVT::f128, MVT::f16, Expand);
281 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
282 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
283 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
285 setExceptionPointerRegister(AArch64::X0);
286 setExceptionSelectorRegister(AArch64::X1);
288 if (Subtarget->hasNEON()) {
289 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i8, Custom);
290 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
291 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
292 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i16, Custom);
293 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
294 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
295 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i32, Custom);
296 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
297 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
298 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
299 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
300 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
301 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
302 setOperationAction(ISD::BUILD_VECTOR, MVT::v1f64, Custom);
303 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
305 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
306 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
307 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
308 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i16, Custom);
309 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
310 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i32, Custom);
311 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
312 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
313 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f32, Custom);
314 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
315 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1f64, Custom);
316 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
318 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Legal);
319 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Legal);
320 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Legal);
321 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Legal);
322 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Legal);
323 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Legal);
324 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Legal);
325 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f32, Legal);
326 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Legal);
328 setOperationAction(ISD::SETCC, MVT::v8i8, Custom);
329 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
330 setOperationAction(ISD::SETCC, MVT::v4i16, Custom);
331 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
332 setOperationAction(ISD::SETCC, MVT::v2i32, Custom);
333 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
334 setOperationAction(ISD::SETCC, MVT::v1i64, Custom);
335 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
336 setOperationAction(ISD::SETCC, MVT::v2f32, Custom);
337 setOperationAction(ISD::SETCC, MVT::v4f32, Custom);
338 setOperationAction(ISD::SETCC, MVT::v1f64, Custom);
339 setOperationAction(ISD::SETCC, MVT::v2f64, Custom);
341 setOperationAction(ISD::FFLOOR, MVT::v2f32, Legal);
342 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
343 setOperationAction(ISD::FFLOOR, MVT::v1f64, Legal);
344 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
346 setOperationAction(ISD::FCEIL, MVT::v2f32, Legal);
347 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
348 setOperationAction(ISD::FCEIL, MVT::v1f64, Legal);
349 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
351 setOperationAction(ISD::FTRUNC, MVT::v2f32, Legal);
352 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
353 setOperationAction(ISD::FTRUNC, MVT::v1f64, Legal);
354 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
356 setOperationAction(ISD::FRINT, MVT::v2f32, Legal);
357 setOperationAction(ISD::FRINT, MVT::v4f32, Legal);
358 setOperationAction(ISD::FRINT, MVT::v1f64, Legal);
359 setOperationAction(ISD::FRINT, MVT::v2f64, Legal);
361 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Legal);
362 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
363 setOperationAction(ISD::FNEARBYINT, MVT::v1f64, Legal);
364 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
366 setOperationAction(ISD::FROUND, MVT::v2f32, Legal);
367 setOperationAction(ISD::FROUND, MVT::v4f32, Legal);
368 setOperationAction(ISD::FROUND, MVT::v1f64, Legal);
369 setOperationAction(ISD::FROUND, MVT::v2f64, Legal);
371 // Vector ExtLoad and TruncStore are expanded.
372 for (unsigned I = MVT::FIRST_VECTOR_VALUETYPE;
373 I <= MVT::LAST_VECTOR_VALUETYPE; ++I) {
374 MVT VT = (MVT::SimpleValueType) I;
375 setLoadExtAction(ISD::SEXTLOAD, VT, Expand);
376 setLoadExtAction(ISD::ZEXTLOAD, VT, Expand);
377 setLoadExtAction(ISD::EXTLOAD, VT, Expand);
378 for (unsigned II = MVT::FIRST_VECTOR_VALUETYPE;
379 II <= MVT::LAST_VECTOR_VALUETYPE; ++II) {
380 MVT VT1 = (MVT::SimpleValueType) II;
381 // A TruncStore has two vector types of the same number of elements
382 // and different element sizes.
383 if (VT.getVectorNumElements() == VT1.getVectorNumElements() &&
384 VT.getVectorElementType().getSizeInBits()
385 > VT1.getVectorElementType().getSizeInBits())
386 setTruncStoreAction(VT, VT1, Expand);
390 // There is no v1i64/v2i64 multiply, expand v1i64/v2i64 to GPR i64 multiply.
391 // FIXME: For a v2i64 multiply, we copy VPR to GPR and do 2 i64 multiplies,
392 // and then copy back to VPR. This solution may be optimized by Following 3
393 // NEON instructions:
394 // pmull v2.1q, v0.1d, v1.1d
395 // pmull2 v3.1q, v0.2d, v1.2d
396 // ins v2.d[1], v3.d[0]
397 // As currently we can't verify the correctness of such assumption, we can
398 // do such optimization in the future.
399 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
400 setOperationAction(ISD::MUL, MVT::v2i64, Expand);
404 EVT AArch64TargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
405 // It's reasonably important that this value matches the "natural" legal
406 // promotion from i1 for scalar types. Otherwise LegalizeTypes can get itself
407 // in a twist (e.g. inserting an any_extend which then becomes i64 -> i64).
408 if (!VT.isVector()) return MVT::i32;
409 return VT.changeVectorElementTypeToInteger();
412 static void getExclusiveOperation(unsigned Size, AtomicOrdering Ord,
415 static const unsigned LoadBares[] = {AArch64::LDXR_byte, AArch64::LDXR_hword,
416 AArch64::LDXR_word, AArch64::LDXR_dword};
417 static const unsigned LoadAcqs[] = {AArch64::LDAXR_byte, AArch64::LDAXR_hword,
418 AArch64::LDAXR_word, AArch64::LDAXR_dword};
419 static const unsigned StoreBares[] = {AArch64::STXR_byte, AArch64::STXR_hword,
420 AArch64::STXR_word, AArch64::STXR_dword};
421 static const unsigned StoreRels[] = {AArch64::STLXR_byte,AArch64::STLXR_hword,
422 AArch64::STLXR_word, AArch64::STLXR_dword};
424 const unsigned *LoadOps, *StoreOps;
425 if (Ord == Acquire || Ord == AcquireRelease || Ord == SequentiallyConsistent)
430 if (Ord == Release || Ord == AcquireRelease || Ord == SequentiallyConsistent)
431 StoreOps = StoreRels;
433 StoreOps = StoreBares;
435 assert(isPowerOf2_32(Size) && Size <= 8 &&
436 "unsupported size for atomic binary op!");
438 LdrOpc = LoadOps[Log2_32(Size)];
439 StrOpc = StoreOps[Log2_32(Size)];
442 // FIXME: AArch64::DTripleRegClass and AArch64::QTripleRegClass don't really
443 // have value type mapped, and they are both being defined as MVT::untyped.
444 // Without knowing the MVT type, MachineLICM::getRegisterClassIDAndCost
445 // would fail to figure out the register pressure correctly.
446 std::pair<const TargetRegisterClass*, uint8_t>
447 AArch64TargetLowering::findRepresentativeClass(MVT VT) const{
448 const TargetRegisterClass *RRC = 0;
450 switch (VT.SimpleTy) {
452 return TargetLowering::findRepresentativeClass(VT);
454 RRC = &AArch64::QPairRegClass;
458 RRC = &AArch64::QQuadRegClass;
462 return std::make_pair(RRC, Cost);
466 AArch64TargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
468 unsigned BinOpcode) const {
469 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
470 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
472 const BasicBlock *LLVM_BB = BB->getBasicBlock();
473 MachineFunction *MF = BB->getParent();
474 MachineFunction::iterator It = BB;
477 unsigned dest = MI->getOperand(0).getReg();
478 unsigned ptr = MI->getOperand(1).getReg();
479 unsigned incr = MI->getOperand(2).getReg();
480 AtomicOrdering Ord = static_cast<AtomicOrdering>(MI->getOperand(3).getImm());
481 DebugLoc dl = MI->getDebugLoc();
483 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
485 unsigned ldrOpc, strOpc;
486 getExclusiveOperation(Size, Ord, ldrOpc, strOpc);
488 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
489 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
490 MF->insert(It, loopMBB);
491 MF->insert(It, exitMBB);
493 // Transfer the remainder of BB and its successor edges to exitMBB.
494 exitMBB->splice(exitMBB->begin(), BB,
495 llvm::next(MachineBasicBlock::iterator(MI)),
497 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
499 const TargetRegisterClass *TRC
500 = Size == 8 ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
501 unsigned scratch = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
505 // fallthrough --> loopMBB
506 BB->addSuccessor(loopMBB);
510 // <binop> scratch, dest, incr
511 // stxr stxr_status, scratch, ptr
512 // cbnz stxr_status, loopMBB
513 // fallthrough --> exitMBB
515 BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
517 // All arithmetic operations we'll be creating are designed to take an extra
518 // shift or extend operand, which we can conveniently set to zero.
520 // Operand order needs to go the other way for NAND.
521 if (BinOpcode == AArch64::BICwww_lsl || BinOpcode == AArch64::BICxxx_lsl)
522 BuildMI(BB, dl, TII->get(BinOpcode), scratch)
523 .addReg(incr).addReg(dest).addImm(0);
525 BuildMI(BB, dl, TII->get(BinOpcode), scratch)
526 .addReg(dest).addReg(incr).addImm(0);
529 // From the stxr, the register is GPR32; from the cmp it's GPR32wsp
530 unsigned stxr_status = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
531 MRI.constrainRegClass(stxr_status, &AArch64::GPR32wspRegClass);
533 BuildMI(BB, dl, TII->get(strOpc), stxr_status).addReg(scratch).addReg(ptr);
534 BuildMI(BB, dl, TII->get(AArch64::CBNZw))
535 .addReg(stxr_status).addMBB(loopMBB);
537 BB->addSuccessor(loopMBB);
538 BB->addSuccessor(exitMBB);
544 MI->eraseFromParent(); // The instruction is gone now.
550 AArch64TargetLowering::emitAtomicBinaryMinMax(MachineInstr *MI,
551 MachineBasicBlock *BB,
554 A64CC::CondCodes Cond) const {
555 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
557 const BasicBlock *LLVM_BB = BB->getBasicBlock();
558 MachineFunction *MF = BB->getParent();
559 MachineFunction::iterator It = BB;
562 unsigned dest = MI->getOperand(0).getReg();
563 unsigned ptr = MI->getOperand(1).getReg();
564 unsigned incr = MI->getOperand(2).getReg();
565 AtomicOrdering Ord = static_cast<AtomicOrdering>(MI->getOperand(3).getImm());
567 unsigned oldval = dest;
568 DebugLoc dl = MI->getDebugLoc();
570 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
571 const TargetRegisterClass *TRC, *TRCsp;
573 TRC = &AArch64::GPR64RegClass;
574 TRCsp = &AArch64::GPR64xspRegClass;
576 TRC = &AArch64::GPR32RegClass;
577 TRCsp = &AArch64::GPR32wspRegClass;
580 unsigned ldrOpc, strOpc;
581 getExclusiveOperation(Size, Ord, ldrOpc, strOpc);
583 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
584 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
585 MF->insert(It, loopMBB);
586 MF->insert(It, exitMBB);
588 // Transfer the remainder of BB and its successor edges to exitMBB.
589 exitMBB->splice(exitMBB->begin(), BB,
590 llvm::next(MachineBasicBlock::iterator(MI)),
592 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
594 unsigned scratch = MRI.createVirtualRegister(TRC);
595 MRI.constrainRegClass(scratch, TRCsp);
599 // fallthrough --> loopMBB
600 BB->addSuccessor(loopMBB);
604 // cmp incr, dest (, sign extend if necessary)
605 // csel scratch, dest, incr, cond
606 // stxr stxr_status, scratch, ptr
607 // cbnz stxr_status, loopMBB
608 // fallthrough --> exitMBB
610 BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
612 // Build compare and cmov instructions.
613 MRI.constrainRegClass(incr, TRCsp);
614 BuildMI(BB, dl, TII->get(CmpOp))
615 .addReg(incr).addReg(oldval).addImm(0);
617 BuildMI(BB, dl, TII->get(Size == 8 ? AArch64::CSELxxxc : AArch64::CSELwwwc),
619 .addReg(oldval).addReg(incr).addImm(Cond);
621 unsigned stxr_status = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
622 MRI.constrainRegClass(stxr_status, &AArch64::GPR32wspRegClass);
624 BuildMI(BB, dl, TII->get(strOpc), stxr_status)
625 .addReg(scratch).addReg(ptr);
626 BuildMI(BB, dl, TII->get(AArch64::CBNZw))
627 .addReg(stxr_status).addMBB(loopMBB);
629 BB->addSuccessor(loopMBB);
630 BB->addSuccessor(exitMBB);
636 MI->eraseFromParent(); // The instruction is gone now.
642 AArch64TargetLowering::emitAtomicCmpSwap(MachineInstr *MI,
643 MachineBasicBlock *BB,
644 unsigned Size) const {
645 unsigned dest = MI->getOperand(0).getReg();
646 unsigned ptr = MI->getOperand(1).getReg();
647 unsigned oldval = MI->getOperand(2).getReg();
648 unsigned newval = MI->getOperand(3).getReg();
649 AtomicOrdering Ord = static_cast<AtomicOrdering>(MI->getOperand(4).getImm());
650 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
651 DebugLoc dl = MI->getDebugLoc();
653 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
654 const TargetRegisterClass *TRCsp;
655 TRCsp = Size == 8 ? &AArch64::GPR64xspRegClass : &AArch64::GPR32wspRegClass;
657 unsigned ldrOpc, strOpc;
658 getExclusiveOperation(Size, Ord, ldrOpc, strOpc);
660 MachineFunction *MF = BB->getParent();
661 const BasicBlock *LLVM_BB = BB->getBasicBlock();
662 MachineFunction::iterator It = BB;
663 ++It; // insert the new blocks after the current block
665 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
666 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
667 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
668 MF->insert(It, loop1MBB);
669 MF->insert(It, loop2MBB);
670 MF->insert(It, exitMBB);
672 // Transfer the remainder of BB and its successor edges to exitMBB.
673 exitMBB->splice(exitMBB->begin(), BB,
674 llvm::next(MachineBasicBlock::iterator(MI)),
676 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
680 // fallthrough --> loop1MBB
681 BB->addSuccessor(loop1MBB);
688 BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
690 unsigned CmpOp = Size == 8 ? AArch64::CMPxx_lsl : AArch64::CMPww_lsl;
691 MRI.constrainRegClass(dest, TRCsp);
692 BuildMI(BB, dl, TII->get(CmpOp))
693 .addReg(dest).addReg(oldval).addImm(0);
694 BuildMI(BB, dl, TII->get(AArch64::Bcc))
695 .addImm(A64CC::NE).addMBB(exitMBB);
696 BB->addSuccessor(loop2MBB);
697 BB->addSuccessor(exitMBB);
700 // strex stxr_status, newval, [ptr]
701 // cbnz stxr_status, loop1MBB
703 unsigned stxr_status = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
704 MRI.constrainRegClass(stxr_status, &AArch64::GPR32wspRegClass);
706 BuildMI(BB, dl, TII->get(strOpc), stxr_status).addReg(newval).addReg(ptr);
707 BuildMI(BB, dl, TII->get(AArch64::CBNZw))
708 .addReg(stxr_status).addMBB(loop1MBB);
709 BB->addSuccessor(loop1MBB);
710 BB->addSuccessor(exitMBB);
716 MI->eraseFromParent(); // The instruction is gone now.
722 AArch64TargetLowering::EmitF128CSEL(MachineInstr *MI,
723 MachineBasicBlock *MBB) const {
724 // We materialise the F128CSEL pseudo-instruction using conditional branches
725 // and loads, giving an instruciton sequence like:
734 // Using virtual registers would probably not be beneficial since COPY
735 // instructions are expensive for f128 (there's no actual instruction to
738 // An alternative would be to do an integer-CSEL on some address. E.g.:
743 // csel x0, x0, x1, ne
746 // It's unclear which approach is actually optimal.
747 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
748 MachineFunction *MF = MBB->getParent();
749 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
750 DebugLoc DL = MI->getDebugLoc();
751 MachineFunction::iterator It = MBB;
754 unsigned DestReg = MI->getOperand(0).getReg();
755 unsigned IfTrueReg = MI->getOperand(1).getReg();
756 unsigned IfFalseReg = MI->getOperand(2).getReg();
757 unsigned CondCode = MI->getOperand(3).getImm();
758 bool NZCVKilled = MI->getOperand(4).isKill();
760 MachineBasicBlock *TrueBB = MF->CreateMachineBasicBlock(LLVM_BB);
761 MachineBasicBlock *EndBB = MF->CreateMachineBasicBlock(LLVM_BB);
762 MF->insert(It, TrueBB);
763 MF->insert(It, EndBB);
765 // Transfer rest of current basic-block to EndBB
766 EndBB->splice(EndBB->begin(), MBB,
767 llvm::next(MachineBasicBlock::iterator(MI)),
769 EndBB->transferSuccessorsAndUpdatePHIs(MBB);
771 // We need somewhere to store the f128 value needed.
772 int ScratchFI = MF->getFrameInfo()->CreateSpillStackObject(16, 16);
774 // [... start of incoming MBB ...]
775 // str qIFFALSE, [sp]
778 BuildMI(MBB, DL, TII->get(AArch64::LSFP128_STR))
780 .addFrameIndex(ScratchFI)
782 BuildMI(MBB, DL, TII->get(AArch64::Bcc))
785 BuildMI(MBB, DL, TII->get(AArch64::Bimm))
787 MBB->addSuccessor(TrueBB);
788 MBB->addSuccessor(EndBB);
791 // NZCV is live-through TrueBB.
792 TrueBB->addLiveIn(AArch64::NZCV);
793 EndBB->addLiveIn(AArch64::NZCV);
798 BuildMI(TrueBB, DL, TII->get(AArch64::LSFP128_STR))
800 .addFrameIndex(ScratchFI)
803 // Note: fallthrough. We can rely on LLVM adding a branch if it reorders the
805 TrueBB->addSuccessor(EndBB);
809 // [... rest of incoming MBB ...]
810 MachineInstr *StartOfEnd = EndBB->begin();
811 BuildMI(*EndBB, StartOfEnd, DL, TII->get(AArch64::LSFP128_LDR), DestReg)
812 .addFrameIndex(ScratchFI)
815 MI->eraseFromParent();
820 AArch64TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
821 MachineBasicBlock *MBB) const {
822 switch (MI->getOpcode()) {
823 default: llvm_unreachable("Unhandled instruction with custom inserter");
824 case AArch64::F128CSEL:
825 return EmitF128CSEL(MI, MBB);
826 case AArch64::ATOMIC_LOAD_ADD_I8:
827 return emitAtomicBinary(MI, MBB, 1, AArch64::ADDwww_lsl);
828 case AArch64::ATOMIC_LOAD_ADD_I16:
829 return emitAtomicBinary(MI, MBB, 2, AArch64::ADDwww_lsl);
830 case AArch64::ATOMIC_LOAD_ADD_I32:
831 return emitAtomicBinary(MI, MBB, 4, AArch64::ADDwww_lsl);
832 case AArch64::ATOMIC_LOAD_ADD_I64:
833 return emitAtomicBinary(MI, MBB, 8, AArch64::ADDxxx_lsl);
835 case AArch64::ATOMIC_LOAD_SUB_I8:
836 return emitAtomicBinary(MI, MBB, 1, AArch64::SUBwww_lsl);
837 case AArch64::ATOMIC_LOAD_SUB_I16:
838 return emitAtomicBinary(MI, MBB, 2, AArch64::SUBwww_lsl);
839 case AArch64::ATOMIC_LOAD_SUB_I32:
840 return emitAtomicBinary(MI, MBB, 4, AArch64::SUBwww_lsl);
841 case AArch64::ATOMIC_LOAD_SUB_I64:
842 return emitAtomicBinary(MI, MBB, 8, AArch64::SUBxxx_lsl);
844 case AArch64::ATOMIC_LOAD_AND_I8:
845 return emitAtomicBinary(MI, MBB, 1, AArch64::ANDwww_lsl);
846 case AArch64::ATOMIC_LOAD_AND_I16:
847 return emitAtomicBinary(MI, MBB, 2, AArch64::ANDwww_lsl);
848 case AArch64::ATOMIC_LOAD_AND_I32:
849 return emitAtomicBinary(MI, MBB, 4, AArch64::ANDwww_lsl);
850 case AArch64::ATOMIC_LOAD_AND_I64:
851 return emitAtomicBinary(MI, MBB, 8, AArch64::ANDxxx_lsl);
853 case AArch64::ATOMIC_LOAD_OR_I8:
854 return emitAtomicBinary(MI, MBB, 1, AArch64::ORRwww_lsl);
855 case AArch64::ATOMIC_LOAD_OR_I16:
856 return emitAtomicBinary(MI, MBB, 2, AArch64::ORRwww_lsl);
857 case AArch64::ATOMIC_LOAD_OR_I32:
858 return emitAtomicBinary(MI, MBB, 4, AArch64::ORRwww_lsl);
859 case AArch64::ATOMIC_LOAD_OR_I64:
860 return emitAtomicBinary(MI, MBB, 8, AArch64::ORRxxx_lsl);
862 case AArch64::ATOMIC_LOAD_XOR_I8:
863 return emitAtomicBinary(MI, MBB, 1, AArch64::EORwww_lsl);
864 case AArch64::ATOMIC_LOAD_XOR_I16:
865 return emitAtomicBinary(MI, MBB, 2, AArch64::EORwww_lsl);
866 case AArch64::ATOMIC_LOAD_XOR_I32:
867 return emitAtomicBinary(MI, MBB, 4, AArch64::EORwww_lsl);
868 case AArch64::ATOMIC_LOAD_XOR_I64:
869 return emitAtomicBinary(MI, MBB, 8, AArch64::EORxxx_lsl);
871 case AArch64::ATOMIC_LOAD_NAND_I8:
872 return emitAtomicBinary(MI, MBB, 1, AArch64::BICwww_lsl);
873 case AArch64::ATOMIC_LOAD_NAND_I16:
874 return emitAtomicBinary(MI, MBB, 2, AArch64::BICwww_lsl);
875 case AArch64::ATOMIC_LOAD_NAND_I32:
876 return emitAtomicBinary(MI, MBB, 4, AArch64::BICwww_lsl);
877 case AArch64::ATOMIC_LOAD_NAND_I64:
878 return emitAtomicBinary(MI, MBB, 8, AArch64::BICxxx_lsl);
880 case AArch64::ATOMIC_LOAD_MIN_I8:
881 return emitAtomicBinaryMinMax(MI, MBB, 1, AArch64::CMPww_sxtb, A64CC::GT);
882 case AArch64::ATOMIC_LOAD_MIN_I16:
883 return emitAtomicBinaryMinMax(MI, MBB, 2, AArch64::CMPww_sxth, A64CC::GT);
884 case AArch64::ATOMIC_LOAD_MIN_I32:
885 return emitAtomicBinaryMinMax(MI, MBB, 4, AArch64::CMPww_lsl, A64CC::GT);
886 case AArch64::ATOMIC_LOAD_MIN_I64:
887 return emitAtomicBinaryMinMax(MI, MBB, 8, AArch64::CMPxx_lsl, A64CC::GT);
889 case AArch64::ATOMIC_LOAD_MAX_I8:
890 return emitAtomicBinaryMinMax(MI, MBB, 1, AArch64::CMPww_sxtb, A64CC::LT);
891 case AArch64::ATOMIC_LOAD_MAX_I16:
892 return emitAtomicBinaryMinMax(MI, MBB, 2, AArch64::CMPww_sxth, A64CC::LT);
893 case AArch64::ATOMIC_LOAD_MAX_I32:
894 return emitAtomicBinaryMinMax(MI, MBB, 4, AArch64::CMPww_lsl, A64CC::LT);
895 case AArch64::ATOMIC_LOAD_MAX_I64:
896 return emitAtomicBinaryMinMax(MI, MBB, 8, AArch64::CMPxx_lsl, A64CC::LT);
898 case AArch64::ATOMIC_LOAD_UMIN_I8:
899 return emitAtomicBinaryMinMax(MI, MBB, 1, AArch64::CMPww_uxtb, A64CC::HI);
900 case AArch64::ATOMIC_LOAD_UMIN_I16:
901 return emitAtomicBinaryMinMax(MI, MBB, 2, AArch64::CMPww_uxth, A64CC::HI);
902 case AArch64::ATOMIC_LOAD_UMIN_I32:
903 return emitAtomicBinaryMinMax(MI, MBB, 4, AArch64::CMPww_lsl, A64CC::HI);
904 case AArch64::ATOMIC_LOAD_UMIN_I64:
905 return emitAtomicBinaryMinMax(MI, MBB, 8, AArch64::CMPxx_lsl, A64CC::HI);
907 case AArch64::ATOMIC_LOAD_UMAX_I8:
908 return emitAtomicBinaryMinMax(MI, MBB, 1, AArch64::CMPww_uxtb, A64CC::LO);
909 case AArch64::ATOMIC_LOAD_UMAX_I16:
910 return emitAtomicBinaryMinMax(MI, MBB, 2, AArch64::CMPww_uxth, A64CC::LO);
911 case AArch64::ATOMIC_LOAD_UMAX_I32:
912 return emitAtomicBinaryMinMax(MI, MBB, 4, AArch64::CMPww_lsl, A64CC::LO);
913 case AArch64::ATOMIC_LOAD_UMAX_I64:
914 return emitAtomicBinaryMinMax(MI, MBB, 8, AArch64::CMPxx_lsl, A64CC::LO);
916 case AArch64::ATOMIC_SWAP_I8:
917 return emitAtomicBinary(MI, MBB, 1, 0);
918 case AArch64::ATOMIC_SWAP_I16:
919 return emitAtomicBinary(MI, MBB, 2, 0);
920 case AArch64::ATOMIC_SWAP_I32:
921 return emitAtomicBinary(MI, MBB, 4, 0);
922 case AArch64::ATOMIC_SWAP_I64:
923 return emitAtomicBinary(MI, MBB, 8, 0);
925 case AArch64::ATOMIC_CMP_SWAP_I8:
926 return emitAtomicCmpSwap(MI, MBB, 1);
927 case AArch64::ATOMIC_CMP_SWAP_I16:
928 return emitAtomicCmpSwap(MI, MBB, 2);
929 case AArch64::ATOMIC_CMP_SWAP_I32:
930 return emitAtomicCmpSwap(MI, MBB, 4);
931 case AArch64::ATOMIC_CMP_SWAP_I64:
932 return emitAtomicCmpSwap(MI, MBB, 8);
937 const char *AArch64TargetLowering::getTargetNodeName(unsigned Opcode) const {
939 case AArch64ISD::BR_CC: return "AArch64ISD::BR_CC";
940 case AArch64ISD::Call: return "AArch64ISD::Call";
941 case AArch64ISD::FPMOV: return "AArch64ISD::FPMOV";
942 case AArch64ISD::GOTLoad: return "AArch64ISD::GOTLoad";
943 case AArch64ISD::BFI: return "AArch64ISD::BFI";
944 case AArch64ISD::EXTR: return "AArch64ISD::EXTR";
945 case AArch64ISD::Ret: return "AArch64ISD::Ret";
946 case AArch64ISD::SBFX: return "AArch64ISD::SBFX";
947 case AArch64ISD::SELECT_CC: return "AArch64ISD::SELECT_CC";
948 case AArch64ISD::SETCC: return "AArch64ISD::SETCC";
949 case AArch64ISD::TC_RETURN: return "AArch64ISD::TC_RETURN";
950 case AArch64ISD::THREAD_POINTER: return "AArch64ISD::THREAD_POINTER";
951 case AArch64ISD::TLSDESCCALL: return "AArch64ISD::TLSDESCCALL";
952 case AArch64ISD::WrapperLarge: return "AArch64ISD::WrapperLarge";
953 case AArch64ISD::WrapperSmall: return "AArch64ISD::WrapperSmall";
955 case AArch64ISD::NEON_MOVIMM:
956 return "AArch64ISD::NEON_MOVIMM";
957 case AArch64ISD::NEON_MVNIMM:
958 return "AArch64ISD::NEON_MVNIMM";
959 case AArch64ISD::NEON_FMOVIMM:
960 return "AArch64ISD::NEON_FMOVIMM";
961 case AArch64ISD::NEON_CMP:
962 return "AArch64ISD::NEON_CMP";
963 case AArch64ISD::NEON_CMPZ:
964 return "AArch64ISD::NEON_CMPZ";
965 case AArch64ISD::NEON_TST:
966 return "AArch64ISD::NEON_TST";
967 case AArch64ISD::NEON_QSHLs:
968 return "AArch64ISD::NEON_QSHLs";
969 case AArch64ISD::NEON_QSHLu:
970 return "AArch64ISD::NEON_QSHLu";
971 case AArch64ISD::NEON_VDUP:
972 return "AArch64ISD::NEON_VDUP";
973 case AArch64ISD::NEON_VDUPLANE:
974 return "AArch64ISD::NEON_VDUPLANE";
975 case AArch64ISD::NEON_REV16:
976 return "AArch64ISD::NEON_REV16";
977 case AArch64ISD::NEON_REV32:
978 return "AArch64ISD::NEON_REV32";
979 case AArch64ISD::NEON_REV64:
980 return "AArch64ISD::NEON_REV64";
981 case AArch64ISD::NEON_UZP1:
982 return "AArch64ISD::NEON_UZP1";
983 case AArch64ISD::NEON_UZP2:
984 return "AArch64ISD::NEON_UZP2";
985 case AArch64ISD::NEON_ZIP1:
986 return "AArch64ISD::NEON_ZIP1";
987 case AArch64ISD::NEON_ZIP2:
988 return "AArch64ISD::NEON_ZIP2";
989 case AArch64ISD::NEON_TRN1:
990 return "AArch64ISD::NEON_TRN1";
991 case AArch64ISD::NEON_TRN2:
992 return "AArch64ISD::NEON_TRN2";
993 case AArch64ISD::NEON_LD1_UPD:
994 return "AArch64ISD::NEON_LD1_UPD";
995 case AArch64ISD::NEON_LD2_UPD:
996 return "AArch64ISD::NEON_LD2_UPD";
997 case AArch64ISD::NEON_LD3_UPD:
998 return "AArch64ISD::NEON_LD3_UPD";
999 case AArch64ISD::NEON_LD4_UPD:
1000 return "AArch64ISD::NEON_LD4_UPD";
1001 case AArch64ISD::NEON_ST1_UPD:
1002 return "AArch64ISD::NEON_ST1_UPD";
1003 case AArch64ISD::NEON_ST2_UPD:
1004 return "AArch64ISD::NEON_ST2_UPD";
1005 case AArch64ISD::NEON_ST3_UPD:
1006 return "AArch64ISD::NEON_ST3_UPD";
1007 case AArch64ISD::NEON_ST4_UPD:
1008 return "AArch64ISD::NEON_ST4_UPD";
1009 case AArch64ISD::NEON_LD1x2_UPD:
1010 return "AArch64ISD::NEON_LD1x2_UPD";
1011 case AArch64ISD::NEON_LD1x3_UPD:
1012 return "AArch64ISD::NEON_LD1x3_UPD";
1013 case AArch64ISD::NEON_LD1x4_UPD:
1014 return "AArch64ISD::NEON_LD1x4_UPD";
1015 case AArch64ISD::NEON_ST1x2_UPD:
1016 return "AArch64ISD::NEON_ST1x2_UPD";
1017 case AArch64ISD::NEON_ST1x3_UPD:
1018 return "AArch64ISD::NEON_ST1x3_UPD";
1019 case AArch64ISD::NEON_ST1x4_UPD:
1020 return "AArch64ISD::NEON_ST1x4_UPD";
1021 case AArch64ISD::NEON_LD2DUP:
1022 return "AArch64ISD::NEON_LD2DUP";
1023 case AArch64ISD::NEON_LD3DUP:
1024 return "AArch64ISD::NEON_LD3DUP";
1025 case AArch64ISD::NEON_LD4DUP:
1026 return "AArch64ISD::NEON_LD4DUP";
1027 case AArch64ISD::NEON_LD2DUP_UPD:
1028 return "AArch64ISD::NEON_LD2DUP_UPD";
1029 case AArch64ISD::NEON_LD3DUP_UPD:
1030 return "AArch64ISD::NEON_LD3DUP_UPD";
1031 case AArch64ISD::NEON_LD4DUP_UPD:
1032 return "AArch64ISD::NEON_LD4DUP_UPD";
1033 case AArch64ISD::NEON_LD2LN_UPD:
1034 return "AArch64ISD::NEON_LD2LN_UPD";
1035 case AArch64ISD::NEON_LD3LN_UPD:
1036 return "AArch64ISD::NEON_LD3LN_UPD";
1037 case AArch64ISD::NEON_LD4LN_UPD:
1038 return "AArch64ISD::NEON_LD4LN_UPD";
1039 case AArch64ISD::NEON_ST2LN_UPD:
1040 return "AArch64ISD::NEON_ST2LN_UPD";
1041 case AArch64ISD::NEON_ST3LN_UPD:
1042 return "AArch64ISD::NEON_ST3LN_UPD";
1043 case AArch64ISD::NEON_ST4LN_UPD:
1044 return "AArch64ISD::NEON_ST4LN_UPD";
1045 case AArch64ISD::NEON_VEXTRACT:
1046 return "AArch64ISD::NEON_VEXTRACT";
1052 static const uint16_t AArch64FPRArgRegs[] = {
1053 AArch64::Q0, AArch64::Q1, AArch64::Q2, AArch64::Q3,
1054 AArch64::Q4, AArch64::Q5, AArch64::Q6, AArch64::Q7
1056 static const unsigned NumFPRArgRegs = llvm::array_lengthof(AArch64FPRArgRegs);
1058 static const uint16_t AArch64ArgRegs[] = {
1059 AArch64::X0, AArch64::X1, AArch64::X2, AArch64::X3,
1060 AArch64::X4, AArch64::X5, AArch64::X6, AArch64::X7
1062 static const unsigned NumArgRegs = llvm::array_lengthof(AArch64ArgRegs);
1064 static bool CC_AArch64NoMoreRegs(unsigned ValNo, MVT ValVT, MVT LocVT,
1065 CCValAssign::LocInfo LocInfo,
1066 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1067 // Mark all remaining general purpose registers as allocated. We don't
1068 // backtrack: if (for example) an i128 gets put on the stack, no subsequent
1069 // i64 will go in registers (C.11).
1070 for (unsigned i = 0; i < NumArgRegs; ++i)
1071 State.AllocateReg(AArch64ArgRegs[i]);
1076 #include "AArch64GenCallingConv.inc"
1078 CCAssignFn *AArch64TargetLowering::CCAssignFnForNode(CallingConv::ID CC) const {
1081 default: llvm_unreachable("Unsupported calling convention");
1082 case CallingConv::Fast:
1083 case CallingConv::C:
1089 AArch64TargetLowering::SaveVarArgRegisters(CCState &CCInfo, SelectionDAG &DAG,
1090 SDLoc DL, SDValue &Chain) const {
1091 MachineFunction &MF = DAG.getMachineFunction();
1092 MachineFrameInfo *MFI = MF.getFrameInfo();
1093 AArch64MachineFunctionInfo *FuncInfo
1094 = MF.getInfo<AArch64MachineFunctionInfo>();
1096 SmallVector<SDValue, 8> MemOps;
1098 unsigned FirstVariadicGPR = CCInfo.getFirstUnallocated(AArch64ArgRegs,
1100 unsigned FirstVariadicFPR = CCInfo.getFirstUnallocated(AArch64FPRArgRegs,
1103 unsigned GPRSaveSize = 8 * (NumArgRegs - FirstVariadicGPR);
1105 if (GPRSaveSize != 0) {
1106 GPRIdx = MFI->CreateStackObject(GPRSaveSize, 8, false);
1108 SDValue FIN = DAG.getFrameIndex(GPRIdx, getPointerTy());
1110 for (unsigned i = FirstVariadicGPR; i < NumArgRegs; ++i) {
1111 unsigned VReg = MF.addLiveIn(AArch64ArgRegs[i], &AArch64::GPR64RegClass);
1112 SDValue Val = DAG.getCopyFromReg(Chain, DL, VReg, MVT::i64);
1113 SDValue Store = DAG.getStore(Val.getValue(1), DL, Val, FIN,
1114 MachinePointerInfo::getStack(i * 8),
1116 MemOps.push_back(Store);
1117 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(), FIN,
1118 DAG.getConstant(8, getPointerTy()));
1122 if (getSubtarget()->hasFPARMv8()) {
1123 unsigned FPRSaveSize = 16 * (NumFPRArgRegs - FirstVariadicFPR);
1125 // According to the AArch64 Procedure Call Standard, section B.1/B.3, we
1126 // can omit a register save area if we know we'll never use registers of
1128 if (FPRSaveSize != 0) {
1129 FPRIdx = MFI->CreateStackObject(FPRSaveSize, 16, false);
1131 SDValue FIN = DAG.getFrameIndex(FPRIdx, getPointerTy());
1133 for (unsigned i = FirstVariadicFPR; i < NumFPRArgRegs; ++i) {
1134 unsigned VReg = MF.addLiveIn(AArch64FPRArgRegs[i],
1135 &AArch64::FPR128RegClass);
1136 SDValue Val = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f128);
1137 SDValue Store = DAG.getStore(Val.getValue(1), DL, Val, FIN,
1138 MachinePointerInfo::getStack(i * 16),
1140 MemOps.push_back(Store);
1141 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(), FIN,
1142 DAG.getConstant(16, getPointerTy()));
1145 FuncInfo->setVariadicFPRIdx(FPRIdx);
1146 FuncInfo->setVariadicFPRSize(FPRSaveSize);
1149 int StackIdx = MFI->CreateFixedObject(8, CCInfo.getNextStackOffset(), true);
1151 FuncInfo->setVariadicStackIdx(StackIdx);
1152 FuncInfo->setVariadicGPRIdx(GPRIdx);
1153 FuncInfo->setVariadicGPRSize(GPRSaveSize);
1155 if (!MemOps.empty()) {
1156 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, &MemOps[0],
1163 AArch64TargetLowering::LowerFormalArguments(SDValue Chain,
1164 CallingConv::ID CallConv, bool isVarArg,
1165 const SmallVectorImpl<ISD::InputArg> &Ins,
1166 SDLoc dl, SelectionDAG &DAG,
1167 SmallVectorImpl<SDValue> &InVals) const {
1168 MachineFunction &MF = DAG.getMachineFunction();
1169 AArch64MachineFunctionInfo *FuncInfo
1170 = MF.getInfo<AArch64MachineFunctionInfo>();
1171 MachineFrameInfo *MFI = MF.getFrameInfo();
1172 bool TailCallOpt = MF.getTarget().Options.GuaranteedTailCallOpt;
1174 SmallVector<CCValAssign, 16> ArgLocs;
1175 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1176 getTargetMachine(), ArgLocs, *DAG.getContext());
1177 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForNode(CallConv));
1179 SmallVector<SDValue, 16> ArgValues;
1182 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1183 CCValAssign &VA = ArgLocs[i];
1184 ISD::ArgFlagsTy Flags = Ins[i].Flags;
1186 if (Flags.isByVal()) {
1187 // Byval is used for small structs and HFAs in the PCS, but the system
1188 // should work in a non-compliant manner for larger structs.
1189 EVT PtrTy = getPointerTy();
1190 int Size = Flags.getByValSize();
1191 unsigned NumRegs = (Size + 7) / 8;
1193 unsigned FrameIdx = MFI->CreateFixedObject(8 * NumRegs,
1194 VA.getLocMemOffset(),
1196 SDValue FrameIdxN = DAG.getFrameIndex(FrameIdx, PtrTy);
1197 InVals.push_back(FrameIdxN);
1200 } else if (VA.isRegLoc()) {
1201 MVT RegVT = VA.getLocVT();
1202 const TargetRegisterClass *RC = getRegClassFor(RegVT);
1203 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
1205 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
1206 } else { // VA.isRegLoc()
1207 assert(VA.isMemLoc());
1209 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
1210 VA.getLocMemOffset(), true);
1212 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1213 ArgValue = DAG.getLoad(VA.getLocVT(), dl, Chain, FIN,
1214 MachinePointerInfo::getFixedStack(FI),
1215 false, false, false, 0);
1220 switch (VA.getLocInfo()) {
1221 default: llvm_unreachable("Unknown loc info!");
1222 case CCValAssign::Full: break;
1223 case CCValAssign::BCvt:
1224 ArgValue = DAG.getNode(ISD::BITCAST,dl, VA.getValVT(), ArgValue);
1226 case CCValAssign::SExt:
1227 case CCValAssign::ZExt:
1228 case CCValAssign::AExt:
1229 case CCValAssign::FPExt: {
1230 unsigned DestSize = VA.getValVT().getSizeInBits();
1231 unsigned DestSubReg;
1234 case 8: DestSubReg = AArch64::sub_8; break;
1235 case 16: DestSubReg = AArch64::sub_16; break;
1236 case 32: DestSubReg = AArch64::sub_32; break;
1237 case 64: DestSubReg = AArch64::sub_64; break;
1238 default: llvm_unreachable("Unexpected argument promotion");
1241 ArgValue = SDValue(DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG, dl,
1242 VA.getValVT(), ArgValue,
1243 DAG.getTargetConstant(DestSubReg, MVT::i32)),
1249 InVals.push_back(ArgValue);
1253 SaveVarArgRegisters(CCInfo, DAG, dl, Chain);
1255 unsigned StackArgSize = CCInfo.getNextStackOffset();
1256 if (DoesCalleeRestoreStack(CallConv, TailCallOpt)) {
1257 // This is a non-standard ABI so by fiat I say we're allowed to make full
1258 // use of the stack area to be popped, which must be aligned to 16 bytes in
1260 StackArgSize = RoundUpToAlignment(StackArgSize, 16);
1262 // If we're expected to restore the stack (e.g. fastcc) then we'll be adding
1263 // a multiple of 16.
1264 FuncInfo->setArgumentStackToRestore(StackArgSize);
1266 // This realignment carries over to the available bytes below. Our own
1267 // callers will guarantee the space is free by giving an aligned value to
1270 // Even if we're not expected to free up the space, it's useful to know how
1271 // much is there while considering tail calls (because we can reuse it).
1272 FuncInfo->setBytesInStackArgArea(StackArgSize);
1278 AArch64TargetLowering::LowerReturn(SDValue Chain,
1279 CallingConv::ID CallConv, bool isVarArg,
1280 const SmallVectorImpl<ISD::OutputArg> &Outs,
1281 const SmallVectorImpl<SDValue> &OutVals,
1282 SDLoc dl, SelectionDAG &DAG) const {
1283 // CCValAssign - represent the assignment of the return value to a location.
1284 SmallVector<CCValAssign, 16> RVLocs;
1286 // CCState - Info about the registers and stack slots.
1287 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1288 getTargetMachine(), RVLocs, *DAG.getContext());
1290 // Analyze outgoing return values.
1291 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv));
1294 SmallVector<SDValue, 4> RetOps(1, Chain);
1296 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
1297 // PCS: "If the type, T, of the result of a function is such that
1298 // void func(T arg) would require that arg be passed as a value in a
1299 // register (or set of registers) according to the rules in 5.4, then the
1300 // result is returned in the same registers as would be used for such an
1303 // Otherwise, the caller shall reserve a block of memory of sufficient
1304 // size and alignment to hold the result. The address of the memory block
1305 // shall be passed as an additional argument to the function in x8."
1307 // This is implemented in two places. The register-return values are dealt
1308 // with here, more complex returns are passed as an sret parameter, which
1309 // means we don't have to worry about it during actual return.
1310 CCValAssign &VA = RVLocs[i];
1311 assert(VA.isRegLoc() && "Only register-returns should be created by PCS");
1314 SDValue Arg = OutVals[i];
1316 // There's no convenient note in the ABI about this as there is for normal
1317 // arguments, but it says return values are passed in the same registers as
1318 // an argument would be. I believe that includes the comments about
1319 // unspecified higher bits, putting the burden of widening on the *caller*
1320 // for return values.
1321 switch (VA.getLocInfo()) {
1322 default: llvm_unreachable("Unknown loc info");
1323 case CCValAssign::Full: break;
1324 case CCValAssign::SExt:
1325 case CCValAssign::ZExt:
1326 case CCValAssign::AExt:
1327 // Floating-point values should only be extended when they're going into
1328 // memory, which can't happen here so an integer extend is acceptable.
1329 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1331 case CCValAssign::BCvt:
1332 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
1336 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1337 Flag = Chain.getValue(1);
1338 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
1341 RetOps[0] = Chain; // Update chain.
1343 // Add the flag if we have it.
1345 RetOps.push_back(Flag);
1347 return DAG.getNode(AArch64ISD::Ret, dl, MVT::Other,
1348 &RetOps[0], RetOps.size());
1351 unsigned AArch64TargetLowering::getByValTypeAlignment(Type *Ty) const {
1352 // This is a new backend. For anything more precise than this a FE should
1353 // set an explicit alignment.
1358 AArch64TargetLowering::LowerCall(CallLoweringInfo &CLI,
1359 SmallVectorImpl<SDValue> &InVals) const {
1360 SelectionDAG &DAG = CLI.DAG;
1362 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1363 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1364 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
1365 SDValue Chain = CLI.Chain;
1366 SDValue Callee = CLI.Callee;
1367 bool &IsTailCall = CLI.IsTailCall;
1368 CallingConv::ID CallConv = CLI.CallConv;
1369 bool IsVarArg = CLI.IsVarArg;
1371 MachineFunction &MF = DAG.getMachineFunction();
1372 AArch64MachineFunctionInfo *FuncInfo
1373 = MF.getInfo<AArch64MachineFunctionInfo>();
1374 bool TailCallOpt = MF.getTarget().Options.GuaranteedTailCallOpt;
1375 bool IsStructRet = !Outs.empty() && Outs[0].Flags.isSRet();
1376 bool IsSibCall = false;
1379 IsTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1380 IsVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
1381 Outs, OutVals, Ins, DAG);
1383 // A sibling call is one where we're under the usual C ABI and not planning
1384 // to change that but can still do a tail call:
1385 if (!TailCallOpt && IsTailCall)
1389 SmallVector<CCValAssign, 16> ArgLocs;
1390 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
1391 getTargetMachine(), ArgLocs, *DAG.getContext());
1392 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CallConv));
1394 // On AArch64 (and all other architectures I'm aware of) the most this has to
1395 // do is adjust the stack pointer.
1396 unsigned NumBytes = RoundUpToAlignment(CCInfo.getNextStackOffset(), 16);
1398 // Since we're not changing the ABI to make this a tail call, the memory
1399 // operands are already available in the caller's incoming argument space.
1403 // FPDiff is the byte offset of the call's argument area from the callee's.
1404 // Stores to callee stack arguments will be placed in FixedStackSlots offset
1405 // by this amount for a tail call. In a sibling call it must be 0 because the
1406 // caller will deallocate the entire stack and the callee still expects its
1407 // arguments to begin at SP+0. Completely unused for non-tail calls.
1410 if (IsTailCall && !IsSibCall) {
1411 unsigned NumReusableBytes = FuncInfo->getBytesInStackArgArea();
1413 // FPDiff will be negative if this tail call requires more space than we
1414 // would automatically have in our incoming argument space. Positive if we
1415 // can actually shrink the stack.
1416 FPDiff = NumReusableBytes - NumBytes;
1418 // The stack pointer must be 16-byte aligned at all times it's used for a
1419 // memory operation, which in practice means at *all* times and in
1420 // particular across call boundaries. Therefore our own arguments started at
1421 // a 16-byte aligned SP and the delta applied for the tail call should
1422 // satisfy the same constraint.
1423 assert(FPDiff % 16 == 0 && "unaligned stack on tail call");
1427 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
1430 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, AArch64::XSP,
1433 SmallVector<SDValue, 8> MemOpChains;
1434 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1436 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1437 CCValAssign &VA = ArgLocs[i];
1438 ISD::ArgFlagsTy Flags = Outs[i].Flags;
1439 SDValue Arg = OutVals[i];
1441 // Callee does the actual widening, so all extensions just use an implicit
1442 // definition of the rest of the Loc. Aesthetically, this would be nicer as
1443 // an ANY_EXTEND, but that isn't valid for floating-point types and this
1444 // alternative works on integer types too.
1445 switch (VA.getLocInfo()) {
1446 default: llvm_unreachable("Unknown loc info!");
1447 case CCValAssign::Full: break;
1448 case CCValAssign::SExt:
1449 case CCValAssign::ZExt:
1450 case CCValAssign::AExt:
1451 case CCValAssign::FPExt: {
1452 unsigned SrcSize = VA.getValVT().getSizeInBits();
1456 case 8: SrcSubReg = AArch64::sub_8; break;
1457 case 16: SrcSubReg = AArch64::sub_16; break;
1458 case 32: SrcSubReg = AArch64::sub_32; break;
1459 case 64: SrcSubReg = AArch64::sub_64; break;
1460 default: llvm_unreachable("Unexpected argument promotion");
1463 Arg = SDValue(DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, dl,
1465 DAG.getUNDEF(VA.getLocVT()),
1467 DAG.getTargetConstant(SrcSubReg, MVT::i32)),
1472 case CCValAssign::BCvt:
1473 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
1477 if (VA.isRegLoc()) {
1478 // A normal register (sub-) argument. For now we just note it down because
1479 // we want to copy things into registers as late as possible to avoid
1480 // register-pressure (and possibly worse).
1481 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1485 assert(VA.isMemLoc() && "unexpected argument location");
1488 MachinePointerInfo DstInfo;
1490 uint32_t OpSize = Flags.isByVal() ? Flags.getByValSize() :
1491 VA.getLocVT().getSizeInBits();
1492 OpSize = (OpSize + 7) / 8;
1493 int32_t Offset = VA.getLocMemOffset() + FPDiff;
1494 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
1496 DstAddr = DAG.getFrameIndex(FI, getPointerTy());
1497 DstInfo = MachinePointerInfo::getFixedStack(FI);
1499 // Make sure any stack arguments overlapping with where we're storing are
1500 // loaded before this eventual operation. Otherwise they'll be clobbered.
1501 Chain = addTokenForArgument(Chain, DAG, MF.getFrameInfo(), FI);
1503 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset());
1505 DstAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
1506 DstInfo = MachinePointerInfo::getStack(VA.getLocMemOffset());
1509 if (Flags.isByVal()) {
1510 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i64);
1511 SDValue Cpy = DAG.getMemcpy(Chain, dl, DstAddr, Arg, SizeNode,
1512 Flags.getByValAlign(),
1513 /*isVolatile = */ false,
1514 /*alwaysInline = */ false,
1515 DstInfo, MachinePointerInfo(0));
1516 MemOpChains.push_back(Cpy);
1518 // Normal stack argument, put it where it's needed.
1519 SDValue Store = DAG.getStore(Chain, dl, Arg, DstAddr, DstInfo,
1521 MemOpChains.push_back(Store);
1525 // The loads and stores generated above shouldn't clash with each
1526 // other. Combining them with this TokenFactor notes that fact for the rest of
1528 if (!MemOpChains.empty())
1529 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1530 &MemOpChains[0], MemOpChains.size());
1532 // Most of the rest of the instructions need to be glued together; we don't
1533 // want assignments to actual registers used by a call to be rearranged by a
1534 // well-meaning scheduler.
1537 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1538 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1539 RegsToPass[i].second, InFlag);
1540 InFlag = Chain.getValue(1);
1543 // The linker is responsible for inserting veneers when necessary to put a
1544 // function call destination in range, so we don't need to bother with a
1546 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1547 const GlobalValue *GV = G->getGlobal();
1548 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy());
1549 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1550 const char *Sym = S->getSymbol();
1551 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy());
1554 // We don't usually want to end the call-sequence here because we would tidy
1555 // the frame up *after* the call, however in the ABI-changing tail-call case
1556 // we've carefully laid out the parameters so that when sp is reset they'll be
1557 // in the correct location.
1558 if (IsTailCall && !IsSibCall) {
1559 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1560 DAG.getIntPtrConstant(0, true), InFlag, dl);
1561 InFlag = Chain.getValue(1);
1564 // We produce the following DAG scheme for the actual call instruction:
1565 // (AArch64Call Chain, Callee, reg1, ..., regn, preserveMask, inflag?
1567 // Most arguments aren't going to be used and just keep the values live as
1568 // far as LLVM is concerned. It's expected to be selected as simply "bl
1569 // callee" (for a direct, non-tail call).
1570 std::vector<SDValue> Ops;
1571 Ops.push_back(Chain);
1572 Ops.push_back(Callee);
1575 // Each tail call may have to adjust the stack by a different amount, so
1576 // this information must travel along with the operation for eventual
1577 // consumption by emitEpilogue.
1578 Ops.push_back(DAG.getTargetConstant(FPDiff, MVT::i32));
1581 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1582 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1583 RegsToPass[i].second.getValueType()));
1586 // Add a register mask operand representing the call-preserved registers. This
1587 // is used later in codegen to constrain register-allocation.
1588 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1589 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
1590 assert(Mask && "Missing call preserved mask for calling convention");
1591 Ops.push_back(DAG.getRegisterMask(Mask));
1593 // If we needed glue, put it in as the last argument.
1594 if (InFlag.getNode())
1595 Ops.push_back(InFlag);
1597 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1600 return DAG.getNode(AArch64ISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
1603 Chain = DAG.getNode(AArch64ISD::Call, dl, NodeTys, &Ops[0], Ops.size());
1604 InFlag = Chain.getValue(1);
1606 // Now we can reclaim the stack, just as well do it before working out where
1607 // our return value is.
1609 uint64_t CalleePopBytes
1610 = DoesCalleeRestoreStack(CallConv, TailCallOpt) ? NumBytes : 0;
1612 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1613 DAG.getIntPtrConstant(CalleePopBytes, true),
1615 InFlag = Chain.getValue(1);
1618 return LowerCallResult(Chain, InFlag, CallConv,
1619 IsVarArg, Ins, dl, DAG, InVals);
1623 AArch64TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
1624 CallingConv::ID CallConv, bool IsVarArg,
1625 const SmallVectorImpl<ISD::InputArg> &Ins,
1626 SDLoc dl, SelectionDAG &DAG,
1627 SmallVectorImpl<SDValue> &InVals) const {
1628 // Assign locations to each value returned by this call.
1629 SmallVector<CCValAssign, 16> RVLocs;
1630 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
1631 getTargetMachine(), RVLocs, *DAG.getContext());
1632 CCInfo.AnalyzeCallResult(Ins, CCAssignFnForNode(CallConv));
1634 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1635 CCValAssign VA = RVLocs[i];
1637 // Return values that are too big to fit into registers should use an sret
1638 // pointer, so this can be a lot simpler than the main argument code.
1639 assert(VA.isRegLoc() && "Memory locations not expected for call return");
1641 SDValue Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1643 Chain = Val.getValue(1);
1644 InFlag = Val.getValue(2);
1646 switch (VA.getLocInfo()) {
1647 default: llvm_unreachable("Unknown loc info!");
1648 case CCValAssign::Full: break;
1649 case CCValAssign::BCvt:
1650 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
1652 case CCValAssign::ZExt:
1653 case CCValAssign::SExt:
1654 case CCValAssign::AExt:
1655 // Floating-point arguments only get extended/truncated if they're going
1656 // in memory, so using the integer operation is acceptable here.
1657 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
1661 InVals.push_back(Val);
1668 AArch64TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1669 CallingConv::ID CalleeCC,
1671 bool IsCalleeStructRet,
1672 bool IsCallerStructRet,
1673 const SmallVectorImpl<ISD::OutputArg> &Outs,
1674 const SmallVectorImpl<SDValue> &OutVals,
1675 const SmallVectorImpl<ISD::InputArg> &Ins,
1676 SelectionDAG& DAG) const {
1678 // For CallingConv::C this function knows whether the ABI needs
1679 // changing. That's not true for other conventions so they will have to opt in
1681 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1684 const MachineFunction &MF = DAG.getMachineFunction();
1685 const Function *CallerF = MF.getFunction();
1686 CallingConv::ID CallerCC = CallerF->getCallingConv();
1687 bool CCMatch = CallerCC == CalleeCC;
1689 // Byval parameters hand the function a pointer directly into the stack area
1690 // we want to reuse during a tail call. Working around this *is* possible (see
1691 // X86) but less efficient and uglier in LowerCall.
1692 for (Function::const_arg_iterator i = CallerF->arg_begin(),
1693 e = CallerF->arg_end(); i != e; ++i)
1694 if (i->hasByValAttr())
1697 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
1698 if (IsTailCallConvention(CalleeCC) && CCMatch)
1703 // Now we search for cases where we can use a tail call without changing the
1704 // ABI. Sibcall is used in some places (particularly gcc) to refer to this
1707 // I want anyone implementing a new calling convention to think long and hard
1708 // about this assert.
1709 assert((!IsVarArg || CalleeCC == CallingConv::C)
1710 && "Unexpected variadic calling convention");
1712 if (IsVarArg && !Outs.empty()) {
1713 // At least two cases here: if caller is fastcc then we can't have any
1714 // memory arguments (we'd be expected to clean up the stack afterwards). If
1715 // caller is C then we could potentially use its argument area.
1717 // FIXME: for now we take the most conservative of these in both cases:
1718 // disallow all variadic memory operands.
1719 SmallVector<CCValAssign, 16> ArgLocs;
1720 CCState CCInfo(CalleeCC, IsVarArg, DAG.getMachineFunction(),
1721 getTargetMachine(), ArgLocs, *DAG.getContext());
1723 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CalleeCC));
1724 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
1725 if (!ArgLocs[i].isRegLoc())
1729 // If the calling conventions do not match, then we'd better make sure the
1730 // results are returned in the same way as what the caller expects.
1732 SmallVector<CCValAssign, 16> RVLocs1;
1733 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1734 getTargetMachine(), RVLocs1, *DAG.getContext());
1735 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC));
1737 SmallVector<CCValAssign, 16> RVLocs2;
1738 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1739 getTargetMachine(), RVLocs2, *DAG.getContext());
1740 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC));
1742 if (RVLocs1.size() != RVLocs2.size())
1744 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1745 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1747 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1749 if (RVLocs1[i].isRegLoc()) {
1750 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1753 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1759 // Nothing more to check if the callee is taking no arguments
1763 SmallVector<CCValAssign, 16> ArgLocs;
1764 CCState CCInfo(CalleeCC, IsVarArg, DAG.getMachineFunction(),
1765 getTargetMachine(), ArgLocs, *DAG.getContext());
1767 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CalleeCC));
1769 const AArch64MachineFunctionInfo *FuncInfo
1770 = MF.getInfo<AArch64MachineFunctionInfo>();
1772 // If the stack arguments for this call would fit into our own save area then
1773 // the call can be made tail.
1774 return CCInfo.getNextStackOffset() <= FuncInfo->getBytesInStackArgArea();
1777 bool AArch64TargetLowering::DoesCalleeRestoreStack(CallingConv::ID CallCC,
1778 bool TailCallOpt) const {
1779 return CallCC == CallingConv::Fast && TailCallOpt;
1782 bool AArch64TargetLowering::IsTailCallConvention(CallingConv::ID CallCC) const {
1783 return CallCC == CallingConv::Fast;
1786 SDValue AArch64TargetLowering::addTokenForArgument(SDValue Chain,
1788 MachineFrameInfo *MFI,
1789 int ClobberedFI) const {
1790 SmallVector<SDValue, 8> ArgChains;
1791 int64_t FirstByte = MFI->getObjectOffset(ClobberedFI);
1792 int64_t LastByte = FirstByte + MFI->getObjectSize(ClobberedFI) - 1;
1794 // Include the original chain at the beginning of the list. When this is
1795 // used by target LowerCall hooks, this helps legalize find the
1796 // CALLSEQ_BEGIN node.
1797 ArgChains.push_back(Chain);
1799 // Add a chain value for each stack argument corresponding
1800 for (SDNode::use_iterator U = DAG.getEntryNode().getNode()->use_begin(),
1801 UE = DAG.getEntryNode().getNode()->use_end(); U != UE; ++U)
1802 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
1803 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
1804 if (FI->getIndex() < 0) {
1805 int64_t InFirstByte = MFI->getObjectOffset(FI->getIndex());
1806 int64_t InLastByte = InFirstByte;
1807 InLastByte += MFI->getObjectSize(FI->getIndex()) - 1;
1809 if ((InFirstByte <= FirstByte && FirstByte <= InLastByte) ||
1810 (FirstByte <= InFirstByte && InFirstByte <= LastByte))
1811 ArgChains.push_back(SDValue(L, 1));
1814 // Build a tokenfactor for all the chains.
1815 return DAG.getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other,
1816 &ArgChains[0], ArgChains.size());
1819 static A64CC::CondCodes IntCCToA64CC(ISD::CondCode CC) {
1821 case ISD::SETEQ: return A64CC::EQ;
1822 case ISD::SETGT: return A64CC::GT;
1823 case ISD::SETGE: return A64CC::GE;
1824 case ISD::SETLT: return A64CC::LT;
1825 case ISD::SETLE: return A64CC::LE;
1826 case ISD::SETNE: return A64CC::NE;
1827 case ISD::SETUGT: return A64CC::HI;
1828 case ISD::SETUGE: return A64CC::HS;
1829 case ISD::SETULT: return A64CC::LO;
1830 case ISD::SETULE: return A64CC::LS;
1831 default: llvm_unreachable("Unexpected condition code");
1835 bool AArch64TargetLowering::isLegalICmpImmediate(int64_t Val) const {
1836 // icmp is implemented using adds/subs immediate, which take an unsigned
1837 // 12-bit immediate, optionally shifted left by 12 bits.
1839 // Symmetric by using adds/subs
1843 return (Val & ~0xfff) == 0 || (Val & ~0xfff000) == 0;
1846 SDValue AArch64TargetLowering::getSelectableIntSetCC(SDValue LHS, SDValue RHS,
1847 ISD::CondCode CC, SDValue &A64cc,
1848 SelectionDAG &DAG, SDLoc &dl) const {
1849 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
1851 EVT VT = RHSC->getValueType(0);
1852 bool knownInvalid = false;
1854 // I'm not convinced the rest of LLVM handles these edge cases properly, but
1855 // we can at least get it right.
1856 if (isSignedIntSetCC(CC)) {
1857 C = RHSC->getSExtValue();
1858 } else if (RHSC->getZExtValue() > INT64_MAX) {
1859 // A 64-bit constant not representable by a signed 64-bit integer is far
1860 // too big to fit into a SUBS immediate anyway.
1861 knownInvalid = true;
1863 C = RHSC->getZExtValue();
1866 if (!knownInvalid && !isLegalICmpImmediate(C)) {
1867 // Constant does not fit, try adjusting it by one?
1872 if (isLegalICmpImmediate(C-1)) {
1873 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
1874 RHS = DAG.getConstant(C-1, VT);
1879 if (isLegalICmpImmediate(C-1)) {
1880 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
1881 RHS = DAG.getConstant(C-1, VT);
1886 if (isLegalICmpImmediate(C+1)) {
1887 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
1888 RHS = DAG.getConstant(C+1, VT);
1893 if (isLegalICmpImmediate(C+1)) {
1894 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
1895 RHS = DAG.getConstant(C+1, VT);
1902 A64CC::CondCodes CondCode = IntCCToA64CC(CC);
1903 A64cc = DAG.getConstant(CondCode, MVT::i32);
1904 return DAG.getNode(AArch64ISD::SETCC, dl, MVT::i32, LHS, RHS,
1905 DAG.getCondCode(CC));
1908 static A64CC::CondCodes FPCCToA64CC(ISD::CondCode CC,
1909 A64CC::CondCodes &Alternative) {
1910 A64CC::CondCodes CondCode = A64CC::Invalid;
1911 Alternative = A64CC::Invalid;
1914 default: llvm_unreachable("Unknown FP condition!");
1916 case ISD::SETOEQ: CondCode = A64CC::EQ; break;
1918 case ISD::SETOGT: CondCode = A64CC::GT; break;
1920 case ISD::SETOGE: CondCode = A64CC::GE; break;
1921 case ISD::SETOLT: CondCode = A64CC::MI; break;
1922 case ISD::SETOLE: CondCode = A64CC::LS; break;
1923 case ISD::SETONE: CondCode = A64CC::MI; Alternative = A64CC::GT; break;
1924 case ISD::SETO: CondCode = A64CC::VC; break;
1925 case ISD::SETUO: CondCode = A64CC::VS; break;
1926 case ISD::SETUEQ: CondCode = A64CC::EQ; Alternative = A64CC::VS; break;
1927 case ISD::SETUGT: CondCode = A64CC::HI; break;
1928 case ISD::SETUGE: CondCode = A64CC::PL; break;
1930 case ISD::SETULT: CondCode = A64CC::LT; break;
1932 case ISD::SETULE: CondCode = A64CC::LE; break;
1934 case ISD::SETUNE: CondCode = A64CC::NE; break;
1940 AArch64TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
1942 EVT PtrVT = getPointerTy();
1943 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1945 switch(getTargetMachine().getCodeModel()) {
1946 case CodeModel::Small:
1947 // The most efficient code is PC-relative anyway for the small memory model,
1948 // so we don't need to worry about relocation model.
1949 return DAG.getNode(AArch64ISD::WrapperSmall, DL, PtrVT,
1950 DAG.getTargetBlockAddress(BA, PtrVT, 0,
1951 AArch64II::MO_NO_FLAG),
1952 DAG.getTargetBlockAddress(BA, PtrVT, 0,
1953 AArch64II::MO_LO12),
1954 DAG.getConstant(/*Alignment=*/ 4, MVT::i32));
1955 case CodeModel::Large:
1957 AArch64ISD::WrapperLarge, DL, PtrVT,
1958 DAG.getTargetBlockAddress(BA, PtrVT, 0, AArch64II::MO_ABS_G3),
1959 DAG.getTargetBlockAddress(BA, PtrVT, 0, AArch64II::MO_ABS_G2_NC),
1960 DAG.getTargetBlockAddress(BA, PtrVT, 0, AArch64II::MO_ABS_G1_NC),
1961 DAG.getTargetBlockAddress(BA, PtrVT, 0, AArch64II::MO_ABS_G0_NC));
1963 llvm_unreachable("Only small and large code models supported now");
1968 // (BRCOND chain, val, dest)
1970 AArch64TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
1972 SDValue Chain = Op.getOperand(0);
1973 SDValue TheBit = Op.getOperand(1);
1974 SDValue DestBB = Op.getOperand(2);
1976 // AArch64 BooleanContents is the default UndefinedBooleanContent, which means
1977 // that as the consumer we are responsible for ignoring rubbish in higher
1979 TheBit = DAG.getNode(ISD::AND, dl, MVT::i32, TheBit,
1980 DAG.getConstant(1, MVT::i32));
1982 SDValue A64CMP = DAG.getNode(AArch64ISD::SETCC, dl, MVT::i32, TheBit,
1983 DAG.getConstant(0, TheBit.getValueType()),
1984 DAG.getCondCode(ISD::SETNE));
1986 return DAG.getNode(AArch64ISD::BR_CC, dl, MVT::Other, Chain,
1987 A64CMP, DAG.getConstant(A64CC::NE, MVT::i32),
1991 // (BR_CC chain, condcode, lhs, rhs, dest)
1993 AArch64TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
1995 SDValue Chain = Op.getOperand(0);
1996 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1997 SDValue LHS = Op.getOperand(2);
1998 SDValue RHS = Op.getOperand(3);
1999 SDValue DestBB = Op.getOperand(4);
2001 if (LHS.getValueType() == MVT::f128) {
2002 // f128 comparisons are lowered to runtime calls by a routine which sets
2003 // LHS, RHS and CC appropriately for the rest of this function to continue.
2004 softenSetCCOperands(DAG, MVT::f128, LHS, RHS, CC, dl);
2006 // If softenSetCCOperands returned a scalar, we need to compare the result
2007 // against zero to select between true and false values.
2008 if (RHS.getNode() == 0) {
2009 RHS = DAG.getConstant(0, LHS.getValueType());
2014 if (LHS.getValueType().isInteger()) {
2017 // Integers are handled in a separate function because the combinations of
2018 // immediates and tests can get hairy and we may want to fiddle things.
2019 SDValue CmpOp = getSelectableIntSetCC(LHS, RHS, CC, A64cc, DAG, dl);
2021 return DAG.getNode(AArch64ISD::BR_CC, dl, MVT::Other,
2022 Chain, CmpOp, A64cc, DestBB);
2025 // Note that some LLVM floating-point CondCodes can't be lowered to a single
2026 // conditional branch, hence FPCCToA64CC can set a second test, where either
2027 // passing is sufficient.
2028 A64CC::CondCodes CondCode, Alternative = A64CC::Invalid;
2029 CondCode = FPCCToA64CC(CC, Alternative);
2030 SDValue A64cc = DAG.getConstant(CondCode, MVT::i32);
2031 SDValue SetCC = DAG.getNode(AArch64ISD::SETCC, dl, MVT::i32, LHS, RHS,
2032 DAG.getCondCode(CC));
2033 SDValue A64BR_CC = DAG.getNode(AArch64ISD::BR_CC, dl, MVT::Other,
2034 Chain, SetCC, A64cc, DestBB);
2036 if (Alternative != A64CC::Invalid) {
2037 A64cc = DAG.getConstant(Alternative, MVT::i32);
2038 A64BR_CC = DAG.getNode(AArch64ISD::BR_CC, dl, MVT::Other,
2039 A64BR_CC, SetCC, A64cc, DestBB);
2047 AArch64TargetLowering::LowerF128ToCall(SDValue Op, SelectionDAG &DAG,
2048 RTLIB::Libcall Call) const {
2051 for (unsigned i = 0, e = Op->getNumOperands(); i != e; ++i) {
2052 EVT ArgVT = Op.getOperand(i).getValueType();
2053 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2054 Entry.Node = Op.getOperand(i); Entry.Ty = ArgTy;
2055 Entry.isSExt = false;
2056 Entry.isZExt = false;
2057 Args.push_back(Entry);
2059 SDValue Callee = DAG.getExternalSymbol(getLibcallName(Call), getPointerTy());
2061 Type *RetTy = Op.getValueType().getTypeForEVT(*DAG.getContext());
2063 // By default, the input chain to this libcall is the entry node of the
2064 // function. If the libcall is going to be emitted as a tail call then
2065 // isUsedByReturnOnly will change it to the right chain if the return
2066 // node which is being folded has a non-entry input chain.
2067 SDValue InChain = DAG.getEntryNode();
2069 // isTailCall may be true since the callee does not reference caller stack
2070 // frame. Check if it's in the right position.
2071 SDValue TCChain = InChain;
2072 bool isTailCall = isInTailCallPosition(DAG, Op.getNode(), TCChain);
2077 CallLoweringInfo CLI(InChain, RetTy, false, false, false, false,
2078 0, getLibcallCallingConv(Call), isTailCall,
2079 /*doesNotReturn=*/false, /*isReturnValueUsed=*/true,
2080 Callee, Args, DAG, SDLoc(Op));
2081 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
2083 if (!CallInfo.second.getNode())
2084 // It's a tailcall, return the chain (which is the DAG root).
2085 return DAG.getRoot();
2087 return CallInfo.first;
2091 AArch64TargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
2092 if (Op.getOperand(0).getValueType() != MVT::f128) {
2093 // It's legal except when f128 is involved
2098 LC = RTLIB::getFPROUND(Op.getOperand(0).getValueType(), Op.getValueType());
2100 SDValue SrcVal = Op.getOperand(0);
2101 return makeLibCall(DAG, LC, Op.getValueType(), &SrcVal, 1,
2102 /*isSigned*/ false, SDLoc(Op)).first;
2106 AArch64TargetLowering::LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const {
2107 assert(Op.getValueType() == MVT::f128 && "Unexpected lowering");
2110 LC = RTLIB::getFPEXT(Op.getOperand(0).getValueType(), Op.getValueType());
2112 return LowerF128ToCall(Op, DAG, LC);
2116 AArch64TargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
2117 bool IsSigned) const {
2118 if (Op.getOperand(0).getValueType() != MVT::f128) {
2119 // It's legal except when f128 is involved
2125 LC = RTLIB::getFPTOSINT(Op.getOperand(0).getValueType(), Op.getValueType());
2127 LC = RTLIB::getFPTOUINT(Op.getOperand(0).getValueType(), Op.getValueType());
2129 return LowerF128ToCall(Op, DAG, LC);
2132 SDValue AArch64TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
2133 MachineFunction &MF = DAG.getMachineFunction();
2134 MachineFrameInfo *MFI = MF.getFrameInfo();
2135 MFI->setReturnAddressIsTaken(true);
2137 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
2140 EVT VT = Op.getValueType();
2142 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2144 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
2145 SDValue Offset = DAG.getConstant(8, MVT::i64);
2146 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
2147 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
2148 MachinePointerInfo(), false, false, false, 0);
2151 // Return X30, which contains the return address. Mark it an implicit live-in.
2152 unsigned Reg = MF.addLiveIn(AArch64::X30, getRegClassFor(MVT::i64));
2153 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, MVT::i64);
2157 SDValue AArch64TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG)
2159 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2160 MFI->setFrameAddressIsTaken(true);
2162 EVT VT = Op.getValueType();
2164 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2165 unsigned FrameReg = AArch64::X29;
2166 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2168 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
2169 MachinePointerInfo(),
2170 false, false, false, 0);
2175 AArch64TargetLowering::LowerGlobalAddressELFLarge(SDValue Op,
2176 SelectionDAG &DAG) const {
2177 assert(getTargetMachine().getCodeModel() == CodeModel::Large);
2178 assert(getTargetMachine().getRelocationModel() == Reloc::Static);
2180 EVT PtrVT = getPointerTy();
2182 const GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Op);
2183 const GlobalValue *GV = GN->getGlobal();
2185 SDValue GlobalAddr = DAG.getNode(
2186 AArch64ISD::WrapperLarge, dl, PtrVT,
2187 DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, AArch64II::MO_ABS_G3),
2188 DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, AArch64II::MO_ABS_G2_NC),
2189 DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, AArch64II::MO_ABS_G1_NC),
2190 DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, AArch64II::MO_ABS_G0_NC));
2192 if (GN->getOffset() != 0)
2193 return DAG.getNode(ISD::ADD, dl, PtrVT, GlobalAddr,
2194 DAG.getConstant(GN->getOffset(), PtrVT));
2200 AArch64TargetLowering::LowerGlobalAddressELFSmall(SDValue Op,
2201 SelectionDAG &DAG) const {
2202 assert(getTargetMachine().getCodeModel() == CodeModel::Small);
2204 EVT PtrVT = getPointerTy();
2206 const GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Op);
2207 const GlobalValue *GV = GN->getGlobal();
2208 unsigned Alignment = GV->getAlignment();
2209 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2210 if (GV->isWeakForLinker() && GV->isDeclaration() && RelocM == Reloc::Static) {
2211 // Weak undefined symbols can't use ADRP/ADD pair since they should evaluate
2212 // to zero when they remain undefined. In PIC mode the GOT can take care of
2213 // this, but in absolute mode we use a constant pool load.
2215 PoolAddr = DAG.getNode(AArch64ISD::WrapperSmall, dl, PtrVT,
2216 DAG.getTargetConstantPool(GV, PtrVT, 0, 0,
2217 AArch64II::MO_NO_FLAG),
2218 DAG.getTargetConstantPool(GV, PtrVT, 0, 0,
2219 AArch64II::MO_LO12),
2220 DAG.getConstant(8, MVT::i32));
2221 SDValue GlobalAddr = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), PoolAddr,
2222 MachinePointerInfo::getConstantPool(),
2223 /*isVolatile=*/ false,
2224 /*isNonTemporal=*/ true,
2225 /*isInvariant=*/ true, 8);
2226 if (GN->getOffset() != 0)
2227 return DAG.getNode(ISD::ADD, dl, PtrVT, GlobalAddr,
2228 DAG.getConstant(GN->getOffset(), PtrVT));
2233 if (Alignment == 0) {
2234 const PointerType *GVPtrTy = cast<PointerType>(GV->getType());
2235 if (GVPtrTy->getElementType()->isSized()) {
2237 = getDataLayout()->getABITypeAlignment(GVPtrTy->getElementType());
2239 // Be conservative if we can't guess, not that it really matters:
2240 // functions and labels aren't valid for loads, and the methods used to
2241 // actually calculate an address work with any alignment.
2246 unsigned char HiFixup, LoFixup;
2247 bool UseGOT = getSubtarget()->GVIsIndirectSymbol(GV, RelocM);
2250 HiFixup = AArch64II::MO_GOT;
2251 LoFixup = AArch64II::MO_GOT_LO12;
2254 HiFixup = AArch64II::MO_NO_FLAG;
2255 LoFixup = AArch64II::MO_LO12;
2258 // AArch64's small model demands the following sequence:
2259 // ADRP x0, somewhere
2260 // ADD x0, x0, #:lo12:somewhere ; (or LDR directly).
2261 SDValue GlobalRef = DAG.getNode(AArch64ISD::WrapperSmall, dl, PtrVT,
2262 DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
2264 DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
2266 DAG.getConstant(Alignment, MVT::i32));
2269 GlobalRef = DAG.getNode(AArch64ISD::GOTLoad, dl, PtrVT, DAG.getEntryNode(),
2273 if (GN->getOffset() != 0)
2274 return DAG.getNode(ISD::ADD, dl, PtrVT, GlobalRef,
2275 DAG.getConstant(GN->getOffset(), PtrVT));
2281 AArch64TargetLowering::LowerGlobalAddressELF(SDValue Op,
2282 SelectionDAG &DAG) const {
2283 // TableGen doesn't have easy access to the CodeModel or RelocationModel, so
2284 // we make those distinctions here.
2286 switch (getTargetMachine().getCodeModel()) {
2287 case CodeModel::Small:
2288 return LowerGlobalAddressELFSmall(Op, DAG);
2289 case CodeModel::Large:
2290 return LowerGlobalAddressELFLarge(Op, DAG);
2292 llvm_unreachable("Only small and large code models supported now");
2297 AArch64TargetLowering::LowerConstantPool(SDValue Op,
2298 SelectionDAG &DAG) const {
2300 EVT PtrVT = getPointerTy();
2301 ConstantPoolSDNode *CN = cast<ConstantPoolSDNode>(Op);
2302 const Constant *C = CN->getConstVal();
2304 switch(getTargetMachine().getCodeModel()) {
2305 case CodeModel::Small:
2306 // The most efficient code is PC-relative anyway for the small memory model,
2307 // so we don't need to worry about relocation model.
2308 return DAG.getNode(AArch64ISD::WrapperSmall, DL, PtrVT,
2309 DAG.getTargetConstantPool(C, PtrVT, 0, 0,
2310 AArch64II::MO_NO_FLAG),
2311 DAG.getTargetConstantPool(C, PtrVT, 0, 0,
2312 AArch64II::MO_LO12),
2313 DAG.getConstant(CN->getAlignment(), MVT::i32));
2314 case CodeModel::Large:
2316 AArch64ISD::WrapperLarge, DL, PtrVT,
2317 DAG.getTargetConstantPool(C, PtrVT, 0, 0, AArch64II::MO_ABS_G3),
2318 DAG.getTargetConstantPool(C, PtrVT, 0, 0, AArch64II::MO_ABS_G2_NC),
2319 DAG.getTargetConstantPool(C, PtrVT, 0, 0, AArch64II::MO_ABS_G1_NC),
2320 DAG.getTargetConstantPool(C, PtrVT, 0, 0, AArch64II::MO_ABS_G0_NC));
2322 llvm_unreachable("Only small and large code models supported now");
2326 SDValue AArch64TargetLowering::LowerTLSDescCall(SDValue SymAddr,
2329 SelectionDAG &DAG) const {
2330 EVT PtrVT = getPointerTy();
2332 // The function we need to call is simply the first entry in the GOT for this
2333 // descriptor, load it in preparation.
2334 SDValue Func, Chain;
2335 Func = DAG.getNode(AArch64ISD::GOTLoad, DL, PtrVT, DAG.getEntryNode(),
2338 // The function takes only one argument: the address of the descriptor itself
2341 Chain = DAG.getCopyToReg(DAG.getEntryNode(), DL, AArch64::X0, DescAddr, Glue);
2342 Glue = Chain.getValue(1);
2344 // Finally, there's a special calling-convention which means that the lookup
2345 // must preserve all registers (except X0, obviously).
2346 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2347 const AArch64RegisterInfo *A64RI
2348 = static_cast<const AArch64RegisterInfo *>(TRI);
2349 const uint32_t *Mask = A64RI->getTLSDescCallPreservedMask();
2351 // We're now ready to populate the argument list, as with a normal call:
2352 std::vector<SDValue> Ops;
2353 Ops.push_back(Chain);
2354 Ops.push_back(Func);
2355 Ops.push_back(SymAddr);
2356 Ops.push_back(DAG.getRegister(AArch64::X0, PtrVT));
2357 Ops.push_back(DAG.getRegisterMask(Mask));
2358 Ops.push_back(Glue);
2360 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
2361 Chain = DAG.getNode(AArch64ISD::TLSDESCCALL, DL, NodeTys, &Ops[0],
2363 Glue = Chain.getValue(1);
2365 // After the call, the offset from TPIDR_EL0 is in X0, copy it out and pass it
2366 // back to the generic handling code.
2367 return DAG.getCopyFromReg(Chain, DL, AArch64::X0, PtrVT, Glue);
2371 AArch64TargetLowering::LowerGlobalTLSAddress(SDValue Op,
2372 SelectionDAG &DAG) const {
2373 assert(getSubtarget()->isTargetELF() &&
2374 "TLS not implemented for non-ELF targets");
2375 assert(getTargetMachine().getCodeModel() == CodeModel::Small
2376 && "TLS only supported in small memory model");
2377 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2379 TLSModel::Model Model = getTargetMachine().getTLSModel(GA->getGlobal());
2382 EVT PtrVT = getPointerTy();
2384 const GlobalValue *GV = GA->getGlobal();
2386 SDValue ThreadBase = DAG.getNode(AArch64ISD::THREAD_POINTER, DL, PtrVT);
2388 if (Model == TLSModel::InitialExec) {
2389 TPOff = DAG.getNode(AArch64ISD::WrapperSmall, DL, PtrVT,
2390 DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2391 AArch64II::MO_GOTTPREL),
2392 DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2393 AArch64II::MO_GOTTPREL_LO12),
2394 DAG.getConstant(8, MVT::i32));
2395 TPOff = DAG.getNode(AArch64ISD::GOTLoad, DL, PtrVT, DAG.getEntryNode(),
2397 } else if (Model == TLSModel::LocalExec) {
2398 SDValue HiVar = DAG.getTargetGlobalAddress(GV, DL, MVT::i64, 0,
2399 AArch64II::MO_TPREL_G1);
2400 SDValue LoVar = DAG.getTargetGlobalAddress(GV, DL, MVT::i64, 0,
2401 AArch64II::MO_TPREL_G0_NC);
2403 TPOff = SDValue(DAG.getMachineNode(AArch64::MOVZxii, DL, PtrVT, HiVar,
2404 DAG.getTargetConstant(1, MVT::i32)), 0);
2405 TPOff = SDValue(DAG.getMachineNode(AArch64::MOVKxii, DL, PtrVT,
2407 DAG.getTargetConstant(0, MVT::i32)), 0);
2408 } else if (Model == TLSModel::GeneralDynamic) {
2409 // Accesses used in this sequence go via the TLS descriptor which lives in
2410 // the GOT. Prepare an address we can use to handle this.
2411 SDValue HiDesc = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2412 AArch64II::MO_TLSDESC);
2413 SDValue LoDesc = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2414 AArch64II::MO_TLSDESC_LO12);
2415 SDValue DescAddr = DAG.getNode(AArch64ISD::WrapperSmall, DL, PtrVT,
2417 DAG.getConstant(8, MVT::i32));
2418 SDValue SymAddr = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0);
2420 TPOff = LowerTLSDescCall(SymAddr, DescAddr, DL, DAG);
2421 } else if (Model == TLSModel::LocalDynamic) {
2422 // Local-dynamic accesses proceed in two phases. A general-dynamic TLS
2423 // descriptor call against the special symbol _TLS_MODULE_BASE_ to calculate
2424 // the beginning of the module's TLS region, followed by a DTPREL offset
2427 // These accesses will need deduplicating if there's more than one.
2428 AArch64MachineFunctionInfo* MFI = DAG.getMachineFunction()
2429 .getInfo<AArch64MachineFunctionInfo>();
2430 MFI->incNumLocalDynamicTLSAccesses();
2433 // Get the location of _TLS_MODULE_BASE_:
2434 SDValue HiDesc = DAG.getTargetExternalSymbol("_TLS_MODULE_BASE_", PtrVT,
2435 AArch64II::MO_TLSDESC);
2436 SDValue LoDesc = DAG.getTargetExternalSymbol("_TLS_MODULE_BASE_", PtrVT,
2437 AArch64II::MO_TLSDESC_LO12);
2438 SDValue DescAddr = DAG.getNode(AArch64ISD::WrapperSmall, DL, PtrVT,
2440 DAG.getConstant(8, MVT::i32));
2441 SDValue SymAddr = DAG.getTargetExternalSymbol("_TLS_MODULE_BASE_", PtrVT);
2443 ThreadBase = LowerTLSDescCall(SymAddr, DescAddr, DL, DAG);
2445 // Get the variable's offset from _TLS_MODULE_BASE_
2446 SDValue HiVar = DAG.getTargetGlobalAddress(GV, DL, MVT::i64, 0,
2447 AArch64II::MO_DTPREL_G1);
2448 SDValue LoVar = DAG.getTargetGlobalAddress(GV, DL, MVT::i64, 0,
2449 AArch64II::MO_DTPREL_G0_NC);
2451 TPOff = SDValue(DAG.getMachineNode(AArch64::MOVZxii, DL, PtrVT, HiVar,
2452 DAG.getTargetConstant(0, MVT::i32)), 0);
2453 TPOff = SDValue(DAG.getMachineNode(AArch64::MOVKxii, DL, PtrVT,
2455 DAG.getTargetConstant(0, MVT::i32)), 0);
2457 llvm_unreachable("Unsupported TLS access model");
2460 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadBase, TPOff);
2464 AArch64TargetLowering::LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG,
2465 bool IsSigned) const {
2466 if (Op.getValueType() != MVT::f128) {
2467 // Legal for everything except f128.
2473 LC = RTLIB::getSINTTOFP(Op.getOperand(0).getValueType(), Op.getValueType());
2475 LC = RTLIB::getUINTTOFP(Op.getOperand(0).getValueType(), Op.getValueType());
2477 return LowerF128ToCall(Op, DAG, LC);
2482 AArch64TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
2483 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
2485 EVT PtrVT = getPointerTy();
2487 // When compiling PIC, jump tables get put in the code section so a static
2488 // relocation-style is acceptable for both cases.
2489 switch (getTargetMachine().getCodeModel()) {
2490 case CodeModel::Small:
2491 return DAG.getNode(AArch64ISD::WrapperSmall, dl, PtrVT,
2492 DAG.getTargetJumpTable(JT->getIndex(), PtrVT),
2493 DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
2494 AArch64II::MO_LO12),
2495 DAG.getConstant(1, MVT::i32));
2496 case CodeModel::Large:
2498 AArch64ISD::WrapperLarge, dl, PtrVT,
2499 DAG.getTargetJumpTable(JT->getIndex(), PtrVT, AArch64II::MO_ABS_G3),
2500 DAG.getTargetJumpTable(JT->getIndex(), PtrVT, AArch64II::MO_ABS_G2_NC),
2501 DAG.getTargetJumpTable(JT->getIndex(), PtrVT, AArch64II::MO_ABS_G1_NC),
2502 DAG.getTargetJumpTable(JT->getIndex(), PtrVT, AArch64II::MO_ABS_G0_NC));
2504 llvm_unreachable("Only small and large code models supported now");
2508 // (SELECT_CC lhs, rhs, iftrue, iffalse, condcode)
2510 AArch64TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
2512 SDValue LHS = Op.getOperand(0);
2513 SDValue RHS = Op.getOperand(1);
2514 SDValue IfTrue = Op.getOperand(2);
2515 SDValue IfFalse = Op.getOperand(3);
2516 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
2518 if (LHS.getValueType() == MVT::f128) {
2519 // f128 comparisons are lowered to libcalls, but slot in nicely here
2521 softenSetCCOperands(DAG, MVT::f128, LHS, RHS, CC, dl);
2523 // If softenSetCCOperands returned a scalar, we need to compare the result
2524 // against zero to select between true and false values.
2525 if (RHS.getNode() == 0) {
2526 RHS = DAG.getConstant(0, LHS.getValueType());
2531 if (LHS.getValueType().isInteger()) {
2534 // Integers are handled in a separate function because the combinations of
2535 // immediates and tests can get hairy and we may want to fiddle things.
2536 SDValue CmpOp = getSelectableIntSetCC(LHS, RHS, CC, A64cc, DAG, dl);
2538 return DAG.getNode(AArch64ISD::SELECT_CC, dl, Op.getValueType(),
2539 CmpOp, IfTrue, IfFalse, A64cc);
2542 // Note that some LLVM floating-point CondCodes can't be lowered to a single
2543 // conditional branch, hence FPCCToA64CC can set a second test, where either
2544 // passing is sufficient.
2545 A64CC::CondCodes CondCode, Alternative = A64CC::Invalid;
2546 CondCode = FPCCToA64CC(CC, Alternative);
2547 SDValue A64cc = DAG.getConstant(CondCode, MVT::i32);
2548 SDValue SetCC = DAG.getNode(AArch64ISD::SETCC, dl, MVT::i32, LHS, RHS,
2549 DAG.getCondCode(CC));
2550 SDValue A64SELECT_CC = DAG.getNode(AArch64ISD::SELECT_CC, dl,
2552 SetCC, IfTrue, IfFalse, A64cc);
2554 if (Alternative != A64CC::Invalid) {
2555 A64cc = DAG.getConstant(Alternative, MVT::i32);
2556 A64SELECT_CC = DAG.getNode(AArch64ISD::SELECT_CC, dl, Op.getValueType(),
2557 SetCC, IfTrue, A64SELECT_CC, A64cc);
2561 return A64SELECT_CC;
2564 // (SELECT testbit, iftrue, iffalse)
2566 AArch64TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2568 SDValue TheBit = Op.getOperand(0);
2569 SDValue IfTrue = Op.getOperand(1);
2570 SDValue IfFalse = Op.getOperand(2);
2572 // AArch64 BooleanContents is the default UndefinedBooleanContent, which means
2573 // that as the consumer we are responsible for ignoring rubbish in higher
2575 TheBit = DAG.getNode(ISD::AND, dl, MVT::i32, TheBit,
2576 DAG.getConstant(1, MVT::i32));
2577 SDValue A64CMP = DAG.getNode(AArch64ISD::SETCC, dl, MVT::i32, TheBit,
2578 DAG.getConstant(0, TheBit.getValueType()),
2579 DAG.getCondCode(ISD::SETNE));
2581 return DAG.getNode(AArch64ISD::SELECT_CC, dl, Op.getValueType(),
2582 A64CMP, IfTrue, IfFalse,
2583 DAG.getConstant(A64CC::NE, MVT::i32));
2586 static SDValue LowerVectorSETCC(SDValue Op, SelectionDAG &DAG) {
2588 SDValue LHS = Op.getOperand(0);
2589 SDValue RHS = Op.getOperand(1);
2590 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
2591 EVT VT = Op.getValueType();
2592 bool Invert = false;
2596 if (LHS.getValueType().isInteger()) {
2598 // Attempt to use Vector Integer Compare Mask Test instruction.
2599 // TST = icmp ne (and (op0, op1), zero).
2600 if (CC == ISD::SETNE) {
2601 if (((LHS.getOpcode() == ISD::AND) &&
2602 ISD::isBuildVectorAllZeros(RHS.getNode())) ||
2603 ((RHS.getOpcode() == ISD::AND) &&
2604 ISD::isBuildVectorAllZeros(LHS.getNode()))) {
2606 SDValue AndOp = (LHS.getOpcode() == ISD::AND) ? LHS : RHS;
2607 SDValue NewLHS = DAG.getNode(ISD::BITCAST, DL, VT, AndOp.getOperand(0));
2608 SDValue NewRHS = DAG.getNode(ISD::BITCAST, DL, VT, AndOp.getOperand(1));
2609 return DAG.getNode(AArch64ISD::NEON_TST, DL, VT, NewLHS, NewRHS);
2613 // Attempt to use Vector Integer Compare Mask against Zero instr (Signed).
2614 // Note: Compare against Zero does not support unsigned predicates.
2615 if ((ISD::isBuildVectorAllZeros(RHS.getNode()) ||
2616 ISD::isBuildVectorAllZeros(LHS.getNode())) &&
2617 !isUnsignedIntSetCC(CC)) {
2619 // If LHS is the zero value, swap operands and CondCode.
2620 if (ISD::isBuildVectorAllZeros(LHS.getNode())) {
2621 CC = getSetCCSwappedOperands(CC);
2626 // Ensure valid CondCode for Compare Mask against Zero instruction:
2627 // EQ, GE, GT, LE, LT.
2628 if (ISD::SETNE == CC) {
2633 // Using constant type to differentiate integer and FP compares with zero.
2634 Op1 = DAG.getConstant(0, MVT::i32);
2635 Opcode = AArch64ISD::NEON_CMPZ;
2638 // Attempt to use Vector Integer Compare Mask instr (Signed/Unsigned).
2639 // Ensure valid CondCode for Compare Mask instr: EQ, GE, GT, UGE, UGT.
2643 llvm_unreachable("Illegal integer comparison.");
2659 CC = getSetCCSwappedOperands(CC);
2663 std::swap(LHS, RHS);
2665 Opcode = AArch64ISD::NEON_CMP;
2670 // Generate Compare Mask instr or Compare Mask against Zero instr.
2672 DAG.getNode(Opcode, DL, VT, Op0, Op1, DAG.getCondCode(CC));
2675 NeonCmp = DAG.getNOT(DL, NeonCmp, VT);
2680 // Now handle Floating Point cases.
2681 // Attempt to use Vector Floating Point Compare Mask against Zero instruction.
2682 if (ISD::isBuildVectorAllZeros(RHS.getNode()) ||
2683 ISD::isBuildVectorAllZeros(LHS.getNode())) {
2685 // If LHS is the zero value, swap operands and CondCode.
2686 if (ISD::isBuildVectorAllZeros(LHS.getNode())) {
2687 CC = getSetCCSwappedOperands(CC);
2692 // Using constant type to differentiate integer and FP compares with zero.
2693 Op1 = DAG.getConstantFP(0, MVT::f32);
2694 Opcode = AArch64ISD::NEON_CMPZ;
2696 // Attempt to use Vector Floating Point Compare Mask instruction.
2699 Opcode = AArch64ISD::NEON_CMP;
2703 // Some register compares have to be implemented with swapped CC and operands,
2704 // e.g.: OLT implemented as OGT with swapped operands.
2705 bool SwapIfRegArgs = false;
2707 // Ensure valid CondCode for FP Compare Mask against Zero instruction:
2708 // EQ, GE, GT, LE, LT.
2709 // And ensure valid CondCode for FP Compare Mask instruction: EQ, GE, GT.
2712 llvm_unreachable("Illegal FP comparison");
2715 Invert = true; // Fallthrough
2723 SwapIfRegArgs = true;
2732 SwapIfRegArgs = true;
2741 SwapIfRegArgs = true;
2750 SwapIfRegArgs = true;
2757 Invert = true; // Fallthrough
2759 // Expand this to (OGT |OLT).
2761 DAG.getNode(Opcode, DL, VT, Op0, Op1, DAG.getCondCode(ISD::SETGT));
2763 SwapIfRegArgs = true;
2766 Invert = true; // Fallthrough
2768 // Expand this to (OGE | OLT).
2770 DAG.getNode(Opcode, DL, VT, Op0, Op1, DAG.getCondCode(ISD::SETGE));
2772 SwapIfRegArgs = true;
2776 if (Opcode == AArch64ISD::NEON_CMP && SwapIfRegArgs) {
2777 CC = getSetCCSwappedOperands(CC);
2778 std::swap(Op0, Op1);
2781 // Generate FP Compare Mask instr or FP Compare Mask against Zero instr
2782 SDValue NeonCmp = DAG.getNode(Opcode, DL, VT, Op0, Op1, DAG.getCondCode(CC));
2784 if (NeonCmpAlt.getNode())
2785 NeonCmp = DAG.getNode(ISD::OR, DL, VT, NeonCmp, NeonCmpAlt);
2788 NeonCmp = DAG.getNOT(DL, NeonCmp, VT);
2793 // (SETCC lhs, rhs, condcode)
2795 AArch64TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
2797 SDValue LHS = Op.getOperand(0);
2798 SDValue RHS = Op.getOperand(1);
2799 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
2800 EVT VT = Op.getValueType();
2803 return LowerVectorSETCC(Op, DAG);
2805 if (LHS.getValueType() == MVT::f128) {
2806 // f128 comparisons will be lowered to libcalls giving a valid LHS and RHS
2807 // for the rest of the function (some i32 or i64 values).
2808 softenSetCCOperands(DAG, MVT::f128, LHS, RHS, CC, dl);
2810 // If softenSetCCOperands returned a scalar, use it.
2811 if (RHS.getNode() == 0) {
2812 assert(LHS.getValueType() == Op.getValueType() &&
2813 "Unexpected setcc expansion!");
2818 if (LHS.getValueType().isInteger()) {
2821 // Integers are handled in a separate function because the combinations of
2822 // immediates and tests can get hairy and we may want to fiddle things.
2823 SDValue CmpOp = getSelectableIntSetCC(LHS, RHS, CC, A64cc, DAG, dl);
2825 return DAG.getNode(AArch64ISD::SELECT_CC, dl, VT,
2826 CmpOp, DAG.getConstant(1, VT), DAG.getConstant(0, VT),
2830 // Note that some LLVM floating-point CondCodes can't be lowered to a single
2831 // conditional branch, hence FPCCToA64CC can set a second test, where either
2832 // passing is sufficient.
2833 A64CC::CondCodes CondCode, Alternative = A64CC::Invalid;
2834 CondCode = FPCCToA64CC(CC, Alternative);
2835 SDValue A64cc = DAG.getConstant(CondCode, MVT::i32);
2836 SDValue CmpOp = DAG.getNode(AArch64ISD::SETCC, dl, MVT::i32, LHS, RHS,
2837 DAG.getCondCode(CC));
2838 SDValue A64SELECT_CC = DAG.getNode(AArch64ISD::SELECT_CC, dl, VT,
2839 CmpOp, DAG.getConstant(1, VT),
2840 DAG.getConstant(0, VT), A64cc);
2842 if (Alternative != A64CC::Invalid) {
2843 A64cc = DAG.getConstant(Alternative, MVT::i32);
2844 A64SELECT_CC = DAG.getNode(AArch64ISD::SELECT_CC, dl, VT, CmpOp,
2845 DAG.getConstant(1, VT), A64SELECT_CC, A64cc);
2848 return A64SELECT_CC;
2852 AArch64TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
2853 const Value *DestSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
2854 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
2856 // We have to make sure we copy the entire structure: 8+8+8+4+4 = 32 bytes
2857 // rather than just 8.
2858 return DAG.getMemcpy(Op.getOperand(0), SDLoc(Op),
2859 Op.getOperand(1), Op.getOperand(2),
2860 DAG.getConstant(32, MVT::i32), 8, false, false,
2861 MachinePointerInfo(DestSV), MachinePointerInfo(SrcSV));
2865 AArch64TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
2866 // The layout of the va_list struct is specified in the AArch64 Procedure Call
2867 // Standard, section B.3.
2868 MachineFunction &MF = DAG.getMachineFunction();
2869 AArch64MachineFunctionInfo *FuncInfo
2870 = MF.getInfo<AArch64MachineFunctionInfo>();
2873 SDValue Chain = Op.getOperand(0);
2874 SDValue VAList = Op.getOperand(1);
2875 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
2876 SmallVector<SDValue, 4> MemOps;
2878 // void *__stack at offset 0
2879 SDValue Stack = DAG.getFrameIndex(FuncInfo->getVariadicStackIdx(),
2881 MemOps.push_back(DAG.getStore(Chain, DL, Stack, VAList,
2882 MachinePointerInfo(SV), false, false, 0));
2884 // void *__gr_top at offset 8
2885 int GPRSize = FuncInfo->getVariadicGPRSize();
2887 SDValue GRTop, GRTopAddr;
2889 GRTopAddr = DAG.getNode(ISD::ADD, DL, getPointerTy(), VAList,
2890 DAG.getConstant(8, getPointerTy()));
2892 GRTop = DAG.getFrameIndex(FuncInfo->getVariadicGPRIdx(), getPointerTy());
2893 GRTop = DAG.getNode(ISD::ADD, DL, getPointerTy(), GRTop,
2894 DAG.getConstant(GPRSize, getPointerTy()));
2896 MemOps.push_back(DAG.getStore(Chain, DL, GRTop, GRTopAddr,
2897 MachinePointerInfo(SV, 8),
2901 // void *__vr_top at offset 16
2902 int FPRSize = FuncInfo->getVariadicFPRSize();
2904 SDValue VRTop, VRTopAddr;
2905 VRTopAddr = DAG.getNode(ISD::ADD, DL, getPointerTy(), VAList,
2906 DAG.getConstant(16, getPointerTy()));
2908 VRTop = DAG.getFrameIndex(FuncInfo->getVariadicFPRIdx(), getPointerTy());
2909 VRTop = DAG.getNode(ISD::ADD, DL, getPointerTy(), VRTop,
2910 DAG.getConstant(FPRSize, getPointerTy()));
2912 MemOps.push_back(DAG.getStore(Chain, DL, VRTop, VRTopAddr,
2913 MachinePointerInfo(SV, 16),
2917 // int __gr_offs at offset 24
2918 SDValue GROffsAddr = DAG.getNode(ISD::ADD, DL, getPointerTy(), VAList,
2919 DAG.getConstant(24, getPointerTy()));
2920 MemOps.push_back(DAG.getStore(Chain, DL, DAG.getConstant(-GPRSize, MVT::i32),
2921 GROffsAddr, MachinePointerInfo(SV, 24),
2924 // int __vr_offs at offset 28
2925 SDValue VROffsAddr = DAG.getNode(ISD::ADD, DL, getPointerTy(), VAList,
2926 DAG.getConstant(28, getPointerTy()));
2927 MemOps.push_back(DAG.getStore(Chain, DL, DAG.getConstant(-FPRSize, MVT::i32),
2928 VROffsAddr, MachinePointerInfo(SV, 28),
2931 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, &MemOps[0],
2936 AArch64TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
2937 switch (Op.getOpcode()) {
2938 default: llvm_unreachable("Don't know how to custom lower this!");
2939 case ISD::FADD: return LowerF128ToCall(Op, DAG, RTLIB::ADD_F128);
2940 case ISD::FSUB: return LowerF128ToCall(Op, DAG, RTLIB::SUB_F128);
2941 case ISD::FMUL: return LowerF128ToCall(Op, DAG, RTLIB::MUL_F128);
2942 case ISD::FDIV: return LowerF128ToCall(Op, DAG, RTLIB::DIV_F128);
2943 case ISD::FP_TO_SINT: return LowerFP_TO_INT(Op, DAG, true);
2944 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG, false);
2945 case ISD::SINT_TO_FP: return LowerINT_TO_FP(Op, DAG, true);
2946 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG, false);
2947 case ISD::FP_ROUND: return LowerFP_ROUND(Op, DAG);
2948 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
2949 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
2950 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
2952 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
2953 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
2954 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
2955 case ISD::GlobalAddress: return LowerGlobalAddressELF(Op, DAG);
2956 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
2957 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
2958 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
2959 case ISD::SELECT: return LowerSELECT(Op, DAG);
2960 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
2961 case ISD::SETCC: return LowerSETCC(Op, DAG);
2962 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
2963 case ISD::VASTART: return LowerVASTART(Op, DAG);
2964 case ISD::BUILD_VECTOR:
2965 return LowerBUILD_VECTOR(Op, DAG, getSubtarget());
2966 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
2972 /// Check if the specified splat value corresponds to a valid vector constant
2973 /// for a Neon instruction with a "modified immediate" operand (e.g., MOVI). If
2974 /// so, return the encoded 8-bit immediate and the OpCmode instruction fields
2976 static bool isNeonModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
2977 unsigned SplatBitSize, SelectionDAG &DAG,
2978 bool is128Bits, NeonModImmType type, EVT &VT,
2979 unsigned &Imm, unsigned &OpCmode) {
2980 switch (SplatBitSize) {
2982 llvm_unreachable("unexpected size for isNeonModifiedImm");
2984 if (type != Neon_Mov_Imm)
2986 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
2987 // Neon movi per byte: Op=0, Cmode=1110.
2990 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
2994 // Neon move inst per halfword
2995 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
2996 if ((SplatBits & ~0xff) == 0) {
2997 // Value = 0x00nn is 0x00nn LSL 0
2998 // movi: Op=0, Cmode=1000; mvni: Op=1, Cmode=1000
2999 // bic: Op=1, Cmode=1001; orr: Op=0, Cmode=1001
3005 if ((SplatBits & ~0xff00) == 0) {
3006 // Value = 0xnn00 is 0x00nn LSL 8
3007 // movi: Op=0, Cmode=1010; mvni: Op=1, Cmode=1010
3008 // bic: Op=1, Cmode=1011; orr: Op=0, Cmode=1011
3010 Imm = SplatBits >> 8;
3014 // can't handle any other
3019 // First the LSL variants (MSL is unusable by some interested instructions).
3021 // Neon move instr per word, shift zeros
3022 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
3023 if ((SplatBits & ~0xff) == 0) {
3024 // Value = 0x000000nn is 0x000000nn LSL 0
3025 // movi: Op=0, Cmode= 0000; mvni: Op=1, Cmode= 0000
3026 // bic: Op=1, Cmode= 0001; orr: Op=0, Cmode= 0001
3032 if ((SplatBits & ~0xff00) == 0) {
3033 // Value = 0x0000nn00 is 0x000000nn LSL 8
3034 // movi: Op=0, Cmode= 0010; mvni: Op=1, Cmode= 0010
3035 // bic: Op=1, Cmode= 0011; orr : Op=0, Cmode= 0011
3037 Imm = SplatBits >> 8;
3041 if ((SplatBits & ~0xff0000) == 0) {
3042 // Value = 0x00nn0000 is 0x000000nn LSL 16
3043 // movi: Op=0, Cmode= 0100; mvni: Op=1, Cmode= 0100
3044 // bic: Op=1, Cmode= 0101; orr: Op=0, Cmode= 0101
3046 Imm = SplatBits >> 16;
3050 if ((SplatBits & ~0xff000000) == 0) {
3051 // Value = 0xnn000000 is 0x000000nn LSL 24
3052 // movi: Op=0, Cmode= 0110; mvni: Op=1, Cmode= 0110
3053 // bic: Op=1, Cmode= 0111; orr: Op=0, Cmode= 0111
3055 Imm = SplatBits >> 24;
3060 // Now the MSL immediates.
3062 // Neon move instr per word, shift ones
3063 if ((SplatBits & ~0xffff) == 0 &&
3064 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3065 // Value = 0x0000nnff is 0x000000nn MSL 8
3066 // movi: Op=0, Cmode= 1100; mvni: Op=1, Cmode= 1100
3068 Imm = SplatBits >> 8;
3072 if ((SplatBits & ~0xffffff) == 0 &&
3073 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3074 // Value = 0x00nnffff is 0x000000nn MSL 16
3075 // movi: Op=1, Cmode= 1101; mvni: Op=1, Cmode= 1101
3077 Imm = SplatBits >> 16;
3081 // can't handle any other
3086 if (type != Neon_Mov_Imm)
3088 // Neon move instr bytemask, where each byte is either 0x00 or 0xff.
3089 // movi Op=1, Cmode=1110.
3091 uint64_t BitMask = 0xff;
3093 unsigned ImmMask = 1;
3095 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
3096 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
3099 } else if ((SplatBits & BitMask) != 0) {
3106 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
3114 static SDValue PerformANDCombine(SDNode *N,
3115 TargetLowering::DAGCombinerInfo &DCI) {
3117 SelectionDAG &DAG = DCI.DAG;
3119 EVT VT = N->getValueType(0);
3121 // We're looking for an SRA/SHL pair which form an SBFX.
3123 if (VT != MVT::i32 && VT != MVT::i64)
3126 if (!isa<ConstantSDNode>(N->getOperand(1)))
3129 uint64_t TruncMask = N->getConstantOperandVal(1);
3130 if (!isMask_64(TruncMask))
3133 uint64_t Width = CountPopulation_64(TruncMask);
3134 SDValue Shift = N->getOperand(0);
3136 if (Shift.getOpcode() != ISD::SRL)
3139 if (!isa<ConstantSDNode>(Shift->getOperand(1)))
3141 uint64_t LSB = Shift->getConstantOperandVal(1);
3143 if (LSB > VT.getSizeInBits() || Width > VT.getSizeInBits())
3146 return DAG.getNode(AArch64ISD::UBFX, DL, VT, Shift.getOperand(0),
3147 DAG.getConstant(LSB, MVT::i64),
3148 DAG.getConstant(LSB + Width - 1, MVT::i64));
3151 /// For a true bitfield insert, the bits getting into that contiguous mask
3152 /// should come from the low part of an existing value: they must be formed from
3153 /// a compatible SHL operation (unless they're already low). This function
3154 /// checks that condition and returns the least-significant bit that's
3155 /// intended. If the operation not a field preparation, -1 is returned.
3156 static int32_t getLSBForBFI(SelectionDAG &DAG, SDLoc DL, EVT VT,
3157 SDValue &MaskedVal, uint64_t Mask) {
3158 if (!isShiftedMask_64(Mask))
3161 // Now we need to alter MaskedVal so that it is an appropriate input for a BFI
3162 // instruction. BFI will do a left-shift by LSB before applying the mask we've
3163 // spotted, so in general we should pre-emptively "undo" that by making sure
3164 // the incoming bits have had a right-shift applied to them.
3166 // This right shift, however, will combine with existing left/right shifts. In
3167 // the simplest case of a completely straight bitfield operation, it will be
3168 // expected to completely cancel out with an existing SHL. More complicated
3169 // cases (e.g. bitfield to bitfield copy) may still need a real shift before
3172 uint64_t LSB = countTrailingZeros(Mask);
3173 int64_t ShiftRightRequired = LSB;
3174 if (MaskedVal.getOpcode() == ISD::SHL &&
3175 isa<ConstantSDNode>(MaskedVal.getOperand(1))) {
3176 ShiftRightRequired -= MaskedVal.getConstantOperandVal(1);
3177 MaskedVal = MaskedVal.getOperand(0);
3178 } else if (MaskedVal.getOpcode() == ISD::SRL &&
3179 isa<ConstantSDNode>(MaskedVal.getOperand(1))) {
3180 ShiftRightRequired += MaskedVal.getConstantOperandVal(1);
3181 MaskedVal = MaskedVal.getOperand(0);
3184 if (ShiftRightRequired > 0)
3185 MaskedVal = DAG.getNode(ISD::SRL, DL, VT, MaskedVal,
3186 DAG.getConstant(ShiftRightRequired, MVT::i64));
3187 else if (ShiftRightRequired < 0) {
3188 // We could actually end up with a residual left shift, for example with
3189 // "struc.bitfield = val << 1".
3190 MaskedVal = DAG.getNode(ISD::SHL, DL, VT, MaskedVal,
3191 DAG.getConstant(-ShiftRightRequired, MVT::i64));
3197 /// Searches from N for an existing AArch64ISD::BFI node, possibly surrounded by
3198 /// a mask and an extension. Returns true if a BFI was found and provides
3199 /// information on its surroundings.
3200 static bool findMaskedBFI(SDValue N, SDValue &BFI, uint64_t &Mask,
3203 if (N.getOpcode() == ISD::ZERO_EXTEND) {
3205 N = N.getOperand(0);
3208 if (N.getOpcode() == ISD::AND && isa<ConstantSDNode>(N.getOperand(1))) {
3209 Mask = N->getConstantOperandVal(1);
3210 N = N.getOperand(0);
3212 // Mask is the whole width.
3213 Mask = -1ULL >> (64 - N.getValueType().getSizeInBits());
3216 if (N.getOpcode() == AArch64ISD::BFI) {
3224 /// Try to combine a subtree (rooted at an OR) into a "masked BFI" node, which
3225 /// is roughly equivalent to (and (BFI ...), mask). This form is used because it
3226 /// can often be further combined with a larger mask. Ultimately, we want mask
3227 /// to be 2^32-1 or 2^64-1 so the AND can be skipped.
3228 static SDValue tryCombineToBFI(SDNode *N,
3229 TargetLowering::DAGCombinerInfo &DCI,
3230 const AArch64Subtarget *Subtarget) {
3231 SelectionDAG &DAG = DCI.DAG;
3233 EVT VT = N->getValueType(0);
3235 assert(N->getOpcode() == ISD::OR && "Unexpected root");
3237 // We need the LHS to be (and SOMETHING, MASK). Find out what that mask is or
3238 // abandon the effort.
3239 SDValue LHS = N->getOperand(0);
3240 if (LHS.getOpcode() != ISD::AND)
3244 if (isa<ConstantSDNode>(LHS.getOperand(1)))
3245 LHSMask = LHS->getConstantOperandVal(1);
3249 // We also need the RHS to be (and SOMETHING, MASK). Find out what that mask
3250 // is or abandon the effort.
3251 SDValue RHS = N->getOperand(1);
3252 if (RHS.getOpcode() != ISD::AND)
3256 if (isa<ConstantSDNode>(RHS.getOperand(1)))
3257 RHSMask = RHS->getConstantOperandVal(1);
3261 // Can't do anything if the masks are incompatible.
3262 if (LHSMask & RHSMask)
3265 // Now we need one of the masks to be a contiguous field. Without loss of
3266 // generality that should be the RHS one.
3267 SDValue Bitfield = LHS.getOperand(0);
3268 if (getLSBForBFI(DAG, DL, VT, Bitfield, LHSMask) != -1) {
3269 // We know that LHS is a candidate new value, and RHS isn't already a better
3271 std::swap(LHS, RHS);
3272 std::swap(LHSMask, RHSMask);
3275 // We've done our best to put the right operands in the right places, all we
3276 // can do now is check whether a BFI exists.
3277 Bitfield = RHS.getOperand(0);
3278 int32_t LSB = getLSBForBFI(DAG, DL, VT, Bitfield, RHSMask);
3282 uint32_t Width = CountPopulation_64(RHSMask);
3283 assert(Width && "Expected non-zero bitfield width");
3285 SDValue BFI = DAG.getNode(AArch64ISD::BFI, DL, VT,
3286 LHS.getOperand(0), Bitfield,
3287 DAG.getConstant(LSB, MVT::i64),
3288 DAG.getConstant(Width, MVT::i64));
3291 if ((LHSMask | RHSMask) == (-1ULL >> (64 - VT.getSizeInBits())))
3294 return DAG.getNode(ISD::AND, DL, VT, BFI,
3295 DAG.getConstant(LHSMask | RHSMask, VT));
3298 /// Search for the bitwise combining (with careful masks) of a MaskedBFI and its
3299 /// original input. This is surprisingly common because SROA splits things up
3300 /// into i8 chunks, so the originally detected MaskedBFI may actually only act
3301 /// on the low (say) byte of a word. This is then orred into the rest of the
3302 /// word afterwards.
3304 /// Basic input: (or (and OLDFIELD, MASK1), (MaskedBFI MASK2, OLDFIELD, ...)).
3306 /// If MASK1 and MASK2 are compatible, we can fold the whole thing into the
3307 /// MaskedBFI. We can also deal with a certain amount of extend/truncate being
3309 static SDValue tryCombineToLargerBFI(SDNode *N,
3310 TargetLowering::DAGCombinerInfo &DCI,
3311 const AArch64Subtarget *Subtarget) {
3312 SelectionDAG &DAG = DCI.DAG;
3314 EVT VT = N->getValueType(0);
3316 // First job is to hunt for a MaskedBFI on either the left or right. Swap
3317 // operands if it's actually on the right.
3319 SDValue PossExtraMask;
3320 uint64_t ExistingMask = 0;
3321 bool Extended = false;
3322 if (findMaskedBFI(N->getOperand(0), BFI, ExistingMask, Extended))
3323 PossExtraMask = N->getOperand(1);
3324 else if (findMaskedBFI(N->getOperand(1), BFI, ExistingMask, Extended))
3325 PossExtraMask = N->getOperand(0);
3329 // We can only combine a BFI with another compatible mask.
3330 if (PossExtraMask.getOpcode() != ISD::AND ||
3331 !isa<ConstantSDNode>(PossExtraMask.getOperand(1)))
3334 uint64_t ExtraMask = PossExtraMask->getConstantOperandVal(1);
3336 // Masks must be compatible.
3337 if (ExtraMask & ExistingMask)
3340 SDValue OldBFIVal = BFI.getOperand(0);
3341 SDValue NewBFIVal = BFI.getOperand(1);
3343 // We skipped a ZERO_EXTEND above, so the input to the MaskedBFIs should be
3344 // 32-bit and we'll be forming a 64-bit MaskedBFI. The MaskedBFI arguments
3345 // need to be made compatible.
3346 assert(VT == MVT::i64 && BFI.getValueType() == MVT::i32
3347 && "Invalid types for BFI");
3348 OldBFIVal = DAG.getNode(ISD::ANY_EXTEND, DL, VT, OldBFIVal);
3349 NewBFIVal = DAG.getNode(ISD::ANY_EXTEND, DL, VT, NewBFIVal);
3352 // We need the MaskedBFI to be combined with a mask of the *same* value.
3353 if (PossExtraMask.getOperand(0) != OldBFIVal)
3356 BFI = DAG.getNode(AArch64ISD::BFI, DL, VT,
3357 OldBFIVal, NewBFIVal,
3358 BFI.getOperand(2), BFI.getOperand(3));
3360 // If the masking is trivial, we don't need to create it.
3361 if ((ExtraMask | ExistingMask) == (-1ULL >> (64 - VT.getSizeInBits())))
3364 return DAG.getNode(ISD::AND, DL, VT, BFI,
3365 DAG.getConstant(ExtraMask | ExistingMask, VT));
3368 /// An EXTR instruction is made up of two shifts, ORed together. This helper
3369 /// searches for and classifies those shifts.
3370 static bool findEXTRHalf(SDValue N, SDValue &Src, uint32_t &ShiftAmount,
3372 if (N.getOpcode() == ISD::SHL)
3374 else if (N.getOpcode() == ISD::SRL)
3379 if (!isa<ConstantSDNode>(N.getOperand(1)))
3382 ShiftAmount = N->getConstantOperandVal(1);
3383 Src = N->getOperand(0);
3387 /// EXTR instruction extracts a contiguous chunk of bits from two existing
3388 /// registers viewed as a high/low pair. This function looks for the pattern:
3389 /// (or (shl VAL1, #N), (srl VAL2, #RegWidth-N)) and replaces it with an
3390 /// EXTR. Can't quite be done in TableGen because the two immediates aren't
3392 static SDValue tryCombineToEXTR(SDNode *N,
3393 TargetLowering::DAGCombinerInfo &DCI) {
3394 SelectionDAG &DAG = DCI.DAG;
3396 EVT VT = N->getValueType(0);
3398 assert(N->getOpcode() == ISD::OR && "Unexpected root");
3400 if (VT != MVT::i32 && VT != MVT::i64)
3404 uint32_t ShiftLHS = 0;
3406 if (!findEXTRHalf(N->getOperand(0), LHS, ShiftLHS, LHSFromHi))
3410 uint32_t ShiftRHS = 0;
3412 if (!findEXTRHalf(N->getOperand(1), RHS, ShiftRHS, RHSFromHi))
3415 // If they're both trying to come from the high part of the register, they're
3416 // not really an EXTR.
3417 if (LHSFromHi == RHSFromHi)
3420 if (ShiftLHS + ShiftRHS != VT.getSizeInBits())
3424 std::swap(LHS, RHS);
3425 std::swap(ShiftLHS, ShiftRHS);
3428 return DAG.getNode(AArch64ISD::EXTR, DL, VT,
3430 DAG.getConstant(ShiftRHS, MVT::i64));
3433 /// Target-specific dag combine xforms for ISD::OR
3434 static SDValue PerformORCombine(SDNode *N,
3435 TargetLowering::DAGCombinerInfo &DCI,
3436 const AArch64Subtarget *Subtarget) {
3438 SelectionDAG &DAG = DCI.DAG;
3440 EVT VT = N->getValueType(0);
3442 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
3445 // Attempt to recognise bitfield-insert operations.
3446 SDValue Res = tryCombineToBFI(N, DCI, Subtarget);
3450 // Attempt to combine an existing MaskedBFI operation into one with a larger
3452 Res = tryCombineToLargerBFI(N, DCI, Subtarget);
3456 Res = tryCombineToEXTR(N, DCI);
3460 if (!Subtarget->hasNEON())
3463 // Attempt to use vector immediate-form BSL
3464 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
3466 SDValue N0 = N->getOperand(0);
3467 if (N0.getOpcode() != ISD::AND)
3470 SDValue N1 = N->getOperand(1);
3471 if (N1.getOpcode() != ISD::AND)
3474 if (VT.isVector() && DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
3476 unsigned SplatBitSize;
3478 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
3480 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
3483 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
3485 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
3486 HasAnyUndefs) && !HasAnyUndefs &&
3487 SplatBits0.getBitWidth() == SplatBits1.getBitWidth() &&
3488 SplatBits0 == ~SplatBits1) {
3490 return DAG.getNode(ISD::VSELECT, DL, VT, N0->getOperand(1),
3491 N0->getOperand(0), N1->getOperand(0));
3499 /// Target-specific dag combine xforms for ISD::SRA
3500 static SDValue PerformSRACombine(SDNode *N,
3501 TargetLowering::DAGCombinerInfo &DCI) {
3503 SelectionDAG &DAG = DCI.DAG;
3505 EVT VT = N->getValueType(0);
3507 // We're looking for an SRA/SHL pair which form an SBFX.
3509 if (VT != MVT::i32 && VT != MVT::i64)
3512 if (!isa<ConstantSDNode>(N->getOperand(1)))
3515 uint64_t ExtraSignBits = N->getConstantOperandVal(1);
3516 SDValue Shift = N->getOperand(0);
3518 if (Shift.getOpcode() != ISD::SHL)
3521 if (!isa<ConstantSDNode>(Shift->getOperand(1)))
3524 uint64_t BitsOnLeft = Shift->getConstantOperandVal(1);
3525 uint64_t Width = VT.getSizeInBits() - ExtraSignBits;
3526 uint64_t LSB = VT.getSizeInBits() - Width - BitsOnLeft;
3528 if (LSB > VT.getSizeInBits() || Width > VT.getSizeInBits())
3531 return DAG.getNode(AArch64ISD::SBFX, DL, VT, Shift.getOperand(0),
3532 DAG.getConstant(LSB, MVT::i64),
3533 DAG.getConstant(LSB + Width - 1, MVT::i64));
3536 /// Check if this is a valid build_vector for the immediate operand of
3537 /// a vector shift operation, where all the elements of the build_vector
3538 /// must have the same constant integer value.
3539 static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
3540 // Ignore bit_converts.
3541 while (Op.getOpcode() == ISD::BITCAST)
3542 Op = Op.getOperand(0);
3543 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3544 APInt SplatBits, SplatUndef;
3545 unsigned SplatBitSize;
3547 if (!BVN || !BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
3548 HasAnyUndefs, ElementBits) ||
3549 SplatBitSize > ElementBits)
3551 Cnt = SplatBits.getSExtValue();
3555 /// Check if this is a valid build_vector for the immediate operand of
3556 /// a vector shift left operation. That value must be in the range:
3557 /// 0 <= Value < ElementBits
3558 static bool isVShiftLImm(SDValue Op, EVT VT, int64_t &Cnt) {
3559 assert(VT.isVector() && "vector shift count is not a vector type");
3560 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3561 if (!getVShiftImm(Op, ElementBits, Cnt))
3563 return (Cnt >= 0 && Cnt < ElementBits);
3566 /// Check if this is a valid build_vector for the immediate operand of a
3567 /// vector shift right operation. The value must be in the range:
3568 /// 1 <= Value <= ElementBits
3569 static bool isVShiftRImm(SDValue Op, EVT VT, int64_t &Cnt) {
3570 assert(VT.isVector() && "vector shift count is not a vector type");
3571 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
3572 if (!getVShiftImm(Op, ElementBits, Cnt))
3574 return (Cnt >= 1 && Cnt <= ElementBits);
3577 /// Checks for immediate versions of vector shifts and lowers them.
3578 static SDValue PerformShiftCombine(SDNode *N,
3579 TargetLowering::DAGCombinerInfo &DCI,
3580 const AArch64Subtarget *ST) {
3581 SelectionDAG &DAG = DCI.DAG;
3582 EVT VT = N->getValueType(0);
3583 if (N->getOpcode() == ISD::SRA && (VT == MVT::i32 || VT == MVT::i64))
3584 return PerformSRACombine(N, DCI);
3586 // Nothing to be done for scalar shifts.
3587 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3588 if (!VT.isVector() || !TLI.isTypeLegal(VT))
3591 assert(ST->hasNEON() && "unexpected vector shift");
3594 switch (N->getOpcode()) {
3596 llvm_unreachable("unexpected shift opcode");
3599 if (isVShiftLImm(N->getOperand(1), VT, Cnt)) {
3601 DAG.getNode(AArch64ISD::NEON_VDUP, SDLoc(N->getOperand(1)), VT,
3602 DAG.getConstant(Cnt, MVT::i32));
3603 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N->getOperand(0), RHS);
3609 if (isVShiftRImm(N->getOperand(1), VT, Cnt)) {
3611 DAG.getNode(AArch64ISD::NEON_VDUP, SDLoc(N->getOperand(1)), VT,
3612 DAG.getConstant(Cnt, MVT::i32));
3613 return DAG.getNode(N->getOpcode(), SDLoc(N), VT, N->getOperand(0), RHS);
3621 /// ARM-specific DAG combining for intrinsics.
3622 static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
3623 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3627 // Don't do anything for most intrinsics.
3630 case Intrinsic::arm_neon_vqshifts:
3631 case Intrinsic::arm_neon_vqshiftu:
3632 EVT VT = N->getOperand(1).getValueType();
3634 if (!isVShiftLImm(N->getOperand(2), VT, Cnt))
3636 unsigned VShiftOpc = (IntNo == Intrinsic::arm_neon_vqshifts)
3637 ? AArch64ISD::NEON_QSHLs
3638 : AArch64ISD::NEON_QSHLu;
3639 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0),
3640 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
3646 /// Target-specific DAG combine function for NEON load/store intrinsics
3647 /// to merge base address updates.
3648 static SDValue CombineBaseUpdate(SDNode *N,
3649 TargetLowering::DAGCombinerInfo &DCI) {
3650 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
3653 SelectionDAG &DAG = DCI.DAG;
3654 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
3655 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
3656 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
3657 SDValue Addr = N->getOperand(AddrOpIdx);
3659 // Search for a use of the address operand that is an increment.
3660 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
3661 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
3663 if (User->getOpcode() != ISD::ADD ||
3664 UI.getUse().getResNo() != Addr.getResNo())
3667 // Check that the add is independent of the load/store. Otherwise, folding
3668 // it would create a cycle.
3669 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
3672 // Find the new opcode for the updating load/store.
3674 bool isLaneOp = false;
3675 unsigned NewOpc = 0;
3676 unsigned NumVecs = 0;
3678 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
3680 default: llvm_unreachable("unexpected intrinsic for Neon base update");
3681 case Intrinsic::arm_neon_vld1: NewOpc = AArch64ISD::NEON_LD1_UPD;
3683 case Intrinsic::arm_neon_vld2: NewOpc = AArch64ISD::NEON_LD2_UPD;
3685 case Intrinsic::arm_neon_vld3: NewOpc = AArch64ISD::NEON_LD3_UPD;
3687 case Intrinsic::arm_neon_vld4: NewOpc = AArch64ISD::NEON_LD4_UPD;
3689 case Intrinsic::arm_neon_vst1: NewOpc = AArch64ISD::NEON_ST1_UPD;
3690 NumVecs = 1; isLoad = false; break;
3691 case Intrinsic::arm_neon_vst2: NewOpc = AArch64ISD::NEON_ST2_UPD;
3692 NumVecs = 2; isLoad = false; break;
3693 case Intrinsic::arm_neon_vst3: NewOpc = AArch64ISD::NEON_ST3_UPD;
3694 NumVecs = 3; isLoad = false; break;
3695 case Intrinsic::arm_neon_vst4: NewOpc = AArch64ISD::NEON_ST4_UPD;
3696 NumVecs = 4; isLoad = false; break;
3697 case Intrinsic::aarch64_neon_vld1x2: NewOpc = AArch64ISD::NEON_LD1x2_UPD;
3699 case Intrinsic::aarch64_neon_vld1x3: NewOpc = AArch64ISD::NEON_LD1x3_UPD;
3701 case Intrinsic::aarch64_neon_vld1x4: NewOpc = AArch64ISD::NEON_LD1x4_UPD;
3703 case Intrinsic::aarch64_neon_vst1x2: NewOpc = AArch64ISD::NEON_ST1x2_UPD;
3704 NumVecs = 2; isLoad = false; break;
3705 case Intrinsic::aarch64_neon_vst1x3: NewOpc = AArch64ISD::NEON_ST1x3_UPD;
3706 NumVecs = 3; isLoad = false; break;
3707 case Intrinsic::aarch64_neon_vst1x4: NewOpc = AArch64ISD::NEON_ST1x4_UPD;
3708 NumVecs = 4; isLoad = false; break;
3709 case Intrinsic::arm_neon_vld2lane: NewOpc = AArch64ISD::NEON_LD2LN_UPD;
3710 NumVecs = 2; isLaneOp = true; break;
3711 case Intrinsic::arm_neon_vld3lane: NewOpc = AArch64ISD::NEON_LD3LN_UPD;
3712 NumVecs = 3; isLaneOp = true; break;
3713 case Intrinsic::arm_neon_vld4lane: NewOpc = AArch64ISD::NEON_LD4LN_UPD;
3714 NumVecs = 4; isLaneOp = true; break;
3715 case Intrinsic::arm_neon_vst2lane: NewOpc = AArch64ISD::NEON_ST2LN_UPD;
3716 NumVecs = 2; isLoad = false; isLaneOp = true; break;
3717 case Intrinsic::arm_neon_vst3lane: NewOpc = AArch64ISD::NEON_ST3LN_UPD;
3718 NumVecs = 3; isLoad = false; isLaneOp = true; break;
3719 case Intrinsic::arm_neon_vst4lane: NewOpc = AArch64ISD::NEON_ST4LN_UPD;
3720 NumVecs = 4; isLoad = false; isLaneOp = true; break;
3724 switch (N->getOpcode()) {
3725 default: llvm_unreachable("unexpected opcode for Neon base update");
3726 case AArch64ISD::NEON_LD2DUP: NewOpc = AArch64ISD::NEON_LD2DUP_UPD;
3728 case AArch64ISD::NEON_LD3DUP: NewOpc = AArch64ISD::NEON_LD3DUP_UPD;
3730 case AArch64ISD::NEON_LD4DUP: NewOpc = AArch64ISD::NEON_LD4DUP_UPD;
3735 // Find the size of memory referenced by the load/store.
3738 VecTy = N->getValueType(0);
3740 VecTy = N->getOperand(AddrOpIdx + 1).getValueType();
3741 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
3743 NumBytes /= VecTy.getVectorNumElements();
3745 // If the increment is a constant, it must match the memory ref size.
3746 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
3747 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
3748 uint32_t IncVal = CInc->getZExtValue();
3749 if (IncVal != NumBytes)
3751 Inc = DAG.getTargetConstant(IncVal, MVT::i32);
3754 // Create the new updating load/store node.
3756 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
3758 for (n = 0; n < NumResultVecs; ++n)
3760 Tys[n++] = MVT::i64;
3761 Tys[n] = MVT::Other;
3762 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs + 2);
3763 SmallVector<SDValue, 8> Ops;
3764 Ops.push_back(N->getOperand(0)); // incoming chain
3765 Ops.push_back(N->getOperand(AddrOpIdx));
3767 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
3768 Ops.push_back(N->getOperand(i));
3770 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
3771 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, SDLoc(N), SDTys,
3772 Ops.data(), Ops.size(),
3773 MemInt->getMemoryVT(),
3774 MemInt->getMemOperand());
3777 std::vector<SDValue> NewResults;
3778 for (unsigned i = 0; i < NumResultVecs; ++i) {
3779 NewResults.push_back(SDValue(UpdN.getNode(), i));
3781 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs + 1)); // chain
3782 DCI.CombineTo(N, NewResults);
3783 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
3790 /// For a VDUPLANE node N, check if its source operand is a vldN-lane (N > 1)
3791 /// intrinsic, and if all the other uses of that intrinsic are also VDUPLANEs.
3792 /// If so, combine them to a vldN-dup operation and return true.
3793 static SDValue CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
3794 SelectionDAG &DAG = DCI.DAG;
3795 EVT VT = N->getValueType(0);
3797 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
3798 SDNode *VLD = N->getOperand(0).getNode();
3799 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
3801 unsigned NumVecs = 0;
3802 unsigned NewOpc = 0;
3803 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
3804 if (IntNo == Intrinsic::arm_neon_vld2lane) {
3806 NewOpc = AArch64ISD::NEON_LD2DUP;
3807 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
3809 NewOpc = AArch64ISD::NEON_LD3DUP;
3810 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
3812 NewOpc = AArch64ISD::NEON_LD4DUP;
3817 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
3818 // numbers match the load.
3819 unsigned VLDLaneNo =
3820 cast<ConstantSDNode>(VLD->getOperand(NumVecs + 3))->getZExtValue();
3821 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
3823 // Ignore uses of the chain result.
3824 if (UI.getUse().getResNo() == NumVecs)
3827 if (User->getOpcode() != AArch64ISD::NEON_VDUPLANE ||
3828 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
3832 // Create the vldN-dup node.
3835 for (n = 0; n < NumVecs; ++n)
3837 Tys[n] = MVT::Other;
3838 SDVTList SDTys = DAG.getVTList(Tys, NumVecs + 1);
3839 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
3840 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
3841 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys, Ops, 2,
3842 VLDMemInt->getMemoryVT(),
3843 VLDMemInt->getMemOperand());
3846 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
3848 unsigned ResNo = UI.getUse().getResNo();
3849 // Ignore uses of the chain result.
3850 if (ResNo == NumVecs)
3853 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
3856 // Now the vldN-lane intrinsic is dead except for its chain result.
3857 // Update uses of the chain.
3858 std::vector<SDValue> VLDDupResults;
3859 for (unsigned n = 0; n < NumVecs; ++n)
3860 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
3861 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
3862 DCI.CombineTo(VLD, VLDDupResults);
3864 return SDValue(N, 0);
3868 AArch64TargetLowering::PerformDAGCombine(SDNode *N,
3869 DAGCombinerInfo &DCI) const {
3870 switch (N->getOpcode()) {
3872 case ISD::AND: return PerformANDCombine(N, DCI);
3873 case ISD::OR: return PerformORCombine(N, DCI, getSubtarget());
3877 return PerformShiftCombine(N, DCI, getSubtarget());
3878 case ISD::INTRINSIC_WO_CHAIN:
3879 return PerformIntrinsicCombine(N, DCI.DAG);
3880 case AArch64ISD::NEON_VDUPLANE:
3881 return CombineVLDDUP(N, DCI);
3882 case AArch64ISD::NEON_LD2DUP:
3883 case AArch64ISD::NEON_LD3DUP:
3884 case AArch64ISD::NEON_LD4DUP:
3885 return CombineBaseUpdate(N, DCI);
3886 case ISD::INTRINSIC_VOID:
3887 case ISD::INTRINSIC_W_CHAIN:
3888 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
3889 case Intrinsic::arm_neon_vld1:
3890 case Intrinsic::arm_neon_vld2:
3891 case Intrinsic::arm_neon_vld3:
3892 case Intrinsic::arm_neon_vld4:
3893 case Intrinsic::arm_neon_vst1:
3894 case Intrinsic::arm_neon_vst2:
3895 case Intrinsic::arm_neon_vst3:
3896 case Intrinsic::arm_neon_vst4:
3897 case Intrinsic::arm_neon_vld2lane:
3898 case Intrinsic::arm_neon_vld3lane:
3899 case Intrinsic::arm_neon_vld4lane:
3900 case Intrinsic::aarch64_neon_vld1x2:
3901 case Intrinsic::aarch64_neon_vld1x3:
3902 case Intrinsic::aarch64_neon_vld1x4:
3903 case Intrinsic::aarch64_neon_vst1x2:
3904 case Intrinsic::aarch64_neon_vst1x3:
3905 case Intrinsic::aarch64_neon_vst1x4:
3906 case Intrinsic::arm_neon_vst2lane:
3907 case Intrinsic::arm_neon_vst3lane:
3908 case Intrinsic::arm_neon_vst4lane:
3909 return CombineBaseUpdate(N, DCI);
3918 AArch64TargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
3919 VT = VT.getScalarType();
3924 switch (VT.getSimpleVT().SimpleTy) {
3938 // Check whether a Build Vector could be presented as Shuffle Vector. If yes,
3939 // try to call LowerVECTOR_SHUFFLE to lower it.
3940 bool AArch64TargetLowering::isKnownShuffleVector(SDValue Op, SelectionDAG &DAG,
3941 SDValue &Res) const {
3943 EVT VT = Op.getValueType();
3944 unsigned NumElts = VT.getVectorNumElements();
3945 unsigned V0NumElts = 0;
3949 // Check if all elements are extracted from less than 3 vectors.
3950 for (unsigned i = 0; i < NumElts; ++i) {
3951 SDValue Elt = Op.getOperand(i);
3952 if (Elt.getOpcode() != ISD::EXTRACT_VECTOR_ELT)
3955 if (V0.getNode() == 0) {
3956 V0 = Elt.getOperand(0);
3957 V0NumElts = V0.getValueType().getVectorNumElements();
3959 if (Elt.getOperand(0) == V0) {
3960 Mask[i] = (cast<ConstantSDNode>(Elt->getOperand(1))->getZExtValue());
3962 } else if (V1.getNode() == 0) {
3963 V1 = Elt.getOperand(0);
3965 if (Elt.getOperand(0) == V1) {
3966 unsigned Lane = cast<ConstantSDNode>(Elt->getOperand(1))->getZExtValue();
3967 Mask[i] = (Lane + V0NumElts);
3974 if (!V1.getNode() && V0NumElts == NumElts * 2) {
3975 V1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, V0,
3976 DAG.getConstant(NumElts, MVT::i64));
3977 V0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, V0,
3978 DAG.getConstant(0, MVT::i64));
3979 V0NumElts = V0.getValueType().getVectorNumElements();
3982 if (V1.getNode() && NumElts == V0NumElts &&
3983 V0NumElts == V1.getValueType().getVectorNumElements()) {
3984 SDValue Shuffle = DAG.getVectorShuffle(VT, DL, V0, V1, Mask);
3985 if(Shuffle.getOpcode() != ISD::VECTOR_SHUFFLE)
3988 Res = LowerVECTOR_SHUFFLE(Shuffle, DAG);
3994 // If this is a case we can't handle, return null and let the default
3995 // expansion code take care of it.
3997 AArch64TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3998 const AArch64Subtarget *ST) const {
4000 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
4002 EVT VT = Op.getValueType();
4004 APInt SplatBits, SplatUndef;
4005 unsigned SplatBitSize;
4008 unsigned UseNeonMov = VT.getSizeInBits() >= 64;
4010 // Note we favor lowering MOVI over MVNI.
4011 // This has implications on the definition of patterns in TableGen to select
4012 // BIC immediate instructions but not ORR immediate instructions.
4013 // If this lowering order is changed, TableGen patterns for BIC immediate and
4014 // ORR immediate instructions have to be updated.
4016 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
4017 if (SplatBitSize <= 64) {
4018 // First attempt to use vector immediate-form MOVI
4021 unsigned OpCmode = 0;
4023 if (isNeonModifiedImm(SplatBits.getZExtValue(), SplatUndef.getZExtValue(),
4024 SplatBitSize, DAG, VT.is128BitVector(),
4025 Neon_Mov_Imm, NeonMovVT, Imm, OpCmode)) {
4026 SDValue ImmVal = DAG.getTargetConstant(Imm, MVT::i32);
4027 SDValue OpCmodeVal = DAG.getConstant(OpCmode, MVT::i32);
4029 if (ImmVal.getNode() && OpCmodeVal.getNode()) {
4030 SDValue NeonMov = DAG.getNode(AArch64ISD::NEON_MOVIMM, DL, NeonMovVT,
4031 ImmVal, OpCmodeVal);
4032 return DAG.getNode(ISD::BITCAST, DL, VT, NeonMov);
4036 // Then attempt to use vector immediate-form MVNI
4037 uint64_t NegatedImm = (~SplatBits).getZExtValue();
4038 if (isNeonModifiedImm(NegatedImm, SplatUndef.getZExtValue(), SplatBitSize,
4039 DAG, VT.is128BitVector(), Neon_Mvn_Imm, NeonMovVT,
4041 SDValue ImmVal = DAG.getTargetConstant(Imm, MVT::i32);
4042 SDValue OpCmodeVal = DAG.getConstant(OpCmode, MVT::i32);
4043 if (ImmVal.getNode() && OpCmodeVal.getNode()) {
4044 SDValue NeonMov = DAG.getNode(AArch64ISD::NEON_MVNIMM, DL, NeonMovVT,
4045 ImmVal, OpCmodeVal);
4046 return DAG.getNode(ISD::BITCAST, DL, VT, NeonMov);
4050 // Attempt to use vector immediate-form FMOV
4051 if (((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) ||
4052 (VT == MVT::v2f64 && SplatBitSize == 64)) {
4054 SplatBitSize == 32 ? APFloat::IEEEsingle : APFloat::IEEEdouble,
4057 if (A64Imms::isFPImm(RealVal, ImmVal)) {
4058 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4059 return DAG.getNode(AArch64ISD::NEON_FMOVIMM, DL, VT, Val);
4065 unsigned NumElts = VT.getVectorNumElements();
4066 bool isOnlyLowElement = true;
4067 bool usesOnlyOneValue = true;
4068 bool hasDominantValue = false;
4069 bool isConstant = true;
4071 // Map of the number of times a particular SDValue appears in the
4073 DenseMap<SDValue, unsigned> ValueCounts;
4075 for (unsigned i = 0; i < NumElts; ++i) {
4076 SDValue V = Op.getOperand(i);
4077 if (V.getOpcode() == ISD::UNDEF)
4080 isOnlyLowElement = false;
4081 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4084 ValueCounts.insert(std::make_pair(V, 0));
4085 unsigned &Count = ValueCounts[V];
4087 // Is this value dominant? (takes up more than half of the lanes)
4088 if (++Count > (NumElts / 2)) {
4089 hasDominantValue = true;
4093 if (ValueCounts.size() != 1)
4094 usesOnlyOneValue = false;
4095 if (!Value.getNode() && ValueCounts.size() > 0)
4096 Value = ValueCounts.begin()->first;
4098 if (ValueCounts.size() == 0)
4099 return DAG.getUNDEF(VT);
4101 if (isOnlyLowElement)
4102 return DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, Value);
4104 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4105 if (hasDominantValue && EltSize <= 64) {
4106 // Use VDUP for non-constant splats.
4110 // If we are DUPing a value that comes directly from a vector, we could
4111 // just use DUPLANE. We can only do this if the lane being extracted
4112 // is at a constant index, as the DUP from lane instructions only have
4113 // constant-index forms.
4115 // If there is a TRUNCATE between EXTRACT_VECTOR_ELT and DUP, we can
4116 // remove TRUNCATE for DUPLANE by apdating the source vector to
4117 // appropriate vector type and lane index.
4119 // FIXME: for now we have v1i8, v1i16, v1i32 legal vector types, if they
4120 // are not legal any more, no need to check the type size in bits should
4121 // be large than 64.
4123 if (Value->getOpcode() == ISD::TRUNCATE)
4124 V = Value->getOperand(0);
4125 if (V->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4126 isa<ConstantSDNode>(V->getOperand(1)) &&
4127 V->getOperand(0).getValueType().getSizeInBits() >= 64) {
4129 // If the element size of source vector is larger than DUPLANE
4130 // element size, we can do transformation by,
4131 // 1) bitcasting source register to smaller element vector
4132 // 2) mutiplying the lane index by SrcEltSize/ResEltSize
4133 // For example, we can lower
4134 // "v8i16 vdup_lane(v4i32, 1)"
4136 // "v8i16 vdup_lane(v8i16 bitcast(v4i32), 2)".
4137 SDValue SrcVec = V->getOperand(0);
4138 unsigned SrcEltSize =
4139 SrcVec.getValueType().getVectorElementType().getSizeInBits();
4140 unsigned ResEltSize = VT.getVectorElementType().getSizeInBits();
4141 if (SrcEltSize > ResEltSize) {
4142 assert((SrcEltSize % ResEltSize == 0) && "Invalid element size");
4144 unsigned SrcSize = SrcVec.getValueType().getSizeInBits();
4145 unsigned ResSize = VT.getSizeInBits();
4147 if (SrcSize > ResSize) {
4148 assert((SrcSize % ResSize == 0) && "Invalid vector size");
4150 EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
4151 SrcSize / ResEltSize);
4152 BitCast = DAG.getNode(ISD::BITCAST, DL, CastVT, SrcVec);
4154 assert((SrcSize == ResSize) && "Invalid vector size of source vec");
4155 BitCast = DAG.getNode(ISD::BITCAST, DL, VT, SrcVec);
4158 unsigned LaneIdx = V->getConstantOperandVal(1);
4160 DAG.getConstant((SrcEltSize / ResEltSize) * LaneIdx, MVT::i64);
4161 N = DAG.getNode(AArch64ISD::NEON_VDUPLANE, DL, VT, BitCast, Lane);
4163 assert((SrcEltSize == ResEltSize) &&
4164 "Invalid element size of source vec");
4165 N = DAG.getNode(AArch64ISD::NEON_VDUPLANE, DL, VT, V->getOperand(0),
4169 N = DAG.getNode(AArch64ISD::NEON_VDUP, DL, VT, Value);
4171 if (!usesOnlyOneValue) {
4172 // The dominant value was splatted as 'N', but we now have to insert
4173 // all differing elements.
4174 for (unsigned I = 0; I < NumElts; ++I) {
4175 if (Op.getOperand(I) == Value)
4177 SmallVector<SDValue, 3> Ops;
4179 Ops.push_back(Op.getOperand(I));
4180 Ops.push_back(DAG.getConstant(I, MVT::i64));
4181 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, &Ops[0], 3);
4186 if (usesOnlyOneValue && isConstant) {
4187 return DAG.getNode(AArch64ISD::NEON_VDUP, DL, VT, Value);
4190 // If all elements are constants and the case above didn't get hit, fall back
4191 // to the default expansion, which will generate a load from the constant
4196 // Try to lower this in lowering ShuffleVector way.
4198 if (isKnownShuffleVector(Op, DAG, Shuf))
4201 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
4202 // know the default expansion would otherwise fall back on something even
4203 // worse. For a vector with one or two non-undef values, that's
4204 // scalar_to_vector for the elements followed by a shuffle (provided the
4205 // shuffle is valid for the target) and materialization element by element
4206 // on the stack followed by a load for everything else.
4207 if (!isConstant && !usesOnlyOneValue) {
4208 SDValue Vec = DAG.getUNDEF(VT);
4209 for (unsigned i = 0 ; i < NumElts; ++i) {
4210 SDValue V = Op.getOperand(i);
4211 if (V.getOpcode() == ISD::UNDEF)
4213 SDValue LaneIdx = DAG.getConstant(i, MVT::i64);
4214 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, Vec, V, LaneIdx);
4221 /// isREVMask - Check if a vector shuffle corresponds to a REV
4222 /// instruction with the specified blocksize. (The order of the elements
4223 /// within each block of the vector is reversed.)
4224 static bool isREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
4225 assert((BlockSize == 16 || BlockSize == 32 || BlockSize == 64) &&
4226 "Only possible block sizes for REV are: 16, 32, 64");
4228 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4232 unsigned NumElts = VT.getVectorNumElements();
4233 unsigned BlockElts = M[0] + 1;
4234 // If the first shuffle index is UNDEF, be optimistic.
4236 BlockElts = BlockSize / EltSz;
4238 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
4241 for (unsigned i = 0; i < NumElts; ++i) {
4243 continue; // ignore UNDEF indices
4244 if ((unsigned)M[i] != (i - i % BlockElts) + (BlockElts - 1 - i % BlockElts))
4251 // isPermuteMask - Check whether the vector shuffle matches to UZP, ZIP and
4253 static unsigned isPermuteMask(ArrayRef<int> M, EVT VT, bool isV2undef) {
4254 unsigned NumElts = VT.getVectorNumElements();
4258 bool ismatch = true;
4261 for (unsigned i = 0; i < NumElts; ++i) {
4262 unsigned answer = i * 2;
4263 if (isV2undef && answer >= NumElts)
4265 if (M[i] != -1 && (unsigned)M[i] != answer) {
4271 return AArch64ISD::NEON_UZP1;
4275 for (unsigned i = 0; i < NumElts; ++i) {
4276 unsigned answer = i * 2 + 1;
4277 if (isV2undef && answer >= NumElts)
4279 if (M[i] != -1 && (unsigned)M[i] != answer) {
4285 return AArch64ISD::NEON_UZP2;
4289 for (unsigned i = 0; i < NumElts; ++i) {
4290 unsigned answer = i / 2 + NumElts * (i % 2);
4291 if (isV2undef && answer >= NumElts)
4293 if (M[i] != -1 && (unsigned)M[i] != answer) {
4299 return AArch64ISD::NEON_ZIP1;
4303 for (unsigned i = 0; i < NumElts; ++i) {
4304 unsigned answer = (NumElts + i) / 2 + NumElts * (i % 2);
4305 if (isV2undef && answer >= NumElts)
4307 if (M[i] != -1 && (unsigned)M[i] != answer) {
4313 return AArch64ISD::NEON_ZIP2;
4317 for (unsigned i = 0; i < NumElts; ++i) {
4318 unsigned answer = i + (NumElts - 1) * (i % 2);
4319 if (isV2undef && answer >= NumElts)
4321 if (M[i] != -1 && (unsigned)M[i] != answer) {
4327 return AArch64ISD::NEON_TRN1;
4331 for (unsigned i = 0; i < NumElts; ++i) {
4332 unsigned answer = 1 + i + (NumElts - 1) * (i % 2);
4333 if (isV2undef && answer >= NumElts)
4335 if (M[i] != -1 && (unsigned)M[i] != answer) {
4341 return AArch64ISD::NEON_TRN2;
4347 AArch64TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
4348 SelectionDAG &DAG) const {
4349 SDValue V1 = Op.getOperand(0);
4350 SDValue V2 = Op.getOperand(1);
4352 EVT VT = Op.getValueType();
4353 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
4355 // Convert shuffles that are directly supported on NEON to target-specific
4356 // DAG nodes, instead of keeping them as shuffles and matching them again
4357 // during code selection. This is more efficient and avoids the possibility
4358 // of inconsistencies between legalization and selection.
4359 ArrayRef<int> ShuffleMask = SVN->getMask();
4361 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4365 if (isREVMask(ShuffleMask, VT, 64))
4366 return DAG.getNode(AArch64ISD::NEON_REV64, dl, VT, V1);
4367 if (isREVMask(ShuffleMask, VT, 32))
4368 return DAG.getNode(AArch64ISD::NEON_REV32, dl, VT, V1);
4369 if (isREVMask(ShuffleMask, VT, 16))
4370 return DAG.getNode(AArch64ISD::NEON_REV16, dl, VT, V1);
4373 if (V2.getOpcode() == ISD::UNDEF)
4374 ISDNo = isPermuteMask(ShuffleMask, VT, true);
4376 ISDNo = isPermuteMask(ShuffleMask, VT, false);
4379 if (V2.getOpcode() == ISD::UNDEF)
4380 return DAG.getNode(ISDNo, dl, VT, V1, V1);
4382 return DAG.getNode(ISDNo, dl, VT, V1, V2);
4385 // If the element of shuffle mask are all the same constant, we can
4386 // transform it into either NEON_VDUP or NEON_VDUPLANE
4387 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4388 int Lane = SVN->getSplatIndex();
4389 // If this is undef splat, generate it via "just" vdup, if possible.
4390 if (Lane == -1) Lane = 0;
4392 // Test if V1 is a SCALAR_TO_VECTOR.
4393 if (V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4394 return DAG.getNode(AArch64ISD::NEON_VDUP, dl, VT, V1.getOperand(0));
4396 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR.
4397 if (V1.getOpcode() == ISD::BUILD_VECTOR) {
4398 bool IsScalarToVector = true;
4399 for (unsigned i = 0, e = V1.getNumOperands(); i != e; ++i)
4400 if (V1.getOperand(i).getOpcode() != ISD::UNDEF &&
4401 i != (unsigned)Lane) {
4402 IsScalarToVector = false;
4405 if (IsScalarToVector)
4406 return DAG.getNode(AArch64ISD::NEON_VDUP, dl, VT,
4407 V1.getOperand(Lane));
4410 // Test if V1 is a EXTRACT_SUBVECTOR.
4411 if (V1.getOpcode() == ISD::EXTRACT_SUBVECTOR) {
4412 int ExtLane = cast<ConstantSDNode>(V1.getOperand(1))->getZExtValue();
4413 return DAG.getNode(AArch64ISD::NEON_VDUPLANE, dl, VT, V1.getOperand(0),
4414 DAG.getConstant(Lane + ExtLane, MVT::i64));
4416 // Test if V1 is a CONCAT_VECTORS.
4417 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
4418 V1.getOperand(1).getOpcode() == ISD::UNDEF) {
4419 SDValue Op0 = V1.getOperand(0);
4420 assert((unsigned)Lane < Op0.getValueType().getVectorNumElements() &&
4421 "Invalid vector lane access");
4422 return DAG.getNode(AArch64ISD::NEON_VDUPLANE, dl, VT, Op0,
4423 DAG.getConstant(Lane, MVT::i64));
4426 return DAG.getNode(AArch64ISD::NEON_VDUPLANE, dl, VT, V1,
4427 DAG.getConstant(Lane, MVT::i64));
4430 int Length = ShuffleMask.size();
4431 int V1EltNum = V1.getValueType().getVectorNumElements();
4433 // If the number of v1 elements is the same as the number of shuffle mask
4434 // element and the shuffle masks are sequential values, we can transform
4435 // it into NEON_VEXTRACT.
4436 if (V1EltNum == Length) {
4437 // Check if the shuffle mask is sequential.
4438 bool IsSequential = true;
4439 int CurMask = ShuffleMask[0];
4440 for (int I = 0; I < Length; ++I) {
4441 if (ShuffleMask[I] != CurMask) {
4442 IsSequential = false;
4448 assert((EltSize % 8 == 0) && "Bitsize of vector element is incorrect");
4449 unsigned VecSize = EltSize * V1EltNum;
4450 unsigned Index = (EltSize/8) * ShuffleMask[0];
4451 if (VecSize == 64 || VecSize == 128)
4452 return DAG.getNode(AArch64ISD::NEON_VEXTRACT, dl, VT, V1, V2,
4453 DAG.getConstant(Index, MVT::i64));
4457 // For shuffle mask like "0, 1, 2, 3, 4, 5, 13, 7", try to generate insert
4458 // by element from V2 to V1 .
4459 // If shuffle mask is like "0, 1, 10, 11, 12, 13, 14, 15", V2 would be a
4460 // better choice to be inserted than V1 as less insert needed, so we count
4461 // element to be inserted for both V1 and V2, and select less one as insert
4464 // Collect elements need to be inserted and their index.
4465 SmallVector<int, 8> NV1Elt;
4466 SmallVector<int, 8> N1Index;
4467 SmallVector<int, 8> NV2Elt;
4468 SmallVector<int, 8> N2Index;
4469 for (int I = 0; I != Length; ++I) {
4470 if (ShuffleMask[I] != I) {
4471 NV1Elt.push_back(ShuffleMask[I]);
4472 N1Index.push_back(I);
4475 for (int I = 0; I != Length; ++I) {
4476 if (ShuffleMask[I] != (I + V1EltNum)) {
4477 NV2Elt.push_back(ShuffleMask[I]);
4478 N2Index.push_back(I);
4482 // Decide which to be inserted. If all lanes mismatch, neither V1 nor V2
4483 // will be inserted.
4485 SmallVector<int, 8> InsMasks = NV1Elt;
4486 SmallVector<int, 8> InsIndex = N1Index;
4487 if ((int)NV1Elt.size() != Length || (int)NV2Elt.size() != Length) {
4488 if (NV1Elt.size() > NV2Elt.size()) {
4494 InsV = DAG.getNode(ISD::UNDEF, dl, VT);
4497 for (int I = 0, E = InsMasks.size(); I != E; ++I) {
4499 int Mask = InsMasks[I];
4500 if (Mask >= V1EltNum) {
4504 // Any value type smaller than i32 is illegal in AArch64, and this lower
4505 // function is called after legalize pass, so we need to legalize
4508 if (VT.getVectorElementType().isFloatingPoint())
4509 EltVT = (EltSize == 64) ? MVT::f64 : MVT::f32;
4511 EltVT = (EltSize == 64) ? MVT::i64 : MVT::i32;
4514 ExtV = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, ExtV,
4515 DAG.getConstant(Mask, MVT::i64));
4516 InsV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, InsV, ExtV,
4517 DAG.getConstant(InsIndex[I], MVT::i64));
4523 AArch64TargetLowering::ConstraintType
4524 AArch64TargetLowering::getConstraintType(const std::string &Constraint) const {
4525 if (Constraint.size() == 1) {
4526 switch (Constraint[0]) {
4528 case 'w': // An FP/SIMD vector register
4529 return C_RegisterClass;
4530 case 'I': // Constant that can be used with an ADD instruction
4531 case 'J': // Constant that can be used with a SUB instruction
4532 case 'K': // Constant that can be used with a 32-bit logical instruction
4533 case 'L': // Constant that can be used with a 64-bit logical instruction
4534 case 'M': // Constant that can be used as a 32-bit MOV immediate
4535 case 'N': // Constant that can be used as a 64-bit MOV immediate
4536 case 'Y': // Floating point constant zero
4537 case 'Z': // Integer constant zero
4539 case 'Q': // A memory reference with base register and no offset
4541 case 'S': // A symbolic address
4546 // FIXME: Ump, Utf, Usa, Ush
4547 // Ump: A memory address suitable for ldp/stp in SI, DI, SF and DF modes,
4548 // whatever they may be
4549 // Utf: A memory address suitable for ldp/stp in TF mode, whatever it may be
4550 // Usa: An absolute symbolic address
4551 // Ush: The high part (bits 32:12) of a pc-relative symbolic address
4552 assert(Constraint != "Ump" && Constraint != "Utf" && Constraint != "Usa"
4553 && Constraint != "Ush" && "Unimplemented constraints");
4555 return TargetLowering::getConstraintType(Constraint);
4558 TargetLowering::ConstraintWeight
4559 AArch64TargetLowering::getSingleConstraintMatchWeight(AsmOperandInfo &Info,
4560 const char *Constraint) const {
4562 llvm_unreachable("Constraint weight unimplemented");
4566 AArch64TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
4567 std::string &Constraint,
4568 std::vector<SDValue> &Ops,
4569 SelectionDAG &DAG) const {
4570 SDValue Result(0, 0);
4572 // Only length 1 constraints are C_Other.
4573 if (Constraint.size() != 1) return;
4575 // Only C_Other constraints get lowered like this. That means constants for us
4576 // so return early if there's no hope the constraint can be lowered.
4578 switch(Constraint[0]) {
4580 case 'I': case 'J': case 'K': case 'L':
4581 case 'M': case 'N': case 'Z': {
4582 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
4586 uint64_t CVal = C->getZExtValue();
4589 switch (Constraint[0]) {
4591 // FIXME: 'M' and 'N' are MOV pseudo-insts -- unsupported in assembly. 'J'
4592 // is a peculiarly useless SUB constraint.
4593 llvm_unreachable("Unimplemented C_Other constraint");
4599 if (A64Imms::isLogicalImm(32, CVal, Bits))
4603 if (A64Imms::isLogicalImm(64, CVal, Bits))
4612 Result = DAG.getTargetConstant(CVal, Op.getValueType());
4616 // An absolute symbolic address or label reference.
4617 if (const GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op)) {
4618 Result = DAG.getTargetGlobalAddress(GA->getGlobal(), SDLoc(Op),
4619 GA->getValueType(0));
4620 } else if (const BlockAddressSDNode *BA
4621 = dyn_cast<BlockAddressSDNode>(Op)) {
4622 Result = DAG.getTargetBlockAddress(BA->getBlockAddress(),
4623 BA->getValueType(0));
4624 } else if (const ExternalSymbolSDNode *ES
4625 = dyn_cast<ExternalSymbolSDNode>(Op)) {
4626 Result = DAG.getTargetExternalSymbol(ES->getSymbol(),
4627 ES->getValueType(0));
4633 if (const ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op)) {
4634 if (CFP->isExactlyValue(0.0)) {
4635 Result = DAG.getTargetConstantFP(0.0, CFP->getValueType(0));
4642 if (Result.getNode()) {
4643 Ops.push_back(Result);
4647 // It's an unknown constraint for us. Let generic code have a go.
4648 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
4651 std::pair<unsigned, const TargetRegisterClass*>
4652 AArch64TargetLowering::getRegForInlineAsmConstraint(
4653 const std::string &Constraint,
4655 if (Constraint.size() == 1) {
4656 switch (Constraint[0]) {
4658 if (VT.getSizeInBits() <= 32)
4659 return std::make_pair(0U, &AArch64::GPR32RegClass);
4660 else if (VT == MVT::i64)
4661 return std::make_pair(0U, &AArch64::GPR64RegClass);
4665 return std::make_pair(0U, &AArch64::FPR16RegClass);
4666 else if (VT == MVT::f32)
4667 return std::make_pair(0U, &AArch64::FPR32RegClass);
4668 else if (VT.getSizeInBits() == 64)
4669 return std::make_pair(0U, &AArch64::FPR64RegClass);
4670 else if (VT.getSizeInBits() == 128)
4671 return std::make_pair(0U, &AArch64::FPR128RegClass);
4676 // Use the default implementation in TargetLowering to convert the register
4677 // constraint into a member of a register class.
4678 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
4681 /// Represent NEON load and store intrinsics as MemIntrinsicNodes.
4682 /// The associated MachineMemOperands record the alignment specified
4683 /// in the intrinsic calls.
4684 bool AArch64TargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
4686 unsigned Intrinsic) const {
4687 switch (Intrinsic) {
4688 case Intrinsic::arm_neon_vld1:
4689 case Intrinsic::arm_neon_vld2:
4690 case Intrinsic::arm_neon_vld3:
4691 case Intrinsic::arm_neon_vld4:
4692 case Intrinsic::aarch64_neon_vld1x2:
4693 case Intrinsic::aarch64_neon_vld1x3:
4694 case Intrinsic::aarch64_neon_vld1x4:
4695 case Intrinsic::arm_neon_vld2lane:
4696 case Intrinsic::arm_neon_vld3lane:
4697 case Intrinsic::arm_neon_vld4lane: {
4698 Info.opc = ISD::INTRINSIC_W_CHAIN;
4699 // Conservatively set memVT to the entire set of vectors loaded.
4700 uint64_t NumElts = getDataLayout()->getTypeAllocSize(I.getType()) / 8;
4701 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
4702 Info.ptrVal = I.getArgOperand(0);
4704 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
4705 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
4706 Info.vol = false; // volatile loads with NEON intrinsics not supported
4707 Info.readMem = true;
4708 Info.writeMem = false;
4711 case Intrinsic::arm_neon_vst1:
4712 case Intrinsic::arm_neon_vst2:
4713 case Intrinsic::arm_neon_vst3:
4714 case Intrinsic::arm_neon_vst4:
4715 case Intrinsic::aarch64_neon_vst1x2:
4716 case Intrinsic::aarch64_neon_vst1x3:
4717 case Intrinsic::aarch64_neon_vst1x4:
4718 case Intrinsic::arm_neon_vst2lane:
4719 case Intrinsic::arm_neon_vst3lane:
4720 case Intrinsic::arm_neon_vst4lane: {
4721 Info.opc = ISD::INTRINSIC_VOID;
4722 // Conservatively set memVT to the entire set of vectors stored.
4723 unsigned NumElts = 0;
4724 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
4725 Type *ArgTy = I.getArgOperand(ArgI)->getType();
4726 if (!ArgTy->isVectorTy())
4728 NumElts += getDataLayout()->getTypeAllocSize(ArgTy) / 8;
4730 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
4731 Info.ptrVal = I.getArgOperand(0);
4733 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
4734 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
4735 Info.vol = false; // volatile stores with NEON intrinsics not supported
4736 Info.readMem = false;
4737 Info.writeMem = true;