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