LLVM 24.0.0git
AArch64AddressingModes.h
Go to the documentation of this file.
1//===- AArch64AddressingModes.h - AArch64 Addressing Modes ------*- 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//
9// This file contains the AArch64 addressing mode implementation stuff.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_LIB_TARGET_AARCH64_MCTARGETDESC_AARCH64ADDRESSINGMODES_H
14#define LLVM_LIB_TARGET_AARCH64_MCTARGETDESC_AARCH64ADDRESSINGMODES_H
15
16#include "llvm/ADT/APFloat.h"
17#include "llvm/ADT/APInt.h"
18#include "llvm/ADT/bit.h"
21#include <cassert>
22
23namespace llvm {
24
25/// AArch64_AM - AArch64 Addressing Mode Stuff
26namespace AArch64_AM {
27
28//===----------------------------------------------------------------------===//
29// Shifts
30//
31
50
51/// isSignExtendShiftType - Returns true if \p Type is sign extending.
53 switch (Type) {
58 return true;
59 default:
60 return false;
61 }
62}
63
64/// getShiftName - Get the string encoding for the shift type.
65static inline const char *getShiftExtendName(AArch64_AM::ShiftExtendType ST) {
66 switch (ST) {
67 default: llvm_unreachable("unhandled shift type!");
68 case AArch64_AM::LSL: return "lsl";
69 case AArch64_AM::LSR: return "lsr";
70 case AArch64_AM::ASR: return "asr";
71 case AArch64_AM::ROR: return "ror";
72 case AArch64_AM::MSL: return "msl";
73 case AArch64_AM::UXTB: return "uxtb";
74 case AArch64_AM::UXTH: return "uxth";
75 case AArch64_AM::UXTW: return "uxtw";
76 case AArch64_AM::UXTX: return "uxtx";
77 case AArch64_AM::SXTB: return "sxtb";
78 case AArch64_AM::SXTH: return "sxth";
79 case AArch64_AM::SXTW: return "sxtw";
80 case AArch64_AM::SXTX: return "sxtx";
81 }
82 return nullptr;
83}
84
85/// getShiftType - Extract the shift type.
87 switch ((Imm >> 6) & 0x7) {
88 default: return AArch64_AM::InvalidShiftExtend;
89 case 0: return AArch64_AM::LSL;
90 case 1: return AArch64_AM::LSR;
91 case 2: return AArch64_AM::ASR;
92 case 3: return AArch64_AM::ROR;
93 case 4: return AArch64_AM::MSL;
94 }
95}
96
97/// getShiftValue - Extract the shift value.
98static inline unsigned getShiftValue(unsigned Imm) {
99 return Imm & 0x3f;
100}
101
102/// getShifterImm - Encode the shift type and amount:
103/// imm: 6-bit shift amount
104/// shifter: 000 ==> lsl
105/// 001 ==> lsr
106/// 010 ==> asr
107/// 011 ==> ror
108/// 100 ==> msl
109/// {8-6} = shifter
110/// {5-0} = imm
112 unsigned Imm) {
113 assert((Imm & 0x3f) == Imm && "Illegal shifted immediate value!");
114 unsigned STEnc = 0;
115 switch (ST) {
116 default: llvm_unreachable("Invalid shift requested");
117 case AArch64_AM::LSL: STEnc = 0; break;
118 case AArch64_AM::LSR: STEnc = 1; break;
119 case AArch64_AM::ASR: STEnc = 2; break;
120 case AArch64_AM::ROR: STEnc = 3; break;
121 case AArch64_AM::MSL: STEnc = 4; break;
122 }
123 return (STEnc << 6) | (Imm & 0x3f);
124}
125
126//===----------------------------------------------------------------------===//
127// Extends
128//
129
130/// getArithShiftValue - get the arithmetic shift value.
131static inline unsigned getArithShiftValue(unsigned Imm) {
132 return Imm & 0x7;
133}
134
135/// getExtendType - Extract the extend type for operands of arithmetic ops.
137 assert((Imm & 0x7) == Imm && "invalid immediate!");
138 switch (Imm) {
139 default: llvm_unreachable("Compiler bug!");
140 case 0: return AArch64_AM::UXTB;
141 case 1: return AArch64_AM::UXTH;
142 case 2: return AArch64_AM::UXTW;
143 case 3: return AArch64_AM::UXTX;
144 case 4: return AArch64_AM::SXTB;
145 case 5: return AArch64_AM::SXTH;
146 case 6: return AArch64_AM::SXTW;
147 case 7: return AArch64_AM::SXTX;
148 }
149}
150
152 return getExtendType((Imm >> 3) & 0x7);
153}
154
155/// Mapping from extend bits to required operation:
156/// shifter: 000 ==> uxtb
157/// 001 ==> uxth
158/// 010 ==> uxtw
159/// 011 ==> uxtx
160/// 100 ==> sxtb
161/// 101 ==> sxth
162/// 110 ==> sxtw
163/// 111 ==> sxtx
165 switch (ET) {
166 default: llvm_unreachable("Invalid extend type requested");
167 case AArch64_AM::UXTB: return 0; break;
168 case AArch64_AM::UXTH: return 1; break;
169 case AArch64_AM::UXTW: return 2; break;
170 case AArch64_AM::UXTX: return 3; break;
171 case AArch64_AM::SXTB: return 4; break;
172 case AArch64_AM::SXTH: return 5; break;
173 case AArch64_AM::SXTW: return 6; break;
174 case AArch64_AM::SXTX: return 7; break;
175 }
176}
177
178/// getArithExtendImm - Encode the extend type and shift amount for an
179/// arithmetic instruction:
180/// imm: 3-bit extend amount
181/// {5-3} = shifter
182/// {2-0} = imm3
184 unsigned Imm) {
185 assert((Imm & 0x7) == Imm && "Illegal shifted immediate value!");
186 return (getExtendEncoding(ET) << 3) | (Imm & 0x7);
187}
188
189static inline uint64_t ror(uint64_t elt, unsigned size) {
190 return ((elt & 1) << (size-1)) | (elt >> 1);
191}
192
193/// processLogicalImmediate - Determine if an immediate value can be encoded
194/// as the immediate operand of a logical instruction for the given register
195/// size. If so, return true with "encoding" set to the encoded value in
196/// the form N:immr:imms.
197static inline bool processLogicalImmediate(uint64_t Imm, unsigned RegSize,
198 uint64_t &Encoding) {
199 if (Imm == 0ULL || Imm == ~0ULL ||
200 (RegSize != 64 &&
201 (Imm >> RegSize != 0 || Imm == (~0ULL >> (64 - RegSize)))))
202 return false;
203
204 // First, determine the element size.
205 unsigned Size = RegSize;
206
207 do {
208 Size /= 2;
209 uint64_t Mask = (1ULL << Size) - 1;
210
211 if ((Imm & Mask) != ((Imm >> Size) & Mask)) {
212 Size *= 2;
213 break;
214 }
215 } while (Size > 2);
216
217 // Second, determine the rotation to make the element be: 0^m 1^n.
218 uint32_t CTO, I;
219 uint64_t Mask = ((uint64_t)-1LL) >> (64 - Size);
220 Imm &= Mask;
221
222 if (isShiftedMask_64(Imm)) {
224 assert(I < 64 && "undefined behavior");
225 CTO = llvm::countr_one(Imm >> I);
226 } else {
227 Imm |= ~Mask;
228 if (!isShiftedMask_64(~Imm))
229 return false;
230
231 unsigned CLO = llvm::countl_one(Imm);
232 I = 64 - CLO;
233 CTO = CLO + llvm::countr_one(Imm) - (64 - Size);
234 }
235
236 // Encode in Immr the number of RORs it would take to get *from* 0^m 1^n
237 // to our target value, where I is the number of RORs to go the opposite
238 // direction.
239 assert(Size > I && "I should be smaller than element size");
240 unsigned Immr = (Size - I) & (Size - 1);
241
242 // If size has a 1 in the n'th bit, create a value that has zeroes in
243 // bits [0, n] and ones above that.
244 uint64_t NImms = ~(Size-1) << 1;
245
246 // Or the CTO value into the low bits, which must be below the Nth bit
247 // bit mentioned above.
248 NImms |= (CTO-1);
249
250 // Extract the seventh bit and toggle it to create the N field.
251 unsigned N = ((NImms >> 6) & 1) ^ 1;
252
253 Encoding = (N << 12) | (Immr << 6) | (NImms & 0x3f);
254 return true;
255}
256
257/// isLogicalImmediate - Return true if the immediate is valid for a logical
258/// immediate instruction of the given register size. Return false otherwise.
259static inline bool isLogicalImmediate(uint64_t imm, unsigned regSize) {
260 uint64_t encoding;
261 return processLogicalImmediate(imm, regSize, encoding);
262}
263
264/// encodeLogicalImmediate - Return the encoded immediate value for a logical
265/// immediate instruction of the given register size.
266static inline uint64_t encodeLogicalImmediate(uint64_t imm, unsigned regSize) {
267 uint64_t encoding = 0;
268 bool res = processLogicalImmediate(imm, regSize, encoding);
269 assert(res && "invalid logical immediate");
270 (void)res;
271 return encoding;
272}
273
274/// decodeLogicalImmediate - Decode a logical immediate value in the form
275/// "N:immr:imms" (where the immr and imms fields are each 6 bits) into the
276/// integer value it represents with regSize bits.
277static inline uint64_t decodeLogicalImmediate(uint64_t val, unsigned regSize) {
278 // Extract the N, imms, and immr fields.
279 unsigned N = (val >> 12) & 1;
280 unsigned immr = (val >> 6) & 0x3f;
281 unsigned imms = val & 0x3f;
282
283 assert((regSize == 64 || N == 0) && "undefined logical immediate encoding");
284 int len = 31 - llvm::countl_zero((N << 6) | (~imms & 0x3f));
285 assert(len >= 0 && "undefined logical immediate encoding");
286 unsigned size = (1 << len);
287 unsigned R = immr & (size - 1);
288 unsigned S = imms & (size - 1);
289 assert(S != size - 1 && "undefined logical immediate encoding");
290 uint64_t pattern = (1ULL << (S + 1)) - 1;
291 for (unsigned i = 0; i < R; ++i)
292 pattern = ror(pattern, size);
293
294 // Replicate the pattern to fill the regSize.
295 while (size != regSize) {
296 pattern |= (pattern << size);
297 size *= 2;
298 }
299 return pattern;
300}
301
302/// isValidDecodeLogicalImmediate - Check to see if the logical immediate value
303/// in the form "N:immr:imms" (where the immr and imms fields are each 6 bits)
304/// is a valid encoding for an integer value with regSize bits.
306 unsigned regSize) {
307 // Extract the N and imms fields needed for checking.
308 unsigned N = (val >> 12) & 1;
309 unsigned imms = val & 0x3f;
310
311 if (regSize == 32 && N != 0) // undefined logical immediate encoding
312 return false;
313 int len = 31 - llvm::countl_zero((N << 6) | (~imms & 0x3f));
314 if (len < 0) // undefined logical immediate encoding
315 return false;
316 unsigned size = (1 << len);
317 unsigned S = imms & (size - 1);
318 if (S == size - 1) // undefined logical immediate encoding
319 return false;
320
321 return true;
322}
323
324/// isLegalArithImmed - \returns true if \p C is a legal immediate operand for
325/// an arithmetic instruction.
326constexpr bool isLegalArithImmed(const uint64_t C) {
327 return (C >> 12 == 0) || ((C & 0xFFFULL) == 0 && C >> 24 == 0);
328}
329
330/// getArithImmedShift - assumes \p C is a legal immediate for arithmetic
331/// instructions and \returns the required shift for this immediate.
332constexpr unsigned getArithImmedShift(const uint64_t C) {
334 "Tried to get the shift amount for an illegal immediate");
335 return C >> 12 == 0 ? 0 : 12;
336}
337
338/// isLegalCmpImmed - \returns true if \p C is a legal immediate operand for a
339/// comparison instruction.
340static inline bool isLegalCmpImmed(const APInt &C) {
341 // Works for negative immediates too, as it can be written as an ADDS
342 // instruction with a negated immediate.
343 return isLegalArithImmed(C.abs().getZExtValue());
344}
345
346//===----------------------------------------------------------------------===//
347// Floating-point Immediates
348//
349static inline float getFPImmFloat(unsigned Imm) {
350 // We expect an 8-bit binary encoding of a floating-point number here.
351
352 uint8_t Sign = (Imm >> 7) & 0x1;
353 uint8_t Exp = (Imm >> 4) & 0x7;
354 uint8_t Mantissa = Imm & 0xf;
355
356 // 8-bit FP IEEE Float Encoding
357 // abcd efgh aBbbbbbc defgh000 00000000 00000000
358 //
359 // where B = NOT(b);
360
361 uint32_t I = 0;
362 I |= Sign << 31;
363 I |= ((Exp & 0x4) != 0 ? 0 : 1) << 30;
364 I |= ((Exp & 0x4) != 0 ? 0x1f : 0) << 25;
365 I |= (Exp & 0x3) << 23;
366 I |= Mantissa << 19;
367 return bit_cast<float>(I);
368}
369
370/// getFP16Imm - Return an 8-bit floating-point version of the 16-bit
371/// floating-point value. If the value cannot be represented as an 8-bit
372/// floating-point value, then return -1.
373static inline int getFP16Imm(const APInt &Imm) {
374 uint32_t Sign = Imm.lshr(15).getZExtValue() & 1;
375 int32_t Exp = (Imm.lshr(10).getSExtValue() & 0x1f) - 15; // -14 to 15
376 int32_t Mantissa = Imm.getZExtValue() & 0x3ff; // 10 bits
377
378 // We can handle 4 bits of mantissa.
379 // mantissa = (16+UInt(e:f:g:h))/16.
380 if (Mantissa & 0x3f)
381 return -1;
382 Mantissa >>= 6;
383
384 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
385 if (Exp < -3 || Exp > 4)
386 return -1;
387 Exp = ((Exp+3) & 0x7) ^ 4;
388
389 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
390}
391
392static inline int getFP16Imm(const APFloat &FPImm) {
393 return getFP16Imm(FPImm.bitcastToAPInt());
394}
395
396/// getFP32Imm - Return an 8-bit floating-point version of the 32-bit
397/// floating-point value. If the value cannot be represented as an 8-bit
398/// floating-point value, then return -1.
399static inline int getFP32Imm(const APInt &Imm) {
400 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
401 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
402 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
403
404 // We can handle 4 bits of mantissa.
405 // mantissa = (16+UInt(e:f:g:h))/16.
406 if (Mantissa & 0x7ffff)
407 return -1;
408 Mantissa >>= 19;
409 if ((Mantissa & 0xf) != Mantissa)
410 return -1;
411
412 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
413 if (Exp < -3 || Exp > 4)
414 return -1;
415 Exp = ((Exp+3) & 0x7) ^ 4;
416
417 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
418}
419
420static inline int getFP32Imm(const APFloat &FPImm) {
421 return getFP32Imm(FPImm.bitcastToAPInt());
422}
423
424/// getFP64Imm - Return an 8-bit floating-point version of the 64-bit
425/// floating-point value. If the value cannot be represented as an 8-bit
426/// floating-point value, then return -1.
427static inline int getFP64Imm(const APInt &Imm) {
428 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
429 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
430 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffULL;
431
432 // We can handle 4 bits of mantissa.
433 // mantissa = (16+UInt(e:f:g:h))/16.
434 if (Mantissa & 0xffffffffffffULL)
435 return -1;
436 Mantissa >>= 48;
437 if ((Mantissa & 0xf) != Mantissa)
438 return -1;
439
440 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
441 if (Exp < -3 || Exp > 4)
442 return -1;
443 Exp = ((Exp+3) & 0x7) ^ 4;
444
445 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
446}
447
448static inline int getFP64Imm(const APFloat &FPImm) {
449 return getFP64Imm(FPImm.bitcastToAPInt());
450}
451
452//===--------------------------------------------------------------------===//
453// AdvSIMD Modified Immediates
454//===--------------------------------------------------------------------===//
455
456// 0x00 0x00 0x00 abcdefgh 0x00 0x00 0x00 abcdefgh
457static inline bool isAdvSIMDModImmType1(uint64_t Imm) {
458 return ((Imm >> 32) == (Imm & 0xffffffffULL)) &&
459 ((Imm & 0xffffff00ffffff00ULL) == 0);
460}
461
463 return (Imm & 0xffULL);
464}
465
467 uint64_t EncVal = Imm;
468 return (EncVal << 32) | EncVal;
469}
470
471// 0x00 0x00 abcdefgh 0x00 0x00 0x00 abcdefgh 0x00
472static inline bool isAdvSIMDModImmType2(uint64_t Imm) {
473 return ((Imm >> 32) == (Imm & 0xffffffffULL)) &&
474 ((Imm & 0xffff00ffffff00ffULL) == 0);
475}
476
478 return (Imm & 0xff00ULL) >> 8;
479}
480
482 uint64_t EncVal = Imm;
483 return (EncVal << 40) | (EncVal << 8);
484}
485
486// 0x00 abcdefgh 0x00 0x00 0x00 abcdefgh 0x00 0x00
487static inline bool isAdvSIMDModImmType3(uint64_t Imm) {
488 return ((Imm >> 32) == (Imm & 0xffffffffULL)) &&
489 ((Imm & 0xff00ffffff00ffffULL) == 0);
490}
491
493 return (Imm & 0xff0000ULL) >> 16;
494}
495
497 uint64_t EncVal = Imm;
498 return (EncVal << 48) | (EncVal << 16);
499}
500
501// abcdefgh 0x00 0x00 0x00 abcdefgh 0x00 0x00 0x00
502static inline bool isAdvSIMDModImmType4(uint64_t Imm) {
503 return ((Imm >> 32) == (Imm & 0xffffffffULL)) &&
504 ((Imm & 0x00ffffff00ffffffULL) == 0);
505}
506
508 return (Imm & 0xff000000ULL) >> 24;
509}
510
512 uint64_t EncVal = Imm;
513 return (EncVal << 56) | (EncVal << 24);
514}
515
516// 0x00 abcdefgh 0x00 abcdefgh 0x00 abcdefgh 0x00 abcdefgh
517static inline bool isAdvSIMDModImmType5(uint64_t Imm) {
518 return ((Imm >> 32) == (Imm & 0xffffffffULL)) &&
519 (((Imm & 0x00ff0000ULL) >> 16) == (Imm & 0x000000ffULL)) &&
520 ((Imm & 0xff00ff00ff00ff00ULL) == 0);
521}
522
524 return (Imm & 0xffULL);
525}
526
528 uint64_t EncVal = Imm;
529 return (EncVal << 48) | (EncVal << 32) | (EncVal << 16) | EncVal;
530}
531
532// abcdefgh 0x00 abcdefgh 0x00 abcdefgh 0x00 abcdefgh 0x00
533static inline bool isAdvSIMDModImmType6(uint64_t Imm) {
534 return ((Imm >> 32) == (Imm & 0xffffffffULL)) &&
535 (((Imm & 0xff000000ULL) >> 16) == (Imm & 0x0000ff00ULL)) &&
536 ((Imm & 0x00ff00ff00ff00ffULL) == 0);
537}
538
540 return (Imm & 0xff00ULL) >> 8;
541}
542
544 uint64_t EncVal = Imm;
545 return (EncVal << 56) | (EncVal << 40) | (EncVal << 24) | (EncVal << 8);
546}
547
548// 0x00 0x00 abcdefgh 0xFF 0x00 0x00 abcdefgh 0xFF
549static inline bool isAdvSIMDModImmType7(uint64_t Imm) {
550 return ((Imm >> 32) == (Imm & 0xffffffffULL)) &&
551 ((Imm & 0xffff00ffffff00ffULL) == 0x000000ff000000ffULL);
552}
553
555 return (Imm & 0xff00ULL) >> 8;
556}
557
559 uint64_t EncVal = Imm;
560 return (EncVal << 40) | (EncVal << 8) | 0x000000ff000000ffULL;
561}
562
563// 0x00 abcdefgh 0xFF 0xFF 0x00 abcdefgh 0xFF 0xFF
564static inline bool isAdvSIMDModImmType8(uint64_t Imm) {
565 return ((Imm >> 32) == (Imm & 0xffffffffULL)) &&
566 ((Imm & 0xff00ffffff00ffffULL) == 0x0000ffff0000ffffULL);
567}
568
570 uint64_t EncVal = Imm;
571 return (EncVal << 48) | (EncVal << 16) | 0x0000ffff0000ffffULL;
572}
573
575 return (Imm & 0x00ff0000ULL) >> 16;
576}
577
578// abcdefgh abcdefgh abcdefgh abcdefgh abcdefgh abcdefgh abcdefgh abcdefgh
579static inline bool isAdvSIMDModImmType9(uint64_t Imm) {
580 return ((Imm >> 32) == (Imm & 0xffffffffULL)) &&
581 ((Imm >> 48) == (Imm & 0x0000ffffULL)) &&
582 ((Imm >> 56) == (Imm & 0x000000ffULL));
583}
584
586 return (Imm & 0xffULL);
587}
588
590 uint64_t EncVal = Imm;
591 EncVal |= (EncVal << 8);
592 EncVal |= (EncVal << 16);
593 EncVal |= (EncVal << 32);
594 return EncVal;
595}
596
597// aaaaaaaa bbbbbbbb cccccccc dddddddd eeeeeeee ffffffff gggggggg hhhhhhhh
598// cmode: 1110, op: 1
599static inline bool isAdvSIMDModImmType10(uint64_t Imm) {
600#if defined(_MSC_VER) && _MSC_VER == 1937 && !defined(__clang__) && \
601 defined(_M_ARM64)
602 // The MSVC compiler 19.37 for ARM64 has an optimization bug that
603 // causes an incorrect behavior with the original version. Work around
604 // by using a slightly different variation.
605 // https://developercommunity.visualstudio.com/t/C-ARM64-compiler-optimization-bug/10481261
606 constexpr uint64_t Mask = 0xFFULL;
607 uint64_t ByteA = (Imm >> 56) & Mask;
608 uint64_t ByteB = (Imm >> 48) & Mask;
609 uint64_t ByteC = (Imm >> 40) & Mask;
610 uint64_t ByteD = (Imm >> 32) & Mask;
611 uint64_t ByteE = (Imm >> 24) & Mask;
612 uint64_t ByteF = (Imm >> 16) & Mask;
613 uint64_t ByteG = (Imm >> 8) & Mask;
614 uint64_t ByteH = Imm & Mask;
615
616 return (ByteA == 0ULL || ByteA == Mask) && (ByteB == 0ULL || ByteB == Mask) &&
617 (ByteC == 0ULL || ByteC == Mask) && (ByteD == 0ULL || ByteD == Mask) &&
618 (ByteE == 0ULL || ByteE == Mask) && (ByteF == 0ULL || ByteF == Mask) &&
619 (ByteG == 0ULL || ByteG == Mask) && (ByteH == 0ULL || ByteH == Mask);
620#else
621 uint64_t ByteA = Imm & 0xff00000000000000ULL;
622 uint64_t ByteB = Imm & 0x00ff000000000000ULL;
623 uint64_t ByteC = Imm & 0x0000ff0000000000ULL;
624 uint64_t ByteD = Imm & 0x000000ff00000000ULL;
625 uint64_t ByteE = Imm & 0x00000000ff000000ULL;
626 uint64_t ByteF = Imm & 0x0000000000ff0000ULL;
627 uint64_t ByteG = Imm & 0x000000000000ff00ULL;
628 uint64_t ByteH = Imm & 0x00000000000000ffULL;
629
630 return (ByteA == 0ULL || ByteA == 0xff00000000000000ULL) &&
631 (ByteB == 0ULL || ByteB == 0x00ff000000000000ULL) &&
632 (ByteC == 0ULL || ByteC == 0x0000ff0000000000ULL) &&
633 (ByteD == 0ULL || ByteD == 0x000000ff00000000ULL) &&
634 (ByteE == 0ULL || ByteE == 0x00000000ff000000ULL) &&
635 (ByteF == 0ULL || ByteF == 0x0000000000ff0000ULL) &&
636 (ByteG == 0ULL || ByteG == 0x000000000000ff00ULL) &&
637 (ByteH == 0ULL || ByteH == 0x00000000000000ffULL);
638#endif
639}
640
642 uint8_t BitA = (Imm & 0xff00000000000000ULL) != 0;
643 uint8_t BitB = (Imm & 0x00ff000000000000ULL) != 0;
644 uint8_t BitC = (Imm & 0x0000ff0000000000ULL) != 0;
645 uint8_t BitD = (Imm & 0x000000ff00000000ULL) != 0;
646 uint8_t BitE = (Imm & 0x00000000ff000000ULL) != 0;
647 uint8_t BitF = (Imm & 0x0000000000ff0000ULL) != 0;
648 uint8_t BitG = (Imm & 0x000000000000ff00ULL) != 0;
649 uint8_t BitH = (Imm & 0x00000000000000ffULL) != 0;
650
651 uint8_t EncVal = BitA;
652 EncVal <<= 1;
653 EncVal |= BitB;
654 EncVal <<= 1;
655 EncVal |= BitC;
656 EncVal <<= 1;
657 EncVal |= BitD;
658 EncVal <<= 1;
659 EncVal |= BitE;
660 EncVal <<= 1;
661 EncVal |= BitF;
662 EncVal <<= 1;
663 EncVal |= BitG;
664 EncVal <<= 1;
665 EncVal |= BitH;
666 return EncVal;
667}
668
670 uint64_t EncVal = 0;
671 if (Imm & 0x80) EncVal |= 0xff00000000000000ULL;
672 if (Imm & 0x40) EncVal |= 0x00ff000000000000ULL;
673 if (Imm & 0x20) EncVal |= 0x0000ff0000000000ULL;
674 if (Imm & 0x10) EncVal |= 0x000000ff00000000ULL;
675 if (Imm & 0x08) EncVal |= 0x00000000ff000000ULL;
676 if (Imm & 0x04) EncVal |= 0x0000000000ff0000ULL;
677 if (Imm & 0x02) EncVal |= 0x000000000000ff00ULL;
678 if (Imm & 0x01) EncVal |= 0x00000000000000ffULL;
679 return EncVal;
680}
681
682// aBbbbbbc defgh000 0x00 0x00 aBbbbbbc defgh000 0x00 0x00
683static inline bool isAdvSIMDModImmType11(uint64_t Imm) {
684 uint64_t BString = (Imm & 0x7E000000ULL) >> 25;
685 return ((Imm >> 32) == (Imm & 0xffffffffULL)) &&
686 (BString == 0x1f || BString == 0x20) &&
687 ((Imm & 0x0007ffff0007ffffULL) == 0);
688}
689
691 uint8_t BitA = (Imm & 0x80000000ULL) != 0;
692 uint8_t BitB = (Imm & 0x20000000ULL) != 0;
693 uint8_t BitC = (Imm & 0x01000000ULL) != 0;
694 uint8_t BitD = (Imm & 0x00800000ULL) != 0;
695 uint8_t BitE = (Imm & 0x00400000ULL) != 0;
696 uint8_t BitF = (Imm & 0x00200000ULL) != 0;
697 uint8_t BitG = (Imm & 0x00100000ULL) != 0;
698 uint8_t BitH = (Imm & 0x00080000ULL) != 0;
699
700 uint8_t EncVal = BitA;
701 EncVal <<= 1;
702 EncVal |= BitB;
703 EncVal <<= 1;
704 EncVal |= BitC;
705 EncVal <<= 1;
706 EncVal |= BitD;
707 EncVal <<= 1;
708 EncVal |= BitE;
709 EncVal <<= 1;
710 EncVal |= BitF;
711 EncVal <<= 1;
712 EncVal |= BitG;
713 EncVal <<= 1;
714 EncVal |= BitH;
715 return EncVal;
716}
717
719 uint64_t EncVal = 0;
720 if (Imm & 0x80) EncVal |= 0x80000000ULL;
721 if (Imm & 0x40) EncVal |= 0x3e000000ULL;
722 else EncVal |= 0x40000000ULL;
723 if (Imm & 0x20) EncVal |= 0x01000000ULL;
724 if (Imm & 0x10) EncVal |= 0x00800000ULL;
725 if (Imm & 0x08) EncVal |= 0x00400000ULL;
726 if (Imm & 0x04) EncVal |= 0x00200000ULL;
727 if (Imm & 0x02) EncVal |= 0x00100000ULL;
728 if (Imm & 0x01) EncVal |= 0x00080000ULL;
729 return (EncVal << 32) | EncVal;
730}
731
732// aBbbbbbb bbcdefgh 0x00 0x00 0x00 0x00 0x00 0x00
733static inline bool isAdvSIMDModImmType12(uint64_t Imm) {
734 uint64_t BString = (Imm & 0x7fc0000000000000ULL) >> 54;
735 return ((BString == 0xff || BString == 0x100) &&
736 ((Imm & 0x0000ffffffffffffULL) == 0));
737}
738
740 uint8_t BitA = (Imm & 0x8000000000000000ULL) != 0;
741 uint8_t BitB = (Imm & 0x0040000000000000ULL) != 0;
742 uint8_t BitC = (Imm & 0x0020000000000000ULL) != 0;
743 uint8_t BitD = (Imm & 0x0010000000000000ULL) != 0;
744 uint8_t BitE = (Imm & 0x0008000000000000ULL) != 0;
745 uint8_t BitF = (Imm & 0x0004000000000000ULL) != 0;
746 uint8_t BitG = (Imm & 0x0002000000000000ULL) != 0;
747 uint8_t BitH = (Imm & 0x0001000000000000ULL) != 0;
748
749 uint8_t EncVal = BitA;
750 EncVal <<= 1;
751 EncVal |= BitB;
752 EncVal <<= 1;
753 EncVal |= BitC;
754 EncVal <<= 1;
755 EncVal |= BitD;
756 EncVal <<= 1;
757 EncVal |= BitE;
758 EncVal <<= 1;
759 EncVal |= BitF;
760 EncVal <<= 1;
761 EncVal |= BitG;
762 EncVal <<= 1;
763 EncVal |= BitH;
764 return EncVal;
765}
766
768 uint64_t EncVal = 0;
769 if (Imm & 0x80) EncVal |= 0x8000000000000000ULL;
770 if (Imm & 0x40) EncVal |= 0x3fc0000000000000ULL;
771 else EncVal |= 0x4000000000000000ULL;
772 if (Imm & 0x20) EncVal |= 0x0020000000000000ULL;
773 if (Imm & 0x10) EncVal |= 0x0010000000000000ULL;
774 if (Imm & 0x08) EncVal |= 0x0008000000000000ULL;
775 if (Imm & 0x04) EncVal |= 0x0004000000000000ULL;
776 if (Imm & 0x02) EncVal |= 0x0002000000000000ULL;
777 if (Imm & 0x01) EncVal |= 0x0001000000000000ULL;
778 return EncVal;
779}
780
781/// Returns true if Imm is the concatenation of a repeating pattern of type T.
782template <typename T>
783static inline bool isSVEMaskOfIdenticalElements(int64_t Imm) {
784 auto Parts = bit_cast<std::array<T, sizeof(int64_t) / sizeof(T)>>(Imm);
785 return llvm::all_equal(Parts);
786}
787
788/// Returns true if Imm is valid for CPY/DUP.
789template <typename T>
790static inline bool isSVECpyImm(int64_t Imm) {
791 // Imm is interpreted as a signed value, which means top bits must be all ones
792 // (sign bits if the immediate value is negative and passed in a larger
793 // container), or all zeroes.
794 int64_t Mask = ~int64_t(std::numeric_limits<std::make_unsigned_t<T>>::max());
795 if ((Imm & Mask) != 0 && (Imm & Mask) != Mask)
796 return false;
797
798 // Imm is a signed 8-bit value.
799 // Top bits must be zeroes or sign bits.
800 if (Imm & 0xff)
801 return int8_t(Imm) == T(Imm);
802
803 // Imm is a signed 16-bit value and multiple of 256.
804 // Top bits must be zeroes or sign bits.
805 if (Imm & 0xff00)
806 return int16_t(Imm) == T(Imm);
807
808 return Imm == 0;
809}
810
811/// Returns true if Imm is valid for ADD/SUB.
812template <typename T>
813static inline bool isSVEAddSubImm(int64_t Imm) {
814 bool IsInt8t = std::is_same<int8_t, std::make_signed_t<T>>::value ||
815 std::is_same<int8_t, T>::value;
816 return uint8_t(Imm) == Imm || (!IsInt8t && uint16_t(Imm & ~0xff) == Imm);
817}
818
819/// Return true if Imm is valid for DUPM and has no single CPY/DUP equivalent.
820static inline bool isSVEMoveMaskPreferredLogicalImmediate(int64_t Imm) {
822 return false;
823
827
829 return false;
831 return false;
833 return false;
834 return isLogicalImmediate(Imm, 64);
835}
836
837inline static bool isAnyMOVZMovAlias(uint64_t Value, int RegWidth) {
838 for (int Shift = 0; Shift <= RegWidth - 16; Shift += 16)
839 if ((Value & ~(0xffffULL << Shift)) == 0)
840 return true;
841
842 return false;
843}
844
845inline static bool isMOVZMovAlias(uint64_t Value, int Shift, int RegWidth) {
846 if (RegWidth == 32)
847 Value &= 0xffffffffULL;
848
849 // "lsl #0" takes precedence: in practice this only affects "#0, lsl #0".
850 if (Value == 0 && Shift != 0)
851 return false;
852
853 return (Value & ~(0xffffULL << Shift)) == 0;
854}
855
856inline static bool isMOVNMovAlias(uint64_t Value, int Shift, int RegWidth) {
857 // MOVZ takes precedence over MOVN.
858 if (isAnyMOVZMovAlias(Value, RegWidth))
859 return false;
860
861 Value = ~Value;
862 if (RegWidth == 32)
863 Value &= 0xffffffffULL;
864
865 return isMOVZMovAlias(Value, Shift, RegWidth);
866}
867
868inline static bool isAnyMOVWMovAlias(uint64_t Value, int RegWidth) {
869 if (isAnyMOVZMovAlias(Value, RegWidth))
870 return true;
871
872 // It's not a MOVZ, but it might be a MOVN.
873 Value = ~Value;
874 if (RegWidth == 32)
875 Value &= 0xffffffffULL;
876
877 return isAnyMOVZMovAlias(Value, RegWidth);
878}
879
880static inline bool isSVECpyDupImm(int SizeInBits, int64_t Val, int32_t &Imm,
881 int32_t &Shift) {
882 switch (SizeInBits) {
883 case 8:
884 // All immediates are supported.
885 Shift = 0;
886 Imm = Val & 0xFF;
887 return true;
888 case 16:
889 case 32:
890 case 64:
891 // Support 8bit signed immediates.
892 if (Val >= -128 && Val <= 127) {
893 Shift = 0;
894 Imm = Val & 0xFF;
895 return true;
896 }
897 // Support 16bit signed immediates that are a multiple of 256.
898 if (Val >= -32768 && Val <= 32512 && Val % 256 == 0) {
899 Shift = 8;
900 Imm = (Val >> 8) & 0xFF;
901 return true;
902 }
903 break;
904 default:
905 break;
906 }
907 return false;
908}
909
910static inline bool isSVELogicalImm(unsigned SizeInBits, uint64_t ImmVal,
911 uint64_t &Encoding) {
912 // Shift mask depending on type size.
913 switch (SizeInBits) {
914 case 8:
915 ImmVal &= 0xFF;
916 ImmVal |= ImmVal << 8;
917 ImmVal |= ImmVal << 16;
918 ImmVal |= ImmVal << 32;
919 break;
920 case 16:
921 ImmVal &= 0xFFFF;
922 ImmVal |= ImmVal << 16;
923 ImmVal |= ImmVal << 32;
924 break;
925 case 32:
926 ImmVal &= 0xFFFFFFFF;
927 ImmVal |= ImmVal << 32;
928 break;
929 case 64:
930 break;
931 default:
932 llvm_unreachable("Unexpected size");
933 }
934
935 return processLogicalImmediate(ImmVal, 64, Encoding);
936}
937
938} // end namespace AArch64_AM
939
940} // end namespace llvm
941
942#endif
unsigned RegSize
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned Imm
unsigned uint64_t
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define I(x, y, z)
Definition MD5.cpp:57
#define H(x, y, z)
Definition MD5.cpp:56
#define T
This file implements the C++20 <bit> header.
APInt bitcastToAPInt() const
Definition APFloat.h:1475
Class for arbitrary precision integers.
Definition APInt.h:78
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM Value Representation.
Definition Value.h:75
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
AArch64_AM - AArch64 Addressing Mode Stuff.
static bool isValidDecodeLogicalImmediate(uint64_t val, unsigned regSize)
isValidDecodeLogicalImmediate - Check to see if the logical immediate value in the form "N:immr:imms"...
static bool isSVEMoveMaskPreferredLogicalImmediate(int64_t Imm)
Return true if Imm is valid for DUPM and has no single CPY/DUP equivalent.
static bool isAnyMOVZMovAlias(uint64_t Value, int RegWidth)
static bool isMOVNMovAlias(uint64_t Value, int Shift, int RegWidth)
static uint64_t decodeLogicalImmediate(uint64_t val, unsigned regSize)
decodeLogicalImmediate - Decode a logical immediate value in the form "N:immr:imms" (where the immr a...
static unsigned getShiftValue(unsigned Imm)
getShiftValue - Extract the shift value.
static uint64_t decodeAdvSIMDModImmType4(uint8_t Imm)
static bool isLogicalImmediate(uint64_t imm, unsigned regSize)
isLogicalImmediate - Return true if the immediate is valid for a logical immediate instruction of the...
static uint8_t encodeAdvSIMDModImmType2(uint64_t Imm)
static bool isSVEAddSubImm(int64_t Imm)
Returns true if Imm is valid for ADD/SUB.
static bool processLogicalImmediate(uint64_t Imm, unsigned RegSize, uint64_t &Encoding)
processLogicalImmediate - Determine if an immediate value can be encoded as the immediate operand of ...
static bool isAdvSIMDModImmType9(uint64_t Imm)
static uint64_t decodeAdvSIMDModImmType2(uint8_t Imm)
static bool isAdvSIMDModImmType4(uint64_t Imm)
static unsigned getArithExtendImm(AArch64_AM::ShiftExtendType ET, unsigned Imm)
getArithExtendImm - Encode the extend type and shift amount for an arithmetic instruction: imm: 3-bit...
static uint64_t decodeAdvSIMDModImmType12(uint8_t Imm)
constexpr bool isLegalArithImmed(const uint64_t C)
isLegalArithImmed -
static bool isAdvSIMDModImmType5(uint64_t Imm)
static bool isAnyMOVWMovAlias(uint64_t Value, int RegWidth)
static unsigned getArithShiftValue(unsigned Imm)
getArithShiftValue - get the arithmetic shift value.
static uint64_t decodeAdvSIMDModImmType11(uint8_t Imm)
static int getFP32Imm(const APInt &Imm)
getFP32Imm - Return an 8-bit floating-point version of the 32-bit floating-point value.
static float getFPImmFloat(unsigned Imm)
static uint8_t encodeAdvSIMDModImmType7(uint64_t Imm)
static uint64_t decodeAdvSIMDModImmType1(uint8_t Imm)
static uint8_t encodeAdvSIMDModImmType12(uint64_t Imm)
static uint8_t encodeAdvSIMDModImmType10(uint64_t Imm)
static uint8_t encodeAdvSIMDModImmType9(uint64_t Imm)
static bool isSVEMaskOfIdenticalElements(int64_t Imm)
Returns true if Imm is the concatenation of a repeating pattern of type T.
static bool isMOVZMovAlias(uint64_t Value, int Shift, int RegWidth)
static uint64_t encodeLogicalImmediate(uint64_t imm, unsigned regSize)
encodeLogicalImmediate - Return the encoded immediate value for a logical immediate instruction of th...
static const char * getShiftExtendName(AArch64_AM::ShiftExtendType ST)
getShiftName - Get the string encoding for the shift type.
static bool isLegalCmpImmed(const APInt &C)
isLegalCmpImmed -
static bool isAdvSIMDModImmType7(uint64_t Imm)
static uint64_t decodeAdvSIMDModImmType3(uint8_t Imm)
static uint64_t decodeAdvSIMDModImmType7(uint8_t Imm)
unsigned getExtendEncoding(AArch64_AM::ShiftExtendType ET)
Mapping from extend bits to required operation: shifter: 000 ==> uxtb 001 ==> uxth 010 ==> uxtw 011 =...
static bool isSVECpyImm(int64_t Imm)
Returns true if Imm is valid for CPY/DUP.
static uint8_t encodeAdvSIMDModImmType5(uint64_t Imm)
static int getFP64Imm(const APInt &Imm)
getFP64Imm - Return an 8-bit floating-point version of the 64-bit floating-point value.
static uint64_t ror(uint64_t elt, unsigned size)
static bool isAdvSIMDModImmType10(uint64_t Imm)
static AArch64_AM::ShiftExtendType getExtendType(unsigned Imm)
getExtendType - Extract the extend type for operands of arithmetic ops.
static int getFP16Imm(const APInt &Imm)
getFP16Imm - Return an 8-bit floating-point version of the 16-bit floating-point value.
static uint64_t decodeAdvSIMDModImmType9(uint8_t Imm)
static uint64_t decodeAdvSIMDModImmType10(uint8_t Imm)
static uint64_t decodeAdvSIMDModImmType5(uint8_t Imm)
static uint64_t decodeAdvSIMDModImmType8(uint8_t Imm)
static uint8_t encodeAdvSIMDModImmType8(uint64_t Imm)
static bool isAdvSIMDModImmType12(uint64_t Imm)
static bool isSVELogicalImm(unsigned SizeInBits, uint64_t ImmVal, uint64_t &Encoding)
constexpr unsigned getArithImmedShift(const uint64_t C)
getArithImmedShift - assumes C is a legal immediate for arithmetic instructions and
static uint8_t encodeAdvSIMDModImmType11(uint64_t Imm)
static AArch64_AM::ShiftExtendType getArithExtendType(unsigned Imm)
static bool isSVECpyDupImm(int SizeInBits, int64_t Val, int32_t &Imm, int32_t &Shift)
static bool isAdvSIMDModImmType11(uint64_t Imm)
static uint8_t encodeAdvSIMDModImmType6(uint64_t Imm)
static AArch64_AM::ShiftExtendType getShiftType(unsigned Imm)
getShiftType - Extract the shift type.
static bool isAdvSIMDModImmType8(uint64_t Imm)
static uint8_t encodeAdvSIMDModImmType4(uint64_t Imm)
static unsigned getShifterImm(AArch64_AM::ShiftExtendType ST, unsigned Imm)
getShifterImm - Encode the shift type and amount: imm: 6-bit shift amount shifter: 000 ==> lsl 001 ==...
static bool isAdvSIMDModImmType6(uint64_t Imm)
static uint8_t encodeAdvSIMDModImmType1(uint64_t Imm)
static uint8_t encodeAdvSIMDModImmType3(uint64_t Imm)
static bool isAdvSIMDModImmType2(uint64_t Imm)
static uint64_t decodeAdvSIMDModImmType6(uint8_t Imm)
static bool isAdvSIMDModImmType3(uint64_t Imm)
static bool isSignExtendShiftType(AArch64_AM::ShiftExtendType Type)
isSignExtendShiftType - Returns true if Type is sign extending.
static bool isAdvSIMDModImmType1(uint64_t Imm)
This is an optimization pass for GlobalISel generic memory operations.
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1685
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition bit.h:315
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:204
constexpr bool isShiftedMask_64(uint64_t Value)
Return true if the argument contains a non-empty sequence of ones with the remainder zero (64 bit ver...
Definition MathExtras.h:274
int countl_zero(T Val)
Count number of 0's from the most significant bit to the least stopping at the first 1.
Definition bit.h:263
int countl_one(T Value)
Count the number of ones from the most significant bit to the first zero bit.
Definition bit.h:302
To bit_cast(const From &from) noexcept
Definition bit.h:90
constexpr NextUseDistance max(NextUseDistance A, NextUseDistance B)
bool all_equal(std::initializer_list< T > Values)
Returns true if all Values in the initializer lists are equal or the list.
Definition STLExtras.h:2182
#define N