xref: /freebsd/contrib/llvm-project/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h (revision 1db9f3b21e39176dd5b67cf8ac378633b172463e)
1 //===-- X86BaseInfo.h - Top level definitions for X86 -------- --*- 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 small standalone helper functions and enum definitions for
10 // the X86 target useful for the compiler back-end and the MC libraries.
11 // As such, it deliberately does not include references to LLVM core
12 // code gen types, passes, etc..
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_LIB_TARGET_X86_MCTARGETDESC_X86BASEINFO_H
17 #define LLVM_LIB_TARGET_X86_MCTARGETDESC_X86BASEINFO_H
18 
19 #include "X86MCTargetDesc.h"
20 #include "llvm/MC/MCInstrDesc.h"
21 #include "llvm/Support/DataTypes.h"
22 #include "llvm/Support/ErrorHandling.h"
23 
24 namespace llvm {
25 namespace X86 {
26 // Enums for memory operand decoding. Each memory operand is represented with
27 // a 5 operand sequence in the form: [Base, Scale, Index, Disp, Segment]
28 enum {
29   AddrBaseReg = 0,
30   AddrScaleAmt = 1,
31   AddrIndexReg = 2,
32   AddrDisp = 3,
33   // The operand # of the segment in the memory operand.
34   AddrSegmentReg = 4,
35   // Total number of operands in a memory reference.
36   AddrNumOperands = 5
37 };
38 
39 /// AVX512 static rounding constants. These need to match the values in
40 /// avx512fintrin.h.
41 enum STATIC_ROUNDING {
42   TO_NEAREST_INT = 0,
43   TO_NEG_INF = 1,
44   TO_POS_INF = 2,
45   TO_ZERO = 3,
46   CUR_DIRECTION = 4,
47   NO_EXC = 8
48 };
49 
50 /// The constants to describe instr prefixes if there are
51 enum IPREFIXES {
52   IP_NO_PREFIX = 0,
53   IP_HAS_OP_SIZE = 1U << 0,
54   IP_HAS_AD_SIZE = 1U << 1,
55   IP_HAS_REPEAT_NE = 1U << 2,
56   IP_HAS_REPEAT = 1U << 3,
57   IP_HAS_LOCK = 1U << 4,
58   IP_HAS_NOTRACK = 1U << 5,
59   IP_USE_VEX = 1U << 6,
60   IP_USE_VEX2 = 1U << 7,
61   IP_USE_VEX3 = 1U << 8,
62   IP_USE_EVEX = 1U << 9,
63   IP_USE_DISP8 = 1U << 10,
64   IP_USE_DISP32 = 1U << 11,
65 };
66 
67 enum OperandType : unsigned {
68   // AVX512 embedded rounding control. This should only have values 0-3.
69   OPERAND_ROUNDING_CONTROL = MCOI::OPERAND_FIRST_TARGET,
70   OPERAND_COND_CODE,
71 };
72 
73 // X86 specific condition code. These correspond to X86_*_COND in
74 // X86InstrInfo.td. They must be kept in synch.
75 enum CondCode {
76   COND_O = 0,
77   COND_NO = 1,
78   COND_B = 2,
79   COND_AE = 3,
80   COND_E = 4,
81   COND_NE = 5,
82   COND_BE = 6,
83   COND_A = 7,
84   COND_S = 8,
85   COND_NS = 9,
86   COND_P = 10,
87   COND_NP = 11,
88   COND_L = 12,
89   COND_GE = 13,
90   COND_LE = 14,
91   COND_G = 15,
92   LAST_VALID_COND = COND_G,
93   // Artificial condition codes. These are used by analyzeBranch
94   // to indicate a block terminated with two conditional branches that together
95   // form a compound condition. They occur in code using FCMP_OEQ or FCMP_UNE,
96   // which can't be represented on x86 with a single condition. These
97   // are never used in MachineInstrs and are inverses of one another.
98   COND_NE_OR_P,
99   COND_E_AND_NP,
100   COND_INVALID
101 };
102 
103 // The classification for the first instruction in macro fusion.
104 // FIXME: Zen 3 support branch fusion for OR/XOR.
105 enum class FirstMacroFusionInstKind {
106   Test,   // TEST
107   Cmp,    // CMP
108   And,    // AND
109   AddSub, // ADD, SUB
110   IncDec, // INC, DEC
111   Invalid // Not valid as a first macro fusion instruction
112 };
113 
114 enum class SecondMacroFusionInstKind {
115   AB,      // JA, JB and variants
116   ELG,     // JE, JL, JG and variants
117   SPO,     // JS, JP, JO and variants
118   Invalid, // Not a fusible jump.
119 };
120 
121 /// \returns the type of the first instruction in macro-fusion.
122 // FIXME: Zen 3 support branch fusion for OR/XOR.
123 inline FirstMacroFusionInstKind
124 classifyFirstOpcodeInMacroFusion(unsigned Opcode) {
125   switch (Opcode) {
126   default:
127     return FirstMacroFusionInstKind::Invalid;
128   // TEST
129   case X86::TEST16i16:
130   case X86::TEST16mr:
131   case X86::TEST16ri:
132   case X86::TEST16rr:
133   case X86::TEST32i32:
134   case X86::TEST32mr:
135   case X86::TEST32ri:
136   case X86::TEST32rr:
137   case X86::TEST64i32:
138   case X86::TEST64mr:
139   case X86::TEST64ri32:
140   case X86::TEST64rr:
141   case X86::TEST8i8:
142   case X86::TEST8mr:
143   case X86::TEST8ri:
144   case X86::TEST8rr:
145     return FirstMacroFusionInstKind::Test;
146   case X86::AND16i16:
147   case X86::AND16ri:
148   case X86::AND16ri8:
149   case X86::AND16rm:
150   case X86::AND16rr:
151   case X86::AND32i32:
152   case X86::AND32ri:
153   case X86::AND32ri8:
154   case X86::AND32rm:
155   case X86::AND32rr:
156   case X86::AND64i32:
157   case X86::AND64ri32:
158   case X86::AND64ri8:
159   case X86::AND64rm:
160   case X86::AND64rr:
161   case X86::AND8i8:
162   case X86::AND8ri:
163   case X86::AND8ri8:
164   case X86::AND8rm:
165   case X86::AND8rr:
166     return FirstMacroFusionInstKind::And;
167   // CMP
168   case X86::CMP16i16:
169   case X86::CMP16mr:
170   case X86::CMP16ri:
171   case X86::CMP16ri8:
172   case X86::CMP16rm:
173   case X86::CMP16rr:
174   case X86::CMP32i32:
175   case X86::CMP32mr:
176   case X86::CMP32ri:
177   case X86::CMP32ri8:
178   case X86::CMP32rm:
179   case X86::CMP32rr:
180   case X86::CMP64i32:
181   case X86::CMP64mr:
182   case X86::CMP64ri32:
183   case X86::CMP64ri8:
184   case X86::CMP64rm:
185   case X86::CMP64rr:
186   case X86::CMP8i8:
187   case X86::CMP8mr:
188   case X86::CMP8ri:
189   case X86::CMP8ri8:
190   case X86::CMP8rm:
191   case X86::CMP8rr:
192     return FirstMacroFusionInstKind::Cmp;
193   // ADD
194   case X86::ADD16i16:
195   case X86::ADD16ri:
196   case X86::ADD16ri8:
197   case X86::ADD16rm:
198   case X86::ADD16rr:
199   case X86::ADD32i32:
200   case X86::ADD32ri:
201   case X86::ADD32ri8:
202   case X86::ADD32rm:
203   case X86::ADD32rr:
204   case X86::ADD64i32:
205   case X86::ADD64ri32:
206   case X86::ADD64ri8:
207   case X86::ADD64rm:
208   case X86::ADD64rr:
209   case X86::ADD8i8:
210   case X86::ADD8ri:
211   case X86::ADD8ri8:
212   case X86::ADD8rm:
213   case X86::ADD8rr:
214   // SUB
215   case X86::SUB16i16:
216   case X86::SUB16ri:
217   case X86::SUB16ri8:
218   case X86::SUB16rm:
219   case X86::SUB16rr:
220   case X86::SUB32i32:
221   case X86::SUB32ri:
222   case X86::SUB32ri8:
223   case X86::SUB32rm:
224   case X86::SUB32rr:
225   case X86::SUB64i32:
226   case X86::SUB64ri32:
227   case X86::SUB64ri8:
228   case X86::SUB64rm:
229   case X86::SUB64rr:
230   case X86::SUB8i8:
231   case X86::SUB8ri:
232   case X86::SUB8ri8:
233   case X86::SUB8rm:
234   case X86::SUB8rr:
235     return FirstMacroFusionInstKind::AddSub;
236   // INC
237   case X86::INC16r:
238   case X86::INC16r_alt:
239   case X86::INC32r:
240   case X86::INC32r_alt:
241   case X86::INC64r:
242   case X86::INC8r:
243   // DEC
244   case X86::DEC16r:
245   case X86::DEC16r_alt:
246   case X86::DEC32r:
247   case X86::DEC32r_alt:
248   case X86::DEC64r:
249   case X86::DEC8r:
250     return FirstMacroFusionInstKind::IncDec;
251   }
252 }
253 
254 /// \returns the type of the second instruction in macro-fusion.
255 inline SecondMacroFusionInstKind
256 classifySecondCondCodeInMacroFusion(X86::CondCode CC) {
257   if (CC == X86::COND_INVALID)
258     return SecondMacroFusionInstKind::Invalid;
259   switch (CC) {
260   default:
261     return SecondMacroFusionInstKind::Invalid;
262   case X86::COND_E:  // JE,JZ
263   case X86::COND_NE: // JNE,JNZ
264   case X86::COND_L:  // JL,JNGE
265   case X86::COND_LE: // JLE,JNG
266   case X86::COND_G:  // JG,JNLE
267   case X86::COND_GE: // JGE,JNL
268     return SecondMacroFusionInstKind::ELG;
269   case X86::COND_B:  // JB,JC
270   case X86::COND_BE: // JNA,JBE
271   case X86::COND_A:  // JA,JNBE
272   case X86::COND_AE: // JAE,JNC,JNB
273     return SecondMacroFusionInstKind::AB;
274   case X86::COND_S:  // JS
275   case X86::COND_NS: // JNS
276   case X86::COND_P:  // JP,JPE
277   case X86::COND_NP: // JNP,JPO
278   case X86::COND_O:  // JO
279   case X86::COND_NO: // JNO
280     return SecondMacroFusionInstKind::SPO;
281   }
282 }
283 
284 /// \param FirstKind kind of the first instruction in macro fusion.
285 /// \param SecondKind kind of the second instruction in macro fusion.
286 ///
287 /// \returns true if the two instruction can be macro fused.
288 inline bool isMacroFused(FirstMacroFusionInstKind FirstKind,
289                          SecondMacroFusionInstKind SecondKind) {
290   switch (FirstKind) {
291   case X86::FirstMacroFusionInstKind::Test:
292   case X86::FirstMacroFusionInstKind::And:
293     return true;
294   case X86::FirstMacroFusionInstKind::Cmp:
295   case X86::FirstMacroFusionInstKind::AddSub:
296     return SecondKind == X86::SecondMacroFusionInstKind::AB ||
297            SecondKind == X86::SecondMacroFusionInstKind::ELG;
298   case X86::FirstMacroFusionInstKind::IncDec:
299     return SecondKind == X86::SecondMacroFusionInstKind::ELG;
300   case X86::FirstMacroFusionInstKind::Invalid:
301     return false;
302   }
303   llvm_unreachable("unknown fusion type");
304 }
305 
306 /// Defines the possible values of the branch boundary alignment mask.
307 enum AlignBranchBoundaryKind : uint8_t {
308   AlignBranchNone = 0,
309   AlignBranchFused = 1U << 0,
310   AlignBranchJcc = 1U << 1,
311   AlignBranchJmp = 1U << 2,
312   AlignBranchCall = 1U << 3,
313   AlignBranchRet = 1U << 4,
314   AlignBranchIndirect = 1U << 5
315 };
316 
317 /// Defines the encoding values for segment override prefix.
318 enum EncodingOfSegmentOverridePrefix : uint8_t {
319   CS_Encoding = 0x2E,
320   DS_Encoding = 0x3E,
321   ES_Encoding = 0x26,
322   FS_Encoding = 0x64,
323   GS_Encoding = 0x65,
324   SS_Encoding = 0x36
325 };
326 
327 /// Given a segment register, return the encoding of the segment override
328 /// prefix for it.
329 inline EncodingOfSegmentOverridePrefix
330 getSegmentOverridePrefixForReg(unsigned Reg) {
331   switch (Reg) {
332   default:
333     llvm_unreachable("Unknown segment register!");
334   case X86::CS:
335     return CS_Encoding;
336   case X86::DS:
337     return DS_Encoding;
338   case X86::ES:
339     return ES_Encoding;
340   case X86::FS:
341     return FS_Encoding;
342   case X86::GS:
343     return GS_Encoding;
344   case X86::SS:
345     return SS_Encoding;
346   }
347 }
348 
349 } // namespace X86
350 
351 /// X86II - This namespace holds all of the target specific flags that
352 /// instruction info tracks.
353 ///
354 namespace X86II {
355 /// Target Operand Flag enum.
356 enum TOF {
357   //===------------------------------------------------------------------===//
358   // X86 Specific MachineOperand flags.
359   //
360   /// MO_NO_FLAG - No flag for the operand
361   MO_NO_FLAG,
362   /// MO_GOT_ABSOLUTE_ADDRESS - On a symbol operand, this represents a
363   /// relocation of:
364   ///    SYMBOL_LABEL + [. - PICBASELABEL]
365   MO_GOT_ABSOLUTE_ADDRESS,
366   /// MO_PIC_BASE_OFFSET - On a symbol operand this indicates that the
367   /// immediate should get the value of the symbol minus the PIC base label:
368   ///    SYMBOL_LABEL - PICBASELABEL
369   MO_PIC_BASE_OFFSET,
370   /// MO_GOT - On a symbol operand this indicates that the immediate is the
371   /// offset to the GOT entry for the symbol name from the base of the GOT.
372   /// See the X86-64 ELF ABI supplement for more details.
373   ///    SYMBOL_LABEL @GOT
374   MO_GOT,
375   /// MO_GOTOFF - On a symbol operand this indicates that the immediate is
376   /// the offset to the location of the symbol name from the base of the GOT.
377   /// See the X86-64 ELF ABI supplement for more details.
378   ///    SYMBOL_LABEL @GOTOFF
379   MO_GOTOFF,
380   /// MO_GOTPCREL - On a symbol operand this indicates that the immediate is
381   /// offset to the GOT entry for the symbol name from the current code
382   /// location.
383   /// See the X86-64 ELF ABI supplement for more details.
384   ///    SYMBOL_LABEL @GOTPCREL
385   MO_GOTPCREL,
386   /// MO_GOTPCREL_NORELAX - Same as MO_GOTPCREL except that R_X86_64_GOTPCREL
387   /// relocations are guaranteed to be emitted by the integrated assembler
388   /// instead of the relaxable R_X86_64[_REX]_GOTPCRELX relocations.
389   MO_GOTPCREL_NORELAX,
390   /// MO_PLT - On a symbol operand this indicates that the immediate is
391   /// offset to the PLT entry of symbol name from the current code location.
392   /// See the X86-64 ELF ABI supplement for more details.
393   ///    SYMBOL_LABEL @PLT
394   MO_PLT,
395   /// MO_TLSGD - On a symbol operand this indicates that the immediate is
396   /// the offset of the GOT entry with the TLS index structure that contains
397   /// the module number and variable offset for the symbol. Used in the
398   /// general dynamic TLS access model.
399   /// See 'ELF Handling for Thread-Local Storage' for more details.
400   ///    SYMBOL_LABEL @TLSGD
401   MO_TLSGD,
402   /// MO_TLSLD - On a symbol operand this indicates that the immediate is
403   /// the offset of the GOT entry with the TLS index for the module that
404   /// contains the symbol. When this index is passed to a call to
405   /// __tls_get_addr, the function will return the base address of the TLS
406   /// block for the symbol. Used in the x86-64 local dynamic TLS access model.
407   /// See 'ELF Handling for Thread-Local Storage' for more details.
408   ///    SYMBOL_LABEL @TLSLD
409   MO_TLSLD,
410   /// MO_TLSLDM - On a symbol operand this indicates that the immediate is
411   /// the offset of the GOT entry with the TLS index for the module that
412   /// contains the symbol. When this index is passed to a call to
413   /// ___tls_get_addr, the function will return the base address of the TLS
414   /// block for the symbol. Used in the IA32 local dynamic TLS access model.
415   /// See 'ELF Handling for Thread-Local Storage' for more details.
416   ///    SYMBOL_LABEL @TLSLDM
417   MO_TLSLDM,
418   /// MO_GOTTPOFF - On a symbol operand this indicates that the immediate is
419   /// the offset of the GOT entry with the thread-pointer offset for the
420   /// symbol. Used in the x86-64 initial exec TLS access model.
421   /// See 'ELF Handling for Thread-Local Storage' for more details.
422   ///    SYMBOL_LABEL @GOTTPOFF
423   MO_GOTTPOFF,
424   /// MO_INDNTPOFF - On a symbol operand this indicates that the immediate is
425   /// the absolute address of the GOT entry with the negative thread-pointer
426   /// offset for the symbol. Used in the non-PIC IA32 initial exec TLS access
427   /// model.
428   /// See 'ELF Handling for Thread-Local Storage' for more details.
429   ///    SYMBOL_LABEL @INDNTPOFF
430   MO_INDNTPOFF,
431   /// MO_TPOFF - On a symbol operand this indicates that the immediate is
432   /// the thread-pointer offset for the symbol. Used in the x86-64 local
433   /// exec TLS access model.
434   /// See 'ELF Handling for Thread-Local Storage' for more details.
435   ///    SYMBOL_LABEL @TPOFF
436   MO_TPOFF,
437   /// MO_DTPOFF - On a symbol operand this indicates that the immediate is
438   /// the offset of the GOT entry with the TLS offset of the symbol. Used
439   /// in the local dynamic TLS access model.
440   /// See 'ELF Handling for Thread-Local Storage' for more details.
441   ///    SYMBOL_LABEL @DTPOFF
442   MO_DTPOFF,
443   /// MO_NTPOFF - On a symbol operand this indicates that the immediate is
444   /// the negative thread-pointer offset for the symbol. Used in the IA32
445   /// local exec TLS access model.
446   /// See 'ELF Handling for Thread-Local Storage' for more details.
447   ///    SYMBOL_LABEL @NTPOFF
448   MO_NTPOFF,
449   /// MO_GOTNTPOFF - On a symbol operand this indicates that the immediate is
450   /// the offset of the GOT entry with the negative thread-pointer offset for
451   /// the symbol. Used in the PIC IA32 initial exec TLS access model.
452   /// See 'ELF Handling for Thread-Local Storage' for more details.
453   ///    SYMBOL_LABEL @GOTNTPOFF
454   MO_GOTNTPOFF,
455   /// MO_DLLIMPORT - On a symbol operand "FOO", this indicates that the
456   /// reference is actually to the "__imp_FOO" symbol.  This is used for
457   /// dllimport linkage on windows.
458   MO_DLLIMPORT,
459   /// MO_DARWIN_NONLAZY - On a symbol operand "FOO", this indicates that the
460   /// reference is actually to the "FOO$non_lazy_ptr" symbol, which is a
461   /// non-PIC-base-relative reference to a non-hidden dyld lazy pointer stub.
462   MO_DARWIN_NONLAZY,
463   /// MO_DARWIN_NONLAZY_PIC_BASE - On a symbol operand "FOO", this indicates
464   /// that the reference is actually to "FOO$non_lazy_ptr - PICBASE", which is
465   /// a PIC-base-relative reference to a non-hidden dyld lazy pointer stub.
466   MO_DARWIN_NONLAZY_PIC_BASE,
467   /// MO_TLVP - On a symbol operand this indicates that the immediate is
468   /// some TLS offset.
469   /// This is the TLS offset for the Darwin TLS mechanism.
470   MO_TLVP,
471   /// MO_TLVP_PIC_BASE - On a symbol operand this indicates that the immediate
472   /// is some TLS offset from the picbase.
473   /// This is the 32-bit TLS offset for Darwin TLS in PIC mode.
474   MO_TLVP_PIC_BASE,
475   /// MO_SECREL - On a symbol operand this indicates that the immediate is
476   /// the offset from beginning of section.
477   /// This is the TLS offset for the COFF/Windows TLS mechanism.
478   MO_SECREL,
479   /// MO_ABS8 - On a symbol operand this indicates that the symbol is known
480   /// to be an absolute symbol in range [0,128), so we can use the @ABS8
481   /// symbol modifier.
482   MO_ABS8,
483   /// MO_COFFSTUB - On a symbol operand "FOO", this indicates that the
484   /// reference is actually to the ".refptr.FOO" symbol.  This is used for
485   /// stub symbols on windows.
486   MO_COFFSTUB,
487 };
488 
489 enum : uint64_t {
490   //===------------------------------------------------------------------===//
491   // Instruction encodings.  These are the standard/most common forms for X86
492   // instructions.
493   //
494   /// PseudoFrm - This represents an instruction that is a pseudo instruction
495   /// or one that has not been implemented yet.  It is illegal to code generate
496   /// it, but tolerated for intermediate implementation stages.
497   Pseudo = 0,
498   /// Raw - This form is for instructions that don't have any operands, so
499   /// they are just a fixed opcode value, like 'leave'.
500   RawFrm = 1,
501   /// AddRegFrm - This form is used for instructions like 'push r32' that have
502   /// their one register operand added to their opcode.
503   AddRegFrm = 2,
504   /// RawFrmMemOffs - This form is for instructions that store an absolute
505   /// memory offset as an immediate with a possible segment override.
506   RawFrmMemOffs = 3,
507   /// RawFrmSrc - This form is for instructions that use the source index
508   /// register SI/ESI/RSI with a possible segment override.
509   RawFrmSrc = 4,
510   /// RawFrmDst - This form is for instructions that use the destination index
511   /// register DI/EDI/RDI.
512   RawFrmDst = 5,
513   /// RawFrmDstSrc - This form is for instructions that use the source index
514   /// register SI/ESI/RSI with a possible segment override, and also the
515   /// destination index register DI/EDI/RDI.
516   RawFrmDstSrc = 6,
517   /// RawFrmImm8 - This is used for the ENTER instruction, which has two
518   /// immediates, the first of which is a 16-bit immediate (specified by
519   /// the imm encoding) and the second is a 8-bit fixed value.
520   RawFrmImm8 = 7,
521   /// RawFrmImm16 - This is used for CALL FAR instructions, which have two
522   /// immediates, the first of which is a 16 or 32-bit immediate (specified by
523   /// the imm encoding) and the second is a 16-bit fixed value.  In the AMD
524   /// manual, this operand is described as pntr16:32 and pntr16:16
525   RawFrmImm16 = 8,
526   /// AddCCFrm - This form is used for Jcc that encode the condition code
527   /// in the lower 4 bits of the opcode.
528   AddCCFrm = 9,
529   /// PrefixByte - This form is used for instructions that represent a prefix
530   /// byte like data16 or rep.
531   PrefixByte = 10,
532   /// MRMDestMem4VOp3CC - This form is used for instructions that use the Mod/RM
533   /// byte to specify a destination which in this case is memory and operand 3
534   /// with VEX.VVVV, and also encodes a condition code.
535   MRMDestMem4VOp3CC = 20,
536   /// Instructions operate on a register Reg/Opcode operand not the r/m field.
537   MRMr0 = 21,
538   /// MRMSrcMem - But force to use the SIB field.
539   MRMSrcMemFSIB = 22,
540   /// MRMDestMem - But force to use the SIB field.
541   MRMDestMemFSIB = 23,
542   /// MRMDestMem - This form is used for instructions that use the Mod/RM byte
543   /// to specify a destination, which in this case is memory.
544   MRMDestMem = 24,
545   /// MRMSrcMem - This form is used for instructions that use the Mod/RM byte
546   /// to specify a source, which in this case is memory.
547   MRMSrcMem = 25,
548   /// MRMSrcMem4VOp3 - This form is used for instructions that encode
549   /// operand 3 with VEX.VVVV and load from memory.
550   MRMSrcMem4VOp3 = 26,
551   /// MRMSrcMemOp4 - This form is used for instructions that use the Mod/RM
552   /// byte to specify the fourth source, which in this case is memory.
553   MRMSrcMemOp4 = 27,
554   /// MRMSrcMemCC - This form is used for instructions that use the Mod/RM
555   /// byte to specify the operands and also encodes a condition code.
556   MRMSrcMemCC = 28,
557   /// MRMXm - This form is used for instructions that use the Mod/RM byte
558   /// to specify a memory source, but doesn't use the middle field. And has
559   /// a condition code.
560   MRMXmCC = 30,
561   /// MRMXm - This form is used for instructions that use the Mod/RM byte
562   /// to specify a memory source, but doesn't use the middle field.
563   MRMXm = 31,
564   /// MRM0m-MRM7m - Instructions that operate on a memory r/m operand and use
565   /// reg field to hold extended opcode, which is represented as /0, /1, ...
566   MRM0m = 32, // Format /0
567   MRM1m = 33, // Format /1
568   MRM2m = 34, // Format /2
569   MRM3m = 35, // Format /3
570   MRM4m = 36, // Format /4
571   MRM5m = 37, // Format /5
572   MRM6m = 38, // Format /6
573   MRM7m = 39, // Format /7
574   /// MRMDestReg - This form is used for instructions that use the Mod/RM byte
575   /// to specify a destination, which in this case is a register.
576   MRMDestReg = 40,
577   /// MRMSrcReg - This form is used for instructions that use the Mod/RM byte
578   /// to specify a source, which in this case is a register.
579   MRMSrcReg = 41,
580   /// MRMSrcReg4VOp3 - This form is used for instructions that encode
581   /// operand 3 with VEX.VVVV and do not load from memory.
582   MRMSrcReg4VOp3 = 42,
583   /// MRMSrcRegOp4 - This form is used for instructions that use the Mod/RM
584   /// byte to specify the fourth source, which in this case is a register.
585   MRMSrcRegOp4 = 43,
586   /// MRMSrcRegCC - This form is used for instructions that use the Mod/RM
587   /// byte to specify the operands and also encodes a condition code
588   MRMSrcRegCC = 44,
589   /// MRMXCCr - This form is used for instructions that use the Mod/RM byte
590   /// to specify a register source, but doesn't use the middle field. And has
591   /// a condition code.
592   MRMXrCC = 46,
593   /// MRMXr - This form is used for instructions that use the Mod/RM byte
594   /// to specify a register source, but doesn't use the middle field.
595   MRMXr = 47,
596   /// MRM0r-MRM7r - Instructions that operate on a register r/m operand and use
597   /// reg field to hold extended opcode, which is represented as /0, /1, ...
598   MRM0r = 48, // Format /0
599   MRM1r = 49, // Format /1
600   MRM2r = 50, // Format /2
601   MRM3r = 51, // Format /3
602   MRM4r = 52, // Format /4
603   MRM5r = 53, // Format /5
604   MRM6r = 54, // Format /6
605   MRM7r = 55, // Format /7
606   /// MRM0X-MRM7X - Instructions that operate that have mod=11 and an opcode but
607   /// ignore r/m.
608   MRM0X = 56, // Format /0
609   MRM1X = 57, // Format /1
610   MRM2X = 58, // Format /2
611   MRM3X = 59, // Format /3
612   MRM4X = 60, // Format /4
613   MRM5X = 61, // Format /5
614   MRM6X = 62, // Format /6
615   MRM7X = 63, // Format /7
616   /// MRM_XX (XX: C0-FF)- A mod/rm byte of exactly 0xXX.
617   MRM_C0 = 64,
618   MRM_C1 = 65,
619   MRM_C2 = 66,
620   MRM_C3 = 67,
621   MRM_C4 = 68,
622   MRM_C5 = 69,
623   MRM_C6 = 70,
624   MRM_C7 = 71,
625   MRM_C8 = 72,
626   MRM_C9 = 73,
627   MRM_CA = 74,
628   MRM_CB = 75,
629   MRM_CC = 76,
630   MRM_CD = 77,
631   MRM_CE = 78,
632   MRM_CF = 79,
633   MRM_D0 = 80,
634   MRM_D1 = 81,
635   MRM_D2 = 82,
636   MRM_D3 = 83,
637   MRM_D4 = 84,
638   MRM_D5 = 85,
639   MRM_D6 = 86,
640   MRM_D7 = 87,
641   MRM_D8 = 88,
642   MRM_D9 = 89,
643   MRM_DA = 90,
644   MRM_DB = 91,
645   MRM_DC = 92,
646   MRM_DD = 93,
647   MRM_DE = 94,
648   MRM_DF = 95,
649   MRM_E0 = 96,
650   MRM_E1 = 97,
651   MRM_E2 = 98,
652   MRM_E3 = 99,
653   MRM_E4 = 100,
654   MRM_E5 = 101,
655   MRM_E6 = 102,
656   MRM_E7 = 103,
657   MRM_E8 = 104,
658   MRM_E9 = 105,
659   MRM_EA = 106,
660   MRM_EB = 107,
661   MRM_EC = 108,
662   MRM_ED = 109,
663   MRM_EE = 110,
664   MRM_EF = 111,
665   MRM_F0 = 112,
666   MRM_F1 = 113,
667   MRM_F2 = 114,
668   MRM_F3 = 115,
669   MRM_F4 = 116,
670   MRM_F5 = 117,
671   MRM_F6 = 118,
672   MRM_F7 = 119,
673   MRM_F8 = 120,
674   MRM_F9 = 121,
675   MRM_FA = 122,
676   MRM_FB = 123,
677   MRM_FC = 124,
678   MRM_FD = 125,
679   MRM_FE = 126,
680   MRM_FF = 127,
681   FormMask = 127,
682   //===------------------------------------------------------------------===//
683   // Actual flags...
684   /// OpSize - OpSizeFixed implies instruction never needs a 0x66 prefix.
685   /// OpSize16 means this is a 16-bit instruction and needs 0x66 prefix in
686   /// 32-bit mode. OpSize32 means this is a 32-bit instruction needs a 0x66
687   /// prefix in 16-bit mode.
688   OpSizeShift = 7,
689   OpSizeMask = 0x3 << OpSizeShift,
690   OpSizeFixed = 0 << OpSizeShift,
691   OpSize16 = 1 << OpSizeShift,
692   OpSize32 = 2 << OpSizeShift,
693   /// AsSize - AdSizeX implies this instruction determines its need of 0x67
694   /// prefix from a normal ModRM memory operand. The other types indicate that
695   /// an operand is encoded with a specific width and a prefix is needed if
696   /// it differs from the current mode.
697   AdSizeShift = OpSizeShift + 2,
698   AdSizeMask = 0x3 << AdSizeShift,
699   AdSizeX = 0 << AdSizeShift,
700   AdSize16 = 1 << AdSizeShift,
701   AdSize32 = 2 << AdSizeShift,
702   AdSize64 = 3 << AdSizeShift,
703   //===------------------------------------------------------------------===//
704   /// OpPrefix - There are several prefix bytes that are used as opcode
705   /// extensions. These are 0x66, 0xF3, and 0xF2. If this field is 0 there is
706   /// no prefix.
707   OpPrefixShift = AdSizeShift + 2,
708   OpPrefixMask = 0x3 << OpPrefixShift,
709   /// PD - Prefix code for packed double precision vector floating point
710   /// operations performed in the SSE registers.
711   PD = 1 << OpPrefixShift,
712   /// XS, XD - These prefix codes are for single and double precision scalar
713   /// floating point operations performed in the SSE registers.
714   XS = 2 << OpPrefixShift,
715   XD = 3 << OpPrefixShift,
716   //===------------------------------------------------------------------===//
717   /// OpMap - This field determines which opcode map this instruction
718   /// belongs to. i.e. one-byte, two-byte, 0x0f 0x38, 0x0f 0x3a, etc.
719   OpMapShift = OpPrefixShift + 2,
720   OpMapMask = 0xF << OpMapShift,
721   /// OB - OneByte - Set if this instruction has a one byte opcode.
722   OB = 0 << OpMapShift,
723   /// TB - TwoByte - Set if this instruction has a two byte opcode, which
724   /// starts with a 0x0F byte before the real opcode.
725   TB = 1 << OpMapShift,
726   /// T8, TA - Prefix after the 0x0F prefix.
727   T8 = 2 << OpMapShift,
728   TA = 3 << OpMapShift,
729   /// XOP8 - Prefix to include use of imm byte.
730   XOP8 = 4 << OpMapShift,
731   /// XOP9 - Prefix to exclude use of imm byte.
732   XOP9 = 5 << OpMapShift,
733   /// XOPA - Prefix to encode 0xA in VEX.MMMM of XOP instructions.
734   XOPA = 6 << OpMapShift,
735   /// ThreeDNow - This indicates that the instruction uses the
736   /// wacky 0x0F 0x0F prefix for 3DNow! instructions.  The manual documents
737   /// this as having a 0x0F prefix with a 0x0F opcode, and each instruction
738   /// storing a classifier in the imm8 field.  To simplify our implementation,
739   /// we handle this by storeing the classifier in the opcode field and using
740   /// this flag to indicate that the encoder should do the wacky 3DNow! thing.
741   ThreeDNow = 7 << OpMapShift,
742   /// MAP4, MAP5, MAP6, MAP7 - Prefix after the 0x0F prefix.
743   T_MAP4 = 8 << OpMapShift,
744   T_MAP5 = 9 << OpMapShift,
745   T_MAP6 = 10 << OpMapShift,
746   T_MAP7 = 11 << OpMapShift,
747   //===------------------------------------------------------------------===//
748   /// REX_W - REX prefixes are instruction prefixes used in 64-bit mode.
749   /// They are used to specify GPRs and SSE registers, 64-bit operand size,
750   /// etc. We only cares about REX.W and REX.R bits and only the former is
751   /// statically determined.
752   REXShift = OpMapShift + 4,
753   REX_W = 1 << REXShift,
754   //===------------------------------------------------------------------===//
755   // This 4-bit field describes the size of an immediate operand. Zero is
756   // unused so that we can tell if we forgot to set a value.
757   ImmShift = REXShift + 1,
758   Imm8 = 1 << ImmShift,
759   Imm8PCRel = 2 << ImmShift,
760   Imm8Reg = 3 << ImmShift,
761   Imm16 = 4 << ImmShift,
762   Imm16PCRel = 5 << ImmShift,
763   Imm32 = 6 << ImmShift,
764   Imm32PCRel = 7 << ImmShift,
765   Imm32S = 8 << ImmShift,
766   Imm64 = 9 << ImmShift,
767   ImmMask = 15 << ImmShift,
768   //===------------------------------------------------------------------===//
769   /// FP Instruction Classification...  Zero is non-fp instruction.
770   /// FPTypeMask - Mask for all of the FP types...
771   FPTypeShift = ImmShift + 4,
772   FPTypeMask = 7 << FPTypeShift,
773   /// NotFP - The default, set for instructions that do not use FP registers.
774   NotFP = 0 << FPTypeShift,
775   /// ZeroArgFP - 0 arg FP instruction which implicitly pushes ST(0), f.e. fld0
776   ZeroArgFP = 1 << FPTypeShift,
777   /// OneArgFP - 1 arg FP instructions which implicitly read ST(0), such as fst
778   OneArgFP = 2 << FPTypeShift,
779   /// OneArgFPRW - 1 arg FP instruction which implicitly read ST(0) and write a
780   /// result back to ST(0).  For example, fcos, fsqrt, etc.
781   OneArgFPRW = 3 << FPTypeShift,
782   /// TwoArgFP - 2 arg FP instructions which implicitly read ST(0), and an
783   /// explicit argument, storing the result to either ST(0) or the implicit
784   /// argument.  For example: fadd, fsub, fmul, etc...
785   TwoArgFP = 4 << FPTypeShift,
786   /// CompareFP - 2 arg FP instructions which implicitly read ST(0) and an
787   /// explicit argument, but have no destination.  Example: fucom, fucomi, ...
788   CompareFP = 5 << FPTypeShift,
789   /// CondMovFP - "2 operand" floating point conditional move instructions.
790   CondMovFP = 6 << FPTypeShift,
791   /// SpecialFP - Special instruction forms.  Dispatch by opcode explicitly.
792   SpecialFP = 7 << FPTypeShift,
793   /// Lock prefix
794   LOCKShift = FPTypeShift + 3,
795   LOCK = 1 << LOCKShift,
796   /// REP prefix
797   REPShift = LOCKShift + 1,
798   REP = 1 << REPShift,
799   /// Execution domain for SSE instructions.
800   /// 0 means normal, non-SSE instruction.
801   SSEDomainShift = REPShift + 1,
802   /// Encoding
803   EncodingShift = SSEDomainShift + 2,
804   EncodingMask = 0x3 << EncodingShift,
805   /// VEX - encoding using 0xC4/0xC5
806   VEX = 1 << EncodingShift,
807   /// XOP - Opcode prefix used by XOP instructions.
808   XOP = 2 << EncodingShift,
809   /// EVEX - Specifies that this instruction use EVEX form which provides
810   /// syntax support up to 32 512-bit register operands and up to 7 16-bit
811   /// mask operands as well as source operand data swizzling/memory operand
812   /// conversion, eviction hint, and rounding mode.
813   EVEX = 3 << EncodingShift,
814   /// Opcode
815   OpcodeShift = EncodingShift + 2,
816   /// VEX_4V - Used to specify an additional AVX/SSE register. Several 2
817   /// address instructions in SSE are represented as 3 address ones in AVX
818   /// and the additional register is encoded in VEX_VVVV prefix.
819   VEX_4VShift = OpcodeShift + 8,
820   VEX_4V = 1ULL << VEX_4VShift,
821   /// VEX_L - Stands for a bit in the VEX opcode prefix meaning the current
822   /// instruction uses 256-bit wide registers. This is usually auto detected
823   /// if a VR256 register is used, but some AVX instructions also have this
824   /// field marked when using a f256 memory references.
825   VEX_LShift = VEX_4VShift + 1,
826   VEX_L = 1ULL << VEX_LShift,
827   /// EVEX_K - Set if this instruction requires masking
828   EVEX_KShift = VEX_LShift + 1,
829   EVEX_K = 1ULL << EVEX_KShift,
830   /// EVEX_Z - Set if this instruction has EVEX.Z field set.
831   EVEX_ZShift = EVEX_KShift + 1,
832   EVEX_Z = 1ULL << EVEX_ZShift,
833   /// EVEX_L2 - Set if this instruction has EVEX.L' field set.
834   EVEX_L2Shift = EVEX_ZShift + 1,
835   EVEX_L2 = 1ULL << EVEX_L2Shift,
836   /// EVEX_B - Set if this instruction has EVEX.B field set.
837   EVEX_BShift = EVEX_L2Shift + 1,
838   EVEX_B = 1ULL << EVEX_BShift,
839   /// The scaling factor for the AVX512's 8-bit compressed displacement.
840   CD8_Scale_Shift = EVEX_BShift + 1,
841   CD8_Scale_Mask = 7ULL << CD8_Scale_Shift,
842   /// Explicitly specified rounding control
843   EVEX_RCShift = CD8_Scale_Shift + 3,
844   EVEX_RC = 1ULL << EVEX_RCShift,
845   /// NOTRACK prefix
846   NoTrackShift = EVEX_RCShift + 1,
847   NOTRACK = 1ULL << NoTrackShift,
848   /// Force REX2/VEX/EVEX encoding
849   ExplicitOpPrefixShift = NoTrackShift + 1,
850   /// For instructions that require REX2 prefix even if EGPR is not used.
851   ExplicitREX2Prefix = 1ULL << ExplicitOpPrefixShift,
852   /// For instructions that use VEX encoding only when {vex}, {vex2} or {vex3}
853   /// is present.
854   ExplicitVEXPrefix = 2ULL << ExplicitOpPrefixShift,
855   /// For instructions that are promoted to EVEX space for EGPR.
856   ExplicitEVEXPrefix = 3ULL << ExplicitOpPrefixShift,
857   ExplicitOpPrefixMask = 3ULL << ExplicitOpPrefixShift,
858   /// EVEX_NF - Set if this instruction has EVEX.NF field set.
859   EVEX_NFShift = ExplicitOpPrefixShift + 2,
860   EVEX_NF = 1ULL << EVEX_NFShift
861 };
862 
863 /// \returns true if the instruction with given opcode is a prefix.
864 inline bool isPrefix(uint64_t TSFlags) {
865   return (TSFlags & X86II::FormMask) == PrefixByte;
866 }
867 
868 /// \returns true if the instruction with given opcode is a pseudo.
869 inline bool isPseudo(uint64_t TSFlags) {
870   return (TSFlags & X86II::FormMask) == Pseudo;
871 }
872 
873 /// \returns the "base" X86 opcode for the specified machine
874 /// instruction.
875 inline uint8_t getBaseOpcodeFor(uint64_t TSFlags) {
876   return TSFlags >> X86II::OpcodeShift;
877 }
878 
879 inline bool hasImm(uint64_t TSFlags) { return (TSFlags & X86II::ImmMask) != 0; }
880 
881 /// Decode the "size of immediate" field from the TSFlags field of the
882 /// specified instruction.
883 inline unsigned getSizeOfImm(uint64_t TSFlags) {
884   switch (TSFlags & X86II::ImmMask) {
885   default:
886     llvm_unreachable("Unknown immediate size");
887   case X86II::Imm8:
888   case X86II::Imm8PCRel:
889   case X86II::Imm8Reg:
890     return 1;
891   case X86II::Imm16:
892   case X86II::Imm16PCRel:
893     return 2;
894   case X86II::Imm32:
895   case X86II::Imm32S:
896   case X86II::Imm32PCRel:
897     return 4;
898   case X86II::Imm64:
899     return 8;
900   }
901 }
902 
903 /// \returns true if the immediate of the specified instruction's TSFlags
904 /// indicates that it is pc relative.
905 inline bool isImmPCRel(uint64_t TSFlags) {
906   switch (TSFlags & X86II::ImmMask) {
907   default:
908     llvm_unreachable("Unknown immediate size");
909   case X86II::Imm8PCRel:
910   case X86II::Imm16PCRel:
911   case X86II::Imm32PCRel:
912     return true;
913   case X86II::Imm8:
914   case X86II::Imm8Reg:
915   case X86II::Imm16:
916   case X86II::Imm32:
917   case X86II::Imm32S:
918   case X86II::Imm64:
919     return false;
920   }
921 }
922 
923 /// \returns true if the immediate of the specified instruction's
924 /// TSFlags indicates that it is signed.
925 inline bool isImmSigned(uint64_t TSFlags) {
926   switch (TSFlags & X86II::ImmMask) {
927   default:
928     llvm_unreachable("Unknown immediate signedness");
929   case X86II::Imm32S:
930     return true;
931   case X86II::Imm8:
932   case X86II::Imm8PCRel:
933   case X86II::Imm8Reg:
934   case X86II::Imm16:
935   case X86II::Imm16PCRel:
936   case X86II::Imm32:
937   case X86II::Imm32PCRel:
938   case X86II::Imm64:
939     return false;
940   }
941 }
942 
943 /// Compute whether all of the def operands are repeated in the uses and
944 /// therefore should be skipped.
945 /// This determines the start of the unique operand list. We need to determine
946 /// if all of the defs have a corresponding tied operand in the uses.
947 /// Unfortunately, the tied operand information is encoded in the uses not
948 /// the defs so we have to use some heuristics to find which operands to
949 /// query.
950 inline unsigned getOperandBias(const MCInstrDesc &Desc) {
951   unsigned NumDefs = Desc.getNumDefs();
952   unsigned NumOps = Desc.getNumOperands();
953   switch (NumDefs) {
954   default:
955     llvm_unreachable("Unexpected number of defs");
956   case 0:
957     return 0;
958   case 1:
959     // Common two addr case.
960     if (NumOps > 1 && Desc.getOperandConstraint(1, MCOI::TIED_TO) == 0)
961       return 1;
962     // Check for AVX-512 scatter which has a TIED_TO in the second to last
963     // operand.
964     if (NumOps == 8 && Desc.getOperandConstraint(6, MCOI::TIED_TO) == 0)
965       return 1;
966     return 0;
967   case 2:
968     // XCHG/XADD have two destinations and two sources.
969     if (NumOps >= 4 && Desc.getOperandConstraint(2, MCOI::TIED_TO) == 0 &&
970         Desc.getOperandConstraint(3, MCOI::TIED_TO) == 1)
971       return 2;
972     // Check for gather. AVX-512 has the second tied operand early. AVX2
973     // has it as the last op.
974     if (NumOps == 9 && Desc.getOperandConstraint(2, MCOI::TIED_TO) == 0 &&
975         (Desc.getOperandConstraint(3, MCOI::TIED_TO) == 1 ||
976          Desc.getOperandConstraint(8, MCOI::TIED_TO) == 1))
977       return 2;
978     return 0;
979   }
980 }
981 
982 /// \returns true if the instruction has a NDD (new data destination).
983 inline bool hasNewDataDest(uint64_t TSFlags) {
984   return (TSFlags & X86II::OpMapMask) == X86II::T_MAP4 &&
985          (TSFlags & X86II::EVEX_B) && (TSFlags & X86II::VEX_4V);
986 }
987 
988 /// \returns operand # for the first field of the memory operand or -1 if no
989 /// memory operands.
990 /// NOTE: This ignores tied operands.  If there is a tied register which is
991 /// duplicated in the MCInst (e.g. "EAX = addl EAX, [mem]") it is only counted
992 /// as one operand.
993 inline int getMemoryOperandNo(uint64_t TSFlags) {
994   bool HasVEX_4V = TSFlags & X86II::VEX_4V;
995   bool HasEVEX_K = TSFlags & X86II::EVEX_K;
996 
997   switch (TSFlags & X86II::FormMask) {
998   default:
999     llvm_unreachable("Unknown FormMask value in getMemoryOperandNo!");
1000   case X86II::Pseudo:
1001   case X86II::RawFrm:
1002   case X86II::AddRegFrm:
1003   case X86II::RawFrmImm8:
1004   case X86II::RawFrmImm16:
1005   case X86II::RawFrmMemOffs:
1006   case X86II::RawFrmSrc:
1007   case X86II::RawFrmDst:
1008   case X86II::RawFrmDstSrc:
1009   case X86II::AddCCFrm:
1010   case X86II::PrefixByte:
1011     return -1;
1012   case X86II::MRMDestMem:
1013   case X86II::MRMDestMemFSIB:
1014     return hasNewDataDest(TSFlags);
1015   case X86II::MRMSrcMem:
1016   case X86II::MRMSrcMemFSIB:
1017     // Start from 1, skip any registers encoded in VEX_VVVV or I8IMM, or a
1018     // mask register.
1019     return 1 + HasVEX_4V + HasEVEX_K;
1020   case X86II::MRMSrcMem4VOp3:
1021     // Skip registers encoded in reg.
1022     return 1 + HasEVEX_K;
1023   case X86II::MRMSrcMemOp4:
1024     // Skip registers encoded in reg, VEX_VVVV, and I8IMM.
1025     return 3;
1026   case X86II::MRMSrcMemCC:
1027   case X86II::MRMDestMem4VOp3CC:
1028     // Start from 1, skip any registers encoded in VEX_VVVV or I8IMM, or a
1029     // mask register.
1030     return 1;
1031   case X86II::MRMDestReg:
1032   case X86II::MRMSrcReg:
1033   case X86II::MRMSrcReg4VOp3:
1034   case X86II::MRMSrcRegOp4:
1035   case X86II::MRMSrcRegCC:
1036   case X86II::MRMXrCC:
1037   case X86II::MRMr0:
1038   case X86II::MRMXr:
1039   case X86II::MRM0r:
1040   case X86II::MRM1r:
1041   case X86II::MRM2r:
1042   case X86II::MRM3r:
1043   case X86II::MRM4r:
1044   case X86II::MRM5r:
1045   case X86II::MRM6r:
1046   case X86II::MRM7r:
1047     return -1;
1048   case X86II::MRM0X:
1049   case X86II::MRM1X:
1050   case X86II::MRM2X:
1051   case X86II::MRM3X:
1052   case X86II::MRM4X:
1053   case X86II::MRM5X:
1054   case X86II::MRM6X:
1055   case X86II::MRM7X:
1056     return -1;
1057   case X86II::MRMXmCC:
1058   case X86II::MRMXm:
1059   case X86II::MRM0m:
1060   case X86II::MRM1m:
1061   case X86II::MRM2m:
1062   case X86II::MRM3m:
1063   case X86II::MRM4m:
1064   case X86II::MRM5m:
1065   case X86II::MRM6m:
1066   case X86II::MRM7m:
1067     // Start from 0, skip registers encoded in VEX_VVVV or a mask register.
1068     return 0 + HasVEX_4V + HasEVEX_K;
1069   case X86II::MRM_C0:
1070   case X86II::MRM_C1:
1071   case X86II::MRM_C2:
1072   case X86II::MRM_C3:
1073   case X86II::MRM_C4:
1074   case X86II::MRM_C5:
1075   case X86II::MRM_C6:
1076   case X86II::MRM_C7:
1077   case X86II::MRM_C8:
1078   case X86II::MRM_C9:
1079   case X86II::MRM_CA:
1080   case X86II::MRM_CB:
1081   case X86II::MRM_CC:
1082   case X86II::MRM_CD:
1083   case X86II::MRM_CE:
1084   case X86II::MRM_CF:
1085   case X86II::MRM_D0:
1086   case X86II::MRM_D1:
1087   case X86II::MRM_D2:
1088   case X86II::MRM_D3:
1089   case X86II::MRM_D4:
1090   case X86II::MRM_D5:
1091   case X86II::MRM_D6:
1092   case X86II::MRM_D7:
1093   case X86II::MRM_D8:
1094   case X86II::MRM_D9:
1095   case X86II::MRM_DA:
1096   case X86II::MRM_DB:
1097   case X86II::MRM_DC:
1098   case X86II::MRM_DD:
1099   case X86II::MRM_DE:
1100   case X86II::MRM_DF:
1101   case X86II::MRM_E0:
1102   case X86II::MRM_E1:
1103   case X86II::MRM_E2:
1104   case X86II::MRM_E3:
1105   case X86II::MRM_E4:
1106   case X86II::MRM_E5:
1107   case X86II::MRM_E6:
1108   case X86II::MRM_E7:
1109   case X86II::MRM_E8:
1110   case X86II::MRM_E9:
1111   case X86II::MRM_EA:
1112   case X86II::MRM_EB:
1113   case X86II::MRM_EC:
1114   case X86II::MRM_ED:
1115   case X86II::MRM_EE:
1116   case X86II::MRM_EF:
1117   case X86II::MRM_F0:
1118   case X86II::MRM_F1:
1119   case X86II::MRM_F2:
1120   case X86II::MRM_F3:
1121   case X86II::MRM_F4:
1122   case X86II::MRM_F5:
1123   case X86II::MRM_F6:
1124   case X86II::MRM_F7:
1125   case X86II::MRM_F8:
1126   case X86II::MRM_F9:
1127   case X86II::MRM_FA:
1128   case X86II::MRM_FB:
1129   case X86II::MRM_FC:
1130   case X86II::MRM_FD:
1131   case X86II::MRM_FE:
1132   case X86II::MRM_FF:
1133     return -1;
1134   }
1135 }
1136 
1137 /// \returns true if the register is a XMM.
1138 inline bool isXMMReg(unsigned RegNo) {
1139   assert(X86::XMM15 - X86::XMM0 == 15 &&
1140          "XMM0-15 registers are not continuous");
1141   assert(X86::XMM31 - X86::XMM16 == 15 &&
1142          "XMM16-31 registers are not continuous");
1143   return (RegNo >= X86::XMM0 && RegNo <= X86::XMM15) ||
1144          (RegNo >= X86::XMM16 && RegNo <= X86::XMM31);
1145 }
1146 
1147 /// \returns true if the register is a YMM.
1148 inline bool isYMMReg(unsigned RegNo) {
1149   assert(X86::YMM15 - X86::YMM0 == 15 &&
1150          "YMM0-15 registers are not continuous");
1151   assert(X86::YMM31 - X86::YMM16 == 15 &&
1152          "YMM16-31 registers are not continuous");
1153   return (RegNo >= X86::YMM0 && RegNo <= X86::YMM15) ||
1154          (RegNo >= X86::YMM16 && RegNo <= X86::YMM31);
1155 }
1156 
1157 /// \returns true if the register is a ZMM.
1158 inline bool isZMMReg(unsigned RegNo) {
1159   assert(X86::ZMM31 - X86::ZMM0 == 31 && "ZMM registers are not continuous");
1160   return RegNo >= X86::ZMM0 && RegNo <= X86::ZMM31;
1161 }
1162 
1163 /// \returns true if \p RegNo is an apx extended register.
1164 inline bool isApxExtendedReg(unsigned RegNo) {
1165   assert(X86::R31WH - X86::R16 == 95 && "EGPRs are not continuous");
1166   return RegNo >= X86::R16 && RegNo <= X86::R31WH;
1167 }
1168 
1169 /// \returns true if the MachineOperand is a x86-64 extended (r8 or
1170 /// higher) register,  e.g. r8, xmm8, xmm13, etc.
1171 inline bool isX86_64ExtendedReg(unsigned RegNo) {
1172   if ((RegNo >= X86::XMM8 && RegNo <= X86::XMM15) ||
1173       (RegNo >= X86::XMM16 && RegNo <= X86::XMM31) ||
1174       (RegNo >= X86::YMM8 && RegNo <= X86::YMM15) ||
1175       (RegNo >= X86::YMM16 && RegNo <= X86::YMM31) ||
1176       (RegNo >= X86::ZMM8 && RegNo <= X86::ZMM31))
1177     return true;
1178 
1179   if (isApxExtendedReg(RegNo))
1180     return true;
1181 
1182   switch (RegNo) {
1183   default:
1184     break;
1185   case X86::R8:
1186   case X86::R9:
1187   case X86::R10:
1188   case X86::R11:
1189   case X86::R12:
1190   case X86::R13:
1191   case X86::R14:
1192   case X86::R15:
1193   case X86::R8D:
1194   case X86::R9D:
1195   case X86::R10D:
1196   case X86::R11D:
1197   case X86::R12D:
1198   case X86::R13D:
1199   case X86::R14D:
1200   case X86::R15D:
1201   case X86::R8W:
1202   case X86::R9W:
1203   case X86::R10W:
1204   case X86::R11W:
1205   case X86::R12W:
1206   case X86::R13W:
1207   case X86::R14W:
1208   case X86::R15W:
1209   case X86::R8B:
1210   case X86::R9B:
1211   case X86::R10B:
1212   case X86::R11B:
1213   case X86::R12B:
1214   case X86::R13B:
1215   case X86::R14B:
1216   case X86::R15B:
1217   case X86::CR8:
1218   case X86::CR9:
1219   case X86::CR10:
1220   case X86::CR11:
1221   case X86::CR12:
1222   case X86::CR13:
1223   case X86::CR14:
1224   case X86::CR15:
1225   case X86::DR8:
1226   case X86::DR9:
1227   case X86::DR10:
1228   case X86::DR11:
1229   case X86::DR12:
1230   case X86::DR13:
1231   case X86::DR14:
1232   case X86::DR15:
1233     return true;
1234   }
1235   return false;
1236 }
1237 
1238 inline bool canUseApxExtendedReg(const MCInstrDesc &Desc) {
1239   uint64_t TSFlags = Desc.TSFlags;
1240   uint64_t Encoding = TSFlags & EncodingMask;
1241   // EVEX can always use egpr.
1242   if (Encoding == X86II::EVEX)
1243     return true;
1244 
1245   // To be conservative, egpr is not used for all pseudo instructions
1246   // because we are not sure what instruction it will become.
1247   // FIXME: Could we improve it in X86ExpandPseudo?
1248   if (isPseudo(TSFlags))
1249     return false;
1250 
1251   // MAP OB/TB in legacy encoding space can always use egpr except
1252   // XSAVE*/XRSTOR*.
1253   unsigned Opcode = Desc.Opcode;
1254   switch (Opcode) {
1255   default:
1256     break;
1257   case X86::XSAVE:
1258   case X86::XSAVE64:
1259   case X86::XSAVEOPT:
1260   case X86::XSAVEOPT64:
1261   case X86::XSAVEC:
1262   case X86::XSAVEC64:
1263   case X86::XSAVES:
1264   case X86::XSAVES64:
1265   case X86::XRSTOR:
1266   case X86::XRSTOR64:
1267   case X86::XRSTORS:
1268   case X86::XRSTORS64:
1269     return false;
1270   }
1271   uint64_t OpMap = TSFlags & X86II::OpMapMask;
1272   return !Encoding && (OpMap == X86II::OB || OpMap == X86II::TB);
1273 }
1274 
1275 /// \returns true if the MemoryOperand is a 32 extended (zmm16 or higher)
1276 /// registers, e.g. zmm21, etc.
1277 static inline bool is32ExtendedReg(unsigned RegNo) {
1278   return ((RegNo >= X86::XMM16 && RegNo <= X86::XMM31) ||
1279           (RegNo >= X86::YMM16 && RegNo <= X86::YMM31) ||
1280           (RegNo >= X86::ZMM16 && RegNo <= X86::ZMM31));
1281 }
1282 
1283 inline bool isX86_64NonExtLowByteReg(unsigned reg) {
1284   return (reg == X86::SPL || reg == X86::BPL || reg == X86::SIL ||
1285           reg == X86::DIL);
1286 }
1287 
1288 /// \returns true if this is a masked instruction.
1289 inline bool isKMasked(uint64_t TSFlags) {
1290   return (TSFlags & X86II::EVEX_K) != 0;
1291 }
1292 
1293 /// \returns true if this is a merge masked instruction.
1294 inline bool isKMergeMasked(uint64_t TSFlags) {
1295   return isKMasked(TSFlags) && (TSFlags & X86II::EVEX_Z) == 0;
1296 }
1297 } // namespace X86II
1298 } // namespace llvm
1299 #endif
1300