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