LLVM 23.0.0git
AArch64LegalizerInfo.cpp
Go to the documentation of this file.
1//===- AArch64LegalizerInfo.cpp ----------------------------------*- C++ -*-==//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8/// \file
9/// This file implements the targeting of the Machinelegalizer class for
10/// AArch64.
11/// \todo This should be generated by TableGen.
12//===----------------------------------------------------------------------===//
13
15#include "AArch64Subtarget.h"
16#include "llvm/ADT/STLExtras.h"
28#include "llvm/IR/Intrinsics.h"
29#include "llvm/IR/IntrinsicsAArch64.h"
30#include "llvm/IR/Type.h"
32#include <initializer_list>
33
34#define DEBUG_TYPE "aarch64-legalinfo"
35
36using namespace llvm;
37using namespace LegalizeActions;
38using namespace LegalizeMutations;
39using namespace LegalityPredicates;
40using namespace MIPatternMatch;
41
43 : ST(&ST) {
44 using namespace TargetOpcode;
45 const LLT p0 = LLT::pointer(0, 64);
46 const LLT s8 = LLT::scalar(8);
47 const LLT s16 = LLT::scalar(16);
48 const LLT s32 = LLT::scalar(32);
49 const LLT s64 = LLT::scalar(64);
50 const LLT s128 = LLT::scalar(128);
51 const LLT v16s8 = LLT::fixed_vector(16, 8);
52 const LLT v8s8 = LLT::fixed_vector(8, 8);
53 const LLT v4s8 = LLT::fixed_vector(4, 8);
54 const LLT v2s8 = LLT::fixed_vector(2, 8);
55 const LLT v8s16 = LLT::fixed_vector(8, 16);
56 const LLT v4s16 = LLT::fixed_vector(4, 16);
57 const LLT v2s16 = LLT::fixed_vector(2, 16);
58 const LLT v2s32 = LLT::fixed_vector(2, 32);
59 const LLT v4s32 = LLT::fixed_vector(4, 32);
60 const LLT v2s64 = LLT::fixed_vector(2, 64);
61 const LLT v2p0 = LLT::fixed_vector(2, p0);
62
63 const LLT nxv16s8 = LLT::scalable_vector(16, s8);
64 const LLT nxv8s16 = LLT::scalable_vector(8, s16);
65 const LLT nxv4s32 = LLT::scalable_vector(4, s32);
66 const LLT nxv2s64 = LLT::scalable_vector(2, s64);
67
68 std::initializer_list<LLT> PackedVectorAllTypeList = {/* Begin 128bit types */
69 v16s8, v8s16, v4s32,
70 v2s64, v2p0,
71 /* End 128bit types */
72 /* Begin 64bit types */
73 v8s8, v4s16, v2s32};
74 std::initializer_list<LLT> ScalarAndPtrTypesList = {s8, s16, s32, s64, p0};
75 SmallVector<LLT, 8> PackedVectorAllTypesVec(PackedVectorAllTypeList);
76 SmallVector<LLT, 8> ScalarAndPtrTypesVec(ScalarAndPtrTypesList);
77
78 const TargetMachine &TM = ST.getTargetLowering()->getTargetMachine();
79
80 // FIXME: support subtargets which have neon/fp-armv8 disabled.
81 if (!ST.hasNEON() || !ST.hasFPARMv8()) {
83 return;
84 }
85
86 // Some instructions only support s16 if the subtarget has full 16-bit FP
87 // support.
88 const bool HasFP16 = ST.hasFullFP16();
89 const LLT &MinFPScalar = HasFP16 ? s16 : s32;
90
91 const bool HasCSSC = ST.hasCSSC();
92 const bool HasRCPC3 = ST.hasRCPC3();
93 const bool HasSVE = ST.hasSVE();
94
96 {G_IMPLICIT_DEF, G_FREEZE, G_CONSTANT_FOLD_BARRIER})
97 .legalFor({p0, s8, s16, s32, s64})
98 .legalFor({v2s8, v4s8, v8s8, v16s8, v2s16, v4s16, v8s16, v2s32, v4s32,
99 v2s64, v2p0})
100 .widenScalarToNextPow2(0)
101 .clampScalar(0, s8, s64)
104 .clampNumElements(0, v8s8, v16s8)
105 .clampNumElements(0, v4s16, v8s16)
106 .clampNumElements(0, v2s32, v4s32)
107 .clampMaxNumElements(0, s64, 2)
108 .clampMaxNumElements(0, p0, 2)
110
112 .legalFor({p0, s16, s32, s64})
113 .legalFor(PackedVectorAllTypeList)
117 .clampScalar(0, s16, s64)
118 .clampNumElements(0, v8s8, v16s8)
119 .clampNumElements(0, v4s16, v8s16)
120 .clampNumElements(0, v2s32, v4s32)
121 .clampMaxNumElements(0, s64, 2)
122 .clampMaxNumElements(0, p0, 2);
123
125 .legalIf(all(typeInSet(0, {s32, s64, p0}), typeInSet(1, {s8, s16, s32}),
126 smallerThan(1, 0)))
127 .widenScalarToNextPow2(0)
128 .clampScalar(0, s32, s64)
130 .minScalar(1, s8)
131 .maxScalarIf(typeInSet(0, {s32}), 1, s16)
132 .maxScalarIf(typeInSet(0, {s64, p0}), 1, s32);
133
135 .legalIf(all(typeInSet(0, {s16, s32, s64, p0}),
136 typeInSet(1, {s32, s64, s128, p0}), smallerThan(0, 1)))
137 .widenScalarToNextPow2(1)
138 .clampScalar(1, s32, s128)
140 .minScalar(0, s16)
141 .maxScalarIf(typeInSet(1, {s32}), 0, s16)
142 .maxScalarIf(typeInSet(1, {s64, p0}), 0, s32)
143 .maxScalarIf(typeInSet(1, {s128}), 0, s64);
144
145 getActionDefinitionsBuilder({G_ADD, G_SUB, G_AND, G_OR, G_XOR})
146 .legalFor({s32, s64, v8s8, v16s8, v4s16, v8s16, v2s32, v4s32, v2s64})
147 .legalFor(HasSVE, {nxv16s8, nxv8s16, nxv4s32, nxv2s64})
148 .widenScalarToNextPow2(0)
149 .clampScalar(0, s32, s64)
150 .clampMaxNumElements(0, s8, 16)
151 .clampMaxNumElements(0, s16, 8)
152 .clampNumElements(0, v2s32, v4s32)
153 .clampNumElements(0, v2s64, v2s64)
155 [=](const LegalityQuery &Query) {
156 return Query.Types[0].getNumElements() <= 2;
157 },
158 0, s32)
159 .minScalarOrEltIf(
160 [=](const LegalityQuery &Query) {
161 return Query.Types[0].getNumElements() <= 4;
162 },
163 0, s16)
164 .minScalarOrEltIf(
165 [=](const LegalityQuery &Query) {
166 return Query.Types[0].getNumElements() <= 16;
167 },
168 0, s8)
169 .scalarizeIf(scalarOrEltWiderThan(0, 64), 0)
171
173 .legalFor({s32, s64, v8s8, v16s8, v4s16, v8s16, v2s32, v4s32, v2s64})
174 .widenScalarToNextPow2(0)
175 .clampScalar(0, s32, s64)
176 .clampMaxNumElements(0, s8, 16)
177 .clampMaxNumElements(0, s16, 8)
178 .clampNumElements(0, v2s32, v4s32)
179 .clampNumElements(0, v2s64, v2s64)
181 [=](const LegalityQuery &Query) {
182 return Query.Types[0].getNumElements() <= 2;
183 },
184 0, s32)
185 .minScalarOrEltIf(
186 [=](const LegalityQuery &Query) {
187 return Query.Types[0].getNumElements() <= 4;
188 },
189 0, s16)
190 .minScalarOrEltIf(
191 [=](const LegalityQuery &Query) {
192 return Query.Types[0].getNumElements() <= 16;
193 },
194 0, s8)
195 .scalarizeIf(scalarOrEltWiderThan(0, 64), 0)
197
198 getActionDefinitionsBuilder({G_SHL, G_ASHR, G_LSHR})
199 .customIf([=](const LegalityQuery &Query) {
200 const auto &SrcTy = Query.Types[0];
201 const auto &AmtTy = Query.Types[1];
202 return !SrcTy.isVector() && SrcTy.getSizeInBits() == 32 &&
203 AmtTy.getSizeInBits() == 32;
204 })
205 .legalFor({
206 {s32, s32},
207 {s32, s64},
208 {s64, s64},
209 {v8s8, v8s8},
210 {v16s8, v16s8},
211 {v4s16, v4s16},
212 {v8s16, v8s16},
213 {v2s32, v2s32},
214 {v4s32, v4s32},
215 {v2s64, v2s64},
216 })
217 .widenScalarToNextPow2(0)
218 .clampScalar(1, s32, s64)
219 .clampScalar(0, s32, s64)
220 .clampNumElements(0, v8s8, v16s8)
221 .clampNumElements(0, v4s16, v8s16)
222 .clampNumElements(0, v2s32, v4s32)
223 .clampNumElements(0, v2s64, v2s64)
225 .minScalarSameAs(1, 0)
229
231 .legalFor({{p0, s64}, {v2p0, v2s64}})
232 .clampScalarOrElt(1, s64, s64)
233 .clampNumElements(0, v2p0, v2p0);
234
235 getActionDefinitionsBuilder(G_PTRMASK).legalFor({{p0, s64}});
236
237 getActionDefinitionsBuilder({G_SDIV, G_UDIV})
238 .legalFor({s32, s64})
239 .libcallFor({s128})
240 .clampScalar(0, s32, s64)
242 .scalarize(0);
243
244 getActionDefinitionsBuilder({G_SREM, G_UREM, G_SDIVREM, G_UDIVREM})
245 .lowerFor({s8, s16, s32, s64, v2s32, v4s32, v2s64})
246 .libcallFor({s128})
248 .minScalarOrElt(0, s32)
249 .clampNumElements(0, v2s32, v4s32)
250 .clampNumElements(0, v2s64, v2s64)
251 .scalarize(0);
252
253 getActionDefinitionsBuilder({G_SMULO, G_UMULO})
254 .widenScalarToNextPow2(0, /*Min = */ 32)
255 .clampScalar(0, s32, s64)
256 .lower();
257
258 getActionDefinitionsBuilder({G_SMULH, G_UMULH})
259 .legalFor({s64, v16s8, v8s16, v4s32})
260 .lower();
261
262 getActionDefinitionsBuilder({G_SMIN, G_SMAX, G_UMIN, G_UMAX})
263 .legalFor({v8s8, v16s8, v4s16, v8s16, v2s32, v4s32})
264 .legalFor(HasCSSC, {s32, s64})
265 .minScalar(HasCSSC, 0, s32)
266 .clampNumElements(0, v8s8, v16s8)
267 .clampNumElements(0, v4s16, v8s16)
268 .clampNumElements(0, v2s32, v4s32)
269 .lower();
270
271 // FIXME: Legal vector types are only legal with NEON.
273 .legalFor(HasCSSC, {s32, s64})
274 .legalFor(PackedVectorAllTypeList)
275 .customIf([=](const LegalityQuery &Q) {
276 // TODO: Fix suboptimal codegen for 128+ bit types.
277 LLT SrcTy = Q.Types[0];
278 return SrcTy.isScalar() && SrcTy.getSizeInBits() < 128;
279 })
280 .widenScalarIf(
281 [=](const LegalityQuery &Query) { return Query.Types[0] == v4s8; },
282 [=](const LegalityQuery &Query) { return std::make_pair(0, v4s16); })
283 .widenScalarIf(
284 [=](const LegalityQuery &Query) { return Query.Types[0] == v2s16; },
285 [=](const LegalityQuery &Query) { return std::make_pair(0, v2s32); })
286 .clampNumElements(0, v8s8, v16s8)
287 .clampNumElements(0, v4s16, v8s16)
288 .clampNumElements(0, v2s32, v4s32)
289 .clampNumElements(0, v2s64, v2s64)
291 .lower();
292
294 {G_ABDS, G_ABDU, G_UAVGFLOOR, G_UAVGCEIL, G_SAVGFLOOR, G_SAVGCEIL})
295 .legalFor({v8s8, v16s8, v4s16, v8s16, v2s32, v4s32})
296 .lower();
297
299 {G_SADDE, G_SSUBE, G_UADDE, G_USUBE, G_SADDO, G_SSUBO, G_UADDO, G_USUBO})
300 .legalFor({{s32, s32}, {s64, s32}})
301 .clampScalar(0, s32, s64)
302 .clampScalar(1, s32, s64)
304
305 getActionDefinitionsBuilder({G_FSHL, G_FSHR})
306 .customFor({{s32, s32}, {s32, s64}, {s64, s64}})
307 .lower();
308
310 .legalFor({{s32, s64}, {s64, s64}})
311 .customIf([=](const LegalityQuery &Q) {
312 return Q.Types[0].isScalar() && Q.Types[1].getScalarSizeInBits() < 64;
313 })
314 .lower();
316
317 getActionDefinitionsBuilder({G_SBFX, G_UBFX})
318 .customFor({{s32, s32}, {s64, s64}});
319
320 auto always = [=](const LegalityQuery &Q) { return true; };
322 .legalFor(HasCSSC, {{s32, s32}, {s64, s64}})
323 .legalFor({{v8s8, v8s8}, {v16s8, v16s8}})
324 .customFor(!HasCSSC, {{s32, s32}, {s64, s64}})
325 .customFor({{s128, s128},
326 {v4s16, v4s16},
327 {v8s16, v8s16},
328 {v2s32, v2s32},
329 {v4s32, v4s32},
330 {v2s64, v2s64}})
331 .clampScalar(0, s32, s128)
333 .minScalarEltSameAsIf(always, 1, 0)
334 .maxScalarEltSameAsIf(always, 1, 0)
335 .clampNumElements(0, v8s8, v16s8)
336 .clampNumElements(0, v4s16, v8s16)
337 .clampNumElements(0, v2s32, v4s32)
338 .clampNumElements(0, v2s64, v2s64)
341
342 getActionDefinitionsBuilder({G_CTLZ, G_CTLS})
343 .legalFor({{s32, s32},
344 {s64, s64},
345 {v8s8, v8s8},
346 {v16s8, v16s8},
347 {v4s16, v4s16},
348 {v8s16, v8s16},
349 {v2s32, v2s32},
350 {v4s32, v4s32}})
351 .widenScalarToNextPow2(1, /*Min=*/32)
352 .clampScalar(1, s32, s64)
353 .clampNumElements(0, v8s8, v16s8)
354 .clampNumElements(0, v4s16, v8s16)
355 .clampNumElements(0, v2s32, v4s32)
358 .scalarSameSizeAs(0, 1);
359
360 getActionDefinitionsBuilder(G_CTLZ_ZERO_UNDEF).lower();
361
363 .lowerIf(isVector(0))
364 .widenScalarToNextPow2(1, /*Min=*/32)
365 .clampScalar(1, s32, s64)
366 .scalarSameSizeAs(0, 1)
367 .legalFor(HasCSSC, {s32, s64})
368 .customFor(!HasCSSC, {s32, s64});
369
370 getActionDefinitionsBuilder(G_CTTZ_ZERO_UNDEF).lower();
371
372 getActionDefinitionsBuilder(G_BITREVERSE)
373 .legalFor({s32, s64, v8s8, v16s8})
374 .widenScalarToNextPow2(0, /*Min = */ 32)
376 .clampScalar(0, s32, s64)
377 .clampNumElements(0, v8s8, v16s8)
378 .clampNumElements(0, v4s16, v8s16)
379 .clampNumElements(0, v2s32, v4s32)
380 .clampNumElements(0, v2s64, v2s64)
383 .lower();
384
386 .legalFor({s32, s64, v4s16, v8s16, v2s32, v4s32, v2s64})
388 .clampScalar(0, s32, s64)
389 .clampNumElements(0, v4s16, v8s16)
390 .clampNumElements(0, v2s32, v4s32)
391 .clampNumElements(0, v2s64, v2s64)
393
394 getActionDefinitionsBuilder({G_UADDSAT, G_SADDSAT, G_USUBSAT, G_SSUBSAT})
395 .legalFor({v8s8, v16s8, v4s16, v8s16, v2s32, v4s32, v2s64})
396 .legalFor(HasSVE, {nxv16s8, nxv8s16, nxv4s32, nxv2s64})
397 .clampNumElements(0, v8s8, v16s8)
398 .clampNumElements(0, v4s16, v8s16)
399 .clampNumElements(0, v2s32, v4s32)
400 .clampMaxNumElements(0, s64, 2)
403 .lower();
404
406 {G_FADD, G_FSUB, G_FMUL, G_FDIV, G_FMA, G_FSQRT, G_FMAXNUM, G_FMINNUM,
407 G_FMAXIMUM, G_FMINIMUM, G_FCEIL, G_FFLOOR, G_FRINT, G_FNEARBYINT,
408 G_INTRINSIC_TRUNC, G_INTRINSIC_ROUND, G_INTRINSIC_ROUNDEVEN})
409 .legalFor({s32, s64, v2s32, v4s32, v2s64})
410 .legalFor(HasFP16, {s16, v4s16, v8s16})
411 .libcallFor({s128})
412 .scalarizeIf(scalarOrEltWiderThan(0, 64), 0)
413 .minScalarOrElt(0, MinFPScalar)
414 .clampNumElements(0, v4s16, v8s16)
415 .clampNumElements(0, v2s32, v4s32)
416 .clampNumElements(0, v2s64, v2s64)
418
419 getActionDefinitionsBuilder({G_FABS, G_FNEG})
420 .legalFor({s32, s64, v2s32, v4s32, v2s64})
421 .legalFor(HasFP16, {s16, v4s16, v8s16})
422 .scalarizeIf(scalarOrEltWiderThan(0, 64), 0)
424 .clampNumElements(0, v4s16, v8s16)
425 .clampNumElements(0, v2s32, v4s32)
426 .clampNumElements(0, v2s64, v2s64)
428 .lowerFor({s16, v4s16, v8s16});
429
431 .libcallFor({s32, s64, s128})
432 .minScalar(0, s32)
433 .scalarize(0);
434
435 getActionDefinitionsBuilder({G_FCOS, G_FSIN, G_FPOW, G_FLOG, G_FLOG2,
436 G_FLOG10, G_FTAN, G_FEXP, G_FEXP2, G_FEXP10,
437 G_FACOS, G_FASIN, G_FATAN, G_FATAN2, G_FCOSH,
438 G_FSINH, G_FTANH, G_FMODF})
439 // We need a call for these, so we always need to scalarize.
440 .scalarize(0)
441 // Regardless of FP16 support, widen 16-bit elements to 32-bits.
442 .minScalar(0, s32)
443 .libcallFor({s32, s64, s128});
444 getActionDefinitionsBuilder({G_FPOWI, G_FLDEXP})
445 .scalarize(0)
446 .minScalar(0, s32)
447 .libcallFor({{s32, s32}, {s64, s32}, {s128, s32}});
448
449 getActionDefinitionsBuilder({G_LROUND, G_INTRINSIC_LRINT})
450 .legalFor({{s32, s32}, {s32, s64}, {s64, s32}, {s64, s64}})
451 .legalFor(HasFP16, {{s32, s16}, {s64, s16}})
452 .minScalar(1, s32)
453 .libcallFor({{s64, s128}})
454 .lower();
455 getActionDefinitionsBuilder({G_LLROUND, G_INTRINSIC_LLRINT})
456 .legalFor({{s64, s32}, {s64, s64}})
457 .legalFor(HasFP16, {{s64, s16}})
458 .minScalar(0, s64)
459 .minScalar(1, s32)
460 .libcallFor({{s64, s128}})
461 .lower();
462
463 // TODO: Custom legalization for mismatched types.
464 getActionDefinitionsBuilder(G_FCOPYSIGN)
466 [](const LegalityQuery &Query) { return Query.Types[0].isScalar(); },
467 [=](const LegalityQuery &Query) {
468 const LLT Ty = Query.Types[0];
469 return std::pair(0, LLT::fixed_vector(Ty == s16 ? 4 : 2, Ty));
470 })
471 .lower();
472
474
475 for (unsigned Op : {G_SEXTLOAD, G_ZEXTLOAD}) {
476 auto &Actions = getActionDefinitionsBuilder(Op);
477
478 if (Op == G_SEXTLOAD)
480
481 // Atomics have zero extending behavior.
482 Actions
483 .legalForTypesWithMemDesc({{s32, p0, s8, 8},
484 {s32, p0, s16, 8},
485 {s32, p0, s32, 8},
486 {s64, p0, s8, 2},
487 {s64, p0, s16, 2},
488 {s64, p0, s32, 4},
489 {s64, p0, s64, 8},
490 {p0, p0, s64, 8},
491 {v2s32, p0, s64, 8}})
492 .widenScalarToNextPow2(0)
493 .clampScalar(0, s32, s64)
494 // TODO: We could support sum-of-pow2's but the lowering code doesn't know
495 // how to do that yet.
496 .unsupportedIfMemSizeNotPow2()
497 // Lower anything left over into G_*EXT and G_LOAD
498 .lower();
499 }
500
501 auto IsPtrVecPred = [=](const LegalityQuery &Query) {
502 const LLT &ValTy = Query.Types[0];
503 return ValTy.isPointerVector() && ValTy.getAddressSpace() == 0;
504 };
505
507 .customIf([=](const LegalityQuery &Query) {
508 return HasRCPC3 && Query.Types[0] == s128 &&
509 Query.MMODescrs[0].Ordering == AtomicOrdering::Acquire;
510 })
511 .customIf([=](const LegalityQuery &Query) {
512 return Query.Types[0] == s128 &&
513 Query.MMODescrs[0].Ordering != AtomicOrdering::NotAtomic;
514 })
515 .legalForTypesWithMemDesc({{s8, p0, s8, 8},
516 {s16, p0, s16, 8},
517 {s32, p0, s32, 8},
518 {s64, p0, s64, 8},
519 {p0, p0, s64, 8},
520 {s128, p0, s128, 8},
521 {v8s8, p0, s64, 8},
522 {v16s8, p0, s128, 8},
523 {v4s16, p0, s64, 8},
524 {v8s16, p0, s128, 8},
525 {v2s32, p0, s64, 8},
526 {v4s32, p0, s128, 8},
527 {v2s64, p0, s128, 8}})
528 // These extends are also legal
529 .legalForTypesWithMemDesc(
530 {{s32, p0, s8, 8}, {s32, p0, s16, 8}, {s64, p0, s32, 8}})
531 .legalForTypesWithMemDesc({
532 // SVE vscale x 128 bit base sizes
533 {nxv16s8, p0, nxv16s8, 8},
534 {nxv8s16, p0, nxv8s16, 8},
535 {nxv4s32, p0, nxv4s32, 8},
536 {nxv2s64, p0, nxv2s64, 8},
537 })
538 .widenScalarToNextPow2(0, /* MinSize = */ 8)
539 .clampMaxNumElements(0, s8, 16)
540 .clampMaxNumElements(0, s16, 8)
541 .clampMaxNumElements(0, s32, 4)
542 .clampMaxNumElements(0, s64, 2)
543 .clampMaxNumElements(0, p0, 2)
545 .clampScalar(0, s8, s64)
547 [=](const LegalityQuery &Query) {
548 // Clamp extending load results to 32-bits.
549 return Query.Types[0].isScalar() &&
550 Query.Types[0] != Query.MMODescrs[0].MemoryTy &&
551 Query.Types[0].getSizeInBits() > 32;
552 },
553 changeTo(0, s32))
554 // TODO: Use BITCAST for v2i8, v2i16 after G_TRUNC gets sorted out
555 .bitcastIf(typeInSet(0, {v4s8}),
556 [=](const LegalityQuery &Query) {
557 const LLT VecTy = Query.Types[0];
558 return std::pair(0, LLT::scalar(VecTy.getSizeInBits()));
559 })
560 .customIf(IsPtrVecPred)
561 .scalarizeIf(typeInSet(0, {v2s16, v2s8}), 0)
562 .scalarizeIf(scalarOrEltWiderThan(0, 64), 0);
563
565 .customIf([=](const LegalityQuery &Query) {
566 return HasRCPC3 && Query.Types[0] == s128 &&
567 Query.MMODescrs[0].Ordering == AtomicOrdering::Release;
568 })
569 .customIf([=](const LegalityQuery &Query) {
570 return Query.Types[0] == s128 &&
571 Query.MMODescrs[0].Ordering != AtomicOrdering::NotAtomic;
572 })
573 .widenScalarIf(
574 all(scalarNarrowerThan(0, 32),
576 changeTo(0, s32))
578 {{s8, p0, s8, 8}, {s16, p0, s8, 8}, // truncstorei8 from s16
579 {s32, p0, s8, 8}, // truncstorei8 from s32
580 {s64, p0, s8, 8}, // truncstorei8 from s64
581 {s16, p0, s16, 8}, {s32, p0, s16, 8}, // truncstorei16 from s32
582 {s64, p0, s16, 8}, // truncstorei16 from s64
583 {s32, p0, s8, 8}, {s32, p0, s16, 8}, {s32, p0, s32, 8},
584 {s64, p0, s64, 8}, {s64, p0, s32, 8}, // truncstorei32 from s64
585 {p0, p0, s64, 8}, {s128, p0, s128, 8}, {v16s8, p0, s128, 8},
586 {v8s8, p0, s64, 8}, {v4s16, p0, s64, 8}, {v8s16, p0, s128, 8},
587 {v2s32, p0, s64, 8}, {v4s32, p0, s128, 8}, {v2s64, p0, s128, 8}})
588 .legalForTypesWithMemDesc({
589 // SVE vscale x 128 bit base sizes
590 // TODO: Add nxv2p0. Consider bitcastIf.
591 // See #92130
592 // https://github.com/llvm/llvm-project/pull/92130#discussion_r1616888461
593 {nxv16s8, p0, nxv16s8, 8},
594 {nxv8s16, p0, nxv8s16, 8},
595 {nxv4s32, p0, nxv4s32, 8},
596 {nxv2s64, p0, nxv2s64, 8},
597 })
598 .clampScalar(0, s8, s64)
599 .minScalarOrElt(0, s8)
600 .lowerIf([=](const LegalityQuery &Query) {
601 return Query.Types[0].isScalar() &&
602 Query.Types[0] != Query.MMODescrs[0].MemoryTy;
603 })
604 // Maximum: sN * k = 128
605 .clampMaxNumElements(0, s8, 16)
606 .clampMaxNumElements(0, s16, 8)
607 .clampMaxNumElements(0, s32, 4)
608 .clampMaxNumElements(0, s64, 2)
609 .clampMaxNumElements(0, p0, 2)
611 // TODO: Use BITCAST for v2i8, v2i16 after G_TRUNC gets sorted out
612 .bitcastIf(all(typeInSet(0, {v4s8}),
613 LegalityPredicate([=](const LegalityQuery &Query) {
614 return Query.Types[0].getSizeInBits() ==
615 Query.MMODescrs[0].MemoryTy.getSizeInBits();
616 })),
617 [=](const LegalityQuery &Query) {
618 const LLT VecTy = Query.Types[0];
619 return std::pair(0, LLT::scalar(VecTy.getSizeInBits()));
620 })
621 .customIf(IsPtrVecPred)
622 .scalarizeIf(typeInSet(0, {v2s16, v2s8}), 0)
623 .scalarizeIf(scalarOrEltWiderThan(0, 64), 0)
624 .lower();
625
626 getActionDefinitionsBuilder(G_INDEXED_STORE)
627 // Idx 0 == Ptr, Idx 1 == Val
628 // TODO: we can implement legalizations but as of now these are
629 // generated in a very specific way.
631 {p0, s8, s8, 8},
632 {p0, s16, s16, 8},
633 {p0, s32, s8, 8},
634 {p0, s32, s16, 8},
635 {p0, s32, s32, 8},
636 {p0, s64, s64, 8},
637 {p0, p0, p0, 8},
638 {p0, v8s8, v8s8, 8},
639 {p0, v16s8, v16s8, 8},
640 {p0, v4s16, v4s16, 8},
641 {p0, v8s16, v8s16, 8},
642 {p0, v2s32, v2s32, 8},
643 {p0, v4s32, v4s32, 8},
644 {p0, v2s64, v2s64, 8},
645 {p0, v2p0, v2p0, 8},
646 {p0, s128, s128, 8},
647 })
648 .unsupported();
649
650 auto IndexedLoadBasicPred = [=](const LegalityQuery &Query) {
651 LLT LdTy = Query.Types[0];
652 LLT PtrTy = Query.Types[1];
653 if (!llvm::is_contained(PackedVectorAllTypesVec, LdTy) &&
654 !llvm::is_contained(ScalarAndPtrTypesVec, LdTy) && LdTy != s128)
655 return false;
656 if (PtrTy != p0)
657 return false;
658 return true;
659 };
660 getActionDefinitionsBuilder(G_INDEXED_LOAD)
663 .legalIf(IndexedLoadBasicPred)
664 .unsupported();
665 getActionDefinitionsBuilder({G_INDEXED_SEXTLOAD, G_INDEXED_ZEXTLOAD})
666 .unsupportedIf(
668 .legalIf(all(typeInSet(0, {s16, s32, s64}),
669 LegalityPredicate([=](const LegalityQuery &Q) {
670 LLT LdTy = Q.Types[0];
671 LLT PtrTy = Q.Types[1];
672 LLT MemTy = Q.MMODescrs[0].MemoryTy;
673 if (PtrTy != p0)
674 return false;
675 if (LdTy == s16)
676 return MemTy == s8;
677 if (LdTy == s32)
678 return MemTy == s8 || MemTy == s16;
679 if (LdTy == s64)
680 return MemTy == s8 || MemTy == s16 || MemTy == s32;
681 return false;
682 })))
683 .unsupported();
684
685 // Constants
687 .legalFor({p0, s8, s16, s32, s64})
688 .widenScalarToNextPow2(0)
689 .clampScalar(0, s8, s64);
690 getActionDefinitionsBuilder(G_FCONSTANT)
691 // Always legalize s16 to prevent G_FCONSTANT being widened to G_CONSTANT
692 .legalFor({s16, s32, s64, s128})
693 .clampScalar(0, MinFPScalar, s128);
694
695 // FIXME: fix moreElementsToNextPow2
697 .legalFor({{s32, s32}, {s32, s64}, {s32, p0}})
699 .clampScalar(1, s32, s64)
700 .clampScalar(0, s32, s32)
703 [=](const LegalityQuery &Query) {
704 const LLT &Ty = Query.Types[0];
705 const LLT &SrcTy = Query.Types[1];
706 return Ty.isVector() && !SrcTy.isPointerVector() &&
707 Ty.getElementType() != SrcTy.getElementType();
708 },
709 0, 1)
710 .minScalarOrEltIf(
711 [=](const LegalityQuery &Query) { return Query.Types[1] == v2s16; },
712 1, s32)
713 .minScalarOrEltIf(
714 [=](const LegalityQuery &Query) {
715 return Query.Types[1].isPointerVector();
716 },
717 0, s64)
719 .clampNumElements(1, v8s8, v16s8)
720 .clampNumElements(1, v4s16, v8s16)
721 .clampNumElements(1, v2s32, v4s32)
722 .clampNumElements(1, v2s64, v2s64)
723 .clampNumElements(1, v2p0, v2p0)
724 .customIf(isVector(0));
725
727 .legalFor({{s32, s32},
728 {s32, s64},
729 {v4s32, v4s32},
730 {v2s32, v2s32},
731 {v2s64, v2s64}})
732 .legalFor(HasFP16, {{s32, s16}, {v4s16, v4s16}, {v8s16, v8s16}})
734 .clampScalar(0, s32, s32)
735 .minScalarOrElt(1, MinFPScalar)
738 [=](const LegalityQuery &Query) {
739 const LLT &Ty = Query.Types[0];
740 const LLT &SrcTy = Query.Types[1];
741 return Ty.isVector() && !SrcTy.isPointerVector() &&
742 Ty.getElementType() != SrcTy.getElementType();
743 },
744 0, 1)
745 .clampNumElements(1, v4s16, v8s16)
746 .clampNumElements(1, v2s32, v4s32)
747 .clampMaxNumElements(1, s64, 2)
749 .libcallFor({{s32, s128}});
750
751 // Extensions
752 auto ExtLegalFunc = [=](const LegalityQuery &Query) {
753 unsigned DstSize = Query.Types[0].getSizeInBits();
754
755 // Handle legal vectors using legalFor
756 if (Query.Types[0].isVector())
757 return false;
758
759 if (DstSize < 8 || DstSize >= 128 || !isPowerOf2_32(DstSize))
760 return false; // Extending to a scalar s128 needs narrowing.
761
762 const LLT &SrcTy = Query.Types[1];
763
764 // Make sure we fit in a register otherwise. Don't bother checking that
765 // the source type is below 128 bits. We shouldn't be allowing anything
766 // through which is wider than the destination in the first place.
767 unsigned SrcSize = SrcTy.getSizeInBits();
768 if (SrcSize < 8 || !isPowerOf2_32(SrcSize))
769 return false;
770
771 return true;
772 };
773 getActionDefinitionsBuilder({G_ZEXT, G_SEXT, G_ANYEXT})
774 .legalIf(ExtLegalFunc)
775 .legalFor({{v8s16, v8s8}, {v4s32, v4s16}, {v2s64, v2s32}})
776 .clampScalar(0, s64, s64) // Just for s128, others are handled above.
778 .clampMaxNumElements(1, s8, 8)
779 .clampMaxNumElements(1, s16, 4)
780 .clampMaxNumElements(1, s32, 2)
781 // Tries to convert a large EXTEND into two smaller EXTENDs
782 .lowerIf([=](const LegalityQuery &Query) {
783 return (Query.Types[0].getScalarSizeInBits() >
784 Query.Types[1].getScalarSizeInBits() * 2) &&
785 Query.Types[0].isVector() &&
786 (Query.Types[1].getScalarSizeInBits() == 8 ||
787 Query.Types[1].getScalarSizeInBits() == 16);
788 })
789 .clampMinNumElements(1, s8, 8)
790 .clampMinNumElements(1, s16, 4)
792
794 .legalFor({{v8s8, v8s16}, {v4s16, v4s32}, {v2s32, v2s64}})
796 .clampMaxNumElements(0, s8, 8)
797 .clampMaxNumElements(0, s16, 4)
798 .clampMaxNumElements(0, s32, 2)
800 [=](const LegalityQuery &Query) { return Query.Types[0].isVector(); },
801 0, s8)
802 .lowerIf([=](const LegalityQuery &Query) {
803 LLT DstTy = Query.Types[0];
804 LLT SrcTy = Query.Types[1];
805 return DstTy.isVector() && SrcTy.getSizeInBits() > 128 &&
806 DstTy.getScalarSizeInBits() * 2 <= SrcTy.getScalarSizeInBits();
807 })
808 .clampMinNumElements(0, s8, 8)
809 .clampMinNumElements(0, s16, 4)
810 .alwaysLegal();
811
812 getActionDefinitionsBuilder({G_TRUNC_SSAT_S, G_TRUNC_SSAT_U, G_TRUNC_USAT_U})
813 .legalFor({{v8s8, v8s16}, {v4s16, v4s32}, {v2s32, v2s64}})
814 .clampNumElements(0, v2s32, v2s32);
815
816 getActionDefinitionsBuilder(G_SEXT_INREG)
817 .legalFor({s32, s64})
818 .legalFor(PackedVectorAllTypeList)
819 .maxScalar(0, s64)
820 .clampNumElements(0, v8s8, v16s8)
821 .clampNumElements(0, v4s16, v8s16)
822 .clampNumElements(0, v2s32, v4s32)
823 .clampMaxNumElements(0, s64, 2)
824 .lower();
825
826 // FP conversions
828 .legalFor(
829 {{s16, s32}, {s16, s64}, {s32, s64}, {v4s16, v4s32}, {v2s32, v2s64}})
830 .libcallFor({{s16, s128}, {s32, s128}, {s64, s128}})
832 .customIf([](const LegalityQuery &Q) {
833 LLT DstTy = Q.Types[0];
834 LLT SrcTy = Q.Types[1];
835 return SrcTy.isFixedVector() && DstTy.isFixedVector() &&
836 SrcTy.getScalarSizeInBits() == 64 &&
837 DstTy.getScalarSizeInBits() == 16;
838 })
839 // Clamp based on input
840 .clampNumElements(1, v4s32, v4s32)
841 .clampNumElements(1, v2s64, v2s64)
842 .scalarize(0);
843
845 .legalFor(
846 {{s32, s16}, {s64, s16}, {s64, s32}, {v4s32, v4s16}, {v2s64, v2s32}})
847 .libcallFor({{s128, s64}, {s128, s32}, {s128, s16}})
850 [](const LegalityQuery &Q) {
851 LLT DstTy = Q.Types[0];
852 LLT SrcTy = Q.Types[1];
853 return SrcTy.isVector() && DstTy.isVector() &&
854 SrcTy.getScalarSizeInBits() == 16 &&
855 DstTy.getScalarSizeInBits() == 64;
856 },
857 changeElementTo(1, s32))
858 .clampNumElements(0, v4s32, v4s32)
859 .clampNumElements(0, v2s64, v2s64)
860 .scalarize(0);
861
862 // Conversions
863 getActionDefinitionsBuilder({G_FPTOSI, G_FPTOUI})
864 .legalFor({{s32, s32},
865 {s64, s32},
866 {s32, s64},
867 {s64, s64},
868 {v2s32, v2s32},
869 {v4s32, v4s32},
870 {v2s64, v2s64}})
871 .legalFor(HasFP16,
872 {{s32, s16}, {s64, s16}, {v4s16, v4s16}, {v8s16, v8s16}})
873 .scalarizeIf(scalarOrEltWiderThan(0, 64), 0)
875 // The range of a fp16 value fits into an i17, so we can lower the width
876 // to i64.
878 [=](const LegalityQuery &Query) {
879 return Query.Types[1] == s16 && Query.Types[0].getSizeInBits() > 64;
880 },
881 changeTo(0, s64))
884 .minScalar(0, s32)
885 .widenScalarOrEltToNextPow2OrMinSize(1, /*MinSize=*/HasFP16 ? 16 : 32)
887 [=](const LegalityQuery &Query) {
888 return Query.Types[0].getScalarSizeInBits() <= 64 &&
889 Query.Types[0].getScalarSizeInBits() >
890 Query.Types[1].getScalarSizeInBits();
891 },
893 .widenScalarIf(
894 [=](const LegalityQuery &Query) {
895 return Query.Types[1].getScalarSizeInBits() <= 64 &&
896 Query.Types[0].getScalarSizeInBits() <
897 Query.Types[1].getScalarSizeInBits();
898 },
900 .clampNumElements(0, v4s16, v8s16)
901 .clampNumElements(0, v2s32, v4s32)
902 .clampMaxNumElements(0, s64, 2)
903 .libcallFor(
904 {{s32, s128}, {s64, s128}, {s128, s128}, {s128, s32}, {s128, s64}});
905
906 getActionDefinitionsBuilder({G_FPTOSI_SAT, G_FPTOUI_SAT})
907 .legalFor({{s32, s32},
908 {s64, s32},
909 {s32, s64},
910 {s64, s64},
911 {v2s32, v2s32},
912 {v4s32, v4s32},
913 {v2s64, v2s64}})
914 .legalFor(
915 HasFP16,
916 {{s16, s16}, {s32, s16}, {s64, s16}, {v4s16, v4s16}, {v8s16, v8s16}})
917 // Handle types larger than i64 by scalarizing/lowering.
918 .scalarizeIf(scalarOrEltWiderThan(0, 64), 0)
920 // The range of a fp16 value fits into an i17, so we can lower the width
921 // to i64.
923 [=](const LegalityQuery &Query) {
924 return Query.Types[1] == s16 && Query.Types[0].getSizeInBits() > 64;
925 },
926 changeTo(0, s64))
927 .lowerIf(::any(scalarWiderThan(0, 64), scalarWiderThan(1, 64)), 0)
929 .widenScalarToNextPow2(0, /*MinSize=*/32)
930 .minScalar(0, s32)
931 .widenScalarOrEltToNextPow2OrMinSize(1, /*MinSize=*/HasFP16 ? 16 : 32)
933 [=](const LegalityQuery &Query) {
934 unsigned ITySize = Query.Types[0].getScalarSizeInBits();
935 return (ITySize == 16 || ITySize == 32 || ITySize == 64) &&
936 ITySize > Query.Types[1].getScalarSizeInBits();
937 },
939 .widenScalarIf(
940 [=](const LegalityQuery &Query) {
941 unsigned FTySize = Query.Types[1].getScalarSizeInBits();
942 return (FTySize == 16 || FTySize == 32 || FTySize == 64) &&
943 Query.Types[0].getScalarSizeInBits() < FTySize;
944 },
947 .clampNumElements(0, v4s16, v8s16)
948 .clampNumElements(0, v2s32, v4s32)
949 .clampMaxNumElements(0, s64, 2);
950
951 getActionDefinitionsBuilder({G_SITOFP, G_UITOFP})
952 .legalFor({{s32, s32},
953 {s64, s32},
954 {s32, s64},
955 {s64, s64},
956 {v2s32, v2s32},
957 {v4s32, v4s32},
958 {v2s64, v2s64}})
959 .legalFor(HasFP16,
960 {{s16, s32}, {s16, s64}, {v4s16, v4s16}, {v8s16, v8s16}})
961 .scalarizeIf(scalarOrEltWiderThan(1, 64), 1)
965 .minScalar(1, s32)
966 .lowerIf([](const LegalityQuery &Query) {
967 return Query.Types[1].isVector() &&
968 Query.Types[1].getScalarSizeInBits() == 64 &&
969 Query.Types[0].getScalarSizeInBits() == 16;
970 })
971 .widenScalarOrEltToNextPow2OrMinSize(0, /*MinSize=*/HasFP16 ? 16 : 32)
973 // v2i64->v2f32 needs to scalarize to avoid double-rounding issues.
974 [](const LegalityQuery &Query) {
975 return Query.Types[0].getScalarSizeInBits() == 32 &&
976 Query.Types[1].getScalarSizeInBits() == 64;
977 },
978 0)
979 .widenScalarIf(
980 [](const LegalityQuery &Query) {
981 return Query.Types[1].getScalarSizeInBits() <= 64 &&
982 Query.Types[0].getScalarSizeInBits() <
983 Query.Types[1].getScalarSizeInBits();
984 },
986 .widenScalarIf(
987 [](const LegalityQuery &Query) {
988 return Query.Types[0].getScalarSizeInBits() <= 64 &&
989 Query.Types[0].getScalarSizeInBits() >
990 Query.Types[1].getScalarSizeInBits();
991 },
993 .clampNumElements(0, v4s16, v8s16)
994 .clampNumElements(0, v2s32, v4s32)
995 .clampMaxNumElements(0, s64, 2)
996 .libcallFor({{s16, s128},
997 {s32, s128},
998 {s64, s128},
999 {s128, s128},
1000 {s128, s32},
1001 {s128, s64}});
1002
1003 // Control-flow
1006 .legalFor({s32})
1007 .clampScalar(0, s32, s32);
1008 getActionDefinitionsBuilder(G_BRINDIRECT).legalFor({p0});
1009
1011 .legalFor({{s32, s32}, {s64, s32}, {p0, s32}})
1012 .widenScalarToNextPow2(0)
1013 .clampScalar(0, s32, s64)
1014 .clampScalar(1, s32, s32)
1017 .lowerIf(isVector(0));
1018
1019 // Pointer-handling
1020 getActionDefinitionsBuilder(G_FRAME_INDEX).legalFor({p0});
1021
1022 if (TM.getCodeModel() == CodeModel::Small)
1023 getActionDefinitionsBuilder(G_GLOBAL_VALUE).custom();
1024 else
1025 getActionDefinitionsBuilder(G_GLOBAL_VALUE).legalFor({p0});
1026
1027 getActionDefinitionsBuilder(G_PTRAUTH_GLOBAL_VALUE)
1028 .legalIf(all(typeIs(0, p0), typeIs(1, p0)));
1029
1030 getActionDefinitionsBuilder(G_PTRTOINT)
1031 .legalFor({{s64, p0}, {v2s64, v2p0}})
1032 .widenScalarToNextPow2(0, 64)
1033 .clampScalar(0, s64, s64)
1034 .clampMaxNumElements(0, s64, 2);
1035
1036 getActionDefinitionsBuilder(G_INTTOPTR)
1037 .unsupportedIf([&](const LegalityQuery &Query) {
1038 return Query.Types[0].getSizeInBits() != Query.Types[1].getSizeInBits();
1039 })
1040 .legalFor({{p0, s64}, {v2p0, v2s64}})
1041 .clampMaxNumElements(1, s64, 2);
1042
1043 // Casts for 32 and 64-bit width type are just copies.
1044 // Same for 128-bit width type, except they are on the FPR bank.
1046 // Keeping 32-bit instructions legal to prevent regression in some tests
1047 .legalForCartesianProduct({s32, v2s16, v4s8})
1048 .legalForCartesianProduct({s64, v8s8, v4s16, v2s32})
1049 .legalForCartesianProduct({s128, v16s8, v8s16, v4s32, v2s64, v2p0})
1050 .customIf([=](const LegalityQuery &Query) {
1051 // Handle casts from i1 vectors to scalars.
1052 LLT DstTy = Query.Types[0];
1053 LLT SrcTy = Query.Types[1];
1054 return DstTy.isScalar() && SrcTy.isVector() &&
1055 SrcTy.getScalarSizeInBits() == 1;
1056 })
1057 .lowerIf([=](const LegalityQuery &Query) {
1058 return Query.Types[0].isVector() != Query.Types[1].isVector();
1059 })
1061 .clampNumElements(0, v8s8, v16s8)
1062 .clampNumElements(0, v4s16, v8s16)
1063 .clampNumElements(0, v2s32, v4s32)
1064 .lower();
1065
1066 getActionDefinitionsBuilder(G_VASTART).legalFor({p0});
1067
1068 // va_list must be a pointer, but most sized types are pretty easy to handle
1069 // as the destination.
1071 .customForCartesianProduct({s8, s16, s32, s64, p0}, {p0})
1072 .clampScalar(0, s8, s64)
1073 .widenScalarToNextPow2(0, /*Min*/ 8);
1074
1075 getActionDefinitionsBuilder(G_ATOMIC_CMPXCHG_WITH_SUCCESS)
1076 .lowerIf(
1077 all(typeInSet(0, {s8, s16, s32, s64, s128}), typeIs(2, p0)));
1078
1079 bool UseOutlineAtomics = ST.outlineAtomics() && !ST.hasLSE();
1080
1081 getActionDefinitionsBuilder(G_ATOMIC_CMPXCHG)
1082 .legalFor(!UseOutlineAtomics, {{s32, p0}, {s64, p0}})
1083 .customFor(!UseOutlineAtomics, {{s128, p0}})
1084 .libcallFor(UseOutlineAtomics,
1085 {{s8, p0}, {s16, p0}, {s32, p0}, {s64, p0}, {s128, p0}})
1086 .clampScalar(0, s32, s64);
1087
1088 getActionDefinitionsBuilder({G_ATOMICRMW_XCHG, G_ATOMICRMW_ADD,
1089 G_ATOMICRMW_SUB, G_ATOMICRMW_AND, G_ATOMICRMW_OR,
1090 G_ATOMICRMW_XOR})
1091 .legalFor(!UseOutlineAtomics, {{s32, p0}, {s64, p0}})
1092 .libcallFor(UseOutlineAtomics,
1093 {{s8, p0}, {s16, p0}, {s32, p0}, {s64, p0}})
1094 .clampScalar(0, s32, s64);
1095
1096 // Do not outline these atomics operations, as per comment in
1097 // AArch64ISelLowering.cpp's shouldExpandAtomicRMWInIR().
1099 {G_ATOMICRMW_MIN, G_ATOMICRMW_MAX, G_ATOMICRMW_UMIN, G_ATOMICRMW_UMAX})
1100 .legalIf(all(typeInSet(0, {s32, s64}), typeIs(1, p0)))
1101 .clampScalar(0, s32, s64);
1102
1103 getActionDefinitionsBuilder(G_BLOCK_ADDR).legalFor({p0});
1104
1105 // Merge/Unmerge
1106 for (unsigned Op : {G_MERGE_VALUES, G_UNMERGE_VALUES}) {
1107 unsigned BigTyIdx = Op == G_MERGE_VALUES ? 0 : 1;
1108 unsigned LitTyIdx = Op == G_MERGE_VALUES ? 1 : 0;
1110 .widenScalarToNextPow2(LitTyIdx, 8)
1111 .widenScalarToNextPow2(BigTyIdx, 32)
1112 .clampScalar(LitTyIdx, s8, s64)
1113 .clampScalar(BigTyIdx, s32, s128)
1114 .legalIf([=](const LegalityQuery &Q) {
1115 switch (Q.Types[BigTyIdx].getSizeInBits()) {
1116 case 32:
1117 case 64:
1118 case 128:
1119 break;
1120 default:
1121 return false;
1122 }
1123 switch (Q.Types[LitTyIdx].getSizeInBits()) {
1124 case 8:
1125 case 16:
1126 case 32:
1127 case 64:
1128 return true;
1129 default:
1130 return false;
1131 }
1132 });
1133 }
1134
1135 // TODO : nxv4s16, nxv2s16, nxv2s32
1136 getActionDefinitionsBuilder(G_EXTRACT_VECTOR_ELT)
1137 .legalFor(HasSVE, {{s16, nxv16s8, s64},
1138 {s16, nxv8s16, s64},
1139 {s32, nxv4s32, s64},
1140 {s64, nxv2s64, s64}})
1141 .unsupportedIf([=](const LegalityQuery &Query) {
1142 const LLT &EltTy = Query.Types[1].getElementType();
1143 if (Query.Types[1].isScalableVector())
1144 return false;
1145 return Query.Types[0] != EltTy;
1146 })
1147 .minScalar(2, s64)
1148 .customIf([=](const LegalityQuery &Query) {
1149 const LLT &VecTy = Query.Types[1];
1150 return VecTy == v8s8 || VecTy == v16s8 || VecTy == v2s16 ||
1151 VecTy == v4s16 || VecTy == v8s16 || VecTy == v2s32 ||
1152 VecTy == v4s32 || VecTy == v2s64 || VecTy == v2p0;
1153 })
1154 .minScalarOrEltIf(
1155 [=](const LegalityQuery &Query) {
1156 // We want to promote to <M x s1> to <M x s64> if that wouldn't
1157 // cause the total vec size to be > 128b.
1158 return Query.Types[1].isFixedVector() &&
1159 Query.Types[1].getNumElements() <= 2;
1160 },
1161 0, s64)
1162 .minScalarOrEltIf(
1163 [=](const LegalityQuery &Query) {
1164 return Query.Types[1].isFixedVector() &&
1165 Query.Types[1].getNumElements() <= 4;
1166 },
1167 0, s32)
1168 .minScalarOrEltIf(
1169 [=](const LegalityQuery &Query) {
1170 return Query.Types[1].isFixedVector() &&
1171 Query.Types[1].getNumElements() <= 8;
1172 },
1173 0, s16)
1174 .minScalarOrEltIf(
1175 [=](const LegalityQuery &Query) {
1176 return Query.Types[1].isFixedVector() &&
1177 Query.Types[1].getNumElements() <= 16;
1178 },
1179 0, s8)
1180 .minScalarOrElt(0, s8) // Worst case, we need at least s8.
1182 .clampMaxNumElements(1, s64, 2)
1183 .clampMaxNumElements(1, s32, 4)
1184 .clampMaxNumElements(1, s16, 8)
1185 .clampMaxNumElements(1, s8, 16)
1186 .clampMaxNumElements(1, p0, 2)
1188
1189 getActionDefinitionsBuilder(G_INSERT_VECTOR_ELT)
1190 .legalIf(
1191 typeInSet(0, {v8s8, v16s8, v4s16, v8s16, v2s32, v4s32, v2s64, v2p0}))
1192 .legalFor(HasSVE, {{nxv16s8, s32, s64},
1193 {nxv8s16, s32, s64},
1194 {nxv4s32, s32, s64},
1195 {nxv2s64, s64, s64}})
1198 .clampNumElements(0, v8s8, v16s8)
1199 .clampNumElements(0, v4s16, v8s16)
1200 .clampNumElements(0, v2s32, v4s32)
1201 .clampMaxNumElements(0, s64, 2)
1202 .clampMaxNumElements(0, p0, 2)
1204
1205 getActionDefinitionsBuilder(G_BUILD_VECTOR)
1206 .legalFor({{v8s8, s8},
1207 {v16s8, s8},
1208 {v4s16, s16},
1209 {v8s16, s16},
1210 {v2s32, s32},
1211 {v4s32, s32},
1212 {v2s64, s64},
1213 {v2p0, p0}})
1214 .clampNumElements(0, v4s32, v4s32)
1215 .clampNumElements(0, v2s64, v2s64)
1216 .minScalarOrElt(0, s8)
1219 .minScalarSameAs(1, 0);
1220
1221 getActionDefinitionsBuilder(G_BUILD_VECTOR_TRUNC).lower();
1222
1223 getActionDefinitionsBuilder(G_SHUFFLE_VECTOR)
1224 .legalIf([=](const LegalityQuery &Query) {
1225 const LLT &DstTy = Query.Types[0];
1226 const LLT &SrcTy = Query.Types[1];
1227 // For now just support the TBL2 variant which needs the source vectors
1228 // to be the same size as the dest.
1229 if (DstTy != SrcTy)
1230 return false;
1231 return llvm::is_contained(
1232 {v8s8, v16s8, v4s16, v8s16, v2s32, v4s32, v2s64}, DstTy);
1233 })
1234 .moreElementsIf(
1235 [](const LegalityQuery &Query) {
1236 return Query.Types[0].getNumElements() >
1237 Query.Types[1].getNumElements();
1238 },
1239 changeTo(1, 0))
1242 [](const LegalityQuery &Query) {
1243 return Query.Types[0].getNumElements() <
1244 Query.Types[1].getNumElements();
1245 },
1246 changeTo(0, 1))
1247 .widenScalarOrEltToNextPow2OrMinSize(0, 8)
1248 .clampNumElements(0, v8s8, v16s8)
1249 .clampNumElements(0, v4s16, v8s16)
1250 .clampNumElements(0, v4s32, v4s32)
1251 .clampNumElements(0, v2s64, v2s64)
1253 .bitcastIf(isPointerVector(0), [=](const LegalityQuery &Query) {
1254 // Bitcast pointers vector to i64.
1255 const LLT DstTy = Query.Types[0];
1256 return std::pair(0, LLT::vector(DstTy.getElementCount(), 64));
1257 });
1258
1259 getActionDefinitionsBuilder(G_CONCAT_VECTORS)
1260 .legalFor({{v16s8, v8s8}, {v8s16, v4s16}, {v4s32, v2s32}})
1261 .bitcastIf(
1262 [=](const LegalityQuery &Query) {
1263 return Query.Types[0].isFixedVector() &&
1264 Query.Types[1].isFixedVector() &&
1265 Query.Types[0].getSizeInBits() <= 128 &&
1266 Query.Types[1].getSizeInBits() <= 64;
1267 },
1268 [=](const LegalityQuery &Query) {
1269 const LLT DstTy = Query.Types[0];
1270 const LLT SrcTy = Query.Types[1];
1271 return std::pair(
1272 0, DstTy.changeElementSize(SrcTy.getSizeInBits())
1275 SrcTy.getNumElements())));
1276 });
1277
1278 getActionDefinitionsBuilder(G_EXTRACT_SUBVECTOR)
1279 .legalFor({{v8s8, v16s8}, {v4s16, v8s16}, {v2s32, v4s32}})
1281 .immIdx(0); // Inform verifier imm idx 0 is handled.
1282
1283 // TODO: {nxv16s8, s8}, {nxv8s16, s16}
1284 getActionDefinitionsBuilder(G_SPLAT_VECTOR)
1285 .legalFor(HasSVE, {{nxv4s32, s32}, {nxv2s64, s64}});
1286
1287 getActionDefinitionsBuilder(G_JUMP_TABLE).legalFor({p0});
1288
1289 getActionDefinitionsBuilder(G_BRJT).legalFor({{p0, s64}});
1290
1291 getActionDefinitionsBuilder({G_TRAP, G_DEBUGTRAP, G_UBSANTRAP}).alwaysLegal();
1292
1293 getActionDefinitionsBuilder(G_DYN_STACKALLOC).custom();
1294
1295 getActionDefinitionsBuilder({G_STACKSAVE, G_STACKRESTORE}).lower();
1296
1297 if (ST.hasMOPS()) {
1298 // G_BZERO is not supported. Currently it is only emitted by
1299 // PreLegalizerCombiner for G_MEMSET with zero constant.
1301
1303 .legalForCartesianProduct({p0}, {s64}, {s64})
1304 .customForCartesianProduct({p0}, {s8}, {s64})
1305 .immIdx(0); // Inform verifier imm idx 0 is handled.
1306
1307 getActionDefinitionsBuilder({G_MEMCPY, G_MEMMOVE})
1308 .legalForCartesianProduct({p0}, {p0}, {s64})
1309 .immIdx(0); // Inform verifier imm idx 0 is handled.
1310
1311 // G_MEMCPY_INLINE does not have a tailcall immediate
1312 getActionDefinitionsBuilder(G_MEMCPY_INLINE)
1313 .legalForCartesianProduct({p0}, {p0}, {s64});
1314
1315 } else {
1316 getActionDefinitionsBuilder({G_BZERO, G_MEMCPY, G_MEMMOVE, G_MEMSET})
1317 .libcall();
1318 }
1319
1320 // For fadd reductions we have pairwise operations available. We treat the
1321 // usual legal types as legal and handle the lowering to pairwise instructions
1322 // later.
1323 getActionDefinitionsBuilder(G_VECREDUCE_FADD)
1324 .legalFor({{s32, v2s32}, {s32, v4s32}, {s64, v2s64}})
1325 .legalFor(HasFP16, {{s16, v4s16}, {s16, v8s16}})
1326 .minScalarOrElt(0, MinFPScalar)
1327 .clampMaxNumElements(1, s64, 2)
1328 .clampMaxNumElements(1, s32, 4)
1329 .clampMaxNumElements(1, s16, 8)
1331 .scalarize(1)
1332 .lower();
1333
1334 // For fmul reductions we need to split up into individual operations. We
1335 // clamp to 128 bit vectors then to 64bit vectors to produce a cascade of
1336 // smaller types, followed by scalarizing what remains.
1337 getActionDefinitionsBuilder(G_VECREDUCE_FMUL)
1338 .minScalarOrElt(0, MinFPScalar)
1339 .clampMaxNumElements(1, s64, 2)
1340 .clampMaxNumElements(1, s32, 4)
1341 .clampMaxNumElements(1, s16, 8)
1342 .clampMaxNumElements(1, s32, 2)
1343 .clampMaxNumElements(1, s16, 4)
1344 .scalarize(1)
1345 .lower();
1346
1347 getActionDefinitionsBuilder({G_VECREDUCE_SEQ_FADD, G_VECREDUCE_SEQ_FMUL})
1348 .scalarize(2)
1349 .lower();
1350
1351 getActionDefinitionsBuilder(G_VECREDUCE_ADD)
1352 .legalFor({{s8, v8s8},
1353 {s8, v16s8},
1354 {s16, v4s16},
1355 {s16, v8s16},
1356 {s32, v2s32},
1357 {s32, v4s32},
1358 {s64, v2s64}})
1360 .clampMaxNumElements(1, s64, 2)
1361 .clampMaxNumElements(1, s32, 4)
1362 .clampMaxNumElements(1, s16, 8)
1363 .clampMaxNumElements(1, s8, 16)
1365 .scalarize(1);
1366
1367 getActionDefinitionsBuilder({G_VECREDUCE_FMIN, G_VECREDUCE_FMAX,
1368 G_VECREDUCE_FMINIMUM, G_VECREDUCE_FMAXIMUM})
1369 .legalFor({{s32, v2s32}, {s32, v4s32}, {s64, v2s64}})
1370 .legalFor(HasFP16, {{s16, v4s16}, {s16, v8s16}})
1371 .minScalarOrElt(0, MinFPScalar)
1372 .clampMaxNumElements(1, s64, 2)
1373 .clampMaxNumElements(1, s32, 4)
1374 .clampMaxNumElements(1, s16, 8)
1375 .scalarize(1)
1376 .lower();
1377
1378 getActionDefinitionsBuilder(G_VECREDUCE_MUL)
1379 .clampMaxNumElements(1, s32, 2)
1380 .clampMaxNumElements(1, s16, 4)
1381 .clampMaxNumElements(1, s8, 8)
1382 .scalarize(1)
1383 .lower();
1384
1386 {G_VECREDUCE_SMIN, G_VECREDUCE_SMAX, G_VECREDUCE_UMIN, G_VECREDUCE_UMAX})
1387 .legalFor({{s8, v8s8},
1388 {s8, v16s8},
1389 {s16, v4s16},
1390 {s16, v8s16},
1391 {s32, v2s32},
1392 {s32, v4s32}})
1393 .moreElementsIf(
1394 [=](const LegalityQuery &Query) {
1395 return Query.Types[1].isVector() &&
1396 Query.Types[1].getElementType() != s8 &&
1397 Query.Types[1].getNumElements() & 1;
1398 },
1400 .clampMaxNumElements(1, s64, 2)
1401 .clampMaxNumElements(1, s32, 4)
1402 .clampMaxNumElements(1, s16, 8)
1403 .clampMaxNumElements(1, s8, 16)
1404 .scalarize(1)
1405 .lower();
1406
1408 {G_VECREDUCE_OR, G_VECREDUCE_AND, G_VECREDUCE_XOR})
1409 // Try to break down into smaller vectors as long as they're at least 64
1410 // bits. This lets us use vector operations for some parts of the
1411 // reduction.
1412 .fewerElementsIf(
1413 [=](const LegalityQuery &Q) {
1414 LLT SrcTy = Q.Types[1];
1415 if (SrcTy.isScalar())
1416 return false;
1417 if (!isPowerOf2_32(SrcTy.getNumElements()))
1418 return false;
1419 // We can usually perform 64b vector operations.
1420 return SrcTy.getSizeInBits() > 64;
1421 },
1422 [=](const LegalityQuery &Q) {
1423 LLT SrcTy = Q.Types[1];
1424 return std::make_pair(1, SrcTy.divide(2));
1425 })
1426 .scalarize(1)
1427 .lower();
1428
1429 // TODO: Update this to correct handling when adding AArch64/SVE support.
1430 getActionDefinitionsBuilder(G_VECTOR_COMPRESS).lower();
1431
1432 // Access to floating-point environment.
1433 getActionDefinitionsBuilder({G_GET_FPENV, G_SET_FPENV, G_RESET_FPENV,
1434 G_GET_FPMODE, G_SET_FPMODE, G_RESET_FPMODE})
1435 .libcall();
1436
1437 getActionDefinitionsBuilder(G_IS_FPCLASS).lower();
1438
1439 getActionDefinitionsBuilder(G_PREFETCH).custom();
1440
1441 getActionDefinitionsBuilder({G_SCMP, G_UCMP}).lower();
1442
1444 verify(*ST.getInstrInfo());
1445}
1446
1449 LostDebugLocObserver &LocObserver) const {
1450 MachineIRBuilder &MIRBuilder = Helper.MIRBuilder;
1451 MachineRegisterInfo &MRI = *MIRBuilder.getMRI();
1452 GISelChangeObserver &Observer = Helper.Observer;
1453 switch (MI.getOpcode()) {
1454 default:
1455 // No idea what to do.
1456 return false;
1457 case TargetOpcode::G_VAARG:
1458 return legalizeVaArg(MI, MRI, MIRBuilder);
1459 case TargetOpcode::G_LOAD:
1460 case TargetOpcode::G_STORE:
1461 return legalizeLoadStore(MI, MRI, MIRBuilder, Observer);
1462 case TargetOpcode::G_SHL:
1463 case TargetOpcode::G_ASHR:
1464 case TargetOpcode::G_LSHR:
1465 return legalizeShlAshrLshr(MI, MRI, MIRBuilder, Observer);
1466 case TargetOpcode::G_GLOBAL_VALUE:
1467 return legalizeSmallCMGlobalValue(MI, MRI, MIRBuilder, Observer);
1468 case TargetOpcode::G_SBFX:
1469 case TargetOpcode::G_UBFX:
1470 return legalizeBitfieldExtract(MI, MRI, Helper);
1471 case TargetOpcode::G_FSHL:
1472 case TargetOpcode::G_FSHR:
1473 return legalizeFunnelShift(MI, MRI, MIRBuilder, Observer, Helper);
1474 case TargetOpcode::G_ROTR:
1475 return legalizeRotate(MI, MRI, Helper);
1476 case TargetOpcode::G_CTPOP:
1477 return legalizeCTPOP(MI, MRI, Helper);
1478 case TargetOpcode::G_ATOMIC_CMPXCHG:
1479 return legalizeAtomicCmpxchg128(MI, MRI, Helper);
1480 case TargetOpcode::G_CTTZ:
1481 return legalizeCTTZ(MI, Helper);
1482 case TargetOpcode::G_BZERO:
1483 case TargetOpcode::G_MEMCPY:
1484 case TargetOpcode::G_MEMMOVE:
1485 case TargetOpcode::G_MEMSET:
1486 return legalizeMemOps(MI, Helper);
1487 case TargetOpcode::G_EXTRACT_VECTOR_ELT:
1488 return legalizeExtractVectorElt(MI, MRI, Helper);
1489 case TargetOpcode::G_DYN_STACKALLOC:
1490 return legalizeDynStackAlloc(MI, Helper);
1491 case TargetOpcode::G_PREFETCH:
1492 return legalizePrefetch(MI, Helper);
1493 case TargetOpcode::G_ABS:
1494 return Helper.lowerAbsToCNeg(MI);
1495 case TargetOpcode::G_ICMP:
1496 return legalizeICMP(MI, MRI, MIRBuilder);
1497 case TargetOpcode::G_BITCAST:
1498 return legalizeBitcast(MI, Helper);
1499 case TargetOpcode::G_FPTRUNC:
1500 // In order to lower f16 to f64 properly, we need to use f32 as an
1501 // intermediary
1502 return legalizeFptrunc(MI, MIRBuilder, MRI);
1503 }
1504
1505 llvm_unreachable("expected switch to return");
1506}
1507
1508bool AArch64LegalizerInfo::legalizeBitcast(MachineInstr &MI,
1509 LegalizerHelper &Helper) const {
1510 assert(MI.getOpcode() == TargetOpcode::G_BITCAST && "Unexpected opcode");
1511 auto [DstReg, DstTy, SrcReg, SrcTy] = MI.getFirst2RegLLTs();
1512 // We're trying to handle casts from i1 vectors to scalars but reloading from
1513 // stack.
1514 if (!DstTy.isScalar() || !SrcTy.isVector() ||
1515 SrcTy.getElementType() != LLT::scalar(1))
1516 return false;
1517
1518 Helper.createStackStoreLoad(DstReg, SrcReg);
1519 MI.eraseFromParent();
1520 return true;
1521}
1522
1523bool AArch64LegalizerInfo::legalizeFunnelShift(MachineInstr &MI,
1525 MachineIRBuilder &MIRBuilder,
1526 GISelChangeObserver &Observer,
1527 LegalizerHelper &Helper) const {
1528 assert(MI.getOpcode() == TargetOpcode::G_FSHL ||
1529 MI.getOpcode() == TargetOpcode::G_FSHR);
1530
1531 // Keep as G_FSHR if shift amount is a G_CONSTANT, else use generic
1532 // lowering
1533 Register ShiftNo = MI.getOperand(3).getReg();
1534 LLT ShiftTy = MRI.getType(ShiftNo);
1535 auto VRegAndVal = getIConstantVRegValWithLookThrough(ShiftNo, MRI);
1536
1537 // Adjust shift amount according to Opcode (FSHL/FSHR)
1538 // Convert FSHL to FSHR
1539 LLT OperationTy = MRI.getType(MI.getOperand(0).getReg());
1540 APInt BitWidth(ShiftTy.getSizeInBits(), OperationTy.getSizeInBits(), false);
1541
1542 // Lower non-constant shifts and leave zero shifts to the optimizer.
1543 if (!VRegAndVal || VRegAndVal->Value.urem(BitWidth) == 0)
1544 return (Helper.lowerFunnelShiftAsShifts(MI) ==
1546
1547 APInt Amount = VRegAndVal->Value.urem(BitWidth);
1548
1549 Amount = MI.getOpcode() == TargetOpcode::G_FSHL ? BitWidth - Amount : Amount;
1550
1551 // If the instruction is G_FSHR, has a 64-bit G_CONSTANT for shift amount
1552 // in the range of 0 <-> BitWidth, it is legal
1553 if (ShiftTy.getSizeInBits() == 64 && MI.getOpcode() == TargetOpcode::G_FSHR &&
1554 VRegAndVal->Value.ult(BitWidth))
1555 return true;
1556
1557 // Cast the ShiftNumber to a 64-bit type
1558 auto Cast64 = MIRBuilder.buildConstant(LLT::scalar(64), Amount.zext(64));
1559
1560 if (MI.getOpcode() == TargetOpcode::G_FSHR) {
1561 Observer.changingInstr(MI);
1562 MI.getOperand(3).setReg(Cast64.getReg(0));
1563 Observer.changedInstr(MI);
1564 }
1565 // If Opcode is FSHL, remove the FSHL instruction and create a FSHR
1566 // instruction
1567 else if (MI.getOpcode() == TargetOpcode::G_FSHL) {
1568 MIRBuilder.buildInstr(TargetOpcode::G_FSHR, {MI.getOperand(0).getReg()},
1569 {MI.getOperand(1).getReg(), MI.getOperand(2).getReg(),
1570 Cast64.getReg(0)});
1571 MI.eraseFromParent();
1572 }
1573 return true;
1574}
1575
1576bool AArch64LegalizerInfo::legalizeICMP(MachineInstr &MI,
1578 MachineIRBuilder &MIRBuilder) const {
1579 Register DstReg = MI.getOperand(0).getReg();
1580 Register SrcReg1 = MI.getOperand(2).getReg();
1581 Register SrcReg2 = MI.getOperand(3).getReg();
1582 LLT DstTy = MRI.getType(DstReg);
1583 LLT SrcTy = MRI.getType(SrcReg1);
1584
1585 // Check the vector types are legal
1586 if (DstTy.getScalarSizeInBits() != SrcTy.getScalarSizeInBits() ||
1587 DstTy.getNumElements() != SrcTy.getNumElements() ||
1588 (DstTy.getSizeInBits() != 64 && DstTy.getSizeInBits() != 128))
1589 return false;
1590
1591 // Lowers G_ICMP NE => G_ICMP EQ to allow better pattern matching for
1592 // following passes
1593 CmpInst::Predicate Pred = (CmpInst::Predicate)MI.getOperand(1).getPredicate();
1594 if (Pred != CmpInst::ICMP_NE)
1595 return true;
1596 Register CmpReg =
1597 MIRBuilder
1598 .buildICmp(CmpInst::ICMP_EQ, MRI.getType(DstReg), SrcReg1, SrcReg2)
1599 .getReg(0);
1600 MIRBuilder.buildNot(DstReg, CmpReg);
1601
1602 MI.eraseFromParent();
1603 return true;
1604}
1605
1606bool AArch64LegalizerInfo::legalizeRotate(MachineInstr &MI,
1608 LegalizerHelper &Helper) const {
1609 // To allow for imported patterns to match, we ensure that the rotate amount
1610 // is 64b with an extension.
1611 Register AmtReg = MI.getOperand(2).getReg();
1612 LLT AmtTy = MRI.getType(AmtReg);
1613 (void)AmtTy;
1614 assert(AmtTy.isScalar() && "Expected a scalar rotate");
1615 assert(AmtTy.getSizeInBits() < 64 && "Expected this rotate to be legal");
1616 auto NewAmt = Helper.MIRBuilder.buildZExt(LLT::scalar(64), AmtReg);
1617 Helper.Observer.changingInstr(MI);
1618 MI.getOperand(2).setReg(NewAmt.getReg(0));
1619 Helper.Observer.changedInstr(MI);
1620 return true;
1621}
1622
1623bool AArch64LegalizerInfo::legalizeSmallCMGlobalValue(
1625 GISelChangeObserver &Observer) const {
1626 assert(MI.getOpcode() == TargetOpcode::G_GLOBAL_VALUE);
1627 // We do this custom legalization to convert G_GLOBAL_VALUE into target ADRP +
1628 // G_ADD_LOW instructions.
1629 // By splitting this here, we can optimize accesses in the small code model by
1630 // folding in the G_ADD_LOW into the load/store offset.
1631 auto &GlobalOp = MI.getOperand(1);
1632 // Don't modify an intrinsic call.
1633 if (GlobalOp.isSymbol())
1634 return true;
1635 const auto* GV = GlobalOp.getGlobal();
1636 if (GV->isThreadLocal())
1637 return true; // Don't want to modify TLS vars.
1638
1639 auto &TM = ST->getTargetLowering()->getTargetMachine();
1640 unsigned OpFlags = ST->ClassifyGlobalReference(GV, TM);
1641
1642 if (OpFlags & AArch64II::MO_GOT)
1643 return true;
1644
1645 auto Offset = GlobalOp.getOffset();
1646 Register DstReg = MI.getOperand(0).getReg();
1647 auto ADRP = MIRBuilder.buildInstr(AArch64::ADRP, {LLT::pointer(0, 64)}, {})
1648 .addGlobalAddress(GV, Offset, OpFlags | AArch64II::MO_PAGE);
1649 // Set the regclass on the dest reg too.
1650 MRI.setRegClass(ADRP.getReg(0), &AArch64::GPR64RegClass);
1651
1652 // MO_TAGGED on the page indicates a tagged address. Set the tag now. We do so
1653 // by creating a MOVK that sets bits 48-63 of the register to (global address
1654 // + 0x100000000 - PC) >> 48. The additional 0x100000000 offset here is to
1655 // prevent an incorrect tag being generated during relocation when the
1656 // global appears before the code section. Without the offset, a global at
1657 // `0x0f00'0000'0000'1000` (i.e. at `0x1000` with tag `0xf`) that's referenced
1658 // by code at `0x2000` would result in `0x0f00'0000'0000'1000 - 0x2000 =
1659 // 0x0eff'ffff'ffff'f000`, meaning the tag would be incorrectly set to `0xe`
1660 // instead of `0xf`.
1661 // This assumes that we're in the small code model so we can assume a binary
1662 // size of <= 4GB, which makes the untagged PC relative offset positive. The
1663 // binary must also be loaded into address range [0, 2^48). Both of these
1664 // properties need to be ensured at runtime when using tagged addresses.
1665 if (OpFlags & AArch64II::MO_TAGGED) {
1666 assert(!Offset &&
1667 "Should not have folded in an offset for a tagged global!");
1668 ADRP = MIRBuilder.buildInstr(AArch64::MOVKXi, {LLT::pointer(0, 64)}, {ADRP})
1669 .addGlobalAddress(GV, 0x100000000,
1671 .addImm(48);
1672 MRI.setRegClass(ADRP.getReg(0), &AArch64::GPR64RegClass);
1673 }
1674
1675 MIRBuilder.buildInstr(AArch64::G_ADD_LOW, {DstReg}, {ADRP})
1676 .addGlobalAddress(GV, Offset,
1678 MI.eraseFromParent();
1679 return true;
1680}
1681
1683 MachineInstr &MI) const {
1684 MachineIRBuilder &MIB = Helper.MIRBuilder;
1685 MachineRegisterInfo &MRI = *MIB.getMRI();
1686
1687 auto LowerUnaryOp = [&MI, &MIB](unsigned Opcode) {
1688 MIB.buildInstr(Opcode, {MI.getOperand(0)}, {MI.getOperand(2)});
1689 MI.eraseFromParent();
1690 return true;
1691 };
1692 auto LowerBinOp = [&MI, &MIB](unsigned Opcode) {
1693 MIB.buildInstr(Opcode, {MI.getOperand(0)},
1694 {MI.getOperand(2), MI.getOperand(3)});
1695 MI.eraseFromParent();
1696 return true;
1697 };
1698 auto LowerTriOp = [&MI, &MIB](unsigned Opcode) {
1699 MIB.buildInstr(Opcode, {MI.getOperand(0)},
1700 {MI.getOperand(2), MI.getOperand(3), MI.getOperand(4)});
1701 MI.eraseFromParent();
1702 return true;
1703 };
1704
1705 Intrinsic::ID IntrinsicID = cast<GIntrinsic>(MI).getIntrinsicID();
1706 switch (IntrinsicID) {
1707 case Intrinsic::vacopy: {
1708 unsigned PtrSize = ST->isTargetILP32() ? 4 : 8;
1709 unsigned VaListSize =
1710 (ST->isTargetDarwin() || ST->isTargetWindows())
1711 ? PtrSize
1712 : ST->isTargetILP32() ? 20 : 32;
1713
1714 MachineFunction &MF = *MI.getMF();
1716 LLT::scalar(VaListSize * 8));
1717 MIB.buildLoad(Val, MI.getOperand(2),
1720 VaListSize, Align(PtrSize)));
1721 MIB.buildStore(Val, MI.getOperand(1),
1724 VaListSize, Align(PtrSize)));
1725 MI.eraseFromParent();
1726 return true;
1727 }
1728 case Intrinsic::get_dynamic_area_offset: {
1729 MIB.buildConstant(MI.getOperand(0).getReg(), 0);
1730 MI.eraseFromParent();
1731 return true;
1732 }
1733 case Intrinsic::aarch64_mops_memset_tag: {
1734 assert(MI.getOpcode() == TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS);
1735 // Anyext the value being set to 64 bit (only the bottom 8 bits are read by
1736 // the instruction).
1737 auto &Value = MI.getOperand(3);
1738 Register ExtValueReg = MIB.buildAnyExt(LLT::scalar(64), Value).getReg(0);
1739 Value.setReg(ExtValueReg);
1740 return true;
1741 }
1742 case Intrinsic::aarch64_prefetch: {
1743 auto &AddrVal = MI.getOperand(1);
1744
1745 int64_t IsWrite = MI.getOperand(2).getImm();
1746 int64_t Target = MI.getOperand(3).getImm();
1747 int64_t IsStream = MI.getOperand(4).getImm();
1748 int64_t IsData = MI.getOperand(5).getImm();
1749
1750 unsigned PrfOp = (IsWrite << 4) | // Load/Store bit
1751 (!IsData << 3) | // IsDataCache bit
1752 (Target << 1) | // Cache level bits
1753 (unsigned)IsStream; // Stream bit
1754
1755 MIB.buildInstr(AArch64::G_AARCH64_PREFETCH).addImm(PrfOp).add(AddrVal);
1756 MI.eraseFromParent();
1757 return true;
1758 }
1759 case Intrinsic::aarch64_range_prefetch: {
1760 auto &AddrVal = MI.getOperand(1);
1761
1762 int64_t IsWrite = MI.getOperand(2).getImm();
1763 int64_t IsStream = MI.getOperand(3).getImm();
1764 unsigned PrfOp = (IsStream << 2) | IsWrite;
1765
1766 MIB.buildInstr(AArch64::G_AARCH64_RANGE_PREFETCH)
1767 .addImm(PrfOp)
1768 .add(AddrVal)
1769 .addUse(MI.getOperand(4).getReg()); // Metadata
1770 MI.eraseFromParent();
1771 return true;
1772 }
1773 case Intrinsic::aarch64_prefetch_ir: {
1774 auto &AddrVal = MI.getOperand(1);
1775 MIB.buildInstr(AArch64::G_AARCH64_PREFETCH).addImm(24).add(AddrVal);
1776 MI.eraseFromParent();
1777 return true;
1778 }
1779 case Intrinsic::aarch64_neon_uaddv:
1780 case Intrinsic::aarch64_neon_saddv:
1781 case Intrinsic::aarch64_neon_umaxv:
1782 case Intrinsic::aarch64_neon_smaxv:
1783 case Intrinsic::aarch64_neon_uminv:
1784 case Intrinsic::aarch64_neon_sminv: {
1785 bool IsSigned = IntrinsicID == Intrinsic::aarch64_neon_saddv ||
1786 IntrinsicID == Intrinsic::aarch64_neon_smaxv ||
1787 IntrinsicID == Intrinsic::aarch64_neon_sminv;
1788
1789 auto OldDst = MI.getOperand(0).getReg();
1790 auto OldDstTy = MRI.getType(OldDst);
1791 LLT NewDstTy = MRI.getType(MI.getOperand(2).getReg()).getElementType();
1792 if (OldDstTy == NewDstTy)
1793 return true;
1794
1795 auto NewDst = MRI.createGenericVirtualRegister(NewDstTy);
1796
1797 Helper.Observer.changingInstr(MI);
1798 MI.getOperand(0).setReg(NewDst);
1799 Helper.Observer.changedInstr(MI);
1800
1801 MIB.setInsertPt(MIB.getMBB(), ++MIB.getInsertPt());
1802 MIB.buildExtOrTrunc(IsSigned ? TargetOpcode::G_SEXT : TargetOpcode::G_ZEXT,
1803 OldDst, NewDst);
1804
1805 return true;
1806 }
1807 case Intrinsic::aarch64_neon_uaddlp:
1808 case Intrinsic::aarch64_neon_saddlp: {
1809 unsigned Opc = IntrinsicID == Intrinsic::aarch64_neon_uaddlp
1810 ? AArch64::G_UADDLP
1811 : AArch64::G_SADDLP;
1812 MIB.buildInstr(Opc, {MI.getOperand(0)}, {MI.getOperand(2)});
1813 MI.eraseFromParent();
1814
1815 return true;
1816 }
1817 case Intrinsic::aarch64_neon_uaddlv:
1818 case Intrinsic::aarch64_neon_saddlv: {
1819 unsigned Opc = IntrinsicID == Intrinsic::aarch64_neon_uaddlv
1820 ? AArch64::G_UADDLV
1821 : AArch64::G_SADDLV;
1822 Register DstReg = MI.getOperand(0).getReg();
1823 Register SrcReg = MI.getOperand(2).getReg();
1824 LLT DstTy = MRI.getType(DstReg);
1825
1826 LLT MidTy, ExtTy;
1827 if (DstTy.isScalar() && DstTy.getScalarSizeInBits() <= 32) {
1828 MidTy = LLT::fixed_vector(4, 32);
1829 ExtTy = LLT::scalar(32);
1830 } else {
1831 MidTy = LLT::fixed_vector(2, 64);
1832 ExtTy = LLT::scalar(64);
1833 }
1834
1835 Register MidReg =
1836 MIB.buildInstr(Opc, {MidTy}, {SrcReg})->getOperand(0).getReg();
1837 Register ZeroReg =
1838 MIB.buildConstant(LLT::scalar(64), 0)->getOperand(0).getReg();
1839 Register ExtReg = MIB.buildInstr(AArch64::G_EXTRACT_VECTOR_ELT, {ExtTy},
1840 {MidReg, ZeroReg})
1841 .getReg(0);
1842
1843 if (DstTy.getScalarSizeInBits() < 32)
1844 MIB.buildTrunc(DstReg, ExtReg);
1845 else
1846 MIB.buildCopy(DstReg, ExtReg);
1847
1848 MI.eraseFromParent();
1849
1850 return true;
1851 }
1852 case Intrinsic::aarch64_neon_smax:
1853 return LowerBinOp(TargetOpcode::G_SMAX);
1854 case Intrinsic::aarch64_neon_smin:
1855 return LowerBinOp(TargetOpcode::G_SMIN);
1856 case Intrinsic::aarch64_neon_umax:
1857 return LowerBinOp(TargetOpcode::G_UMAX);
1858 case Intrinsic::aarch64_neon_umin:
1859 return LowerBinOp(TargetOpcode::G_UMIN);
1860 case Intrinsic::aarch64_neon_fmax:
1861 return LowerBinOp(TargetOpcode::G_FMAXIMUM);
1862 case Intrinsic::aarch64_neon_fmin:
1863 return LowerBinOp(TargetOpcode::G_FMINIMUM);
1864 case Intrinsic::aarch64_neon_fmaxnm:
1865 return LowerBinOp(TargetOpcode::G_FMAXNUM);
1866 case Intrinsic::aarch64_neon_fminnm:
1867 return LowerBinOp(TargetOpcode::G_FMINNUM);
1868 case Intrinsic::aarch64_neon_pmull:
1869 case Intrinsic::aarch64_neon_pmull64:
1870 return LowerBinOp(AArch64::G_PMULL);
1871 case Intrinsic::aarch64_neon_smull:
1872 return LowerBinOp(AArch64::G_SMULL);
1873 case Intrinsic::aarch64_neon_umull:
1874 return LowerBinOp(AArch64::G_UMULL);
1875 case Intrinsic::aarch64_neon_sabd:
1876 return LowerBinOp(TargetOpcode::G_ABDS);
1877 case Intrinsic::aarch64_neon_uabd:
1878 return LowerBinOp(TargetOpcode::G_ABDU);
1879 case Intrinsic::aarch64_neon_uhadd:
1880 return LowerBinOp(TargetOpcode::G_UAVGFLOOR);
1881 case Intrinsic::aarch64_neon_urhadd:
1882 return LowerBinOp(TargetOpcode::G_UAVGCEIL);
1883 case Intrinsic::aarch64_neon_shadd:
1884 return LowerBinOp(TargetOpcode::G_SAVGFLOOR);
1885 case Intrinsic::aarch64_neon_srhadd:
1886 return LowerBinOp(TargetOpcode::G_SAVGCEIL);
1887 case Intrinsic::aarch64_neon_sqshrn: {
1888 if (!MRI.getType(MI.getOperand(0).getReg()).isVector())
1889 return true;
1890 // Create right shift instruction. Store the output register in Shr.
1891 auto Shr = MIB.buildInstr(AArch64::G_VASHR,
1892 {MRI.getType(MI.getOperand(2).getReg())},
1893 {MI.getOperand(2), MI.getOperand(3).getImm()});
1894 // Build the narrow intrinsic, taking in Shr.
1895 MIB.buildInstr(TargetOpcode::G_TRUNC_SSAT_S, {MI.getOperand(0)}, {Shr});
1896 MI.eraseFromParent();
1897 return true;
1898 }
1899 case Intrinsic::aarch64_neon_sqshrun: {
1900 if (!MRI.getType(MI.getOperand(0).getReg()).isVector())
1901 return true;
1902 // Create right shift instruction. Store the output register in Shr.
1903 auto Shr = MIB.buildInstr(AArch64::G_VASHR,
1904 {MRI.getType(MI.getOperand(2).getReg())},
1905 {MI.getOperand(2), MI.getOperand(3).getImm()});
1906 // Build the narrow intrinsic, taking in Shr.
1907 MIB.buildInstr(TargetOpcode::G_TRUNC_SSAT_U, {MI.getOperand(0)}, {Shr});
1908 MI.eraseFromParent();
1909 return true;
1910 }
1911 case Intrinsic::aarch64_neon_sqrshrn: {
1912 if (!MRI.getType(MI.getOperand(0).getReg()).isVector())
1913 return true;
1914 // Create right shift instruction. Store the output register in Shr.
1915 auto Shr = MIB.buildInstr(AArch64::G_SRSHR_I,
1916 {MRI.getType(MI.getOperand(2).getReg())},
1917 {MI.getOperand(2), MI.getOperand(3).getImm()});
1918 // Build the narrow intrinsic, taking in Shr.
1919 MIB.buildInstr(TargetOpcode::G_TRUNC_SSAT_S, {MI.getOperand(0)}, {Shr});
1920 MI.eraseFromParent();
1921 return true;
1922 }
1923 case Intrinsic::aarch64_neon_sqrshrun: {
1924 if (!MRI.getType(MI.getOperand(0).getReg()).isVector())
1925 return true;
1926 // Create right shift instruction. Store the output register in Shr.
1927 auto Shr = MIB.buildInstr(AArch64::G_SRSHR_I,
1928 {MRI.getType(MI.getOperand(2).getReg())},
1929 {MI.getOperand(2), MI.getOperand(3).getImm()});
1930 // Build the narrow intrinsic, taking in Shr.
1931 MIB.buildInstr(TargetOpcode::G_TRUNC_SSAT_U, {MI.getOperand(0)}, {Shr});
1932 MI.eraseFromParent();
1933 return true;
1934 }
1935 case Intrinsic::aarch64_neon_uqrshrn: {
1936 if (!MRI.getType(MI.getOperand(0).getReg()).isVector())
1937 return true;
1938 // Create right shift instruction. Store the output register in Shr.
1939 auto Shr = MIB.buildInstr(AArch64::G_URSHR_I,
1940 {MRI.getType(MI.getOperand(2).getReg())},
1941 {MI.getOperand(2), MI.getOperand(3).getImm()});
1942 // Build the narrow intrinsic, taking in Shr.
1943 MIB.buildInstr(TargetOpcode::G_TRUNC_USAT_U, {MI.getOperand(0)}, {Shr});
1944 MI.eraseFromParent();
1945 return true;
1946 }
1947 case Intrinsic::aarch64_neon_uqshrn: {
1948 if (!MRI.getType(MI.getOperand(0).getReg()).isVector())
1949 return true;
1950 // Create right shift instruction. Store the output register in Shr.
1951 auto Shr = MIB.buildInstr(AArch64::G_VLSHR,
1952 {MRI.getType(MI.getOperand(2).getReg())},
1953 {MI.getOperand(2), MI.getOperand(3).getImm()});
1954 // Build the narrow intrinsic, taking in Shr.
1955 MIB.buildInstr(TargetOpcode::G_TRUNC_USAT_U, {MI.getOperand(0)}, {Shr});
1956 MI.eraseFromParent();
1957 return true;
1958 }
1959 case Intrinsic::aarch64_neon_sqshlu: {
1960 // Check if last operand is constant vector dup
1961 auto ShiftAmount = isConstantOrConstantSplatVector(
1962 *MRI.getVRegDef(MI.getOperand(3).getReg()), MRI);
1963 if (ShiftAmount) {
1964 // If so, create a new intrinsic with the correct shift amount
1965 MIB.buildInstr(AArch64::G_SQSHLU_I, {MI.getOperand(0)},
1966 {MI.getOperand(2)})
1967 .addImm(ShiftAmount->getSExtValue());
1968 MI.eraseFromParent();
1969 return true;
1970 }
1971 return false;
1972 }
1973 case Intrinsic::aarch64_neon_vsli: {
1974 MIB.buildInstr(
1975 AArch64::G_SLI, {MI.getOperand(0)},
1976 {MI.getOperand(2), MI.getOperand(3), MI.getOperand(4).getImm()});
1977 MI.eraseFromParent();
1978 break;
1979 }
1980 case Intrinsic::aarch64_neon_vsri: {
1981 MIB.buildInstr(
1982 AArch64::G_SRI, {MI.getOperand(0)},
1983 {MI.getOperand(2), MI.getOperand(3), MI.getOperand(4).getImm()});
1984 MI.eraseFromParent();
1985 break;
1986 }
1987 case Intrinsic::aarch64_neon_abs: {
1988 // Lower the intrinsic to G_ABS.
1989 MIB.buildInstr(TargetOpcode::G_ABS, {MI.getOperand(0)}, {MI.getOperand(2)});
1990 MI.eraseFromParent();
1991 return true;
1992 }
1993 case Intrinsic::aarch64_neon_sqadd: {
1994 if (MRI.getType(MI.getOperand(0).getReg()).isVector())
1995 return LowerBinOp(TargetOpcode::G_SADDSAT);
1996 break;
1997 }
1998 case Intrinsic::aarch64_neon_sqsub: {
1999 if (MRI.getType(MI.getOperand(0).getReg()).isVector())
2000 return LowerBinOp(TargetOpcode::G_SSUBSAT);
2001 break;
2002 }
2003 case Intrinsic::aarch64_neon_uqadd: {
2004 if (MRI.getType(MI.getOperand(0).getReg()).isVector())
2005 return LowerBinOp(TargetOpcode::G_UADDSAT);
2006 break;
2007 }
2008 case Intrinsic::aarch64_neon_uqsub: {
2009 if (MRI.getType(MI.getOperand(0).getReg()).isVector())
2010 return LowerBinOp(TargetOpcode::G_USUBSAT);
2011 break;
2012 }
2013 case Intrinsic::aarch64_neon_udot:
2014 return LowerTriOp(AArch64::G_UDOT);
2015 case Intrinsic::aarch64_neon_sdot:
2016 return LowerTriOp(AArch64::G_SDOT);
2017 case Intrinsic::aarch64_neon_usdot:
2018 return LowerTriOp(AArch64::G_USDOT);
2019 case Intrinsic::aarch64_neon_sqxtn:
2020 return LowerUnaryOp(TargetOpcode::G_TRUNC_SSAT_S);
2021 case Intrinsic::aarch64_neon_sqxtun:
2022 return LowerUnaryOp(TargetOpcode::G_TRUNC_SSAT_U);
2023 case Intrinsic::aarch64_neon_uqxtn:
2024 return LowerUnaryOp(TargetOpcode::G_TRUNC_USAT_U);
2025 case Intrinsic::aarch64_neon_fcvtzu:
2026 return LowerUnaryOp(TargetOpcode::G_FPTOUI_SAT);
2027 case Intrinsic::aarch64_neon_fcvtzs:
2028 return LowerUnaryOp(TargetOpcode::G_FPTOSI_SAT);
2029
2030 case Intrinsic::vector_reverse:
2031 // TODO: Add support for vector_reverse
2032 return false;
2033 }
2034
2035 return true;
2036}
2037
2038bool AArch64LegalizerInfo::legalizeShlAshrLshr(
2040 GISelChangeObserver &Observer) const {
2041 assert(MI.getOpcode() == TargetOpcode::G_ASHR ||
2042 MI.getOpcode() == TargetOpcode::G_LSHR ||
2043 MI.getOpcode() == TargetOpcode::G_SHL);
2044 // If the shift amount is a G_CONSTANT, promote it to a 64 bit type so the
2045 // imported patterns can select it later. Either way, it will be legal.
2046 Register AmtReg = MI.getOperand(2).getReg();
2047 auto VRegAndVal = getIConstantVRegValWithLookThrough(AmtReg, MRI);
2048 if (!VRegAndVal)
2049 return true;
2050 // Check the shift amount is in range for an immediate form.
2051 int64_t Amount = VRegAndVal->Value.getSExtValue();
2052 if (Amount > 31)
2053 return true; // This will have to remain a register variant.
2054 auto ExtCst = MIRBuilder.buildConstant(LLT::scalar(64), Amount);
2055 Observer.changingInstr(MI);
2056 MI.getOperand(2).setReg(ExtCst.getReg(0));
2057 Observer.changedInstr(MI);
2058 return true;
2059}
2060
2063 Base = Root;
2064 Offset = 0;
2065
2066 Register NewBase;
2067 int64_t NewOffset;
2068 if (mi_match(Root, MRI, m_GPtrAdd(m_Reg(NewBase), m_ICst(NewOffset))) &&
2069 isShiftedInt<7, 3>(NewOffset)) {
2070 Base = NewBase;
2071 Offset = NewOffset;
2072 }
2073}
2074
2075// FIXME: This should be removed and replaced with the generic bitcast legalize
2076// action.
2077bool AArch64LegalizerInfo::legalizeLoadStore(
2079 GISelChangeObserver &Observer) const {
2080 assert(MI.getOpcode() == TargetOpcode::G_STORE ||
2081 MI.getOpcode() == TargetOpcode::G_LOAD);
2082 // Here we just try to handle vector loads/stores where our value type might
2083 // have pointer elements, which the SelectionDAG importer can't handle. To
2084 // allow the existing patterns for s64 to fire for p0, we just try to bitcast
2085 // the value to use s64 types.
2086
2087 // Custom legalization requires the instruction, if not deleted, must be fully
2088 // legalized. In order to allow further legalization of the inst, we create
2089 // a new instruction and erase the existing one.
2090
2091 Register ValReg = MI.getOperand(0).getReg();
2092 const LLT ValTy = MRI.getType(ValReg);
2093
2094 if (ValTy == LLT::scalar(128)) {
2095
2096 AtomicOrdering Ordering = (*MI.memoperands_begin())->getSuccessOrdering();
2097 bool IsLoad = MI.getOpcode() == TargetOpcode::G_LOAD;
2098 bool IsLoadAcquire = IsLoad && Ordering == AtomicOrdering::Acquire;
2099 bool IsStoreRelease = !IsLoad && Ordering == AtomicOrdering::Release;
2100 bool IsRcpC3 =
2101 ST->hasLSE2() && ST->hasRCPC3() && (IsLoadAcquire || IsStoreRelease);
2102
2103 LLT s64 = LLT::scalar(64);
2104
2105 unsigned Opcode;
2106 if (IsRcpC3) {
2107 Opcode = IsLoad ? AArch64::LDIAPPX : AArch64::STILPX;
2108 } else {
2109 // For LSE2, loads/stores should have been converted to monotonic and had
2110 // a fence inserted after them.
2111 assert(Ordering == AtomicOrdering::Monotonic ||
2112 Ordering == AtomicOrdering::Unordered);
2113 assert(ST->hasLSE2() && "ldp/stp not single copy atomic without +lse2");
2114
2115 Opcode = IsLoad ? AArch64::LDPXi : AArch64::STPXi;
2116 }
2117
2118 MachineInstrBuilder NewI;
2119 if (IsLoad) {
2120 NewI = MIRBuilder.buildInstr(Opcode, {s64, s64}, {});
2121 MIRBuilder.buildMergeLikeInstr(
2122 ValReg, {NewI->getOperand(0), NewI->getOperand(1)});
2123 } else {
2124 auto Split = MIRBuilder.buildUnmerge(s64, MI.getOperand(0));
2125 NewI = MIRBuilder.buildInstr(
2126 Opcode, {}, {Split->getOperand(0), Split->getOperand(1)});
2127 }
2128
2129 if (IsRcpC3) {
2130 NewI.addUse(MI.getOperand(1).getReg());
2131 } else {
2132 Register Base;
2133 int Offset;
2134 matchLDPSTPAddrMode(MI.getOperand(1).getReg(), Base, Offset, MRI);
2135 NewI.addUse(Base);
2136 NewI.addImm(Offset / 8);
2137 }
2138
2139 NewI.cloneMemRefs(MI);
2140 constrainSelectedInstRegOperands(*NewI, *ST->getInstrInfo(),
2141 *MRI.getTargetRegisterInfo(),
2142 *ST->getRegBankInfo());
2143 MI.eraseFromParent();
2144 return true;
2145 }
2146
2147 if (!ValTy.isPointerVector() ||
2148 ValTy.getElementType().getAddressSpace() != 0) {
2149 LLVM_DEBUG(dbgs() << "Tried to do custom legalization on wrong load/store");
2150 return false;
2151 }
2152
2153 unsigned PtrSize = ValTy.getElementType().getSizeInBits();
2154 const LLT NewTy = LLT::vector(ValTy.getElementCount(), PtrSize);
2155 auto &MMO = **MI.memoperands_begin();
2156 MMO.setType(NewTy);
2157
2158 if (MI.getOpcode() == TargetOpcode::G_STORE) {
2159 auto Bitcast = MIRBuilder.buildBitcast(NewTy, ValReg);
2160 MIRBuilder.buildStore(Bitcast.getReg(0), MI.getOperand(1), MMO);
2161 } else {
2162 auto NewLoad = MIRBuilder.buildLoad(NewTy, MI.getOperand(1), MMO);
2163 MIRBuilder.buildBitcast(ValReg, NewLoad);
2164 }
2165 MI.eraseFromParent();
2166 return true;
2167}
2168
2169bool AArch64LegalizerInfo::legalizeVaArg(MachineInstr &MI,
2171 MachineIRBuilder &MIRBuilder) const {
2172 MachineFunction &MF = MIRBuilder.getMF();
2173 Align Alignment(MI.getOperand(2).getImm());
2174 Register Dst = MI.getOperand(0).getReg();
2175 Register ListPtr = MI.getOperand(1).getReg();
2176
2177 LLT PtrTy = MRI.getType(ListPtr);
2178 LLT IntPtrTy = LLT::scalar(PtrTy.getSizeInBits());
2179
2180 const unsigned PtrSize = PtrTy.getSizeInBits() / 8;
2181 const Align PtrAlign = Align(PtrSize);
2182 auto List = MIRBuilder.buildLoad(
2183 PtrTy, ListPtr,
2184 *MF.getMachineMemOperand(MachinePointerInfo(), MachineMemOperand::MOLoad,
2185 PtrTy, PtrAlign));
2186
2187 MachineInstrBuilder DstPtr;
2188 if (Alignment > PtrAlign) {
2189 // Realign the list to the actual required alignment.
2190 auto AlignMinus1 =
2191 MIRBuilder.buildConstant(IntPtrTy, Alignment.value() - 1);
2192 auto ListTmp = MIRBuilder.buildPtrAdd(PtrTy, List, AlignMinus1.getReg(0));
2193 DstPtr = MIRBuilder.buildMaskLowPtrBits(PtrTy, ListTmp, Log2(Alignment));
2194 } else
2195 DstPtr = List;
2196
2197 LLT ValTy = MRI.getType(Dst);
2198 uint64_t ValSize = ValTy.getSizeInBits() / 8;
2199 MIRBuilder.buildLoad(
2200 Dst, DstPtr,
2201 *MF.getMachineMemOperand(MachinePointerInfo(), MachineMemOperand::MOLoad,
2202 ValTy, std::max(Alignment, PtrAlign)));
2203
2204 auto Size = MIRBuilder.buildConstant(IntPtrTy, alignTo(ValSize, PtrAlign));
2205
2206 auto NewList = MIRBuilder.buildPtrAdd(PtrTy, DstPtr, Size.getReg(0));
2207
2208 MIRBuilder.buildStore(NewList, ListPtr,
2209 *MF.getMachineMemOperand(MachinePointerInfo(),
2211 PtrTy, PtrAlign));
2212
2213 MI.eraseFromParent();
2214 return true;
2215}
2216
2217bool AArch64LegalizerInfo::legalizeBitfieldExtract(
2219 // Only legal if we can select immediate forms.
2220 // TODO: Lower this otherwise.
2221 return getIConstantVRegValWithLookThrough(MI.getOperand(2).getReg(), MRI) &&
2222 getIConstantVRegValWithLookThrough(MI.getOperand(3).getReg(), MRI);
2223}
2224
2225bool AArch64LegalizerInfo::legalizeCTPOP(MachineInstr &MI,
2227 LegalizerHelper &Helper) const {
2228 // When there is no integer popcount instruction (FEAT_CSSC isn't available),
2229 // it can be more efficiently lowered to the following sequence that uses
2230 // AdvSIMD registers/instructions as long as the copies to/from the AdvSIMD
2231 // registers are cheap.
2232 // FMOV D0, X0 // copy 64-bit int to vector, high bits zero'd
2233 // CNT V0.8B, V0.8B // 8xbyte pop-counts
2234 // ADDV B0, V0.8B // sum 8xbyte pop-counts
2235 // UMOV X0, V0.B[0] // copy byte result back to integer reg
2236 //
2237 // For 128 bit vector popcounts, we lower to the following sequence:
2238 // cnt.16b v0, v0 // v8s16, v4s32, v2s64
2239 // uaddlp.8h v0, v0 // v8s16, v4s32, v2s64
2240 // uaddlp.4s v0, v0 // v4s32, v2s64
2241 // uaddlp.2d v0, v0 // v2s64
2242 //
2243 // For 64 bit vector popcounts, we lower to the following sequence:
2244 // cnt.8b v0, v0 // v4s16, v2s32
2245 // uaddlp.4h v0, v0 // v4s16, v2s32
2246 // uaddlp.2s v0, v0 // v2s32
2247
2248 MachineIRBuilder &MIRBuilder = Helper.MIRBuilder;
2249 Register Dst = MI.getOperand(0).getReg();
2250 Register Val = MI.getOperand(1).getReg();
2251 LLT Ty = MRI.getType(Val);
2252 unsigned Size = Ty.getSizeInBits();
2253
2254 assert(Ty == MRI.getType(Dst) &&
2255 "Expected src and dst to have the same type!");
2256
2257 if (ST->hasCSSC() && Ty.isScalar() && Size == 128) {
2258 LLT s64 = LLT::scalar(64);
2259
2260 auto Split = MIRBuilder.buildUnmerge(s64, Val);
2261 auto CTPOP1 = MIRBuilder.buildCTPOP(s64, Split->getOperand(0));
2262 auto CTPOP2 = MIRBuilder.buildCTPOP(s64, Split->getOperand(1));
2263 auto Add = MIRBuilder.buildAdd(s64, CTPOP1, CTPOP2);
2264
2265 MIRBuilder.buildZExt(Dst, Add);
2266 MI.eraseFromParent();
2267 return true;
2268 }
2269
2270 if (!ST->hasNEON() ||
2271 MI.getMF()->getFunction().hasFnAttribute(Attribute::NoImplicitFloat)) {
2272 // Use generic lowering when custom lowering is not possible.
2273 return Ty.isScalar() && (Size == 32 || Size == 64) &&
2274 Helper.lowerBitCount(MI) ==
2276 }
2277
2278 // Pre-conditioning: widen Val up to the nearest vector type.
2279 // s32,s64,v4s16,v2s32 -> v8i8
2280 // v8s16,v4s32,v2s64 -> v16i8
2281 LLT VTy = Size == 128 ? LLT::fixed_vector(16, 8) : LLT::fixed_vector(8, 8);
2282 if (Ty.isScalar()) {
2283 assert((Size == 32 || Size == 64 || Size == 128) && "Expected only 32, 64, or 128 bit scalars!");
2284 if (Size == 32) {
2285 Val = MIRBuilder.buildZExt(LLT::scalar(64), Val).getReg(0);
2286 }
2287 }
2288 Val = MIRBuilder.buildBitcast(VTy, Val).getReg(0);
2289
2290 // Count bits in each byte-sized lane.
2291 auto CTPOP = MIRBuilder.buildCTPOP(VTy, Val);
2292
2293 // Sum across lanes.
2294
2295 if (ST->hasDotProd() && Ty.isVector() && Ty.getNumElements() >= 2 &&
2296 Ty.getScalarSizeInBits() != 16) {
2297 LLT Dt = Ty == LLT::fixed_vector(2, 64) ? LLT::fixed_vector(4, 32) : Ty;
2298 auto Zeros = MIRBuilder.buildConstant(Dt, 0);
2299 auto Ones = MIRBuilder.buildConstant(VTy, 1);
2300 MachineInstrBuilder Sum;
2301
2302 if (Ty == LLT::fixed_vector(2, 64)) {
2303 auto UDOT =
2304 MIRBuilder.buildInstr(AArch64::G_UDOT, {Dt}, {Zeros, Ones, CTPOP});
2305 Sum = MIRBuilder.buildInstr(AArch64::G_UADDLP, {Ty}, {UDOT});
2306 } else if (Ty == LLT::fixed_vector(4, 32)) {
2307 Sum = MIRBuilder.buildInstr(AArch64::G_UDOT, {Dt}, {Zeros, Ones, CTPOP});
2308 } else if (Ty == LLT::fixed_vector(2, 32)) {
2309 Sum = MIRBuilder.buildInstr(AArch64::G_UDOT, {Dt}, {Zeros, Ones, CTPOP});
2310 } else {
2311 llvm_unreachable("unexpected vector shape");
2312 }
2313
2314 Sum->getOperand(0).setReg(Dst);
2315 MI.eraseFromParent();
2316 return true;
2317 }
2318
2319 Register HSum = CTPOP.getReg(0);
2320 unsigned Opc;
2321 SmallVector<LLT> HAddTys;
2322 if (Ty.isScalar()) {
2323 Opc = Intrinsic::aarch64_neon_uaddlv;
2324 HAddTys.push_back(LLT::scalar(32));
2325 } else if (Ty == LLT::fixed_vector(8, 16)) {
2326 Opc = Intrinsic::aarch64_neon_uaddlp;
2327 HAddTys.push_back(LLT::fixed_vector(8, 16));
2328 } else if (Ty == LLT::fixed_vector(4, 32)) {
2329 Opc = Intrinsic::aarch64_neon_uaddlp;
2330 HAddTys.push_back(LLT::fixed_vector(8, 16));
2331 HAddTys.push_back(LLT::fixed_vector(4, 32));
2332 } else if (Ty == LLT::fixed_vector(2, 64)) {
2333 Opc = Intrinsic::aarch64_neon_uaddlp;
2334 HAddTys.push_back(LLT::fixed_vector(8, 16));
2335 HAddTys.push_back(LLT::fixed_vector(4, 32));
2336 HAddTys.push_back(LLT::fixed_vector(2, 64));
2337 } else if (Ty == LLT::fixed_vector(4, 16)) {
2338 Opc = Intrinsic::aarch64_neon_uaddlp;
2339 HAddTys.push_back(LLT::fixed_vector(4, 16));
2340 } else if (Ty == LLT::fixed_vector(2, 32)) {
2341 Opc = Intrinsic::aarch64_neon_uaddlp;
2342 HAddTys.push_back(LLT::fixed_vector(4, 16));
2343 HAddTys.push_back(LLT::fixed_vector(2, 32));
2344 } else
2345 llvm_unreachable("unexpected vector shape");
2347 for (LLT HTy : HAddTys) {
2348 UADD = MIRBuilder.buildIntrinsic(Opc, {HTy}).addUse(HSum);
2349 HSum = UADD.getReg(0);
2350 }
2351
2352 // Post-conditioning.
2353 if (Ty.isScalar() && (Size == 64 || Size == 128))
2354 MIRBuilder.buildZExt(Dst, UADD);
2355 else
2356 UADD->getOperand(0).setReg(Dst);
2357 MI.eraseFromParent();
2358 return true;
2359}
2360
2361bool AArch64LegalizerInfo::legalizeAtomicCmpxchg128(
2363 MachineIRBuilder &MIRBuilder = Helper.MIRBuilder;
2364 LLT s64 = LLT::scalar(64);
2365 auto Addr = MI.getOperand(1).getReg();
2366 auto DesiredI = MIRBuilder.buildUnmerge({s64, s64}, MI.getOperand(2));
2367 auto NewI = MIRBuilder.buildUnmerge({s64, s64}, MI.getOperand(3));
2368 auto DstLo = MRI.createGenericVirtualRegister(s64);
2369 auto DstHi = MRI.createGenericVirtualRegister(s64);
2370
2371 MachineInstrBuilder CAS;
2372 if (ST->hasLSE()) {
2373 // We have 128-bit CASP instructions taking XSeqPair registers, which are
2374 // s128. We need the merge/unmerge to bracket the expansion and pair up with
2375 // the rest of the MIR so we must reassemble the extracted registers into a
2376 // 128-bit known-regclass one with code like this:
2377 //
2378 // %in1 = REG_SEQUENCE Lo, Hi ; One for each input
2379 // %out = CASP %in1, ...
2380 // %OldLo = G_EXTRACT %out, 0
2381 // %OldHi = G_EXTRACT %out, 64
2382 auto Ordering = (*MI.memoperands_begin())->getMergedOrdering();
2383 unsigned Opcode;
2384 switch (Ordering) {
2386 Opcode = AArch64::CASPAX;
2387 break;
2389 Opcode = AArch64::CASPLX;
2390 break;
2393 Opcode = AArch64::CASPALX;
2394 break;
2395 default:
2396 Opcode = AArch64::CASPX;
2397 break;
2398 }
2399
2400 LLT s128 = LLT::scalar(128);
2401 auto CASDst = MRI.createGenericVirtualRegister(s128);
2402 auto CASDesired = MRI.createGenericVirtualRegister(s128);
2403 auto CASNew = MRI.createGenericVirtualRegister(s128);
2404 MIRBuilder.buildInstr(TargetOpcode::REG_SEQUENCE, {CASDesired}, {})
2405 .addUse(DesiredI->getOperand(0).getReg())
2406 .addImm(AArch64::sube64)
2407 .addUse(DesiredI->getOperand(1).getReg())
2408 .addImm(AArch64::subo64);
2409 MIRBuilder.buildInstr(TargetOpcode::REG_SEQUENCE, {CASNew}, {})
2410 .addUse(NewI->getOperand(0).getReg())
2411 .addImm(AArch64::sube64)
2412 .addUse(NewI->getOperand(1).getReg())
2413 .addImm(AArch64::subo64);
2414
2415 CAS = MIRBuilder.buildInstr(Opcode, {CASDst}, {CASDesired, CASNew, Addr});
2416
2417 MIRBuilder.buildExtract({DstLo}, {CASDst}, 0);
2418 MIRBuilder.buildExtract({DstHi}, {CASDst}, 64);
2419 } else {
2420 // The -O0 CMP_SWAP_128 is friendlier to generate code for because LDXP/STXP
2421 // can take arbitrary registers so it just has the normal GPR64 operands the
2422 // rest of AArch64 is expecting.
2423 auto Ordering = (*MI.memoperands_begin())->getMergedOrdering();
2424 unsigned Opcode;
2425 switch (Ordering) {
2427 Opcode = AArch64::CMP_SWAP_128_ACQUIRE;
2428 break;
2430 Opcode = AArch64::CMP_SWAP_128_RELEASE;
2431 break;
2434 Opcode = AArch64::CMP_SWAP_128;
2435 break;
2436 default:
2437 Opcode = AArch64::CMP_SWAP_128_MONOTONIC;
2438 break;
2439 }
2440
2441 auto Scratch = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
2442 CAS = MIRBuilder.buildInstr(Opcode, {DstLo, DstHi, Scratch},
2443 {Addr, DesiredI->getOperand(0),
2444 DesiredI->getOperand(1), NewI->getOperand(0),
2445 NewI->getOperand(1)});
2446 }
2447
2448 CAS.cloneMemRefs(MI);
2449 constrainSelectedInstRegOperands(*CAS, *ST->getInstrInfo(),
2450 *MRI.getTargetRegisterInfo(),
2451 *ST->getRegBankInfo());
2452
2453 MIRBuilder.buildMergeLikeInstr(MI.getOperand(0), {DstLo, DstHi});
2454 MI.eraseFromParent();
2455 return true;
2456}
2457
2458bool AArch64LegalizerInfo::legalizeCTTZ(MachineInstr &MI,
2459 LegalizerHelper &Helper) const {
2460 MachineIRBuilder &MIRBuilder = Helper.MIRBuilder;
2461 MachineRegisterInfo &MRI = *MIRBuilder.getMRI();
2462 LLT Ty = MRI.getType(MI.getOperand(1).getReg());
2463 auto BitReverse = MIRBuilder.buildBitReverse(Ty, MI.getOperand(1));
2464 MIRBuilder.buildCTLZ(MI.getOperand(0).getReg(), BitReverse);
2465 MI.eraseFromParent();
2466 return true;
2467}
2468
2469bool AArch64LegalizerInfo::legalizeMemOps(MachineInstr &MI,
2470 LegalizerHelper &Helper) const {
2471 MachineIRBuilder &MIRBuilder = Helper.MIRBuilder;
2472
2473 // Tagged version MOPSMemorySetTagged is legalised in legalizeIntrinsic
2474 if (MI.getOpcode() == TargetOpcode::G_MEMSET) {
2475 // Anyext the value being set to 64 bit (only the bottom 8 bits are read by
2476 // the instruction).
2477 auto &Value = MI.getOperand(1);
2478 Register ExtValueReg =
2479 MIRBuilder.buildAnyExt(LLT::scalar(64), Value).getReg(0);
2480 Value.setReg(ExtValueReg);
2481 return true;
2482 }
2483
2484 return false;
2485}
2486
2487bool AArch64LegalizerInfo::legalizeExtractVectorElt(
2489 const GExtractVectorElement *Element = cast<GExtractVectorElement>(&MI);
2490 auto VRegAndVal =
2492 if (VRegAndVal)
2493 return true;
2494 LLT VecTy = MRI.getType(Element->getVectorReg());
2495 if (VecTy.isScalableVector())
2496 return true;
2497 return Helper.lowerExtractInsertVectorElt(MI) !=
2499}
2500
2501bool AArch64LegalizerInfo::legalizeDynStackAlloc(
2502 MachineInstr &MI, LegalizerHelper &Helper) const {
2503 MachineFunction &MF = *MI.getParent()->getParent();
2504 MachineIRBuilder &MIRBuilder = Helper.MIRBuilder;
2505 MachineRegisterInfo &MRI = *MIRBuilder.getMRI();
2506
2507 // If stack probing is not enabled for this function, use the default
2508 // lowering.
2509 if (!MF.getFunction().hasFnAttribute("probe-stack") ||
2510 MF.getFunction().getFnAttribute("probe-stack").getValueAsString() !=
2511 "inline-asm") {
2512 Helper.lowerDynStackAlloc(MI);
2513 return true;
2514 }
2515
2516 Register Dst = MI.getOperand(0).getReg();
2517 Register AllocSize = MI.getOperand(1).getReg();
2518 Align Alignment = assumeAligned(MI.getOperand(2).getImm());
2519
2520 assert(MRI.getType(Dst) == LLT::pointer(0, 64) &&
2521 "Unexpected type for dynamic alloca");
2522 assert(MRI.getType(AllocSize) == LLT::scalar(64) &&
2523 "Unexpected type for dynamic alloca");
2524
2525 LLT PtrTy = MRI.getType(Dst);
2526 Register SPReg =
2528 Register SPTmp =
2529 Helper.getDynStackAllocTargetPtr(SPReg, AllocSize, Alignment, PtrTy);
2530 auto NewMI =
2531 MIRBuilder.buildInstr(AArch64::PROBED_STACKALLOC_DYN, {}, {SPTmp});
2532 MRI.setRegClass(NewMI.getReg(0), &AArch64::GPR64commonRegClass);
2533 MIRBuilder.setInsertPt(*NewMI->getParent(), NewMI);
2534 MIRBuilder.buildCopy(Dst, SPTmp);
2535
2536 MI.eraseFromParent();
2537 return true;
2538}
2539
2540bool AArch64LegalizerInfo::legalizePrefetch(MachineInstr &MI,
2541 LegalizerHelper &Helper) const {
2542 MachineIRBuilder &MIB = Helper.MIRBuilder;
2543 auto &AddrVal = MI.getOperand(0);
2544
2545 int64_t IsWrite = MI.getOperand(1).getImm();
2546 int64_t Locality = MI.getOperand(2).getImm();
2547 int64_t IsData = MI.getOperand(3).getImm();
2548
2549 bool IsStream = Locality == 0;
2550 if (Locality != 0) {
2551 assert(Locality <= 3 && "Prefetch locality out-of-range");
2552 // The locality degree is the opposite of the cache speed.
2553 // Put the number the other way around.
2554 // The encoding starts at 0 for level 1
2555 Locality = 3 - Locality;
2556 }
2557
2558 unsigned PrfOp = (IsWrite << 4) | (!IsData << 3) | (Locality << 1) | IsStream;
2559
2560 MIB.buildInstr(AArch64::G_AARCH64_PREFETCH).addImm(PrfOp).add(AddrVal);
2561 MI.eraseFromParent();
2562 return true;
2563}
2564
2565bool AArch64LegalizerInfo::legalizeFptrunc(MachineInstr &MI,
2566 MachineIRBuilder &MIRBuilder,
2567 MachineRegisterInfo &MRI) const {
2568 auto [Dst, DstTy, Src, SrcTy] = MI.getFirst2RegLLTs();
2569 assert(SrcTy.isFixedVector() && isPowerOf2_32(SrcTy.getNumElements()) &&
2570 "Expected a power of 2 elements");
2571
2572 LLT s16 = LLT::scalar(16);
2573 LLT s32 = LLT::scalar(32);
2574 LLT s64 = LLT::scalar(64);
2575 LLT v2s16 = LLT::fixed_vector(2, s16);
2576 LLT v4s16 = LLT::fixed_vector(4, s16);
2577 LLT v2s32 = LLT::fixed_vector(2, s32);
2578 LLT v4s32 = LLT::fixed_vector(4, s32);
2579 LLT v2s64 = LLT::fixed_vector(2, s64);
2580
2581 SmallVector<Register> RegsToUnmergeTo;
2582 SmallVector<Register> TruncOddDstRegs;
2583 SmallVector<Register> RegsToMerge;
2584
2585 unsigned ElemCount = SrcTy.getNumElements();
2586
2587 // Find the biggest size chunks we can work with
2588 int StepSize = ElemCount % 4 ? 2 : 4;
2589
2590 // If we have a power of 2 greater than 2, we need to first unmerge into
2591 // enough pieces
2592 if (ElemCount <= 2)
2593 RegsToUnmergeTo.push_back(Src);
2594 else {
2595 for (unsigned i = 0; i < ElemCount / 2; ++i)
2596 RegsToUnmergeTo.push_back(MRI.createGenericVirtualRegister(v2s64));
2597
2598 MIRBuilder.buildUnmerge(RegsToUnmergeTo, Src);
2599 }
2600
2601 // Create all of the round-to-odd instructions and store them
2602 for (auto SrcReg : RegsToUnmergeTo) {
2603 Register Mid =
2604 MIRBuilder.buildInstr(AArch64::G_FPTRUNC_ODD, {v2s32}, {SrcReg})
2605 .getReg(0);
2606 TruncOddDstRegs.push_back(Mid);
2607 }
2608
2609 // Truncate 4s32 to 4s16 if we can to reduce instruction count, otherwise
2610 // truncate 2s32 to 2s16.
2611 unsigned Index = 0;
2612 for (unsigned LoopIter = 0; LoopIter < ElemCount / StepSize; ++LoopIter) {
2613 if (StepSize == 4) {
2614 Register ConcatDst =
2615 MIRBuilder
2617 {v4s32}, {TruncOddDstRegs[Index++], TruncOddDstRegs[Index++]})
2618 .getReg(0);
2619
2620 RegsToMerge.push_back(
2621 MIRBuilder.buildFPTrunc(v4s16, ConcatDst).getReg(0));
2622 } else {
2623 RegsToMerge.push_back(
2624 MIRBuilder.buildFPTrunc(v2s16, TruncOddDstRegs[Index++]).getReg(0));
2625 }
2626 }
2627
2628 // If there is only one register, replace the destination
2629 if (RegsToMerge.size() == 1) {
2630 MRI.replaceRegWith(Dst, RegsToMerge.pop_back_val());
2631 MI.eraseFromParent();
2632 return true;
2633 }
2634
2635 // Merge the rest of the instructions & replace the register
2636 Register Fin = MIRBuilder.buildMergeLikeInstr(DstTy, RegsToMerge).getReg(0);
2637 MRI.replaceRegWith(Dst, Fin);
2638 MI.eraseFromParent();
2639 return true;
2640}
unsigned const MachineRegisterInfo * MRI
static void matchLDPSTPAddrMode(Register Root, Register &Base, int &Offset, MachineRegisterInfo &MRI)
This file declares the targeting of the Machinelegalizer class for AArch64.
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static Error unsupported(const char *Str, const Triple &T)
Definition MachO.cpp:71
Declares convenience wrapper classes for interpreting MachineInstr instances as specific generic oper...
IRTranslator LLVM IR MI
Interface for Targets to specify which operations they can successfully select and how the others sho...
Contains matchers for matching SSA Machine Instructions.
This file declares the MachineIRBuilder class.
Promote Memory to Register
Definition Mem2Reg.cpp:110
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
ppc ctr loops verify
if(PassOpts->AAPipeline)
static constexpr MCPhysReg SPReg
This file contains some templates that are useful if you are working with the STL at all.
#define LLVM_DEBUG(...)
Definition Debug.h:114
bool legalizeCustom(LegalizerHelper &Helper, MachineInstr &MI, LostDebugLocObserver &LocObserver) const override
Called for instructions with the Custom LegalizationAction.
bool legalizeIntrinsic(LegalizerHelper &Helper, MachineInstr &MI) const override
AArch64LegalizerInfo(const AArch64Subtarget &ST)
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
Definition APInt.cpp:1023
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition APInt.cpp:1677
int64_t getSExtValue() const
Get sign extended value.
Definition APInt.h:1577
LLVM_ABI StringRef getValueAsString() const
Return the attribute's value as a string.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:676
@ ICMP_NE
not equal
Definition InstrTypes.h:698
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition Function.cpp:764
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:729
Abstract class that contains various methods for clients to notify about changes.
virtual void changingInstr(MachineInstr &MI)=0
This instruction is about to be mutated in some way.
virtual void changedInstr(MachineInstr &MI)=0
This instruction was mutated in some way.
constexpr bool isScalableVector() const
Returns true if the LLT is a scalable vector.
constexpr unsigned getScalarSizeInBits() const
constexpr bool isScalar() const
static constexpr LLT scalable_vector(unsigned MinNumElements, unsigned ScalarSizeInBits)
Get a low-level scalable vector of some number of elements and element width.
static constexpr LLT vector(ElementCount EC, unsigned ScalarSizeInBits)
Get a low-level vector of some number of elements and element width.
constexpr bool isPointerVector() const
static constexpr LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
constexpr uint16_t getNumElements() const
Returns the number of elements in a vector LLT.
constexpr bool isVector() const
static constexpr LLT pointer(unsigned AddressSpace, unsigned SizeInBits)
Get a low-level pointer in the given address space.
constexpr TypeSize getSizeInBits() const
Returns the total size of the type. Must only be called on sized types.
constexpr LLT getElementType() const
Returns the vector's element type. Only valid for vector types.
constexpr ElementCount getElementCount() const
constexpr LLT changeElementSize(unsigned NewEltSize) const
If this type is a vector, return a vector with the same number of elements but the new element size.
constexpr unsigned getAddressSpace() const
static constexpr LLT fixed_vector(unsigned NumElements, unsigned ScalarSizeInBits)
Get a low-level fixed-width vector of some number of elements and element width.
constexpr bool isFixedVector() const
Returns true if the LLT is a fixed vector.
constexpr LLT changeElementCount(ElementCount EC) const
Return a vector or scalar with the same element type and the new element count.
LLVM_ABI void computeTables()
Compute any ancillary tables needed to quickly decide how an operation should be handled.
LegalizeRuleSet & minScalar(unsigned TypeIdx, const LLT Ty)
Ensure the scalar is at least as wide as Ty.
LegalizeRuleSet & widenScalarOrEltToNextPow2OrMinSize(unsigned TypeIdx, unsigned MinSize=0)
Widen the scalar or vector element type to the next power of two that is at least MinSize.
LegalizeRuleSet & legalFor(std::initializer_list< LLT > Types)
The instruction is legal when type index 0 is any type in the given list.
LegalizeRuleSet & maxScalarEltSameAsIf(LegalityPredicate Predicate, unsigned TypeIdx, unsigned SmallTypeIdx)
Conditionally narrow the scalar or elt to match the size of another.
LegalizeRuleSet & unsupported()
The instruction is unsupported.
LegalizeRuleSet & scalarSameSizeAs(unsigned TypeIdx, unsigned SameSizeIdx)
Change the type TypeIdx to have the same scalar size as type SameSizeIdx.
LegalizeRuleSet & bitcastIf(LegalityPredicate Predicate, LegalizeMutation Mutation)
The specified type index is coerced if predicate is true.
LegalizeRuleSet & libcallFor(std::initializer_list< LLT > Types)
LegalizeRuleSet & maxScalar(unsigned TypeIdx, const LLT Ty)
Ensure the scalar is at most as wide as Ty.
LegalizeRuleSet & minScalarOrElt(unsigned TypeIdx, const LLT Ty)
Ensure the scalar or element is at least as wide as Ty.
LegalizeRuleSet & clampMaxNumElements(unsigned TypeIdx, const LLT EltTy, unsigned MaxElements)
Limit the number of elements in EltTy vectors to at most MaxElements.
LegalizeRuleSet & clampMinNumElements(unsigned TypeIdx, const LLT EltTy, unsigned MinElements)
Limit the number of elements in EltTy vectors to at least MinElements.
LegalizeRuleSet & widenVectorEltsToVectorMinSize(unsigned TypeIdx, unsigned VectorSize)
Ensure the vector size is at least as wide as VectorSize by promoting the element.
LegalizeRuleSet & lowerIfMemSizeNotPow2()
Lower a memory operation if the memory size, rounded to bytes, is not a power of 2.
LegalizeRuleSet & minScalarEltSameAsIf(LegalityPredicate Predicate, unsigned TypeIdx, unsigned LargeTypeIdx)
Conditionally widen the scalar or elt to match the size of another.
LegalizeRuleSet & customForCartesianProduct(std::initializer_list< LLT > Types)
LegalizeRuleSet & lowerIfMemSizeNotByteSizePow2()
Lower a memory operation if the memory access size is not a round power of 2 byte size.
LegalizeRuleSet & moreElementsToNextPow2(unsigned TypeIdx)
Add more elements to the vector to reach the next power of two.
LegalizeRuleSet & narrowScalarIf(LegalityPredicate Predicate, LegalizeMutation Mutation)
Narrow the scalar to the one selected by the mutation if the predicate is true.
LegalizeRuleSet & lower()
The instruction is lowered.
LegalizeRuleSet & moreElementsIf(LegalityPredicate Predicate, LegalizeMutation Mutation)
Add more elements to reach the type selected by the mutation if the predicate is true.
LegalizeRuleSet & lowerFor(std::initializer_list< LLT > Types)
The instruction is lowered when type index 0 is any type in the given list.
LegalizeRuleSet & scalarizeIf(LegalityPredicate Predicate, unsigned TypeIdx)
LegalizeRuleSet & lowerIf(LegalityPredicate Predicate)
The instruction is lowered if predicate is true.
LegalizeRuleSet & clampScalar(unsigned TypeIdx, const LLT MinTy, const LLT MaxTy)
Limit the range of scalar sizes to MinTy and MaxTy.
LegalizeRuleSet & custom()
Unconditionally custom lower.
LegalizeRuleSet & minScalarSameAs(unsigned TypeIdx, unsigned LargeTypeIdx)
Widen the scalar to match the size of another.
LegalizeRuleSet & unsupportedIf(LegalityPredicate Predicate)
LegalizeRuleSet & minScalarOrEltIf(LegalityPredicate Predicate, unsigned TypeIdx, const LLT Ty)
Ensure the scalar or element is at least as wide as Ty.
LegalizeRuleSet & widenScalarIf(LegalityPredicate Predicate, LegalizeMutation Mutation)
Widen the scalar to the one selected by the mutation if the predicate is true.
LegalizeRuleSet & alwaysLegal()
LegalizeRuleSet & clampNumElements(unsigned TypeIdx, const LLT MinTy, const LLT MaxTy)
Limit the number of elements for the given vectors to at least MinTy's number of elements and at most...
LegalizeRuleSet & maxScalarIf(LegalityPredicate Predicate, unsigned TypeIdx, const LLT Ty)
Conditionally limit the maximum size of the scalar.
LegalizeRuleSet & customIf(LegalityPredicate Predicate)
LegalizeRuleSet & widenScalarToNextPow2(unsigned TypeIdx, unsigned MinSize=0)
Widen the scalar to the next power of two that is at least MinSize.
LegalizeRuleSet & scalarize(unsigned TypeIdx)
LegalizeRuleSet & legalForCartesianProduct(std::initializer_list< LLT > Types)
The instruction is legal when type indexes 0 and 1 are both in the given list.
LegalizeRuleSet & legalForTypesWithMemDesc(std::initializer_list< LegalityPredicates::TypePairAndMemDesc > TypesAndMemDesc)
The instruction is legal when type indexes 0 and 1 along with the memory size and minimum alignment i...
unsigned immIdx(unsigned ImmIdx)
LegalizeRuleSet & widenScalarOrEltToNextPow2(unsigned TypeIdx, unsigned MinSize=0)
Widen the scalar or vector element type to the next power of two that is at least MinSize.
LegalizeRuleSet & legalIf(LegalityPredicate Predicate)
The instruction is legal if predicate is true.
LLVM_ABI LegalizeResult lowerDynStackAlloc(MachineInstr &MI)
LLVM_ABI LegalizeResult lowerBitCount(MachineInstr &MI)
LLVM_ABI LegalizeResult lowerExtractInsertVectorElt(MachineInstr &MI)
Lower a vector extract or insert by writing the vector to a stack temporary and reloading the element...
LLVM_ABI LegalizeResult lowerAbsToCNeg(MachineInstr &MI)
const TargetLowering & getTargetLowering() const
LLVM_ABI LegalizeResult lowerFunnelShiftAsShifts(MachineInstr &MI)
LLVM_ABI MachineInstrBuilder createStackStoreLoad(const DstOp &Res, const SrcOp &Val)
Create a store of Val to a stack temporary and return a load as the same type as Res.
@ Legalized
Instruction has been legalized and the MachineFunction changed.
@ UnableToLegalize
Some kind of error has occurred and we could not legalize this instruction.
GISelChangeObserver & Observer
To keep track of changes made by the LegalizerHelper.
LLVM_ABI Register getDynStackAllocTargetPtr(Register SPReg, Register AllocSize, Align Alignment, LLT PtrTy)
MachineIRBuilder & MIRBuilder
Expose MIRBuilder so clients can set their own RecordInsertInstruction functions.
LegalizeRuleSet & getActionDefinitionsBuilder(unsigned Opcode)
Get the action definition builder for the given opcode.
const LegacyLegalizerInfo & getLegacyLegalizerInfo() const
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, LLT MemTy, Align base_alignment, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr, SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
Helper class to build MachineInstr.
void setInsertPt(MachineBasicBlock &MBB, MachineBasicBlock::iterator II)
Set the insertion point before the specified position.
MachineInstrBuilder buildAdd(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_ADD Op0, Op1.
MachineInstrBuilder buildNot(const DstOp &Dst, const SrcOp &Src0)
Build and insert a bitwise not, NegOne = G_CONSTANT -1 Res = G_OR Op0, NegOne.
MachineInstrBuilder buildUnmerge(ArrayRef< LLT > Res, const SrcOp &Op)
Build and insert Res0, ... = G_UNMERGE_VALUES Op.
MachineInstrBuilder buildExtract(const DstOp &Res, const SrcOp &Src, uint64_t Index)
Build and insert Res0, ... = G_EXTRACT Src, Idx0.
MachineInstrBuilder buildICmp(CmpInst::Predicate Pred, const DstOp &Res, const SrcOp &Op0, const SrcOp &Op1, std::optional< unsigned > Flags=std::nullopt)
Build and insert a Res = G_ICMP Pred, Op0, Op1.
MachineBasicBlock::iterator getInsertPt()
Current insertion point for new instructions.
MachineInstrBuilder buildZExt(const DstOp &Res, const SrcOp &Op, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_ZEXT Op.
MachineInstrBuilder buildIntrinsic(Intrinsic::ID ID, ArrayRef< Register > Res, bool HasSideEffects, bool isConvergent)
Build and insert a G_INTRINSIC instruction.
MachineInstrBuilder buildCTLZ(const DstOp &Dst, const SrcOp &Src0)
Build and insert Res = G_CTLZ Op0, Src0.
MachineInstrBuilder buildMergeLikeInstr(const DstOp &Res, ArrayRef< Register > Ops)
Build and insert Res = G_MERGE_VALUES Op0, ... or Res = G_BUILD_VECTOR Op0, ... or Res = G_CONCAT_VEC...
MachineInstrBuilder buildLoad(const DstOp &Res, const SrcOp &Addr, MachineMemOperand &MMO)
Build and insert Res = G_LOAD Addr, MMO.
MachineInstrBuilder buildPtrAdd(const DstOp &Res, const SrcOp &Op0, const SrcOp &Op1, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_PTR_ADD Op0, Op1.
MachineInstrBuilder buildBitReverse(const DstOp &Dst, const SrcOp &Src)
Build and insert Dst = G_BITREVERSE Src.
MachineInstrBuilder buildStore(const SrcOp &Val, const SrcOp &Addr, MachineMemOperand &MMO)
Build and insert G_STORE Val, Addr, MMO.
MachineInstrBuilder buildInstr(unsigned Opcode)
Build and insert <empty> = Opcode <empty>.
MachineInstrBuilder buildCTPOP(const DstOp &Dst, const SrcOp &Src0)
Build and insert Res = G_CTPOP Op0, Src0.
MachineFunction & getMF()
Getter for the function we currently build.
MachineInstrBuilder buildExtOrTrunc(unsigned ExtOpc, const DstOp &Res, const SrcOp &Op)
Build and insert Res = ExtOpc, Res = G_TRUNC Op, or Res = COPY Op depending on the differing sizes of...
MachineInstrBuilder buildTrunc(const DstOp &Res, const SrcOp &Op, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_TRUNC Op.
const MachineBasicBlock & getMBB() const
Getter for the basic block we currently build.
MachineInstrBuilder buildAnyExt(const DstOp &Res, const SrcOp &Op)
Build and insert Res = G_ANYEXT Op0.
MachineInstrBuilder buildBitcast(const DstOp &Dst, const SrcOp &Src)
Build and insert Dst = G_BITCAST Src.
MachineRegisterInfo * getMRI()
Getter for MRI.
MachineInstrBuilder buildFPTrunc(const DstOp &Res, const SrcOp &Op, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_FPTRUNC Op.
MachineInstrBuilder buildCopy(const DstOp &Res, const SrcOp &Op)
Build and insert Res = COPY Op.
MachineInstrBuilder buildMaskLowPtrBits(const DstOp &Res, const SrcOp &Op0, uint32_t NumBits)
Build and insert Res = G_PTRMASK Op0, G_CONSTANT (1 << NumBits) - 1.
virtual MachineInstrBuilder buildConstant(const DstOp &Res, const ConstantInt &Val)
Build and insert Res = G_CONSTANT Val.
Register getReg(unsigned Idx) const
Get the register for the operand index.
const MachineInstrBuilder & addUse(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register use operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & cloneMemRefs(const MachineInstr &OtherMI) const
Representation of each machine instruction.
const MachineOperand & getOperand(unsigned i) const
@ MOLoad
The memory access reads data.
@ MOStore
The memory access writes data.
LLVM_ABI void setReg(Register Reg)
Change the register this operand corresponds to.
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI Register createGenericVirtualRegister(LLT Ty, StringRef Name="")
Create and return a new generic virtual register with low-level type Ty.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Register getStackPointerRegisterToSaveRestore() const
If a physical register, this specifies the register that llvm.savestack/llvm.restorestack should save...
Primary interface to the complete machine description for the target machine.
CodeModel::Model getCodeModel() const
Returns the code model.
Target - Wrapper for Target specific information.
LLVM Value Representation.
Definition Value.h:75
constexpr LeafTy divideCoefficientBy(ScalarTy RHS) const
We do not provide the '/' operator here because division for polynomial types does not work in the sa...
Definition TypeSize.h:252
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ MO_NC
MO_NC - Indicates whether the linker is expected to check the symbol reference for overflow.
@ MO_PAGEOFF
MO_PAGEOFF - A symbol operand with this flag represents the offset of that symbol within a 4K page.
@ MO_GOT
MO_GOT - This flag indicates that a symbol operand represents the address of the GOT entry for the sy...
@ MO_PREL
MO_PREL - Indicates that the bits of the symbol operand represented by MO_G0 etc are PC relative.
@ MO_PAGE
MO_PAGE - A symbol operand with this flag represents the pc-relative offset of the 4K page containing...
@ MO_TAGGED
MO_TAGGED - With MO_PAGE, indicates that the page includes a memory tag in bits 56-63.
@ MO_G3
MO_G3 - A symbol operand with this flag (granule 3) represents the high 16-bits of a 64-bit address,...
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
LLVM_ABI LegalityPredicate scalarOrEltWiderThan(unsigned TypeIdx, unsigned Size)
True iff the specified type index is a scalar or a vector with an element type that's wider than the ...
LLVM_ABI LegalityPredicate isPointerVector(unsigned TypeIdx)
True iff the specified type index is a vector of pointers (with any address space).
LLVM_ABI LegalityPredicate typeInSet(unsigned TypeIdx, std::initializer_list< LLT > TypesInit)
True iff the given type index is one of the specified types.
LLVM_ABI LegalityPredicate smallerThan(unsigned TypeIdx0, unsigned TypeIdx1)
True iff the first type index has a smaller total bit size than second type index.
LLVM_ABI LegalityPredicate atomicOrderingAtLeastOrStrongerThan(unsigned MMOIdx, AtomicOrdering Ordering)
True iff the specified MMO index has at an atomic ordering of at Ordering or stronger.
Predicate any(Predicate P0, Predicate P1)
True iff P0 or P1 are true.
LLVM_ABI LegalityPredicate isVector(unsigned TypeIdx)
True iff the specified type index is a vector.
Predicate all(Predicate P0, Predicate P1)
True iff P0 and P1 are true.
LLVM_ABI LegalityPredicate typeIs(unsigned TypeIdx, LLT TypesInit)
True iff the given type index is the specified type.
LLVM_ABI LegalityPredicate scalarWiderThan(unsigned TypeIdx, unsigned Size)
True iff the specified type index is a scalar that's wider than the given size.
LLVM_ABI LegalityPredicate scalarNarrowerThan(unsigned TypeIdx, unsigned Size)
True iff the specified type index is a scalar that's narrower than the given size.
@ Bitcast
Perform the operation on a different, but equivalently sized type.
LLVM_ABI LegalizeMutation moreElementsToNextPow2(unsigned TypeIdx, unsigned Min=0)
Add more elements to the type for the given type index to the next power of.
LLVM_ABI LegalizeMutation scalarize(unsigned TypeIdx)
Break up the vector type for the given type index into the element type.
LLVM_ABI LegalizeMutation changeElementTo(unsigned TypeIdx, unsigned FromTypeIdx)
Keep the same scalar or element type as the given type index.
LLVM_ABI LegalizeMutation widenScalarOrEltToNextPow2(unsigned TypeIdx, unsigned Min=0)
Widen the scalar type or vector element type for the given type index to the next power of 2.
LLVM_ABI LegalizeMutation changeTo(unsigned TypeIdx, LLT Ty)
Select this specific type for the given type index.
LLVM_ABI LegalizeMutation changeElementSizeTo(unsigned TypeIdx, unsigned FromTypeIdx)
Change the scalar size or element size to have the same scalar size as type index FromIndex.
operand_type_match m_Reg()
ConstantMatch< APInt > m_ICst(APInt &Cst)
bool mi_match(Reg R, const MachineRegisterInfo &MRI, Pattern &&P)
BinaryOp_match< LHS, RHS, TargetOpcode::G_PTR_ADD, false > m_GPtrAdd(const LHS &L, const RHS &R)
Invariant opcodes: All instruction sets have these as their low opcodes.
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
@ Offset
Definition DWP.cpp:532
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
LLVM_ABI void constrainSelectedInstRegOperands(MachineInstr &I, const TargetInstrInfo &TII, const TargetRegisterInfo &TRI, const RegisterBankInfo &RBI)
Mutate the newly-selected instruction I to constrain its (possibly generic) virtual register operands...
Definition Utils.cpp:155
LLVM_ABI std::optional< APInt > isConstantOrConstantSplatVector(MachineInstr &MI, const MachineRegisterInfo &MRI)
Determines if MI defines a constant integer or a splat vector of constant integers.
Definition Utils.cpp:1566
std::function< bool(const LegalityQuery &)> LegalityPredicate
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:279
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
AtomicOrdering
Atomic ordering for LLVM's memory model.
@ Add
Sum of integers.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
DWARFExpression::Operation Op
constexpr bool isShiftedInt(int64_t x)
Checks if a signed integer is an N bit number shifted left by S.
Definition MathExtras.h:182
constexpr unsigned BitWidth
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI std::optional< ValueAndVReg > getIConstantVRegValWithLookThrough(Register VReg, const MachineRegisterInfo &MRI, bool LookThroughInstrs=true)
If VReg is defined by a statically evaluable chain of instructions rooted on a G_CONSTANT returns its...
Definition Utils.cpp:433
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1947
Align assumeAligned(uint64_t Value)
Treats the value 0 as a 1, so Align is always at least 1.
Definition Alignment.h:100
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:197
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
The LegalityQuery object bundles together all the information that's needed to decide whether a given...
ArrayRef< MemDesc > MMODescrs
Operations which require memory can use this to place requirements on the memory type for each MMO.
ArrayRef< LLT > Types
This class contains a discriminated union of information about pointers in memory operands,...