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