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">;
52def FMA : Predicate<"Subtarget->hasFMA()">;
53
54def InstFlag : OperandWithDefaultOps <i32, (ops (i32 0))>;
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
136def srl_16 : PatFrag<
137  (ops node:$src0), (srl_oneuse node:$src0, (i32 16))
138>;
139
140
141def hi_i16_elt : PatFrag<
142  (ops node:$src0), (i16 (trunc (i32 (srl_16 node:$src0))))
143>;
144
145
146def hi_f16_elt : PatLeaf<
147  (vt), [{
148  if (N->getOpcode() != ISD::BITCAST)
149    return false;
150  SDValue Tmp = N->getOperand(0);
151
152  if (Tmp.getOpcode() != ISD::SRL)
153    return false;
154    if (const auto *RHS = dyn_cast<ConstantSDNode>(Tmp.getOperand(1))
155      return RHS->getZExtValue() == 16;
156    return false;
157}]>;
158
159//===----------------------------------------------------------------------===//
160// PatLeafs for floating-point comparisons
161//===----------------------------------------------------------------------===//
162
163def COND_OEQ : PatLeaf <
164  (cond),
165  [{return N->get() == ISD::SETOEQ || N->get() == ISD::SETEQ;}]
166>;
167
168def COND_ONE : PatLeaf <
169  (cond),
170  [{return N->get() == ISD::SETONE || N->get() == ISD::SETNE;}]
171>;
172
173def COND_OGT : PatLeaf <
174  (cond),
175  [{return N->get() == ISD::SETOGT || N->get() == ISD::SETGT;}]
176>;
177
178def COND_OGE : PatLeaf <
179  (cond),
180  [{return N->get() == ISD::SETOGE || N->get() == ISD::SETGE;}]
181>;
182
183def COND_OLT : PatLeaf <
184  (cond),
185  [{return N->get() == ISD::SETOLT || N->get() == ISD::SETLT;}]
186>;
187
188def COND_OLE : PatLeaf <
189  (cond),
190  [{return N->get() == ISD::SETOLE || N->get() == ISD::SETLE;}]
191>;
192
193def COND_O : PatLeaf <(cond), [{return N->get() == ISD::SETO;}]>;
194def COND_UO : PatLeaf <(cond), [{return N->get() == ISD::SETUO;}]>;
195
196//===----------------------------------------------------------------------===//
197// PatLeafs for unsigned / unordered comparisons
198//===----------------------------------------------------------------------===//
199
200def COND_UEQ : PatLeaf <(cond), [{return N->get() == ISD::SETUEQ;}]>;
201def COND_UNE : PatLeaf <(cond), [{return N->get() == ISD::SETUNE;}]>;
202def COND_UGT : PatLeaf <(cond), [{return N->get() == ISD::SETUGT;}]>;
203def COND_UGE : PatLeaf <(cond), [{return N->get() == ISD::SETUGE;}]>;
204def COND_ULT : PatLeaf <(cond), [{return N->get() == ISD::SETULT;}]>;
205def COND_ULE : PatLeaf <(cond), [{return N->get() == ISD::SETULE;}]>;
206
207// XXX - For some reason R600 version is preferring to use unordered
208// for setne?
209def COND_UNE_NE : PatLeaf <
210  (cond),
211  [{return N->get() == ISD::SETUNE || N->get() == ISD::SETNE;}]
212>;
213
214//===----------------------------------------------------------------------===//
215// PatLeafs for signed comparisons
216//===----------------------------------------------------------------------===//
217
218def COND_SGT : PatLeaf <(cond), [{return N->get() == ISD::SETGT;}]>;
219def COND_SGE : PatLeaf <(cond), [{return N->get() == ISD::SETGE;}]>;
220def COND_SLT : PatLeaf <(cond), [{return N->get() == ISD::SETLT;}]>;
221def COND_SLE : PatLeaf <(cond), [{return N->get() == ISD::SETLE;}]>;
222
223//===----------------------------------------------------------------------===//
224// PatLeafs for integer equality
225//===----------------------------------------------------------------------===//
226
227def COND_EQ : PatLeaf <
228  (cond),
229  [{return N->get() == ISD::SETEQ || N->get() == ISD::SETUEQ;}]
230>;
231
232def COND_NE : PatLeaf <
233  (cond),
234  [{return N->get() == ISD::SETNE || N->get() == ISD::SETUNE;}]
235>;
236
237def COND_NULL : PatLeaf <
238  (cond),
239  [{(void)N; return false;}]
240>;
241
242
243//===----------------------------------------------------------------------===//
244// Load/Store Pattern Fragments
245//===----------------------------------------------------------------------===//
246
247class Aligned8Bytes <dag ops, dag frag> : PatFrag <ops, frag, [{
248  return cast<MemSDNode>(N)->getAlignment() % 8 == 0;
249}]>;
250
251class LoadFrag <SDPatternOperator op> : PatFrag<(ops node:$ptr), (op node:$ptr)>;
252
253class StoreFrag<SDPatternOperator op> : PatFrag <
254  (ops node:$value, node:$ptr), (op node:$value, node:$ptr)
255>;
256
257class StoreHi16<SDPatternOperator op> : PatFrag <
258  (ops node:$value, node:$ptr), (op (srl node:$value, (i32 16)), node:$ptr)
259>;
260
261class PrivateAddress : CodePatPred<[{
262  return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUASI.PRIVATE_ADDRESS;
263}]>;
264
265class ConstantAddress : CodePatPred<[{
266  return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUASI.CONSTANT_ADDRESS;
267}]>;
268
269class LocalAddress : CodePatPred<[{
270  return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUASI.LOCAL_ADDRESS;
271}]>;
272
273class GlobalAddress : CodePatPred<[{
274  return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUASI.GLOBAL_ADDRESS;
275}]>;
276
277class GlobalLoadAddress : CodePatPred<[{
278  auto AS = cast<MemSDNode>(N)->getAddressSpace();
279  return AS == AMDGPUASI.GLOBAL_ADDRESS || AS == AMDGPUASI.CONSTANT_ADDRESS;
280}]>;
281
282class FlatLoadAddress : CodePatPred<[{
283  const auto AS = cast<MemSDNode>(N)->getAddressSpace();
284  return AS == AMDGPUASI.FLAT_ADDRESS ||
285         AS == AMDGPUASI.GLOBAL_ADDRESS ||
286         AS == AMDGPUASI.CONSTANT_ADDRESS;
287}]>;
288
289class FlatStoreAddress : CodePatPred<[{
290  const auto AS = cast<MemSDNode>(N)->getAddressSpace();
291  return AS == AMDGPUASI.FLAT_ADDRESS ||
292         AS == AMDGPUASI.GLOBAL_ADDRESS;
293}]>;
294
295class AZExtLoadBase <SDPatternOperator ld_node>: PatFrag<(ops node:$ptr),
296                                              (ld_node node:$ptr), [{
297  LoadSDNode *L = cast<LoadSDNode>(N);
298  return L->getExtensionType() == ISD::ZEXTLOAD ||
299         L->getExtensionType() == ISD::EXTLOAD;
300}]>;
301
302def az_extload : AZExtLoadBase <unindexedload>;
303
304def az_extloadi8 : PatFrag<(ops node:$ptr), (az_extload node:$ptr), [{
305  return cast<LoadSDNode>(N)->getMemoryVT() == MVT::i8;
306}]>;
307
308def az_extloadi16 : PatFrag<(ops node:$ptr), (az_extload node:$ptr), [{
309  return cast<LoadSDNode>(N)->getMemoryVT() == MVT::i16;
310}]>;
311
312def az_extloadi32 : PatFrag<(ops node:$ptr), (az_extload node:$ptr), [{
313  return cast<LoadSDNode>(N)->getMemoryVT() == MVT::i32;
314}]>;
315
316class PrivateLoad <SDPatternOperator op> : LoadFrag <op>, PrivateAddress;
317class PrivateStore <SDPatternOperator op> : StoreFrag <op>, PrivateAddress;
318
319class LocalLoad <SDPatternOperator op> : LoadFrag <op>, LocalAddress;
320class LocalStore <SDPatternOperator op> : StoreFrag <op>, LocalAddress;
321
322class GlobalLoad <SDPatternOperator op> : LoadFrag<op>, GlobalLoadAddress;
323class GlobalStore <SDPatternOperator op> : StoreFrag<op>, GlobalAddress;
324
325class FlatLoad <SDPatternOperator op> : LoadFrag <op>, FlatLoadAddress;
326class FlatStore <SDPatternOperator op> : StoreFrag <op>, FlatStoreAddress;
327
328class ConstantLoad <SDPatternOperator op> : LoadFrag <op>, ConstantAddress;
329
330
331def load_private : PrivateLoad <load>;
332def az_extloadi8_private : PrivateLoad <az_extloadi8>;
333def sextloadi8_private : PrivateLoad <sextloadi8>;
334def az_extloadi16_private : PrivateLoad <az_extloadi16>;
335def sextloadi16_private : PrivateLoad <sextloadi16>;
336
337def store_private : PrivateStore <store>;
338def truncstorei8_private : PrivateStore<truncstorei8>;
339def truncstorei16_private : PrivateStore <truncstorei16>;
340def store_hi16_private : StoreHi16 <truncstorei16>, PrivateAddress;
341def truncstorei8_hi16_private : StoreHi16<truncstorei8>, PrivateAddress;
342
343
344def load_global : GlobalLoad <load>;
345def sextloadi8_global : GlobalLoad <sextloadi8>;
346def az_extloadi8_global : GlobalLoad <az_extloadi8>;
347def sextloadi16_global : GlobalLoad <sextloadi16>;
348def az_extloadi16_global : GlobalLoad <az_extloadi16>;
349def atomic_load_global : GlobalLoad<atomic_load>;
350
351def store_global : GlobalStore <store>;
352def truncstorei8_global : GlobalStore <truncstorei8>;
353def truncstorei16_global : GlobalStore <truncstorei16>;
354def store_atomic_global : GlobalStore<atomic_store>;
355def truncstorei8_hi16_global : StoreHi16 <truncstorei8>, GlobalAddress;
356def truncstorei16_hi16_global : StoreHi16 <truncstorei16>, GlobalAddress;
357
358def load_local : LocalLoad <load>;
359def az_extloadi8_local : LocalLoad <az_extloadi8>;
360def sextloadi8_local : LocalLoad <sextloadi8>;
361def az_extloadi16_local : LocalLoad <az_extloadi16>;
362def sextloadi16_local : LocalLoad <sextloadi16>;
363
364def store_local : LocalStore <store>;
365def truncstorei8_local : LocalStore <truncstorei8>;
366def truncstorei16_local : LocalStore <truncstorei16>;
367def store_local_hi16 : StoreHi16 <truncstorei16>, LocalAddress;
368def truncstorei8_local_hi16 : StoreHi16<truncstorei8>, LocalAddress;
369
370def load_align8_local : Aligned8Bytes <
371  (ops node:$ptr), (load_local node:$ptr)
372>;
373
374def store_align8_local : Aligned8Bytes <
375  (ops node:$val, node:$ptr), (store_local node:$val, node:$ptr)
376>;
377
378
379def load_flat          : FlatLoad <load>;
380def az_extloadi8_flat  : FlatLoad <az_extloadi8>;
381def sextloadi8_flat    : FlatLoad <sextloadi8>;
382def az_extloadi16_flat : FlatLoad <az_extloadi16>;
383def sextloadi16_flat   : FlatLoad <sextloadi16>;
384def atomic_load_flat   : FlatLoad<atomic_load>;
385
386def store_flat         : FlatStore <store>;
387def truncstorei8_flat  : FlatStore <truncstorei8>;
388def truncstorei16_flat : FlatStore <truncstorei16>;
389def atomic_store_flat  : FlatStore <atomic_store>;
390def truncstorei8_hi16_flat  : StoreHi16<truncstorei8>, FlatStoreAddress;
391def truncstorei16_hi16_flat : StoreHi16<truncstorei16>, FlatStoreAddress;
392
393
394def constant_load : ConstantLoad<load>;
395def sextloadi8_constant : ConstantLoad <sextloadi8>;
396def az_extloadi8_constant : ConstantLoad <az_extloadi8>;
397def sextloadi16_constant : ConstantLoad <sextloadi16>;
398def az_extloadi16_constant : ConstantLoad <az_extloadi16>;
399
400
401class local_binary_atomic_op<SDNode atomic_op> :
402  PatFrag<(ops node:$ptr, node:$value),
403    (atomic_op node:$ptr, node:$value), [{
404  return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUASI.LOCAL_ADDRESS;
405}]>;
406
407def atomic_swap_local : local_binary_atomic_op<atomic_swap>;
408def atomic_load_add_local : local_binary_atomic_op<atomic_load_add>;
409def atomic_load_sub_local : local_binary_atomic_op<atomic_load_sub>;
410def atomic_load_and_local : local_binary_atomic_op<atomic_load_and>;
411def atomic_load_or_local : local_binary_atomic_op<atomic_load_or>;
412def atomic_load_xor_local : local_binary_atomic_op<atomic_load_xor>;
413def atomic_load_nand_local : local_binary_atomic_op<atomic_load_nand>;
414def atomic_load_min_local : local_binary_atomic_op<atomic_load_min>;
415def atomic_load_max_local : local_binary_atomic_op<atomic_load_max>;
416def atomic_load_umin_local : local_binary_atomic_op<atomic_load_umin>;
417def atomic_load_umax_local : local_binary_atomic_op<atomic_load_umax>;
418
419def mskor_global : PatFrag<(ops node:$val, node:$ptr),
420                            (AMDGPUstore_mskor node:$val, node:$ptr), [{
421  return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUASI.GLOBAL_ADDRESS;
422}]>;
423
424class AtomicCmpSwapLocal <SDNode cmp_swap_node> : PatFrag<
425    (ops node:$ptr, node:$cmp, node:$swap),
426    (cmp_swap_node node:$ptr, node:$cmp, node:$swap), [{
427      AtomicSDNode *AN = cast<AtomicSDNode>(N);
428      return AN->getAddressSpace() == AMDGPUASI.LOCAL_ADDRESS;
429}]>;
430
431def atomic_cmp_swap_local : AtomicCmpSwapLocal <atomic_cmp_swap>;
432
433multiclass global_binary_atomic_op<SDNode atomic_op> {
434  def "" : PatFrag<
435        (ops node:$ptr, node:$value),
436        (atomic_op node:$ptr, node:$value),
437        [{return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUASI.GLOBAL_ADDRESS;}]>;
438
439  def _noret : PatFrag<
440        (ops node:$ptr, node:$value),
441        (atomic_op node:$ptr, node:$value),
442        [{return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUASI.GLOBAL_ADDRESS && (SDValue(N, 0).use_empty());}]>;
443
444  def _ret : PatFrag<
445        (ops node:$ptr, node:$value),
446        (atomic_op node:$ptr, node:$value),
447        [{return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUASI.GLOBAL_ADDRESS && (!SDValue(N, 0).use_empty());}]>;
448}
449
450defm atomic_swap_global : global_binary_atomic_op<atomic_swap>;
451defm atomic_add_global : global_binary_atomic_op<atomic_load_add>;
452defm atomic_and_global : global_binary_atomic_op<atomic_load_and>;
453defm atomic_max_global : global_binary_atomic_op<atomic_load_max>;
454defm atomic_min_global : global_binary_atomic_op<atomic_load_min>;
455defm atomic_or_global : global_binary_atomic_op<atomic_load_or>;
456defm atomic_sub_global : global_binary_atomic_op<atomic_load_sub>;
457defm atomic_umax_global : global_binary_atomic_op<atomic_load_umax>;
458defm atomic_umin_global : global_binary_atomic_op<atomic_load_umin>;
459defm atomic_xor_global : global_binary_atomic_op<atomic_load_xor>;
460
461// Legacy.
462def AMDGPUatomic_cmp_swap_global : PatFrag<
463  (ops node:$ptr, node:$value),
464  (AMDGPUatomic_cmp_swap node:$ptr, node:$value)>, GlobalAddress;
465
466def atomic_cmp_swap_global : PatFrag<
467  (ops node:$ptr, node:$cmp, node:$value),
468  (atomic_cmp_swap node:$ptr, node:$cmp, node:$value)>, GlobalAddress;
469
470
471def atomic_cmp_swap_global_noret : PatFrag<
472  (ops node:$ptr, node:$cmp, node:$value),
473  (atomic_cmp_swap node:$ptr, node:$cmp, node:$value),
474  [{return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUASI.GLOBAL_ADDRESS && (SDValue(N, 0).use_empty());}]>;
475
476def atomic_cmp_swap_global_ret : PatFrag<
477  (ops node:$ptr, node:$cmp, node:$value),
478  (atomic_cmp_swap node:$ptr, node:$cmp, node:$value),
479  [{return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUASI.GLOBAL_ADDRESS && (!SDValue(N, 0).use_empty());}]>;
480
481//===----------------------------------------------------------------------===//
482// Misc Pattern Fragments
483//===----------------------------------------------------------------------===//
484
485class Constants {
486int TWO_PI = 0x40c90fdb;
487int PI = 0x40490fdb;
488int TWO_PI_INV = 0x3e22f983;
489int FP_UINT_MAX_PLUS_1 = 0x4f800000;    // 1 << 32 in floating point encoding
490int FP16_ONE = 0x3C00;
491int V2FP16_ONE = 0x3C003C00;
492int FP32_ONE = 0x3f800000;
493int FP32_NEG_ONE = 0xbf800000;
494int FP64_ONE = 0x3ff0000000000000;
495int FP64_NEG_ONE = 0xbff0000000000000;
496}
497def CONST : Constants;
498
499def FP_ZERO : PatLeaf <
500  (fpimm),
501  [{return N->getValueAPF().isZero();}]
502>;
503
504def FP_ONE : PatLeaf <
505  (fpimm),
506  [{return N->isExactlyValue(1.0);}]
507>;
508
509def FP_HALF : PatLeaf <
510  (fpimm),
511  [{return N->isExactlyValue(0.5);}]
512>;
513
514/* Generic helper patterns for intrinsics */
515/* -------------------------------------- */
516
517class POW_Common <AMDGPUInst log_ieee, AMDGPUInst exp_ieee, AMDGPUInst mul>
518  : AMDGPUPat <
519  (fpow f32:$src0, f32:$src1),
520  (exp_ieee (mul f32:$src1, (log_ieee f32:$src0)))
521>;
522
523/* Other helper patterns */
524/* --------------------- */
525
526/* Extract element pattern */
527class Extract_Element <ValueType sub_type, ValueType vec_type, int sub_idx,
528                       SubRegIndex sub_reg>
529  : AMDGPUPat<
530  (sub_type (extractelt vec_type:$src, sub_idx)),
531  (EXTRACT_SUBREG $src, sub_reg)
532> {
533  let SubtargetPredicate = TruePredicate;
534}
535
536/* Insert element pattern */
537class Insert_Element <ValueType elem_type, ValueType vec_type,
538                      int sub_idx, SubRegIndex sub_reg>
539  : AMDGPUPat <
540  (insertelt vec_type:$vec, elem_type:$elem, sub_idx),
541  (INSERT_SUBREG $vec, $elem, sub_reg)
542> {
543  let SubtargetPredicate = TruePredicate;
544}
545
546// XXX: Convert to new syntax and use COPY_TO_REG, once the DFAPacketizer
547// can handle COPY instructions.
548// bitconvert pattern
549class BitConvert <ValueType dt, ValueType st, RegisterClass rc> : AMDGPUPat <
550  (dt (bitconvert (st rc:$src0))),
551  (dt rc:$src0)
552>;
553
554// XXX: Convert to new syntax and use COPY_TO_REG, once the DFAPacketizer
555// can handle COPY instructions.
556class DwordAddrPat<ValueType vt, RegisterClass rc> : AMDGPUPat <
557  (vt (AMDGPUdwordaddr (vt rc:$addr))),
558  (vt rc:$addr)
559>;
560
561// BFI_INT patterns
562
563multiclass BFIPatterns <Instruction BFI_INT,
564                        Instruction LoadImm32,
565                        RegisterClass RC64> {
566  // Definition from ISA doc:
567  // (y & x) | (z & ~x)
568  def : AMDGPUPat <
569    (or (and i32:$y, i32:$x), (and i32:$z, (not i32:$x))),
570    (BFI_INT $x, $y, $z)
571  >;
572
573  // 64-bit version
574  def : AMDGPUPat <
575    (or (and i64:$y, i64:$x), (and i64:$z, (not i64:$x))),
576    (REG_SEQUENCE RC64,
577      (BFI_INT (i32 (EXTRACT_SUBREG $x, sub0)),
578               (i32 (EXTRACT_SUBREG $y, sub0)),
579               (i32 (EXTRACT_SUBREG $z, sub0))), sub0,
580      (BFI_INT (i32 (EXTRACT_SUBREG $x, sub1)),
581               (i32 (EXTRACT_SUBREG $y, sub1)),
582               (i32 (EXTRACT_SUBREG $z, sub1))), sub1)
583  >;
584
585  // SHA-256 Ch function
586  // z ^ (x & (y ^ z))
587  def : AMDGPUPat <
588    (xor i32:$z, (and i32:$x, (xor i32:$y, i32:$z))),
589    (BFI_INT $x, $y, $z)
590  >;
591
592  // 64-bit version
593  def : AMDGPUPat <
594    (xor i64:$z, (and i64:$x, (xor i64:$y, i64:$z))),
595    (REG_SEQUENCE RC64,
596      (BFI_INT (i32 (EXTRACT_SUBREG $x, sub0)),
597               (i32 (EXTRACT_SUBREG $y, sub0)),
598               (i32 (EXTRACT_SUBREG $z, sub0))), sub0,
599      (BFI_INT (i32 (EXTRACT_SUBREG $x, sub1)),
600               (i32 (EXTRACT_SUBREG $y, sub1)),
601               (i32 (EXTRACT_SUBREG $z, sub1))), sub1)
602  >;
603
604  def : AMDGPUPat <
605    (fcopysign f32:$src0, f32:$src1),
606    (BFI_INT (LoadImm32 (i32 0x7fffffff)), $src0, $src1)
607  >;
608
609  def : AMDGPUPat <
610    (f32 (fcopysign f32:$src0, f64:$src1)),
611    (BFI_INT (LoadImm32 (i32 0x7fffffff)), $src0,
612             (i32 (EXTRACT_SUBREG $src1, sub1)))
613  >;
614
615  def : AMDGPUPat <
616    (f64 (fcopysign f64:$src0, f64:$src1)),
617    (REG_SEQUENCE RC64,
618      (i32 (EXTRACT_SUBREG $src0, sub0)), sub0,
619      (BFI_INT (LoadImm32 (i32 0x7fffffff)),
620               (i32 (EXTRACT_SUBREG $src0, sub1)),
621               (i32 (EXTRACT_SUBREG $src1, sub1))), sub1)
622  >;
623
624  def : AMDGPUPat <
625    (f64 (fcopysign f64:$src0, f32:$src1)),
626    (REG_SEQUENCE RC64,
627      (i32 (EXTRACT_SUBREG $src0, sub0)), sub0,
628      (BFI_INT (LoadImm32 (i32 0x7fffffff)),
629               (i32 (EXTRACT_SUBREG $src0, sub1)),
630               $src1), sub1)
631  >;
632}
633
634// SHA-256 Ma patterns
635
636// ((x & z) | (y & (x | z))) -> BFI_INT (XOR x, y), z, y
637multiclass SHA256MaPattern <Instruction BFI_INT, Instruction XOR, RegisterClass RC64> {
638  def : AMDGPUPat <
639    (or (and i32:$x, i32:$z), (and i32:$y, (or i32:$x, i32:$z))),
640    (BFI_INT (XOR i32:$x, i32:$y), i32:$z, i32:$y)
641  >;
642
643  def : AMDGPUPat <
644    (or (and i64:$x, i64:$z), (and i64:$y, (or i64:$x, i64:$z))),
645    (REG_SEQUENCE RC64,
646      (BFI_INT (XOR (i32 (EXTRACT_SUBREG $x, sub0)),
647                    (i32 (EXTRACT_SUBREG $y, sub0))),
648               (i32 (EXTRACT_SUBREG $z, sub0)),
649               (i32 (EXTRACT_SUBREG $y, sub0))), sub0,
650      (BFI_INT (XOR (i32 (EXTRACT_SUBREG $x, sub1)),
651                    (i32 (EXTRACT_SUBREG $y, sub1))),
652               (i32 (EXTRACT_SUBREG $z, sub1)),
653               (i32 (EXTRACT_SUBREG $y, sub1))), sub1)
654  >;
655}
656
657// Bitfield extract patterns
658
659def IMMZeroBasedBitfieldMask : PatLeaf <(imm), [{
660  return isMask_32(N->getZExtValue());
661}]>;
662
663def IMMPopCount : SDNodeXForm<imm, [{
664  return CurDAG->getTargetConstant(countPopulation(N->getZExtValue()), SDLoc(N),
665                                   MVT::i32);
666}]>;
667
668multiclass BFEPattern <Instruction UBFE, Instruction SBFE, Instruction MOV> {
669  def : AMDGPUPat <
670    (i32 (and (i32 (srl i32:$src, i32:$rshift)), IMMZeroBasedBitfieldMask:$mask)),
671    (UBFE $src, $rshift, (MOV (i32 (IMMPopCount $mask))))
672  >;
673
674  def : AMDGPUPat <
675    (srl (shl_oneuse i32:$src, (sub 32, i32:$width)), (sub 32, i32:$width)),
676    (UBFE $src, (i32 0), $width)
677  >;
678
679  def : AMDGPUPat <
680    (sra (shl_oneuse i32:$src, (sub 32, i32:$width)), (sub 32, i32:$width)),
681    (SBFE $src, (i32 0), $width)
682  >;
683}
684
685// rotr pattern
686class ROTRPattern <Instruction BIT_ALIGN> : AMDGPUPat <
687  (rotr i32:$src0, i32:$src1),
688  (BIT_ALIGN $src0, $src0, $src1)
689>;
690
691// This matches 16 permutations of
692// max(min(x, y), min(max(x, y), z))
693class IntMed3Pat<Instruction med3Inst,
694                 SDPatternOperator max,
695                 SDPatternOperator max_oneuse,
696                 SDPatternOperator min_oneuse,
697                 ValueType vt = i32> : AMDGPUPat<
698  (max (min_oneuse vt:$src0, vt:$src1),
699       (min_oneuse (max_oneuse vt:$src0, vt:$src1), vt:$src2)),
700  (med3Inst $src0, $src1, $src2)
701>;
702
703// Special conversion patterns
704
705def cvt_rpi_i32_f32 : PatFrag <
706  (ops node:$src),
707  (fp_to_sint (ffloor (fadd $src, FP_HALF))),
708  [{ (void) N; return TM.Options.NoNaNsFPMath; }]
709>;
710
711def cvt_flr_i32_f32 : PatFrag <
712  (ops node:$src),
713  (fp_to_sint (ffloor $src)),
714  [{ (void)N; return TM.Options.NoNaNsFPMath; }]
715>;
716
717class IMad24Pat<Instruction Inst, bit HasClamp = 0> : AMDGPUPat <
718  (add (AMDGPUmul_i24 i32:$src0, i32:$src1), i32:$src2),
719  !if(HasClamp, (Inst $src0, $src1, $src2, (i1 0)),
720                (Inst $src0, $src1, $src2))
721>;
722
723class UMad24Pat<Instruction Inst, bit HasClamp = 0> : AMDGPUPat <
724  (add (AMDGPUmul_u24 i32:$src0, i32:$src1), i32:$src2),
725  !if(HasClamp, (Inst $src0, $src1, $src2, (i1 0)),
726                (Inst $src0, $src1, $src2))
727>;
728
729class RcpPat<Instruction RcpInst, ValueType vt> : AMDGPUPat <
730  (fdiv FP_ONE, vt:$src),
731  (RcpInst $src)
732>;
733
734class RsqPat<Instruction RsqInst, ValueType vt> : AMDGPUPat <
735  (AMDGPUrcp (fsqrt vt:$src)),
736  (RsqInst $src)
737>;
738
739include "R600Instructions.td"
740include "R700Instructions.td"
741include "EvergreenInstructions.td"
742include "CaymanInstructions.td"
743
744include "SIInstrInfo.td"
745
746