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
15class AMDGPUInst <dag outs, dag ins, string asm = "",
16  list<dag> pattern = []> : Instruction {
17  field bit isRegisterLoad = 0;
18  field bit isRegisterStore = 0;
19
20  let Namespace = "AMDGPU";
21  let OutOperandList = outs;
22  let InOperandList = ins;
23  let AsmString = asm;
24  let Pattern = pattern;
25  let Itinerary = NullALU;
26
27  // SoftFail is a field the disassembler can use to provide a way for
28  // instructions to not match without killing the whole decode process. It is
29  // mainly used for ARM, but Tablegen expects this field to exist or it fails
30  // to build the decode table.
31  field bits<64> SoftFail = 0;
32
33  let DecoderNamespace = Namespace;
34
35  let TSFlags{63} = isRegisterLoad;
36  let TSFlags{62} = isRegisterStore;
37}
38
39class AMDGPUShaderInst <dag outs, dag ins, string asm = "",
40  list<dag> pattern = []> : AMDGPUInst<outs, ins, asm, pattern> {
41
42  field bits<32> Inst = 0xffffffff;
43}
44
45def FP16Denormals : Predicate<"Subtarget->hasFP16Denormals()">;
46def FP32Denormals : Predicate<"Subtarget->hasFP32Denormals()">;
47def FP64Denormals : Predicate<"Subtarget->hasFP64Denormals()">;
48def NoFP16Denormals : Predicate<"!Subtarget->hasFP16Denormals()">;
49def NoFP32Denormals : Predicate<"!Subtarget->hasFP32Denormals()">;
50def NoFP64Denormals : Predicate<"!Subtarget->hasFP64Denormals()">;
51def UnsafeFPMath : Predicate<"TM.Options.UnsafeFPMath">;
52
53def InstFlag : OperandWithDefaultOps <i32, (ops (i32 0))>;
54def ADDRIndirect : ComplexPattern<iPTR, 2, "SelectADDRIndirect", [], []>;
55
56def u16ImmTarget : AsmOperandClass {
57  let Name = "U16Imm";
58  let RenderMethod = "addImmOperands";
59}
60
61def s16ImmTarget : AsmOperandClass {
62  let Name = "S16Imm";
63  let RenderMethod = "addImmOperands";
64}
65
66let OperandType = "OPERAND_IMMEDIATE" in {
67
68def u32imm : Operand<i32> {
69  let PrintMethod = "printU32ImmOperand";
70}
71
72def u16imm : Operand<i16> {
73  let PrintMethod = "printU16ImmOperand";
74  let ParserMatchClass = u16ImmTarget;
75}
76
77def s16imm : Operand<i16> {
78  let PrintMethod = "printU16ImmOperand";
79  let ParserMatchClass = s16ImmTarget;
80}
81
82def u8imm : Operand<i8> {
83  let PrintMethod = "printU8ImmOperand";
84}
85
86} // End OperandType = "OPERAND_IMMEDIATE"
87
88//===--------------------------------------------------------------------===//
89// Custom Operands
90//===--------------------------------------------------------------------===//
91def brtarget   : Operand<OtherVT>;
92
93//===----------------------------------------------------------------------===//
94// Misc. PatFrags
95//===----------------------------------------------------------------------===//
96
97class HasOneUseUnaryOp<SDPatternOperator op> : PatFrag<
98  (ops node:$src0),
99  (op $src0),
100  [{ return N->hasOneUse(); }]
101>;
102
103class HasOneUseBinOp<SDPatternOperator op> : PatFrag<
104  (ops node:$src0, node:$src1),
105  (op $src0, $src1),
106  [{ return N->hasOneUse(); }]
107>;
108
109class HasOneUseTernaryOp<SDPatternOperator op> : PatFrag<
110  (ops node:$src0, node:$src1, node:$src2),
111  (op $src0, $src1, $src2),
112  [{ return N->hasOneUse(); }]
113>;
114
115def trunc_oneuse : HasOneUseUnaryOp<trunc>;
116
117let Properties = [SDNPCommutative, SDNPAssociative] in {
118def smax_oneuse : HasOneUseBinOp<smax>;
119def smin_oneuse : HasOneUseBinOp<smin>;
120def umax_oneuse : HasOneUseBinOp<umax>;
121def umin_oneuse : HasOneUseBinOp<umin>;
122def fminnum_oneuse : HasOneUseBinOp<fminnum>;
123def fmaxnum_oneuse : HasOneUseBinOp<fmaxnum>;
124def and_oneuse : HasOneUseBinOp<and>;
125def or_oneuse : HasOneUseBinOp<or>;
126def xor_oneuse : HasOneUseBinOp<xor>;
127} // Properties = [SDNPCommutative, SDNPAssociative]
128
129def sub_oneuse : HasOneUseBinOp<sub>;
130
131def srl_oneuse : HasOneUseBinOp<srl>;
132def shl_oneuse : HasOneUseBinOp<shl>;
133
134def select_oneuse : HasOneUseTernaryOp<select>;
135
136//===----------------------------------------------------------------------===//
137// PatLeafs for floating-point comparisons
138//===----------------------------------------------------------------------===//
139
140def COND_OEQ : PatLeaf <
141  (cond),
142  [{return N->get() == ISD::SETOEQ || N->get() == ISD::SETEQ;}]
143>;
144
145def COND_ONE : PatLeaf <
146  (cond),
147  [{return N->get() == ISD::SETONE || N->get() == ISD::SETNE;}]
148>;
149
150def COND_OGT : PatLeaf <
151  (cond),
152  [{return N->get() == ISD::SETOGT || N->get() == ISD::SETGT;}]
153>;
154
155def COND_OGE : PatLeaf <
156  (cond),
157  [{return N->get() == ISD::SETOGE || N->get() == ISD::SETGE;}]
158>;
159
160def COND_OLT : PatLeaf <
161  (cond),
162  [{return N->get() == ISD::SETOLT || N->get() == ISD::SETLT;}]
163>;
164
165def COND_OLE : PatLeaf <
166  (cond),
167  [{return N->get() == ISD::SETOLE || N->get() == ISD::SETLE;}]
168>;
169
170
171def COND_O : PatLeaf <(cond), [{return N->get() == ISD::SETO;}]>;
172def COND_UO : PatLeaf <(cond), [{return N->get() == ISD::SETUO;}]>;
173
174//===----------------------------------------------------------------------===//
175// PatLeafs for unsigned / unordered comparisons
176//===----------------------------------------------------------------------===//
177
178def COND_UEQ : PatLeaf <(cond), [{return N->get() == ISD::SETUEQ;}]>;
179def COND_UNE : PatLeaf <(cond), [{return N->get() == ISD::SETUNE;}]>;
180def COND_UGT : PatLeaf <(cond), [{return N->get() == ISD::SETUGT;}]>;
181def COND_UGE : PatLeaf <(cond), [{return N->get() == ISD::SETUGE;}]>;
182def COND_ULT : PatLeaf <(cond), [{return N->get() == ISD::SETULT;}]>;
183def COND_ULE : PatLeaf <(cond), [{return N->get() == ISD::SETULE;}]>;
184
185// XXX - For some reason R600 version is preferring to use unordered
186// for setne?
187def COND_UNE_NE : PatLeaf <
188  (cond),
189  [{return N->get() == ISD::SETUNE || N->get() == ISD::SETNE;}]
190>;
191
192//===----------------------------------------------------------------------===//
193// PatLeafs for signed comparisons
194//===----------------------------------------------------------------------===//
195
196def COND_SGT : PatLeaf <(cond), [{return N->get() == ISD::SETGT;}]>;
197def COND_SGE : PatLeaf <(cond), [{return N->get() == ISD::SETGE;}]>;
198def COND_SLT : PatLeaf <(cond), [{return N->get() == ISD::SETLT;}]>;
199def COND_SLE : PatLeaf <(cond), [{return N->get() == ISD::SETLE;}]>;
200
201//===----------------------------------------------------------------------===//
202// PatLeafs for integer equality
203//===----------------------------------------------------------------------===//
204
205def COND_EQ : PatLeaf <
206  (cond),
207  [{return N->get() == ISD::SETEQ || N->get() == ISD::SETUEQ;}]
208>;
209
210def COND_NE : PatLeaf <
211  (cond),
212  [{return N->get() == ISD::SETNE || N->get() == ISD::SETUNE;}]
213>;
214
215def COND_NULL : PatLeaf <
216  (cond),
217  [{(void)N; return false;}]
218>;
219
220
221//===----------------------------------------------------------------------===//
222// Load/Store Pattern Fragments
223//===----------------------------------------------------------------------===//
224
225class Aligned8Bytes <dag ops, dag frag> : PatFrag <ops, frag, [{
226  return cast<MemSDNode>(N)->getAlignment() % 8 == 0;
227}]>;
228
229class LoadFrag <SDPatternOperator op> : PatFrag<(ops node:$ptr), (op node:$ptr)>;
230
231class StoreFrag<SDPatternOperator op> : PatFrag <
232  (ops node:$value, node:$ptr), (op node:$value, node:$ptr)
233>;
234
235class StoreHi16<SDPatternOperator op> : PatFrag <
236  (ops node:$value, node:$ptr), (op (srl node:$value, (i32 16)), node:$ptr)
237>;
238
239class PrivateAddress : CodePatPred<[{
240  return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUASI.PRIVATE_ADDRESS;
241}]>;
242
243class ConstantAddress : CodePatPred<[{
244  return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUASI.CONSTANT_ADDRESS;
245}]>;
246
247class LocalAddress : CodePatPred<[{
248  return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUASI.LOCAL_ADDRESS;
249}]>;
250
251class GlobalAddress : CodePatPred<[{
252  return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUASI.GLOBAL_ADDRESS;
253}]>;
254
255class GlobalLoadAddress : CodePatPred<[{
256  auto AS = cast<MemSDNode>(N)->getAddressSpace();
257  return AS == AMDGPUASI.GLOBAL_ADDRESS || AS == AMDGPUASI.CONSTANT_ADDRESS;
258}]>;
259
260class FlatLoadAddress : CodePatPred<[{
261  const auto AS = cast<MemSDNode>(N)->getAddressSpace();
262  return AS == AMDGPUASI.FLAT_ADDRESS ||
263         AS == AMDGPUASI.GLOBAL_ADDRESS ||
264         AS == AMDGPUASI.CONSTANT_ADDRESS;
265}]>;
266
267class FlatStoreAddress : CodePatPred<[{
268  const auto AS = cast<MemSDNode>(N)->getAddressSpace();
269  return AS == AMDGPUASI.FLAT_ADDRESS ||
270         AS == AMDGPUASI.GLOBAL_ADDRESS;
271}]>;
272
273class AZExtLoadBase <SDPatternOperator ld_node>: PatFrag<(ops node:$ptr),
274                                              (ld_node node:$ptr), [{
275  LoadSDNode *L = cast<LoadSDNode>(N);
276  return L->getExtensionType() == ISD::ZEXTLOAD ||
277         L->getExtensionType() == ISD::EXTLOAD;
278}]>;
279
280def az_extload : AZExtLoadBase <unindexedload>;
281
282def az_extloadi8 : PatFrag<(ops node:$ptr), (az_extload node:$ptr), [{
283  return cast<LoadSDNode>(N)->getMemoryVT() == MVT::i8;
284}]>;
285
286def az_extloadi16 : PatFrag<(ops node:$ptr), (az_extload node:$ptr), [{
287  return cast<LoadSDNode>(N)->getMemoryVT() == MVT::i16;
288}]>;
289
290def az_extloadi32 : PatFrag<(ops node:$ptr), (az_extload node:$ptr), [{
291  return cast<LoadSDNode>(N)->getMemoryVT() == MVT::i32;
292}]>;
293
294class PrivateLoad <SDPatternOperator op> : LoadFrag <op>, PrivateAddress;
295class PrivateStore <SDPatternOperator op> : StoreFrag <op>, PrivateAddress;
296
297class LocalLoad <SDPatternOperator op> : LoadFrag <op>, LocalAddress;
298class LocalStore <SDPatternOperator op> : StoreFrag <op>, LocalAddress;
299
300class GlobalLoad <SDPatternOperator op> : LoadFrag<op>, GlobalLoadAddress;
301class GlobalStore <SDPatternOperator op> : StoreFrag<op>, GlobalAddress;
302
303class FlatLoad <SDPatternOperator op> : LoadFrag <op>, FlatLoadAddress;
304class FlatStore <SDPatternOperator op> : StoreFrag <op>, FlatStoreAddress;
305
306class ConstantLoad <SDPatternOperator op> : LoadFrag <op>, ConstantAddress;
307
308
309def load_private : PrivateLoad <load>;
310def az_extloadi8_private : PrivateLoad <az_extloadi8>;
311def sextloadi8_private : PrivateLoad <sextloadi8>;
312def az_extloadi16_private : PrivateLoad <az_extloadi16>;
313def sextloadi16_private : PrivateLoad <sextloadi16>;
314
315def store_private : PrivateStore <store>;
316def truncstorei8_private : PrivateStore<truncstorei8>;
317def truncstorei16_private : PrivateStore <truncstorei16>;
318def store_hi16_private : StoreHi16 <truncstorei16>, PrivateAddress;
319def truncstorei8_hi16_private : StoreHi16<truncstorei8>, PrivateAddress;
320
321
322def load_global : GlobalLoad <load>;
323def sextloadi8_global : GlobalLoad <sextloadi8>;
324def az_extloadi8_global : GlobalLoad <az_extloadi8>;
325def sextloadi16_global : GlobalLoad <sextloadi16>;
326def az_extloadi16_global : GlobalLoad <az_extloadi16>;
327def atomic_load_global : GlobalLoad<atomic_load>;
328
329def store_global : GlobalStore <store>;
330def truncstorei8_global : GlobalStore <truncstorei8>;
331def truncstorei16_global : GlobalStore <truncstorei16>;
332def store_atomic_global : GlobalStore<atomic_store>;
333def truncstorei8_hi16_global : StoreHi16 <truncstorei8>, GlobalAddress;
334def truncstorei16_hi16_global : StoreHi16 <truncstorei16>, GlobalAddress;
335
336def load_local : LocalLoad <load>;
337def az_extloadi8_local : LocalLoad <az_extloadi8>;
338def sextloadi8_local : LocalLoad <sextloadi8>;
339def az_extloadi16_local : LocalLoad <az_extloadi16>;
340def sextloadi16_local : LocalLoad <sextloadi16>;
341
342def store_local : LocalStore <store>;
343def truncstorei8_local : LocalStore <truncstorei8>;
344def truncstorei16_local : LocalStore <truncstorei16>;
345def store_local_hi16 : StoreHi16 <truncstorei16>, LocalAddress;
346def truncstorei8_local_hi16 : StoreHi16<truncstorei8>, LocalAddress;
347
348def load_align8_local : Aligned8Bytes <
349  (ops node:$ptr), (load_local node:$ptr)
350>;
351
352def store_align8_local : Aligned8Bytes <
353  (ops node:$val, node:$ptr), (store_local node:$val, node:$ptr)
354>;
355
356
357def load_flat          : FlatLoad <load>;
358def az_extloadi8_flat  : FlatLoad <az_extloadi8>;
359def sextloadi8_flat    : FlatLoad <sextloadi8>;
360def az_extloadi16_flat : FlatLoad <az_extloadi16>;
361def sextloadi16_flat   : FlatLoad <sextloadi16>;
362def atomic_load_flat   : FlatLoad<atomic_load>;
363
364def store_flat         : FlatStore <store>;
365def truncstorei8_flat  : FlatStore <truncstorei8>;
366def truncstorei16_flat : FlatStore <truncstorei16>;
367def atomic_store_flat  : FlatStore <atomic_store>;
368def truncstorei8_hi16_flat  : StoreHi16<truncstorei8>, FlatStoreAddress;
369def truncstorei16_hi16_flat : StoreHi16<truncstorei16>, FlatStoreAddress;
370
371
372def constant_load : ConstantLoad<load>;
373def sextloadi8_constant : ConstantLoad <sextloadi8>;
374def az_extloadi8_constant : ConstantLoad <az_extloadi8>;
375def sextloadi16_constant : ConstantLoad <sextloadi16>;
376def az_extloadi16_constant : ConstantLoad <az_extloadi16>;
377
378
379class local_binary_atomic_op<SDNode atomic_op> :
380  PatFrag<(ops node:$ptr, node:$value),
381    (atomic_op node:$ptr, node:$value), [{
382  return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUASI.LOCAL_ADDRESS;
383}]>;
384
385def atomic_swap_local : local_binary_atomic_op<atomic_swap>;
386def atomic_load_add_local : local_binary_atomic_op<atomic_load_add>;
387def atomic_load_sub_local : local_binary_atomic_op<atomic_load_sub>;
388def atomic_load_and_local : local_binary_atomic_op<atomic_load_and>;
389def atomic_load_or_local : local_binary_atomic_op<atomic_load_or>;
390def atomic_load_xor_local : local_binary_atomic_op<atomic_load_xor>;
391def atomic_load_nand_local : local_binary_atomic_op<atomic_load_nand>;
392def atomic_load_min_local : local_binary_atomic_op<atomic_load_min>;
393def atomic_load_max_local : local_binary_atomic_op<atomic_load_max>;
394def atomic_load_umin_local : local_binary_atomic_op<atomic_load_umin>;
395def atomic_load_umax_local : local_binary_atomic_op<atomic_load_umax>;
396
397def mskor_global : PatFrag<(ops node:$val, node:$ptr),
398                            (AMDGPUstore_mskor node:$val, node:$ptr), [{
399  return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUASI.GLOBAL_ADDRESS;
400}]>;
401
402multiclass AtomicCmpSwapLocal <SDNode cmp_swap_node> {
403
404  def _32_local : PatFrag <
405    (ops node:$ptr, node:$cmp, node:$swap),
406    (cmp_swap_node node:$ptr, node:$cmp, node:$swap), [{
407      AtomicSDNode *AN = cast<AtomicSDNode>(N);
408      return AN->getMemoryVT() == MVT::i32 &&
409             AN->getAddressSpace() == AMDGPUASI.LOCAL_ADDRESS;
410  }]>;
411
412  def _64_local : PatFrag<
413    (ops node:$ptr, node:$cmp, node:$swap),
414    (cmp_swap_node node:$ptr, node:$cmp, node:$swap), [{
415      AtomicSDNode *AN = cast<AtomicSDNode>(N);
416      return AN->getMemoryVT() == MVT::i64 &&
417             AN->getAddressSpace() == AMDGPUASI.LOCAL_ADDRESS;
418  }]>;
419}
420
421defm atomic_cmp_swap : AtomicCmpSwapLocal <atomic_cmp_swap>;
422
423multiclass global_binary_atomic_op<SDNode atomic_op> {
424  def "" : PatFrag<
425        (ops node:$ptr, node:$value),
426        (atomic_op node:$ptr, node:$value),
427        [{return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUASI.GLOBAL_ADDRESS;}]>;
428
429  def _noret : PatFrag<
430        (ops node:$ptr, node:$value),
431        (atomic_op node:$ptr, node:$value),
432        [{return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUASI.GLOBAL_ADDRESS && (SDValue(N, 0).use_empty());}]>;
433
434  def _ret : PatFrag<
435        (ops node:$ptr, node:$value),
436        (atomic_op node:$ptr, node:$value),
437        [{return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUASI.GLOBAL_ADDRESS && (!SDValue(N, 0).use_empty());}]>;
438}
439
440defm atomic_swap_global : global_binary_atomic_op<atomic_swap>;
441defm atomic_add_global : global_binary_atomic_op<atomic_load_add>;
442defm atomic_and_global : global_binary_atomic_op<atomic_load_and>;
443defm atomic_max_global : global_binary_atomic_op<atomic_load_max>;
444defm atomic_min_global : global_binary_atomic_op<atomic_load_min>;
445defm atomic_or_global : global_binary_atomic_op<atomic_load_or>;
446defm atomic_sub_global : global_binary_atomic_op<atomic_load_sub>;
447defm atomic_umax_global : global_binary_atomic_op<atomic_load_umax>;
448defm atomic_umin_global : global_binary_atomic_op<atomic_load_umin>;
449defm atomic_xor_global : global_binary_atomic_op<atomic_load_xor>;
450
451// Legacy.
452def AMDGPUatomic_cmp_swap_global : PatFrag<
453  (ops node:$ptr, node:$value),
454  (AMDGPUatomic_cmp_swap node:$ptr, node:$value)>, GlobalAddress;
455
456def atomic_cmp_swap_global : PatFrag<
457  (ops node:$ptr, node:$cmp, node:$value),
458  (atomic_cmp_swap node:$ptr, node:$cmp, node:$value)>, GlobalAddress;
459
460
461def atomic_cmp_swap_global_noret : PatFrag<
462  (ops node:$ptr, node:$cmp, node:$value),
463  (atomic_cmp_swap node:$ptr, node:$cmp, node:$value),
464  [{return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUASI.GLOBAL_ADDRESS && (SDValue(N, 0).use_empty());}]>;
465
466def atomic_cmp_swap_global_ret : PatFrag<
467  (ops node:$ptr, node:$cmp, node:$value),
468  (atomic_cmp_swap node:$ptr, node:$cmp, node:$value),
469  [{return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUASI.GLOBAL_ADDRESS && (!SDValue(N, 0).use_empty());}]>;
470
471//===----------------------------------------------------------------------===//
472// Misc Pattern Fragments
473//===----------------------------------------------------------------------===//
474
475class Constants {
476int TWO_PI = 0x40c90fdb;
477int PI = 0x40490fdb;
478int TWO_PI_INV = 0x3e22f983;
479int FP_UINT_MAX_PLUS_1 = 0x4f800000;    // 1 << 32 in floating point encoding
480int FP16_ONE = 0x3C00;
481int V2FP16_ONE = 0x3C003C00;
482int FP32_ONE = 0x3f800000;
483int FP32_NEG_ONE = 0xbf800000;
484int FP64_ONE = 0x3ff0000000000000;
485int FP64_NEG_ONE = 0xbff0000000000000;
486}
487def CONST : Constants;
488
489def FP_ZERO : PatLeaf <
490  (fpimm),
491  [{return N->getValueAPF().isZero();}]
492>;
493
494def FP_ONE : PatLeaf <
495  (fpimm),
496  [{return N->isExactlyValue(1.0);}]
497>;
498
499def FP_HALF : PatLeaf <
500  (fpimm),
501  [{return N->isExactlyValue(0.5);}]
502>;
503
504/* Generic helper patterns for intrinsics */
505/* -------------------------------------- */
506
507class POW_Common <AMDGPUInst log_ieee, AMDGPUInst exp_ieee, AMDGPUInst mul>
508  : Pat <
509  (fpow f32:$src0, f32:$src1),
510  (exp_ieee (mul f32:$src1, (log_ieee f32:$src0)))
511>;
512
513/* Other helper patterns */
514/* --------------------- */
515
516/* Extract element pattern */
517class Extract_Element <ValueType sub_type, ValueType vec_type, int sub_idx,
518                       SubRegIndex sub_reg>
519  : Pat<
520  (sub_type (extractelt vec_type:$src, sub_idx)),
521  (EXTRACT_SUBREG $src, sub_reg)
522>;
523
524/* Insert element pattern */
525class Insert_Element <ValueType elem_type, ValueType vec_type,
526                      int sub_idx, SubRegIndex sub_reg>
527  : Pat <
528  (insertelt vec_type:$vec, elem_type:$elem, sub_idx),
529  (INSERT_SUBREG $vec, $elem, sub_reg)
530>;
531
532// XXX: Convert to new syntax and use COPY_TO_REG, once the DFAPacketizer
533// can handle COPY instructions.
534// bitconvert pattern
535class BitConvert <ValueType dt, ValueType st, RegisterClass rc> : Pat <
536  (dt (bitconvert (st rc:$src0))),
537  (dt rc:$src0)
538>;
539
540// XXX: Convert to new syntax and use COPY_TO_REG, once the DFAPacketizer
541// can handle COPY instructions.
542class DwordAddrPat<ValueType vt, RegisterClass rc> : Pat <
543  (vt (AMDGPUdwordaddr (vt rc:$addr))),
544  (vt rc:$addr)
545>;
546
547// BFI_INT patterns
548
549multiclass BFIPatterns <Instruction BFI_INT,
550                        Instruction LoadImm32,
551                        RegisterClass RC64> {
552  // Definition from ISA doc:
553  // (y & x) | (z & ~x)
554  def : Pat <
555    (or (and i32:$y, i32:$x), (and i32:$z, (not i32:$x))),
556    (BFI_INT $x, $y, $z)
557  >;
558
559  // SHA-256 Ch function
560  // z ^ (x & (y ^ z))
561  def : Pat <
562    (xor i32:$z, (and i32:$x, (xor i32:$y, i32:$z))),
563    (BFI_INT $x, $y, $z)
564  >;
565
566  def : Pat <
567    (fcopysign f32:$src0, f32:$src1),
568    (BFI_INT (LoadImm32 (i32 0x7fffffff)), $src0, $src1)
569  >;
570
571  def : Pat <
572    (f32 (fcopysign f32:$src0, f64:$src1)),
573    (BFI_INT (LoadImm32 (i32 0x7fffffff)), $src0,
574             (i32 (EXTRACT_SUBREG $src1, sub1)))
575  >;
576
577  def : Pat <
578    (f64 (fcopysign f64:$src0, f64:$src1)),
579    (REG_SEQUENCE RC64,
580      (i32 (EXTRACT_SUBREG $src0, sub0)), sub0,
581      (BFI_INT (LoadImm32 (i32 0x7fffffff)),
582               (i32 (EXTRACT_SUBREG $src0, sub1)),
583               (i32 (EXTRACT_SUBREG $src1, sub1))), sub1)
584  >;
585
586  def : Pat <
587    (f64 (fcopysign f64:$src0, f32:$src1)),
588    (REG_SEQUENCE RC64,
589      (i32 (EXTRACT_SUBREG $src0, sub0)), sub0,
590      (BFI_INT (LoadImm32 (i32 0x7fffffff)),
591               (i32 (EXTRACT_SUBREG $src0, sub1)),
592               $src1), sub1)
593  >;
594}
595
596// SHA-256 Ma patterns
597
598// ((x & z) | (y & (x | z))) -> BFI_INT (XOR x, y), z, y
599class SHA256MaPattern <Instruction BFI_INT, Instruction XOR> : Pat <
600  (or (and i32:$x, i32:$z), (and i32:$y, (or i32:$x, i32:$z))),
601  (BFI_INT (XOR i32:$x, i32:$y), i32:$z, i32:$y)
602>;
603
604// Bitfield extract patterns
605
606def IMMZeroBasedBitfieldMask : PatLeaf <(imm), [{
607  return isMask_32(N->getZExtValue());
608}]>;
609
610def IMMPopCount : SDNodeXForm<imm, [{
611  return CurDAG->getTargetConstant(countPopulation(N->getZExtValue()), SDLoc(N),
612                                   MVT::i32);
613}]>;
614
615multiclass BFEPattern <Instruction UBFE, Instruction SBFE, Instruction MOV> {
616  def : Pat <
617    (i32 (and (i32 (srl i32:$src, i32:$rshift)), IMMZeroBasedBitfieldMask:$mask)),
618    (UBFE $src, $rshift, (MOV (i32 (IMMPopCount $mask))))
619  >;
620
621  def : Pat <
622    (srl (shl_oneuse i32:$src, (sub 32, i32:$width)), (sub 32, i32:$width)),
623    (UBFE $src, (i32 0), $width)
624  >;
625
626  def : Pat <
627    (sra (shl_oneuse i32:$src, (sub 32, i32:$width)), (sub 32, i32:$width)),
628    (SBFE $src, (i32 0), $width)
629  >;
630}
631
632// rotr pattern
633class ROTRPattern <Instruction BIT_ALIGN> : Pat <
634  (rotr i32:$src0, i32:$src1),
635  (BIT_ALIGN $src0, $src0, $src1)
636>;
637
638// This matches 16 permutations of
639// max(min(x, y), min(max(x, y), z))
640class IntMed3Pat<Instruction med3Inst,
641                 SDPatternOperator max,
642                 SDPatternOperator max_oneuse,
643                 SDPatternOperator min_oneuse,
644                 ValueType vt = i32> : Pat<
645  (max (min_oneuse vt:$src0, vt:$src1),
646       (min_oneuse (max_oneuse vt:$src0, vt:$src1), vt:$src2)),
647  (med3Inst $src0, $src1, $src2)
648>;
649
650// Special conversion patterns
651
652def cvt_rpi_i32_f32 : PatFrag <
653  (ops node:$src),
654  (fp_to_sint (ffloor (fadd $src, FP_HALF))),
655  [{ (void) N; return TM.Options.NoNaNsFPMath; }]
656>;
657
658def cvt_flr_i32_f32 : PatFrag <
659  (ops node:$src),
660  (fp_to_sint (ffloor $src)),
661  [{ (void)N; return TM.Options.NoNaNsFPMath; }]
662>;
663
664class IMad24Pat<Instruction Inst, bit HasClamp = 0> : Pat <
665  (add (AMDGPUmul_i24 i32:$src0, i32:$src1), i32:$src2),
666  !if(HasClamp, (Inst $src0, $src1, $src2, (i1 0)),
667                (Inst $src0, $src1, $src2))
668>;
669
670class UMad24Pat<Instruction Inst, bit HasClamp = 0> : Pat <
671  (add (AMDGPUmul_u24 i32:$src0, i32:$src1), i32:$src2),
672  !if(HasClamp, (Inst $src0, $src1, $src2, (i1 0)),
673                (Inst $src0, $src1, $src2))
674>;
675
676class RcpPat<Instruction RcpInst, ValueType vt> : Pat <
677  (fdiv FP_ONE, vt:$src),
678  (RcpInst $src)
679>;
680
681class RsqPat<Instruction RsqInst, ValueType vt> : Pat <
682  (AMDGPUrcp (fsqrt vt:$src)),
683  (RsqInst $src)
684>;
685
686include "R600Instructions.td"
687include "R700Instructions.td"
688include "EvergreenInstructions.td"
689include "CaymanInstructions.td"
690
691include "SIInstrInfo.td"
692
693