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