R600: Handle fcopysign
[oota-llvm.git] / lib / Target / R600 / AMDGPUInstructions.td
1 //===-- AMDGPUInstructions.td - Common instruction defs ---*- 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 contains instruction defs that are common to all hw codegen
11 // targets.
12 //
13 //===----------------------------------------------------------------------===//
14
15 class AMDGPUInst <dag outs, dag ins, string asm, list<dag> pattern> : Instruction {
16   field bit isRegisterLoad = 0;
17   field bit isRegisterStore = 0;
18
19   let Namespace = "AMDGPU";
20   let OutOperandList = outs;
21   let InOperandList = ins;
22   let AsmString = asm;
23   let Pattern = pattern;
24   let Itinerary = NullALU;
25
26   let TSFlags{63} = isRegisterLoad;
27   let TSFlags{62} = isRegisterStore;
28 }
29
30 class AMDGPUShaderInst <dag outs, dag ins, string asm, list<dag> pattern>
31     : AMDGPUInst<outs, ins, asm, pattern> {
32
33   field bits<32> Inst = 0xffffffff;
34
35 }
36
37 def InstFlag : OperandWithDefaultOps <i32, (ops (i32 0))>;
38 def ADDRIndirect : ComplexPattern<iPTR, 2, "SelectADDRIndirect", [], []>;
39
40 def u32imm : Operand<i32> {
41   let PrintMethod = "printU32ImmOperand";
42 }
43
44 def u16imm : Operand<i16> {
45   let PrintMethod = "printU16ImmOperand";
46 }
47
48 def u8imm : Operand<i8> {
49   let PrintMethod = "printU8ImmOperand";
50 }
51
52 //===----------------------------------------------------------------------===//
53 // PatLeafs for floating-point comparisons
54 //===----------------------------------------------------------------------===//
55
56 def COND_OEQ : PatLeaf <
57   (cond),
58   [{return N->get() == ISD::SETOEQ || N->get() == ISD::SETEQ;}]
59 >;
60
61 def COND_OGT : PatLeaf <
62   (cond),
63   [{return N->get() == ISD::SETOGT || N->get() == ISD::SETGT;}]
64 >;
65
66 def COND_OGE : PatLeaf <
67   (cond),
68   [{return N->get() == ISD::SETOGE || N->get() == ISD::SETGE;}]
69 >;
70
71 def COND_OLT : PatLeaf <
72   (cond),
73   [{return N->get() == ISD::SETOLT || N->get() == ISD::SETLT;}]
74 >;
75
76 def COND_OLE : PatLeaf <
77   (cond),
78   [{return N->get() == ISD::SETOLE || N->get() == ISD::SETLE;}]
79 >;
80
81 def COND_UNE : PatLeaf <
82   (cond),
83   [{return N->get() == ISD::SETUNE || N->get() == ISD::SETNE;}]
84 >;
85
86 def COND_O : PatLeaf <(cond), [{return N->get() == ISD::SETO;}]>;
87 def COND_UO : PatLeaf <(cond), [{return N->get() == ISD::SETUO;}]>;
88
89 //===----------------------------------------------------------------------===//
90 // PatLeafs for unsigned comparisons
91 //===----------------------------------------------------------------------===//
92
93 def COND_UGT : PatLeaf <(cond), [{return N->get() == ISD::SETUGT;}]>;
94 def COND_UGE : PatLeaf <(cond), [{return N->get() == ISD::SETUGE;}]>;
95 def COND_ULT : PatLeaf <(cond), [{return N->get() == ISD::SETULT;}]>;
96 def COND_ULE : PatLeaf <(cond), [{return N->get() == ISD::SETULE;}]>;
97
98 //===----------------------------------------------------------------------===//
99 // PatLeafs for signed comparisons
100 //===----------------------------------------------------------------------===//
101
102 def COND_SGT : PatLeaf <(cond), [{return N->get() == ISD::SETGT;}]>;
103 def COND_SGE : PatLeaf <(cond), [{return N->get() == ISD::SETGE;}]>;
104 def COND_SLT : PatLeaf <(cond), [{return N->get() == ISD::SETLT;}]>;
105 def COND_SLE : PatLeaf <(cond), [{return N->get() == ISD::SETLE;}]>;
106
107 //===----------------------------------------------------------------------===//
108 // PatLeafs for integer equality
109 //===----------------------------------------------------------------------===//
110
111 def COND_EQ : PatLeaf <
112   (cond),
113   [{return N->get() == ISD::SETEQ || N->get() == ISD::SETUEQ;}]
114 >;
115
116 def COND_NE : PatLeaf <
117   (cond),
118   [{return N->get() == ISD::SETNE || N->get() == ISD::SETUNE;}]
119 >;
120
121 def COND_NULL : PatLeaf <
122   (cond),
123   [{return false;}]
124 >;
125
126 //===----------------------------------------------------------------------===//
127 // Load/Store Pattern Fragments
128 //===----------------------------------------------------------------------===//
129
130 def az_extload : PatFrag<(ops node:$ptr), (unindexedload node:$ptr), [{
131   LoadSDNode *L = cast<LoadSDNode>(N);
132   return L->getExtensionType() == ISD::ZEXTLOAD ||
133          L->getExtensionType() == ISD::EXTLOAD;
134 }]>;
135
136 def az_extloadi8 : PatFrag<(ops node:$ptr), (az_extload node:$ptr), [{
137   return cast<LoadSDNode>(N)->getMemoryVT() == MVT::i8;
138 }]>;
139
140 def az_extloadi8_global : PatFrag<(ops node:$ptr), (az_extloadi8 node:$ptr), [{
141     return isGlobalLoad(dyn_cast<LoadSDNode>(N));
142 }]>;
143
144 def sextloadi8_global : PatFrag<(ops node:$ptr), (sextloadi8 node:$ptr), [{
145     return isGlobalLoad(dyn_cast<LoadSDNode>(N));
146 }]>;
147
148 def az_extloadi8_constant : PatFrag<(ops node:$ptr), (az_extloadi8 node:$ptr), [{
149     return isConstantLoad(dyn_cast<LoadSDNode>(N), -1);
150 }]>;
151
152 def sextloadi8_constant : PatFrag<(ops node:$ptr), (sextloadi8 node:$ptr), [{
153     return isConstantLoad(dyn_cast<LoadSDNode>(N), -1);
154 }]>;
155
156 def az_extloadi8_local : PatFrag<(ops node:$ptr), (az_extloadi8 node:$ptr), [{
157     return isLocalLoad(dyn_cast<LoadSDNode>(N));
158 }]>;
159
160 def sextloadi8_local : PatFrag<(ops node:$ptr), (sextloadi8 node:$ptr), [{
161     return isLocalLoad(dyn_cast<LoadSDNode>(N));
162 }]>;
163
164 def az_extloadi16 : PatFrag<(ops node:$ptr), (az_extload node:$ptr), [{
165   return cast<LoadSDNode>(N)->getMemoryVT() == MVT::i16;
166 }]>;
167
168 def az_extloadi16_global : PatFrag<(ops node:$ptr), (az_extloadi16 node:$ptr), [{
169     return isGlobalLoad(dyn_cast<LoadSDNode>(N));
170 }]>;
171
172 def sextloadi16_global : PatFrag<(ops node:$ptr), (sextloadi16 node:$ptr), [{
173     return isGlobalLoad(dyn_cast<LoadSDNode>(N));
174 }]>;
175
176 def az_extloadi16_constant : PatFrag<(ops node:$ptr), (az_extloadi16 node:$ptr), [{
177     return isConstantLoad(dyn_cast<LoadSDNode>(N), -1);
178 }]>;
179
180 def sextloadi16_constant : PatFrag<(ops node:$ptr), (sextloadi16 node:$ptr), [{
181     return isConstantLoad(dyn_cast<LoadSDNode>(N), -1);
182 }]>;
183
184 def az_extloadi16_local : PatFrag<(ops node:$ptr), (az_extloadi16 node:$ptr), [{
185     return isLocalLoad(dyn_cast<LoadSDNode>(N));
186 }]>;
187
188 def sextloadi16_local : PatFrag<(ops node:$ptr), (sextloadi16 node:$ptr), [{
189     return isLocalLoad(dyn_cast<LoadSDNode>(N));
190 }]>;
191
192 def az_extloadi32 : PatFrag<(ops node:$ptr), (az_extload node:$ptr), [{
193   return cast<LoadSDNode>(N)->getMemoryVT() == MVT::i32;
194 }]>;
195
196 def az_extloadi32_global : PatFrag<(ops node:$ptr),
197                                    (az_extloadi32 node:$ptr), [{
198   return isGlobalLoad(dyn_cast<LoadSDNode>(N));
199 }]>;
200
201 def az_extloadi32_constant : PatFrag<(ops node:$ptr),
202                                      (az_extloadi32 node:$ptr), [{
203   return isConstantLoad(dyn_cast<LoadSDNode>(N), -1);
204 }]>;
205
206 def truncstorei8_global : PatFrag<(ops node:$val, node:$ptr),
207                                   (truncstorei8 node:$val, node:$ptr), [{
208   return isGlobalStore(dyn_cast<StoreSDNode>(N));
209 }]>;
210
211 def truncstorei16_global : PatFrag<(ops node:$val, node:$ptr),
212                                   (truncstorei16 node:$val, node:$ptr), [{
213   return isGlobalStore(dyn_cast<StoreSDNode>(N));
214 }]>;
215
216 def local_store : PatFrag<(ops node:$val, node:$ptr),
217                              (store node:$val, node:$ptr), [{
218   return isLocalStore(dyn_cast<StoreSDNode>(N));
219 }]>;
220
221 def truncstorei8_local : PatFrag<(ops node:$val, node:$ptr),
222                                   (truncstorei8 node:$val, node:$ptr), [{
223   return isLocalStore(dyn_cast<StoreSDNode>(N));
224 }]>;
225
226 def truncstorei16_local : PatFrag<(ops node:$val, node:$ptr),
227                                   (truncstorei16 node:$val, node:$ptr), [{
228   return isLocalStore(dyn_cast<StoreSDNode>(N));
229 }]>;
230
231 def local_load : PatFrag<(ops node:$ptr), (load node:$ptr), [{
232     return isLocalLoad(dyn_cast<LoadSDNode>(N));
233 }]>;
234
235 def atomic_load_add_local : PatFrag<(ops node:$ptr, node:$value),
236                                     (atomic_load_add node:$ptr, node:$value), [{
237   return dyn_cast<MemSDNode>(N)->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS;
238 }]>;
239
240 def atomic_load_sub_local : PatFrag<(ops node:$ptr, node:$value),
241                                     (atomic_load_sub node:$ptr, node:$value), [{
242   return dyn_cast<MemSDNode>(N)->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS;
243 }]>;
244
245 def mskor_global : PatFrag<(ops node:$val, node:$ptr),
246                             (AMDGPUstore_mskor node:$val, node:$ptr), [{
247   return dyn_cast<MemSDNode>(N)->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS;
248 }]>;
249
250 class Constants {
251 int TWO_PI = 0x40c90fdb;
252 int PI = 0x40490fdb;
253 int TWO_PI_INV = 0x3e22f983;
254 int FP_UINT_MAX_PLUS_1 = 0x4f800000;    // 1 << 32 in floating point encoding
255 int FP32_NEG_ONE = 0xbf800000;
256 int FP32_ONE = 0x3f800000;
257 }
258 def CONST : Constants;
259
260 def FP_ZERO : PatLeaf <
261   (fpimm),
262   [{return N->getValueAPF().isZero();}]
263 >;
264
265 def FP_ONE : PatLeaf <
266   (fpimm),
267   [{return N->isExactlyValue(1.0);}]
268 >;
269
270 let isCodeGenOnly = 1, isPseudo = 1 in {
271
272 let usesCustomInserter = 1  in {
273
274 class CLAMP <RegisterClass rc> : AMDGPUShaderInst <
275   (outs rc:$dst),
276   (ins rc:$src0),
277   "CLAMP $dst, $src0",
278   [(set f32:$dst, (int_AMDIL_clamp f32:$src0, (f32 FP_ZERO), (f32 FP_ONE)))]
279 >;
280
281 class FABS <RegisterClass rc> : AMDGPUShaderInst <
282   (outs rc:$dst),
283   (ins rc:$src0),
284   "FABS $dst, $src0",
285   [(set f32:$dst, (fabs f32:$src0))]
286 >;
287
288 class FNEG <RegisterClass rc> : AMDGPUShaderInst <
289   (outs rc:$dst),
290   (ins rc:$src0),
291   "FNEG $dst, $src0",
292   [(set f32:$dst, (fneg f32:$src0))]
293 >;
294
295 } // usesCustomInserter = 1
296
297 multiclass RegisterLoadStore <RegisterClass dstClass, Operand addrClass,
298                     ComplexPattern addrPat> {
299 let UseNamedOperandTable = 1 in {
300
301   def RegisterLoad : AMDGPUShaderInst <
302     (outs dstClass:$dst),
303     (ins addrClass:$addr, i32imm:$chan),
304     "RegisterLoad $dst, $addr",
305     [(set i32:$dst, (AMDGPUregister_load addrPat:$addr, (i32 timm:$chan)))]
306   > {
307     let isRegisterLoad = 1;
308   }
309
310   def RegisterStore : AMDGPUShaderInst <
311     (outs),
312     (ins dstClass:$val, addrClass:$addr, i32imm:$chan),
313     "RegisterStore $val, $addr",
314     [(AMDGPUregister_store i32:$val, addrPat:$addr, (i32 timm:$chan))]
315   > {
316     let isRegisterStore = 1;
317   }
318 }
319 }
320
321 } // End isCodeGenOnly = 1, isPseudo = 1
322
323 /* Generic helper patterns for intrinsics */
324 /* -------------------------------------- */
325
326 class POW_Common <AMDGPUInst log_ieee, AMDGPUInst exp_ieee, AMDGPUInst mul>
327   : Pat <
328   (fpow f32:$src0, f32:$src1),
329   (exp_ieee (mul f32:$src1, (log_ieee f32:$src0)))
330 >;
331
332 /* Other helper patterns */
333 /* --------------------- */
334
335 /* Extract element pattern */
336 class Extract_Element <ValueType sub_type, ValueType vec_type, int sub_idx,
337                        SubRegIndex sub_reg>
338   : Pat<
339   (sub_type (vector_extract vec_type:$src, sub_idx)),
340   (EXTRACT_SUBREG $src, sub_reg)
341 >;
342
343 /* Insert element pattern */
344 class Insert_Element <ValueType elem_type, ValueType vec_type,
345                       int sub_idx, SubRegIndex sub_reg>
346   : Pat <
347   (vector_insert vec_type:$vec, elem_type:$elem, sub_idx),
348   (INSERT_SUBREG $vec, $elem, sub_reg)
349 >;
350
351 // XXX: Convert to new syntax and use COPY_TO_REG, once the DFAPacketizer
352 // can handle COPY instructions.
353 // bitconvert pattern
354 class BitConvert <ValueType dt, ValueType st, RegisterClass rc> : Pat <
355   (dt (bitconvert (st rc:$src0))),
356   (dt rc:$src0)
357 >;
358
359 // XXX: Convert to new syntax and use COPY_TO_REG, once the DFAPacketizer
360 // can handle COPY instructions.
361 class DwordAddrPat<ValueType vt, RegisterClass rc> : Pat <
362   (vt (AMDGPUdwordaddr (vt rc:$addr))),
363   (vt rc:$addr)
364 >;
365
366 // BFI_INT patterns
367
368 multiclass BFIPatterns <Instruction BFI_INT, Instruction LoadImm32> {
369
370   // Definition from ISA doc:
371   // (y & x) | (z & ~x)
372   def : Pat <
373     (or (and i32:$y, i32:$x), (and i32:$z, (not i32:$x))),
374     (BFI_INT $x, $y, $z)
375   >;
376
377   // SHA-256 Ch function
378   // z ^ (x & (y ^ z))
379   def : Pat <
380     (xor i32:$z, (and i32:$x, (xor i32:$y, i32:$z))),
381     (BFI_INT $x, $y, $z)
382   >;
383
384   def : Pat <
385     (fcopysign f32:$src0, f32:$src1),
386     (BFI_INT (LoadImm32 0x7fffffff), $src0, $src1)
387   >;
388
389   def : Pat <
390     (f64 (fcopysign f64:$src0, f64:$src1)),
391       (INSERT_SUBREG (INSERT_SUBREG (f64 (IMPLICIT_DEF)),
392       (i32 (EXTRACT_SUBREG $src0, sub0)), sub0),
393       (BFI_INT (LoadImm32 0x7fffffff),
394                (i32 (EXTRACT_SUBREG $src0, sub1)),
395                (i32 (EXTRACT_SUBREG $src1, sub1))), sub1)
396   >;
397 }
398
399 // SHA-256 Ma patterns
400
401 // ((x & z) | (y & (x | z))) -> BFI_INT (XOR x, y), z, y
402 class SHA256MaPattern <Instruction BFI_INT, Instruction XOR> : Pat <
403   (or (and i32:$x, i32:$z), (and i32:$y, (or i32:$x, i32:$z))),
404   (BFI_INT (XOR i32:$x, i32:$y), i32:$z, i32:$y)
405 >;
406
407 // Bitfield extract patterns
408
409 /*
410
411 XXX: The BFE pattern is not working correctly because the XForm is not being
412 applied.
413
414 def legalshift32 : ImmLeaf <i32, [{return Imm >=0 && Imm < 32;}]>;
415 def bfemask : PatLeaf <(imm), [{return isMask_32(N->getZExtValue());}],
416                             SDNodeXForm<imm, [{ return CurDAG->getTargetConstant(CountTrailingOnes_32(N->getZExtValue()), MVT::i32);}]>>;
417
418 class BFEPattern <Instruction BFE> : Pat <
419   (and (srl i32:$x, legalshift32:$y), bfemask:$z),
420   (BFE $x, $y, $z)
421 >;
422
423 */
424
425 // rotr pattern
426 class ROTRPattern <Instruction BIT_ALIGN> : Pat <
427   (rotr i32:$src0, i32:$src1),
428   (BIT_ALIGN $src0, $src0, $src1)
429 >;
430
431 // 24-bit arithmetic patterns
432 def umul24 : PatFrag <(ops node:$x, node:$y), (mul node:$x, node:$y)>;
433
434 /*
435 class UMUL24Pattern <Instruction UMUL24> : Pat <
436   (mul U24:$x, U24:$y),
437   (UMUL24 $x, $y)
438 >;
439 */
440
441 class IMad24Pat<Instruction Inst> : Pat <
442   (add (AMDGPUmul_i24 i32:$src0, i32:$src1), i32:$src2),
443   (Inst $src0, $src1, $src2)
444 >;
445
446 class UMad24Pat<Instruction Inst> : Pat <
447   (add (AMDGPUmul_u24 i32:$src0, i32:$src1), i32:$src2),
448   (Inst $src0, $src1, $src2)
449 >;
450
451 multiclass Expand24IBitOps<Instruction MulInst, Instruction AddInst> {
452   def _expand_imad24 : Pat <
453     (AMDGPUmad_i24 i32:$src0, i32:$src1, i32:$src2),
454     (AddInst (MulInst $src0, $src1), $src2)
455   >;
456
457   def _expand_imul24 : Pat <
458     (AMDGPUmul_i24 i32:$src0, i32:$src1),
459     (MulInst $src0, $src1)
460   >;
461 }
462
463 multiclass Expand24UBitOps<Instruction MulInst, Instruction AddInst> {
464   def _expand_umad24 : Pat <
465     (AMDGPUmad_u24 i32:$src0, i32:$src1, i32:$src2),
466     (AddInst (MulInst $src0, $src1), $src2)
467   >;
468
469   def _expand_umul24 : Pat <
470     (AMDGPUmul_u24 i32:$src0, i32:$src1),
471     (MulInst $src0, $src1)
472   >;
473 }
474
475 include "R600Instructions.td"
476 include "R700Instructions.td"
477 include "EvergreenInstructions.td"
478 include "CaymanInstructions.td"
479
480 include "SIInstrInfo.td"
481