1 //===-- X86BaseInfo.h - Top level definitions for X86 -------- --*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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..
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_LIB_TARGET_X86_MCTARGETDESC_X86BASEINFO_H
17 #define LLVM_LIB_TARGET_X86_MCTARGETDESC_X86BASEINFO_H
19 #include "X86MCTargetDesc.h"
20 #include "llvm/MC/MCInstrDesc.h"
21 #include "llvm/Support/DataTypes.h"
22 #include "llvm/Support/ErrorHandling.h"
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]
33 // The operand # of the segment in the memory operand.
35 // Total number of operands in a memory reference.
39 /// AVX512 static rounding constants. These need to match the values in
41 enum STATIC_ROUNDING
{
50 /// The constants to describe instr prefixes if there are
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,
60 IP_USE_REX2
= 1U << 7,
62 IP_USE_VEX2
= 1U << 9,
63 IP_USE_VEX3
= 1U << 10,
64 IP_USE_EVEX
= 1U << 11,
65 IP_USE_DISP8
= 1U << 12,
66 IP_USE_DISP32
= 1U << 13,
69 enum OperandType
: unsigned {
70 // AVX512 embedded rounding control. This should only have values 0-3.
71 OPERAND_ROUNDING_CONTROL
= MCOI::OPERAND_FIRST_TARGET
,
75 // X86 specific condition code. These correspond to X86_*_COND in
76 // X86InstrInfo.td. They must be kept in synch.
94 LAST_VALID_COND
= COND_G
,
95 // Artificial condition codes. These are used by analyzeBranch
96 // to indicate a block terminated with two conditional branches that together
97 // form a compound condition. They occur in code using FCMP_OEQ or FCMP_UNE,
98 // which can't be represented on x86 with a single condition. These
99 // are never used in MachineInstrs and are inverses of one another.
105 // The classification for the first instruction in macro fusion.
106 // FIXME: Zen 3 support branch fusion for OR/XOR.
107 enum class FirstMacroFusionInstKind
{
113 Invalid
// Not valid as a first macro fusion instruction
116 enum class SecondMacroFusionInstKind
{
117 AB
, // JA, JB and variants
118 ELG
, // JE, JL, JG and variants
119 SPO
, // JS, JP, JO and variants
120 Invalid
, // Not a fusible jump.
123 /// \returns the type of the first instruction in macro-fusion.
124 // FIXME: Zen 3 support branch fusion for OR/XOR.
125 inline FirstMacroFusionInstKind
126 classifyFirstOpcodeInMacroFusion(unsigned Opcode
) {
129 return FirstMacroFusionInstKind::Invalid
;
141 case X86::TEST64ri32
:
147 return FirstMacroFusionInstKind::Test
;
168 return FirstMacroFusionInstKind::And
;
194 return FirstMacroFusionInstKind::Cmp
;
237 return FirstMacroFusionInstKind::AddSub
;
240 case X86::INC16r_alt
:
242 case X86::INC32r_alt
:
247 case X86::DEC16r_alt
:
249 case X86::DEC32r_alt
:
252 return FirstMacroFusionInstKind::IncDec
;
256 /// \returns the type of the second instruction in macro-fusion.
257 inline SecondMacroFusionInstKind
258 classifySecondCondCodeInMacroFusion(X86::CondCode CC
) {
259 if (CC
== X86::COND_INVALID
)
260 return SecondMacroFusionInstKind::Invalid
;
263 return SecondMacroFusionInstKind::Invalid
;
264 case X86::COND_E
: // JE,JZ
265 case X86::COND_NE
: // JNE,JNZ
266 case X86::COND_L
: // JL,JNGE
267 case X86::COND_LE
: // JLE,JNG
268 case X86::COND_G
: // JG,JNLE
269 case X86::COND_GE
: // JGE,JNL
270 return SecondMacroFusionInstKind::ELG
;
271 case X86::COND_B
: // JB,JC
272 case X86::COND_BE
: // JNA,JBE
273 case X86::COND_A
: // JA,JNBE
274 case X86::COND_AE
: // JAE,JNC,JNB
275 return SecondMacroFusionInstKind::AB
;
276 case X86::COND_S
: // JS
277 case X86::COND_NS
: // JNS
278 case X86::COND_P
: // JP,JPE
279 case X86::COND_NP
: // JNP,JPO
280 case X86::COND_O
: // JO
281 case X86::COND_NO
: // JNO
282 return SecondMacroFusionInstKind::SPO
;
286 /// \param FirstKind kind of the first instruction in macro fusion.
287 /// \param SecondKind kind of the second instruction in macro fusion.
289 /// \returns true if the two instruction can be macro fused.
290 inline bool isMacroFused(FirstMacroFusionInstKind FirstKind
,
291 SecondMacroFusionInstKind SecondKind
) {
293 case X86::FirstMacroFusionInstKind::Test
:
294 case X86::FirstMacroFusionInstKind::And
:
296 case X86::FirstMacroFusionInstKind::Cmp
:
297 case X86::FirstMacroFusionInstKind::AddSub
:
298 return SecondKind
== X86::SecondMacroFusionInstKind::AB
||
299 SecondKind
== X86::SecondMacroFusionInstKind::ELG
;
300 case X86::FirstMacroFusionInstKind::IncDec
:
301 return SecondKind
== X86::SecondMacroFusionInstKind::ELG
;
302 case X86::FirstMacroFusionInstKind::Invalid
:
305 llvm_unreachable("unknown fusion type");
308 /// Defines the possible values of the branch boundary alignment mask.
309 enum AlignBranchBoundaryKind
: uint8_t {
311 AlignBranchFused
= 1U << 0,
312 AlignBranchJcc
= 1U << 1,
313 AlignBranchJmp
= 1U << 2,
314 AlignBranchCall
= 1U << 3,
315 AlignBranchRet
= 1U << 4,
316 AlignBranchIndirect
= 1U << 5
319 /// Defines the encoding values for segment override prefix.
320 enum EncodingOfSegmentOverridePrefix
: uint8_t {
329 /// Given a segment register, return the encoding of the segment override
331 inline EncodingOfSegmentOverridePrefix
332 getSegmentOverridePrefixForReg(MCRegister Reg
) {
335 llvm_unreachable("Unknown segment register!");
353 /// X86II - This namespace holds all of the target specific flags that
354 /// instruction info tracks.
357 /// Target Operand Flag enum.
359 //===------------------------------------------------------------------===//
360 // X86 Specific MachineOperand flags.
362 /// MO_NO_FLAG - No flag for the operand
364 /// MO_GOT_ABSOLUTE_ADDRESS - On a symbol operand, this represents a
366 /// SYMBOL_LABEL + [. - PICBASELABEL]
367 MO_GOT_ABSOLUTE_ADDRESS
,
368 /// MO_PIC_BASE_OFFSET - On a symbol operand this indicates that the
369 /// immediate should get the value of the symbol minus the PIC base label:
370 /// SYMBOL_LABEL - PICBASELABEL
372 /// MO_GOT - On a symbol operand this indicates that the immediate is the
373 /// offset to the GOT entry for the symbol name from the base of the GOT.
374 /// See the X86-64 ELF ABI supplement for more details.
375 /// SYMBOL_LABEL @GOT
377 /// MO_GOTOFF - On a symbol operand this indicates that the immediate is
378 /// the offset to the location of the symbol name from the base of the GOT.
379 /// See the X86-64 ELF ABI supplement for more details.
380 /// SYMBOL_LABEL @GOTOFF
382 /// MO_GOTPCREL - On a symbol operand this indicates that the immediate is
383 /// offset to the GOT entry for the symbol name from the current code
385 /// See the X86-64 ELF ABI supplement for more details.
386 /// SYMBOL_LABEL @GOTPCREL
388 /// MO_GOTPCREL_NORELAX - Same as MO_GOTPCREL except that R_X86_64_GOTPCREL
389 /// relocations are guaranteed to be emitted by the integrated assembler
390 /// instead of the relaxable R_X86_64[_REX]_GOTPCRELX relocations.
392 /// MO_PLT - On a symbol operand this indicates that the immediate is
393 /// offset to the PLT entry of symbol name from the current code location.
394 /// See the X86-64 ELF ABI supplement for more details.
395 /// SYMBOL_LABEL @PLT
397 /// MO_TLSGD - On a symbol operand this indicates that the immediate is
398 /// the offset of the GOT entry with the TLS index structure that contains
399 /// the module number and variable offset for the symbol. Used in the
400 /// general dynamic TLS access model.
401 /// See 'ELF Handling for Thread-Local Storage' for more details.
402 /// SYMBOL_LABEL @TLSGD
404 /// MO_TLSLD - On a symbol operand this indicates that the immediate is
405 /// the offset of the GOT entry with the TLS index for the module that
406 /// contains the symbol. When this index is passed to a call to
407 /// __tls_get_addr, the function will return the base address of the TLS
408 /// block for the symbol. Used in the x86-64 local dynamic TLS access model.
409 /// See 'ELF Handling for Thread-Local Storage' for more details.
410 /// SYMBOL_LABEL @TLSLD
412 /// MO_TLSLDM - On a symbol operand this indicates that the immediate is
413 /// the offset of the GOT entry with the TLS index for the module that
414 /// contains the symbol. When this index is passed to a call to
415 /// ___tls_get_addr, the function will return the base address of the TLS
416 /// block for the symbol. Used in the IA32 local dynamic TLS access model.
417 /// See 'ELF Handling for Thread-Local Storage' for more details.
418 /// SYMBOL_LABEL @TLSLDM
420 /// MO_GOTTPOFF - On a symbol operand this indicates that the immediate is
421 /// the offset of the GOT entry with the thread-pointer offset for the
422 /// symbol. Used in the x86-64 initial exec TLS access model.
423 /// See 'ELF Handling for Thread-Local Storage' for more details.
424 /// SYMBOL_LABEL @GOTTPOFF
426 /// MO_INDNTPOFF - On a symbol operand this indicates that the immediate is
427 /// the absolute address of the GOT entry with the negative thread-pointer
428 /// offset for the symbol. Used in the non-PIC IA32 initial exec TLS access
430 /// See 'ELF Handling for Thread-Local Storage' for more details.
431 /// SYMBOL_LABEL @INDNTPOFF
433 /// MO_TPOFF - On a symbol operand this indicates that the immediate is
434 /// the thread-pointer offset for the symbol. Used in the x86-64 local
435 /// exec TLS access model.
436 /// See 'ELF Handling for Thread-Local Storage' for more details.
437 /// SYMBOL_LABEL @TPOFF
439 /// MO_DTPOFF - On a symbol operand this indicates that the immediate is
440 /// the offset of the GOT entry with the TLS offset of the symbol. Used
441 /// in the local dynamic TLS access model.
442 /// See 'ELF Handling for Thread-Local Storage' for more details.
443 /// SYMBOL_LABEL @DTPOFF
445 /// MO_NTPOFF - On a symbol operand this indicates that the immediate is
446 /// the negative thread-pointer offset for the symbol. Used in the IA32
447 /// local exec TLS access model.
448 /// See 'ELF Handling for Thread-Local Storage' for more details.
449 /// SYMBOL_LABEL @NTPOFF
451 /// MO_GOTNTPOFF - On a symbol operand this indicates that the immediate is
452 /// the offset of the GOT entry with the negative thread-pointer offset for
453 /// the symbol. Used in the PIC IA32 initial exec TLS access model.
454 /// See 'ELF Handling for Thread-Local Storage' for more details.
455 /// SYMBOL_LABEL @GOTNTPOFF
457 /// MO_DLLIMPORT - On a symbol operand "FOO", this indicates that the
458 /// reference is actually to the "__imp_FOO" symbol. This is used for
459 /// dllimport linkage on windows.
461 /// MO_DARWIN_NONLAZY - On a symbol operand "FOO", this indicates that the
462 /// reference is actually to the "FOO$non_lazy_ptr" symbol, which is a
463 /// non-PIC-base-relative reference to a non-hidden dyld lazy pointer stub.
465 /// MO_DARWIN_NONLAZY_PIC_BASE - On a symbol operand "FOO", this indicates
466 /// that the reference is actually to "FOO$non_lazy_ptr - PICBASE", which is
467 /// a PIC-base-relative reference to a non-hidden dyld lazy pointer stub.
468 MO_DARWIN_NONLAZY_PIC_BASE
,
469 /// MO_TLVP - On a symbol operand this indicates that the immediate is
471 /// This is the TLS offset for the Darwin TLS mechanism.
473 /// MO_TLVP_PIC_BASE - On a symbol operand this indicates that the immediate
474 /// is some TLS offset from the picbase.
475 /// This is the 32-bit TLS offset for Darwin TLS in PIC mode.
477 /// MO_SECREL - On a symbol operand this indicates that the immediate is
478 /// the offset from beginning of section.
479 /// This is the TLS offset for the COFF/Windows TLS mechanism.
481 /// MO_ABS8 - On a symbol operand this indicates that the symbol is known
482 /// to be an absolute symbol in range [0,128), so we can use the @ABS8
485 /// MO_COFFSTUB - On a symbol operand "FOO", this indicates that the
486 /// reference is actually to the ".refptr.FOO" symbol. This is used for
487 /// stub symbols on windows.
492 //===------------------------------------------------------------------===//
493 // Instruction encodings. These are the standard/most common forms for X86
496 /// PseudoFrm - This represents an instruction that is a pseudo instruction
497 /// or one that has not been implemented yet. It is illegal to code generate
498 /// it, but tolerated for intermediate implementation stages.
500 /// Raw - This form is for instructions that don't have any operands, so
501 /// they are just a fixed opcode value, like 'leave'.
503 /// AddRegFrm - This form is used for instructions like 'push r32' that have
504 /// their one register operand added to their opcode.
506 /// RawFrmMemOffs - This form is for instructions that store an absolute
507 /// memory offset as an immediate with a possible segment override.
509 /// RawFrmSrc - This form is for instructions that use the source index
510 /// register SI/ESI/RSI with a possible segment override.
512 /// RawFrmDst - This form is for instructions that use the destination index
513 /// register DI/EDI/RDI.
515 /// RawFrmDstSrc - This form is for instructions that use the source index
516 /// register SI/ESI/RSI with a possible segment override, and also the
517 /// destination index register DI/EDI/RDI.
519 /// RawFrmImm8 - This is used for the ENTER instruction, which has two
520 /// immediates, the first of which is a 16-bit immediate (specified by
521 /// the imm encoding) and the second is a 8-bit fixed value.
523 /// RawFrmImm16 - This is used for CALL FAR instructions, which have two
524 /// immediates, the first of which is a 16 or 32-bit immediate (specified by
525 /// the imm encoding) and the second is a 16-bit fixed value. In the AMD
526 /// manual, this operand is described as pntr16:32 and pntr16:16
528 /// AddCCFrm - This form is used for Jcc that encode the condition code
529 /// in the lower 4 bits of the opcode.
531 /// PrefixByte - This form is used for instructions that represent a prefix
532 /// byte like data16 or rep.
534 /// MRMDestRegCC - This form is used for the cfcmov instructions, which use
535 /// the Mod/RM byte to specify the operands reg(r/m) and reg(reg) and also
536 /// encodes a condition code.
538 /// MRMDestMemCC - This form is used for the cfcmov instructions, which use
539 /// the Mod/RM byte to specify the operands mem(r/m) and reg(reg) and also
540 /// encodes a condition code.
542 /// MRMDestMem4VOp3CC - This form is used for instructions that use the Mod/RM
543 /// byte to specify a destination which in this case is memory and operand 3
544 /// with VEX.VVVV, and also encodes a condition code.
545 MRMDestMem4VOp3CC
= 20,
546 /// Instructions operate on a register Reg/Opcode operand not the r/m field.
548 /// MRMSrcMem - But force to use the SIB field.
550 /// MRMDestMem - But force to use the SIB field.
552 /// MRMDestMem - This form is used for instructions that use the Mod/RM byte
553 /// to specify a destination, which in this case is memory.
555 /// MRMSrcMem - This form is used for instructions that use the Mod/RM byte
556 /// to specify a source, which in this case is memory.
558 /// MRMSrcMem4VOp3 - This form is used for instructions that encode
559 /// operand 3 with VEX.VVVV and load from memory.
561 /// MRMSrcMemOp4 - This form is used for instructions that use the Mod/RM
562 /// byte to specify the fourth source, which in this case is memory.
564 /// MRMSrcMemCC - This form is used for instructions that use the Mod/RM
565 /// byte to specify the operands and also encodes a condition code.
567 /// MRMXm - This form is used for instructions that use the Mod/RM byte
568 /// to specify a memory source, but doesn't use the middle field. And has
569 /// a condition code.
571 /// MRMXm - This form is used for instructions that use the Mod/RM byte
572 /// to specify a memory source, but doesn't use the middle field.
574 /// MRM0m-MRM7m - Instructions that operate on a memory r/m operand and use
575 /// reg field to hold extended opcode, which is represented as /0, /1, ...
576 MRM0m
= 32, // Format /0
577 MRM1m
= 33, // Format /1
578 MRM2m
= 34, // Format /2
579 MRM3m
= 35, // Format /3
580 MRM4m
= 36, // Format /4
581 MRM5m
= 37, // Format /5
582 MRM6m
= 38, // Format /6
583 MRM7m
= 39, // Format /7
584 /// MRMDestReg - This form is used for instructions that use the Mod/RM byte
585 /// to specify a destination, which in this case is a register.
587 /// MRMSrcReg - This form is used for instructions that use the Mod/RM byte
588 /// to specify a source, which in this case is a register.
590 /// MRMSrcReg4VOp3 - This form is used for instructions that encode
591 /// operand 3 with VEX.VVVV and do not load from memory.
593 /// MRMSrcRegOp4 - This form is used for instructions that use the Mod/RM
594 /// byte to specify the fourth source, which in this case is a register.
596 /// MRMSrcRegCC - This form is used for instructions that use the Mod/RM
597 /// byte to specify the operands and also encodes a condition code
599 /// MRMXCCr - This form is used for instructions that use the Mod/RM byte
600 /// to specify a register source, but doesn't use the middle field. And has
601 /// a condition code.
603 /// MRMXr - This form is used for instructions that use the Mod/RM byte
604 /// to specify a register source, but doesn't use the middle field.
606 /// MRM0r-MRM7r - Instructions that operate on a register r/m operand and use
607 /// reg field to hold extended opcode, which is represented as /0, /1, ...
608 MRM0r
= 48, // Format /0
609 MRM1r
= 49, // Format /1
610 MRM2r
= 50, // Format /2
611 MRM3r
= 51, // Format /3
612 MRM4r
= 52, // Format /4
613 MRM5r
= 53, // Format /5
614 MRM6r
= 54, // Format /6
615 MRM7r
= 55, // Format /7
616 /// MRM0X-MRM7X - Instructions that operate that have mod=11 and an opcode but
618 MRM0X
= 56, // Format /0
619 MRM1X
= 57, // Format /1
620 MRM2X
= 58, // Format /2
621 MRM3X
= 59, // Format /3
622 MRM4X
= 60, // Format /4
623 MRM5X
= 61, // Format /5
624 MRM6X
= 62, // Format /6
625 MRM7X
= 63, // Format /7
626 /// MRM_XX (XX: C0-FF)- A mod/rm byte of exactly 0xXX.
692 //===------------------------------------------------------------------===//
694 /// OpSize - OpSizeFixed implies instruction never needs a 0x66 prefix.
695 /// OpSize16 means this is a 16-bit instruction and needs 0x66 prefix in
696 /// 32-bit mode. OpSize32 means this is a 32-bit instruction needs a 0x66
697 /// prefix in 16-bit mode.
699 OpSizeMask
= 0x3 << OpSizeShift
,
700 OpSizeFixed
= 0 << OpSizeShift
,
701 OpSize16
= 1 << OpSizeShift
,
702 OpSize32
= 2 << OpSizeShift
,
703 /// AsSize - AdSizeX implies this instruction determines its need of 0x67
704 /// prefix from a normal ModRM memory operand. The other types indicate that
705 /// an operand is encoded with a specific width and a prefix is needed if
706 /// it differs from the current mode.
707 AdSizeShift
= OpSizeShift
+ 2,
708 AdSizeMask
= 0x3 << AdSizeShift
,
709 AdSizeX
= 0 << AdSizeShift
,
710 AdSize16
= 1 << AdSizeShift
,
711 AdSize32
= 2 << AdSizeShift
,
712 AdSize64
= 3 << AdSizeShift
,
713 //===------------------------------------------------------------------===//
714 /// OpPrefix - There are several prefix bytes that are used as opcode
715 /// extensions. These are 0x66, 0xF3, and 0xF2. If this field is 0 there is
717 OpPrefixShift
= AdSizeShift
+ 2,
718 OpPrefixMask
= 0x3 << OpPrefixShift
,
719 /// PD - Prefix code for packed double precision vector floating point
720 /// operations performed in the SSE registers.
721 PD
= 1 << OpPrefixShift
,
722 /// XS, XD - These prefix codes are for single and double precision scalar
723 /// floating point operations performed in the SSE registers.
724 XS
= 2 << OpPrefixShift
,
725 XD
= 3 << OpPrefixShift
,
726 //===------------------------------------------------------------------===//
727 /// OpMap - This field determines which opcode map this instruction
728 /// belongs to. i.e. one-byte, two-byte, 0x0f 0x38, 0x0f 0x3a, etc.
729 OpMapShift
= OpPrefixShift
+ 2,
730 OpMapMask
= 0xF << OpMapShift
,
731 /// OB - OneByte - Set if this instruction has a one byte opcode.
732 OB
= 0 << OpMapShift
,
733 /// TB - TwoByte - Set if this instruction has a two byte opcode, which
734 /// starts with a 0x0F byte before the real opcode.
735 TB
= 1 << OpMapShift
,
736 /// T8, TA - Prefix after the 0x0F prefix.
737 T8
= 2 << OpMapShift
,
738 TA
= 3 << OpMapShift
,
739 /// XOP8 - Prefix to include use of imm byte.
740 XOP8
= 4 << OpMapShift
,
741 /// XOP9 - Prefix to exclude use of imm byte.
742 XOP9
= 5 << OpMapShift
,
743 /// XOPA - Prefix to encode 0xA in VEX.MMMM of XOP instructions.
744 XOPA
= 6 << OpMapShift
,
745 /// ThreeDNow - This indicates that the instruction uses the
746 /// wacky 0x0F 0x0F prefix for 3DNow! instructions. The manual documents
747 /// this as having a 0x0F prefix with a 0x0F opcode, and each instruction
748 /// storing a classifier in the imm8 field. To simplify our implementation,
749 /// we handle this by storeing the classifier in the opcode field and using
750 /// this flag to indicate that the encoder should do the wacky 3DNow! thing.
751 ThreeDNow
= 7 << OpMapShift
,
752 /// MAP4, MAP5, MAP6, MAP7 - Prefix after the 0x0F prefix.
753 T_MAP4
= 8 << OpMapShift
,
754 T_MAP5
= 9 << OpMapShift
,
755 T_MAP6
= 10 << OpMapShift
,
756 T_MAP7
= 11 << OpMapShift
,
757 //===------------------------------------------------------------------===//
758 /// REX_W - REX prefixes are instruction prefixes used in 64-bit mode.
759 /// They are used to specify GPRs and SSE registers, 64-bit operand size,
760 /// etc. We only cares about REX.W and REX.R bits and only the former is
761 /// statically determined.
762 REXShift
= OpMapShift
+ 4,
763 REX_W
= 1 << REXShift
,
764 //===------------------------------------------------------------------===//
765 // This 4-bit field describes the size of an immediate operand. Zero is
766 // unused so that we can tell if we forgot to set a value.
767 ImmShift
= REXShift
+ 1,
768 Imm8
= 1 << ImmShift
,
769 Imm8PCRel
= 2 << ImmShift
,
770 Imm8Reg
= 3 << ImmShift
,
771 Imm16
= 4 << ImmShift
,
772 Imm16PCRel
= 5 << ImmShift
,
773 Imm32
= 6 << ImmShift
,
774 Imm32PCRel
= 7 << ImmShift
,
775 Imm32S
= 8 << ImmShift
,
776 Imm64
= 9 << ImmShift
,
777 ImmMask
= 15 << ImmShift
,
778 //===------------------------------------------------------------------===//
779 /// FP Instruction Classification... Zero is non-fp instruction.
780 /// FPTypeMask - Mask for all of the FP types...
781 FPTypeShift
= ImmShift
+ 4,
782 FPTypeMask
= 7 << FPTypeShift
,
783 /// NotFP - The default, set for instructions that do not use FP registers.
784 NotFP
= 0 << FPTypeShift
,
785 /// ZeroArgFP - 0 arg FP instruction which implicitly pushes ST(0), f.e. fld0
786 ZeroArgFP
= 1 << FPTypeShift
,
787 /// OneArgFP - 1 arg FP instructions which implicitly read ST(0), such as fst
788 OneArgFP
= 2 << FPTypeShift
,
789 /// OneArgFPRW - 1 arg FP instruction which implicitly read ST(0) and write a
790 /// result back to ST(0). For example, fcos, fsqrt, etc.
791 OneArgFPRW
= 3 << FPTypeShift
,
792 /// TwoArgFP - 2 arg FP instructions which implicitly read ST(0), and an
793 /// explicit argument, storing the result to either ST(0) or the implicit
794 /// argument. For example: fadd, fsub, fmul, etc...
795 TwoArgFP
= 4 << FPTypeShift
,
796 /// CompareFP - 2 arg FP instructions which implicitly read ST(0) and an
797 /// explicit argument, but have no destination. Example: fucom, fucomi, ...
798 CompareFP
= 5 << FPTypeShift
,
799 /// CondMovFP - "2 operand" floating point conditional move instructions.
800 CondMovFP
= 6 << FPTypeShift
,
801 /// SpecialFP - Special instruction forms. Dispatch by opcode explicitly.
802 SpecialFP
= 7 << FPTypeShift
,
804 LOCKShift
= FPTypeShift
+ 3,
805 LOCK
= 1 << LOCKShift
,
807 REPShift
= LOCKShift
+ 1,
809 /// Execution domain for SSE instructions.
810 /// 0 means normal, non-SSE instruction.
811 SSEDomainShift
= REPShift
+ 1,
813 EncodingShift
= SSEDomainShift
+ 2,
814 EncodingMask
= 0x3 << EncodingShift
,
815 /// LEGACY - encoding using REX/REX2 or w/o opcode prefix.
816 LEGACY
= 0 << EncodingShift
,
817 /// VEX - encoding using 0xC4/0xC5
818 VEX
= 1 << EncodingShift
,
819 /// XOP - Opcode prefix used by XOP instructions.
820 XOP
= 2 << EncodingShift
,
821 /// EVEX - Specifies that this instruction use EVEX form which provides
822 /// syntax support up to 32 512-bit register operands and up to 7 16-bit
823 /// mask operands as well as source operand data swizzling/memory operand
824 /// conversion, eviction hint, and rounding mode.
825 EVEX
= 3 << EncodingShift
,
827 OpcodeShift
= EncodingShift
+ 2,
828 /// VEX_4V - Used to specify an additional AVX/SSE register. Several 2
829 /// address instructions in SSE are represented as 3 address ones in AVX
830 /// and the additional register is encoded in VEX_VVVV prefix.
831 VEX_4VShift
= OpcodeShift
+ 8,
832 VEX_4V
= 1ULL << VEX_4VShift
,
833 /// VEX_L - Stands for a bit in the VEX opcode prefix meaning the current
834 /// instruction uses 256-bit wide registers. This is usually auto detected
835 /// if a VR256 register is used, but some AVX instructions also have this
836 /// field marked when using a f256 memory references.
837 VEX_LShift
= VEX_4VShift
+ 1,
838 VEX_L
= 1ULL << VEX_LShift
,
839 /// EVEX_K - Set if this instruction requires masking
840 EVEX_KShift
= VEX_LShift
+ 1,
841 EVEX_K
= 1ULL << EVEX_KShift
,
842 /// EVEX_Z - Set if this instruction has EVEX.Z field set.
843 EVEX_ZShift
= EVEX_KShift
+ 1,
844 EVEX_Z
= 1ULL << EVEX_ZShift
,
845 /// EVEX_L2 - Set if this instruction has EVEX.L' field set.
846 EVEX_L2Shift
= EVEX_ZShift
+ 1,
847 EVEX_L2
= 1ULL << EVEX_L2Shift
,
848 /// EVEX_B - Set if this instruction has EVEX.B field set.
849 EVEX_BShift
= EVEX_L2Shift
+ 1,
850 EVEX_B
= 1ULL << EVEX_BShift
,
851 /// The scaling factor for the AVX512's 8-bit compressed displacement.
852 CD8_Scale_Shift
= EVEX_BShift
+ 1,
853 CD8_Scale_Mask
= 7ULL << CD8_Scale_Shift
,
854 /// Explicitly specified rounding control
855 EVEX_RCShift
= CD8_Scale_Shift
+ 3,
856 EVEX_RC
= 1ULL << EVEX_RCShift
,
858 NoTrackShift
= EVEX_RCShift
+ 1,
859 NOTRACK
= 1ULL << NoTrackShift
,
860 /// Force REX2/VEX/EVEX encoding
861 ExplicitOpPrefixShift
= NoTrackShift
+ 1,
862 /// For instructions that require REX2 prefix even if EGPR is not used.
863 ExplicitREX2Prefix
= 1ULL << ExplicitOpPrefixShift
,
864 /// For instructions that use VEX encoding only when {vex}, {vex2} or {vex3}
866 ExplicitVEXPrefix
= 2ULL << ExplicitOpPrefixShift
,
867 /// For instructions that are promoted to EVEX space for EGPR.
868 ExplicitEVEXPrefix
= 3ULL << ExplicitOpPrefixShift
,
869 ExplicitOpPrefixMask
= 3ULL << ExplicitOpPrefixShift
,
870 /// EVEX_NF - Set if this instruction has EVEX.NF field set.
871 EVEX_NFShift
= ExplicitOpPrefixShift
+ 2,
872 EVEX_NF
= 1ULL << EVEX_NFShift
,
873 // TwoConditionalOps - Set if this instruction has two conditional operands
874 TwoConditionalOps_Shift
= EVEX_NFShift
+ 1,
875 TwoConditionalOps
= 1ULL << TwoConditionalOps_Shift
,
876 // EVEX_U - Set if this instruction has EVEX.U field set.
877 EVEX_UShift
= TwoConditionalOps_Shift
+ 1,
878 EVEX_U
= 1ULL << EVEX_UShift
881 /// \returns true if the instruction with given opcode is a prefix.
882 inline bool isPrefix(uint64_t TSFlags
) {
883 return (TSFlags
& X86II::FormMask
) == PrefixByte
;
886 /// \returns true if the instruction with given opcode is a pseudo.
887 inline bool isPseudo(uint64_t TSFlags
) {
888 return (TSFlags
& X86II::FormMask
) == Pseudo
;
891 /// \returns the "base" X86 opcode for the specified machine
893 inline uint8_t getBaseOpcodeFor(uint64_t TSFlags
) {
894 return TSFlags
>> X86II::OpcodeShift
;
897 inline bool hasImm(uint64_t TSFlags
) { return (TSFlags
& X86II::ImmMask
) != 0; }
899 /// Decode the "size of immediate" field from the TSFlags field of the
900 /// specified instruction.
901 inline unsigned getSizeOfImm(uint64_t TSFlags
) {
902 switch (TSFlags
& X86II::ImmMask
) {
904 llvm_unreachable("Unknown immediate size");
906 case X86II::Imm8PCRel
:
910 case X86II::Imm16PCRel
:
914 case X86II::Imm32PCRel
:
921 /// \returns true if the immediate of the specified instruction's TSFlags
922 /// indicates that it is pc relative.
923 inline bool isImmPCRel(uint64_t TSFlags
) {
924 switch (TSFlags
& X86II::ImmMask
) {
926 llvm_unreachable("Unknown immediate size");
927 case X86II::Imm8PCRel
:
928 case X86II::Imm16PCRel
:
929 case X86II::Imm32PCRel
:
941 /// \returns true if the immediate of the specified instruction's
942 /// TSFlags indicates that it is signed.
943 inline bool isImmSigned(uint64_t TSFlags
) {
944 switch (TSFlags
& X86II::ImmMask
) {
946 llvm_unreachable("Unknown immediate signedness");
950 case X86II::Imm8PCRel
:
953 case X86II::Imm16PCRel
:
955 case X86II::Imm32PCRel
:
961 /// Compute whether all of the def operands are repeated in the uses and
962 /// therefore should be skipped.
963 /// This determines the start of the unique operand list. We need to determine
964 /// if all of the defs have a corresponding tied operand in the uses.
965 /// Unfortunately, the tied operand information is encoded in the uses not
966 /// the defs so we have to use some heuristics to find which operands to
968 inline unsigned getOperandBias(const MCInstrDesc
&Desc
) {
969 unsigned NumDefs
= Desc
.getNumDefs();
970 unsigned NumOps
= Desc
.getNumOperands();
973 llvm_unreachable("Unexpected number of defs");
977 // Common two addr case.
978 if (NumOps
> 1 && Desc
.getOperandConstraint(1, MCOI::TIED_TO
) == 0)
980 // Check for AVX-512 scatter which has a TIED_TO in the second to last
982 if (NumOps
== 8 && Desc
.getOperandConstraint(6, MCOI::TIED_TO
) == 0)
986 // XCHG/XADD have two destinations and two sources.
987 if (NumOps
>= 4 && Desc
.getOperandConstraint(2, MCOI::TIED_TO
) == 0 &&
988 Desc
.getOperandConstraint(3, MCOI::TIED_TO
) == 1)
990 // Check for gather. AVX-512 has the second tied operand early. AVX2
991 // has it as the last op.
992 if (NumOps
== 9 && Desc
.getOperandConstraint(2, MCOI::TIED_TO
) == 0 &&
993 (Desc
.getOperandConstraint(3, MCOI::TIED_TO
) == 1 ||
994 Desc
.getOperandConstraint(8, MCOI::TIED_TO
) == 1))
1000 /// \returns true if the instruction has a NDD (new data destination).
1001 inline bool hasNewDataDest(uint64_t TSFlags
) {
1002 return (TSFlags
& X86II::OpMapMask
) == X86II::T_MAP4
&&
1003 (TSFlags
& X86II::EVEX_B
) && (TSFlags
& X86II::VEX_4V
);
1006 /// \returns operand # for the first field of the memory operand or -1 if no
1007 /// memory operands.
1008 /// NOTE: This ignores tied operands. If there is a tied register which is
1009 /// duplicated in the MCInst (e.g. "EAX = addl EAX, [mem]") it is only counted
1011 inline int getMemoryOperandNo(uint64_t TSFlags
) {
1012 bool HasVEX_4V
= TSFlags
& X86II::VEX_4V
;
1013 bool HasEVEX_K
= TSFlags
& X86II::EVEX_K
;
1015 switch (TSFlags
& X86II::FormMask
) {
1017 llvm_unreachable("Unknown FormMask value in getMemoryOperandNo!");
1020 case X86II::AddRegFrm
:
1021 case X86II::RawFrmImm8
:
1022 case X86II::RawFrmImm16
:
1023 case X86II::RawFrmMemOffs
:
1024 case X86II::RawFrmSrc
:
1025 case X86II::RawFrmDst
:
1026 case X86II::RawFrmDstSrc
:
1027 case X86II::AddCCFrm
:
1028 case X86II::PrefixByte
:
1030 case X86II::MRMDestMem
:
1031 case X86II::MRMDestMemFSIB
:
1032 case X86II::MRMDestMemCC
:
1033 return hasNewDataDest(TSFlags
);
1034 case X86II::MRMSrcMem
:
1035 case X86II::MRMSrcMemFSIB
:
1036 // Start from 1, skip any registers encoded in VEX_VVVV or I8IMM, or a
1038 return 1 + HasVEX_4V
+ HasEVEX_K
;
1039 case X86II::MRMSrcMem4VOp3
:
1040 // Skip registers encoded in reg.
1041 return 1 + HasEVEX_K
;
1042 case X86II::MRMSrcMemOp4
:
1043 // Skip registers encoded in reg, VEX_VVVV, and I8IMM.
1045 case X86II::MRMSrcMemCC
:
1046 return 1 + hasNewDataDest(TSFlags
);
1047 case X86II::MRMDestMem4VOp3CC
:
1048 // Start from 1, skip any registers encoded in VEX_VVVV or I8IMM, or a
1051 case X86II::MRMDestReg
:
1052 case X86II::MRMDestRegCC
:
1053 case X86II::MRMSrcReg
:
1054 case X86II::MRMSrcReg4VOp3
:
1055 case X86II::MRMSrcRegOp4
:
1056 case X86II::MRMSrcRegCC
:
1057 case X86II::MRMXrCC
:
1078 case X86II::MRMXmCC
:
1088 // Start from 0, skip registers encoded in VEX_VVVV or a mask register.
1089 return 0 + HasVEX_4V
+ HasEVEX_K
;
1158 /// \returns true if the register is a XMM.
1159 inline bool isXMMReg(MCRegister Reg
) {
1160 static_assert(X86::XMM15
- X86::XMM0
== 15,
1161 "XMM0-15 registers are not continuous");
1162 static_assert(X86::XMM31
- X86::XMM16
== 15,
1163 "XMM16-31 registers are not continuous");
1164 return (Reg
>= X86::XMM0
&& Reg
<= X86::XMM15
) ||
1165 (Reg
>= X86::XMM16
&& Reg
<= X86::XMM31
);
1168 /// \returns true if the register is a YMM.
1169 inline bool isYMMReg(MCRegister Reg
) {
1170 static_assert(X86::YMM15
- X86::YMM0
== 15,
1171 "YMM0-15 registers are not continuous");
1172 static_assert(X86::YMM31
- X86::YMM16
== 15,
1173 "YMM16-31 registers are not continuous");
1174 return (Reg
>= X86::YMM0
&& Reg
<= X86::YMM15
) ||
1175 (Reg
>= X86::YMM16
&& Reg
<= X86::YMM31
);
1178 /// \returns true if the register is a ZMM.
1179 inline bool isZMMReg(MCRegister Reg
) {
1180 static_assert(X86::ZMM31
- X86::ZMM0
== 31,
1181 "ZMM registers are not continuous");
1182 return Reg
>= X86::ZMM0
&& Reg
<= X86::ZMM31
;
1185 /// \returns true if \p Reg is an apx extended register.
1186 inline bool isApxExtendedReg(MCRegister Reg
) {
1187 static_assert(X86::R31WH
- X86::R16
== 95, "EGPRs are not continuous");
1188 return Reg
>= X86::R16
&& Reg
<= X86::R31WH
;
1191 /// \returns true if the MachineOperand is a x86-64 extended (r8 or
1192 /// higher) register, e.g. r8, xmm8, xmm13, etc.
1193 inline bool isX86_64ExtendedReg(MCRegister Reg
) {
1194 if ((Reg
>= X86::XMM8
&& Reg
<= X86::XMM15
) ||
1195 (Reg
>= X86::XMM16
&& Reg
<= X86::XMM31
) ||
1196 (Reg
>= X86::YMM8
&& Reg
<= X86::YMM15
) ||
1197 (Reg
>= X86::YMM16
&& Reg
<= X86::YMM31
) ||
1198 (Reg
>= X86::ZMM8
&& Reg
<= X86::ZMM31
))
1201 if (isApxExtendedReg(Reg
))
1260 inline bool canUseApxExtendedReg(const MCInstrDesc
&Desc
) {
1261 uint64_t TSFlags
= Desc
.TSFlags
;
1262 uint64_t Encoding
= TSFlags
& EncodingMask
;
1263 // EVEX can always use egpr.
1264 if (Encoding
== X86II::EVEX
)
1267 unsigned Opcode
= Desc
.Opcode
;
1268 // MOV32r0 is always expanded to XOR32rr
1269 if (Opcode
== X86::MOV32r0
)
1271 // To be conservative, egpr is not used for all pseudo instructions
1272 // because we are not sure what instruction it will become.
1273 // FIXME: Could we improve it in X86ExpandPseudo?
1274 if (isPseudo(TSFlags
))
1277 // MAP OB/TB in legacy encoding space can always use egpr except
1285 case X86::XSAVEOPT64
:
1293 case X86::XRSTORS64
:
1296 uint64_t OpMap
= TSFlags
& X86II::OpMapMask
;
1297 return !Encoding
&& (OpMap
== X86II::OB
|| OpMap
== X86II::TB
);
1300 /// \returns true if the MemoryOperand is a 32 extended (zmm16 or higher)
1301 /// registers, e.g. zmm21, etc.
1302 static inline bool is32ExtendedReg(MCRegister Reg
) {
1303 return ((Reg
>= X86::XMM16
&& Reg
<= X86::XMM31
) ||
1304 (Reg
>= X86::YMM16
&& Reg
<= X86::YMM31
) ||
1305 (Reg
>= X86::ZMM16
&& Reg
<= X86::ZMM31
));
1308 inline bool isX86_64NonExtLowByteReg(MCRegister Reg
) {
1309 return (Reg
== X86::SPL
|| Reg
== X86::BPL
|| Reg
== X86::SIL
||
1313 /// \returns true if this is a masked instruction.
1314 inline bool isKMasked(uint64_t TSFlags
) {
1315 return (TSFlags
& X86II::EVEX_K
) != 0;
1318 /// \returns true if this is a merge masked instruction.
1319 inline bool isKMergeMasked(uint64_t TSFlags
) {
1320 return isKMasked(TSFlags
) && (TSFlags
& X86II::EVEX_Z
) == 0;
1323 /// \returns true if the intruction needs a SIB.
1324 inline bool needSIB(MCRegister BaseReg
, MCRegister IndexReg
, bool In64BitMode
) {
1325 // The SIB byte must be used if there is an index register.
1329 // The SIB byte must be used if the base is ESP/RSP/R12/R20/R28, all of
1330 // which encode to an R/M value of 4, which indicates that a SIB byte is
1332 switch (BaseReg
.id()) {
1334 // If there is no base register and we're in 64-bit mode, we need a SIB
1335 // byte to emit an addr that is just 'disp32' (the non-RIP relative form).
1336 return In64BitMode
&& !BaseReg
;
1349 } // namespace X86II