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 implements the AArch64TargetLowering class.
12 //===----------------------------------------------------------------------===//
14 #include "AArch64ISelLowering.h"
15 #include "AArch64CallingConvention.h"
16 #include "AArch64MachineFunctionInfo.h"
17 #include "AArch64PerfectShuffle.h"
18 #include "AArch64Subtarget.h"
19 #include "AArch64TargetMachine.h"
20 #include "AArch64TargetObjectFile.h"
21 #include "MCTargetDesc/AArch64AddressingModes.h"
22 #include "llvm/ADT/Statistic.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/IR/Function.h"
28 #include "llvm/IR/GetElementPtrTypeIterator.h"
29 #include "llvm/IR/Intrinsics.h"
30 #include "llvm/IR/Type.h"
31 #include "llvm/Support/CommandLine.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include "llvm/Target/TargetOptions.h"
38 #define DEBUG_TYPE "aarch64-lower"
40 STATISTIC(NumTailCalls, "Number of tail calls");
41 STATISTIC(NumShiftInserts, "Number of vector shift inserts");
50 static cl::opt<AlignMode>
51 Align(cl::desc("Load/store alignment support"),
52 cl::Hidden, cl::init(NoStrictAlign),
54 clEnumValN(StrictAlign, "aarch64-strict-align",
55 "Disallow all unaligned memory accesses"),
56 clEnumValN(NoStrictAlign, "aarch64-no-strict-align",
57 "Allow unaligned memory accesses"),
60 // Place holder until extr generation is tested fully.
62 EnableAArch64ExtrGeneration("aarch64-extr-generation", cl::Hidden,
63 cl::desc("Allow AArch64 (or (shift)(shift))->extract"),
67 EnableAArch64SlrGeneration("aarch64-shift-insert-generation", cl::Hidden,
68 cl::desc("Allow AArch64 SLI/SRI formation"),
71 // FIXME: The necessary dtprel relocations don't seem to be supported
72 // well in the GNU bfd and gold linkers at the moment. Therefore, by
73 // default, for now, fall back to GeneralDynamic code generation.
74 cl::opt<bool> EnableAArch64ELFLocalDynamicTLSGeneration(
75 "aarch64-elf-ldtls-generation", cl::Hidden,
76 cl::desc("Allow AArch64 Local Dynamic TLS code generation"),
79 /// Value type used for condition codes.
80 static const MVT MVT_CC = MVT::i32;
82 AArch64TargetLowering::AArch64TargetLowering(const TargetMachine &TM,
83 const AArch64Subtarget &STI)
84 : TargetLowering(TM), Subtarget(&STI) {
86 // AArch64 doesn't have comparisons which set GPRs or setcc instructions, so
87 // we have to make something up. Arbitrarily, choose ZeroOrOne.
88 setBooleanContents(ZeroOrOneBooleanContent);
89 // When comparing vectors the result sets the different elements in the
90 // vector to all-one or all-zero.
91 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
93 // Set up the register classes.
94 addRegisterClass(MVT::i32, &AArch64::GPR32allRegClass);
95 addRegisterClass(MVT::i64, &AArch64::GPR64allRegClass);
97 if (Subtarget->hasFPARMv8()) {
98 addRegisterClass(MVT::f16, &AArch64::FPR16RegClass);
99 addRegisterClass(MVT::f32, &AArch64::FPR32RegClass);
100 addRegisterClass(MVT::f64, &AArch64::FPR64RegClass);
101 addRegisterClass(MVT::f128, &AArch64::FPR128RegClass);
104 if (Subtarget->hasNEON()) {
105 addRegisterClass(MVT::v16i8, &AArch64::FPR8RegClass);
106 addRegisterClass(MVT::v8i16, &AArch64::FPR16RegClass);
107 // Someone set us up the NEON.
108 addDRTypeForNEON(MVT::v2f32);
109 addDRTypeForNEON(MVT::v8i8);
110 addDRTypeForNEON(MVT::v4i16);
111 addDRTypeForNEON(MVT::v2i32);
112 addDRTypeForNEON(MVT::v1i64);
113 addDRTypeForNEON(MVT::v1f64);
114 addDRTypeForNEON(MVT::v4f16);
116 addQRTypeForNEON(MVT::v4f32);
117 addQRTypeForNEON(MVT::v2f64);
118 addQRTypeForNEON(MVT::v16i8);
119 addQRTypeForNEON(MVT::v8i16);
120 addQRTypeForNEON(MVT::v4i32);
121 addQRTypeForNEON(MVT::v2i64);
122 addQRTypeForNEON(MVT::v8f16);
125 // Compute derived properties from the register classes
126 computeRegisterProperties(Subtarget->getRegisterInfo());
128 // Provide all sorts of operation actions
129 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
130 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
131 setOperationAction(ISD::SETCC, MVT::i32, Custom);
132 setOperationAction(ISD::SETCC, MVT::i64, Custom);
133 setOperationAction(ISD::SETCC, MVT::f32, Custom);
134 setOperationAction(ISD::SETCC, MVT::f64, Custom);
135 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
136 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
137 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
138 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
139 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
140 setOperationAction(ISD::SELECT, MVT::i32, Custom);
141 setOperationAction(ISD::SELECT, MVT::i64, Custom);
142 setOperationAction(ISD::SELECT, MVT::f32, Custom);
143 setOperationAction(ISD::SELECT, MVT::f64, Custom);
144 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
145 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
146 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
147 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
148 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
149 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
151 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
152 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
153 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
155 setOperationAction(ISD::FREM, MVT::f32, Expand);
156 setOperationAction(ISD::FREM, MVT::f64, Expand);
157 setOperationAction(ISD::FREM, MVT::f80, Expand);
159 // Custom lowering hooks are needed for XOR
160 // to fold it into CSINC/CSINV.
161 setOperationAction(ISD::XOR, MVT::i32, Custom);
162 setOperationAction(ISD::XOR, MVT::i64, Custom);
164 // Virtually no operation on f128 is legal, but LLVM can't expand them when
165 // there's a valid register class, so we need custom operations in most cases.
166 setOperationAction(ISD::FABS, MVT::f128, Expand);
167 setOperationAction(ISD::FADD, MVT::f128, Custom);
168 setOperationAction(ISD::FCOPYSIGN, MVT::f128, Expand);
169 setOperationAction(ISD::FCOS, MVT::f128, Expand);
170 setOperationAction(ISD::FDIV, MVT::f128, Custom);
171 setOperationAction(ISD::FMA, MVT::f128, Expand);
172 setOperationAction(ISD::FMUL, MVT::f128, Custom);
173 setOperationAction(ISD::FNEG, MVT::f128, Expand);
174 setOperationAction(ISD::FPOW, MVT::f128, Expand);
175 setOperationAction(ISD::FREM, MVT::f128, Expand);
176 setOperationAction(ISD::FRINT, MVT::f128, Expand);
177 setOperationAction(ISD::FSIN, MVT::f128, Expand);
178 setOperationAction(ISD::FSINCOS, MVT::f128, Expand);
179 setOperationAction(ISD::FSQRT, MVT::f128, Expand);
180 setOperationAction(ISD::FSUB, MVT::f128, Custom);
181 setOperationAction(ISD::FTRUNC, MVT::f128, Expand);
182 setOperationAction(ISD::SETCC, MVT::f128, Custom);
183 setOperationAction(ISD::BR_CC, MVT::f128, Custom);
184 setOperationAction(ISD::SELECT, MVT::f128, Custom);
185 setOperationAction(ISD::SELECT_CC, MVT::f128, Custom);
186 setOperationAction(ISD::FP_EXTEND, MVT::f128, Custom);
188 // Lowering for many of the conversions is actually specified by the non-f128
189 // type. The LowerXXX function will be trivial when f128 isn't involved.
190 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
191 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
192 setOperationAction(ISD::FP_TO_SINT, MVT::i128, Custom);
193 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
194 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
195 setOperationAction(ISD::FP_TO_UINT, MVT::i128, Custom);
196 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
197 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
198 setOperationAction(ISD::SINT_TO_FP, MVT::i128, Custom);
199 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
200 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
201 setOperationAction(ISD::UINT_TO_FP, MVT::i128, Custom);
202 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
203 setOperationAction(ISD::FP_ROUND, MVT::f64, Custom);
205 // Variable arguments.
206 setOperationAction(ISD::VASTART, MVT::Other, Custom);
207 setOperationAction(ISD::VAARG, MVT::Other, Custom);
208 setOperationAction(ISD::VACOPY, MVT::Other, Custom);
209 setOperationAction(ISD::VAEND, MVT::Other, Expand);
211 // Variable-sized objects.
212 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
213 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
214 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
216 // Exception handling.
217 // FIXME: These are guesses. Has this been defined yet?
218 setExceptionPointerRegister(AArch64::X0);
219 setExceptionSelectorRegister(AArch64::X1);
221 // Constant pool entries
222 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
225 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
227 // Add/Sub overflow ops with MVT::Glues are lowered to NZCV dependences.
228 setOperationAction(ISD::ADDC, MVT::i32, Custom);
229 setOperationAction(ISD::ADDE, MVT::i32, Custom);
230 setOperationAction(ISD::SUBC, MVT::i32, Custom);
231 setOperationAction(ISD::SUBE, MVT::i32, Custom);
232 setOperationAction(ISD::ADDC, MVT::i64, Custom);
233 setOperationAction(ISD::ADDE, MVT::i64, Custom);
234 setOperationAction(ISD::SUBC, MVT::i64, Custom);
235 setOperationAction(ISD::SUBE, MVT::i64, Custom);
237 // AArch64 lacks both left-rotate and popcount instructions.
238 setOperationAction(ISD::ROTL, MVT::i32, Expand);
239 setOperationAction(ISD::ROTL, MVT::i64, Expand);
241 // AArch64 doesn't have {U|S}MUL_LOHI.
242 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
243 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
246 // Expand the undefined-at-zero variants to cttz/ctlz to their defined-at-zero
247 // counterparts, which AArch64 supports directly.
248 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
249 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
250 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
251 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
253 setOperationAction(ISD::CTPOP, MVT::i32, Custom);
254 setOperationAction(ISD::CTPOP, MVT::i64, Custom);
256 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
257 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
258 setOperationAction(ISD::SREM, MVT::i32, Expand);
259 setOperationAction(ISD::SREM, MVT::i64, Expand);
260 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
261 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
262 setOperationAction(ISD::UREM, MVT::i32, Expand);
263 setOperationAction(ISD::UREM, MVT::i64, Expand);
265 // Custom lower Add/Sub/Mul with overflow.
266 setOperationAction(ISD::SADDO, MVT::i32, Custom);
267 setOperationAction(ISD::SADDO, MVT::i64, Custom);
268 setOperationAction(ISD::UADDO, MVT::i32, Custom);
269 setOperationAction(ISD::UADDO, MVT::i64, Custom);
270 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
271 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
272 setOperationAction(ISD::USUBO, MVT::i32, Custom);
273 setOperationAction(ISD::USUBO, MVT::i64, Custom);
274 setOperationAction(ISD::SMULO, MVT::i32, Custom);
275 setOperationAction(ISD::SMULO, MVT::i64, Custom);
276 setOperationAction(ISD::UMULO, MVT::i32, Custom);
277 setOperationAction(ISD::UMULO, MVT::i64, Custom);
279 setOperationAction(ISD::FSIN, MVT::f32, Expand);
280 setOperationAction(ISD::FSIN, MVT::f64, Expand);
281 setOperationAction(ISD::FCOS, MVT::f32, Expand);
282 setOperationAction(ISD::FCOS, MVT::f64, Expand);
283 setOperationAction(ISD::FPOW, MVT::f32, Expand);
284 setOperationAction(ISD::FPOW, MVT::f64, Expand);
285 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
286 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
288 // f16 is a storage-only type, always promote it to f32.
289 setOperationAction(ISD::SETCC, MVT::f16, Promote);
290 setOperationAction(ISD::BR_CC, MVT::f16, Promote);
291 setOperationAction(ISD::SELECT_CC, MVT::f16, Promote);
292 setOperationAction(ISD::SELECT, MVT::f16, Promote);
293 setOperationAction(ISD::FADD, MVT::f16, Promote);
294 setOperationAction(ISD::FSUB, MVT::f16, Promote);
295 setOperationAction(ISD::FMUL, MVT::f16, Promote);
296 setOperationAction(ISD::FDIV, MVT::f16, Promote);
297 setOperationAction(ISD::FREM, MVT::f16, Promote);
298 setOperationAction(ISD::FMA, MVT::f16, Promote);
299 setOperationAction(ISD::FNEG, MVT::f16, Promote);
300 setOperationAction(ISD::FABS, MVT::f16, Promote);
301 setOperationAction(ISD::FCEIL, MVT::f16, Promote);
302 setOperationAction(ISD::FCOPYSIGN, MVT::f16, Promote);
303 setOperationAction(ISD::FCOS, MVT::f16, Promote);
304 setOperationAction(ISD::FFLOOR, MVT::f16, Promote);
305 setOperationAction(ISD::FNEARBYINT, MVT::f16, Promote);
306 setOperationAction(ISD::FPOW, MVT::f16, Promote);
307 setOperationAction(ISD::FPOWI, MVT::f16, Promote);
308 setOperationAction(ISD::FRINT, MVT::f16, Promote);
309 setOperationAction(ISD::FSIN, MVT::f16, Promote);
310 setOperationAction(ISD::FSINCOS, MVT::f16, Promote);
311 setOperationAction(ISD::FSQRT, MVT::f16, Promote);
312 setOperationAction(ISD::FEXP, MVT::f16, Promote);
313 setOperationAction(ISD::FEXP2, MVT::f16, Promote);
314 setOperationAction(ISD::FLOG, MVT::f16, Promote);
315 setOperationAction(ISD::FLOG2, MVT::f16, Promote);
316 setOperationAction(ISD::FLOG10, MVT::f16, Promote);
317 setOperationAction(ISD::FROUND, MVT::f16, Promote);
318 setOperationAction(ISD::FTRUNC, MVT::f16, Promote);
319 setOperationAction(ISD::FMINNUM, MVT::f16, Promote);
320 setOperationAction(ISD::FMAXNUM, MVT::f16, Promote);
322 // v4f16 is also a storage-only type, so promote it to v4f32 when that is
324 setOperationAction(ISD::FADD, MVT::v4f16, Promote);
325 setOperationAction(ISD::FSUB, MVT::v4f16, Promote);
326 setOperationAction(ISD::FMUL, MVT::v4f16, Promote);
327 setOperationAction(ISD::FDIV, MVT::v4f16, Promote);
328 setOperationAction(ISD::FP_EXTEND, MVT::v4f16, Promote);
329 setOperationAction(ISD::FP_ROUND, MVT::v4f16, Promote);
330 AddPromotedToType(ISD::FADD, MVT::v4f16, MVT::v4f32);
331 AddPromotedToType(ISD::FSUB, MVT::v4f16, MVT::v4f32);
332 AddPromotedToType(ISD::FMUL, MVT::v4f16, MVT::v4f32);
333 AddPromotedToType(ISD::FDIV, MVT::v4f16, MVT::v4f32);
334 AddPromotedToType(ISD::FP_EXTEND, MVT::v4f16, MVT::v4f32);
335 AddPromotedToType(ISD::FP_ROUND, MVT::v4f16, MVT::v4f32);
337 // Expand all other v4f16 operations.
338 // FIXME: We could generate better code by promoting some operations to
340 setOperationAction(ISD::FABS, MVT::v4f16, Expand);
341 setOperationAction(ISD::FCEIL, MVT::v4f16, Expand);
342 setOperationAction(ISD::FCOPYSIGN, MVT::v4f16, Expand);
343 setOperationAction(ISD::FCOS, MVT::v4f16, Expand);
344 setOperationAction(ISD::FFLOOR, MVT::v4f16, Expand);
345 setOperationAction(ISD::FMA, MVT::v4f16, Expand);
346 setOperationAction(ISD::FNEARBYINT, MVT::v4f16, Expand);
347 setOperationAction(ISD::FNEG, MVT::v4f16, Expand);
348 setOperationAction(ISD::FPOW, MVT::v4f16, Expand);
349 setOperationAction(ISD::FPOWI, MVT::v4f16, Expand);
350 setOperationAction(ISD::FREM, MVT::v4f16, Expand);
351 setOperationAction(ISD::FROUND, MVT::v4f16, Expand);
352 setOperationAction(ISD::FRINT, MVT::v4f16, Expand);
353 setOperationAction(ISD::FSIN, MVT::v4f16, Expand);
354 setOperationAction(ISD::FSINCOS, MVT::v4f16, Expand);
355 setOperationAction(ISD::FSQRT, MVT::v4f16, Expand);
356 setOperationAction(ISD::FTRUNC, MVT::v4f16, Expand);
357 setOperationAction(ISD::SETCC, MVT::v4f16, Expand);
358 setOperationAction(ISD::BR_CC, MVT::v4f16, Expand);
359 setOperationAction(ISD::SELECT, MVT::v4f16, Expand);
360 setOperationAction(ISD::SELECT_CC, MVT::v4f16, Expand);
361 setOperationAction(ISD::FEXP, MVT::v4f16, Expand);
362 setOperationAction(ISD::FEXP2, MVT::v4f16, Expand);
363 setOperationAction(ISD::FLOG, MVT::v4f16, Expand);
364 setOperationAction(ISD::FLOG2, MVT::v4f16, Expand);
365 setOperationAction(ISD::FLOG10, MVT::v4f16, Expand);
368 // v8f16 is also a storage-only type, so expand it.
369 setOperationAction(ISD::FABS, MVT::v8f16, Expand);
370 setOperationAction(ISD::FADD, MVT::v8f16, Expand);
371 setOperationAction(ISD::FCEIL, MVT::v8f16, Expand);
372 setOperationAction(ISD::FCOPYSIGN, MVT::v8f16, Expand);
373 setOperationAction(ISD::FCOS, MVT::v8f16, Expand);
374 setOperationAction(ISD::FDIV, MVT::v8f16, Expand);
375 setOperationAction(ISD::FFLOOR, MVT::v8f16, Expand);
376 setOperationAction(ISD::FMA, MVT::v8f16, Expand);
377 setOperationAction(ISD::FMUL, MVT::v8f16, Expand);
378 setOperationAction(ISD::FNEARBYINT, MVT::v8f16, Expand);
379 setOperationAction(ISD::FNEG, MVT::v8f16, Expand);
380 setOperationAction(ISD::FPOW, MVT::v8f16, Expand);
381 setOperationAction(ISD::FPOWI, MVT::v8f16, Expand);
382 setOperationAction(ISD::FREM, MVT::v8f16, Expand);
383 setOperationAction(ISD::FROUND, MVT::v8f16, Expand);
384 setOperationAction(ISD::FRINT, MVT::v8f16, Expand);
385 setOperationAction(ISD::FSIN, MVT::v8f16, Expand);
386 setOperationAction(ISD::FSINCOS, MVT::v8f16, Expand);
387 setOperationAction(ISD::FSQRT, MVT::v8f16, Expand);
388 setOperationAction(ISD::FSUB, MVT::v8f16, Expand);
389 setOperationAction(ISD::FTRUNC, MVT::v8f16, Expand);
390 setOperationAction(ISD::SETCC, MVT::v8f16, Expand);
391 setOperationAction(ISD::BR_CC, MVT::v8f16, Expand);
392 setOperationAction(ISD::SELECT, MVT::v8f16, Expand);
393 setOperationAction(ISD::SELECT_CC, MVT::v8f16, Expand);
394 setOperationAction(ISD::FP_EXTEND, MVT::v8f16, Expand);
395 setOperationAction(ISD::FEXP, MVT::v8f16, Expand);
396 setOperationAction(ISD::FEXP2, MVT::v8f16, Expand);
397 setOperationAction(ISD::FLOG, MVT::v8f16, Expand);
398 setOperationAction(ISD::FLOG2, MVT::v8f16, Expand);
399 setOperationAction(ISD::FLOG10, MVT::v8f16, Expand);
401 // AArch64 has implementations of a lot of rounding-like FP operations.
402 for (MVT Ty : {MVT::f32, MVT::f64}) {
403 setOperationAction(ISD::FFLOOR, Ty, Legal);
404 setOperationAction(ISD::FNEARBYINT, Ty, Legal);
405 setOperationAction(ISD::FCEIL, Ty, Legal);
406 setOperationAction(ISD::FRINT, Ty, Legal);
407 setOperationAction(ISD::FTRUNC, Ty, Legal);
408 setOperationAction(ISD::FROUND, Ty, Legal);
411 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
413 if (Subtarget->isTargetMachO()) {
414 // For iOS, we don't want to the normal expansion of a libcall to
415 // sincos. We want to issue a libcall to __sincos_stret to avoid memory
417 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
418 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
420 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
421 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
424 // Make floating-point constants legal for the large code model, so they don't
425 // become loads from the constant pool.
426 if (Subtarget->isTargetMachO() && TM.getCodeModel() == CodeModel::Large) {
427 setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
428 setOperationAction(ISD::ConstantFP, MVT::f64, Legal);
431 // AArch64 does not have floating-point extending loads, i1 sign-extending
432 // load, floating-point truncating stores, or v2i32->v2i16 truncating store.
433 for (MVT VT : MVT::fp_valuetypes()) {
434 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
435 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
436 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f64, Expand);
437 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f80, Expand);
439 for (MVT VT : MVT::integer_valuetypes())
440 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Expand);
442 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
443 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
444 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
445 setTruncStoreAction(MVT::f128, MVT::f80, Expand);
446 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
447 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
448 setTruncStoreAction(MVT::f128, MVT::f16, Expand);
450 setOperationAction(ISD::BITCAST, MVT::i16, Custom);
451 setOperationAction(ISD::BITCAST, MVT::f16, Custom);
453 // Indexed loads and stores are supported.
454 for (unsigned im = (unsigned)ISD::PRE_INC;
455 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
456 setIndexedLoadAction(im, MVT::i8, Legal);
457 setIndexedLoadAction(im, MVT::i16, Legal);
458 setIndexedLoadAction(im, MVT::i32, Legal);
459 setIndexedLoadAction(im, MVT::i64, Legal);
460 setIndexedLoadAction(im, MVT::f64, Legal);
461 setIndexedLoadAction(im, MVT::f32, Legal);
462 setIndexedStoreAction(im, MVT::i8, Legal);
463 setIndexedStoreAction(im, MVT::i16, Legal);
464 setIndexedStoreAction(im, MVT::i32, Legal);
465 setIndexedStoreAction(im, MVT::i64, Legal);
466 setIndexedStoreAction(im, MVT::f64, Legal);
467 setIndexedStoreAction(im, MVT::f32, Legal);
471 setOperationAction(ISD::TRAP, MVT::Other, Legal);
473 // We combine OR nodes for bitfield operations.
474 setTargetDAGCombine(ISD::OR);
476 // Vector add and sub nodes may conceal a high-half opportunity.
477 // Also, try to fold ADD into CSINC/CSINV..
478 setTargetDAGCombine(ISD::ADD);
479 setTargetDAGCombine(ISD::SUB);
481 setTargetDAGCombine(ISD::XOR);
482 setTargetDAGCombine(ISD::SINT_TO_FP);
483 setTargetDAGCombine(ISD::UINT_TO_FP);
485 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
487 setTargetDAGCombine(ISD::ANY_EXTEND);
488 setTargetDAGCombine(ISD::ZERO_EXTEND);
489 setTargetDAGCombine(ISD::SIGN_EXTEND);
490 setTargetDAGCombine(ISD::BITCAST);
491 setTargetDAGCombine(ISD::CONCAT_VECTORS);
492 setTargetDAGCombine(ISD::STORE);
494 setTargetDAGCombine(ISD::MUL);
496 setTargetDAGCombine(ISD::SELECT);
497 setTargetDAGCombine(ISD::VSELECT);
498 setTargetDAGCombine(ISD::SELECT_CC);
500 setTargetDAGCombine(ISD::INTRINSIC_VOID);
501 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
502 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
504 MaxStoresPerMemset = MaxStoresPerMemsetOptSize = 8;
505 MaxStoresPerMemcpy = MaxStoresPerMemcpyOptSize = 4;
506 MaxStoresPerMemmove = MaxStoresPerMemmoveOptSize = 4;
508 setStackPointerRegisterToSaveRestore(AArch64::SP);
510 setSchedulingPreference(Sched::Hybrid);
513 MaskAndBranchFoldingIsLegal = true;
514 EnableExtLdPromotion = true;
516 setMinFunctionAlignment(2);
518 RequireStrictAlign = (Align == StrictAlign);
520 setHasExtractBitsInsn(true);
522 if (Subtarget->hasNEON()) {
523 // FIXME: v1f64 shouldn't be legal if we can avoid it, because it leads to
524 // silliness like this:
525 setOperationAction(ISD::FABS, MVT::v1f64, Expand);
526 setOperationAction(ISD::FADD, MVT::v1f64, Expand);
527 setOperationAction(ISD::FCEIL, MVT::v1f64, Expand);
528 setOperationAction(ISD::FCOPYSIGN, MVT::v1f64, Expand);
529 setOperationAction(ISD::FCOS, MVT::v1f64, Expand);
530 setOperationAction(ISD::FDIV, MVT::v1f64, Expand);
531 setOperationAction(ISD::FFLOOR, MVT::v1f64, Expand);
532 setOperationAction(ISD::FMA, MVT::v1f64, Expand);
533 setOperationAction(ISD::FMUL, MVT::v1f64, Expand);
534 setOperationAction(ISD::FNEARBYINT, MVT::v1f64, Expand);
535 setOperationAction(ISD::FNEG, MVT::v1f64, Expand);
536 setOperationAction(ISD::FPOW, MVT::v1f64, Expand);
537 setOperationAction(ISD::FREM, MVT::v1f64, Expand);
538 setOperationAction(ISD::FROUND, MVT::v1f64, Expand);
539 setOperationAction(ISD::FRINT, MVT::v1f64, Expand);
540 setOperationAction(ISD::FSIN, MVT::v1f64, Expand);
541 setOperationAction(ISD::FSINCOS, MVT::v1f64, Expand);
542 setOperationAction(ISD::FSQRT, MVT::v1f64, Expand);
543 setOperationAction(ISD::FSUB, MVT::v1f64, Expand);
544 setOperationAction(ISD::FTRUNC, MVT::v1f64, Expand);
545 setOperationAction(ISD::SETCC, MVT::v1f64, Expand);
546 setOperationAction(ISD::BR_CC, MVT::v1f64, Expand);
547 setOperationAction(ISD::SELECT, MVT::v1f64, Expand);
548 setOperationAction(ISD::SELECT_CC, MVT::v1f64, Expand);
549 setOperationAction(ISD::FP_EXTEND, MVT::v1f64, Expand);
551 setOperationAction(ISD::FP_TO_SINT, MVT::v1i64, Expand);
552 setOperationAction(ISD::FP_TO_UINT, MVT::v1i64, Expand);
553 setOperationAction(ISD::SINT_TO_FP, MVT::v1i64, Expand);
554 setOperationAction(ISD::UINT_TO_FP, MVT::v1i64, Expand);
555 setOperationAction(ISD::FP_ROUND, MVT::v1f64, Expand);
557 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
559 // AArch64 doesn't have a direct vector ->f32 conversion instructions for
560 // elements smaller than i32, so promote the input to i32 first.
561 setOperationAction(ISD::UINT_TO_FP, MVT::v4i8, Promote);
562 setOperationAction(ISD::SINT_TO_FP, MVT::v4i8, Promote);
563 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Promote);
564 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Promote);
565 // i8 and i16 vector elements also need promotion to i32 for v8i8 or v8i16
566 // -> v8f16 conversions.
567 setOperationAction(ISD::SINT_TO_FP, MVT::v8i8, Promote);
568 setOperationAction(ISD::UINT_TO_FP, MVT::v8i8, Promote);
569 setOperationAction(ISD::SINT_TO_FP, MVT::v8i16, Promote);
570 setOperationAction(ISD::UINT_TO_FP, MVT::v8i16, Promote);
571 // Similarly, there is no direct i32 -> f64 vector conversion instruction.
572 setOperationAction(ISD::SINT_TO_FP, MVT::v2i32, Custom);
573 setOperationAction(ISD::UINT_TO_FP, MVT::v2i32, Custom);
574 setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Custom);
575 setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Custom);
576 // Or, direct i32 -> f16 vector conversion. Set it so custom, so the
577 // conversion happens in two steps: v4i32 -> v4f32 -> v4f16
578 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Custom);
579 setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, Custom);
581 // AArch64 doesn't have MUL.2d:
582 setOperationAction(ISD::MUL, MVT::v2i64, Expand);
583 // Custom handling for some quad-vector types to detect MULL.
584 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
585 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
586 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
588 setOperationAction(ISD::ANY_EXTEND, MVT::v4i32, Legal);
589 setTruncStoreAction(MVT::v2i32, MVT::v2i16, Expand);
590 // Likewise, narrowing and extending vector loads/stores aren't handled
592 for (MVT VT : MVT::vector_valuetypes()) {
593 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
595 setOperationAction(ISD::MULHS, VT, Expand);
596 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
597 setOperationAction(ISD::MULHU, VT, Expand);
598 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
600 setOperationAction(ISD::BSWAP, VT, Expand);
602 for (MVT InnerVT : MVT::vector_valuetypes()) {
603 setTruncStoreAction(VT, InnerVT, Expand);
604 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
605 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
606 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
610 // AArch64 has implementations of a lot of rounding-like FP operations.
611 for (MVT Ty : {MVT::v2f32, MVT::v4f32, MVT::v2f64}) {
612 setOperationAction(ISD::FFLOOR, Ty, Legal);
613 setOperationAction(ISD::FNEARBYINT, Ty, Legal);
614 setOperationAction(ISD::FCEIL, Ty, Legal);
615 setOperationAction(ISD::FRINT, Ty, Legal);
616 setOperationAction(ISD::FTRUNC, Ty, Legal);
617 setOperationAction(ISD::FROUND, Ty, Legal);
621 // Prefer likely predicted branches to selects on out-of-order cores.
622 if (Subtarget->isCortexA57())
623 PredictableSelectIsExpensive = true;
626 void AArch64TargetLowering::addTypeForNEON(EVT VT, EVT PromotedBitwiseVT) {
627 if (VT == MVT::v2f32 || VT == MVT::v4f16) {
628 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
629 AddPromotedToType(ISD::LOAD, VT.getSimpleVT(), MVT::v2i32);
631 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
632 AddPromotedToType(ISD::STORE, VT.getSimpleVT(), MVT::v2i32);
633 } else if (VT == MVT::v2f64 || VT == MVT::v4f32 || VT == MVT::v8f16) {
634 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
635 AddPromotedToType(ISD::LOAD, VT.getSimpleVT(), MVT::v2i64);
637 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
638 AddPromotedToType(ISD::STORE, VT.getSimpleVT(), MVT::v2i64);
641 // Mark vector float intrinsics as expand.
642 if (VT == MVT::v2f32 || VT == MVT::v4f32 || VT == MVT::v2f64) {
643 setOperationAction(ISD::FSIN, VT.getSimpleVT(), Expand);
644 setOperationAction(ISD::FCOS, VT.getSimpleVT(), Expand);
645 setOperationAction(ISD::FPOWI, VT.getSimpleVT(), Expand);
646 setOperationAction(ISD::FPOW, VT.getSimpleVT(), Expand);
647 setOperationAction(ISD::FLOG, VT.getSimpleVT(), Expand);
648 setOperationAction(ISD::FLOG2, VT.getSimpleVT(), Expand);
649 setOperationAction(ISD::FLOG10, VT.getSimpleVT(), Expand);
650 setOperationAction(ISD::FEXP, VT.getSimpleVT(), Expand);
651 setOperationAction(ISD::FEXP2, VT.getSimpleVT(), Expand);
654 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
655 setOperationAction(ISD::INSERT_VECTOR_ELT, VT.getSimpleVT(), Custom);
656 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
657 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
658 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Custom);
659 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
660 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
661 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
662 setOperationAction(ISD::AND, VT.getSimpleVT(), Custom);
663 setOperationAction(ISD::OR, VT.getSimpleVT(), Custom);
664 setOperationAction(ISD::SETCC, VT.getSimpleVT(), Custom);
665 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
667 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
668 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
669 setOperationAction(ISD::VSELECT, VT.getSimpleVT(), Expand);
670 for (MVT InnerVT : MVT::all_valuetypes())
671 setLoadExtAction(ISD::EXTLOAD, InnerVT, VT.getSimpleVT(), Expand);
673 // CNT supports only B element sizes.
674 if (VT != MVT::v8i8 && VT != MVT::v16i8)
675 setOperationAction(ISD::CTPOP, VT.getSimpleVT(), Expand);
677 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
678 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
679 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
680 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
681 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
683 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Custom);
684 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Custom);
686 // [SU][MIN|MAX] and [SU]ABSDIFF are available for all NEON types apart from
688 if (!VT.isFloatingPoint() &&
689 VT.getSimpleVT() != MVT::v2i64 && VT.getSimpleVT() != MVT::v1i64)
690 for (unsigned Opcode : {ISD::SMIN, ISD::SMAX, ISD::UMIN, ISD::UMAX,
691 ISD::SABSDIFF, ISD::UABSDIFF})
692 setOperationAction(Opcode, VT.getSimpleVT(), Legal);
694 if (Subtarget->isLittleEndian()) {
695 for (unsigned im = (unsigned)ISD::PRE_INC;
696 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
697 setIndexedLoadAction(im, VT.getSimpleVT(), Legal);
698 setIndexedStoreAction(im, VT.getSimpleVT(), Legal);
703 void AArch64TargetLowering::addDRTypeForNEON(MVT VT) {
704 addRegisterClass(VT, &AArch64::FPR64RegClass);
705 addTypeForNEON(VT, MVT::v2i32);
708 void AArch64TargetLowering::addQRTypeForNEON(MVT VT) {
709 addRegisterClass(VT, &AArch64::FPR128RegClass);
710 addTypeForNEON(VT, MVT::v4i32);
713 EVT AArch64TargetLowering::getSetCCResultType(const DataLayout &, LLVMContext &,
717 return VT.changeVectorElementTypeToInteger();
720 /// computeKnownBitsForTargetNode - Determine which of the bits specified in
721 /// Mask are known to be either zero or one and return them in the
722 /// KnownZero/KnownOne bitsets.
723 void AArch64TargetLowering::computeKnownBitsForTargetNode(
724 const SDValue Op, APInt &KnownZero, APInt &KnownOne,
725 const SelectionDAG &DAG, unsigned Depth) const {
726 switch (Op.getOpcode()) {
729 case AArch64ISD::CSEL: {
730 APInt KnownZero2, KnownOne2;
731 DAG.computeKnownBits(Op->getOperand(0), KnownZero, KnownOne, Depth + 1);
732 DAG.computeKnownBits(Op->getOperand(1), KnownZero2, KnownOne2, Depth + 1);
733 KnownZero &= KnownZero2;
734 KnownOne &= KnownOne2;
737 case ISD::INTRINSIC_W_CHAIN: {
738 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1));
739 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue());
742 case Intrinsic::aarch64_ldaxr:
743 case Intrinsic::aarch64_ldxr: {
744 unsigned BitWidth = KnownOne.getBitWidth();
745 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT();
746 unsigned MemBits = VT.getScalarType().getSizeInBits();
747 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
753 case ISD::INTRINSIC_WO_CHAIN:
754 case ISD::INTRINSIC_VOID: {
755 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
759 case Intrinsic::aarch64_neon_umaxv:
760 case Intrinsic::aarch64_neon_uminv: {
761 // Figure out the datatype of the vector operand. The UMINV instruction
762 // will zero extend the result, so we can mark as known zero all the
763 // bits larger than the element datatype. 32-bit or larget doesn't need
764 // this as those are legal types and will be handled by isel directly.
765 MVT VT = Op.getOperand(1).getValueType().getSimpleVT();
766 unsigned BitWidth = KnownZero.getBitWidth();
767 if (VT == MVT::v8i8 || VT == MVT::v16i8) {
768 assert(BitWidth >= 8 && "Unexpected width!");
769 APInt Mask = APInt::getHighBitsSet(BitWidth, BitWidth - 8);
771 } else if (VT == MVT::v4i16 || VT == MVT::v8i16) {
772 assert(BitWidth >= 16 && "Unexpected width!");
773 APInt Mask = APInt::getHighBitsSet(BitWidth, BitWidth - 16);
783 MVT AArch64TargetLowering::getScalarShiftAmountTy(const DataLayout &DL,
789 AArch64TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
790 const TargetLibraryInfo *libInfo) const {
791 return AArch64::createFastISel(funcInfo, libInfo);
794 const char *AArch64TargetLowering::getTargetNodeName(unsigned Opcode) const {
795 switch ((AArch64ISD::NodeType)Opcode) {
796 case AArch64ISD::FIRST_NUMBER: break;
797 case AArch64ISD::CALL: return "AArch64ISD::CALL";
798 case AArch64ISD::ADRP: return "AArch64ISD::ADRP";
799 case AArch64ISD::ADDlow: return "AArch64ISD::ADDlow";
800 case AArch64ISD::LOADgot: return "AArch64ISD::LOADgot";
801 case AArch64ISD::RET_FLAG: return "AArch64ISD::RET_FLAG";
802 case AArch64ISD::BRCOND: return "AArch64ISD::BRCOND";
803 case AArch64ISD::CSEL: return "AArch64ISD::CSEL";
804 case AArch64ISD::FCSEL: return "AArch64ISD::FCSEL";
805 case AArch64ISD::CSINV: return "AArch64ISD::CSINV";
806 case AArch64ISD::CSNEG: return "AArch64ISD::CSNEG";
807 case AArch64ISD::CSINC: return "AArch64ISD::CSINC";
808 case AArch64ISD::THREAD_POINTER: return "AArch64ISD::THREAD_POINTER";
809 case AArch64ISD::TLSDESC_CALLSEQ: return "AArch64ISD::TLSDESC_CALLSEQ";
810 case AArch64ISD::ADC: return "AArch64ISD::ADC";
811 case AArch64ISD::SBC: return "AArch64ISD::SBC";
812 case AArch64ISD::ADDS: return "AArch64ISD::ADDS";
813 case AArch64ISD::SUBS: return "AArch64ISD::SUBS";
814 case AArch64ISD::ADCS: return "AArch64ISD::ADCS";
815 case AArch64ISD::SBCS: return "AArch64ISD::SBCS";
816 case AArch64ISD::ANDS: return "AArch64ISD::ANDS";
817 case AArch64ISD::CCMP: return "AArch64ISD::CCMP";
818 case AArch64ISD::CCMN: return "AArch64ISD::CCMN";
819 case AArch64ISD::FCCMP: return "AArch64ISD::FCCMP";
820 case AArch64ISD::FCMP: return "AArch64ISD::FCMP";
821 case AArch64ISD::FMIN: return "AArch64ISD::FMIN";
822 case AArch64ISD::FMAX: return "AArch64ISD::FMAX";
823 case AArch64ISD::DUP: return "AArch64ISD::DUP";
824 case AArch64ISD::DUPLANE8: return "AArch64ISD::DUPLANE8";
825 case AArch64ISD::DUPLANE16: return "AArch64ISD::DUPLANE16";
826 case AArch64ISD::DUPLANE32: return "AArch64ISD::DUPLANE32";
827 case AArch64ISD::DUPLANE64: return "AArch64ISD::DUPLANE64";
828 case AArch64ISD::MOVI: return "AArch64ISD::MOVI";
829 case AArch64ISD::MOVIshift: return "AArch64ISD::MOVIshift";
830 case AArch64ISD::MOVIedit: return "AArch64ISD::MOVIedit";
831 case AArch64ISD::MOVImsl: return "AArch64ISD::MOVImsl";
832 case AArch64ISD::FMOV: return "AArch64ISD::FMOV";
833 case AArch64ISD::MVNIshift: return "AArch64ISD::MVNIshift";
834 case AArch64ISD::MVNImsl: return "AArch64ISD::MVNImsl";
835 case AArch64ISD::BICi: return "AArch64ISD::BICi";
836 case AArch64ISD::ORRi: return "AArch64ISD::ORRi";
837 case AArch64ISD::BSL: return "AArch64ISD::BSL";
838 case AArch64ISD::NEG: return "AArch64ISD::NEG";
839 case AArch64ISD::EXTR: return "AArch64ISD::EXTR";
840 case AArch64ISD::ZIP1: return "AArch64ISD::ZIP1";
841 case AArch64ISD::ZIP2: return "AArch64ISD::ZIP2";
842 case AArch64ISD::UZP1: return "AArch64ISD::UZP1";
843 case AArch64ISD::UZP2: return "AArch64ISD::UZP2";
844 case AArch64ISD::TRN1: return "AArch64ISD::TRN1";
845 case AArch64ISD::TRN2: return "AArch64ISD::TRN2";
846 case AArch64ISD::REV16: return "AArch64ISD::REV16";
847 case AArch64ISD::REV32: return "AArch64ISD::REV32";
848 case AArch64ISD::REV64: return "AArch64ISD::REV64";
849 case AArch64ISD::EXT: return "AArch64ISD::EXT";
850 case AArch64ISD::VSHL: return "AArch64ISD::VSHL";
851 case AArch64ISD::VLSHR: return "AArch64ISD::VLSHR";
852 case AArch64ISD::VASHR: return "AArch64ISD::VASHR";
853 case AArch64ISD::CMEQ: return "AArch64ISD::CMEQ";
854 case AArch64ISD::CMGE: return "AArch64ISD::CMGE";
855 case AArch64ISD::CMGT: return "AArch64ISD::CMGT";
856 case AArch64ISD::CMHI: return "AArch64ISD::CMHI";
857 case AArch64ISD::CMHS: return "AArch64ISD::CMHS";
858 case AArch64ISD::FCMEQ: return "AArch64ISD::FCMEQ";
859 case AArch64ISD::FCMGE: return "AArch64ISD::FCMGE";
860 case AArch64ISD::FCMGT: return "AArch64ISD::FCMGT";
861 case AArch64ISD::CMEQz: return "AArch64ISD::CMEQz";
862 case AArch64ISD::CMGEz: return "AArch64ISD::CMGEz";
863 case AArch64ISD::CMGTz: return "AArch64ISD::CMGTz";
864 case AArch64ISD::CMLEz: return "AArch64ISD::CMLEz";
865 case AArch64ISD::CMLTz: return "AArch64ISD::CMLTz";
866 case AArch64ISD::FCMEQz: return "AArch64ISD::FCMEQz";
867 case AArch64ISD::FCMGEz: return "AArch64ISD::FCMGEz";
868 case AArch64ISD::FCMGTz: return "AArch64ISD::FCMGTz";
869 case AArch64ISD::FCMLEz: return "AArch64ISD::FCMLEz";
870 case AArch64ISD::FCMLTz: return "AArch64ISD::FCMLTz";
871 case AArch64ISD::SADDV: return "AArch64ISD::SADDV";
872 case AArch64ISD::UADDV: return "AArch64ISD::UADDV";
873 case AArch64ISD::SMINV: return "AArch64ISD::SMINV";
874 case AArch64ISD::UMINV: return "AArch64ISD::UMINV";
875 case AArch64ISD::SMAXV: return "AArch64ISD::SMAXV";
876 case AArch64ISD::UMAXV: return "AArch64ISD::UMAXV";
877 case AArch64ISD::NOT: return "AArch64ISD::NOT";
878 case AArch64ISD::BIT: return "AArch64ISD::BIT";
879 case AArch64ISD::CBZ: return "AArch64ISD::CBZ";
880 case AArch64ISD::CBNZ: return "AArch64ISD::CBNZ";
881 case AArch64ISD::TBZ: return "AArch64ISD::TBZ";
882 case AArch64ISD::TBNZ: return "AArch64ISD::TBNZ";
883 case AArch64ISD::TC_RETURN: return "AArch64ISD::TC_RETURN";
884 case AArch64ISD::PREFETCH: return "AArch64ISD::PREFETCH";
885 case AArch64ISD::SITOF: return "AArch64ISD::SITOF";
886 case AArch64ISD::UITOF: return "AArch64ISD::UITOF";
887 case AArch64ISD::NVCAST: return "AArch64ISD::NVCAST";
888 case AArch64ISD::SQSHL_I: return "AArch64ISD::SQSHL_I";
889 case AArch64ISD::UQSHL_I: return "AArch64ISD::UQSHL_I";
890 case AArch64ISD::SRSHR_I: return "AArch64ISD::SRSHR_I";
891 case AArch64ISD::URSHR_I: return "AArch64ISD::URSHR_I";
892 case AArch64ISD::SQSHLU_I: return "AArch64ISD::SQSHLU_I";
893 case AArch64ISD::WrapperLarge: return "AArch64ISD::WrapperLarge";
894 case AArch64ISD::LD2post: return "AArch64ISD::LD2post";
895 case AArch64ISD::LD3post: return "AArch64ISD::LD3post";
896 case AArch64ISD::LD4post: return "AArch64ISD::LD4post";
897 case AArch64ISD::ST2post: return "AArch64ISD::ST2post";
898 case AArch64ISD::ST3post: return "AArch64ISD::ST3post";
899 case AArch64ISD::ST4post: return "AArch64ISD::ST4post";
900 case AArch64ISD::LD1x2post: return "AArch64ISD::LD1x2post";
901 case AArch64ISD::LD1x3post: return "AArch64ISD::LD1x3post";
902 case AArch64ISD::LD1x4post: return "AArch64ISD::LD1x4post";
903 case AArch64ISD::ST1x2post: return "AArch64ISD::ST1x2post";
904 case AArch64ISD::ST1x3post: return "AArch64ISD::ST1x3post";
905 case AArch64ISD::ST1x4post: return "AArch64ISD::ST1x4post";
906 case AArch64ISD::LD1DUPpost: return "AArch64ISD::LD1DUPpost";
907 case AArch64ISD::LD2DUPpost: return "AArch64ISD::LD2DUPpost";
908 case AArch64ISD::LD3DUPpost: return "AArch64ISD::LD3DUPpost";
909 case AArch64ISD::LD4DUPpost: return "AArch64ISD::LD4DUPpost";
910 case AArch64ISD::LD1LANEpost: return "AArch64ISD::LD1LANEpost";
911 case AArch64ISD::LD2LANEpost: return "AArch64ISD::LD2LANEpost";
912 case AArch64ISD::LD3LANEpost: return "AArch64ISD::LD3LANEpost";
913 case AArch64ISD::LD4LANEpost: return "AArch64ISD::LD4LANEpost";
914 case AArch64ISD::ST2LANEpost: return "AArch64ISD::ST2LANEpost";
915 case AArch64ISD::ST3LANEpost: return "AArch64ISD::ST3LANEpost";
916 case AArch64ISD::ST4LANEpost: return "AArch64ISD::ST4LANEpost";
917 case AArch64ISD::SMULL: return "AArch64ISD::SMULL";
918 case AArch64ISD::UMULL: return "AArch64ISD::UMULL";
924 AArch64TargetLowering::EmitF128CSEL(MachineInstr *MI,
925 MachineBasicBlock *MBB) const {
926 // We materialise the F128CSEL pseudo-instruction as some control flow and a
930 // [... previous instrs leading to comparison ...]
936 // Dest = PHI [IfTrue, TrueBB], [IfFalse, OrigBB]
938 MachineFunction *MF = MBB->getParent();
939 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
940 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
941 DebugLoc DL = MI->getDebugLoc();
942 MachineFunction::iterator It = MBB;
945 unsigned DestReg = MI->getOperand(0).getReg();
946 unsigned IfTrueReg = MI->getOperand(1).getReg();
947 unsigned IfFalseReg = MI->getOperand(2).getReg();
948 unsigned CondCode = MI->getOperand(3).getImm();
949 bool NZCVKilled = MI->getOperand(4).isKill();
951 MachineBasicBlock *TrueBB = MF->CreateMachineBasicBlock(LLVM_BB);
952 MachineBasicBlock *EndBB = MF->CreateMachineBasicBlock(LLVM_BB);
953 MF->insert(It, TrueBB);
954 MF->insert(It, EndBB);
956 // Transfer rest of current basic-block to EndBB
957 EndBB->splice(EndBB->begin(), MBB, std::next(MachineBasicBlock::iterator(MI)),
959 EndBB->transferSuccessorsAndUpdatePHIs(MBB);
961 BuildMI(MBB, DL, TII->get(AArch64::Bcc)).addImm(CondCode).addMBB(TrueBB);
962 BuildMI(MBB, DL, TII->get(AArch64::B)).addMBB(EndBB);
963 MBB->addSuccessor(TrueBB);
964 MBB->addSuccessor(EndBB);
966 // TrueBB falls through to the end.
967 TrueBB->addSuccessor(EndBB);
970 TrueBB->addLiveIn(AArch64::NZCV);
971 EndBB->addLiveIn(AArch64::NZCV);
974 BuildMI(*EndBB, EndBB->begin(), DL, TII->get(AArch64::PHI), DestReg)
980 MI->eraseFromParent();
985 AArch64TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
986 MachineBasicBlock *BB) const {
987 switch (MI->getOpcode()) {
992 llvm_unreachable("Unexpected instruction for custom inserter!");
994 case AArch64::F128CSEL:
995 return EmitF128CSEL(MI, BB);
997 case TargetOpcode::STACKMAP:
998 case TargetOpcode::PATCHPOINT:
999 return emitPatchPoint(MI, BB);
1003 //===----------------------------------------------------------------------===//
1004 // AArch64 Lowering private implementation.
1005 //===----------------------------------------------------------------------===//
1007 //===----------------------------------------------------------------------===//
1009 //===----------------------------------------------------------------------===//
1011 /// changeIntCCToAArch64CC - Convert a DAG integer condition code to an AArch64
1013 static AArch64CC::CondCode changeIntCCToAArch64CC(ISD::CondCode CC) {
1016 llvm_unreachable("Unknown condition code!");
1018 return AArch64CC::NE;
1020 return AArch64CC::EQ;
1022 return AArch64CC::GT;
1024 return AArch64CC::GE;
1026 return AArch64CC::LT;
1028 return AArch64CC::LE;
1030 return AArch64CC::HI;
1032 return AArch64CC::HS;
1034 return AArch64CC::LO;
1036 return AArch64CC::LS;
1040 /// changeFPCCToAArch64CC - Convert a DAG fp condition code to an AArch64 CC.
1041 static void changeFPCCToAArch64CC(ISD::CondCode CC,
1042 AArch64CC::CondCode &CondCode,
1043 AArch64CC::CondCode &CondCode2) {
1044 CondCode2 = AArch64CC::AL;
1047 llvm_unreachable("Unknown FP condition!");
1050 CondCode = AArch64CC::EQ;
1054 CondCode = AArch64CC::GT;
1058 CondCode = AArch64CC::GE;
1061 CondCode = AArch64CC::MI;
1064 CondCode = AArch64CC::LS;
1067 CondCode = AArch64CC::MI;
1068 CondCode2 = AArch64CC::GT;
1071 CondCode = AArch64CC::VC;
1074 CondCode = AArch64CC::VS;
1077 CondCode = AArch64CC::EQ;
1078 CondCode2 = AArch64CC::VS;
1081 CondCode = AArch64CC::HI;
1084 CondCode = AArch64CC::PL;
1088 CondCode = AArch64CC::LT;
1092 CondCode = AArch64CC::LE;
1096 CondCode = AArch64CC::NE;
1101 /// changeVectorFPCCToAArch64CC - Convert a DAG fp condition code to an AArch64
1102 /// CC usable with the vector instructions. Fewer operations are available
1103 /// without a real NZCV register, so we have to use less efficient combinations
1104 /// to get the same effect.
1105 static void changeVectorFPCCToAArch64CC(ISD::CondCode CC,
1106 AArch64CC::CondCode &CondCode,
1107 AArch64CC::CondCode &CondCode2,
1112 // Mostly the scalar mappings work fine.
1113 changeFPCCToAArch64CC(CC, CondCode, CondCode2);
1116 Invert = true; // Fallthrough
1118 CondCode = AArch64CC::MI;
1119 CondCode2 = AArch64CC::GE;
1126 // All of the compare-mask comparisons are ordered, but we can switch
1127 // between the two by a double inversion. E.g. ULE == !OGT.
1129 changeFPCCToAArch64CC(getSetCCInverse(CC, false), CondCode, CondCode2);
1134 static bool isLegalArithImmed(uint64_t C) {
1135 // Matches AArch64DAGToDAGISel::SelectArithImmed().
1136 return (C >> 12 == 0) || ((C & 0xFFFULL) == 0 && C >> 24 == 0);
1139 static SDValue emitComparison(SDValue LHS, SDValue RHS, ISD::CondCode CC,
1140 SDLoc dl, SelectionDAG &DAG) {
1141 EVT VT = LHS.getValueType();
1143 if (VT.isFloatingPoint())
1144 return DAG.getNode(AArch64ISD::FCMP, dl, VT, LHS, RHS);
1146 // The CMP instruction is just an alias for SUBS, and representing it as
1147 // SUBS means that it's possible to get CSE with subtract operations.
1148 // A later phase can perform the optimization of setting the destination
1149 // register to WZR/XZR if it ends up being unused.
1150 unsigned Opcode = AArch64ISD::SUBS;
1152 if (RHS.getOpcode() == ISD::SUB && isa<ConstantSDNode>(RHS.getOperand(0)) &&
1153 cast<ConstantSDNode>(RHS.getOperand(0))->getZExtValue() == 0 &&
1154 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
1155 // We'd like to combine a (CMP op1, (sub 0, op2) into a CMN instruction on
1156 // the grounds that "op1 - (-op2) == op1 + op2". However, the C and V flags
1157 // can be set differently by this operation. It comes down to whether
1158 // "SInt(~op2)+1 == SInt(~op2+1)" (and the same for UInt). If they are then
1159 // everything is fine. If not then the optimization is wrong. Thus general
1160 // comparisons are only valid if op2 != 0.
1162 // So, finally, the only LLVM-native comparisons that don't mention C and V
1163 // are SETEQ and SETNE. They're the only ones we can safely use CMN for in
1164 // the absence of information about op2.
1165 Opcode = AArch64ISD::ADDS;
1166 RHS = RHS.getOperand(1);
1167 } else if (LHS.getOpcode() == ISD::AND && isa<ConstantSDNode>(RHS) &&
1168 cast<ConstantSDNode>(RHS)->getZExtValue() == 0 &&
1169 !isUnsignedIntSetCC(CC)) {
1170 // Similarly, (CMP (and X, Y), 0) can be implemented with a TST
1171 // (a.k.a. ANDS) except that the flags are only guaranteed to work for one
1172 // of the signed comparisons.
1173 Opcode = AArch64ISD::ANDS;
1174 RHS = LHS.getOperand(1);
1175 LHS = LHS.getOperand(0);
1178 return DAG.getNode(Opcode, dl, DAG.getVTList(VT, MVT_CC), LHS, RHS)
1182 /// \defgroup AArch64CCMP CMP;CCMP matching
1184 /// These functions deal with the formation of CMP;CCMP;... sequences.
1185 /// The CCMP/CCMN/FCCMP/FCCMPE instructions allow the conditional execution of
1186 /// a comparison. They set the NZCV flags to a predefined value if their
1187 /// predicate is false. This allows to express arbitrary conjunctions, for
1188 /// example "cmp 0 (and (setCA (cmp A)) (setCB (cmp B))))"
1191 /// ccmp B, inv(CB), CA
1192 /// check for CB flags
1194 /// In general we can create code for arbitrary "... (and (and A B) C)"
1195 /// sequences. We can also implement some "or" expressions, because "(or A B)"
1196 /// is equivalent to "not (and (not A) (not B))" and we can implement some
1197 /// negation operations:
1198 /// We can negate the results of a single comparison by inverting the flags
1199 /// used when the predicate fails and inverting the flags tested in the next
1200 /// instruction; We can also negate the results of the whole previous
1201 /// conditional compare sequence by inverting the flags tested in the next
1202 /// instruction. However there is no way to negate the result of a partial
1205 /// Therefore on encountering an "or" expression we can negate the subtree on
1206 /// one side and have to be able to push the negate to the leafs of the subtree
1207 /// on the other side (see also the comments in code). As complete example:
1208 /// "or (or (setCA (cmp A)) (setCB (cmp B)))
1209 /// (and (setCC (cmp C)) (setCD (cmp D)))"
1210 /// is transformed to
1211 /// "not (and (not (and (setCC (cmp C)) (setCC (cmp D))))
1212 /// (and (not (setCA (cmp A)) (not (setCB (cmp B))))))"
1213 /// and implemented as:
1215 /// ccmp D, inv(CD), CC
1216 /// ccmp A, CA, inv(CD)
1217 /// ccmp B, CB, inv(CA)
1218 /// check for CB flags
1219 /// A counterexample is "or (and A B) (and C D)" which cannot be implemented
1220 /// by conditional compare sequences.
1223 /// Create a conditional comparison; Use CCMP, CCMN or FCCMP as appropriate.
1224 static SDValue emitConditionalComparison(SDValue LHS, SDValue RHS,
1225 ISD::CondCode CC, SDValue CCOp,
1226 SDValue Condition, unsigned NZCV,
1227 SDLoc DL, SelectionDAG &DAG) {
1228 unsigned Opcode = 0;
1229 if (LHS.getValueType().isFloatingPoint())
1230 Opcode = AArch64ISD::FCCMP;
1231 else if (RHS.getOpcode() == ISD::SUB) {
1232 SDValue SubOp0 = RHS.getOperand(0);
1233 if (const ConstantSDNode *SubOp0C = dyn_cast<ConstantSDNode>(SubOp0))
1234 if (SubOp0C->isNullValue() && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
1235 // See emitComparison() on why we can only do this for SETEQ and SETNE.
1236 Opcode = AArch64ISD::CCMN;
1237 RHS = RHS.getOperand(1);
1241 Opcode = AArch64ISD::CCMP;
1243 SDValue NZCVOp = DAG.getConstant(NZCV, DL, MVT::i32);
1244 return DAG.getNode(Opcode, DL, MVT_CC, LHS, RHS, NZCVOp, Condition, CCOp);
1247 /// Returns true if @p Val is a tree of AND/OR/SETCC operations.
1248 /// CanPushNegate is set to true if we can push a negate operation through
1249 /// the tree in a was that we are left with AND operations and negate operations
1250 /// at the leafs only. i.e. "not (or (or x y) z)" can be changed to
1251 /// "and (and (not x) (not y)) (not z)"; "not (or (and x y) z)" cannot be
1252 /// brought into such a form.
1253 static bool isConjunctionDisjunctionTree(const SDValue Val, bool &CanPushNegate,
1254 unsigned Depth = 0) {
1255 if (!Val.hasOneUse())
1257 unsigned Opcode = Val->getOpcode();
1258 if (Opcode == ISD::SETCC) {
1259 CanPushNegate = true;
1262 // Protect against stack overflow.
1265 if (Opcode == ISD::AND || Opcode == ISD::OR) {
1266 SDValue O0 = Val->getOperand(0);
1267 SDValue O1 = Val->getOperand(1);
1268 bool CanPushNegateL;
1269 if (!isConjunctionDisjunctionTree(O0, CanPushNegateL, Depth+1))
1271 bool CanPushNegateR;
1272 if (!isConjunctionDisjunctionTree(O1, CanPushNegateR, Depth+1))
1274 // We cannot push a negate through an AND operation (it would become an OR),
1275 // we can however change a (not (or x y)) to (and (not x) (not y)) if we can
1276 // push the negate through the x/y subtrees.
1277 CanPushNegate = (Opcode == ISD::OR) && CanPushNegateL && CanPushNegateR;
1283 /// Emit conjunction or disjunction tree with the CMP/FCMP followed by a chain
1284 /// of CCMP/CFCMP ops. See @ref AArch64CCMP.
1285 /// Tries to transform the given i1 producing node @p Val to a series compare
1286 /// and conditional compare operations. @returns an NZCV flags producing node
1287 /// and sets @p OutCC to the flags that should be tested or returns SDValue() if
1288 /// transformation was not possible.
1289 /// On recursive invocations @p PushNegate may be set to true to have negation
1290 /// effects pushed to the tree leafs; @p Predicate is an NZCV flag predicate
1291 /// for the comparisons in the current subtree; @p Depth limits the search
1292 /// depth to avoid stack overflow.
1293 static SDValue emitConjunctionDisjunctionTree(SelectionDAG &DAG, SDValue Val,
1294 AArch64CC::CondCode &OutCC, bool PushNegate = false,
1295 SDValue CCOp = SDValue(), AArch64CC::CondCode Predicate = AArch64CC::AL,
1296 unsigned Depth = 0) {
1297 // We're at a tree leaf, produce a conditional comparison operation.
1298 unsigned Opcode = Val->getOpcode();
1299 if (Opcode == ISD::SETCC) {
1300 SDValue LHS = Val->getOperand(0);
1301 SDValue RHS = Val->getOperand(1);
1302 ISD::CondCode CC = cast<CondCodeSDNode>(Val->getOperand(2))->get();
1303 bool isInteger = LHS.getValueType().isInteger();
1305 CC = getSetCCInverse(CC, isInteger);
1307 // Determine OutCC and handle FP special case.
1309 OutCC = changeIntCCToAArch64CC(CC);
1311 assert(LHS.getValueType().isFloatingPoint());
1312 AArch64CC::CondCode ExtraCC;
1313 changeFPCCToAArch64CC(CC, OutCC, ExtraCC);
1314 // Surpisingly some floating point conditions can't be tested with a
1315 // single condition code. Construct an additional comparison in this case.
1316 // See comment below on how we deal with OR conditions.
1317 if (ExtraCC != AArch64CC::AL) {
1319 if (!CCOp.getNode())
1320 ExtraCmp = emitComparison(LHS, RHS, CC, DL, DAG);
1322 SDValue ConditionOp = DAG.getConstant(Predicate, DL, MVT_CC);
1323 // Note that we want the inverse of ExtraCC, so NZCV is not inversed.
1324 unsigned NZCV = AArch64CC::getNZCVToSatisfyCondCode(ExtraCC);
1325 ExtraCmp = emitConditionalComparison(LHS, RHS, CC, CCOp, ConditionOp,
1329 Predicate = AArch64CC::getInvertedCondCode(ExtraCC);
1330 OutCC = AArch64CC::getInvertedCondCode(OutCC);
1334 // Produce a normal comparison if we are first in the chain
1335 if (!CCOp.getNode())
1336 return emitComparison(LHS, RHS, CC, DL, DAG);
1337 // Otherwise produce a ccmp.
1338 SDValue ConditionOp = DAG.getConstant(Predicate, DL, MVT_CC);
1339 AArch64CC::CondCode InvOutCC = AArch64CC::getInvertedCondCode(OutCC);
1340 unsigned NZCV = AArch64CC::getNZCVToSatisfyCondCode(InvOutCC);
1341 return emitConditionalComparison(LHS, RHS, CC, CCOp, ConditionOp, NZCV, DL,
1343 } else if (Opcode != ISD::AND && Opcode != ISD::OR)
1346 assert((Opcode == ISD::OR || !PushNegate)
1347 && "Can only push negate through OR operation");
1349 // Check if both sides can be transformed.
1350 SDValue LHS = Val->getOperand(0);
1351 SDValue RHS = Val->getOperand(1);
1352 bool CanPushNegateL;
1353 if (!isConjunctionDisjunctionTree(LHS, CanPushNegateL, Depth+1))
1355 bool CanPushNegateR;
1356 if (!isConjunctionDisjunctionTree(RHS, CanPushNegateR, Depth+1))
1359 // Do we need to negate our operands?
1360 bool NegateOperands = Opcode == ISD::OR;
1361 // We can negate the results of all previous operations by inverting the
1362 // predicate flags giving us a free negation for one side. For the other side
1363 // we need to be able to push the negation to the leafs of the tree.
1364 if (NegateOperands) {
1365 if (!CanPushNegateL && !CanPushNegateR)
1367 // Order the side where we can push the negate through to LHS.
1368 if (!CanPushNegateL && CanPushNegateR) {
1369 std::swap(LHS, RHS);
1370 CanPushNegateL = true;
1374 // Emit RHS. If we want to negate the tree we only need to push a negate
1375 // through if we are already in a PushNegate case, otherwise we can negate
1376 // the "flags to test" afterwards.
1377 AArch64CC::CondCode RHSCC;
1378 SDValue CmpR = emitConjunctionDisjunctionTree(DAG, RHS, RHSCC, PushNegate,
1379 CCOp, Predicate, Depth+1);
1380 if (NegateOperands && !PushNegate)
1381 RHSCC = AArch64CC::getInvertedCondCode(RHSCC);
1382 // Emit LHS. We must push the negate through if we need to negate it.
1383 SDValue CmpL = emitConjunctionDisjunctionTree(DAG, LHS, OutCC, NegateOperands,
1384 CmpR, RHSCC, Depth+1);
1385 // If we transformed an OR to and AND then we have to negate the result
1386 // (or absorb a PushNegate resulting in a double negation).
1387 if (Opcode == ISD::OR && !PushNegate)
1388 OutCC = AArch64CC::getInvertedCondCode(OutCC);
1394 static SDValue getAArch64Cmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
1395 SDValue &AArch64cc, SelectionDAG &DAG, SDLoc dl) {
1396 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
1397 EVT VT = RHS.getValueType();
1398 uint64_t C = RHSC->getZExtValue();
1399 if (!isLegalArithImmed(C)) {
1400 // Constant does not fit, try adjusting it by one?
1406 if ((VT == MVT::i32 && C != 0x80000000 &&
1407 isLegalArithImmed((uint32_t)(C - 1))) ||
1408 (VT == MVT::i64 && C != 0x80000000ULL &&
1409 isLegalArithImmed(C - 1ULL))) {
1410 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
1411 C = (VT == MVT::i32) ? (uint32_t)(C - 1) : C - 1;
1412 RHS = DAG.getConstant(C, dl, VT);
1417 if ((VT == MVT::i32 && C != 0 &&
1418 isLegalArithImmed((uint32_t)(C - 1))) ||
1419 (VT == MVT::i64 && C != 0ULL && isLegalArithImmed(C - 1ULL))) {
1420 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
1421 C = (VT == MVT::i32) ? (uint32_t)(C - 1) : C - 1;
1422 RHS = DAG.getConstant(C, dl, VT);
1427 if ((VT == MVT::i32 && C != INT32_MAX &&
1428 isLegalArithImmed((uint32_t)(C + 1))) ||
1429 (VT == MVT::i64 && C != INT64_MAX &&
1430 isLegalArithImmed(C + 1ULL))) {
1431 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
1432 C = (VT == MVT::i32) ? (uint32_t)(C + 1) : C + 1;
1433 RHS = DAG.getConstant(C, dl, VT);
1438 if ((VT == MVT::i32 && C != UINT32_MAX &&
1439 isLegalArithImmed((uint32_t)(C + 1))) ||
1440 (VT == MVT::i64 && C != UINT64_MAX &&
1441 isLegalArithImmed(C + 1ULL))) {
1442 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
1443 C = (VT == MVT::i32) ? (uint32_t)(C + 1) : C + 1;
1444 RHS = DAG.getConstant(C, dl, VT);
1451 AArch64CC::CondCode AArch64CC;
1452 if ((CC == ISD::SETEQ || CC == ISD::SETNE) && isa<ConstantSDNode>(RHS)) {
1453 const ConstantSDNode *RHSC = cast<ConstantSDNode>(RHS);
1455 // The imm operand of ADDS is an unsigned immediate, in the range 0 to 4095.
1456 // For the i8 operand, the largest immediate is 255, so this can be easily
1457 // encoded in the compare instruction. For the i16 operand, however, the
1458 // largest immediate cannot be encoded in the compare.
1459 // Therefore, use a sign extending load and cmn to avoid materializing the
1460 // -1 constant. For example,
1462 // ldrh w0, [x0, #0]
1465 // ldrsh w0, [x0, #0]
1467 // Fundamental, we're relying on the property that (zext LHS) == (zext RHS)
1468 // if and only if (sext LHS) == (sext RHS). The checks are in place to
1469 // ensure both the LHS and RHS are truely zero extended and to make sure the
1470 // transformation is profitable.
1471 if ((RHSC->getZExtValue() >> 16 == 0) && isa<LoadSDNode>(LHS) &&
1472 cast<LoadSDNode>(LHS)->getExtensionType() == ISD::ZEXTLOAD &&
1473 cast<LoadSDNode>(LHS)->getMemoryVT() == MVT::i16 &&
1474 LHS.getNode()->hasNUsesOfValue(1, 0)) {
1475 int16_t ValueofRHS = cast<ConstantSDNode>(RHS)->getZExtValue();
1476 if (ValueofRHS < 0 && isLegalArithImmed(-ValueofRHS)) {
1478 DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, LHS.getValueType(), LHS,
1479 DAG.getValueType(MVT::i16));
1480 Cmp = emitComparison(SExt, DAG.getConstant(ValueofRHS, dl,
1481 RHS.getValueType()),
1483 AArch64CC = changeIntCCToAArch64CC(CC);
1487 if (!Cmp && (RHSC->isNullValue() || RHSC->isOne())) {
1488 if ((Cmp = emitConjunctionDisjunctionTree(DAG, LHS, AArch64CC))) {
1489 if ((CC == ISD::SETNE) ^ RHSC->isNullValue())
1490 AArch64CC = AArch64CC::getInvertedCondCode(AArch64CC);
1496 Cmp = emitComparison(LHS, RHS, CC, dl, DAG);
1497 AArch64CC = changeIntCCToAArch64CC(CC);
1499 AArch64cc = DAG.getConstant(AArch64CC, dl, MVT_CC);
1503 static std::pair<SDValue, SDValue>
1504 getAArch64XALUOOp(AArch64CC::CondCode &CC, SDValue Op, SelectionDAG &DAG) {
1505 assert((Op.getValueType() == MVT::i32 || Op.getValueType() == MVT::i64) &&
1506 "Unsupported value type");
1507 SDValue Value, Overflow;
1509 SDValue LHS = Op.getOperand(0);
1510 SDValue RHS = Op.getOperand(1);
1512 switch (Op.getOpcode()) {
1514 llvm_unreachable("Unknown overflow instruction!");
1516 Opc = AArch64ISD::ADDS;
1520 Opc = AArch64ISD::ADDS;
1524 Opc = AArch64ISD::SUBS;
1528 Opc = AArch64ISD::SUBS;
1531 // Multiply needs a little bit extra work.
1535 bool IsSigned = Op.getOpcode() == ISD::SMULO;
1536 if (Op.getValueType() == MVT::i32) {
1537 unsigned ExtendOpc = IsSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
1538 // For a 32 bit multiply with overflow check we want the instruction
1539 // selector to generate a widening multiply (SMADDL/UMADDL). For that we
1540 // need to generate the following pattern:
1541 // (i64 add 0, (i64 mul (i64 sext|zext i32 %a), (i64 sext|zext i32 %b))
1542 LHS = DAG.getNode(ExtendOpc, DL, MVT::i64, LHS);
1543 RHS = DAG.getNode(ExtendOpc, DL, MVT::i64, RHS);
1544 SDValue Mul = DAG.getNode(ISD::MUL, DL, MVT::i64, LHS, RHS);
1545 SDValue Add = DAG.getNode(ISD::ADD, DL, MVT::i64, Mul,
1546 DAG.getConstant(0, DL, MVT::i64));
1547 // On AArch64 the upper 32 bits are always zero extended for a 32 bit
1548 // operation. We need to clear out the upper 32 bits, because we used a
1549 // widening multiply that wrote all 64 bits. In the end this should be a
1551 Value = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Add);
1553 // The signed overflow check requires more than just a simple check for
1554 // any bit set in the upper 32 bits of the result. These bits could be
1555 // just the sign bits of a negative number. To perform the overflow
1556 // check we have to arithmetic shift right the 32nd bit of the result by
1557 // 31 bits. Then we compare the result to the upper 32 bits.
1558 SDValue UpperBits = DAG.getNode(ISD::SRL, DL, MVT::i64, Add,
1559 DAG.getConstant(32, DL, MVT::i64));
1560 UpperBits = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, UpperBits);
1561 SDValue LowerBits = DAG.getNode(ISD::SRA, DL, MVT::i32, Value,
1562 DAG.getConstant(31, DL, MVT::i64));
1563 // It is important that LowerBits is last, otherwise the arithmetic
1564 // shift will not be folded into the compare (SUBS).
1565 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::i32);
1566 Overflow = DAG.getNode(AArch64ISD::SUBS, DL, VTs, UpperBits, LowerBits)
1569 // The overflow check for unsigned multiply is easy. We only need to
1570 // check if any of the upper 32 bits are set. This can be done with a
1571 // CMP (shifted register). For that we need to generate the following
1573 // (i64 AArch64ISD::SUBS i64 0, (i64 srl i64 %Mul, i64 32)
1574 SDValue UpperBits = DAG.getNode(ISD::SRL, DL, MVT::i64, Mul,
1575 DAG.getConstant(32, DL, MVT::i64));
1576 SDVTList VTs = DAG.getVTList(MVT::i64, MVT::i32);
1578 DAG.getNode(AArch64ISD::SUBS, DL, VTs,
1579 DAG.getConstant(0, DL, MVT::i64),
1580 UpperBits).getValue(1);
1584 assert(Op.getValueType() == MVT::i64 && "Expected an i64 value type");
1585 // For the 64 bit multiply
1586 Value = DAG.getNode(ISD::MUL, DL, MVT::i64, LHS, RHS);
1588 SDValue UpperBits = DAG.getNode(ISD::MULHS, DL, MVT::i64, LHS, RHS);
1589 SDValue LowerBits = DAG.getNode(ISD::SRA, DL, MVT::i64, Value,
1590 DAG.getConstant(63, DL, MVT::i64));
1591 // It is important that LowerBits is last, otherwise the arithmetic
1592 // shift will not be folded into the compare (SUBS).
1593 SDVTList VTs = DAG.getVTList(MVT::i64, MVT::i32);
1594 Overflow = DAG.getNode(AArch64ISD::SUBS, DL, VTs, UpperBits, LowerBits)
1597 SDValue UpperBits = DAG.getNode(ISD::MULHU, DL, MVT::i64, LHS, RHS);
1598 SDVTList VTs = DAG.getVTList(MVT::i64, MVT::i32);
1600 DAG.getNode(AArch64ISD::SUBS, DL, VTs,
1601 DAG.getConstant(0, DL, MVT::i64),
1602 UpperBits).getValue(1);
1609 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::i32);
1611 // Emit the AArch64 operation with overflow check.
1612 Value = DAG.getNode(Opc, DL, VTs, LHS, RHS);
1613 Overflow = Value.getValue(1);
1615 return std::make_pair(Value, Overflow);
1618 SDValue AArch64TargetLowering::LowerF128Call(SDValue Op, SelectionDAG &DAG,
1619 RTLIB::Libcall Call) const {
1620 SmallVector<SDValue, 2> Ops(Op->op_begin(), Op->op_end());
1621 return makeLibCall(DAG, Call, MVT::f128, &Ops[0], Ops.size(), false,
1625 static SDValue LowerXOR(SDValue Op, SelectionDAG &DAG) {
1626 SDValue Sel = Op.getOperand(0);
1627 SDValue Other = Op.getOperand(1);
1629 // If neither operand is a SELECT_CC, give up.
1630 if (Sel.getOpcode() != ISD::SELECT_CC)
1631 std::swap(Sel, Other);
1632 if (Sel.getOpcode() != ISD::SELECT_CC)
1635 // The folding we want to perform is:
1636 // (xor x, (select_cc a, b, cc, 0, -1) )
1638 // (csel x, (xor x, -1), cc ...)
1640 // The latter will get matched to a CSINV instruction.
1642 ISD::CondCode CC = cast<CondCodeSDNode>(Sel.getOperand(4))->get();
1643 SDValue LHS = Sel.getOperand(0);
1644 SDValue RHS = Sel.getOperand(1);
1645 SDValue TVal = Sel.getOperand(2);
1646 SDValue FVal = Sel.getOperand(3);
1649 // FIXME: This could be generalized to non-integer comparisons.
1650 if (LHS.getValueType() != MVT::i32 && LHS.getValueType() != MVT::i64)
1653 ConstantSDNode *CFVal = dyn_cast<ConstantSDNode>(FVal);
1654 ConstantSDNode *CTVal = dyn_cast<ConstantSDNode>(TVal);
1656 // The values aren't constants, this isn't the pattern we're looking for.
1657 if (!CFVal || !CTVal)
1660 // We can commute the SELECT_CC by inverting the condition. This
1661 // might be needed to make this fit into a CSINV pattern.
1662 if (CTVal->isAllOnesValue() && CFVal->isNullValue()) {
1663 std::swap(TVal, FVal);
1664 std::swap(CTVal, CFVal);
1665 CC = ISD::getSetCCInverse(CC, true);
1668 // If the constants line up, perform the transform!
1669 if (CTVal->isNullValue() && CFVal->isAllOnesValue()) {
1671 SDValue Cmp = getAArch64Cmp(LHS, RHS, CC, CCVal, DAG, dl);
1674 TVal = DAG.getNode(ISD::XOR, dl, Other.getValueType(), Other,
1675 DAG.getConstant(-1ULL, dl, Other.getValueType()));
1677 return DAG.getNode(AArch64ISD::CSEL, dl, Sel.getValueType(), FVal, TVal,
1684 static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
1685 EVT VT = Op.getValueType();
1687 // Let legalize expand this if it isn't a legal type yet.
1688 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
1691 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
1694 bool ExtraOp = false;
1695 switch (Op.getOpcode()) {
1697 llvm_unreachable("Invalid code");
1699 Opc = AArch64ISD::ADDS;
1702 Opc = AArch64ISD::SUBS;
1705 Opc = AArch64ISD::ADCS;
1709 Opc = AArch64ISD::SBCS;
1715 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0), Op.getOperand(1));
1716 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0), Op.getOperand(1),
1720 static SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG) {
1721 // Let legalize expand this if it isn't a legal type yet.
1722 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType()))
1726 AArch64CC::CondCode CC;
1727 // The actual operation that sets the overflow or carry flag.
1728 SDValue Value, Overflow;
1729 std::tie(Value, Overflow) = getAArch64XALUOOp(CC, Op, DAG);
1731 // We use 0 and 1 as false and true values.
1732 SDValue TVal = DAG.getConstant(1, dl, MVT::i32);
1733 SDValue FVal = DAG.getConstant(0, dl, MVT::i32);
1735 // We use an inverted condition, because the conditional select is inverted
1736 // too. This will allow it to be selected to a single instruction:
1737 // CSINC Wd, WZR, WZR, invert(cond).
1738 SDValue CCVal = DAG.getConstant(getInvertedCondCode(CC), dl, MVT::i32);
1739 Overflow = DAG.getNode(AArch64ISD::CSEL, dl, MVT::i32, FVal, TVal,
1742 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
1743 return DAG.getNode(ISD::MERGE_VALUES, dl, VTs, Value, Overflow);
1746 // Prefetch operands are:
1747 // 1: Address to prefetch
1749 // 3: int locality (0 = no locality ... 3 = extreme locality)
1750 // 4: bool isDataCache
1751 static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG) {
1753 unsigned IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
1754 unsigned Locality = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
1755 unsigned IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
1757 bool IsStream = !Locality;
1758 // When the locality number is set
1760 // The front-end should have filtered out the out-of-range values
1761 assert(Locality <= 3 && "Prefetch locality out-of-range");
1762 // The locality degree is the opposite of the cache speed.
1763 // Put the number the other way around.
1764 // The encoding starts at 0 for level 1
1765 Locality = 3 - Locality;
1768 // built the mask value encoding the expected behavior.
1769 unsigned PrfOp = (IsWrite << 4) | // Load/Store bit
1770 (!IsData << 3) | // IsDataCache bit
1771 (Locality << 1) | // Cache level bits
1772 (unsigned)IsStream; // Stream bit
1773 return DAG.getNode(AArch64ISD::PREFETCH, DL, MVT::Other, Op.getOperand(0),
1774 DAG.getConstant(PrfOp, DL, MVT::i32), Op.getOperand(1));
1777 SDValue AArch64TargetLowering::LowerFP_EXTEND(SDValue Op,
1778 SelectionDAG &DAG) const {
1779 assert(Op.getValueType() == MVT::f128 && "Unexpected lowering");
1782 LC = RTLIB::getFPEXT(Op.getOperand(0).getValueType(), Op.getValueType());
1784 return LowerF128Call(Op, DAG, LC);
1787 SDValue AArch64TargetLowering::LowerFP_ROUND(SDValue Op,
1788 SelectionDAG &DAG) const {
1789 if (Op.getOperand(0).getValueType() != MVT::f128) {
1790 // It's legal except when f128 is involved
1795 LC = RTLIB::getFPROUND(Op.getOperand(0).getValueType(), Op.getValueType());
1797 // FP_ROUND node has a second operand indicating whether it is known to be
1798 // precise. That doesn't take part in the LibCall so we can't directly use
1800 SDValue SrcVal = Op.getOperand(0);
1801 return makeLibCall(DAG, LC, Op.getValueType(), &SrcVal, 1,
1802 /*isSigned*/ false, SDLoc(Op)).first;
1805 static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
1806 // Warning: We maintain cost tables in AArch64TargetTransformInfo.cpp.
1807 // Any additional optimization in this function should be recorded
1808 // in the cost tables.
1809 EVT InVT = Op.getOperand(0).getValueType();
1810 EVT VT = Op.getValueType();
1812 if (VT.getSizeInBits() < InVT.getSizeInBits()) {
1815 DAG.getNode(Op.getOpcode(), dl, InVT.changeVectorElementTypeToInteger(),
1817 return DAG.getNode(ISD::TRUNCATE, dl, VT, Cv);
1820 if (VT.getSizeInBits() > InVT.getSizeInBits()) {
1823 MVT::getVectorVT(MVT::getFloatingPointVT(VT.getScalarSizeInBits()),
1824 VT.getVectorNumElements());
1825 SDValue Ext = DAG.getNode(ISD::FP_EXTEND, dl, ExtVT, Op.getOperand(0));
1826 return DAG.getNode(Op.getOpcode(), dl, VT, Ext);
1829 // Type changing conversions are illegal.
1833 SDValue AArch64TargetLowering::LowerFP_TO_INT(SDValue Op,
1834 SelectionDAG &DAG) const {
1835 if (Op.getOperand(0).getValueType().isVector())
1836 return LowerVectorFP_TO_INT(Op, DAG);
1838 // f16 conversions are promoted to f32.
1839 if (Op.getOperand(0).getValueType() == MVT::f16) {
1842 Op.getOpcode(), dl, Op.getValueType(),
1843 DAG.getNode(ISD::FP_EXTEND, dl, MVT::f32, Op.getOperand(0)));
1846 if (Op.getOperand(0).getValueType() != MVT::f128) {
1847 // It's legal except when f128 is involved
1852 if (Op.getOpcode() == ISD::FP_TO_SINT)
1853 LC = RTLIB::getFPTOSINT(Op.getOperand(0).getValueType(), Op.getValueType());
1855 LC = RTLIB::getFPTOUINT(Op.getOperand(0).getValueType(), Op.getValueType());
1857 SmallVector<SDValue, 2> Ops(Op->op_begin(), Op->op_end());
1858 return makeLibCall(DAG, LC, Op.getValueType(), &Ops[0], Ops.size(), false,
1862 static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
1863 // Warning: We maintain cost tables in AArch64TargetTransformInfo.cpp.
1864 // Any additional optimization in this function should be recorded
1865 // in the cost tables.
1866 EVT VT = Op.getValueType();
1868 SDValue In = Op.getOperand(0);
1869 EVT InVT = In.getValueType();
1871 if (VT.getSizeInBits() < InVT.getSizeInBits()) {
1873 MVT::getVectorVT(MVT::getFloatingPointVT(InVT.getScalarSizeInBits()),
1874 InVT.getVectorNumElements());
1875 In = DAG.getNode(Op.getOpcode(), dl, CastVT, In);
1876 return DAG.getNode(ISD::FP_ROUND, dl, VT, In, DAG.getIntPtrConstant(0, dl));
1879 if (VT.getSizeInBits() > InVT.getSizeInBits()) {
1881 Op.getOpcode() == ISD::SINT_TO_FP ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
1882 EVT CastVT = VT.changeVectorElementTypeToInteger();
1883 In = DAG.getNode(CastOpc, dl, CastVT, In);
1884 return DAG.getNode(Op.getOpcode(), dl, VT, In);
1890 SDValue AArch64TargetLowering::LowerINT_TO_FP(SDValue Op,
1891 SelectionDAG &DAG) const {
1892 if (Op.getValueType().isVector())
1893 return LowerVectorINT_TO_FP(Op, DAG);
1895 // f16 conversions are promoted to f32.
1896 if (Op.getValueType() == MVT::f16) {
1899 ISD::FP_ROUND, dl, MVT::f16,
1900 DAG.getNode(Op.getOpcode(), dl, MVT::f32, Op.getOperand(0)),
1901 DAG.getIntPtrConstant(0, dl));
1904 // i128 conversions are libcalls.
1905 if (Op.getOperand(0).getValueType() == MVT::i128)
1908 // Other conversions are legal, unless it's to the completely software-based
1910 if (Op.getValueType() != MVT::f128)
1914 if (Op.getOpcode() == ISD::SINT_TO_FP)
1915 LC = RTLIB::getSINTTOFP(Op.getOperand(0).getValueType(), Op.getValueType());
1917 LC = RTLIB::getUINTTOFP(Op.getOperand(0).getValueType(), Op.getValueType());
1919 return LowerF128Call(Op, DAG, LC);
1922 SDValue AArch64TargetLowering::LowerFSINCOS(SDValue Op,
1923 SelectionDAG &DAG) const {
1924 // For iOS, we want to call an alternative entry point: __sincos_stret,
1925 // which returns the values in two S / D registers.
1927 SDValue Arg = Op.getOperand(0);
1928 EVT ArgVT = Arg.getValueType();
1929 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
1936 Entry.isSExt = false;
1937 Entry.isZExt = false;
1938 Args.push_back(Entry);
1940 const char *LibcallName =
1941 (ArgVT == MVT::f64) ? "__sincos_stret" : "__sincosf_stret";
1943 DAG.getExternalSymbol(LibcallName, getPointerTy(DAG.getDataLayout()));
1945 StructType *RetTy = StructType::get(ArgTy, ArgTy, nullptr);
1946 TargetLowering::CallLoweringInfo CLI(DAG);
1947 CLI.setDebugLoc(dl).setChain(DAG.getEntryNode())
1948 .setCallee(CallingConv::Fast, RetTy, Callee, std::move(Args), 0);
1950 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
1951 return CallResult.first;
1954 static SDValue LowerBITCAST(SDValue Op, SelectionDAG &DAG) {
1955 if (Op.getValueType() != MVT::f16)
1958 assert(Op.getOperand(0).getValueType() == MVT::i16);
1961 Op = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, Op.getOperand(0));
1962 Op = DAG.getNode(ISD::BITCAST, DL, MVT::f32, Op);
1964 DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL, MVT::f16, Op,
1965 DAG.getTargetConstant(AArch64::hsub, DL, MVT::i32)),
1969 static EVT getExtensionTo64Bits(const EVT &OrigVT) {
1970 if (OrigVT.getSizeInBits() >= 64)
1973 assert(OrigVT.isSimple() && "Expecting a simple value type");
1975 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
1976 switch (OrigSimpleTy) {
1977 default: llvm_unreachable("Unexpected Vector Type");
1986 static SDValue addRequiredExtensionForVectorMULL(SDValue N, SelectionDAG &DAG,
1989 unsigned ExtOpcode) {
1990 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
1991 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
1992 // 64-bits we need to insert a new extension so that it will be 64-bits.
1993 assert(ExtTy.is128BitVector() && "Unexpected extension size");
1994 if (OrigTy.getSizeInBits() >= 64)
1997 // Must extend size to at least 64 bits to be used as an operand for VMULL.
1998 EVT NewVT = getExtensionTo64Bits(OrigTy);
2000 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
2003 static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
2005 EVT VT = N->getValueType(0);
2007 if (N->getOpcode() != ISD::BUILD_VECTOR)
2010 for (const SDValue &Elt : N->op_values()) {
2011 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
2012 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
2013 unsigned HalfSize = EltSize / 2;
2015 if (!isIntN(HalfSize, C->getSExtValue()))
2018 if (!isUIntN(HalfSize, C->getZExtValue()))
2029 static SDValue skipExtensionForVectorMULL(SDNode *N, SelectionDAG &DAG) {
2030 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
2031 return addRequiredExtensionForVectorMULL(N->getOperand(0), DAG,
2032 N->getOperand(0)->getValueType(0),
2036 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
2037 EVT VT = N->getValueType(0);
2039 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
2040 unsigned NumElts = VT.getVectorNumElements();
2041 MVT TruncVT = MVT::getIntegerVT(EltSize);
2042 SmallVector<SDValue, 8> Ops;
2043 for (unsigned i = 0; i != NumElts; ++i) {
2044 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
2045 const APInt &CInt = C->getAPIntValue();
2046 // Element types smaller than 32 bits are not legal, so use i32 elements.
2047 // The values are implicitly truncated so sext vs. zext doesn't matter.
2048 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), dl, MVT::i32));
2050 return DAG.getNode(ISD::BUILD_VECTOR, dl,
2051 MVT::getVectorVT(TruncVT, NumElts), Ops);
2054 static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
2055 if (N->getOpcode() == ISD::SIGN_EXTEND)
2057 if (isExtendedBUILD_VECTOR(N, DAG, true))
2062 static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
2063 if (N->getOpcode() == ISD::ZERO_EXTEND)
2065 if (isExtendedBUILD_VECTOR(N, DAG, false))
2070 static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
2071 unsigned Opcode = N->getOpcode();
2072 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
2073 SDNode *N0 = N->getOperand(0).getNode();
2074 SDNode *N1 = N->getOperand(1).getNode();
2075 return N0->hasOneUse() && N1->hasOneUse() &&
2076 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
2081 static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
2082 unsigned Opcode = N->getOpcode();
2083 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
2084 SDNode *N0 = N->getOperand(0).getNode();
2085 SDNode *N1 = N->getOperand(1).getNode();
2086 return N0->hasOneUse() && N1->hasOneUse() &&
2087 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
2092 static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
2093 // Multiplications are only custom-lowered for 128-bit vectors so that
2094 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
2095 EVT VT = Op.getValueType();
2096 assert(VT.is128BitVector() && VT.isInteger() &&
2097 "unexpected type for custom-lowering ISD::MUL");
2098 SDNode *N0 = Op.getOperand(0).getNode();
2099 SDNode *N1 = Op.getOperand(1).getNode();
2100 unsigned NewOpc = 0;
2102 bool isN0SExt = isSignExtended(N0, DAG);
2103 bool isN1SExt = isSignExtended(N1, DAG);
2104 if (isN0SExt && isN1SExt)
2105 NewOpc = AArch64ISD::SMULL;
2107 bool isN0ZExt = isZeroExtended(N0, DAG);
2108 bool isN1ZExt = isZeroExtended(N1, DAG);
2109 if (isN0ZExt && isN1ZExt)
2110 NewOpc = AArch64ISD::UMULL;
2111 else if (isN1SExt || isN1ZExt) {
2112 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
2113 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
2114 if (isN1SExt && isAddSubSExt(N0, DAG)) {
2115 NewOpc = AArch64ISD::SMULL;
2117 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
2118 NewOpc = AArch64ISD::UMULL;
2120 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
2122 NewOpc = AArch64ISD::UMULL;
2128 if (VT == MVT::v2i64)
2129 // Fall through to expand this. It is not legal.
2132 // Other vector multiplications are legal.
2137 // Legalize to a S/UMULL instruction
2140 SDValue Op1 = skipExtensionForVectorMULL(N1, DAG);
2142 Op0 = skipExtensionForVectorMULL(N0, DAG);
2143 assert(Op0.getValueType().is64BitVector() &&
2144 Op1.getValueType().is64BitVector() &&
2145 "unexpected types for extended operands to VMULL");
2146 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
2148 // Optimizing (zext A + zext B) * C, to (S/UMULL A, C) + (S/UMULL B, C) during
2149 // isel lowering to take advantage of no-stall back to back s/umul + s/umla.
2150 // This is true for CPUs with accumulate forwarding such as Cortex-A53/A57
2151 SDValue N00 = skipExtensionForVectorMULL(N0->getOperand(0).getNode(), DAG);
2152 SDValue N01 = skipExtensionForVectorMULL(N0->getOperand(1).getNode(), DAG);
2153 EVT Op1VT = Op1.getValueType();
2154 return DAG.getNode(N0->getOpcode(), DL, VT,
2155 DAG.getNode(NewOpc, DL, VT,
2156 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
2157 DAG.getNode(NewOpc, DL, VT,
2158 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
2161 SDValue AArch64TargetLowering::LowerOperation(SDValue Op,
2162 SelectionDAG &DAG) const {
2163 switch (Op.getOpcode()) {
2165 llvm_unreachable("unimplemented operand");
2168 return LowerBITCAST(Op, DAG);
2169 case ISD::GlobalAddress:
2170 return LowerGlobalAddress(Op, DAG);
2171 case ISD::GlobalTLSAddress:
2172 return LowerGlobalTLSAddress(Op, DAG);
2174 return LowerSETCC(Op, DAG);
2176 return LowerBR_CC(Op, DAG);
2178 return LowerSELECT(Op, DAG);
2179 case ISD::SELECT_CC:
2180 return LowerSELECT_CC(Op, DAG);
2181 case ISD::JumpTable:
2182 return LowerJumpTable(Op, DAG);
2183 case ISD::ConstantPool:
2184 return LowerConstantPool(Op, DAG);
2185 case ISD::BlockAddress:
2186 return LowerBlockAddress(Op, DAG);
2188 return LowerVASTART(Op, DAG);
2190 return LowerVACOPY(Op, DAG);
2192 return LowerVAARG(Op, DAG);
2197 return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
2204 return LowerXALUO(Op, DAG);
2206 return LowerF128Call(Op, DAG, RTLIB::ADD_F128);
2208 return LowerF128Call(Op, DAG, RTLIB::SUB_F128);
2210 return LowerF128Call(Op, DAG, RTLIB::MUL_F128);
2212 return LowerF128Call(Op, DAG, RTLIB::DIV_F128);
2214 return LowerFP_ROUND(Op, DAG);
2215 case ISD::FP_EXTEND:
2216 return LowerFP_EXTEND(Op, DAG);
2217 case ISD::FRAMEADDR:
2218 return LowerFRAMEADDR(Op, DAG);
2219 case ISD::RETURNADDR:
2220 return LowerRETURNADDR(Op, DAG);
2221 case ISD::INSERT_VECTOR_ELT:
2222 return LowerINSERT_VECTOR_ELT(Op, DAG);
2223 case ISD::EXTRACT_VECTOR_ELT:
2224 return LowerEXTRACT_VECTOR_ELT(Op, DAG);
2225 case ISD::BUILD_VECTOR:
2226 return LowerBUILD_VECTOR(Op, DAG);
2227 case ISD::VECTOR_SHUFFLE:
2228 return LowerVECTOR_SHUFFLE(Op, DAG);
2229 case ISD::EXTRACT_SUBVECTOR:
2230 return LowerEXTRACT_SUBVECTOR(Op, DAG);
2234 return LowerVectorSRA_SRL_SHL(Op, DAG);
2235 case ISD::SHL_PARTS:
2236 return LowerShiftLeftParts(Op, DAG);
2237 case ISD::SRL_PARTS:
2238 case ISD::SRA_PARTS:
2239 return LowerShiftRightParts(Op, DAG);
2241 return LowerCTPOP(Op, DAG);
2242 case ISD::FCOPYSIGN:
2243 return LowerFCOPYSIGN(Op, DAG);
2245 return LowerVectorAND(Op, DAG);
2247 return LowerVectorOR(Op, DAG);
2249 return LowerXOR(Op, DAG);
2251 return LowerPREFETCH(Op, DAG);
2252 case ISD::SINT_TO_FP:
2253 case ISD::UINT_TO_FP:
2254 return LowerINT_TO_FP(Op, DAG);
2255 case ISD::FP_TO_SINT:
2256 case ISD::FP_TO_UINT:
2257 return LowerFP_TO_INT(Op, DAG);
2259 return LowerFSINCOS(Op, DAG);
2261 return LowerMUL(Op, DAG);
2265 /// getFunctionAlignment - Return the Log2 alignment of this function.
2266 unsigned AArch64TargetLowering::getFunctionAlignment(const Function *F) const {
2270 //===----------------------------------------------------------------------===//
2271 // Calling Convention Implementation
2272 //===----------------------------------------------------------------------===//
2274 #include "AArch64GenCallingConv.inc"
2276 /// Selects the correct CCAssignFn for a given CallingConvention value.
2277 CCAssignFn *AArch64TargetLowering::CCAssignFnForCall(CallingConv::ID CC,
2278 bool IsVarArg) const {
2281 llvm_unreachable("Unsupported calling convention.");
2282 case CallingConv::WebKit_JS:
2283 return CC_AArch64_WebKit_JS;
2284 case CallingConv::GHC:
2285 return CC_AArch64_GHC;
2286 case CallingConv::C:
2287 case CallingConv::Fast:
2288 if (!Subtarget->isTargetDarwin())
2289 return CC_AArch64_AAPCS;
2290 return IsVarArg ? CC_AArch64_DarwinPCS_VarArg : CC_AArch64_DarwinPCS;
2294 SDValue AArch64TargetLowering::LowerFormalArguments(
2295 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
2296 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
2297 SmallVectorImpl<SDValue> &InVals) const {
2298 MachineFunction &MF = DAG.getMachineFunction();
2299 MachineFrameInfo *MFI = MF.getFrameInfo();
2301 // Assign locations to all of the incoming arguments.
2302 SmallVector<CCValAssign, 16> ArgLocs;
2303 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
2306 // At this point, Ins[].VT may already be promoted to i32. To correctly
2307 // handle passing i8 as i8 instead of i32 on stack, we pass in both i32 and
2308 // i8 to CC_AArch64_AAPCS with i32 being ValVT and i8 being LocVT.
2309 // Since AnalyzeFormalArguments uses Ins[].VT for both ValVT and LocVT, here
2310 // we use a special version of AnalyzeFormalArguments to pass in ValVT and
2312 unsigned NumArgs = Ins.size();
2313 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
2314 unsigned CurArgIdx = 0;
2315 for (unsigned i = 0; i != NumArgs; ++i) {
2316 MVT ValVT = Ins[i].VT;
2317 if (Ins[i].isOrigArg()) {
2318 std::advance(CurOrigArg, Ins[i].getOrigArgIndex() - CurArgIdx);
2319 CurArgIdx = Ins[i].getOrigArgIndex();
2321 // Get type of the original argument.
2322 EVT ActualVT = getValueType(DAG.getDataLayout(), CurOrigArg->getType(),
2323 /*AllowUnknown*/ true);
2324 MVT ActualMVT = ActualVT.isSimple() ? ActualVT.getSimpleVT() : MVT::Other;
2325 // If ActualMVT is i1/i8/i16, we should set LocVT to i8/i8/i16.
2326 if (ActualMVT == MVT::i1 || ActualMVT == MVT::i8)
2328 else if (ActualMVT == MVT::i16)
2331 CCAssignFn *AssignFn = CCAssignFnForCall(CallConv, /*IsVarArg=*/false);
2333 AssignFn(i, ValVT, ValVT, CCValAssign::Full, Ins[i].Flags, CCInfo);
2334 assert(!Res && "Call operand has unhandled type");
2337 assert(ArgLocs.size() == Ins.size());
2338 SmallVector<SDValue, 16> ArgValues;
2339 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2340 CCValAssign &VA = ArgLocs[i];
2342 if (Ins[i].Flags.isByVal()) {
2343 // Byval is used for HFAs in the PCS, but the system should work in a
2344 // non-compliant manner for larger structs.
2345 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2346 int Size = Ins[i].Flags.getByValSize();
2347 unsigned NumRegs = (Size + 7) / 8;
2349 // FIXME: This works on big-endian for composite byvals, which are the common
2350 // case. It should also work for fundamental types too.
2352 MFI->CreateFixedObject(8 * NumRegs, VA.getLocMemOffset(), false);
2353 SDValue FrameIdxN = DAG.getFrameIndex(FrameIdx, PtrVT);
2354 InVals.push_back(FrameIdxN);
2359 if (VA.isRegLoc()) {
2360 // Arguments stored in registers.
2361 EVT RegVT = VA.getLocVT();
2364 const TargetRegisterClass *RC;
2366 if (RegVT == MVT::i32)
2367 RC = &AArch64::GPR32RegClass;
2368 else if (RegVT == MVT::i64)
2369 RC = &AArch64::GPR64RegClass;
2370 else if (RegVT == MVT::f16)
2371 RC = &AArch64::FPR16RegClass;
2372 else if (RegVT == MVT::f32)
2373 RC = &AArch64::FPR32RegClass;
2374 else if (RegVT == MVT::f64 || RegVT.is64BitVector())
2375 RC = &AArch64::FPR64RegClass;
2376 else if (RegVT == MVT::f128 || RegVT.is128BitVector())
2377 RC = &AArch64::FPR128RegClass;
2379 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
2381 // Transform the arguments in physical registers into virtual ones.
2382 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
2383 ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
2385 // If this is an 8, 16 or 32-bit value, it is really passed promoted
2386 // to 64 bits. Insert an assert[sz]ext to capture this, then
2387 // truncate to the right size.
2388 switch (VA.getLocInfo()) {
2390 llvm_unreachable("Unknown loc info!");
2391 case CCValAssign::Full:
2393 case CCValAssign::BCvt:
2394 ArgValue = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), ArgValue);
2396 case CCValAssign::AExt:
2397 case CCValAssign::SExt:
2398 case CCValAssign::ZExt:
2399 // SelectionDAGBuilder will insert appropriate AssertZExt & AssertSExt
2400 // nodes after our lowering.
2401 assert(RegVT == Ins[i].VT && "incorrect register location selected");
2405 InVals.push_back(ArgValue);
2407 } else { // VA.isRegLoc()
2408 assert(VA.isMemLoc() && "CCValAssign is neither reg nor mem");
2409 unsigned ArgOffset = VA.getLocMemOffset();
2410 unsigned ArgSize = VA.getValVT().getSizeInBits() / 8;
2412 uint32_t BEAlign = 0;
2413 if (!Subtarget->isLittleEndian() && ArgSize < 8 &&
2414 !Ins[i].Flags.isInConsecutiveRegs())
2415 BEAlign = 8 - ArgSize;
2417 int FI = MFI->CreateFixedObject(ArgSize, ArgOffset + BEAlign, true);
2419 // Create load nodes to retrieve arguments from the stack.
2420 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
2423 // For NON_EXTLOAD, generic code in getLoad assert(ValVT == MemVT)
2424 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
2425 MVT MemVT = VA.getValVT();
2427 switch (VA.getLocInfo()) {
2430 case CCValAssign::BCvt:
2431 MemVT = VA.getLocVT();
2433 case CCValAssign::SExt:
2434 ExtType = ISD::SEXTLOAD;
2436 case CCValAssign::ZExt:
2437 ExtType = ISD::ZEXTLOAD;
2439 case CCValAssign::AExt:
2440 ExtType = ISD::EXTLOAD;
2444 ArgValue = DAG.getExtLoad(ExtType, DL, VA.getLocVT(), Chain, FIN,
2445 MachinePointerInfo::getFixedStack(FI),
2446 MemVT, false, false, false, 0);
2448 InVals.push_back(ArgValue);
2454 if (!Subtarget->isTargetDarwin()) {
2455 // The AAPCS variadic function ABI is identical to the non-variadic
2456 // one. As a result there may be more arguments in registers and we should
2457 // save them for future reference.
2458 saveVarArgRegisters(CCInfo, DAG, DL, Chain);
2461 AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
2462 // This will point to the next argument passed via stack.
2463 unsigned StackOffset = CCInfo.getNextStackOffset();
2464 // We currently pass all varargs at 8-byte alignment.
2465 StackOffset = ((StackOffset + 7) & ~7);
2466 AFI->setVarArgsStackIndex(MFI->CreateFixedObject(4, StackOffset, true));
2469 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>();
2470 unsigned StackArgSize = CCInfo.getNextStackOffset();
2471 bool TailCallOpt = MF.getTarget().Options.GuaranteedTailCallOpt;
2472 if (DoesCalleeRestoreStack(CallConv, TailCallOpt)) {
2473 // This is a non-standard ABI so by fiat I say we're allowed to make full
2474 // use of the stack area to be popped, which must be aligned to 16 bytes in
2476 StackArgSize = RoundUpToAlignment(StackArgSize, 16);
2478 // If we're expected to restore the stack (e.g. fastcc) then we'll be adding
2479 // a multiple of 16.
2480 FuncInfo->setArgumentStackToRestore(StackArgSize);
2482 // This realignment carries over to the available bytes below. Our own
2483 // callers will guarantee the space is free by giving an aligned value to
2486 // Even if we're not expected to free up the space, it's useful to know how
2487 // much is there while considering tail calls (because we can reuse it).
2488 FuncInfo->setBytesInStackArgArea(StackArgSize);
2493 void AArch64TargetLowering::saveVarArgRegisters(CCState &CCInfo,
2494 SelectionDAG &DAG, SDLoc DL,
2495 SDValue &Chain) const {
2496 MachineFunction &MF = DAG.getMachineFunction();
2497 MachineFrameInfo *MFI = MF.getFrameInfo();
2498 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>();
2499 auto PtrVT = getPointerTy(DAG.getDataLayout());
2501 SmallVector<SDValue, 8> MemOps;
2503 static const MCPhysReg GPRArgRegs[] = { AArch64::X0, AArch64::X1, AArch64::X2,
2504 AArch64::X3, AArch64::X4, AArch64::X5,
2505 AArch64::X6, AArch64::X7 };
2506 static const unsigned NumGPRArgRegs = array_lengthof(GPRArgRegs);
2507 unsigned FirstVariadicGPR = CCInfo.getFirstUnallocated(GPRArgRegs);
2509 unsigned GPRSaveSize = 8 * (NumGPRArgRegs - FirstVariadicGPR);
2511 if (GPRSaveSize != 0) {
2512 GPRIdx = MFI->CreateStackObject(GPRSaveSize, 8, false);
2514 SDValue FIN = DAG.getFrameIndex(GPRIdx, PtrVT);
2516 for (unsigned i = FirstVariadicGPR; i < NumGPRArgRegs; ++i) {
2517 unsigned VReg = MF.addLiveIn(GPRArgRegs[i], &AArch64::GPR64RegClass);
2518 SDValue Val = DAG.getCopyFromReg(Chain, DL, VReg, MVT::i64);
2520 DAG.getStore(Val.getValue(1), DL, Val, FIN,
2521 MachinePointerInfo::getStack(i * 8), false, false, 0);
2522 MemOps.push_back(Store);
2524 DAG.getNode(ISD::ADD, DL, PtrVT, FIN, DAG.getConstant(8, DL, PtrVT));
2527 FuncInfo->setVarArgsGPRIndex(GPRIdx);
2528 FuncInfo->setVarArgsGPRSize(GPRSaveSize);
2530 if (Subtarget->hasFPARMv8()) {
2531 static const MCPhysReg FPRArgRegs[] = {
2532 AArch64::Q0, AArch64::Q1, AArch64::Q2, AArch64::Q3,
2533 AArch64::Q4, AArch64::Q5, AArch64::Q6, AArch64::Q7};
2534 static const unsigned NumFPRArgRegs = array_lengthof(FPRArgRegs);
2535 unsigned FirstVariadicFPR = CCInfo.getFirstUnallocated(FPRArgRegs);
2537 unsigned FPRSaveSize = 16 * (NumFPRArgRegs - FirstVariadicFPR);
2539 if (FPRSaveSize != 0) {
2540 FPRIdx = MFI->CreateStackObject(FPRSaveSize, 16, false);
2542 SDValue FIN = DAG.getFrameIndex(FPRIdx, PtrVT);
2544 for (unsigned i = FirstVariadicFPR; i < NumFPRArgRegs; ++i) {
2545 unsigned VReg = MF.addLiveIn(FPRArgRegs[i], &AArch64::FPR128RegClass);
2546 SDValue Val = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f128);
2549 DAG.getStore(Val.getValue(1), DL, Val, FIN,
2550 MachinePointerInfo::getStack(i * 16), false, false, 0);
2551 MemOps.push_back(Store);
2552 FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN,
2553 DAG.getConstant(16, DL, PtrVT));
2556 FuncInfo->setVarArgsFPRIndex(FPRIdx);
2557 FuncInfo->setVarArgsFPRSize(FPRSaveSize);
2560 if (!MemOps.empty()) {
2561 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps);
2565 /// LowerCallResult - Lower the result values of a call into the
2566 /// appropriate copies out of appropriate physical registers.
2567 SDValue AArch64TargetLowering::LowerCallResult(
2568 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool isVarArg,
2569 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
2570 SmallVectorImpl<SDValue> &InVals, bool isThisReturn,
2571 SDValue ThisVal) const {
2572 CCAssignFn *RetCC = CallConv == CallingConv::WebKit_JS
2573 ? RetCC_AArch64_WebKit_JS
2574 : RetCC_AArch64_AAPCS;
2575 // Assign locations to each value returned by this call.
2576 SmallVector<CCValAssign, 16> RVLocs;
2577 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
2579 CCInfo.AnalyzeCallResult(Ins, RetCC);
2581 // Copy all of the result registers out of their specified physreg.
2582 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2583 CCValAssign VA = RVLocs[i];
2585 // Pass 'this' value directly from the argument to return value, to avoid
2586 // reg unit interference
2587 if (i == 0 && isThisReturn) {
2588 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i64 &&
2589 "unexpected return calling convention register assignment");
2590 InVals.push_back(ThisVal);
2595 DAG.getCopyFromReg(Chain, DL, VA.getLocReg(), VA.getLocVT(), InFlag);
2596 Chain = Val.getValue(1);
2597 InFlag = Val.getValue(2);
2599 switch (VA.getLocInfo()) {
2601 llvm_unreachable("Unknown loc info!");
2602 case CCValAssign::Full:
2604 case CCValAssign::BCvt:
2605 Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
2609 InVals.push_back(Val);
2615 bool AArch64TargetLowering::isEligibleForTailCallOptimization(
2616 SDValue Callee, CallingConv::ID CalleeCC, bool isVarArg,
2617 bool isCalleeStructRet, bool isCallerStructRet,
2618 const SmallVectorImpl<ISD::OutputArg> &Outs,
2619 const SmallVectorImpl<SDValue> &OutVals,
2620 const SmallVectorImpl<ISD::InputArg> &Ins, SelectionDAG &DAG) const {
2621 // For CallingConv::C this function knows whether the ABI needs
2622 // changing. That's not true for other conventions so they will have to opt in
2624 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
2627 const MachineFunction &MF = DAG.getMachineFunction();
2628 const Function *CallerF = MF.getFunction();
2629 CallingConv::ID CallerCC = CallerF->getCallingConv();
2630 bool CCMatch = CallerCC == CalleeCC;
2632 // Byval parameters hand the function a pointer directly into the stack area
2633 // we want to reuse during a tail call. Working around this *is* possible (see
2634 // X86) but less efficient and uglier in LowerCall.
2635 for (Function::const_arg_iterator i = CallerF->arg_begin(),
2636 e = CallerF->arg_end();
2638 if (i->hasByValAttr())
2641 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
2642 if (IsTailCallConvention(CalleeCC) && CCMatch)
2647 // Externally-defined functions with weak linkage should not be
2648 // tail-called on AArch64 when the OS does not support dynamic
2649 // pre-emption of symbols, as the AAELF spec requires normal calls
2650 // to undefined weak functions to be replaced with a NOP or jump to the
2651 // next instruction. The behaviour of branch instructions in this
2652 // situation (as used for tail calls) is implementation-defined, so we
2653 // cannot rely on the linker replacing the tail call with a return.
2654 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2655 const GlobalValue *GV = G->getGlobal();
2656 const Triple &TT = getTargetMachine().getTargetTriple();
2657 if (GV->hasExternalWeakLinkage() &&
2658 (!TT.isOSWindows() || TT.isOSBinFormatELF() || TT.isOSBinFormatMachO()))
2662 // Now we search for cases where we can use a tail call without changing the
2663 // ABI. Sibcall is used in some places (particularly gcc) to refer to this
2666 // I want anyone implementing a new calling convention to think long and hard
2667 // about this assert.
2668 assert((!isVarArg || CalleeCC == CallingConv::C) &&
2669 "Unexpected variadic calling convention");
2671 if (isVarArg && !Outs.empty()) {
2672 // At least two cases here: if caller is fastcc then we can't have any
2673 // memory arguments (we'd be expected to clean up the stack afterwards). If
2674 // caller is C then we could potentially use its argument area.
2676 // FIXME: for now we take the most conservative of these in both cases:
2677 // disallow all variadic memory operands.
2678 SmallVector<CCValAssign, 16> ArgLocs;
2679 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), ArgLocs,
2682 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForCall(CalleeCC, true));
2683 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2684 if (!ArgLocs[i].isRegLoc())
2688 // If the calling conventions do not match, then we'd better make sure the
2689 // results are returned in the same way as what the caller expects.
2691 SmallVector<CCValAssign, 16> RVLocs1;
2692 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(), RVLocs1,
2694 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForCall(CalleeCC, isVarArg));
2696 SmallVector<CCValAssign, 16> RVLocs2;
2697 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(), RVLocs2,
2699 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForCall(CallerCC, isVarArg));
2701 if (RVLocs1.size() != RVLocs2.size())
2703 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2704 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2706 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2708 if (RVLocs1[i].isRegLoc()) {
2709 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2712 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2718 // Nothing more to check if the callee is taking no arguments
2722 SmallVector<CCValAssign, 16> ArgLocs;
2723 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), ArgLocs,
2726 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForCall(CalleeCC, isVarArg));
2728 const AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>();
2730 // If the stack arguments for this call would fit into our own save area then
2731 // the call can be made tail.
2732 return CCInfo.getNextStackOffset() <= FuncInfo->getBytesInStackArgArea();
2735 SDValue AArch64TargetLowering::addTokenForArgument(SDValue Chain,
2737 MachineFrameInfo *MFI,
2738 int ClobberedFI) const {
2739 SmallVector<SDValue, 8> ArgChains;
2740 int64_t FirstByte = MFI->getObjectOffset(ClobberedFI);
2741 int64_t LastByte = FirstByte + MFI->getObjectSize(ClobberedFI) - 1;
2743 // Include the original chain at the beginning of the list. When this is
2744 // used by target LowerCall hooks, this helps legalize find the
2745 // CALLSEQ_BEGIN node.
2746 ArgChains.push_back(Chain);
2748 // Add a chain value for each stack argument corresponding
2749 for (SDNode::use_iterator U = DAG.getEntryNode().getNode()->use_begin(),
2750 UE = DAG.getEntryNode().getNode()->use_end();
2752 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
2753 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
2754 if (FI->getIndex() < 0) {
2755 int64_t InFirstByte = MFI->getObjectOffset(FI->getIndex());
2756 int64_t InLastByte = InFirstByte;
2757 InLastByte += MFI->getObjectSize(FI->getIndex()) - 1;
2759 if ((InFirstByte <= FirstByte && FirstByte <= InLastByte) ||
2760 (FirstByte <= InFirstByte && InFirstByte <= LastByte))
2761 ArgChains.push_back(SDValue(L, 1));
2764 // Build a tokenfactor for all the chains.
2765 return DAG.getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
2768 bool AArch64TargetLowering::DoesCalleeRestoreStack(CallingConv::ID CallCC,
2769 bool TailCallOpt) const {
2770 return CallCC == CallingConv::Fast && TailCallOpt;
2773 bool AArch64TargetLowering::IsTailCallConvention(CallingConv::ID CallCC) const {
2774 return CallCC == CallingConv::Fast;
2777 /// LowerCall - Lower a call to a callseq_start + CALL + callseq_end chain,
2778 /// and add input and output parameter nodes.
2780 AArch64TargetLowering::LowerCall(CallLoweringInfo &CLI,
2781 SmallVectorImpl<SDValue> &InVals) const {
2782 SelectionDAG &DAG = CLI.DAG;
2784 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2785 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2786 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2787 SDValue Chain = CLI.Chain;
2788 SDValue Callee = CLI.Callee;
2789 bool &IsTailCall = CLI.IsTailCall;
2790 CallingConv::ID CallConv = CLI.CallConv;
2791 bool IsVarArg = CLI.IsVarArg;
2793 MachineFunction &MF = DAG.getMachineFunction();
2794 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
2795 bool IsThisReturn = false;
2797 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>();
2798 bool TailCallOpt = MF.getTarget().Options.GuaranteedTailCallOpt;
2799 bool IsSibCall = false;
2802 // Check if it's really possible to do a tail call.
2803 IsTailCall = isEligibleForTailCallOptimization(
2804 Callee, CallConv, IsVarArg, IsStructRet,
2805 MF.getFunction()->hasStructRetAttr(), Outs, OutVals, Ins, DAG);
2806 if (!IsTailCall && CLI.CS && CLI.CS->isMustTailCall())
2807 report_fatal_error("failed to perform tail call elimination on a call "
2808 "site marked musttail");
2810 // A sibling call is one where we're under the usual C ABI and not planning
2811 // to change that but can still do a tail call:
2812 if (!TailCallOpt && IsTailCall)
2819 // Analyze operands of the call, assigning locations to each operand.
2820 SmallVector<CCValAssign, 16> ArgLocs;
2821 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
2825 // Handle fixed and variable vector arguments differently.
2826 // Variable vector arguments always go into memory.
2827 unsigned NumArgs = Outs.size();
2829 for (unsigned i = 0; i != NumArgs; ++i) {
2830 MVT ArgVT = Outs[i].VT;
2831 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
2832 CCAssignFn *AssignFn = CCAssignFnForCall(CallConv,
2833 /*IsVarArg=*/ !Outs[i].IsFixed);
2834 bool Res = AssignFn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
2835 assert(!Res && "Call operand has unhandled type");
2839 // At this point, Outs[].VT may already be promoted to i32. To correctly
2840 // handle passing i8 as i8 instead of i32 on stack, we pass in both i32 and
2841 // i8 to CC_AArch64_AAPCS with i32 being ValVT and i8 being LocVT.
2842 // Since AnalyzeCallOperands uses Ins[].VT for both ValVT and LocVT, here
2843 // we use a special version of AnalyzeCallOperands to pass in ValVT and
2845 unsigned NumArgs = Outs.size();
2846 for (unsigned i = 0; i != NumArgs; ++i) {
2847 MVT ValVT = Outs[i].VT;
2848 // Get type of the original argument.
2849 EVT ActualVT = getValueType(DAG.getDataLayout(),
2850 CLI.getArgs()[Outs[i].OrigArgIndex].Ty,
2851 /*AllowUnknown*/ true);
2852 MVT ActualMVT = ActualVT.isSimple() ? ActualVT.getSimpleVT() : ValVT;
2853 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
2854 // If ActualMVT is i1/i8/i16, we should set LocVT to i8/i8/i16.
2855 if (ActualMVT == MVT::i1 || ActualMVT == MVT::i8)
2857 else if (ActualMVT == MVT::i16)
2860 CCAssignFn *AssignFn = CCAssignFnForCall(CallConv, /*IsVarArg=*/false);
2861 bool Res = AssignFn(i, ValVT, ValVT, CCValAssign::Full, ArgFlags, CCInfo);
2862 assert(!Res && "Call operand has unhandled type");
2867 // Get a count of how many bytes are to be pushed on the stack.
2868 unsigned NumBytes = CCInfo.getNextStackOffset();
2871 // Since we're not changing the ABI to make this a tail call, the memory
2872 // operands are already available in the caller's incoming argument space.
2876 // FPDiff is the byte offset of the call's argument area from the callee's.
2877 // Stores to callee stack arguments will be placed in FixedStackSlots offset
2878 // by this amount for a tail call. In a sibling call it must be 0 because the
2879 // caller will deallocate the entire stack and the callee still expects its
2880 // arguments to begin at SP+0. Completely unused for non-tail calls.
2883 if (IsTailCall && !IsSibCall) {
2884 unsigned NumReusableBytes = FuncInfo->getBytesInStackArgArea();
2886 // Since callee will pop argument stack as a tail call, we must keep the
2887 // popped size 16-byte aligned.
2888 NumBytes = RoundUpToAlignment(NumBytes, 16);
2890 // FPDiff will be negative if this tail call requires more space than we
2891 // would automatically have in our incoming argument space. Positive if we
2892 // can actually shrink the stack.
2893 FPDiff = NumReusableBytes - NumBytes;
2895 // The stack pointer must be 16-byte aligned at all times it's used for a
2896 // memory operation, which in practice means at *all* times and in
2897 // particular across call boundaries. Therefore our own arguments started at
2898 // a 16-byte aligned SP and the delta applied for the tail call should
2899 // satisfy the same constraint.
2900 assert(FPDiff % 16 == 0 && "unaligned stack on tail call");
2903 // Adjust the stack pointer for the new arguments...
2904 // These operations are automatically eliminated by the prolog/epilog pass
2906 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, DL,
2910 SDValue StackPtr = DAG.getCopyFromReg(Chain, DL, AArch64::SP,
2911 getPointerTy(DAG.getDataLayout()));
2913 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2914 SmallVector<SDValue, 8> MemOpChains;
2915 auto PtrVT = getPointerTy(DAG.getDataLayout());
2917 // Walk the register/memloc assignments, inserting copies/loads.
2918 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size(); i != e;
2919 ++i, ++realArgIdx) {
2920 CCValAssign &VA = ArgLocs[i];
2921 SDValue Arg = OutVals[realArgIdx];
2922 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
2924 // Promote the value if needed.
2925 switch (VA.getLocInfo()) {
2927 llvm_unreachable("Unknown loc info!");
2928 case CCValAssign::Full:
2930 case CCValAssign::SExt:
2931 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg);
2933 case CCValAssign::ZExt:
2934 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg);
2936 case CCValAssign::AExt:
2937 if (Outs[realArgIdx].ArgVT == MVT::i1) {
2938 // AAPCS requires i1 to be zero-extended to 8-bits by the caller.
2939 Arg = DAG.getNode(ISD::TRUNCATE, DL, MVT::i1, Arg);
2940 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i8, Arg);
2942 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg);
2944 case CCValAssign::BCvt:
2945 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
2947 case CCValAssign::FPExt:
2948 Arg = DAG.getNode(ISD::FP_EXTEND, DL, VA.getLocVT(), Arg);
2952 if (VA.isRegLoc()) {
2953 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i64) {
2954 assert(VA.getLocVT() == MVT::i64 &&
2955 "unexpected calling convention register assignment");
2956 assert(!Ins.empty() && Ins[0].VT == MVT::i64 &&
2957 "unexpected use of 'returned'");
2958 IsThisReturn = true;
2960 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2962 assert(VA.isMemLoc());
2965 MachinePointerInfo DstInfo;
2967 // FIXME: This works on big-endian for composite byvals, which are the
2968 // common case. It should also work for fundamental types too.
2969 uint32_t BEAlign = 0;
2970 unsigned OpSize = Flags.isByVal() ? Flags.getByValSize() * 8
2971 : VA.getValVT().getSizeInBits();
2972 OpSize = (OpSize + 7) / 8;
2973 if (!Subtarget->isLittleEndian() && !Flags.isByVal() &&
2974 !Flags.isInConsecutiveRegs()) {
2976 BEAlign = 8 - OpSize;
2978 unsigned LocMemOffset = VA.getLocMemOffset();
2979 int32_t Offset = LocMemOffset + BEAlign;
2980 SDValue PtrOff = DAG.getIntPtrConstant(Offset, DL);
2981 PtrOff = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, PtrOff);
2984 Offset = Offset + FPDiff;
2985 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
2987 DstAddr = DAG.getFrameIndex(FI, PtrVT);
2988 DstInfo = MachinePointerInfo::getFixedStack(FI);
2990 // Make sure any stack arguments overlapping with where we're storing
2991 // are loaded before this eventual operation. Otherwise they'll be
2993 Chain = addTokenForArgument(Chain, DAG, MF.getFrameInfo(), FI);
2995 SDValue PtrOff = DAG.getIntPtrConstant(Offset, DL);
2997 DstAddr = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, PtrOff);
2998 DstInfo = MachinePointerInfo::getStack(LocMemOffset);
3001 if (Outs[i].Flags.isByVal()) {
3003 DAG.getConstant(Outs[i].Flags.getByValSize(), DL, MVT::i64);
3004 SDValue Cpy = DAG.getMemcpy(
3005 Chain, DL, DstAddr, Arg, SizeNode, Outs[i].Flags.getByValAlign(),
3006 /*isVol = */ false, /*AlwaysInline = */ false,
3007 /*isTailCall = */ false,
3008 DstInfo, MachinePointerInfo());
3010 MemOpChains.push_back(Cpy);
3012 // Since we pass i1/i8/i16 as i1/i8/i16 on stack and Arg is already
3013 // promoted to a legal register type i32, we should truncate Arg back to
3015 if (VA.getValVT() == MVT::i1 || VA.getValVT() == MVT::i8 ||
3016 VA.getValVT() == MVT::i16)
3017 Arg = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Arg);
3020 DAG.getStore(Chain, DL, Arg, DstAddr, DstInfo, false, false, 0);
3021 MemOpChains.push_back(Store);
3026 if (!MemOpChains.empty())
3027 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
3029 // Build a sequence of copy-to-reg nodes chained together with token chain
3030 // and flag operands which copy the outgoing args into the appropriate regs.
3032 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
3033 Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[i].first,
3034 RegsToPass[i].second, InFlag);
3035 InFlag = Chain.getValue(1);
3038 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
3039 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
3040 // node so that legalize doesn't hack it.
3041 if (getTargetMachine().getCodeModel() == CodeModel::Large &&
3042 Subtarget->isTargetMachO()) {
3043 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3044 const GlobalValue *GV = G->getGlobal();
3045 bool InternalLinkage = GV->hasInternalLinkage();
3046 if (InternalLinkage)
3047 Callee = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, 0);
3050 DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, AArch64II::MO_GOT);
3051 Callee = DAG.getNode(AArch64ISD::LOADgot, DL, PtrVT, Callee);
3053 } else if (ExternalSymbolSDNode *S =
3054 dyn_cast<ExternalSymbolSDNode>(Callee)) {
3055 const char *Sym = S->getSymbol();
3056 Callee = DAG.getTargetExternalSymbol(Sym, PtrVT, AArch64II::MO_GOT);
3057 Callee = DAG.getNode(AArch64ISD::LOADgot, DL, PtrVT, Callee);
3059 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3060 const GlobalValue *GV = G->getGlobal();
3061 Callee = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, 0);
3062 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
3063 const char *Sym = S->getSymbol();
3064 Callee = DAG.getTargetExternalSymbol(Sym, PtrVT, 0);
3067 // We don't usually want to end the call-sequence here because we would tidy
3068 // the frame up *after* the call, however in the ABI-changing tail-call case
3069 // we've carefully laid out the parameters so that when sp is reset they'll be
3070 // in the correct location.
3071 if (IsTailCall && !IsSibCall) {
3072 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, DL, true),
3073 DAG.getIntPtrConstant(0, DL, true), InFlag, DL);
3074 InFlag = Chain.getValue(1);
3077 std::vector<SDValue> Ops;
3078 Ops.push_back(Chain);
3079 Ops.push_back(Callee);
3082 // Each tail call may have to adjust the stack by a different amount, so
3083 // this information must travel along with the operation for eventual
3084 // consumption by emitEpilogue.
3085 Ops.push_back(DAG.getTargetConstant(FPDiff, DL, MVT::i32));
3088 // Add argument registers to the end of the list so that they are known live
3090 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
3091 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
3092 RegsToPass[i].second.getValueType()));
3094 // Add a register mask operand representing the call-preserved registers.
3095 const uint32_t *Mask;
3096 const AArch64RegisterInfo *TRI = Subtarget->getRegisterInfo();
3098 // For 'this' returns, use the X0-preserving mask if applicable
3099 Mask = TRI->getThisReturnPreservedMask(MF, CallConv);
3101 IsThisReturn = false;
3102 Mask = TRI->getCallPreservedMask(MF, CallConv);
3105 Mask = TRI->getCallPreservedMask(MF, CallConv);
3107 assert(Mask && "Missing call preserved mask for calling convention");
3108 Ops.push_back(DAG.getRegisterMask(Mask));
3110 if (InFlag.getNode())
3111 Ops.push_back(InFlag);
3113 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
3115 // If we're doing a tall call, use a TC_RETURN here rather than an
3116 // actual call instruction.
3118 MF.getFrameInfo()->setHasTailCall();
3119 return DAG.getNode(AArch64ISD::TC_RETURN, DL, NodeTys, Ops);
3122 // Returns a chain and a flag for retval copy to use.
3123 Chain = DAG.getNode(AArch64ISD::CALL, DL, NodeTys, Ops);
3124 InFlag = Chain.getValue(1);
3126 uint64_t CalleePopBytes = DoesCalleeRestoreStack(CallConv, TailCallOpt)
3127 ? RoundUpToAlignment(NumBytes, 16)
3130 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, DL, true),
3131 DAG.getIntPtrConstant(CalleePopBytes, DL, true),
3134 InFlag = Chain.getValue(1);
3136 // Handle result values, copying them out of physregs into vregs that we
3138 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg, Ins, DL, DAG,
3139 InVals, IsThisReturn,
3140 IsThisReturn ? OutVals[0] : SDValue());
3143 bool AArch64TargetLowering::CanLowerReturn(
3144 CallingConv::ID CallConv, MachineFunction &MF, bool isVarArg,
3145 const SmallVectorImpl<ISD::OutputArg> &Outs, LLVMContext &Context) const {
3146 CCAssignFn *RetCC = CallConv == CallingConv::WebKit_JS
3147 ? RetCC_AArch64_WebKit_JS
3148 : RetCC_AArch64_AAPCS;
3149 SmallVector<CCValAssign, 16> RVLocs;
3150 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
3151 return CCInfo.CheckReturn(Outs, RetCC);
3155 AArch64TargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
3157 const SmallVectorImpl<ISD::OutputArg> &Outs,
3158 const SmallVectorImpl<SDValue> &OutVals,
3159 SDLoc DL, SelectionDAG &DAG) const {
3160 CCAssignFn *RetCC = CallConv == CallingConv::WebKit_JS
3161 ? RetCC_AArch64_WebKit_JS
3162 : RetCC_AArch64_AAPCS;
3163 SmallVector<CCValAssign, 16> RVLocs;
3164 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
3166 CCInfo.AnalyzeReturn(Outs, RetCC);
3168 // Copy the result values into the output registers.
3170 SmallVector<SDValue, 4> RetOps(1, Chain);
3171 for (unsigned i = 0, realRVLocIdx = 0; i != RVLocs.size();
3172 ++i, ++realRVLocIdx) {
3173 CCValAssign &VA = RVLocs[i];
3174 assert(VA.isRegLoc() && "Can only return in registers!");
3175 SDValue Arg = OutVals[realRVLocIdx];
3177 switch (VA.getLocInfo()) {
3179 llvm_unreachable("Unknown loc info!");
3180 case CCValAssign::Full:
3181 if (Outs[i].ArgVT == MVT::i1) {
3182 // AAPCS requires i1 to be zero-extended to i8 by the producer of the
3183 // value. This is strictly redundant on Darwin (which uses "zeroext
3184 // i1"), but will be optimised out before ISel.
3185 Arg = DAG.getNode(ISD::TRUNCATE, DL, MVT::i1, Arg);
3186 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg);
3189 case CCValAssign::BCvt:
3190 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
3194 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag);
3195 Flag = Chain.getValue(1);
3196 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
3199 RetOps[0] = Chain; // Update chain.
3201 // Add the flag if we have it.
3203 RetOps.push_back(Flag);
3205 return DAG.getNode(AArch64ISD::RET_FLAG, DL, MVT::Other, RetOps);
3208 //===----------------------------------------------------------------------===//
3209 // Other Lowering Code
3210 //===----------------------------------------------------------------------===//
3212 SDValue AArch64TargetLowering::LowerGlobalAddress(SDValue Op,
3213 SelectionDAG &DAG) const {
3214 EVT PtrVT = getPointerTy(DAG.getDataLayout());
3216 const GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Op);
3217 const GlobalValue *GV = GN->getGlobal();
3218 unsigned char OpFlags =
3219 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
3221 assert(cast<GlobalAddressSDNode>(Op)->getOffset() == 0 &&
3222 "unexpected offset in global node");
3224 // This also catched the large code model case for Darwin.
3225 if ((OpFlags & AArch64II::MO_GOT) != 0) {
3226 SDValue GotAddr = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, OpFlags);
3227 // FIXME: Once remat is capable of dealing with instructions with register
3228 // operands, expand this into two nodes instead of using a wrapper node.
3229 return DAG.getNode(AArch64ISD::LOADgot, DL, PtrVT, GotAddr);
3232 if ((OpFlags & AArch64II::MO_CONSTPOOL) != 0) {
3233 assert(getTargetMachine().getCodeModel() == CodeModel::Small &&
3234 "use of MO_CONSTPOOL only supported on small model");
3235 SDValue Hi = DAG.getTargetConstantPool(GV, PtrVT, 0, 0, AArch64II::MO_PAGE);
3236 SDValue ADRP = DAG.getNode(AArch64ISD::ADRP, DL, PtrVT, Hi);
3237 unsigned char LoFlags = AArch64II::MO_PAGEOFF | AArch64II::MO_NC;
3238 SDValue Lo = DAG.getTargetConstantPool(GV, PtrVT, 0, 0, LoFlags);
3239 SDValue PoolAddr = DAG.getNode(AArch64ISD::ADDlow, DL, PtrVT, ADRP, Lo);
3240 SDValue GlobalAddr = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), PoolAddr,
3241 MachinePointerInfo::getConstantPool(),
3242 /*isVolatile=*/ false,
3243 /*isNonTemporal=*/ true,
3244 /*isInvariant=*/ true, 8);
3245 if (GN->getOffset() != 0)
3246 return DAG.getNode(ISD::ADD, DL, PtrVT, GlobalAddr,
3247 DAG.getConstant(GN->getOffset(), DL, PtrVT));
3251 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
3252 const unsigned char MO_NC = AArch64II::MO_NC;
3254 AArch64ISD::WrapperLarge, DL, PtrVT,
3255 DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, AArch64II::MO_G3),
3256 DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, AArch64II::MO_G2 | MO_NC),
3257 DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, AArch64II::MO_G1 | MO_NC),
3258 DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, AArch64II::MO_G0 | MO_NC));
3260 // Use ADRP/ADD or ADRP/LDR for everything else: the small model on ELF and
3261 // the only correct model on Darwin.
3262 SDValue Hi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
3263 OpFlags | AArch64II::MO_PAGE);
3264 unsigned char LoFlags = OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC;
3265 SDValue Lo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, LoFlags);
3267 SDValue ADRP = DAG.getNode(AArch64ISD::ADRP, DL, PtrVT, Hi);
3268 return DAG.getNode(AArch64ISD::ADDlow, DL, PtrVT, ADRP, Lo);
3272 /// \brief Convert a TLS address reference into the correct sequence of loads
3273 /// and calls to compute the variable's address (for Darwin, currently) and
3274 /// return an SDValue containing the final node.
3276 /// Darwin only has one TLS scheme which must be capable of dealing with the
3277 /// fully general situation, in the worst case. This means:
3278 /// + "extern __thread" declaration.
3279 /// + Defined in a possibly unknown dynamic library.
3281 /// The general system is that each __thread variable has a [3 x i64] descriptor
3282 /// which contains information used by the runtime to calculate the address. The
3283 /// only part of this the compiler needs to know about is the first xword, which
3284 /// contains a function pointer that must be called with the address of the
3285 /// entire descriptor in "x0".
3287 /// Since this descriptor may be in a different unit, in general even the
3288 /// descriptor must be accessed via an indirect load. The "ideal" code sequence
3290 /// adrp x0, _var@TLVPPAGE
3291 /// ldr x0, [x0, _var@TLVPPAGEOFF] ; x0 now contains address of descriptor
3292 /// ldr x1, [x0] ; x1 contains 1st entry of descriptor,
3293 /// ; the function pointer
3294 /// blr x1 ; Uses descriptor address in x0
3295 /// ; Address of _var is now in x0.
3297 /// If the address of _var's descriptor *is* known to the linker, then it can
3298 /// change the first "ldr" instruction to an appropriate "add x0, x0, #imm" for
3299 /// a slight efficiency gain.
3301 AArch64TargetLowering::LowerDarwinGlobalTLSAddress(SDValue Op,
3302 SelectionDAG &DAG) const {
3303 assert(Subtarget->isTargetDarwin() && "TLS only supported on Darwin");