1//===-- SIInstructions.td - SI Instruction Defintions ---------------------===//
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// This file was originally auto-generated from a GPU register header file and
10// all the instruction definitions were originally commented out.  Instructions
11// that are not yet supported remain commented out.
12//===----------------------------------------------------------------------===//
13
14class GCNPat<dag pattern, dag result> : Pat<pattern, result>, GCNPredicateControl {
15  let SubtargetPredicate = isGCN;
16}
17
18include "VOPInstructions.td"
19include "SOPInstructions.td"
20include "SMInstructions.td"
21include "FLATInstructions.td"
22include "BUFInstructions.td"
23
24//===----------------------------------------------------------------------===//
25// EXP Instructions
26//===----------------------------------------------------------------------===//
27
28defm EXP : EXP_m<0, AMDGPUexport>;
29defm EXP_DONE : EXP_m<1, AMDGPUexport_done>;
30
31//===----------------------------------------------------------------------===//
32// VINTRP Instructions
33//===----------------------------------------------------------------------===//
34
35// Used to inject printing of "_e32" suffix for VI (there are "_e64" variants for VI)
36def VINTRPDst : VINTRPDstOperand <VGPR_32>;
37
38let Uses = [M0, EXEC] in {
39
40// FIXME: Specify SchedRW for VINTRP insturctions.
41
42multiclass V_INTERP_P1_F32_m : VINTRP_m <
43  0x00000000,
44  (outs VINTRPDst:$vdst),
45  (ins VGPR_32:$vsrc, Attr:$attr, AttrChan:$attrchan),
46  "v_interp_p1_f32$vdst, $vsrc, $attr$attrchan",
47  [(set f32:$vdst, (AMDGPUinterp_p1 f32:$vsrc, (i32 imm:$attrchan),
48                                               (i32 imm:$attr)))]
49>;
50
51let OtherPredicates = [has32BankLDS] in {
52
53defm V_INTERP_P1_F32 : V_INTERP_P1_F32_m;
54
55} // End OtherPredicates = [has32BankLDS]
56
57let OtherPredicates = [has16BankLDS], Constraints = "@earlyclobber $vdst", isAsmParserOnly=1 in {
58
59defm V_INTERP_P1_F32_16bank : V_INTERP_P1_F32_m;
60
61} // End OtherPredicates = [has32BankLDS], Constraints = "@earlyclobber $vdst", isAsmParserOnly=1
62
63let DisableEncoding = "$src0", Constraints = "$src0 = $vdst" in {
64
65defm V_INTERP_P2_F32 : VINTRP_m <
66  0x00000001,
67  (outs VINTRPDst:$vdst),
68  (ins VGPR_32:$src0, VGPR_32:$vsrc, Attr:$attr, AttrChan:$attrchan),
69  "v_interp_p2_f32$vdst, $vsrc, $attr$attrchan",
70  [(set f32:$vdst, (AMDGPUinterp_p2 f32:$src0, f32:$vsrc, (i32 imm:$attrchan),
71                                                          (i32 imm:$attr)))]>;
72
73} // End DisableEncoding = "$src0", Constraints = "$src0 = $vdst"
74
75defm V_INTERP_MOV_F32 : VINTRP_m <
76  0x00000002,
77  (outs VINTRPDst:$vdst),
78  (ins InterpSlot:$vsrc, Attr:$attr, AttrChan:$attrchan),
79  "v_interp_mov_f32$vdst, $vsrc, $attr$attrchan",
80  [(set f32:$vdst, (AMDGPUinterp_mov (i32 imm:$vsrc), (i32 imm:$attrchan),
81                                     (i32 imm:$attr)))]>;
82
83} // End Uses = [M0, EXEC]
84
85//===----------------------------------------------------------------------===//
86// Pseudo Instructions
87//===----------------------------------------------------------------------===//
88def ATOMIC_FENCE : SPseudoInstSI<
89  (outs), (ins i32imm:$ordering, i32imm:$scope),
90  [(atomic_fence (i32 imm:$ordering), (i32 imm:$scope))],
91  "ATOMIC_FENCE $ordering, $scope"> {
92  let hasSideEffects = 1;
93  let maybeAtomic = 1;
94}
95
96let hasSideEffects = 0, mayLoad = 0, mayStore = 0, Uses = [EXEC] in {
97
98// For use in patterns
99def V_CNDMASK_B64_PSEUDO : VOP3Common <(outs VReg_64:$vdst),
100  (ins VSrc_b64:$src0, VSrc_b64:$src1, SSrc_b64:$src2), "", []> {
101  let isPseudo = 1;
102  let isCodeGenOnly = 1;
103  let usesCustomInserter = 1;
104}
105
106// 64-bit vector move instruction. This is mainly used by the
107// SIFoldOperands pass to enable folding of inline immediates.
108def V_MOV_B64_PSEUDO : VPseudoInstSI <(outs VReg_64:$vdst),
109                                      (ins VSrc_b64:$src0)>;
110
111// Pseudoinstruction for @llvm.amdgcn.wqm. It is turned into a copy after the
112// WQM pass processes it.
113def WQM : PseudoInstSI <(outs unknown:$vdst), (ins unknown:$src0)>;
114
115// Pseudoinstruction for @llvm.amdgcn.wwm. It is turned into a copy post-RA, so
116// that the @earlyclobber is respected. The @earlyclobber is to make sure that
117// the instruction that defines $src0 (which is run in WWM) doesn't
118// accidentally clobber inactive channels of $vdst.
119let Constraints = "@earlyclobber $vdst" in {
120def WWM : PseudoInstSI <(outs unknown:$vdst), (ins unknown:$src0)>;
121}
122
123} // End let hasSideEffects = 0, mayLoad = 0, mayStore = 0, Uses = [EXEC]
124
125def EXIT_WWM : SPseudoInstSI <(outs SReg_64:$sdst), (ins SReg_64:$src0)> {
126  let hasSideEffects = 0;
127  let mayLoad = 0;
128  let mayStore = 0;
129}
130
131// Invert the exec mask and overwrite the inactive lanes of dst with inactive,
132// restoring it after we're done.
133def V_SET_INACTIVE_B32 : VPseudoInstSI <(outs VGPR_32:$vdst),
134  (ins VGPR_32: $src, VSrc_b32:$inactive),
135  [(set i32:$vdst, (int_amdgcn_set_inactive i32:$src, i32:$inactive))]> {
136  let Constraints = "$src = $vdst";
137}
138
139def V_SET_INACTIVE_B64 : VPseudoInstSI <(outs VReg_64:$vdst),
140  (ins VReg_64: $src, VSrc_b64:$inactive),
141  [(set i64:$vdst, (int_amdgcn_set_inactive i64:$src, i64:$inactive))]> {
142  let Constraints = "$src = $vdst";
143}
144
145
146let usesCustomInserter = 1, Defs = [SCC] in {
147def S_ADD_U64_PSEUDO : SPseudoInstSI <
148  (outs SReg_64:$vdst), (ins SSrc_b64:$src0, SSrc_b64:$src1),
149  [(set SReg_64:$vdst, (add i64:$src0, i64:$src1))]
150>;
151
152def S_SUB_U64_PSEUDO : SPseudoInstSI <
153  (outs SReg_64:$vdst), (ins SSrc_b64:$src0, SSrc_b64:$src1),
154  [(set SReg_64:$vdst, (sub i64:$src0, i64:$src1))]
155>;
156
157def S_ADD_U64_CO_PSEUDO : SPseudoInstSI <
158  (outs SReg_64:$vdst, VOPDstS64:$sdst), (ins SSrc_b64:$src0, SSrc_b64:$src1)
159>;
160
161def S_SUB_U64_CO_PSEUDO : SPseudoInstSI <
162  (outs SReg_64:$vdst, VOPDstS64:$sdst), (ins SSrc_b64:$src0, SSrc_b64:$src1)
163>;
164
165} // End usesCustomInserter = 1, Defs = [SCC]
166
167let usesCustomInserter = 1, SALU = 1 in {
168def GET_GROUPSTATICSIZE : PseudoInstSI <(outs SReg_32:$sdst), (ins),
169  [(set SReg_32:$sdst, (int_amdgcn_groupstaticsize))]>;
170} // End let usesCustomInserter = 1, SALU = 1
171
172def S_MOV_B64_term : PseudoInstSI<(outs SReg_64:$dst),
173   (ins SSrc_b64:$src0)> {
174  let SALU = 1;
175  let isAsCheapAsAMove = 1;
176  let isTerminator = 1;
177}
178
179def S_XOR_B64_term : PseudoInstSI<(outs SReg_64:$dst),
180   (ins SSrc_b64:$src0, SSrc_b64:$src1)> {
181  let SALU = 1;
182  let isAsCheapAsAMove = 1;
183  let isTerminator = 1;
184  let Defs = [SCC];
185}
186
187def S_ANDN2_B64_term : PseudoInstSI<(outs SReg_64:$dst),
188   (ins SSrc_b64:$src0, SSrc_b64:$src1)> {
189  let SALU = 1;
190  let isAsCheapAsAMove = 1;
191  let isTerminator = 1;
192}
193
194def WAVE_BARRIER : SPseudoInstSI<(outs), (ins),
195  [(int_amdgcn_wave_barrier)]> {
196  let SchedRW = [];
197  let hasNoSchedulingInfo = 1;
198  let hasSideEffects = 1;
199  let mayLoad = 1;
200  let mayStore = 1;
201  let isBarrier = 1;
202  let isConvergent = 1;
203  let FixedSize = 1;
204  let Size = 0;
205}
206
207// SI pseudo instructions. These are used by the CFG structurizer pass
208// and should be lowered to ISA instructions prior to codegen.
209
210// Dummy terminator instruction to use after control flow instructions
211// replaced with exec mask operations.
212def SI_MASK_BRANCH : VPseudoInstSI <
213  (outs), (ins brtarget:$target)> {
214  let isBranch = 0;
215  let isTerminator = 1;
216  let isBarrier = 0;
217  let SchedRW = [];
218  let hasNoSchedulingInfo = 1;
219  let FixedSize = 1;
220  let Size = 0;
221}
222
223let isTerminator = 1 in {
224
225let OtherPredicates = [EnableLateCFGStructurize] in {
226 def SI_NON_UNIFORM_BRCOND_PSEUDO : CFPseudoInstSI <
227  (outs),
228  (ins SReg_64:$vcc, brtarget:$target),
229  [(brcond i1:$vcc, bb:$target)]> {
230    let Size = 12;
231}
232}
233
234def SI_IF: CFPseudoInstSI <
235  (outs SReg_64:$dst), (ins SReg_64:$vcc, brtarget:$target),
236  [(set i64:$dst, (AMDGPUif i1:$vcc, bb:$target))], 1, 1> {
237  let Constraints = "";
238  let Size = 12;
239  let hasSideEffects = 1;
240}
241
242def SI_ELSE : CFPseudoInstSI <
243  (outs SReg_64:$dst),
244  (ins SReg_64:$src, brtarget:$target, i1imm:$execfix), [], 1, 1> {
245  let Size = 12;
246  let hasSideEffects = 1;
247}
248
249def SI_LOOP : CFPseudoInstSI <
250  (outs), (ins SReg_64:$saved, brtarget:$target),
251  [(AMDGPUloop i64:$saved, bb:$target)], 1, 1> {
252  let Size = 8;
253  let isBranch = 0;
254  let hasSideEffects = 1;
255}
256
257} // End isTerminator = 1
258
259def SI_END_CF : CFPseudoInstSI <
260  (outs), (ins SReg_64:$saved),
261  [(int_amdgcn_end_cf i64:$saved)], 1, 1> {
262  let Size = 4;
263  let isAsCheapAsAMove = 1;
264  let isReMaterializable = 1;
265  let hasSideEffects = 1;
266  let mayLoad = 1; // FIXME: Should not need memory flags
267  let mayStore = 1;
268}
269
270def SI_BREAK : CFPseudoInstSI <
271  (outs SReg_64:$dst), (ins SReg_64:$src),
272  [(set i64:$dst, (int_amdgcn_break i64:$src))], 1> {
273  let Size = 4;
274  let isAsCheapAsAMove = 1;
275  let isReMaterializable = 1;
276}
277
278def SI_IF_BREAK : CFPseudoInstSI <
279  (outs SReg_64:$dst), (ins SReg_64:$vcc, SReg_64:$src),
280  [(set i64:$dst, (int_amdgcn_if_break i1:$vcc, i64:$src))]> {
281  let Size = 4;
282  let isAsCheapAsAMove = 1;
283  let isReMaterializable = 1;
284}
285
286def SI_ELSE_BREAK : CFPseudoInstSI <
287  (outs SReg_64:$dst), (ins SReg_64:$src0, SReg_64:$src1),
288  [(set i64:$dst, (int_amdgcn_else_break i64:$src0, i64:$src1))]> {
289  let Size = 4;
290  let isAsCheapAsAMove = 1;
291  let isReMaterializable = 1;
292}
293
294let Uses = [EXEC] in {
295
296multiclass PseudoInstKill <dag ins> {
297  // Even though this pseudo can usually be expanded without an SCC def, we
298  // conservatively assume that it has an SCC def, both because it is sometimes
299  // required in degenerate cases (when V_CMPX cannot be used due to constant
300  // bus limitations) and because it allows us to avoid having to track SCC
301  // liveness across basic blocks.
302  let Defs = [EXEC,VCC,SCC] in
303  def _PSEUDO : PseudoInstSI <(outs), ins> {
304    let isConvergent = 1;
305    let usesCustomInserter = 1;
306  }
307
308  let Defs = [EXEC,VCC,SCC] in
309  def _TERMINATOR : SPseudoInstSI <(outs), ins> {
310    let isTerminator = 1;
311  }
312}
313
314defm SI_KILL_I1 : PseudoInstKill <(ins SSrc_b64:$src, i1imm:$killvalue)>;
315defm SI_KILL_F32_COND_IMM : PseudoInstKill <(ins VSrc_b32:$src0, i32imm:$src1, i32imm:$cond)>;
316
317let Defs = [EXEC,VCC] in
318def SI_ILLEGAL_COPY : SPseudoInstSI <
319  (outs unknown:$dst), (ins unknown:$src),
320  [], " ; illegal copy $src to $dst">;
321
322} // End Uses = [EXEC], Defs = [EXEC,VCC]
323
324// Branch on undef scc. Used to avoid intermediate copy from
325// IMPLICIT_DEF to SCC.
326def SI_BR_UNDEF : SPseudoInstSI <(outs), (ins sopp_brtarget:$simm16)> {
327  let isTerminator = 1;
328  let usesCustomInserter = 1;
329}
330
331def SI_PS_LIVE : PseudoInstSI <
332  (outs SReg_64:$dst), (ins),
333  [(set i1:$dst, (int_amdgcn_ps_live))]> {
334  let SALU = 1;
335}
336
337def SI_MASKED_UNREACHABLE : SPseudoInstSI <(outs), (ins),
338  [(int_amdgcn_unreachable)],
339  "; divergent unreachable"> {
340  let Size = 0;
341  let hasNoSchedulingInfo = 1;
342  let FixedSize = 1;
343}
344
345// Used as an isel pseudo to directly emit initialization with an
346// s_mov_b32 rather than a copy of another initialized
347// register. MachineCSE skips copies, and we don't want to have to
348// fold operands before it runs.
349def SI_INIT_M0 : SPseudoInstSI <(outs), (ins SSrc_b32:$src)> {
350  let Defs = [M0];
351  let usesCustomInserter = 1;
352  let isAsCheapAsAMove = 1;
353  let isReMaterializable = 1;
354}
355
356def SI_INIT_EXEC : SPseudoInstSI <
357  (outs), (ins i64imm:$src), []> {
358  let Defs = [EXEC];
359  let usesCustomInserter = 1;
360  let isAsCheapAsAMove = 1;
361}
362
363def SI_INIT_EXEC_FROM_INPUT : SPseudoInstSI <
364  (outs), (ins SSrc_b32:$input, i32imm:$shift), []> {
365  let Defs = [EXEC];
366  let usesCustomInserter = 1;
367}
368
369// Return for returning shaders to a shader variant epilog.
370def SI_RETURN_TO_EPILOG : SPseudoInstSI <
371  (outs), (ins variable_ops), [(AMDGPUreturn_to_epilog)]> {
372  let isTerminator = 1;
373  let isBarrier = 1;
374  let isReturn = 1;
375  let hasNoSchedulingInfo = 1;
376  let DisableWQM = 1;
377  let FixedSize = 1;
378}
379
380// Return for returning function calls.
381def SI_RETURN : SPseudoInstSI <
382  (outs), (ins), [],
383  "; return"> {
384  let isTerminator = 1;
385  let isBarrier = 1;
386  let isReturn = 1;
387  let SchedRW = [WriteBranch];
388}
389
390// Return for returning function calls without output register.
391//
392// This version is only needed so we can fill in the output regiter in
393// the custom inserter.
394def SI_CALL_ISEL : SPseudoInstSI <
395  (outs), (ins SSrc_b64:$src0), [(AMDGPUcall i64:$src0)]> {
396  let Size = 4;
397  let isCall = 1;
398  let SchedRW = [WriteBranch];
399  let usesCustomInserter = 1;
400}
401
402// Wrapper around s_swappc_b64 with extra $callee parameter to track
403// the called function after regalloc.
404def SI_CALL : SPseudoInstSI <
405  (outs SReg_64:$dst), (ins SSrc_b64:$src0, unknown:$callee)> {
406  let Size = 4;
407  let isCall = 1;
408  let UseNamedOperandTable = 1;
409  let SchedRW = [WriteBranch];
410}
411
412// Tail call handling pseudo
413def SI_TCRETURN_ISEL : SPseudoInstSI<(outs),
414  (ins SSrc_b64:$src0, i32imm:$fpdiff),
415  [(AMDGPUtc_return i64:$src0, i32:$fpdiff)]> {
416  let isCall = 1;
417  let isTerminator = 1;
418  let isReturn = 1;
419  let isBarrier = 1;
420  let SchedRW = [WriteBranch];
421  let usesCustomInserter = 1;
422}
423
424def SI_TCRETURN : SPseudoInstSI <
425  (outs),
426  (ins SSrc_b64:$src0, unknown:$callee, i32imm:$fpdiff)> {
427  let Size = 4;
428  let isCall = 1;
429  let isTerminator = 1;
430  let isReturn = 1;
431  let isBarrier = 1;
432  let UseNamedOperandTable = 1;
433  let SchedRW = [WriteBranch];
434}
435
436
437def ADJCALLSTACKUP : SPseudoInstSI<
438  (outs), (ins i32imm:$amt0, i32imm:$amt1),
439  [(callseq_start timm:$amt0, timm:$amt1)],
440  "; adjcallstackup $amt0 $amt1"> {
441  let Size = 8; // Worst case. (s_add_u32 + constant)
442  let FixedSize = 1;
443  let hasSideEffects = 1;
444  let usesCustomInserter = 1;
445}
446
447def ADJCALLSTACKDOWN : SPseudoInstSI<
448  (outs), (ins i32imm:$amt1, i32imm:$amt2),
449  [(callseq_end timm:$amt1, timm:$amt2)],
450  "; adjcallstackdown $amt1"> {
451  let Size = 8; // Worst case. (s_add_u32 + constant)
452  let hasSideEffects = 1;
453  let usesCustomInserter = 1;
454}
455
456let Defs = [M0, EXEC, SCC],
457  UseNamedOperandTable = 1 in {
458
459class SI_INDIRECT_SRC<RegisterClass rc> : VPseudoInstSI <
460  (outs VGPR_32:$vdst),
461  (ins rc:$src, VS_32:$idx, i32imm:$offset)> {
462  let usesCustomInserter = 1;
463}
464
465class SI_INDIRECT_DST<RegisterClass rc> : VPseudoInstSI <
466  (outs rc:$vdst),
467  (ins rc:$src, VS_32:$idx, i32imm:$offset, VGPR_32:$val)> {
468  let Constraints = "$src = $vdst";
469  let usesCustomInserter = 1;
470}
471
472// TODO: We can support indirect SGPR access.
473def SI_INDIRECT_SRC_V1 : SI_INDIRECT_SRC<VGPR_32>;
474def SI_INDIRECT_SRC_V2 : SI_INDIRECT_SRC<VReg_64>;
475def SI_INDIRECT_SRC_V4 : SI_INDIRECT_SRC<VReg_128>;
476def SI_INDIRECT_SRC_V8 : SI_INDIRECT_SRC<VReg_256>;
477def SI_INDIRECT_SRC_V16 : SI_INDIRECT_SRC<VReg_512>;
478
479def SI_INDIRECT_DST_V1 : SI_INDIRECT_DST<VGPR_32>;
480def SI_INDIRECT_DST_V2 : SI_INDIRECT_DST<VReg_64>;
481def SI_INDIRECT_DST_V4 : SI_INDIRECT_DST<VReg_128>;
482def SI_INDIRECT_DST_V8 : SI_INDIRECT_DST<VReg_256>;
483def SI_INDIRECT_DST_V16 : SI_INDIRECT_DST<VReg_512>;
484
485} // End Uses = [EXEC], Defs = [M0, EXEC]
486
487multiclass SI_SPILL_SGPR <RegisterClass sgpr_class> {
488  let UseNamedOperandTable = 1, SGPRSpill = 1, Uses = [EXEC] in {
489    def _SAVE : PseudoInstSI <
490      (outs),
491      (ins sgpr_class:$data, i32imm:$addr)> {
492      let mayStore = 1;
493      let mayLoad = 0;
494    }
495
496    def _RESTORE : PseudoInstSI <
497      (outs sgpr_class:$data),
498      (ins i32imm:$addr)> {
499      let mayStore = 0;
500      let mayLoad = 1;
501    }
502  } // End UseNamedOperandTable = 1
503}
504
505// You cannot use M0 as the output of v_readlane_b32 instructions or
506// use it in the sdata operand of SMEM instructions. We still need to
507// be able to spill the physical register m0, so allow it for
508// SI_SPILL_32_* instructions.
509defm SI_SPILL_S32  : SI_SPILL_SGPR <SReg_32>;
510defm SI_SPILL_S64  : SI_SPILL_SGPR <SReg_64>;
511defm SI_SPILL_S128 : SI_SPILL_SGPR <SReg_128>;
512defm SI_SPILL_S256 : SI_SPILL_SGPR <SReg_256>;
513defm SI_SPILL_S512 : SI_SPILL_SGPR <SReg_512>;
514
515multiclass SI_SPILL_VGPR <RegisterClass vgpr_class> {
516  let UseNamedOperandTable = 1, VGPRSpill = 1,
517       SchedRW = [WriteVMEM] in {
518    def _SAVE : VPseudoInstSI <
519      (outs),
520      (ins vgpr_class:$vdata, i32imm:$vaddr, SReg_128:$srsrc,
521           SReg_32:$soffset, i32imm:$offset)> {
522      let mayStore = 1;
523      let mayLoad = 0;
524      // (2 * 4) + (8 * num_subregs) bytes maximum
525      let Size = !add(!shl(!srl(vgpr_class.Size, 5), 3), 8);
526    }
527
528    def _RESTORE : VPseudoInstSI <
529      (outs vgpr_class:$vdata),
530      (ins i32imm:$vaddr, SReg_128:$srsrc, SReg_32:$soffset,
531           i32imm:$offset)> {
532      let mayStore = 0;
533      let mayLoad = 1;
534
535      // (2 * 4) + (8 * num_subregs) bytes maximum
536      let Size = !add(!shl(!srl(vgpr_class.Size, 5), 3), 8);
537    }
538  } // End UseNamedOperandTable = 1, VGPRSpill = 1, SchedRW = [WriteVMEM]
539}
540
541defm SI_SPILL_V32  : SI_SPILL_VGPR <VGPR_32>;
542defm SI_SPILL_V64  : SI_SPILL_VGPR <VReg_64>;
543defm SI_SPILL_V96  : SI_SPILL_VGPR <VReg_96>;
544defm SI_SPILL_V128 : SI_SPILL_VGPR <VReg_128>;
545defm SI_SPILL_V256 : SI_SPILL_VGPR <VReg_256>;
546defm SI_SPILL_V512 : SI_SPILL_VGPR <VReg_512>;
547
548def SI_PC_ADD_REL_OFFSET : SPseudoInstSI <
549  (outs SReg_64:$dst),
550  (ins si_ga:$ptr_lo, si_ga:$ptr_hi),
551  [(set SReg_64:$dst,
552   (i64 (SIpc_add_rel_offset (tglobaladdr:$ptr_lo), (tglobaladdr:$ptr_hi))))]> {
553  let Defs = [SCC];
554}
555
556def : GCNPat <
557  (AMDGPUinit_exec i64:$src),
558  (SI_INIT_EXEC (as_i64imm $src))
559>;
560
561def : GCNPat <
562  (AMDGPUinit_exec_from_input i32:$input, i32:$shift),
563  (SI_INIT_EXEC_FROM_INPUT (i32 $input), (as_i32imm $shift))
564>;
565
566def : GCNPat<
567  (AMDGPUtrap timm:$trapid),
568  (S_TRAP $trapid)
569>;
570
571def : GCNPat<
572  (AMDGPUelse i64:$src, bb:$target),
573  (SI_ELSE $src, $target, 0)
574>;
575
576def : Pat <
577  // -1.0 as i32 (LowerINTRINSIC_VOID converts all other constants to -1.0)
578  (AMDGPUkill (i32 -1082130432)),
579  (SI_KILL_I1_PSEUDO (i1 0), 0)
580>;
581
582def : Pat <
583  (int_amdgcn_kill i1:$src),
584  (SI_KILL_I1_PSEUDO $src, 0)
585>;
586
587def : Pat <
588  (int_amdgcn_kill (i1 (not i1:$src))),
589  (SI_KILL_I1_PSEUDO $src, -1)
590>;
591
592def : Pat <
593  (AMDGPUkill i32:$src),
594  (SI_KILL_F32_COND_IMM_PSEUDO $src, 0, 3) // 3 means SETOGE
595>;
596
597def : Pat <
598  (int_amdgcn_kill (i1 (setcc f32:$src, InlineFPImm<f32>:$imm, cond:$cond))),
599  (SI_KILL_F32_COND_IMM_PSEUDO $src, (bitcast_fpimm_to_i32 $imm), (cond_as_i32imm $cond))
600>;
601// TODO: we could add more variants for other types of conditionals
602
603//===----------------------------------------------------------------------===//
604// VOP1 Patterns
605//===----------------------------------------------------------------------===//
606
607let SubtargetPredicate = isGCN, OtherPredicates = [UnsafeFPMath] in {
608
609//def : RcpPat<V_RCP_F64_e32, f64>;
610//defm : RsqPat<V_RSQ_F64_e32, f64>;
611//defm : RsqPat<V_RSQ_F32_e32, f32>;
612
613def : RsqPat<V_RSQ_F32_e32, f32>;
614def : RsqPat<V_RSQ_F64_e32, f64>;
615
616// Convert (x - floor(x)) to fract(x)
617def : GCNPat <
618  (f32 (fsub (f32 (VOP3Mods f32:$x, i32:$mods)),
619             (f32 (ffloor (f32 (VOP3Mods f32:$x, i32:$mods)))))),
620  (V_FRACT_F32_e64 $mods, $x, DSTCLAMP.NONE, DSTOMOD.NONE)
621>;
622
623// Convert (x + (-floor(x))) to fract(x)
624def : GCNPat <
625  (f64 (fadd (f64 (VOP3Mods f64:$x, i32:$mods)),
626             (f64 (fneg (f64 (ffloor (f64 (VOP3Mods f64:$x, i32:$mods)))))))),
627  (V_FRACT_F64_e64 $mods, $x, DSTCLAMP.NONE, DSTOMOD.NONE)
628>;
629
630} // End SubtargetPredicate = isGCN, OtherPredicates = [UnsafeFPMath]
631
632
633// f16_to_fp patterns
634def : GCNPat <
635  (f32 (f16_to_fp i32:$src0)),
636  (V_CVT_F32_F16_e64 SRCMODS.NONE, $src0, DSTCLAMP.NONE, DSTOMOD.NONE)
637>;
638
639def : GCNPat <
640  (f32 (f16_to_fp (and_oneuse i32:$src0, 0x7fff))),
641  (V_CVT_F32_F16_e64 SRCMODS.ABS, $src0, DSTCLAMP.NONE, DSTOMOD.NONE)
642>;
643
644def : GCNPat <
645  (f32 (f16_to_fp (i32 (srl_oneuse (and_oneuse i32:$src0, 0x7fff0000), (i32 16))))),
646  (V_CVT_F32_F16_e64 SRCMODS.ABS, (i32 (V_LSHRREV_B32_e64 (i32 16), i32:$src0)), DSTCLAMP.NONE, DSTOMOD.NONE)
647>;
648
649def : GCNPat <
650  (f32 (f16_to_fp (or_oneuse i32:$src0, 0x8000))),
651  (V_CVT_F32_F16_e64 SRCMODS.NEG_ABS, $src0, DSTCLAMP.NONE, DSTOMOD.NONE)
652>;
653
654def : GCNPat <
655  (f32 (f16_to_fp (xor_oneuse i32:$src0, 0x8000))),
656  (V_CVT_F32_F16_e64 SRCMODS.NEG, $src0, DSTCLAMP.NONE, DSTOMOD.NONE)
657>;
658
659def : GCNPat <
660  (f64 (fpextend f16:$src)),
661  (V_CVT_F64_F32_e32 (V_CVT_F32_F16_e32 $src))
662>;
663
664// fp_to_fp16 patterns
665def : GCNPat <
666  (i32 (AMDGPUfp_to_f16 (f32 (VOP3Mods f32:$src0, i32:$src0_modifiers)))),
667  (V_CVT_F16_F32_e64 $src0_modifiers, f32:$src0, DSTCLAMP.NONE, DSTOMOD.NONE)
668>;
669
670def : GCNPat <
671  (i32 (fp_to_sint f16:$src)),
672  (V_CVT_I32_F32_e32 (V_CVT_F32_F16_e32 $src))
673>;
674
675def : GCNPat <
676  (i32 (fp_to_uint f16:$src)),
677  (V_CVT_U32_F32_e32 (V_CVT_F32_F16_e32 $src))
678>;
679
680def : GCNPat <
681  (f16 (sint_to_fp i32:$src)),
682  (V_CVT_F16_F32_e32 (V_CVT_F32_I32_e32 $src))
683>;
684
685def : GCNPat <
686  (f16 (uint_to_fp i32:$src)),
687  (V_CVT_F16_F32_e32 (V_CVT_F32_U32_e32 $src))
688>;
689
690//===----------------------------------------------------------------------===//
691// VOP2 Patterns
692//===----------------------------------------------------------------------===//
693
694multiclass FMADPat <ValueType vt, Instruction inst> {
695  def : GCNPat <
696    (vt (fmad (VOP3NoMods vt:$src0),
697              (VOP3NoMods vt:$src1),
698              (VOP3NoMods vt:$src2))),
699    (inst SRCMODS.NONE, $src0, SRCMODS.NONE, $src1,
700          SRCMODS.NONE, $src2, DSTCLAMP.NONE, DSTOMOD.NONE)
701  >;
702}
703
704defm : FMADPat <f16, V_MAC_F16_e64>;
705defm : FMADPat <f32, V_MAC_F32_e64>;
706
707class FMADModsPat<Instruction inst, SDPatternOperator mad_opr, ValueType Ty>
708  : GCNPat<
709  (Ty (mad_opr (VOP3Mods Ty:$src0, i32:$src0_mod),
710  (VOP3Mods Ty:$src1, i32:$src1_mod),
711  (VOP3Mods Ty:$src2, i32:$src2_mod))),
712  (inst $src0_mod, $src0, $src1_mod, $src1,
713  $src2_mod, $src2, DSTCLAMP.NONE, DSTOMOD.NONE)
714>;
715
716def : FMADModsPat<V_MAD_F32, AMDGPUfmad_ftz, f32>;
717def : FMADModsPat<V_MAD_F16, AMDGPUfmad_ftz, f16> {
718  let SubtargetPredicate = Has16BitInsts;
719}
720
721multiclass SelectPat <ValueType vt, Instruction inst> {
722  def : GCNPat <
723    (vt (select i1:$src0, vt:$src1, vt:$src2)),
724    (inst $src2, $src1, $src0)
725  >;
726}
727
728defm : SelectPat <i16, V_CNDMASK_B32_e64>;
729defm : SelectPat <i32, V_CNDMASK_B32_e64>;
730defm : SelectPat <f16, V_CNDMASK_B32_e64>;
731defm : SelectPat <f32, V_CNDMASK_B32_e64>;
732
733def : GCNPat <
734  (i32 (add (i32 (ctpop i32:$popcnt)), i32:$val)),
735  (V_BCNT_U32_B32_e64 $popcnt, $val)
736>;
737def : GCNPat <
738  (i16 (add (i16 (trunc (ctpop i32:$popcnt))), i16:$val)),
739  (V_BCNT_U32_B32_e64 $popcnt, $val)
740>;
741
742/********** ============================================ **********/
743/********** Extraction, Insertion, Building and Casting  **********/
744/********** ============================================ **********/
745
746foreach Index = 0-2 in {
747  def Extract_Element_v2i32_#Index : Extract_Element <
748    i32, v2i32, Index, !cast<SubRegIndex>(sub#Index)
749  >;
750  def Insert_Element_v2i32_#Index : Insert_Element <
751    i32, v2i32, Index, !cast<SubRegIndex>(sub#Index)
752  >;
753
754  def Extract_Element_v2f32_#Index : Extract_Element <
755    f32, v2f32, Index, !cast<SubRegIndex>(sub#Index)
756  >;
757  def Insert_Element_v2f32_#Index : Insert_Element <
758    f32, v2f32, Index, !cast<SubRegIndex>(sub#Index)
759  >;
760}
761
762foreach Index = 0-3 in {
763  def Extract_Element_v4i32_#Index : Extract_Element <
764    i32, v4i32, Index, !cast<SubRegIndex>(sub#Index)
765  >;
766  def Insert_Element_v4i32_#Index : Insert_Element <
767    i32, v4i32, Index, !cast<SubRegIndex>(sub#Index)
768  >;
769
770  def Extract_Element_v4f32_#Index : Extract_Element <
771    f32, v4f32, Index, !cast<SubRegIndex>(sub#Index)
772  >;
773  def Insert_Element_v4f32_#Index : Insert_Element <
774    f32, v4f32, Index, !cast<SubRegIndex>(sub#Index)
775  >;
776}
777
778foreach Index = 0-7 in {
779  def Extract_Element_v8i32_#Index : Extract_Element <
780    i32, v8i32, Index, !cast<SubRegIndex>(sub#Index)
781  >;
782  def Insert_Element_v8i32_#Index : Insert_Element <
783    i32, v8i32, Index, !cast<SubRegIndex>(sub#Index)
784  >;
785
786  def Extract_Element_v8f32_#Index : Extract_Element <
787    f32, v8f32, Index, !cast<SubRegIndex>(sub#Index)
788  >;
789  def Insert_Element_v8f32_#Index : Insert_Element <
790    f32, v8f32, Index, !cast<SubRegIndex>(sub#Index)
791  >;
792}
793
794foreach Index = 0-15 in {
795  def Extract_Element_v16i32_#Index : Extract_Element <
796    i32, v16i32, Index, !cast<SubRegIndex>(sub#Index)
797  >;
798  def Insert_Element_v16i32_#Index : Insert_Element <
799    i32, v16i32, Index, !cast<SubRegIndex>(sub#Index)
800  >;
801
802  def Extract_Element_v16f32_#Index : Extract_Element <
803    f32, v16f32, Index, !cast<SubRegIndex>(sub#Index)
804  >;
805  def Insert_Element_v16f32_#Index : Insert_Element <
806    f32, v16f32, Index, !cast<SubRegIndex>(sub#Index)
807  >;
808}
809
810
811def : Pat <
812  (extract_subvector v4i16:$vec, (i32 0)),
813  (v2i16 (EXTRACT_SUBREG v4i16:$vec, sub0))
814>;
815
816def : Pat <
817  (extract_subvector v4i16:$vec, (i32 2)),
818  (v2i16 (EXTRACT_SUBREG v4i16:$vec, sub1))
819>;
820
821def : Pat <
822  (extract_subvector v4f16:$vec, (i32 0)),
823  (v2f16 (EXTRACT_SUBREG v4f16:$vec, sub0))
824>;
825
826def : Pat <
827  (extract_subvector v4f16:$vec, (i32 2)),
828  (v2f16 (EXTRACT_SUBREG v4f16:$vec, sub1))
829>;
830
831let SubtargetPredicate = isGCN in {
832
833// FIXME: Why do only some of these type combinations for SReg and
834// VReg?
835// 16-bit bitcast
836def : BitConvert <i16, f16, VGPR_32>;
837def : BitConvert <f16, i16, VGPR_32>;
838def : BitConvert <i16, f16, SReg_32>;
839def : BitConvert <f16, i16, SReg_32>;
840
841// 32-bit bitcast
842def : BitConvert <i32, f32, VGPR_32>;
843def : BitConvert <f32, i32, VGPR_32>;
844def : BitConvert <i32, f32, SReg_32>;
845def : BitConvert <f32, i32, SReg_32>;
846def : BitConvert <v2i16, i32, SReg_32>;
847def : BitConvert <i32, v2i16, SReg_32>;
848def : BitConvert <v2f16, i32, SReg_32>;
849def : BitConvert <i32, v2f16, SReg_32>;
850def : BitConvert <v2i16, v2f16, SReg_32>;
851def : BitConvert <v2f16, v2i16, SReg_32>;
852def : BitConvert <v2f16, f32, SReg_32>;
853def : BitConvert <f32, v2f16, SReg_32>;
854def : BitConvert <v2i16, f32, SReg_32>;
855def : BitConvert <f32, v2i16, SReg_32>;
856
857// 64-bit bitcast
858def : BitConvert <i64, f64, VReg_64>;
859def : BitConvert <f64, i64, VReg_64>;
860def : BitConvert <v2i32, v2f32, VReg_64>;
861def : BitConvert <v2f32, v2i32, VReg_64>;
862def : BitConvert <i64, v2i32, VReg_64>;
863def : BitConvert <v2i32, i64, VReg_64>;
864def : BitConvert <i64, v2f32, VReg_64>;
865def : BitConvert <v2f32, i64, VReg_64>;
866def : BitConvert <f64, v2f32, VReg_64>;
867def : BitConvert <v2f32, f64, VReg_64>;
868def : BitConvert <f64, v2i32, VReg_64>;
869def : BitConvert <v2i32, f64, VReg_64>;
870
871// FIXME: Make SGPR
872def : BitConvert <v2i32, v4f16, VReg_64>;
873def : BitConvert <v4f16, v2i32, VReg_64>;
874def : BitConvert <v2i32, v4f16, VReg_64>;
875def : BitConvert <v2i32, v4i16, VReg_64>;
876def : BitConvert <v4i16, v2i32, VReg_64>;
877def : BitConvert <v2f32, v4f16, VReg_64>;
878def : BitConvert <v4f16, v2f32, VReg_64>;
879def : BitConvert <v2f32, v4i16, VReg_64>;
880def : BitConvert <v4i16, v2f32, VReg_64>;
881def : BitConvert <v4i16, f64, VReg_64>;
882def : BitConvert <v4f16, f64, VReg_64>;
883def : BitConvert <f64, v4i16, VReg_64>;
884def : BitConvert <f64, v4f16, VReg_64>;
885def : BitConvert <v4i16, i64, VReg_64>;
886def : BitConvert <v4f16, i64, VReg_64>;
887def : BitConvert <i64, v4i16, VReg_64>;
888def : BitConvert <i64, v4f16, VReg_64>;
889
890def : BitConvert <v4i32, v4f32, VReg_128>;
891def : BitConvert <v4f32, v4i32, VReg_128>;
892
893// 128-bit bitcast
894def : BitConvert <v2i64, v4i32, SReg_128>;
895def : BitConvert <v4i32, v2i64, SReg_128>;
896def : BitConvert <v2f64, v4f32, VReg_128>;
897def : BitConvert <v2f64, v4i32, VReg_128>;
898def : BitConvert <v4f32, v2f64, VReg_128>;
899def : BitConvert <v4i32, v2f64, VReg_128>;
900def : BitConvert <v2i64, v2f64, VReg_128>;
901def : BitConvert <v2f64, v2i64, VReg_128>;
902
903// 256-bit bitcast
904def : BitConvert <v8i32, v8f32, SReg_256>;
905def : BitConvert <v8f32, v8i32, SReg_256>;
906def : BitConvert <v8i32, v8f32, VReg_256>;
907def : BitConvert <v8f32, v8i32, VReg_256>;
908
909// 512-bit bitcast
910def : BitConvert <v16i32, v16f32, VReg_512>;
911def : BitConvert <v16f32, v16i32, VReg_512>;
912
913} // End SubtargetPredicate = isGCN
914
915/********** =================== **********/
916/********** Src & Dst modifiers **********/
917/********** =================== **********/
918
919
920// If denormals are not enabled, it only impacts the compare of the
921// inputs. The output result is not flushed.
922class ClampPat<Instruction inst, ValueType vt> : GCNPat <
923  (vt (AMDGPUclamp (VOP3Mods vt:$src0, i32:$src0_modifiers))),
924  (inst i32:$src0_modifiers, vt:$src0,
925        i32:$src0_modifiers, vt:$src0, DSTCLAMP.ENABLE, DSTOMOD.NONE)
926>;
927
928def : ClampPat<V_MAX_F32_e64, f32>;
929def : ClampPat<V_MAX_F64, f64>;
930def : ClampPat<V_MAX_F16_e64, f16>;
931
932let SubtargetPredicate = HasVOP3PInsts in {
933def : GCNPat <
934  (v2f16 (AMDGPUclamp (VOP3PMods v2f16:$src0, i32:$src0_modifiers))),
935  (V_PK_MAX_F16 $src0_modifiers, $src0,
936                $src0_modifiers, $src0, DSTCLAMP.ENABLE)
937>;
938}
939
940/********** ================================ **********/
941/********** Floating point absolute/negative **********/
942/********** ================================ **********/
943
944// Prevent expanding both fneg and fabs.
945
946def : GCNPat <
947  (fneg (fabs f32:$src)),
948  (S_OR_B32 $src, (S_MOV_B32(i32 0x80000000))) // Set sign bit
949>;
950
951// FIXME: Should use S_OR_B32
952def : GCNPat <
953  (fneg (fabs f64:$src)),
954  (REG_SEQUENCE VReg_64,
955    (i32 (EXTRACT_SUBREG f64:$src, sub0)),
956    sub0,
957    (V_OR_B32_e32 (i32 (EXTRACT_SUBREG f64:$src, sub1)),
958                  (V_MOV_B32_e32 (i32 0x80000000))), // Set sign bit.
959    sub1)
960>;
961
962def : GCNPat <
963  (fabs f32:$src),
964  (S_AND_B32 $src, (S_MOV_B32 (i32 0x7fffffff)))
965>;
966
967def : GCNPat <
968  (fneg f32:$src),
969  (V_XOR_B32_e32 $src, (V_MOV_B32_e32 (i32 0x80000000)))
970>;
971
972def : GCNPat <
973  (fabs f64:$src),
974  (REG_SEQUENCE VReg_64,
975    (i32 (EXTRACT_SUBREG f64:$src, sub0)),
976    sub0,
977    (V_AND_B32_e64 (i32 (EXTRACT_SUBREG f64:$src, sub1)),
978                   (V_MOV_B32_e32 (i32 0x7fffffff))), // Set sign bit.
979     sub1)
980>;
981
982def : GCNPat <
983  (fneg f64:$src),
984  (REG_SEQUENCE VReg_64,
985    (i32 (EXTRACT_SUBREG f64:$src, sub0)),
986    sub0,
987    (V_XOR_B32_e32 (i32 (EXTRACT_SUBREG f64:$src, sub1)),
988                   (i32 (V_MOV_B32_e32 (i32 0x80000000)))),
989    sub1)
990>;
991
992def : GCNPat <
993  (fcopysign f16:$src0, f16:$src1),
994  (V_BFI_B32 (S_MOV_B32 (i32 0x00007fff)), $src0, $src1)
995>;
996
997def : GCNPat <
998  (fcopysign f32:$src0, f16:$src1),
999  (V_BFI_B32 (S_MOV_B32 (i32 0x7fffffff)), $src0,
1000             (V_LSHLREV_B32_e64 (i32 16), $src1))
1001>;
1002
1003def : GCNPat <
1004  (fcopysign f64:$src0, f16:$src1),
1005  (REG_SEQUENCE SReg_64,
1006    (i32 (EXTRACT_SUBREG $src0, sub0)), sub0,
1007    (V_BFI_B32 (S_MOV_B32 (i32 0x7fffffff)), (i32 (EXTRACT_SUBREG $src0, sub1)),
1008               (V_LSHLREV_B32_e64 (i32 16), $src1)), sub1)
1009>;
1010
1011def : GCNPat <
1012  (fcopysign f16:$src0, f32:$src1),
1013  (V_BFI_B32 (S_MOV_B32 (i32 0x00007fff)), $src0,
1014             (V_LSHRREV_B32_e64 (i32 16), $src1))
1015>;
1016
1017def : GCNPat <
1018  (fcopysign f16:$src0, f64:$src1),
1019  (V_BFI_B32 (S_MOV_B32 (i32 0x00007fff)), $src0,
1020             (V_LSHRREV_B32_e64 (i32 16), (EXTRACT_SUBREG $src1, sub1)))
1021>;
1022
1023def : GCNPat <
1024  (fneg f16:$src),
1025  (S_XOR_B32 $src, (S_MOV_B32 (i32 0x00008000)))
1026>;
1027
1028def : GCNPat <
1029  (fabs f16:$src),
1030  (S_AND_B32 $src, (S_MOV_B32 (i32 0x00007fff)))
1031>;
1032
1033def : GCNPat <
1034  (fneg (fabs f16:$src)),
1035  (S_OR_B32 $src, (S_MOV_B32 (i32 0x00008000))) // Set sign bit
1036>;
1037
1038def : GCNPat <
1039  (fneg v2f16:$src),
1040  (S_XOR_B32 $src, (S_MOV_B32 (i32 0x80008000)))
1041>;
1042
1043def : GCNPat <
1044  (fabs v2f16:$src),
1045  (S_AND_B32 $src, (S_MOV_B32 (i32 0x7fff7fff)))
1046>;
1047
1048// This is really (fneg (fabs v2f16:$src))
1049//
1050// fabs is not reported as free because there is modifier for it in
1051// VOP3P instructions, so it is turned into the bit op.
1052def : GCNPat <
1053  (fneg (v2f16 (bitconvert (and_oneuse i32:$src, 0x7fff7fff)))),
1054  (S_OR_B32 $src, (S_MOV_B32 (i32 0x80008000))) // Set sign bit
1055>;
1056
1057def : GCNPat <
1058  (fneg (v2f16 (fabs v2f16:$src))),
1059  (S_OR_B32 $src, (S_MOV_B32 (i32 0x80008000))) // Set sign bit
1060>;
1061
1062/********** ================== **********/
1063/********** Immediate Patterns **********/
1064/********** ================== **********/
1065
1066def : GCNPat <
1067  (VGPRImm<(i32 imm)>:$imm),
1068  (V_MOV_B32_e32 imm:$imm)
1069>;
1070
1071def : GCNPat <
1072  (VGPRImm<(f32 fpimm)>:$imm),
1073  (V_MOV_B32_e32 (f32 (bitcast_fpimm_to_i32 $imm)))
1074>;
1075
1076def : GCNPat <
1077  (i32 imm:$imm),
1078  (S_MOV_B32 imm:$imm)
1079>;
1080
1081// FIXME: Workaround for ordering issue with peephole optimizer where
1082// a register class copy interferes with immediate folding.  Should
1083// use s_mov_b32, which can be shrunk to s_movk_i32
1084def : GCNPat <
1085  (VGPRImm<(f16 fpimm)>:$imm),
1086  (V_MOV_B32_e32 (f16 (bitcast_fpimm_to_i32 $imm)))
1087>;
1088
1089def : GCNPat <
1090  (f32 fpimm:$imm),
1091  (S_MOV_B32 (f32 (bitcast_fpimm_to_i32 $imm)))
1092>;
1093
1094def : GCNPat <
1095  (f16 fpimm:$imm),
1096  (S_MOV_B32 (i32 (bitcast_fpimm_to_i32 $imm)))
1097>;
1098
1099def : GCNPat <
1100 (i32 frameindex:$fi),
1101 (V_MOV_B32_e32 (i32 (frameindex_to_targetframeindex $fi)))
1102>;
1103
1104def : GCNPat <
1105  (i64 InlineImm<i64>:$imm),
1106  (S_MOV_B64 InlineImm<i64>:$imm)
1107>;
1108
1109// XXX - Should this use a s_cmp to set SCC?
1110
1111// Set to sign-extended 64-bit value (true = -1, false = 0)
1112def : GCNPat <
1113  (i1 imm:$imm),
1114  (S_MOV_B64 (i64 (as_i64imm $imm)))
1115>;
1116
1117def : GCNPat <
1118  (f64 InlineFPImm<f64>:$imm),
1119  (S_MOV_B64 (f64 (bitcast_fpimm_to_i64 InlineFPImm<f64>:$imm)))
1120>;
1121
1122/********** ================== **********/
1123/********** Intrinsic Patterns **********/
1124/********** ================== **********/
1125
1126let SubtargetPredicate = isGCN in {
1127def : POW_Common <V_LOG_F32_e32, V_EXP_F32_e32, V_MUL_LEGACY_F32_e32>;
1128}
1129
1130def : GCNPat <
1131  (i32 (sext i1:$src0)),
1132  (V_CNDMASK_B32_e64 (i32 0), (i32 -1), $src0)
1133>;
1134
1135class Ext32Pat <SDNode ext> : GCNPat <
1136  (i32 (ext i1:$src0)),
1137  (V_CNDMASK_B32_e64 (i32 0), (i32 1), $src0)
1138>;
1139
1140def : Ext32Pat <zext>;
1141def : Ext32Pat <anyext>;
1142
1143// The multiplication scales from [0,1] to the unsigned integer range
1144def : GCNPat <
1145  (AMDGPUurecip i32:$src0),
1146  (V_CVT_U32_F32_e32
1147    (V_MUL_F32_e32 (i32 CONST.FP_UINT_MAX_PLUS_1),
1148                   (V_RCP_IFLAG_F32_e32 (V_CVT_F32_U32_e32 $src0))))
1149>;
1150
1151//===----------------------------------------------------------------------===//
1152// VOP3 Patterns
1153//===----------------------------------------------------------------------===//
1154
1155let SubtargetPredicate = isGCN in {
1156
1157def : IMad24Pat<V_MAD_I32_I24, 1>;
1158def : UMad24Pat<V_MAD_U32_U24, 1>;
1159
1160// FIXME: This should only be done for VALU inputs
1161defm : BFIPatterns <V_BFI_B32, S_MOV_B32, SReg_64>;
1162def : ROTRPattern <V_ALIGNBIT_B32>;
1163
1164}
1165
1166def : GCNPat<(i32 (trunc (srl i64:$src0, (and i32:$src1, (i32 31))))),
1167          (V_ALIGNBIT_B32 (i32 (EXTRACT_SUBREG (i64 $src0), sub1)),
1168                          (i32 (EXTRACT_SUBREG (i64 $src0), sub0)), $src1)>;
1169
1170def : GCNPat<(i32 (trunc (srl i64:$src0, (i32 ShiftAmt32Imm:$src1)))),
1171          (V_ALIGNBIT_B32 (i32 (EXTRACT_SUBREG (i64 $src0), sub1)),
1172                          (i32 (EXTRACT_SUBREG (i64 $src0), sub0)), $src1)>;
1173
1174/********** ====================== **********/
1175/**********   Indirect addressing  **********/
1176/********** ====================== **********/
1177
1178multiclass SI_INDIRECT_Pattern <ValueType vt, ValueType eltvt, string VecSize> {
1179  // Extract with offset
1180  def : GCNPat<
1181    (eltvt (extractelt vt:$src, (MOVRELOffset i32:$idx, (i32 imm:$offset)))),
1182    (!cast<Instruction>("SI_INDIRECT_SRC_"#VecSize) $src, $idx, imm:$offset)
1183  >;
1184
1185  // Insert with offset
1186  def : GCNPat<
1187    (insertelt vt:$src, eltvt:$val, (MOVRELOffset i32:$idx, (i32 imm:$offset))),
1188    (!cast<Instruction>("SI_INDIRECT_DST_"#VecSize) $src, $idx, imm:$offset, $val)
1189  >;
1190}
1191
1192defm : SI_INDIRECT_Pattern <v2f32, f32, "V2">;
1193defm : SI_INDIRECT_Pattern <v4f32, f32, "V4">;
1194defm : SI_INDIRECT_Pattern <v8f32, f32, "V8">;
1195defm : SI_INDIRECT_Pattern <v16f32, f32, "V16">;
1196
1197defm : SI_INDIRECT_Pattern <v2i32, i32, "V2">;
1198defm : SI_INDIRECT_Pattern <v4i32, i32, "V4">;
1199defm : SI_INDIRECT_Pattern <v8i32, i32, "V8">;
1200defm : SI_INDIRECT_Pattern <v16i32, i32, "V16">;
1201
1202//===----------------------------------------------------------------------===//
1203// SAD Patterns
1204//===----------------------------------------------------------------------===//
1205
1206def : GCNPat <
1207  (add (sub_oneuse (umax i32:$src0, i32:$src1),
1208                   (umin i32:$src0, i32:$src1)),
1209       i32:$src2),
1210  (V_SAD_U32 $src0, $src1, $src2, (i1 0))
1211>;
1212
1213def : GCNPat <
1214  (add (select_oneuse (i1 (setugt i32:$src0, i32:$src1)),
1215                      (sub i32:$src0, i32:$src1),
1216                      (sub i32:$src1, i32:$src0)),
1217       i32:$src2),
1218  (V_SAD_U32 $src0, $src1, $src2, (i1 0))
1219>;
1220
1221//===----------------------------------------------------------------------===//
1222// Conversion Patterns
1223//===----------------------------------------------------------------------===//
1224
1225def : GCNPat<(i32 (sext_inreg i32:$src, i1)),
1226  (S_BFE_I32 i32:$src, (i32 65536))>; // 0 | 1 << 16
1227
1228// Handle sext_inreg in i64
1229def : GCNPat <
1230  (i64 (sext_inreg i64:$src, i1)),
1231  (S_BFE_I64 i64:$src, (i32 0x10000)) // 0 | 1 << 16
1232>;
1233
1234def : GCNPat <
1235  (i16 (sext_inreg i16:$src, i1)),
1236  (S_BFE_I32 $src, (i32 0x00010000)) // 0 | 1 << 16
1237>;
1238
1239def : GCNPat <
1240  (i16 (sext_inreg i16:$src, i8)),
1241  (S_BFE_I32 $src, (i32 0x80000)) // 0 | 8 << 16
1242>;
1243
1244def : GCNPat <
1245  (i64 (sext_inreg i64:$src, i8)),
1246  (S_BFE_I64 i64:$src, (i32 0x80000)) // 0 | 8 << 16
1247>;
1248
1249def : GCNPat <
1250  (i64 (sext_inreg i64:$src, i16)),
1251  (S_BFE_I64 i64:$src, (i32 0x100000)) // 0 | 16 << 16
1252>;
1253
1254def : GCNPat <
1255  (i64 (sext_inreg i64:$src, i32)),
1256  (S_BFE_I64 i64:$src, (i32 0x200000)) // 0 | 32 << 16
1257>;
1258
1259def : GCNPat <
1260  (i64 (zext i32:$src)),
1261  (REG_SEQUENCE SReg_64, $src, sub0, (S_MOV_B32 (i32 0)), sub1)
1262>;
1263
1264def : GCNPat <
1265  (i64 (anyext i32:$src)),
1266  (REG_SEQUENCE SReg_64, $src, sub0, (i32 (IMPLICIT_DEF)), sub1)
1267>;
1268
1269class ZExt_i64_i1_Pat <SDNode ext> : GCNPat <
1270  (i64 (ext i1:$src)),
1271    (REG_SEQUENCE VReg_64,
1272      (V_CNDMASK_B32_e64 (i32 0), (i32 1), $src), sub0,
1273      (S_MOV_B32 (i32 0)), sub1)
1274>;
1275
1276
1277def : ZExt_i64_i1_Pat<zext>;
1278def : ZExt_i64_i1_Pat<anyext>;
1279
1280// FIXME: We need to use COPY_TO_REGCLASS to work-around the fact that
1281// REG_SEQUENCE patterns don't support instructions with multiple outputs.
1282def : GCNPat <
1283  (i64 (sext i32:$src)),
1284    (REG_SEQUENCE SReg_64, $src, sub0,
1285    (i32 (COPY_TO_REGCLASS (S_ASHR_I32 $src, (i32 31)), SReg_32_XM0)), sub1)
1286>;
1287
1288def : GCNPat <
1289  (i64 (sext i1:$src)),
1290  (REG_SEQUENCE VReg_64,
1291    (V_CNDMASK_B32_e64 (i32 0), (i32 -1), $src), sub0,
1292    (V_CNDMASK_B32_e64 (i32 0), (i32 -1), $src), sub1)
1293>;
1294
1295class FPToI1Pat<Instruction Inst, int KOne, ValueType kone_type, ValueType vt, SDPatternOperator fp_to_int> : GCNPat <
1296  (i1 (fp_to_int (vt (VOP3Mods vt:$src0, i32:$src0_modifiers)))),
1297  (i1 (Inst 0, (kone_type KOne), $src0_modifiers, $src0, DSTCLAMP.NONE))
1298>;
1299
1300def : FPToI1Pat<V_CMP_EQ_F32_e64, CONST.FP32_ONE, i32, f32, fp_to_uint>;
1301def : FPToI1Pat<V_CMP_EQ_F32_e64, CONST.FP32_NEG_ONE, i32, f32, fp_to_sint>;
1302def : FPToI1Pat<V_CMP_EQ_F64_e64, CONST.FP64_ONE, i64, f64, fp_to_uint>;
1303def : FPToI1Pat<V_CMP_EQ_F64_e64, CONST.FP64_NEG_ONE, i64, f64, fp_to_sint>;
1304
1305// If we need to perform a logical operation on i1 values, we need to
1306// use vector comparisons since there is only one SCC register. Vector
1307// comparisons still write to a pair of SGPRs, so treat these as
1308// 64-bit comparisons. When legalizing SGPR copies, instructions
1309// resulting in the copies from SCC to these instructions will be
1310// moved to the VALU.
1311def : GCNPat <
1312  (i1 (and i1:$src0, i1:$src1)),
1313  (S_AND_B64 $src0, $src1)
1314>;
1315
1316def : GCNPat <
1317  (i1 (or i1:$src0, i1:$src1)),
1318  (S_OR_B64 $src0, $src1)
1319>;
1320
1321def : GCNPat <
1322  (i1 (xor i1:$src0, i1:$src1)),
1323  (S_XOR_B64 $src0, $src1)
1324>;
1325
1326def : GCNPat <
1327  (f32 (sint_to_fp i1:$src)),
1328  (V_CNDMASK_B32_e64 (i32 0), (i32 CONST.FP32_NEG_ONE), $src)
1329>;
1330
1331def : GCNPat <
1332  (f32 (uint_to_fp i1:$src)),
1333  (V_CNDMASK_B32_e64 (i32 0), (i32 CONST.FP32_ONE), $src)
1334>;
1335
1336def : GCNPat <
1337  (f64 (sint_to_fp i1:$src)),
1338  (V_CVT_F64_I32_e32 (V_CNDMASK_B32_e64 (i32 0), (i32 -1), $src))
1339>;
1340
1341def : GCNPat <
1342  (f64 (uint_to_fp i1:$src)),
1343  (V_CVT_F64_U32_e32 (V_CNDMASK_B32_e64 (i32 0), (i32 1), $src))
1344>;
1345
1346//===----------------------------------------------------------------------===//
1347// Miscellaneous Patterns
1348//===----------------------------------------------------------------------===//
1349def : GCNPat <
1350  (i32 (AMDGPUfp16_zext f16:$src)),
1351  (COPY $src)
1352>;
1353
1354
1355def : GCNPat <
1356  (i32 (trunc i64:$a)),
1357  (EXTRACT_SUBREG $a, sub0)
1358>;
1359
1360def : GCNPat <
1361  (i1 (trunc i32:$a)),
1362  (V_CMP_EQ_U32_e64 (S_AND_B32 (i32 1), $a), (i32 1))
1363>;
1364
1365def : GCNPat <
1366  (i1 (trunc i16:$a)),
1367  (V_CMP_EQ_U32_e64 (S_AND_B32 (i32 1), $a), (i32 1))
1368>;
1369
1370def : GCNPat <
1371  (i1 (trunc i64:$a)),
1372  (V_CMP_EQ_U32_e64 (S_AND_B32 (i32 1),
1373                    (i32 (EXTRACT_SUBREG $a, sub0))), (i32 1))
1374>;
1375
1376def : GCNPat <
1377  (i32 (bswap i32:$a)),
1378  (V_BFI_B32 (S_MOV_B32 (i32 0x00ff00ff)),
1379             (V_ALIGNBIT_B32 $a, $a, (i32 24)),
1380             (V_ALIGNBIT_B32 $a, $a, (i32 8)))
1381>;
1382
1383let OtherPredicates = [NoFP16Denormals] in {
1384def : GCNPat<
1385  (fcanonicalize (f16 (VOP3Mods f16:$src, i32:$src_mods))),
1386  (V_MUL_F16_e64 0, (i32 CONST.FP16_ONE), $src_mods, $src, 0, 0)
1387>;
1388
1389def : GCNPat<
1390  (fcanonicalize (f16 (fneg (VOP3Mods f16:$src, i32:$src_mods)))),
1391  (V_MUL_F16_e64 0, (i32 CONST.FP16_NEG_ONE), $src_mods, $src, 0, 0)
1392>;
1393
1394def : GCNPat<
1395  (fcanonicalize (v2f16 (VOP3PMods v2f16:$src, i32:$src_mods))),
1396  (V_PK_MUL_F16 0, (i32 CONST.V2FP16_ONE), $src_mods, $src, DSTCLAMP.NONE)
1397>;
1398}
1399
1400let OtherPredicates = [FP16Denormals] in {
1401def : GCNPat<
1402  (fcanonicalize (f16 (VOP3Mods f16:$src, i32:$src_mods))),
1403  (V_MAX_F16_e64 $src_mods, $src, $src_mods, $src, 0, 0)
1404>;
1405
1406let SubtargetPredicate = HasVOP3PInsts in {
1407def : GCNPat<
1408  (fcanonicalize (v2f16 (VOP3PMods v2f16:$src, i32:$src_mods))),
1409  (V_PK_MAX_F16 $src_mods, $src, $src_mods, $src, DSTCLAMP.NONE)
1410>;
1411}
1412}
1413
1414let OtherPredicates = [NoFP32Denormals] in {
1415def : GCNPat<
1416  (fcanonicalize (f32 (VOP3Mods f32:$src, i32:$src_mods))),
1417  (V_MUL_F32_e64 0, (i32 CONST.FP32_ONE), $src_mods, $src, 0, 0)
1418>;
1419
1420def : GCNPat<
1421  (fcanonicalize (f32 (fneg (VOP3Mods f32:$src, i32:$src_mods)))),
1422  (V_MUL_F32_e64 0, (i32 CONST.FP32_NEG_ONE), $src_mods, $src, 0, 0)
1423>;
1424}
1425
1426let OtherPredicates = [FP32Denormals] in {
1427def : GCNPat<
1428  (fcanonicalize (f32 (VOP3Mods f32:$src, i32:$src_mods))),
1429  (V_MAX_F32_e64 $src_mods, $src, $src_mods, $src, 0, 0)
1430>;
1431}
1432
1433let OtherPredicates = [NoFP64Denormals] in {
1434def : GCNPat<
1435  (fcanonicalize (f64 (VOP3Mods f64:$src, i32:$src_mods))),
1436  (V_MUL_F64 0, CONST.FP64_ONE, $src_mods, $src, 0, 0)
1437>;
1438}
1439
1440let OtherPredicates = [FP64Denormals] in {
1441def : GCNPat<
1442  (fcanonicalize (f64 (VOP3Mods f64:$src, i32:$src_mods))),
1443  (V_MAX_F64 $src_mods, $src, $src_mods, $src, 0, 0)
1444>;
1445}
1446
1447let OtherPredicates = [HasDLInsts] in {
1448def : GCNPat <
1449  (fma (f32 (VOP3Mods0 f32:$src0, i32:$src0_modifiers, i1:$clamp, i32:$omod)),
1450       (f32 (VOP3Mods f32:$src1, i32:$src1_modifiers)),
1451       (f32 (VOP3NoMods f32:$src2))),
1452  (V_FMAC_F32_e64 $src0_modifiers, $src0, $src1_modifiers, $src1,
1453                  SRCMODS.NONE, $src2, $clamp, $omod)
1454>;
1455} // End OtherPredicates = [HasDLInsts]
1456
1457
1458// Allow integer inputs
1459class ExpPattern<SDPatternOperator node, ValueType vt, Instruction Inst> : GCNPat<
1460  (node (i8 timm:$tgt), (i8 timm:$en), vt:$src0, vt:$src1, vt:$src2, vt:$src3, (i1 timm:$compr), (i1 timm:$vm)),
1461  (Inst i8:$tgt, vt:$src0, vt:$src1, vt:$src2, vt:$src3, i1:$vm, i1:$compr, i8:$en)
1462>;
1463
1464def : ExpPattern<AMDGPUexport, i32, EXP>;
1465def : ExpPattern<AMDGPUexport_done, i32, EXP_DONE>;
1466
1467// COPY_TO_REGCLASS is workaround tablegen bug from multiple outputs
1468// from S_LSHL_B32's multiple outputs from implicit scc def.
1469def : GCNPat <
1470  (v2i16 (build_vector (i16 0), i16:$src1)),
1471  (v2i16 (COPY_TO_REGCLASS (S_LSHL_B32 i16:$src1, (i16 16)), SReg_32_XM0))
1472>;
1473
1474
1475let SubtargetPredicate = HasVOP3PInsts in {
1476def : GCNPat <
1477  (v2i16 (build_vector i16:$src0, i16:$src1)),
1478  (v2i16 (S_PACK_LL_B32_B16 $src0, $src1))
1479>;
1480
1481// With multiple uses of the shift, this will duplicate the shift and
1482// increase register pressure.
1483def : GCNPat <
1484  (v2i16 (build_vector i16:$src0, (i16 (trunc (srl_oneuse i32:$src1, (i32 16)))))),
1485  (v2i16 (S_PACK_LH_B32_B16 i16:$src0, i32:$src1))
1486>;
1487
1488
1489def : GCNPat <
1490  (v2i16 (build_vector (i16 (trunc (srl_oneuse i32:$src0, (i32 16)))),
1491                       (i16 (trunc (srl_oneuse i32:$src1, (i32 16)))))),
1492  (v2i16 (S_PACK_HH_B32_B16 $src0, $src1))
1493>;
1494
1495// TODO: Should source modifiers be matched to v_pack_b32_f16?
1496def : GCNPat <
1497  (v2f16 (build_vector f16:$src0, f16:$src1)),
1498  (v2f16 (S_PACK_LL_B32_B16 $src0, $src1))
1499>;
1500
1501} // End SubtargetPredicate = HasVOP3PInsts
1502
1503
1504// def : GCNPat <
1505//   (v2f16 (scalar_to_vector f16:$src0)),
1506//   (COPY $src0)
1507// >;
1508
1509// def : GCNPat <
1510//   (v2i16 (scalar_to_vector i16:$src0)),
1511//   (COPY $src0)
1512// >;
1513
1514def : GCNPat <
1515  (v4i16 (scalar_to_vector i16:$src0)),
1516  (INSERT_SUBREG (IMPLICIT_DEF), $src0, sub0)
1517>;
1518
1519def : GCNPat <
1520  (v4f16 (scalar_to_vector f16:$src0)),
1521  (INSERT_SUBREG (IMPLICIT_DEF), $src0, sub0)
1522>;
1523
1524//===----------------------------------------------------------------------===//
1525// Fract Patterns
1526//===----------------------------------------------------------------------===//
1527
1528let SubtargetPredicate = isSI in {
1529
1530// V_FRACT is buggy on SI, so the F32 version is never used and (x-floor(x)) is
1531// used instead. However, SI doesn't have V_FLOOR_F64, so the most efficient
1532// way to implement it is using V_FRACT_F64.
1533// The workaround for the V_FRACT bug is:
1534//    fract(x) = isnan(x) ? x : min(V_FRACT(x), 0.99999999999999999)
1535
1536// Convert floor(x) to (x - fract(x))
1537def : GCNPat <
1538  (f64 (ffloor (f64 (VOP3Mods f64:$x, i32:$mods)))),
1539  (V_ADD_F64
1540      $mods,
1541      $x,
1542      SRCMODS.NEG,
1543      (V_CNDMASK_B64_PSEUDO
1544         (V_MIN_F64
1545             SRCMODS.NONE,
1546             (V_FRACT_F64_e64 $mods, $x, DSTCLAMP.NONE, DSTOMOD.NONE),
1547             SRCMODS.NONE,
1548             (V_MOV_B64_PSEUDO 0x3fefffffffffffff),
1549             DSTCLAMP.NONE, DSTOMOD.NONE),
1550         $x,
1551         (V_CMP_CLASS_F64_e64 SRCMODS.NONE, $x, (i32 3 /*NaN*/))),
1552      DSTCLAMP.NONE, DSTOMOD.NONE)
1553>;
1554
1555} // End SubtargetPredicates = isSI
1556
1557//============================================================================//
1558// Miscellaneous Optimization Patterns
1559//============================================================================//
1560
1561// Undo sub x, c -> add x, -c canonicalization since c is more likely
1562// an inline immediate than -c.
1563// TODO: Also do for 64-bit.
1564def : GCNPat<
1565  (add i32:$src0, (i32 NegSubInlineConst32:$src1)),
1566  (S_SUB_I32 $src0, NegSubInlineConst32:$src1)
1567>;
1568
1569
1570multiclass BFMPatterns <ValueType vt, InstSI BFM, InstSI MOV> {
1571  def : GCNPat <
1572    (vt (shl (vt (add (vt (shl 1, vt:$a)), -1)), vt:$b)),
1573    (BFM $a, $b)
1574  >;
1575
1576  def : GCNPat <
1577    (vt (add (vt (shl 1, vt:$a)), -1)),
1578    (BFM $a, (MOV (i32 0)))
1579  >;
1580}
1581
1582let SubtargetPredicate = isGCN in {
1583
1584defm : BFMPatterns <i32, S_BFM_B32, S_MOV_B32>;
1585// FIXME: defm : BFMPatterns <i64, S_BFM_B64, S_MOV_B64>;
1586
1587defm : BFEPattern <V_BFE_U32, V_BFE_I32, S_MOV_B32>;
1588defm : SHA256MaPattern <V_BFI_B32, V_XOR_B32_e64, SReg_64>;
1589
1590def : IntMed3Pat<V_MED3_I32, smax, smax_oneuse, smin_oneuse>;
1591def : IntMed3Pat<V_MED3_U32, umax, umax_oneuse, umin_oneuse>;
1592
1593}
1594
1595// This matches 16 permutations of
1596// max(min(x, y), min(max(x, y), z))
1597class FPMed3Pat<ValueType vt,
1598                Instruction med3Inst> : GCNPat<
1599  (fmaxnum (fminnum_oneuse (VOP3Mods_nnan vt:$src0, i32:$src0_mods),
1600                           (VOP3Mods_nnan vt:$src1, i32:$src1_mods)),
1601           (fminnum_oneuse (fmaxnum_oneuse (VOP3Mods_nnan vt:$src0, i32:$src0_mods),
1602                                           (VOP3Mods_nnan vt:$src1, i32:$src1_mods)),
1603                           (vt (VOP3Mods_nnan vt:$src2, i32:$src2_mods)))),
1604  (med3Inst $src0_mods, $src0, $src1_mods, $src1, $src2_mods, $src2, DSTCLAMP.NONE, DSTOMOD.NONE)
1605>;
1606
1607class FP16Med3Pat<ValueType vt,
1608                Instruction med3Inst> : GCNPat<
1609  (fmaxnum (fminnum_oneuse (VOP3Mods_nnan vt:$src0, i32:$src0_mods),
1610                           (VOP3Mods_nnan vt:$src1, i32:$src1_mods)),
1611           (fminnum_oneuse (fmaxnum_oneuse (VOP3Mods_nnan vt:$src0, i32:$src0_mods),
1612                                           (VOP3Mods_nnan vt:$src1, i32:$src1_mods)),
1613                           (vt (VOP3Mods_nnan vt:$src2, i32:$src2_mods)))),
1614  (med3Inst $src0_mods, $src0, $src1_mods, $src1, $src2_mods, $src2, DSTCLAMP.NONE)
1615>;
1616
1617class Int16Med3Pat<Instruction med3Inst,
1618                   SDPatternOperator max,
1619                   SDPatternOperator max_oneuse,
1620                   SDPatternOperator min_oneuse,
1621                   ValueType vt = i32> : GCNPat<
1622  (max (min_oneuse vt:$src0, vt:$src1),
1623       (min_oneuse (max_oneuse vt:$src0, vt:$src1), vt:$src2)),
1624  (med3Inst SRCMODS.NONE, $src0, SRCMODS.NONE, $src1, SRCMODS.NONE, $src2, DSTCLAMP.NONE)
1625>;
1626
1627def : FPMed3Pat<f32, V_MED3_F32>;
1628
1629let OtherPredicates = [isGFX9] in {
1630def : FP16Med3Pat<f16, V_MED3_F16>;
1631def : Int16Med3Pat<V_MED3_I16, smax, smax_oneuse, smin_oneuse, i16>;
1632def : Int16Med3Pat<V_MED3_U16, umax, umax_oneuse, umin_oneuse, i16>;
1633} // End Predicates = [isGFX9]
1634