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