X86InstrExtension.td 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //===-- X86InstrExtension.td - Sign and Zero Extensions ----*- tablegen -*-===//
  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 describes the sign and zero extension operations.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. let hasSideEffects = 0 in {
  13. let Defs = [AX], Uses = [AL] in // AX = signext(AL)
  14. def CBW : I<0x98, RawFrm, (outs), (ins),
  15. "{cbtw|cbw}", []>, OpSize16, Sched<[WriteALU]>;
  16. let Defs = [EAX], Uses = [AX] in // EAX = signext(AX)
  17. def CWDE : I<0x98, RawFrm, (outs), (ins),
  18. "{cwtl|cwde}", []>, OpSize32, Sched<[WriteALU]>;
  19. let Defs = [RAX], Uses = [EAX] in // RAX = signext(EAX)
  20. def CDQE : RI<0x98, RawFrm, (outs), (ins),
  21. "{cltq|cdqe}", []>, Sched<[WriteALU]>, Requires<[In64BitMode]>;
  22. // FIXME: CWD/CDQ/CQO shouldn't Def the A register, but the fast register
  23. // allocator crashes if you remove it.
  24. let Defs = [AX,DX], Uses = [AX] in // DX:AX = signext(AX)
  25. def CWD : I<0x99, RawFrm, (outs), (ins),
  26. "{cwtd|cwd}", []>, OpSize16, Sched<[WriteALU]>;
  27. let Defs = [EAX,EDX], Uses = [EAX] in // EDX:EAX = signext(EAX)
  28. def CDQ : I<0x99, RawFrm, (outs), (ins),
  29. "{cltd|cdq}", []>, OpSize32, Sched<[WriteALU]>;
  30. let Defs = [RAX,RDX], Uses = [RAX] in // RDX:RAX = signext(RAX)
  31. def CQO : RI<0x99, RawFrm, (outs), (ins),
  32. "{cqto|cqo}", []>, Sched<[WriteALU]>, Requires<[In64BitMode]>;
  33. }
  34. // Sign/Zero extenders
  35. let hasSideEffects = 0 in {
  36. def MOVSX16rr8 : I<0xBE, MRMSrcReg, (outs GR16:$dst), (ins GR8:$src),
  37. "movs{bw|x}\t{$src, $dst|$dst, $src}", []>,
  38. TB, OpSize16, Sched<[WriteALU]>;
  39. let mayLoad = 1 in
  40. def MOVSX16rm8 : I<0xBE, MRMSrcMem, (outs GR16:$dst), (ins i8mem:$src),
  41. "movs{bw|x}\t{$src, $dst|$dst, $src}", []>,
  42. TB, OpSize16, Sched<[WriteLoad]>;
  43. } // hasSideEffects = 0
  44. def MOVSX32rr8 : I<0xBE, MRMSrcReg, (outs GR32:$dst), (ins GR8:$src),
  45. "movs{bl|x}\t{$src, $dst|$dst, $src}",
  46. [(set GR32:$dst, (sext GR8:$src))]>, TB,
  47. OpSize32, Sched<[WriteALU]>;
  48. def MOVSX32rm8 : I<0xBE, MRMSrcMem, (outs GR32:$dst), (ins i8mem :$src),
  49. "movs{bl|x}\t{$src, $dst|$dst, $src}",
  50. [(set GR32:$dst, (sextloadi32i8 addr:$src))]>, TB,
  51. OpSize32, Sched<[WriteLoad]>;
  52. def MOVSX32rr16: I<0xBF, MRMSrcReg, (outs GR32:$dst), (ins GR16:$src),
  53. "movs{wl|x}\t{$src, $dst|$dst, $src}",
  54. [(set GR32:$dst, (sext GR16:$src))]>, TB,
  55. OpSize32, Sched<[WriteALU]>;
  56. def MOVSX32rm16: I<0xBF, MRMSrcMem, (outs GR32:$dst), (ins i16mem:$src),
  57. "movs{wl|x}\t{$src, $dst|$dst, $src}",
  58. [(set GR32:$dst, (sextloadi32i16 addr:$src))]>,
  59. OpSize32, TB, Sched<[WriteLoad]>;
  60. let hasSideEffects = 0 in {
  61. def MOVZX16rr8 : I<0xB6, MRMSrcReg, (outs GR16:$dst), (ins GR8:$src),
  62. "movz{bw|x}\t{$src, $dst|$dst, $src}", []>,
  63. TB, OpSize16, Sched<[WriteALU]>;
  64. let mayLoad = 1 in
  65. def MOVZX16rm8 : I<0xB6, MRMSrcMem, (outs GR16:$dst), (ins i8mem:$src),
  66. "movz{bw|x}\t{$src, $dst|$dst, $src}", []>,
  67. TB, OpSize16, Sched<[WriteLoad]>;
  68. } // hasSideEffects = 0
  69. def MOVZX32rr8 : I<0xB6, MRMSrcReg, (outs GR32:$dst), (ins GR8 :$src),
  70. "movz{bl|x}\t{$src, $dst|$dst, $src}",
  71. [(set GR32:$dst, (zext GR8:$src))]>, TB,
  72. OpSize32, Sched<[WriteALU]>;
  73. def MOVZX32rm8 : I<0xB6, MRMSrcMem, (outs GR32:$dst), (ins i8mem :$src),
  74. "movz{bl|x}\t{$src, $dst|$dst, $src}",
  75. [(set GR32:$dst, (zextloadi32i8 addr:$src))]>, TB,
  76. OpSize32, Sched<[WriteLoad]>;
  77. def MOVZX32rr16: I<0xB7, MRMSrcReg, (outs GR32:$dst), (ins GR16:$src),
  78. "movz{wl|x}\t{$src, $dst|$dst, $src}",
  79. [(set GR32:$dst, (zext GR16:$src))]>, TB,
  80. OpSize32, Sched<[WriteALU]>;
  81. def MOVZX32rm16: I<0xB7, MRMSrcMem, (outs GR32:$dst), (ins i16mem:$src),
  82. "movz{wl|x}\t{$src, $dst|$dst, $src}",
  83. [(set GR32:$dst, (zextloadi32i16 addr:$src))]>,
  84. TB, OpSize32, Sched<[WriteLoad]>;
  85. // These instructions exist as a consequence of operand size prefix having
  86. // control of the destination size, but not the input size. Only support them
  87. // for the disassembler.
  88. let isCodeGenOnly = 1, ForceDisassemble = 1, hasSideEffects = 0 in {
  89. def MOVSX16rr16: I<0xBF, MRMSrcReg, (outs GR16:$dst), (ins GR16:$src),
  90. "movs{ww|x}\t{$src, $dst|$dst, $src}",
  91. []>, TB, OpSize16, Sched<[WriteALU]>, NotMemoryFoldable;
  92. def MOVZX16rr16: I<0xB7, MRMSrcReg, (outs GR16:$dst), (ins GR16:$src),
  93. "movz{ww|x}\t{$src, $dst|$dst, $src}",
  94. []>, TB, OpSize16, Sched<[WriteALU]>, NotMemoryFoldable;
  95. let mayLoad = 1 in {
  96. def MOVSX16rm16: I<0xBF, MRMSrcMem, (outs GR16:$dst), (ins i16mem:$src),
  97. "movs{ww|x}\t{$src, $dst|$dst, $src}",
  98. []>, OpSize16, TB, Sched<[WriteLoad]>, NotMemoryFoldable;
  99. def MOVZX16rm16: I<0xB7, MRMSrcMem, (outs GR16:$dst), (ins i16mem:$src),
  100. "movz{ww|x}\t{$src, $dst|$dst, $src}",
  101. []>, TB, OpSize16, Sched<[WriteLoad]>, NotMemoryFoldable;
  102. } // mayLoad = 1
  103. } // isCodeGenOnly = 1, ForceDisassemble = 1, hasSideEffects = 0
  104. // These are the same as the regular MOVZX32rr8 and MOVZX32rm8
  105. // except that they use GR32_NOREX for the output operand register class
  106. // instead of GR32. This allows them to operate on h registers on x86-64.
  107. let hasSideEffects = 0, isCodeGenOnly = 1 in {
  108. def MOVZX32rr8_NOREX : I<0xB6, MRMSrcReg,
  109. (outs GR32_NOREX:$dst), (ins GR8_NOREX:$src),
  110. "movz{bl|x}\t{$src, $dst|$dst, $src}",
  111. []>, TB, OpSize32, Sched<[WriteALU]>;
  112. let mayLoad = 1 in
  113. def MOVZX32rm8_NOREX : I<0xB6, MRMSrcMem,
  114. (outs GR32_NOREX:$dst), (ins i8mem_NOREX:$src),
  115. "movz{bl|x}\t{$src, $dst|$dst, $src}",
  116. []>, TB, OpSize32, Sched<[WriteLoad]>;
  117. def MOVSX32rr8_NOREX : I<0xBE, MRMSrcReg,
  118. (outs GR32_NOREX:$dst), (ins GR8_NOREX:$src),
  119. "movs{bl|x}\t{$src, $dst|$dst, $src}",
  120. []>, TB, OpSize32, Sched<[WriteALU]>;
  121. let mayLoad = 1 in
  122. def MOVSX32rm8_NOREX : I<0xBE, MRMSrcMem,
  123. (outs GR32_NOREX:$dst), (ins i8mem_NOREX:$src),
  124. "movs{bl|x}\t{$src, $dst|$dst, $src}",
  125. []>, TB, OpSize32, Sched<[WriteLoad]>;
  126. }
  127. // MOVSX64rr8 always has a REX prefix and it has an 8-bit register
  128. // operand, which makes it a rare instruction with an 8-bit register
  129. // operand that can never access an h register. If support for h registers
  130. // were generalized, this would require a special register class.
  131. def MOVSX64rr8 : RI<0xBE, MRMSrcReg, (outs GR64:$dst), (ins GR8 :$src),
  132. "movs{bq|x}\t{$src, $dst|$dst, $src}",
  133. [(set GR64:$dst, (sext GR8:$src))]>, TB,
  134. Sched<[WriteALU]>;
  135. def MOVSX64rm8 : RI<0xBE, MRMSrcMem, (outs GR64:$dst), (ins i8mem :$src),
  136. "movs{bq|x}\t{$src, $dst|$dst, $src}",
  137. [(set GR64:$dst, (sextloadi64i8 addr:$src))]>,
  138. TB, Sched<[WriteLoad]>;
  139. def MOVSX64rr16: RI<0xBF, MRMSrcReg, (outs GR64:$dst), (ins GR16:$src),
  140. "movs{wq|x}\t{$src, $dst|$dst, $src}",
  141. [(set GR64:$dst, (sext GR16:$src))]>, TB,
  142. Sched<[WriteALU]>;
  143. def MOVSX64rm16: RI<0xBF, MRMSrcMem, (outs GR64:$dst), (ins i16mem:$src),
  144. "movs{wq|x}\t{$src, $dst|$dst, $src}",
  145. [(set GR64:$dst, (sextloadi64i16 addr:$src))]>,
  146. TB, Sched<[WriteLoad]>;
  147. def MOVSX64rr32: RI<0x63, MRMSrcReg, (outs GR64:$dst), (ins GR32:$src),
  148. "movs{lq|xd}\t{$src, $dst|$dst, $src}",
  149. [(set GR64:$dst, (sext GR32:$src))]>,
  150. Sched<[WriteALU]>, Requires<[In64BitMode]>;
  151. def MOVSX64rm32: RI<0x63, MRMSrcMem, (outs GR64:$dst), (ins i32mem:$src),
  152. "movs{lq|xd}\t{$src, $dst|$dst, $src}",
  153. [(set GR64:$dst, (sextloadi64i32 addr:$src))]>,
  154. Sched<[WriteLoad]>, Requires<[In64BitMode]>;
  155. // These instructions exist as a consequence of operand size prefix having
  156. // control of the destination size, but not the input size. Only support them
  157. // for the disassembler.
  158. let isCodeGenOnly = 1, ForceDisassemble = 1, hasSideEffects = 0 in {
  159. def MOVSX16rr32: I<0x63, MRMSrcReg, (outs GR16:$dst), (ins GR32:$src),
  160. "movs{lq|xd}\t{$src, $dst|$dst, $src}", []>,
  161. Sched<[WriteALU]>, OpSize16, Requires<[In64BitMode]>;
  162. def MOVSX32rr32: I<0x63, MRMSrcReg, (outs GR32:$dst), (ins GR32:$src),
  163. "movs{lq|xd}\t{$src, $dst|$dst, $src}", []>,
  164. Sched<[WriteALU]>, OpSize32, Requires<[In64BitMode]>;
  165. let mayLoad = 1 in {
  166. def MOVSX16rm32: I<0x63, MRMSrcMem, (outs GR16:$dst), (ins i32mem:$src),
  167. "movs{lq|xd}\t{$src, $dst|$dst, $src}", []>,
  168. Sched<[WriteLoad]>, OpSize16, Requires<[In64BitMode]>;
  169. def MOVSX32rm32: I<0x63, MRMSrcMem, (outs GR32:$dst), (ins i32mem:$src),
  170. "movs{lq|xd}\t{$src, $dst|$dst, $src}", []>,
  171. Sched<[WriteLoad]>, OpSize32, Requires<[In64BitMode]>;
  172. } // mayLoad = 1
  173. } // isCodeGenOnly = 1, ForceDisassemble = 1, hasSideEffects = 0
  174. // movzbq and movzwq encodings for the disassembler
  175. let hasSideEffects = 0 in {
  176. def MOVZX64rr8 : RI<0xB6, MRMSrcReg, (outs GR64:$dst), (ins GR8:$src),
  177. "movz{bq|x}\t{$src, $dst|$dst, $src}", []>,
  178. TB, Sched<[WriteALU]>;
  179. let mayLoad = 1 in
  180. def MOVZX64rm8 : RI<0xB6, MRMSrcMem, (outs GR64:$dst), (ins i8mem:$src),
  181. "movz{bq|x}\t{$src, $dst|$dst, $src}", []>,
  182. TB, Sched<[WriteLoad]>;
  183. def MOVZX64rr16 : RI<0xB7, MRMSrcReg, (outs GR64:$dst), (ins GR16:$src),
  184. "movz{wq|x}\t{$src, $dst|$dst, $src}", []>,
  185. TB, Sched<[WriteALU]>;
  186. let mayLoad = 1 in
  187. def MOVZX64rm16 : RI<0xB7, MRMSrcMem, (outs GR64:$dst), (ins i16mem:$src),
  188. "movz{wq|x}\t{$src, $dst|$dst, $src}", []>,
  189. TB, Sched<[WriteLoad]>;
  190. }
  191. // 64-bit zero-extension patterns use SUBREG_TO_REG and an operation writing a
  192. // 32-bit register.
  193. def : Pat<(i64 (zext GR8:$src)),
  194. (SUBREG_TO_REG (i64 0), (MOVZX32rr8 GR8:$src), sub_32bit)>;
  195. def : Pat<(zextloadi64i8 addr:$src),
  196. (SUBREG_TO_REG (i64 0), (MOVZX32rm8 addr:$src), sub_32bit)>;
  197. def : Pat<(i64 (zext GR16:$src)),
  198. (SUBREG_TO_REG (i64 0), (MOVZX32rr16 GR16:$src), sub_32bit)>;
  199. def : Pat<(zextloadi64i16 addr:$src),
  200. (SUBREG_TO_REG (i64 0), (MOVZX32rm16 addr:$src), sub_32bit)>;
  201. // The preferred way to do 32-bit-to-64-bit zero extension on x86-64 is to use a
  202. // SUBREG_TO_REG to utilize implicit zero-extension, however this isn't possible
  203. // when the 32-bit value is defined by a truncate or is copied from something
  204. // where the high bits aren't necessarily all zero. In such cases, we fall back
  205. // to these explicit zext instructions.
  206. def : Pat<(i64 (zext GR32:$src)),
  207. (SUBREG_TO_REG (i64 0), (MOV32rr GR32:$src), sub_32bit)>;
  208. def : Pat<(i64 (zextloadi64i32 addr:$src)),
  209. (SUBREG_TO_REG (i64 0), (MOV32rm addr:$src), sub_32bit)>;