LLVM 24.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 const LLT bf16 = LLT::bfloat16();
69 const LLT v4bf16 = LLT::fixed_vector(4, bf16);
70 const LLT v8bf16 = LLT::fixed_vector(8, bf16);
71
72 const LLT f16 = LLT::float16();
73 const LLT v4f16 = LLT::fixed_vector(4, f16);
74 const LLT v8f16 = LLT::fixed_vector(8, f16);
75
76 const LLT f32 = LLT::float32();
77 const LLT v2f32 = LLT::fixed_vector(2, f32);
78 const LLT v4f32 = LLT::fixed_vector(4, f32);
79
80 const LLT f64 = LLT::float64();
81 const LLT v2f64 = LLT::fixed_vector(2, f64);
82
83 const LLT f128 = LLT::float128();
84
85 const LLT i8 = LLT::integer(8);
86 const LLT v8i8 = LLT::fixed_vector(8, i8);
87 const LLT v16i8 = LLT::fixed_vector(16, i8);
88
89 const LLT i16 = LLT::integer(16);
90 const LLT v8i16 = LLT::fixed_vector(8, i16);
91 const LLT v4i16 = LLT::fixed_vector(4, i16);
92
93 const LLT i32 = LLT::integer(32);
94 const LLT v2i32 = LLT::fixed_vector(2, i32);
95 const LLT v4i32 = LLT::fixed_vector(4, i32);
96
97 const LLT i64 = LLT::integer(64);
98 const LLT v2i64 = LLT::fixed_vector(2, i64);
99
100 const LLT i128 = LLT::integer(128);
101
102 const LLT nxv16i8 = LLT::scalable_vector(16, i8);
103 const LLT nxv8i16 = LLT::scalable_vector(8, i16);
104 const LLT nxv4i32 = LLT::scalable_vector(4, i32);
105 const LLT nxv2i64 = LLT::scalable_vector(2, i64);
106
107 std::initializer_list<LLT> PackedVectorAllTypeList = {/* Begin 128bit types */
108 v16s8, v8s16, v4s32,
109 v2s64, v2p0,
110 /* End 128bit types */
111 /* Begin 64bit types */
112 v8s8, v4s16, v2s32};
113 std::initializer_list<LLT> ScalarAndPtrTypesList = {s8, s16, s32, s64, p0};
114 SmallVector<LLT, 8> PackedVectorAllTypesVec(PackedVectorAllTypeList);
115 SmallVector<LLT, 8> ScalarAndPtrTypesVec(ScalarAndPtrTypesList);
116
117 const TargetMachine &TM = ST.getTargetLowering()->getTargetMachine();
118
119 // FIXME: support subtargets which have neon/fp-armv8 disabled.
120 if (!ST.hasNEON() || !ST.hasFPARMv8())
121 return;
122
123 // Some instructions only support s16 if the subtarget has full 16-bit FP
124 // support.
125 const bool HasFP16 = ST.hasFullFP16();
126 const bool HasCSSC = ST.hasCSSC();
127 const bool HasRCPC3 = ST.hasRCPC3();
128 const bool HasSVE = ST.hasSVE();
129
131 {G_IMPLICIT_DEF, G_FREEZE, G_CONSTANT_FOLD_BARRIER})
132 .legalFor({p0, s8, s16, s32, s64, s128})
133 .legalFor({v2s8, v4s8, v8s8, v16s8, v2s16, v4s16, v8s16, v2s32, v4s32,
134 v2s64, v2p0})
135 .widenScalarToNextPow2(0)
136 .clampScalar(0, s8, s64)
139 .clampNumElements(0, v8s8, v16s8)
140 .clampNumElements(0, v4s16, v8s16)
141 .clampNumElements(0, v2s32, v4s32)
142 .clampMaxNumElements(0, s64, 2)
143 .clampMaxNumElements(0, p0, 2)
145
147 .legalFor({p0, s16, s32, s64})
148 .legalFor(PackedVectorAllTypeList)
152 .clampScalar(0, s16, s64)
153 .clampNumElements(0, v8s8, v16s8)
154 .clampNumElements(0, v4s16, v8s16)
155 .clampNumElements(0, v2s32, v4s32)
156 .clampMaxNumElements(0, s64, 2)
157 .clampMaxNumElements(0, p0, 2)
159
161 .legalIf(all(typeInSet(0, {s32, s64, p0}), typeInSet(1, {s8, s16, s32}),
162 smallerThan(1, 0)))
163 .widenScalarToNextPow2(0)
164 .clampScalar(0, s32, s64)
166 .minScalar(1, s8)
167 .maxScalarIf(typeInSet(0, {s32}), 1, s16)
168 .maxScalarIf(typeInSet(0, {s64, p0}), 1, s32);
169
171 .legalIf(all(typeInSet(0, {s16, s32, s64, p0}),
172 typeInSet(1, {s32, s64, s128, p0}), smallerThan(0, 1)))
173 .widenScalarToNextPow2(1)
174 .clampScalar(1, s32, s128)
176 .minScalar(0, s16)
177 .maxScalarIf(typeInSet(1, {s32}), 0, s16)
178 .maxScalarIf(typeInSet(1, {s64, p0}), 0, s32)
179 .maxScalarIf(typeInSet(1, {s128}), 0, s64);
180
181 getActionDefinitionsBuilder({G_ADD, G_SUB, G_AND, G_OR, G_XOR})
182 .legalFor({i32, i64, v8i8, v16i8, v4i16, v8i16, v2i32, v4i32, v2i64})
183 .legalFor(HasSVE, {nxv16i8, nxv8i16, nxv4i32, nxv2i64})
184 .widenScalarToNextPow2(0)
185 .clampScalar(0, s32, s64)
186 .clampMaxNumElements(0, s8, 16)
187 .clampMaxNumElements(0, s16, 8)
188 .clampNumElements(0, v2s32, v4s32)
189 .clampNumElements(0, v2s64, v2s64)
191 [=](const LegalityQuery &Query) {
192 return Query.Types[0].getNumElements() <= 2;
193 },
194 0, s32)
195 .minScalarOrEltIf(
196 [=](const LegalityQuery &Query) {
197 return Query.Types[0].getNumElements() <= 4;
198 },
199 0, s16)
200 .minScalarOrEltIf(
201 [=](const LegalityQuery &Query) {
202 return Query.Types[0].getNumElements() <= 16;
203 },
204 0, s8)
205 .scalarizeIf(scalarOrEltWiderThan(0, 64), 0)
207
209 .legalFor({i32, i64, v8i8, v16i8, v4i16, v8i16, v2i32, v4i32, v2i64})
210 .widenScalarToNextPow2(0)
211 .clampScalar(0, s32, s64)
212 .clampMaxNumElements(0, s8, 16)
213 .clampMaxNumElements(0, s16, 8)
214 .clampNumElements(0, v2s32, v4s32)
215 .clampNumElements(0, v2s64, v2s64)
217 [=](const LegalityQuery &Query) {
218 return Query.Types[0].getNumElements() <= 2;
219 },
220 0, s32)
221 .minScalarOrEltIf(
222 [=](const LegalityQuery &Query) {
223 return Query.Types[0].getNumElements() <= 4;
224 },
225 0, s16)
226 .minScalarOrEltIf(
227 [=](const LegalityQuery &Query) {
228 return Query.Types[0].getNumElements() <= 16;
229 },
230 0, s8)
231 .scalarizeIf(scalarOrEltWiderThan(0, 64), 0)
233
234 getActionDefinitionsBuilder({G_SHL, G_ASHR, G_LSHR})
235 .customIf([=](const LegalityQuery &Query) {
236 const auto &SrcTy = Query.Types[0];
237 const auto &AmtTy = Query.Types[1];
238 return !SrcTy.isVector() && SrcTy.getSizeInBits() == 32 &&
239 AmtTy.getSizeInBits() == 32;
240 })
241 .legalFor({
242 {i32, i32},
243 {i32, i64},
244 {i64, i64},
245 {v8i8, v8i8},
246 {v16i8, v16i8},
247 {v4i16, v4i16},
248 {v8i16, v8i16},
249 {v2i32, v2i32},
250 {v4i32, v4i32},
251 {v2i64, v2i64},
252 })
253 .widenScalarToNextPow2(1)
255 .clampScalar(1, s32, s64)
256 .clampScalar(0, s32, s64)
257 .clampNumElements(0, v8s8, v16s8)
258 .clampNumElements(0, v4s16, v8s16)
259 .clampNumElements(0, v2s32, v4s32)
260 .clampNumElements(0, v2s64, v2s64)
262 .minScalarSameAs(1, 0)
266
268 .legalFor({{p0, i64}, {v2p0, v2i64}})
269 .clampScalarOrElt(1, s64, s64)
270 .clampNumElements(0, v2p0, v2p0);
271
272 getActionDefinitionsBuilder(G_PTRMASK).legalFor({{p0, s64}});
273
274 getActionDefinitionsBuilder({G_SDIV, G_UDIV})
275 .legalFor({i32, i64})
276 .libcallFor({i128})
277 .clampScalar(0, s32, s64)
279 .scalarize(0);
280
281 getActionDefinitionsBuilder({G_SREM, G_UREM, G_SDIVREM, G_UDIVREM})
282 .lowerFor({i8, i16, i32, i64, v2i32, v4i32, v2i64})
283 .libcallFor({i128})
285 .minScalarOrElt(0, s32)
286 .clampNumElements(0, v2s32, v4s32)
287 .clampNumElements(0, v2s64, v2s64)
288 .scalarize(0);
289
290 getActionDefinitionsBuilder({G_SMULO, G_UMULO})
291 .widenScalarToNextPow2(0, /*Min = */ 32)
292 .clampScalar(0, s32, s64)
293 .lower();
294
295 getActionDefinitionsBuilder({G_SMULH, G_UMULH})
296 .legalFor({i64, v16i8, v8i16, v4i32})
297 .lower();
298
299 getActionDefinitionsBuilder({G_SMULFIX, G_UMULFIX}).lower();
300
301 getActionDefinitionsBuilder({G_SMIN, G_SMAX, G_UMIN, G_UMAX})
302 .legalFor({v8i8, v16i8, v4i16, v8i16, v2i32, v4i32})
303 .legalFor(HasCSSC, {i32, i64})
304 .minScalar(HasCSSC, 0, s32)
305 .clampNumElements(0, v8s8, v16s8)
306 .clampNumElements(0, v4s16, v8s16)
307 .clampNumElements(0, v2s32, v4s32)
308 .lower();
309
310 // FIXME: Legal vector types are only legal with NEON.
312 .legalFor(HasCSSC, {i32, i64})
313 .legalFor({v16i8, v8i16, v4i32, v2i64, v2p0, v8i8, v4i16, v2i32})
314 .customIf([=](const LegalityQuery &Q) {
315 // TODO: Fix suboptimal codegen for 128+ bit types.
316 LLT SrcTy = Q.Types[0];
317 return SrcTy.isScalar() && SrcTy.getSizeInBits() < 128;
318 })
319 .widenScalarIf(
320 [=](const LegalityQuery &Query) { return Query.Types[0] == v4s8; },
321 [=](const LegalityQuery &Query) { return std::make_pair(0, v4i16); })
322 .widenScalarIf(
323 [=](const LegalityQuery &Query) { return Query.Types[0] == v2s16; },
324 [=](const LegalityQuery &Query) { return std::make_pair(0, v2i32); })
325 .clampNumElements(0, v8s8, v16s8)
326 .clampNumElements(0, v4s16, v8s16)
327 .clampNumElements(0, v2s32, v4s32)
328 .clampNumElements(0, v2s64, v2s64)
330 .lower();
331
333 {G_ABDS, G_ABDU, G_UAVGFLOOR, G_UAVGCEIL, G_SAVGFLOOR, G_SAVGCEIL})
334 .legalFor({v8i8, v16i8, v4i16, v8i16, v2i32, v4i32})
335 .lower();
336
338 {G_SADDE, G_SSUBE, G_UADDE, G_USUBE, G_SADDO, G_SSUBO, G_UADDO, G_USUBO})
339 .legalFor({{i32, i32}, {i64, i32}})
340 .clampScalar(0, s32, s64)
341 .clampScalar(1, s32, s64)
343
344 getActionDefinitionsBuilder({G_FSHL, G_FSHR})
345 .customFor({{i32, i32}, {i32, i64}, {i64, i64}})
346 .lower();
347
349 .legalFor({{i32, i64}, {i64, i64}})
350 .customIf([=](const LegalityQuery &Q) {
351 return Q.Types[0].isScalar() && Q.Types[1].getScalarSizeInBits() < 64;
352 })
353 .lower();
355
356 getActionDefinitionsBuilder({G_SBFX, G_UBFX})
357 .customFor({{s32, s32}, {s64, s64}});
358
359 auto always = [=](const LegalityQuery &Q) { return true; };
361 .legalFor(HasCSSC, {{i32, i32}, {i64, i64}})
362 .legalFor({{v8i8, v8i8}, {v16i8, v16i8}})
363 .customFor(!HasCSSC, {{s32, s32}, {s64, s64}})
364 .customFor({{s128, s128},
365 {v4s16, v4s16},
366 {v8s16, v8s16},
367 {v2s32, v2s32},
368 {v4s32, v4s32},
369 {v2s64, v2s64}})
370 .clampScalar(0, s32, s128)
373 .minScalarEltSameAsIf(always, 1, 0)
374 .maxScalarEltSameAsIf(always, 1, 0)
375 .clampNumElements(0, v8s8, v16s8)
376 .clampNumElements(0, v4s16, v8s16)
377 .clampNumElements(0, v2s32, v4s32)
378 .clampNumElements(0, v2s64, v2s64)
381
382 getActionDefinitionsBuilder({G_CTLZ, G_CTLS})
383 .legalFor({{i32, i32},
384 {i64, i64},
385 {v8i8, v8i8},
386 {v16i8, v16i8},
387 {v4i16, v4i16},
388 {v8i16, v8i16},
389 {v2i32, v2i32},
390 {v4i32, v4i32}})
391 .widenScalarToNextPow2(1, /*Min=*/32)
392 .clampScalar(1, s32, s64)
394 .clampNumElements(0, v8s8, v16s8)
395 .clampNumElements(0, v4s16, v8s16)
396 .clampNumElements(0, v2s32, v4s32)
399 .scalarSameSizeAs(0, 1);
400
401 getActionDefinitionsBuilder(G_INSERT_SUBVECTOR).lower();
402
403 getActionDefinitionsBuilder(G_CTLZ_ZERO_POISON).lower();
404
406 .lowerIf(isVector(0))
407 .widenScalarToNextPow2(1, /*Min=*/32)
408 .clampScalar(1, s32, s64)
409 .scalarSameSizeAs(0, 1)
410 .legalFor(HasCSSC, {s32, s64})
411 .customFor(!HasCSSC, {s32, s64});
412
413 getActionDefinitionsBuilder(G_CTTZ_ZERO_POISON).lower();
414
415 getActionDefinitionsBuilder(G_BITREVERSE)
416 .legalFor({i32, i64, v8i8, v16i8})
417 .widenScalarToNextPow2(0, /*Min = */ 32)
419 .clampScalar(0, s32, s64)
420 .clampNumElements(0, v8s8, v16s8)
421 .clampNumElements(0, v4s16, v8s16)
422 .clampNumElements(0, v2s32, v4s32)
423 .clampNumElements(0, v2s64, v2s64)
426 .lower();
427
428 getActionDefinitionsBuilder(G_CLMUL).legalFor({v8i8, v16i8});
429
431 .legalFor({i32, i64, v4i16, v8i16, v2i32, v4i32, v2i64})
433 .clampScalar(0, s32, s64)
434 .clampNumElements(0, v4s16, v8s16)
435 .clampNumElements(0, v2s32, v4s32)
436 .clampNumElements(0, v2s64, v2s64)
438
439 getActionDefinitionsBuilder({G_UADDSAT, G_SADDSAT, G_USUBSAT, G_SSUBSAT})
440 .legalFor({v8i8, v16i8, v4i16, v8i16, v2i32, v4i32, v2i64})
441 .legalFor(HasSVE, {nxv16i8, nxv8i16, nxv4i32, nxv2i64})
442 .clampNumElements(0, v8s8, v16s8)
443 .clampNumElements(0, v4s16, v8s16)
444 .clampNumElements(0, v2s32, v4s32)
445 .clampMaxNumElements(0, s64, 2)
448 .lower();
449
451 {G_FADD, G_FSUB, G_FMUL, G_FDIV, G_FMA, G_FSQRT, G_FMAXNUM, G_FMINNUM,
452 G_FMAXIMUM, G_FMINIMUM, G_FCEIL, G_FFLOOR, G_FRINT, G_FNEARBYINT,
453 G_INTRINSIC_TRUNC, G_INTRINSIC_ROUND, G_INTRINSIC_ROUNDEVEN})
454 .legalFor({f32, f64, v2f32, v4f32, v2f64})
455 .legalFor(HasFP16, {f16, v4f16, v8f16})
456 .libcallFor({f128})
457 .scalarizeIf(scalarOrEltWiderThan(0, 64), 0)
459 [=](const LegalityQuery &Q) {
460 return (!HasFP16 && Q.Types[0].getScalarType().isFloat16()) ||
461 Q.Types[0].getScalarType().isBFloat16();
462 },
463 changeElementTo(0, f32))
464 .clampNumElements(0, v4s16, v8s16)
465 .clampNumElements(0, v2s32, v4s32)
466 .clampNumElements(0, v2s64, v2s64)
468
469 getActionDefinitionsBuilder({G_FABS, G_FNEG})
470 .legalFor({f32, f64, v2f32, v4f32, v2f64})
471 .legalFor(HasFP16, {f16, bf16, v4f16, v4bf16, v8f16, v8bf16})
472 .scalarizeIf(scalarOrEltWiderThan(0, 64), 0)
474 .clampNumElements(0, v4s16, v8s16)
475 .clampNumElements(0, v2s32, v4s32)
476 .clampNumElements(0, v2s64, v2s64)
478 .lowerFor({f16, bf16, v4f16, v4bf16, v8f16, v8bf16});
479
480 getActionDefinitionsBuilder({G_FREM, G_FCOS, G_FSIN, G_FPOW, G_FLOG, G_FLOG2,
481 G_FLOG10, G_FTAN, G_FEXP, G_FEXP2, G_FEXP10,
482 G_FACOS, G_FASIN, G_FATAN, G_FATAN2, G_FCOSH,
483 G_FSINH, G_FTANH, G_FMODF})
484 .libcallFor({f32, f64, f128})
485 .widenScalarFor({f16, bf16}, changeElementTo(0, f32))
486 .scalarize(0);
487 getActionDefinitionsBuilder({G_FPOWI, G_FLDEXP})
488 .libcallFor({{f32, i32}, {f64, i32}, {f128, i32}})
489 .widenScalarFor({f16, bf16}, changeElementTo(0, f32))
490 .scalarize(0);
491
492 getActionDefinitionsBuilder({G_LROUND, G_INTRINSIC_LRINT})
493 .legalFor({{i32, f32}, {i32, f64}, {i64, f32}, {i64, f64}})
494 .legalFor(HasFP16, {{i32, f16}, {i64, f16}})
495 .minScalar(1, s32)
496 .libcallFor({{s64, s128}})
497 .lower();
498 getActionDefinitionsBuilder({G_LLROUND, G_INTRINSIC_LLRINT})
499 .legalFor({{i64, f32}, {i64, f64}})
500 .legalFor(HasFP16, {{i64, f16}})
501 .minScalar(0, s64)
502 .minScalar(1, s32)
503 .libcallFor({{s64, s128}})
504 .lower();
505
506 // TODO: Custom legalization for mismatched types.
507 getActionDefinitionsBuilder(G_FCOPYSIGN)
509 [](const LegalityQuery &Query) { return Query.Types[0].isScalar(); },
510 [=](const LegalityQuery &Query) {
511 const LLT Ty = Query.Types[0];
512 return std::pair(0, LLT::fixed_vector(Ty == s16 ? 4 : 2, Ty));
513 })
514 .lower();
515
517
518 for (unsigned Op : {G_SEXTLOAD, G_ZEXTLOAD}) {
519 auto &Actions = getActionDefinitionsBuilder(Op);
520
521 if (Op == G_SEXTLOAD)
523
524 // Atomics have zero extending behavior.
525 Actions
526 .legalForTypesWithMemDesc({{s32, p0, s8, 8},
527 {s32, p0, s16, 8},
528 {s32, p0, s32, 8},
529 {s64, p0, s8, 2},
530 {s64, p0, s16, 2},
531 {s64, p0, s32, 4},
532 {s64, p0, s64, 8},
533 {p0, p0, s64, 8},
534 {v2s32, p0, s64, 8}})
535 .widenScalarToNextPow2(0)
536 .clampScalar(0, s32, s64)
537 // TODO: We could support sum-of-pow2's but the lowering code doesn't know
538 // how to do that yet.
539 .unsupportedIfMemSizeNotPow2()
540 // Lower anything left over into G_*EXT and G_LOAD
541 .lower();
542 }
543
544 auto IsPtrVecPred = [=](const LegalityQuery &Query) {
545 const LLT &ValTy = Query.Types[0];
546 return ValTy.isPointerVector() && ValTy.getAddressSpace() == 0;
547 };
548
550 .customIf([=](const LegalityQuery &Query) {
551 return HasRCPC3 && Query.Types[0] == s128 &&
552 Query.MMODescrs[0].Ordering == AtomicOrdering::Acquire;
553 })
554 .customIf([=](const LegalityQuery &Query) {
555 return Query.Types[0] == s128 &&
556 Query.MMODescrs[0].Ordering != AtomicOrdering::NotAtomic;
557 })
558 .legalForTypesWithMemDesc({{s8, p0, s8, 8},
559 {s16, p0, s16, 8},
560 {s32, p0, s32, 8},
561 {s64, p0, s64, 8},
562 {p0, p0, s64, 8},
563 {s128, p0, s128, 8},
564 {v8s8, p0, s64, 8},
565 {v16s8, p0, s128, 8},
566 {v4s16, p0, s64, 8},
567 {v8s16, p0, s128, 8},
568 {v2s32, p0, s64, 8},
569 {v4s32, p0, s128, 8},
570 {v2s64, p0, s128, 8}})
571 // These extends are also legal
572 .legalForTypesWithMemDesc(
573 {{s32, p0, s8, 8}, {s32, p0, s16, 8}, {s64, p0, s32, 8}})
574 .legalForTypesWithMemDesc({
575 // SVE vscale x 128 bit base sizes
576 {nxv16s8, p0, nxv16s8, 8},
577 {nxv8s16, p0, nxv8s16, 8},
578 {nxv4s32, p0, nxv4s32, 8},
579 {nxv2s64, p0, nxv2s64, 8},
580 })
581 .widenScalarToNextPow2(0, /* MinSize = */ 8)
582 .clampMaxNumElements(0, s8, 16)
583 .clampMaxNumElements(0, s16, 8)
584 .clampMaxNumElements(0, s32, 4)
585 .clampMaxNumElements(0, s64, 2)
586 .clampMaxNumElements(0, p0, 2)
588 .clampScalar(0, s8, s64)
590 [=](const LegalityQuery &Query) {
591 // Clamp extending load results to 32-bits.
592 return Query.Types[0].isScalar() &&
593 Query.Types[0] != Query.MMODescrs[0].MemoryTy &&
594 Query.Types[0].getSizeInBits() > 32;
595 },
596 changeTo(0, s32))
597 // TODO: Use BITCAST for v2i8, v2i16 after G_TRUNC gets sorted out
598 .bitcastIf(typeInSet(0, {v4s8}),
599 [=](const LegalityQuery &Query) {
600 const LLT VecTy = Query.Types[0];
601 return std::pair(0, LLT::integer(VecTy.getSizeInBits()));
602 })
603 .customIf(IsPtrVecPred)
604 .scalarizeIf(typeInSet(0, {v2s16, v2s8}), 0)
605 .scalarizeIf(scalarOrEltWiderThan(0, 64), 0);
606
608 .customIf([=](const LegalityQuery &Query) {
609 return HasRCPC3 && Query.Types[0] == s128 &&
610 Query.MMODescrs[0].Ordering == AtomicOrdering::Release;
611 })
612 .customIf([=](const LegalityQuery &Query) {
613 return Query.Types[0] == s128 &&
614 Query.MMODescrs[0].Ordering != AtomicOrdering::NotAtomic;
615 })
616 .widenScalarIf(
617 all(scalarNarrowerThan(0, 32),
619 changeTo(0, s32))
621 {{s8, p0, s8, 8}, {s16, p0, s8, 8}, // truncstorei8 from s16
622 {s32, p0, s8, 8}, // truncstorei8 from s32
623 {s64, p0, s8, 8}, // truncstorei8 from s64
624 {s16, p0, s16, 8}, {s32, p0, s16, 8}, // truncstorei16 from s32
625 {s64, p0, s16, 8}, // truncstorei16 from s64
626 {s32, p0, s8, 8}, {s32, p0, s16, 8}, {s32, p0, s32, 8},
627 {s64, p0, s64, 8}, {s64, p0, s32, 8}, // truncstorei32 from s64
628 {p0, p0, s64, 8}, {s128, p0, s128, 8}, {v16s8, p0, s128, 8},
629 {v8s8, p0, s64, 8}, {v4s16, p0, s64, 8}, {v8s16, p0, s128, 8},
630 {v2s32, p0, s64, 8}, {v4s32, p0, s128, 8}, {v2s64, p0, s128, 8}})
631 .legalForTypesWithMemDesc({
632 // SVE vscale x 128 bit base sizes
633 // TODO: Add nxv2p0. Consider bitcastIf.
634 // See #92130
635 // https://github.com/llvm/llvm-project/pull/92130#discussion_r1616888461
636 {nxv16s8, p0, nxv16s8, 8},
637 {nxv8s16, p0, nxv8s16, 8},
638 {nxv4s32, p0, nxv4s32, 8},
639 {nxv2s64, p0, nxv2s64, 8},
640 })
641 .clampScalar(0, s8, s64)
642 .minScalarOrElt(0, s8)
643 .lowerIf([=](const LegalityQuery &Query) {
644 return Query.Types[0].isScalar() &&
645 Query.Types[0] != Query.MMODescrs[0].MemoryTy;
646 })
647 // Maximum: sN * k = 128
648 .clampMaxNumElements(0, s8, 16)
649 .clampMaxNumElements(0, s16, 8)
650 .clampMaxNumElements(0, s32, 4)
651 .clampMaxNumElements(0, s64, 2)
652 .clampMaxNumElements(0, p0, 2)
654 // TODO: Use BITCAST for v2i8, v2i16 after G_TRUNC gets sorted out
655 .bitcastIf(all(typeInSet(0, {v4s8}),
656 LegalityPredicate([=](const LegalityQuery &Query) {
657 return Query.Types[0].getSizeInBits() ==
658 Query.MMODescrs[0].MemoryTy.getSizeInBits();
659 })),
660 [=](const LegalityQuery &Query) {
661 const LLT VecTy = Query.Types[0];
662 return std::pair(0, LLT::integer(VecTy.getSizeInBits()));
663 })
664 .customIf(IsPtrVecPred)
665 .scalarizeIf(typeInSet(0, {v2s16, v2s8}), 0)
666 .scalarizeIf(scalarOrEltWiderThan(0, 64), 0)
667 .lower();
668
669 getActionDefinitionsBuilder(G_INDEXED_STORE)
670 // Idx 0 == Ptr, Idx 1 == Val
671 // TODO: we can implement legalizations but as of now these are
672 // generated in a very specific way.
674 {p0, s8, s8, 8},
675 {p0, s16, s16, 8},
676 {p0, s32, s8, 8},
677 {p0, s32, s16, 8},
678 {p0, s32, s32, 8},
679 {p0, s64, s64, 8},
680 {p0, p0, p0, 8},
681 {p0, v8s8, v8s8, 8},
682 {p0, v16s8, v16s8, 8},
683 {p0, v4s16, v4s16, 8},
684 {p0, v8s16, v8s16, 8},
685 {p0, v2s32, v2s32, 8},
686 {p0, v4s32, v4s32, 8},
687 {p0, v2s64, v2s64, 8},
688 {p0, v2p0, v2p0, 8},
689 {p0, s128, s128, 8},
690 })
691 .unsupported();
692
693 auto IndexedLoadBasicPred = [=](const LegalityQuery &Query) {
694 LLT LdTy = Query.Types[0];
695 LLT PtrTy = Query.Types[1];
696 if (!llvm::is_contained(PackedVectorAllTypesVec, LdTy) &&
697 !llvm::is_contained(ScalarAndPtrTypesVec, LdTy) && LdTy != s128)
698 return false;
699 if (PtrTy != p0)
700 return false;
701 return true;
702 };
703 getActionDefinitionsBuilder(G_INDEXED_LOAD)
706 .legalIf(IndexedLoadBasicPred)
707 .unsupported();
708 getActionDefinitionsBuilder({G_INDEXED_SEXTLOAD, G_INDEXED_ZEXTLOAD})
709 .unsupportedIf(
711 .legalIf(all(typeInSet(0, {s16, s32, s64}),
712 LegalityPredicate([=](const LegalityQuery &Q) {
713 LLT LdTy = Q.Types[0];
714 LLT PtrTy = Q.Types[1];
715 LLT MemTy = Q.MMODescrs[0].MemoryTy;
716 if (PtrTy != p0)
717 return false;
718 if (LdTy == s16)
719 return MemTy == s8;
720 if (LdTy == s32)
721 return MemTy == s8 || MemTy == s16;
722 if (LdTy == s64)
723 return MemTy == s8 || MemTy == s16 || MemTy == s32;
724 return false;
725 })))
726 .unsupported();
727
728 // Constants
730 .legalFor({p0, s8, s16, s32, s64})
731 .widenScalarToNextPow2(0)
732 .clampScalar(0, s8, s64);
733 getActionDefinitionsBuilder(G_FCONSTANT)
734 .legalFor({s16, s32, s64, s128});
735
736 // FIXME: fix moreElementsToNextPow2
738 .legalFor({{i32, i32}, {i32, i64}, {i32, p0}})
740 .minScalarOrElt(1, s8)
741 .clampScalar(1, s32, s64)
742 .clampScalar(0, s32, s32)
745 [=](const LegalityQuery &Query) {
746 const LLT &Ty = Query.Types[0];
747 const LLT &SrcTy = Query.Types[1];
748 return Ty.isVector() && !SrcTy.isPointerVector() &&
749 Ty.getElementType() != SrcTy.getElementType();
750 },
751 0, 1)
752 .minScalarOrEltIf(
753 [=](const LegalityQuery &Query) { return Query.Types[1] == v2s16; },
754 1, s32)
755 .minScalarOrEltIf(
756 [=](const LegalityQuery &Query) {
757 return Query.Types[1].isPointerVector();
758 },
759 0, s64)
761 .clampNumElements(1, v8s8, v16s8)
762 .clampNumElements(1, v4s16, v8s16)
763 .clampNumElements(1, v2s32, v4s32)
764 .clampNumElements(1, v2s64, v2s64)
765 .clampNumElements(1, v2p0, v2p0)
766 .customIf(isVector(0));
767
769 .legalFor({{i32, f32},
770 {i32, f64},
771 {v4i32, v4f32},
772 {v2i32, v2f32},
773 {v2i64, v2f64}})
774 .legalFor(HasFP16, {{i32, f16}, {v4i16, v4f16}, {v8i16, v8f16}})
776 .clampScalar(0, s32, s32)
778 [=](const LegalityQuery &Q) {
779 return (!HasFP16 && Q.Types[1].getScalarType().isFloat16()) ||
780 Q.Types[1].getScalarType().isBFloat16();
781 },
782 changeElementTo(1, f32))
783 .scalarizeIf(scalarOrEltWiderThan(1, 64), 1)
785 [=](const LegalityQuery &Query) {
786 const LLT &Ty = Query.Types[0];
787 const LLT &SrcTy = Query.Types[1];
788 return Ty.isVector() && !SrcTy.isPointerVector() &&
789 Ty.getElementType() != SrcTy.getElementType();
790 },
791 0, 1)
792 .clampNumElements(1, v4s16, v8s16)
793 .clampNumElements(1, v2s32, v4s32)
794 .clampMaxNumElements(1, s64, 2)
796 .libcallFor({{s32, s128}});
797
798 // Extensions
799 auto ExtLegalFunc = [=](const LegalityQuery &Query) {
800 unsigned DstSize = Query.Types[0].getSizeInBits();
801
802 // Handle legal vectors using legalFor
803 if (Query.Types[0].isVector())
804 return false;
805
806 if (DstSize < 8 || DstSize >= 128 || !isPowerOf2_32(DstSize))
807 return false; // Extending to a scalar s128 needs narrowing.
808
809 const LLT &SrcTy = Query.Types[1];
810
811 // Make sure we fit in a register otherwise. Don't bother checking that
812 // the source type is below 128 bits. We shouldn't be allowing anything
813 // through which is wider than the destination in the first place.
814 unsigned SrcSize = SrcTy.getSizeInBits();
815 if (SrcSize < 8 || !isPowerOf2_32(SrcSize))
816 return false;
817
818 return true;
819 };
820 getActionDefinitionsBuilder({G_ZEXT, G_SEXT, G_ANYEXT})
821 .legalIf(ExtLegalFunc)
822 .legalFor({{v8s16, v8s8}, {v4s32, v4s16}, {v2s64, v2s32}})
823 .clampScalar(0, s64, s64) // Just for s128, others are handled above.
825 .clampMaxNumElements(1, s8, 8)
826 .clampMaxNumElements(1, s16, 4)
827 .clampMaxNumElements(1, s32, 2)
828 // Tries to convert a large EXTEND into two smaller EXTENDs
829 .lowerIf([=](const LegalityQuery &Query) {
830 return (Query.Types[0].getScalarSizeInBits() >
831 Query.Types[1].getScalarSizeInBits() * 2) &&
832 Query.Types[0].isVector() &&
833 (Query.Types[1].getScalarSizeInBits() == 8 ||
834 Query.Types[1].getScalarSizeInBits() == 16);
835 })
836 .clampMinNumElements(1, s8, 8)
837 .clampMinNumElements(1, s16, 4)
839
841 .legalFor({{v8s8, v8s16}, {v4s16, v4s32}, {v2s32, v2s64}})
843 .clampMaxNumElements(0, s8, 8)
844 .clampMaxNumElements(0, s16, 4)
845 .clampMaxNumElements(0, s32, 2)
847 [=](const LegalityQuery &Query) { return Query.Types[0].isVector(); },
848 0, s8)
849 .lowerIf([=](const LegalityQuery &Query) {
850 LLT DstTy = Query.Types[0];
851 LLT SrcTy = Query.Types[1];
852 return DstTy.isVector() && SrcTy.getSizeInBits() > 128 &&
853 DstTy.getScalarSizeInBits() * 2 <= SrcTy.getScalarSizeInBits();
854 })
855 .clampMinNumElements(0, s8, 8)
856 .clampMinNumElements(0, s16, 4)
857 .alwaysLegal();
858
859 getActionDefinitionsBuilder({G_TRUNC_SSAT_S, G_TRUNC_SSAT_U, G_TRUNC_USAT_U})
860 .legalFor({{v8i8, v8i16}, {v4i16, v4i32}, {v2i32, v2i64}})
861 .clampNumElements(0, v2s32, v2s32);
862
863 getActionDefinitionsBuilder(G_SEXT_INREG)
864 .legalFor({i32, i64, v8i8, v16i8, v4i16, v8i16, v2i32, v4i32, v2i64})
865 .maxScalar(0, s64)
866 .clampNumElements(0, v8s8, v16s8)
867 .clampNumElements(0, v4s16, v8s16)
868 .clampNumElements(0, v2s32, v4s32)
869 .clampMaxNumElements(0, s64, 2)
870 .lower();
871
872 // FP conversions
874 .legalFor(
875 {{f16, f32}, {f16, f64}, {f32, f64}, {v4f16, v4f32}, {v2f32, v2f64}})
876 .legalFor(ST.hasBF16(), {{bf16, f32}, {v4bf16, v4f32}})
877 .libcallFor({{f16, f128}, {f32, f128}, {f64, f128}})
879 .customIf([](const LegalityQuery &Q) {
880 LLT DstTy = Q.Types[0];
881 LLT SrcTy = Q.Types[1];
882 return SrcTy.getScalarSizeInBits() == 64 &&
883 DstTy.getScalarSizeInBits() == 16;
884 })
885 .lowerFor({{bf16, f32}, {v4bf16, v4f32}})
886 // Clamp based on input
887 .clampNumElements(1, v4s32, v4s32)
888 .clampNumElements(1, v2s64, v2s64)
889 .scalarize(0);
890
891 getActionDefinitionsBuilder(G_FPEXT)
892 .legalFor({{f32, f16},
893 {f64, f16},
894 {f32, bf16},
895 {f64, f32},
896 {v4f32, v4f16},
897 {v4f32, v4bf16},
898 {v2f64, v2f32}})
899 .libcallFor({{f128, f64}, {f128, f32}, {f128, f16}})
902 [](const LegalityQuery &Q) {
903 LLT DstTy = Q.Types[0];
904 LLT SrcTy = Q.Types[1];
905 return SrcTy.isVector() && DstTy.isVector() &&
906 SrcTy.getScalarSizeInBits() == 16 &&
907 DstTy.getScalarSizeInBits() == 64;
908 },
909 changeElementTo(1, f32))
910 .clampNumElements(0, v4s32, v4s32)
911 .clampNumElements(0, v2s64, v2s64)
912 .scalarize(0);
913
914 // Conversions
915 getActionDefinitionsBuilder({G_FPTOSI, G_FPTOUI})
916 .legalFor({{i32, f32},
917 {i64, f32},
918 {i32, f64},
919 {i64, f64},
920 {v2i32, v2f32},
921 {v4i32, v4f32},
922 {v2i64, v2f64}})
923 .legalFor(HasFP16,
924 {{i32, f16}, {i64, f16}, {v4i16, v4f16}, {v8i16, v8f16}})
925 .scalarizeIf(scalarOrEltWiderThan(0, 64), 0)
927 // The range of a fp16 value fits into an i17, so we can lower the width
928 // to i64.
930 [=](const LegalityQuery &Query) {
931 return Query.Types[1] == f16 && Query.Types[0].getSizeInBits() > 64;
932 },
933 changeTo(0, i64))
936 .minScalar(0, s32)
938 [HasFP16](const LegalityQuery &Query) {
939 return (!HasFP16 && Query.Types[1].getScalarType().isFloat16()) ||
940 Query.Types[1].getScalarType().isBFloat16();
941 },
942 changeElementTo(1, f32))
943 .widenScalarIf(
944 [=](const LegalityQuery &Query) {
945 return Query.Types[0].getScalarSizeInBits() <= 64 &&
946 Query.Types[0].getScalarSizeInBits() >
947 Query.Types[1].getScalarSizeInBits();
948 },
950 .widenScalarIf(
951 [=](const LegalityQuery &Query) {
952 return Query.Types[1].getScalarSizeInBits() <= 64 &&
953 Query.Types[0].getScalarSizeInBits() <
954 Query.Types[1].getScalarSizeInBits();
955 },
957 .clampNumElements(0, v4s16, v8s16)
958 .clampNumElements(0, v2s32, v4s32)
959 .clampMaxNumElements(0, s64, 2)
960 .libcallFor(
961 {{i32, f128}, {i64, f128}, {i128, f128}, {i128, f32}, {i128, f64}});
962
963 getActionDefinitionsBuilder({G_FPTOSI_SAT, G_FPTOUI_SAT})
964 .legalFor({{i32, f32},
965 {i64, f32},
966 {i32, f64},
967 {i64, f64},
968 {v2i32, v2f32},
969 {v4i32, v4f32},
970 {v2i64, v2f64}})
971 .legalFor(
972 HasFP16,
973 {{i16, f16}, {i32, f16}, {i64, f16}, {v4i16, v4f16}, {v8i16, v8f16}})
974 // Handle types larger than i64 by scalarizing/lowering.
975 .scalarizeIf(scalarOrEltWiderThan(0, 64), 0)
977 // The range of a fp16 value fits into an i17, so we can lower the width
978 // to i64.
980 [=](const LegalityQuery &Query) {
981 return Query.Types[1] == f16 && Query.Types[0].getSizeInBits() > 64;
982 },
983 changeTo(0, i64))
984 .lowerIf(::any(scalarWiderThan(0, 64), scalarWiderThan(1, 64)), 0)
986 .widenScalarToNextPow2(0, /*MinSize=*/32)
987 .minScalar(0, s32)
989 [HasFP16](const LegalityQuery &Query) {
990 return (!HasFP16 && Query.Types[1].getScalarType().isFloat16()) ||
991 Query.Types[1].getScalarType().isBFloat16();
992 },
993 changeElementTo(1, f32))
994 .widenScalarIf(
995 [=](const LegalityQuery &Query) {
996 unsigned ITySize = Query.Types[0].getScalarSizeInBits();
997 return (ITySize == 16 || ITySize == 32 || ITySize == 64) &&
998 ITySize > Query.Types[1].getScalarSizeInBits();
999 },
1001 .widenScalarIf(
1002 [=](const LegalityQuery &Query) {
1003 unsigned FTySize = Query.Types[1].getScalarSizeInBits();
1004 return (FTySize == 16 || FTySize == 32 || FTySize == 64) &&
1005 Query.Types[0].getScalarSizeInBits() < FTySize;
1006 },
1009 .clampNumElements(0, v4s16, v8s16)
1010 .clampNumElements(0, v2s32, v4s32)
1011 .clampMaxNumElements(0, s64, 2);
1012
1013 getActionDefinitionsBuilder({G_SITOFP, G_UITOFP})
1014 .legalFor({{f32, i32},
1015 {f64, i32},
1016 {f32, i64},
1017 {f64, i64},
1018 {v2f32, v2i32},
1019 {v4f32, v4i32},
1020 {v2f64, v2i64}})
1021 .legalFor(HasFP16,
1022 {{f16, i32}, {f16, i64}, {v4f16, v4i16}, {v8f16, v8i16}})
1023 .unsupportedIf([&](const LegalityQuery &Query) {
1024 return Query.Types[0].getScalarType().isBFloat16();
1025 })
1026 .scalarizeIf(scalarOrEltWiderThan(1, 64), 1)
1030 .minScalar(1, f32)
1031 .lowerIf([](const LegalityQuery &Query) {
1032 return Query.Types[1].isVector() &&
1033 Query.Types[1].getScalarSizeInBits() == 64 &&
1034 Query.Types[0].getScalarSizeInBits() == 16;
1035 })
1036 .widenScalarOrEltToNextPow2OrMinSize(0, /*MinSize=*/HasFP16 ? 16 : 32)
1037 .scalarizeIf(
1038 // v2i64->v2f32 needs to scalarize to avoid double-rounding issues.
1039 [](const LegalityQuery &Query) {
1040 return Query.Types[0].getScalarSizeInBits() == 32 &&
1041 Query.Types[1].getScalarSizeInBits() == 64;
1042 },
1043 0)
1044 .widenScalarIf(
1045 [](const LegalityQuery &Query) {
1046 return Query.Types[1].getScalarSizeInBits() <= 64 &&
1047 Query.Types[0].getScalarSizeInBits() <
1048 Query.Types[1].getScalarSizeInBits();
1049 },
1051 .widenScalarIf(
1052 [](const LegalityQuery &Query) {
1053 return Query.Types[0].getScalarSizeInBits() <= 64 &&
1054 Query.Types[0].getScalarSizeInBits() >
1055 Query.Types[1].getScalarSizeInBits();
1056 },
1058 .clampNumElements(0, v4s16, v8s16)
1059 .clampNumElements(0, v2s32, v4s32)
1060 .clampMaxNumElements(0, s64, 2)
1061 .libcallFor({{f16, i128},
1062 {f32, i128},
1063 {f64, i128},
1064 {f128, i128},
1065 {f128, i32},
1066 {f128, i64}});
1067
1068 // Control-flow
1069 getActionDefinitionsBuilder(G_BR).alwaysLegal();
1070 getActionDefinitionsBuilder(G_BRCOND)
1071 .legalFor({s32})
1072 .clampScalar(0, s32, s32);
1073 getActionDefinitionsBuilder(G_BRINDIRECT).legalFor({p0});
1074
1075 getActionDefinitionsBuilder(G_SELECT)
1076 .legalFor({{s32, s32}, {s64, s32}, {p0, s32}})
1077 .widenScalarToNextPow2(0)
1078 .clampScalar(0, s32, s64)
1079 .clampScalar(1, s32, s32)
1082 .lowerIf(isVector(0));
1083
1084 // Pointer-handling
1085 getActionDefinitionsBuilder(G_FRAME_INDEX).legalFor({p0});
1086
1087 if (TM.getCodeModel() == CodeModel::Small)
1088 getActionDefinitionsBuilder(G_GLOBAL_VALUE).custom();
1089 else
1090 getActionDefinitionsBuilder(G_GLOBAL_VALUE).legalFor({p0});
1091
1092 getActionDefinitionsBuilder(G_PTRAUTH_GLOBAL_VALUE)
1093 .legalIf(all(typeIs(0, p0), typeIs(1, p0)));
1094
1095 getActionDefinitionsBuilder(G_PTRTOINT)
1096 .legalFor({{i64, p0}, {v2i64, v2p0}})
1097 .widenScalarToNextPow2(0, 64)
1098 .clampScalar(0, s64, s64)
1099 .clampMaxNumElements(0, s64, 2);
1100
1101 getActionDefinitionsBuilder(G_INTTOPTR)
1102 .unsupportedIf([&](const LegalityQuery &Query) {
1103 return Query.Types[0].getSizeInBits() != Query.Types[1].getSizeInBits();
1104 })
1105 .legalFor({{p0, i64}, {v2p0, v2i64}})
1106 .clampMaxNumElements(1, s64, 2);
1107
1108 // Casts for 32 and 64-bit width type are just copies.
1109 // Same for 128-bit width type, except they are on the FPR bank.
1110 getActionDefinitionsBuilder(G_BITCAST)
1112 // Keeping 32-bit instructions legal to prevent regression in some tests
1113 .legalForCartesianProduct({s32, v2s16, v4s8})
1114 .legalForCartesianProduct({s64, v8s8, v4s16, v2s32})
1115 .legalForCartesianProduct({s128, v16s8, v8s16, v4s32, v2s64, v2p0})
1116 .customIf([=](const LegalityQuery &Query) {
1117 // Handle casts from i1 vectors to scalars.
1118 LLT DstTy = Query.Types[0];
1119 LLT SrcTy = Query.Types[1];
1120 return DstTy.isScalar() && SrcTy.isVector() &&
1121 SrcTy.getScalarSizeInBits() == 1;
1122 })
1123 .lowerIf([=](const LegalityQuery &Query) {
1124 return Query.Types[0].isVector() != Query.Types[1].isVector();
1125 })
1127 .clampNumElements(0, v8s8, v16s8)
1128 .clampNumElements(0, v4s16, v8s16)
1129 .clampNumElements(0, v2s32, v4s32)
1130 .clampMaxNumElements(0, s64, 2)
1131 .lower();
1132
1133 getActionDefinitionsBuilder(G_VASTART).legalFor({p0});
1134
1135 // va_list must be a pointer, but most sized types are pretty easy to handle
1136 // as the destination.
1137 getActionDefinitionsBuilder(G_VAARG)
1138 .customForCartesianProduct({s8, s16, s32, s64, p0}, {p0})
1139 .clampScalar(0, s8, s64)
1140 .widenScalarToNextPow2(0, /*Min*/ 8);
1141
1142 getActionDefinitionsBuilder(G_ATOMIC_CMPXCHG_WITH_SUCCESS)
1143 .lowerIf(
1144 all(typeInSet(0, {s8, s16, s32, s64, s128}), typeIs(2, p0)));
1145
1146 bool UseOutlineAtomics = ST.outlineAtomics() && !ST.hasLSE();
1147
1148 getActionDefinitionsBuilder(G_ATOMIC_CMPXCHG)
1149 .legalFor(!UseOutlineAtomics, {{s32, p0}, {s64, p0}})
1150 .customFor(!UseOutlineAtomics, {{s128, p0}})
1151 .libcallFor(UseOutlineAtomics,
1152 {{s8, p0}, {s16, p0}, {s32, p0}, {s64, p0}, {s128, p0}})
1153 .clampScalar(0, s32, s64);
1154
1155 getActionDefinitionsBuilder({G_ATOMICRMW_XCHG, G_ATOMICRMW_ADD,
1156 G_ATOMICRMW_SUB, G_ATOMICRMW_AND, G_ATOMICRMW_OR,
1157 G_ATOMICRMW_XOR})
1158 .legalFor(!UseOutlineAtomics, {{s32, p0}, {s64, p0}})
1159 .libcallFor(UseOutlineAtomics,
1160 {{s8, p0}, {s16, p0}, {s32, p0}, {s64, p0}})
1161 .clampScalar(0, s32, s64);
1162
1163 // Do not outline these atomics operations, as per comment in
1164 // AArch64ISelLowering.cpp's shouldExpandAtomicRMWInIR().
1165 getActionDefinitionsBuilder(
1166 {G_ATOMICRMW_MIN, G_ATOMICRMW_MAX, G_ATOMICRMW_UMIN, G_ATOMICRMW_UMAX})
1167 .legalIf(all(typeInSet(0, {s32, s64}), typeIs(1, p0)))
1168 .clampScalar(0, s32, s64);
1169
1170 getActionDefinitionsBuilder(G_BLOCK_ADDR).legalFor({p0});
1171
1172 // Merge/Unmerge
1173 for (unsigned Op : {G_MERGE_VALUES, G_UNMERGE_VALUES}) {
1174 unsigned BigTyIdx = Op == G_MERGE_VALUES ? 0 : 1;
1175 unsigned LitTyIdx = Op == G_MERGE_VALUES ? 1 : 0;
1176 getActionDefinitionsBuilder(Op)
1177 .widenScalarToNextPow2(LitTyIdx, 8)
1178 .widenScalarToNextPow2(BigTyIdx, 32)
1179 .clampScalar(LitTyIdx, s8, s64)
1180 .clampScalar(BigTyIdx, s32, s128)
1181 .legalIf([=](const LegalityQuery &Q) {
1182 switch (Q.Types[BigTyIdx].getSizeInBits()) {
1183 case 32:
1184 case 64:
1185 case 128:
1186 break;
1187 default:
1188 return false;
1189 }
1190 switch (Q.Types[LitTyIdx].getSizeInBits()) {
1191 case 8:
1192 case 16:
1193 case 32:
1194 case 64:
1195 return true;
1196 default:
1197 return false;
1198 }
1199 });
1200 }
1201
1202 // TODO : nxv4s16, nxv2s16, nxv2s32
1203 getActionDefinitionsBuilder(G_EXTRACT_VECTOR_ELT)
1204 .legalFor(HasSVE, {{s16, nxv16s8, s64},
1205 {s16, nxv8s16, s64},
1206 {s32, nxv4s32, s64},
1207 {s64, nxv2s64, s64}})
1208 .unsupportedIf([=](const LegalityQuery &Query) {
1209 const LLT &EltTy = Query.Types[1].getElementType();
1210 if (Query.Types[1].isScalableVector())
1211 return false;
1212 return Query.Types[0] != EltTy;
1213 })
1214 .minScalar(2, s64)
1215 .customIf([=](const LegalityQuery &Query) {
1216 const LLT &VecTy = Query.Types[1];
1217 return VecTy == v8s8 || VecTy == v16s8 || VecTy == v2s16 ||
1218 VecTy == v4s16 || VecTy == v8s16 || VecTy == v2s32 ||
1219 VecTy == v4s32 || VecTy == v2s64 || VecTy == v2p0;
1220 })
1221 .minScalarOrEltIf(
1222 [=](const LegalityQuery &Query) {
1223 // We want to promote to <M x s1> to <M x s64> if that wouldn't
1224 // cause the total vec size to be > 128b.
1225 return Query.Types[1].isFixedVector() &&
1226 Query.Types[1].getNumElements() <= 2;
1227 },
1228 0, s64)
1229 .minScalarOrEltIf(
1230 [=](const LegalityQuery &Query) {
1231 return Query.Types[1].isFixedVector() &&
1232 Query.Types[1].getNumElements() <= 4;
1233 },
1234 0, s32)
1235 .minScalarOrEltIf(
1236 [=](const LegalityQuery &Query) {
1237 return Query.Types[1].isFixedVector() &&
1238 Query.Types[1].getNumElements() <= 8;
1239 },
1240 0, s16)
1241 .minScalarOrEltIf(
1242 [=](const LegalityQuery &Query) {
1243 return Query.Types[1].isFixedVector() &&
1244 Query.Types[1].getNumElements() <= 16;
1245 },
1246 0, s8)
1247 .minScalarOrElt(0, s8) // Worst case, we need at least s8.
1248 .moreElementsToNextPow2(1)
1249 .clampMaxNumElements(1, s64, 2)
1250 .clampMaxNumElements(1, s32, 4)
1251 .clampMaxNumElements(1, s16, 8)
1252 .clampMaxNumElements(1, s8, 16)
1253 .clampMaxNumElements(1, p0, 2)
1254 .scalarizeIf(scalarOrEltWiderThan(1, 64), 1);
1255
1256 getActionDefinitionsBuilder(G_INSERT_VECTOR_ELT)
1257 .legalIf(
1258 typeInSet(0, {v8s8, v16s8, v4s16, v8s16, v2s32, v4s32, v2s64, v2p0}))
1259 .legalFor(HasSVE, {{nxv16s8, s32, s64},
1260 {nxv8s16, s32, s64},
1261 {nxv4s32, s32, s64},
1262 {nxv2s64, s64, s64}})
1264 .widenVectorEltsToVectorMinSize(0, 64)
1265 .clampNumElements(0, v8s8, v16s8)
1266 .clampNumElements(0, v4s16, v8s16)
1267 .clampNumElements(0, v2s32, v4s32)
1268 .clampMaxNumElements(0, s64, 2)
1269 .clampMaxNumElements(0, p0, 2)
1270 .scalarizeIf(scalarOrEltWiderThan(0, 64), 0);
1271
1272 getActionDefinitionsBuilder(G_BUILD_VECTOR)
1273 .legalFor({{v8s8, s8},
1274 {v16s8, s8},
1275 {v4s16, s16},
1276 {v8s16, s16},
1277 {v2s32, s32},
1278 {v4s32, s32},
1279 {v2s64, s64},
1280 {v2p0, p0}})
1281 .clampNumElements(0, v4s32, v4s32)
1282 .clampNumElements(0, v2s64, v2s64)
1283 .minScalarOrElt(0, s8)
1284 .widenVectorEltsToVectorMinSize(0, 64)
1285 .widenScalarOrEltToNextPow2(0)
1286 .minScalarSameAs(1, 0);
1287
1288 getActionDefinitionsBuilder(G_BUILD_VECTOR_TRUNC).lower();
1289
1290 getActionDefinitionsBuilder(G_SHUFFLE_VECTOR)
1291 .legalIf([=](const LegalityQuery &Query) {
1292 const LLT &DstTy = Query.Types[0];
1293 const LLT &SrcTy = Query.Types[1];
1294 // For now just support the TBL2 variant which needs the source vectors
1295 // to be the same size as the dest.
1296 if (DstTy != SrcTy)
1297 return false;
1298 return llvm::is_contained(
1299 {v8s8, v16s8, v4s16, v8s16, v2s32, v4s32, v2s64}, DstTy);
1300 })
1301 .moreElementsIf(
1302 [](const LegalityQuery &Query) {
1303 return Query.Types[0].getNumElements() >
1304 Query.Types[1].getNumElements();
1305 },
1306 changeTo(1, 0))
1308 .moreElementsIf(
1309 [](const LegalityQuery &Query) {
1310 return Query.Types[0].getNumElements() <
1311 Query.Types[1].getNumElements();
1312 },
1313 changeTo(0, 1))
1314 .widenScalarOrEltToNextPow2OrMinSize(0, 8)
1315 .clampNumElements(0, v8s8, v16s8)
1316 .clampNumElements(0, v4s16, v8s16)
1317 .clampNumElements(0, v4s32, v4s32)
1318 .clampNumElements(0, v2s64, v2s64)
1319 .scalarizeIf(scalarOrEltWiderThan(0, 64), 0)
1320 .bitcastIf(isPointerVector(0), [=](const LegalityQuery &Query) {
1321 // Bitcast pointers vector to i64.
1322 const LLT DstTy = Query.Types[0];
1323 return std::pair(
1324 0, LLT::vector(DstTy.getElementCount(), LLT::integer(64)));
1325 });
1326
1327 getActionDefinitionsBuilder(G_CONCAT_VECTORS)
1328 .legalFor({{v16s8, v8s8}, {v8s16, v4s16}, {v4s32, v2s32}})
1329 .customIf([=](const LegalityQuery &Query) {
1330 return Query.Types[0].isFixedVector() &&
1331 Query.Types[0].getScalarSizeInBits() < 8;
1332 })
1333 .bitcastIf(
1334 [=](const LegalityQuery &Query) {
1335 return Query.Types[0].isFixedVector() &&
1336 Query.Types[1].isFixedVector() &&
1337 Query.Types[0].getScalarSizeInBits() >= 8 &&
1338 isPowerOf2_64(Query.Types[0].getScalarSizeInBits()) &&
1339 Query.Types[0].getSizeInBits() <= 128 &&
1340 Query.Types[1].getSizeInBits() <= 64;
1341 },
1342 [=](const LegalityQuery &Query) {
1343 const LLT DstTy = Query.Types[0];
1344 const LLT SrcTy = Query.Types[1];
1345 return std::pair(
1346 0, DstTy.changeElementSize(SrcTy.getSizeInBits())
1349 SrcTy.getNumElements())));
1350 });
1351
1352 getActionDefinitionsBuilder(G_EXTRACT_SUBVECTOR)
1353 .legalFor({{v8s8, v16s8}, {v4s16, v8s16}, {v2s32, v4s32}})
1355 .clampMaxNumElements(0, s8, 16)
1356 .clampMaxNumElements(0, s16, 8)
1357 .clampMaxNumElements(0, s32, 4)
1358 .clampNumElements(1, v8s8, v16s8)
1359 .clampNumElements(1, v4s16, v8s16)
1360 .clampNumElements(1, v2s32, v4s32)
1361 .lower()
1362 .immIdx(0); // Inform verifier imm idx 0 is handled.
1363
1364 // TODO: {nxv16s8, s8}, {nxv8s16, s16}
1365 getActionDefinitionsBuilder(G_SPLAT_VECTOR)
1366 .legalFor(HasSVE, {{nxv4s32, s32}, {nxv2s64, s64}});
1367
1368 getActionDefinitionsBuilder(G_JUMP_TABLE).legalFor({p0});
1369
1370 getActionDefinitionsBuilder(G_BRJT).legalFor({{p0, s64}});
1371
1372 getActionDefinitionsBuilder({G_TRAP, G_DEBUGTRAP, G_UBSANTRAP}).alwaysLegal();
1373
1374 getActionDefinitionsBuilder(G_DYN_STACKALLOC).custom();
1375
1376 getActionDefinitionsBuilder({G_STACKSAVE, G_STACKRESTORE}).lower();
1377
1378 if (ST.hasMOPS()) {
1379 // G_BZERO is not supported. Currently it is only emitted by
1380 // PreLegalizerCombiner for G_MEMSET with zero constant.
1381 getActionDefinitionsBuilder(G_BZERO).unsupported();
1382
1383 getActionDefinitionsBuilder(G_MEMSET)
1384 .legalForCartesianProduct({p0}, {s64}, {s64})
1385 .customForCartesianProduct({p0}, {s8}, {s64})
1386 .immIdx(0); // Inform verifier imm idx 0 is handled.
1387
1388 getActionDefinitionsBuilder({G_MEMCPY, G_MEMMOVE})
1389 .legalForCartesianProduct({p0}, {p0}, {s64})
1390 .immIdx(0); // Inform verifier imm idx 0 is handled.
1391
1392 // G_MEMCPY_INLINE does not have a tailcall immediate
1393 getActionDefinitionsBuilder(G_MEMCPY_INLINE)
1394 .legalForCartesianProduct({p0}, {p0}, {s64});
1395
1396 getActionDefinitionsBuilder(G_MEMSET_INLINE)
1397 .legalForCartesianProduct({p0}, {s64}, {s64})
1398 .customForCartesianProduct({p0}, {s8}, {s64});
1399 } else {
1400 getActionDefinitionsBuilder({G_BZERO, G_MEMCPY, G_MEMMOVE, G_MEMSET})
1401 .libcall();
1402 }
1403
1404 // For fadd reductions we have pairwise operations available. We treat the
1405 // usual legal types as legal and handle the lowering to pairwise instructions
1406 // later.
1407 getActionDefinitionsBuilder(G_VECREDUCE_FADD)
1408 .legalFor({{f32, v2f32}, {f32, v4f32}, {f64, v2f64}})
1409 .legalFor(HasFP16, {{f16, v4f16}, {f16, v8f16}})
1410 .widenScalarIf(
1411 [HasFP16](const LegalityQuery &Query) {
1412 return (!HasFP16 && Query.Types[0].getScalarType().isFloat16()) ||
1413 Query.Types[0].getScalarType().isBFloat16();
1414 },
1415 changeElementTo(0, f32))
1416 .clampMaxNumElements(1, s64, 2)
1417 .clampMaxNumElements(1, s32, 4)
1418 .clampMaxNumElements(1, s16, 8)
1419 .moreElementsToNextPow2(1)
1420 .scalarize(1)
1421 .lower();
1422
1423 // For fmul reductions we need to split up into individual operations. We
1424 // clamp to 128 bit vectors then to 64bit vectors to produce a cascade of
1425 // smaller types, followed by scalarizing what remains.
1426 getActionDefinitionsBuilder(G_VECREDUCE_FMUL)
1427 .widenScalarIf(
1428 [HasFP16](const LegalityQuery &Query) {
1429 return (!HasFP16 && Query.Types[0].getScalarType().isFloat16()) ||
1430 Query.Types[0].getScalarType().isBFloat16();
1431 },
1432 changeElementTo(0, f32))
1433 .clampMaxNumElements(1, s64, 2)
1434 .clampMaxNumElements(1, s32, 4)
1435 .clampMaxNumElements(1, s16, 8)
1436 .clampMaxNumElements(1, s32, 2)
1437 .clampMaxNumElements(1, s16, 4)
1438 .scalarize(1)
1439 .lower();
1440
1441 getActionDefinitionsBuilder({G_VECREDUCE_SEQ_FADD, G_VECREDUCE_SEQ_FMUL})
1442 .scalarize(2)
1443 .lower();
1444
1445 getActionDefinitionsBuilder(G_VECREDUCE_ADD)
1446 .legalFor({{i8, v8i8},
1447 {i8, v16i8},
1448 {i16, v4i16},
1449 {i16, v8i16},
1450 {i32, v2i32},
1451 {i32, v4i32},
1452 {i64, v2i64}})
1454 .clampMaxNumElements(1, s64, 2)
1455 .clampMaxNumElements(1, s32, 4)
1456 .clampMaxNumElements(1, s16, 8)
1457 .clampMaxNumElements(1, s8, 16)
1458 .widenVectorEltsToVectorMinSize(1, 64)
1459 .scalarize(1);
1460
1461 getActionDefinitionsBuilder({G_VECREDUCE_FMIN, G_VECREDUCE_FMAX,
1462 G_VECREDUCE_FMINIMUM, G_VECREDUCE_FMAXIMUM})
1463 .legalFor({{f32, v2f32}, {f32, v4f32}, {f64, v2f64}})
1464 .legalFor(HasFP16, {{f16, v4f16}, {f16, v8f16}})
1465 .widenScalarIf(
1466 [HasFP16](const LegalityQuery &Query) {
1467 return (!HasFP16 && Query.Types[0].getScalarType().isFloat16()) ||
1468 Query.Types[0].getScalarType().isBFloat16();
1469 },
1470 changeElementTo(0, f32))
1471 .clampMaxNumElements(1, s64, 2)
1472 .clampMaxNumElements(1, s32, 4)
1473 .clampMaxNumElements(1, s16, 8)
1474 .scalarize(1)
1475 .lower();
1476
1477 getActionDefinitionsBuilder(G_VECREDUCE_MUL)
1478 .clampMaxNumElements(1, s32, 2)
1479 .clampMaxNumElements(1, s16, 4)
1480 .clampMaxNumElements(1, s8, 8)
1481 .scalarize(1)
1482 .lower();
1483
1484 getActionDefinitionsBuilder(
1485 {G_VECREDUCE_SMIN, G_VECREDUCE_SMAX, G_VECREDUCE_UMIN, G_VECREDUCE_UMAX})
1486 .legalFor({{i8, v8i8},
1487 {i8, v16i8},
1488 {i16, v4i16},
1489 {i16, v8i16},
1490 {i32, v2i32},
1491 {i32, v4i32}})
1492 .moreElementsIf(
1493 [=](const LegalityQuery &Query) {
1494 return Query.Types[1].isVector() &&
1495 Query.Types[1].getElementType() != s8 &&
1496 Query.Types[1].getNumElements() & 1;
1497 },
1499 .clampMaxNumElements(1, s64, 2)
1500 .clampMaxNumElements(1, s32, 4)
1501 .clampMaxNumElements(1, s16, 8)
1502 .clampMaxNumElements(1, s8, 16)
1503 .scalarize(1)
1504 .lower();
1505
1506 getActionDefinitionsBuilder(
1507 {G_VECREDUCE_OR, G_VECREDUCE_AND, G_VECREDUCE_XOR})
1508 // Try to break down into smaller vectors as long as they're at least 64
1509 // bits. This lets us use vector operations for some parts of the
1510 // reduction.
1511 .fewerElementsIf(
1512 [=](const LegalityQuery &Q) {
1513 LLT SrcTy = Q.Types[1];
1514 if (SrcTy.isScalar())
1515 return false;
1516 if (!isPowerOf2_32(SrcTy.getNumElements()))
1517 return false;
1518 // We can usually perform 64b vector operations.
1519 return SrcTy.getSizeInBits() > 64;
1520 },
1521 [=](const LegalityQuery &Q) {
1522 LLT SrcTy = Q.Types[1];
1523 return std::make_pair(1, SrcTy.divide(2));
1524 })
1525 .scalarize(1)
1526 .lower();
1527
1528 // TODO: Update this to correct handling when adding AArch64/SVE support.
1529 getActionDefinitionsBuilder(G_VECTOR_COMPRESS).lower();
1530
1531 // Access to floating-point environment.
1532 getActionDefinitionsBuilder({G_GET_FPENV, G_SET_FPENV, G_RESET_FPENV,
1533 G_GET_FPMODE, G_SET_FPMODE, G_RESET_FPMODE})
1534 .libcall();
1535
1536 getActionDefinitionsBuilder(G_IS_FPCLASS).lower();
1537
1538 getActionDefinitionsBuilder(G_PREFETCH).custom();
1539
1540 getActionDefinitionsBuilder({G_SCMP, G_UCMP}).lower();
1541
1542 getActionDefinitionsBuilder({G_INTRINSIC, G_INTRINSIC_W_SIDE_EFFECTS})
1543 .alwaysLegal();
1544 getActionDefinitionsBuilder(G_FENCE).alwaysLegal();
1545 getActionDefinitionsBuilder(G_INVOKE_REGION_START).alwaysLegal();
1546
1547 verify(*ST.getInstrInfo());
1548}
1549
1552 LostDebugLocObserver &LocObserver) const {
1553 MachineIRBuilder &MIRBuilder = Helper.MIRBuilder;
1554 MachineRegisterInfo &MRI = *MIRBuilder.getMRI();
1555 GISelChangeObserver &Observer = Helper.Observer;
1556 switch (MI.getOpcode()) {
1557 default:
1558 // No idea what to do.
1559 return false;
1560 case TargetOpcode::G_VAARG:
1561 return legalizeVaArg(MI, MRI, MIRBuilder);
1562 case TargetOpcode::G_LOAD:
1563 case TargetOpcode::G_STORE:
1564 return legalizeLoadStore(MI, MRI, MIRBuilder, Observer);
1565 case TargetOpcode::G_SHL:
1566 case TargetOpcode::G_ASHR:
1567 case TargetOpcode::G_LSHR:
1568 return legalizeShlAshrLshr(MI, MRI, MIRBuilder, Observer);
1569 case TargetOpcode::G_GLOBAL_VALUE:
1570 return legalizeSmallCMGlobalValue(MI, MRI, MIRBuilder, Observer);
1571 case TargetOpcode::G_SBFX:
1572 case TargetOpcode::G_UBFX:
1573 return legalizeBitfieldExtract(MI, MRI, Helper);
1574 case TargetOpcode::G_FSHL:
1575 case TargetOpcode::G_FSHR:
1576 return legalizeFunnelShift(MI, MRI, MIRBuilder, Observer, Helper);
1577 case TargetOpcode::G_ROTR:
1578 return legalizeRotate(MI, MRI, Helper);
1579 case TargetOpcode::G_CTPOP:
1580 return legalizeCTPOP(MI, MRI, Helper);
1581 case TargetOpcode::G_ATOMIC_CMPXCHG:
1582 return legalizeAtomicCmpxchg128(MI, MRI, Helper);
1583 case TargetOpcode::G_CTTZ:
1584 return legalizeCTTZ(MI, Helper);
1585 case TargetOpcode::G_BZERO:
1586 case TargetOpcode::G_MEMCPY:
1587 case TargetOpcode::G_MEMMOVE:
1588 case TargetOpcode::G_MEMSET:
1589 case TargetOpcode::G_MEMSET_INLINE:
1590 return legalizeMemOps(MI, Helper);
1591 case TargetOpcode::G_EXTRACT_VECTOR_ELT:
1592 return legalizeExtractVectorElt(MI, MRI, Helper);
1593 case TargetOpcode::G_DYN_STACKALLOC:
1594 return legalizeDynStackAlloc(MI, Helper);
1595 case TargetOpcode::G_PREFETCH:
1596 return legalizePrefetch(MI, Helper);
1597 case TargetOpcode::G_ABS:
1598 return Helper.lowerAbsToCNeg(MI);
1599 case TargetOpcode::G_ICMP:
1600 return legalizeICMP(MI, MRI, MIRBuilder);
1601 case TargetOpcode::G_BITCAST:
1602 return legalizeBitcast(MI, Helper);
1603 case TargetOpcode::G_CONCAT_VECTORS:
1604 return legalizeConcatVectors(MI, MRI, MIRBuilder);
1605 case TargetOpcode::G_FPTRUNC:
1606 // In order to lower f16 to f64 properly, we need to use f32 as an
1607 // intermediary
1608 return legalizeFptrunc(MI, MIRBuilder, MRI);
1609 }
1610
1611 llvm_unreachable("expected switch to return");
1612}
1613
1614bool AArch64LegalizerInfo::legalizeBitcast(MachineInstr &MI,
1615 LegalizerHelper &Helper) const {
1616 assert(MI.getOpcode() == TargetOpcode::G_BITCAST && "Unexpected opcode");
1617 auto [DstReg, DstTy, SrcReg, SrcTy] = MI.getFirst2RegLLTs();
1618 // We're trying to handle casts from i1 vectors to scalars but reloading from
1619 // stack.
1620 if (!DstTy.isScalar() || !SrcTy.isVector() ||
1621 SrcTy.getElementType() != LLT::scalar(1))
1622 return false;
1623
1624 Helper.createStackStoreLoad(DstReg, SrcReg);
1625 MI.eraseFromParent();
1626 return true;
1627}
1628
1629bool AArch64LegalizerInfo::legalizeFunnelShift(MachineInstr &MI,
1631 MachineIRBuilder &MIRBuilder,
1632 GISelChangeObserver &Observer,
1633 LegalizerHelper &Helper) const {
1634 assert(MI.getOpcode() == TargetOpcode::G_FSHL ||
1635 MI.getOpcode() == TargetOpcode::G_FSHR);
1636
1637 // Keep as G_FSHR if shift amount is a G_CONSTANT, else use generic
1638 // lowering
1639 Register ShiftNo = MI.getOperand(3).getReg();
1640 LLT ShiftTy = MRI.getType(ShiftNo);
1641 auto VRegAndVal = getIConstantVRegValWithLookThrough(ShiftNo, MRI);
1642
1643 // Adjust shift amount according to Opcode (FSHL/FSHR)
1644 // Convert FSHL to FSHR
1645 LLT OperationTy = MRI.getType(MI.getOperand(0).getReg());
1646 APInt BitWidth(ShiftTy.getSizeInBits(), OperationTy.getSizeInBits(), false);
1647
1648 // Lower non-constant shifts and leave zero shifts to the optimizer.
1649 if (!VRegAndVal || VRegAndVal->Value.urem(BitWidth) == 0)
1650 return (Helper.lowerFunnelShiftAsShifts(MI) ==
1652
1653 APInt Amount = VRegAndVal->Value.urem(BitWidth);
1654
1655 Amount = MI.getOpcode() == TargetOpcode::G_FSHL ? BitWidth - Amount : Amount;
1656
1657 // If the instruction is G_FSHR, has a 64-bit G_CONSTANT for shift amount
1658 // in the range of 0 <-> BitWidth, it is legal
1659 if (ShiftTy.getSizeInBits() == 64 && MI.getOpcode() == TargetOpcode::G_FSHR &&
1660 VRegAndVal->Value.ult(BitWidth))
1661 return true;
1662
1663 // Cast the ShiftNumber to a 64-bit type
1664 auto Cast64 = MIRBuilder.buildConstant(LLT::integer(64), Amount.zext(64));
1665
1666 if (MI.getOpcode() == TargetOpcode::G_FSHR) {
1667 Observer.changingInstr(MI);
1668 MI.getOperand(3).setReg(Cast64.getReg(0));
1669 Observer.changedInstr(MI);
1670 }
1671 // If Opcode is FSHL, remove the FSHL instruction and create a FSHR
1672 // instruction
1673 else if (MI.getOpcode() == TargetOpcode::G_FSHL) {
1674 MIRBuilder.buildInstr(TargetOpcode::G_FSHR, {MI.getOperand(0).getReg()},
1675 {MI.getOperand(1).getReg(), MI.getOperand(2).getReg(),
1676 Cast64.getReg(0)});
1677 MI.eraseFromParent();
1678 }
1679 return true;
1680}
1681
1682bool AArch64LegalizerInfo::legalizeICMP(MachineInstr &MI,
1684 MachineIRBuilder &MIRBuilder) const {
1685 Register DstReg = MI.getOperand(0).getReg();
1686 Register SrcReg1 = MI.getOperand(2).getReg();
1687 Register SrcReg2 = MI.getOperand(3).getReg();
1688 LLT DstTy = MRI.getType(DstReg);
1689 LLT SrcTy = MRI.getType(SrcReg1);
1690
1691 // Check the vector types are legal
1692 if (DstTy.getScalarSizeInBits() != SrcTy.getScalarSizeInBits() ||
1693 DstTy.getNumElements() != SrcTy.getNumElements() ||
1694 (DstTy.getSizeInBits() != 64 && DstTy.getSizeInBits() != 128))
1695 return false;
1696
1697 // Lowers G_ICMP NE => G_ICMP EQ to allow better pattern matching for
1698 // following passes
1699 CmpInst::Predicate Pred = (CmpInst::Predicate)MI.getOperand(1).getPredicate();
1700 if (Pred != CmpInst::ICMP_NE)
1701 return true;
1702 Register CmpReg =
1703 MIRBuilder
1704 .buildICmp(CmpInst::ICMP_EQ, MRI.getType(DstReg), SrcReg1, SrcReg2)
1705 .getReg(0);
1706 MIRBuilder.buildNot(DstReg, CmpReg);
1707
1708 MI.eraseFromParent();
1709 return true;
1710}
1711
1712bool AArch64LegalizerInfo::legalizeRotate(MachineInstr &MI,
1714 LegalizerHelper &Helper) const {
1715 // To allow for imported patterns to match, we ensure that the rotate amount
1716 // is 64b with an extension.
1717 Register AmtReg = MI.getOperand(2).getReg();
1718 LLT AmtTy = MRI.getType(AmtReg);
1719 (void)AmtTy;
1720 assert(AmtTy.isScalar() && "Expected a scalar rotate");
1721 assert(AmtTy.getSizeInBits() < 64 && "Expected this rotate to be legal");
1722 auto NewAmt = Helper.MIRBuilder.buildZExt(LLT::integer(64), AmtReg);
1723 Helper.Observer.changingInstr(MI);
1724 MI.getOperand(2).setReg(NewAmt.getReg(0));
1725 Helper.Observer.changedInstr(MI);
1726 return true;
1727}
1728
1729bool AArch64LegalizerInfo::legalizeSmallCMGlobalValue(
1731 GISelChangeObserver &Observer) const {
1732 assert(MI.getOpcode() == TargetOpcode::G_GLOBAL_VALUE);
1733 // We do this custom legalization to convert G_GLOBAL_VALUE into target ADRP +
1734 // G_ADD_LOW instructions.
1735 // By splitting this here, we can optimize accesses in the small code model by
1736 // folding in the G_ADD_LOW into the load/store offset.
1737 auto &GlobalOp = MI.getOperand(1);
1738 // Don't modify an intrinsic call.
1739 if (GlobalOp.isSymbol())
1740 return true;
1741 const auto* GV = GlobalOp.getGlobal();
1742 if (GV->isThreadLocal())
1743 return true; // Don't want to modify TLS vars.
1744
1745 auto &TM = ST->getTargetLowering()->getTargetMachine();
1746 unsigned OpFlags = ST->ClassifyGlobalReference(GV, TM);
1747
1748 if (OpFlags & AArch64II::MO_GOT)
1749 return true;
1750
1751 auto Offset = GlobalOp.getOffset();
1752 Register DstReg = MI.getOperand(0).getReg();
1753 auto ADRP = MIRBuilder.buildInstr(AArch64::ADRP, {LLT::pointer(0, 64)}, {})
1754 .addGlobalAddress(GV, Offset, OpFlags | AArch64II::MO_PAGE);
1755 // Set the regclass on the dest reg too.
1756 MRI.setRegClass(ADRP.getReg(0), &AArch64::GPR64RegClass);
1757
1758 // MO_TAGGED on the page indicates a tagged address. Set the tag now. We do so
1759 // by creating a MOVK that sets bits 48-63 of the register to (global address
1760 // + 0x100000000 - PC) >> 48. The additional 0x100000000 offset here is to
1761 // prevent an incorrect tag being generated during relocation when the
1762 // global appears before the code section. Without the offset, a global at
1763 // `0x0f00'0000'0000'1000` (i.e. at `0x1000` with tag `0xf`) that's referenced
1764 // by code at `0x2000` would result in `0x0f00'0000'0000'1000 - 0x2000 =
1765 // 0x0eff'ffff'ffff'f000`, meaning the tag would be incorrectly set to `0xe`
1766 // instead of `0xf`.
1767 // This assumes that we're in the small code model so we can assume a binary
1768 // size of <= 4GB, which makes the untagged PC relative offset positive. The
1769 // binary must also be loaded into address range [0, 2^48). Both of these
1770 // properties need to be ensured at runtime when using tagged addresses.
1771 if (OpFlags & AArch64II::MO_TAGGED) {
1772 assert(!Offset &&
1773 "Should not have folded in an offset for a tagged global!");
1774 ADRP = MIRBuilder.buildInstr(AArch64::MOVKXi, {LLT::pointer(0, 64)}, {ADRP})
1775 .addGlobalAddress(GV, 0x100000000,
1777 .addImm(48);
1778 MRI.setRegClass(ADRP.getReg(0), &AArch64::GPR64RegClass);
1779 }
1780
1781 MIRBuilder.buildInstr(AArch64::G_ADD_LOW, {DstReg}, {ADRP})
1782 .addGlobalAddress(GV, Offset,
1784 MI.eraseFromParent();
1785 return true;
1786}
1787
1789 MachineInstr &MI) const {
1790 MachineIRBuilder &MIB = Helper.MIRBuilder;
1791 MachineRegisterInfo &MRI = *MIB.getMRI();
1792
1793 auto LowerUnaryOp = [&MI, &MIB](unsigned Opcode) {
1794 MIB.buildInstr(Opcode, {MI.getOperand(0)}, {MI.getOperand(2)});
1795 MI.eraseFromParent();
1796 return true;
1797 };
1798 auto LowerBinOp = [&MI, &MIB](unsigned Opcode) {
1799 MIB.buildInstr(Opcode, {MI.getOperand(0)},
1800 {MI.getOperand(2), MI.getOperand(3)});
1801 MI.eraseFromParent();
1802 return true;
1803 };
1804 auto LowerTriOp = [&MI, &MIB](unsigned Opcode) {
1805 MIB.buildInstr(Opcode, {MI.getOperand(0)},
1806 {MI.getOperand(2), MI.getOperand(3), MI.getOperand(4)});
1807 MI.eraseFromParent();
1808 return true;
1809 };
1810
1811 Intrinsic::ID IntrinsicID = cast<GIntrinsic>(MI).getIntrinsicID();
1812 switch (IntrinsicID) {
1813 case Intrinsic::vacopy: {
1814 unsigned PtrSize = ST->isTargetILP32() ? 4 : 8;
1815 unsigned VaListSize =
1816 (ST->isTargetDarwin() || ST->isTargetWindows())
1817 ? PtrSize
1818 : ST->isTargetILP32() ? 20 : 32;
1819
1820 MachineFunction &MF = *MI.getMF();
1822 LLT::scalar(VaListSize * 8));
1823 MIB.buildLoad(Val, MI.getOperand(2),
1826 VaListSize, Align(PtrSize)));
1827 MIB.buildStore(Val, MI.getOperand(1),
1830 VaListSize, Align(PtrSize)));
1831 MI.eraseFromParent();
1832 return true;
1833 }
1834 case Intrinsic::get_dynamic_area_offset: {
1835 MIB.buildConstant(MI.getOperand(0).getReg(), 0);
1836 MI.eraseFromParent();
1837 return true;
1838 }
1839 case Intrinsic::aarch64_mops_memset_tag: {
1840 assert(MI.getOpcode() == TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS);
1841 // Anyext the value being set to 64 bit (only the bottom 8 bits are read by
1842 // the instruction).
1843 auto &Value = MI.getOperand(3);
1844 Register ExtValueReg = MIB.buildAnyExt(LLT::integer(64), Value).getReg(0);
1845 Value.setReg(ExtValueReg);
1846 return true;
1847 }
1848 case Intrinsic::aarch64_prefetch: {
1849 auto &AddrVal = MI.getOperand(1);
1850
1851 int64_t IsWrite = MI.getOperand(2).getImm();
1852 int64_t Target = MI.getOperand(3).getImm();
1853 int64_t IsStream = MI.getOperand(4).getImm();
1854 int64_t IsData = MI.getOperand(5).getImm();
1855
1856 unsigned PrfOp = (IsWrite << 4) | // Load/Store bit
1857 (!IsData << 3) | // IsDataCache bit
1858 (Target << 1) | // Cache level bits
1859 (unsigned)IsStream; // Stream bit
1860
1861 MIB.buildInstr(AArch64::G_AARCH64_PREFETCH).addImm(PrfOp).add(AddrVal);
1862 MI.eraseFromParent();
1863 return true;
1864 }
1865 case Intrinsic::aarch64_range_prefetch: {
1866 auto &AddrVal = MI.getOperand(1);
1867
1868 int64_t IsWrite = MI.getOperand(2).getImm();
1869 int64_t IsStream = MI.getOperand(3).getImm();
1870 unsigned PrfOp = (IsStream << 2) | IsWrite;
1871
1872 MIB.buildInstr(AArch64::G_AARCH64_RANGE_PREFETCH)
1873 .addImm(PrfOp)
1874 .add(AddrVal)
1875 .addUse(MI.getOperand(4).getReg()); // Metadata
1876 MI.eraseFromParent();
1877 return true;
1878 }
1879 case Intrinsic::aarch64_prefetch_ir: {
1880 auto &AddrVal = MI.getOperand(1);
1881 MIB.buildInstr(AArch64::G_AARCH64_PREFETCH).addImm(24).add(AddrVal);
1882 MI.eraseFromParent();
1883 return true;
1884 }
1885 case Intrinsic::aarch64_neon_uaddv:
1886 case Intrinsic::aarch64_neon_saddv:
1887 case Intrinsic::aarch64_neon_umaxv:
1888 case Intrinsic::aarch64_neon_smaxv:
1889 case Intrinsic::aarch64_neon_uminv:
1890 case Intrinsic::aarch64_neon_sminv: {
1891 bool IsSigned = IntrinsicID == Intrinsic::aarch64_neon_saddv ||
1892 IntrinsicID == Intrinsic::aarch64_neon_smaxv ||
1893 IntrinsicID == Intrinsic::aarch64_neon_sminv;
1894
1895 auto OldDst = MI.getOperand(0).getReg();
1896 auto OldDstTy = MRI.getType(OldDst);
1897 LLT NewDstTy = MRI.getType(MI.getOperand(2).getReg()).getElementType();
1898 if (OldDstTy == NewDstTy)
1899 return true;
1900
1901 auto NewDst = MRI.createGenericVirtualRegister(NewDstTy);
1902
1903 Helper.Observer.changingInstr(MI);
1904 MI.getOperand(0).setReg(NewDst);
1905 Helper.Observer.changedInstr(MI);
1906
1907 MIB.setInsertPt(MIB.getMBB(), ++MIB.getInsertPt());
1908 MIB.buildExtOrTrunc(IsSigned ? TargetOpcode::G_SEXT : TargetOpcode::G_ZEXT,
1909 OldDst, NewDst);
1910
1911 return true;
1912 }
1913 case Intrinsic::aarch64_neon_uaddlp:
1914 case Intrinsic::aarch64_neon_saddlp: {
1915 unsigned Opc = IntrinsicID == Intrinsic::aarch64_neon_uaddlp
1916 ? AArch64::G_UADDLP
1917 : AArch64::G_SADDLP;
1918 MIB.buildInstr(Opc, {MI.getOperand(0)}, {MI.getOperand(2)});
1919 MI.eraseFromParent();
1920
1921 return true;
1922 }
1923 case Intrinsic::aarch64_neon_uaddlv:
1924 case Intrinsic::aarch64_neon_saddlv: {
1925 unsigned Opc = IntrinsicID == Intrinsic::aarch64_neon_uaddlv
1926 ? AArch64::G_UADDLV
1927 : AArch64::G_SADDLV;
1928 Register DstReg = MI.getOperand(0).getReg();
1929 Register SrcReg = MI.getOperand(2).getReg();
1930 LLT DstTy = MRI.getType(DstReg);
1931
1932 LLT MidTy, ExtTy;
1933 if (DstTy.isScalar() && DstTy.getScalarSizeInBits() <= 32) {
1934 ExtTy = LLT::integer(32);
1935 MidTy = LLT::fixed_vector(4, ExtTy);
1936 } else {
1937 ExtTy = LLT::integer(64);
1938 MidTy = LLT::fixed_vector(2, ExtTy);
1939 }
1940
1941 Register MidReg =
1942 MIB.buildInstr(Opc, {MidTy}, {SrcReg})->getOperand(0).getReg();
1943 Register ZeroReg =
1944 MIB.buildConstant(LLT::integer(64), 0)->getOperand(0).getReg();
1945 Register ExtReg = MIB.buildInstr(AArch64::G_EXTRACT_VECTOR_ELT, {ExtTy},
1946 {MidReg, ZeroReg})
1947 .getReg(0);
1948
1949 if (DstTy.getScalarSizeInBits() < 32)
1950 MIB.buildTrunc(DstReg, ExtReg);
1951 else
1952 MIB.buildCopy(DstReg, ExtReg);
1953
1954 MI.eraseFromParent();
1955
1956 return true;
1957 }
1958 case Intrinsic::aarch64_neon_smax:
1959 return LowerBinOp(TargetOpcode::G_SMAX);
1960 case Intrinsic::aarch64_neon_smin:
1961 return LowerBinOp(TargetOpcode::G_SMIN);
1962 case Intrinsic::aarch64_neon_umax:
1963 return LowerBinOp(TargetOpcode::G_UMAX);
1964 case Intrinsic::aarch64_neon_umin:
1965 return LowerBinOp(TargetOpcode::G_UMIN);
1966 case Intrinsic::aarch64_neon_fmax:
1967 return LowerBinOp(TargetOpcode::G_FMAXIMUM);
1968 case Intrinsic::aarch64_neon_fmin:
1969 return LowerBinOp(TargetOpcode::G_FMINIMUM);
1970 case Intrinsic::aarch64_neon_fmaxnm:
1971 return LowerBinOp(TargetOpcode::G_FMAXNUM);
1972 case Intrinsic::aarch64_neon_fminnm:
1973 return LowerBinOp(TargetOpcode::G_FMINNUM);
1974 case Intrinsic::aarch64_neon_pmul:
1975 return LowerBinOp(TargetOpcode::G_CLMUL);
1976 case Intrinsic::aarch64_neon_pmull:
1977 case Intrinsic::aarch64_neon_pmull64:
1978 return LowerBinOp(AArch64::G_PMULL);
1979 case Intrinsic::aarch64_neon_smull:
1980 return LowerBinOp(AArch64::G_SMULL);
1981 case Intrinsic::aarch64_neon_umull:
1982 return LowerBinOp(AArch64::G_UMULL);
1983 case Intrinsic::aarch64_neon_sabd:
1984 return LowerBinOp(TargetOpcode::G_ABDS);
1985 case Intrinsic::aarch64_neon_uabd:
1986 return LowerBinOp(TargetOpcode::G_ABDU);
1987 case Intrinsic::aarch64_neon_uhadd:
1988 return LowerBinOp(TargetOpcode::G_UAVGFLOOR);
1989 case Intrinsic::aarch64_neon_urhadd:
1990 return LowerBinOp(TargetOpcode::G_UAVGCEIL);
1991 case Intrinsic::aarch64_neon_shadd:
1992 return LowerBinOp(TargetOpcode::G_SAVGFLOOR);
1993 case Intrinsic::aarch64_neon_srhadd:
1994 return LowerBinOp(TargetOpcode::G_SAVGCEIL);
1995 case Intrinsic::aarch64_neon_sqshrn: {
1996 if (!MRI.getType(MI.getOperand(0).getReg()).isVector())
1997 return true;
1998 // Create right shift instruction. Store the output register in Shr.
1999 auto Shr = MIB.buildInstr(AArch64::G_VASHR,
2000 {MRI.getType(MI.getOperand(2).getReg())},
2001 {MI.getOperand(2), MI.getOperand(3).getImm()});
2002 // Build the narrow intrinsic, taking in Shr.
2003 MIB.buildInstr(TargetOpcode::G_TRUNC_SSAT_S, {MI.getOperand(0)}, {Shr});
2004 MI.eraseFromParent();
2005 return true;
2006 }
2007 case Intrinsic::aarch64_neon_sqshrun: {
2008 if (!MRI.getType(MI.getOperand(0).getReg()).isVector())
2009 return true;
2010 // Create right shift instruction. Store the output register in Shr.
2011 auto Shr = MIB.buildInstr(AArch64::G_VASHR,
2012 {MRI.getType(MI.getOperand(2).getReg())},
2013 {MI.getOperand(2), MI.getOperand(3).getImm()});
2014 // Build the narrow intrinsic, taking in Shr.
2015 MIB.buildInstr(TargetOpcode::G_TRUNC_SSAT_U, {MI.getOperand(0)}, {Shr});
2016 MI.eraseFromParent();
2017 return true;
2018 }
2019 case Intrinsic::aarch64_neon_sqrshrn: {
2020 if (!MRI.getType(MI.getOperand(0).getReg()).isVector())
2021 return true;
2022 // Create right shift instruction. Store the output register in Shr.
2023 auto Shr = MIB.buildInstr(AArch64::G_SRSHR_I,
2024 {MRI.getType(MI.getOperand(2).getReg())},
2025 {MI.getOperand(2), MI.getOperand(3).getImm()});
2026 // Build the narrow intrinsic, taking in Shr.
2027 MIB.buildInstr(TargetOpcode::G_TRUNC_SSAT_S, {MI.getOperand(0)}, {Shr});
2028 MI.eraseFromParent();
2029 return true;
2030 }
2031 case Intrinsic::aarch64_neon_sqrshrun: {
2032 if (!MRI.getType(MI.getOperand(0).getReg()).isVector())
2033 return true;
2034 // Create right shift instruction. Store the output register in Shr.
2035 auto Shr = MIB.buildInstr(AArch64::G_SRSHR_I,
2036 {MRI.getType(MI.getOperand(2).getReg())},
2037 {MI.getOperand(2), MI.getOperand(3).getImm()});
2038 // Build the narrow intrinsic, taking in Shr.
2039 MIB.buildInstr(TargetOpcode::G_TRUNC_SSAT_U, {MI.getOperand(0)}, {Shr});
2040 MI.eraseFromParent();
2041 return true;
2042 }
2043 case Intrinsic::aarch64_neon_uqrshrn: {
2044 if (!MRI.getType(MI.getOperand(0).getReg()).isVector())
2045 return true;
2046 // Create right shift instruction. Store the output register in Shr.
2047 auto Shr = MIB.buildInstr(AArch64::G_URSHR_I,
2048 {MRI.getType(MI.getOperand(2).getReg())},
2049 {MI.getOperand(2), MI.getOperand(3).getImm()});
2050 // Build the narrow intrinsic, taking in Shr.
2051 MIB.buildInstr(TargetOpcode::G_TRUNC_USAT_U, {MI.getOperand(0)}, {Shr});
2052 MI.eraseFromParent();
2053 return true;
2054 }
2055 case Intrinsic::aarch64_neon_uqshrn: {
2056 if (!MRI.getType(MI.getOperand(0).getReg()).isVector())
2057 return true;
2058 // Create right shift instruction. Store the output register in Shr.
2059 auto Shr = MIB.buildInstr(AArch64::G_VLSHR,
2060 {MRI.getType(MI.getOperand(2).getReg())},
2061 {MI.getOperand(2), MI.getOperand(3).getImm()});
2062 // Build the narrow intrinsic, taking in Shr.
2063 MIB.buildInstr(TargetOpcode::G_TRUNC_USAT_U, {MI.getOperand(0)}, {Shr});
2064 MI.eraseFromParent();
2065 return true;
2066 }
2067 case Intrinsic::aarch64_neon_sqshlu: {
2068 // Check if last operand is constant vector dup
2069 auto ShiftAmount =
2070 isConstantOrConstantSplatVector(MI.getOperand(3).getReg(), MRI);
2071 if (ShiftAmount) {
2072 // If so, create a new intrinsic with the correct shift amount
2073 MIB.buildInstr(AArch64::G_SQSHLU_I, {MI.getOperand(0)},
2074 {MI.getOperand(2)})
2075 .addImm(ShiftAmount->getSExtValue());
2076 MI.eraseFromParent();
2077 return true;
2078 }
2079 return false;
2080 }
2081 case Intrinsic::aarch64_neon_vsli: {
2082 MIB.buildInstr(
2083 AArch64::G_SLI, {MI.getOperand(0)},
2084 {MI.getOperand(2), MI.getOperand(3), MI.getOperand(4).getImm()});
2085 MI.eraseFromParent();
2086 break;
2087 }
2088 case Intrinsic::aarch64_neon_vsri: {
2089 MIB.buildInstr(
2090 AArch64::G_SRI, {MI.getOperand(0)},
2091 {MI.getOperand(2), MI.getOperand(3), MI.getOperand(4).getImm()});
2092 MI.eraseFromParent();
2093 break;
2094 }
2095 case Intrinsic::aarch64_neon_abs: {
2096 // Lower the intrinsic to G_ABS.
2097 MIB.buildInstr(TargetOpcode::G_ABS, {MI.getOperand(0)}, {MI.getOperand(2)});
2098 MI.eraseFromParent();
2099 return true;
2100 }
2101 case Intrinsic::aarch64_neon_addhn:
2102 return LowerBinOp(AArch64::G_ADDHN);
2103 case Intrinsic::aarch64_neon_sqadd: {
2104 if (MRI.getType(MI.getOperand(0).getReg()).isVector())
2105 return LowerBinOp(TargetOpcode::G_SADDSAT);
2106 break;
2107 }
2108 case Intrinsic::aarch64_neon_sqsub: {
2109 if (MRI.getType(MI.getOperand(0).getReg()).isVector())
2110 return LowerBinOp(TargetOpcode::G_SSUBSAT);
2111 break;
2112 }
2113 case Intrinsic::aarch64_neon_uqadd: {
2114 if (MRI.getType(MI.getOperand(0).getReg()).isVector())
2115 return LowerBinOp(TargetOpcode::G_UADDSAT);
2116 break;
2117 }
2118 case Intrinsic::aarch64_neon_uqsub: {
2119 if (MRI.getType(MI.getOperand(0).getReg()).isVector())
2120 return LowerBinOp(TargetOpcode::G_USUBSAT);
2121 break;
2122 }
2123 case Intrinsic::aarch64_neon_udot:
2124 return LowerTriOp(AArch64::G_UDOT);
2125 case Intrinsic::aarch64_neon_sdot:
2126 return LowerTriOp(AArch64::G_SDOT);
2127 case Intrinsic::aarch64_neon_usdot:
2128 return LowerTriOp(AArch64::G_USDOT);
2129 case Intrinsic::aarch64_neon_sqxtn:
2130 return LowerUnaryOp(TargetOpcode::G_TRUNC_SSAT_S);
2131 case Intrinsic::aarch64_neon_sqxtun:
2132 return LowerUnaryOp(TargetOpcode::G_TRUNC_SSAT_U);
2133 case Intrinsic::aarch64_neon_uqxtn:
2134 return LowerUnaryOp(TargetOpcode::G_TRUNC_USAT_U);
2135 case Intrinsic::aarch64_neon_fcvtzu:
2136 return LowerUnaryOp(TargetOpcode::G_FPTOUI_SAT);
2137 case Intrinsic::aarch64_neon_fcvtzs:
2138 return LowerUnaryOp(TargetOpcode::G_FPTOSI_SAT);
2139 case Intrinsic::aarch64_neon_cls:
2140 return LowerUnaryOp(TargetOpcode::G_CTLS);
2141
2142 case Intrinsic::vector_reverse:
2143 // TODO: Add support for vector_reverse
2144 return false;
2145 }
2146
2147 return true;
2148}
2149
2150bool AArch64LegalizerInfo::legalizeShlAshrLshr(
2152 GISelChangeObserver &Observer) const {
2153 assert(MI.getOpcode() == TargetOpcode::G_ASHR ||
2154 MI.getOpcode() == TargetOpcode::G_LSHR ||
2155 MI.getOpcode() == TargetOpcode::G_SHL);
2156 // If the shift amount is a G_CONSTANT, promote it to a 64 bit type so the
2157 // imported patterns can select it later. Either way, it will be legal.
2158 Register AmtReg = MI.getOperand(2).getReg();
2159 LLT AmtRegEltTy = MRI.getType(AmtReg).getScalarType();
2160 auto VRegAndVal = getIConstantVRegValWithLookThrough(AmtReg, MRI);
2161 if (!VRegAndVal)
2162 return true;
2163 // Check the shift amount is in range for an immediate form.
2164 int64_t Amount = VRegAndVal->Value.getSExtValue();
2165 if (Amount > 31)
2166 return true; // This will have to remain a register variant.
2167 auto ExtCst =
2168 MIRBuilder.buildConstant(AmtRegEltTy.changeElementSize(64), Amount);
2169 Observer.changingInstr(MI);
2170 MI.getOperand(2).setReg(ExtCst.getReg(0));
2171 Observer.changedInstr(MI);
2172 return true;
2173}
2174
2176 MachineRegisterInfo &MRI) {
2177 Base = Root;
2178 Offset = 0;
2179
2180 Register NewBase;
2181 int64_t NewOffset;
2182 if (mi_match(Root, MRI, m_GPtrAdd(m_Reg(NewBase), m_ICst(NewOffset))) &&
2183 isShiftedInt<7, 3>(NewOffset)) {
2184 Base = NewBase;
2185 Offset = NewOffset;
2186 }
2187}
2188
2189// FIXME: This should be removed and replaced with the generic bitcast legalize
2190// action.
2191bool AArch64LegalizerInfo::legalizeLoadStore(
2193 GISelChangeObserver &Observer) const {
2194 assert(MI.getOpcode() == TargetOpcode::G_STORE ||
2195 MI.getOpcode() == TargetOpcode::G_LOAD);
2196 // Here we just try to handle vector loads/stores where our value type might
2197 // have pointer elements, which the SelectionDAG importer can't handle. To
2198 // allow the existing patterns for s64 to fire for p0, we just try to bitcast
2199 // the value to use s64 types.
2200
2201 // Custom legalization requires the instruction, if not deleted, must be fully
2202 // legalized. In order to allow further legalization of the inst, we create
2203 // a new instruction and erase the existing one.
2204
2205 Register ValReg = MI.getOperand(0).getReg();
2206 const LLT ValTy = MRI.getType(ValReg);
2207
2208 if (ValTy == LLT::scalar(128)) {
2209
2210 AtomicOrdering Ordering = (*MI.memoperands_begin())->getSuccessOrdering();
2211 bool IsLoad = MI.getOpcode() == TargetOpcode::G_LOAD;
2212 bool IsLoadAcquire = IsLoad && Ordering == AtomicOrdering::Acquire;
2213 bool IsStoreRelease = !IsLoad && Ordering == AtomicOrdering::Release;
2214 bool IsRcpC3 =
2215 ST->hasLSE2() && ST->hasRCPC3() && (IsLoadAcquire || IsStoreRelease);
2216
2217 LLT s64 = LLT::integer(64);
2218
2219 unsigned Opcode;
2220 if (IsRcpC3) {
2221 Opcode = IsLoad ? AArch64::LDIAPPX : AArch64::STILPX;
2222 } else {
2223 // For LSE2, loads/stores should have been converted to monotonic and had
2224 // a fence inserted after them.
2225 assert(Ordering == AtomicOrdering::Monotonic ||
2226 Ordering == AtomicOrdering::Unordered);
2227 assert(ST->hasLSE2() && "ldp/stp not single copy atomic without +lse2");
2228
2229 Opcode = IsLoad ? AArch64::LDPXi : AArch64::STPXi;
2230 }
2231
2232 MachineInstrBuilder NewI;
2233 if (IsLoad) {
2234 NewI = MIRBuilder.buildInstr(Opcode, {s64, s64}, {});
2235 MIRBuilder.buildMergeLikeInstr(
2236 ValReg, {NewI->getOperand(0), NewI->getOperand(1)});
2237 } else {
2238 auto Split = MIRBuilder.buildUnmerge(s64, MI.getOperand(0));
2239 NewI = MIRBuilder.buildInstr(
2240 Opcode, {}, {Split->getOperand(0), Split->getOperand(1)});
2241 }
2242
2243 if (IsRcpC3) {
2244 NewI.addUse(MI.getOperand(1).getReg());
2245 } else {
2246 Register Base;
2247 int Offset;
2248 matchLDPSTPAddrMode(MI.getOperand(1).getReg(), Base, Offset, MRI);
2249 NewI.addUse(Base);
2250 NewI.addImm(Offset / 8);
2251 }
2252
2253 NewI.cloneMemRefs(MI);
2254 constrainSelectedInstRegOperands(*NewI, *ST->getInstrInfo(),
2255 *MRI.getTargetRegisterInfo(),
2256 *ST->getRegBankInfo());
2257 MI.eraseFromParent();
2258 return true;
2259 }
2260
2261 if (!ValTy.isPointerVector() ||
2262 ValTy.getElementType().getAddressSpace() != 0) {
2263 LLVM_DEBUG(dbgs() << "Tried to do custom legalization on wrong load/store");
2264 return false;
2265 }
2266
2267 unsigned PtrSize = ValTy.getElementType().getSizeInBits();
2268 const LLT NewTy = LLT::vector(ValTy.getElementCount(), LLT::integer(PtrSize));
2269 auto &MMO = **MI.memoperands_begin();
2270 MMO.setType(NewTy);
2271
2272 if (MI.getOpcode() == TargetOpcode::G_STORE) {
2273 auto Bitcast = MIRBuilder.buildBitcast(NewTy, ValReg);
2274 MIRBuilder.buildStore(Bitcast.getReg(0), MI.getOperand(1), MMO);
2275 } else {
2276 auto NewLoad = MIRBuilder.buildLoad(NewTy, MI.getOperand(1), MMO);
2277 MIRBuilder.buildBitcast(ValReg, NewLoad);
2278 }
2279 MI.eraseFromParent();
2280 return true;
2281}
2282
2283bool AArch64LegalizerInfo::legalizeVaArg(MachineInstr &MI,
2285 MachineIRBuilder &MIRBuilder) const {
2286 MachineFunction &MF = MIRBuilder.getMF();
2287 Align Alignment(MI.getOperand(2).getImm());
2288 Register Dst = MI.getOperand(0).getReg();
2289 Register ListPtr = MI.getOperand(1).getReg();
2290
2291 LLT PtrTy = MRI.getType(ListPtr);
2292 LLT IntPtrTy = LLT::integer(PtrTy.getSizeInBits());
2293
2294 const unsigned PtrSize = PtrTy.getSizeInBits() / 8;
2295 const Align PtrAlign = Align(PtrSize);
2296 auto List = MIRBuilder.buildLoad(
2297 PtrTy, ListPtr,
2298 *MF.getMachineMemOperand(MachinePointerInfo(), MachineMemOperand::MOLoad,
2299 PtrTy, PtrAlign));
2300
2301 MachineInstrBuilder DstPtr;
2302 if (Alignment > PtrAlign) {
2303 // Realign the list to the actual required alignment.
2304 auto AlignMinus1 =
2305 MIRBuilder.buildConstant(IntPtrTy, Alignment.value() - 1);
2306 auto ListTmp = MIRBuilder.buildPtrAdd(PtrTy, List, AlignMinus1.getReg(0));
2307 DstPtr = MIRBuilder.buildMaskLowPtrBits(PtrTy, ListTmp, Log2(Alignment));
2308 } else
2309 DstPtr = List;
2310
2311 LLT ValTy = MRI.getType(Dst);
2312 uint64_t ValSize = ValTy.getSizeInBits() / 8;
2313 MIRBuilder.buildLoad(
2314 Dst, DstPtr,
2315 *MF.getMachineMemOperand(MachinePointerInfo(), MachineMemOperand::MOLoad,
2316 ValTy, std::max(Alignment, PtrAlign)));
2317
2318 auto Size = MIRBuilder.buildConstant(IntPtrTy, alignTo(ValSize, PtrAlign));
2319
2320 auto NewList = MIRBuilder.buildPtrAdd(PtrTy, DstPtr, Size.getReg(0));
2321
2322 MIRBuilder.buildStore(NewList, ListPtr,
2323 *MF.getMachineMemOperand(MachinePointerInfo(),
2325 PtrTy, PtrAlign));
2326
2327 MI.eraseFromParent();
2328 return true;
2329}
2330
2331bool AArch64LegalizerInfo::legalizeBitfieldExtract(
2332 MachineInstr &MI, MachineRegisterInfo &MRI, LegalizerHelper &Helper) const {
2333 // Only legal if we can select immediate forms.
2334 // TODO: Lower this otherwise.
2335 return getIConstantVRegValWithLookThrough(MI.getOperand(2).getReg(), MRI) &&
2336 getIConstantVRegValWithLookThrough(MI.getOperand(3).getReg(), MRI);
2337}
2338
2339bool AArch64LegalizerInfo::legalizeCTPOP(MachineInstr &MI,
2341 LegalizerHelper &Helper) const {
2342 // When there is no integer popcount instruction (FEAT_CSSC isn't available),
2343 // it can be more efficiently lowered to the following sequence that uses
2344 // AdvSIMD registers/instructions as long as the copies to/from the AdvSIMD
2345 // registers are cheap.
2346 // FMOV D0, X0 // copy 64-bit int to vector, high bits zero'd
2347 // CNT V0.8B, V0.8B // 8xbyte pop-counts
2348 // ADDV B0, V0.8B // sum 8xbyte pop-counts
2349 // UMOV X0, V0.B[0] // copy byte result back to integer reg
2350 //
2351 // For 128 bit vector popcounts, we lower to the following sequence:
2352 // cnt.16b v0, v0 // v8s16, v4s32, v2s64
2353 // uaddlp.8h v0, v0 // v8s16, v4s32, v2s64
2354 // uaddlp.4s v0, v0 // v4s32, v2s64
2355 // uaddlp.2d v0, v0 // v2s64
2356 //
2357 // For 64 bit vector popcounts, we lower to the following sequence:
2358 // cnt.8b v0, v0 // v4s16, v2s32
2359 // uaddlp.4h v0, v0 // v4s16, v2s32
2360 // uaddlp.2s v0, v0 // v2s32
2361
2362 MachineIRBuilder &MIRBuilder = Helper.MIRBuilder;
2363 Register Dst = MI.getOperand(0).getReg();
2364 Register Val = MI.getOperand(1).getReg();
2365 LLT Ty = MRI.getType(Val);
2366
2367 LLT i64 = LLT::integer(64);
2368 LLT i32 = LLT::integer(32);
2369 LLT i16 = LLT::integer(16);
2370 LLT i8 = LLT::integer(8);
2371 unsigned Size = Ty.getSizeInBits();
2372
2373 assert(Ty == MRI.getType(Dst) &&
2374 "Expected src and dst to have the same type!");
2375
2376 if (ST->hasCSSC() && Ty.isScalar() && Size == 128) {
2377
2378 auto Split = MIRBuilder.buildUnmerge(i64, Val);
2379 auto CTPOP1 = MIRBuilder.buildCTPOP(i64, Split->getOperand(0));
2380 auto CTPOP2 = MIRBuilder.buildCTPOP(i64, Split->getOperand(1));
2381 auto Add = MIRBuilder.buildAdd(i64, CTPOP1, CTPOP2);
2382
2383 MIRBuilder.buildZExt(Dst, Add);
2384 MI.eraseFromParent();
2385 return true;
2386 }
2387
2388 if (!ST->hasNEON() ||
2389 MI.getMF()->getFunction().hasFnAttribute(Attribute::NoImplicitFloat)) {
2390 // Use generic lowering when custom lowering is not possible.
2391 return Ty.isScalar() && (Size == 32 || Size == 64) &&
2392 Helper.lowerBitCount(MI) ==
2394 }
2395
2396 // Pre-conditioning: widen Val up to the nearest vector type.
2397 // s32,s64,v4s16,v2s32 -> v8i8
2398 // v8s16,v4s32,v2s64 -> v16i8
2399 LLT VTy = Size == 128 ? LLT::fixed_vector(16, i8) : LLT::fixed_vector(8, i8);
2400 if (Ty.isScalar()) {
2401 assert((Size == 32 || Size == 64 || Size == 128) && "Expected only 32, 64, or 128 bit scalars!");
2402 if (Size == 32) {
2403 Val = MIRBuilder.buildZExt(i64, Val).getReg(0);
2404 }
2405 }
2406 Val = MIRBuilder.buildBitcast(VTy, Val).getReg(0);
2407
2408 // Count bits in each byte-sized lane.
2409 auto CTPOP = MIRBuilder.buildCTPOP(VTy, Val);
2410
2411 // Sum across lanes.
2412 if (ST->hasDotProd() && Ty.isVector() && Ty.getNumElements() >= 2 &&
2413 Ty.getScalarSizeInBits() != 16) {
2414 LLT Dt = Ty == LLT::fixed_vector(2, i64) ? LLT::fixed_vector(4, i32) : Ty;
2415 auto Zeros = MIRBuilder.buildConstant(Dt, 0);
2416 auto Ones = MIRBuilder.buildConstant(VTy, 1);
2417 MachineInstrBuilder Sum;
2418
2419 if (Ty == LLT::fixed_vector(2, i64)) {
2420 auto UDOT =
2421 MIRBuilder.buildInstr(AArch64::G_UDOT, {Dt}, {Zeros, Ones, CTPOP});
2422 Sum = MIRBuilder.buildInstr(AArch64::G_UADDLP, {Ty}, {UDOT});
2423 } else if (Ty == LLT::fixed_vector(4, i32)) {
2424 Sum = MIRBuilder.buildInstr(AArch64::G_UDOT, {Dt}, {Zeros, Ones, CTPOP});
2425 } else if (Ty == LLT::fixed_vector(2, i32)) {
2426 Sum = MIRBuilder.buildInstr(AArch64::G_UDOT, {Dt}, {Zeros, Ones, CTPOP});
2427 } else {
2428 llvm_unreachable("unexpected vector shape");
2429 }
2430
2431 Sum->getOperand(0).setReg(Dst);
2432 MI.eraseFromParent();
2433 return true;
2434 }
2435
2436 Register HSum = CTPOP.getReg(0);
2437 unsigned Opc;
2438 SmallVector<LLT> HAddTys;
2439 if (Ty.isScalar()) {
2440 Opc = Intrinsic::aarch64_neon_uaddlv;
2441 HAddTys.push_back(i32);
2442 } else if (Ty == LLT::fixed_vector(8, i16)) {
2443 Opc = Intrinsic::aarch64_neon_uaddlp;
2444 HAddTys.push_back(LLT::fixed_vector(8, i16));
2445 } else if (Ty == LLT::fixed_vector(4, i32)) {
2446 Opc = Intrinsic::aarch64_neon_uaddlp;
2447 HAddTys.push_back(LLT::fixed_vector(8, i16));
2448 HAddTys.push_back(LLT::fixed_vector(4, i32));
2449 } else if (Ty == LLT::fixed_vector(2, i64)) {
2450 Opc = Intrinsic::aarch64_neon_uaddlp;
2451 HAddTys.push_back(LLT::fixed_vector(8, i16));
2452 HAddTys.push_back(LLT::fixed_vector(4, i32));
2453 HAddTys.push_back(LLT::fixed_vector(2, i64));
2454 } else if (Ty == LLT::fixed_vector(4, i16)) {
2455 Opc = Intrinsic::aarch64_neon_uaddlp;
2456 HAddTys.push_back(LLT::fixed_vector(4, i16));
2457 } else if (Ty == LLT::fixed_vector(2, i32)) {
2458 Opc = Intrinsic::aarch64_neon_uaddlp;
2459 HAddTys.push_back(LLT::fixed_vector(4, i16));
2460 HAddTys.push_back(LLT::fixed_vector(2, i32));
2461 } else
2462 llvm_unreachable("unexpected vector shape");
2464 for (LLT HTy : HAddTys) {
2465 UADD = MIRBuilder.buildIntrinsic(Opc, {HTy}).addUse(HSum);
2466 HSum = UADD.getReg(0);
2467 }
2468
2469 // Post-conditioning.
2470 if (Ty.isScalar() && (Size == 64 || Size == 128))
2471 MIRBuilder.buildZExt(Dst, UADD);
2472 else
2473 UADD->getOperand(0).setReg(Dst);
2474 MI.eraseFromParent();
2475 return true;
2476}
2477
2478bool AArch64LegalizerInfo::legalizeAtomicCmpxchg128(
2479 MachineInstr &MI, MachineRegisterInfo &MRI, LegalizerHelper &Helper) const {
2480 MachineIRBuilder &MIRBuilder = Helper.MIRBuilder;
2481 LLT i64 = LLT::integer(64);
2482 auto Addr = MI.getOperand(1).getReg();
2483 auto DesiredI = MIRBuilder.buildUnmerge({i64, i64}, MI.getOperand(2));
2484 auto NewI = MIRBuilder.buildUnmerge({i64, i64}, MI.getOperand(3));
2485 auto DstLo = MRI.createGenericVirtualRegister(i64);
2486 auto DstHi = MRI.createGenericVirtualRegister(i64);
2487
2488 MachineInstrBuilder CAS;
2489 if (ST->hasLSE()) {
2490 // We have 128-bit CASP instructions taking XSeqPair registers, which are
2491 // s128. We need the merge/unmerge to bracket the expansion and pair up with
2492 // the rest of the MIR so we must reassemble the extracted registers into a
2493 // 128-bit known-regclass one with code like this:
2494 //
2495 // %in1 = REG_SEQUENCE Lo, Hi ; One for each input
2496 // %out = CASP %in1, ...
2497 // %OldLo = G_EXTRACT %out, 0
2498 // %OldHi = G_EXTRACT %out, 64
2499 auto Ordering = (*MI.memoperands_begin())->getMergedOrdering();
2500 unsigned Opcode;
2501 switch (Ordering) {
2503 Opcode = AArch64::CASPAX;
2504 break;
2506 Opcode = AArch64::CASPLX;
2507 break;
2510 Opcode = AArch64::CASPALX;
2511 break;
2512 default:
2513 Opcode = AArch64::CASPX;
2514 break;
2515 }
2516
2517 LLT s128 = LLT::scalar(128);
2518 auto CASDst = MRI.createGenericVirtualRegister(s128);
2519 auto CASDesired = MRI.createGenericVirtualRegister(s128);
2520 auto CASNew = MRI.createGenericVirtualRegister(s128);
2521 MIRBuilder.buildInstr(TargetOpcode::REG_SEQUENCE, {CASDesired}, {})
2522 .addUse(DesiredI->getOperand(0).getReg())
2523 .addImm(AArch64::sube64)
2524 .addUse(DesiredI->getOperand(1).getReg())
2525 .addImm(AArch64::subo64);
2526 MIRBuilder.buildInstr(TargetOpcode::REG_SEQUENCE, {CASNew}, {})
2527 .addUse(NewI->getOperand(0).getReg())
2528 .addImm(AArch64::sube64)
2529 .addUse(NewI->getOperand(1).getReg())
2530 .addImm(AArch64::subo64);
2531
2532 CAS = MIRBuilder.buildInstr(Opcode, {CASDst}, {CASDesired, CASNew, Addr});
2533
2534 MIRBuilder.buildExtract({DstLo}, {CASDst}, 0);
2535 MIRBuilder.buildExtract({DstHi}, {CASDst}, 64);
2536 } else {
2537 // The -O0 CMP_SWAP_128 is friendlier to generate code for because LDXP/STXP
2538 // can take arbitrary registers so it just has the normal GPR64 operands the
2539 // rest of AArch64 is expecting.
2540 auto Ordering = (*MI.memoperands_begin())->getMergedOrdering();
2541 unsigned Opcode;
2542 switch (Ordering) {
2544 Opcode = AArch64::CMP_SWAP_128_ACQUIRE;
2545 break;
2547 Opcode = AArch64::CMP_SWAP_128_RELEASE;
2548 break;
2551 Opcode = AArch64::CMP_SWAP_128;
2552 break;
2553 default:
2554 Opcode = AArch64::CMP_SWAP_128_MONOTONIC;
2555 break;
2556 }
2557
2558 auto Scratch = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
2559 CAS = MIRBuilder.buildInstr(Opcode, {DstLo, DstHi, Scratch},
2560 {Addr, DesiredI->getOperand(0),
2561 DesiredI->getOperand(1), NewI->getOperand(0),
2562 NewI->getOperand(1)});
2563 }
2564
2565 CAS.cloneMemRefs(MI);
2566 constrainSelectedInstRegOperands(*CAS, *ST->getInstrInfo(),
2567 *MRI.getTargetRegisterInfo(),
2568 *ST->getRegBankInfo());
2569
2570 MIRBuilder.buildMergeLikeInstr(MI.getOperand(0), {DstLo, DstHi});
2571 MI.eraseFromParent();
2572 return true;
2573}
2574
2575bool AArch64LegalizerInfo::legalizeCTTZ(MachineInstr &MI,
2576 LegalizerHelper &Helper) const {
2577 MachineIRBuilder &MIRBuilder = Helper.MIRBuilder;
2578 MachineRegisterInfo &MRI = *MIRBuilder.getMRI();
2579 LLT Ty = MRI.getType(MI.getOperand(1).getReg());
2580 auto BitReverse = MIRBuilder.buildBitReverse(Ty, MI.getOperand(1));
2581 MIRBuilder.buildCTLZ(MI.getOperand(0).getReg(), BitReverse);
2582 MI.eraseFromParent();
2583 return true;
2584}
2585
2586bool AArch64LegalizerInfo::legalizeMemOps(MachineInstr &MI,
2587 LegalizerHelper &Helper) const {
2588 MachineIRBuilder &MIRBuilder = Helper.MIRBuilder;
2589
2590 // Tagged version MOPSMemorySetTagged is legalised in legalizeIntrinsic
2591 if (MI.getOpcode() == TargetOpcode::G_MEMSET ||
2592 MI.getOpcode() == TargetOpcode::G_MEMSET_INLINE) {
2593 // Anyext the value being set to 64 bit (only the bottom 8 bits are read by
2594 // the instruction).
2595 auto &Value = MI.getOperand(1);
2596 Register ExtValueReg =
2597 MIRBuilder.buildAnyExt(LLT::integer(64), Value).getReg(0);
2598 Value.setReg(ExtValueReg);
2599 return true;
2600 }
2601
2602 return false;
2603}
2604
2605bool AArch64LegalizerInfo::legalizeExtractVectorElt(
2606 MachineInstr &MI, MachineRegisterInfo &MRI, LegalizerHelper &Helper) const {
2607 const GExtractVectorElement *Element = cast<GExtractVectorElement>(&MI);
2608 auto VRegAndVal =
2610 if (VRegAndVal)
2611 return true;
2612 LLT VecTy = MRI.getType(Element->getVectorReg());
2613 if (VecTy.isScalableVector())
2614 return true;
2615 return Helper.lowerExtractInsertVectorElt(MI) !=
2617}
2618
2619bool AArch64LegalizerInfo::legalizeDynStackAlloc(
2620 MachineInstr &MI, LegalizerHelper &Helper) const {
2621 MachineFunction &MF = *MI.getParent()->getParent();
2622 MachineIRBuilder &MIRBuilder = Helper.MIRBuilder;
2623 MachineRegisterInfo &MRI = *MIRBuilder.getMRI();
2624
2625 // If stack probing is not enabled for this function, use the default
2626 // lowering.
2627 if (!MF.getFunction().hasFnAttribute("probe-stack") ||
2628 MF.getFunction().getFnAttribute("probe-stack").getValueAsString() !=
2629 "inline-asm") {
2630 Helper.lowerDynStackAlloc(MI);
2631 return true;
2632 }
2633
2634 Register Dst = MI.getOperand(0).getReg();
2635 Register AllocSize = MI.getOperand(1).getReg();
2636 Align Alignment = assumeAligned(MI.getOperand(2).getImm());
2637
2638 assert(MRI.getType(Dst) == LLT::pointer(0, 64) &&
2639 "Unexpected type for dynamic alloca");
2640 assert(MRI.getType(AllocSize) == LLT::scalar(64) &&
2641 "Unexpected type for dynamic alloca");
2642
2643 LLT PtrTy = MRI.getType(Dst);
2644 Register SPReg =
2646 Register SPTmp =
2647 Helper.getDynStackAllocTargetPtr(SPReg, AllocSize, Alignment, PtrTy);
2648 auto NewMI =
2649 MIRBuilder.buildInstr(AArch64::PROBED_STACKALLOC_DYN, {}, {SPTmp});
2650 MRI.setRegClass(NewMI.getReg(0), &AArch64::GPR64commonRegClass);
2651 MIRBuilder.setInsertPt(*NewMI->getParent(), NewMI);
2652 MIRBuilder.buildCopy(Dst, SPTmp);
2653
2654 MI.eraseFromParent();
2655 return true;
2656}
2657
2658bool AArch64LegalizerInfo::legalizePrefetch(MachineInstr &MI,
2659 LegalizerHelper &Helper) const {
2660 MachineIRBuilder &MIB = Helper.MIRBuilder;
2661 auto &AddrVal = MI.getOperand(0);
2662
2663 int64_t IsWrite = MI.getOperand(1).getImm();
2664 int64_t Locality = MI.getOperand(2).getImm();
2665 int64_t IsData = MI.getOperand(3).getImm();
2666
2667 bool IsStream = Locality == 0;
2668 if (Locality != 0) {
2669 assert(Locality <= 3 && "Prefetch locality out-of-range");
2670 // The locality degree is the opposite of the cache speed.
2671 // Put the number the other way around.
2672 // The encoding starts at 0 for level 1
2673 Locality = 3 - Locality;
2674 }
2675
2676 unsigned PrfOp = (IsWrite << 4) | (!IsData << 3) | (Locality << 1) | IsStream;
2677
2678 MIB.buildInstr(AArch64::G_AARCH64_PREFETCH).addImm(PrfOp).add(AddrVal);
2679 MI.eraseFromParent();
2680 return true;
2681}
2682
2683bool AArch64LegalizerInfo::legalizeConcatVectors(
2685 MachineIRBuilder &MIRBuilder) const {
2686 // Widen sub-byte element vectors to byte-sized elements before concatenating.
2687 // This is analogous to SDAG's integer type promotion for sub-byte types.
2689 Register DstReg = Concat.getReg(0);
2690 LLT DstTy = MRI.getType(DstReg);
2691 assert(DstTy.getScalarSizeInBits() < 8 && "Expected dst ty to be < 8b");
2692
2693 unsigned WideEltSize =
2694 std::max(8u, (unsigned)PowerOf2Ceil(DstTy.getScalarSizeInBits()));
2695 LLT SrcTy = MRI.getType(Concat.getSourceReg(0));
2696 LLT WideSrcTy = SrcTy.changeElementSize(WideEltSize);
2697 LLT WideDstTy = DstTy.changeElementSize(WideEltSize);
2698
2699 SmallVector<Register> WideSrcs;
2700 for (unsigned I = 0; I < Concat.getNumSources(); ++I) {
2701 auto Wide = MIRBuilder.buildAnyExt(WideSrcTy, Concat.getSourceReg(I));
2702 WideSrcs.push_back(Wide.getReg(0));
2703 }
2704
2705 auto WideConcat = MIRBuilder.buildConcatVectors(WideDstTy, WideSrcs);
2706 MIRBuilder.buildTrunc(DstReg, WideConcat);
2707 MI.eraseFromParent();
2708 return true;
2709}
2710
2711bool AArch64LegalizerInfo::legalizeFptrunc(MachineInstr &MI,
2712 MachineIRBuilder &MIRBuilder,
2713 MachineRegisterInfo &MRI) const {
2714 auto [Dst, DstTy, Src, SrcTy] = MI.getFirst2RegLLTs();
2715
2716 // This function legalizes f64 -> bf16 and f64 -> f16 truncations via f64 ->
2717 // f32 G_FPTRUNC_ODD and f32 -> [b]f16 G_FPTRUNC, which apparently avoids the
2718 // usual double-rounding issue that could be present from using twin
2719 // G_FPTRUNC.
2720
2721 if (DstTy.isBFloat16() && SrcTy.isFloat64()) {
2722 auto Mid = MIRBuilder.buildInstr(AArch64::G_FPTRUNC_ODD, {LLT::float32()},
2723 {Src}, MI.getFlags());
2724 MIRBuilder.buildInstr(AArch64::G_FPTRUNC, {Dst}, {Mid}, MI.getFlags());
2725 MI.eraseFromParent();
2726 return true;
2727 }
2728
2729 assert(SrcTy.isFixedVector() && isPowerOf2_32(SrcTy.getNumElements()) &&
2730 "Expected a power of 2 elements");
2731
2732 // We must mutate types here as FPTrunc may be used on a IEEE floating point
2733 // or a brainfloat.
2734 LLT v2s16 = DstTy.changeElementCount(2);
2735 LLT v4s16 = DstTy.changeElementCount(4);
2736 LLT v2s32 = SrcTy.changeElementCount(2).changeElementSize(32);
2737 LLT v4s32 = SrcTy.changeElementCount(4).changeElementSize(32);
2738 LLT v2s64 = SrcTy.changeElementCount(2);
2739
2740 SmallVector<Register> RegsToUnmergeTo;
2741 SmallVector<Register> TruncOddDstRegs;
2742 SmallVector<Register> RegsToMerge;
2743
2744 unsigned ElemCount = SrcTy.getNumElements();
2745
2746 // Find the biggest size chunks we can work with
2747 int StepSize = ElemCount % 4 ? 2 : 4;
2748
2749 // If we have a power of 2 greater than 2, we need to first unmerge into
2750 // enough pieces
2751 if (ElemCount <= 2)
2752 RegsToUnmergeTo.push_back(Src);
2753 else {
2754 for (unsigned i = 0; i < ElemCount / 2; ++i)
2755 RegsToUnmergeTo.push_back(MRI.createGenericVirtualRegister(v2s64));
2756
2757 MIRBuilder.buildUnmerge(RegsToUnmergeTo, Src);
2758 }
2759
2760 // Create all of the round-to-odd instructions and store them
2761 for (auto SrcReg : RegsToUnmergeTo) {
2762 Register Mid = MIRBuilder
2763 .buildInstr(AArch64::G_FPTRUNC_ODD, {v2s32}, {SrcReg},
2764 MI.getFlags())
2765 .getReg(0);
2766 TruncOddDstRegs.push_back(Mid);
2767 }
2768
2769 // Truncate 4s32 to 4s16 if we can to reduce instruction count, otherwise
2770 // truncate 2s32 to 2s16.
2771 unsigned Index = 0;
2772 for (unsigned LoopIter = 0; LoopIter < ElemCount / StepSize; ++LoopIter) {
2773 if (StepSize == 4) {
2774 Register ConcatDst =
2775 MIRBuilder
2777 {v4s32}, {TruncOddDstRegs[Index++], TruncOddDstRegs[Index++]})
2778 .getReg(0);
2779
2780 RegsToMerge.push_back(
2781 MIRBuilder.buildFPTrunc(v4s16, ConcatDst, MI.getFlags()).getReg(0));
2782 } else {
2783 RegsToMerge.push_back(
2784 MIRBuilder
2785 .buildFPTrunc(v2s16, TruncOddDstRegs[Index++], MI.getFlags())
2786 .getReg(0));
2787 }
2788 }
2789
2790 // If there is only one register, replace the destination
2791 if (RegsToMerge.size() == 1) {
2792 MRI.replaceRegWith(Dst, RegsToMerge.pop_back_val());
2793 MI.eraseFromParent();
2794 return true;
2795 }
2796
2797 // Merge the rest of the instructions & replace the register
2798 Register Fin = MIRBuilder.buildMergeLikeInstr(DstTy, RegsToMerge).getReg(0);
2799 MRI.replaceRegWith(Dst, Fin);
2800 MI.eraseFromParent();
2801 return true;
2802}
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:77
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...
#define I(x, y, z)
Definition MD5.cpp:57
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:119
static constexpr int Concat[]
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:1050
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition APInt.cpp:1687
int64_t getSExtValue() const
Get sign extended value.
Definition APInt.h:1583
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:740
@ ICMP_NE
not equal
Definition InstrTypes.h:762
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition Function.cpp:762
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:727
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.
static constexpr LLT float64()
Get a 64-bit IEEE double value.
LLT changeElementCount(ElementCount EC) const
Return a vector or scalar with the same element type and the new element count.
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.
LLT getScalarType() const
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.
static constexpr LLT float128()
Get a 128-bit IEEE quad value.
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 ElementCount getElementCount() const
LLT divide(int Factor) const
Return a type that is Factor times smaller.
static constexpr LLT float16()
Get a 16-bit IEEE half value.
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.
static LLT integer(unsigned SizeInBits)
static constexpr LLT bfloat16()
LLT getElementType() const
Returns the vector's element type. Only valid for vector types.
static constexpr LLT float32()
Get a 32-bit IEEE float value.
bool isFloat64() const
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.
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 & 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...
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.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags F, LLT MemTy, Align BaseAlignment, const MMOMetadata &Metadata=MMOMetadata(), SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
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 buildConcatVectors(const DstOp &Res, ArrayRef< Register > Ops)
Build and insert Res = G_CONCAT_VECTORS Op0, ...
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 createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
LLT getType(Register Reg) const
Get the low-level type of Reg or LLT{} if Reg is not a generic (target independent) virtual register.
LLVM_ABI void setRegClass(Register Reg, const TargetRegisterClass *RC)
setRegClass - Set the register class of the specified virtual register.
LLVM_ABI Register createGenericVirtualRegister(LLT Ty, StringRef Name="")
Create and return a new generic virtual register with low-level type Ty.
const TargetRegisterInfo * getTargetRegisterInfo() const
LLVM_ABI void replaceRegWith(Register FromReg, Register ToReg)
replaceRegWith - Replace all instances of FromReg with ToReg in the machine function.
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.
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.
LLVM_ABI std::optional< APInt > isConstantOrConstantSplatVector(Register Def, const MachineRegisterInfo &MRI)
Determines if Def defines a constant integer or a splat vector of constant integers.
Definition Utils.cpp:1517
@ Offset
Definition DWP.cpp:578
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:159
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition MathExtras.h:285
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
uint64_t PowerOf2Ceil(uint64_t A)
Returns the power of two which is greater than or equal to the given value.
Definition MathExtras.h:386
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:280
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
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.
IntPtrTy
Definition InstrProf.h:82
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:183
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:436
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,...