LLVM 23.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.
86static inline AArch64_AM::ShiftExtendType getShiftType(unsigned Imm) {
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.
136static inline AArch64_AM::ShiftExtendType getExtendType(unsigned Imm) {
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
189/// getMemDoShift - Extract the "do shift" flag value for load/store
190/// instructions.
191static inline bool getMemDoShift(unsigned Imm) {
192 return (Imm & 0x1) != 0;
193}
194
195/// getExtendType - Extract the extend type for the offset operand of
196/// loads/stores.
198 return getExtendType((Imm >> 1) & 0x7);
199}
200
201/// getExtendImm - Encode the extend type and amount for a load/store inst:
202/// doshift: should the offset be scaled by the access size
203/// shifter: 000 ==> uxtb
204/// 001 ==> uxth
205/// 010 ==> uxtw
206/// 011 ==> uxtx
207/// 100 ==> sxtb
208/// 101 ==> sxth
209/// 110 ==> sxtw
210/// 111 ==> sxtx
211/// {3-1} = shifter
212/// {0} = doshift
214 bool DoShift) {
215 return (getExtendEncoding(ET) << 1) | unsigned(DoShift);
216}
217
218static inline uint64_t ror(uint64_t elt, unsigned size) {
219 return ((elt & 1) << (size-1)) | (elt >> 1);
220}
221
222/// processLogicalImmediate - Determine if an immediate value can be encoded
223/// as the immediate operand of a logical instruction for the given register
224/// size. If so, return true with "encoding" set to the encoded value in
225/// the form N:immr:imms.
226static inline bool processLogicalImmediate(uint64_t Imm, unsigned RegSize,
227 uint64_t &Encoding) {
228 if (Imm == 0ULL || Imm == ~0ULL ||
229 (RegSize != 64 &&
230 (Imm >> RegSize != 0 || Imm == (~0ULL >> (64 - RegSize)))))
231 return false;
232
233 // First, determine the element size.
234 unsigned Size = RegSize;
235
236 do {
237 Size /= 2;
238 uint64_t Mask = (1ULL << Size) - 1;
239
240 if ((Imm & Mask) != ((Imm >> Size) & Mask)) {
241 Size *= 2;
242 break;
243 }
244 } while (Size > 2);
245
246 // Second, determine the rotation to make the element be: 0^m 1^n.
247 uint32_t CTO, I;
248 uint64_t Mask = ((uint64_t)-1LL) >> (64 - Size);
249 Imm &= Mask;
250
251 if (isShiftedMask_64(Imm)) {
252 I = llvm::countr_zero(Imm);
253 assert(I < 64 && "undefined behavior");
254 CTO = llvm::countr_one(Imm >> I);
255 } else {
256 Imm |= ~Mask;
257 if (!isShiftedMask_64(~Imm))
258 return false;
259
260 unsigned CLO = llvm::countl_one(Imm);
261 I = 64 - CLO;
262 CTO = CLO + llvm::countr_one(Imm) - (64 - Size);
263 }
264
265 // Encode in Immr the number of RORs it would take to get *from* 0^m 1^n
266 // to our target value, where I is the number of RORs to go the opposite
267 // direction.
268 assert(Size > I && "I should be smaller than element size");
269 unsigned Immr = (Size - I) & (Size - 1);
270
271 // If size has a 1 in the n'th bit, create a value that has zeroes in
272 // bits [0, n] and ones above that.
273 uint64_t NImms = ~(Size-1) << 1;
274
275 // Or the CTO value into the low bits, which must be below the Nth bit
276 // bit mentioned above.
277 NImms |= (CTO-1);
278
279 // Extract the seventh bit and toggle it to create the N field.
280 unsigned N = ((NImms >> 6) & 1) ^ 1;
281
282 Encoding = (N << 12) | (Immr << 6) | (NImms & 0x3f);
283 return true;
284}
285
286/// isLogicalImmediate - Return true if the immediate is valid for a logical
287/// immediate instruction of the given register size. Return false otherwise.
288static inline bool isLogicalImmediate(uint64_t imm, unsigned regSize) {
289 uint64_t encoding;
290 return processLogicalImmediate(imm, regSize, encoding);
291}
292
293/// encodeLogicalImmediate - Return the encoded immediate value for a logical
294/// immediate instruction of the given register size.
295static inline uint64_t encodeLogicalImmediate(uint64_t imm, unsigned regSize) {
296 uint64_t encoding = 0;
297 bool res = processLogicalImmediate(imm, regSize, encoding);
298 assert(res && "invalid logical immediate");
299 (void)res;
300 return encoding;
301}
302
303/// decodeLogicalImmediate - Decode a logical immediate value in the form
304/// "N:immr:imms" (where the immr and imms fields are each 6 bits) into the
305/// integer value it represents with regSize bits.
306static inline uint64_t decodeLogicalImmediate(uint64_t val, unsigned regSize) {
307 // Extract the N, imms, and immr fields.
308 unsigned N = (val >> 12) & 1;
309 unsigned immr = (val >> 6) & 0x3f;
310 unsigned imms = val & 0x3f;
311
312 assert((regSize == 64 || N == 0) && "undefined logical immediate encoding");
313 int len = 31 - llvm::countl_zero((N << 6) | (~imms & 0x3f));
314 assert(len >= 0 && "undefined logical immediate encoding");
315 unsigned size = (1 << len);
316 unsigned R = immr & (size - 1);
317 unsigned S = imms & (size - 1);
318 assert(S != size - 1 && "undefined logical immediate encoding");
319 uint64_t pattern = (1ULL << (S + 1)) - 1;
320 for (unsigned i = 0; i < R; ++i)
321 pattern = ror(pattern, size);
322
323 // Replicate the pattern to fill the regSize.
324 while (size != regSize) {
325 pattern |= (pattern << size);
326 size *= 2;
327 }
328 return pattern;
329}
330
331/// isValidDecodeLogicalImmediate - Check to see if the logical immediate value
332/// in the form "N:immr:imms" (where the immr and imms fields are each 6 bits)
333/// is a valid encoding for an integer value with regSize bits.
335 unsigned regSize) {
336 // Extract the N and imms fields needed for checking.
337 unsigned N = (val >> 12) & 1;
338 unsigned imms = val & 0x3f;
339
340 if (regSize == 32 && N != 0) // undefined logical immediate encoding
341 return false;
342 int len = 31 - llvm::countl_zero((N << 6) | (~imms & 0x3f));
343 if (len < 0) // undefined logical immediate encoding
344 return false;
345 unsigned size = (1 << len);
346 unsigned S = imms & (size - 1);
347 if (S == size - 1) // undefined logical immediate encoding
348 return false;
349
350 return true;
351}
352
353/// isLegalArithImmed - \returns true if \p C is a legal immediate operand for
354/// an arithmetic instruction.
355constexpr bool isLegalArithImmed(const uint64_t C) {
356 return (C >> 12 == 0) || ((C & 0xFFFULL) == 0 && C >> 24 == 0);
357}
358
359/// getArithImmedShift - assumes \p C is a legal immediate for arithmetic
360/// instructions and \returns the required shift for this immediate.
361constexpr unsigned getArithImmedShift(const uint64_t C) {
363 "Tried to get the shift amount for an illegal immediate");
364 return C >> 12 == 0 ? 0 : 12;
365}
366
367/// isLegalCmpImmed - \returns true if \p C is a legal immediate operand for a
368/// comparison instruction.
369static inline bool isLegalCmpImmed(const APInt &C) {
370 // Works for negative immediates too, as it can be written as an ADDS
371 // instruction with a negated immediate.
372 return isLegalArithImmed(C.abs().getZExtValue());
373}
374
375//===----------------------------------------------------------------------===//
376// Floating-point Immediates
377//
378static inline float getFPImmFloat(unsigned Imm) {
379 // We expect an 8-bit binary encoding of a floating-point number here.
380
381 uint8_t Sign = (Imm >> 7) & 0x1;
382 uint8_t Exp = (Imm >> 4) & 0x7;
383 uint8_t Mantissa = Imm & 0xf;
384
385 // 8-bit FP IEEE Float Encoding
386 // abcd efgh aBbbbbbc defgh000 00000000 00000000
387 //
388 // where B = NOT(b);
389
390 uint32_t I = 0;
391 I |= Sign << 31;
392 I |= ((Exp & 0x4) != 0 ? 0 : 1) << 30;
393 I |= ((Exp & 0x4) != 0 ? 0x1f : 0) << 25;
394 I |= (Exp & 0x3) << 23;
395 I |= Mantissa << 19;
396 return bit_cast<float>(I);
397}
398
399/// getFP16Imm - Return an 8-bit floating-point version of the 16-bit
400/// floating-point value. If the value cannot be represented as an 8-bit
401/// floating-point value, then return -1.
402static inline int getFP16Imm(const APInt &Imm) {
403 uint32_t Sign = Imm.lshr(15).getZExtValue() & 1;
404 int32_t Exp = (Imm.lshr(10).getSExtValue() & 0x1f) - 15; // -14 to 15
405 int32_t Mantissa = Imm.getZExtValue() & 0x3ff; // 10 bits
406
407 // We can handle 4 bits of mantissa.
408 // mantissa = (16+UInt(e:f:g:h))/16.
409 if (Mantissa & 0x3f)
410 return -1;
411 Mantissa >>= 6;
412
413 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
414 if (Exp < -3 || Exp > 4)
415 return -1;
416 Exp = ((Exp+3) & 0x7) ^ 4;
417
418 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
419}
420
421static inline int getFP16Imm(const APFloat &FPImm) {
422 return getFP16Imm(FPImm.bitcastToAPInt());
423}
424
425/// getFP32Imm - Return an 8-bit floating-point version of the 32-bit
426/// floating-point value. If the value cannot be represented as an 8-bit
427/// floating-point value, then return -1.
428static inline int getFP32Imm(const APInt &Imm) {
429 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
430 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
431 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
432
433 // We can handle 4 bits of mantissa.
434 // mantissa = (16+UInt(e:f:g:h))/16.
435 if (Mantissa & 0x7ffff)
436 return -1;
437 Mantissa >>= 19;
438 if ((Mantissa & 0xf) != Mantissa)
439 return -1;
440
441 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
442 if (Exp < -3 || Exp > 4)
443 return -1;
444 Exp = ((Exp+3) & 0x7) ^ 4;
445
446 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
447}
448
449static inline int getFP32Imm(const APFloat &FPImm) {
450 return getFP32Imm(FPImm.bitcastToAPInt());
451}
452
453/// getFP64Imm - Return an 8-bit floating-point version of the 64-bit
454/// floating-point value. If the value cannot be represented as an 8-bit
455/// floating-point value, then return -1.
456static inline int getFP64Imm(const APInt &Imm) {
457 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
458 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
459 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffULL;
460
461 // We can handle 4 bits of mantissa.
462 // mantissa = (16+UInt(e:f:g:h))/16.
463 if (Mantissa & 0xffffffffffffULL)
464 return -1;
465 Mantissa >>= 48;
466 if ((Mantissa & 0xf) != Mantissa)
467 return -1;
468
469 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
470 if (Exp < -3 || Exp > 4)
471 return -1;
472 Exp = ((Exp+3) & 0x7) ^ 4;
473
474 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
475}
476
477static inline int getFP64Imm(const APFloat &FPImm) {
478 return getFP64Imm(FPImm.bitcastToAPInt());
479}
480
481//===--------------------------------------------------------------------===//
482// AdvSIMD Modified Immediates
483//===--------------------------------------------------------------------===//
484
485// 0x00 0x00 0x00 abcdefgh 0x00 0x00 0x00 abcdefgh
486static inline bool isAdvSIMDModImmType1(uint64_t Imm) {
487 return ((Imm >> 32) == (Imm & 0xffffffffULL)) &&
488 ((Imm & 0xffffff00ffffff00ULL) == 0);
489}
490
492 return (Imm & 0xffULL);
493}
494
496 uint64_t EncVal = Imm;
497 return (EncVal << 32) | EncVal;
498}
499
500// 0x00 0x00 abcdefgh 0x00 0x00 0x00 abcdefgh 0x00
501static inline bool isAdvSIMDModImmType2(uint64_t Imm) {
502 return ((Imm >> 32) == (Imm & 0xffffffffULL)) &&
503 ((Imm & 0xffff00ffffff00ffULL) == 0);
504}
505
507 return (Imm & 0xff00ULL) >> 8;
508}
509
511 uint64_t EncVal = Imm;
512 return (EncVal << 40) | (EncVal << 8);
513}
514
515// 0x00 abcdefgh 0x00 0x00 0x00 abcdefgh 0x00 0x00
516static inline bool isAdvSIMDModImmType3(uint64_t Imm) {
517 return ((Imm >> 32) == (Imm & 0xffffffffULL)) &&
518 ((Imm & 0xff00ffffff00ffffULL) == 0);
519}
520
522 return (Imm & 0xff0000ULL) >> 16;
523}
524
526 uint64_t EncVal = Imm;
527 return (EncVal << 48) | (EncVal << 16);
528}
529
530// abcdefgh 0x00 0x00 0x00 abcdefgh 0x00 0x00 0x00
531static inline bool isAdvSIMDModImmType4(uint64_t Imm) {
532 return ((Imm >> 32) == (Imm & 0xffffffffULL)) &&
533 ((Imm & 0x00ffffff00ffffffULL) == 0);
534}
535
537 return (Imm & 0xff000000ULL) >> 24;
538}
539
541 uint64_t EncVal = Imm;
542 return (EncVal << 56) | (EncVal << 24);
543}
544
545// 0x00 abcdefgh 0x00 abcdefgh 0x00 abcdefgh 0x00 abcdefgh
546static inline bool isAdvSIMDModImmType5(uint64_t Imm) {
547 return ((Imm >> 32) == (Imm & 0xffffffffULL)) &&
548 (((Imm & 0x00ff0000ULL) >> 16) == (Imm & 0x000000ffULL)) &&
549 ((Imm & 0xff00ff00ff00ff00ULL) == 0);
550}
551
553 return (Imm & 0xffULL);
554}
555
557 uint64_t EncVal = Imm;
558 return (EncVal << 48) | (EncVal << 32) | (EncVal << 16) | EncVal;
559}
560
561// abcdefgh 0x00 abcdefgh 0x00 abcdefgh 0x00 abcdefgh 0x00
562static inline bool isAdvSIMDModImmType6(uint64_t Imm) {
563 return ((Imm >> 32) == (Imm & 0xffffffffULL)) &&
564 (((Imm & 0xff000000ULL) >> 16) == (Imm & 0x0000ff00ULL)) &&
565 ((Imm & 0x00ff00ff00ff00ffULL) == 0);
566}
567
569 return (Imm & 0xff00ULL) >> 8;
570}
571
573 uint64_t EncVal = Imm;
574 return (EncVal << 56) | (EncVal << 40) | (EncVal << 24) | (EncVal << 8);
575}
576
577// 0x00 0x00 abcdefgh 0xFF 0x00 0x00 abcdefgh 0xFF
578static inline bool isAdvSIMDModImmType7(uint64_t Imm) {
579 return ((Imm >> 32) == (Imm & 0xffffffffULL)) &&
580 ((Imm & 0xffff00ffffff00ffULL) == 0x000000ff000000ffULL);
581}
582
584 return (Imm & 0xff00ULL) >> 8;
585}
586
588 uint64_t EncVal = Imm;
589 return (EncVal << 40) | (EncVal << 8) | 0x000000ff000000ffULL;
590}
591
592// 0x00 abcdefgh 0xFF 0xFF 0x00 abcdefgh 0xFF 0xFF
593static inline bool isAdvSIMDModImmType8(uint64_t Imm) {
594 return ((Imm >> 32) == (Imm & 0xffffffffULL)) &&
595 ((Imm & 0xff00ffffff00ffffULL) == 0x0000ffff0000ffffULL);
596}
597
599 uint64_t EncVal = Imm;
600 return (EncVal << 48) | (EncVal << 16) | 0x0000ffff0000ffffULL;
601}
602
604 return (Imm & 0x00ff0000ULL) >> 16;
605}
606
607// abcdefgh abcdefgh abcdefgh abcdefgh abcdefgh abcdefgh abcdefgh abcdefgh
608static inline bool isAdvSIMDModImmType9(uint64_t Imm) {
609 return ((Imm >> 32) == (Imm & 0xffffffffULL)) &&
610 ((Imm >> 48) == (Imm & 0x0000ffffULL)) &&
611 ((Imm >> 56) == (Imm & 0x000000ffULL));
612}
613
615 return (Imm & 0xffULL);
616}
617
619 uint64_t EncVal = Imm;
620 EncVal |= (EncVal << 8);
621 EncVal |= (EncVal << 16);
622 EncVal |= (EncVal << 32);
623 return EncVal;
624}
625
626// aaaaaaaa bbbbbbbb cccccccc dddddddd eeeeeeee ffffffff gggggggg hhhhhhhh
627// cmode: 1110, op: 1
628static inline bool isAdvSIMDModImmType10(uint64_t Imm) {
629#if defined(_MSC_VER) && _MSC_VER == 1937 && !defined(__clang__) && \
630 defined(_M_ARM64)
631 // The MSVC compiler 19.37 for ARM64 has an optimization bug that
632 // causes an incorrect behavior with the original version. Work around
633 // by using a slightly different variation.
634 // https://developercommunity.visualstudio.com/t/C-ARM64-compiler-optimization-bug/10481261
635 constexpr uint64_t Mask = 0xFFULL;
636 uint64_t ByteA = (Imm >> 56) & Mask;
637 uint64_t ByteB = (Imm >> 48) & Mask;
638 uint64_t ByteC = (Imm >> 40) & Mask;
639 uint64_t ByteD = (Imm >> 32) & Mask;
640 uint64_t ByteE = (Imm >> 24) & Mask;
641 uint64_t ByteF = (Imm >> 16) & Mask;
642 uint64_t ByteG = (Imm >> 8) & Mask;
643 uint64_t ByteH = Imm & Mask;
644
645 return (ByteA == 0ULL || ByteA == Mask) && (ByteB == 0ULL || ByteB == Mask) &&
646 (ByteC == 0ULL || ByteC == Mask) && (ByteD == 0ULL || ByteD == Mask) &&
647 (ByteE == 0ULL || ByteE == Mask) && (ByteF == 0ULL || ByteF == Mask) &&
648 (ByteG == 0ULL || ByteG == Mask) && (ByteH == 0ULL || ByteH == Mask);
649#else
650 uint64_t ByteA = Imm & 0xff00000000000000ULL;
651 uint64_t ByteB = Imm & 0x00ff000000000000ULL;
652 uint64_t ByteC = Imm & 0x0000ff0000000000ULL;
653 uint64_t ByteD = Imm & 0x000000ff00000000ULL;
654 uint64_t ByteE = Imm & 0x00000000ff000000ULL;
655 uint64_t ByteF = Imm & 0x0000000000ff0000ULL;
656 uint64_t ByteG = Imm & 0x000000000000ff00ULL;
657 uint64_t ByteH = Imm & 0x00000000000000ffULL;
658
659 return (ByteA == 0ULL || ByteA == 0xff00000000000000ULL) &&
660 (ByteB == 0ULL || ByteB == 0x00ff000000000000ULL) &&
661 (ByteC == 0ULL || ByteC == 0x0000ff0000000000ULL) &&
662 (ByteD == 0ULL || ByteD == 0x000000ff00000000ULL) &&
663 (ByteE == 0ULL || ByteE == 0x00000000ff000000ULL) &&
664 (ByteF == 0ULL || ByteF == 0x0000000000ff0000ULL) &&
665 (ByteG == 0ULL || ByteG == 0x000000000000ff00ULL) &&
666 (ByteH == 0ULL || ByteH == 0x00000000000000ffULL);
667#endif
668}
669
671 uint8_t BitA = (Imm & 0xff00000000000000ULL) != 0;
672 uint8_t BitB = (Imm & 0x00ff000000000000ULL) != 0;
673 uint8_t BitC = (Imm & 0x0000ff0000000000ULL) != 0;
674 uint8_t BitD = (Imm & 0x000000ff00000000ULL) != 0;
675 uint8_t BitE = (Imm & 0x00000000ff000000ULL) != 0;
676 uint8_t BitF = (Imm & 0x0000000000ff0000ULL) != 0;
677 uint8_t BitG = (Imm & 0x000000000000ff00ULL) != 0;
678 uint8_t BitH = (Imm & 0x00000000000000ffULL) != 0;
679
680 uint8_t EncVal = BitA;
681 EncVal <<= 1;
682 EncVal |= BitB;
683 EncVal <<= 1;
684 EncVal |= BitC;
685 EncVal <<= 1;
686 EncVal |= BitD;
687 EncVal <<= 1;
688 EncVal |= BitE;
689 EncVal <<= 1;
690 EncVal |= BitF;
691 EncVal <<= 1;
692 EncVal |= BitG;
693 EncVal <<= 1;
694 EncVal |= BitH;
695 return EncVal;
696}
697
699 uint64_t EncVal = 0;
700 if (Imm & 0x80) EncVal |= 0xff00000000000000ULL;
701 if (Imm & 0x40) EncVal |= 0x00ff000000000000ULL;
702 if (Imm & 0x20) EncVal |= 0x0000ff0000000000ULL;
703 if (Imm & 0x10) EncVal |= 0x000000ff00000000ULL;
704 if (Imm & 0x08) EncVal |= 0x00000000ff000000ULL;
705 if (Imm & 0x04) EncVal |= 0x0000000000ff0000ULL;
706 if (Imm & 0x02) EncVal |= 0x000000000000ff00ULL;
707 if (Imm & 0x01) EncVal |= 0x00000000000000ffULL;
708 return EncVal;
709}
710
711// aBbbbbbc defgh000 0x00 0x00 aBbbbbbc defgh000 0x00 0x00
712static inline bool isAdvSIMDModImmType11(uint64_t Imm) {
713 uint64_t BString = (Imm & 0x7E000000ULL) >> 25;
714 return ((Imm >> 32) == (Imm & 0xffffffffULL)) &&
715 (BString == 0x1f || BString == 0x20) &&
716 ((Imm & 0x0007ffff0007ffffULL) == 0);
717}
718
720 uint8_t BitA = (Imm & 0x80000000ULL) != 0;
721 uint8_t BitB = (Imm & 0x20000000ULL) != 0;
722 uint8_t BitC = (Imm & 0x01000000ULL) != 0;
723 uint8_t BitD = (Imm & 0x00800000ULL) != 0;
724 uint8_t BitE = (Imm & 0x00400000ULL) != 0;
725 uint8_t BitF = (Imm & 0x00200000ULL) != 0;
726 uint8_t BitG = (Imm & 0x00100000ULL) != 0;
727 uint8_t BitH = (Imm & 0x00080000ULL) != 0;
728
729 uint8_t EncVal = BitA;
730 EncVal <<= 1;
731 EncVal |= BitB;
732 EncVal <<= 1;
733 EncVal |= BitC;
734 EncVal <<= 1;
735 EncVal |= BitD;
736 EncVal <<= 1;
737 EncVal |= BitE;
738 EncVal <<= 1;
739 EncVal |= BitF;
740 EncVal <<= 1;
741 EncVal |= BitG;
742 EncVal <<= 1;
743 EncVal |= BitH;
744 return EncVal;
745}
746
748 uint64_t EncVal = 0;
749 if (Imm & 0x80) EncVal |= 0x80000000ULL;
750 if (Imm & 0x40) EncVal |= 0x3e000000ULL;
751 else EncVal |= 0x40000000ULL;
752 if (Imm & 0x20) EncVal |= 0x01000000ULL;
753 if (Imm & 0x10) EncVal |= 0x00800000ULL;
754 if (Imm & 0x08) EncVal |= 0x00400000ULL;
755 if (Imm & 0x04) EncVal |= 0x00200000ULL;
756 if (Imm & 0x02) EncVal |= 0x00100000ULL;
757 if (Imm & 0x01) EncVal |= 0x00080000ULL;
758 return (EncVal << 32) | EncVal;
759}
760
761// aBbbbbbb bbcdefgh 0x00 0x00 0x00 0x00 0x00 0x00
762static inline bool isAdvSIMDModImmType12(uint64_t Imm) {
763 uint64_t BString = (Imm & 0x7fc0000000000000ULL) >> 54;
764 return ((BString == 0xff || BString == 0x100) &&
765 ((Imm & 0x0000ffffffffffffULL) == 0));
766}
767
769 uint8_t BitA = (Imm & 0x8000000000000000ULL) != 0;
770 uint8_t BitB = (Imm & 0x0040000000000000ULL) != 0;
771 uint8_t BitC = (Imm & 0x0020000000000000ULL) != 0;
772 uint8_t BitD = (Imm & 0x0010000000000000ULL) != 0;
773 uint8_t BitE = (Imm & 0x0008000000000000ULL) != 0;
774 uint8_t BitF = (Imm & 0x0004000000000000ULL) != 0;
775 uint8_t BitG = (Imm & 0x0002000000000000ULL) != 0;
776 uint8_t BitH = (Imm & 0x0001000000000000ULL) != 0;
777
778 uint8_t EncVal = BitA;
779 EncVal <<= 1;
780 EncVal |= BitB;
781 EncVal <<= 1;
782 EncVal |= BitC;
783 EncVal <<= 1;
784 EncVal |= BitD;
785 EncVal <<= 1;
786 EncVal |= BitE;
787 EncVal <<= 1;
788 EncVal |= BitF;
789 EncVal <<= 1;
790 EncVal |= BitG;
791 EncVal <<= 1;
792 EncVal |= BitH;
793 return EncVal;
794}
795
797 uint64_t EncVal = 0;
798 if (Imm & 0x80) EncVal |= 0x8000000000000000ULL;
799 if (Imm & 0x40) EncVal |= 0x3fc0000000000000ULL;
800 else EncVal |= 0x4000000000000000ULL;
801 if (Imm & 0x20) EncVal |= 0x0020000000000000ULL;
802 if (Imm & 0x10) EncVal |= 0x0010000000000000ULL;
803 if (Imm & 0x08) EncVal |= 0x0008000000000000ULL;
804 if (Imm & 0x04) EncVal |= 0x0004000000000000ULL;
805 if (Imm & 0x02) EncVal |= 0x0002000000000000ULL;
806 if (Imm & 0x01) EncVal |= 0x0001000000000000ULL;
807 return EncVal;
808}
809
810/// Returns true if Imm is the concatenation of a repeating pattern of type T.
811template <typename T>
812static inline bool isSVEMaskOfIdenticalElements(int64_t Imm) {
813 auto Parts = bit_cast<std::array<T, sizeof(int64_t) / sizeof(T)>>(Imm);
814 return llvm::all_equal(Parts);
815}
816
817/// Returns true if Imm is valid for CPY/DUP.
818template <typename T>
819static inline bool isSVECpyImm(int64_t Imm) {
820 // Imm is interpreted as a signed value, which means top bits must be all ones
821 // (sign bits if the immediate value is negative and passed in a larger
822 // container), or all zeroes.
823 int64_t Mask = ~int64_t(std::numeric_limits<std::make_unsigned_t<T>>::max());
824 if ((Imm & Mask) != 0 && (Imm & Mask) != Mask)
825 return false;
826
827 // Imm is a signed 8-bit value.
828 // Top bits must be zeroes or sign bits.
829 if (Imm & 0xff)
830 return int8_t(Imm) == T(Imm);
831
832 // Imm is a signed 16-bit value and multiple of 256.
833 // Top bits must be zeroes or sign bits.
834 if (Imm & 0xff00)
835 return int16_t(Imm) == T(Imm);
836
837 return Imm == 0;
838}
839
840/// Returns true if Imm is valid for ADD/SUB.
841template <typename T>
842static inline bool isSVEAddSubImm(int64_t Imm) {
843 bool IsInt8t = std::is_same<int8_t, std::make_signed_t<T>>::value ||
844 std::is_same<int8_t, T>::value;
845 return uint8_t(Imm) == Imm || (!IsInt8t && uint16_t(Imm & ~0xff) == Imm);
846}
847
848/// Return true if Imm is valid for DUPM and has no single CPY/DUP equivalent.
849static inline bool isSVEMoveMaskPreferredLogicalImmediate(int64_t Imm) {
850 if (isSVECpyImm<int64_t>(Imm))
851 return false;
852
853 auto S = bit_cast<std::array<int32_t, 2>>(Imm);
856
858 return false;
860 return false;
862 return false;
863 return isLogicalImmediate(Imm, 64);
864}
865
866inline static bool isAnyMOVZMovAlias(uint64_t Value, int RegWidth) {
867 for (int Shift = 0; Shift <= RegWidth - 16; Shift += 16)
868 if ((Value & ~(0xffffULL << Shift)) == 0)
869 return true;
870
871 return false;
872}
873
874inline static bool isMOVZMovAlias(uint64_t Value, int Shift, int RegWidth) {
875 if (RegWidth == 32)
876 Value &= 0xffffffffULL;
877
878 // "lsl #0" takes precedence: in practice this only affects "#0, lsl #0".
879 if (Value == 0 && Shift != 0)
880 return false;
881
882 return (Value & ~(0xffffULL << Shift)) == 0;
883}
884
885inline static bool isMOVNMovAlias(uint64_t Value, int Shift, int RegWidth) {
886 // MOVZ takes precedence over MOVN.
887 if (isAnyMOVZMovAlias(Value, RegWidth))
888 return false;
889
890 Value = ~Value;
891 if (RegWidth == 32)
892 Value &= 0xffffffffULL;
893
894 return isMOVZMovAlias(Value, Shift, RegWidth);
895}
896
897inline static bool isAnyMOVWMovAlias(uint64_t Value, int RegWidth) {
898 if (isAnyMOVZMovAlias(Value, RegWidth))
899 return true;
900
901 // It's not a MOVZ, but it might be a MOVN.
902 Value = ~Value;
903 if (RegWidth == 32)
904 Value &= 0xffffffffULL;
905
906 return isAnyMOVZMovAlias(Value, RegWidth);
907}
908
909static inline bool isSVECpyDupImm(int SizeInBits, int64_t Val, int32_t &Imm,
910 int32_t &Shift) {
911 switch (SizeInBits) {
912 case 8:
913 // All immediates are supported.
914 Shift = 0;
915 Imm = Val & 0xFF;
916 return true;
917 case 16:
918 case 32:
919 case 64:
920 // Support 8bit signed immediates.
921 if (Val >= -128 && Val <= 127) {
922 Shift = 0;
923 Imm = Val & 0xFF;
924 return true;
925 }
926 // Support 16bit signed immediates that are a multiple of 256.
927 if (Val >= -32768 && Val <= 32512 && Val % 256 == 0) {
928 Shift = 8;
929 Imm = (Val >> 8) & 0xFF;
930 return true;
931 }
932 break;
933 default:
934 break;
935 }
936 return false;
937}
938
939static inline bool isSVELogicalImm(unsigned SizeInBits, uint64_t ImmVal,
940 uint64_t &Encoding) {
941 // Shift mask depending on type size.
942 switch (SizeInBits) {
943 case 8:
944 ImmVal &= 0xFF;
945 ImmVal |= ImmVal << 8;
946 ImmVal |= ImmVal << 16;
947 ImmVal |= ImmVal << 32;
948 break;
949 case 16:
950 ImmVal &= 0xFFFF;
951 ImmVal |= ImmVal << 16;
952 ImmVal |= ImmVal << 32;
953 break;
954 case 32:
955 ImmVal &= 0xFFFFFFFF;
956 ImmVal |= ImmVal << 32;
957 break;
958 case 64:
959 break;
960 default:
961 llvm_unreachable("Unexpected size");
962 }
963
964 return processLogicalImmediate(ImmVal, 64, Encoding);
965}
966
967} // end namespace AArch64_AM
968
969} // end namespace llvm
970
971#endif
unsigned RegSize
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
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< 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:1436
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 getMemExtendImm(AArch64_AM::ShiftExtendType ET, bool DoShift)
getExtendImm - Encode the extend type and amount for a load/store inst: doshift: should the offset be...
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 AArch64_AM::ShiftExtendType getMemExtendType(unsigned Imm)
getExtendType - Extract the extend type for the offset operand of loads/stores.
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 bool getMemDoShift(unsigned Imm)
getMemDoShift - Extract the "do shift" flag value for load/store instructions.
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)
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
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:1668
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:273
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:2165
#define N