b87c4cee1364bd7aa9dbf5033ac84ba6c36b43b1
[oota-llvm.git] / lib / Target / X86 / X86InstrControl.td
1 //===-- X86InstrControl.td - Control Flow Instructions -----*- 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 jump, return, call, and related instructions.
11 //
12 //===----------------------------------------------------------------------===//
13
14 //===----------------------------------------------------------------------===//
15 //  Control Flow Instructions.
16 //
17
18 // Return instructions.
19 //
20 // The X86retflag return instructions are variadic because we may add ST0 and
21 // ST1 arguments when returning values on the x87 stack.
22 let isTerminator = 1, isReturn = 1, isBarrier = 1,
23     hasCtrlDep = 1, FPForm = SpecialFP, SchedRW = [WriteJumpLd] in {
24   def RETL   : I   <0xC3, RawFrm, (outs), (ins variable_ops),
25                     "ret{l}",
26                     [(X86retflag 0)], IIC_RET>, OpSize16, Requires<[Not64BitMode]>;
27   def RETQ   : I   <0xC3, RawFrm, (outs), (ins variable_ops),
28                     "ret{q}",
29                     [(X86retflag 0)], IIC_RET>, Requires<[In64BitMode]>;
30   def RETW   : I   <0xC3, RawFrm, (outs), (ins),
31                     "ret{w}",
32                     [], IIC_RET>, OpSize;
33   def RETI   : Ii16<0xC2, RawFrm, (outs), (ins i16imm:$amt, variable_ops),
34                     "ret{l}\t$amt",
35                     [(X86retflag timm:$amt)], IIC_RET_IMM>, OpSize16;
36   def RETIW  : Ii16<0xC2, RawFrm, (outs), (ins i16imm:$amt),
37                     "ret{w}\t$amt",
38                     [], IIC_RET_IMM>, OpSize;
39   def LRETL  : I   <0xCB, RawFrm, (outs), (ins),
40                     "{l}ret{l|f}", [], IIC_RET>, OpSize16;
41   def LRETW  : I   <0xCB, RawFrm, (outs), (ins),
42                     "{l}ret{w|f}", [], IIC_RET>, OpSize;
43   def LRETQ  : RI  <0xCB, RawFrm, (outs), (ins),
44                     "{l}ret{q|f}", [], IIC_RET>;
45   def LRETI  : Ii16<0xCA, RawFrm, (outs), (ins i16imm:$amt),
46                     "{l}ret{l|f}\t$amt", [], IIC_RET>, OpSize16;
47   def LRETIW : Ii16<0xCA, RawFrm, (outs), (ins i16imm:$amt),
48                     "{l}ret{w|f}\t$amt", [], IIC_RET>, OpSize;
49 }
50
51 // Unconditional branches.
52 let isBarrier = 1, isBranch = 1, isTerminator = 1, SchedRW = [WriteJump] in {
53   def JMP_4 : Ii32PCRel<0xE9, RawFrm, (outs), (ins brtarget:$dst),
54                         "jmp\t$dst", [(br bb:$dst)], IIC_JMP_REL>, OpSize16;
55   def JMP_2 : Ii16PCRel<0xE9, RawFrm, (outs), (ins brtarget:$dst),
56                         "jmp\t$dst", [(br bb:$dst)], IIC_JMP_REL>, OpSize,
57                         Requires<[In16BitMode]>;
58   let hasSideEffects = 0 in
59   def JMP_1 : Ii8PCRel<0xEB, RawFrm, (outs), (ins brtarget8:$dst),
60                        "jmp\t$dst", [], IIC_JMP_REL>;
61 }
62
63 // Conditional Branches.
64 let isBranch = 1, isTerminator = 1, Uses = [EFLAGS], SchedRW = [WriteJump] in {
65   multiclass ICBr<bits<8> opc1, bits<8> opc4, string asm, PatFrag Cond> {
66     let hasSideEffects = 0 in
67     def _1 : Ii8PCRel <opc1, RawFrm, (outs), (ins brtarget8:$dst), asm, [],
68                        IIC_Jcc>;
69     def _2 : Ii16PCRel<opc4, RawFrm, (outs), (ins brtarget:$dst), asm,
70                        [(X86brcond bb:$dst, Cond, EFLAGS)], IIC_Jcc>, OpSize,
71                        TB, Requires<[In16BitMode]>;
72     def _4 : Ii32PCRel<opc4, RawFrm, (outs), (ins brtarget:$dst), asm,
73                        [(X86brcond bb:$dst, Cond, EFLAGS)], IIC_Jcc>, TB,
74              OpSize16;
75   }
76 }
77
78 defm JO  : ICBr<0x70, 0x80, "jo\t$dst" , X86_COND_O>;
79 defm JNO : ICBr<0x71, 0x81, "jno\t$dst" , X86_COND_NO>;
80 defm JB  : ICBr<0x72, 0x82, "jb\t$dst" , X86_COND_B>;
81 defm JAE : ICBr<0x73, 0x83, "jae\t$dst", X86_COND_AE>;
82 defm JE  : ICBr<0x74, 0x84, "je\t$dst" , X86_COND_E>;
83 defm JNE : ICBr<0x75, 0x85, "jne\t$dst", X86_COND_NE>;
84 defm JBE : ICBr<0x76, 0x86, "jbe\t$dst", X86_COND_BE>;
85 defm JA  : ICBr<0x77, 0x87, "ja\t$dst" , X86_COND_A>;
86 defm JS  : ICBr<0x78, 0x88, "js\t$dst" , X86_COND_S>;
87 defm JNS : ICBr<0x79, 0x89, "jns\t$dst", X86_COND_NS>;
88 defm JP  : ICBr<0x7A, 0x8A, "jp\t$dst" , X86_COND_P>;
89 defm JNP : ICBr<0x7B, 0x8B, "jnp\t$dst", X86_COND_NP>;
90 defm JL  : ICBr<0x7C, 0x8C, "jl\t$dst" , X86_COND_L>;
91 defm JGE : ICBr<0x7D, 0x8D, "jge\t$dst", X86_COND_GE>;
92 defm JLE : ICBr<0x7E, 0x8E, "jle\t$dst", X86_COND_LE>;
93 defm JG  : ICBr<0x7F, 0x8F, "jg\t$dst" , X86_COND_G>;
94
95 // jcx/jecx/jrcx instructions.
96 let isBranch = 1, isTerminator = 1, hasSideEffects = 0, SchedRW = [WriteJump] in {
97   // These are the 32-bit versions of this instruction for the asmparser.  In
98   // 32-bit mode, the address size prefix is jcxz and the unprefixed version is
99   // jecxz.
100   let Uses = [CX] in
101     def JCXZ : Ii8PCRel<0xE3, RawFrm, (outs), (ins brtarget8:$dst),
102                         "jcxz\t$dst", [], IIC_JCXZ>, AdSize, Requires<[Not64BitMode]>;
103   let Uses = [ECX] in
104     def JECXZ_32 : Ii8PCRel<0xE3, RawFrm, (outs), (ins brtarget8:$dst),
105                            "jecxz\t$dst", [], IIC_JCXZ>, Requires<[Not64BitMode]>;
106
107   // J*CXZ instruction: 64-bit versions of this instruction for the asmparser.
108   // In 64-bit mode, the address size prefix is jecxz and the unprefixed version
109   // is jrcxz.
110   let Uses = [ECX] in
111     def JECXZ_64 : Ii8PCRel<0xE3, RawFrm, (outs), (ins brtarget8:$dst),
112                             "jecxz\t$dst", [], IIC_JCXZ>, AdSize, Requires<[In64BitMode]>;
113   let Uses = [RCX] in
114     def JRCXZ : Ii8PCRel<0xE3, RawFrm, (outs), (ins brtarget8:$dst),
115                            "jrcxz\t$dst", [], IIC_JCXZ>, Requires<[In64BitMode]>;
116 }
117
118 // Indirect branches
119 let isBranch = 1, isTerminator = 1, isBarrier = 1, isIndirectBranch = 1 in {
120   def JMP16r     : I<0xFF, MRM4r, (outs), (ins GR16:$dst), "jmp{w}\t{*}$dst",
121                      [(brind GR16:$dst)], IIC_JMP_REG>, Requires<[Not64BitMode]>,
122                    OpSize, Sched<[WriteJump]>;
123   def JMP16m     : I<0xFF, MRM4m, (outs), (ins i16mem:$dst), "jmp{w}\t{*}$dst",
124                      [(brind (loadi16 addr:$dst))], IIC_JMP_MEM>,
125                    Requires<[Not64BitMode]>, OpSize, Sched<[WriteJumpLd]>;
126
127   def JMP32r     : I<0xFF, MRM4r, (outs), (ins GR32:$dst), "jmp{l}\t{*}$dst",
128                      [(brind GR32:$dst)], IIC_JMP_REG>, Requires<[Not64BitMode]>,
129                    OpSize16, Sched<[WriteJump]>;
130   def JMP32m     : I<0xFF, MRM4m, (outs), (ins i32mem:$dst), "jmp{l}\t{*}$dst",
131                      [(brind (loadi32 addr:$dst))], IIC_JMP_MEM>,
132                    Requires<[Not64BitMode]>, OpSize16, Sched<[WriteJumpLd]>;
133
134   def JMP64r     : I<0xFF, MRM4r, (outs), (ins GR64:$dst), "jmp{q}\t{*}$dst",
135                      [(brind GR64:$dst)], IIC_JMP_REG>, Requires<[In64BitMode]>,
136                    Sched<[WriteJump]>;
137   def JMP64m     : I<0xFF, MRM4m, (outs), (ins i64mem:$dst), "jmp{q}\t{*}$dst",
138                      [(brind (loadi64 addr:$dst))], IIC_JMP_MEM>,
139                    Requires<[In64BitMode]>, Sched<[WriteJumpLd]>;
140
141   def FARJMP16i  : Iseg16<0xEA, RawFrmImm16, (outs),
142                           (ins i16imm:$off, i16imm:$seg),
143                           "ljmp{w}\t{$seg, $off|$off, $seg}", [],
144                           IIC_JMP_FAR_PTR>, OpSize, Sched<[WriteJump]>;
145   def FARJMP32i  : Iseg32<0xEA, RawFrmImm16, (outs),
146                           (ins i32imm:$off, i16imm:$seg),
147                           "ljmp{l}\t{$seg, $off|$off, $seg}", [],
148                           IIC_JMP_FAR_PTR>, OpSize16, Sched<[WriteJump]>;
149   def FARJMP64   : RI<0xFF, MRM5m, (outs), (ins opaque80mem:$dst),
150                       "ljmp{q}\t{*}$dst", [], IIC_JMP_FAR_MEM>,
151                    Sched<[WriteJump]>;
152
153   def FARJMP16m  : I<0xFF, MRM5m, (outs), (ins opaque32mem:$dst),
154                      "ljmp{w}\t{*}$dst", [], IIC_JMP_FAR_MEM>, OpSize,
155                    Sched<[WriteJumpLd]>;
156   def FARJMP32m  : I<0xFF, MRM5m, (outs), (ins opaque48mem:$dst),
157                      "ljmp{l}\t{*}$dst", [], IIC_JMP_FAR_MEM>, OpSize16,
158                    Sched<[WriteJumpLd]>;
159 }
160
161
162 // Loop instructions
163 let SchedRW = [WriteJump] in {
164 def LOOP   : Ii8PCRel<0xE2, RawFrm, (outs), (ins brtarget8:$dst), "loop\t$dst", [], IIC_LOOP>;
165 def LOOPE  : Ii8PCRel<0xE1, RawFrm, (outs), (ins brtarget8:$dst), "loope\t$dst", [], IIC_LOOPE>;
166 def LOOPNE : Ii8PCRel<0xE0, RawFrm, (outs), (ins brtarget8:$dst), "loopne\t$dst", [], IIC_LOOPNE>;
167 }
168
169 //===----------------------------------------------------------------------===//
170 //  Call Instructions...
171 //
172 let isCall = 1 in
173   // All calls clobber the non-callee saved registers. ESP is marked as
174   // a use to prevent stack-pointer assignments that appear immediately
175   // before calls from potentially appearing dead. Uses for argument
176   // registers are added manually.
177   let Uses = [ESP] in {
178     def CALLpcrel32 : Ii32PCRel<0xE8, RawFrm,
179                            (outs), (ins i32imm_pcrel:$dst),
180                            "call{l}\t$dst", [], IIC_CALL_RI>, OpSize16,
181                       Requires<[Not64BitMode]>, Sched<[WriteJump]>;
182     def CALL16r     : I<0xFF, MRM2r, (outs), (ins GR16:$dst),
183                         "call{w}\t{*}$dst", [(X86call GR16:$dst)], IIC_CALL_RI>,
184                       OpSize, Requires<[Not64BitMode]>, Sched<[WriteJump]>;
185     def CALL16m     : I<0xFF, MRM2m, (outs), (ins i16mem:$dst),
186                         "call{w}\t{*}$dst", [(X86call (loadi16 addr:$dst))],
187                         IIC_CALL_MEM>, OpSize,
188                       Requires<[Not64BitMode,FavorMemIndirectCall]>,
189                       Sched<[WriteJumpLd]>;
190     def CALL32r     : I<0xFF, MRM2r, (outs), (ins GR32:$dst),
191                         "call{l}\t{*}$dst", [(X86call GR32:$dst)], IIC_CALL_RI>,
192                       OpSize16, Requires<[Not64BitMode]>, Sched<[WriteJump]>;
193     def CALL32m     : I<0xFF, MRM2m, (outs), (ins i32mem:$dst),
194                         "call{l}\t{*}$dst", [(X86call (loadi32 addr:$dst))],
195                         IIC_CALL_MEM>, OpSize16,
196                       Requires<[Not64BitMode,FavorMemIndirectCall]>,
197                       Sched<[WriteJumpLd]>;
198
199     def FARCALL16i  : Iseg16<0x9A, RawFrmImm16, (outs),
200                              (ins i16imm:$off, i16imm:$seg),
201                              "lcall{w}\t{$seg, $off|$off, $seg}", [],
202                              IIC_CALL_FAR_PTR>, OpSize, Sched<[WriteJump]>;
203     def FARCALL32i  : Iseg32<0x9A, RawFrmImm16, (outs),
204                              (ins i32imm:$off, i16imm:$seg),
205                              "lcall{l}\t{$seg, $off|$off, $seg}", [],
206                              IIC_CALL_FAR_PTR>, OpSize16, Sched<[WriteJump]>;
207
208     def FARCALL16m  : I<0xFF, MRM3m, (outs), (ins opaque32mem:$dst),
209                         "lcall{w}\t{*}$dst", [], IIC_CALL_FAR_MEM>, OpSize,
210                       Sched<[WriteJumpLd]>;
211     def FARCALL32m  : I<0xFF, MRM3m, (outs), (ins opaque48mem:$dst),
212                         "lcall{l}\t{*}$dst", [], IIC_CALL_FAR_MEM>, OpSize16,
213                       Sched<[WriteJumpLd]>;
214
215     // callw for 16 bit code for the assembler.
216     let isAsmParserOnly = 1 in
217       def CALLpcrel16 : Ii16PCRel<0xE8, RawFrm,
218                        (outs), (ins i16imm_pcrel:$dst),
219                        "callw\t$dst", []>, OpSize;
220   }
221
222
223 // Tail call stuff.
224
225 let isCall = 1, isTerminator = 1, isReturn = 1, isBarrier = 1,
226     isCodeGenOnly = 1, SchedRW = [WriteJumpLd] in
227   let Uses = [ESP] in {
228   def TCRETURNdi : PseudoI<(outs),
229                      (ins i32imm_pcrel:$dst, i32imm:$offset), []>;
230   def TCRETURNri : PseudoI<(outs),
231                      (ins ptr_rc_tailcall:$dst, i32imm:$offset), []>;
232   let mayLoad = 1 in
233   def TCRETURNmi : PseudoI<(outs),
234                      (ins i32mem_TC:$dst, i32imm:$offset), []>;
235
236   // FIXME: The should be pseudo instructions that are lowered when going to
237   // mcinst.
238   def TAILJMPd : Ii32PCRel<0xE9, RawFrm, (outs),
239                            (ins i32imm_pcrel:$dst),
240                            "jmp\t$dst  # TAILCALL",
241                            [], IIC_JMP_REL>;
242   def TAILJMPr : I<0xFF, MRM4r, (outs), (ins ptr_rc_tailcall:$dst),
243                    "", [], IIC_JMP_REG>;  // FIXME: Remove encoding when JIT is dead.
244   let mayLoad = 1 in
245   def TAILJMPm : I<0xFF, MRM4m, (outs), (ins i32mem_TC:$dst),
246                    "jmp{l}\t{*}$dst  # TAILCALL", [], IIC_JMP_MEM>;
247 }
248
249
250 //===----------------------------------------------------------------------===//
251 //  Call Instructions...
252 //
253
254 // RSP is marked as a use to prevent stack-pointer assignments that appear
255 // immediately before calls from potentially appearing dead. Uses for argument
256 // registers are added manually.
257 let isCall = 1, Uses = [RSP], SchedRW = [WriteJump] in {
258   // NOTE: this pattern doesn't match "X86call imm", because we do not know
259   // that the offset between an arbitrary immediate and the call will fit in
260   // the 32-bit pcrel field that we have.
261   def CALL64pcrel32 : Ii32PCRel<0xE8, RawFrm,
262                         (outs), (ins i64i32imm_pcrel:$dst),
263                         "call{q}\t$dst", [], IIC_CALL_RI>,
264                       Requires<[In64BitMode]>;
265   def CALL64r       : I<0xFF, MRM2r, (outs), (ins GR64:$dst),
266                         "call{q}\t{*}$dst", [(X86call GR64:$dst)],
267                         IIC_CALL_RI>,
268                       Requires<[In64BitMode]>;
269   def CALL64m       : I<0xFF, MRM2m, (outs), (ins i64mem:$dst),
270                         "call{q}\t{*}$dst", [(X86call (loadi64 addr:$dst))],
271                         IIC_CALL_MEM>,
272                       Requires<[In64BitMode,FavorMemIndirectCall]>;
273
274   def FARCALL64   : RI<0xFF, MRM3m, (outs), (ins opaque80mem:$dst),
275                        "lcall{q}\t{*}$dst", [], IIC_CALL_FAR_MEM>;
276 }
277
278 let isCall = 1, isCodeGenOnly = 1 in
279   // __chkstk(MSVC):     clobber R10, R11 and EFLAGS.
280   // ___chkstk(Mingw64): clobber R10, R11, RAX and EFLAGS, and update RSP.
281   let Defs = [RAX, R10, R11, RSP, EFLAGS],
282       Uses = [RSP] in {
283     def W64ALLOCA : Ii32PCRel<0xE8, RawFrm,
284                       (outs), (ins i64i32imm_pcrel:$dst),
285                       "call{q}\t$dst", [], IIC_CALL_RI>,
286                     Requires<[IsWin64]>, Sched<[WriteJump]>;
287   }
288
289 let isCall = 1, isTerminator = 1, isReturn = 1, isBarrier = 1,
290     isCodeGenOnly = 1, Uses = [RSP], usesCustomInserter = 1,
291     SchedRW = [WriteJump] in {
292   def TCRETURNdi64 : PseudoI<(outs),
293                       (ins i64i32imm_pcrel:$dst, i32imm:$offset),
294                       []>;
295   def TCRETURNri64 : PseudoI<(outs),
296                       (ins ptr_rc_tailcall:$dst, i32imm:$offset), []>;
297   let mayLoad = 1 in
298   def TCRETURNmi64 : PseudoI<(outs),
299                        (ins i64mem_TC:$dst, i32imm:$offset), []>;
300
301   def TAILJMPd64 : Ii32PCRel<0xE9, RawFrm, (outs),
302                                       (ins i64i32imm_pcrel:$dst),
303                    "jmp\t$dst  # TAILCALL", [], IIC_JMP_REL>;
304   def TAILJMPr64 : I<0xFF, MRM4r, (outs), (ins ptr_rc_tailcall:$dst),
305                      "jmp{q}\t{*}$dst  # TAILCALL", [], IIC_JMP_MEM>;
306
307   let mayLoad = 1 in
308   def TAILJMPm64 : I<0xFF, MRM4m, (outs), (ins i64mem_TC:$dst),
309                      "jmp{q}\t{*}$dst  # TAILCALL", [], IIC_JMP_MEM>;
310 }