X86BaseInfo.h 43 KB

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