Several changes to Mips backend, experimental fp support being the most
[oota-llvm.git] / lib / Target / Mips / MipsInstrFPU.td
1 //===- MipsInstrFPU.td - Mips FPU Instruction Information -------*- C++ -*-===//
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 contains the Mips implementation of the TargetInstrInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 //===----------------------------------------------------------------------===//
15 // Float 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 // Float Point Compare and Branch
27 def SDT_MipsFPBrcond : SDTypeProfile<0, 3, [SDTCisSameAs<0, 2>, SDTCisInt<0>,
28                                      SDTCisVT<1, OtherVT>]>;
29 def SDT_MipsFPCmp : SDTypeProfile<0, 3, [SDTCisSameAs<0, 1>, SDTCisFP<0>, 
30                                   SDTCisInt<2>]>;
31 def MipsFPBrcond : SDNode<"MipsISD::FPBrcond", SDT_MipsFPBrcond, 
32                           [SDNPHasChain]>; 
33 def MipsFPCmp    : SDNode<"MipsISD::FPCmp", SDT_MipsFPCmp>;
34
35 // Operand for printing out a condition code.
36 let PrintMethod = "printFCCOperand" in
37   def condcode : Operand<i32>;
38
39 //===----------------------------------------------------------------------===//
40 // Feature predicates.
41 //===----------------------------------------------------------------------===//
42
43 def In32BitMode      : Predicate<"!Subtarget.isFP64bit()">;
44 def In64BitMode      : Predicate<"Subtarget.isFP64bit()">;
45 def IsSingleFloat    : Predicate<"Subtarget.isSingleFloat()">;
46 def IsNotSingleFloat : Predicate<"!Subtarget.isSingleFloat()">;
47
48 //===----------------------------------------------------------------------===//
49 // Instruction Class Templates
50 //
51 // A set of multiclasses is used to address this in one shot. 
52 // SO32 - single precision only, uses all 32 32-bit fp registers
53 //        require FGR32 Register Class and IsSingleFloat
54 // AS32 - 16 even fp registers are used for single precision
55 //        require AFGR32 Register Class and In32BitMode
56 // S64  - 32 64 bit registers are used to hold 32-bit single precision values.
57 //        require FGR64 Register Class and In64BitMode
58 // D32  - 16 even fp registers are used for double precision
59 //        require AFGR64 Register Class and In32BitMode
60 // D64  - 32 64 bit registers are used to hold 64-bit double precision values.
61 //        require FGR64 Register Class and In64BitMode
62 //
63 // Only SO32, AS32 and D32 are supported right now.
64 //
65 //===----------------------------------------------------------------------===//
66
67 multiclass FFR1_1<bits<6> funct, string asmstr> 
68 {
69   def _SO32 : FFR<0x11, funct, 0x0, (outs FGR32:$fd), (ins FGR32:$fs),
70       !strconcat(asmstr, ".s $fd, $fs"), []>, Requires<[IsSingleFloat]>;
71
72   def _AS32 : FFR<0x11, funct, 0x0, (outs AFGR32:$fd), (ins AFGR32:$fs),
73       !strconcat(asmstr, ".s $fd, $fs"), []>, Requires<[In32BitMode]>;
74
75   def _D32  : FFR<0x11, funct, 0x1, (outs AFGR64:$fd), (ins AFGR64:$fs),
76       !strconcat(asmstr, ".d $fd, $fs"), []>, Requires<[In32BitMode]>;
77 }
78
79 multiclass FFR1_2<bits<6> funct, string asmstr, SDNode FOp> 
80 {
81   def _SO32 : FFR<0x11, funct, 0x0, (outs FGR32:$fd), (ins FGR32:$fs),
82                  !strconcat(asmstr, ".s $fd, $fs"), 
83                  [(set FGR32:$fd, (FOp FGR32:$fs))]>, Requires<[IsSingleFloat]>;
84
85   def _AS32 : FFR<0x11, funct, 0x0, (outs AFGR32:$fd), (ins AFGR32:$fs),
86                  !strconcat(asmstr, ".s $fd, $fs"), 
87                  [(set AFGR32:$fd, (FOp AFGR32:$fs))]>, Requires<[In32BitMode]>;
88
89   def _D32  : FFR<0x11, funct, 0x1, (outs AFGR64:$fd), (ins AFGR64:$fs),
90                  !strconcat(asmstr, ".d $fd, $fs"), 
91                  [(set AFGR64:$fd, (FOp AFGR64:$fs))]>, Requires<[In32BitMode]>;
92 }
93
94 class FFR1_3<bits<6> funct, bits<5> fmt, RegisterClass RcSrc, 
95               RegisterClass RcDst, string asmstr>: 
96   FFR<0x11, funct, fmt, (outs RcSrc:$fd), (ins RcDst:$fs), 
97       !strconcat(asmstr, " $fd, $fs"), []>; 
98
99
100 multiclass FFR1_4<bits<6> funct, string asmstr, SDNode FOp> {
101   def _SO32 : FFR<0x11, funct, 0x0, (outs FGR32:$fd), (ins FGR32:$fs, FGR32:$ft), 
102                  !strconcat(asmstr, ".s $fd, $fs, $ft"),
103                  [(set FGR32:$fd, (FOp FGR32:$fs, FGR32:$ft))]>,
104                  Requires<[IsSingleFloat]>;
105
106   def _AS32 : FFR<0x11, funct, 0x0, (outs AFGR32:$fd), 
107                  (ins AFGR32:$fs, AFGR32:$ft), 
108                  !strconcat(asmstr, ".s $fd, $fs, $ft"),
109                  [(set AFGR32:$fd, (FOp AFGR32:$fs, AFGR32:$ft))]>,
110                  Requires<[In32BitMode]>;
111
112   def _D32  : FFR<0x11, funct, 0x1, (outs AFGR64:$fd), 
113                  (ins AFGR64:$fs, AFGR64:$ft), 
114                  !strconcat(asmstr, ".d $fd, $fs, $ft"),
115                  [(set AFGR64:$fd, (FOp AFGR64:$fs, AFGR64:$ft))]>,
116                  Requires<[In32BitMode]>;
117 }
118
119 //===----------------------------------------------------------------------===//
120 // Float Point Instructions
121 //===----------------------------------------------------------------------===//
122
123 let ft = 0 in {
124   defm FLOOR_W : FFR1_1<0b001111, "floor.w">;
125   defm CEIL_W  : FFR1_1<0b001110, "ceil.w">;
126   defm ROUND_W : FFR1_1<0b001100, "round.w">;
127   defm TRUNC_W : FFR1_1<0b001101, "trunc.w">;
128   defm CVTW    : FFR1_1<0b100100, "cvt.w">;
129   defm FMOV    : FFR1_1<0b000110, "mov">;
130
131   defm FABS    : FFR1_2<0b000101, "abs",  fabs>; 
132   defm FNEG    : FFR1_2<0b000111, "neg",  fneg>; 
133   defm FSQRT   : FFR1_2<0b000100, "sqrt", fsqrt>;
134
135   let Predicates = [IsNotSingleFloat] in {
136     /// Ceil to long signed integer
137     def CEIL_LS   : FFR1_3<0b001010, 0x0, AFGR32, AFGR32, "ceil.l">;
138     def CEIL_LD   : FFR1_3<0b001010, 0x1, AFGR64, AFGR64, "ceil.l">;
139
140     /// Round to long signed integer
141     def ROUND_LS  : FFR1_3<0b001000, 0x0, AFGR32, AFGR32, "round.l">;
142     def ROUND_LD  : FFR1_3<0b001000, 0x1, AFGR64, AFGR64, "round.l">;
143
144     /// Floor to long signed integer
145     def FLOOR_LS  : FFR1_3<0b001011, 0x0, AFGR32, AFGR32, "floor.l">;
146     def FLOOR_LD  : FFR1_3<0b001011, 0x1, AFGR64, AFGR64, "floor.l">;
147
148     /// Trunc to long signed integer
149     def TRUNC_LS  : FFR1_3<0b001001, 0x0, AFGR32, AFGR32, "trunc.l">;
150     def TRUNC_LD  : FFR1_3<0b001001, 0x1, AFGR64, AFGR64, "trunc.l">;
151
152     /// Convert to long signed integer
153     def CVTL_S    : FFR1_3<0b100101, 0x0, AFGR32, AFGR32, "cvt.l">; 
154     def CVTL_D    : FFR1_3<0b100101, 0x1, AFGR64, AFGR64, "cvt.l">; 
155
156     /// Convert to Double Precison 
157     def CVTD_S32 : FFR1_3<0b100001, 0x0, AFGR64, FGR32, "cvt.d.s">; 
158     def CVTD_W32 : FFR1_3<0b100001, 0x2, AFGR64, FGR32, "cvt.d.w">; 
159     def CVTD_L32 : FFR1_3<0b100001, 0x3, AFGR64, AFGR64, "cvt.d.l">; 
160                    
161     /// Convert to Single Precison
162     def CVTS_D32 : FFR1_3<0b100000, 0x1, FGR32, AFGR64, "cvt.s.d">;
163     def CVTS_L32 : FFR1_3<0b100000, 0x3, FGR32, AFGR64, "cvt.s.l">; 
164   }
165
166   /// Convert to Single Precison
167   def CVTS_W32 : FFR1_3<0b100000, 0x2, FGR32,  FGR32,  "cvt.s.w">, 
168                  Requires<[IsSingleFloat]>;
169 }
170
171 // The odd-numbered registers are only referenced when doing loads,
172 // stores, and moves between floating-point and integer registers.
173 // When defining instructions, we reference all 32-bit registers, 
174 // regardless of register aliasing.
175 let fd = 0 in {
176   /// Move Control Registers From/To CPU Registers
177   ///def CFC1  : FFR<0x11, 0x0, 0x2, (outs CPURegs:$rt), (ins FGR32:$fs),
178   ///                "cfc1 $rt, $fs", []>;
179
180   ///def CTC1  : FFR<0x11, 0x0, 0x6, (outs CPURegs:$rt), (ins FGR32:$fs),
181   ///                "ctc1 $rt, $fs", []>;
182   ///                
183   ///def CFC1A : FFR<0x11, 0x0, 0x2, (outs CPURegs:$rt), (ins AFGR32:$fs),
184   ///                "cfc1 $rt, $fs", []>;
185
186   ///def CTC1A : FFR<0x11, 0x0, 0x6, (outs CPURegs:$rt), (ins AFGR32:$fs),
187   ///                "ctc1 $rt, $fs", []>;
188
189   def MFC1  : FFR<0x11, 0x00, 0x00, (outs CPURegs:$rt), (ins FGR32:$fs),
190                   "mfc1 $rt, $fs", []>;
191
192   def MTC1  : FFR<0x11, 0x00, 0x04, (outs FGR32:$fs), (ins CPURegs:$rt),
193                   "mtc1 $fs, $rt", []>;
194
195   def MFC1A : FFR<0x11, 0x00, 0x00, (outs CPURegs:$rt), (ins AFGR32:$fs),
196                   "mfc1 $rt, $fs", []>;
197
198   def MTC1A : FFR<0x11, 0x00, 0x04, (outs AFGR32:$fs), (ins CPURegs:$rt),
199                   "mtc1 $fs, $rt", []>;
200 }
201
202 /// Float Point Memory Instructions
203 let Predicates = [IsNotSingleFloat] in {
204   def LDC1 : FFI<0b110101, (outs AFGR64:$ft), (ins mem:$addr), 
205                  "ldc1 $ft, $addr", [(set AFGR64:$ft, (load addr:$addr))]>;
206
207   def SDC1 : FFI<0b111101, (outs), (ins AFGR64:$ft, mem:$addr), 
208                  "sdc1 $ft, $addr", [(store AFGR64:$ft, addr:$addr)]>;
209 }
210
211 // LWC1 and SWC1 can always be emited with odd registers.
212 def LWC1  : FFI<0b110001, (outs FGR32:$ft), (ins mem:$addr), "lwc1 $ft, $addr",
213                [(set FGR32:$ft, (load addr:$addr))]>; 
214 def SWC1  : FFI<0b111001, (outs), (ins FGR32:$ft, mem:$addr), "swc1 $ft, $addr",
215                [(store FGR32:$ft, addr:$addr)]>; 
216
217 def LWC1A : FFI<0b110001, (outs AFGR32:$ft), (ins mem:$addr), "lwc1 $ft, $addr",
218                [(set AFGR32:$ft, (load addr:$addr))]>; 
219 def SWC1A : FFI<0b111001, (outs), (ins AFGR32:$ft, mem:$addr), "swc1 $ft, $addr",
220                [(store AFGR32:$ft, addr:$addr)]>; 
221
222 /// Floating-point Aritmetic
223 defm FADD : FFR1_4<0x10, "add", fadd>;
224 defm FDIV : FFR1_4<0x03, "div", fdiv>;
225 defm FMUL : FFR1_4<0x02, "mul", fmul>;
226 defm FSUB : FFR1_4<0x01, "sub", fsub>;
227
228 //===----------------------------------------------------------------------===//
229 // Float Point Branch Codes
230 //===----------------------------------------------------------------------===//
231 // Mips branch codes. These correspond to condcode in MipsInstrInfo.h. 
232 // They must be kept in synch.
233 def MIPS_BRANCH_F  : PatLeaf<(i32 0)>;
234 def MIPS_BRANCH_T  : PatLeaf<(i32 1)>;
235 def MIPS_BRANCH_FL : PatLeaf<(i32 2)>;
236 def MIPS_BRANCH_TL : PatLeaf<(i32 3)>;
237
238 /// Float Point Branch of False/True (Likely)
239 let isBranch=1, isTerminator=1, hasDelaySlot=1, base=0x8, Uses=[FCR31] in {
240   class FBRANCH<PatLeaf op, string asmstr> : FFI<0x11, (ops), 
241         (ins brtarget:$dst), !strconcat(asmstr, " $dst"),
242         [(MipsFPBrcond op, bb:$dst, FCR31)]>;
243 }
244 def BC1F  : FBRANCH<MIPS_BRANCH_F, "bc1f">;
245 def BC1T  : FBRANCH<MIPS_BRANCH_T, "bc1t">;
246 def BC1FL : FBRANCH<MIPS_BRANCH_FL, "bc1fl">;
247 def BC1TL : FBRANCH<MIPS_BRANCH_TL, "bc1tl">;
248
249 //===----------------------------------------------------------------------===//
250 // Float Point Flag Conditions
251 //===----------------------------------------------------------------------===//
252 // Mips condition codes. They must correspond to condcode in MipsInstrInfo.h. 
253 // They must be kept in synch.
254 def MIPS_FCOND_F    : PatLeaf<(i32 0)>;
255 def MIPS_FCOND_UN   : PatLeaf<(i32 1)>;
256 def MIPS_FCOND_EQ   : PatLeaf<(i32 2)>;
257 def MIPS_FCOND_UEQ  : PatLeaf<(i32 3)>;
258 def MIPS_FCOND_OLT  : PatLeaf<(i32 4)>;
259 def MIPS_FCOND_ULT  : PatLeaf<(i32 5)>;
260 def MIPS_FCOND_OLE  : PatLeaf<(i32 6)>;
261 def MIPS_FCOND_ULE  : PatLeaf<(i32 7)>;
262 def MIPS_FCOND_SF   : PatLeaf<(i32 8)>;
263 def MIPS_FCOND_NGLE : PatLeaf<(i32 9)>;
264 def MIPS_FCOND_SEQ  : PatLeaf<(i32 10)>;
265 def MIPS_FCOND_NGL  : PatLeaf<(i32 11)>;
266 def MIPS_FCOND_LT   : PatLeaf<(i32 12)>;
267 def MIPS_FCOND_NGE  : PatLeaf<(i32 13)>;
268 def MIPS_FCOND_LE   : PatLeaf<(i32 14)>;
269 def MIPS_FCOND_NGT  : PatLeaf<(i32 15)>;
270
271 /// Floating Point Compare
272 let hasDelaySlot = 1, Defs=[FCR31] in {
273
274 //multiclass FCC1_1<RegisterClass RC>
275
276   def FCMP_SO32 : FCC<0x0, (outs), (ins FGR32:$fs, FGR32:$ft, condcode:$cc),
277       "c.$cc.s $fs $ft", [(MipsFPCmp FGR32:$fs, FGR32:$ft, imm:$cc), 
278       (implicit FCR31)]>, Requires<[IsSingleFloat]>;
279   
280   def FCMP_AS32 : FCC<0x0, (outs), (ins AFGR32:$fs, AFGR32:$ft, condcode:$cc),
281       "c.$cc.s $fs $ft", [(MipsFPCmp AFGR32:$fs, AFGR32:$ft, imm:$cc), 
282       (implicit FCR31)]>, Requires<[In32BitMode]>;
283
284   def FCMP_D32 : FCC<0x1, (outs), (ins AFGR64:$fs, AFGR64:$ft, condcode:$cc),
285       "c.$cc.d $fs $ft", [(MipsFPCmp AFGR64:$fs, AFGR64:$ft, imm:$cc),
286       (implicit FCR31)]>, Requires<[In32BitMode]>;
287 }
288
289 //===----------------------------------------------------------------------===//
290 // Float Point Patterns
291 //===----------------------------------------------------------------------===//
292 def : Pat<(f32 (sint_to_fp CPURegs:$src)), (CVTS_W32 (MTC1 CPURegs:$src))>;
293 def : Pat<(f64 (sint_to_fp CPURegs:$src)), (CVTD_W32 (MTC1 CPURegs:$src))>;
294 def : Pat<(i32 (fp_to_sint FGR32:$src)), (MFC1 (CVTW_SO32 FGR32:$src))>;
295 def : Pat<(i32 (fp_to_sint AFGR32:$src)), (MFC1 (CVTW_AS32 AFGR32:$src))>;
296