Fixing warnings in the MSVC build. No functional changes intended.
[oota-llvm.git] / lib / Target / ARM64 / MCTargetDesc / ARM64AddressingModes.h
1 //===- ARM64AddressingModes.h - ARM64 Addressing Modes ----------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the ARM64 addressing mode implementation stuff.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_TARGET_ARM64_ARM64ADDRESSINGMODES_H
15 #define LLVM_TARGET_ARM64_ARM64ADDRESSINGMODES_H
16
17 #include "llvm/ADT/APFloat.h"
18 #include "llvm/ADT/APInt.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/MathExtras.h"
21 #include <cassert>
22
23 namespace llvm {
24
25 /// ARM64_AM - ARM64 Addressing Mode Stuff
26 namespace ARM64_AM {
27
28 //===----------------------------------------------------------------------===//
29 // Shifts
30 //
31
32 enum ShiftType {
33   InvalidShift = -1,
34   LSL = 0,
35   LSR = 1,
36   ASR = 2,
37   ROR = 3,
38   MSL = 4
39 };
40
41 /// getShiftName - Get the string encoding for the shift type.
42 static inline const char *getShiftName(ARM64_AM::ShiftType ST) {
43   switch (ST) {
44   default: assert(false && "unhandled shift type!");
45   case ARM64_AM::LSL: return "lsl";
46   case ARM64_AM::LSR: return "lsr";
47   case ARM64_AM::ASR: return "asr";
48   case ARM64_AM::ROR: return "ror";
49   case ARM64_AM::MSL: return "msl";
50   }
51   return 0;
52 }
53
54 /// getShiftType - Extract the shift type.
55 static inline ARM64_AM::ShiftType getShiftType(unsigned Imm) {
56   return ARM64_AM::ShiftType((Imm >> 6) & 0x7);
57 }
58
59 /// getShiftValue - Extract the shift value.
60 static inline unsigned getShiftValue(unsigned Imm) {
61   return Imm & 0x3f;
62 }
63
64 /// getShifterImm - Encode the shift type and amount:
65 ///   imm:     6-bit shift amount
66 ///   shifter: 000 ==> lsl
67 ///            001 ==> lsr
68 ///            010 ==> asr
69 ///            011 ==> ror
70 ///            100 ==> msl
71 ///   {8-6}  = shifter
72 ///   {5-0}  = imm
73 static inline unsigned getShifterImm(ARM64_AM::ShiftType ST, unsigned Imm) {
74   assert((Imm & 0x3f) == Imm && "Illegal shifted immedate value!");
75   return (unsigned(ST) << 6) | (Imm & 0x3f);
76 }
77
78 //===----------------------------------------------------------------------===//
79 // Extends
80 //
81
82 enum ExtendType {
83   InvalidExtend = -1,
84   UXTB = 0,
85   UXTH = 1,
86   UXTW = 2,
87   UXTX = 3,
88   SXTB = 4,
89   SXTH = 5,
90   SXTW = 6,
91   SXTX = 7
92 };
93
94 /// getExtendName - Get the string encoding for the extend type.
95 static inline const char *getExtendName(ARM64_AM::ExtendType ET) {
96   switch (ET) {
97   default: assert(false && "unhandled extend type!");
98   case ARM64_AM::UXTB: return "uxtb";
99   case ARM64_AM::UXTH: return "uxth";
100   case ARM64_AM::UXTW: return "uxtw";
101   case ARM64_AM::UXTX: return "uxtx";
102   case ARM64_AM::SXTB: return "sxtb";
103   case ARM64_AM::SXTH: return "sxth";
104   case ARM64_AM::SXTW: return "sxtw";
105   case ARM64_AM::SXTX: return "sxtx";
106   }
107   return 0;
108 }
109
110 /// getArithShiftValue - get the arithmetic shift value.
111 static inline unsigned getArithShiftValue(unsigned Imm) {
112   return Imm & 0x7;
113 }
114
115 /// getExtendType - Extract the extend type for operands of arithmetic ops.
116 static inline ARM64_AM::ExtendType getArithExtendType(unsigned Imm) {
117   return ARM64_AM::ExtendType((Imm >> 3) & 0x7);
118 }
119
120 /// getArithExtendImm - Encode the extend type and shift amount for an
121 ///                     arithmetic instruction:
122 ///   imm:     3-bit extend amount
123 ///   shifter: 000 ==> uxtb
124 ///            001 ==> uxth
125 ///            010 ==> uxtw
126 ///            011 ==> uxtx
127 ///            100 ==> sxtb
128 ///            101 ==> sxth
129 ///            110 ==> sxtw
130 ///            111 ==> sxtx
131 ///   {5-3}  = shifter
132 ///   {2-0}  = imm3
133 static inline unsigned getArithExtendImm(ARM64_AM::ExtendType ET,
134                                          unsigned Imm) {
135   assert((Imm & 0x7) == Imm && "Illegal shifted immedate value!");
136   return (unsigned(ET) << 3) | (Imm & 0x7);
137 }
138
139 /// getMemDoShift - Extract the "do shift" flag value for load/store
140 /// instructions.
141 static inline bool getMemDoShift(unsigned Imm) {
142   return (Imm & 0x1) != 0;
143 }
144
145 /// getExtendType - Extract the extend type for the offset operand of
146 /// loads/stores.
147 static inline ARM64_AM::ExtendType getMemExtendType(unsigned Imm) {
148   return ARM64_AM::ExtendType((Imm >> 1) & 0x7);
149 }
150
151 /// getExtendImm - Encode the extend type and amount for a load/store inst:
152 ///   doshift:     should the offset be scaled by the access size
153 ///   shifter: 000 ==> uxtb
154 ///            001 ==> uxth
155 ///            010 ==> uxtw
156 ///            011 ==> uxtx
157 ///            100 ==> sxtb
158 ///            101 ==> sxth
159 ///            110 ==> sxtw
160 ///            111 ==> sxtx
161 ///   {3-1}  = shifter
162 ///   {0}  = doshift
163 static inline unsigned getMemExtendImm(ARM64_AM::ExtendType ET, bool DoShift) {
164   return (unsigned(ET) << 1) | unsigned(DoShift);
165 }
166
167 //===----------------------------------------------------------------------===//
168 // Prefetch
169 //
170
171 /// Pre-fetch operator names.
172 /// The enum values match the encoding values:
173 ///   prfop<4:3> 00=preload data, 10=prepare for store
174 ///   prfop<2:1> 00=target L1 cache, 01=target L2 cache, 10=target L3 cache,
175 ///   prfop<0> 0=non-streaming (temporal), 1=streaming (non-temporal)
176 enum PrefetchOp {
177   InvalidPrefetchOp = -1,
178   PLDL1KEEP = 0x00,
179   PLDL1STRM = 0x01,
180   PLDL2KEEP = 0x02,
181   PLDL2STRM = 0x03,
182   PLDL3KEEP = 0x04,
183   PLDL3STRM = 0x05,
184   PSTL1KEEP = 0x10,
185   PSTL1STRM = 0x11,
186   PSTL2KEEP = 0x12,
187   PSTL2STRM = 0x13,
188   PSTL3KEEP = 0x14,
189   PSTL3STRM = 0x15
190 };
191
192 /// isNamedPrefetchOp - Check if the prefetch-op 5-bit value has a name.
193 static inline bool isNamedPrefetchOp(unsigned prfop) {
194   switch (prfop) {
195   default: return false;
196   case ARM64_AM::PLDL1KEEP: case ARM64_AM::PLDL1STRM: case ARM64_AM::PLDL2KEEP:
197   case ARM64_AM::PLDL2STRM: case ARM64_AM::PLDL3KEEP: case ARM64_AM::PLDL3STRM:
198   case ARM64_AM::PSTL1KEEP: case ARM64_AM::PSTL1STRM: case ARM64_AM::PSTL2KEEP:
199   case ARM64_AM::PSTL2STRM: case ARM64_AM::PSTL3KEEP: case ARM64_AM::PSTL3STRM:
200     return true;
201   }
202 }
203
204
205 /// getPrefetchOpName - Get the string encoding for the prefetch operator.
206 static inline const char *getPrefetchOpName(ARM64_AM::PrefetchOp prfop) {
207   switch (prfop) {
208   default: assert(false && "unhandled prefetch-op type!");
209   case ARM64_AM::PLDL1KEEP: return "pldl1keep";
210   case ARM64_AM::PLDL1STRM: return "pldl1strm";
211   case ARM64_AM::PLDL2KEEP: return "pldl2keep";
212   case ARM64_AM::PLDL2STRM: return "pldl2strm";
213   case ARM64_AM::PLDL3KEEP: return "pldl3keep";
214   case ARM64_AM::PLDL3STRM: return "pldl3strm";
215   case ARM64_AM::PSTL1KEEP: return "pstl1keep";
216   case ARM64_AM::PSTL1STRM: return "pstl1strm";
217   case ARM64_AM::PSTL2KEEP: return "pstl2keep";
218   case ARM64_AM::PSTL2STRM: return "pstl2strm";
219   case ARM64_AM::PSTL3KEEP: return "pstl3keep";
220   case ARM64_AM::PSTL3STRM: return "pstl3strm";
221   }
222   return 0;
223 }
224
225 static inline uint64_t ror(uint64_t elt, unsigned size) {
226   return ((elt & 1) << (size-1)) | (elt >> 1);
227 }
228
229 /// processLogicalImmediate - Determine if an immediate value can be encoded
230 /// as the immediate operand of a logical instruction for the given register
231 /// size.  If so, return true with "encoding" set to the encoded value in
232 /// the form N:immr:imms.
233 static inline bool processLogicalImmediate(uint64_t imm, unsigned regSize,
234                                            uint64_t &encoding) {
235   if (imm == 0ULL || imm == ~0ULL ||
236       (regSize != 64 && (imm >> regSize != 0 || imm == ~0U)))
237     return false;
238
239   unsigned size = 2;
240   uint64_t eltVal = imm;
241
242   // First, determine the element size.
243   while (size < regSize) {
244     unsigned numElts = regSize / size;
245     unsigned mask = (1ULL << size) - 1;
246     uint64_t lowestEltVal = imm & mask;
247
248     bool allMatched = true;
249     for (unsigned i = 1; i < numElts; ++i) {
250      uint64_t currEltVal = (imm >> (i*size)) & mask;
251       if (currEltVal != lowestEltVal) {
252         allMatched = false;
253         break;
254       }
255     }
256
257     if (allMatched) {
258       eltVal = lowestEltVal;
259       break;
260     }
261
262     size *= 2;
263   }
264
265   // Second, determine the rotation to make the element be: 0^m 1^n.
266   for (unsigned i = 0; i < size; ++i) {
267     eltVal = ror(eltVal, size);
268     uint32_t clz = countLeadingZeros(eltVal) - (64 - size);
269     uint32_t cto = CountTrailingOnes_64(eltVal);
270
271     if (clz + cto == size) {
272       // Encode in immr the number of RORs it would take to get *from* this
273       // element value to our target value, where i+1 is the number of RORs
274       // to go the opposite direction.
275       unsigned immr = size - (i + 1);
276
277       // If size has a 1 in the n'th bit, create a value that has zeroes in
278       // bits [0, n] and ones above that.
279       uint64_t nimms = ~(size-1) << 1;
280
281       // Or the CTO value into the low bits, which must be below the Nth bit
282       // bit mentioned above.
283       nimms |= (cto-1);
284
285       // Extract the seventh bit and toggle it to create the N field.
286       unsigned N = ((nimms >> 6) & 1) ^ 1;
287
288       encoding = (N << 12) | (immr << 6) | (nimms & 0x3f);
289       return true;
290     }
291   }
292
293   return false;
294 }
295
296 /// isLogicalImmediate - Return true if the immediate is valid for a logical
297 /// immediate instruction of the given register size. Return false otherwise.
298 static inline bool isLogicalImmediate(uint64_t imm, unsigned regSize) {
299   uint64_t encoding;
300   return processLogicalImmediate(imm, regSize, encoding);
301 }
302
303 /// encodeLogicalImmediate - Return the encoded immediate value for a logical
304 /// immediate instruction of the given register size.
305 static inline uint64_t encodeLogicalImmediate(uint64_t imm, unsigned regSize) {
306   uint64_t encoding = 0;
307   bool res = processLogicalImmediate(imm, regSize, encoding);
308   assert(res && "invalid logical immediate");
309   (void)res;
310   return encoding;
311 }
312
313 /// decodeLogicalImmediate - Decode a logical immediate value in the form
314 /// "N:immr:imms" (where the immr and imms fields are each 6 bits) into the
315 /// integer value it represents with regSize bits.
316 static inline uint64_t decodeLogicalImmediate(uint64_t val, unsigned regSize) {
317   // Extract the N, imms, and immr fields.
318   unsigned N = (val >> 12) & 1;
319   unsigned immr = (val >> 6) & 0x3f;
320   unsigned imms = val & 0x3f;
321
322   assert((regSize == 64 || N == 0) && "undefined logical immediate encoding");
323   int len = 31 - countLeadingZeros((N << 6) | (~imms & 0x3f));
324   assert(len >= 0 && "undefined logical immediate encoding");
325   unsigned size = (1 << len);
326   unsigned R = immr & (size - 1);
327   unsigned S = imms & (size - 1);
328   assert(S != size - 1 && "undefined logical immediate encoding");
329   uint64_t pattern = (1ULL << (S + 1)) - 1;
330   for (unsigned i = 0; i < R; ++i)
331     pattern = ror(pattern, size);
332
333   // Replicate the pattern to fill the regSize.
334   while (size != regSize) {
335     pattern |= (pattern << size);
336     size *= 2;
337   }
338   return pattern;
339 }
340
341 /// isValidDecodeLogicalImmediate - Check to see if the logical immediate value
342 /// in the form "N:immr:imms" (where the immr and imms fields are each 6 bits)
343 /// is a valid encoding for an integer value with regSize bits.
344 static inline bool isValidDecodeLogicalImmediate(uint64_t val,
345                                                  unsigned regSize) {
346   // Extract the N and imms fields needed for checking.
347   unsigned N = (val >> 12) & 1;
348   unsigned imms = val & 0x3f;
349
350   if (regSize == 32 && N != 0) // undefined logical immediate encoding
351     return false;
352   int len = 31 - countLeadingZeros((N << 6) | (~imms & 0x3f));
353   if (len < 0) // undefined logical immediate encoding
354     return false;
355   unsigned size = (1 << len);
356   unsigned S = imms & (size - 1);
357   if (S == size - 1) // undefined logical immediate encoding
358     return false;
359
360   return true;
361 }
362
363 //===----------------------------------------------------------------------===//
364 // Floating-point Immediates
365 //
366 static inline float getFPImmFloat(unsigned Imm) {
367   // We expect an 8-bit binary encoding of a floating-point number here.
368   union {
369     uint32_t I;
370     float F;
371   } FPUnion;
372
373   uint8_t Sign = (Imm >> 7) & 0x1;
374   uint8_t Exp = (Imm >> 4) & 0x7;
375   uint8_t Mantissa = Imm & 0xf;
376
377   //   8-bit FP    iEEEE Float Encoding
378   //   abcd efgh   aBbbbbbc defgh000 00000000 00000000
379   //
380   // where B = NOT(b);
381
382   FPUnion.I = 0;
383   FPUnion.I |= Sign << 31;
384   FPUnion.I |= ((Exp & 0x4) != 0 ? 0 : 1) << 30;
385   FPUnion.I |= ((Exp & 0x4) != 0 ? 0x1f : 0) << 25;
386   FPUnion.I |= (Exp & 0x3) << 23;
387   FPUnion.I |= Mantissa << 19;
388   return FPUnion.F;
389 }
390
391 /// getFP32Imm - Return an 8-bit floating-point version of the 32-bit
392 /// floating-point value. If the value cannot be represented as an 8-bit
393 /// floating-point value, then return -1.
394 static inline int getFP32Imm(const APInt &Imm) {
395   uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
396   int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127;  // -126 to 127
397   int64_t Mantissa = Imm.getZExtValue() & 0x7fffff;  // 23 bits
398
399   // We can handle 4 bits of mantissa.
400   // mantissa = (16+UInt(e:f:g:h))/16.
401   if (Mantissa & 0x7ffff)
402     return -1;
403   Mantissa >>= 19;
404   if ((Mantissa & 0xf) != Mantissa)
405     return -1;
406
407   // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
408   if (Exp < -3 || Exp > 4)
409     return -1;
410   Exp = ((Exp+3) & 0x7) ^ 4;
411
412   return ((int)Sign << 7) | (Exp << 4) | Mantissa;
413 }
414
415 static inline int getFP32Imm(const APFloat &FPImm) {
416   return getFP32Imm(FPImm.bitcastToAPInt());
417 }
418
419 /// getFP64Imm - Return an 8-bit floating-point version of the 64-bit
420 /// floating-point value. If the value cannot be represented as an 8-bit
421 /// floating-point value, then return -1.
422 static inline int getFP64Imm(const APInt &Imm) {
423   uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
424   int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023;   // -1022 to 1023
425   uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffULL;
426
427   // We can handle 4 bits of mantissa.
428   // mantissa = (16+UInt(e:f:g:h))/16.
429   if (Mantissa & 0xffffffffffffULL)
430     return -1;
431   Mantissa >>= 48;
432   if ((Mantissa & 0xf) != Mantissa)
433     return -1;
434
435   // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
436   if (Exp < -3 || Exp > 4)
437     return -1;
438   Exp = ((Exp+3) & 0x7) ^ 4;
439
440   return ((int)Sign << 7) | (Exp << 4) | Mantissa;
441 }
442
443 static inline int getFP64Imm(const APFloat &FPImm) {
444   return getFP64Imm(FPImm.bitcastToAPInt());
445 }
446
447 //===--------------------------------------------------------------------===//
448 // AdvSIMD Modified Immediates
449 //===--------------------------------------------------------------------===//
450
451 // 0x00 0x00 0x00 abcdefgh 0x00 0x00 0x00 abcdefgh
452 static inline bool isAdvSIMDModImmType1(uint64_t Imm) {
453   return ((Imm >> 32) == (Imm & 0xffffffffULL)) &&
454          ((Imm & 0xffffff00ffffff00ULL) == 0);
455 }
456
457 static inline uint8_t encodeAdvSIMDModImmType1(uint64_t Imm) {
458   return (Imm & 0xffULL);
459 }
460
461 static inline uint64_t decodeAdvSIMDModImmType1(uint8_t Imm) {
462   uint64_t EncVal = Imm;
463   return (EncVal << 32) | EncVal;
464 }
465
466 // 0x00 0x00 abcdefgh 0x00 0x00 0x00 abcdefgh 0x00
467 static inline bool isAdvSIMDModImmType2(uint64_t Imm) {
468   return ((Imm >> 32) == (Imm & 0xffffffffULL)) &&
469          ((Imm & 0xffff00ffffff00ffULL) == 0);
470 }
471
472 static inline uint8_t encodeAdvSIMDModImmType2(uint64_t Imm) {
473   return (Imm & 0xff00ULL) >> 8;
474 }
475
476 static inline uint64_t decodeAdvSIMDModImmType2(uint8_t Imm) {
477   uint64_t EncVal = Imm;
478   return (EncVal << 40) | (EncVal << 8);
479 }
480
481 // 0x00 abcdefgh 0x00 0x00 0x00 abcdefgh 0x00 0x00
482 static inline bool isAdvSIMDModImmType3(uint64_t Imm) {
483   return ((Imm >> 32) == (Imm & 0xffffffffULL)) &&
484          ((Imm & 0xff00ffffff00ffffULL) == 0);
485 }
486
487 static inline uint8_t encodeAdvSIMDModImmType3(uint64_t Imm) {
488   return (Imm & 0xff0000ULL) >> 16;
489 }
490
491 static inline uint64_t decodeAdvSIMDModImmType3(uint8_t Imm) {
492   uint64_t EncVal = Imm;
493   return (EncVal << 48) | (EncVal << 16);
494 }
495
496 // abcdefgh 0x00 0x00 0x00 abcdefgh 0x00 0x00 0x00
497 static inline bool isAdvSIMDModImmType4(uint64_t Imm) {
498   return ((Imm >> 32) == (Imm & 0xffffffffULL)) &&
499          ((Imm & 0x00ffffff00ffffffULL) == 0);
500 }
501
502 static inline uint8_t encodeAdvSIMDModImmType4(uint64_t Imm) {
503   return (Imm & 0xff000000ULL) >> 24;
504 }
505
506 static inline uint64_t decodeAdvSIMDModImmType4(uint8_t Imm) {
507   uint64_t EncVal = Imm;
508   return (EncVal << 56) | (EncVal << 24);
509 }
510
511 // 0x00 abcdefgh 0x00 abcdefgh 0x00 abcdefgh 0x00 abcdefgh
512 static inline bool isAdvSIMDModImmType5(uint64_t Imm) {
513   return ((Imm >> 32) == (Imm & 0xffffffffULL)) &&
514          (((Imm & 0x00ff0000ULL) >> 16) == (Imm & 0x000000ffULL)) &&
515          ((Imm & 0xff00ff00ff00ff00ULL) == 0);
516 }
517
518 static inline uint8_t encodeAdvSIMDModImmType5(uint64_t Imm) {
519   return (Imm & 0xffULL);
520 }
521
522 static inline uint64_t decodeAdvSIMDModImmType5(uint8_t Imm) {
523   uint64_t EncVal = Imm;
524   return (EncVal << 48) | (EncVal << 32) | (EncVal << 16) | EncVal;
525 }
526
527 // abcdefgh 0x00 abcdefgh 0x00 abcdefgh 0x00 abcdefgh 0x00
528 static inline bool isAdvSIMDModImmType6(uint64_t Imm) {
529   return ((Imm >> 32) == (Imm & 0xffffffffULL)) &&
530          (((Imm & 0xff000000ULL) >> 16) == (Imm & 0x0000ff00ULL)) &&
531          ((Imm & 0x00ff00ff00ff00ffULL) == 0);
532 }
533
534 static inline uint8_t encodeAdvSIMDModImmType6(uint64_t Imm) {
535   return (Imm & 0xff00ULL) >> 8;
536 }
537
538 static inline uint64_t decodeAdvSIMDModImmType6(uint8_t Imm) {
539   uint64_t EncVal = Imm;
540   return (EncVal << 56) | (EncVal << 40) | (EncVal << 24) | (EncVal << 8);
541 }
542
543 // 0x00 0x00 abcdefgh 0xFF 0x00 0x00 abcdefgh 0xFF
544 static inline bool isAdvSIMDModImmType7(uint64_t Imm) {
545   return ((Imm >> 32) == (Imm & 0xffffffffULL)) &&
546          ((Imm & 0xffff00ffffff00ffULL) == 0x000000ff000000ffULL);
547 }
548
549 static inline uint8_t encodeAdvSIMDModImmType7(uint64_t Imm) {
550   return (Imm & 0xff00ULL) >> 8;
551 }
552
553 static inline uint64_t decodeAdvSIMDModImmType7(uint8_t Imm) {
554   uint64_t EncVal = Imm;
555   return (EncVal << 40) | (EncVal << 8) | 0x000000ff000000ffULL;
556 }
557
558 // 0x00 abcdefgh 0xFF 0xFF 0x00 abcdefgh 0xFF 0xFF
559 static inline bool isAdvSIMDModImmType8(uint64_t Imm) {
560   return ((Imm >> 32) == (Imm & 0xffffffffULL)) &&
561          ((Imm & 0xff00ffffff00ffffULL) == 0x0000ffff0000ffffULL);
562 }
563
564 static inline uint64_t decodeAdvSIMDModImmType8(uint8_t Imm) {
565   uint64_t EncVal = Imm;
566   return (EncVal << 48) | (EncVal << 16) | 0x0000ffff0000ffffULL;
567 }
568
569 static inline uint8_t encodeAdvSIMDModImmType8(uint64_t Imm) {
570   return (Imm & 0x00ff0000ULL) >> 16;
571 }
572
573 // abcdefgh abcdefgh abcdefgh abcdefgh abcdefgh abcdefgh abcdefgh abcdefgh
574 static inline bool isAdvSIMDModImmType9(uint64_t Imm) {
575   return ((Imm >> 32) == (Imm & 0xffffffffULL)) &&
576          ((Imm >> 48) == (Imm & 0x0000ffffULL)) &&
577          ((Imm >> 56) == (Imm & 0x000000ffULL));
578 }
579
580 static inline uint8_t encodeAdvSIMDModImmType9(uint64_t Imm) {
581   return (Imm & 0xffULL);
582 }
583
584 static inline uint64_t decodeAdvSIMDModImmType9(uint8_t Imm) {
585   uint64_t EncVal = Imm;
586   EncVal |= (EncVal << 8);
587   EncVal |= (EncVal << 16);
588   EncVal |= (EncVal << 32);
589   return EncVal;
590 }
591
592 // aaaaaaaa bbbbbbbb cccccccc dddddddd eeeeeeee ffffffff gggggggg hhhhhhhh
593 // cmode: 1110, op: 1
594 static inline bool isAdvSIMDModImmType10(uint64_t Imm) {
595   uint64_t ByteA = Imm & 0xff00000000000000ULL;
596   uint64_t ByteB = Imm & 0x00ff000000000000ULL;
597   uint64_t ByteC = Imm & 0x0000ff0000000000ULL;
598   uint64_t ByteD = Imm & 0x000000ff00000000ULL;
599   uint64_t ByteE = Imm & 0x00000000ff000000ULL;
600   uint64_t ByteF = Imm & 0x0000000000ff0000ULL;
601   uint64_t ByteG = Imm & 0x000000000000ff00ULL;
602   uint64_t ByteH = Imm & 0x00000000000000ffULL;
603
604   return (ByteA == 0ULL || ByteA == 0xff00000000000000ULL) &&
605          (ByteB == 0ULL || ByteB == 0x00ff000000000000ULL) &&
606          (ByteC == 0ULL || ByteC == 0x0000ff0000000000ULL) &&
607          (ByteD == 0ULL || ByteD == 0x000000ff00000000ULL) &&
608          (ByteE == 0ULL || ByteE == 0x00000000ff000000ULL) &&
609          (ByteF == 0ULL || ByteF == 0x0000000000ff0000ULL) &&
610          (ByteG == 0ULL || ByteG == 0x000000000000ff00ULL) &&
611          (ByteH == 0ULL || ByteH == 0x00000000000000ffULL);
612 }
613
614 static inline uint8_t encodeAdvSIMDModImmType10(uint64_t Imm) {
615   uint8_t BitA = (Imm & 0xff00000000000000ULL) != 0;
616   uint8_t BitB = (Imm & 0x00ff000000000000ULL) != 0;
617   uint8_t BitC = (Imm & 0x0000ff0000000000ULL) != 0;
618   uint8_t BitD = (Imm & 0x000000ff00000000ULL) != 0;
619   uint8_t BitE = (Imm & 0x00000000ff000000ULL) != 0;
620   uint8_t BitF = (Imm & 0x0000000000ff0000ULL) != 0;
621   uint8_t BitG = (Imm & 0x000000000000ff00ULL) != 0;
622   uint8_t BitH = (Imm & 0x00000000000000ffULL) != 0;
623
624   uint8_t EncVal = BitA;
625   EncVal <<= 1;
626   EncVal |= BitB;
627   EncVal <<= 1;
628   EncVal |= BitC;
629   EncVal <<= 1;
630   EncVal |= BitD;
631   EncVal <<= 1;
632   EncVal |= BitE;
633   EncVal <<= 1;
634   EncVal |= BitF;
635   EncVal <<= 1;
636   EncVal |= BitG;
637   EncVal <<= 1;
638   EncVal |= BitH;
639   return EncVal;
640 }
641
642 static inline uint64_t decodeAdvSIMDModImmType10(uint8_t Imm) {
643   uint64_t EncVal = 0;
644   if (Imm & 0x80) EncVal |= 0xff00000000000000ULL;
645   if (Imm & 0x40) EncVal |= 0x00ff000000000000ULL;
646   if (Imm & 0x20) EncVal |= 0x0000ff0000000000ULL;
647   if (Imm & 0x10) EncVal |= 0x000000ff00000000ULL;
648   if (Imm & 0x08) EncVal |= 0x00000000ff000000ULL;
649   if (Imm & 0x04) EncVal |= 0x0000000000ff0000ULL;
650   if (Imm & 0x02) EncVal |= 0x000000000000ff00ULL;
651   if (Imm & 0x01) EncVal |= 0x00000000000000ffULL;
652   return EncVal;
653 }
654
655 // aBbbbbbc defgh000 0x00 0x00 aBbbbbbc defgh000 0x00 0x00
656 static inline bool isAdvSIMDModImmType11(uint64_t Imm) {
657   uint64_t BString = (Imm & 0x7E000000ULL) >> 25;
658   return ((Imm >> 32) == (Imm & 0xffffffffULL)) &&
659          (BString == 0x1f || BString == 0x20) &&
660          ((Imm & 0x0007ffff0007ffffULL) == 0);
661 }
662
663 static inline uint8_t encodeAdvSIMDModImmType11(uint64_t Imm) {
664   uint8_t BitA = (Imm & 0x80000000ULL) != 0;
665   uint8_t BitB = (Imm & 0x20000000ULL) != 0;
666   uint8_t BitC = (Imm & 0x01000000ULL) != 0;
667   uint8_t BitD = (Imm & 0x00800000ULL) != 0;
668   uint8_t BitE = (Imm & 0x00400000ULL) != 0;
669   uint8_t BitF = (Imm & 0x00200000ULL) != 0;
670   uint8_t BitG = (Imm & 0x00100000ULL) != 0;
671   uint8_t BitH = (Imm & 0x00080000ULL) != 0;
672
673   uint8_t EncVal = BitA;
674   EncVal <<= 1;
675   EncVal |= BitB;
676   EncVal <<= 1;
677   EncVal |= BitC;
678   EncVal <<= 1;
679   EncVal |= BitD;
680   EncVal <<= 1;
681   EncVal |= BitE;
682   EncVal <<= 1;
683   EncVal |= BitF;
684   EncVal <<= 1;
685   EncVal |= BitG;
686   EncVal <<= 1;
687   EncVal |= BitH;
688   return EncVal;
689 }
690
691 static inline uint64_t decodeAdvSIMDModImmType11(uint8_t Imm) {
692   uint64_t EncVal = 0;
693   if (Imm & 0x80) EncVal |= 0x80000000ULL;
694   if (Imm & 0x40) EncVal |= 0x3e000000ULL;
695   else            EncVal |= 0x40000000ULL;
696   if (Imm & 0x20) EncVal |= 0x01000000ULL;
697   if (Imm & 0x10) EncVal |= 0x00800000ULL;
698   if (Imm & 0x08) EncVal |= 0x00400000ULL;
699   if (Imm & 0x04) EncVal |= 0x00200000ULL;
700   if (Imm & 0x02) EncVal |= 0x00100000ULL;
701   if (Imm & 0x01) EncVal |= 0x00080000ULL;
702   return (EncVal << 32) | EncVal;
703 }
704
705 // aBbbbbbb bbcdefgh 0x00 0x00 0x00 0x00 0x00 0x00
706 static inline bool isAdvSIMDModImmType12(uint64_t Imm) {
707   uint64_t BString = (Imm & 0x7fc0000000000000ULL) >> 54;
708   return ((BString == 0xff || BString == 0x100) &&
709          ((Imm & 0x0000ffffffffffffULL) == 0));
710 }
711
712 static inline uint8_t encodeAdvSIMDModImmType12(uint64_t Imm) {
713   uint8_t BitA = (Imm & 0x8000000000000000ULL) != 0;
714   uint8_t BitB = (Imm & 0x0040000000000000ULL) != 0;
715   uint8_t BitC = (Imm & 0x0020000000000000ULL) != 0;
716   uint8_t BitD = (Imm & 0x0010000000000000ULL) != 0;
717   uint8_t BitE = (Imm & 0x0008000000000000ULL) != 0;
718   uint8_t BitF = (Imm & 0x0004000000000000ULL) != 0;
719   uint8_t BitG = (Imm & 0x0002000000000000ULL) != 0;
720   uint8_t BitH = (Imm & 0x0001000000000000ULL) != 0;
721
722   uint8_t EncVal = BitA;
723   EncVal <<= 1;
724   EncVal |= BitB;
725   EncVal <<= 1;
726   EncVal |= BitC;
727   EncVal <<= 1;
728   EncVal |= BitD;
729   EncVal <<= 1;
730   EncVal |= BitE;
731   EncVal <<= 1;
732   EncVal |= BitF;
733   EncVal <<= 1;
734   EncVal |= BitG;
735   EncVal <<= 1;
736   EncVal |= BitH;
737   return EncVal;
738 }
739
740 static inline uint64_t decodeAdvSIMDModImmType12(uint8_t Imm) {
741   uint64_t EncVal = 0;
742   if (Imm & 0x80) EncVal |= 0x8000000000000000ULL;
743   if (Imm & 0x40) EncVal |= 0x3fc0000000000000ULL;
744   else            EncVal |= 0x4000000000000000ULL;
745   if (Imm & 0x20) EncVal |= 0x0020000000000000ULL;
746   if (Imm & 0x10) EncVal |= 0x0010000000000000ULL;
747   if (Imm & 0x08) EncVal |= 0x0008000000000000ULL;
748   if (Imm & 0x04) EncVal |= 0x0004000000000000ULL;
749   if (Imm & 0x02) EncVal |= 0x0002000000000000ULL;
750   if (Imm & 0x01) EncVal |= 0x0001000000000000ULL;
751   return (EncVal << 32) | EncVal;
752 }
753
754 } // end namespace ARM64_AM
755
756 } // end namespace llvm
757
758 #endif