[X86] Require HasBMI2 for the new BZHI tablegen patterns.
[oota-llvm.git] / lib / Target / X86 / X86InstrInfo.td
1 //===-- X86InstrInfo.td - Main X86 Instruction Definition --*- tablegen -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file describes the X86 instruction set, defining the instructions, and
11 // properties of the instructions which are needed for code generation, machine
12 // code emission, and analysis.
13 //
14 //===----------------------------------------------------------------------===//
15
16 //===----------------------------------------------------------------------===//
17 // X86 specific DAG Nodes.
18 //
19
20 def SDTIntShiftDOp: SDTypeProfile<1, 3,
21                                   [SDTCisSameAs<0, 1>, SDTCisSameAs<0, 2>,
22                                    SDTCisInt<0>, SDTCisInt<3>]>;
23
24 def SDTX86CmpTest : SDTypeProfile<1, 2, [SDTCisVT<0, i32>, SDTCisSameAs<1, 2>]>;
25
26 def SDTX86Cmps : SDTypeProfile<1, 3, [SDTCisFP<0>, SDTCisSameAs<1, 2>, SDTCisVT<3, i8>]>;
27 //def SDTX86Cmpss : SDTypeProfile<1, 3, [SDTCisVT<0, f32>, SDTCisSameAs<1, 2>, SDTCisVT<3, i8>]>;
28
29 def SDTX86Cmov    : SDTypeProfile<1, 4,
30                                   [SDTCisSameAs<0, 1>, SDTCisSameAs<1, 2>,
31                                    SDTCisVT<3, i8>, SDTCisVT<4, i32>]>;
32
33 // Unary and binary operator instructions that set EFLAGS as a side-effect.
34 def SDTUnaryArithWithFlags : SDTypeProfile<2, 1,
35                                            [SDTCisInt<0>, SDTCisVT<1, i32>]>;
36
37 def SDTBinaryArithWithFlags : SDTypeProfile<2, 2,
38                                             [SDTCisSameAs<0, 2>,
39                                              SDTCisSameAs<0, 3>,
40                                              SDTCisInt<0>, SDTCisVT<1, i32>]>;
41
42 // SDTBinaryArithWithFlagsInOut - RES1, EFLAGS = op LHS, RHS, EFLAGS
43 def SDTBinaryArithWithFlagsInOut : SDTypeProfile<2, 3,
44                                             [SDTCisSameAs<0, 2>,
45                                              SDTCisSameAs<0, 3>,
46                                              SDTCisInt<0>,
47                                              SDTCisVT<1, i32>,
48                                              SDTCisVT<4, i32>]>;
49 // RES1, RES2, FLAGS = op LHS, RHS
50 def SDT2ResultBinaryArithWithFlags : SDTypeProfile<3, 2,
51                                             [SDTCisSameAs<0, 1>,
52                                              SDTCisSameAs<0, 2>,
53                                              SDTCisSameAs<0, 3>,
54                                              SDTCisInt<0>, SDTCisVT<1, i32>]>;
55 def SDTX86BrCond  : SDTypeProfile<0, 3,
56                                   [SDTCisVT<0, OtherVT>,
57                                    SDTCisVT<1, i8>, SDTCisVT<2, i32>]>;
58
59 def SDTX86SetCC   : SDTypeProfile<1, 2,
60                                   [SDTCisVT<0, i8>,
61                                    SDTCisVT<1, i8>, SDTCisVT<2, i32>]>;
62 def SDTX86SetCC_C : SDTypeProfile<1, 2,
63                                   [SDTCisInt<0>,
64                                    SDTCisVT<1, i8>, SDTCisVT<2, i32>]>;
65
66 def SDTX86sahf : SDTypeProfile<1, 1, [SDTCisVT<0, i32>, SDTCisVT<1, i8>]>;
67
68 def SDTX86rdrand : SDTypeProfile<2, 0, [SDTCisInt<0>, SDTCisVT<1, i32>]>;
69
70 def SDTX86cas : SDTypeProfile<0, 3, [SDTCisPtrTy<0>, SDTCisInt<1>,
71                                      SDTCisVT<2, i8>]>;
72 def SDTX86caspair : SDTypeProfile<0, 1, [SDTCisPtrTy<0>]>;
73
74 def SDTX86atomicBinary : SDTypeProfile<2, 3, [SDTCisInt<0>, SDTCisInt<1>,
75                                 SDTCisPtrTy<2>, SDTCisInt<3>,SDTCisInt<4>]>;
76 def SDTX86Ret     : SDTypeProfile<0, -1, [SDTCisVT<0, i16>]>;
77
78 def SDT_X86CallSeqStart : SDCallSeqStart<[SDTCisVT<0, i32>]>;
79 def SDT_X86CallSeqEnd   : SDCallSeqEnd<[SDTCisVT<0, i32>,
80                                         SDTCisVT<1, i32>]>;
81
82 def SDT_X86Call   : SDTypeProfile<0, -1, [SDTCisVT<0, iPTR>]>;
83
84 def SDT_X86VASTART_SAVE_XMM_REGS : SDTypeProfile<0, -1, [SDTCisVT<0, i8>,
85                                                          SDTCisVT<1, iPTR>,
86                                                          SDTCisVT<2, iPTR>]>;
87
88 def SDT_X86VAARG_64 : SDTypeProfile<1, -1, [SDTCisPtrTy<0>,
89                                             SDTCisPtrTy<1>,
90                                             SDTCisVT<2, i32>,
91                                             SDTCisVT<3, i8>,
92                                             SDTCisVT<4, i32>]>;
93
94 def SDTX86RepStr  : SDTypeProfile<0, 1, [SDTCisVT<0, OtherVT>]>;
95
96 def SDTX86Void    : SDTypeProfile<0, 0, []>;
97
98 def SDTX86Wrapper : SDTypeProfile<1, 1, [SDTCisSameAs<0, 1>, SDTCisPtrTy<0>]>;
99
100 def SDT_X86TLSADDR : SDTypeProfile<0, 1, [SDTCisInt<0>]>;
101
102 def SDT_X86TLSBASEADDR : SDTypeProfile<0, 1, [SDTCisInt<0>]>;
103
104 def SDT_X86TLSCALL : SDTypeProfile<0, 1, [SDTCisInt<0>]>;
105
106 def SDT_X86SEG_ALLOCA : SDTypeProfile<1, 1, [SDTCisVT<0, iPTR>, SDTCisVT<1, iPTR>]>;
107
108 def SDT_X86WIN_FTOL : SDTypeProfile<0, 1, [SDTCisFP<0>]>;
109
110 def SDT_X86EHRET : SDTypeProfile<0, 1, [SDTCisInt<0>]>;
111
112 def SDT_X86TCRET : SDTypeProfile<0, 2, [SDTCisPtrTy<0>, SDTCisVT<1, i32>]>;
113
114 def SDT_X86MEMBARRIER : SDTypeProfile<0, 0, []>;
115
116 def X86MemBarrier : SDNode<"X86ISD::MEMBARRIER", SDT_X86MEMBARRIER,
117                             [SDNPHasChain,SDNPSideEffect]>;
118 def X86MFence : SDNode<"X86ISD::MFENCE", SDT_X86MEMBARRIER,
119                         [SDNPHasChain]>;
120 def X86SFence : SDNode<"X86ISD::SFENCE", SDT_X86MEMBARRIER,
121                         [SDNPHasChain]>;
122 def X86LFence : SDNode<"X86ISD::LFENCE", SDT_X86MEMBARRIER,
123                         [SDNPHasChain]>;
124
125
126 def X86bsf     : SDNode<"X86ISD::BSF",      SDTUnaryArithWithFlags>;
127 def X86bsr     : SDNode<"X86ISD::BSR",      SDTUnaryArithWithFlags>;
128 def X86shld    : SDNode<"X86ISD::SHLD",     SDTIntShiftDOp>;
129 def X86shrd    : SDNode<"X86ISD::SHRD",     SDTIntShiftDOp>;
130
131 def X86cmp     : SDNode<"X86ISD::CMP" ,     SDTX86CmpTest>;
132 def X86bt      : SDNode<"X86ISD::BT",       SDTX86CmpTest>;
133
134 def X86cmov    : SDNode<"X86ISD::CMOV",     SDTX86Cmov>;
135 def X86brcond  : SDNode<"X86ISD::BRCOND",   SDTX86BrCond,
136                         [SDNPHasChain]>;
137 def X86setcc   : SDNode<"X86ISD::SETCC",    SDTX86SetCC>;
138 def X86setcc_c : SDNode<"X86ISD::SETCC_CARRY", SDTX86SetCC_C>;
139
140 def X86sahf    : SDNode<"X86ISD::SAHF",     SDTX86sahf>;
141
142 def X86rdrand  : SDNode<"X86ISD::RDRAND",   SDTX86rdrand,
143                         [SDNPHasChain, SDNPSideEffect]>;
144
145 def X86rdseed  : SDNode<"X86ISD::RDSEED",   SDTX86rdrand,
146                         [SDNPHasChain, SDNPSideEffect]>;
147
148 def X86cas : SDNode<"X86ISD::LCMPXCHG_DAG", SDTX86cas,
149                         [SDNPHasChain, SDNPInGlue, SDNPOutGlue, SDNPMayStore,
150                          SDNPMayLoad, SDNPMemOperand]>;
151 def X86cas8 : SDNode<"X86ISD::LCMPXCHG8_DAG", SDTX86caspair,
152                         [SDNPHasChain, SDNPInGlue, SDNPOutGlue, SDNPMayStore,
153                          SDNPMayLoad, SDNPMemOperand]>;
154 def X86cas16 : SDNode<"X86ISD::LCMPXCHG16_DAG", SDTX86caspair,
155                         [SDNPHasChain, SDNPInGlue, SDNPOutGlue, SDNPMayStore,
156                          SDNPMayLoad, SDNPMemOperand]>;
157
158 def X86AtomAdd64 : SDNode<"X86ISD::ATOMADD64_DAG", SDTX86atomicBinary,
159                         [SDNPHasChain, SDNPMayStore,
160                          SDNPMayLoad, SDNPMemOperand]>;
161 def X86AtomSub64 : SDNode<"X86ISD::ATOMSUB64_DAG", SDTX86atomicBinary,
162                         [SDNPHasChain, SDNPMayStore,
163                          SDNPMayLoad, SDNPMemOperand]>;
164 def X86AtomOr64 : SDNode<"X86ISD::ATOMOR64_DAG", SDTX86atomicBinary,
165                         [SDNPHasChain, SDNPMayStore,
166                          SDNPMayLoad, SDNPMemOperand]>;
167 def X86AtomXor64 : SDNode<"X86ISD::ATOMXOR64_DAG", SDTX86atomicBinary,
168                         [SDNPHasChain, SDNPMayStore,
169                          SDNPMayLoad, SDNPMemOperand]>;
170 def X86AtomAnd64 : SDNode<"X86ISD::ATOMAND64_DAG", SDTX86atomicBinary,
171                         [SDNPHasChain, SDNPMayStore,
172                          SDNPMayLoad, SDNPMemOperand]>;
173 def X86AtomNand64 : SDNode<"X86ISD::ATOMNAND64_DAG", SDTX86atomicBinary,
174                         [SDNPHasChain, SDNPMayStore,
175                          SDNPMayLoad, SDNPMemOperand]>;
176 def X86AtomSwap64 : SDNode<"X86ISD::ATOMSWAP64_DAG", SDTX86atomicBinary,
177                         [SDNPHasChain, SDNPMayStore,
178                          SDNPMayLoad, SDNPMemOperand]>;
179 def X86retflag : SDNode<"X86ISD::RET_FLAG", SDTX86Ret,
180                         [SDNPHasChain, SDNPOptInGlue, SDNPVariadic]>;
181
182 def X86vastart_save_xmm_regs :
183                  SDNode<"X86ISD::VASTART_SAVE_XMM_REGS",
184                         SDT_X86VASTART_SAVE_XMM_REGS,
185                         [SDNPHasChain, SDNPVariadic]>;
186 def X86vaarg64 :
187                  SDNode<"X86ISD::VAARG_64", SDT_X86VAARG_64,
188                         [SDNPHasChain, SDNPMayLoad, SDNPMayStore,
189                          SDNPMemOperand]>;
190 def X86callseq_start :
191                  SDNode<"ISD::CALLSEQ_START", SDT_X86CallSeqStart,
192                         [SDNPHasChain, SDNPOutGlue]>;
193 def X86callseq_end :
194                  SDNode<"ISD::CALLSEQ_END",   SDT_X86CallSeqEnd,
195                         [SDNPHasChain, SDNPOptInGlue, SDNPOutGlue]>;
196
197 def X86call    : SDNode<"X86ISD::CALL",     SDT_X86Call,
198                         [SDNPHasChain, SDNPOutGlue, SDNPOptInGlue,
199                          SDNPVariadic]>;
200
201 def X86rep_stos: SDNode<"X86ISD::REP_STOS", SDTX86RepStr,
202                         [SDNPHasChain, SDNPInGlue, SDNPOutGlue, SDNPMayStore]>;
203 def X86rep_movs: SDNode<"X86ISD::REP_MOVS", SDTX86RepStr,
204                         [SDNPHasChain, SDNPInGlue, SDNPOutGlue, SDNPMayStore,
205                          SDNPMayLoad]>;
206
207 def X86rdtsc   : SDNode<"X86ISD::RDTSC_DAG", SDTX86Void,
208                         [SDNPHasChain, SDNPOutGlue, SDNPSideEffect]>;
209
210 def X86Wrapper    : SDNode<"X86ISD::Wrapper",     SDTX86Wrapper>;
211 def X86WrapperRIP : SDNode<"X86ISD::WrapperRIP",  SDTX86Wrapper>;
212
213 def X86tlsaddr : SDNode<"X86ISD::TLSADDR", SDT_X86TLSADDR,
214                         [SDNPHasChain, SDNPOptInGlue, SDNPOutGlue]>;
215
216 def X86tlsbaseaddr : SDNode<"X86ISD::TLSBASEADDR", SDT_X86TLSBASEADDR,
217                         [SDNPHasChain, SDNPOptInGlue, SDNPOutGlue]>;
218
219 def X86ehret : SDNode<"X86ISD::EH_RETURN", SDT_X86EHRET,
220                         [SDNPHasChain]>;
221
222 def X86eh_sjlj_setjmp  : SDNode<"X86ISD::EH_SJLJ_SETJMP",
223                                 SDTypeProfile<1, 1, [SDTCisInt<0>,
224                                                      SDTCisPtrTy<1>]>,
225                                 [SDNPHasChain, SDNPSideEffect]>;
226 def X86eh_sjlj_longjmp : SDNode<"X86ISD::EH_SJLJ_LONGJMP",
227                                 SDTypeProfile<0, 1, [SDTCisPtrTy<0>]>,
228                                 [SDNPHasChain, SDNPSideEffect]>;
229
230 def X86tcret : SDNode<"X86ISD::TC_RETURN", SDT_X86TCRET,
231                         [SDNPHasChain,  SDNPOptInGlue, SDNPVariadic]>;
232
233 def X86add_flag  : SDNode<"X86ISD::ADD",  SDTBinaryArithWithFlags,
234                           [SDNPCommutative]>;
235 def X86sub_flag  : SDNode<"X86ISD::SUB",  SDTBinaryArithWithFlags>;
236 def X86smul_flag : SDNode<"X86ISD::SMUL", SDTBinaryArithWithFlags,
237                           [SDNPCommutative]>;
238 def X86umul_flag : SDNode<"X86ISD::UMUL", SDT2ResultBinaryArithWithFlags,
239                           [SDNPCommutative]>;
240 def X86adc_flag  : SDNode<"X86ISD::ADC",  SDTBinaryArithWithFlagsInOut>;
241 def X86sbb_flag  : SDNode<"X86ISD::SBB",  SDTBinaryArithWithFlagsInOut>;
242
243 def X86inc_flag  : SDNode<"X86ISD::INC",  SDTUnaryArithWithFlags>;
244 def X86dec_flag  : SDNode<"X86ISD::DEC",  SDTUnaryArithWithFlags>;
245 def X86or_flag   : SDNode<"X86ISD::OR",   SDTBinaryArithWithFlags,
246                           [SDNPCommutative]>;
247 def X86xor_flag  : SDNode<"X86ISD::XOR",  SDTBinaryArithWithFlags,
248                           [SDNPCommutative]>;
249 def X86and_flag  : SDNode<"X86ISD::AND",  SDTBinaryArithWithFlags,
250                           [SDNPCommutative]>;
251
252 def X86bextr  : SDNode<"X86ISD::BEXTR",  SDTIntBinOp>;
253
254 def X86mul_imm : SDNode<"X86ISD::MUL_IMM", SDTIntBinOp>;
255
256 def X86WinAlloca : SDNode<"X86ISD::WIN_ALLOCA", SDTX86Void,
257                           [SDNPHasChain, SDNPInGlue, SDNPOutGlue]>;
258
259 def X86SegAlloca : SDNode<"X86ISD::SEG_ALLOCA", SDT_X86SEG_ALLOCA,
260                           [SDNPHasChain]>;
261
262 def X86TLSCall : SDNode<"X86ISD::TLSCALL", SDT_X86TLSCALL,
263                         [SDNPHasChain, SDNPOptInGlue, SDNPOutGlue]>;
264
265 def X86WinFTOL : SDNode<"X86ISD::WIN_FTOL", SDT_X86WIN_FTOL,
266                         [SDNPHasChain, SDNPOutGlue]>;
267
268 //===----------------------------------------------------------------------===//
269 // X86 Operand Definitions.
270 //
271
272 // A version of ptr_rc which excludes SP, ESP, and RSP. This is used for
273 // the index operand of an address, to conform to x86 encoding restrictions.
274 def ptr_rc_nosp : PointerLikeRegClass<1>;
275
276 // *mem - Operand definitions for the funky X86 addressing mode operands.
277 //
278 def X86MemAsmOperand : AsmOperandClass {
279  let Name = "Mem";
280 }
281 def X86Mem8AsmOperand : AsmOperandClass {
282   let Name = "Mem8"; let RenderMethod = "addMemOperands";
283 }
284 def X86Mem16AsmOperand : AsmOperandClass {
285   let Name = "Mem16"; let RenderMethod = "addMemOperands";
286 }
287 def X86Mem32AsmOperand : AsmOperandClass {
288   let Name = "Mem32"; let RenderMethod = "addMemOperands";
289 }
290 def X86Mem64AsmOperand : AsmOperandClass {
291   let Name = "Mem64"; let RenderMethod = "addMemOperands";
292 }
293 def X86Mem80AsmOperand : AsmOperandClass {
294   let Name = "Mem80"; let RenderMethod = "addMemOperands";
295 }
296 def X86Mem128AsmOperand : AsmOperandClass {
297   let Name = "Mem128"; let RenderMethod = "addMemOperands";
298 }
299 def X86Mem256AsmOperand : AsmOperandClass {
300   let Name = "Mem256"; let RenderMethod = "addMemOperands";
301 }
302 def X86Mem512AsmOperand : AsmOperandClass {
303   let Name = "Mem512"; let RenderMethod = "addMemOperands";
304 }
305
306 // Gather mem operands
307 def X86MemVX32Operand : AsmOperandClass {
308   let Name = "MemVX32"; let RenderMethod = "addMemOperands";
309 }
310 def X86MemVY32Operand : AsmOperandClass {
311   let Name = "MemVY32"; let RenderMethod = "addMemOperands";
312 }
313 def X86MemVZ32Operand : AsmOperandClass {
314   let Name = "MemVZ32"; let RenderMethod = "addMemOperands";
315 }
316 def X86MemVX64Operand : AsmOperandClass {
317   let Name = "MemVX64"; let RenderMethod = "addMemOperands";
318 }
319 def X86MemVY64Operand : AsmOperandClass {
320   let Name = "MemVY64"; let RenderMethod = "addMemOperands";
321 }
322 def X86MemVZ64Operand : AsmOperandClass {
323   let Name = "MemVZ64"; let RenderMethod = "addMemOperands";
324 }
325
326 def X86AbsMemAsmOperand : AsmOperandClass {
327   let Name = "AbsMem";
328   let SuperClasses = [X86MemAsmOperand];
329 }
330 class X86MemOperand<string printMethod> : Operand<iPTR> {
331   let PrintMethod = printMethod;
332   let MIOperandInfo = (ops ptr_rc, i8imm, ptr_rc_nosp, i32imm, i8imm);
333   let ParserMatchClass = X86MemAsmOperand;
334 }
335
336 let OperandType = "OPERAND_MEMORY" in {
337 def opaque32mem : X86MemOperand<"printopaquemem">;
338 def opaque48mem : X86MemOperand<"printopaquemem">;
339 def opaque80mem : X86MemOperand<"printopaquemem">;
340 def opaque512mem : X86MemOperand<"printopaquemem">;
341
342 def i8mem   : X86MemOperand<"printi8mem"> {
343   let ParserMatchClass = X86Mem8AsmOperand; }
344 def i16mem  : X86MemOperand<"printi16mem"> {
345   let ParserMatchClass = X86Mem16AsmOperand; }
346 def i32mem  : X86MemOperand<"printi32mem"> {
347   let ParserMatchClass = X86Mem32AsmOperand; }
348 def i64mem  : X86MemOperand<"printi64mem"> {
349   let ParserMatchClass = X86Mem64AsmOperand; }
350 def i128mem : X86MemOperand<"printi128mem"> {
351   let ParserMatchClass = X86Mem128AsmOperand; }
352 def i256mem : X86MemOperand<"printi256mem"> {
353   let ParserMatchClass = X86Mem256AsmOperand; }
354 def i512mem : X86MemOperand<"printi512mem"> {
355   let ParserMatchClass = X86Mem512AsmOperand; }
356 def f32mem  : X86MemOperand<"printf32mem"> {
357   let ParserMatchClass = X86Mem32AsmOperand; }
358 def f64mem  : X86MemOperand<"printf64mem"> {
359   let ParserMatchClass = X86Mem64AsmOperand; }
360 def f80mem  : X86MemOperand<"printf80mem"> {
361   let ParserMatchClass = X86Mem80AsmOperand; }
362 def f128mem : X86MemOperand<"printf128mem"> {
363   let ParserMatchClass = X86Mem128AsmOperand; }
364 def f256mem : X86MemOperand<"printf256mem">{
365   let ParserMatchClass = X86Mem256AsmOperand; }
366 def f512mem : X86MemOperand<"printf512mem">{
367   let ParserMatchClass = X86Mem512AsmOperand; }
368 def v512mem : Operand<iPTR> {
369   let PrintMethod = "printf512mem";
370   let MIOperandInfo = (ops ptr_rc, i8imm, VR512, i32imm, i8imm);
371   let ParserMatchClass = X86Mem512AsmOperand; }
372
373 // Gather mem operands
374 def vx32mem : X86MemOperand<"printi32mem">{
375   let MIOperandInfo = (ops ptr_rc, i8imm, VR128, i32imm, i8imm);
376   let ParserMatchClass = X86MemVX32Operand; }
377 def vy32mem : X86MemOperand<"printi32mem">{
378   let MIOperandInfo = (ops ptr_rc, i8imm, VR256, i32imm, i8imm);
379   let ParserMatchClass = X86MemVY32Operand; }
380 def vx64mem : X86MemOperand<"printi64mem">{
381   let MIOperandInfo = (ops ptr_rc, i8imm, VR128, i32imm, i8imm);
382   let ParserMatchClass = X86MemVX64Operand; }
383 def vy64mem : X86MemOperand<"printi64mem">{
384   let MIOperandInfo = (ops ptr_rc, i8imm, VR256, i32imm, i8imm);
385   let ParserMatchClass = X86MemVY64Operand; }
386 def vy64xmem : X86MemOperand<"printi64mem">{
387   let MIOperandInfo = (ops ptr_rc, i8imm, VR256X, i32imm, i8imm);
388   let ParserMatchClass = X86MemVY64Operand; }
389 def vz32mem : X86MemOperand<"printi32mem">{
390   let MIOperandInfo = (ops ptr_rc, i16imm, VR512, i32imm, i8imm);
391   let ParserMatchClass = X86MemVZ32Operand; }
392 def vz64mem : X86MemOperand<"printi64mem">{
393   let MIOperandInfo = (ops ptr_rc, i8imm, VR512, i32imm, i8imm);
394   let ParserMatchClass = X86MemVZ64Operand; }
395 }
396
397 // A version of i8mem for use on x86-64 that uses GR64_NOREX instead of
398 // plain GR64, so that it doesn't potentially require a REX prefix.
399 def i8mem_NOREX : Operand<i64> {
400   let PrintMethod = "printi8mem";
401   let MIOperandInfo = (ops GR64_NOREX, i8imm, GR64_NOREX_NOSP, i32imm, i8imm);
402   let ParserMatchClass = X86Mem8AsmOperand;
403   let OperandType = "OPERAND_MEMORY";
404 }
405
406 // GPRs available for tailcall.
407 // It represents GR32_TC, GR64_TC or GR64_TCW64.
408 def ptr_rc_tailcall : PointerLikeRegClass<2>;
409
410 // Special i32mem for addresses of load folding tail calls. These are not
411 // allowed to use callee-saved registers since they must be scheduled
412 // after callee-saved register are popped.
413 def i32mem_TC : Operand<i32> {
414   let PrintMethod = "printi32mem";
415   let MIOperandInfo = (ops ptr_rc_tailcall, i8imm, ptr_rc_tailcall,
416                        i32imm, i8imm);
417   let ParserMatchClass = X86Mem32AsmOperand;
418   let OperandType = "OPERAND_MEMORY";
419 }
420
421 // Special i64mem for addresses of load folding tail calls. These are not
422 // allowed to use callee-saved registers since they must be scheduled
423 // after callee-saved register are popped.
424 def i64mem_TC : Operand<i64> {
425   let PrintMethod = "printi64mem";
426   let MIOperandInfo = (ops ptr_rc_tailcall, i8imm,
427                        ptr_rc_tailcall, i32imm, i8imm);
428   let ParserMatchClass = X86Mem64AsmOperand;
429   let OperandType = "OPERAND_MEMORY";
430 }
431
432 let OperandType = "OPERAND_PCREL",
433     ParserMatchClass = X86AbsMemAsmOperand,
434     PrintMethod = "printPCRelImm" in {
435 def i32imm_pcrel : Operand<i32>;
436 def i16imm_pcrel : Operand<i16>;
437
438 // Branch targets have OtherVT type and print as pc-relative values.
439 def brtarget : Operand<OtherVT>;
440 def brtarget8 : Operand<OtherVT>;
441
442 }
443
444 def X86SrcIdx8Operand : AsmOperandClass {
445   let Name = "SrcIdx8";
446   let RenderMethod = "addSrcIdxOperands";
447   let SuperClasses = [X86Mem8AsmOperand];
448 }
449 def X86SrcIdx16Operand : AsmOperandClass {
450   let Name = "SrcIdx16";
451   let RenderMethod = "addSrcIdxOperands";
452   let SuperClasses = [X86Mem16AsmOperand];
453 }
454 def X86SrcIdx32Operand : AsmOperandClass {
455   let Name = "SrcIdx32";
456   let RenderMethod = "addSrcIdxOperands";
457   let SuperClasses = [X86Mem32AsmOperand];
458 }
459 def X86SrcIdx64Operand : AsmOperandClass {
460   let Name = "SrcIdx64";
461   let RenderMethod = "addSrcIdxOperands";
462   let SuperClasses = [X86Mem64AsmOperand];
463 }
464 def X86DstIdx8Operand : AsmOperandClass {
465   let Name = "DstIdx8";
466   let RenderMethod = "addDstIdxOperands";
467   let SuperClasses = [X86Mem8AsmOperand];
468 }
469 def X86DstIdx16Operand : AsmOperandClass {
470   let Name = "DstIdx16";
471   let RenderMethod = "addDstIdxOperands";
472   let SuperClasses = [X86Mem16AsmOperand];
473 }
474 def X86DstIdx32Operand : AsmOperandClass {
475   let Name = "DstIdx32";
476   let RenderMethod = "addDstIdxOperands";
477   let SuperClasses = [X86Mem32AsmOperand];
478 }
479 def X86DstIdx64Operand : AsmOperandClass {
480   let Name = "DstIdx64";
481   let RenderMethod = "addDstIdxOperands";
482   let SuperClasses = [X86Mem64AsmOperand];
483 }
484 def X86MemOffs8AsmOperand : AsmOperandClass {
485   let Name = "MemOffs8";
486   let RenderMethod = "addMemOffsOperands";
487   let SuperClasses = [X86Mem8AsmOperand];
488 }
489 def X86MemOffs16AsmOperand : AsmOperandClass {
490   let Name = "MemOffs16";
491   let RenderMethod = "addMemOffsOperands";
492   let SuperClasses = [X86Mem16AsmOperand];
493 }
494 def X86MemOffs32AsmOperand : AsmOperandClass {
495   let Name = "MemOffs32";
496   let RenderMethod = "addMemOffsOperands";
497   let SuperClasses = [X86Mem32AsmOperand];
498 }
499 def X86MemOffs64AsmOperand : AsmOperandClass {
500   let Name = "MemOffs64";
501   let RenderMethod = "addMemOffsOperands";
502   let SuperClasses = [X86Mem64AsmOperand];
503 }
504 let OperandType = "OPERAND_MEMORY" in {
505 def srcidx8 : Operand<iPTR> {
506   let ParserMatchClass = X86SrcIdx8Operand;
507   let MIOperandInfo = (ops ptr_rc, i8imm);
508   let PrintMethod = "printSrcIdx8"; }
509 def srcidx16 : Operand<iPTR> {
510   let ParserMatchClass = X86SrcIdx16Operand;
511   let MIOperandInfo = (ops ptr_rc, i8imm);
512   let PrintMethod = "printSrcIdx16"; }
513 def srcidx32 : Operand<iPTR> {
514   let ParserMatchClass = X86SrcIdx32Operand;
515   let MIOperandInfo = (ops ptr_rc, i8imm);
516   let PrintMethod = "printSrcIdx32"; }
517 def srcidx64 : Operand<iPTR> {
518   let ParserMatchClass = X86SrcIdx64Operand;
519   let MIOperandInfo = (ops ptr_rc, i8imm);
520   let PrintMethod = "printSrcIdx64"; }
521 def dstidx8 : Operand<iPTR> {
522   let ParserMatchClass = X86DstIdx8Operand;
523   let MIOperandInfo = (ops ptr_rc);
524   let PrintMethod = "printDstIdx8"; }
525 def dstidx16 : Operand<iPTR> {
526   let ParserMatchClass = X86DstIdx16Operand;
527   let MIOperandInfo = (ops ptr_rc);
528   let PrintMethod = "printDstIdx16"; }
529 def dstidx32 : Operand<iPTR> {
530   let ParserMatchClass = X86DstIdx32Operand;
531   let MIOperandInfo = (ops ptr_rc);
532   let PrintMethod = "printDstIdx32"; }
533 def dstidx64 : Operand<iPTR> {
534   let ParserMatchClass = X86DstIdx64Operand;
535   let MIOperandInfo = (ops ptr_rc);
536   let PrintMethod = "printDstIdx64"; }
537 def offset8 : Operand<iPTR> {
538   let ParserMatchClass = X86MemOffs8AsmOperand;
539   let MIOperandInfo = (ops i64imm, i8imm);
540   let PrintMethod = "printMemOffs8"; }
541 def offset16 : Operand<iPTR> {
542   let ParserMatchClass = X86MemOffs16AsmOperand;
543   let MIOperandInfo = (ops i64imm, i8imm);
544   let PrintMethod = "printMemOffs16"; }
545 def offset32 : Operand<iPTR> {
546   let ParserMatchClass = X86MemOffs32AsmOperand;
547   let MIOperandInfo = (ops i64imm, i8imm);
548   let PrintMethod = "printMemOffs32"; }
549 def offset64 : Operand<iPTR> {
550   let ParserMatchClass = X86MemOffs64AsmOperand;
551   let MIOperandInfo = (ops i64imm, i8imm);
552   let PrintMethod = "printMemOffs64"; }
553 }
554
555
556 def SSECC : Operand<i8> {
557   let PrintMethod = "printSSECC";
558   let OperandType = "OPERAND_IMMEDIATE";
559 }
560
561 def AVXCC : Operand<i8> {
562   let PrintMethod = "printAVXCC";
563   let OperandType = "OPERAND_IMMEDIATE";
564 }
565
566 class ImmSExtAsmOperandClass : AsmOperandClass {
567   let SuperClasses = [ImmAsmOperand];
568   let RenderMethod = "addImmOperands";
569 }
570
571 class ImmZExtAsmOperandClass : AsmOperandClass {
572   let SuperClasses = [ImmAsmOperand];
573   let RenderMethod = "addImmOperands";
574 }
575
576 def X86GR32orGR64AsmOperand : AsmOperandClass {
577   let Name = "GR32orGR64";
578 }
579
580 def GR32orGR64 : RegisterOperand<GR32> {
581   let ParserMatchClass = X86GR32orGR64AsmOperand;
582 }
583
584 def AVX512RC : Operand<i32> {
585   let PrintMethod = "printRoundingControl";
586   let OperandType = "OPERAND_IMMEDIATE";
587 }
588 // Sign-extended immediate classes. We don't need to define the full lattice
589 // here because there is no instruction with an ambiguity between ImmSExti64i32
590 // and ImmSExti32i8.
591 //
592 // The strange ranges come from the fact that the assembler always works with
593 // 64-bit immediates, but for a 16-bit target value we want to accept both "-1"
594 // (which will be a -1ULL), and "0xFF" (-1 in 16-bits).
595
596 // [0, 0x7FFFFFFF]                                            |
597 //   [0xFFFFFFFF80000000, 0xFFFFFFFFFFFFFFFF]
598 def ImmSExti64i32AsmOperand : ImmSExtAsmOperandClass {
599   let Name = "ImmSExti64i32";
600 }
601
602 // [0, 0x0000007F] | [0x000000000000FF80, 0x000000000000FFFF] |
603 //   [0xFFFFFFFFFFFFFF80, 0xFFFFFFFFFFFFFFFF]
604 def ImmSExti16i8AsmOperand : ImmSExtAsmOperandClass {
605   let Name = "ImmSExti16i8";
606   let SuperClasses = [ImmSExti64i32AsmOperand];
607 }
608
609 // [0, 0x0000007F] | [0x00000000FFFFFF80, 0x00000000FFFFFFFF] |
610 //   [0xFFFFFFFFFFFFFF80, 0xFFFFFFFFFFFFFFFF]
611 def ImmSExti32i8AsmOperand : ImmSExtAsmOperandClass {
612   let Name = "ImmSExti32i8";
613 }
614
615 // [0, 0x000000FF]
616 def ImmZExtu32u8AsmOperand : ImmZExtAsmOperandClass {
617   let Name = "ImmZExtu32u8";
618 }
619
620
621 // [0, 0x0000007F]                                            |
622 //   [0xFFFFFFFFFFFFFF80, 0xFFFFFFFFFFFFFFFF]
623 def ImmSExti64i8AsmOperand : ImmSExtAsmOperandClass {
624   let Name = "ImmSExti64i8";
625   let SuperClasses = [ImmSExti16i8AsmOperand, ImmSExti32i8AsmOperand,
626                       ImmSExti64i32AsmOperand];
627 }
628
629 // A couple of more descriptive operand definitions.
630 // 16-bits but only 8 bits are significant.
631 def i16i8imm  : Operand<i16> {
632   let ParserMatchClass = ImmSExti16i8AsmOperand;
633   let OperandType = "OPERAND_IMMEDIATE";
634 }
635 // 32-bits but only 8 bits are significant.
636 def i32i8imm  : Operand<i32> {
637   let ParserMatchClass = ImmSExti32i8AsmOperand;
638   let OperandType = "OPERAND_IMMEDIATE";
639 }
640 // 32-bits but only 8 bits are significant, and those 8 bits are unsigned.
641 def u32u8imm  : Operand<i32> {
642   let ParserMatchClass = ImmZExtu32u8AsmOperand;
643   let OperandType = "OPERAND_IMMEDIATE";
644 }
645
646 // 64-bits but only 32 bits are significant.
647 def i64i32imm  : Operand<i64> {
648   let ParserMatchClass = ImmSExti64i32AsmOperand;
649   let OperandType = "OPERAND_IMMEDIATE";
650 }
651
652 // 64-bits but only 32 bits are significant, and those bits are treated as being
653 // pc relative.
654 def i64i32imm_pcrel : Operand<i64> {
655   let PrintMethod = "printPCRelImm";
656   let ParserMatchClass = X86AbsMemAsmOperand;
657   let OperandType = "OPERAND_PCREL";
658 }
659
660 // 64-bits but only 8 bits are significant.
661 def i64i8imm   : Operand<i64> {
662   let ParserMatchClass = ImmSExti64i8AsmOperand;
663   let OperandType = "OPERAND_IMMEDIATE";
664 }
665
666 def lea64_32mem : Operand<i32> {
667   let PrintMethod = "printi32mem";
668   let MIOperandInfo = (ops GR64, i8imm, GR64_NOSP, i32imm, i8imm);
669   let ParserMatchClass = X86MemAsmOperand;
670 }
671
672 // Memory operands that use 64-bit pointers in both ILP32 and LP64.
673 def lea64mem : Operand<i64> {
674   let PrintMethod = "printi64mem";
675   let MIOperandInfo = (ops GR64, i8imm, GR64_NOSP, i32imm, i8imm);
676   let ParserMatchClass = X86MemAsmOperand;
677 }
678
679
680 //===----------------------------------------------------------------------===//
681 // X86 Complex Pattern Definitions.
682 //
683
684 // Define X86 specific addressing mode.
685 def addr      : ComplexPattern<iPTR, 5, "SelectAddr", [], [SDNPWantParent]>;
686 def lea32addr : ComplexPattern<i32, 5, "SelectLEAAddr",
687                                [add, sub, mul, X86mul_imm, shl, or, frameindex],
688                                []>;
689 // In 64-bit mode 32-bit LEAs can use RIP-relative addressing.
690 def lea64_32addr : ComplexPattern<i32, 5, "SelectLEA64_32Addr",
691                                   [add, sub, mul, X86mul_imm, shl, or,
692                                    frameindex, X86WrapperRIP],
693                                   []>;
694
695 def tls32addr : ComplexPattern<i32, 5, "SelectTLSADDRAddr",
696                                [tglobaltlsaddr], []>;
697
698 def tls32baseaddr : ComplexPattern<i32, 5, "SelectTLSADDRAddr",
699                                [tglobaltlsaddr], []>;
700
701 def lea64addr : ComplexPattern<i64, 5, "SelectLEAAddr",
702                         [add, sub, mul, X86mul_imm, shl, or, frameindex,
703                          X86WrapperRIP], []>;
704
705 def tls64addr : ComplexPattern<i64, 5, "SelectTLSADDRAddr",
706                                [tglobaltlsaddr], []>;
707
708 def tls64baseaddr : ComplexPattern<i64, 5, "SelectTLSADDRAddr",
709                                [tglobaltlsaddr], []>;
710
711 //===----------------------------------------------------------------------===//
712 // X86 Instruction Predicate Definitions.
713 def HasCMov      : Predicate<"Subtarget->hasCMov()">;
714 def NoCMov       : Predicate<"!Subtarget->hasCMov()">;
715
716 def HasMMX       : Predicate<"Subtarget->hasMMX()">;
717 def Has3DNow     : Predicate<"Subtarget->has3DNow()">;
718 def Has3DNowA    : Predicate<"Subtarget->has3DNowA()">;
719 def HasSSE1      : Predicate<"Subtarget->hasSSE1()">;
720 def UseSSE1      : Predicate<"Subtarget->hasSSE1() && !Subtarget->hasAVX()">;
721 def HasSSE2      : Predicate<"Subtarget->hasSSE2()">;
722 def UseSSE2      : Predicate<"Subtarget->hasSSE2() && !Subtarget->hasAVX()">;
723 def HasSSE3      : Predicate<"Subtarget->hasSSE3()">;
724 def UseSSE3      : Predicate<"Subtarget->hasSSE3() && !Subtarget->hasAVX()">;
725 def HasSSSE3     : Predicate<"Subtarget->hasSSSE3()">;
726 def UseSSSE3     : Predicate<"Subtarget->hasSSSE3() && !Subtarget->hasAVX()">;
727 def HasSSE41     : Predicate<"Subtarget->hasSSE41()">;
728 def UseSSE41     : Predicate<"Subtarget->hasSSE41() && !Subtarget->hasAVX()">;
729 def HasSSE42     : Predicate<"Subtarget->hasSSE42()">;
730 def UseSSE42     : Predicate<"Subtarget->hasSSE42() && !Subtarget->hasAVX()">;
731 def HasSSE4A     : Predicate<"Subtarget->hasSSE4A()">;
732 def HasAVX       : Predicate<"Subtarget->hasAVX()">;
733 def HasAVX2      : Predicate<"Subtarget->hasAVX2()">;
734 def HasAVX1Only  : Predicate<"Subtarget->hasAVX() && !Subtarget->hasAVX2()">;
735 def HasAVX512    : Predicate<"Subtarget->hasAVX512()">,
736                      AssemblerPredicate<"FeatureAVX512", "AVX-512 ISA">;
737 def UseAVX       : Predicate<"Subtarget->hasAVX() && !Subtarget->hasAVX512()">;
738 def UseAVX2      : Predicate<"Subtarget->hasAVX2() && !Subtarget->hasAVX512()">;
739 def NoAVX512       : Predicate<"!Subtarget->hasAVX512()">;
740 def HasCDI       : Predicate<"Subtarget->hasCDI()">;
741 def HasPFI       : Predicate<"Subtarget->hasPFI()">;
742 def HasERI       : Predicate<"Subtarget->hasERI()">;
743
744 def HasPOPCNT    : Predicate<"Subtarget->hasPOPCNT()">;
745 def HasAES       : Predicate<"Subtarget->hasAES()">;
746 def HasPCLMUL    : Predicate<"Subtarget->hasPCLMUL()">;
747 def HasFMA       : Predicate<"Subtarget->hasFMA()">;
748 def UseFMAOnAVX  : Predicate<"Subtarget->hasFMA() && !Subtarget->hasAVX512()">;
749 def HasFMA4      : Predicate<"Subtarget->hasFMA4()">;
750 def HasXOP       : Predicate<"Subtarget->hasXOP()">;
751 def HasTBM       : Predicate<"Subtarget->hasTBM()">;
752 def HasMOVBE     : Predicate<"Subtarget->hasMOVBE()">;
753 def HasRDRAND    : Predicate<"Subtarget->hasRDRAND()">;
754 def HasF16C      : Predicate<"Subtarget->hasF16C()">;
755 def HasFSGSBase  : Predicate<"Subtarget->hasFSGSBase()">;
756 def HasLZCNT     : Predicate<"Subtarget->hasLZCNT()">;
757 def HasBMI       : Predicate<"Subtarget->hasBMI()">;
758 def HasBMI2      : Predicate<"Subtarget->hasBMI2()">;
759 def HasRTM       : Predicate<"Subtarget->hasRTM()">;
760 def HasHLE       : Predicate<"Subtarget->hasHLE()">;
761 def HasTSX       : Predicate<"Subtarget->hasRTM() || Subtarget->hasHLE()">;
762 def HasADX       : Predicate<"Subtarget->hasADX()">;
763 def HasSHA       : Predicate<"Subtarget->hasSHA()">;
764 def HasPRFCHW    : Predicate<"Subtarget->hasPRFCHW()">;
765 def HasRDSEED    : Predicate<"Subtarget->hasRDSEED()">;
766 def HasPrefetchW : Predicate<"Subtarget->hasPRFCHW()">;
767 def FPStackf32   : Predicate<"!Subtarget->hasSSE1()">;
768 def FPStackf64   : Predicate<"!Subtarget->hasSSE2()">;
769 def HasCmpxchg16b: Predicate<"Subtarget->hasCmpxchg16b()">;
770 def Not64BitMode : Predicate<"!Subtarget->is64Bit()">,
771                              AssemblerPredicate<"!Mode64Bit", "Not 64-bit mode">;
772 def In64BitMode  : Predicate<"Subtarget->is64Bit()">,
773                              AssemblerPredicate<"Mode64Bit", "64-bit mode">;
774 def In16BitMode  : Predicate<"Subtarget->is16Bit()">,
775                              AssemblerPredicate<"Mode16Bit", "16-bit mode">;
776 def Not16BitMode : Predicate<"!Subtarget->is16Bit()">,
777                              AssemblerPredicate<"!Mode16Bit", "Not 16-bit mode">;
778 def In32BitMode  : Predicate<"Subtarget->is32Bit()">,
779                              AssemblerPredicate<"Mode32Bit", "32-bit mode">;
780 def IsWin64      : Predicate<"Subtarget->isTargetWin64()">;
781 def IsNaCl       : Predicate<"Subtarget->isTargetNaCl()">;
782 def NotNaCl      : Predicate<"!Subtarget->isTargetNaCl()">;
783 def SmallCode    : Predicate<"TM.getCodeModel() == CodeModel::Small">;
784 def KernelCode   : Predicate<"TM.getCodeModel() == CodeModel::Kernel">;
785 def FarData      : Predicate<"TM.getCodeModel() != CodeModel::Small &&"
786                              "TM.getCodeModel() != CodeModel::Kernel">;
787 def NearData     : Predicate<"TM.getCodeModel() == CodeModel::Small ||"
788                              "TM.getCodeModel() == CodeModel::Kernel">;
789 def IsStatic     : Predicate<"TM.getRelocationModel() == Reloc::Static">;
790 def IsNotPIC     : Predicate<"TM.getRelocationModel() != Reloc::PIC_">;
791 def OptForSize   : Predicate<"OptForSize">;
792 def OptForSpeed  : Predicate<"!OptForSize">;
793 def FastBTMem    : Predicate<"!Subtarget->isBTMemSlow()">;
794 def CallImmAddr  : Predicate<"Subtarget->IsLegalToCallImmediateAddr(TM)">;
795 def FavorMemIndirectCall  : Predicate<"!Subtarget->callRegIndirect()">;
796
797 //===----------------------------------------------------------------------===//
798 // X86 Instruction Format Definitions.
799 //
800
801 include "X86InstrFormats.td"
802
803 //===----------------------------------------------------------------------===//
804 // Pattern fragments.
805 //
806
807 // X86 specific condition code. These correspond to CondCode in
808 // X86InstrInfo.h. They must be kept in synch.
809 def X86_COND_A   : PatLeaf<(i8 0)>;  // alt. COND_NBE
810 def X86_COND_AE  : PatLeaf<(i8 1)>;  // alt. COND_NC
811 def X86_COND_B   : PatLeaf<(i8 2)>;  // alt. COND_C
812 def X86_COND_BE  : PatLeaf<(i8 3)>;  // alt. COND_NA
813 def X86_COND_E   : PatLeaf<(i8 4)>;  // alt. COND_Z
814 def X86_COND_G   : PatLeaf<(i8 5)>;  // alt. COND_NLE
815 def X86_COND_GE  : PatLeaf<(i8 6)>;  // alt. COND_NL
816 def X86_COND_L   : PatLeaf<(i8 7)>;  // alt. COND_NGE
817 def X86_COND_LE  : PatLeaf<(i8 8)>;  // alt. COND_NG
818 def X86_COND_NE  : PatLeaf<(i8 9)>;  // alt. COND_NZ
819 def X86_COND_NO  : PatLeaf<(i8 10)>;
820 def X86_COND_NP  : PatLeaf<(i8 11)>; // alt. COND_PO
821 def X86_COND_NS  : PatLeaf<(i8 12)>;
822 def X86_COND_O   : PatLeaf<(i8 13)>;
823 def X86_COND_P   : PatLeaf<(i8 14)>; // alt. COND_PE
824 def X86_COND_S   : PatLeaf<(i8 15)>;
825
826 let FastIselShouldIgnore = 1 in { // FastIsel should ignore all simm8 instrs.
827   def i16immSExt8  : ImmLeaf<i16, [{ return Imm == (int8_t)Imm; }]>;
828   def i32immSExt8  : ImmLeaf<i32, [{ return Imm == (int8_t)Imm; }]>;
829   def i64immSExt8  : ImmLeaf<i64, [{ return Imm == (int8_t)Imm; }]>;
830 }
831
832 def i64immSExt32 : ImmLeaf<i64, [{ return Imm == (int32_t)Imm; }]>;
833
834
835 // i64immZExt32 predicate - True if the 64-bit immediate fits in a 32-bit
836 // unsigned field.
837 def i64immZExt32 : ImmLeaf<i64, [{ return (uint64_t)Imm == (uint32_t)Imm; }]>;
838
839 def i64immZExt32SExt8 : ImmLeaf<i64, [{
840   return (uint64_t)Imm == (uint32_t)Imm && (int32_t)Imm == (int8_t)Imm;
841 }]>;
842
843 // Helper fragments for loads.
844 // It's always safe to treat a anyext i16 load as a i32 load if the i16 is
845 // known to be 32-bit aligned or better. Ditto for i8 to i16.
846 def loadi16 : PatFrag<(ops node:$ptr), (i16 (unindexedload node:$ptr)), [{
847   LoadSDNode *LD = cast<LoadSDNode>(N);
848   ISD::LoadExtType ExtType = LD->getExtensionType();
849   if (ExtType == ISD::NON_EXTLOAD)
850     return true;
851   if (ExtType == ISD::EXTLOAD)
852     return LD->getAlignment() >= 2 && !LD->isVolatile();
853   return false;
854 }]>;
855
856 def loadi16_anyext : PatFrag<(ops node:$ptr), (i32 (unindexedload node:$ptr)),[{
857   LoadSDNode *LD = cast<LoadSDNode>(N);
858   ISD::LoadExtType ExtType = LD->getExtensionType();
859   if (ExtType == ISD::EXTLOAD)
860     return LD->getAlignment() >= 2 && !LD->isVolatile();
861   return false;
862 }]>;
863
864 def loadi32 : PatFrag<(ops node:$ptr), (i32 (unindexedload node:$ptr)), [{
865   LoadSDNode *LD = cast<LoadSDNode>(N);
866   ISD::LoadExtType ExtType = LD->getExtensionType();
867   if (ExtType == ISD::NON_EXTLOAD)
868     return true;
869   if (ExtType == ISD::EXTLOAD)
870     return LD->getAlignment() >= 4 && !LD->isVolatile();
871   return false;
872 }]>;
873
874 def loadi8  : PatFrag<(ops node:$ptr), (i8  (load node:$ptr))>;
875 def loadi64 : PatFrag<(ops node:$ptr), (i64 (load node:$ptr))>;
876 def loadf32 : PatFrag<(ops node:$ptr), (f32 (load node:$ptr))>;
877 def loadf64 : PatFrag<(ops node:$ptr), (f64 (load node:$ptr))>;
878 def loadf80 : PatFrag<(ops node:$ptr), (f80 (load node:$ptr))>;
879
880 def sextloadi16i8  : PatFrag<(ops node:$ptr), (i16 (sextloadi8 node:$ptr))>;
881 def sextloadi32i8  : PatFrag<(ops node:$ptr), (i32 (sextloadi8 node:$ptr))>;
882 def sextloadi32i16 : PatFrag<(ops node:$ptr), (i32 (sextloadi16 node:$ptr))>;
883 def sextloadi64i8  : PatFrag<(ops node:$ptr), (i64 (sextloadi8 node:$ptr))>;
884 def sextloadi64i16 : PatFrag<(ops node:$ptr), (i64 (sextloadi16 node:$ptr))>;
885 def sextloadi64i32 : PatFrag<(ops node:$ptr), (i64 (sextloadi32 node:$ptr))>;
886
887 def zextloadi8i1   : PatFrag<(ops node:$ptr), (i8  (zextloadi1 node:$ptr))>;
888 def zextloadi16i1  : PatFrag<(ops node:$ptr), (i16 (zextloadi1 node:$ptr))>;
889 def zextloadi32i1  : PatFrag<(ops node:$ptr), (i32 (zextloadi1 node:$ptr))>;
890 def zextloadi16i8  : PatFrag<(ops node:$ptr), (i16 (zextloadi8 node:$ptr))>;
891 def zextloadi32i8  : PatFrag<(ops node:$ptr), (i32 (zextloadi8 node:$ptr))>;
892 def zextloadi32i16 : PatFrag<(ops node:$ptr), (i32 (zextloadi16 node:$ptr))>;
893 def zextloadi64i1  : PatFrag<(ops node:$ptr), (i64 (zextloadi1 node:$ptr))>;
894 def zextloadi64i8  : PatFrag<(ops node:$ptr), (i64 (zextloadi8 node:$ptr))>;
895 def zextloadi64i16 : PatFrag<(ops node:$ptr), (i64 (zextloadi16 node:$ptr))>;
896 def zextloadi64i32 : PatFrag<(ops node:$ptr), (i64 (zextloadi32 node:$ptr))>;
897
898 def extloadi8i1    : PatFrag<(ops node:$ptr), (i8  (extloadi1 node:$ptr))>;
899 def extloadi16i1   : PatFrag<(ops node:$ptr), (i16 (extloadi1 node:$ptr))>;
900 def extloadi32i1   : PatFrag<(ops node:$ptr), (i32 (extloadi1 node:$ptr))>;
901 def extloadi16i8   : PatFrag<(ops node:$ptr), (i16 (extloadi8 node:$ptr))>;
902 def extloadi32i8   : PatFrag<(ops node:$ptr), (i32 (extloadi8 node:$ptr))>;
903 def extloadi32i16  : PatFrag<(ops node:$ptr), (i32 (extloadi16 node:$ptr))>;
904 def extloadi64i1   : PatFrag<(ops node:$ptr), (i64 (extloadi1 node:$ptr))>;
905 def extloadi64i8   : PatFrag<(ops node:$ptr), (i64 (extloadi8 node:$ptr))>;
906 def extloadi64i16  : PatFrag<(ops node:$ptr), (i64 (extloadi16 node:$ptr))>;
907 def extloadi64i32  : PatFrag<(ops node:$ptr), (i64 (extloadi32 node:$ptr))>;
908
909
910 // An 'and' node with a single use.
911 def and_su : PatFrag<(ops node:$lhs, node:$rhs), (and node:$lhs, node:$rhs), [{
912   return N->hasOneUse();
913 }]>;
914 // An 'srl' node with a single use.
915 def srl_su : PatFrag<(ops node:$lhs, node:$rhs), (srl node:$lhs, node:$rhs), [{
916   return N->hasOneUse();
917 }]>;
918 // An 'trunc' node with a single use.
919 def trunc_su : PatFrag<(ops node:$src), (trunc node:$src), [{
920   return N->hasOneUse();
921 }]>;
922
923 //===----------------------------------------------------------------------===//
924 // Instruction list.
925 //
926
927 // Nop
928 let neverHasSideEffects = 1, SchedRW = [WriteZero] in {
929   def NOOP : I<0x90, RawFrm, (outs), (ins), "nop", [], IIC_NOP>;
930   def NOOPW : I<0x1f, MRMXm, (outs), (ins i16mem:$zero),
931                 "nop{w}\t$zero", [], IIC_NOP>, TB, OpSize16;
932   def NOOPL : I<0x1f, MRMXm, (outs), (ins i32mem:$zero),
933                 "nop{l}\t$zero", [], IIC_NOP>, TB, OpSize32;
934 }
935
936
937 // Constructing a stack frame.
938 def ENTER : Ii16<0xC8, RawFrmImm8, (outs), (ins i16imm:$len, i8imm:$lvl),
939                  "enter\t$len, $lvl", [], IIC_ENTER>, Sched<[WriteMicrocoded]>;
940
941 let SchedRW = [WriteALU] in {
942 let Defs = [EBP, ESP], Uses = [EBP, ESP], mayLoad = 1, neverHasSideEffects=1 in
943 def LEAVE    : I<0xC9, RawFrm,
944                  (outs), (ins), "leave", [], IIC_LEAVE>,
945                  Requires<[Not64BitMode]>;
946
947 let Defs = [RBP,RSP], Uses = [RBP,RSP], mayLoad = 1, neverHasSideEffects = 1 in
948 def LEAVE64  : I<0xC9, RawFrm,
949                  (outs), (ins), "leave", [], IIC_LEAVE>,
950                  Requires<[In64BitMode]>;
951 } // SchedRW
952
953 //===----------------------------------------------------------------------===//
954 //  Miscellaneous Instructions.
955 //
956
957 let Defs = [ESP], Uses = [ESP], neverHasSideEffects=1 in {
958 let mayLoad = 1, SchedRW = [WriteLoad] in {
959 def POP16r  : I<0x58, AddRegFrm, (outs GR16:$reg), (ins), "pop{w}\t$reg", [],
960                 IIC_POP_REG16>, OpSize16;
961 def POP32r  : I<0x58, AddRegFrm, (outs GR32:$reg), (ins), "pop{l}\t$reg", [],
962                 IIC_POP_REG>, OpSize32, Requires<[Not64BitMode]>;
963 def POP16rmr: I<0x8F, MRM0r, (outs GR16:$reg), (ins), "pop{w}\t$reg", [],
964                 IIC_POP_REG>, OpSize16;
965 def POP16rmm: I<0x8F, MRM0m, (outs), (ins i16mem:$dst), "pop{w}\t$dst", [],
966                 IIC_POP_MEM>, OpSize16;
967 def POP32rmr: I<0x8F, MRM0r, (outs GR32:$reg), (ins), "pop{l}\t$reg", [],
968                 IIC_POP_REG>, OpSize32, Requires<[Not64BitMode]>;
969 def POP32rmm: I<0x8F, MRM0m, (outs), (ins i32mem:$dst), "pop{l}\t$dst", [],
970                 IIC_POP_MEM>, OpSize32, Requires<[Not64BitMode]>;
971
972 def POPF16   : I<0x9D, RawFrm, (outs), (ins), "popf{w}", [], IIC_POP_F>,
973                 OpSize16;
974 def POPF32   : I<0x9D, RawFrm, (outs), (ins), "popf{l|d}", [], IIC_POP_FD>,
975                 OpSize32, Requires<[Not64BitMode]>;
976 } // mayLoad, SchedRW
977
978 let mayStore = 1, SchedRW = [WriteStore] in {
979 def PUSH16r  : I<0x50, AddRegFrm, (outs), (ins GR16:$reg), "push{w}\t$reg",[],
980                  IIC_PUSH_REG>, OpSize16;
981 def PUSH32r  : I<0x50, AddRegFrm, (outs), (ins GR32:$reg), "push{l}\t$reg",[],
982                  IIC_PUSH_REG>, OpSize32, Requires<[Not64BitMode]>;
983 def PUSH16rmr: I<0xFF, MRM6r, (outs), (ins GR16:$reg), "push{w}\t$reg",[],
984                  IIC_PUSH_REG>, OpSize16;
985 def PUSH16rmm: I<0xFF, MRM6m, (outs), (ins i16mem:$src), "push{w}\t$src",[],
986                  IIC_PUSH_MEM>, OpSize16;
987 def PUSH32rmr: I<0xFF, MRM6r, (outs), (ins GR32:$reg), "push{l}\t$reg",[],
988                  IIC_PUSH_REG>, OpSize32, Requires<[Not64BitMode]>;
989 def PUSH32rmm: I<0xFF, MRM6m, (outs), (ins i32mem:$src), "push{l}\t$src",[],
990                  IIC_PUSH_MEM>, OpSize32, Requires<[Not64BitMode]>;
991
992 def PUSH16i8 : Ii8<0x6a, RawFrm, (outs), (ins i16i8imm:$imm),
993                    "push{w}\t$imm", [], IIC_PUSH_IMM>, OpSize16,
994                    Requires<[Not64BitMode]>;
995 def PUSH32i8 : Ii8<0x6a, RawFrm, (outs), (ins i32i8imm:$imm),
996                    "push{l}\t$imm", [], IIC_PUSH_IMM>, OpSize32,
997                    Requires<[Not64BitMode]>;
998 def PUSHi16  : Ii16<0x68, RawFrm, (outs), (ins i16imm:$imm),
999                    "push{w}\t$imm", [], IIC_PUSH_IMM>, OpSize16,
1000                    Requires<[Not64BitMode]>;
1001 def PUSHi32  : Ii32<0x68, RawFrm, (outs), (ins i32imm:$imm),
1002                    "push{l}\t$imm", [], IIC_PUSH_IMM>, OpSize32,
1003                    Requires<[Not64BitMode]>;
1004
1005 def PUSHF16  : I<0x9C, RawFrm, (outs), (ins), "pushf{w}", [], IIC_PUSH_F>,
1006                  OpSize16;
1007 def PUSHF32  : I<0x9C, RawFrm, (outs), (ins), "pushf{l|d}", [], IIC_PUSH_F>,
1008                OpSize32, Requires<[Not64BitMode]>;
1009
1010 } // mayStore, SchedRW
1011 }
1012
1013 let Defs = [RSP], Uses = [RSP], neverHasSideEffects=1 in {
1014 let mayLoad = 1, SchedRW = [WriteLoad] in {
1015 def POP64r   : I<0x58, AddRegFrm, (outs GR64:$reg), (ins), "pop{q}\t$reg", [],
1016                  IIC_POP_REG>, OpSize32, Requires<[In64BitMode]>;
1017 def POP64rmr: I<0x8F, MRM0r, (outs GR64:$reg), (ins), "pop{q}\t$reg", [],
1018                 IIC_POP_REG>, OpSize32, Requires<[In64BitMode]>;
1019 def POP64rmm: I<0x8F, MRM0m, (outs), (ins i64mem:$dst), "pop{q}\t$dst", [],
1020                 IIC_POP_MEM>, OpSize32, Requires<[In64BitMode]>;
1021 } // mayLoad, SchedRW
1022 let mayStore = 1, SchedRW = [WriteStore] in {
1023 def PUSH64r  : I<0x50, AddRegFrm, (outs), (ins GR64:$reg), "push{q}\t$reg", [],
1024                  IIC_PUSH_REG>, OpSize32, Requires<[In64BitMode]>;
1025 def PUSH64rmr: I<0xFF, MRM6r, (outs), (ins GR64:$reg), "push{q}\t$reg", [],
1026                  IIC_PUSH_REG>, OpSize32, Requires<[In64BitMode]>;
1027 def PUSH64rmm: I<0xFF, MRM6m, (outs), (ins i64mem:$src), "push{q}\t$src", [],
1028                  IIC_PUSH_MEM>, OpSize32, Requires<[In64BitMode]>;
1029 } // mayStore, SchedRW
1030 }
1031
1032 let Defs = [RSP], Uses = [RSP], neverHasSideEffects = 1, mayStore = 1,
1033     SchedRW = [WriteStore] in {
1034 def PUSH64i8   : Ii8<0x6a, RawFrm, (outs), (ins i64i8imm:$imm),
1035                     "push{q}\t$imm", [], IIC_PUSH_IMM>, Requires<[In64BitMode]>;
1036 def PUSH64i16  : Ii16<0x68, RawFrm, (outs), (ins i16imm:$imm),
1037                     "push{w}\t$imm", [], IIC_PUSH_IMM>, OpSize16,
1038                     Requires<[In64BitMode]>;
1039 def PUSH64i32  : Ii32S<0x68, RawFrm, (outs), (ins i64i32imm:$imm),
1040                     "push{q}\t$imm", [], IIC_PUSH_IMM>, OpSize32,
1041                     Requires<[In64BitMode]>;
1042 }
1043
1044 let Defs = [RSP, EFLAGS], Uses = [RSP], mayLoad = 1, neverHasSideEffects=1 in
1045 def POPF64   : I<0x9D, RawFrm, (outs), (ins), "popfq", [], IIC_POP_FD>,
1046                OpSize32, Requires<[In64BitMode]>, Sched<[WriteLoad]>;
1047 let Defs = [RSP], Uses = [RSP, EFLAGS], mayStore = 1, neverHasSideEffects=1 in
1048 def PUSHF64    : I<0x9C, RawFrm, (outs), (ins), "pushfq", [], IIC_PUSH_F>,
1049                  OpSize32, Requires<[In64BitMode]>, Sched<[WriteStore]>;
1050
1051 let Defs = [EDI, ESI, EBP, EBX, EDX, ECX, EAX, ESP], Uses = [ESP],
1052     mayLoad = 1, neverHasSideEffects = 1, SchedRW = [WriteLoad] in {
1053 def POPA32   : I<0x61, RawFrm, (outs), (ins), "popal", [], IIC_POP_A>,
1054                OpSize32, Requires<[Not64BitMode]>;
1055 def POPA16   : I<0x61, RawFrm, (outs), (ins), "popaw", [], IIC_POP_A>,
1056                OpSize16, Requires<[Not64BitMode]>;
1057 }
1058 let Defs = [ESP], Uses = [EDI, ESI, EBP, EBX, EDX, ECX, EAX, ESP],
1059     mayStore = 1, neverHasSideEffects = 1, SchedRW = [WriteStore] in {
1060 def PUSHA32  : I<0x60, RawFrm, (outs), (ins), "pushal", [], IIC_PUSH_A>,
1061                OpSize32, Requires<[Not64BitMode]>;
1062 def PUSHA16  : I<0x60, RawFrm, (outs), (ins), "pushaw", [], IIC_PUSH_A>,
1063                OpSize16, Requires<[Not64BitMode]>;
1064 }
1065
1066 let Constraints = "$src = $dst", SchedRW = [WriteALU] in {
1067 // GR32 = bswap GR32
1068 def BSWAP32r : I<0xC8, AddRegFrm,
1069                  (outs GR32:$dst), (ins GR32:$src),
1070                  "bswap{l}\t$dst",
1071                  [(set GR32:$dst, (bswap GR32:$src))], IIC_BSWAP>, OpSize32, TB;
1072
1073 def BSWAP64r : RI<0xC8, AddRegFrm, (outs GR64:$dst), (ins GR64:$src),
1074                   "bswap{q}\t$dst",
1075                   [(set GR64:$dst, (bswap GR64:$src))], IIC_BSWAP>, TB;
1076 } // Constraints = "$src = $dst", SchedRW
1077
1078 // Bit scan instructions.
1079 let Defs = [EFLAGS] in {
1080 def BSF16rr  : I<0xBC, MRMSrcReg, (outs GR16:$dst), (ins GR16:$src),
1081                  "bsf{w}\t{$src, $dst|$dst, $src}",
1082                  [(set GR16:$dst, EFLAGS, (X86bsf GR16:$src))],
1083                   IIC_BIT_SCAN_REG>, PS, OpSize16, Sched<[WriteShift]>;
1084 def BSF16rm  : I<0xBC, MRMSrcMem, (outs GR16:$dst), (ins i16mem:$src),
1085                  "bsf{w}\t{$src, $dst|$dst, $src}",
1086                  [(set GR16:$dst, EFLAGS, (X86bsf (loadi16 addr:$src)))],
1087                   IIC_BIT_SCAN_MEM>, PS, OpSize16, Sched<[WriteShiftLd]>;
1088 def BSF32rr  : I<0xBC, MRMSrcReg, (outs GR32:$dst), (ins GR32:$src),
1089                  "bsf{l}\t{$src, $dst|$dst, $src}",
1090                  [(set GR32:$dst, EFLAGS, (X86bsf GR32:$src))],
1091                  IIC_BIT_SCAN_REG>, PS, OpSize32, Sched<[WriteShift]>;
1092 def BSF32rm  : I<0xBC, MRMSrcMem, (outs GR32:$dst), (ins i32mem:$src),
1093                  "bsf{l}\t{$src, $dst|$dst, $src}",
1094                  [(set GR32:$dst, EFLAGS, (X86bsf (loadi32 addr:$src)))],
1095                  IIC_BIT_SCAN_MEM>, PS, OpSize32, Sched<[WriteShiftLd]>;
1096 def BSF64rr  : RI<0xBC, MRMSrcReg, (outs GR64:$dst), (ins GR64:$src),
1097                   "bsf{q}\t{$src, $dst|$dst, $src}",
1098                   [(set GR64:$dst, EFLAGS, (X86bsf GR64:$src))],
1099                   IIC_BIT_SCAN_REG>, PS, Sched<[WriteShift]>;
1100 def BSF64rm  : RI<0xBC, MRMSrcMem, (outs GR64:$dst), (ins i64mem:$src),
1101                   "bsf{q}\t{$src, $dst|$dst, $src}",
1102                   [(set GR64:$dst, EFLAGS, (X86bsf (loadi64 addr:$src)))],
1103                   IIC_BIT_SCAN_MEM>, PS, Sched<[WriteShiftLd]>;
1104
1105 def BSR16rr  : I<0xBD, MRMSrcReg, (outs GR16:$dst), (ins GR16:$src),
1106                  "bsr{w}\t{$src, $dst|$dst, $src}",
1107                  [(set GR16:$dst, EFLAGS, (X86bsr GR16:$src))],
1108                  IIC_BIT_SCAN_REG>, PS, OpSize16, Sched<[WriteShift]>;
1109 def BSR16rm  : I<0xBD, MRMSrcMem, (outs GR16:$dst), (ins i16mem:$src),
1110                  "bsr{w}\t{$src, $dst|$dst, $src}",
1111                  [(set GR16:$dst, EFLAGS, (X86bsr (loadi16 addr:$src)))],
1112                  IIC_BIT_SCAN_MEM>, PS, OpSize16, Sched<[WriteShiftLd]>;
1113 def BSR32rr  : I<0xBD, MRMSrcReg, (outs GR32:$dst), (ins GR32:$src),
1114                  "bsr{l}\t{$src, $dst|$dst, $src}",
1115                  [(set GR32:$dst, EFLAGS, (X86bsr GR32:$src))],
1116                  IIC_BIT_SCAN_REG>, PS, OpSize32, Sched<[WriteShift]>;
1117 def BSR32rm  : I<0xBD, MRMSrcMem, (outs GR32:$dst), (ins i32mem:$src),
1118                  "bsr{l}\t{$src, $dst|$dst, $src}",
1119                  [(set GR32:$dst, EFLAGS, (X86bsr (loadi32 addr:$src)))],
1120                  IIC_BIT_SCAN_MEM>, PS, OpSize32, Sched<[WriteShiftLd]>;
1121 def BSR64rr  : RI<0xBD, MRMSrcReg, (outs GR64:$dst), (ins GR64:$src),
1122                   "bsr{q}\t{$src, $dst|$dst, $src}",
1123                   [(set GR64:$dst, EFLAGS, (X86bsr GR64:$src))],
1124                   IIC_BIT_SCAN_REG>, PS, Sched<[WriteShift]>;
1125 def BSR64rm  : RI<0xBD, MRMSrcMem, (outs GR64:$dst), (ins i64mem:$src),
1126                   "bsr{q}\t{$src, $dst|$dst, $src}",
1127                   [(set GR64:$dst, EFLAGS, (X86bsr (loadi64 addr:$src)))],
1128                   IIC_BIT_SCAN_MEM>, PS, Sched<[WriteShiftLd]>;
1129 } // Defs = [EFLAGS]
1130
1131 let SchedRW = [WriteMicrocoded] in {
1132 // These uses the DF flag in the EFLAGS register to inc or dec EDI and ESI
1133 let Defs = [EDI,ESI], Uses = [EDI,ESI,EFLAGS] in {
1134 def MOVSB : I<0xA4, RawFrmDstSrc, (outs dstidx8:$dst), (ins srcidx8:$src),
1135               "movsb\t{$src, $dst|$dst, $src}", [], IIC_MOVS>;
1136 def MOVSW : I<0xA5, RawFrmDstSrc, (outs dstidx16:$dst), (ins srcidx16:$src),
1137               "movsw\t{$src, $dst|$dst, $src}", [], IIC_MOVS>, OpSize16;
1138 def MOVSL : I<0xA5, RawFrmDstSrc, (outs dstidx32:$dst), (ins srcidx32:$src),
1139               "movs{l|d}\t{$src, $dst|$dst, $src}", [], IIC_MOVS>, OpSize32;
1140 def MOVSQ : RI<0xA5, RawFrmDstSrc, (outs dstidx64:$dst), (ins srcidx64:$src),
1141                "movsq\t{$src, $dst|$dst, $src}", [], IIC_MOVS>;
1142 }
1143
1144 // These uses the DF flag in the EFLAGS register to inc or dec EDI and ESI
1145 let Defs = [EDI], Uses = [AL,EDI,EFLAGS] in
1146 def STOSB : I<0xAA, RawFrmDst, (outs dstidx8:$dst), (ins),
1147               "stosb\t{%al, $dst|$dst, al}", [], IIC_STOS>;
1148 let Defs = [EDI], Uses = [AX,EDI,EFLAGS] in
1149 def STOSW : I<0xAB, RawFrmDst, (outs dstidx16:$dst), (ins),
1150               "stosw\t{%ax, $dst|$dst, ax}", [], IIC_STOS>, OpSize16;
1151 let Defs = [EDI], Uses = [EAX,EDI,EFLAGS] in
1152 def STOSL : I<0xAB, RawFrmDst, (outs dstidx32:$dst), (ins),
1153               "stos{l|d}\t{%eax, $dst|$dst, eax}", [], IIC_STOS>, OpSize32;
1154 let Defs = [RCX,RDI], Uses = [RAX,RCX,RDI,EFLAGS] in
1155 def STOSQ : RI<0xAB, RawFrmDst, (outs dstidx64:$dst), (ins),
1156                "stosq\t{%rax, $dst|$dst, rax}", [], IIC_STOS>;
1157
1158 // These uses the DF flag in the EFLAGS register to inc or dec EDI and ESI
1159 let Defs = [EDI,EFLAGS], Uses = [AL,EDI,EFLAGS] in
1160 def SCASB : I<0xAE, RawFrmDst, (outs), (ins dstidx8:$dst),
1161               "scasb\t{$dst, %al|al, $dst}", [], IIC_SCAS>;
1162 let Defs = [EDI,EFLAGS], Uses = [AX,EDI,EFLAGS] in
1163 def SCASW : I<0xAF, RawFrmDst, (outs), (ins dstidx16:$dst),
1164               "scasw\t{$dst, %ax|ax, $dst}", [], IIC_SCAS>, OpSize16;
1165 let Defs = [EDI,EFLAGS], Uses = [EAX,EDI,EFLAGS] in
1166 def SCASL : I<0xAF, RawFrmDst, (outs), (ins dstidx32:$dst),
1167               "scas{l|d}\t{$dst, %eax|eax, $dst}", [], IIC_SCAS>, OpSize32;
1168 let Defs = [EDI,EFLAGS], Uses = [RAX,EDI,EFLAGS] in
1169 def SCASQ : RI<0xAF, RawFrmDst, (outs), (ins dstidx64:$dst),
1170                "scasq\t{$dst, %rax|rax, $dst}", [], IIC_SCAS>;
1171
1172 // These uses the DF flag in the EFLAGS register to inc or dec EDI and ESI
1173 let Defs = [EDI,ESI,EFLAGS], Uses = [EDI,ESI,EFLAGS] in {
1174 def CMPSB : I<0xA6, RawFrmDstSrc, (outs), (ins dstidx8:$dst, srcidx8:$src),
1175               "cmpsb\t{$dst, $src|$src, $dst}", [], IIC_CMPS>;
1176 def CMPSW : I<0xA7, RawFrmDstSrc, (outs), (ins dstidx16:$dst, srcidx16:$src),
1177               "cmpsw\t{$dst, $src|$src, $dst}", [], IIC_CMPS>, OpSize16;
1178 def CMPSL : I<0xA7, RawFrmDstSrc, (outs), (ins dstidx32:$dst, srcidx32:$src),
1179               "cmps{l|d}\t{$dst, $src|$src, $dst}", [], IIC_CMPS>, OpSize32;
1180 def CMPSQ : RI<0xA7, RawFrmDstSrc, (outs), (ins dstidx64:$dst, srcidx64:$src),
1181                "cmpsq\t{$dst, $src|$src, $dst}", [], IIC_CMPS>;
1182 }
1183 } // SchedRW
1184
1185 //===----------------------------------------------------------------------===//
1186 //  Move Instructions.
1187 //
1188 let SchedRW = [WriteMove] in {
1189 let neverHasSideEffects = 1 in {
1190 def MOV8rr  : I<0x88, MRMDestReg, (outs GR8 :$dst), (ins GR8 :$src),
1191                 "mov{b}\t{$src, $dst|$dst, $src}", [], IIC_MOV>;
1192 def MOV16rr : I<0x89, MRMDestReg, (outs GR16:$dst), (ins GR16:$src),
1193                 "mov{w}\t{$src, $dst|$dst, $src}", [], IIC_MOV>, OpSize16;
1194 def MOV32rr : I<0x89, MRMDestReg, (outs GR32:$dst), (ins GR32:$src),
1195                 "mov{l}\t{$src, $dst|$dst, $src}", [], IIC_MOV>, OpSize32;
1196 def MOV64rr : RI<0x89, MRMDestReg, (outs GR64:$dst), (ins GR64:$src),
1197                  "mov{q}\t{$src, $dst|$dst, $src}", [], IIC_MOV>;
1198 }
1199
1200 let isReMaterializable = 1, isAsCheapAsAMove = 1 in {
1201 def MOV8ri  : Ii8 <0xB0, AddRegFrm, (outs GR8 :$dst), (ins i8imm :$src),
1202                    "mov{b}\t{$src, $dst|$dst, $src}",
1203                    [(set GR8:$dst, imm:$src)], IIC_MOV>;
1204 def MOV16ri : Ii16<0xB8, AddRegFrm, (outs GR16:$dst), (ins i16imm:$src),
1205                    "mov{w}\t{$src, $dst|$dst, $src}",
1206                    [(set GR16:$dst, imm:$src)], IIC_MOV>, OpSize16;
1207 def MOV32ri : Ii32<0xB8, AddRegFrm, (outs GR32:$dst), (ins i32imm:$src),
1208                    "mov{l}\t{$src, $dst|$dst, $src}",
1209                    [(set GR32:$dst, imm:$src)], IIC_MOV>, OpSize32;
1210 def MOV64ri32 : RIi32S<0xC7, MRM0r, (outs GR64:$dst), (ins i64i32imm:$src),
1211                        "mov{q}\t{$src, $dst|$dst, $src}",
1212                        [(set GR64:$dst, i64immSExt32:$src)], IIC_MOV>;
1213 }
1214 let isReMaterializable = 1 in {
1215 def MOV64ri : RIi64<0xB8, AddRegFrm, (outs GR64:$dst), (ins i64imm:$src),
1216                     "movabs{q}\t{$src, $dst|$dst, $src}",
1217                     [(set GR64:$dst, imm:$src)], IIC_MOV>;
1218 }
1219
1220 // Longer forms that use a ModR/M byte. Needed for disassembler
1221 let isCodeGenOnly = 1, ForceDisassemble = 1, hasSideEffects = 0 in {
1222 def MOV8ri_alt  : Ii8 <0xC6, MRM0r, (outs GR8 :$dst), (ins i8imm :$src),
1223                    "mov{b}\t{$src, $dst|$dst, $src}", [], IIC_MOV>;
1224 def MOV16ri_alt : Ii16<0xC7, MRM0r, (outs GR16:$dst), (ins i16imm:$src),
1225                    "mov{w}\t{$src, $dst|$dst, $src}", [], IIC_MOV>, OpSize16;
1226 def MOV32ri_alt : Ii32<0xC7, MRM0r, (outs GR32:$dst), (ins i32imm:$src),
1227                    "mov{l}\t{$src, $dst|$dst, $src}", [], IIC_MOV>, OpSize32;
1228 }
1229 } // SchedRW
1230
1231 let SchedRW = [WriteStore] in {
1232 def MOV8mi  : Ii8 <0xC6, MRM0m, (outs), (ins i8mem :$dst, i8imm :$src),
1233                    "mov{b}\t{$src, $dst|$dst, $src}",
1234                    [(store (i8 imm:$src), addr:$dst)], IIC_MOV_MEM>;
1235 def MOV16mi : Ii16<0xC7, MRM0m, (outs), (ins i16mem:$dst, i16imm:$src),
1236                    "mov{w}\t{$src, $dst|$dst, $src}",
1237                    [(store (i16 imm:$src), addr:$dst)], IIC_MOV_MEM>, OpSize16;
1238 def MOV32mi : Ii32<0xC7, MRM0m, (outs), (ins i32mem:$dst, i32imm:$src),
1239                    "mov{l}\t{$src, $dst|$dst, $src}",
1240                    [(store (i32 imm:$src), addr:$dst)], IIC_MOV_MEM>, OpSize32;
1241 def MOV64mi32 : RIi32S<0xC7, MRM0m, (outs), (ins i64mem:$dst, i64i32imm:$src),
1242                        "mov{q}\t{$src, $dst|$dst, $src}",
1243                        [(store i64immSExt32:$src, addr:$dst)], IIC_MOV_MEM>;
1244 } // SchedRW
1245
1246 let hasSideEffects = 0 in {
1247
1248 /// moffs8, moffs16 and moffs32 versions of moves.  The immediate is a
1249 /// 32-bit offset from the segment base. These are only valid in x86-32 mode.
1250 let SchedRW = [WriteALU] in {
1251 let mayLoad = 1 in {
1252 let Defs = [AL] in
1253 def MOV8o8a : Ii32 <0xA0, RawFrmMemOffs, (outs), (ins offset8:$src),
1254                    "mov{b}\t{$src, %al|al, $src}", [], IIC_MOV_MEM>,
1255                    Requires<[In32BitMode]>;
1256 let Defs = [AX] in
1257 def MOV16o16a : Ii32 <0xA1, RawFrmMemOffs, (outs), (ins offset16:$src),
1258                       "mov{w}\t{$src, %ax|ax, $src}", [], IIC_MOV_MEM>,
1259                       OpSize16, Requires<[In32BitMode]>;
1260 let Defs = [EAX] in
1261 def MOV32o32a : Ii32 <0xA1, RawFrmMemOffs, (outs), (ins offset32:$src),
1262                       "mov{l}\t{$src, %eax|eax, $src}", [], IIC_MOV_MEM>,
1263                       OpSize32, Requires<[In32BitMode]>;
1264
1265 let Defs = [AL] in
1266 def MOV8o8a_16 : Ii16 <0xA0, RawFrmMemOffs, (outs), (ins offset8:$src),
1267                    "mov{b}\t{$src, %al|al, $src}", [], IIC_MOV_MEM>,
1268                    AdSize, Requires<[In16BitMode]>;
1269 let Defs = [AX] in
1270 def MOV16o16a_16 : Ii16 <0xA1, RawFrmMemOffs, (outs), (ins offset16:$src),
1271                       "mov{w}\t{$src, %ax|ax, $src}", [], IIC_MOV_MEM>,
1272                       OpSize16, AdSize, Requires<[In16BitMode]>;
1273 let Defs = [EAX] in
1274 def MOV32o32a_16 : Ii16 <0xA1, RawFrmMemOffs, (outs), (ins offset32:$src),
1275                       "mov{l}\t{$src, %eax|eax, $src}", [], IIC_MOV_MEM>,
1276                       AdSize, OpSize32, Requires<[In16BitMode]>;
1277 }
1278 let mayStore = 1 in {
1279 let Uses = [AL] in
1280 def MOV8ao8 : Ii32 <0xA2, RawFrmMemOffs, (outs offset8:$dst), (ins),
1281                    "mov{b}\t{%al, $dst|$dst, al}", [], IIC_MOV_MEM>,
1282                   Requires<[In32BitMode]>;
1283 let Uses = [AX] in
1284 def MOV16ao16 : Ii32 <0xA3, RawFrmMemOffs, (outs offset16:$dst), (ins),
1285                       "mov{w}\t{%ax, $dst|$dst, ax}", [], IIC_MOV_MEM>,
1286                       OpSize16, Requires<[In32BitMode]>;
1287 let Uses = [EAX] in
1288 def MOV32ao32 : Ii32 <0xA3, RawFrmMemOffs, (outs offset32:$dst), (ins),
1289                       "mov{l}\t{%eax, $dst|$dst, eax}", [], IIC_MOV_MEM>,
1290                      OpSize32, Requires<[In32BitMode]>;
1291
1292 let Uses = [AL] in
1293 def MOV8ao8_16 : Ii16 <0xA2, RawFrmMemOffs, (outs offset8:$dst), (ins),
1294                    "mov{b}\t{%al, $dst|$dst, al}", [], IIC_MOV_MEM>,
1295                   AdSize, Requires<[In16BitMode]>;
1296 let Uses = [AX] in
1297 def MOV16ao16_16 : Ii16 <0xA3, RawFrmMemOffs, (outs offset16:$dst), (ins),
1298                       "mov{w}\t{%ax, $dst|$dst, ax}", [], IIC_MOV_MEM>,
1299                       OpSize16, AdSize, Requires<[In16BitMode]>;
1300 let Uses = [EAX] in
1301 def MOV32ao32_16 : Ii16 <0xA3, RawFrmMemOffs, (outs offset32:$dst), (ins),
1302                       "mov{l}\t{%eax, $dst|$dst, eax}", [], IIC_MOV_MEM>,
1303                      OpSize32, AdSize, Requires<[In16BitMode]>;
1304 }
1305 }
1306
1307 // These forms all have full 64-bit absolute addresses in their instructions
1308 // and use the movabs mnemonic to indicate this specific form.
1309 let mayLoad = 1 in {
1310 let Defs = [AL] in
1311 def MOV64o8a : RIi64_NOREX<0xA0, RawFrmMemOffs, (outs), (ins offset8:$src),
1312                      "movabs{b}\t{$src, %al|al, $src}", []>,
1313                      Requires<[In64BitMode]>;
1314 let Defs = [AX] in
1315 def MOV64o16a : RIi64_NOREX<0xA1, RawFrmMemOffs, (outs), (ins offset16:$src),
1316                      "movabs{w}\t{$src, %ax|ax, $src}", []>, OpSize16,
1317                      Requires<[In64BitMode]>;
1318 let Defs = [EAX] in
1319 def MOV64o32a : RIi64_NOREX<0xA1, RawFrmMemOffs, (outs), (ins offset32:$src),
1320                      "movabs{l}\t{$src, %eax|eax, $src}", []>, OpSize32,
1321                      Requires<[In64BitMode]>;
1322 let Defs = [RAX] in
1323 def MOV64o64a : RIi64<0xA1, RawFrmMemOffs, (outs), (ins offset64:$src),
1324                      "movabs{q}\t{$src, %rax|rax, $src}", []>,
1325                      Requires<[In64BitMode]>;
1326 }
1327
1328 let mayStore = 1 in {
1329 let Uses = [AL] in
1330 def MOV64ao8 : RIi64_NOREX<0xA2, RawFrmMemOffs, (outs offset8:$dst), (ins),
1331                      "movabs{b}\t{%al, $dst|$dst, al}", []>,
1332                      Requires<[In64BitMode]>;
1333 let Uses = [AX] in
1334 def MOV64ao16 : RIi64_NOREX<0xA3, RawFrmMemOffs, (outs offset16:$dst), (ins),
1335                      "movabs{w}\t{%ax, $dst|$dst, ax}", []>, OpSize16,
1336                      Requires<[In64BitMode]>;
1337 let Uses = [EAX] in
1338 def MOV64ao32 : RIi64_NOREX<0xA3, RawFrmMemOffs, (outs offset32:$dst), (ins),
1339                      "movabs{l}\t{%eax, $dst|$dst, eax}", []>, OpSize32,
1340                      Requires<[In64BitMode]>;
1341 let Uses = [RAX] in
1342 def MOV64ao64 : RIi64<0xA3, RawFrmMemOffs, (outs offset64:$dst), (ins),
1343                      "movabs{q}\t{%rax, $dst|$dst, rax}", []>,
1344                      Requires<[In64BitMode]>;
1345 }
1346 } // hasSideEffects = 0
1347
1348 let isCodeGenOnly = 1, ForceDisassemble = 1, hasSideEffects = 0,
1349     SchedRW = [WriteMove] in {
1350 def MOV8rr_REV : I<0x8A, MRMSrcReg, (outs GR8:$dst), (ins GR8:$src),
1351                    "mov{b}\t{$src, $dst|$dst, $src}", [], IIC_MOV>;
1352 def MOV16rr_REV : I<0x8B, MRMSrcReg, (outs GR16:$dst), (ins GR16:$src),
1353                     "mov{w}\t{$src, $dst|$dst, $src}", [], IIC_MOV>, OpSize16;
1354 def MOV32rr_REV : I<0x8B, MRMSrcReg, (outs GR32:$dst), (ins GR32:$src),
1355                     "mov{l}\t{$src, $dst|$dst, $src}", [], IIC_MOV>, OpSize32;
1356 def MOV64rr_REV : RI<0x8B, MRMSrcReg, (outs GR64:$dst), (ins GR64:$src),
1357                      "mov{q}\t{$src, $dst|$dst, $src}", [], IIC_MOV>;
1358 }
1359
1360 let canFoldAsLoad = 1, isReMaterializable = 1, SchedRW = [WriteLoad] in {
1361 def MOV8rm  : I<0x8A, MRMSrcMem, (outs GR8 :$dst), (ins i8mem :$src),
1362                 "mov{b}\t{$src, $dst|$dst, $src}",
1363                 [(set GR8:$dst, (loadi8 addr:$src))], IIC_MOV_MEM>;
1364 def MOV16rm : I<0x8B, MRMSrcMem, (outs GR16:$dst), (ins i16mem:$src),
1365                 "mov{w}\t{$src, $dst|$dst, $src}",
1366                 [(set GR16:$dst, (loadi16 addr:$src))], IIC_MOV_MEM>, OpSize16;
1367 def MOV32rm : I<0x8B, MRMSrcMem, (outs GR32:$dst), (ins i32mem:$src),
1368                 "mov{l}\t{$src, $dst|$dst, $src}",
1369                 [(set GR32:$dst, (loadi32 addr:$src))], IIC_MOV_MEM>, OpSize32;
1370 def MOV64rm : RI<0x8B, MRMSrcMem, (outs GR64:$dst), (ins i64mem:$src),
1371                  "mov{q}\t{$src, $dst|$dst, $src}",
1372                  [(set GR64:$dst, (load addr:$src))], IIC_MOV_MEM>;
1373 }
1374
1375 let SchedRW = [WriteStore] in {
1376 def MOV8mr  : I<0x88, MRMDestMem, (outs), (ins i8mem :$dst, GR8 :$src),
1377                 "mov{b}\t{$src, $dst|$dst, $src}",
1378                 [(store GR8:$src, addr:$dst)], IIC_MOV_MEM>;
1379 def MOV16mr : I<0x89, MRMDestMem, (outs), (ins i16mem:$dst, GR16:$src),
1380                 "mov{w}\t{$src, $dst|$dst, $src}",
1381                 [(store GR16:$src, addr:$dst)], IIC_MOV_MEM>, OpSize16;
1382 def MOV32mr : I<0x89, MRMDestMem, (outs), (ins i32mem:$dst, GR32:$src),
1383                 "mov{l}\t{$src, $dst|$dst, $src}",
1384                 [(store GR32:$src, addr:$dst)], IIC_MOV_MEM>, OpSize32;
1385 def MOV64mr : RI<0x89, MRMDestMem, (outs), (ins i64mem:$dst, GR64:$src),
1386                  "mov{q}\t{$src, $dst|$dst, $src}",
1387                  [(store GR64:$src, addr:$dst)], IIC_MOV_MEM>;
1388 } // SchedRW
1389
1390 // Versions of MOV8rr, MOV8mr, and MOV8rm that use i8mem_NOREX and GR8_NOREX so
1391 // that they can be used for copying and storing h registers, which can't be
1392 // encoded when a REX prefix is present.
1393 let isCodeGenOnly = 1 in {
1394 let neverHasSideEffects = 1 in
1395 def MOV8rr_NOREX : I<0x88, MRMDestReg,
1396                      (outs GR8_NOREX:$dst), (ins GR8_NOREX:$src),
1397                      "mov{b}\t{$src, $dst|$dst, $src}  # NOREX", [], IIC_MOV>,
1398                    Sched<[WriteMove]>;
1399 let mayStore = 1, neverHasSideEffects = 1 in
1400 def MOV8mr_NOREX : I<0x88, MRMDestMem,
1401                      (outs), (ins i8mem_NOREX:$dst, GR8_NOREX:$src),
1402                      "mov{b}\t{$src, $dst|$dst, $src}  # NOREX", [],
1403                      IIC_MOV_MEM>, Sched<[WriteStore]>;
1404 let mayLoad = 1, neverHasSideEffects = 1,
1405     canFoldAsLoad = 1, isReMaterializable = 1 in
1406 def MOV8rm_NOREX : I<0x8A, MRMSrcMem,
1407                      (outs GR8_NOREX:$dst), (ins i8mem_NOREX:$src),
1408                      "mov{b}\t{$src, $dst|$dst, $src}  # NOREX", [],
1409                      IIC_MOV_MEM>, Sched<[WriteLoad]>;
1410 }
1411
1412
1413 // Condition code ops, incl. set if equal/not equal/...
1414 let SchedRW = [WriteALU] in {
1415 let Defs = [EFLAGS], Uses = [AH] in
1416 def SAHF     : I<0x9E, RawFrm, (outs),  (ins), "sahf",
1417                  [(set EFLAGS, (X86sahf AH))], IIC_AHF>;
1418 let Defs = [AH], Uses = [EFLAGS], neverHasSideEffects = 1 in
1419 def LAHF     : I<0x9F, RawFrm, (outs),  (ins), "lahf", [],
1420                 IIC_AHF>;  // AH = flags
1421 } // SchedRW
1422
1423 //===----------------------------------------------------------------------===//
1424 // Bit tests instructions: BT, BTS, BTR, BTC.
1425
1426 let Defs = [EFLAGS] in {
1427 let SchedRW = [WriteALU] in {
1428 def BT16rr : I<0xA3, MRMDestReg, (outs), (ins GR16:$src1, GR16:$src2),
1429                "bt{w}\t{$src2, $src1|$src1, $src2}",
1430                [(set EFLAGS, (X86bt GR16:$src1, GR16:$src2))], IIC_BT_RR>,
1431                OpSize16, TB;
1432 def BT32rr : I<0xA3, MRMDestReg, (outs), (ins GR32:$src1, GR32:$src2),
1433                "bt{l}\t{$src2, $src1|$src1, $src2}",
1434                [(set EFLAGS, (X86bt GR32:$src1, GR32:$src2))], IIC_BT_RR>,
1435                OpSize32, TB;
1436 def BT64rr : RI<0xA3, MRMDestReg, (outs), (ins GR64:$src1, GR64:$src2),
1437                "bt{q}\t{$src2, $src1|$src1, $src2}",
1438                [(set EFLAGS, (X86bt GR64:$src1, GR64:$src2))], IIC_BT_RR>, TB;
1439 } // SchedRW
1440
1441 // Unlike with the register+register form, the memory+register form of the
1442 // bt instruction does not ignore the high bits of the index. From ISel's
1443 // perspective, this is pretty bizarre. Make these instructions disassembly
1444 // only for now.
1445
1446 let mayLoad = 1, hasSideEffects = 0, SchedRW = [WriteALULd] in {
1447   def BT16mr : I<0xA3, MRMDestMem, (outs), (ins i16mem:$src1, GR16:$src2),
1448                  "bt{w}\t{$src2, $src1|$src1, $src2}",
1449   //               [(X86bt (loadi16 addr:$src1), GR16:$src2),
1450   //                (implicit EFLAGS)]
1451                  [], IIC_BT_MR
1452                  >, OpSize16, TB, Requires<[FastBTMem]>;
1453   def BT32mr : I<0xA3, MRMDestMem, (outs), (ins i32mem:$src1, GR32:$src2),
1454                  "bt{l}\t{$src2, $src1|$src1, $src2}",
1455   //               [(X86bt (loadi32 addr:$src1), GR32:$src2),
1456   //                (implicit EFLAGS)]
1457                  [], IIC_BT_MR
1458                  >, OpSize32, TB, Requires<[FastBTMem]>;
1459   def BT64mr : RI<0xA3, MRMDestMem, (outs), (ins i64mem:$src1, GR64:$src2),
1460                  "bt{q}\t{$src2, $src1|$src1, $src2}",
1461   //               [(X86bt (loadi64 addr:$src1), GR64:$src2),
1462   //                (implicit EFLAGS)]
1463                   [], IIC_BT_MR
1464                   >, TB;
1465 }
1466
1467 let SchedRW = [WriteALU] in {
1468 def BT16ri8 : Ii8<0xBA, MRM4r, (outs), (ins GR16:$src1, i16i8imm:$src2),
1469                 "bt{w}\t{$src2, $src1|$src1, $src2}",
1470                 [(set EFLAGS, (X86bt GR16:$src1, i16immSExt8:$src2))],
1471                 IIC_BT_RI>, OpSize16, TB;
1472 def BT32ri8 : Ii8<0xBA, MRM4r, (outs), (ins GR32:$src1, i32i8imm:$src2),
1473                 "bt{l}\t{$src2, $src1|$src1, $src2}",
1474                 [(set EFLAGS, (X86bt GR32:$src1, i32immSExt8:$src2))],
1475                 IIC_BT_RI>, OpSize32, TB;
1476 def BT64ri8 : RIi8<0xBA, MRM4r, (outs), (ins GR64:$src1, i64i8imm:$src2),
1477                 "bt{q}\t{$src2, $src1|$src1, $src2}",
1478                 [(set EFLAGS, (X86bt GR64:$src1, i64immSExt8:$src2))],
1479                 IIC_BT_RI>, TB;
1480 } // SchedRW
1481
1482 // Note that these instructions don't need FastBTMem because that
1483 // only applies when the other operand is in a register. When it's
1484 // an immediate, bt is still fast.
1485 let SchedRW = [WriteALU] in {
1486 def BT16mi8 : Ii8<0xBA, MRM4m, (outs), (ins i16mem:$src1, i16i8imm:$src2),
1487                 "bt{w}\t{$src2, $src1|$src1, $src2}",
1488                 [(set EFLAGS, (X86bt (loadi16 addr:$src1), i16immSExt8:$src2))
1489                  ], IIC_BT_MI>, OpSize16, TB;
1490 def BT32mi8 : Ii8<0xBA, MRM4m, (outs), (ins i32mem:$src1, i32i8imm:$src2),
1491                 "bt{l}\t{$src2, $src1|$src1, $src2}",
1492                 [(set EFLAGS, (X86bt (loadi32 addr:$src1), i32immSExt8:$src2))
1493                  ], IIC_BT_MI>, OpSize32, TB;
1494 def BT64mi8 : RIi8<0xBA, MRM4m, (outs), (ins i64mem:$src1, i64i8imm:$src2),
1495                 "bt{q}\t{$src2, $src1|$src1, $src2}",
1496                 [(set EFLAGS, (X86bt (loadi64 addr:$src1),
1497                                      i64immSExt8:$src2))], IIC_BT_MI>, TB;
1498 } // SchedRW
1499
1500 let hasSideEffects = 0 in {
1501 let SchedRW = [WriteALU] in {
1502 def BTC16rr : I<0xBB, MRMDestReg, (outs), (ins GR16:$src1, GR16:$src2),
1503                 "btc{w}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_RR>,
1504                 OpSize16, TB;
1505 def BTC32rr : I<0xBB, MRMDestReg, (outs), (ins GR32:$src1, GR32:$src2),
1506                 "btc{l}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_RR>,
1507                 OpSize32, TB;
1508 def BTC64rr : RI<0xBB, MRMDestReg, (outs), (ins GR64:$src1, GR64:$src2),
1509                  "btc{q}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_RR>, TB;
1510 } // SchedRW
1511
1512 let mayLoad = 1, mayStore = 1, SchedRW = [WriteALULd, WriteRMW] in {
1513 def BTC16mr : I<0xBB, MRMDestMem, (outs), (ins i16mem:$src1, GR16:$src2),
1514                 "btc{w}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_MR>,
1515                 OpSize16, TB;
1516 def BTC32mr : I<0xBB, MRMDestMem, (outs), (ins i32mem:$src1, GR32:$src2),
1517                 "btc{l}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_MR>,
1518                 OpSize32, TB;
1519 def BTC64mr : RI<0xBB, MRMDestMem, (outs), (ins i64mem:$src1, GR64:$src2),
1520                  "btc{q}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_MR>, TB;
1521 }
1522
1523 let SchedRW = [WriteALU] in {
1524 def BTC16ri8 : Ii8<0xBA, MRM7r, (outs), (ins GR16:$src1, i16i8imm:$src2),
1525                     "btc{w}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_RI>,
1526                     OpSize16, TB;
1527 def BTC32ri8 : Ii8<0xBA, MRM7r, (outs), (ins GR32:$src1, i32i8imm:$src2),
1528                     "btc{l}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_RI>,
1529                     OpSize32, TB;
1530 def BTC64ri8 : RIi8<0xBA, MRM7r, (outs), (ins GR64:$src1, i64i8imm:$src2),
1531                     "btc{q}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_RI>, TB;
1532 } // SchedRW
1533
1534 let mayLoad = 1, mayStore = 1, SchedRW = [WriteALULd, WriteRMW] in {
1535 def BTC16mi8 : Ii8<0xBA, MRM7m, (outs), (ins i16mem:$src1, i16i8imm:$src2),
1536                     "btc{w}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_MI>,
1537                     OpSize16, TB;
1538 def BTC32mi8 : Ii8<0xBA, MRM7m, (outs), (ins i32mem:$src1, i32i8imm:$src2),
1539                     "btc{l}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_MI>,
1540                     OpSize32, TB;
1541 def BTC64mi8 : RIi8<0xBA, MRM7m, (outs), (ins i64mem:$src1, i64i8imm:$src2),
1542                     "btc{q}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_MI>, TB;
1543 }
1544
1545 let SchedRW = [WriteALU] in {
1546 def BTR16rr : I<0xB3, MRMDestReg, (outs), (ins GR16:$src1, GR16:$src2),
1547                 "btr{w}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_RR>,
1548                 OpSize16, TB;
1549 def BTR32rr : I<0xB3, MRMDestReg, (outs), (ins GR32:$src1, GR32:$src2),
1550                 "btr{l}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_RR>,
1551                 OpSize32, TB;
1552 def BTR64rr : RI<0xB3, MRMDestReg, (outs), (ins GR64:$src1, GR64:$src2),
1553                  "btr{q}\t{$src2, $src1|$src1, $src2}", []>, TB;
1554 } // SchedRW
1555
1556 let mayLoad = 1, mayStore = 1, SchedRW = [WriteALULd, WriteRMW] in {
1557 def BTR16mr : I<0xB3, MRMDestMem, (outs), (ins i16mem:$src1, GR16:$src2),
1558                 "btr{w}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_MR>,
1559                 OpSize16, TB;
1560 def BTR32mr : I<0xB3, MRMDestMem, (outs), (ins i32mem:$src1, GR32:$src2),
1561                 "btr{l}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_MR>,
1562                 OpSize32, TB;
1563 def BTR64mr : RI<0xB3, MRMDestMem, (outs), (ins i64mem:$src1, GR64:$src2),
1564                  "btr{q}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_MR>, TB;
1565 }
1566
1567 let SchedRW = [WriteALU] in {
1568 def BTR16ri8 : Ii8<0xBA, MRM6r, (outs), (ins GR16:$src1, i16i8imm:$src2),
1569                     "btr{w}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_RI>,
1570                     OpSize16, TB;
1571 def BTR32ri8 : Ii8<0xBA, MRM6r, (outs), (ins GR32:$src1, i32i8imm:$src2),
1572                     "btr{l}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_RI>,
1573                     OpSize32, TB;
1574 def BTR64ri8 : RIi8<0xBA, MRM6r, (outs), (ins GR64:$src1, i64i8imm:$src2),
1575                     "btr{q}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_RI>, TB;
1576 } // SchedRW
1577
1578 let mayLoad = 1, mayStore = 1, SchedRW = [WriteALULd, WriteRMW] in {
1579 def BTR16mi8 : Ii8<0xBA, MRM6m, (outs), (ins i16mem:$src1, i16i8imm:$src2),
1580                     "btr{w}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_MI>,
1581                     OpSize16, TB;
1582 def BTR32mi8 : Ii8<0xBA, MRM6m, (outs), (ins i32mem:$src1, i32i8imm:$src2),
1583                     "btr{l}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_MI>,
1584                     OpSize32, TB;
1585 def BTR64mi8 : RIi8<0xBA, MRM6m, (outs), (ins i64mem:$src1, i64i8imm:$src2),
1586                     "btr{q}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_MI>, TB;
1587 }
1588
1589 let SchedRW = [WriteALU] in {
1590 def BTS16rr : I<0xAB, MRMDestReg, (outs), (ins GR16:$src1, GR16:$src2),
1591                 "bts{w}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_RR>,
1592                 OpSize16, TB;
1593 def BTS32rr : I<0xAB, MRMDestReg, (outs), (ins GR32:$src1, GR32:$src2),
1594                 "bts{l}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_RR>,
1595               OpSize32, TB;
1596 def BTS64rr : RI<0xAB, MRMDestReg, (outs), (ins GR64:$src1, GR64:$src2),
1597                "bts{q}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_RR>, TB;
1598 } // SchedRW
1599
1600 let mayLoad = 1, mayStore = 1, SchedRW = [WriteALULd, WriteRMW] in {
1601 def BTS16mr : I<0xAB, MRMDestMem, (outs), (ins i16mem:$src1, GR16:$src2),
1602               "bts{w}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_MR>,
1603               OpSize16, TB;
1604 def BTS32mr : I<0xAB, MRMDestMem, (outs), (ins i32mem:$src1, GR32:$src2),
1605               "bts{l}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_MR>,
1606               OpSize32, TB;
1607 def BTS64mr : RI<0xAB, MRMDestMem, (outs), (ins i64mem:$src1, GR64:$src2),
1608                  "bts{q}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_MR>, TB;
1609 }
1610
1611 let SchedRW = [WriteALU] in {
1612 def BTS16ri8 : Ii8<0xBA, MRM5r, (outs), (ins GR16:$src1, i16i8imm:$src2),
1613                     "bts{w}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_RI>,
1614                     OpSize16, TB;
1615 def BTS32ri8 : Ii8<0xBA, MRM5r, (outs), (ins GR32:$src1, i32i8imm:$src2),
1616                     "bts{l}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_RI>,
1617                     OpSize32, TB;
1618 def BTS64ri8 : RIi8<0xBA, MRM5r, (outs), (ins GR64:$src1, i64i8imm:$src2),
1619                     "bts{q}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_RI>, TB;
1620 } // SchedRW
1621
1622 let mayLoad = 1, mayStore = 1, SchedRW = [WriteALULd, WriteRMW] in {
1623 def BTS16mi8 : Ii8<0xBA, MRM5m, (outs), (ins i16mem:$src1, i16i8imm:$src2),
1624                     "bts{w}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_MI>,
1625                     OpSize16, TB;
1626 def BTS32mi8 : Ii8<0xBA, MRM5m, (outs), (ins i32mem:$src1, i32i8imm:$src2),
1627                     "bts{l}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_MI>,
1628                     OpSize32, TB;
1629 def BTS64mi8 : RIi8<0xBA, MRM5m, (outs), (ins i64mem:$src1, i64i8imm:$src2),
1630                     "bts{q}\t{$src2, $src1|$src1, $src2}", [], IIC_BTX_MI>, TB;
1631 }
1632 } // hasSideEffects = 0
1633 } // Defs = [EFLAGS]
1634
1635
1636 //===----------------------------------------------------------------------===//
1637 // Atomic support
1638 //
1639
1640 // Atomic swap. These are just normal xchg instructions. But since a memory
1641 // operand is referenced, the atomicity is ensured.
1642 multiclass ATOMIC_SWAP<bits<8> opc8, bits<8> opc, string mnemonic, string frag,
1643                        InstrItinClass itin> {
1644   let Constraints = "$val = $dst", SchedRW = [WriteALULd, WriteRMW] in {
1645     def NAME#8rm  : I<opc8, MRMSrcMem, (outs GR8:$dst),
1646                       (ins GR8:$val, i8mem:$ptr),
1647                       !strconcat(mnemonic, "{b}\t{$val, $ptr|$ptr, $val}"),
1648                       [(set
1649                          GR8:$dst,
1650                          (!cast<PatFrag>(frag # "_8") addr:$ptr, GR8:$val))],
1651                       itin>;
1652     def NAME#16rm : I<opc, MRMSrcMem, (outs GR16:$dst),
1653                       (ins GR16:$val, i16mem:$ptr),
1654                       !strconcat(mnemonic, "{w}\t{$val, $ptr|$ptr, $val}"),
1655                       [(set
1656                          GR16:$dst,
1657                          (!cast<PatFrag>(frag # "_16") addr:$ptr, GR16:$val))],
1658                       itin>, OpSize16;
1659     def NAME#32rm : I<opc, MRMSrcMem, (outs GR32:$dst),
1660                       (ins GR32:$val, i32mem:$ptr),
1661                       !strconcat(mnemonic, "{l}\t{$val, $ptr|$ptr, $val}"),
1662                       [(set
1663                          GR32:$dst,
1664                          (!cast<PatFrag>(frag # "_32") addr:$ptr, GR32:$val))],
1665                       itin>, OpSize32;
1666     def NAME#64rm : RI<opc, MRMSrcMem, (outs GR64:$dst),
1667                        (ins GR64:$val, i64mem:$ptr),
1668                        !strconcat(mnemonic, "{q}\t{$val, $ptr|$ptr, $val}"),
1669                        [(set
1670                          GR64:$dst,
1671                          (!cast<PatFrag>(frag # "_64") addr:$ptr, GR64:$val))],
1672                        itin>;
1673   }
1674 }
1675
1676 defm XCHG    : ATOMIC_SWAP<0x86, 0x87, "xchg", "atomic_swap", IIC_XCHG_MEM>;
1677
1678 // Swap between registers.
1679 let SchedRW = [WriteALU] in {
1680 let Constraints = "$val = $dst" in {
1681 def XCHG8rr : I<0x86, MRMSrcReg, (outs GR8:$dst), (ins GR8:$val, GR8:$src),
1682                 "xchg{b}\t{$val, $src|$src, $val}", [], IIC_XCHG_REG>;
1683 def XCHG16rr : I<0x87, MRMSrcReg, (outs GR16:$dst), (ins GR16:$val, GR16:$src),
1684                  "xchg{w}\t{$val, $src|$src, $val}", [], IIC_XCHG_REG>,
1685                  OpSize16;
1686 def XCHG32rr : I<0x87, MRMSrcReg, (outs GR32:$dst), (ins GR32:$val, GR32:$src),
1687                  "xchg{l}\t{$val, $src|$src, $val}", [], IIC_XCHG_REG>,
1688                  OpSize32;
1689 def XCHG64rr : RI<0x87, MRMSrcReg, (outs GR64:$dst), (ins GR64:$val,GR64:$src),
1690                   "xchg{q}\t{$val, $src|$src, $val}", [], IIC_XCHG_REG>;
1691 }
1692
1693 // Swap between EAX and other registers.
1694 let Uses = [AX], Defs = [AX] in
1695 def XCHG16ar : I<0x90, AddRegFrm, (outs), (ins GR16:$src),
1696                   "xchg{w}\t{$src, %ax|ax, $src}", [], IIC_XCHG_REG>, OpSize16;
1697 let Uses = [EAX], Defs = [EAX] in
1698 def XCHG32ar : I<0x90, AddRegFrm, (outs), (ins GR32:$src),
1699                   "xchg{l}\t{$src, %eax|eax, $src}", [], IIC_XCHG_REG>,
1700                   OpSize32, Requires<[Not64BitMode]>;
1701 let Uses = [EAX], Defs = [EAX] in
1702 // Uses GR32_NOAX in 64-bit mode to prevent encoding using the 0x90 NOP encoding.
1703 // xchg %eax, %eax needs to clear upper 32-bits of RAX so is not a NOP.
1704 def XCHG32ar64 : I<0x90, AddRegFrm, (outs), (ins GR32_NOAX:$src),
1705                    "xchg{l}\t{$src, %eax|eax, $src}", [], IIC_XCHG_REG>,
1706                    OpSize32, Requires<[In64BitMode]>;
1707 let Uses = [RAX], Defs = [RAX] in
1708 def XCHG64ar : RI<0x90, AddRegFrm, (outs), (ins GR64:$src),
1709                   "xchg{q}\t{$src, %rax|rax, $src}", [], IIC_XCHG_REG>;
1710 } // SchedRW
1711
1712 let SchedRW = [WriteALU] in {
1713 def XADD8rr : I<0xC0, MRMDestReg, (outs GR8:$dst), (ins GR8:$src),
1714                 "xadd{b}\t{$src, $dst|$dst, $src}", [], IIC_XADD_REG>, TB;
1715 def XADD16rr : I<0xC1, MRMDestReg, (outs GR16:$dst), (ins GR16:$src),
1716                  "xadd{w}\t{$src, $dst|$dst, $src}", [], IIC_XADD_REG>, TB,
1717                  OpSize16;
1718 def XADD32rr  : I<0xC1, MRMDestReg, (outs GR32:$dst), (ins GR32:$src),
1719                  "xadd{l}\t{$src, $dst|$dst, $src}", [], IIC_XADD_REG>, TB,
1720                  OpSize32;
1721 def XADD64rr  : RI<0xC1, MRMDestReg, (outs GR64:$dst), (ins GR64:$src),
1722                    "xadd{q}\t{$src, $dst|$dst, $src}", [], IIC_XADD_REG>, TB;
1723 } // SchedRW
1724
1725 let mayLoad = 1, mayStore = 1, SchedRW = [WriteALULd, WriteRMW] in {
1726 def XADD8rm   : I<0xC0, MRMDestMem, (outs), (ins i8mem:$dst, GR8:$src),
1727                  "xadd{b}\t{$src, $dst|$dst, $src}", [], IIC_XADD_MEM>, TB;
1728 def XADD16rm  : I<0xC1, MRMDestMem, (outs), (ins i16mem:$dst, GR16:$src),
1729                  "xadd{w}\t{$src, $dst|$dst, $src}", [], IIC_XADD_MEM>, TB,
1730                  OpSize16;
1731 def XADD32rm  : I<0xC1, MRMDestMem, (outs), (ins i32mem:$dst, GR32:$src),
1732                  "xadd{l}\t{$src, $dst|$dst, $src}", [], IIC_XADD_MEM>, TB,
1733                  OpSize32;
1734 def XADD64rm  : RI<0xC1, MRMDestMem, (outs), (ins i64mem:$dst, GR64:$src),
1735                    "xadd{q}\t{$src, $dst|$dst, $src}", [], IIC_XADD_MEM>, TB;
1736
1737 }
1738
1739 let SchedRW = [WriteALU] in {
1740 def CMPXCHG8rr : I<0xB0, MRMDestReg, (outs GR8:$dst), (ins GR8:$src),
1741                    "cmpxchg{b}\t{$src, $dst|$dst, $src}", [],
1742                    IIC_CMPXCHG_REG8>, TB;
1743 def CMPXCHG16rr : I<0xB1, MRMDestReg, (outs GR16:$dst), (ins GR16:$src),
1744                     "cmpxchg{w}\t{$src, $dst|$dst, $src}", [],
1745                     IIC_CMPXCHG_REG>, TB, OpSize16;
1746 def CMPXCHG32rr  : I<0xB1, MRMDestReg, (outs GR32:$dst), (ins GR32:$src),
1747                      "cmpxchg{l}\t{$src, $dst|$dst, $src}", [],
1748                      IIC_CMPXCHG_REG>, TB, OpSize32;
1749 def CMPXCHG64rr  : RI<0xB1, MRMDestReg, (outs GR64:$dst), (ins GR64:$src),
1750                       "cmpxchg{q}\t{$src, $dst|$dst, $src}", [],
1751                       IIC_CMPXCHG_REG>, TB;
1752 } // SchedRW
1753
1754 let SchedRW = [WriteALULd, WriteRMW] in {
1755 let mayLoad = 1, mayStore = 1 in {
1756 def CMPXCHG8rm   : I<0xB0, MRMDestMem, (outs), (ins i8mem:$dst, GR8:$src),
1757                      "cmpxchg{b}\t{$src, $dst|$dst, $src}", [],
1758                      IIC_CMPXCHG_MEM8>, TB;
1759 def CMPXCHG16rm  : I<0xB1, MRMDestMem, (outs), (ins i16mem:$dst, GR16:$src),
1760                      "cmpxchg{w}\t{$src, $dst|$dst, $src}", [],
1761                      IIC_CMPXCHG_MEM>, TB, OpSize16;
1762 def CMPXCHG32rm  : I<0xB1, MRMDestMem, (outs), (ins i32mem:$dst, GR32:$src),
1763                      "cmpxchg{l}\t{$src, $dst|$dst, $src}", [],
1764                      IIC_CMPXCHG_MEM>, TB, OpSize32;
1765 def CMPXCHG64rm  : RI<0xB1, MRMDestMem, (outs), (ins i64mem:$dst, GR64:$src),
1766                       "cmpxchg{q}\t{$src, $dst|$dst, $src}", [],
1767                       IIC_CMPXCHG_MEM>, TB;
1768 }
1769
1770 let Defs = [EAX, EDX, EFLAGS], Uses = [EAX, EBX, ECX, EDX] in
1771 def CMPXCHG8B : I<0xC7, MRM1m, (outs), (ins i64mem:$dst),
1772                   "cmpxchg8b\t$dst", [], IIC_CMPXCHG_8B>, TB;
1773
1774 let Defs = [RAX, RDX, EFLAGS], Uses = [RAX, RBX, RCX, RDX] in
1775 def CMPXCHG16B : RI<0xC7, MRM1m, (outs), (ins i128mem:$dst),
1776                     "cmpxchg16b\t$dst", [], IIC_CMPXCHG_16B>,
1777                     TB, Requires<[HasCmpxchg16b]>;
1778 } // SchedRW
1779
1780
1781 // Lock instruction prefix
1782 def LOCK_PREFIX : I<0xF0, RawFrm, (outs),  (ins), "lock", []>;
1783
1784 // Rex64 instruction prefix
1785 def REX64_PREFIX : I<0x48, RawFrm, (outs),  (ins), "rex64", []>,
1786                      Requires<[In64BitMode]>;
1787
1788 // Data16 instruction prefix
1789 def DATA16_PREFIX : I<0x66, RawFrm, (outs),  (ins), "data16", []>;
1790
1791 // Repeat string operation instruction prefixes
1792 // These uses the DF flag in the EFLAGS register to inc or dec ECX
1793 let Defs = [ECX], Uses = [ECX,EFLAGS] in {
1794 // Repeat (used with INS, OUTS, MOVS, LODS and STOS)
1795 def REP_PREFIX : I<0xF3, RawFrm, (outs),  (ins), "rep", []>;
1796 // Repeat while not equal (used with CMPS and SCAS)
1797 def REPNE_PREFIX : I<0xF2, RawFrm, (outs),  (ins), "repne", []>;
1798 }
1799
1800
1801 // String manipulation instructions
1802 let SchedRW = [WriteMicrocoded] in {
1803 // These uses the DF flag in the EFLAGS register to inc or dec EDI and ESI
1804 let Defs = [AL,ESI], Uses = [ESI,EFLAGS] in
1805 def LODSB : I<0xAC, RawFrmSrc, (outs), (ins srcidx8:$src),
1806               "lodsb\t{$src, %al|al, $src}", [], IIC_LODS>;
1807 let Defs = [AX,ESI], Uses = [ESI,EFLAGS] in
1808 def LODSW : I<0xAD, RawFrmSrc, (outs), (ins srcidx16:$src),
1809               "lodsw\t{$src, %ax|ax, $src}", [], IIC_LODS>, OpSize16;
1810 let Defs = [EAX,ESI], Uses = [ESI,EFLAGS] in
1811 def LODSL : I<0xAD, RawFrmSrc, (outs), (ins srcidx32:$src),
1812               "lods{l|d}\t{$src, %eax|eax, $src}", [], IIC_LODS>, OpSize32;
1813 let Defs = [RAX,ESI], Uses = [ESI,EFLAGS] in
1814 def LODSQ : RI<0xAD, RawFrmSrc, (outs), (ins srcidx64:$src),
1815                "lodsq\t{$src, %rax|rax, $src}", [], IIC_LODS>;
1816 }
1817
1818 let SchedRW = [WriteSystem] in {
1819 // These uses the DF flag in the EFLAGS register to inc or dec EDI and ESI
1820 let Defs = [ESI], Uses = [DX,ESI,EFLAGS] in {
1821 def OUTSB : I<0x6E, RawFrmSrc, (outs), (ins srcidx8:$src),
1822              "outsb\t{$src, %dx|dx, $src}", [], IIC_OUTS>;
1823 def OUTSW : I<0x6F, RawFrmSrc, (outs), (ins srcidx16:$src),
1824               "outsw\t{$src, %dx|dx, $src}", [], IIC_OUTS>, OpSize16;
1825 def OUTSL : I<0x6F, RawFrmSrc, (outs), (ins srcidx32:$src),
1826               "outs{l|d}\t{$src, %dx|dx, $src}", [], IIC_OUTS>, OpSize32;
1827 }
1828
1829 // These uses the DF flag in the EFLAGS register to inc or dec EDI and ESI
1830 let Defs = [EDI], Uses = [DX,EDI,EFLAGS] in {
1831 def INSB : I<0x6C, RawFrmDst, (outs dstidx8:$dst), (ins),
1832              "insb\t{%dx, $dst|$dst, dx}", [], IIC_INS>;
1833 def INSW : I<0x6D, RawFrmDst, (outs dstidx16:$dst), (ins),
1834              "insw\t{%dx, $dst|$dst, dx}", [], IIC_INS>,  OpSize16;
1835 def INSL : I<0x6D, RawFrmDst, (outs dstidx32:$dst), (ins),
1836              "ins{l|d}\t{%dx, $dst|$dst, dx}", [], IIC_INS>, OpSize32;
1837 }
1838 }
1839
1840 // Flag instructions
1841 let SchedRW = [WriteALU] in {
1842 def CLC : I<0xF8, RawFrm, (outs), (ins), "clc", [], IIC_CLC>;
1843 def STC : I<0xF9, RawFrm, (outs), (ins), "stc", [], IIC_STC>;
1844 def CLI : I<0xFA, RawFrm, (outs), (ins), "cli", [], IIC_CLI>;
1845 def STI : I<0xFB, RawFrm, (outs), (ins), "sti", [], IIC_STI>;
1846 def CLD : I<0xFC, RawFrm, (outs), (ins), "cld", [], IIC_CLD>;
1847 def STD : I<0xFD, RawFrm, (outs), (ins), "std", [], IIC_STD>;
1848 def CMC : I<0xF5, RawFrm, (outs), (ins), "cmc", [], IIC_CMC>;
1849
1850 def CLTS : I<0x06, RawFrm, (outs), (ins), "clts", [], IIC_CLTS>, TB;
1851 }
1852
1853 // Table lookup instructions
1854 def XLAT : I<0xD7, RawFrm, (outs), (ins), "xlatb", [], IIC_XLAT>,
1855            Sched<[WriteLoad]>;
1856
1857 let SchedRW = [WriteMicrocoded] in {
1858 // ASCII Adjust After Addition
1859 // sets AL, AH and CF and AF of EFLAGS and uses AL and AF of EFLAGS
1860 def AAA : I<0x37, RawFrm, (outs), (ins), "aaa", [], IIC_AAA>,
1861             Requires<[Not64BitMode]>;
1862
1863 // ASCII Adjust AX Before Division
1864 // sets AL, AH and EFLAGS and uses AL and AH
1865 def AAD8i8 : Ii8<0xD5, RawFrm, (outs), (ins i8imm:$src),
1866                  "aad\t$src", [], IIC_AAD>, Requires<[Not64BitMode]>;
1867
1868 // ASCII Adjust AX After Multiply
1869 // sets AL, AH and EFLAGS and uses AL
1870 def AAM8i8 : Ii8<0xD4, RawFrm, (outs), (ins i8imm:$src),
1871                  "aam\t$src", [], IIC_AAM>, Requires<[Not64BitMode]>;
1872
1873 // ASCII Adjust AL After Subtraction - sets
1874 // sets AL, AH and CF and AF of EFLAGS and uses AL and AF of EFLAGS
1875 def AAS : I<0x3F, RawFrm, (outs), (ins), "aas", [], IIC_AAS>,
1876             Requires<[Not64BitMode]>;
1877
1878 // Decimal Adjust AL after Addition
1879 // sets AL, CF and AF of EFLAGS and uses AL, CF and AF of EFLAGS
1880 def DAA : I<0x27, RawFrm, (outs), (ins), "daa", [], IIC_DAA>,
1881             Requires<[Not64BitMode]>;
1882
1883 // Decimal Adjust AL after Subtraction
1884 // sets AL, CF and AF of EFLAGS and uses AL, CF and AF of EFLAGS
1885 def DAS : I<0x2F, RawFrm, (outs), (ins), "das", [], IIC_DAS>,
1886             Requires<[Not64BitMode]>;
1887 } // SchedRW
1888
1889 let SchedRW = [WriteSystem] in {
1890 // Check Array Index Against Bounds
1891 def BOUNDS16rm : I<0x62, MRMSrcMem, (outs GR16:$dst), (ins i16mem:$src),
1892                    "bound\t{$src, $dst|$dst, $src}", [], IIC_BOUND>, OpSize16,
1893                    Requires<[Not64BitMode]>;
1894 def BOUNDS32rm : I<0x62, MRMSrcMem, (outs GR32:$dst), (ins i32mem:$src),
1895                    "bound\t{$src, $dst|$dst, $src}", [], IIC_BOUND>, OpSize32,
1896                    Requires<[Not64BitMode]>;
1897
1898 // Adjust RPL Field of Segment Selector
1899 def ARPL16rr : I<0x63, MRMDestReg, (outs GR16:$dst), (ins GR16:$src),
1900                  "arpl\t{$src, $dst|$dst, $src}", [], IIC_ARPL_REG>,
1901                  Requires<[Not64BitMode]>;
1902 def ARPL16mr : I<0x63, MRMDestMem, (outs), (ins i16mem:$dst, GR16:$src),
1903                  "arpl\t{$src, $dst|$dst, $src}", [], IIC_ARPL_MEM>,
1904                  Requires<[Not64BitMode]>;
1905 } // SchedRW
1906
1907 //===----------------------------------------------------------------------===//
1908 // MOVBE Instructions
1909 //
1910 let Predicates = [HasMOVBE] in {
1911   let SchedRW = [WriteALULd] in {
1912   def MOVBE16rm : I<0xF0, MRMSrcMem, (outs GR16:$dst), (ins i16mem:$src),
1913                     "movbe{w}\t{$src, $dst|$dst, $src}",
1914                     [(set GR16:$dst, (bswap (loadi16 addr:$src)))], IIC_MOVBE>,
1915                     OpSize16, T8PS;
1916   def MOVBE32rm : I<0xF0, MRMSrcMem, (outs GR32:$dst), (ins i32mem:$src),
1917                     "movbe{l}\t{$src, $dst|$dst, $src}",
1918                     [(set GR32:$dst, (bswap (loadi32 addr:$src)))], IIC_MOVBE>,
1919                     OpSize32, T8PS;
1920   def MOVBE64rm : RI<0xF0, MRMSrcMem, (outs GR64:$dst), (ins i64mem:$src),
1921                      "movbe{q}\t{$src, $dst|$dst, $src}",
1922                      [(set GR64:$dst, (bswap (loadi64 addr:$src)))], IIC_MOVBE>,
1923                      T8PS;
1924   }
1925   let SchedRW = [WriteStore] in {
1926   def MOVBE16mr : I<0xF1, MRMDestMem, (outs), (ins i16mem:$dst, GR16:$src),
1927                     "movbe{w}\t{$src, $dst|$dst, $src}",
1928                     [(store (bswap GR16:$src), addr:$dst)], IIC_MOVBE>,
1929                     OpSize16, T8PS;
1930   def MOVBE32mr : I<0xF1, MRMDestMem, (outs), (ins i32mem:$dst, GR32:$src),
1931                     "movbe{l}\t{$src, $dst|$dst, $src}",
1932                     [(store (bswap GR32:$src), addr:$dst)], IIC_MOVBE>,
1933                     OpSize32, T8PS;
1934   def MOVBE64mr : RI<0xF1, MRMDestMem, (outs), (ins i64mem:$dst, GR64:$src),
1935                      "movbe{q}\t{$src, $dst|$dst, $src}",
1936                      [(store (bswap GR64:$src), addr:$dst)], IIC_MOVBE>,
1937                      T8PS;
1938   }
1939 }
1940
1941 //===----------------------------------------------------------------------===//
1942 // RDRAND Instruction
1943 //
1944 let Predicates = [HasRDRAND], Defs = [EFLAGS] in {
1945   def RDRAND16r : I<0xC7, MRM6r, (outs GR16:$dst), (ins),
1946                     "rdrand{w}\t$dst",
1947                     [(set GR16:$dst, EFLAGS, (X86rdrand))]>, OpSize16, TB;
1948   def RDRAND32r : I<0xC7, MRM6r, (outs GR32:$dst), (ins),
1949                     "rdrand{l}\t$dst",
1950                     [(set GR32:$dst, EFLAGS, (X86rdrand))]>, OpSize32, TB;
1951   def RDRAND64r : RI<0xC7, MRM6r, (outs GR64:$dst), (ins),
1952                      "rdrand{q}\t$dst",
1953                      [(set GR64:$dst, EFLAGS, (X86rdrand))]>, TB;
1954 }
1955
1956 //===----------------------------------------------------------------------===//
1957 // RDSEED Instruction
1958 //
1959 let Predicates = [HasRDSEED], Defs = [EFLAGS] in {
1960   def RDSEED16r : I<0xC7, MRM7r, (outs GR16:$dst), (ins),
1961                     "rdseed{w}\t$dst",
1962                     [(set GR16:$dst, EFLAGS, (X86rdseed))]>, OpSize16, TB;
1963   def RDSEED32r : I<0xC7, MRM7r, (outs GR32:$dst), (ins),
1964                     "rdseed{l}\t$dst",
1965                     [(set GR32:$dst, EFLAGS, (X86rdseed))]>, OpSize32, TB;
1966   def RDSEED64r : RI<0xC7, MRM7r, (outs GR64:$dst), (ins),
1967                      "rdseed{q}\t$dst",
1968                      [(set GR64:$dst, EFLAGS, (X86rdseed))]>, TB;
1969 }
1970
1971 //===----------------------------------------------------------------------===//
1972 // LZCNT Instruction
1973 //
1974 let Predicates = [HasLZCNT], Defs = [EFLAGS] in {
1975   def LZCNT16rr : I<0xBD, MRMSrcReg, (outs GR16:$dst), (ins GR16:$src),
1976                     "lzcnt{w}\t{$src, $dst|$dst, $src}",
1977                     [(set GR16:$dst, (ctlz GR16:$src)), (implicit EFLAGS)]>, XS,
1978                     OpSize16;
1979   def LZCNT16rm : I<0xBD, MRMSrcMem, (outs GR16:$dst), (ins i16mem:$src),
1980                     "lzcnt{w}\t{$src, $dst|$dst, $src}",
1981                     [(set GR16:$dst, (ctlz (loadi16 addr:$src))),
1982                      (implicit EFLAGS)]>, XS, OpSize16;
1983
1984   def LZCNT32rr : I<0xBD, MRMSrcReg, (outs GR32:$dst), (ins GR32:$src),
1985                     "lzcnt{l}\t{$src, $dst|$dst, $src}",
1986                     [(set GR32:$dst, (ctlz GR32:$src)), (implicit EFLAGS)]>, XS,
1987                     OpSize32;
1988   def LZCNT32rm : I<0xBD, MRMSrcMem, (outs GR32:$dst), (ins i32mem:$src),
1989                     "lzcnt{l}\t{$src, $dst|$dst, $src}",
1990                     [(set GR32:$dst, (ctlz (loadi32 addr:$src))),
1991                      (implicit EFLAGS)]>, XS, OpSize32;
1992
1993   def LZCNT64rr : RI<0xBD, MRMSrcReg, (outs GR64:$dst), (ins GR64:$src),
1994                      "lzcnt{q}\t{$src, $dst|$dst, $src}",
1995                      [(set GR64:$dst, (ctlz GR64:$src)), (implicit EFLAGS)]>,
1996                      XS;
1997   def LZCNT64rm : RI<0xBD, MRMSrcMem, (outs GR64:$dst), (ins i64mem:$src),
1998                      "lzcnt{q}\t{$src, $dst|$dst, $src}",
1999                      [(set GR64:$dst, (ctlz (loadi64 addr:$src))),
2000                       (implicit EFLAGS)]>, XS;
2001 }
2002
2003 //===----------------------------------------------------------------------===//
2004 // BMI Instructions
2005 //
2006 let Predicates = [HasBMI], Defs = [EFLAGS] in {
2007   def TZCNT16rr : I<0xBC, MRMSrcReg, (outs GR16:$dst), (ins GR16:$src),
2008                     "tzcnt{w}\t{$src, $dst|$dst, $src}",
2009                     [(set GR16:$dst, (cttz GR16:$src)), (implicit EFLAGS)]>, XS,
2010                     OpSize16;
2011   def TZCNT16rm : I<0xBC, MRMSrcMem, (outs GR16:$dst), (ins i16mem:$src),
2012                     "tzcnt{w}\t{$src, $dst|$dst, $src}",
2013                     [(set GR16:$dst, (cttz (loadi16 addr:$src))),
2014                      (implicit EFLAGS)]>, XS, OpSize16;
2015
2016   def TZCNT32rr : I<0xBC, MRMSrcReg, (outs GR32:$dst), (ins GR32:$src),
2017                     "tzcnt{l}\t{$src, $dst|$dst, $src}",
2018                     [(set GR32:$dst, (cttz GR32:$src)), (implicit EFLAGS)]>, XS,
2019                     OpSize32;
2020   def TZCNT32rm : I<0xBC, MRMSrcMem, (outs GR32:$dst), (ins i32mem:$src),
2021                     "tzcnt{l}\t{$src, $dst|$dst, $src}",
2022                     [(set GR32:$dst, (cttz (loadi32 addr:$src))),
2023                      (implicit EFLAGS)]>, XS, OpSize32;
2024
2025   def TZCNT64rr : RI<0xBC, MRMSrcReg, (outs GR64:$dst), (ins GR64:$src),
2026                      "tzcnt{q}\t{$src, $dst|$dst, $src}",
2027                      [(set GR64:$dst, (cttz GR64:$src)), (implicit EFLAGS)]>,
2028                      XS;
2029   def TZCNT64rm : RI<0xBC, MRMSrcMem, (outs GR64:$dst), (ins i64mem:$src),
2030                      "tzcnt{q}\t{$src, $dst|$dst, $src}",
2031                      [(set GR64:$dst, (cttz (loadi64 addr:$src))),
2032                       (implicit EFLAGS)]>, XS;
2033 }
2034
2035 multiclass bmi_bls<string mnemonic, Format RegMRM, Format MemMRM,
2036                   RegisterClass RC, X86MemOperand x86memop> {
2037 let hasSideEffects = 0 in {
2038   def rr : I<0xF3, RegMRM, (outs RC:$dst), (ins RC:$src),
2039              !strconcat(mnemonic, "\t{$src, $dst|$dst, $src}"),
2040              []>, T8PS, VEX_4V;
2041   let mayLoad = 1 in
2042   def rm : I<0xF3, MemMRM, (outs RC:$dst), (ins x86memop:$src),
2043              !strconcat(mnemonic, "\t{$src, $dst|$dst, $src}"),
2044              []>, T8PS, VEX_4V;
2045 }
2046 }
2047
2048 let Predicates = [HasBMI], Defs = [EFLAGS] in {
2049   defm BLSR32 : bmi_bls<"blsr{l}", MRM1r, MRM1m, GR32, i32mem>;
2050   defm BLSR64 : bmi_bls<"blsr{q}", MRM1r, MRM1m, GR64, i64mem>, VEX_W;
2051   defm BLSMSK32 : bmi_bls<"blsmsk{l}", MRM2r, MRM2m, GR32, i32mem>;
2052   defm BLSMSK64 : bmi_bls<"blsmsk{q}", MRM2r, MRM2m, GR64, i64mem>, VEX_W;
2053   defm BLSI32 : bmi_bls<"blsi{l}", MRM3r, MRM3m, GR32, i32mem>;
2054   defm BLSI64 : bmi_bls<"blsi{q}", MRM3r, MRM3m, GR64, i64mem>, VEX_W;
2055 }
2056
2057 //===----------------------------------------------------------------------===//
2058 // Pattern fragments to auto generate BMI instructions.
2059 //===----------------------------------------------------------------------===//
2060
2061 let Predicates = [HasBMI] in {
2062   // FIXME: patterns for the load versions are not implemented
2063   def : Pat<(and GR32:$src, (add GR32:$src, -1)),
2064             (BLSR32rr GR32:$src)>;
2065   def : Pat<(and GR64:$src, (add GR64:$src, -1)),
2066             (BLSR64rr GR64:$src)>;
2067
2068   def : Pat<(xor GR32:$src, (add GR32:$src, -1)),
2069             (BLSMSK32rr GR32:$src)>;
2070   def : Pat<(xor GR64:$src, (add GR64:$src, -1)),
2071             (BLSMSK64rr GR64:$src)>;
2072
2073   def : Pat<(and GR32:$src, (ineg GR32:$src)),
2074             (BLSI32rr GR32:$src)>;
2075   def : Pat<(and GR64:$src, (ineg GR64:$src)),
2076             (BLSI64rr GR64:$src)>;
2077 }
2078
2079 multiclass bmi_bextr_bzhi<bits<8> opc, string mnemonic, RegisterClass RC,
2080                           X86MemOperand x86memop, Intrinsic Int,
2081                           PatFrag ld_frag> {
2082   def rr : I<opc, MRMSrcReg, (outs RC:$dst), (ins RC:$src1, RC:$src2),
2083              !strconcat(mnemonic, "\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
2084              [(set RC:$dst, (Int RC:$src1, RC:$src2)), (implicit EFLAGS)]>,
2085              T8PS, VEX_4VOp3;
2086   def rm : I<opc, MRMSrcMem, (outs RC:$dst), (ins x86memop:$src1, RC:$src2),
2087              !strconcat(mnemonic, "\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
2088              [(set RC:$dst, (Int (ld_frag addr:$src1), RC:$src2)),
2089               (implicit EFLAGS)]>, T8PS, VEX_4VOp3;
2090 }
2091
2092 let Predicates = [HasBMI], Defs = [EFLAGS] in {
2093   defm BEXTR32 : bmi_bextr_bzhi<0xF7, "bextr{l}", GR32, i32mem,
2094                                 int_x86_bmi_bextr_32, loadi32>;
2095   defm BEXTR64 : bmi_bextr_bzhi<0xF7, "bextr{q}", GR64, i64mem,
2096                                 int_x86_bmi_bextr_64, loadi64>, VEX_W;
2097 }
2098
2099 let Predicates = [HasBMI2], Defs = [EFLAGS] in {
2100   defm BZHI32 : bmi_bextr_bzhi<0xF5, "bzhi{l}", GR32, i32mem,
2101                                int_x86_bmi_bzhi_32, loadi32>;
2102   defm BZHI64 : bmi_bextr_bzhi<0xF5, "bzhi{q}", GR64, i64mem,
2103                                int_x86_bmi_bzhi_64, loadi64>, VEX_W;
2104 }
2105
2106
2107 def CountTrailingOnes : SDNodeXForm<imm, [{
2108   // Count the trailing ones in the immediate.
2109   return getI8Imm(CountTrailingOnes_64(N->getZExtValue()));
2110 }]>;
2111
2112 def BZHIMask : ImmLeaf<i64, [{
2113   return isMask_64(Imm) && (CountTrailingOnes_64(Imm) > 32);
2114 }]>;
2115
2116 let Predicates = [HasBMI2] in {
2117   def : Pat<(and GR64:$src, BZHIMask:$mask),
2118             (BZHI64rr GR64:$src,
2119               (INSERT_SUBREG (i64 (IMPLICIT_DEF)),
2120                              (MOV8ri (CountTrailingOnes imm:$mask)), sub_8bit))>;
2121
2122   def : Pat<(and GR32:$src, (add (shl 1, GR8:$lz), -1)),
2123             (BZHI32rr GR32:$src,
2124               (INSERT_SUBREG (i32 (IMPLICIT_DEF)), GR8:$lz, sub_8bit))>;
2125
2126   def : Pat<(and (loadi32 addr:$src), (add (shl 1, GR8:$lz), -1)),
2127             (BZHI32rm addr:$src,
2128               (INSERT_SUBREG (i32 (IMPLICIT_DEF)), GR8:$lz, sub_8bit))>;
2129
2130   def : Pat<(and GR64:$src, (add (shl 1, GR8:$lz), -1)),
2131             (BZHI64rr GR64:$src,
2132               (INSERT_SUBREG (i64 (IMPLICIT_DEF)), GR8:$lz, sub_8bit))>;
2133
2134   def : Pat<(and (loadi64 addr:$src), (add (shl 1, GR8:$lz), -1)),
2135             (BZHI64rm addr:$src,
2136               (INSERT_SUBREG (i64 (IMPLICIT_DEF)), GR8:$lz, sub_8bit))>;
2137 } // HasBMI2
2138
2139 let Predicates = [HasBMI] in {
2140   def : Pat<(X86bextr GR32:$src1, GR32:$src2),
2141             (BEXTR32rr GR32:$src1, GR32:$src2)>;
2142   def : Pat<(X86bextr (loadi32 addr:$src1), GR32:$src2),
2143             (BEXTR32rm addr:$src1, GR32:$src2)>;
2144   def : Pat<(X86bextr GR64:$src1, GR64:$src2),
2145             (BEXTR64rr GR64:$src1, GR64:$src2)>;
2146   def : Pat<(X86bextr (loadi64 addr:$src1), GR64:$src2),
2147             (BEXTR64rm addr:$src1, GR64:$src2)>;
2148 } // HasBMI
2149
2150 multiclass bmi_pdep_pext<string mnemonic, RegisterClass RC,
2151                          X86MemOperand x86memop, Intrinsic Int,
2152                          PatFrag ld_frag> {
2153   def rr : I<0xF5, MRMSrcReg, (outs RC:$dst), (ins RC:$src1, RC:$src2),
2154              !strconcat(mnemonic, "\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
2155              [(set RC:$dst, (Int RC:$src1, RC:$src2))]>,
2156              VEX_4V;
2157   def rm : I<0xF5, MRMSrcMem, (outs RC:$dst), (ins RC:$src1, x86memop:$src2),
2158              !strconcat(mnemonic, "\t{$src2, $src1, $dst|$dst, $src1, $src2}"),
2159              [(set RC:$dst, (Int RC:$src1, (ld_frag addr:$src2)))]>, VEX_4V;
2160 }
2161
2162 let Predicates = [HasBMI2] in {
2163   defm PDEP32 : bmi_pdep_pext<"pdep{l}", GR32, i32mem,
2164                                int_x86_bmi_pdep_32, loadi32>, T8XD;
2165   defm PDEP64 : bmi_pdep_pext<"pdep{q}", GR64, i64mem,
2166                                int_x86_bmi_pdep_64, loadi64>, T8XD, VEX_W;
2167   defm PEXT32 : bmi_pdep_pext<"pext{l}", GR32, i32mem,
2168                                int_x86_bmi_pext_32, loadi32>, T8XS;
2169   defm PEXT64 : bmi_pdep_pext<"pext{q}", GR64, i64mem,
2170                                int_x86_bmi_pext_64, loadi64>, T8XS, VEX_W;
2171 }
2172
2173 //===----------------------------------------------------------------------===//
2174 // TBM Instructions
2175 //
2176 let Predicates = [HasTBM], Defs = [EFLAGS] in {
2177
2178 multiclass tbm_ternary_imm_intr<bits<8> opc, RegisterClass RC, string OpcodeStr,
2179                                 X86MemOperand x86memop, PatFrag ld_frag,
2180                                 Intrinsic Int, Operand immtype,
2181                                 SDPatternOperator immoperator> {
2182   def ri : Ii32<opc,  MRMSrcReg, (outs RC:$dst), (ins RC:$src1, immtype:$cntl),
2183                 !strconcat(OpcodeStr,
2184                            "\t{$cntl, $src1, $dst|$dst, $src1, $cntl}"),
2185                 [(set RC:$dst, (Int RC:$src1, immoperator:$cntl))]>,
2186            XOP, XOPA;
2187   def mi : Ii32<opc,  MRMSrcMem, (outs RC:$dst),
2188                 (ins x86memop:$src1, immtype:$cntl),
2189                 !strconcat(OpcodeStr,
2190                            "\t{$cntl, $src1, $dst|$dst, $src1, $cntl}"),
2191                 [(set RC:$dst, (Int (ld_frag addr:$src1), immoperator:$cntl))]>,
2192            XOP, XOPA;
2193 }
2194
2195 defm BEXTRI32 : tbm_ternary_imm_intr<0x10, GR32, "bextr", i32mem, loadi32,
2196                                      int_x86_tbm_bextri_u32, i32imm, imm>;
2197 let ImmT = Imm32S in
2198 defm BEXTRI64 : tbm_ternary_imm_intr<0x10, GR64, "bextr", i64mem, loadi64,
2199                                      int_x86_tbm_bextri_u64, i64i32imm,
2200                                      i64immSExt32>, VEX_W;
2201
2202 multiclass tbm_binary_rm<bits<8> opc, Format FormReg, Format FormMem,
2203                          RegisterClass RC, string OpcodeStr,
2204                          X86MemOperand x86memop, PatFrag ld_frag> {
2205 let hasSideEffects = 0 in {
2206   def rr : I<opc,  FormReg, (outs RC:$dst), (ins RC:$src),
2207              !strconcat(OpcodeStr,"\t{$src, $dst|$dst, $src}"),
2208              []>, XOP_4V, XOP9;
2209   let mayLoad = 1 in
2210   def rm : I<opc,  FormMem, (outs RC:$dst), (ins x86memop:$src),
2211              !strconcat(OpcodeStr,"\t{$src, $dst|$dst, $src}"),
2212              []>, XOP_4V, XOP9;
2213 }
2214 }
2215
2216 multiclass tbm_binary_intr<bits<8> opc, string OpcodeStr,
2217                            Format FormReg, Format FormMem> {
2218   defm NAME#32 : tbm_binary_rm<opc, FormReg, FormMem, GR32, OpcodeStr, i32mem,
2219                                loadi32>;
2220   defm NAME#64 : tbm_binary_rm<opc, FormReg, FormMem, GR64, OpcodeStr, i64mem,
2221                                loadi64>, VEX_W;
2222 }
2223
2224 defm BLCFILL : tbm_binary_intr<0x01, "blcfill", MRM1r, MRM1m>;
2225 defm BLCI    : tbm_binary_intr<0x02, "blci", MRM6r, MRM6m>;
2226 defm BLCIC   : tbm_binary_intr<0x01, "blcic", MRM5r, MRM5m>;
2227 defm BLCMSK  : tbm_binary_intr<0x02, "blcmsk", MRM1r, MRM1m>;
2228 defm BLCS    : tbm_binary_intr<0x01, "blcs", MRM3r, MRM3m>;
2229 defm BLSFILL : tbm_binary_intr<0x01, "blsfill", MRM2r, MRM2m>;
2230 defm BLSIC   : tbm_binary_intr<0x01, "blsic", MRM6r, MRM6m>;
2231 defm T1MSKC  : tbm_binary_intr<0x01, "t1mskc", MRM7r, MRM7m>;
2232 defm TZMSK   : tbm_binary_intr<0x01, "tzmsk", MRM4r, MRM4m>;
2233 } // HasTBM, EFLAGS
2234
2235 //===----------------------------------------------------------------------===//
2236 // Pattern fragments to auto generate TBM instructions.
2237 //===----------------------------------------------------------------------===//
2238
2239 let Predicates = [HasTBM] in {
2240   def : Pat<(X86bextr GR32:$src1, (i32 imm:$src2)),
2241             (BEXTRI32ri GR32:$src1, imm:$src2)>;
2242   def : Pat<(X86bextr (loadi32 addr:$src1), (i32 imm:$src2)),
2243             (BEXTRI32mi addr:$src1, imm:$src2)>;
2244   def : Pat<(X86bextr GR64:$src1, i64immSExt32:$src2),
2245             (BEXTRI64ri GR64:$src1, i64immSExt32:$src2)>;
2246   def : Pat<(X86bextr (loadi64 addr:$src1), i64immSExt32:$src2),
2247             (BEXTRI64mi addr:$src1, i64immSExt32:$src2)>;
2248
2249   // FIXME: patterns for the load versions are not implemented
2250   def : Pat<(and GR32:$src, (add GR32:$src, 1)),
2251             (BLCFILL32rr GR32:$src)>;
2252   def : Pat<(and GR64:$src, (add GR64:$src, 1)),
2253             (BLCFILL64rr GR64:$src)>;
2254
2255   def : Pat<(or GR32:$src, (not (add GR32:$src, 1))),
2256             (BLCI32rr GR32:$src)>;
2257   def : Pat<(or GR64:$src, (not (add GR64:$src, 1))),
2258             (BLCI64rr GR64:$src)>;
2259
2260   // Extra patterns because opt can optimize the above patterns to this.
2261   def : Pat<(or GR32:$src, (sub -2, GR32:$src)),
2262             (BLCI32rr GR32:$src)>;
2263   def : Pat<(or GR64:$src, (sub -2, GR64:$src)),
2264             (BLCI64rr GR64:$src)>;
2265
2266   def : Pat<(and (not GR32:$src), (add GR32:$src, 1)),
2267             (BLCIC32rr GR32:$src)>;
2268   def : Pat<(and (not GR64:$src), (add GR64:$src, 1)),
2269             (BLCIC64rr GR64:$src)>;
2270
2271   def : Pat<(xor GR32:$src, (add GR32:$src, 1)),
2272             (BLCMSK32rr GR32:$src)>;
2273   def : Pat<(xor GR64:$src, (add GR64:$src, 1)),
2274             (BLCMSK64rr GR64:$src)>;
2275
2276   def : Pat<(or GR32:$src, (add GR32:$src, 1)),
2277             (BLCS32rr GR32:$src)>;
2278   def : Pat<(or GR64:$src, (add GR64:$src, 1)),
2279             (BLCS64rr GR64:$src)>;
2280
2281   def : Pat<(or GR32:$src, (add GR32:$src, -1)),
2282             (BLSFILL32rr GR32:$src)>;
2283   def : Pat<(or GR64:$src, (add GR64:$src, -1)),
2284             (BLSFILL64rr GR64:$src)>;
2285
2286   def : Pat<(or (not GR32:$src), (add GR32:$src, -1)),
2287             (BLSIC32rr GR32:$src)>;
2288   def : Pat<(or (not GR64:$src), (add GR64:$src, -1)),
2289             (BLSIC64rr GR64:$src)>;
2290
2291   def : Pat<(or (not GR32:$src), (add GR32:$src, 1)),
2292             (T1MSKC32rr GR32:$src)>;
2293   def : Pat<(or (not GR64:$src), (add GR64:$src, 1)),
2294             (T1MSKC64rr GR64:$src)>;
2295
2296   def : Pat<(and (not GR32:$src), (add GR32:$src, -1)),
2297             (TZMSK32rr GR32:$src)>;
2298   def : Pat<(and (not GR64:$src), (add GR64:$src, -1)),
2299             (TZMSK64rr GR64:$src)>;
2300 } // HasTBM
2301
2302 //===----------------------------------------------------------------------===//
2303 // Subsystems.
2304 //===----------------------------------------------------------------------===//
2305
2306 include "X86InstrArithmetic.td"
2307 include "X86InstrCMovSetCC.td"
2308 include "X86InstrExtension.td"
2309 include "X86InstrControl.td"
2310 include "X86InstrShiftRotate.td"
2311
2312 // X87 Floating Point Stack.
2313 include "X86InstrFPStack.td"
2314
2315 // SIMD support (SSE, MMX and AVX)
2316 include "X86InstrFragmentsSIMD.td"
2317
2318 // FMA - Fused Multiply-Add support (requires FMA)
2319 include "X86InstrFMA.td"
2320
2321 // XOP
2322 include "X86InstrXOP.td"
2323
2324 // SSE, MMX and 3DNow! vector support.
2325 include "X86InstrSSE.td"
2326 include "X86InstrAVX512.td"
2327 include "X86InstrMMX.td"
2328 include "X86Instr3DNow.td"
2329
2330 include "X86InstrVMX.td"
2331 include "X86InstrSVM.td"
2332
2333 include "X86InstrTSX.td"
2334
2335 // System instructions.
2336 include "X86InstrSystem.td"
2337
2338 // Compiler Pseudo Instructions and Pat Patterns
2339 include "X86InstrCompiler.td"
2340
2341 //===----------------------------------------------------------------------===//
2342 // Assembler Mnemonic Aliases
2343 //===----------------------------------------------------------------------===//
2344
2345 def : MnemonicAlias<"call", "callw", "att">, Requires<[In16BitMode]>;
2346 def : MnemonicAlias<"call", "calll", "att">, Requires<[In32BitMode]>;
2347 def : MnemonicAlias<"call", "callq", "att">, Requires<[In64BitMode]>;
2348
2349 def : MnemonicAlias<"cbw",  "cbtw", "att">;
2350 def : MnemonicAlias<"cwde", "cwtl", "att">;
2351 def : MnemonicAlias<"cwd",  "cwtd", "att">;
2352 def : MnemonicAlias<"cdq",  "cltd", "att">;
2353 def : MnemonicAlias<"cdqe", "cltq", "att">;
2354 def : MnemonicAlias<"cqo",  "cqto", "att">;
2355
2356 // In 64-bit mode lret maps to lretl; it is not ambiguous with lretq.
2357 def : MnemonicAlias<"lret", "lretw", "att">, Requires<[In16BitMode]>;
2358 def : MnemonicAlias<"lret", "lretl", "att">, Requires<[Not16BitMode]>;
2359
2360 def : MnemonicAlias<"leavel", "leave", "att">, Requires<[Not64BitMode]>;
2361 def : MnemonicAlias<"leaveq", "leave", "att">, Requires<[In64BitMode]>;
2362
2363 def : MnemonicAlias<"loopz",  "loope",  "att">;
2364 def : MnemonicAlias<"loopnz", "loopne", "att">;
2365
2366 def : MnemonicAlias<"pop",   "popw",  "att">, Requires<[In16BitMode]>;
2367 def : MnemonicAlias<"pop",   "popl",  "att">, Requires<[In32BitMode]>;
2368 def : MnemonicAlias<"pop",   "popq",  "att">, Requires<[In64BitMode]>;
2369 def : MnemonicAlias<"popf",  "popfw", "att">, Requires<[In16BitMode]>;
2370 def : MnemonicAlias<"popf",  "popfl", "att">, Requires<[In32BitMode]>;
2371 def : MnemonicAlias<"popf",  "popfq", "att">, Requires<[In64BitMode]>;
2372 def : MnemonicAlias<"popfd", "popfl", "att">;
2373
2374 // FIXME: This is wrong for "push reg".  "push %bx" should turn into pushw in
2375 // all modes.  However: "push (addr)" and "push $42" should default to
2376 // pushl/pushq depending on the current mode.  Similar for "pop %bx"
2377 def : MnemonicAlias<"push",   "pushw",  "att">, Requires<[In16BitMode]>;
2378 def : MnemonicAlias<"push",   "pushl",  "att">, Requires<[In32BitMode]>;
2379 def : MnemonicAlias<"push",   "pushq",  "att">, Requires<[In64BitMode]>;
2380 def : MnemonicAlias<"pushf",  "pushfw", "att">, Requires<[In16BitMode]>;
2381 def : MnemonicAlias<"pushf",  "pushfl", "att">, Requires<[In32BitMode]>;
2382 def : MnemonicAlias<"pushf",  "pushfq", "att">, Requires<[In64BitMode]>;
2383 def : MnemonicAlias<"pushfd", "pushfl", "att">;
2384
2385 def : MnemonicAlias<"popad",  "popal",  "intel">, Requires<[Not64BitMode]>;
2386 def : MnemonicAlias<"pushad", "pushal", "intel">, Requires<[Not64BitMode]>;
2387 def : MnemonicAlias<"popa",   "popaw",  "intel">, Requires<[In16BitMode]>;
2388 def : MnemonicAlias<"pusha",  "pushaw", "intel">, Requires<[In16BitMode]>;
2389 def : MnemonicAlias<"popa",   "popal",  "intel">, Requires<[In32BitMode]>;
2390 def : MnemonicAlias<"pusha",  "pushal", "intel">, Requires<[In32BitMode]>;
2391
2392 def : MnemonicAlias<"popa",   "popaw",  "att">, Requires<[In16BitMode]>;
2393 def : MnemonicAlias<"pusha",  "pushaw", "att">, Requires<[In16BitMode]>;
2394 def : MnemonicAlias<"popa",   "popal",  "att">, Requires<[In32BitMode]>;
2395 def : MnemonicAlias<"pusha",  "pushal", "att">, Requires<[In32BitMode]>;
2396
2397 def : MnemonicAlias<"repe",  "rep",   "att">;
2398 def : MnemonicAlias<"repz",  "rep",   "att">;
2399 def : MnemonicAlias<"repnz", "repne", "att">;
2400
2401 def : MnemonicAlias<"ret", "retw", "att">, Requires<[In16BitMode]>;
2402 def : MnemonicAlias<"ret", "retl", "att">, Requires<[In32BitMode]>;
2403 def : MnemonicAlias<"ret", "retq", "att">, Requires<[In64BitMode]>;
2404
2405 def : MnemonicAlias<"salb", "shlb", "att">;
2406 def : MnemonicAlias<"salw", "shlw", "att">;
2407 def : MnemonicAlias<"sall", "shll", "att">;
2408 def : MnemonicAlias<"salq", "shlq", "att">;
2409
2410 def : MnemonicAlias<"smovb", "movsb", "att">;
2411 def : MnemonicAlias<"smovw", "movsw", "att">;
2412 def : MnemonicAlias<"smovl", "movsl", "att">;
2413 def : MnemonicAlias<"smovq", "movsq", "att">;
2414
2415 def : MnemonicAlias<"ud2a",  "ud2",  "att">;
2416 def : MnemonicAlias<"verrw", "verr", "att">;
2417
2418 // System instruction aliases.
2419 def : MnemonicAlias<"iret",    "iretw",    "att">, Requires<[In16BitMode]>;
2420 def : MnemonicAlias<"iret",    "iretl",    "att">, Requires<[Not16BitMode]>;
2421 def : MnemonicAlias<"sysret",  "sysretl",  "att">;
2422 def : MnemonicAlias<"sysexit", "sysexitl", "att">;
2423
2424 def : MnemonicAlias<"lgdt", "lgdtw", "att">, Requires<[In16BitMode]>;
2425 def : MnemonicAlias<"lgdt", "lgdtl", "att">, Requires<[In32BitMode]>;
2426 def : MnemonicAlias<"lgdt", "lgdtq", "att">, Requires<[In64BitMode]>;
2427 def : MnemonicAlias<"lidt", "lidtw", "att">, Requires<[In16BitMode]>;
2428 def : MnemonicAlias<"lidt", "lidtl", "att">, Requires<[In32BitMode]>;
2429 def : MnemonicAlias<"lidt", "lidtq", "att">, Requires<[In64BitMode]>;
2430 def : MnemonicAlias<"sgdt", "sgdtw", "att">, Requires<[In16BitMode]>;
2431 def : MnemonicAlias<"sgdt", "sgdtl", "att">, Requires<[In32BitMode]>;
2432 def : MnemonicAlias<"sgdt", "sgdtq", "att">, Requires<[In64BitMode]>;
2433 def : MnemonicAlias<"sidt", "sidtw", "att">, Requires<[In16BitMode]>;
2434 def : MnemonicAlias<"sidt", "sidtl", "att">, Requires<[In32BitMode]>;
2435 def : MnemonicAlias<"sidt", "sidtq", "att">, Requires<[In64BitMode]>;
2436
2437
2438 // Floating point stack aliases.
2439 def : MnemonicAlias<"fcmovz",   "fcmove",   "att">;
2440 def : MnemonicAlias<"fcmova",   "fcmovnbe", "att">;
2441 def : MnemonicAlias<"fcmovnae", "fcmovb",   "att">;
2442 def : MnemonicAlias<"fcmovna",  "fcmovbe",  "att">;
2443 def : MnemonicAlias<"fcmovae",  "fcmovnb",  "att">;
2444 def : MnemonicAlias<"fcomip",   "fcompi",   "att">;
2445 def : MnemonicAlias<"fildq",    "fildll",   "att">;
2446 def : MnemonicAlias<"fistpq",   "fistpll",  "att">;
2447 def : MnemonicAlias<"fisttpq",  "fisttpll", "att">;
2448 def : MnemonicAlias<"fldcww",   "fldcw",    "att">;
2449 def : MnemonicAlias<"fnstcww",  "fnstcw",   "att">;
2450 def : MnemonicAlias<"fnstsww",  "fnstsw",   "att">;
2451 def : MnemonicAlias<"fucomip",  "fucompi",  "att">;
2452 def : MnemonicAlias<"fwait",    "wait",     "att">;
2453
2454
2455 class CondCodeAlias<string Prefix,string Suffix, string OldCond, string NewCond,
2456                     string VariantName>
2457   : MnemonicAlias<!strconcat(Prefix, OldCond, Suffix),
2458                   !strconcat(Prefix, NewCond, Suffix), VariantName>;
2459
2460 /// IntegerCondCodeMnemonicAlias - This multiclass defines a bunch of
2461 /// MnemonicAlias's that canonicalize the condition code in a mnemonic, for
2462 /// example "setz" -> "sete".
2463 multiclass IntegerCondCodeMnemonicAlias<string Prefix, string Suffix,
2464                                         string V = ""> {
2465   def C   : CondCodeAlias<Prefix, Suffix, "c",   "b",  V>; // setc   -> setb
2466   def Z   : CondCodeAlias<Prefix, Suffix, "z" ,  "e",  V>; // setz   -> sete
2467   def NA  : CondCodeAlias<Prefix, Suffix, "na",  "be", V>; // setna  -> setbe
2468   def NB  : CondCodeAlias<Prefix, Suffix, "nb",  "ae", V>; // setnb  -> setae
2469   def NC  : CondCodeAlias<Prefix, Suffix, "nc",  "ae", V>; // setnc  -> setae
2470   def NG  : CondCodeAlias<Prefix, Suffix, "ng",  "le", V>; // setng  -> setle
2471   def NL  : CondCodeAlias<Prefix, Suffix, "nl",  "ge", V>; // setnl  -> setge
2472   def NZ  : CondCodeAlias<Prefix, Suffix, "nz",  "ne", V>; // setnz  -> setne
2473   def PE  : CondCodeAlias<Prefix, Suffix, "pe",  "p",  V>; // setpe  -> setp
2474   def PO  : CondCodeAlias<Prefix, Suffix, "po",  "np", V>; // setpo  -> setnp
2475
2476   def NAE : CondCodeAlias<Prefix, Suffix, "nae", "b",  V>; // setnae -> setb
2477   def NBE : CondCodeAlias<Prefix, Suffix, "nbe", "a",  V>; // setnbe -> seta
2478   def NGE : CondCodeAlias<Prefix, Suffix, "nge", "l",  V>; // setnge -> setl
2479   def NLE : CondCodeAlias<Prefix, Suffix, "nle", "g",  V>; // setnle -> setg
2480 }
2481
2482 // Aliases for set<CC>
2483 defm : IntegerCondCodeMnemonicAlias<"set", "">;
2484 // Aliases for j<CC>
2485 defm : IntegerCondCodeMnemonicAlias<"j", "">;
2486 // Aliases for cmov<CC>{w,l,q}
2487 defm : IntegerCondCodeMnemonicAlias<"cmov", "w", "att">;
2488 defm : IntegerCondCodeMnemonicAlias<"cmov", "l", "att">;
2489 defm : IntegerCondCodeMnemonicAlias<"cmov", "q", "att">;
2490 // No size suffix for intel-style asm.
2491 defm : IntegerCondCodeMnemonicAlias<"cmov", "", "intel">;
2492
2493
2494 //===----------------------------------------------------------------------===//
2495 // Assembler Instruction Aliases
2496 //===----------------------------------------------------------------------===//
2497
2498 // aad/aam default to base 10 if no operand is specified.
2499 def : InstAlias<"aad", (AAD8i8 10)>;
2500 def : InstAlias<"aam", (AAM8i8 10)>;
2501
2502 // Disambiguate the mem/imm form of bt-without-a-suffix as btl.
2503 // Likewise for btc/btr/bts.
2504 def : InstAlias<"bt {$imm, $mem|$mem, $imm}",
2505                 (BT32mi8 i32mem:$mem, i32i8imm:$imm), 0>;
2506 def : InstAlias<"btc {$imm, $mem|$mem, $imm}",
2507                 (BTC32mi8 i32mem:$mem, i32i8imm:$imm), 0>;
2508 def : InstAlias<"btr {$imm, $mem|$mem, $imm}",
2509                 (BTR32mi8 i32mem:$mem, i32i8imm:$imm), 0>;
2510 def : InstAlias<"bts {$imm, $mem|$mem, $imm}",
2511                 (BTS32mi8 i32mem:$mem, i32i8imm:$imm), 0>;
2512
2513 // clr aliases.
2514 def : InstAlias<"clrb $reg", (XOR8rr  GR8 :$reg, GR8 :$reg), 0>;
2515 def : InstAlias<"clrw $reg", (XOR16rr GR16:$reg, GR16:$reg), 0>;
2516 def : InstAlias<"clrl $reg", (XOR32rr GR32:$reg, GR32:$reg), 0>;
2517 def : InstAlias<"clrq $reg", (XOR64rr GR64:$reg, GR64:$reg), 0>;
2518
2519 // lods aliases. Accept the destination being omitted because it's implicit
2520 // in the mnemonic, or the mnemonic suffix being omitted because it's implicit
2521 // in the destination.
2522 def : InstAlias<"lodsb $src", (LODSB srcidx8:$src),  0>;
2523 def : InstAlias<"lodsw $src", (LODSW srcidx16:$src), 0>;
2524 def : InstAlias<"lods{l|d} $src", (LODSL srcidx32:$src), 0>;
2525 def : InstAlias<"lodsq $src", (LODSQ srcidx64:$src), 0>, Requires<[In64BitMode]>;
2526 def : InstAlias<"lods {$src, %al|al, $src}", (LODSB srcidx8:$src),  0>;
2527 def : InstAlias<"lods {$src, %ax|ax, $src}", (LODSW srcidx16:$src), 0>;
2528 def : InstAlias<"lods {$src, %eax|eax, $src}", (LODSL srcidx32:$src), 0>;
2529 def : InstAlias<"lods {$src, %rax|rax, $src}", (LODSQ srcidx64:$src), 0>, Requires<[In64BitMode]>;
2530
2531 // stos aliases. Accept the source being omitted because it's implicit in
2532 // the mnemonic, or the mnemonic suffix being omitted because it's implicit
2533 // in the source.
2534 def : InstAlias<"stosb $dst", (STOSB dstidx8:$dst),  0>;
2535 def : InstAlias<"stosw $dst", (STOSW dstidx16:$dst), 0>;
2536 def : InstAlias<"stos{l|d} $dst", (STOSL dstidx32:$dst), 0>;
2537 def : InstAlias<"stosq $dst", (STOSQ dstidx64:$dst), 0>, Requires<[In64BitMode]>;
2538 def : InstAlias<"stos {%al, $dst|$dst, al}", (STOSB dstidx8:$dst),  0>;
2539 def : InstAlias<"stos {%ax, $dst|$dst, ax}", (STOSW dstidx16:$dst), 0>;
2540 def : InstAlias<"stos {%eax, $dst|$dst, eax}", (STOSL dstidx32:$dst), 0>;
2541 def : InstAlias<"stos {%rax, $dst|$dst, rax}", (STOSQ dstidx64:$dst), 0>, Requires<[In64BitMode]>;
2542
2543 // scas aliases. Accept the destination being omitted because it's implicit
2544 // in the mnemonic, or the mnemonic suffix being omitted because it's implicit
2545 // in the destination.
2546 def : InstAlias<"scasb $dst", (SCASB dstidx8:$dst),  0>;
2547 def : InstAlias<"scasw $dst", (SCASW dstidx16:$dst), 0>;
2548 def : InstAlias<"scas{l|d} $dst", (SCASL dstidx32:$dst), 0>;
2549 def : InstAlias<"scasq $dst", (SCASQ dstidx64:$dst), 0>, Requires<[In64BitMode]>;
2550 def : InstAlias<"scas {$dst, %al|al, $dst}", (SCASB dstidx8:$dst),  0>;
2551 def : InstAlias<"scas {$dst, %ax|ax, $dst}", (SCASW dstidx16:$dst), 0>;
2552 def : InstAlias<"scas {$dst, %eax|eax, $dst}", (SCASL dstidx32:$dst), 0>;
2553 def : InstAlias<"scas {$dst, %rax|rax, $dst}", (SCASQ dstidx64:$dst), 0>, Requires<[In64BitMode]>;
2554
2555 // div and idiv aliases for explicit A register.
2556 def : InstAlias<"div{b}\t{$src, %al|al, $src}", (DIV8r  GR8 :$src)>;
2557 def : InstAlias<"div{w}\t{$src, %ax|ax, $src}", (DIV16r GR16:$src)>;
2558 def : InstAlias<"div{l}\t{$src, %eax|eax, $src}", (DIV32r GR32:$src)>;
2559 def : InstAlias<"div{q}\t{$src, %rax|rax, $src}", (DIV64r GR64:$src)>;
2560 def : InstAlias<"div{b}\t{$src, %al|al, $src}", (DIV8m  i8mem :$src)>;
2561 def : InstAlias<"div{w}\t{$src, %ax|ax, $src}", (DIV16m i16mem:$src)>;
2562 def : InstAlias<"div{l}\t{$src, %eax|eax, $src}", (DIV32m i32mem:$src)>;
2563 def : InstAlias<"div{q}\t{$src, %rax|rax, $src}", (DIV64m i64mem:$src)>;
2564 def : InstAlias<"idiv{b}\t{$src, %al|al, $src}", (IDIV8r  GR8 :$src)>;
2565 def : InstAlias<"idiv{w}\t{$src, %ax|ax, $src}", (IDIV16r GR16:$src)>;
2566 def : InstAlias<"idiv{l}\t{$src, %eax|eax, $src}", (IDIV32r GR32:$src)>;
2567 def : InstAlias<"idiv{q}\t{$src, %rax|rax, $src}", (IDIV64r GR64:$src)>;
2568 def : InstAlias<"idiv{b}\t{$src, %al|al, $src}", (IDIV8m  i8mem :$src)>;
2569 def : InstAlias<"idiv{w}\t{$src, %ax|ax, $src}", (IDIV16m i16mem:$src)>;
2570 def : InstAlias<"idiv{l}\t{$src, %eax|eax, $src}", (IDIV32m i32mem:$src)>;
2571 def : InstAlias<"idiv{q}\t{$src, %rax|rax, $src}", (IDIV64m i64mem:$src)>;
2572
2573
2574
2575 // Various unary fpstack operations default to operating on on ST1.
2576 // For example, "fxch" -> "fxch %st(1)"
2577 def : InstAlias<"faddp",        (ADD_FPrST0  ST1), 0>;
2578 def : InstAlias<"fsub{|r}p",    (SUBR_FPrST0 ST1), 0>;
2579 def : InstAlias<"fsub{r|}p",    (SUB_FPrST0  ST1), 0>;
2580 def : InstAlias<"fmulp",        (MUL_FPrST0  ST1), 0>;
2581 def : InstAlias<"fdiv{|r}p",    (DIVR_FPrST0 ST1), 0>;
2582 def : InstAlias<"fdiv{r|}p",    (DIV_FPrST0  ST1), 0>;
2583 def : InstAlias<"fxch",         (XCH_F       ST1), 0>;
2584 def : InstAlias<"fcom",         (COM_FST0r   ST1), 0>;
2585 def : InstAlias<"fcomp",        (COMP_FST0r  ST1), 0>;
2586 def : InstAlias<"fcomi",        (COM_FIr     ST1), 0>;
2587 def : InstAlias<"fcompi",       (COM_FIPr    ST1), 0>;
2588 def : InstAlias<"fucom",        (UCOM_Fr     ST1), 0>;
2589 def : InstAlias<"fucomp",       (UCOM_FPr    ST1), 0>;
2590 def : InstAlias<"fucomi",       (UCOM_FIr    ST1), 0>;
2591 def : InstAlias<"fucompi",      (UCOM_FIPr   ST1), 0>;
2592
2593 // Handle fmul/fadd/fsub/fdiv instructions with explicitly written st(0) op.
2594 // For example, "fadd %st(4), %st(0)" -> "fadd %st(4)".  We also disambiguate
2595 // instructions like "fadd %st(0), %st(0)" as "fadd %st(0)" for consistency with
2596 // gas.
2597 multiclass FpUnaryAlias<string Mnemonic, Instruction Inst, bit EmitAlias = 1> {
2598  def : InstAlias<!strconcat(Mnemonic, "\t{$op, %st(0)|st(0), $op}"),
2599                  (Inst RST:$op), EmitAlias>;
2600  def : InstAlias<!strconcat(Mnemonic, "\t{%st(0), %st(0)|st(0), st(0)}"),
2601                  (Inst ST0), EmitAlias>;
2602 }
2603
2604 defm : FpUnaryAlias<"fadd",   ADD_FST0r>;
2605 defm : FpUnaryAlias<"faddp",  ADD_FPrST0, 0>;
2606 defm : FpUnaryAlias<"fsub",   SUB_FST0r>;
2607 defm : FpUnaryAlias<"fsub{|r}p",  SUBR_FPrST0>;
2608 defm : FpUnaryAlias<"fsubr",  SUBR_FST0r>;
2609 defm : FpUnaryAlias<"fsub{r|}p", SUB_FPrST0>;
2610 defm : FpUnaryAlias<"fmul",   MUL_FST0r>;
2611 defm : FpUnaryAlias<"fmulp",  MUL_FPrST0>;
2612 defm : FpUnaryAlias<"fdiv",   DIV_FST0r>;
2613 defm : FpUnaryAlias<"fdiv{|r}p",  DIVR_FPrST0>;
2614 defm : FpUnaryAlias<"fdivr",  DIVR_FST0r>;
2615 defm : FpUnaryAlias<"fdiv{r|}p", DIV_FPrST0>;
2616 defm : FpUnaryAlias<"fcomi",   COM_FIr, 0>;
2617 defm : FpUnaryAlias<"fucomi",  UCOM_FIr, 0>;
2618 defm : FpUnaryAlias<"fcompi",   COM_FIPr>;
2619 defm : FpUnaryAlias<"fucompi",  UCOM_FIPr>;
2620
2621
2622 // Handle "f{mulp,addp} st(0), $op" the same as "f{mulp,addp} $op", since they
2623 // commute.  We also allow fdiv[r]p/fsubrp even though they don't commute,
2624 // solely because gas supports it.
2625 def : InstAlias<"faddp\t{%st(0), $op|$op, st(0)}", (ADD_FPrST0 RST:$op), 0>;
2626 def : InstAlias<"fmulp\t{%st(0), $op|$op, st(0)}", (MUL_FPrST0 RST:$op)>;
2627 def : InstAlias<"fsub{|r}p\t{%st(0), $op|$op, st(0)}", (SUBR_FPrST0 RST:$op)>;
2628 def : InstAlias<"fsub{r|}p\t{%st(0), $op|$op, st(0)}", (SUB_FPrST0 RST:$op)>;
2629 def : InstAlias<"fdiv{|r}p\t{%st(0), $op|$op, st(0)}", (DIVR_FPrST0 RST:$op)>;
2630 def : InstAlias<"fdiv{r|}p\t{%st(0), $op|$op, st(0)}", (DIV_FPrST0 RST:$op)>;
2631
2632 // We accept "fnstsw %eax" even though it only writes %ax.
2633 def : InstAlias<"fnstsw\t{%eax|eax}", (FNSTSW16r)>;
2634 def : InstAlias<"fnstsw\t{%al|al}" , (FNSTSW16r)>;
2635 def : InstAlias<"fnstsw"     , (FNSTSW16r)>;
2636
2637 // lcall and ljmp aliases.  This seems to be an odd mapping in 64-bit mode, but
2638 // this is compatible with what GAS does.
2639 def : InstAlias<"lcall $seg, $off", (FARCALL32i i32imm:$off, i16imm:$seg)>, Requires<[Not16BitMode]>;
2640 def : InstAlias<"ljmp $seg, $off",  (FARJMP32i  i32imm:$off, i16imm:$seg)>, Requires<[Not16BitMode]>;
2641 def : InstAlias<"lcall *$dst",      (FARCALL32m opaque48mem:$dst)>, Requires<[Not16BitMode]>;
2642 def : InstAlias<"ljmp *$dst",       (FARJMP32m  opaque48mem:$dst)>, Requires<[Not16BitMode]>;
2643 def : InstAlias<"lcall $seg, $off", (FARCALL16i i16imm:$off, i16imm:$seg)>, Requires<[In16BitMode]>;
2644 def : InstAlias<"ljmp $seg, $off",  (FARJMP16i  i16imm:$off, i16imm:$seg)>, Requires<[In16BitMode]>;
2645 def : InstAlias<"lcall *$dst",      (FARCALL16m opaque32mem:$dst)>, Requires<[In16BitMode]>;
2646 def : InstAlias<"ljmp *$dst",       (FARJMP16m  opaque32mem:$dst)>, Requires<[In16BitMode]>;
2647
2648 def : InstAlias<"call *$dst",       (CALL64m i16mem:$dst)>, Requires<[In64BitMode]>;
2649 def : InstAlias<"jmp *$dst",        (JMP64m  i16mem:$dst)>, Requires<[In64BitMode]>;
2650 def : InstAlias<"call *$dst",       (CALL32m i16mem:$dst)>, Requires<[In32BitMode]>;
2651 def : InstAlias<"jmp *$dst",        (JMP32m  i16mem:$dst)>, Requires<[In32BitMode]>;
2652 def : InstAlias<"call *$dst",       (CALL16m i16mem:$dst)>, Requires<[In16BitMode]>;
2653 def : InstAlias<"jmp *$dst",        (JMP16m  i16mem:$dst)>, Requires<[In16BitMode]>;
2654
2655
2656 // "imul <imm>, B" is an alias for "imul <imm>, B, B".
2657 def : InstAlias<"imulw $imm, $r", (IMUL16rri  GR16:$r, GR16:$r, i16imm:$imm)>;
2658 def : InstAlias<"imulw $imm, $r", (IMUL16rri8 GR16:$r, GR16:$r, i16i8imm:$imm)>;
2659 def : InstAlias<"imull $imm, $r", (IMUL32rri  GR32:$r, GR32:$r, i32imm:$imm)>;
2660 def : InstAlias<"imull $imm, $r", (IMUL32rri8 GR32:$r, GR32:$r, i32i8imm:$imm)>;
2661 def : InstAlias<"imulq $imm, $r",(IMUL64rri32 GR64:$r, GR64:$r,i64i32imm:$imm)>;
2662 def : InstAlias<"imulq $imm, $r", (IMUL64rri8 GR64:$r, GR64:$r, i64i8imm:$imm)>;
2663
2664 // inb %dx -> inb %al, %dx
2665 def : InstAlias<"inb\t{%dx|dx}", (IN8rr), 0>;
2666 def : InstAlias<"inw\t{%dx|dx}", (IN16rr), 0>;
2667 def : InstAlias<"inl\t{%dx|dx}", (IN32rr), 0>;
2668 def : InstAlias<"inb\t$port", (IN8ri i8imm:$port), 0>;
2669 def : InstAlias<"inw\t$port", (IN16ri i8imm:$port), 0>;
2670 def : InstAlias<"inl\t$port", (IN32ri i8imm:$port), 0>;
2671
2672
2673 // jmp and call aliases for lcall and ljmp.  jmp $42,$5 -> ljmp
2674 def : InstAlias<"call $seg, $off",  (FARCALL16i i16imm:$off, i16imm:$seg)>, Requires<[In16BitMode]>;
2675 def : InstAlias<"jmp $seg, $off",   (FARJMP16i  i16imm:$off, i16imm:$seg)>, Requires<[In16BitMode]>;
2676 def : InstAlias<"call $seg, $off",  (FARCALL32i i32imm:$off, i16imm:$seg)>, Requires<[Not16BitMode]>;
2677 def : InstAlias<"jmp $seg, $off",   (FARJMP32i  i32imm:$off, i16imm:$seg)>, Requires<[Not16BitMode]>;
2678 def : InstAlias<"callw $seg, $off", (FARCALL16i i16imm:$off, i16imm:$seg)>;
2679 def : InstAlias<"jmpw $seg, $off",  (FARJMP16i  i16imm:$off, i16imm:$seg)>;
2680 def : InstAlias<"calll $seg, $off", (FARCALL32i i32imm:$off, i16imm:$seg)>;
2681 def : InstAlias<"jmpl $seg, $off",  (FARJMP32i  i32imm:$off, i16imm:$seg)>;
2682
2683 // Force mov without a suffix with a segment and mem to prefer the 'l' form of
2684 // the move.  All segment/mem forms are equivalent, this has the shortest
2685 // encoding.
2686 def : InstAlias<"mov $mem, $seg", (MOV32sm SEGMENT_REG:$seg, i32mem:$mem)>;
2687 def : InstAlias<"mov $seg, $mem", (MOV32ms i32mem:$mem, SEGMENT_REG:$seg)>;
2688
2689 // Match 'movq <largeimm>, <reg>' as an alias for movabsq.
2690 def : InstAlias<"movq $imm, $reg", (MOV64ri GR64:$reg, i64imm:$imm)>;
2691
2692 // Match 'movq GR64, MMX' as an alias for movd.
2693 def : InstAlias<"movq $src, $dst",
2694                 (MMX_MOVD64to64rr VR64:$dst, GR64:$src), 0>;
2695 def : InstAlias<"movq $src, $dst",
2696                 (MMX_MOVD64from64rr GR64:$dst, VR64:$src), 0>;
2697
2698 // movsx aliases
2699 def : InstAlias<"movsx $src, $dst", (MOVSX16rr8 GR16:$dst, GR8:$src), 0>;
2700 def : InstAlias<"movsx $src, $dst", (MOVSX16rm8 GR16:$dst, i8mem:$src), 0>;
2701 def : InstAlias<"movsx $src, $dst", (MOVSX32rr8 GR32:$dst, GR8:$src), 0>;
2702 def : InstAlias<"movsx $src, $dst", (MOVSX32rr16 GR32:$dst, GR16:$src), 0>;
2703 def : InstAlias<"movsx $src, $dst", (MOVSX64rr8 GR64:$dst, GR8:$src), 0>;
2704 def : InstAlias<"movsx $src, $dst", (MOVSX64rr16 GR64:$dst, GR16:$src), 0>;
2705 def : InstAlias<"movsx $src, $dst", (MOVSX64rr32 GR64:$dst, GR32:$src), 0>;
2706
2707 // movzx aliases
2708 def : InstAlias<"movzx $src, $dst", (MOVZX16rr8 GR16:$dst, GR8:$src), 0>;
2709 def : InstAlias<"movzx $src, $dst", (MOVZX16rm8 GR16:$dst, i8mem:$src), 0>;
2710 def : InstAlias<"movzx $src, $dst", (MOVZX32rr8 GR32:$dst, GR8:$src), 0>;
2711 def : InstAlias<"movzx $src, $dst", (MOVZX32rr16 GR32:$dst, GR16:$src), 0>;
2712 def : InstAlias<"movzx $src, $dst", (MOVZX64rr8_Q GR64:$dst, GR8:$src), 0>;
2713 def : InstAlias<"movzx $src, $dst", (MOVZX64rr16_Q GR64:$dst, GR16:$src), 0>;
2714 // Note: No GR32->GR64 movzx form.
2715
2716 // outb %dx -> outb %al, %dx
2717 def : InstAlias<"outb\t{%dx|dx}", (OUT8rr), 0>;
2718 def : InstAlias<"outw\t{%dx|dx}", (OUT16rr), 0>;
2719 def : InstAlias<"outl\t{%dx|dx}", (OUT32rr), 0>;
2720 def : InstAlias<"outb\t$port", (OUT8ir i8imm:$port), 0>;
2721 def : InstAlias<"outw\t$port", (OUT16ir i8imm:$port), 0>;
2722 def : InstAlias<"outl\t$port", (OUT32ir i8imm:$port), 0>;
2723
2724 // 'sldt <mem>' can be encoded with either sldtw or sldtq with the same
2725 // effect (both store to a 16-bit mem).  Force to sldtw to avoid ambiguity
2726 // errors, since its encoding is the most compact.
2727 def : InstAlias<"sldt $mem", (SLDT16m i16mem:$mem)>;
2728
2729 // shld/shrd op,op -> shld op, op, CL
2730 def : InstAlias<"shld{w}\t{$r2, $r1|$r1, $r2}", (SHLD16rrCL GR16:$r1, GR16:$r2), 0>;
2731 def : InstAlias<"shld{l}\t{$r2, $r1|$r1, $r2}", (SHLD32rrCL GR32:$r1, GR32:$r2), 0>;
2732 def : InstAlias<"shld{q}\t{$r2, $r1|$r1, $r2}", (SHLD64rrCL GR64:$r1, GR64:$r2), 0>;
2733 def : InstAlias<"shrd{w}\t{$r2, $r1|$r1, $r2}", (SHRD16rrCL GR16:$r1, GR16:$r2), 0>;
2734 def : InstAlias<"shrd{l}\t{$r2, $r1|$r1, $r2}", (SHRD32rrCL GR32:$r1, GR32:$r2), 0>;
2735 def : InstAlias<"shrd{q}\t{$r2, $r1|$r1, $r2}", (SHRD64rrCL GR64:$r1, GR64:$r2), 0>;
2736
2737 def : InstAlias<"shld{w}\t{$reg, $mem|$mem, $reg}", (SHLD16mrCL i16mem:$mem, GR16:$reg), 0>;
2738 def : InstAlias<"shld{l}\t{$reg, $mem|$mem, $reg}", (SHLD32mrCL i32mem:$mem, GR32:$reg), 0>;
2739 def : InstAlias<"shld{q}\t{$reg, $mem|$mem, $reg}", (SHLD64mrCL i64mem:$mem, GR64:$reg), 0>;
2740 def : InstAlias<"shrd{w}\t{$reg, $mem|$mem, $reg}", (SHRD16mrCL i16mem:$mem, GR16:$reg), 0>;
2741 def : InstAlias<"shrd{l}\t{$reg, $mem|$mem, $reg}", (SHRD32mrCL i32mem:$mem, GR32:$reg), 0>;
2742 def : InstAlias<"shrd{q}\t{$reg, $mem|$mem, $reg}", (SHRD64mrCL i64mem:$mem, GR64:$reg), 0>;
2743
2744 /*  FIXME: This is disabled because the asm matcher is currently incapable of
2745  *  matching a fixed immediate like $1.
2746 // "shl X, $1" is an alias for "shl X".
2747 multiclass ShiftRotateByOneAlias<string Mnemonic, string Opc> {
2748  def : InstAlias<!strconcat(Mnemonic, "b $op, $$1"),
2749                  (!cast<Instruction>(!strconcat(Opc, "8r1")) GR8:$op)>;
2750  def : InstAlias<!strconcat(Mnemonic, "w $op, $$1"),
2751                  (!cast<Instruction>(!strconcat(Opc, "16r1")) GR16:$op)>;
2752  def : InstAlias<!strconcat(Mnemonic, "l $op, $$1"),
2753                  (!cast<Instruction>(!strconcat(Opc, "32r1")) GR32:$op)>;
2754  def : InstAlias<!strconcat(Mnemonic, "q $op, $$1"),
2755                  (!cast<Instruction>(!strconcat(Opc, "64r1")) GR64:$op)>;
2756  def : InstAlias<!strconcat(Mnemonic, "b $op, $$1"),
2757                  (!cast<Instruction>(!strconcat(Opc, "8m1")) i8mem:$op)>;
2758  def : InstAlias<!strconcat(Mnemonic, "w $op, $$1"),
2759                  (!cast<Instruction>(!strconcat(Opc, "16m1")) i16mem:$op)>;
2760  def : InstAlias<!strconcat(Mnemonic, "l $op, $$1"),
2761                  (!cast<Instruction>(!strconcat(Opc, "32m1")) i32mem:$op)>;
2762  def : InstAlias<!strconcat(Mnemonic, "q $op, $$1"),
2763                  (!cast<Instruction>(!strconcat(Opc, "64m1")) i64mem:$op)>;
2764 }
2765
2766 defm : ShiftRotateByOneAlias<"rcl", "RCL">;
2767 defm : ShiftRotateByOneAlias<"rcr", "RCR">;
2768 defm : ShiftRotateByOneAlias<"rol", "ROL">;
2769 defm : ShiftRotateByOneAlias<"ror", "ROR">;
2770 FIXME */
2771
2772 // test: We accept "testX <reg>, <mem>" and "testX <mem>, <reg>" as synonyms.
2773 def : InstAlias<"test{b}\t{$val, $mem|$mem, $val}", (TEST8rm  GR8 :$val, i8mem :$mem)>;
2774 def : InstAlias<"test{w}\t{$val, $mem|$mem, $val}", (TEST16rm GR16:$val, i16mem:$mem)>;
2775 def : InstAlias<"test{l}\t{$val, $mem|$mem, $val}", (TEST32rm GR32:$val, i32mem:$mem)>;
2776 def : InstAlias<"test{q}\t{$val, $mem|$mem, $val}", (TEST64rm GR64:$val, i64mem:$mem)>;
2777
2778 // xchg: We accept "xchgX <reg>, <mem>" and "xchgX <mem>, <reg>" as synonyms.
2779 def : InstAlias<"xchg{b}\t{$mem, $val|$val, $mem}", (XCHG8rm  GR8 :$val, i8mem :$mem)>;
2780 def : InstAlias<"xchg{w}\t{$mem, $val|$val, $mem}", (XCHG16rm GR16:$val, i16mem:$mem)>;
2781 def : InstAlias<"xchg{l}\t{$mem, $val|$val, $mem}", (XCHG32rm GR32:$val, i32mem:$mem)>;
2782 def : InstAlias<"xchg{q}\t{$mem, $val|$val, $mem}", (XCHG64rm GR64:$val, i64mem:$mem)>;
2783
2784 // xchg: We accept "xchgX <reg>, %eax" and "xchgX %eax, <reg>" as synonyms.
2785 def : InstAlias<"xchg{w}\t{%ax, $src|$src, ax}", (XCHG16ar GR16:$src)>;
2786 def : InstAlias<"xchg{l}\t{%eax, $src|$src, eax}", (XCHG32ar GR32:$src)>, Requires<[Not64BitMode]>;
2787 def : InstAlias<"xchg{l}\t{%eax, $src|$src, eax}", (XCHG32ar64 GR32_NOAX:$src)>, Requires<[In64BitMode]>;
2788 def : InstAlias<"xchg{q}\t{%rax, $src|$src, rax}", (XCHG64ar GR64:$src)>;