a52db350500b448fd7e5afb180135e4870fb1dd4
[oota-llvm.git] / lib / Target / X86 / X86InstrFormats.td
1 //===- X86InstrFormats.td - X86 Instruction Formats --------*- 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 //===----------------------------------------------------------------------===//
11 // X86 Instruction Format Definitions.
12 //
13
14 // Format specifies the encoding used by the instruction.  This is part of the
15 // ad-hoc solution used to emit machine instruction encodings by our machine
16 // code emitter.
17 class Format<bits<6> val> {
18   bits<6> Value = val;
19 }
20
21 def Pseudo     : Format<0>; def RawFrm     : Format<1>;
22 def AddRegFrm  : Format<2>; def MRMDestReg : Format<3>;
23 def MRMDestMem : Format<4>; def MRMSrcReg  : Format<5>;
24 def MRMSrcMem  : Format<6>;
25 def MRM0r  : Format<16>; def MRM1r  : Format<17>; def MRM2r  : Format<18>;
26 def MRM3r  : Format<19>; def MRM4r  : Format<20>; def MRM5r  : Format<21>;
27 def MRM6r  : Format<22>; def MRM7r  : Format<23>;
28 def MRM0m  : Format<24>; def MRM1m  : Format<25>; def MRM2m  : Format<26>;
29 def MRM3m  : Format<27>; def MRM4m  : Format<28>; def MRM5m  : Format<29>;
30 def MRM6m  : Format<30>; def MRM7m  : Format<31>;
31 def MRMInitReg : Format<32>;
32 def MRM_C1 : Format<33>;
33 def MRM_C2 : Format<34>;
34 def MRM_C3 : Format<35>;
35 def MRM_C4 : Format<36>;
36 def MRM_C8 : Format<37>;
37 def MRM_C9 : Format<38>;
38 def MRM_E8 : Format<39>;
39 def MRM_F0 : Format<40>;
40 def MRM_F8 : Format<41>;
41 def MRM_F9 : Format<42>;
42
43 // ImmType - This specifies the immediate type used by an instruction. This is
44 // part of the ad-hoc solution used to emit machine instruction encodings by our
45 // machine code emitter.
46 class ImmType<bits<3> val> {
47   bits<3> Value = val;
48 }
49 def NoImm      : ImmType<0>;
50 def Imm8       : ImmType<1>;
51 def Imm8PCRel  : ImmType<2>;
52 def Imm16      : ImmType<3>;
53 def Imm32      : ImmType<4>;
54 def Imm32PCRel : ImmType<5>;
55 def Imm64      : ImmType<6>;
56
57 // FPFormat - This specifies what form this FP instruction has.  This is used by
58 // the Floating-Point stackifier pass.
59 class FPFormat<bits<3> val> {
60   bits<3> Value = val;
61 }
62 def NotFP      : FPFormat<0>;
63 def ZeroArgFP  : FPFormat<1>;
64 def OneArgFP   : FPFormat<2>;
65 def OneArgFPRW : FPFormat<3>;
66 def TwoArgFP   : FPFormat<4>;
67 def CompareFP  : FPFormat<5>;
68 def CondMovFP  : FPFormat<6>;
69 def SpecialFP  : FPFormat<7>;
70
71 // Class specifying the SSE execution domain, used by the SSEDomainFix pass.
72 // Keep in sync with tables in X86InstrInfo.cpp.
73 class Domain<bits<2> val> {
74   bits<2> Value = val;
75 }
76 def GenericDomain   : Domain<0>;
77 def SSEPackedSingle : Domain<1>;
78 def SSEPackedDouble : Domain<2>;
79 def SSEPackedInt    : Domain<3>;
80
81 // Prefix byte classes which are used to indicate to the ad-hoc machine code
82 // emitter that various prefix bytes are required.
83 class OpSize { bit hasOpSizePrefix = 1; }
84 class AdSize { bit hasAdSizePrefix = 1; }
85 class REX_W  { bit hasREX_WPrefix = 1; }
86 class VEX_4V { bit hasVEX_4VPrefix = 1; }
87 class LOCK   { bit hasLockPrefix = 1; }
88 class SegFS  { bits<2> SegOvrBits = 1; }
89 class SegGS  { bits<2> SegOvrBits = 2; }
90 class TB     { bits<4> Prefix = 1; }
91 class REP    { bits<4> Prefix = 2; }
92 class D8     { bits<4> Prefix = 3; }
93 class D9     { bits<4> Prefix = 4; }
94 class DA     { bits<4> Prefix = 5; }
95 class DB     { bits<4> Prefix = 6; }
96 class DC     { bits<4> Prefix = 7; }
97 class DD     { bits<4> Prefix = 8; }
98 class DE     { bits<4> Prefix = 9; }
99 class DF     { bits<4> Prefix = 10; }
100 class XD     { bits<4> Prefix = 11; }
101 class XS     { bits<4> Prefix = 12; }
102 class T8     { bits<4> Prefix = 13; }
103 class TA     { bits<4> Prefix = 14; }
104 class TF     { bits<4> Prefix = 15; }
105
106 class X86Inst<bits<8> opcod, Format f, ImmType i, dag outs, dag ins,
107               string AsmStr, Domain d = GenericDomain>
108   : Instruction {
109   let Namespace = "X86";
110
111   bits<8> Opcode = opcod;
112   Format Form = f;
113   bits<6> FormBits = Form.Value;
114   ImmType ImmT = i;
115
116   dag OutOperandList = outs;
117   dag InOperandList = ins;
118   string AsmString = AsmStr;
119
120   //
121   // Attributes specific to X86 instructions...
122   //
123   bit hasOpSizePrefix = 0;  // Does this inst have a 0x66 prefix?
124   bit hasAdSizePrefix = 0;  // Does this inst have a 0x67 prefix?
125
126   bits<4> Prefix = 0;       // Which prefix byte does this inst have?
127   bit hasREX_WPrefix  = 0;  // Does this inst requires the REX.W prefix?
128   bit hasVEX_4VPrefix  = 0;  // Does this inst requires the VEX.VVVV prefix?
129   FPFormat FPForm = NotFP;  // What flavor of FP instruction is this?
130   bit hasLockPrefix = 0;    // Does this inst have a 0xF0 prefix?
131   bits<2> SegOvrBits = 0;   // Segment override prefix.
132   Domain ExeDomain = d;
133
134   // TSFlags layout should be kept in sync with X86InstrInfo.h.
135   let TSFlags{5-0}   = FormBits;
136   let TSFlags{6}     = hasOpSizePrefix;
137   let TSFlags{7}     = hasAdSizePrefix;
138   let TSFlags{11-8}  = Prefix;
139   let TSFlags{12}    = hasREX_WPrefix;
140   let TSFlags{15-13} = ImmT.Value;
141   let TSFlags{18-16} = FPForm.Value;
142   let TSFlags{19}    = hasLockPrefix;
143   let TSFlags{21-20} = SegOvrBits;
144   let TSFlags{23-22} = ExeDomain.Value;
145   let TSFlags{31-24} = Opcode;
146   let TSFlags{32}    = hasVEX_4VPrefix;
147 }
148
149 class I<bits<8> o, Format f, dag outs, dag ins, string asm,
150         list<dag> pattern, Domain d = GenericDomain>
151   : X86Inst<o, f, NoImm, outs, ins, asm, d> {
152   let Pattern = pattern;
153   let CodeSize = 3;
154 }
155 class Ii8 <bits<8> o, Format f, dag outs, dag ins, string asm, 
156            list<dag> pattern, Domain d = GenericDomain>
157   : X86Inst<o, f, Imm8, outs, ins, asm, d> {
158   let Pattern = pattern;
159   let CodeSize = 3;
160 }
161 class Ii8PCRel<bits<8> o, Format f, dag outs, dag ins, string asm, 
162                list<dag> pattern>
163   : X86Inst<o, f, Imm8PCRel, outs, ins, asm> {
164   let Pattern = pattern;
165   let CodeSize = 3;
166 }
167 class Ii16<bits<8> o, Format f, dag outs, dag ins, string asm, 
168            list<dag> pattern>
169   : X86Inst<o, f, Imm16, outs, ins, asm> {
170   let Pattern = pattern;
171   let CodeSize = 3;
172 }
173 class Ii32<bits<8> o, Format f, dag outs, dag ins, string asm, 
174            list<dag> pattern>
175   : X86Inst<o, f, Imm32, outs, ins, asm> {
176   let Pattern = pattern;
177   let CodeSize = 3;
178 }
179
180 class Ii32PCRel<bits<8> o, Format f, dag outs, dag ins, string asm, 
181            list<dag> pattern>
182   : X86Inst<o, f, Imm32PCRel, outs, ins, asm> {
183   let Pattern = pattern;
184   let CodeSize = 3;
185 }
186
187 // FPStack Instruction Templates:
188 // FPI - Floating Point Instruction template.
189 class FPI<bits<8> o, Format F, dag outs, dag ins, string asm>
190   : I<o, F, outs, ins, asm, []> {}
191
192 // FpI_ - Floating Point Psuedo Instruction template. Not Predicated.
193 class FpI_<dag outs, dag ins, FPFormat fp, list<dag> pattern>
194   : X86Inst<0, Pseudo, NoImm, outs, ins, ""> {
195   let FPForm = fp;
196   let Pattern = pattern;
197 }
198
199 // Templates for instructions that use a 16- or 32-bit segmented address as
200 //  their only operand: lcall (FAR CALL) and ljmp (FAR JMP)
201 //
202 //   Iseg16 - 16-bit segment selector, 16-bit offset
203 //   Iseg32 - 16-bit segment selector, 32-bit offset
204
205 class Iseg16 <bits<8> o, Format f, dag outs, dag ins, string asm, 
206               list<dag> pattern> : X86Inst<o, f, NoImm, outs, ins, asm> {
207   let Pattern = pattern;
208   let CodeSize = 3;
209 }
210
211 class Iseg32 <bits<8> o, Format f, dag outs, dag ins, string asm, 
212               list<dag> pattern> : X86Inst<o, f, NoImm, outs, ins, asm> {
213   let Pattern = pattern;
214   let CodeSize = 3;
215 }
216
217 // SI - SSE 1 & 2 scalar instructions
218 class SI<bits<8> o, Format F, dag outs, dag ins, string asm, list<dag> pattern>
219       : I<o, F, outs, ins, asm, pattern> {
220   let Predicates = !if(hasVEX_4VPrefix /* VEX_4V */,
221             !if(!eq(Prefix, 11 /* XD */), [HasAVX, HasSSE2], [HasAVX, HasSSE1]),
222             !if(!eq(Prefix, 12 /* XS */), [HasSSE2], [HasSSE1]));
223
224   // AVX instructions have a 'v' prefix in the mnemonic
225   let AsmString = !if(hasVEX_4VPrefix, !strconcat("v", asm), asm);
226 }
227
228 // PI - SSE 1 & 2 packed instructions
229 class PI<bits<8> o, Format F, dag outs, dag ins, string asm, list<dag> pattern,
230          Domain d>
231       : I<o, F, outs, ins, asm, pattern, d> {
232   let Predicates = !if(hasVEX_4VPrefix /* VEX_4V */,
233         !if(hasOpSizePrefix /* OpSize */, [HasAVX, HasSSE2], [HasAVX, HasSSE1]),
234         !if(hasOpSizePrefix /* OpSize */, [HasSSE2], [HasSSE1]));
235
236   // AVX instructions have a 'v' prefix in the mnemonic
237   let AsmString = !if(hasVEX_4VPrefix, !strconcat("v", asm), asm);
238 }
239
240 // SSE1 Instruction Templates:
241 // 
242 //   SSI   - SSE1 instructions with XS prefix.
243 //   PSI   - SSE1 instructions with TB prefix.
244 //   PSIi8 - SSE1 instructions with ImmT == Imm8 and TB prefix.
245 //   VSSI  - SSE1 instructions with XS prefix in AVX form.
246 //   VPSI  - SSE1 instructions with TB prefix in AVX form.
247
248 class SSI<bits<8> o, Format F, dag outs, dag ins, string asm, list<dag> pattern>
249       : I<o, F, outs, ins, asm, pattern>, XS, Requires<[HasSSE1]>;
250 class SSIi8<bits<8> o, Format F, dag outs, dag ins, string asm,
251             list<dag> pattern>
252       : Ii8<o, F, outs, ins, asm, pattern>, XS, Requires<[HasSSE1]>;
253 class PSI<bits<8> o, Format F, dag outs, dag ins, string asm, list<dag> pattern>
254       : I<o, F, outs, ins, asm, pattern, SSEPackedSingle>, TB,
255         Requires<[HasSSE1]>;
256 class PSIi8<bits<8> o, Format F, dag outs, dag ins, string asm,
257             list<dag> pattern>
258       : Ii8<o, F, outs, ins, asm, pattern, SSEPackedSingle>, TB,
259         Requires<[HasSSE1]>;
260 class VSSI<bits<8> o, Format F, dag outs, dag ins, string asm,
261            list<dag> pattern>
262       : I<o, F, outs, ins, !strconcat("v", asm), pattern>, XS, VEX_4V,
263         Requires<[HasAVX, HasSSE1]>;
264 class VPSI<bits<8> o, Format F, dag outs, dag ins, string asm,
265            list<dag> pattern>
266       : I<o, F, outs, ins, !strconcat("v", asm), pattern, SSEPackedSingle>,
267         VEX_4V, Requires<[HasAVX, HasSSE1]>;
268
269 // SSE2 Instruction Templates:
270 // 
271 //   SDI    - SSE2 instructions with XD prefix.
272 //   SDIi8  - SSE2 instructions with ImmT == Imm8 and XD prefix.
273 //   SSDIi8 - SSE2 instructions with ImmT == Imm8 and XS prefix.
274 //   PDI    - SSE2 instructions with TB and OpSize prefixes.
275 //   PDIi8  - SSE2 instructions with ImmT == Imm8 and TB and OpSize prefixes.
276 //   VSDI   - SSE2 instructions with XD prefix in AVX form.
277 //   VPDI   - SSE2 instructions with TB and OpSize prefixes in AVX form.
278
279 class SDI<bits<8> o, Format F, dag outs, dag ins, string asm, list<dag> pattern>
280       : I<o, F, outs, ins, asm, pattern>, XD, Requires<[HasSSE2]>;
281 class SDIi8<bits<8> o, Format F, dag outs, dag ins, string asm,
282             list<dag> pattern>
283       : Ii8<o, F, outs, ins, asm, pattern>, XD, Requires<[HasSSE2]>;
284 class SSDIi8<bits<8> o, Format F, dag outs, dag ins, string asm,
285              list<dag> pattern>
286       : Ii8<o, F, outs, ins, asm, pattern>, XS, Requires<[HasSSE2]>;
287 class PDI<bits<8> o, Format F, dag outs, dag ins, string asm, list<dag> pattern>
288       : I<o, F, outs, ins, asm, pattern, SSEPackedDouble>, TB, OpSize,
289         Requires<[HasSSE2]>;
290 class PDIi8<bits<8> o, Format F, dag outs, dag ins, string asm,
291             list<dag> pattern>
292       : Ii8<o, F, outs, ins, asm, pattern, SSEPackedDouble>, TB, OpSize,
293         Requires<[HasSSE2]>;
294 class VSDI<bits<8> o, Format F, dag outs, dag ins, string asm,
295            list<dag> pattern>
296       : I<o, F, outs, ins, !strconcat("v", asm), pattern>, XD, VEX_4V,
297         Requires<[HasAVX, HasSSE2]>;
298 class VPDI<bits<8> o, Format F, dag outs, dag ins, string asm,
299            list<dag> pattern>
300       : I<o, F, outs, ins, !strconcat("v", asm), pattern, SSEPackedDouble>,
301         VEX_4V, OpSize, Requires<[HasAVX, HasSSE2]>;
302
303 // SSE3 Instruction Templates:
304 // 
305 //   S3I   - SSE3 instructions with TB and OpSize prefixes.
306 //   S3SI  - SSE3 instructions with XS prefix.
307 //   S3DI  - SSE3 instructions with XD prefix.
308
309 class S3SI<bits<8> o, Format F, dag outs, dag ins, string asm, 
310            list<dag> pattern>
311       : I<o, F, outs, ins, asm, pattern, SSEPackedSingle>, XS,
312         Requires<[HasSSE3]>;
313 class S3DI<bits<8> o, Format F, dag outs, dag ins, string asm, 
314            list<dag> pattern>
315       : I<o, F, outs, ins, asm, pattern, SSEPackedDouble>, XD,
316         Requires<[HasSSE3]>;
317 class S3I<bits<8> o, Format F, dag outs, dag ins, string asm, list<dag> pattern>
318       : I<o, F, outs, ins, asm, pattern, SSEPackedDouble>, TB, OpSize,
319         Requires<[HasSSE3]>;
320
321
322 // SSSE3 Instruction Templates:
323 // 
324 //   SS38I - SSSE3 instructions with T8 prefix.
325 //   SS3AI - SSSE3 instructions with TA prefix.
326 //
327 // Note: SSSE3 instructions have 64-bit and 128-bit versions. The 64-bit version
328 // uses the MMX registers. We put those instructions here because they better
329 // fit into the SSSE3 instruction category rather than the MMX category.
330
331 class SS38I<bits<8> o, Format F, dag outs, dag ins, string asm,
332             list<dag> pattern>
333       : Ii8<o, F, outs, ins, asm, pattern, SSEPackedInt>, T8,
334         Requires<[HasSSSE3]>;
335 class SS3AI<bits<8> o, Format F, dag outs, dag ins, string asm,
336             list<dag> pattern>
337       : Ii8<o, F, outs, ins, asm, pattern, SSEPackedInt>, TA,
338         Requires<[HasSSSE3]>;
339
340 // SSE4.1 Instruction Templates:
341 // 
342 //   SS48I - SSE 4.1 instructions with T8 prefix.
343 //   SS41AIi8 - SSE 4.1 instructions with TA prefix and ImmT == Imm8.
344 //
345 class SS48I<bits<8> o, Format F, dag outs, dag ins, string asm,
346             list<dag> pattern>
347       : I<o, F, outs, ins, asm, pattern, SSEPackedInt>, T8,
348         Requires<[HasSSE41]>;
349 class SS4AIi8<bits<8> o, Format F, dag outs, dag ins, string asm,
350             list<dag> pattern>
351       : Ii8<o, F, outs, ins, asm, pattern, SSEPackedInt>, TA,
352         Requires<[HasSSE41]>;
353
354 // SSE4.2 Instruction Templates:
355 // 
356 //   SS428I - SSE 4.2 instructions with T8 prefix.
357 class SS428I<bits<8> o, Format F, dag outs, dag ins, string asm,
358              list<dag> pattern>
359       : I<o, F, outs, ins, asm, pattern, SSEPackedInt>, T8,
360         Requires<[HasSSE42]>;
361
362 //   SS42FI - SSE 4.2 instructions with TF prefix.
363 class SS42FI<bits<8> o, Format F, dag outs, dag ins, string asm,
364               list<dag> pattern>
365       : I<o, F, outs, ins, asm, pattern>, TF, Requires<[HasSSE42]>;
366       
367 //   SS42AI = SSE 4.2 instructions with TA prefix
368 class SS42AI<bits<8> o, Format F, dag outs, dag ins, string asm,
369              list<dag> pattern>
370       : Ii8<o, F, outs, ins, asm, pattern, SSEPackedInt>, TA,
371         Requires<[HasSSE42]>;
372
373 // AES Instruction Templates:
374 //
375 // AES8I
376 // These use the same encoding as the SSE4.2 T8 and TA encodings.
377 class AES8I<bits<8> o, Format F, dag outs, dag ins, string asm,
378             list<dag>pattern>
379       : I<o, F, outs, ins, asm, pattern, SSEPackedInt>, T8,
380         Requires<[HasAES]>;
381
382 class AESAI<bits<8> o, Format F, dag outs, dag ins, string asm,
383             list<dag> pattern>
384       : Ii8<o, F, outs, ins, asm, pattern, SSEPackedInt>, TA,
385         Requires<[HasAES]>;
386
387 // X86-64 Instruction templates...
388 //
389
390 class RI<bits<8> o, Format F, dag outs, dag ins, string asm, list<dag> pattern>
391       : I<o, F, outs, ins, asm, pattern>, REX_W;
392 class RIi8 <bits<8> o, Format F, dag outs, dag ins, string asm,
393             list<dag> pattern>
394       : Ii8<o, F, outs, ins, asm, pattern>, REX_W;
395 class RIi32 <bits<8> o, Format F, dag outs, dag ins, string asm,
396              list<dag> pattern>
397       : Ii32<o, F, outs, ins, asm, pattern>, REX_W;
398
399 class RIi64<bits<8> o, Format f, dag outs, dag ins, string asm,
400             list<dag> pattern>
401   : X86Inst<o, f, Imm64, outs, ins, asm>, REX_W {
402   let Pattern = pattern;
403   let CodeSize = 3;
404 }
405
406 class RSSI<bits<8> o, Format F, dag outs, dag ins, string asm,
407            list<dag> pattern>
408       : SSI<o, F, outs, ins, asm, pattern>, REX_W;
409 class RSDI<bits<8> o, Format F, dag outs, dag ins, string asm,
410            list<dag> pattern>
411       : SDI<o, F, outs, ins, asm, pattern>, REX_W;
412 class RPDI<bits<8> o, Format F, dag outs, dag ins, string asm,
413            list<dag> pattern>
414       : PDI<o, F, outs, ins, asm, pattern>, REX_W;
415
416 // MMX Instruction templates
417 //
418
419 // MMXI   - MMX instructions with TB prefix.
420 // MMXI64 - MMX instructions with TB prefix valid only in 64 bit mode.
421 // MMX2I  - MMX / SSE2 instructions with TB and OpSize prefixes.
422 // MMXIi8 - MMX instructions with ImmT == Imm8 and TB prefix.
423 // MMXIi8 - MMX instructions with ImmT == Imm8 and TB prefix.
424 // MMXID  - MMX instructions with XD prefix.
425 // MMXIS  - MMX instructions with XS prefix.
426 class MMXI<bits<8> o, Format F, dag outs, dag ins, string asm, 
427            list<dag> pattern>
428       : I<o, F, outs, ins, asm, pattern>, TB, Requires<[HasMMX]>;
429 class MMXI64<bits<8> o, Format F, dag outs, dag ins, string asm, 
430              list<dag> pattern>
431       : I<o, F, outs, ins, asm, pattern>, TB, Requires<[HasMMX,In64BitMode]>;
432 class MMXRI<bits<8> o, Format F, dag outs, dag ins, string asm, 
433             list<dag> pattern>
434       : I<o, F, outs, ins, asm, pattern>, TB, REX_W, Requires<[HasMMX]>;
435 class MMX2I<bits<8> o, Format F, dag outs, dag ins, string asm, 
436             list<dag> pattern>
437       : I<o, F, outs, ins, asm, pattern>, TB, OpSize, Requires<[HasMMX]>;
438 class MMXIi8<bits<8> o, Format F, dag outs, dag ins, string asm, 
439              list<dag> pattern>
440       : Ii8<o, F, outs, ins, asm, pattern>, TB, Requires<[HasMMX]>;
441 class MMXID<bits<8> o, Format F, dag outs, dag ins, string asm, 
442             list<dag> pattern>
443       : Ii8<o, F, outs, ins, asm, pattern>, XD, Requires<[HasMMX]>;
444 class MMXIS<bits<8> o, Format F, dag outs, dag ins, string asm, 
445             list<dag> pattern>
446       : Ii8<o, F, outs, ins, asm, pattern>, XS, Requires<[HasMMX]>;