Define classes for unary and binary FP instructions and use them to define
[oota-llvm.git] / lib / Target / Mips / MipsInstrFPU.td
1 //===- MipsInstrFPU.td - Mips FPU Instruction Information --*- 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 Mips FPU instruction set.
11 //
12 //===----------------------------------------------------------------------===//
13
14 //===----------------------------------------------------------------------===//
15 // Floating Point Instructions
16 // ------------------------
17 // * 64bit fp:
18 //    - 32 64-bit registers (default mode)
19 //    - 16 even 32-bit registers (32-bit compatible mode) for
20 //      single and double access.
21 // * 32bit fp:
22 //    - 16 even 32-bit registers - single and double (aliased)
23 //    - 32 32-bit registers (within single-only mode)
24 //===----------------------------------------------------------------------===//
25
26 // Floating Point Compare and Branch
27 def SDT_MipsFPBrcond : SDTypeProfile<0, 2, [SDTCisInt<0>,
28                                             SDTCisVT<1, OtherVT>]>;
29 def SDT_MipsFPCmp : SDTypeProfile<0, 3, [SDTCisSameAs<0, 1>, SDTCisFP<1>,
30                                          SDTCisVT<2, i32>]>;
31 def SDT_MipsCMovFP : SDTypeProfile<1, 2, [SDTCisSameAs<0, 1>,
32                                           SDTCisSameAs<1, 2>]>;
33 def SDT_MipsBuildPairF64 : SDTypeProfile<1, 2, [SDTCisVT<0, f64>,
34                                                 SDTCisVT<1, i32>,
35                                                 SDTCisSameAs<1, 2>]>;
36 def SDT_MipsExtractElementF64 : SDTypeProfile<1, 2, [SDTCisVT<0, i32>,
37                                                      SDTCisVT<1, f64>,
38                                                      SDTCisVT<2, i32>]>;
39
40 def MipsFPCmp : SDNode<"MipsISD::FPCmp", SDT_MipsFPCmp, [SDNPOutGlue]>;
41 def MipsCMovFP_T : SDNode<"MipsISD::CMovFP_T", SDT_MipsCMovFP, [SDNPInGlue]>;
42 def MipsCMovFP_F : SDNode<"MipsISD::CMovFP_F", SDT_MipsCMovFP, [SDNPInGlue]>;
43 def MipsFPBrcond : SDNode<"MipsISD::FPBrcond", SDT_MipsFPBrcond,
44                           [SDNPHasChain, SDNPOptInGlue]>;
45 def MipsBuildPairF64 : SDNode<"MipsISD::BuildPairF64", SDT_MipsBuildPairF64>;
46 def MipsExtractElementF64 : SDNode<"MipsISD::ExtractElementF64",
47                                    SDT_MipsExtractElementF64>;
48
49 // Operand for printing out a condition code.
50 let PrintMethod = "printFCCOperand" in
51   def condcode : Operand<i32>;
52
53 //===----------------------------------------------------------------------===//
54 // Feature predicates.
55 //===----------------------------------------------------------------------===//
56
57 def IsFP64bit        : Predicate<"Subtarget.isFP64bit()">;
58 def NotFP64bit       : Predicate<"!Subtarget.isFP64bit()">;
59 def IsSingleFloat    : Predicate<"Subtarget.isSingleFloat()">;
60 def IsNotSingleFloat : Predicate<"!Subtarget.isSingleFloat()">;
61
62 //===----------------------------------------------------------------------===//
63 // Instruction Class Templates
64 //
65 // A set of multiclasses is used to address the register usage.
66 //
67 // S   - single precision in 16 32bit even fp registers
68 //       single precision in 32 32bit fp registers in SingleOnly mode
69 // D32 - double precision in 16 32bit even fp registers
70 // D64 - double precision in 32 64bit fp registers (In64BitMode)
71 //
72 // Only S and D32 are supported right now.
73 //===----------------------------------------------------------------------===//
74
75 // Unary instruction without pattern.
76 class FFR1<bits<6> funct, bits<5> fmt, RegisterClass RCDst,
77            RegisterClass RCSrc, string asmstr>:
78   FFR<0x11, funct, fmt, (outs RCDst:$fd), (ins RCSrc:$fs),
79       !strconcat(asmstr, "\t$fd, $fs"), []> {
80   let ft = 0;
81 }
82
83 // Unary instruction with pattern.
84 // All operands belong to the same register class. 
85 class FFR1P<bits<6> funct, bits<5> fmt, RegisterClass RC, string asmstr,
86             SDNode FOp>:
87   FFR1<funct, fmt, RC, RC, asmstr> {
88   let Pattern = [(set RC:$fd, (FOp RC:$fs))];
89 }
90
91 // Binary instruction with pattern.
92 // All operands belong to the same register class. 
93 class FFR2<bits<6> funct, bits<5> fmt, RegisterClass RC, string asmstr,
94           SDNode FOp, bit isComm = 0>:
95   FFR<0x11, funct, fmt, (outs RC:$fd), (ins RC:$fs, RC:$ft),
96       !strconcat(asmstr, "\t$fd, $fs, $ft"),
97       [(set RC:$fd, (FOp RC:$fs, RC:$ft))]> {
98   let isCommutable = isComm;
99 }
100
101 // Multiclasses.
102 // Unary instruction without pattern.
103 multiclass FFR1_1<bits<6> funct, string asmstr>
104 {
105   def _S   : FFR1<funct, 16, FGR32, FGR32, asmstr>;
106   def _D32 : FFR1<funct, 17, FGR32, AFGR64, asmstr>, Requires<[NotFP64bit]>;
107 }
108
109 // Unary instruction with pattern.
110 // All operands belong to the same register class. 
111 multiclass FFR1_2<bits<6> funct, string asmstr, SDNode FOp>
112 {
113   def _S   : FFR1P<funct, 16, FGR32, asmstr, FOp>;
114   def _D32 : FFR1P<funct, 17, AFGR64, asmstr, FOp>, Requires<[NotFP64bit]>;
115 }
116
117 // Binary instruction with pattern.
118 // All operands belong to the same register class. 
119 multiclass FFR1_4<bits<6> funct, string asmstr, SDNode FOp, bit isComm = 0> {
120   def _S   : FFR2<funct, 16, FGR32, asmstr, FOp, isComm>;
121   def _D32 : FFR2<funct, 17, AFGR64, asmstr, FOp, isComm>,
122              Requires<[NotFP64bit]>;
123 }
124
125 //===----------------------------------------------------------------------===//
126 // Floating Point Instructions
127 //===----------------------------------------------------------------------===//
128
129 let ft = 0 in {
130   defm FLOOR_W : FFR1_1<0b001111, "floor.w">;
131   defm CEIL_W  : FFR1_1<0b001110, "ceil.w">;
132   defm ROUND_W : FFR1_1<0b001100, "round.w">;
133   defm TRUNC_W : FFR1_1<0b001101, "trunc.w">;
134   defm CVTW    : FFR1_1<0b100100, "cvt.w">;
135
136   defm FABS    : FFR1_2<0b000101, "abs",  fabs>;
137   defm FNEG    : FFR1_2<0b000111, "neg",  fneg>;
138   defm FSQRT   : FFR1_2<0b000100, "sqrt", fsqrt>;
139
140   /// Convert to Single Precison
141   def CVTS_W32 : FFR1<0b100000, 0x2, FGR32,  FGR32,  "cvt.s.w">;
142
143   let Predicates = [IsNotSingleFloat] in {
144     /// Ceil to long signed integer
145     def CEIL_LS   : FFR1<0b001010, 0x0, FGR32, FGR32, "ceil.l">;
146     def CEIL_LD   : FFR1<0b001010, 0x1, AFGR64, AFGR64, "ceil.l">;
147
148     /// Round to long signed integer
149     def ROUND_LS  : FFR1<0b001000, 0x0, FGR32, FGR32, "round.l">;
150     def ROUND_LD  : FFR1<0b001000, 0x1, AFGR64, AFGR64, "round.l">;
151
152     /// Floor to long signed integer
153     def FLOOR_LS  : FFR1<0b001011, 0x0, FGR32, FGR32, "floor.l">;
154     def FLOOR_LD  : FFR1<0b001011, 0x1, AFGR64, AFGR64, "floor.l">;
155
156     /// Trunc to long signed integer
157     def TRUNC_LS  : FFR1<0b001001, 0x0, FGR32, FGR32, "trunc.l">;
158     def TRUNC_LD  : FFR1<0b001001, 0x1, AFGR64, AFGR64, "trunc.l">;
159
160     /// Convert to long signed integer
161     def CVTL_S    : FFR1<0b100101, 0x0, FGR32, FGR32, "cvt.l">;
162     def CVTL_D    : FFR1<0b100101, 0x1, AFGR64, AFGR64, "cvt.l">;
163
164     /// Convert to Double Precison
165     def CVTD_S : FFR1<0b100001, 0x0, AFGR64, FGR32, "cvt.d.s">;
166     def CVTD_W32 : FFR1<0b100001, 0x2, AFGR64, FGR32, "cvt.d.w">;
167     def CVTD_L32 : FFR1<0b100001, 0x3, AFGR64, AFGR64, "cvt.d.l">;
168
169     /// Convert to Single Precison
170     def CVTS_D32 : FFR1<0b100000, 0x1, FGR32, AFGR64, "cvt.s.d">;
171     def CVTS_L32 : FFR1<0b100000, 0x3, FGR32, AFGR64, "cvt.s.l">;
172   }
173 }
174
175 // The odd-numbered registers are only referenced when doing loads,
176 // stores, and moves between floating-point and integer registers.
177 // When defining instructions, we reference all 32-bit registers,
178 // regardless of register aliasing.
179 let fd = 0 in {
180   /// Move Control Registers From/To CPU Registers
181   def CFC1  : FFR<0x11, 0x0, 0x2, (outs CPURegs:$rt), (ins CCR:$fs),
182                   "cfc1\t$rt, $fs", []>;
183
184   def CTC1  : FFR<0x11, 0x0, 0x6, (outs CCR:$rt), (ins CPURegs:$fs),
185                   "ctc1\t$fs, $rt", []>;
186
187   def MFC1  : FFR<0x11, 0x00, 0x00, (outs CPURegs:$rt), (ins FGR32:$fs),
188                   "mfc1\t$rt, $fs",
189                   [(set CPURegs:$rt, (bitconvert FGR32:$fs))]>;
190
191   def MTC1  : FFR<0x11, 0x00, 0x04, (outs FGR32:$fs), (ins CPURegs:$rt),
192                   "mtc1\t$rt, $fs",
193                   [(set FGR32:$fs, (bitconvert CPURegs:$rt))]>;
194 }
195
196 def FMOV_S : FFR<0x11, 0b000110, 0x0, (outs FGR32:$fd), (ins FGR32:$fs),
197                    "mov.s\t$fd, $fs", []>;
198 def FMOV_D32 : FFR<0x11, 0b000110, 0x1, (outs AFGR64:$fd), (ins AFGR64:$fs),
199                    "mov.d\t$fd, $fs", []>;
200
201 /// Floating Point Memory Instructions
202 let Predicates = [IsNotSingleFloat] in {
203   def LDC1 : FFI<0b110101, (outs AFGR64:$ft), (ins mem:$addr),
204                  "ldc1\t$ft, $addr", [(set AFGR64:$ft, (load addr:$addr))]>;
205
206   def SDC1 : FFI<0b111101, (outs), (ins AFGR64:$ft, mem:$addr),
207                  "sdc1\t$ft, $addr", [(store AFGR64:$ft, addr:$addr)]>;
208 }
209
210 // LWC1 and SWC1 can always be emitted with odd registers.
211 def LWC1  : FFI<0b110001, (outs FGR32:$ft), (ins mem:$addr), "lwc1\t$ft, $addr",
212                [(set FGR32:$ft, (load addr:$addr))]>;
213 def SWC1  : FFI<0b111001, (outs), (ins FGR32:$ft, mem:$addr),
214                "swc1\t$ft, $addr", [(store FGR32:$ft, addr:$addr)]>;
215
216 /// Floating-point Aritmetic
217 defm FADD : FFR1_4<0x10, "add", fadd, 1>;
218 defm FDIV : FFR1_4<0x03, "div", fdiv>;
219 defm FMUL : FFR1_4<0x02, "mul", fmul, 1>;
220 defm FSUB : FFR1_4<0x01, "sub", fsub>;
221
222 //===----------------------------------------------------------------------===//
223 // Floating Point Branch Codes
224 //===----------------------------------------------------------------------===//
225 // Mips branch codes. These correspond to condcode in MipsInstrInfo.h.
226 // They must be kept in synch.
227 def MIPS_BRANCH_F  : PatLeaf<(i32 0)>;
228 def MIPS_BRANCH_T  : PatLeaf<(i32 1)>;
229
230 /// Floating Point Branch of False/True (Likely)
231 let isBranch=1, isTerminator=1, hasDelaySlot=1, base=0x8, Uses=[FCR31] in
232   class FBRANCH<PatLeaf op, string asmstr> : FFI<0x11, (outs),
233         (ins brtarget:$dst), !strconcat(asmstr, "\t$dst"),
234         [(MipsFPBrcond op, bb:$dst)]>;
235
236 def BC1F  : FBRANCH<MIPS_BRANCH_F,  "bc1f">;
237 def BC1T  : FBRANCH<MIPS_BRANCH_T,  "bc1t">;
238
239 //===----------------------------------------------------------------------===//
240 // Floating Point Flag Conditions
241 //===----------------------------------------------------------------------===//
242 // Mips condition codes. They must correspond to condcode in MipsInstrInfo.h.
243 // They must be kept in synch.
244 def MIPS_FCOND_F    : PatLeaf<(i32 0)>;
245 def MIPS_FCOND_UN   : PatLeaf<(i32 1)>;
246 def MIPS_FCOND_OEQ  : PatLeaf<(i32 2)>;
247 def MIPS_FCOND_UEQ  : PatLeaf<(i32 3)>;
248 def MIPS_FCOND_OLT  : PatLeaf<(i32 4)>;
249 def MIPS_FCOND_ULT  : PatLeaf<(i32 5)>;
250 def MIPS_FCOND_OLE  : PatLeaf<(i32 6)>;
251 def MIPS_FCOND_ULE  : PatLeaf<(i32 7)>;
252 def MIPS_FCOND_SF   : PatLeaf<(i32 8)>;
253 def MIPS_FCOND_NGLE : PatLeaf<(i32 9)>;
254 def MIPS_FCOND_SEQ  : PatLeaf<(i32 10)>;
255 def MIPS_FCOND_NGL  : PatLeaf<(i32 11)>;
256 def MIPS_FCOND_LT   : PatLeaf<(i32 12)>;
257 def MIPS_FCOND_NGE  : PatLeaf<(i32 13)>;
258 def MIPS_FCOND_LE   : PatLeaf<(i32 14)>;
259 def MIPS_FCOND_NGT  : PatLeaf<(i32 15)>;
260
261 /// Floating Point Compare
262 let Defs=[FCR31] in {
263   def FCMP_S : FCC<0x0, (outs), (ins FGR32:$fs, FGR32:$ft, condcode:$cc),
264                      "c.$cc.s\t$fs, $ft",
265                      [(MipsFPCmp FGR32:$fs, FGR32:$ft, imm:$cc)]>;
266
267   def FCMP_D32 : FCC<0x1, (outs), (ins AFGR64:$fs, AFGR64:$ft, condcode:$cc),
268                      "c.$cc.d\t$fs, $ft",
269                      [(MipsFPCmp AFGR64:$fs, AFGR64:$ft, imm:$cc)]>,
270                      Requires<[NotFP64bit]>;
271 }
272
273
274 // Conditional moves:
275 // These instructions are expanded in
276 // MipsISelLowering::EmitInstrWithCustomInserter if target does not have
277 // conditional move instructions.
278 // flag:int, data:float
279 let usesCustomInserter = 1, Constraints = "$F = $dst" in
280 class CondMovIntFP<RegisterClass RC, bits<5> fmt, bits<6> func,
281                    string instr_asm> :
282   FFR<0x11, func, fmt, (outs RC:$dst), (ins RC:$T, CPURegs:$cond, RC:$F),
283       !strconcat(instr_asm, "\t$dst, $T, $cond"), []>;
284
285 def MOVZ_S : CondMovIntFP<FGR32, 16, 18, "movz.s">;
286 def MOVN_S : CondMovIntFP<FGR32, 16, 19, "movn.s">;
287
288 let Predicates = [NotFP64bit] in {
289   def MOVZ_D : CondMovIntFP<AFGR64, 17, 18, "movz.d">;
290   def MOVN_D : CondMovIntFP<AFGR64, 17, 19, "movn.d">;
291 }
292
293 defm : MovzPats<FGR32, MOVZ_S>;
294 defm : MovnPats<FGR32, MOVN_S>;
295
296 let Predicates = [NotFP64bit] in {
297   defm : MovzPats<AFGR64, MOVZ_D>;
298   defm : MovnPats<AFGR64, MOVN_D>;
299 }
300
301 let usesCustomInserter = 1, Uses = [FCR31], Constraints = "$F = $dst" in {
302 // flag:float, data:int
303 class CondMovFPInt<SDNode cmov, bits<1> tf, string instr_asm> :
304   FCMOV<tf, (outs CPURegs:$dst), (ins CPURegs:$T, CPURegs:$F),
305         !strconcat(instr_asm, "\t$dst, $T, $$fcc0"),
306         [(set CPURegs:$dst, (cmov CPURegs:$T, CPURegs:$F))]>;
307
308 // flag:float, data:float
309 class CondMovFPFP<RegisterClass RC, SDNode cmov, bits<5> fmt, bits<1> tf,
310                   string instr_asm> :
311   FFCMOV<fmt, tf, (outs RC:$dst), (ins RC:$T, RC:$F),
312          !strconcat(instr_asm, "\t$dst, $T, $$fcc0"),
313          [(set RC:$dst, (cmov RC:$T, RC:$F))]>;
314 }
315
316 def MOVT : CondMovFPInt<MipsCMovFP_T, 1, "movt">;
317 def MOVF : CondMovFPInt<MipsCMovFP_F, 0, "movf">;
318 def MOVT_S : CondMovFPFP<FGR32, MipsCMovFP_T, 16, 1, "movt.s">;
319 def MOVF_S : CondMovFPFP<FGR32, MipsCMovFP_F, 16, 0, "movf.s">;
320
321 let Predicates = [NotFP64bit] in {
322   def MOVT_D : CondMovFPFP<AFGR64, MipsCMovFP_T, 17, 1, "movt.d">;
323   def MOVF_D : CondMovFPFP<AFGR64, MipsCMovFP_F, 17, 0, "movf.d">;
324 }
325
326 //===----------------------------------------------------------------------===//
327 // Floating Point Pseudo-Instructions
328 //===----------------------------------------------------------------------===//
329 def MOVCCRToCCR : MipsPseudo<(outs CCR:$dst), (ins CCR:$src),
330                              "# MOVCCRToCCR", []>;
331
332 // This pseudo instr gets expanded into 2 mtc1 instrs after register
333 // allocation.
334 def BuildPairF64 :
335   MipsPseudo<(outs AFGR64:$dst),
336              (ins CPURegs:$lo, CPURegs:$hi), "",
337              [(set AFGR64:$dst, (MipsBuildPairF64 CPURegs:$lo, CPURegs:$hi))]>;
338
339 // This pseudo instr gets expanded into 2 mfc1 instrs after register
340 // allocation.
341 // if n is 0, lower part of src is extracted.
342 // if n is 1, higher part of src is extracted.
343 def ExtractElementF64 :
344   MipsPseudo<(outs CPURegs:$dst),
345              (ins AFGR64:$src, i32imm:$n), "",
346              [(set CPURegs:$dst,
347                (MipsExtractElementF64 AFGR64:$src, imm:$n))]>;
348
349 //===----------------------------------------------------------------------===//
350 // Floating Point Patterns
351 //===----------------------------------------------------------------------===//
352 def fpimm0 : PatLeaf<(fpimm), [{
353   return N->isExactlyValue(+0.0);
354 }]>;
355
356 def fpimm0neg : PatLeaf<(fpimm), [{
357   return N->isExactlyValue(-0.0);
358 }]>;
359
360 def : Pat<(f32 fpimm0), (MTC1 ZERO)>;
361 def : Pat<(f32 fpimm0neg), (FNEG_S (MTC1 ZERO))>;
362
363 def : Pat<(f32 (sint_to_fp CPURegs:$src)), (CVTS_W32 (MTC1 CPURegs:$src))>;
364 def : Pat<(f64 (sint_to_fp CPURegs:$src)), (CVTD_W32 (MTC1 CPURegs:$src))>;
365
366 def : Pat<(i32 (fp_to_sint FGR32:$src)), (MFC1 (TRUNC_W_S FGR32:$src))>;
367 def : Pat<(i32 (fp_to_sint AFGR64:$src)), (MFC1 (TRUNC_W_D32 AFGR64:$src))>;
368
369 let Predicates = [NotFP64bit] in {
370   def : Pat<(f32 (fround AFGR64:$src)), (CVTS_D32 AFGR64:$src)>;
371   def : Pat<(f64 (fextend FGR32:$src)), (CVTD_S FGR32:$src)>;
372 }
373