]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Target/AMDGPU/SIInstructions.td
Import tzdata 2018d
[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
730 /********** ============================================ **********/
731 /********** Extraction, Insertion, Building and Casting  **********/
732 /********** ============================================ **********/
733
734 foreach Index = 0-2 in {
735   def Extract_Element_v2i32_#Index : Extract_Element <
736     i32, v2i32, Index, !cast<SubRegIndex>(sub#Index)
737   >;
738   def Insert_Element_v2i32_#Index : Insert_Element <
739     i32, v2i32, Index, !cast<SubRegIndex>(sub#Index)
740   >;
741
742   def Extract_Element_v2f32_#Index : Extract_Element <
743     f32, v2f32, Index, !cast<SubRegIndex>(sub#Index)
744   >;
745   def Insert_Element_v2f32_#Index : Insert_Element <
746     f32, v2f32, Index, !cast<SubRegIndex>(sub#Index)
747   >;
748 }
749
750 foreach Index = 0-3 in {
751   def Extract_Element_v4i32_#Index : Extract_Element <
752     i32, v4i32, Index, !cast<SubRegIndex>(sub#Index)
753   >;
754   def Insert_Element_v4i32_#Index : Insert_Element <
755     i32, v4i32, Index, !cast<SubRegIndex>(sub#Index)
756   >;
757
758   def Extract_Element_v4f32_#Index : Extract_Element <
759     f32, v4f32, Index, !cast<SubRegIndex>(sub#Index)
760   >;
761   def Insert_Element_v4f32_#Index : Insert_Element <
762     f32, v4f32, Index, !cast<SubRegIndex>(sub#Index)
763   >;
764 }
765
766 foreach Index = 0-7 in {
767   def Extract_Element_v8i32_#Index : Extract_Element <
768     i32, v8i32, Index, !cast<SubRegIndex>(sub#Index)
769   >;
770   def Insert_Element_v8i32_#Index : Insert_Element <
771     i32, v8i32, Index, !cast<SubRegIndex>(sub#Index)
772   >;
773
774   def Extract_Element_v8f32_#Index : Extract_Element <
775     f32, v8f32, Index, !cast<SubRegIndex>(sub#Index)
776   >;
777   def Insert_Element_v8f32_#Index : Insert_Element <
778     f32, v8f32, Index, !cast<SubRegIndex>(sub#Index)
779   >;
780 }
781
782 foreach Index = 0-15 in {
783   def Extract_Element_v16i32_#Index : Extract_Element <
784     i32, v16i32, Index, !cast<SubRegIndex>(sub#Index)
785   >;
786   def Insert_Element_v16i32_#Index : Insert_Element <
787     i32, v16i32, Index, !cast<SubRegIndex>(sub#Index)
788   >;
789
790   def Extract_Element_v16f32_#Index : Extract_Element <
791     f32, v16f32, Index, !cast<SubRegIndex>(sub#Index)
792   >;
793   def Insert_Element_v16f32_#Index : Insert_Element <
794     f32, v16f32, Index, !cast<SubRegIndex>(sub#Index)
795   >;
796 }
797
798 let SubtargetPredicate = isGCN in {
799
800 // FIXME: Why do only some of these type combinations for SReg and
801 // VReg?
802 // 16-bit bitcast
803 def : BitConvert <i16, f16, VGPR_32>;
804 def : BitConvert <f16, i16, VGPR_32>;
805 def : BitConvert <i16, f16, SReg_32>;
806 def : BitConvert <f16, i16, SReg_32>;
807
808 // 32-bit bitcast
809 def : BitConvert <i32, f32, VGPR_32>;
810 def : BitConvert <f32, i32, VGPR_32>;
811 def : BitConvert <i32, f32, SReg_32>;
812 def : BitConvert <f32, i32, SReg_32>;
813 def : BitConvert <v2i16, i32, SReg_32>;
814 def : BitConvert <i32, v2i16, SReg_32>;
815 def : BitConvert <v2f16, i32, SReg_32>;
816 def : BitConvert <i32, v2f16, SReg_32>;
817 def : BitConvert <v2i16, v2f16, SReg_32>;
818 def : BitConvert <v2f16, v2i16, SReg_32>;
819 def : BitConvert <v2f16, f32, SReg_32>;
820 def : BitConvert <f32, v2f16, SReg_32>;
821 def : BitConvert <v2i16, f32, SReg_32>;
822 def : BitConvert <f32, v2i16, SReg_32>;
823
824 // 64-bit bitcast
825 def : BitConvert <i64, f64, VReg_64>;
826 def : BitConvert <f64, i64, VReg_64>;
827 def : BitConvert <v2i32, v2f32, VReg_64>;
828 def : BitConvert <v2f32, v2i32, VReg_64>;
829 def : BitConvert <i64, v2i32, VReg_64>;
830 def : BitConvert <v2i32, i64, VReg_64>;
831 def : BitConvert <i64, v2f32, VReg_64>;
832 def : BitConvert <v2f32, i64, VReg_64>;
833 def : BitConvert <f64, v2f32, VReg_64>;
834 def : BitConvert <v2f32, f64, VReg_64>;
835 def : BitConvert <f64, v2i32, VReg_64>;
836 def : BitConvert <v2i32, f64, VReg_64>;
837 def : BitConvert <v4i32, v4f32, VReg_128>;
838 def : BitConvert <v4f32, v4i32, VReg_128>;
839
840 // 128-bit bitcast
841 def : BitConvert <v2i64, v4i32, SReg_128>;
842 def : BitConvert <v4i32, v2i64, SReg_128>;
843 def : BitConvert <v2f64, v4f32, VReg_128>;
844 def : BitConvert <v2f64, v4i32, VReg_128>;
845 def : BitConvert <v4f32, v2f64, VReg_128>;
846 def : BitConvert <v4i32, v2f64, VReg_128>;
847 def : BitConvert <v2i64, v2f64, VReg_128>;
848 def : BitConvert <v2f64, v2i64, VReg_128>;
849
850 // 256-bit bitcast
851 def : BitConvert <v8i32, v8f32, SReg_256>;
852 def : BitConvert <v8f32, v8i32, SReg_256>;
853 def : BitConvert <v8i32, v8f32, VReg_256>;
854 def : BitConvert <v8f32, v8i32, VReg_256>;
855
856 // 512-bit bitcast
857 def : BitConvert <v16i32, v16f32, VReg_512>;
858 def : BitConvert <v16f32, v16i32, VReg_512>;
859
860 } // End SubtargetPredicate = isGCN
861
862 /********** =================== **********/
863 /********** Src & Dst modifiers **********/
864 /********** =================== **********/
865
866
867 // If denormals are not enabled, it only impacts the compare of the
868 // inputs. The output result is not flushed.
869 class ClampPat<Instruction inst, ValueType vt> : GCNPat <
870   (vt (AMDGPUclamp (VOP3Mods vt:$src0, i32:$src0_modifiers))),
871   (inst i32:$src0_modifiers, vt:$src0,
872         i32:$src0_modifiers, vt:$src0, DSTCLAMP.ENABLE, DSTOMOD.NONE)
873 >;
874
875 def : ClampPat<V_MAX_F32_e64, f32>;
876 def : ClampPat<V_MAX_F64, f64>;
877 def : ClampPat<V_MAX_F16_e64, f16>;
878
879 def : GCNPat <
880   (v2f16 (AMDGPUclamp (VOP3PMods v2f16:$src0, i32:$src0_modifiers))),
881   (V_PK_MAX_F16 $src0_modifiers, $src0,
882                 $src0_modifiers, $src0, DSTCLAMP.ENABLE)
883 >;
884
885 /********** ================================ **********/
886 /********** Floating point absolute/negative **********/
887 /********** ================================ **********/
888
889 // Prevent expanding both fneg and fabs.
890
891 def : GCNPat <
892   (fneg (fabs f32:$src)),
893   (S_OR_B32 $src, (S_MOV_B32(i32 0x80000000))) // Set sign bit
894 >;
895
896 // FIXME: Should use S_OR_B32
897 def : GCNPat <
898   (fneg (fabs f64:$src)),
899   (REG_SEQUENCE VReg_64,
900     (i32 (EXTRACT_SUBREG f64:$src, sub0)),
901     sub0,
902     (V_OR_B32_e32 (i32 (EXTRACT_SUBREG f64:$src, sub1)),
903                   (V_MOV_B32_e32 (i32 0x80000000))), // Set sign bit.
904     sub1)
905 >;
906
907 def : GCNPat <
908   (fabs f32:$src),
909   (V_AND_B32_e64 $src, (V_MOV_B32_e32 (i32 0x7fffffff)))
910 >;
911
912 def : GCNPat <
913   (fneg f32:$src),
914   (V_XOR_B32_e32 $src, (V_MOV_B32_e32 (i32 0x80000000)))
915 >;
916
917 def : GCNPat <
918   (fabs f64:$src),
919   (REG_SEQUENCE VReg_64,
920     (i32 (EXTRACT_SUBREG f64:$src, sub0)),
921     sub0,
922     (V_AND_B32_e64 (i32 (EXTRACT_SUBREG f64:$src, sub1)),
923                    (V_MOV_B32_e32 (i32 0x7fffffff))), // Set sign bit.
924      sub1)
925 >;
926
927 def : GCNPat <
928   (fneg f64:$src),
929   (REG_SEQUENCE VReg_64,
930     (i32 (EXTRACT_SUBREG f64:$src, sub0)),
931     sub0,
932     (V_XOR_B32_e32 (i32 (EXTRACT_SUBREG f64:$src, sub1)),
933                    (i32 (V_MOV_B32_e32 (i32 0x80000000)))),
934     sub1)
935 >;
936
937 def : GCNPat <
938   (fcopysign f16:$src0, f16:$src1),
939   (V_BFI_B32 (S_MOV_B32 (i32 0x00007fff)), $src0, $src1)
940 >;
941
942 def : GCNPat <
943   (fcopysign f32:$src0, f16:$src1),
944   (V_BFI_B32 (S_MOV_B32 (i32 0x7fffffff)), $src0,
945              (V_LSHLREV_B32_e64 (i32 16), $src1))
946 >;
947
948 def : GCNPat <
949   (fcopysign f64:$src0, f16:$src1),
950   (REG_SEQUENCE SReg_64,
951     (i32 (EXTRACT_SUBREG $src0, sub0)), sub0,
952     (V_BFI_B32 (S_MOV_B32 (i32 0x7fffffff)), (i32 (EXTRACT_SUBREG $src0, sub1)),
953                (V_LSHLREV_B32_e64 (i32 16), $src1)), sub1)
954 >;
955
956 def : GCNPat <
957   (fcopysign f16:$src0, f32:$src1),
958   (V_BFI_B32 (S_MOV_B32 (i32 0x00007fff)), $src0,
959              (V_LSHRREV_B32_e64 (i32 16), $src1))
960 >;
961
962 def : GCNPat <
963   (fcopysign f16:$src0, f64:$src1),
964   (V_BFI_B32 (S_MOV_B32 (i32 0x00007fff)), $src0,
965              (V_LSHRREV_B32_e64 (i32 16), (EXTRACT_SUBREG $src1, sub1)))
966 >;
967
968 def : GCNPat <
969   (fneg f16:$src),
970   (V_XOR_B32_e32 $src, (V_MOV_B32_e32 (i32 0x00008000)))
971 >;
972
973 def : GCNPat <
974   (fabs f16:$src),
975   (V_AND_B32_e64 $src, (V_MOV_B32_e32 (i32 0x00007fff)))
976 >;
977
978 def : GCNPat <
979   (fneg (fabs f16:$src)),
980   (S_OR_B32 $src, (S_MOV_B32 (i32 0x00008000))) // Set sign bit
981 >;
982
983 def : GCNPat <
984   (fneg v2f16:$src),
985   (V_XOR_B32_e64 (S_MOV_B32 (i32 0x80008000)), $src)
986 >;
987
988 def : GCNPat <
989   (fabs v2f16:$src),
990   (V_AND_B32_e64 (S_MOV_B32 (i32 0x7fff7fff)), $src)
991 >;
992
993 // This is really (fneg (fabs v2f16:$src))
994 //
995 // fabs is not reported as free because there is modifier for it in
996 // VOP3P instructions, so it is turned into the bit op.
997 def : GCNPat <
998   (fneg (v2f16 (bitconvert (and_oneuse i32:$src, 0x7fff7fff)))),
999   (S_OR_B32 (S_MOV_B32 (i32 0x80008000)), $src) // Set sign bit
1000 >;
1001
1002 /********** ================== **********/
1003 /********** Immediate Patterns **********/
1004 /********** ================== **********/
1005
1006 def : GCNPat <
1007   (VGPRImm<(i32 imm)>:$imm),
1008   (V_MOV_B32_e32 imm:$imm)
1009 >;
1010
1011 def : GCNPat <
1012   (VGPRImm<(f32 fpimm)>:$imm),
1013   (V_MOV_B32_e32 (f32 (bitcast_fpimm_to_i32 $imm)))
1014 >;
1015
1016 def : GCNPat <
1017   (i32 imm:$imm),
1018   (S_MOV_B32 imm:$imm)
1019 >;
1020
1021 // FIXME: Workaround for ordering issue with peephole optimizer where
1022 // a register class copy interferes with immediate folding.  Should
1023 // use s_mov_b32, which can be shrunk to s_movk_i32
1024 def : GCNPat <
1025   (VGPRImm<(f16 fpimm)>:$imm),
1026   (V_MOV_B32_e32 (f16 (bitcast_fpimm_to_i32 $imm)))
1027 >;
1028
1029 def : GCNPat <
1030   (f32 fpimm:$imm),
1031   (S_MOV_B32 (f32 (bitcast_fpimm_to_i32 $imm)))
1032 >;
1033
1034 def : GCNPat <
1035   (f16 fpimm:$imm),
1036   (S_MOV_B32 (i32 (bitcast_fpimm_to_i32 $imm)))
1037 >;
1038
1039 def : GCNPat <
1040  (i32 frameindex:$fi),
1041  (V_MOV_B32_e32 (i32 (frameindex_to_targetframeindex $fi)))
1042 >;
1043
1044 def : GCNPat <
1045   (i64 InlineImm<i64>:$imm),
1046   (S_MOV_B64 InlineImm<i64>:$imm)
1047 >;
1048
1049 // XXX - Should this use a s_cmp to set SCC?
1050
1051 // Set to sign-extended 64-bit value (true = -1, false = 0)
1052 def : GCNPat <
1053   (i1 imm:$imm),
1054   (S_MOV_B64 (i64 (as_i64imm $imm)))
1055 >;
1056
1057 def : GCNPat <
1058   (f64 InlineFPImm<f64>:$imm),
1059   (S_MOV_B64 (f64 (bitcast_fpimm_to_i64 InlineFPImm<f64>:$imm)))
1060 >;
1061
1062 /********** ================== **********/
1063 /********** Intrinsic Patterns **********/
1064 /********** ================== **********/
1065
1066 let SubtargetPredicate = isGCN in {
1067 def : POW_Common <V_LOG_F32_e32, V_EXP_F32_e32, V_MUL_LEGACY_F32_e32>;
1068 }
1069
1070 def : GCNPat <
1071   (i32 (sext i1:$src0)),
1072   (V_CNDMASK_B32_e64 (i32 0), (i32 -1), $src0)
1073 >;
1074
1075 class Ext32Pat <SDNode ext> : GCNPat <
1076   (i32 (ext i1:$src0)),
1077   (V_CNDMASK_B32_e64 (i32 0), (i32 1), $src0)
1078 >;
1079
1080 def : Ext32Pat <zext>;
1081 def : Ext32Pat <anyext>;
1082
1083 // The multiplication scales from [0,1] to the unsigned integer range
1084 def : GCNPat <
1085   (AMDGPUurecip i32:$src0),
1086   (V_CVT_U32_F32_e32
1087     (V_MUL_F32_e32 (i32 CONST.FP_UINT_MAX_PLUS_1),
1088                    (V_RCP_IFLAG_F32_e32 (V_CVT_F32_U32_e32 $src0))))
1089 >;
1090
1091 //===----------------------------------------------------------------------===//
1092 // VOP3 Patterns
1093 //===----------------------------------------------------------------------===//
1094
1095 let SubtargetPredicate = isGCN in {
1096
1097 def : IMad24Pat<V_MAD_I32_I24, 1>;
1098 def : UMad24Pat<V_MAD_U32_U24, 1>;
1099
1100 defm : BFIPatterns <V_BFI_B32, S_MOV_B32, SReg_64>;
1101 def : ROTRPattern <V_ALIGNBIT_B32>;
1102
1103 }
1104
1105 def : GCNPat<(i32 (trunc (srl i64:$src0, (and i32:$src1, (i32 31))))),
1106           (V_ALIGNBIT_B32 (i32 (EXTRACT_SUBREG (i64 $src0), sub1)),
1107                           (i32 (EXTRACT_SUBREG (i64 $src0), sub0)), $src1)>;
1108
1109 def : GCNPat<(i32 (trunc (srl i64:$src0, (i32 ShiftAmt32Imm:$src1)))),
1110           (V_ALIGNBIT_B32 (i32 (EXTRACT_SUBREG (i64 $src0), sub1)),
1111                           (i32 (EXTRACT_SUBREG (i64 $src0), sub0)), $src1)>;
1112
1113 /********** ====================== **********/
1114 /**********   Indirect addressing  **********/
1115 /********** ====================== **********/
1116
1117 multiclass SI_INDIRECT_Pattern <ValueType vt, ValueType eltvt, string VecSize> {
1118   // Extract with offset
1119   def : GCNPat<
1120     (eltvt (extractelt vt:$src, (MOVRELOffset i32:$idx, (i32 imm:$offset)))),
1121     (!cast<Instruction>("SI_INDIRECT_SRC_"#VecSize) $src, $idx, imm:$offset)
1122   >;
1123
1124   // Insert with offset
1125   def : GCNPat<
1126     (insertelt vt:$src, eltvt:$val, (MOVRELOffset i32:$idx, (i32 imm:$offset))),
1127     (!cast<Instruction>("SI_INDIRECT_DST_"#VecSize) $src, $idx, imm:$offset, $val)
1128   >;
1129 }
1130
1131 defm : SI_INDIRECT_Pattern <v2f32, f32, "V2">;
1132 defm : SI_INDIRECT_Pattern <v4f32, f32, "V4">;
1133 defm : SI_INDIRECT_Pattern <v8f32, f32, "V8">;
1134 defm : SI_INDIRECT_Pattern <v16f32, f32, "V16">;
1135
1136 defm : SI_INDIRECT_Pattern <v2i32, i32, "V2">;
1137 defm : SI_INDIRECT_Pattern <v4i32, i32, "V4">;
1138 defm : SI_INDIRECT_Pattern <v8i32, i32, "V8">;
1139 defm : SI_INDIRECT_Pattern <v16i32, i32, "V16">;
1140
1141 //===----------------------------------------------------------------------===//
1142 // SAD Patterns
1143 //===----------------------------------------------------------------------===//
1144
1145 def : GCNPat <
1146   (add (sub_oneuse (umax i32:$src0, i32:$src1),
1147                    (umin i32:$src0, i32:$src1)),
1148        i32:$src2),
1149   (V_SAD_U32 $src0, $src1, $src2, (i1 0))
1150 >;
1151
1152 def : GCNPat <
1153   (add (select_oneuse (i1 (setugt i32:$src0, i32:$src1)),
1154                       (sub i32:$src0, i32:$src1),
1155                       (sub i32:$src1, i32:$src0)),
1156        i32:$src2),
1157   (V_SAD_U32 $src0, $src1, $src2, (i1 0))
1158 >;
1159
1160 //===----------------------------------------------------------------------===//
1161 // Conversion Patterns
1162 //===----------------------------------------------------------------------===//
1163
1164 def : GCNPat<(i32 (sext_inreg i32:$src, i1)),
1165   (S_BFE_I32 i32:$src, (i32 65536))>; // 0 | 1 << 16
1166
1167 // Handle sext_inreg in i64
1168 def : GCNPat <
1169   (i64 (sext_inreg i64:$src, i1)),
1170   (S_BFE_I64 i64:$src, (i32 0x10000)) // 0 | 1 << 16
1171 >;
1172
1173 def : GCNPat <
1174   (i16 (sext_inreg i16:$src, i1)),
1175   (S_BFE_I32 $src, (i32 0x00010000)) // 0 | 1 << 16
1176 >;
1177
1178 def : GCNPat <
1179   (i16 (sext_inreg i16:$src, i8)),
1180   (S_BFE_I32 $src, (i32 0x80000)) // 0 | 8 << 16
1181 >;
1182
1183 def : GCNPat <
1184   (i64 (sext_inreg i64:$src, i8)),
1185   (S_BFE_I64 i64:$src, (i32 0x80000)) // 0 | 8 << 16
1186 >;
1187
1188 def : GCNPat <
1189   (i64 (sext_inreg i64:$src, i16)),
1190   (S_BFE_I64 i64:$src, (i32 0x100000)) // 0 | 16 << 16
1191 >;
1192
1193 def : GCNPat <
1194   (i64 (sext_inreg i64:$src, i32)),
1195   (S_BFE_I64 i64:$src, (i32 0x200000)) // 0 | 32 << 16
1196 >;
1197
1198 def : GCNPat <
1199   (i64 (zext i32:$src)),
1200   (REG_SEQUENCE SReg_64, $src, sub0, (S_MOV_B32 (i32 0)), sub1)
1201 >;
1202
1203 def : GCNPat <
1204   (i64 (anyext i32:$src)),
1205   (REG_SEQUENCE SReg_64, $src, sub0, (i32 (IMPLICIT_DEF)), sub1)
1206 >;
1207
1208 class ZExt_i64_i1_Pat <SDNode ext> : GCNPat <
1209   (i64 (ext i1:$src)),
1210     (REG_SEQUENCE VReg_64,
1211       (V_CNDMASK_B32_e64 (i32 0), (i32 1), $src), sub0,
1212       (S_MOV_B32 (i32 0)), sub1)
1213 >;
1214
1215
1216 def : ZExt_i64_i1_Pat<zext>;
1217 def : ZExt_i64_i1_Pat<anyext>;
1218
1219 // FIXME: We need to use COPY_TO_REGCLASS to work-around the fact that
1220 // REG_SEQUENCE patterns don't support instructions with multiple outputs.
1221 def : GCNPat <
1222   (i64 (sext i32:$src)),
1223     (REG_SEQUENCE SReg_64, $src, sub0,
1224     (i32 (COPY_TO_REGCLASS (S_ASHR_I32 $src, (i32 31)), SReg_32_XM0)), sub1)
1225 >;
1226
1227 def : GCNPat <
1228   (i64 (sext i1:$src)),
1229   (REG_SEQUENCE VReg_64,
1230     (V_CNDMASK_B32_e64 (i32 0), (i32 -1), $src), sub0,
1231     (V_CNDMASK_B32_e64 (i32 0), (i32 -1), $src), sub1)
1232 >;
1233
1234 class FPToI1Pat<Instruction Inst, int KOne, ValueType kone_type, ValueType vt, SDPatternOperator fp_to_int> : GCNPat <
1235   (i1 (fp_to_int (vt (VOP3Mods vt:$src0, i32:$src0_modifiers)))),
1236   (i1 (Inst 0, (kone_type KOne), $src0_modifiers, $src0, DSTCLAMP.NONE))
1237 >;
1238
1239 def : FPToI1Pat<V_CMP_EQ_F32_e64, CONST.FP32_ONE, i32, f32, fp_to_uint>;
1240 def : FPToI1Pat<V_CMP_EQ_F32_e64, CONST.FP32_NEG_ONE, i32, f32, fp_to_sint>;
1241 def : FPToI1Pat<V_CMP_EQ_F64_e64, CONST.FP64_ONE, i64, f64, fp_to_uint>;
1242 def : FPToI1Pat<V_CMP_EQ_F64_e64, CONST.FP64_NEG_ONE, i64, f64, fp_to_sint>;
1243
1244 // If we need to perform a logical operation on i1 values, we need to
1245 // use vector comparisons since there is only one SCC register. Vector
1246 // comparisons still write to a pair of SGPRs, so treat these as
1247 // 64-bit comparisons. When legalizing SGPR copies, instructions
1248 // resulting in the copies from SCC to these instructions will be
1249 // moved to the VALU.
1250 def : GCNPat <
1251   (i1 (and i1:$src0, i1:$src1)),
1252   (S_AND_B64 $src0, $src1)
1253 >;
1254
1255 def : GCNPat <
1256   (i1 (or i1:$src0, i1:$src1)),
1257   (S_OR_B64 $src0, $src1)
1258 >;
1259
1260 def : GCNPat <
1261   (i1 (xor i1:$src0, i1:$src1)),
1262   (S_XOR_B64 $src0, $src1)
1263 >;
1264
1265 def : GCNPat <
1266   (f32 (sint_to_fp i1:$src)),
1267   (V_CNDMASK_B32_e64 (i32 0), (i32 CONST.FP32_NEG_ONE), $src)
1268 >;
1269
1270 def : GCNPat <
1271   (f32 (uint_to_fp i1:$src)),
1272   (V_CNDMASK_B32_e64 (i32 0), (i32 CONST.FP32_ONE), $src)
1273 >;
1274
1275 def : GCNPat <
1276   (f64 (sint_to_fp i1:$src)),
1277   (V_CVT_F64_I32_e32 (V_CNDMASK_B32_e64 (i32 0), (i32 -1), $src))
1278 >;
1279
1280 def : GCNPat <
1281   (f64 (uint_to_fp i1:$src)),
1282   (V_CVT_F64_U32_e32 (V_CNDMASK_B32_e64 (i32 0), (i32 1), $src))
1283 >;
1284
1285 //===----------------------------------------------------------------------===//
1286 // Miscellaneous Patterns
1287 //===----------------------------------------------------------------------===//
1288 def : GCNPat <
1289   (i32 (AMDGPUfp16_zext f16:$src)),
1290   (COPY $src)
1291 >;
1292
1293
1294 def : GCNPat <
1295   (i32 (trunc i64:$a)),
1296   (EXTRACT_SUBREG $a, sub0)
1297 >;
1298
1299 def : GCNPat <
1300   (i1 (trunc i32:$a)),
1301   (V_CMP_EQ_U32_e64 (S_AND_B32 (i32 1), $a), (i32 1))
1302 >;
1303
1304 def : GCNPat <
1305   (i1 (trunc i16:$a)),
1306   (V_CMP_EQ_U32_e64 (S_AND_B32 (i32 1), $a), (i32 1))
1307 >;
1308
1309 def : GCNPat <
1310   (i1 (trunc i64:$a)),
1311   (V_CMP_EQ_U32_e64 (S_AND_B32 (i32 1),
1312                     (i32 (EXTRACT_SUBREG $a, sub0))), (i32 1))
1313 >;
1314
1315 def : GCNPat <
1316   (i32 (bswap i32:$a)),
1317   (V_BFI_B32 (S_MOV_B32 (i32 0x00ff00ff)),
1318              (V_ALIGNBIT_B32 $a, $a, (i32 24)),
1319              (V_ALIGNBIT_B32 $a, $a, (i32 8)))
1320 >;
1321
1322 let OtherPredicates = [NoFP16Denormals] in {
1323 def : GCNPat<
1324   (fcanonicalize (f16 (VOP3Mods f16:$src, i32:$src_mods))),
1325   (V_MUL_F16_e64 0, (i32 CONST.FP16_ONE), $src_mods, $src, 0, 0)
1326 >;
1327
1328 def : GCNPat<
1329   (fcanonicalize (v2f16 (VOP3PMods v2f16:$src, i32:$src_mods))),
1330   (V_PK_MUL_F16 0, (i32 CONST.V2FP16_ONE), $src_mods, $src, DSTCLAMP.NONE)
1331 >;
1332 }
1333
1334 let OtherPredicates = [FP16Denormals] in {
1335 def : GCNPat<
1336   (fcanonicalize (f16 (VOP3Mods f16:$src, i32:$src_mods))),
1337   (V_MAX_F16_e64 $src_mods, $src, $src_mods, $src, 0, 0)
1338 >;
1339
1340 def : GCNPat<
1341   (fcanonicalize (v2f16 (VOP3PMods v2f16:$src, i32:$src_mods))),
1342   (V_PK_MAX_F16 $src_mods, $src, $src_mods, $src, DSTCLAMP.NONE)
1343 >;
1344 }
1345
1346 let OtherPredicates = [NoFP32Denormals] in {
1347 def : GCNPat<
1348   (fcanonicalize (f32 (VOP3Mods f32:$src, i32:$src_mods))),
1349   (V_MUL_F32_e64 0, (i32 CONST.FP32_ONE), $src_mods, $src, 0, 0)
1350 >;
1351 }
1352
1353 let OtherPredicates = [FP32Denormals] in {
1354 def : GCNPat<
1355   (fcanonicalize (f32 (VOP3Mods f32:$src, i32:$src_mods))),
1356   (V_MAX_F32_e64 $src_mods, $src, $src_mods, $src, 0, 0)
1357 >;
1358 }
1359
1360 let OtherPredicates = [NoFP64Denormals] in {
1361 def : GCNPat<
1362   (fcanonicalize (f64 (VOP3Mods f64:$src, i32:$src_mods))),
1363   (V_MUL_F64 0, CONST.FP64_ONE, $src_mods, $src, 0, 0)
1364 >;
1365 }
1366
1367 let OtherPredicates = [FP64Denormals] in {
1368 def : GCNPat<
1369   (fcanonicalize (f64 (VOP3Mods f64:$src, i32:$src_mods))),
1370   (V_MAX_F64 $src_mods, $src, $src_mods, $src, 0, 0)
1371 >;
1372 }
1373
1374
1375 // Allow integer inputs
1376 class ExpPattern<SDPatternOperator node, ValueType vt, Instruction Inst> : GCNPat<
1377   (node (i8 timm:$tgt), (i8 timm:$en), vt:$src0, vt:$src1, vt:$src2, vt:$src3, (i1 timm:$compr), (i1 timm:$vm)),
1378   (Inst i8:$tgt, vt:$src0, vt:$src1, vt:$src2, vt:$src3, i1:$vm, i1:$compr, i8:$en)
1379 >;
1380
1381 def : ExpPattern<AMDGPUexport, i32, EXP>;
1382 def : ExpPattern<AMDGPUexport_done, i32, EXP_DONE>;
1383
1384 def : GCNPat <
1385   (v2i16 (build_vector i16:$src0, i16:$src1)),
1386   (v2i16 (S_PACK_LL_B32_B16 $src0, $src1))
1387 >;
1388
1389 // COPY_TO_REGCLASS is workaround tablegen bug from multiple outputs
1390 // from S_LSHL_B32's multiple outputs from implicit scc def.
1391 def : GCNPat <
1392   (v2i16 (build_vector (i16 0), i16:$src1)),
1393   (v2i16 (COPY_TO_REGCLASS (S_LSHL_B32 i16:$src1, (i16 16)), SReg_32_XM0))
1394 >;
1395
1396 // With multiple uses of the shift, this will duplicate the shift and
1397 // increase register pressure.
1398 def : GCNPat <
1399   (v2i16 (build_vector i16:$src0, (i16 (trunc (srl_oneuse i32:$src1, (i32 16)))))),
1400   (v2i16 (S_PACK_LH_B32_B16 i16:$src0, i32:$src1))
1401 >;
1402
1403 def : GCNPat <
1404   (v2i16 (build_vector (i16 (trunc (srl_oneuse i32:$src0, (i32 16)))),
1405                        (i16 (trunc (srl_oneuse i32:$src1, (i32 16)))))),
1406   (v2i16 (S_PACK_HH_B32_B16 $src0, $src1))
1407 >;
1408
1409 // TODO: Should source modifiers be matched to v_pack_b32_f16?
1410 def : GCNPat <
1411   (v2f16 (build_vector f16:$src0, f16:$src1)),
1412   (v2f16 (S_PACK_LL_B32_B16 $src0, $src1))
1413 >;
1414
1415 // def : GCNPat <
1416 //   (v2f16 (scalar_to_vector f16:$src0)),
1417 //   (COPY $src0)
1418 // >;
1419
1420 // def : GCNPat <
1421 //   (v2i16 (scalar_to_vector i16:$src0)),
1422 //   (COPY $src0)
1423 // >;
1424
1425 //===----------------------------------------------------------------------===//
1426 // Fract Patterns
1427 //===----------------------------------------------------------------------===//
1428
1429 let SubtargetPredicate = isSI in {
1430
1431 // V_FRACT is buggy on SI, so the F32 version is never used and (x-floor(x)) is
1432 // used instead. However, SI doesn't have V_FLOOR_F64, so the most efficient
1433 // way to implement it is using V_FRACT_F64.
1434 // The workaround for the V_FRACT bug is:
1435 //    fract(x) = isnan(x) ? x : min(V_FRACT(x), 0.99999999999999999)
1436
1437 // Convert floor(x) to (x - fract(x))
1438 def : GCNPat <
1439   (f64 (ffloor (f64 (VOP3Mods f64:$x, i32:$mods)))),
1440   (V_ADD_F64
1441       $mods,
1442       $x,
1443       SRCMODS.NEG,
1444       (V_CNDMASK_B64_PSEUDO
1445          (V_MIN_F64
1446              SRCMODS.NONE,
1447              (V_FRACT_F64_e64 $mods, $x, DSTCLAMP.NONE, DSTOMOD.NONE),
1448              SRCMODS.NONE,
1449              (V_MOV_B64_PSEUDO 0x3fefffffffffffff),
1450              DSTCLAMP.NONE, DSTOMOD.NONE),
1451          $x,
1452          (V_CMP_CLASS_F64_e64 SRCMODS.NONE, $x, (i32 3 /*NaN*/))),
1453       DSTCLAMP.NONE, DSTOMOD.NONE)
1454 >;
1455
1456 } // End SubtargetPredicates = isSI
1457
1458 //============================================================================//
1459 // Miscellaneous Optimization Patterns
1460 //============================================================================//
1461
1462 // Undo sub x, c -> add x, -c canonicalization since c is more likely
1463 // an inline immediate than -c.
1464 // TODO: Also do for 64-bit.
1465 def : GCNPat<
1466   (add i32:$src0, (i32 NegSubInlineConst32:$src1)),
1467   (S_SUB_I32 $src0, NegSubInlineConst32:$src1)
1468 >;
1469
1470
1471 multiclass BFMPatterns <ValueType vt, InstSI BFM, InstSI MOV> {
1472   def : GCNPat <
1473     (vt (shl (vt (add (vt (shl 1, vt:$a)), -1)), vt:$b)),
1474     (BFM $a, $b)
1475   >;
1476
1477   def : GCNPat <
1478     (vt (add (vt (shl 1, vt:$a)), -1)),
1479     (BFM $a, (MOV (i32 0)))
1480   >;
1481 }
1482
1483 let SubtargetPredicate = isGCN in {
1484
1485 defm : BFMPatterns <i32, S_BFM_B32, S_MOV_B32>;
1486 // FIXME: defm : BFMPatterns <i64, S_BFM_B64, S_MOV_B64>;
1487
1488 defm : BFEPattern <V_BFE_U32, V_BFE_I32, S_MOV_B32>;
1489 def : SHA256MaPattern <V_BFI_B32, V_XOR_B32_e64>;
1490
1491 def : IntMed3Pat<V_MED3_I32, smax, smax_oneuse, smin_oneuse>;
1492 def : IntMed3Pat<V_MED3_U32, umax, umax_oneuse, umin_oneuse>;
1493
1494 }
1495
1496 // This matches 16 permutations of
1497 // max(min(x, y), min(max(x, y), z))
1498 class FPMed3Pat<ValueType vt,
1499                 Instruction med3Inst> : GCNPat<
1500   (fmaxnum (fminnum_oneuse (VOP3Mods_nnan vt:$src0, i32:$src0_mods),
1501                            (VOP3Mods_nnan vt:$src1, i32:$src1_mods)),
1502            (fminnum_oneuse (fmaxnum_oneuse (VOP3Mods_nnan vt:$src0, i32:$src0_mods),
1503                                            (VOP3Mods_nnan vt:$src1, i32:$src1_mods)),
1504                            (vt (VOP3Mods_nnan vt:$src2, i32:$src2_mods)))),
1505   (med3Inst $src0_mods, $src0, $src1_mods, $src1, $src2_mods, $src2, DSTCLAMP.NONE, DSTOMOD.NONE)
1506 >;
1507
1508 class FP16Med3Pat<ValueType vt,
1509                 Instruction med3Inst> : GCNPat<
1510   (fmaxnum (fminnum_oneuse (VOP3Mods_nnan vt:$src0, i32:$src0_mods),
1511                            (VOP3Mods_nnan vt:$src1, i32:$src1_mods)),
1512            (fminnum_oneuse (fmaxnum_oneuse (VOP3Mods_nnan vt:$src0, i32:$src0_mods),
1513                                            (VOP3Mods_nnan vt:$src1, i32:$src1_mods)),
1514                            (vt (VOP3Mods_nnan vt:$src2, i32:$src2_mods)))),
1515   (med3Inst $src0_mods, $src0, $src1_mods, $src1, $src2_mods, $src2, DSTCLAMP.NONE)
1516 >;
1517
1518 class Int16Med3Pat<Instruction med3Inst,
1519                    SDPatternOperator max,
1520                    SDPatternOperator max_oneuse,
1521                    SDPatternOperator min_oneuse,
1522                    ValueType vt = i32> : GCNPat<
1523   (max (min_oneuse vt:$src0, vt:$src1),
1524        (min_oneuse (max_oneuse vt:$src0, vt:$src1), vt:$src2)),
1525   (med3Inst SRCMODS.NONE, $src0, SRCMODS.NONE, $src1, SRCMODS.NONE, $src2, DSTCLAMP.NONE)
1526 >;
1527
1528 def : FPMed3Pat<f32, V_MED3_F32>;
1529
1530 let OtherPredicates = [isGFX9] in {
1531 def : FP16Med3Pat<f16, V_MED3_F16>;
1532 def : Int16Med3Pat<V_MED3_I16, smax, smax_oneuse, smin_oneuse, i16>;
1533 def : Int16Med3Pat<V_MED3_U16, umax, umax_oneuse, umin_oneuse, i16>;
1534 } // End Predicates = [isGFX9]