1//===-- SIInstructions.td - SI Instruction Definitions --------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8// This file was originally auto-generated from a GPU register header file and
9// all the instruction definitions were originally commented out.  Instructions
10// that are not yet supported remain commented out.
11//===----------------------------------------------------------------------===//
12
13class GCNPat<dag pattern, dag result> : Pat<pattern, result>, GCNPredicateControl {
14
15}
16
17include "SOPInstructions.td"
18include "VOPInstructions.td"
19include "SMInstructions.td"
20include "FLATInstructions.td"
21include "BUFInstructions.td"
22include "EXPInstructions.td"
23
24//===----------------------------------------------------------------------===//
25// VINTRP Instructions
26//===----------------------------------------------------------------------===//
27
28// Used to inject printing of "_e32" suffix for VI (there are "_e64" variants for VI)
29def VINTRPDst : VINTRPDstOperand <VGPR_32>;
30
31let Uses = [MODE, M0, EXEC] in {
32
33// FIXME: Specify SchedRW for VINTRP instructions.
34
35multiclass V_INTERP_P1_F32_m : VINTRP_m <
36  0x00000000,
37  (outs VINTRPDst:$vdst),
38  (ins VGPR_32:$vsrc, Attr:$attr, AttrChan:$attrchan),
39  "v_interp_p1_f32$vdst, $vsrc, $attr$attrchan",
40  [(set f32:$vdst, (int_amdgcn_interp_p1 f32:$vsrc,
41                   (i32 timm:$attrchan), (i32 timm:$attr), M0))]
42>;
43
44let OtherPredicates = [has32BankLDS, isNotGFX90APlus] in {
45
46defm V_INTERP_P1_F32 : V_INTERP_P1_F32_m;
47
48} // End OtherPredicates = [has32BankLDS, isNotGFX90APlus]
49
50let OtherPredicates = [has16BankLDS, isNotGFX90APlus],
51    Constraints = "@earlyclobber $vdst", isAsmParserOnly=1 in {
52
53defm V_INTERP_P1_F32_16bank : V_INTERP_P1_F32_m;
54
55} // End OtherPredicates = [has32BankLDS, isNotGFX90APlus],
56  //     Constraints = "@earlyclobber $vdst", isAsmParserOnly=1
57
58let OtherPredicates = [isNotGFX90APlus] in {
59let DisableEncoding = "$src0", Constraints = "$src0 = $vdst" in {
60
61defm V_INTERP_P2_F32 : VINTRP_m <
62  0x00000001,
63  (outs VINTRPDst:$vdst),
64  (ins VGPR_32:$src0, VGPR_32:$vsrc, Attr:$attr, AttrChan:$attrchan),
65  "v_interp_p2_f32$vdst, $vsrc, $attr$attrchan",
66  [(set f32:$vdst, (int_amdgcn_interp_p2 f32:$src0, f32:$vsrc,
67                   (i32 timm:$attrchan), (i32 timm:$attr), M0))]>;
68
69} // End DisableEncoding = "$src0", Constraints = "$src0 = $vdst"
70
71defm V_INTERP_MOV_F32 : VINTRP_m <
72  0x00000002,
73  (outs VINTRPDst:$vdst),
74  (ins InterpSlot:$vsrc, Attr:$attr, AttrChan:$attrchan),
75  "v_interp_mov_f32$vdst, $vsrc, $attr$attrchan",
76  [(set f32:$vdst, (int_amdgcn_interp_mov (i32 timm:$vsrc),
77                   (i32 timm:$attrchan), (i32 timm:$attr), M0))]>;
78
79} // End OtherPredicates = [isNotGFX90APlus]
80
81} // End Uses = [MODE, M0, EXEC]
82
83//===----------------------------------------------------------------------===//
84// Pseudo Instructions
85//===----------------------------------------------------------------------===//
86def ATOMIC_FENCE : SPseudoInstSI<
87  (outs), (ins i32imm:$ordering, i32imm:$scope),
88  [(atomic_fence (i32 timm:$ordering), (i32 timm:$scope))],
89  "ATOMIC_FENCE $ordering, $scope"> {
90  let hasSideEffects = 1;
91  let maybeAtomic = 1;
92}
93
94let hasSideEffects = 0, mayLoad = 0, mayStore = 0, Uses = [EXEC] in {
95
96// For use in patterns
97def V_CNDMASK_B64_PSEUDO : VOP3Common <(outs VReg_64:$vdst),
98  (ins VSrc_b64:$src0, VSrc_b64:$src1, SSrc_b64:$src2), "", []> {
99  let isPseudo = 1;
100  let isCodeGenOnly = 1;
101  let usesCustomInserter = 1;
102}
103
104// 64-bit vector move instruction. This is mainly used by the
105// SIFoldOperands pass to enable folding of inline immediates.
106def V_MOV_B64_PSEUDO : VPseudoInstSI <(outs VReg_64:$vdst),
107                                      (ins VSrc_b64:$src0)> {
108  let isReMaterializable = 1;
109  let isAsCheapAsAMove = 1;
110  let isMoveImm = 1;
111  let SchedRW = [Write64Bit];
112  let Size = 16; // Needs maximum 2 v_mov_b32 instructions 8 byte long each.
113}
114
115// 64-bit vector move with dpp. Expanded post-RA.
116def V_MOV_B64_DPP_PSEUDO : VOP_DPP_Pseudo <"v_mov_b64_dpp", VOP_I64_I64> {
117  let Size = 16; // Requires two 8-byte v_mov_b32_dpp to complete.
118}
119
120// 64-bit scalar move immediate instruction. This is used to avoid subregs
121// initialization and allow rematerialization.
122def S_MOV_B64_IMM_PSEUDO : SPseudoInstSI <(outs SReg_64:$sdst),
123                                          (ins i64imm:$src0)> {
124  let isReMaterializable = 1;
125  let isAsCheapAsAMove = 1;
126  let isMoveImm = 1;
127  let SchedRW = [WriteSALU, Write64Bit];
128  let Size = 16; // Needs maximum 2 s_mov_b32 instructions 8 byte long each.
129  let Uses = [];
130}
131
132// Pseudoinstruction for @llvm.amdgcn.wqm. It is turned into a copy after the
133// WQM pass processes it.
134def WQM : PseudoInstSI <(outs unknown:$vdst), (ins unknown:$src0)>;
135
136// Pseudoinstruction for @llvm.amdgcn.softwqm. Like @llvm.amdgcn.wqm it is
137// turned into a copy by WQM pass, but does not seed WQM requirements.
138def SOFT_WQM : PseudoInstSI <(outs unknown:$vdst), (ins unknown:$src0)>;
139
140// Pseudoinstruction for @llvm.amdgcn.strict.wwm. It is turned into a copy post-RA, so
141// that the @earlyclobber is respected. The @earlyclobber is to make sure that
142// the instruction that defines $src0 (which is run in Whole Wave Mode) doesn't
143// accidentally clobber inactive channels of $vdst.
144let Constraints = "@earlyclobber $vdst" in {
145def STRICT_WWM : PseudoInstSI <(outs unknown:$vdst), (ins unknown:$src0)>;
146def STRICT_WQM : PseudoInstSI <(outs unknown:$vdst), (ins unknown:$src0)>;
147}
148
149} // End let hasSideEffects = 0, mayLoad = 0, mayStore = 0, Uses = [EXEC]
150
151def ENTER_STRICT_WWM : SPseudoInstSI <(outs SReg_1:$sdst), (ins i64imm:$src0)> {
152  let Uses = [EXEC];
153  let Defs = [EXEC, SCC];
154  let hasSideEffects = 0;
155  let mayLoad = 0;
156  let mayStore = 0;
157}
158
159def EXIT_STRICT_WWM : SPseudoInstSI <(outs SReg_1:$sdst), (ins SReg_1:$src0)> {
160  let hasSideEffects = 0;
161  let mayLoad = 0;
162  let mayStore = 0;
163}
164
165def ENTER_STRICT_WQM : SPseudoInstSI <(outs SReg_1:$sdst), (ins i64imm:$src0)> {
166  let Uses = [EXEC];
167  let Defs = [EXEC, SCC];
168  let hasSideEffects = 0;
169  let mayLoad = 0;
170  let mayStore = 0;
171}
172
173def EXIT_STRICT_WQM : SPseudoInstSI <(outs SReg_1:$sdst), (ins SReg_1:$src0)> {
174  let hasSideEffects = 0;
175  let mayLoad = 0;
176  let mayStore = 0;
177}
178
179// Invert the exec mask and overwrite the inactive lanes of dst with inactive,
180// restoring it after we're done.
181let Defs = [SCC] in {
182def V_SET_INACTIVE_B32 : VPseudoInstSI <(outs VGPR_32:$vdst),
183  (ins VGPR_32: $src, VSrc_b32:$inactive),
184  [(set i32:$vdst, (int_amdgcn_set_inactive i32:$src, i32:$inactive))]> {
185  let Constraints = "$src = $vdst";
186}
187
188def V_SET_INACTIVE_B64 : VPseudoInstSI <(outs VReg_64:$vdst),
189  (ins VReg_64: $src, VSrc_b64:$inactive),
190  [(set i64:$vdst, (int_amdgcn_set_inactive i64:$src, i64:$inactive))]> {
191  let Constraints = "$src = $vdst";
192}
193} // End Defs = [SCC]
194
195let usesCustomInserter = 1, Defs = [VCC, EXEC] in {
196def V_ADD_U64_PSEUDO : VPseudoInstSI <
197  (outs VReg_64:$vdst), (ins VSrc_b64:$src0, VSrc_b64:$src1),
198  [(set VReg_64:$vdst, (getDivergentFrag<add>.ret i64:$src0, i64:$src1))]
199>;
200
201def V_SUB_U64_PSEUDO : VPseudoInstSI <
202  (outs VReg_64:$vdst), (ins VSrc_b64:$src0, VSrc_b64:$src1),
203  [(set VReg_64:$vdst, (getDivergentFrag<sub>.ret i64:$src0, i64:$src1))]
204>;
205} // End usesCustomInserter = 1, Defs = [VCC, EXEC]
206
207let usesCustomInserter = 1, Defs = [SCC] in {
208def S_ADD_U64_PSEUDO : SPseudoInstSI <
209  (outs SReg_64:$sdst), (ins SSrc_b64:$src0, SSrc_b64:$src1),
210  [(set SReg_64:$sdst, (UniformBinFrag<add> i64:$src0, i64:$src1))]
211>;
212
213def S_SUB_U64_PSEUDO : SPseudoInstSI <
214  (outs SReg_64:$sdst), (ins SSrc_b64:$src0, SSrc_b64:$src1),
215  [(set SReg_64:$sdst, (UniformBinFrag<sub> i64:$src0, i64:$src1))]
216>;
217
218def S_ADD_U64_CO_PSEUDO : SPseudoInstSI <
219  (outs SReg_64:$vdst, VOPDstS64orS32:$sdst), (ins SSrc_b64:$src0, SSrc_b64:$src1)
220>;
221
222def S_SUB_U64_CO_PSEUDO : SPseudoInstSI <
223  (outs SReg_64:$vdst, VOPDstS64orS32:$sdst), (ins SSrc_b64:$src0, SSrc_b64:$src1)
224>;
225
226def S_ADD_CO_PSEUDO : SPseudoInstSI <
227  (outs SReg_32:$sdst, SSrc_i1:$scc_out), (ins SSrc_b32:$src0, SSrc_b32:$src1, SSrc_i1:$scc_in)
228>;
229
230def S_SUB_CO_PSEUDO : SPseudoInstSI <
231  (outs SReg_32:$sdst, SSrc_i1:$scc_out), (ins SSrc_b32:$src0, SSrc_b32:$src1, SSrc_i1:$scc_in)
232>;
233
234def S_UADDO_PSEUDO : SPseudoInstSI <
235  (outs SReg_32:$sdst, SSrc_i1:$scc_out), (ins SSrc_b32:$src0, SSrc_b32:$src1)
236>;
237
238def S_USUBO_PSEUDO : SPseudoInstSI <
239  (outs SReg_32:$sdst, SSrc_i1:$scc_out), (ins SSrc_b32:$src0, SSrc_b32:$src1)
240>;
241
242} // End usesCustomInserter = 1, Defs = [SCC]
243
244let usesCustomInserter = 1 in {
245def GET_GROUPSTATICSIZE : SPseudoInstSI <(outs SReg_32:$sdst), (ins),
246  [(set SReg_32:$sdst, (int_amdgcn_groupstaticsize))]>;
247} // End let usesCustomInserter = 1, SALU = 1
248
249// Wrap an instruction by duplicating it, except for setting isTerminator.
250class WrapTerminatorInst<SOP_Pseudo base_inst> : SPseudoInstSI<
251      base_inst.OutOperandList,
252      base_inst.InOperandList> {
253  let Uses = base_inst.Uses;
254  let Defs = base_inst.Defs;
255  let isTerminator = 1;
256  let isAsCheapAsAMove = base_inst.isAsCheapAsAMove;
257  let hasSideEffects = base_inst.hasSideEffects;
258  let UseNamedOperandTable = base_inst.UseNamedOperandTable;
259  let CodeSize = base_inst.CodeSize;
260  let SchedRW = base_inst.SchedRW;
261}
262
263let WaveSizePredicate = isWave64 in {
264def S_MOV_B64_term : WrapTerminatorInst<S_MOV_B64>;
265def S_XOR_B64_term : WrapTerminatorInst<S_XOR_B64>;
266def S_OR_B64_term : WrapTerminatorInst<S_OR_B64>;
267def S_ANDN2_B64_term : WrapTerminatorInst<S_ANDN2_B64>;
268def S_AND_B64_term : WrapTerminatorInst<S_AND_B64>;
269}
270
271let WaveSizePredicate = isWave32 in {
272def S_MOV_B32_term : WrapTerminatorInst<S_MOV_B32>;
273def S_XOR_B32_term : WrapTerminatorInst<S_XOR_B32>;
274def S_OR_B32_term : WrapTerminatorInst<S_OR_B32>;
275def S_ANDN2_B32_term : WrapTerminatorInst<S_ANDN2_B32>;
276def S_AND_B32_term : WrapTerminatorInst<S_AND_B32>;
277}
278
279
280def WAVE_BARRIER : SPseudoInstSI<(outs), (ins),
281  [(int_amdgcn_wave_barrier)]> {
282  let SchedRW = [];
283  let hasNoSchedulingInfo = 1;
284  let hasSideEffects = 1;
285  let mayLoad = 0;
286  let mayStore = 0;
287  let isConvergent = 1;
288  let FixedSize = 1;
289  let Size = 0;
290}
291
292// SI pseudo instructions. These are used by the CFG structurizer pass
293// and should be lowered to ISA instructions prior to codegen.
294
295let isTerminator = 1 in {
296
297let OtherPredicates = [EnableLateCFGStructurize] in {
298 def SI_NON_UNIFORM_BRCOND_PSEUDO : CFPseudoInstSI <
299  (outs),
300  (ins SReg_1:$vcc, brtarget:$target),
301  [(brcond i1:$vcc, bb:$target)]> {
302    let Size = 12;
303}
304}
305
306def SI_IF: CFPseudoInstSI <
307  (outs SReg_1:$dst), (ins SReg_1:$vcc, brtarget:$target),
308  [(set i1:$dst, (AMDGPUif i1:$vcc, bb:$target))], 1, 1> {
309  let Constraints = "";
310  let Size = 12;
311  let hasSideEffects = 1;
312}
313
314def SI_ELSE : CFPseudoInstSI <
315  (outs SReg_1:$dst),
316  (ins SReg_1:$src, brtarget:$target), [], 1, 1> {
317  let Size = 12;
318  let hasSideEffects = 1;
319}
320
321def SI_WATERFALL_LOOP : CFPseudoInstSI <
322  (outs),
323  (ins brtarget:$target), [], 1> {
324  let Size = 8;
325  let isBranch = 1;
326  let Defs = [];
327}
328
329def SI_LOOP : CFPseudoInstSI <
330  (outs), (ins SReg_1:$saved, brtarget:$target),
331  [(AMDGPUloop i1:$saved, bb:$target)], 1, 1> {
332  let Size = 8;
333  let isBranch = 1;
334  let hasSideEffects = 1;
335}
336
337} // End isTerminator = 1
338
339def SI_END_CF : CFPseudoInstSI <
340  (outs), (ins SReg_1:$saved), [], 1, 1> {
341  let Size = 4;
342  let isAsCheapAsAMove = 1;
343  let isReMaterializable = 1;
344  let hasSideEffects = 1;
345  let mayLoad = 1; // FIXME: Should not need memory flags
346  let mayStore = 1;
347}
348
349def SI_IF_BREAK : CFPseudoInstSI <
350  (outs SReg_1:$dst), (ins SReg_1:$vcc, SReg_1:$src), []> {
351  let Size = 4;
352  let isAsCheapAsAMove = 1;
353  let isReMaterializable = 1;
354}
355
356// Branch to the early termination block of the shader if SCC is 0.
357// This uses SCC from a previous SALU operation, i.e. the update of
358// a mask of live lanes after a kill/demote operation.
359// Only valid in pixel shaders.
360def SI_EARLY_TERMINATE_SCC0 : SPseudoInstSI <(outs), (ins)> {
361  let Uses = [EXEC,SCC];
362}
363
364let Uses = [EXEC] in {
365
366multiclass PseudoInstKill <dag ins> {
367  // Even though this pseudo can usually be expanded without an SCC def, we
368  // conservatively assume that it has an SCC def, both because it is sometimes
369  // required in degenerate cases (when V_CMPX cannot be used due to constant
370  // bus limitations) and because it allows us to avoid having to track SCC
371  // liveness across basic blocks.
372  let Defs = [EXEC,SCC] in
373  def _PSEUDO : PseudoInstSI <(outs), ins> {
374    let isConvergent = 1;
375    let usesCustomInserter = 1;
376  }
377
378  let Defs = [EXEC,SCC] in
379  def _TERMINATOR : SPseudoInstSI <(outs), ins> {
380    let isTerminator = 1;
381  }
382}
383
384defm SI_KILL_I1 : PseudoInstKill <(ins SCSrc_i1:$src, i1imm:$killvalue)>;
385let Defs = [VCC] in
386defm SI_KILL_F32_COND_IMM : PseudoInstKill <(ins VSrc_b32:$src0, i32imm:$src1, i32imm:$cond)>;
387
388let Defs = [EXEC,VCC] in
389def SI_ILLEGAL_COPY : SPseudoInstSI <
390  (outs unknown:$dst), (ins unknown:$src),
391  [], " ; illegal copy $src to $dst">;
392
393} // End Uses = [EXEC], Defs = [EXEC,VCC]
394
395// Branch on undef scc. Used to avoid intermediate copy from
396// IMPLICIT_DEF to SCC.
397def SI_BR_UNDEF : SPseudoInstSI <(outs), (ins sopp_brtarget:$simm16)> {
398  let isTerminator = 1;
399  let usesCustomInserter = 1;
400  let isBranch = 1;
401}
402
403def SI_PS_LIVE : PseudoInstSI <
404  (outs SReg_1:$dst), (ins),
405  [(set i1:$dst, (int_amdgcn_ps_live))]> {
406  let SALU = 1;
407}
408
409let Uses = [EXEC] in {
410def SI_LIVE_MASK : PseudoInstSI <
411  (outs SReg_1:$dst), (ins),
412  [(set i1:$dst, (int_amdgcn_live_mask))]> {
413  let SALU = 1;
414}
415let Defs = [EXEC,SCC] in {
416// Demote: Turn a pixel shader thread into a helper lane.
417def SI_DEMOTE_I1 : SPseudoInstSI <(outs), (ins SCSrc_i1:$src, i1imm:$killvalue)>;
418} // End Defs = [EXEC,SCC]
419} // End Uses = [EXEC]
420
421def SI_MASKED_UNREACHABLE : SPseudoInstSI <(outs), (ins),
422  [(int_amdgcn_unreachable)],
423  "; divergent unreachable"> {
424  let Size = 0;
425  let hasNoSchedulingInfo = 1;
426  let FixedSize = 1;
427}
428
429// Used as an isel pseudo to directly emit initialization with an
430// s_mov_b32 rather than a copy of another initialized
431// register. MachineCSE skips copies, and we don't want to have to
432// fold operands before it runs.
433def SI_INIT_M0 : SPseudoInstSI <(outs), (ins SSrc_b32:$src)> {
434  let Defs = [M0];
435  let usesCustomInserter = 1;
436  let isAsCheapAsAMove = 1;
437  let isReMaterializable = 1;
438}
439
440def SI_INIT_EXEC : SPseudoInstSI <
441  (outs), (ins i64imm:$src),
442  [(int_amdgcn_init_exec (i64 timm:$src))]> {
443  let Defs = [EXEC];
444  let isAsCheapAsAMove = 1;
445}
446
447def SI_INIT_EXEC_FROM_INPUT : SPseudoInstSI <
448  (outs), (ins SSrc_b32:$input, i32imm:$shift),
449  [(int_amdgcn_init_exec_from_input i32:$input, (i32 timm:$shift))]> {
450  let Defs = [EXEC];
451}
452
453// Return for returning shaders to a shader variant epilog.
454def SI_RETURN_TO_EPILOG : SPseudoInstSI <
455  (outs), (ins variable_ops), [(AMDGPUreturn_to_epilog)]> {
456  let isTerminator = 1;
457  let isBarrier = 1;
458  let isReturn = 1;
459  let hasNoSchedulingInfo = 1;
460  let DisableWQM = 1;
461  let FixedSize = 1;
462}
463
464// Return for returning function calls.
465def SI_RETURN : SPseudoInstSI <
466  (outs), (ins), [],
467  "; return"> {
468  let isTerminator = 1;
469  let isBarrier = 1;
470  let isReturn = 1;
471  let SchedRW = [WriteBranch];
472}
473
474// Return for returning function calls without output register.
475//
476// This version is only needed so we can fill in the output register
477// in the custom inserter.
478def SI_CALL_ISEL : SPseudoInstSI <
479  (outs), (ins SSrc_b64:$src0, unknown:$callee),
480  [(AMDGPUcall i64:$src0, tglobaladdr:$callee)]> {
481  let Size = 4;
482  let isCall = 1;
483  let SchedRW = [WriteBranch];
484  let usesCustomInserter = 1;
485  // TODO: Should really base this on the call target
486  let isConvergent = 1;
487}
488
489def : GCNPat<
490  (AMDGPUcall i64:$src0, (i64 0)),
491  (SI_CALL_ISEL $src0, (i64 0))
492>;
493
494// Wrapper around s_swappc_b64 with extra $callee parameter to track
495// the called function after regalloc.
496def SI_CALL : SPseudoInstSI <
497  (outs SReg_64:$dst), (ins SSrc_b64:$src0, unknown:$callee)> {
498  let Size = 4;
499  let isCall = 1;
500  let UseNamedOperandTable = 1;
501  let SchedRW = [WriteBranch];
502  // TODO: Should really base this on the call target
503  let isConvergent = 1;
504}
505
506// Tail call handling pseudo
507def SI_TCRETURN : SPseudoInstSI <(outs),
508  (ins SReg_64:$src0, unknown:$callee, i32imm:$fpdiff),
509  [(AMDGPUtc_return i64:$src0, tglobaladdr:$callee, i32:$fpdiff)]> {
510  let Size = 4;
511  let isCall = 1;
512  let isTerminator = 1;
513  let isReturn = 1;
514  let isBarrier = 1;
515  let UseNamedOperandTable = 1;
516  let SchedRW = [WriteBranch];
517  // TODO: Should really base this on the call target
518  let isConvergent = 1;
519}
520
521// Handle selecting indirect tail calls
522def : GCNPat<
523  (AMDGPUtc_return i64:$src0, (i64 0), (i32 timm:$fpdiff)),
524  (SI_TCRETURN SReg_64:$src0, (i64 0), i32imm:$fpdiff)
525>;
526
527def ADJCALLSTACKUP : SPseudoInstSI<
528  (outs), (ins i32imm:$amt0, i32imm:$amt1),
529  [(callseq_start timm:$amt0, timm:$amt1)],
530  "; adjcallstackup $amt0 $amt1"> {
531  let Size = 8; // Worst case. (s_add_u32 + constant)
532  let FixedSize = 1;
533  let hasSideEffects = 1;
534  let usesCustomInserter = 1;
535  let SchedRW = [WriteSALU];
536  let Defs = [SCC];
537}
538
539def ADJCALLSTACKDOWN : SPseudoInstSI<
540  (outs), (ins i32imm:$amt1, i32imm:$amt2),
541  [(callseq_end timm:$amt1, timm:$amt2)],
542  "; adjcallstackdown $amt1"> {
543  let Size = 8; // Worst case. (s_add_u32 + constant)
544  let hasSideEffects = 1;
545  let usesCustomInserter = 1;
546  let SchedRW = [WriteSALU];
547  let Defs = [SCC];
548}
549
550let Defs = [M0, EXEC, SCC],
551  UseNamedOperandTable = 1 in {
552
553// SI_INDIRECT_SRC/DST are only used by legacy SelectionDAG indirect
554// addressing implementation.
555class SI_INDIRECT_SRC<RegisterClass rc> : VPseudoInstSI <
556  (outs VGPR_32:$vdst),
557  (ins rc:$src, VS_32:$idx, i32imm:$offset)> {
558  let usesCustomInserter = 1;
559}
560
561class SI_INDIRECT_DST<RegisterClass rc> : VPseudoInstSI <
562  (outs rc:$vdst),
563  (ins rc:$src, VS_32:$idx, i32imm:$offset, VGPR_32:$val)> {
564  let Constraints = "$src = $vdst";
565  let usesCustomInserter = 1;
566}
567
568def SI_INDIRECT_SRC_V1 : SI_INDIRECT_SRC<VGPR_32>;
569def SI_INDIRECT_SRC_V2 : SI_INDIRECT_SRC<VReg_64>;
570def SI_INDIRECT_SRC_V4 : SI_INDIRECT_SRC<VReg_128>;
571def SI_INDIRECT_SRC_V8 : SI_INDIRECT_SRC<VReg_256>;
572def SI_INDIRECT_SRC_V16 : SI_INDIRECT_SRC<VReg_512>;
573def SI_INDIRECT_SRC_V32 : SI_INDIRECT_SRC<VReg_1024>;
574
575def SI_INDIRECT_DST_V1 : SI_INDIRECT_DST<VGPR_32>;
576def SI_INDIRECT_DST_V2 : SI_INDIRECT_DST<VReg_64>;
577def SI_INDIRECT_DST_V4 : SI_INDIRECT_DST<VReg_128>;
578def SI_INDIRECT_DST_V8 : SI_INDIRECT_DST<VReg_256>;
579def SI_INDIRECT_DST_V16 : SI_INDIRECT_DST<VReg_512>;
580def SI_INDIRECT_DST_V32 : SI_INDIRECT_DST<VReg_1024>;
581
582} // End Uses = [EXEC], Defs = [M0, EXEC]
583
584// This is a pseudo variant of the v_movreld_b32 instruction in which the
585// vector operand appears only twice, once as def and once as use. Using this
586// pseudo avoids problems with the Two Address instructions pass.
587class INDIRECT_REG_WRITE_MOVREL_pseudo<RegisterClass rc,
588                                RegisterOperand val_ty> : PseudoInstSI <
589  (outs rc:$vdst), (ins rc:$vsrc, val_ty:$val, i32imm:$subreg)> {
590  let Constraints = "$vsrc = $vdst";
591  let Uses = [M0];
592}
593
594class V_INDIRECT_REG_WRITE_MOVREL_B32_pseudo<RegisterClass rc> :
595  INDIRECT_REG_WRITE_MOVREL_pseudo<rc, VSrc_b32> {
596  let VALU = 1;
597  let VOP1 = 1;
598  let Uses = [M0, EXEC];
599}
600
601class S_INDIRECT_REG_WRITE_MOVREL_pseudo<RegisterClass rc,
602                                  RegisterOperand val_ty> :
603  INDIRECT_REG_WRITE_MOVREL_pseudo<rc, val_ty> {
604  let SALU = 1;
605  let SOP1 = 1;
606  let Uses = [M0];
607}
608
609class S_INDIRECT_REG_WRITE_MOVREL_B32_pseudo<RegisterClass rc> :
610  S_INDIRECT_REG_WRITE_MOVREL_pseudo<rc, SSrc_b32>;
611class S_INDIRECT_REG_WRITE_MOVREL_B64_pseudo<RegisterClass rc> :
612  S_INDIRECT_REG_WRITE_MOVREL_pseudo<rc, SSrc_b64>;
613
614def V_INDIRECT_REG_WRITE_MOVREL_B32_V1 : V_INDIRECT_REG_WRITE_MOVREL_B32_pseudo<VGPR_32>;
615def V_INDIRECT_REG_WRITE_MOVREL_B32_V2 : V_INDIRECT_REG_WRITE_MOVREL_B32_pseudo<VReg_64>;
616def V_INDIRECT_REG_WRITE_MOVREL_B32_V3 : V_INDIRECT_REG_WRITE_MOVREL_B32_pseudo<VReg_96>;
617def V_INDIRECT_REG_WRITE_MOVREL_B32_V4 : V_INDIRECT_REG_WRITE_MOVREL_B32_pseudo<VReg_128>;
618def V_INDIRECT_REG_WRITE_MOVREL_B32_V5 : V_INDIRECT_REG_WRITE_MOVREL_B32_pseudo<VReg_160>;
619def V_INDIRECT_REG_WRITE_MOVREL_B32_V8 : V_INDIRECT_REG_WRITE_MOVREL_B32_pseudo<VReg_256>;
620def V_INDIRECT_REG_WRITE_MOVREL_B32_V16 : V_INDIRECT_REG_WRITE_MOVREL_B32_pseudo<VReg_512>;
621def V_INDIRECT_REG_WRITE_MOVREL_B32_V32 : V_INDIRECT_REG_WRITE_MOVREL_B32_pseudo<VReg_1024>;
622
623def S_INDIRECT_REG_WRITE_MOVREL_B32_V1 : S_INDIRECT_REG_WRITE_MOVREL_B32_pseudo<SReg_32>;
624def S_INDIRECT_REG_WRITE_MOVREL_B32_V2 : S_INDIRECT_REG_WRITE_MOVREL_B32_pseudo<SReg_64>;
625def S_INDIRECT_REG_WRITE_MOVREL_B32_V3 : S_INDIRECT_REG_WRITE_MOVREL_B32_pseudo<SReg_96>;
626def S_INDIRECT_REG_WRITE_MOVREL_B32_V4 : S_INDIRECT_REG_WRITE_MOVREL_B32_pseudo<SReg_128>;
627def S_INDIRECT_REG_WRITE_MOVREL_B32_V5 : S_INDIRECT_REG_WRITE_MOVREL_B32_pseudo<SReg_160>;
628def S_INDIRECT_REG_WRITE_MOVREL_B32_V8 : S_INDIRECT_REG_WRITE_MOVREL_B32_pseudo<SReg_256>;
629def S_INDIRECT_REG_WRITE_MOVREL_B32_V16 : S_INDIRECT_REG_WRITE_MOVREL_B32_pseudo<SReg_512>;
630def S_INDIRECT_REG_WRITE_MOVREL_B32_V32 : S_INDIRECT_REG_WRITE_MOVREL_B32_pseudo<SReg_1024>;
631
632def S_INDIRECT_REG_WRITE_MOVREL_B64_V1 : S_INDIRECT_REG_WRITE_MOVREL_B64_pseudo<SReg_64>;
633def S_INDIRECT_REG_WRITE_MOVREL_B64_V2 : S_INDIRECT_REG_WRITE_MOVREL_B64_pseudo<SReg_128>;
634def S_INDIRECT_REG_WRITE_MOVREL_B64_V4 : S_INDIRECT_REG_WRITE_MOVREL_B64_pseudo<SReg_256>;
635def S_INDIRECT_REG_WRITE_MOVREL_B64_V8 : S_INDIRECT_REG_WRITE_MOVREL_B64_pseudo<SReg_512>;
636def S_INDIRECT_REG_WRITE_MOVREL_B64_V16 : S_INDIRECT_REG_WRITE_MOVREL_B64_pseudo<SReg_1024>;
637
638// These variants of V_INDIRECT_REG_READ/WRITE use VGPR indexing. By using these
639// pseudos we avoid spills or copies being inserted within indirect sequences
640// that switch the VGPR indexing mode. Spills to accvgprs could be effected by
641// this mode switching.
642
643class V_INDIRECT_REG_WRITE_GPR_IDX_pseudo<RegisterClass rc> : PseudoInstSI <
644  (outs rc:$vdst), (ins rc:$vsrc, VSrc_b32:$val, SSrc_b32:$idx, i32imm:$subreg)> {
645  let Constraints = "$vsrc = $vdst";
646  let VALU = 1;
647  let Uses = [M0, EXEC];
648  let Defs = [M0];
649}
650
651def V_INDIRECT_REG_WRITE_GPR_IDX_B32_V1 : V_INDIRECT_REG_WRITE_GPR_IDX_pseudo<VGPR_32>;
652def V_INDIRECT_REG_WRITE_GPR_IDX_B32_V2 : V_INDIRECT_REG_WRITE_GPR_IDX_pseudo<VReg_64>;
653def V_INDIRECT_REG_WRITE_GPR_IDX_B32_V3 : V_INDIRECT_REG_WRITE_GPR_IDX_pseudo<VReg_96>;
654def V_INDIRECT_REG_WRITE_GPR_IDX_B32_V4 : V_INDIRECT_REG_WRITE_GPR_IDX_pseudo<VReg_128>;
655def V_INDIRECT_REG_WRITE_GPR_IDX_B32_V5 : V_INDIRECT_REG_WRITE_GPR_IDX_pseudo<VReg_160>;
656def V_INDIRECT_REG_WRITE_GPR_IDX_B32_V8 : V_INDIRECT_REG_WRITE_GPR_IDX_pseudo<VReg_256>;
657def V_INDIRECT_REG_WRITE_GPR_IDX_B32_V16 : V_INDIRECT_REG_WRITE_GPR_IDX_pseudo<VReg_512>;
658def V_INDIRECT_REG_WRITE_GPR_IDX_B32_V32 : V_INDIRECT_REG_WRITE_GPR_IDX_pseudo<VReg_1024>;
659
660class V_INDIRECT_REG_READ_GPR_IDX_pseudo<RegisterClass rc> : PseudoInstSI <
661  (outs VGPR_32:$vdst), (ins rc:$vsrc, SSrc_b32:$idx, i32imm:$subreg)> {
662  let VALU = 1;
663  let Uses = [M0, EXEC];
664  let Defs = [M0];
665}
666
667def V_INDIRECT_REG_READ_GPR_IDX_B32_V1 : V_INDIRECT_REG_READ_GPR_IDX_pseudo<VGPR_32>;
668def V_INDIRECT_REG_READ_GPR_IDX_B32_V2 : V_INDIRECT_REG_READ_GPR_IDX_pseudo<VReg_64>;
669def V_INDIRECT_REG_READ_GPR_IDX_B32_V3 : V_INDIRECT_REG_READ_GPR_IDX_pseudo<VReg_96>;
670def V_INDIRECT_REG_READ_GPR_IDX_B32_V4 : V_INDIRECT_REG_READ_GPR_IDX_pseudo<VReg_128>;
671def V_INDIRECT_REG_READ_GPR_IDX_B32_V5 : V_INDIRECT_REG_READ_GPR_IDX_pseudo<VReg_160>;
672def V_INDIRECT_REG_READ_GPR_IDX_B32_V8 : V_INDIRECT_REG_READ_GPR_IDX_pseudo<VReg_256>;
673def V_INDIRECT_REG_READ_GPR_IDX_B32_V16 : V_INDIRECT_REG_READ_GPR_IDX_pseudo<VReg_512>;
674def V_INDIRECT_REG_READ_GPR_IDX_B32_V32 : V_INDIRECT_REG_READ_GPR_IDX_pseudo<VReg_1024>;
675
676multiclass SI_SPILL_SGPR <RegisterClass sgpr_class> {
677  let UseNamedOperandTable = 1, SGPRSpill = 1, Uses = [EXEC] in {
678    def _SAVE : PseudoInstSI <
679      (outs),
680      (ins sgpr_class:$data, i32imm:$addr)> {
681      let mayStore = 1;
682      let mayLoad = 0;
683    }
684
685    def _RESTORE : PseudoInstSI <
686      (outs sgpr_class:$data),
687      (ins i32imm:$addr)> {
688      let mayStore = 0;
689      let mayLoad = 1;
690    }
691  } // End UseNamedOperandTable = 1
692}
693
694// You cannot use M0 as the output of v_readlane_b32 instructions or
695// use it in the sdata operand of SMEM instructions. We still need to
696// be able to spill the physical register m0, so allow it for
697// SI_SPILL_32_* instructions.
698defm SI_SPILL_S32  : SI_SPILL_SGPR <SReg_32>;
699defm SI_SPILL_S64  : SI_SPILL_SGPR <SReg_64>;
700defm SI_SPILL_S96  : SI_SPILL_SGPR <SReg_96>;
701defm SI_SPILL_S128 : SI_SPILL_SGPR <SReg_128>;
702defm SI_SPILL_S160 : SI_SPILL_SGPR <SReg_160>;
703defm SI_SPILL_S192 : SI_SPILL_SGPR <SReg_192>;
704defm SI_SPILL_S224 : SI_SPILL_SGPR <SReg_224>;
705defm SI_SPILL_S256 : SI_SPILL_SGPR <SReg_256>;
706defm SI_SPILL_S512 : SI_SPILL_SGPR <SReg_512>;
707defm SI_SPILL_S1024 : SI_SPILL_SGPR <SReg_1024>;
708
709// VGPR or AGPR spill instructions. In case of AGPR spilling a temp register
710// needs to be used and an extra instruction to move between VGPR and AGPR.
711// UsesTmp adds to the total size of an expanded spill in this case.
712multiclass SI_SPILL_VGPR <RegisterClass vgpr_class, bit UsesTmp = 0> {
713  let UseNamedOperandTable = 1, VGPRSpill = 1,
714       SchedRW = [WriteVMEM] in {
715    def _SAVE : VPseudoInstSI <
716      (outs),
717      (ins vgpr_class:$vdata, i32imm:$vaddr,
718           SReg_32:$soffset, i32imm:$offset)> {
719      let mayStore = 1;
720      let mayLoad = 0;
721      // (2 * 4) + (8 * num_subregs) bytes maximum
722      int MaxSize = !add(!shl(!srl(vgpr_class.Size, 5), !add(UsesTmp, 3)), 8);
723      // Size field is unsigned char and cannot fit more.
724      let Size = !if(!le(MaxSize, 256), MaxSize, 252);
725    }
726
727    def _RESTORE : VPseudoInstSI <
728      (outs vgpr_class:$vdata),
729      (ins i32imm:$vaddr,
730           SReg_32:$soffset, i32imm:$offset)> {
731      let mayStore = 0;
732      let mayLoad = 1;
733
734      // (2 * 4) + (8 * num_subregs) bytes maximum
735      int MaxSize = !add(!shl(!srl(vgpr_class.Size, 5), !add(UsesTmp, 3)), 8);
736      // Size field is unsigned char and cannot fit more.
737      let Size = !if(!le(MaxSize, 256), MaxSize, 252);
738    }
739  } // End UseNamedOperandTable = 1, VGPRSpill = 1, SchedRW = [WriteVMEM]
740}
741
742defm SI_SPILL_V32  : SI_SPILL_VGPR <VGPR_32>;
743defm SI_SPILL_V64  : SI_SPILL_VGPR <VReg_64>;
744defm SI_SPILL_V96  : SI_SPILL_VGPR <VReg_96>;
745defm SI_SPILL_V128 : SI_SPILL_VGPR <VReg_128>;
746defm SI_SPILL_V160 : SI_SPILL_VGPR <VReg_160>;
747defm SI_SPILL_V192 : SI_SPILL_VGPR <VReg_192>;
748defm SI_SPILL_V224 : SI_SPILL_VGPR <VReg_224>;
749defm SI_SPILL_V256 : SI_SPILL_VGPR <VReg_256>;
750defm SI_SPILL_V512 : SI_SPILL_VGPR <VReg_512>;
751defm SI_SPILL_V1024 : SI_SPILL_VGPR <VReg_1024>;
752
753defm SI_SPILL_A32  : SI_SPILL_VGPR <AGPR_32, 1>;
754defm SI_SPILL_A64  : SI_SPILL_VGPR <AReg_64, 1>;
755defm SI_SPILL_A96  : SI_SPILL_VGPR <AReg_96, 1>;
756defm SI_SPILL_A128 : SI_SPILL_VGPR <AReg_128, 1>;
757defm SI_SPILL_A160 : SI_SPILL_VGPR <AReg_160, 1>;
758defm SI_SPILL_A192 : SI_SPILL_VGPR <AReg_192, 1>;
759defm SI_SPILL_A224 : SI_SPILL_VGPR <AReg_224, 1>;
760defm SI_SPILL_A256 : SI_SPILL_VGPR <AReg_256, 1>;
761defm SI_SPILL_A512 : SI_SPILL_VGPR <AReg_512, 1>;
762defm SI_SPILL_A1024 : SI_SPILL_VGPR <AReg_1024, 1>;
763
764def SI_PC_ADD_REL_OFFSET : SPseudoInstSI <
765  (outs SReg_64:$dst),
766  (ins si_ga:$ptr_lo, si_ga:$ptr_hi),
767  [(set SReg_64:$dst,
768      (i64 (SIpc_add_rel_offset tglobaladdr:$ptr_lo, tglobaladdr:$ptr_hi)))]> {
769  let Defs = [SCC];
770}
771
772def : GCNPat <
773  (SIpc_add_rel_offset tglobaladdr:$ptr_lo, 0),
774  (SI_PC_ADD_REL_OFFSET $ptr_lo, (i32 0))
775>;
776
777def : GCNPat<
778  (AMDGPUtrap timm:$trapid),
779  (S_TRAP $trapid)
780>;
781
782def : GCNPat<
783  (AMDGPUelse i1:$src, bb:$target),
784  (SI_ELSE $src, $target)
785>;
786
787def : Pat <
788  (int_amdgcn_kill i1:$src),
789  (SI_KILL_I1_PSEUDO SCSrc_i1:$src, 0)
790>;
791
792def : Pat <
793  (int_amdgcn_kill (i1 (not i1:$src))),
794  (SI_KILL_I1_PSEUDO SCSrc_i1:$src, -1)
795>;
796
797def : Pat <
798  (int_amdgcn_kill (i1 (setcc f32:$src, InlineImmFP32:$imm, cond:$cond))),
799  (SI_KILL_F32_COND_IMM_PSEUDO VSrc_b32:$src, (bitcast_fpimm_to_i32 $imm), (cond_as_i32imm $cond))
800>;
801
802def : Pat <
803  (int_amdgcn_wqm_demote i1:$src),
804  (SI_DEMOTE_I1 SCSrc_i1:$src, 0)
805>;
806
807def : Pat <
808  (int_amdgcn_wqm_demote (i1 (not i1:$src))),
809  (SI_DEMOTE_I1 SCSrc_i1:$src, -1)
810>;
811
812  // TODO: we could add more variants for other types of conditionals
813
814def : Pat <
815  (i64 (int_amdgcn_icmp i1:$src, (i1 0), (i32 33))),
816  (COPY $src) // Return the SGPRs representing i1 src
817>;
818
819def : Pat <
820  (i32 (int_amdgcn_icmp i1:$src, (i1 0), (i32 33))),
821  (COPY $src) // Return the SGPRs representing i1 src
822>;
823
824//===----------------------------------------------------------------------===//
825// VOP1 Patterns
826//===----------------------------------------------------------------------===//
827
828let OtherPredicates = [UnsafeFPMath] in {
829
830//defm : RsqPat<V_RSQ_F32_e32, f32>;
831
832def : RsqPat<V_RSQ_F32_e32, f32>;
833
834// Convert (x - floor(x)) to fract(x)
835def : GCNPat <
836  (f32 (fsub (f32 (VOP3Mods f32:$x, i32:$mods)),
837             (f32 (ffloor (f32 (VOP3Mods f32:$x, i32:$mods)))))),
838  (V_FRACT_F32_e64 $mods, $x)
839>;
840
841// Convert (x + (-floor(x))) to fract(x)
842def : GCNPat <
843  (f64 (fadd (f64 (VOP3Mods f64:$x, i32:$mods)),
844             (f64 (fneg (f64 (ffloor (f64 (VOP3Mods f64:$x, i32:$mods)))))))),
845  (V_FRACT_F64_e64 $mods, $x)
846>;
847
848} // End OtherPredicates = [UnsafeFPMath]
849
850
851// f16_to_fp patterns
852def : GCNPat <
853  (f32 (f16_to_fp i32:$src0)),
854  (V_CVT_F32_F16_e64 SRCMODS.NONE, $src0)
855>;
856
857def : GCNPat <
858  (f32 (f16_to_fp (and_oneuse i32:$src0, 0x7fff))),
859  (V_CVT_F32_F16_e64 SRCMODS.ABS, $src0)
860>;
861
862def : GCNPat <
863  (f32 (f16_to_fp (i32 (srl_oneuse (and_oneuse i32:$src0, 0x7fff0000), (i32 16))))),
864  (V_CVT_F32_F16_e64 SRCMODS.ABS, (i32 (V_LSHRREV_B32_e64 (i32 16), i32:$src0)))
865>;
866
867def : GCNPat <
868  (f32 (f16_to_fp (or_oneuse i32:$src0, 0x8000))),
869  (V_CVT_F32_F16_e64 SRCMODS.NEG_ABS, $src0)
870>;
871
872def : GCNPat <
873  (f32 (f16_to_fp (xor_oneuse i32:$src0, 0x8000))),
874  (V_CVT_F32_F16_e64 SRCMODS.NEG, $src0)
875>;
876
877def : GCNPat <
878  (f64 (fpextend f16:$src)),
879  (V_CVT_F64_F32_e32 (V_CVT_F32_F16_e32 $src))
880>;
881
882// fp_to_fp16 patterns
883def : GCNPat <
884  (i32 (AMDGPUfp_to_f16 (f32 (VOP3Mods f32:$src0, i32:$src0_modifiers)))),
885  (V_CVT_F16_F32_e64 $src0_modifiers, f32:$src0)
886>;
887
888def : GCNPat <
889  (i32 (fp_to_sint f16:$src)),
890  (V_CVT_I32_F32_e32 (V_CVT_F32_F16_e32 VSrc_b32:$src))
891>;
892
893def : GCNPat <
894  (i32 (fp_to_uint f16:$src)),
895  (V_CVT_U32_F32_e32 (V_CVT_F32_F16_e32 VSrc_b32:$src))
896>;
897
898def : GCNPat <
899  (f16 (sint_to_fp i32:$src)),
900  (V_CVT_F16_F32_e32 (V_CVT_F32_I32_e32 VSrc_b32:$src))
901>;
902
903def : GCNPat <
904  (f16 (uint_to_fp i32:$src)),
905  (V_CVT_F16_F32_e32 (V_CVT_F32_U32_e32 VSrc_b32:$src))
906>;
907
908//===----------------------------------------------------------------------===//
909// VOP2 Patterns
910//===----------------------------------------------------------------------===//
911
912// NoMods pattern used for mac. If there are any source modifiers then it's
913// better to select mad instead of mac.
914class FMADPat <ValueType vt, Instruction inst, SDPatternOperator node>
915  : GCNPat <(vt (node (vt (VOP3NoMods vt:$src0)),
916                      (vt (VOP3NoMods vt:$src1)),
917                      (vt (VOP3NoMods vt:$src2)))),
918    (inst SRCMODS.NONE, $src0, SRCMODS.NONE, $src1,
919          SRCMODS.NONE, $src2, DSTCLAMP.NONE, DSTOMOD.NONE)
920>;
921
922// Prefer mac form when there are no modifiers.
923let AddedComplexity = 9 in {
924let OtherPredicates = [HasMadMacF32Insts] in {
925def : FMADPat <f32, V_MAC_F32_e64, fmad>;
926def : FMADPat <f32, V_MAC_F32_e64, AMDGPUfmad_ftz>;
927} // OtherPredicates = [HasMadMacF32Insts]
928
929// Don't allow source modifiers. If there are any source modifiers then it's
930// better to select mad instead of mac.
931let SubtargetPredicate = isGFX6GFX7GFX10,
932    OtherPredicates = [HasMadMacF32Insts, NoFP32Denormals] in
933def : GCNPat <
934      (f32 (fadd (AMDGPUfmul_legacy (VOP3NoMods f32:$src0),
935                                    (VOP3NoMods f32:$src1)),
936                 (VOP3NoMods f32:$src2))),
937      (V_MAC_LEGACY_F32_e64 SRCMODS.NONE, $src0, SRCMODS.NONE, $src1,
938                            SRCMODS.NONE, $src2, DSTCLAMP.NONE, DSTOMOD.NONE)
939>;
940
941// Don't allow source modifiers. If there are any source modifiers then it's
942// better to select fma instead of fmac.
943let SubtargetPredicate = HasFmaLegacy32 in
944def : GCNPat <
945      (f32 (int_amdgcn_fma_legacy (VOP3NoMods f32:$src0),
946                                  (VOP3NoMods f32:$src1),
947                                  (VOP3NoMods f32:$src2))),
948      (V_FMAC_LEGACY_F32_e64 SRCMODS.NONE, $src0, SRCMODS.NONE, $src1,
949                             SRCMODS.NONE, $src2, DSTCLAMP.NONE, DSTOMOD.NONE)
950>;
951
952let SubtargetPredicate = Has16BitInsts in {
953def : FMADPat <f16, V_MAC_F16_e64, fmad>;
954def : FMADPat <f16, V_MAC_F16_e64, AMDGPUfmad_ftz>;
955} // SubtargetPredicate = Has16BitInsts
956} // AddedComplexity = 9
957
958class FMADModsPat<ValueType Ty, Instruction inst, SDPatternOperator mad_opr>
959  : GCNPat<
960  (Ty (mad_opr (Ty (VOP3Mods Ty:$src0, i32:$src0_mod)),
961               (Ty (VOP3Mods Ty:$src1, i32:$src1_mod)),
962               (Ty (VOP3Mods Ty:$src2, i32:$src2_mod)))),
963  (inst $src0_mod, $src0, $src1_mod, $src1,
964  $src2_mod, $src2, DSTCLAMP.NONE, DSTOMOD.NONE)
965>;
966
967let OtherPredicates = [HasMadMacF32Insts] in
968def : FMADModsPat<f32, V_MAD_F32_e64, AMDGPUfmad_ftz>;
969
970let OtherPredicates = [HasMadMacF32Insts, NoFP32Denormals] in
971def : GCNPat <
972      (f32 (fadd (AMDGPUfmul_legacy (VOP3Mods f32:$src0, i32:$src0_mod),
973                                    (VOP3Mods f32:$src1, i32:$src1_mod)),
974                 (VOP3Mods f32:$src2, i32:$src2_mod))),
975      (V_MAD_LEGACY_F32_e64 $src0_mod, $src0, $src1_mod, $src1,
976                        $src2_mod, $src2, DSTCLAMP.NONE, DSTOMOD.NONE)
977>;
978
979let SubtargetPredicate = Has16BitInsts in
980def : FMADModsPat<f16, V_MAD_F16_e64, AMDGPUfmad_ftz>;
981
982class VOPSelectModsPat <ValueType vt> : GCNPat <
983  (vt (select i1:$src0, (VOP3Mods vt:$src1, i32:$src1_mods),
984                        (VOP3Mods vt:$src2, i32:$src2_mods))),
985  (V_CNDMASK_B32_e64 FP32InputMods:$src2_mods, VSrc_b32:$src2,
986                     FP32InputMods:$src1_mods, VSrc_b32:$src1, SSrc_i1:$src0)
987>;
988
989class VOPSelectPat <ValueType vt> : GCNPat <
990  (vt (select i1:$src0, vt:$src1, vt:$src2)),
991  (V_CNDMASK_B32_e64 0, VSrc_b32:$src2, 0, VSrc_b32:$src1, SSrc_i1:$src0)
992>;
993
994def : VOPSelectModsPat <i32>;
995def : VOPSelectModsPat <f32>;
996def : VOPSelectPat <f16>;
997def : VOPSelectPat <i16>;
998
999let AddedComplexity = 1 in {
1000def : GCNPat <
1001  (i32 (add (i32 (getDivergentFrag<ctpop>.ret i32:$popcnt)), i32:$val)),
1002  (V_BCNT_U32_B32_e64 $popcnt, $val)
1003>;
1004}
1005
1006def : GCNPat <
1007  (i32 (ctpop i32:$popcnt)),
1008  (V_BCNT_U32_B32_e64 VSrc_b32:$popcnt, (i32 0))
1009>;
1010
1011def : GCNPat <
1012  (i16 (add (i16 (trunc (i32 (getDivergentFrag<ctpop>.ret i32:$popcnt)))), i16:$val)),
1013  (V_BCNT_U32_B32_e64 $popcnt, $val)
1014>;
1015
1016/********** ============================================ **********/
1017/********** Extraction, Insertion, Building and Casting  **********/
1018/********** ============================================ **********/
1019
1020foreach Index = 0-2 in {
1021  def Extract_Element_v2i32_#Index : Extract_Element <
1022    i32, v2i32, Index, !cast<SubRegIndex>(sub#Index)
1023  >;
1024  def Insert_Element_v2i32_#Index : Insert_Element <
1025    i32, v2i32, Index, !cast<SubRegIndex>(sub#Index)
1026  >;
1027
1028  def Extract_Element_v2f32_#Index : Extract_Element <
1029    f32, v2f32, Index, !cast<SubRegIndex>(sub#Index)
1030  >;
1031  def Insert_Element_v2f32_#Index : Insert_Element <
1032    f32, v2f32, Index, !cast<SubRegIndex>(sub#Index)
1033  >;
1034}
1035
1036foreach Index = 0-2 in {
1037  def Extract_Element_v3i32_#Index : Extract_Element <
1038    i32, v3i32, Index, !cast<SubRegIndex>(sub#Index)
1039  >;
1040  def Insert_Element_v3i32_#Index : Insert_Element <
1041    i32, v3i32, Index, !cast<SubRegIndex>(sub#Index)
1042  >;
1043
1044  def Extract_Element_v3f32_#Index : Extract_Element <
1045    f32, v3f32, Index, !cast<SubRegIndex>(sub#Index)
1046  >;
1047  def Insert_Element_v3f32_#Index : Insert_Element <
1048    f32, v3f32, Index, !cast<SubRegIndex>(sub#Index)
1049  >;
1050}
1051
1052foreach Index = 0-3 in {
1053  def Extract_Element_v4i32_#Index : Extract_Element <
1054    i32, v4i32, Index, !cast<SubRegIndex>(sub#Index)
1055  >;
1056  def Insert_Element_v4i32_#Index : Insert_Element <
1057    i32, v4i32, Index, !cast<SubRegIndex>(sub#Index)
1058  >;
1059
1060  def Extract_Element_v4f32_#Index : Extract_Element <
1061    f32, v4f32, Index, !cast<SubRegIndex>(sub#Index)
1062  >;
1063  def Insert_Element_v4f32_#Index : Insert_Element <
1064    f32, v4f32, Index, !cast<SubRegIndex>(sub#Index)
1065  >;
1066}
1067
1068foreach Index = 0-4 in {
1069  def Extract_Element_v5i32_#Index : Extract_Element <
1070    i32, v5i32, Index, !cast<SubRegIndex>(sub#Index)
1071  >;
1072  def Insert_Element_v5i32_#Index : Insert_Element <
1073    i32, v5i32, Index, !cast<SubRegIndex>(sub#Index)
1074  >;
1075
1076  def Extract_Element_v5f32_#Index : Extract_Element <
1077    f32, v5f32, Index, !cast<SubRegIndex>(sub#Index)
1078  >;
1079  def Insert_Element_v5f32_#Index : Insert_Element <
1080    f32, v5f32, Index, !cast<SubRegIndex>(sub#Index)
1081  >;
1082}
1083
1084foreach Index = 0-5 in {
1085  def Extract_Element_v6i32_#Index : Extract_Element <
1086    i32, v6i32, Index, !cast<SubRegIndex>(sub#Index)
1087  >;
1088  def Insert_Element_v6i32_#Index : Insert_Element <
1089    i32, v6i32, Index, !cast<SubRegIndex>(sub#Index)
1090  >;
1091
1092  def Extract_Element_v6f32_#Index : Extract_Element <
1093    f32, v6f32, Index, !cast<SubRegIndex>(sub#Index)
1094  >;
1095  def Insert_Element_v6f32_#Index : Insert_Element <
1096    f32, v6f32, Index, !cast<SubRegIndex>(sub#Index)
1097  >;
1098}
1099
1100foreach Index = 0-6 in {
1101  def Extract_Element_v7i32_#Index : Extract_Element <
1102    i32, v7i32, Index, !cast<SubRegIndex>(sub#Index)
1103  >;
1104  def Insert_Element_v7i32_#Index : Insert_Element <
1105    i32, v7i32, Index, !cast<SubRegIndex>(sub#Index)
1106  >;
1107
1108  def Extract_Element_v7f32_#Index : Extract_Element <
1109    f32, v7f32, Index, !cast<SubRegIndex>(sub#Index)
1110  >;
1111  def Insert_Element_v7f32_#Index : Insert_Element <
1112    f32, v7f32, Index, !cast<SubRegIndex>(sub#Index)
1113  >;
1114}
1115
1116foreach Index = 0-7 in {
1117  def Extract_Element_v8i32_#Index : Extract_Element <
1118    i32, v8i32, Index, !cast<SubRegIndex>(sub#Index)
1119  >;
1120  def Insert_Element_v8i32_#Index : Insert_Element <
1121    i32, v8i32, Index, !cast<SubRegIndex>(sub#Index)
1122  >;
1123
1124  def Extract_Element_v8f32_#Index : Extract_Element <
1125    f32, v8f32, Index, !cast<SubRegIndex>(sub#Index)
1126  >;
1127  def Insert_Element_v8f32_#Index : Insert_Element <
1128    f32, v8f32, Index, !cast<SubRegIndex>(sub#Index)
1129  >;
1130}
1131
1132foreach Index = 0-15 in {
1133  def Extract_Element_v16i32_#Index : Extract_Element <
1134    i32, v16i32, Index, !cast<SubRegIndex>(sub#Index)
1135  >;
1136  def Insert_Element_v16i32_#Index : Insert_Element <
1137    i32, v16i32, Index, !cast<SubRegIndex>(sub#Index)
1138  >;
1139
1140  def Extract_Element_v16f32_#Index : Extract_Element <
1141    f32, v16f32, Index, !cast<SubRegIndex>(sub#Index)
1142  >;
1143  def Insert_Element_v16f32_#Index : Insert_Element <
1144    f32, v16f32, Index, !cast<SubRegIndex>(sub#Index)
1145  >;
1146}
1147
1148
1149def : Pat <
1150  (extract_subvector v4i16:$vec, (i32 0)),
1151  (v2i16 (EXTRACT_SUBREG v4i16:$vec, sub0))
1152>;
1153
1154def : Pat <
1155  (extract_subvector v4i16:$vec, (i32 2)),
1156  (v2i16 (EXTRACT_SUBREG v4i16:$vec, sub1))
1157>;
1158
1159def : Pat <
1160  (extract_subvector v4f16:$vec, (i32 0)),
1161  (v2f16 (EXTRACT_SUBREG v4f16:$vec, sub0))
1162>;
1163
1164def : Pat <
1165  (extract_subvector v4f16:$vec, (i32 2)),
1166  (v2f16 (EXTRACT_SUBREG v4f16:$vec, sub1))
1167>;
1168
1169foreach Index = 0-31 in {
1170  def Extract_Element_v32i32_#Index : Extract_Element <
1171    i32, v32i32, Index, !cast<SubRegIndex>(sub#Index)
1172  >;
1173
1174  def Insert_Element_v32i32_#Index : Insert_Element <
1175    i32, v32i32, Index, !cast<SubRegIndex>(sub#Index)
1176  >;
1177
1178  def Extract_Element_v32f32_#Index : Extract_Element <
1179    f32, v32f32, Index, !cast<SubRegIndex>(sub#Index)
1180  >;
1181
1182  def Insert_Element_v32f32_#Index : Insert_Element <
1183    f32, v32f32, Index, !cast<SubRegIndex>(sub#Index)
1184  >;
1185}
1186
1187// FIXME: Why do only some of these type combinations for SReg and
1188// VReg?
1189// 16-bit bitcast
1190def : BitConvert <i16, f16, VGPR_32>;
1191def : BitConvert <f16, i16, VGPR_32>;
1192def : BitConvert <i16, f16, SReg_32>;
1193def : BitConvert <f16, i16, SReg_32>;
1194
1195// 32-bit bitcast
1196def : BitConvert <i32, f32, VGPR_32>;
1197def : BitConvert <f32, i32, VGPR_32>;
1198def : BitConvert <i32, f32, SReg_32>;
1199def : BitConvert <f32, i32, SReg_32>;
1200def : BitConvert <v2i16, i32, SReg_32>;
1201def : BitConvert <i32, v2i16, SReg_32>;
1202def : BitConvert <v2f16, i32, SReg_32>;
1203def : BitConvert <i32, v2f16, SReg_32>;
1204def : BitConvert <v2i16, v2f16, SReg_32>;
1205def : BitConvert <v2f16, v2i16, SReg_32>;
1206def : BitConvert <v2f16, f32, SReg_32>;
1207def : BitConvert <f32, v2f16, SReg_32>;
1208def : BitConvert <v2i16, f32, SReg_32>;
1209def : BitConvert <f32, v2i16, SReg_32>;
1210
1211// 64-bit bitcast
1212def : BitConvert <i64, f64, VReg_64>;
1213def : BitConvert <f64, i64, VReg_64>;
1214def : BitConvert <v2i32, v2f32, VReg_64>;
1215def : BitConvert <v2f32, v2i32, VReg_64>;
1216def : BitConvert <i64, v2i32, VReg_64>;
1217def : BitConvert <v2i32, i64, VReg_64>;
1218def : BitConvert <i64, v2f32, VReg_64>;
1219def : BitConvert <v2f32, i64, VReg_64>;
1220def : BitConvert <f64, v2f32, VReg_64>;
1221def : BitConvert <v2f32, f64, VReg_64>;
1222def : BitConvert <f64, v2i32, VReg_64>;
1223def : BitConvert <v2i32, f64, VReg_64>;
1224def : BitConvert <v4i16, v4f16, VReg_64>;
1225def : BitConvert <v4f16, v4i16, VReg_64>;
1226
1227// FIXME: Make SGPR
1228def : BitConvert <v2i32, v4f16, VReg_64>;
1229def : BitConvert <v4f16, v2i32, VReg_64>;
1230def : BitConvert <v2i32, v4f16, VReg_64>;
1231def : BitConvert <v2i32, v4i16, VReg_64>;
1232def : BitConvert <v4i16, v2i32, VReg_64>;
1233def : BitConvert <v2f32, v4f16, VReg_64>;
1234def : BitConvert <v4f16, v2f32, VReg_64>;
1235def : BitConvert <v2f32, v4i16, VReg_64>;
1236def : BitConvert <v4i16, v2f32, VReg_64>;
1237def : BitConvert <v4i16, f64, VReg_64>;
1238def : BitConvert <v4f16, f64, VReg_64>;
1239def : BitConvert <f64, v4i16, VReg_64>;
1240def : BitConvert <f64, v4f16, VReg_64>;
1241def : BitConvert <v4i16, i64, VReg_64>;
1242def : BitConvert <v4f16, i64, VReg_64>;
1243def : BitConvert <i64, v4i16, VReg_64>;
1244def : BitConvert <i64, v4f16, VReg_64>;
1245
1246def : BitConvert <v4i32, v4f32, VReg_128>;
1247def : BitConvert <v4f32, v4i32, VReg_128>;
1248
1249// 96-bit bitcast
1250def : BitConvert <v3i32, v3f32, SGPR_96>;
1251def : BitConvert <v3f32, v3i32, SGPR_96>;
1252
1253// 128-bit bitcast
1254def : BitConvert <v2i64, v4i32, SReg_128>;
1255def : BitConvert <v4i32, v2i64, SReg_128>;
1256def : BitConvert <v2f64, v4f32, VReg_128>;
1257def : BitConvert <v2f64, v4i32, VReg_128>;
1258def : BitConvert <v4f32, v2f64, VReg_128>;
1259def : BitConvert <v4i32, v2f64, VReg_128>;
1260def : BitConvert <v2i64, v2f64, VReg_128>;
1261def : BitConvert <v2f64, v2i64, VReg_128>;
1262def : BitConvert <v4f32, v2i64, VReg_128>;
1263def : BitConvert <v2i64, v4f32, VReg_128>;
1264
1265// 160-bit bitcast
1266def : BitConvert <v5i32, v5f32, SReg_160>;
1267def : BitConvert <v5f32, v5i32, SReg_160>;
1268def : BitConvert <v5i32, v5f32, VReg_160>;
1269def : BitConvert <v5f32, v5i32, VReg_160>;
1270
1271// 192-bit bitcast
1272def : BitConvert <v6i32, v6f32, SReg_192>;
1273def : BitConvert <v6f32, v6i32, SReg_192>;
1274def : BitConvert <v6i32, v6f32, VReg_192>;
1275def : BitConvert <v6f32, v6i32, VReg_192>;
1276def : BitConvert <v3i64, v3f64, VReg_192>;
1277def : BitConvert <v3f64, v3i64, VReg_192>;
1278def : BitConvert <v3i64, v6i32, VReg_192>;
1279def : BitConvert <v3i64, v6f32, VReg_192>;
1280def : BitConvert <v3f64, v6i32, VReg_192>;
1281def : BitConvert <v3f64, v6f32, VReg_192>;
1282def : BitConvert <v6i32, v3i64, VReg_192>;
1283def : BitConvert <v6f32, v3i64, VReg_192>;
1284def : BitConvert <v6i32, v3f64, VReg_192>;
1285def : BitConvert <v6f32, v3f64, VReg_192>;
1286
1287// 224-bit bitcast
1288def : BitConvert <v7i32, v7f32, SReg_224>;
1289def : BitConvert <v7f32, v7i32, SReg_224>;
1290def : BitConvert <v7i32, v7f32, VReg_224>;
1291def : BitConvert <v7f32, v7i32, VReg_224>;
1292
1293// 256-bit bitcast
1294def : BitConvert <v8i32, v8f32, SReg_256>;
1295def : BitConvert <v8f32, v8i32, SReg_256>;
1296def : BitConvert <v8i32, v8f32, VReg_256>;
1297def : BitConvert <v8f32, v8i32, VReg_256>;
1298def : BitConvert <v4i64, v4f64, VReg_256>;
1299def : BitConvert <v4f64, v4i64, VReg_256>;
1300def : BitConvert <v4i64, v8i32, VReg_256>;
1301def : BitConvert <v4i64, v8f32, VReg_256>;
1302def : BitConvert <v4f64, v8i32, VReg_256>;
1303def : BitConvert <v4f64, v8f32, VReg_256>;
1304def : BitConvert <v8i32, v4i64, VReg_256>;
1305def : BitConvert <v8f32, v4i64, VReg_256>;
1306def : BitConvert <v8i32, v4f64, VReg_256>;
1307def : BitConvert <v8f32, v4f64, VReg_256>;
1308
1309
1310// 512-bit bitcast
1311def : BitConvert <v16i32, v16f32, VReg_512>;
1312def : BitConvert <v16f32, v16i32, VReg_512>;
1313def : BitConvert <v8i64,  v8f64,  VReg_512>;
1314def : BitConvert <v8f64,  v8i64,  VReg_512>;
1315def : BitConvert <v8i64,  v16i32, VReg_512>;
1316def : BitConvert <v8f64,  v16i32, VReg_512>;
1317def : BitConvert <v16i32, v8i64,  VReg_512>;
1318def : BitConvert <v16i32, v8f64,  VReg_512>;
1319def : BitConvert <v8i64,  v16f32, VReg_512>;
1320def : BitConvert <v8f64,  v16f32, VReg_512>;
1321def : BitConvert <v16f32, v8i64,  VReg_512>;
1322def : BitConvert <v16f32, v8f64,  VReg_512>;
1323
1324// 1024-bit bitcast
1325def : BitConvert <v32i32, v32f32, VReg_1024>;
1326def : BitConvert <v32f32, v32i32, VReg_1024>;
1327def : BitConvert <v16i64, v16f64, VReg_1024>;
1328def : BitConvert <v16f64, v16i64, VReg_1024>;
1329def : BitConvert <v16i64, v32i32, VReg_1024>;
1330def : BitConvert <v32i32, v16i64, VReg_1024>;
1331def : BitConvert <v16f64, v32f32, VReg_1024>;
1332def : BitConvert <v32f32, v16f64, VReg_1024>;
1333def : BitConvert <v16i64, v32f32, VReg_1024>;
1334def : BitConvert <v32i32, v16f64, VReg_1024>;
1335def : BitConvert <v16f64, v32i32, VReg_1024>;
1336def : BitConvert <v32f32, v16i64, VReg_1024>;
1337
1338
1339/********** =================== **********/
1340/********** Src & Dst modifiers **********/
1341/********** =================== **********/
1342
1343
1344// If denormals are not enabled, it only impacts the compare of the
1345// inputs. The output result is not flushed.
1346class ClampPat<Instruction inst, ValueType vt> : GCNPat <
1347  (vt (AMDGPUclamp (VOP3Mods vt:$src0, i32:$src0_modifiers))),
1348  (inst i32:$src0_modifiers, vt:$src0,
1349        i32:$src0_modifiers, vt:$src0, DSTCLAMP.ENABLE, DSTOMOD.NONE)
1350>;
1351
1352def : ClampPat<V_MAX_F32_e64, f32>;
1353def : ClampPat<V_MAX_F64_e64, f64>;
1354def : ClampPat<V_MAX_F16_e64, f16>;
1355
1356let SubtargetPredicate = HasVOP3PInsts in {
1357def : GCNPat <
1358  (v2f16 (AMDGPUclamp (VOP3PMods v2f16:$src0, i32:$src0_modifiers))),
1359  (V_PK_MAX_F16 $src0_modifiers, $src0,
1360                $src0_modifiers, $src0, DSTCLAMP.ENABLE)
1361>;
1362}
1363
1364/********** ================================ **********/
1365/********** Floating point absolute/negative **********/
1366/********** ================================ **********/
1367
1368// Prevent expanding both fneg and fabs.
1369// TODO: Add IgnoredBySelectionDAG bit?
1370let AddedComplexity = 1 in { // Prefer SALU to VALU patterns for DAG
1371
1372def : GCNPat <
1373  (fneg (fabs (f32 SReg_32:$src))),
1374  (S_OR_B32 SReg_32:$src, (S_MOV_B32 (i32 0x80000000))) // Set sign bit
1375>;
1376
1377def : GCNPat <
1378  (fabs (f32 SReg_32:$src)),
1379  (S_AND_B32 SReg_32:$src, (S_MOV_B32 (i32 0x7fffffff)))
1380>;
1381
1382def : GCNPat <
1383  (fneg (f32 SReg_32:$src)),
1384  (S_XOR_B32 SReg_32:$src, (S_MOV_B32 (i32 0x80000000)))
1385>;
1386
1387def : GCNPat <
1388  (fneg (f16 SReg_32:$src)),
1389  (S_XOR_B32 SReg_32:$src, (S_MOV_B32 (i32 0x00008000)))
1390>;
1391
1392def : GCNPat <
1393  (fneg (f16 VGPR_32:$src)),
1394  (V_XOR_B32_e32 (S_MOV_B32 (i32 0x00008000)), VGPR_32:$src)
1395>;
1396
1397def : GCNPat <
1398  (fabs (f16 SReg_32:$src)),
1399  (S_AND_B32 SReg_32:$src, (S_MOV_B32 (i32 0x00007fff)))
1400>;
1401
1402def : GCNPat <
1403  (fneg (fabs (f16 SReg_32:$src))),
1404  (S_OR_B32 SReg_32:$src, (S_MOV_B32 (i32 0x00008000))) // Set sign bit
1405>;
1406
1407def : GCNPat <
1408  (fneg (fabs (f16 VGPR_32:$src))),
1409  (V_OR_B32_e32 (S_MOV_B32 (i32 0x00008000)), VGPR_32:$src) // Set sign bit
1410>;
1411
1412def : GCNPat <
1413  (fneg (v2f16 SReg_32:$src)),
1414  (S_XOR_B32 SReg_32:$src, (S_MOV_B32 (i32 0x80008000)))
1415>;
1416
1417def : GCNPat <
1418  (fabs (v2f16 SReg_32:$src)),
1419  (S_AND_B32 SReg_32:$src, (S_MOV_B32 (i32 0x7fff7fff)))
1420>;
1421
1422// This is really (fneg (fabs v2f16:$src))
1423//
1424// fabs is not reported as free because there is modifier for it in
1425// VOP3P instructions, so it is turned into the bit op.
1426def : GCNPat <
1427  (fneg (v2f16 (bitconvert (and_oneuse (i32 SReg_32:$src), 0x7fff7fff)))),
1428  (S_OR_B32 SReg_32:$src, (S_MOV_B32 (i32 0x80008000))) // Set sign bit
1429>;
1430
1431def : GCNPat <
1432  (fneg (v2f16 (fabs SReg_32:$src))),
1433  (S_OR_B32 SReg_32:$src, (S_MOV_B32 (i32 0x80008000))) // Set sign bit
1434>;
1435
1436// FIXME: The implicit-def of scc from S_[X]OR/AND_B32 is mishandled
1437 // def : GCNPat <
1438//   (fneg (f64 SReg_64:$src)),
1439//   (REG_SEQUENCE SReg_64,
1440//     (i32 (EXTRACT_SUBREG SReg_64:$src, sub0)),
1441//     sub0,
1442//     (S_XOR_B32 (i32 (EXTRACT_SUBREG SReg_64:$src, sub1)),
1443//                (i32 (S_MOV_B32 (i32 0x80000000)))),
1444//     sub1)
1445// >;
1446
1447// def : GCNPat <
1448//   (fneg (fabs (f64 SReg_64:$src))),
1449//   (REG_SEQUENCE SReg_64,
1450//     (i32 (EXTRACT_SUBREG SReg_64:$src, sub0)),
1451//     sub0,
1452//     (S_OR_B32 (i32 (EXTRACT_SUBREG SReg_64:$src, sub1)),
1453//               (S_MOV_B32 (i32 0x80000000))), // Set sign bit.
1454//     sub1)
1455// >;
1456
1457// FIXME: Use S_BITSET0_B32/B64?
1458// def : GCNPat <
1459//   (fabs (f64 SReg_64:$src)),
1460//   (REG_SEQUENCE SReg_64,
1461//     (i32 (EXTRACT_SUBREG SReg_64:$src, sub0)),
1462//     sub0,
1463//     (S_AND_B32 (i32 (EXTRACT_SUBREG SReg_64:$src, sub1)),
1464//                (i32 (S_MOV_B32 (i32 0x7fffffff)))),
1465//     sub1)
1466// >;
1467
1468// COPY_TO_REGCLASS is needed to avoid using SCC from S_XOR_B32 instead
1469// of the real value.
1470def : GCNPat <
1471  (fneg (v2f32 SReg_64:$src)),
1472  (v2f32 (REG_SEQUENCE SReg_64,
1473         (f32 (COPY_TO_REGCLASS (S_XOR_B32 (i32 (EXTRACT_SUBREG $src, sub0)),
1474                                           (i32 (S_MOV_B32 (i32 0x80000000)))),
1475                                 SReg_32)), sub0,
1476         (f32 (COPY_TO_REGCLASS (S_XOR_B32 (i32 (EXTRACT_SUBREG $src, sub1)),
1477                                           (i32 (S_MOV_B32 (i32 0x80000000)))),
1478                                 SReg_32)), sub1))
1479>;
1480
1481} // End let AddedComplexity = 1
1482
1483def : GCNPat <
1484  (fabs (f32 VGPR_32:$src)),
1485  (V_AND_B32_e32 (S_MOV_B32 (i32 0x7fffffff)), VGPR_32:$src)
1486>;
1487
1488def : GCNPat <
1489  (fneg (f32 VGPR_32:$src)),
1490  (V_XOR_B32_e32 (S_MOV_B32 (i32 0x80000000)), VGPR_32:$src)
1491>;
1492
1493def : GCNPat <
1494  (fabs (f16 VGPR_32:$src)),
1495  (V_AND_B32_e32 (S_MOV_B32 (i32 0x00007fff)), VGPR_32:$src)
1496>;
1497
1498def : GCNPat <
1499  (fneg (v2f16 VGPR_32:$src)),
1500  (V_XOR_B32_e32 (S_MOV_B32 (i32 0x80008000)), VGPR_32:$src)
1501>;
1502
1503def : GCNPat <
1504  (fabs (v2f16 VGPR_32:$src)),
1505  (V_AND_B32_e32 (S_MOV_B32 (i32 0x7fff7fff)), VGPR_32:$src)
1506>;
1507
1508def : GCNPat <
1509  (fneg (v2f16 (fabs VGPR_32:$src))),
1510  (V_OR_B32_e32 (S_MOV_B32 (i32 0x80008000)), VGPR_32:$src) // Set sign bit
1511>;
1512
1513def : GCNPat <
1514  (fabs (f64 VReg_64:$src)),
1515  (REG_SEQUENCE VReg_64,
1516    (i32 (EXTRACT_SUBREG VReg_64:$src, sub0)),
1517    sub0,
1518    (V_AND_B32_e64 (i32 (EXTRACT_SUBREG VReg_64:$src, sub1)),
1519                   (V_MOV_B32_e32 (i32 0x7fffffff))), // Set sign bit.
1520     sub1)
1521>;
1522
1523// TODO: Use SGPR for constant
1524def : GCNPat <
1525  (fneg (f64 VReg_64:$src)),
1526  (REG_SEQUENCE VReg_64,
1527    (i32 (EXTRACT_SUBREG VReg_64:$src, sub0)),
1528    sub0,
1529    (V_XOR_B32_e32 (i32 (EXTRACT_SUBREG VReg_64:$src, sub1)),
1530                   (i32 (V_MOV_B32_e32 (i32 0x80000000)))),
1531    sub1)
1532>;
1533
1534// TODO: Use SGPR for constant
1535def : GCNPat <
1536  (fneg (fabs (f64 VReg_64:$src))),
1537  (REG_SEQUENCE VReg_64,
1538    (i32 (EXTRACT_SUBREG VReg_64:$src, sub0)),
1539    sub0,
1540    (V_OR_B32_e32 (i32 (EXTRACT_SUBREG VReg_64:$src, sub1)),
1541                  (V_MOV_B32_e32 (i32 0x80000000))), // Set sign bit.
1542    sub1)
1543>;
1544
1545def : GCNPat <
1546  (getDivergentFrag<fneg>.ret (v2f32 VReg_64:$src)),
1547  (V_PK_ADD_F32 11 /* OP_SEL_1 | NEG_LO | HEG_HI */, VReg_64:$src,
1548                11 /* OP_SEL_1 | NEG_LO | HEG_HI */, 0,
1549                0, 0, 0, 0, 0)
1550> {
1551  let SubtargetPredicate = HasPackedFP32Ops;
1552}
1553
1554def : GCNPat <
1555  (fcopysign f16:$src0, f16:$src1),
1556  (V_BFI_B32_e64 (S_MOV_B32 (i32 0x00007fff)), $src0, $src1)
1557>;
1558
1559def : GCNPat <
1560  (fcopysign f32:$src0, f16:$src1),
1561  (V_BFI_B32_e64 (S_MOV_B32 (i32 0x7fffffff)), $src0,
1562             (V_LSHLREV_B32_e64 (i32 16), $src1))
1563>;
1564
1565def : GCNPat <
1566  (fcopysign f64:$src0, f16:$src1),
1567  (REG_SEQUENCE SReg_64,
1568    (i32 (EXTRACT_SUBREG $src0, sub0)), sub0,
1569    (V_BFI_B32_e64 (S_MOV_B32 (i32 0x7fffffff)), (i32 (EXTRACT_SUBREG $src0, sub1)),
1570               (V_LSHLREV_B32_e64 (i32 16), $src1)), sub1)
1571>;
1572
1573def : GCNPat <
1574  (fcopysign f16:$src0, f32:$src1),
1575  (V_BFI_B32_e64 (S_MOV_B32 (i32 0x00007fff)), $src0,
1576             (V_LSHRREV_B32_e64 (i32 16), $src1))
1577>;
1578
1579def : GCNPat <
1580  (fcopysign f16:$src0, f64:$src1),
1581  (V_BFI_B32_e64 (S_MOV_B32 (i32 0x00007fff)), $src0,
1582             (V_LSHRREV_B32_e64 (i32 16), (EXTRACT_SUBREG $src1, sub1)))
1583>;
1584
1585/********** ================== **********/
1586/********** Immediate Patterns **********/
1587/********** ================== **********/
1588
1589def : GCNPat <
1590  (VGPRImm<(i32 imm)>:$imm),
1591  (V_MOV_B32_e32 imm:$imm)
1592>;
1593
1594def : GCNPat <
1595  (VGPRImm<(f32 fpimm)>:$imm),
1596  (V_MOV_B32_e32 (f32 (bitcast_fpimm_to_i32 $imm)))
1597>;
1598
1599def : GCNPat <
1600  (i32 imm:$imm),
1601  (S_MOV_B32 imm:$imm)
1602>;
1603
1604def : GCNPat <
1605  (VGPRImm<(SIlds tglobaladdr:$ga)>),
1606  (V_MOV_B32_e32 $ga)
1607>;
1608
1609def : GCNPat <
1610  (SIlds tglobaladdr:$ga),
1611  (S_MOV_B32 $ga)
1612>;
1613
1614// FIXME: Workaround for ordering issue with peephole optimizer where
1615// a register class copy interferes with immediate folding.  Should
1616// use s_mov_b32, which can be shrunk to s_movk_i32
1617def : GCNPat <
1618  (VGPRImm<(f16 fpimm)>:$imm),
1619  (V_MOV_B32_e32 (f16 (bitcast_fpimm_to_i32 $imm)))
1620>;
1621
1622def : GCNPat <
1623  (f32 fpimm:$imm),
1624  (S_MOV_B32 (f32 (bitcast_fpimm_to_i32 $imm)))
1625>;
1626
1627def : GCNPat <
1628  (f16 fpimm:$imm),
1629  (S_MOV_B32 (i32 (bitcast_fpimm_to_i32 $imm)))
1630>;
1631
1632def : GCNPat <
1633  (p5 frameindex:$fi),
1634  (V_MOV_B32_e32 (p5 (frameindex_to_targetframeindex $fi)))
1635>;
1636
1637def : GCNPat <
1638  (p5 frameindex:$fi),
1639  (S_MOV_B32 (p5 (frameindex_to_targetframeindex $fi)))
1640>;
1641
1642def : GCNPat <
1643  (i64 InlineImm64:$imm),
1644  (S_MOV_B64 InlineImm64:$imm)
1645>;
1646
1647// XXX - Should this use a s_cmp to set SCC?
1648
1649// Set to sign-extended 64-bit value (true = -1, false = 0)
1650def : GCNPat <
1651  (i1 imm:$imm),
1652  (S_MOV_B64 (i64 (as_i64imm $imm)))
1653> {
1654  let WaveSizePredicate = isWave64;
1655}
1656
1657def : GCNPat <
1658  (i1 imm:$imm),
1659  (S_MOV_B32 (i32 (as_i32imm $imm)))
1660> {
1661  let WaveSizePredicate = isWave32;
1662}
1663
1664def : GCNPat <
1665  (f64 InlineImmFP64:$imm),
1666  (S_MOV_B64 (f64 (bitcast_fpimm_to_i64 InlineImmFP64:$imm)))
1667>;
1668
1669/********** ================== **********/
1670/********** Intrinsic Patterns **********/
1671/********** ================== **********/
1672
1673let OtherPredicates = [isNotGFX90APlus] in
1674// FIXME: Should use _e64 and select source modifiers.
1675def : POW_Common <V_LOG_F32_e32, V_EXP_F32_e32, V_MUL_LEGACY_F32_e32>;
1676
1677let OtherPredicates = [isGFX90APlus] in
1678def : GCNPat <
1679  (fpow f32:$src0, f32:$src1),
1680  (V_EXP_F32_e32 (V_MUL_LEGACY_F32_e64 0, f32:$src1, SRCMODS.NONE, (V_LOG_F32_e32 f32:$src0), 0, 0))
1681>;
1682
1683def : GCNPat <
1684  (i32 (sext i1:$src0)),
1685  (V_CNDMASK_B32_e64 /*src0mod*/(i32 0), /*src0*/(i32 0),
1686                     /*src1mod*/(i32 0), /*src1*/(i32 -1), $src0)
1687>;
1688
1689class Ext32Pat <SDNode ext> : GCNPat <
1690  (i32 (ext i1:$src0)),
1691  (V_CNDMASK_B32_e64 /*src0mod*/(i32 0), /*src0*/(i32 0),
1692                     /*src1mod*/(i32 0), /*src1*/(i32 1), $src0)
1693>;
1694
1695def : Ext32Pat <zext>;
1696def : Ext32Pat <anyext>;
1697
1698// The multiplication scales from [0,1) to the unsigned integer range,
1699// rounding down a bit to avoid unwanted overflow.
1700def : GCNPat <
1701  (AMDGPUurecip i32:$src0),
1702  (V_CVT_U32_F32_e32
1703    (V_MUL_F32_e32 (i32 CONST.FP_4294966784),
1704                   (V_RCP_IFLAG_F32_e32 (V_CVT_F32_U32_e32 $src0))))
1705>;
1706
1707//===----------------------------------------------------------------------===//
1708// VOP3 Patterns
1709//===----------------------------------------------------------------------===//
1710
1711def : IMad24Pat<V_MAD_I32_I24_e64, 1>;
1712def : UMad24Pat<V_MAD_U32_U24_e64, 1>;
1713
1714// BFI patterns
1715
1716def BFIImm32 : PatFrag<
1717  (ops node:$x, node:$y, node:$z),
1718  (i32 (DivergentBinFrag<or> (and node:$y, node:$x), (and node:$z, imm))),
1719  [{
1720    auto *X = dyn_cast<ConstantSDNode>(N->getOperand(0)->getOperand(1));
1721    auto *NotX = dyn_cast<ConstantSDNode>(N->getOperand(1)->getOperand(1));
1722    return X && NotX &&
1723      ~(unsigned)X->getZExtValue() == (unsigned)NotX->getZExtValue();
1724  }]
1725>;
1726
1727// Definition from ISA doc:
1728// (y & x) | (z & ~x)
1729def : AMDGPUPat <
1730  (DivergentBinFrag<or> (and i32:$y, i32:$x), (and i32:$z, (not i32:$x))),
1731  (V_BFI_B32_e64 $x, $y, $z)
1732>;
1733
1734// (y & C) | (z & ~C)
1735def : AMDGPUPat <
1736  (BFIImm32 i32:$x, i32:$y, i32:$z),
1737  (V_BFI_B32_e64 $x, $y, $z)
1738>;
1739
1740// 64-bit version
1741def : AMDGPUPat <
1742  (DivergentBinFrag<or> (and i64:$y, i64:$x), (and i64:$z, (not i64:$x))),
1743  (REG_SEQUENCE SReg_64,
1744    (V_BFI_B32_e64 (i32 (EXTRACT_SUBREG SReg_64:$x, sub0)),
1745               (i32 (EXTRACT_SUBREG SReg_64:$y, sub0)),
1746               (i32 (EXTRACT_SUBREG SReg_64:$z, sub0))), sub0,
1747    (V_BFI_B32_e64 (i32 (EXTRACT_SUBREG SReg_64:$x, sub1)),
1748               (i32 (EXTRACT_SUBREG SReg_64:$y, sub1)),
1749               (i32 (EXTRACT_SUBREG SReg_64:$z, sub1))), sub1)
1750>;
1751
1752// SHA-256 Ch function
1753// z ^ (x & (y ^ z))
1754def : AMDGPUPat <
1755  (DivergentBinFrag<xor> i32:$z, (and i32:$x, (xor i32:$y, i32:$z))),
1756  (V_BFI_B32_e64 $x, $y, $z)
1757>;
1758
1759// 64-bit version
1760def : AMDGPUPat <
1761  (DivergentBinFrag<xor> i64:$z, (and i64:$x, (xor i64:$y, i64:$z))),
1762  (REG_SEQUENCE SReg_64,
1763    (V_BFI_B32_e64 (i32 (EXTRACT_SUBREG SReg_64:$x, sub0)),
1764               (i32 (EXTRACT_SUBREG SReg_64:$y, sub0)),
1765               (i32 (EXTRACT_SUBREG SReg_64:$z, sub0))), sub0,
1766    (V_BFI_B32_e64 (i32 (EXTRACT_SUBREG SReg_64:$x, sub1)),
1767               (i32 (EXTRACT_SUBREG SReg_64:$y, sub1)),
1768               (i32 (EXTRACT_SUBREG SReg_64:$z, sub1))), sub1)
1769>;
1770
1771def : AMDGPUPat <
1772  (fcopysign f32:$src0, f32:$src1),
1773  (V_BFI_B32_e64 (S_MOV_B32 (i32 0x7fffffff)), $src0, $src1)
1774>;
1775
1776def : AMDGPUPat <
1777  (fcopysign f32:$src0, f64:$src1),
1778  (V_BFI_B32_e64 (S_MOV_B32 (i32 0x7fffffff)), $src0,
1779             (i32 (EXTRACT_SUBREG SReg_64:$src1, sub1)))
1780>;
1781
1782def : AMDGPUPat <
1783  (fcopysign f64:$src0, f64:$src1),
1784  (REG_SEQUENCE SReg_64,
1785    (i32 (EXTRACT_SUBREG $src0, sub0)), sub0,
1786    (V_BFI_B32_e64 (S_MOV_B32 (i32 0x7fffffff)),
1787               (i32 (EXTRACT_SUBREG SReg_64:$src0, sub1)),
1788               (i32 (EXTRACT_SUBREG SReg_64:$src1, sub1))), sub1)
1789>;
1790
1791def : AMDGPUPat <
1792  (fcopysign f64:$src0, f32:$src1),
1793  (REG_SEQUENCE SReg_64,
1794    (i32 (EXTRACT_SUBREG $src0, sub0)), sub0,
1795    (V_BFI_B32_e64 (S_MOV_B32 (i32 0x7fffffff)),
1796               (i32 (EXTRACT_SUBREG SReg_64:$src0, sub1)),
1797               $src1), sub1)
1798>;
1799
1800def : ROTRPattern <V_ALIGNBIT_B32_e64>;
1801
1802def : GCNPat<(i32 (trunc (srl i64:$src0, (and i32:$src1, (i32 31))))),
1803          (V_ALIGNBIT_B32_e64 (i32 (EXTRACT_SUBREG (i64 $src0), sub1)),
1804                          (i32 (EXTRACT_SUBREG (i64 $src0), sub0)), $src1)>;
1805
1806def : GCNPat<(i32 (trunc (srl i64:$src0, (i32 ShiftAmt32Imm:$src1)))),
1807          (V_ALIGNBIT_B32_e64 (i32 (EXTRACT_SUBREG (i64 $src0), sub1)),
1808                          (i32 (EXTRACT_SUBREG (i64 $src0), sub0)), $src1)>;
1809
1810/********** ====================== **********/
1811/**********   Indirect addressing  **********/
1812/********** ====================== **********/
1813
1814multiclass SI_INDIRECT_Pattern <ValueType vt, ValueType eltvt, string VecSize> {
1815  // Extract with offset
1816  def : GCNPat<
1817    (eltvt (extractelt vt:$src, (MOVRELOffset i32:$idx, (i32 imm:$offset)))),
1818    (!cast<Instruction>("SI_INDIRECT_SRC_"#VecSize) $src, $idx, imm:$offset)
1819  >;
1820
1821  // Insert with offset
1822  def : GCNPat<
1823    (insertelt vt:$src, eltvt:$val, (MOVRELOffset i32:$idx, (i32 imm:$offset))),
1824    (!cast<Instruction>("SI_INDIRECT_DST_"#VecSize) $src, $idx, imm:$offset, $val)
1825  >;
1826}
1827
1828defm : SI_INDIRECT_Pattern <v2f32, f32, "V2">;
1829defm : SI_INDIRECT_Pattern <v4f32, f32, "V4">;
1830defm : SI_INDIRECT_Pattern <v8f32, f32, "V8">;
1831defm : SI_INDIRECT_Pattern <v16f32, f32, "V16">;
1832defm : SI_INDIRECT_Pattern <v32f32, f32, "V32">;
1833
1834defm : SI_INDIRECT_Pattern <v2i32, i32, "V2">;
1835defm : SI_INDIRECT_Pattern <v4i32, i32, "V4">;
1836defm : SI_INDIRECT_Pattern <v8i32, i32, "V8">;
1837defm : SI_INDIRECT_Pattern <v16i32, i32, "V16">;
1838defm : SI_INDIRECT_Pattern <v32i32, i32, "V32">;
1839
1840//===----------------------------------------------------------------------===//
1841// SAD Patterns
1842//===----------------------------------------------------------------------===//
1843
1844def : GCNPat <
1845  (add (sub_oneuse (umax i32:$src0, i32:$src1),
1846                   (umin i32:$src0, i32:$src1)),
1847       i32:$src2),
1848  (V_SAD_U32_e64 $src0, $src1, $src2, (i1 0))
1849>;
1850
1851def : GCNPat <
1852  (add (select_oneuse (i1 (setugt i32:$src0, i32:$src1)),
1853                      (sub i32:$src0, i32:$src1),
1854                      (sub i32:$src1, i32:$src0)),
1855       i32:$src2),
1856  (V_SAD_U32_e64 $src0, $src1, $src2, (i1 0))
1857>;
1858
1859//===----------------------------------------------------------------------===//
1860// Conversion Patterns
1861//===----------------------------------------------------------------------===//
1862
1863def : GCNPat<(i32 (sext_inreg i32:$src, i1)),
1864  (S_BFE_I32 i32:$src, (i32 65536))>; // 0 | 1 << 16
1865
1866// Handle sext_inreg in i64
1867def : GCNPat <
1868  (i64 (sext_inreg i64:$src, i1)),
1869  (S_BFE_I64 i64:$src, (i32 0x10000)) // 0 | 1 << 16
1870>;
1871
1872def : GCNPat <
1873  (i16 (sext_inreg i16:$src, i1)),
1874  (S_BFE_I32 $src, (i32 0x00010000)) // 0 | 1 << 16
1875>;
1876
1877def : GCNPat <
1878  (i16 (sext_inreg i16:$src, i8)),
1879  (S_BFE_I32 $src, (i32 0x80000)) // 0 | 8 << 16
1880>;
1881
1882def : GCNPat <
1883  (i64 (sext_inreg i64:$src, i8)),
1884  (S_BFE_I64 i64:$src, (i32 0x80000)) // 0 | 8 << 16
1885>;
1886
1887def : GCNPat <
1888  (i64 (sext_inreg i64:$src, i16)),
1889  (S_BFE_I64 i64:$src, (i32 0x100000)) // 0 | 16 << 16
1890>;
1891
1892def : GCNPat <
1893  (i64 (sext_inreg i64:$src, i32)),
1894  (S_BFE_I64 i64:$src, (i32 0x200000)) // 0 | 32 << 16
1895>;
1896
1897def : GCNPat <
1898  (i64 (zext i32:$src)),
1899  (REG_SEQUENCE SReg_64, $src, sub0, (S_MOV_B32 (i32 0)), sub1)
1900>;
1901
1902def : GCNPat <
1903  (i64 (anyext i32:$src)),
1904  (REG_SEQUENCE SReg_64, $src, sub0, (i32 (IMPLICIT_DEF)), sub1)
1905>;
1906
1907class ZExt_i64_i1_Pat <SDNode ext> : GCNPat <
1908  (i64 (ext i1:$src)),
1909    (REG_SEQUENCE VReg_64,
1910      (V_CNDMASK_B32_e64 /*src0mod*/(i32 0), /*src0*/(i32 0),
1911                         /*src1mod*/(i32 0), /*src1*/(i32 1), $src),
1912      sub0, (S_MOV_B32 (i32 0)), sub1)
1913>;
1914
1915
1916def : ZExt_i64_i1_Pat<zext>;
1917def : ZExt_i64_i1_Pat<anyext>;
1918
1919// FIXME: We need to use COPY_TO_REGCLASS to work-around the fact that
1920// REG_SEQUENCE patterns don't support instructions with multiple outputs.
1921def : GCNPat <
1922  (i64 (sext i32:$src)),
1923    (REG_SEQUENCE SReg_64, $src, sub0,
1924    (i32 (COPY_TO_REGCLASS (S_ASHR_I32 $src, (i32 31)), SReg_32_XM0)), sub1)
1925>;
1926
1927def : GCNPat <
1928  (i64 (sext i1:$src)),
1929  (REG_SEQUENCE VReg_64,
1930    (V_CNDMASK_B32_e64 /*src0mod*/(i32 0), /*src0*/(i32 0),
1931                       /*src1mod*/(i32 0), /*src1*/(i32 -1), $src), sub0,
1932    (V_CNDMASK_B32_e64 /*src0mod*/(i32 0), /*src0*/(i32 0),
1933                       /*src1mod*/(i32 0), /*src1*/(i32 -1), $src), sub1)
1934>;
1935
1936class FPToI1Pat<Instruction Inst, int KOne, ValueType kone_type, ValueType vt, SDPatternOperator fp_to_int> : GCNPat <
1937  (i1 (fp_to_int (vt (VOP3Mods vt:$src0, i32:$src0_modifiers)))),
1938  (i1 (Inst 0, (kone_type KOne), $src0_modifiers, $src0, DSTCLAMP.NONE))
1939>;
1940
1941def : FPToI1Pat<V_CMP_EQ_F16_e64, CONST.FP16_ONE, i16, f16, fp_to_uint>;
1942def : FPToI1Pat<V_CMP_EQ_F16_e64, CONST.FP16_NEG_ONE, i16, f16, fp_to_sint>;
1943def : FPToI1Pat<V_CMP_EQ_F32_e64, CONST.FP32_ONE, i32, f32, fp_to_uint>;
1944def : FPToI1Pat<V_CMP_EQ_F32_e64, CONST.FP32_NEG_ONE, i32, f32, fp_to_sint>;
1945def : FPToI1Pat<V_CMP_EQ_F64_e64, CONST.FP64_ONE, i64, f64, fp_to_uint>;
1946def : FPToI1Pat<V_CMP_EQ_F64_e64, CONST.FP64_NEG_ONE, i64, f64, fp_to_sint>;
1947
1948// If we need to perform a logical operation on i1 values, we need to
1949// use vector comparisons since there is only one SCC register. Vector
1950// comparisons may write to a pair of SGPRs or a single SGPR, so treat
1951// these as 32 or 64-bit comparisons. When legalizing SGPR copies,
1952// instructions resulting in the copies from SCC to these instructions
1953// will be moved to the VALU.
1954
1955let WaveSizePredicate = isWave64 in {
1956def : GCNPat <
1957  (i1 (and i1:$src0, i1:$src1)),
1958  (S_AND_B64 $src0, $src1)
1959>;
1960
1961def : GCNPat <
1962  (i1 (or i1:$src0, i1:$src1)),
1963  (S_OR_B64 $src0, $src1)
1964>;
1965
1966def : GCNPat <
1967  (i1 (xor i1:$src0, i1:$src1)),
1968  (S_XOR_B64 $src0, $src1)
1969>;
1970
1971def : GCNPat <
1972  (i1 (add i1:$src0, i1:$src1)),
1973  (S_XOR_B64 $src0, $src1)
1974>;
1975
1976def : GCNPat <
1977  (i1 (sub i1:$src0, i1:$src1)),
1978  (S_XOR_B64 $src0, $src1)
1979>;
1980
1981let AddedComplexity = 1 in {
1982def : GCNPat <
1983  (i1 (add i1:$src0, (i1 -1))),
1984  (S_NOT_B64 $src0)
1985>;
1986
1987def : GCNPat <
1988  (i1 (sub i1:$src0, (i1 -1))),
1989  (S_NOT_B64 $src0)
1990>;
1991}
1992} // end isWave64
1993
1994let WaveSizePredicate = isWave32 in {
1995def : GCNPat <
1996  (i1 (and i1:$src0, i1:$src1)),
1997  (S_AND_B32 $src0, $src1)
1998>;
1999
2000def : GCNPat <
2001  (i1 (or i1:$src0, i1:$src1)),
2002  (S_OR_B32 $src0, $src1)
2003>;
2004
2005def : GCNPat <
2006  (i1 (xor i1:$src0, i1:$src1)),
2007  (S_XOR_B32 $src0, $src1)
2008>;
2009
2010def : GCNPat <
2011  (i1 (add i1:$src0, i1:$src1)),
2012  (S_XOR_B32 $src0, $src1)
2013>;
2014
2015def : GCNPat <
2016  (i1 (sub i1:$src0, i1:$src1)),
2017  (S_XOR_B32 $src0, $src1)
2018>;
2019
2020let AddedComplexity = 1 in {
2021def : GCNPat <
2022  (i1 (add i1:$src0, (i1 -1))),
2023  (S_NOT_B32 $src0)
2024>;
2025
2026def : GCNPat <
2027  (i1 (sub i1:$src0, (i1 -1))),
2028  (S_NOT_B32 $src0)
2029>;
2030}
2031} // end isWave32
2032
2033def : GCNPat <
2034  (f16 (sint_to_fp i1:$src)),
2035  (V_CVT_F16_F32_e32 (
2036      V_CNDMASK_B32_e64 /*src0mod*/(i32 0), /*src0*/(i32 0),
2037                        /*src1mod*/(i32 0), /*src1*/(i32 CONST.FP32_NEG_ONE),
2038                        SSrc_i1:$src))
2039>;
2040
2041def : GCNPat <
2042  (f16 (uint_to_fp i1:$src)),
2043  (V_CVT_F16_F32_e32 (
2044      V_CNDMASK_B32_e64 /*src0mod*/(i32 0), /*src0*/(i32 0),
2045                        /*src1mod*/(i32 0), /*src1*/(i32 CONST.FP32_ONE),
2046                        SSrc_i1:$src))
2047>;
2048
2049def : GCNPat <
2050  (f32 (sint_to_fp i1:$src)),
2051  (V_CNDMASK_B32_e64 /*src0mod*/(i32 0), /*src0*/(i32 0),
2052                        /*src1mod*/(i32 0), /*src1*/(i32 CONST.FP32_NEG_ONE),
2053                        SSrc_i1:$src)
2054>;
2055
2056def : GCNPat <
2057  (f32 (uint_to_fp i1:$src)),
2058  (V_CNDMASK_B32_e64 /*src0mod*/(i32 0), /*src0*/(i32 0),
2059                        /*src1mod*/(i32 0), /*src1*/(i32 CONST.FP32_ONE),
2060                        SSrc_i1:$src)
2061>;
2062
2063def : GCNPat <
2064  (f64 (sint_to_fp i1:$src)),
2065  (V_CVT_F64_I32_e32 (V_CNDMASK_B32_e64 /*src0mod*/(i32 0), /*src0*/(i32 0),
2066                                        /*src1mod*/(i32 0), /*src1*/(i32 -1),
2067                                        SSrc_i1:$src))
2068>;
2069
2070def : GCNPat <
2071  (f64 (uint_to_fp i1:$src)),
2072  (V_CVT_F64_U32_e32 (V_CNDMASK_B32_e64 /*src0mod*/(i32 0), /*src0*/(i32 0),
2073                                        /*src1mod*/(i32 0), /*src1*/(i32 1),
2074                                        SSrc_i1:$src))
2075>;
2076
2077//===----------------------------------------------------------------------===//
2078// Miscellaneous Patterns
2079//===----------------------------------------------------------------------===//
2080
2081// Eliminate a zero extension from an fp16 operation if it already
2082// zeros the high bits of the 32-bit register.
2083//
2084// This is complicated on gfx9+. Some instructions maintain the legacy
2085// zeroing behavior, but others preserve the high bits. Some have a
2086// control bit to change the behavior. We can't simply say with
2087// certainty what the source behavior is without more context on how
2088// the src is lowered. e.g. fptrunc + fma may be lowered to a
2089// v_fma_mix* instruction which does not zero, or may not.
2090def : GCNPat<
2091  (i32 (zext (i16 (bitconvert fp16_zeros_high_16bits:$src)))),
2092  (COPY VSrc_b16:$src)>;
2093
2094def : GCNPat <
2095  (i32 (trunc i64:$a)),
2096  (EXTRACT_SUBREG $a, sub0)
2097>;
2098
2099def : GCNPat <
2100  (i1 (trunc i32:$a)),
2101  (V_CMP_EQ_U32_e64 (S_AND_B32 (i32 1), $a), (i32 1))
2102>;
2103
2104def : GCNPat <
2105  (i1 (trunc i16:$a)),
2106  (V_CMP_EQ_U32_e64 (S_AND_B32 (i32 1), $a), (i32 1))
2107>;
2108
2109def : GCNPat <
2110  (i1 (trunc i64:$a)),
2111  (V_CMP_EQ_U32_e64 (S_AND_B32 (i32 1),
2112                    (i32 (EXTRACT_SUBREG $a, sub0))), (i32 1))
2113>;
2114
2115def : GCNPat <
2116  (i32 (bswap i32:$a)),
2117  (V_BFI_B32_e64 (S_MOV_B32 (i32 0x00ff00ff)),
2118             (V_ALIGNBIT_B32_e64 VSrc_b32:$a, VSrc_b32:$a, (i32 24)),
2119             (V_ALIGNBIT_B32_e64 VSrc_b32:$a, VSrc_b32:$a, (i32 8)))
2120>;
2121
2122// FIXME: This should have been narrowed to i32 during legalization.
2123// This pattern should also be skipped for GlobalISel
2124def : GCNPat <
2125  (i64 (bswap i64:$a)),
2126  (REG_SEQUENCE VReg_64,
2127  (V_BFI_B32_e64 (S_MOV_B32 (i32 0x00ff00ff)),
2128             (V_ALIGNBIT_B32_e64 (i32 (EXTRACT_SUBREG VReg_64:$a, sub1)),
2129                             (i32 (EXTRACT_SUBREG VReg_64:$a, sub1)),
2130                             (i32 24)),
2131             (V_ALIGNBIT_B32_e64 (i32 (EXTRACT_SUBREG VReg_64:$a, sub1)),
2132                             (i32 (EXTRACT_SUBREG VReg_64:$a, sub1)),
2133                             (i32 8))),
2134  sub0,
2135  (V_BFI_B32_e64 (S_MOV_B32 (i32 0x00ff00ff)),
2136             (V_ALIGNBIT_B32_e64 (i32 (EXTRACT_SUBREG VReg_64:$a, sub0)),
2137                             (i32 (EXTRACT_SUBREG VReg_64:$a, sub0)),
2138                             (i32 24)),
2139             (V_ALIGNBIT_B32_e64 (i32 (EXTRACT_SUBREG VReg_64:$a, sub0)),
2140                             (i32 (EXTRACT_SUBREG VReg_64:$a, sub0)),
2141                             (i32 8))),
2142  sub1)
2143>;
2144
2145// FIXME: The AddedComplexity should not be needed, but in GlobalISel
2146// the BFI pattern ends up taking precedence without it.
2147let SubtargetPredicate = isGFX8Plus, AddedComplexity = 1 in {
2148// Magic number: 3 | (2 << 8) | (1 << 16) | (0 << 24)
2149//
2150// My reading of the manual suggests we should be using src0 for the
2151// register value, but this is what seems to work.
2152def : GCNPat <
2153  (i32 (bswap i32:$a)),
2154  (V_PERM_B32_e64 (i32 0), VSrc_b32:$a, (S_MOV_B32 (i32 0x00010203)))
2155>;
2156
2157// FIXME: This should have been narrowed to i32 during legalization.
2158// This pattern should also be skipped for GlobalISel
2159def : GCNPat <
2160  (i64 (bswap i64:$a)),
2161  (REG_SEQUENCE VReg_64,
2162  (V_PERM_B32_e64  (i32 0), (EXTRACT_SUBREG VReg_64:$a, sub1),
2163              (S_MOV_B32 (i32 0x00010203))),
2164  sub0,
2165  (V_PERM_B32_e64  (i32 0), (EXTRACT_SUBREG VReg_64:$a, sub0),
2166              (S_MOV_B32 (i32 0x00010203))),
2167  sub1)
2168>;
2169
2170// Magic number: 1 | (0 << 8) | (12 << 16) | (12 << 24)
2171// The 12s emit 0s.
2172def : GCNPat <
2173  (i16 (bswap i16:$a)),
2174  (V_PERM_B32_e64  (i32 0), VSrc_b32:$a, (S_MOV_B32 (i32 0x0c0c0001)))
2175>;
2176
2177def : GCNPat <
2178  (i32 (zext (bswap i16:$a))),
2179  (V_PERM_B32_e64  (i32 0), VSrc_b32:$a, (S_MOV_B32 (i32 0x0c0c0001)))
2180>;
2181
2182// Magic number: 1 | (0 << 8) | (3 << 16) | (2 << 24)
2183def : GCNPat <
2184  (v2i16 (bswap v2i16:$a)),
2185  (V_PERM_B32_e64  (i32 0), VSrc_b32:$a, (S_MOV_B32 (i32 0x02030001)))
2186>;
2187
2188}
2189
2190
2191// Prefer selecting to max when legal, but using mul is always valid.
2192let AddedComplexity = -5 in {
2193def : GCNPat<
2194  (fcanonicalize (f16 (VOP3Mods f16:$src, i32:$src_mods))),
2195  (V_MUL_F16_e64 0, (i32 CONST.FP16_ONE), $src_mods, $src)
2196>;
2197
2198def : GCNPat<
2199  (fcanonicalize (f16 (fneg (VOP3Mods f16:$src, i32:$src_mods)))),
2200  (V_MUL_F16_e64 0, (i32 CONST.FP16_NEG_ONE), $src_mods, $src)
2201>;
2202
2203def : GCNPat<
2204  (fcanonicalize (v2f16 (VOP3PMods v2f16:$src, i32:$src_mods))),
2205  (V_PK_MUL_F16 0, (i32 CONST.FP16_ONE), $src_mods, $src, DSTCLAMP.NONE)
2206>;
2207
2208def : GCNPat<
2209  (fcanonicalize (f32 (VOP3Mods f32:$src, i32:$src_mods))),
2210  (V_MUL_F32_e64 0, (i32 CONST.FP32_ONE), $src_mods, $src)
2211>;
2212
2213def : GCNPat<
2214  (fcanonicalize (f32 (fneg (VOP3Mods f32:$src, i32:$src_mods)))),
2215  (V_MUL_F32_e64 0, (i32 CONST.FP32_NEG_ONE), $src_mods, $src)
2216>;
2217
2218// TODO: Handle fneg like other types.
2219def : GCNPat<
2220  (fcanonicalize (f64 (VOP3Mods f64:$src, i32:$src_mods))),
2221  (V_MUL_F64_e64  0, CONST.FP64_ONE, $src_mods, $src)
2222>;
2223} // End AddedComplexity = -5
2224
2225multiclass SelectCanonicalizeAsMax<
2226  list<Predicate> f32_preds = [],
2227  list<Predicate> f64_preds = [],
2228  list<Predicate> f16_preds = []> {
2229  def : GCNPat<
2230    (fcanonicalize (f32 (VOP3Mods f32:$src, i32:$src_mods))),
2231    (V_MAX_F32_e64 $src_mods, $src, $src_mods, $src)> {
2232    let OtherPredicates = f32_preds;
2233  }
2234
2235  def : GCNPat<
2236    (fcanonicalize (f64 (VOP3Mods f64:$src, i32:$src_mods))),
2237    (V_MAX_F64_e64  $src_mods, $src, $src_mods, $src)> {
2238    let OtherPredicates = f64_preds;
2239  }
2240
2241  def : GCNPat<
2242    (fcanonicalize (f16 (VOP3Mods f16:$src, i32:$src_mods))),
2243    (V_MAX_F16_e64 $src_mods, $src, $src_mods, $src, 0, 0)> {
2244    // FIXME: Should have 16-bit inst subtarget predicate
2245    let OtherPredicates = f16_preds;
2246  }
2247
2248  def : GCNPat<
2249    (fcanonicalize (v2f16 (VOP3PMods v2f16:$src, i32:$src_mods))),
2250    (V_PK_MAX_F16 $src_mods, $src, $src_mods, $src, DSTCLAMP.NONE)> {
2251    // FIXME: Should have VOP3P subtarget predicate
2252    let OtherPredicates = f16_preds;
2253  }
2254}
2255
2256// On pre-gfx9 targets, v_max_*/v_min_* did not respect the denormal
2257// mode, and would never flush. For f64, it's faster to do implement
2258// this with a max. For f16/f32 it's a wash, but prefer max when
2259// valid.
2260//
2261// FIXME: Lowering f32/f16 with max is worse since we can use a
2262// smaller encoding if the input is fneg'd. It also adds an extra
2263// register use.
2264let SubtargetPredicate = HasMinMaxDenormModes in {
2265  defm : SelectCanonicalizeAsMax<[], [], []>;
2266} // End SubtargetPredicate = HasMinMaxDenormModes
2267
2268let SubtargetPredicate = NotHasMinMaxDenormModes in {
2269  // Use the max lowering if we don't need to flush.
2270
2271  // FIXME: We don't do use this for f32 as a workaround for the
2272  // library being compiled with the default ieee mode, but
2273  // potentially being called from flushing kernels. Really we should
2274  // not be mixing code expecting different default FP modes, but mul
2275  // works in any FP environment.
2276  defm : SelectCanonicalizeAsMax<[FalsePredicate], [FP64Denormals], [FP16Denormals]>;
2277} // End SubtargetPredicate = NotHasMinMaxDenormModes
2278
2279
2280let OtherPredicates = [HasDLInsts] in {
2281def : GCNPat <
2282  (fma (f32 (VOP3Mods f32:$src0, i32:$src0_modifiers)),
2283       (f32 (VOP3Mods f32:$src1, i32:$src1_modifiers)),
2284       (f32 (VOP3NoMods f32:$src2))),
2285  (V_FMAC_F32_e64 $src0_modifiers, $src0, $src1_modifiers, $src1,
2286                  SRCMODS.NONE, $src2)
2287>;
2288} // End OtherPredicates = [HasDLInsts]
2289
2290let SubtargetPredicate = isGFX10Plus in
2291def : GCNPat <
2292  (fma (f16 (VOP3Mods f32:$src0, i32:$src0_modifiers)),
2293       (f16 (VOP3Mods f32:$src1, i32:$src1_modifiers)),
2294       (f16 (VOP3NoMods f32:$src2))),
2295  (V_FMAC_F16_e64 $src0_modifiers, $src0, $src1_modifiers, $src1,
2296                  SRCMODS.NONE, $src2)
2297>;
2298
2299let SubtargetPredicate = isGFX90APlus in
2300def : GCNPat <
2301  (fma (f64 (VOP3Mods0 f64:$src0, i32:$src0_modifiers, i1:$clamp, i32:$omod)),
2302       (f64 (VOP3Mods f64:$src1, i32:$src1_modifiers)),
2303       (f64 (VOP3NoMods f64:$src2))),
2304  (V_FMAC_F64_e64 $src0_modifiers, $src0, $src1_modifiers, $src1,
2305                  SRCMODS.NONE, $src2, $clamp, $omod)
2306>;
2307
2308// COPY is workaround tablegen bug from multiple outputs
2309// from S_LSHL_B32's multiple outputs from implicit scc def.
2310def : GCNPat <
2311  (v2i16 (build_vector (i16 0), (i16 SReg_32:$src1))),
2312  (S_LSHL_B32 SReg_32:$src1, (i16 16))
2313>;
2314
2315def : GCNPat <
2316  (v2i16 (build_vector (i16 SReg_32:$src1), (i16 0))),
2317  (S_AND_B32 (S_MOV_B32 (i32 0xffff)), SReg_32:$src1)
2318>;
2319
2320def : GCNPat <
2321  (v2f16 (build_vector (f16 SReg_32:$src1), (f16 FP_ZERO))),
2322  (S_AND_B32 (S_MOV_B32 (i32 0xffff)), SReg_32:$src1)
2323>;
2324
2325def : GCNPat <
2326  (v2i16 (build_vector (i16 SReg_32:$src0), (i16 undef))),
2327  (COPY_TO_REGCLASS SReg_32:$src0, SReg_32)
2328>;
2329
2330def : GCNPat <
2331  (v2i16 (build_vector (i16 VGPR_32:$src0), (i16 undef))),
2332  (COPY_TO_REGCLASS VGPR_32:$src0, VGPR_32)
2333>;
2334
2335def : GCNPat <
2336  (v2f16 (build_vector f16:$src0, (f16 undef))),
2337  (COPY $src0)
2338>;
2339
2340def : GCNPat <
2341  (v2i16 (build_vector (i16 undef), (i16 SReg_32:$src1))),
2342  (S_LSHL_B32 SReg_32:$src1, (i32 16))
2343>;
2344
2345def : GCNPat <
2346  (v2f16 (build_vector (f16 undef), (f16 SReg_32:$src1))),
2347  (S_LSHL_B32 SReg_32:$src1, (i32 16))
2348>;
2349
2350let SubtargetPredicate = HasVOP3PInsts in {
2351def : GCNPat <
2352  (v2i16 (build_vector (i16 SReg_32:$src0), (i16 SReg_32:$src1))),
2353  (S_PACK_LL_B32_B16 SReg_32:$src0, SReg_32:$src1)
2354>;
2355
2356// With multiple uses of the shift, this will duplicate the shift and
2357// increase register pressure.
2358def : GCNPat <
2359  (v2i16 (build_vector (i16 SReg_32:$src0), (i16 (trunc (srl_oneuse SReg_32:$src1, (i32 16)))))),
2360  (v2i16 (S_PACK_LH_B32_B16 SReg_32:$src0, SReg_32:$src1))
2361>;
2362
2363
2364def : GCNPat <
2365  (v2i16 (build_vector (i16 (trunc (srl_oneuse SReg_32:$src0, (i32 16)))),
2366                       (i16 (trunc (srl_oneuse SReg_32:$src1, (i32 16)))))),
2367  (S_PACK_HH_B32_B16 SReg_32:$src0, SReg_32:$src1)
2368>;
2369
2370// TODO: Should source modifiers be matched to v_pack_b32_f16?
2371def : GCNPat <
2372  (v2f16 (build_vector (f16 SReg_32:$src0), (f16 SReg_32:$src1))),
2373  (S_PACK_LL_B32_B16 SReg_32:$src0, SReg_32:$src1)
2374>;
2375
2376def : GCNPat <
2377  (v2f16 (is_canonicalized<build_vector> (f16 (VOP3Mods (f16 VGPR_32:$src0), i32:$src0_mods)),
2378                                         (f16 (VOP3Mods (f16 VGPR_32:$src1), i32:$src1_mods)))),
2379  (V_PACK_B32_F16_e64 $src0_mods, VGPR_32:$src0, $src1_mods, VGPR_32:$src1)
2380>;
2381} // End SubtargetPredicate = HasVOP3PInsts
2382
2383def : GCNPat <
2384  (v2f16 (scalar_to_vector f16:$src0)),
2385  (COPY $src0)
2386>;
2387
2388def : GCNPat <
2389  (v2i16 (scalar_to_vector i16:$src0)),
2390  (COPY $src0)
2391>;
2392
2393def : GCNPat <
2394  (v4i16 (scalar_to_vector i16:$src0)),
2395  (INSERT_SUBREG (IMPLICIT_DEF), $src0, sub0)
2396>;
2397
2398def : GCNPat <
2399  (v4f16 (scalar_to_vector f16:$src0)),
2400  (INSERT_SUBREG (IMPLICIT_DEF), $src0, sub0)
2401>;
2402
2403def : GCNPat <
2404  (i64 (int_amdgcn_mov_dpp i64:$src, timm:$dpp_ctrl, timm:$row_mask,
2405                           timm:$bank_mask, timm:$bound_ctrl)),
2406  (V_MOV_B64_DPP_PSEUDO VReg_64_Align2:$src, VReg_64_Align2:$src,
2407                        (as_i32timm $dpp_ctrl), (as_i32timm $row_mask),
2408                        (as_i32timm $bank_mask),
2409                        (as_i1timm $bound_ctrl))
2410>;
2411
2412def : GCNPat <
2413  (i64 (int_amdgcn_update_dpp i64:$old, i64:$src, timm:$dpp_ctrl, timm:$row_mask,
2414                              timm:$bank_mask, timm:$bound_ctrl)),
2415  (V_MOV_B64_DPP_PSEUDO VReg_64_Align2:$old, VReg_64_Align2:$src, (as_i32timm $dpp_ctrl),
2416                        (as_i32timm $row_mask), (as_i32timm $bank_mask),
2417                        (as_i1timm $bound_ctrl))
2418>;
2419
2420//===----------------------------------------------------------------------===//
2421// Fract Patterns
2422//===----------------------------------------------------------------------===//
2423
2424let SubtargetPredicate = isGFX6 in {
2425
2426// V_FRACT is buggy on SI, so the F32 version is never used and (x-floor(x)) is
2427// used instead. However, SI doesn't have V_FLOOR_F64, so the most efficient
2428// way to implement it is using V_FRACT_F64.
2429// The workaround for the V_FRACT bug is:
2430//    fract(x) = isnan(x) ? x : min(V_FRACT(x), 0.99999999999999999)
2431
2432// Convert floor(x) to (x - fract(x))
2433
2434// Don't bother handling this for GlobalISel, it's handled during
2435// lowering.
2436//
2437// FIXME: DAG should also custom lower this.
2438def : GCNPat <
2439  (f64 (ffloor (f64 (VOP3Mods f64:$x, i32:$mods)))),
2440  (V_ADD_F64_e64
2441      $mods,
2442      $x,
2443      SRCMODS.NEG,
2444      (V_CNDMASK_B64_PSEUDO
2445         (V_MIN_F64_e64
2446             SRCMODS.NONE,
2447             (V_FRACT_F64_e64 $mods, $x),
2448             SRCMODS.NONE,
2449             (V_MOV_B64_PSEUDO 0x3fefffffffffffff)),
2450         $x,
2451         (V_CMP_CLASS_F64_e64 SRCMODS.NONE, $x, (i32 3 /*NaN*/))))
2452>;
2453
2454} // End SubtargetPredicates = isGFX6
2455
2456//============================================================================//
2457// Miscellaneous Optimization Patterns
2458//============================================================================//
2459
2460// Undo sub x, c -> add x, -c canonicalization since c is more likely
2461// an inline immediate than -c.
2462// TODO: Also do for 64-bit.
2463def : GCNPat<
2464  (add i32:$src0, (i32 NegSubInlineConst32:$src1)),
2465  (S_SUB_I32 SReg_32:$src0, NegSubInlineConst32:$src1)
2466>;
2467
2468def : GCNPat<
2469  (add i32:$src0, (i32 NegSubInlineConst32:$src1)),
2470  (V_SUB_U32_e64 VS_32:$src0, NegSubInlineConst32:$src1)> {
2471  let SubtargetPredicate = HasAddNoCarryInsts;
2472}
2473
2474def : GCNPat<
2475  (add i32:$src0, (i32 NegSubInlineConst32:$src1)),
2476  (V_SUB_CO_U32_e64 VS_32:$src0, NegSubInlineConst32:$src1)> {
2477  let SubtargetPredicate = NotHasAddNoCarryInsts;
2478}
2479
2480
2481// Avoid pointlessly materializing a constant in VGPR.
2482// FIXME: Should also do this for readlane, but tablegen crashes on
2483// the ignored src1.
2484def : GCNPat<
2485  (int_amdgcn_readfirstlane (i32 imm:$src)),
2486  (S_MOV_B32 SReg_32:$src)
2487>;
2488
2489multiclass BFMPatterns <ValueType vt, InstSI BFM, InstSI MOV> {
2490  def : GCNPat <
2491    (vt (shl (vt (add (vt (shl 1, vt:$a)), -1)), vt:$b)),
2492    (BFM $a, $b)
2493  >;
2494
2495  def : GCNPat <
2496    (vt (add (vt (shl 1, vt:$a)), -1)),
2497    (BFM $a, (MOV (i32 0)))
2498  >;
2499}
2500
2501defm : BFMPatterns <i32, S_BFM_B32, S_MOV_B32>;
2502// FIXME: defm : BFMPatterns <i64, S_BFM_B64, S_MOV_B64>;
2503
2504// Bitfield extract patterns
2505
2506def IMMZeroBasedBitfieldMask : ImmLeaf <i32, [{
2507  return isMask_32(Imm);
2508}]>;
2509
2510def IMMPopCount : SDNodeXForm<imm, [{
2511  return CurDAG->getTargetConstant(countPopulation(N->getZExtValue()), SDLoc(N),
2512                                   MVT::i32);
2513}]>;
2514
2515def : AMDGPUPat <
2516  (DivergentBinFrag<and> (i32 (srl i32:$src, i32:$rshift)),
2517                         IMMZeroBasedBitfieldMask:$mask),
2518  (V_BFE_U32_e64 $src, $rshift, (i32 (IMMPopCount $mask)))
2519>;
2520
2521// x & ((1 << y) - 1)
2522def : AMDGPUPat <
2523  (DivergentBinFrag<and> i32:$src, (add_oneuse (shl_oneuse 1, i32:$width), -1)),
2524  (V_BFE_U32_e64 $src, (i32 0), $width)
2525>;
2526
2527// x & ~(-1 << y)
2528def : AMDGPUPat <
2529  (DivergentBinFrag<and> i32:$src,
2530                         (xor_oneuse (shl_oneuse -1, i32:$width), -1)),
2531  (V_BFE_U32_e64 $src, (i32 0), $width)
2532>;
2533
2534// x & (-1 >> (bitwidth - y))
2535def : AMDGPUPat <
2536  (DivergentBinFrag<and> i32:$src, (srl_oneuse -1, (sub 32, i32:$width))),
2537  (V_BFE_U32_e64 $src, (i32 0), $width)
2538>;
2539
2540// x << (bitwidth - y) >> (bitwidth - y)
2541def : AMDGPUPat <
2542  (DivergentBinFrag<srl> (shl_oneuse i32:$src, (sub 32, i32:$width)),
2543                         (sub 32, i32:$width)),
2544  (V_BFE_U32_e64 $src, (i32 0), $width)
2545>;
2546
2547def : AMDGPUPat <
2548  (DivergentBinFrag<sra> (shl_oneuse i32:$src, (sub 32, i32:$width)),
2549                         (sub 32, i32:$width)),
2550  (V_BFE_I32_e64 $src, (i32 0), $width)
2551>;
2552
2553// SHA-256 Ma patterns
2554
2555// ((x & z) | (y & (x | z))) -> BFI (XOR x, y), z, y
2556def : AMDGPUPat <
2557  (DivergentBinFrag<or> (and i32:$x, i32:$z),
2558                        (and i32:$y, (or i32:$x, i32:$z))),
2559  (V_BFI_B32_e64 (V_XOR_B32_e64 i32:$x, i32:$y), i32:$z, i32:$y)
2560>;
2561
2562def : AMDGPUPat <
2563  (DivergentBinFrag<or> (and i64:$x, i64:$z),
2564                        (and i64:$y, (or i64:$x, i64:$z))),
2565  (REG_SEQUENCE SReg_64,
2566    (V_BFI_B32_e64 (V_XOR_B32_e64 (i32 (EXTRACT_SUBREG SReg_64:$x, sub0)),
2567                    (i32 (EXTRACT_SUBREG SReg_64:$y, sub0))),
2568               (i32 (EXTRACT_SUBREG SReg_64:$z, sub0)),
2569               (i32 (EXTRACT_SUBREG SReg_64:$y, sub0))), sub0,
2570    (V_BFI_B32_e64 (V_XOR_B32_e64 (i32 (EXTRACT_SUBREG SReg_64:$x, sub1)),
2571                    (i32 (EXTRACT_SUBREG SReg_64:$y, sub1))),
2572               (i32 (EXTRACT_SUBREG SReg_64:$z, sub1)),
2573               (i32 (EXTRACT_SUBREG SReg_64:$y, sub1))), sub1)
2574>;
2575
2576multiclass IntMed3Pat<Instruction med3Inst,
2577                 SDPatternOperator min,
2578                 SDPatternOperator max,
2579                 SDPatternOperator min_oneuse,
2580                 SDPatternOperator max_oneuse> {
2581
2582  // This matches 16 permutations of
2583  // min(max(a, b), max(min(a, b), c))
2584  def : AMDGPUPat <
2585  (min (max_oneuse i32:$src0, i32:$src1),
2586       (max_oneuse (min_oneuse i32:$src0, i32:$src1), i32:$src2)),
2587  (med3Inst VSrc_b32:$src0, VSrc_b32:$src1, VSrc_b32:$src2)
2588>;
2589
2590  // This matches 16 permutations of
2591  // max(min(x, y), min(max(x, y), z))
2592  def : AMDGPUPat <
2593  (max (min_oneuse i32:$src0, i32:$src1),
2594       (min_oneuse (max_oneuse i32:$src0, i32:$src1), i32:$src2)),
2595  (med3Inst VSrc_b32:$src0, VSrc_b32:$src1, VSrc_b32:$src2)
2596>;
2597}
2598
2599defm : IntMed3Pat<V_MED3_I32_e64, smin, smax, smin_oneuse, smax_oneuse>;
2600defm : IntMed3Pat<V_MED3_U32_e64, umin, umax, umin_oneuse, umax_oneuse>;
2601
2602// This matches 16 permutations of
2603// max(min(x, y), min(max(x, y), z))
2604class FPMed3Pat<ValueType vt,
2605                //SDPatternOperator max, SDPatternOperator min,
2606                Instruction med3Inst> : GCNPat<
2607  (fmaxnum_like (fminnum_like_oneuse (VOP3Mods_nnan vt:$src0, i32:$src0_mods),
2608                           (VOP3Mods_nnan vt:$src1, i32:$src1_mods)),
2609           (fminnum_like_oneuse (fmaxnum_like_oneuse (VOP3Mods_nnan vt:$src0, i32:$src0_mods),
2610                                           (VOP3Mods_nnan vt:$src1, i32:$src1_mods)),
2611                           (vt (VOP3Mods_nnan vt:$src2, i32:$src2_mods)))),
2612  (med3Inst $src0_mods, $src0, $src1_mods, $src1, $src2_mods, $src2, DSTCLAMP.NONE, DSTOMOD.NONE)
2613>;
2614
2615class FP16Med3Pat<ValueType vt,
2616                Instruction med3Inst> : GCNPat<
2617  (fmaxnum_like (fminnum_like_oneuse (VOP3Mods_nnan vt:$src0, i32:$src0_mods),
2618                                     (VOP3Mods_nnan vt:$src1, i32:$src1_mods)),
2619           (fminnum_like_oneuse (fmaxnum_like_oneuse (VOP3Mods_nnan vt:$src0, i32:$src0_mods),
2620                                                     (VOP3Mods_nnan vt:$src1, i32:$src1_mods)),
2621                           (vt (VOP3Mods_nnan vt:$src2, i32:$src2_mods)))),
2622  (med3Inst $src0_mods, $src0, $src1_mods, $src1, $src2_mods, $src2, DSTCLAMP.NONE)
2623>;
2624
2625multiclass Int16Med3Pat<Instruction med3Inst,
2626                   SDPatternOperator min,
2627                   SDPatternOperator max,
2628                   SDPatternOperator max_oneuse,
2629                   SDPatternOperator min_oneuse> {
2630  // This matches 16 permutations of
2631  // max(min(x, y), min(max(x, y), z))
2632  def : GCNPat <
2633  (max (min_oneuse i16:$src0, i16:$src1),
2634       (min_oneuse (max_oneuse i16:$src0, i16:$src1), i16:$src2)),
2635  (med3Inst SRCMODS.NONE, VSrc_b16:$src0, SRCMODS.NONE, VSrc_b16:$src1, SRCMODS.NONE, VSrc_b16:$src2, DSTCLAMP.NONE)
2636>;
2637
2638  // This matches 16 permutations of
2639  // min(max(a, b), max(min(a, b), c))
2640  def : GCNPat <
2641  (min (max_oneuse i16:$src0, i16:$src1),
2642      (max_oneuse (min_oneuse i16:$src0, i16:$src1), i16:$src2)),
2643  (med3Inst SRCMODS.NONE, VSrc_b16:$src0, SRCMODS.NONE, VSrc_b16:$src1, SRCMODS.NONE, VSrc_b16:$src2, DSTCLAMP.NONE)
2644>;
2645}
2646
2647def : FPMed3Pat<f32, V_MED3_F32_e64>;
2648
2649let OtherPredicates = [isGFX9Plus] in {
2650def : FP16Med3Pat<f16, V_MED3_F16_e64>;
2651defm : Int16Med3Pat<V_MED3_I16_e64, smin, smax, smax_oneuse, smin_oneuse>;
2652defm : Int16Med3Pat<V_MED3_U16_e64, umin, umax, umax_oneuse, umin_oneuse>;
2653} // End Predicates = [isGFX9Plus]
2654
2655class AMDGPUGenericInstruction : GenericInstruction {
2656  let Namespace = "AMDGPU";
2657}
2658
2659def G_AMDGPU_FFBH_U32 : AMDGPUGenericInstruction {
2660  let OutOperandList = (outs type0:$dst);
2661  let InOperandList = (ins type1:$src);
2662  let hasSideEffects = 0;
2663}
2664
2665def G_AMDGPU_RCP_IFLAG : AMDGPUGenericInstruction {
2666  let OutOperandList = (outs type0:$dst);
2667  let InOperandList = (ins type1:$src);
2668  let hasSideEffects = 0;
2669}
2670
2671class BufferLoadGenericInstruction : AMDGPUGenericInstruction {
2672  let OutOperandList = (outs type0:$dst);
2673  let InOperandList = (ins type1:$rsrc, type2:$vindex, type2:$voffset,
2674                           type2:$soffset, untyped_imm_0:$offset,
2675                           untyped_imm_0:$cachepolicy, untyped_imm_0:$idxen);
2676  let hasSideEffects = 0;
2677  let mayLoad = 1;
2678}
2679
2680class TBufferLoadGenericInstruction : AMDGPUGenericInstruction {
2681  let OutOperandList = (outs type0:$dst);
2682  let InOperandList = (ins type1:$rsrc, type2:$vindex, type2:$voffset,
2683                           type2:$soffset, untyped_imm_0:$offset, untyped_imm_0:$format,
2684                           untyped_imm_0:$cachepolicy, untyped_imm_0:$idxen);
2685  let hasSideEffects = 0;
2686  let mayLoad = 1;
2687}
2688
2689def G_AMDGPU_BUFFER_LOAD_UBYTE : BufferLoadGenericInstruction;
2690def G_AMDGPU_BUFFER_LOAD_SBYTE : BufferLoadGenericInstruction;
2691def G_AMDGPU_BUFFER_LOAD_USHORT : BufferLoadGenericInstruction;
2692def G_AMDGPU_BUFFER_LOAD_SSHORT : BufferLoadGenericInstruction;
2693def G_AMDGPU_BUFFER_LOAD : BufferLoadGenericInstruction;
2694def G_AMDGPU_BUFFER_LOAD_FORMAT : BufferLoadGenericInstruction;
2695def G_AMDGPU_BUFFER_LOAD_FORMAT_D16 : BufferLoadGenericInstruction;
2696def G_AMDGPU_TBUFFER_LOAD_FORMAT : TBufferLoadGenericInstruction;
2697def G_AMDGPU_TBUFFER_LOAD_FORMAT_D16 : TBufferLoadGenericInstruction;
2698
2699class BufferStoreGenericInstruction : AMDGPUGenericInstruction {
2700  let OutOperandList = (outs);
2701  let InOperandList = (ins type0:$vdata, type1:$rsrc, type2:$vindex, type2:$voffset,
2702                           type2:$soffset, untyped_imm_0:$offset,
2703                           untyped_imm_0:$cachepolicy, untyped_imm_0:$idxen);
2704  let hasSideEffects = 0;
2705  let mayStore = 1;
2706}
2707
2708class TBufferStoreGenericInstruction : AMDGPUGenericInstruction {
2709  let OutOperandList = (outs);
2710  let InOperandList = (ins type0:$vdata, type1:$rsrc, type2:$vindex, type2:$voffset,
2711                           type2:$soffset, untyped_imm_0:$offset,
2712                           untyped_imm_0:$format,
2713                           untyped_imm_0:$cachepolicy, untyped_imm_0:$idxen);
2714  let hasSideEffects = 0;
2715  let mayStore = 1;
2716}
2717
2718def G_AMDGPU_BUFFER_STORE : BufferStoreGenericInstruction;
2719def G_AMDGPU_BUFFER_STORE_BYTE : BufferStoreGenericInstruction;
2720def G_AMDGPU_BUFFER_STORE_SHORT : BufferStoreGenericInstruction;
2721def G_AMDGPU_BUFFER_STORE_FORMAT : BufferStoreGenericInstruction;
2722def G_AMDGPU_BUFFER_STORE_FORMAT_D16 : BufferStoreGenericInstruction;
2723def G_AMDGPU_TBUFFER_STORE_FORMAT : TBufferStoreGenericInstruction;
2724def G_AMDGPU_TBUFFER_STORE_FORMAT_D16 : TBufferStoreGenericInstruction;
2725
2726def G_AMDGPU_FMIN_LEGACY : AMDGPUGenericInstruction {
2727  let OutOperandList = (outs type0:$dst);
2728  let InOperandList = (ins type0:$src0, type0:$src1);
2729  let hasSideEffects = 0;
2730}
2731
2732def G_AMDGPU_FMAX_LEGACY : AMDGPUGenericInstruction {
2733  let OutOperandList = (outs type0:$dst);
2734  let InOperandList = (ins type0:$src0, type0:$src1);
2735  let hasSideEffects = 0;
2736}
2737
2738foreach N = 0-3 in {
2739def G_AMDGPU_CVT_F32_UBYTE#N : AMDGPUGenericInstruction {
2740  let OutOperandList = (outs type0:$dst);
2741  let InOperandList = (ins type0:$src0);
2742  let hasSideEffects = 0;
2743}
2744}
2745
2746def G_AMDGPU_CVT_PK_I16_I32 : AMDGPUGenericInstruction {
2747  let OutOperandList = (outs type0:$dst);
2748  let InOperandList = (ins type0:$src0, type0:$src1);
2749  let hasSideEffects = 0;
2750}
2751
2752def G_AMDGPU_SMED3 : AMDGPUGenericInstruction {
2753  let OutOperandList = (outs type0:$dst);
2754  let InOperandList = (ins type0:$src0, type0:$src1, type0:$src2);
2755  let hasSideEffects = 0;
2756}
2757
2758def G_AMDGPU_UMED3 : AMDGPUGenericInstruction {
2759  let OutOperandList = (outs type0:$dst);
2760  let InOperandList = (ins type0:$src0, type0:$src1, type0:$src2);
2761  let hasSideEffects = 0;
2762}
2763
2764// Atomic cmpxchg. $cmpval ad $newval are packed in a single vector
2765// operand Expects a MachineMemOperand in addition to explicit
2766// operands.
2767def G_AMDGPU_ATOMIC_CMPXCHG : AMDGPUGenericInstruction {
2768  let OutOperandList = (outs type0:$oldval);
2769  let InOperandList = (ins ptype1:$addr, type0:$cmpval_newval);
2770  let hasSideEffects = 0;
2771  let mayLoad = 1;
2772  let mayStore = 1;
2773}
2774
2775let Namespace = "AMDGPU" in {
2776def G_AMDGPU_ATOMIC_INC : G_ATOMICRMW_OP;
2777def G_AMDGPU_ATOMIC_DEC : G_ATOMICRMW_OP;
2778def G_AMDGPU_ATOMIC_FMIN : G_ATOMICRMW_OP;
2779def G_AMDGPU_ATOMIC_FMAX : G_ATOMICRMW_OP;
2780}
2781
2782class BufferAtomicGenericInstruction<bit NoRtn = 0> : AMDGPUGenericInstruction {
2783  let OutOperandList = !if(NoRtn, (outs), (outs type0:$dst));
2784  let InOperandList = (ins type0:$vdata, type1:$rsrc, type2:$vindex, type2:$voffset,
2785                           type2:$soffset, untyped_imm_0:$offset,
2786                           untyped_imm_0:$cachepolicy, untyped_imm_0:$idxen);
2787  let hasSideEffects = 0;
2788  let mayLoad = 1;
2789  let mayStore = 1;
2790}
2791
2792def G_AMDGPU_BUFFER_ATOMIC_SWAP : BufferAtomicGenericInstruction;
2793def G_AMDGPU_BUFFER_ATOMIC_ADD : BufferAtomicGenericInstruction;
2794def G_AMDGPU_BUFFER_ATOMIC_SUB : BufferAtomicGenericInstruction;
2795def G_AMDGPU_BUFFER_ATOMIC_SMIN : BufferAtomicGenericInstruction;
2796def G_AMDGPU_BUFFER_ATOMIC_UMIN : BufferAtomicGenericInstruction;
2797def G_AMDGPU_BUFFER_ATOMIC_SMAX : BufferAtomicGenericInstruction;
2798def G_AMDGPU_BUFFER_ATOMIC_UMAX : BufferAtomicGenericInstruction;
2799def G_AMDGPU_BUFFER_ATOMIC_AND : BufferAtomicGenericInstruction;
2800def G_AMDGPU_BUFFER_ATOMIC_OR : BufferAtomicGenericInstruction;
2801def G_AMDGPU_BUFFER_ATOMIC_XOR : BufferAtomicGenericInstruction;
2802def G_AMDGPU_BUFFER_ATOMIC_INC : BufferAtomicGenericInstruction;
2803def G_AMDGPU_BUFFER_ATOMIC_DEC : BufferAtomicGenericInstruction;
2804def G_AMDGPU_BUFFER_ATOMIC_FADD : BufferAtomicGenericInstruction;
2805def G_AMDGPU_BUFFER_ATOMIC_FMIN : BufferAtomicGenericInstruction;
2806def G_AMDGPU_BUFFER_ATOMIC_FMAX : BufferAtomicGenericInstruction;
2807
2808def G_AMDGPU_BUFFER_ATOMIC_CMPSWAP : AMDGPUGenericInstruction {
2809  let OutOperandList = (outs type0:$dst);
2810  let InOperandList = (ins type0:$vdata, type0:$cmp, type1:$rsrc, type2:$vindex,
2811                           type2:$voffset, type2:$soffset, untyped_imm_0:$offset,
2812                           untyped_imm_0:$cachepolicy, untyped_imm_0:$idxen);
2813  let hasSideEffects = 0;
2814  let mayLoad = 1;
2815  let mayStore = 1;
2816}
2817
2818// Wrapper around llvm.amdgcn.s.buffer.load. This is mostly needed as
2819// a workaround for the intrinsic being defined as readnone, but
2820// really needs a memory operand.
2821def G_AMDGPU_S_BUFFER_LOAD : AMDGPUGenericInstruction {
2822  let OutOperandList = (outs type0:$dst);
2823  let InOperandList = (ins type1:$rsrc, type2:$offset, untyped_imm_0:$cachepolicy);
2824  let hasSideEffects = 0;
2825  let mayLoad = 1;
2826  let mayStore = 0;
2827}
2828
2829// This is equivalent to the G_INTRINSIC*, but the operands may have
2830// been legalized depending on the subtarget requirements.
2831def G_AMDGPU_INTRIN_IMAGE_LOAD : AMDGPUGenericInstruction {
2832  let OutOperandList = (outs type0:$dst);
2833  let InOperandList = (ins unknown:$intrin, variable_ops);
2834  let hasSideEffects = 0;
2835  let mayLoad = 1;
2836
2837  // FIXME: Use separate opcode for atomics.
2838  let mayStore = 1;
2839}
2840
2841// This is equivalent to the G_INTRINSIC*, but the operands may have
2842// been legalized depending on the subtarget requirements.
2843def G_AMDGPU_INTRIN_IMAGE_STORE : AMDGPUGenericInstruction {
2844  let OutOperandList = (outs);
2845  let InOperandList = (ins unknown:$intrin, variable_ops);
2846  let hasSideEffects = 0;
2847  let mayStore = 1;
2848}
2849
2850def G_AMDGPU_INTRIN_BVH_INTERSECT_RAY : AMDGPUGenericInstruction {
2851  let OutOperandList = (outs type0:$dst);
2852  let InOperandList = (ins unknown:$intrin, variable_ops);
2853  let hasSideEffects = 0;
2854  let mayLoad = 1;
2855  let mayStore = 0;
2856}
2857