1 //===-- SIISelLowering.cpp - SI DAG Lowering Implementation ---------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file
10 /// Custom DAG lowering for SI
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "SIISelLowering.h"
15 #include "AMDGPU.h"
16 #include "AMDGPUInstrInfo.h"
17 #include "AMDGPUTargetMachine.h"
18 #include "SIMachineFunctionInfo.h"
19 #include "SIRegisterInfo.h"
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/Analysis/LegacyDivergenceAnalysis.h"
22 #include "llvm/BinaryFormat/ELF.h"
23 #include "llvm/CodeGen/Analysis.h"
24 #include "llvm/CodeGen/FunctionLoweringInfo.h"
25 #include "llvm/CodeGen/GlobalISel/GISelKnownBits.h"
26 #include "llvm/CodeGen/MachineLoopInfo.h"
27 #include "llvm/IR/DiagnosticInfo.h"
28 #include "llvm/IR/IntrinsicsAMDGPU.h"
29 #include "llvm/IR/IntrinsicsR600.h"
30 #include "llvm/Support/CommandLine.h"
31 #include "llvm/Support/KnownBits.h"
32 
33 using namespace llvm;
34 
35 #define DEBUG_TYPE "si-lower"
36 
37 STATISTIC(NumTailCalls, "Number of tail calls");
38 
39 static cl::opt<bool> DisableLoopAlignment(
40   "amdgpu-disable-loop-alignment",
41   cl::desc("Do not align and prefetch loops"),
42   cl::init(false));
43 
44 static cl::opt<bool> VGPRReserveforSGPRSpill(
45     "amdgpu-reserve-vgpr-for-sgpr-spill",
46     cl::desc("Allocates one VGPR for future SGPR Spill"), cl::init(true));
47 
48 static cl::opt<bool> UseDivergentRegisterIndexing(
49   "amdgpu-use-divergent-register-indexing",
50   cl::Hidden,
51   cl::desc("Use indirect register addressing for divergent indexes"),
52   cl::init(false));
53 
54 static bool hasFP32Denormals(const MachineFunction &MF) {
55   const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
56   return Info->getMode().allFP32Denormals();
57 }
58 
59 static bool hasFP64FP16Denormals(const MachineFunction &MF) {
60   const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
61   return Info->getMode().allFP64FP16Denormals();
62 }
63 
64 static unsigned findFirstFreeSGPR(CCState &CCInfo) {
65   unsigned NumSGPRs = AMDGPU::SGPR_32RegClass.getNumRegs();
66   for (unsigned Reg = 0; Reg < NumSGPRs; ++Reg) {
67     if (!CCInfo.isAllocated(AMDGPU::SGPR0 + Reg)) {
68       return AMDGPU::SGPR0 + Reg;
69     }
70   }
71   llvm_unreachable("Cannot allocate sgpr");
72 }
73 
74 SITargetLowering::SITargetLowering(const TargetMachine &TM,
75                                    const GCNSubtarget &STI)
76     : AMDGPUTargetLowering(TM, STI),
77       Subtarget(&STI) {
78   addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass);
79   addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass);
80 
81   addRegisterClass(MVT::i32, &AMDGPU::SReg_32RegClass);
82   addRegisterClass(MVT::f32, &AMDGPU::VGPR_32RegClass);
83 
84   addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass);
85 
86   const SIRegisterInfo *TRI = STI.getRegisterInfo();
87   const TargetRegisterClass *V64RegClass = TRI->getVGPR64Class();
88 
89   addRegisterClass(MVT::f64, V64RegClass);
90   addRegisterClass(MVT::v2f32, V64RegClass);
91 
92   addRegisterClass(MVT::v3i32, &AMDGPU::SGPR_96RegClass);
93   addRegisterClass(MVT::v3f32, TRI->getVGPRClassForBitWidth(96));
94 
95   addRegisterClass(MVT::v2i64, &AMDGPU::SGPR_128RegClass);
96   addRegisterClass(MVT::v2f64, &AMDGPU::SGPR_128RegClass);
97 
98   addRegisterClass(MVT::v4i32, &AMDGPU::SGPR_128RegClass);
99   addRegisterClass(MVT::v4f32, TRI->getVGPRClassForBitWidth(128));
100 
101   addRegisterClass(MVT::v5i32, &AMDGPU::SGPR_160RegClass);
102   addRegisterClass(MVT::v5f32, TRI->getVGPRClassForBitWidth(160));
103 
104   addRegisterClass(MVT::v8i32, &AMDGPU::SGPR_256RegClass);
105   addRegisterClass(MVT::v8f32, TRI->getVGPRClassForBitWidth(256));
106 
107   addRegisterClass(MVT::v4i64, &AMDGPU::SGPR_256RegClass);
108   addRegisterClass(MVT::v4f64, TRI->getVGPRClassForBitWidth(256));
109 
110   addRegisterClass(MVT::v16i32, &AMDGPU::SGPR_512RegClass);
111   addRegisterClass(MVT::v16f32, TRI->getVGPRClassForBitWidth(512));
112 
113   addRegisterClass(MVT::v8i64, &AMDGPU::SGPR_512RegClass);
114   addRegisterClass(MVT::v8f64, TRI->getVGPRClassForBitWidth(512));
115 
116   addRegisterClass(MVT::v16i64, &AMDGPU::SGPR_1024RegClass);
117   addRegisterClass(MVT::v16f64, TRI->getVGPRClassForBitWidth(1024));
118 
119   if (Subtarget->has16BitInsts()) {
120     addRegisterClass(MVT::i16, &AMDGPU::SReg_32RegClass);
121     addRegisterClass(MVT::f16, &AMDGPU::SReg_32RegClass);
122 
123     // Unless there are also VOP3P operations, not operations are really legal.
124     addRegisterClass(MVT::v2i16, &AMDGPU::SReg_32RegClass);
125     addRegisterClass(MVT::v2f16, &AMDGPU::SReg_32RegClass);
126     addRegisterClass(MVT::v4i16, &AMDGPU::SReg_64RegClass);
127     addRegisterClass(MVT::v4f16, &AMDGPU::SReg_64RegClass);
128   }
129 
130   addRegisterClass(MVT::v32i32, &AMDGPU::VReg_1024RegClass);
131   addRegisterClass(MVT::v32f32, TRI->getVGPRClassForBitWidth(1024));
132 
133   computeRegisterProperties(Subtarget->getRegisterInfo());
134 
135   // The boolean content concept here is too inflexible. Compares only ever
136   // really produce a 1-bit result. Any copy/extend from these will turn into a
137   // select, and zext/1 or sext/-1 are equally cheap. Arbitrarily choose 0/1, as
138   // it's what most targets use.
139   setBooleanContents(ZeroOrOneBooleanContent);
140   setBooleanVectorContents(ZeroOrOneBooleanContent);
141 
142   // We need to custom lower vector stores from local memory
143   setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
144   setOperationAction(ISD::LOAD, MVT::v3i32, Custom);
145   setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
146   setOperationAction(ISD::LOAD, MVT::v5i32, Custom);
147   setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
148   setOperationAction(ISD::LOAD, MVT::v16i32, Custom);
149   setOperationAction(ISD::LOAD, MVT::i1, Custom);
150   setOperationAction(ISD::LOAD, MVT::v32i32, Custom);
151 
152   setOperationAction(ISD::STORE, MVT::v2i32, Custom);
153   setOperationAction(ISD::STORE, MVT::v3i32, Custom);
154   setOperationAction(ISD::STORE, MVT::v4i32, Custom);
155   setOperationAction(ISD::STORE, MVT::v5i32, Custom);
156   setOperationAction(ISD::STORE, MVT::v8i32, Custom);
157   setOperationAction(ISD::STORE, MVT::v16i32, Custom);
158   setOperationAction(ISD::STORE, MVT::i1, Custom);
159   setOperationAction(ISD::STORE, MVT::v32i32, Custom);
160 
161   setTruncStoreAction(MVT::v2i32, MVT::v2i16, Expand);
162   setTruncStoreAction(MVT::v3i32, MVT::v3i16, Expand);
163   setTruncStoreAction(MVT::v4i32, MVT::v4i16, Expand);
164   setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand);
165   setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand);
166   setTruncStoreAction(MVT::v32i32, MVT::v32i16, Expand);
167   setTruncStoreAction(MVT::v2i32, MVT::v2i8, Expand);
168   setTruncStoreAction(MVT::v4i32, MVT::v4i8, Expand);
169   setTruncStoreAction(MVT::v8i32, MVT::v8i8, Expand);
170   setTruncStoreAction(MVT::v16i32, MVT::v16i8, Expand);
171   setTruncStoreAction(MVT::v32i32, MVT::v32i8, Expand);
172   setTruncStoreAction(MVT::v2i16, MVT::v2i8, Expand);
173   setTruncStoreAction(MVT::v4i16, MVT::v4i8, Expand);
174   setTruncStoreAction(MVT::v8i16, MVT::v8i8, Expand);
175   setTruncStoreAction(MVT::v16i16, MVT::v16i8, Expand);
176   setTruncStoreAction(MVT::v32i16, MVT::v32i8, Expand);
177 
178   setTruncStoreAction(MVT::v4i64, MVT::v4i8, Expand);
179   setTruncStoreAction(MVT::v8i64, MVT::v8i8, Expand);
180   setTruncStoreAction(MVT::v8i64, MVT::v8i16, Expand);
181   setTruncStoreAction(MVT::v8i64, MVT::v8i32, Expand);
182   setTruncStoreAction(MVT::v16i64, MVT::v16i32, Expand);
183 
184   setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
185   setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
186 
187   setOperationAction(ISD::SELECT, MVT::i1, Promote);
188   setOperationAction(ISD::SELECT, MVT::i64, Custom);
189   setOperationAction(ISD::SELECT, MVT::f64, Promote);
190   AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64);
191 
192   setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
193   setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
194   setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
195   setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
196   setOperationAction(ISD::SELECT_CC, MVT::i1, Expand);
197 
198   setOperationAction(ISD::SETCC, MVT::i1, Promote);
199   setOperationAction(ISD::SETCC, MVT::v2i1, Expand);
200   setOperationAction(ISD::SETCC, MVT::v4i1, Expand);
201   AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
202 
203   setOperationAction(ISD::TRUNCATE, MVT::v2i32, Expand);
204   setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
205   setOperationAction(ISD::TRUNCATE, MVT::v4i32, Expand);
206   setOperationAction(ISD::FP_ROUND, MVT::v4f32, Expand);
207   setOperationAction(ISD::TRUNCATE, MVT::v8i32, Expand);
208   setOperationAction(ISD::FP_ROUND, MVT::v8f32, Expand);
209   setOperationAction(ISD::TRUNCATE, MVT::v16i32, Expand);
210   setOperationAction(ISD::FP_ROUND, MVT::v16f32, Expand);
211 
212   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom);
213   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom);
214   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
215   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom);
216   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
217   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v3i16, Custom);
218   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom);
219   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom);
220 
221   setOperationAction(ISD::BRCOND, MVT::Other, Custom);
222   setOperationAction(ISD::BR_CC, MVT::i1, Expand);
223   setOperationAction(ISD::BR_CC, MVT::i32, Expand);
224   setOperationAction(ISD::BR_CC, MVT::i64, Expand);
225   setOperationAction(ISD::BR_CC, MVT::f32, Expand);
226   setOperationAction(ISD::BR_CC, MVT::f64, Expand);
227 
228   setOperationAction(ISD::UADDO, MVT::i32, Legal);
229   setOperationAction(ISD::USUBO, MVT::i32, Legal);
230 
231   setOperationAction(ISD::ADDCARRY, MVT::i32, Legal);
232   setOperationAction(ISD::SUBCARRY, MVT::i32, Legal);
233 
234   setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
235   setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
236   setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
237 
238 #if 0
239   setOperationAction(ISD::ADDCARRY, MVT::i64, Legal);
240   setOperationAction(ISD::SUBCARRY, MVT::i64, Legal);
241 #endif
242 
243   // We only support LOAD/STORE and vector manipulation ops for vectors
244   // with > 4 elements.
245   for (MVT VT : { MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32,
246                   MVT::v2i64, MVT::v2f64, MVT::v4i16, MVT::v4f16,
247                   MVT::v4i64, MVT::v4f64, MVT::v8i64, MVT::v8f64,
248                   MVT::v16i64, MVT::v16f64, MVT::v32i32, MVT::v32f32 }) {
249     for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
250       switch (Op) {
251       case ISD::LOAD:
252       case ISD::STORE:
253       case ISD::BUILD_VECTOR:
254       case ISD::BITCAST:
255       case ISD::EXTRACT_VECTOR_ELT:
256       case ISD::INSERT_VECTOR_ELT:
257       case ISD::INSERT_SUBVECTOR:
258       case ISD::EXTRACT_SUBVECTOR:
259       case ISD::SCALAR_TO_VECTOR:
260         break;
261       case ISD::CONCAT_VECTORS:
262         setOperationAction(Op, VT, Custom);
263         break;
264       default:
265         setOperationAction(Op, VT, Expand);
266         break;
267       }
268     }
269   }
270 
271   setOperationAction(ISD::FP_EXTEND, MVT::v4f32, Expand);
272 
273   // TODO: For dynamic 64-bit vector inserts/extracts, should emit a pseudo that
274   // is expanded to avoid having two separate loops in case the index is a VGPR.
275 
276   // Most operations are naturally 32-bit vector operations. We only support
277   // load and store of i64 vectors, so promote v2i64 vector operations to v4i32.
278   for (MVT Vec64 : { MVT::v2i64, MVT::v2f64 }) {
279     setOperationAction(ISD::BUILD_VECTOR, Vec64, Promote);
280     AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v4i32);
281 
282     setOperationAction(ISD::EXTRACT_VECTOR_ELT, Vec64, Promote);
283     AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v4i32);
284 
285     setOperationAction(ISD::INSERT_VECTOR_ELT, Vec64, Promote);
286     AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v4i32);
287 
288     setOperationAction(ISD::SCALAR_TO_VECTOR, Vec64, Promote);
289     AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v4i32);
290   }
291 
292   for (MVT Vec64 : { MVT::v4i64, MVT::v4f64 }) {
293     setOperationAction(ISD::BUILD_VECTOR, Vec64, Promote);
294     AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v8i32);
295 
296     setOperationAction(ISD::EXTRACT_VECTOR_ELT, Vec64, Promote);
297     AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v8i32);
298 
299     setOperationAction(ISD::INSERT_VECTOR_ELT, Vec64, Promote);
300     AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v8i32);
301 
302     setOperationAction(ISD::SCALAR_TO_VECTOR, Vec64, Promote);
303     AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v8i32);
304   }
305 
306   for (MVT Vec64 : { MVT::v8i64, MVT::v8f64 }) {
307     setOperationAction(ISD::BUILD_VECTOR, Vec64, Promote);
308     AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v16i32);
309 
310     setOperationAction(ISD::EXTRACT_VECTOR_ELT, Vec64, Promote);
311     AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v16i32);
312 
313     setOperationAction(ISD::INSERT_VECTOR_ELT, Vec64, Promote);
314     AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v16i32);
315 
316     setOperationAction(ISD::SCALAR_TO_VECTOR, Vec64, Promote);
317     AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v16i32);
318   }
319 
320   for (MVT Vec64 : { MVT::v16i64, MVT::v16f64 }) {
321     setOperationAction(ISD::BUILD_VECTOR, Vec64, Promote);
322     AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v32i32);
323 
324     setOperationAction(ISD::EXTRACT_VECTOR_ELT, Vec64, Promote);
325     AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v32i32);
326 
327     setOperationAction(ISD::INSERT_VECTOR_ELT, Vec64, Promote);
328     AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v32i32);
329 
330     setOperationAction(ISD::SCALAR_TO_VECTOR, Vec64, Promote);
331     AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v32i32);
332   }
333 
334   setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand);
335   setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand);
336   setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand);
337   setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand);
338 
339   setOperationAction(ISD::BUILD_VECTOR, MVT::v4f16, Custom);
340   setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
341 
342   // Avoid stack access for these.
343   // TODO: Generalize to more vector types.
344   setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i16, Custom);
345   setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f16, Custom);
346   setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
347   setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f16, Custom);
348 
349   setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i16, Custom);
350   setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f16, Custom);
351   setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i8, Custom);
352   setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i8, Custom);
353   setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i8, Custom);
354 
355   setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i8, Custom);
356   setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i8, Custom);
357   setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i8, Custom);
358 
359   setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i16, Custom);
360   setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f16, Custom);
361   setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
362   setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f16, Custom);
363 
364   // Deal with vec3 vector operations when widened to vec4.
365   setOperationAction(ISD::INSERT_SUBVECTOR, MVT::v3i32, Custom);
366   setOperationAction(ISD::INSERT_SUBVECTOR, MVT::v3f32, Custom);
367   setOperationAction(ISD::INSERT_SUBVECTOR, MVT::v4i32, Custom);
368   setOperationAction(ISD::INSERT_SUBVECTOR, MVT::v4f32, Custom);
369 
370   // Deal with vec5 vector operations when widened to vec8.
371   setOperationAction(ISD::INSERT_SUBVECTOR, MVT::v5i32, Custom);
372   setOperationAction(ISD::INSERT_SUBVECTOR, MVT::v5f32, Custom);
373   setOperationAction(ISD::INSERT_SUBVECTOR, MVT::v8i32, Custom);
374   setOperationAction(ISD::INSERT_SUBVECTOR, MVT::v8f32, Custom);
375 
376   // BUFFER/FLAT_ATOMIC_CMP_SWAP on GCN GPUs needs input marshalling,
377   // and output demarshalling
378   setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
379   setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
380 
381   // We can't return success/failure, only the old value,
382   // let LLVM add the comparison
383   setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i32, Expand);
384   setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i64, Expand);
385 
386   if (Subtarget->hasFlatAddressSpace()) {
387     setOperationAction(ISD::ADDRSPACECAST, MVT::i32, Custom);
388     setOperationAction(ISD::ADDRSPACECAST, MVT::i64, Custom);
389   }
390 
391   setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
392   setOperationAction(ISD::BITREVERSE, MVT::i64, Legal);
393 
394   // FIXME: This should be narrowed to i32, but that only happens if i64 is
395   // illegal.
396   // FIXME: Should lower sub-i32 bswaps to bit-ops without v_perm_b32.
397   setOperationAction(ISD::BSWAP, MVT::i64, Legal);
398   setOperationAction(ISD::BSWAP, MVT::i32, Legal);
399 
400   // On SI this is s_memtime and s_memrealtime on VI.
401   setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal);
402   setOperationAction(ISD::TRAP, MVT::Other, Custom);
403   setOperationAction(ISD::DEBUGTRAP, MVT::Other, Custom);
404 
405   if (Subtarget->has16BitInsts()) {
406     setOperationAction(ISD::FPOW, MVT::f16, Promote);
407     setOperationAction(ISD::FPOWI, MVT::f16, Promote);
408     setOperationAction(ISD::FLOG, MVT::f16, Custom);
409     setOperationAction(ISD::FEXP, MVT::f16, Custom);
410     setOperationAction(ISD::FLOG10, MVT::f16, Custom);
411   }
412 
413   if (Subtarget->hasMadMacF32Insts())
414     setOperationAction(ISD::FMAD, MVT::f32, Legal);
415 
416   if (!Subtarget->hasBFI()) {
417     // fcopysign can be done in a single instruction with BFI.
418     setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
419     setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
420   }
421 
422   if (!Subtarget->hasBCNT(32))
423     setOperationAction(ISD::CTPOP, MVT::i32, Expand);
424 
425   if (!Subtarget->hasBCNT(64))
426     setOperationAction(ISD::CTPOP, MVT::i64, Expand);
427 
428   if (Subtarget->hasFFBH())
429     setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Custom);
430 
431   if (Subtarget->hasFFBL())
432     setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Custom);
433 
434   // We only really have 32-bit BFE instructions (and 16-bit on VI).
435   //
436   // On SI+ there are 64-bit BFEs, but they are scalar only and there isn't any
437   // effort to match them now. We want this to be false for i64 cases when the
438   // extraction isn't restricted to the upper or lower half. Ideally we would
439   // have some pass reduce 64-bit extracts to 32-bit if possible. Extracts that
440   // span the midpoint are probably relatively rare, so don't worry about them
441   // for now.
442   if (Subtarget->hasBFE())
443     setHasExtractBitsInsn(true);
444 
445   // Clamp modifier on add/sub
446   if (Subtarget->hasIntClamp()) {
447     setOperationAction(ISD::UADDSAT, MVT::i32, Legal);
448     setOperationAction(ISD::USUBSAT, MVT::i32, Legal);
449   }
450 
451   if (Subtarget->hasAddNoCarry()) {
452     setOperationAction(ISD::SADDSAT, MVT::i16, Legal);
453     setOperationAction(ISD::SSUBSAT, MVT::i16, Legal);
454     setOperationAction(ISD::SADDSAT, MVT::i32, Legal);
455     setOperationAction(ISD::SSUBSAT, MVT::i32, Legal);
456   }
457 
458   setOperationAction(ISD::FMINNUM, MVT::f32, Custom);
459   setOperationAction(ISD::FMAXNUM, MVT::f32, Custom);
460   setOperationAction(ISD::FMINNUM, MVT::f64, Custom);
461   setOperationAction(ISD::FMAXNUM, MVT::f64, Custom);
462 
463 
464   // These are really only legal for ieee_mode functions. We should be avoiding
465   // them for functions that don't have ieee_mode enabled, so just say they are
466   // legal.
467   setOperationAction(ISD::FMINNUM_IEEE, MVT::f32, Legal);
468   setOperationAction(ISD::FMAXNUM_IEEE, MVT::f32, Legal);
469   setOperationAction(ISD::FMINNUM_IEEE, MVT::f64, Legal);
470   setOperationAction(ISD::FMAXNUM_IEEE, MVT::f64, Legal);
471 
472 
473   if (Subtarget->haveRoundOpsF64()) {
474     setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
475     setOperationAction(ISD::FCEIL, MVT::f64, Legal);
476     setOperationAction(ISD::FRINT, MVT::f64, Legal);
477   } else {
478     setOperationAction(ISD::FCEIL, MVT::f64, Custom);
479     setOperationAction(ISD::FTRUNC, MVT::f64, Custom);
480     setOperationAction(ISD::FRINT, MVT::f64, Custom);
481     setOperationAction(ISD::FFLOOR, MVT::f64, Custom);
482   }
483 
484   setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
485 
486   setOperationAction(ISD::FSIN, MVT::f32, Custom);
487   setOperationAction(ISD::FCOS, MVT::f32, Custom);
488   setOperationAction(ISD::FDIV, MVT::f32, Custom);
489   setOperationAction(ISD::FDIV, MVT::f64, Custom);
490 
491   if (Subtarget->has16BitInsts()) {
492     setOperationAction(ISD::Constant, MVT::i16, Legal);
493 
494     setOperationAction(ISD::SMIN, MVT::i16, Legal);
495     setOperationAction(ISD::SMAX, MVT::i16, Legal);
496 
497     setOperationAction(ISD::UMIN, MVT::i16, Legal);
498     setOperationAction(ISD::UMAX, MVT::i16, Legal);
499 
500     setOperationAction(ISD::SIGN_EXTEND, MVT::i16, Promote);
501     AddPromotedToType(ISD::SIGN_EXTEND, MVT::i16, MVT::i32);
502 
503     setOperationAction(ISD::ROTR, MVT::i16, Expand);
504     setOperationAction(ISD::ROTL, MVT::i16, Expand);
505 
506     setOperationAction(ISD::SDIV, MVT::i16, Promote);
507     setOperationAction(ISD::UDIV, MVT::i16, Promote);
508     setOperationAction(ISD::SREM, MVT::i16, Promote);
509     setOperationAction(ISD::UREM, MVT::i16, Promote);
510     setOperationAction(ISD::UADDSAT, MVT::i16, Legal);
511     setOperationAction(ISD::USUBSAT, MVT::i16, Legal);
512 
513     setOperationAction(ISD::BITREVERSE, MVT::i16, Promote);
514 
515     setOperationAction(ISD::CTTZ, MVT::i16, Promote);
516     setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16, Promote);
517     setOperationAction(ISD::CTLZ, MVT::i16, Promote);
518     setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16, Promote);
519     setOperationAction(ISD::CTPOP, MVT::i16, Promote);
520 
521     setOperationAction(ISD::SELECT_CC, MVT::i16, Expand);
522 
523     setOperationAction(ISD::BR_CC, MVT::i16, Expand);
524 
525     setOperationAction(ISD::LOAD, MVT::i16, Custom);
526 
527     setTruncStoreAction(MVT::i64, MVT::i16, Expand);
528 
529     setOperationAction(ISD::FP16_TO_FP, MVT::i16, Promote);
530     AddPromotedToType(ISD::FP16_TO_FP, MVT::i16, MVT::i32);
531     setOperationAction(ISD::FP_TO_FP16, MVT::i16, Promote);
532     AddPromotedToType(ISD::FP_TO_FP16, MVT::i16, MVT::i32);
533 
534     setOperationAction(ISD::FP_TO_SINT, MVT::i16, Custom);
535     setOperationAction(ISD::FP_TO_UINT, MVT::i16, Custom);
536 
537     // F16 - Constant Actions.
538     setOperationAction(ISD::ConstantFP, MVT::f16, Legal);
539 
540     // F16 - Load/Store Actions.
541     setOperationAction(ISD::LOAD, MVT::f16, Promote);
542     AddPromotedToType(ISD::LOAD, MVT::f16, MVT::i16);
543     setOperationAction(ISD::STORE, MVT::f16, Promote);
544     AddPromotedToType(ISD::STORE, MVT::f16, MVT::i16);
545 
546     // F16 - VOP1 Actions.
547     setOperationAction(ISD::FP_ROUND, MVT::f16, Custom);
548     setOperationAction(ISD::FCOS, MVT::f16, Custom);
549     setOperationAction(ISD::FSIN, MVT::f16, Custom);
550 
551     setOperationAction(ISD::SINT_TO_FP, MVT::i16, Custom);
552     setOperationAction(ISD::UINT_TO_FP, MVT::i16, Custom);
553 
554     setOperationAction(ISD::FP_TO_SINT, MVT::f16, Promote);
555     setOperationAction(ISD::FP_TO_UINT, MVT::f16, Promote);
556     setOperationAction(ISD::SINT_TO_FP, MVT::f16, Promote);
557     setOperationAction(ISD::UINT_TO_FP, MVT::f16, Promote);
558     setOperationAction(ISD::FROUND, MVT::f16, Custom);
559 
560     // F16 - VOP2 Actions.
561     setOperationAction(ISD::BR_CC, MVT::f16, Expand);
562     setOperationAction(ISD::SELECT_CC, MVT::f16, Expand);
563 
564     setOperationAction(ISD::FDIV, MVT::f16, Custom);
565 
566     // F16 - VOP3 Actions.
567     setOperationAction(ISD::FMA, MVT::f16, Legal);
568     if (STI.hasMadF16())
569       setOperationAction(ISD::FMAD, MVT::f16, Legal);
570 
571     for (MVT VT : {MVT::v2i16, MVT::v2f16, MVT::v4i16, MVT::v4f16}) {
572       for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
573         switch (Op) {
574         case ISD::LOAD:
575         case ISD::STORE:
576         case ISD::BUILD_VECTOR:
577         case ISD::BITCAST:
578         case ISD::EXTRACT_VECTOR_ELT:
579         case ISD::INSERT_VECTOR_ELT:
580         case ISD::INSERT_SUBVECTOR:
581         case ISD::EXTRACT_SUBVECTOR:
582         case ISD::SCALAR_TO_VECTOR:
583           break;
584         case ISD::CONCAT_VECTORS:
585           setOperationAction(Op, VT, Custom);
586           break;
587         default:
588           setOperationAction(Op, VT, Expand);
589           break;
590         }
591       }
592     }
593 
594     // v_perm_b32 can handle either of these.
595     setOperationAction(ISD::BSWAP, MVT::i16, Legal);
596     setOperationAction(ISD::BSWAP, MVT::v2i16, Legal);
597     setOperationAction(ISD::BSWAP, MVT::v4i16, Custom);
598 
599     // XXX - Do these do anything? Vector constants turn into build_vector.
600     setOperationAction(ISD::Constant, MVT::v2i16, Legal);
601     setOperationAction(ISD::ConstantFP, MVT::v2f16, Legal);
602 
603     setOperationAction(ISD::UNDEF, MVT::v2i16, Legal);
604     setOperationAction(ISD::UNDEF, MVT::v2f16, Legal);
605 
606     setOperationAction(ISD::STORE, MVT::v2i16, Promote);
607     AddPromotedToType(ISD::STORE, MVT::v2i16, MVT::i32);
608     setOperationAction(ISD::STORE, MVT::v2f16, Promote);
609     AddPromotedToType(ISD::STORE, MVT::v2f16, MVT::i32);
610 
611     setOperationAction(ISD::LOAD, MVT::v2i16, Promote);
612     AddPromotedToType(ISD::LOAD, MVT::v2i16, MVT::i32);
613     setOperationAction(ISD::LOAD, MVT::v2f16, Promote);
614     AddPromotedToType(ISD::LOAD, MVT::v2f16, MVT::i32);
615 
616     setOperationAction(ISD::AND, MVT::v2i16, Promote);
617     AddPromotedToType(ISD::AND, MVT::v2i16, MVT::i32);
618     setOperationAction(ISD::OR, MVT::v2i16, Promote);
619     AddPromotedToType(ISD::OR, MVT::v2i16, MVT::i32);
620     setOperationAction(ISD::XOR, MVT::v2i16, Promote);
621     AddPromotedToType(ISD::XOR, MVT::v2i16, MVT::i32);
622 
623     setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
624     AddPromotedToType(ISD::LOAD, MVT::v4i16, MVT::v2i32);
625     setOperationAction(ISD::LOAD, MVT::v4f16, Promote);
626     AddPromotedToType(ISD::LOAD, MVT::v4f16, MVT::v2i32);
627 
628     setOperationAction(ISD::STORE, MVT::v4i16, Promote);
629     AddPromotedToType(ISD::STORE, MVT::v4i16, MVT::v2i32);
630     setOperationAction(ISD::STORE, MVT::v4f16, Promote);
631     AddPromotedToType(ISD::STORE, MVT::v4f16, MVT::v2i32);
632 
633     setOperationAction(ISD::ANY_EXTEND, MVT::v2i32, Expand);
634     setOperationAction(ISD::ZERO_EXTEND, MVT::v2i32, Expand);
635     setOperationAction(ISD::SIGN_EXTEND, MVT::v2i32, Expand);
636     setOperationAction(ISD::FP_EXTEND, MVT::v2f32, Expand);
637 
638     setOperationAction(ISD::ANY_EXTEND, MVT::v4i32, Expand);
639     setOperationAction(ISD::ZERO_EXTEND, MVT::v4i32, Expand);
640     setOperationAction(ISD::SIGN_EXTEND, MVT::v4i32, Expand);
641 
642     if (!Subtarget->hasVOP3PInsts()) {
643       setOperationAction(ISD::BUILD_VECTOR, MVT::v2i16, Custom);
644       setOperationAction(ISD::BUILD_VECTOR, MVT::v2f16, Custom);
645     }
646 
647     setOperationAction(ISD::FNEG, MVT::v2f16, Legal);
648     // This isn't really legal, but this avoids the legalizer unrolling it (and
649     // allows matching fneg (fabs x) patterns)
650     setOperationAction(ISD::FABS, MVT::v2f16, Legal);
651 
652     setOperationAction(ISD::FMAXNUM, MVT::f16, Custom);
653     setOperationAction(ISD::FMINNUM, MVT::f16, Custom);
654     setOperationAction(ISD::FMAXNUM_IEEE, MVT::f16, Legal);
655     setOperationAction(ISD::FMINNUM_IEEE, MVT::f16, Legal);
656 
657     setOperationAction(ISD::FMINNUM_IEEE, MVT::v4f16, Custom);
658     setOperationAction(ISD::FMAXNUM_IEEE, MVT::v4f16, Custom);
659 
660     setOperationAction(ISD::FMINNUM, MVT::v4f16, Expand);
661     setOperationAction(ISD::FMAXNUM, MVT::v4f16, Expand);
662   }
663 
664   if (Subtarget->hasVOP3PInsts()) {
665     setOperationAction(ISD::ADD, MVT::v2i16, Legal);
666     setOperationAction(ISD::SUB, MVT::v2i16, Legal);
667     setOperationAction(ISD::MUL, MVT::v2i16, Legal);
668     setOperationAction(ISD::SHL, MVT::v2i16, Legal);
669     setOperationAction(ISD::SRL, MVT::v2i16, Legal);
670     setOperationAction(ISD::SRA, MVT::v2i16, Legal);
671     setOperationAction(ISD::SMIN, MVT::v2i16, Legal);
672     setOperationAction(ISD::UMIN, MVT::v2i16, Legal);
673     setOperationAction(ISD::SMAX, MVT::v2i16, Legal);
674     setOperationAction(ISD::UMAX, MVT::v2i16, Legal);
675 
676     setOperationAction(ISD::UADDSAT, MVT::v2i16, Legal);
677     setOperationAction(ISD::USUBSAT, MVT::v2i16, Legal);
678     setOperationAction(ISD::SADDSAT, MVT::v2i16, Legal);
679     setOperationAction(ISD::SSUBSAT, MVT::v2i16, Legal);
680 
681     setOperationAction(ISD::FADD, MVT::v2f16, Legal);
682     setOperationAction(ISD::FMUL, MVT::v2f16, Legal);
683     setOperationAction(ISD::FMA, MVT::v2f16, Legal);
684 
685     setOperationAction(ISD::FMINNUM_IEEE, MVT::v2f16, Legal);
686     setOperationAction(ISD::FMAXNUM_IEEE, MVT::v2f16, Legal);
687 
688     setOperationAction(ISD::FCANONICALIZE, MVT::v2f16, Legal);
689 
690     setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i16, Custom);
691     setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f16, Custom);
692 
693     setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f16, Custom);
694     setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
695 
696     setOperationAction(ISD::SHL, MVT::v4i16, Custom);
697     setOperationAction(ISD::SRA, MVT::v4i16, Custom);
698     setOperationAction(ISD::SRL, MVT::v4i16, Custom);
699     setOperationAction(ISD::ADD, MVT::v4i16, Custom);
700     setOperationAction(ISD::SUB, MVT::v4i16, Custom);
701     setOperationAction(ISD::MUL, MVT::v4i16, Custom);
702 
703     setOperationAction(ISD::SMIN, MVT::v4i16, Custom);
704     setOperationAction(ISD::SMAX, MVT::v4i16, Custom);
705     setOperationAction(ISD::UMIN, MVT::v4i16, Custom);
706     setOperationAction(ISD::UMAX, MVT::v4i16, Custom);
707 
708     setOperationAction(ISD::UADDSAT, MVT::v4i16, Custom);
709     setOperationAction(ISD::SADDSAT, MVT::v4i16, Custom);
710     setOperationAction(ISD::USUBSAT, MVT::v4i16, Custom);
711     setOperationAction(ISD::SSUBSAT, MVT::v4i16, Custom);
712 
713     setOperationAction(ISD::FADD, MVT::v4f16, Custom);
714     setOperationAction(ISD::FMUL, MVT::v4f16, Custom);
715     setOperationAction(ISD::FMA, MVT::v4f16, Custom);
716 
717     setOperationAction(ISD::FMAXNUM, MVT::v2f16, Custom);
718     setOperationAction(ISD::FMINNUM, MVT::v2f16, Custom);
719 
720     setOperationAction(ISD::FMINNUM, MVT::v4f16, Custom);
721     setOperationAction(ISD::FMAXNUM, MVT::v4f16, Custom);
722     setOperationAction(ISD::FCANONICALIZE, MVT::v4f16, Custom);
723 
724     setOperationAction(ISD::FEXP, MVT::v2f16, Custom);
725     setOperationAction(ISD::SELECT, MVT::v4i16, Custom);
726     setOperationAction(ISD::SELECT, MVT::v4f16, Custom);
727 
728     if (Subtarget->hasPackedFP32Ops()) {
729       setOperationAction(ISD::FADD, MVT::v2f32, Legal);
730       setOperationAction(ISD::FMUL, MVT::v2f32, Legal);
731       setOperationAction(ISD::FMA,  MVT::v2f32, Legal);
732       setOperationAction(ISD::FNEG, MVT::v2f32, Legal);
733 
734       for (MVT VT : { MVT::v4f32, MVT::v8f32, MVT::v16f32, MVT::v32f32 }) {
735         setOperationAction(ISD::FADD, VT, Custom);
736         setOperationAction(ISD::FMUL, VT, Custom);
737         setOperationAction(ISD::FMA, VT, Custom);
738       }
739     }
740   }
741 
742   setOperationAction(ISD::FNEG, MVT::v4f16, Custom);
743   setOperationAction(ISD::FABS, MVT::v4f16, Custom);
744 
745   if (Subtarget->has16BitInsts()) {
746     setOperationAction(ISD::SELECT, MVT::v2i16, Promote);
747     AddPromotedToType(ISD::SELECT, MVT::v2i16, MVT::i32);
748     setOperationAction(ISD::SELECT, MVT::v2f16, Promote);
749     AddPromotedToType(ISD::SELECT, MVT::v2f16, MVT::i32);
750   } else {
751     // Legalization hack.
752     setOperationAction(ISD::SELECT, MVT::v2i16, Custom);
753     setOperationAction(ISD::SELECT, MVT::v2f16, Custom);
754 
755     setOperationAction(ISD::FNEG, MVT::v2f16, Custom);
756     setOperationAction(ISD::FABS, MVT::v2f16, Custom);
757   }
758 
759   for (MVT VT : { MVT::v4i16, MVT::v4f16, MVT::v2i8, MVT::v4i8, MVT::v8i8 }) {
760     setOperationAction(ISD::SELECT, VT, Custom);
761   }
762 
763   setOperationAction(ISD::SMULO, MVT::i64, Custom);
764   setOperationAction(ISD::UMULO, MVT::i64, Custom);
765 
766   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
767   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom);
768   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom);
769   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i16, Custom);
770   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f16, Custom);
771   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v2i16, Custom);
772   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v2f16, Custom);
773 
774   setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::v2f16, Custom);
775   setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::v2i16, Custom);
776   setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::v3f16, Custom);
777   setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::v3i16, Custom);
778   setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::v4f16, Custom);
779   setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::v4i16, Custom);
780   setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::v8f16, Custom);
781   setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
782   setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::f16, Custom);
783   setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i16, Custom);
784   setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i8, Custom);
785 
786   setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
787   setOperationAction(ISD::INTRINSIC_VOID, MVT::v2i16, Custom);
788   setOperationAction(ISD::INTRINSIC_VOID, MVT::v2f16, Custom);
789   setOperationAction(ISD::INTRINSIC_VOID, MVT::v3i16, Custom);
790   setOperationAction(ISD::INTRINSIC_VOID, MVT::v3f16, Custom);
791   setOperationAction(ISD::INTRINSIC_VOID, MVT::v4f16, Custom);
792   setOperationAction(ISD::INTRINSIC_VOID, MVT::v4i16, Custom);
793   setOperationAction(ISD::INTRINSIC_VOID, MVT::f16, Custom);
794   setOperationAction(ISD::INTRINSIC_VOID, MVT::i16, Custom);
795   setOperationAction(ISD::INTRINSIC_VOID, MVT::i8, Custom);
796 
797   setTargetDAGCombine(ISD::ADD);
798   setTargetDAGCombine(ISD::ADDCARRY);
799   setTargetDAGCombine(ISD::SUB);
800   setTargetDAGCombine(ISD::SUBCARRY);
801   setTargetDAGCombine(ISD::FADD);
802   setTargetDAGCombine(ISD::FSUB);
803   setTargetDAGCombine(ISD::FMINNUM);
804   setTargetDAGCombine(ISD::FMAXNUM);
805   setTargetDAGCombine(ISD::FMINNUM_IEEE);
806   setTargetDAGCombine(ISD::FMAXNUM_IEEE);
807   setTargetDAGCombine(ISD::FMA);
808   setTargetDAGCombine(ISD::SMIN);
809   setTargetDAGCombine(ISD::SMAX);
810   setTargetDAGCombine(ISD::UMIN);
811   setTargetDAGCombine(ISD::UMAX);
812   setTargetDAGCombine(ISD::SETCC);
813   setTargetDAGCombine(ISD::AND);
814   setTargetDAGCombine(ISD::OR);
815   setTargetDAGCombine(ISD::XOR);
816   setTargetDAGCombine(ISD::SINT_TO_FP);
817   setTargetDAGCombine(ISD::UINT_TO_FP);
818   setTargetDAGCombine(ISD::FCANONICALIZE);
819   setTargetDAGCombine(ISD::SCALAR_TO_VECTOR);
820   setTargetDAGCombine(ISD::ZERO_EXTEND);
821   setTargetDAGCombine(ISD::SIGN_EXTEND_INREG);
822   setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
823   setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
824 
825   // All memory operations. Some folding on the pointer operand is done to help
826   // matching the constant offsets in the addressing modes.
827   setTargetDAGCombine(ISD::LOAD);
828   setTargetDAGCombine(ISD::STORE);
829   setTargetDAGCombine(ISD::ATOMIC_LOAD);
830   setTargetDAGCombine(ISD::ATOMIC_STORE);
831   setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP);
832   setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
833   setTargetDAGCombine(ISD::ATOMIC_SWAP);
834   setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD);
835   setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB);
836   setTargetDAGCombine(ISD::ATOMIC_LOAD_AND);
837   setTargetDAGCombine(ISD::ATOMIC_LOAD_OR);
838   setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR);
839   setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND);
840   setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN);
841   setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX);
842   setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN);
843   setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX);
844   setTargetDAGCombine(ISD::ATOMIC_LOAD_FADD);
845   setTargetDAGCombine(ISD::INTRINSIC_VOID);
846   setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
847 
848   // FIXME: In other contexts we pretend this is a per-function property.
849   setStackPointerRegisterToSaveRestore(AMDGPU::SGPR32);
850 
851   setSchedulingPreference(Sched::RegPressure);
852 }
853 
854 const GCNSubtarget *SITargetLowering::getSubtarget() const {
855   return Subtarget;
856 }
857 
858 //===----------------------------------------------------------------------===//
859 // TargetLowering queries
860 //===----------------------------------------------------------------------===//
861 
862 // v_mad_mix* support a conversion from f16 to f32.
863 //
864 // There is only one special case when denormals are enabled we don't currently,
865 // where this is OK to use.
866 bool SITargetLowering::isFPExtFoldable(const SelectionDAG &DAG, unsigned Opcode,
867                                        EVT DestVT, EVT SrcVT) const {
868   return ((Opcode == ISD::FMAD && Subtarget->hasMadMixInsts()) ||
869           (Opcode == ISD::FMA && Subtarget->hasFmaMixInsts())) &&
870     DestVT.getScalarType() == MVT::f32 &&
871     SrcVT.getScalarType() == MVT::f16 &&
872     // TODO: This probably only requires no input flushing?
873     !hasFP32Denormals(DAG.getMachineFunction());
874 }
875 
876 bool SITargetLowering::isShuffleMaskLegal(ArrayRef<int>, EVT) const {
877   // SI has some legal vector types, but no legal vector operations. Say no
878   // shuffles are legal in order to prefer scalarizing some vector operations.
879   return false;
880 }
881 
882 MVT SITargetLowering::getRegisterTypeForCallingConv(LLVMContext &Context,
883                                                     CallingConv::ID CC,
884                                                     EVT VT) const {
885   if (CC == CallingConv::AMDGPU_KERNEL)
886     return TargetLowering::getRegisterTypeForCallingConv(Context, CC, VT);
887 
888   if (VT.isVector()) {
889     EVT ScalarVT = VT.getScalarType();
890     unsigned Size = ScalarVT.getSizeInBits();
891     if (Size == 16) {
892       if (Subtarget->has16BitInsts())
893         return VT.isInteger() ? MVT::v2i16 : MVT::v2f16;
894       return VT.isInteger() ? MVT::i32 : MVT::f32;
895     }
896 
897     if (Size < 16)
898       return Subtarget->has16BitInsts() ? MVT::i16 : MVT::i32;
899     return Size == 32 ? ScalarVT.getSimpleVT() : MVT::i32;
900   }
901 
902   if (VT.getSizeInBits() > 32)
903     return MVT::i32;
904 
905   return TargetLowering::getRegisterTypeForCallingConv(Context, CC, VT);
906 }
907 
908 unsigned SITargetLowering::getNumRegistersForCallingConv(LLVMContext &Context,
909                                                          CallingConv::ID CC,
910                                                          EVT VT) const {
911   if (CC == CallingConv::AMDGPU_KERNEL)
912     return TargetLowering::getNumRegistersForCallingConv(Context, CC, VT);
913 
914   if (VT.isVector()) {
915     unsigned NumElts = VT.getVectorNumElements();
916     EVT ScalarVT = VT.getScalarType();
917     unsigned Size = ScalarVT.getSizeInBits();
918 
919     // FIXME: Should probably promote 8-bit vectors to i16.
920     if (Size == 16 && Subtarget->has16BitInsts())
921       return (NumElts + 1) / 2;
922 
923     if (Size <= 32)
924       return NumElts;
925 
926     if (Size > 32)
927       return NumElts * ((Size + 31) / 32);
928   } else if (VT.getSizeInBits() > 32)
929     return (VT.getSizeInBits() + 31) / 32;
930 
931   return TargetLowering::getNumRegistersForCallingConv(Context, CC, VT);
932 }
933 
934 unsigned SITargetLowering::getVectorTypeBreakdownForCallingConv(
935   LLVMContext &Context, CallingConv::ID CC,
936   EVT VT, EVT &IntermediateVT,
937   unsigned &NumIntermediates, MVT &RegisterVT) const {
938   if (CC != CallingConv::AMDGPU_KERNEL && VT.isVector()) {
939     unsigned NumElts = VT.getVectorNumElements();
940     EVT ScalarVT = VT.getScalarType();
941     unsigned Size = ScalarVT.getSizeInBits();
942     // FIXME: We should fix the ABI to be the same on targets without 16-bit
943     // support, but unless we can properly handle 3-vectors, it will be still be
944     // inconsistent.
945     if (Size == 16 && Subtarget->has16BitInsts()) {
946       RegisterVT = VT.isInteger() ? MVT::v2i16 : MVT::v2f16;
947       IntermediateVT = RegisterVT;
948       NumIntermediates = (NumElts + 1) / 2;
949       return NumIntermediates;
950     }
951 
952     if (Size == 32) {
953       RegisterVT = ScalarVT.getSimpleVT();
954       IntermediateVT = RegisterVT;
955       NumIntermediates = NumElts;
956       return NumIntermediates;
957     }
958 
959     if (Size < 16 && Subtarget->has16BitInsts()) {
960       // FIXME: Should probably form v2i16 pieces
961       RegisterVT = MVT::i16;
962       IntermediateVT = ScalarVT;
963       NumIntermediates = NumElts;
964       return NumIntermediates;
965     }
966 
967 
968     if (Size != 16 && Size <= 32) {
969       RegisterVT = MVT::i32;
970       IntermediateVT = ScalarVT;
971       NumIntermediates = NumElts;
972       return NumIntermediates;
973     }
974 
975     if (Size > 32) {
976       RegisterVT = MVT::i32;
977       IntermediateVT = RegisterVT;
978       NumIntermediates = NumElts * ((Size + 31) / 32);
979       return NumIntermediates;
980     }
981   }
982 
983   return TargetLowering::getVectorTypeBreakdownForCallingConv(
984     Context, CC, VT, IntermediateVT, NumIntermediates, RegisterVT);
985 }
986 
987 static EVT memVTFromImageData(Type *Ty, unsigned DMaskLanes) {
988   assert(DMaskLanes != 0);
989 
990   if (auto *VT = dyn_cast<FixedVectorType>(Ty)) {
991     unsigned NumElts = std::min(DMaskLanes, VT->getNumElements());
992     return EVT::getVectorVT(Ty->getContext(),
993                             EVT::getEVT(VT->getElementType()),
994                             NumElts);
995   }
996 
997   return EVT::getEVT(Ty);
998 }
999 
1000 // Peek through TFE struct returns to only use the data size.
1001 static EVT memVTFromImageReturn(Type *Ty, unsigned DMaskLanes) {
1002   auto *ST = dyn_cast<StructType>(Ty);
1003   if (!ST)
1004     return memVTFromImageData(Ty, DMaskLanes);
1005 
1006   // Some intrinsics return an aggregate type - special case to work out the
1007   // correct memVT.
1008   //
1009   // Only limited forms of aggregate type currently expected.
1010   if (ST->getNumContainedTypes() != 2 ||
1011       !ST->getContainedType(1)->isIntegerTy(32))
1012     return EVT();
1013   return memVTFromImageData(ST->getContainedType(0), DMaskLanes);
1014 }
1015 
1016 bool SITargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
1017                                           const CallInst &CI,
1018                                           MachineFunction &MF,
1019                                           unsigned IntrID) const {
1020   if (const AMDGPU::RsrcIntrinsic *RsrcIntr =
1021           AMDGPU::lookupRsrcIntrinsic(IntrID)) {
1022     AttributeList Attr = Intrinsic::getAttributes(CI.getContext(),
1023                                                   (Intrinsic::ID)IntrID);
1024     if (Attr.hasFnAttribute(Attribute::ReadNone))
1025       return false;
1026 
1027     SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
1028 
1029     if (RsrcIntr->IsImage) {
1030       Info.ptrVal =
1031           MFI->getImagePSV(*MF.getSubtarget<GCNSubtarget>().getInstrInfo());
1032       Info.align.reset();
1033     } else {
1034       Info.ptrVal =
1035           MFI->getBufferPSV(*MF.getSubtarget<GCNSubtarget>().getInstrInfo());
1036     }
1037 
1038     Info.flags = MachineMemOperand::MODereferenceable;
1039     if (Attr.hasFnAttribute(Attribute::ReadOnly)) {
1040       unsigned DMaskLanes = 4;
1041 
1042       if (RsrcIntr->IsImage) {
1043         const AMDGPU::ImageDimIntrinsicInfo *Intr
1044           = AMDGPU::getImageDimIntrinsicInfo(IntrID);
1045         const AMDGPU::MIMGBaseOpcodeInfo *BaseOpcode =
1046           AMDGPU::getMIMGBaseOpcodeInfo(Intr->BaseOpcode);
1047 
1048         if (!BaseOpcode->Gather4) {
1049           // If this isn't a gather, we may have excess loaded elements in the
1050           // IR type. Check the dmask for the real number of elements loaded.
1051           unsigned DMask
1052             = cast<ConstantInt>(CI.getArgOperand(0))->getZExtValue();
1053           DMaskLanes = DMask == 0 ? 1 : countPopulation(DMask);
1054         }
1055 
1056         Info.memVT = memVTFromImageReturn(CI.getType(), DMaskLanes);
1057       } else
1058         Info.memVT = EVT::getEVT(CI.getType());
1059 
1060       // FIXME: What does alignment mean for an image?
1061       Info.opc = ISD::INTRINSIC_W_CHAIN;
1062       Info.flags |= MachineMemOperand::MOLoad;
1063     } else if (Attr.hasFnAttribute(Attribute::WriteOnly)) {
1064       Info.opc = ISD::INTRINSIC_VOID;
1065 
1066       Type *DataTy = CI.getArgOperand(0)->getType();
1067       if (RsrcIntr->IsImage) {
1068         unsigned DMask = cast<ConstantInt>(CI.getArgOperand(1))->getZExtValue();
1069         unsigned DMaskLanes = DMask == 0 ? 1 : countPopulation(DMask);
1070         Info.memVT = memVTFromImageData(DataTy, DMaskLanes);
1071       } else
1072         Info.memVT = EVT::getEVT(DataTy);
1073 
1074       Info.flags |= MachineMemOperand::MOStore;
1075     } else {
1076       // Atomic
1077       Info.opc = CI.getType()->isVoidTy() ? ISD::INTRINSIC_VOID :
1078                                             ISD::INTRINSIC_W_CHAIN;
1079       Info.memVT = MVT::getVT(CI.getArgOperand(0)->getType());
1080       Info.flags = MachineMemOperand::MOLoad |
1081                    MachineMemOperand::MOStore |
1082                    MachineMemOperand::MODereferenceable;
1083 
1084       // XXX - Should this be volatile without known ordering?
1085       Info.flags |= MachineMemOperand::MOVolatile;
1086     }
1087     return true;
1088   }
1089 
1090   switch (IntrID) {
1091   case Intrinsic::amdgcn_atomic_inc:
1092   case Intrinsic::amdgcn_atomic_dec:
1093   case Intrinsic::amdgcn_ds_ordered_add:
1094   case Intrinsic::amdgcn_ds_ordered_swap:
1095   case Intrinsic::amdgcn_ds_fadd:
1096   case Intrinsic::amdgcn_ds_fmin:
1097   case Intrinsic::amdgcn_ds_fmax: {
1098     Info.opc = ISD::INTRINSIC_W_CHAIN;
1099     Info.memVT = MVT::getVT(CI.getType());
1100     Info.ptrVal = CI.getOperand(0);
1101     Info.align.reset();
1102     Info.flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
1103 
1104     const ConstantInt *Vol = cast<ConstantInt>(CI.getOperand(4));
1105     if (!Vol->isZero())
1106       Info.flags |= MachineMemOperand::MOVolatile;
1107 
1108     return true;
1109   }
1110   case Intrinsic::amdgcn_buffer_atomic_fadd: {
1111     SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
1112 
1113     Info.opc = ISD::INTRINSIC_W_CHAIN;
1114     Info.memVT = MVT::getVT(CI.getOperand(0)->getType());
1115     Info.ptrVal =
1116         MFI->getBufferPSV(*MF.getSubtarget<GCNSubtarget>().getInstrInfo());
1117     Info.align.reset();
1118     Info.flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
1119 
1120     const ConstantInt *Vol = dyn_cast<ConstantInt>(CI.getOperand(4));
1121     if (!Vol || !Vol->isZero())
1122       Info.flags |= MachineMemOperand::MOVolatile;
1123 
1124     return true;
1125   }
1126   case Intrinsic::amdgcn_ds_append:
1127   case Intrinsic::amdgcn_ds_consume: {
1128     Info.opc = ISD::INTRINSIC_W_CHAIN;
1129     Info.memVT = MVT::getVT(CI.getType());
1130     Info.ptrVal = CI.getOperand(0);
1131     Info.align.reset();
1132     Info.flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
1133 
1134     const ConstantInt *Vol = cast<ConstantInt>(CI.getOperand(1));
1135     if (!Vol->isZero())
1136       Info.flags |= MachineMemOperand::MOVolatile;
1137 
1138     return true;
1139   }
1140   case Intrinsic::amdgcn_global_atomic_csub: {
1141     Info.opc = ISD::INTRINSIC_W_CHAIN;
1142     Info.memVT = MVT::getVT(CI.getType());
1143     Info.ptrVal = CI.getOperand(0);
1144     Info.align.reset();
1145     Info.flags = MachineMemOperand::MOLoad |
1146                  MachineMemOperand::MOStore |
1147                  MachineMemOperand::MOVolatile;
1148     return true;
1149   }
1150   case Intrinsic::amdgcn_image_bvh_intersect_ray: {
1151     SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
1152     Info.opc = ISD::INTRINSIC_W_CHAIN;
1153     Info.memVT = MVT::getVT(CI.getType()); // XXX: what is correct VT?
1154     Info.ptrVal =
1155         MFI->getImagePSV(*MF.getSubtarget<GCNSubtarget>().getInstrInfo());
1156     Info.align.reset();
1157     Info.flags = MachineMemOperand::MOLoad |
1158                  MachineMemOperand::MODereferenceable;
1159     return true;
1160   }
1161   case Intrinsic::amdgcn_global_atomic_fadd:
1162   case Intrinsic::amdgcn_global_atomic_fmin:
1163   case Intrinsic::amdgcn_global_atomic_fmax:
1164   case Intrinsic::amdgcn_flat_atomic_fadd:
1165   case Intrinsic::amdgcn_flat_atomic_fmin:
1166   case Intrinsic::amdgcn_flat_atomic_fmax: {
1167     Info.opc = ISD::INTRINSIC_W_CHAIN;
1168     Info.memVT = MVT::getVT(CI.getType());
1169     Info.ptrVal = CI.getOperand(0);
1170     Info.align.reset();
1171     Info.flags = MachineMemOperand::MOLoad |
1172                  MachineMemOperand::MOStore |
1173                  MachineMemOperand::MODereferenceable |
1174                  MachineMemOperand::MOVolatile;
1175     return true;
1176   }
1177   case Intrinsic::amdgcn_ds_gws_init:
1178   case Intrinsic::amdgcn_ds_gws_barrier:
1179   case Intrinsic::amdgcn_ds_gws_sema_v:
1180   case Intrinsic::amdgcn_ds_gws_sema_br:
1181   case Intrinsic::amdgcn_ds_gws_sema_p:
1182   case Intrinsic::amdgcn_ds_gws_sema_release_all: {
1183     Info.opc = ISD::INTRINSIC_VOID;
1184 
1185     SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
1186     Info.ptrVal =
1187         MFI->getGWSPSV(*MF.getSubtarget<GCNSubtarget>().getInstrInfo());
1188 
1189     // This is an abstract access, but we need to specify a type and size.
1190     Info.memVT = MVT::i32;
1191     Info.size = 4;
1192     Info.align = Align(4);
1193 
1194     Info.flags = MachineMemOperand::MOStore;
1195     if (IntrID == Intrinsic::amdgcn_ds_gws_barrier)
1196       Info.flags = MachineMemOperand::MOLoad;
1197     return true;
1198   }
1199   default:
1200     return false;
1201   }
1202 }
1203 
1204 bool SITargetLowering::getAddrModeArguments(IntrinsicInst *II,
1205                                             SmallVectorImpl<Value*> &Ops,
1206                                             Type *&AccessTy) const {
1207   switch (II->getIntrinsicID()) {
1208   case Intrinsic::amdgcn_atomic_inc:
1209   case Intrinsic::amdgcn_atomic_dec:
1210   case Intrinsic::amdgcn_ds_ordered_add:
1211   case Intrinsic::amdgcn_ds_ordered_swap:
1212   case Intrinsic::amdgcn_ds_append:
1213   case Intrinsic::amdgcn_ds_consume:
1214   case Intrinsic::amdgcn_ds_fadd:
1215   case Intrinsic::amdgcn_ds_fmin:
1216   case Intrinsic::amdgcn_ds_fmax:
1217   case Intrinsic::amdgcn_global_atomic_fadd:
1218   case Intrinsic::amdgcn_flat_atomic_fadd:
1219   case Intrinsic::amdgcn_flat_atomic_fmin:
1220   case Intrinsic::amdgcn_flat_atomic_fmax:
1221   case Intrinsic::amdgcn_global_atomic_csub: {
1222     Value *Ptr = II->getArgOperand(0);
1223     AccessTy = II->getType();
1224     Ops.push_back(Ptr);
1225     return true;
1226   }
1227   default:
1228     return false;
1229   }
1230 }
1231 
1232 bool SITargetLowering::isLegalFlatAddressingMode(const AddrMode &AM) const {
1233   if (!Subtarget->hasFlatInstOffsets()) {
1234     // Flat instructions do not have offsets, and only have the register
1235     // address.
1236     return AM.BaseOffs == 0 && AM.Scale == 0;
1237   }
1238 
1239   return AM.Scale == 0 &&
1240          (AM.BaseOffs == 0 ||
1241           Subtarget->getInstrInfo()->isLegalFLATOffset(
1242               AM.BaseOffs, AMDGPUAS::FLAT_ADDRESS, SIInstrFlags::FLAT));
1243 }
1244 
1245 bool SITargetLowering::isLegalGlobalAddressingMode(const AddrMode &AM) const {
1246   if (Subtarget->hasFlatGlobalInsts())
1247     return AM.Scale == 0 &&
1248            (AM.BaseOffs == 0 || Subtarget->getInstrInfo()->isLegalFLATOffset(
1249                                     AM.BaseOffs, AMDGPUAS::GLOBAL_ADDRESS,
1250                                     SIInstrFlags::FlatGlobal));
1251 
1252   if (!Subtarget->hasAddr64() || Subtarget->useFlatForGlobal()) {
1253       // Assume the we will use FLAT for all global memory accesses
1254       // on VI.
1255       // FIXME: This assumption is currently wrong.  On VI we still use
1256       // MUBUF instructions for the r + i addressing mode.  As currently
1257       // implemented, the MUBUF instructions only work on buffer < 4GB.
1258       // It may be possible to support > 4GB buffers with MUBUF instructions,
1259       // by setting the stride value in the resource descriptor which would
1260       // increase the size limit to (stride * 4GB).  However, this is risky,
1261       // because it has never been validated.
1262     return isLegalFlatAddressingMode(AM);
1263   }
1264 
1265   return isLegalMUBUFAddressingMode(AM);
1266 }
1267 
1268 bool SITargetLowering::isLegalMUBUFAddressingMode(const AddrMode &AM) const {
1269   // MUBUF / MTBUF instructions have a 12-bit unsigned byte offset, and
1270   // additionally can do r + r + i with addr64. 32-bit has more addressing
1271   // mode options. Depending on the resource constant, it can also do
1272   // (i64 r0) + (i32 r1) * (i14 i).
1273   //
1274   // Private arrays end up using a scratch buffer most of the time, so also
1275   // assume those use MUBUF instructions. Scratch loads / stores are currently
1276   // implemented as mubuf instructions with offen bit set, so slightly
1277   // different than the normal addr64.
1278   if (!SIInstrInfo::isLegalMUBUFImmOffset(AM.BaseOffs))
1279     return false;
1280 
1281   // FIXME: Since we can split immediate into soffset and immediate offset,
1282   // would it make sense to allow any immediate?
1283 
1284   switch (AM.Scale) {
1285   case 0: // r + i or just i, depending on HasBaseReg.
1286     return true;
1287   case 1:
1288     return true; // We have r + r or r + i.
1289   case 2:
1290     if (AM.HasBaseReg) {
1291       // Reject 2 * r + r.
1292       return false;
1293     }
1294 
1295     // Allow 2 * r as r + r
1296     // Or  2 * r + i is allowed as r + r + i.
1297     return true;
1298   default: // Don't allow n * r
1299     return false;
1300   }
1301 }
1302 
1303 bool SITargetLowering::isLegalAddressingMode(const DataLayout &DL,
1304                                              const AddrMode &AM, Type *Ty,
1305                                              unsigned AS, Instruction *I) const {
1306   // No global is ever allowed as a base.
1307   if (AM.BaseGV)
1308     return false;
1309 
1310   if (AS == AMDGPUAS::GLOBAL_ADDRESS)
1311     return isLegalGlobalAddressingMode(AM);
1312 
1313   if (AS == AMDGPUAS::CONSTANT_ADDRESS ||
1314       AS == AMDGPUAS::CONSTANT_ADDRESS_32BIT ||
1315       AS == AMDGPUAS::BUFFER_FAT_POINTER) {
1316     // If the offset isn't a multiple of 4, it probably isn't going to be
1317     // correctly aligned.
1318     // FIXME: Can we get the real alignment here?
1319     if (AM.BaseOffs % 4 != 0)
1320       return isLegalMUBUFAddressingMode(AM);
1321 
1322     // There are no SMRD extloads, so if we have to do a small type access we
1323     // will use a MUBUF load.
1324     // FIXME?: We also need to do this if unaligned, but we don't know the
1325     // alignment here.
1326     if (Ty->isSized() && DL.getTypeStoreSize(Ty) < 4)
1327       return isLegalGlobalAddressingMode(AM);
1328 
1329     if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) {
1330       // SMRD instructions have an 8-bit, dword offset on SI.
1331       if (!isUInt<8>(AM.BaseOffs / 4))
1332         return false;
1333     } else if (Subtarget->getGeneration() == AMDGPUSubtarget::SEA_ISLANDS) {
1334       // On CI+, this can also be a 32-bit literal constant offset. If it fits
1335       // in 8-bits, it can use a smaller encoding.
1336       if (!isUInt<32>(AM.BaseOffs / 4))
1337         return false;
1338     } else if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) {
1339       // On VI, these use the SMEM format and the offset is 20-bit in bytes.
1340       if (!isUInt<20>(AM.BaseOffs))
1341         return false;
1342     } else
1343       llvm_unreachable("unhandled generation");
1344 
1345     if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
1346       return true;
1347 
1348     if (AM.Scale == 1 && AM.HasBaseReg)
1349       return true;
1350 
1351     return false;
1352 
1353   } else if (AS == AMDGPUAS::PRIVATE_ADDRESS) {
1354     return isLegalMUBUFAddressingMode(AM);
1355   } else if (AS == AMDGPUAS::LOCAL_ADDRESS ||
1356              AS == AMDGPUAS::REGION_ADDRESS) {
1357     // Basic, single offset DS instructions allow a 16-bit unsigned immediate
1358     // field.
1359     // XXX - If doing a 4-byte aligned 8-byte type access, we effectively have
1360     // an 8-bit dword offset but we don't know the alignment here.
1361     if (!isUInt<16>(AM.BaseOffs))
1362       return false;
1363 
1364     if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
1365       return true;
1366 
1367     if (AM.Scale == 1 && AM.HasBaseReg)
1368       return true;
1369 
1370     return false;
1371   } else if (AS == AMDGPUAS::FLAT_ADDRESS ||
1372              AS == AMDGPUAS::UNKNOWN_ADDRESS_SPACE) {
1373     // For an unknown address space, this usually means that this is for some
1374     // reason being used for pure arithmetic, and not based on some addressing
1375     // computation. We don't have instructions that compute pointers with any
1376     // addressing modes, so treat them as having no offset like flat
1377     // instructions.
1378     return isLegalFlatAddressingMode(AM);
1379   }
1380 
1381   // Assume a user alias of global for unknown address spaces.
1382   return isLegalGlobalAddressingMode(AM);
1383 }
1384 
1385 bool SITargetLowering::canMergeStoresTo(unsigned AS, EVT MemVT,
1386                                         const SelectionDAG &DAG) const {
1387   if (AS == AMDGPUAS::GLOBAL_ADDRESS || AS == AMDGPUAS::FLAT_ADDRESS) {
1388     return (MemVT.getSizeInBits() <= 4 * 32);
1389   } else if (AS == AMDGPUAS::PRIVATE_ADDRESS) {
1390     unsigned MaxPrivateBits = 8 * getSubtarget()->getMaxPrivateElementSize();
1391     return (MemVT.getSizeInBits() <= MaxPrivateBits);
1392   } else if (AS == AMDGPUAS::LOCAL_ADDRESS || AS == AMDGPUAS::REGION_ADDRESS) {
1393     return (MemVT.getSizeInBits() <= 2 * 32);
1394   }
1395   return true;
1396 }
1397 
1398 bool SITargetLowering::allowsMisalignedMemoryAccessesImpl(
1399     unsigned Size, unsigned AddrSpace, Align Alignment,
1400     MachineMemOperand::Flags Flags, bool *IsFast) const {
1401   if (IsFast)
1402     *IsFast = false;
1403 
1404   if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS ||
1405       AddrSpace == AMDGPUAS::REGION_ADDRESS) {
1406     // Check if alignment requirements for ds_read/write instructions are
1407     // disabled.
1408     if (Subtarget->hasUnalignedDSAccessEnabled() &&
1409         !Subtarget->hasLDSMisalignedBug()) {
1410       if (IsFast)
1411         *IsFast = Alignment != Align(2);
1412       return true;
1413     }
1414 
1415     // Either, the alignment requirements are "enabled", or there is an
1416     // unaligned LDS access related hardware bug though alignment requirements
1417     // are "disabled". In either case, we need to check for proper alignment
1418     // requirements.
1419     //
1420     if (Size == 64) {
1421       // 8 byte accessing via ds_read/write_b64 require 8-byte alignment, but we
1422       // can do a 4 byte aligned, 8 byte access in a single operation using
1423       // ds_read2/write2_b32 with adjacent offsets.
1424       bool AlignedBy4 = Alignment >= Align(4);
1425       if (IsFast)
1426         *IsFast = AlignedBy4;
1427 
1428       return AlignedBy4;
1429     }
1430     if (Size == 96) {
1431       // 12 byte accessing via ds_read/write_b96 require 16-byte alignment on
1432       // gfx8 and older.
1433       bool AlignedBy16 = Alignment >= Align(16);
1434       if (IsFast)
1435         *IsFast = AlignedBy16;
1436 
1437       return AlignedBy16;
1438     }
1439     if (Size == 128) {
1440       // 16 byte accessing via ds_read/write_b128 require 16-byte alignment on
1441       // gfx8 and older, but  we can do a 8 byte aligned, 16 byte access in a
1442       // single operation using ds_read2/write2_b64.
1443       bool AlignedBy8 = Alignment >= Align(8);
1444       if (IsFast)
1445         *IsFast = AlignedBy8;
1446 
1447       return AlignedBy8;
1448     }
1449   }
1450 
1451   if (AddrSpace == AMDGPUAS::PRIVATE_ADDRESS) {
1452     bool AlignedBy4 = Alignment >= Align(4);
1453     if (IsFast)
1454       *IsFast = AlignedBy4;
1455 
1456     return AlignedBy4 ||
1457            Subtarget->enableFlatScratch() ||
1458            Subtarget->hasUnalignedScratchAccess();
1459   }
1460 
1461   // FIXME: We have to be conservative here and assume that flat operations
1462   // will access scratch.  If we had access to the IR function, then we
1463   // could determine if any private memory was used in the function.
1464   if (AddrSpace == AMDGPUAS::FLAT_ADDRESS &&
1465       !Subtarget->hasUnalignedScratchAccess()) {
1466     bool AlignedBy4 = Alignment >= Align(4);
1467     if (IsFast)
1468       *IsFast = AlignedBy4;
1469 
1470     return AlignedBy4;
1471   }
1472 
1473   if (Subtarget->hasUnalignedBufferAccessEnabled() &&
1474       !(AddrSpace == AMDGPUAS::LOCAL_ADDRESS ||
1475         AddrSpace == AMDGPUAS::REGION_ADDRESS)) {
1476     // If we have an uniform constant load, it still requires using a slow
1477     // buffer instruction if unaligned.
1478     if (IsFast) {
1479       // Accesses can really be issued as 1-byte aligned or 4-byte aligned, so
1480       // 2-byte alignment is worse than 1 unless doing a 2-byte accesss.
1481       *IsFast = (AddrSpace == AMDGPUAS::CONSTANT_ADDRESS ||
1482                  AddrSpace == AMDGPUAS::CONSTANT_ADDRESS_32BIT) ?
1483         Alignment >= Align(4) : Alignment != Align(2);
1484     }
1485 
1486     return true;
1487   }
1488 
1489   // Smaller than dword value must be aligned.
1490   if (Size < 32)
1491     return false;
1492 
1493   // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
1494   // byte-address are ignored, thus forcing Dword alignment.
1495   // This applies to private, global, and constant memory.
1496   if (IsFast)
1497     *IsFast = true;
1498 
1499   return Size >= 32 && Alignment >= Align(4);
1500 }
1501 
1502 bool SITargetLowering::allowsMisalignedMemoryAccesses(
1503     EVT VT, unsigned AddrSpace, Align Alignment, MachineMemOperand::Flags Flags,
1504     bool *IsFast) const {
1505   if (IsFast)
1506     *IsFast = false;
1507 
1508   // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
1509   // which isn't a simple VT.
1510   // Until MVT is extended to handle this, simply check for the size and
1511   // rely on the condition below: allow accesses if the size is a multiple of 4.
1512   if (VT == MVT::Other || (VT != MVT::Other && VT.getSizeInBits() > 1024 &&
1513                            VT.getStoreSize() > 16)) {
1514     return false;
1515   }
1516 
1517   return allowsMisalignedMemoryAccessesImpl(VT.getSizeInBits(), AddrSpace,
1518                                             Alignment, Flags, IsFast);
1519 }
1520 
1521 EVT SITargetLowering::getOptimalMemOpType(
1522     const MemOp &Op, const AttributeList &FuncAttributes) const {
1523   // FIXME: Should account for address space here.
1524 
1525   // The default fallback uses the private pointer size as a guess for a type to
1526   // use. Make sure we switch these to 64-bit accesses.
1527 
1528   if (Op.size() >= 16 &&
1529       Op.isDstAligned(Align(4))) // XXX: Should only do for global
1530     return MVT::v4i32;
1531 
1532   if (Op.size() >= 8 && Op.isDstAligned(Align(4)))
1533     return MVT::v2i32;
1534 
1535   // Use the default.
1536   return MVT::Other;
1537 }
1538 
1539 bool SITargetLowering::isMemOpHasNoClobberedMemOperand(const SDNode *N) const {
1540   const MemSDNode *MemNode = cast<MemSDNode>(N);
1541   const Value *Ptr = MemNode->getMemOperand()->getValue();
1542   const Instruction *I = dyn_cast_or_null<Instruction>(Ptr);
1543   return I && I->getMetadata("amdgpu.noclobber");
1544 }
1545 
1546 bool SITargetLowering::isNonGlobalAddrSpace(unsigned AS) {
1547   return AS == AMDGPUAS::LOCAL_ADDRESS || AS == AMDGPUAS::REGION_ADDRESS ||
1548          AS == AMDGPUAS::PRIVATE_ADDRESS;
1549 }
1550 
1551 bool SITargetLowering::isFreeAddrSpaceCast(unsigned SrcAS,
1552                                            unsigned DestAS) const {
1553   // Flat -> private/local is a simple truncate.
1554   // Flat -> global is no-op
1555   if (SrcAS == AMDGPUAS::FLAT_ADDRESS)
1556     return true;
1557 
1558   const GCNTargetMachine &TM =
1559       static_cast<const GCNTargetMachine &>(getTargetMachine());
1560   return TM.isNoopAddrSpaceCast(SrcAS, DestAS);
1561 }
1562 
1563 bool SITargetLowering::isMemOpUniform(const SDNode *N) const {
1564   const MemSDNode *MemNode = cast<MemSDNode>(N);
1565 
1566   return AMDGPUInstrInfo::isUniformMMO(MemNode->getMemOperand());
1567 }
1568 
1569 TargetLoweringBase::LegalizeTypeAction
1570 SITargetLowering::getPreferredVectorAction(MVT VT) const {
1571   int NumElts = VT.getVectorNumElements();
1572   if (NumElts != 1 && VT.getScalarType().bitsLE(MVT::i16))
1573     return VT.isPow2VectorType() ? TypeSplitVector : TypeWidenVector;
1574   return TargetLoweringBase::getPreferredVectorAction(VT);
1575 }
1576 
1577 bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
1578                                                          Type *Ty) const {
1579   // FIXME: Could be smarter if called for vector constants.
1580   return true;
1581 }
1582 
1583 bool SITargetLowering::isTypeDesirableForOp(unsigned Op, EVT VT) const {
1584   if (Subtarget->has16BitInsts() && VT == MVT::i16) {
1585     switch (Op) {
1586     case ISD::LOAD:
1587     case ISD::STORE:
1588 
1589     // These operations are done with 32-bit instructions anyway.
1590     case ISD::AND:
1591     case ISD::OR:
1592     case ISD::XOR:
1593     case ISD::SELECT:
1594       // TODO: Extensions?
1595       return true;
1596     default:
1597       return false;
1598     }
1599   }
1600 
1601   // SimplifySetCC uses this function to determine whether or not it should
1602   // create setcc with i1 operands.  We don't have instructions for i1 setcc.
1603   if (VT == MVT::i1 && Op == ISD::SETCC)
1604     return false;
1605 
1606   return TargetLowering::isTypeDesirableForOp(Op, VT);
1607 }
1608 
1609 SDValue SITargetLowering::lowerKernArgParameterPtr(SelectionDAG &DAG,
1610                                                    const SDLoc &SL,
1611                                                    SDValue Chain,
1612                                                    uint64_t Offset) const {
1613   const DataLayout &DL = DAG.getDataLayout();
1614   MachineFunction &MF = DAG.getMachineFunction();
1615   const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
1616 
1617   const ArgDescriptor *InputPtrReg;
1618   const TargetRegisterClass *RC;
1619   LLT ArgTy;
1620 
1621   std::tie(InputPtrReg, RC, ArgTy) =
1622       Info->getPreloadedValue(AMDGPUFunctionArgInfo::KERNARG_SEGMENT_PTR);
1623 
1624   MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1625   MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS);
1626   SDValue BasePtr = DAG.getCopyFromReg(Chain, SL,
1627     MRI.getLiveInVirtReg(InputPtrReg->getRegister()), PtrVT);
1628 
1629   return DAG.getObjectPtrOffset(SL, BasePtr, TypeSize::Fixed(Offset));
1630 }
1631 
1632 SDValue SITargetLowering::getImplicitArgPtr(SelectionDAG &DAG,
1633                                             const SDLoc &SL) const {
1634   uint64_t Offset = getImplicitParameterOffset(DAG.getMachineFunction(),
1635                                                FIRST_IMPLICIT);
1636   return lowerKernArgParameterPtr(DAG, SL, DAG.getEntryNode(), Offset);
1637 }
1638 
1639 SDValue SITargetLowering::convertArgType(SelectionDAG &DAG, EVT VT, EVT MemVT,
1640                                          const SDLoc &SL, SDValue Val,
1641                                          bool Signed,
1642                                          const ISD::InputArg *Arg) const {
1643   // First, if it is a widened vector, narrow it.
1644   if (VT.isVector() &&
1645       VT.getVectorNumElements() != MemVT.getVectorNumElements()) {
1646     EVT NarrowedVT =
1647         EVT::getVectorVT(*DAG.getContext(), MemVT.getVectorElementType(),
1648                          VT.getVectorNumElements());
1649     Val = DAG.getNode(ISD::EXTRACT_SUBVECTOR, SL, NarrowedVT, Val,
1650                       DAG.getConstant(0, SL, MVT::i32));
1651   }
1652 
1653   // Then convert the vector elements or scalar value.
1654   if (Arg && (Arg->Flags.isSExt() || Arg->Flags.isZExt()) &&
1655       VT.bitsLT(MemVT)) {
1656     unsigned Opc = Arg->Flags.isZExt() ? ISD::AssertZext : ISD::AssertSext;
1657     Val = DAG.getNode(Opc, SL, MemVT, Val, DAG.getValueType(VT));
1658   }
1659 
1660   if (MemVT.isFloatingPoint())
1661     Val = getFPExtOrFPRound(DAG, Val, SL, VT);
1662   else if (Signed)
1663     Val = DAG.getSExtOrTrunc(Val, SL, VT);
1664   else
1665     Val = DAG.getZExtOrTrunc(Val, SL, VT);
1666 
1667   return Val;
1668 }
1669 
1670 SDValue SITargetLowering::lowerKernargMemParameter(
1671     SelectionDAG &DAG, EVT VT, EVT MemVT, const SDLoc &SL, SDValue Chain,
1672     uint64_t Offset, Align Alignment, bool Signed,
1673     const ISD::InputArg *Arg) const {
1674   MachinePointerInfo PtrInfo(AMDGPUAS::CONSTANT_ADDRESS);
1675 
1676   // Try to avoid using an extload by loading earlier than the argument address,
1677   // and extracting the relevant bits. The load should hopefully be merged with
1678   // the previous argument.
1679   if (MemVT.getStoreSize() < 4 && Alignment < 4) {
1680     // TODO: Handle align < 4 and size >= 4 (can happen with packed structs).
1681     int64_t AlignDownOffset = alignDown(Offset, 4);
1682     int64_t OffsetDiff = Offset - AlignDownOffset;
1683 
1684     EVT IntVT = MemVT.changeTypeToInteger();
1685 
1686     // TODO: If we passed in the base kernel offset we could have a better
1687     // alignment than 4, but we don't really need it.
1688     SDValue Ptr = lowerKernArgParameterPtr(DAG, SL, Chain, AlignDownOffset);
1689     SDValue Load = DAG.getLoad(MVT::i32, SL, Chain, Ptr, PtrInfo, Align(4),
1690                                MachineMemOperand::MODereferenceable |
1691                                    MachineMemOperand::MOInvariant);
1692 
1693     SDValue ShiftAmt = DAG.getConstant(OffsetDiff * 8, SL, MVT::i32);
1694     SDValue Extract = DAG.getNode(ISD::SRL, SL, MVT::i32, Load, ShiftAmt);
1695 
1696     SDValue ArgVal = DAG.getNode(ISD::TRUNCATE, SL, IntVT, Extract);
1697     ArgVal = DAG.getNode(ISD::BITCAST, SL, MemVT, ArgVal);
1698     ArgVal = convertArgType(DAG, VT, MemVT, SL, ArgVal, Signed, Arg);
1699 
1700 
1701     return DAG.getMergeValues({ ArgVal, Load.getValue(1) }, SL);
1702   }
1703 
1704   SDValue Ptr = lowerKernArgParameterPtr(DAG, SL, Chain, Offset);
1705   SDValue Load = DAG.getLoad(MemVT, SL, Chain, Ptr, PtrInfo, Alignment,
1706                              MachineMemOperand::MODereferenceable |
1707                                  MachineMemOperand::MOInvariant);
1708 
1709   SDValue Val = convertArgType(DAG, VT, MemVT, SL, Load, Signed, Arg);
1710   return DAG.getMergeValues({ Val, Load.getValue(1) }, SL);
1711 }
1712 
1713 SDValue SITargetLowering::lowerStackParameter(SelectionDAG &DAG, CCValAssign &VA,
1714                                               const SDLoc &SL, SDValue Chain,
1715                                               const ISD::InputArg &Arg) const {
1716   MachineFunction &MF = DAG.getMachineFunction();
1717   MachineFrameInfo &MFI = MF.getFrameInfo();
1718 
1719   if (Arg.Flags.isByVal()) {
1720     unsigned Size = Arg.Flags.getByValSize();
1721     int FrameIdx = MFI.CreateFixedObject(Size, VA.getLocMemOffset(), false);
1722     return DAG.getFrameIndex(FrameIdx, MVT::i32);
1723   }
1724 
1725   unsigned ArgOffset = VA.getLocMemOffset();
1726   unsigned ArgSize = VA.getValVT().getStoreSize();
1727 
1728   int FI = MFI.CreateFixedObject(ArgSize, ArgOffset, true);
1729 
1730   // Create load nodes to retrieve arguments from the stack.
1731   SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
1732   SDValue ArgValue;
1733 
1734   // For NON_EXTLOAD, generic code in getLoad assert(ValVT == MemVT)
1735   ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
1736   MVT MemVT = VA.getValVT();
1737 
1738   switch (VA.getLocInfo()) {
1739   default:
1740     break;
1741   case CCValAssign::BCvt:
1742     MemVT = VA.getLocVT();
1743     break;
1744   case CCValAssign::SExt:
1745     ExtType = ISD::SEXTLOAD;
1746     break;
1747   case CCValAssign::ZExt:
1748     ExtType = ISD::ZEXTLOAD;
1749     break;
1750   case CCValAssign::AExt:
1751     ExtType = ISD::EXTLOAD;
1752     break;
1753   }
1754 
1755   ArgValue = DAG.getExtLoad(
1756     ExtType, SL, VA.getLocVT(), Chain, FIN,
1757     MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
1758     MemVT);
1759   return ArgValue;
1760 }
1761 
1762 SDValue SITargetLowering::getPreloadedValue(SelectionDAG &DAG,
1763   const SIMachineFunctionInfo &MFI,
1764   EVT VT,
1765   AMDGPUFunctionArgInfo::PreloadedValue PVID) const {
1766   const ArgDescriptor *Reg;
1767   const TargetRegisterClass *RC;
1768   LLT Ty;
1769 
1770   std::tie(Reg, RC, Ty) = MFI.getPreloadedValue(PVID);
1771   return CreateLiveInRegister(DAG, RC, Reg->getRegister(), VT);
1772 }
1773 
1774 static void processPSInputArgs(SmallVectorImpl<ISD::InputArg> &Splits,
1775                                CallingConv::ID CallConv,
1776                                ArrayRef<ISD::InputArg> Ins, BitVector &Skipped,
1777                                FunctionType *FType,
1778                                SIMachineFunctionInfo *Info) {
1779   for (unsigned I = 0, E = Ins.size(), PSInputNum = 0; I != E; ++I) {
1780     const ISD::InputArg *Arg = &Ins[I];
1781 
1782     assert((!Arg->VT.isVector() || Arg->VT.getScalarSizeInBits() == 16) &&
1783            "vector type argument should have been split");
1784 
1785     // First check if it's a PS input addr.
1786     if (CallConv == CallingConv::AMDGPU_PS &&
1787         !Arg->Flags.isInReg() && PSInputNum <= 15) {
1788       bool SkipArg = !Arg->Used && !Info->isPSInputAllocated(PSInputNum);
1789 
1790       // Inconveniently only the first part of the split is marked as isSplit,
1791       // so skip to the end. We only want to increment PSInputNum once for the
1792       // entire split argument.
1793       if (Arg->Flags.isSplit()) {
1794         while (!Arg->Flags.isSplitEnd()) {
1795           assert((!Arg->VT.isVector() ||
1796                   Arg->VT.getScalarSizeInBits() == 16) &&
1797                  "unexpected vector split in ps argument type");
1798           if (!SkipArg)
1799             Splits.push_back(*Arg);
1800           Arg = &Ins[++I];
1801         }
1802       }
1803 
1804       if (SkipArg) {
1805         // We can safely skip PS inputs.
1806         Skipped.set(Arg->getOrigArgIndex());
1807         ++PSInputNum;
1808         continue;
1809       }
1810 
1811       Info->markPSInputAllocated(PSInputNum);
1812       if (Arg->Used)
1813         Info->markPSInputEnabled(PSInputNum);
1814 
1815       ++PSInputNum;
1816     }
1817 
1818     Splits.push_back(*Arg);
1819   }
1820 }
1821 
1822 // Allocate special inputs passed in VGPRs.
1823 void SITargetLowering::allocateSpecialEntryInputVGPRs(CCState &CCInfo,
1824                                                       MachineFunction &MF,
1825                                                       const SIRegisterInfo &TRI,
1826                                                       SIMachineFunctionInfo &Info) const {
1827   const LLT S32 = LLT::scalar(32);
1828   MachineRegisterInfo &MRI = MF.getRegInfo();
1829 
1830   if (Info.hasWorkItemIDX()) {
1831     Register Reg = AMDGPU::VGPR0;
1832     MRI.setType(MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass), S32);
1833 
1834     CCInfo.AllocateReg(Reg);
1835     unsigned Mask = (Subtarget->hasPackedTID() &&
1836                      Info.hasWorkItemIDY()) ? 0x3ff : ~0u;
1837     Info.setWorkItemIDX(ArgDescriptor::createRegister(Reg, Mask));
1838   }
1839 
1840   if (Info.hasWorkItemIDY()) {
1841     assert(Info.hasWorkItemIDX());
1842     if (Subtarget->hasPackedTID()) {
1843       Info.setWorkItemIDY(ArgDescriptor::createRegister(AMDGPU::VGPR0,
1844                                                         0x3ff << 10));
1845     } else {
1846       unsigned Reg = AMDGPU::VGPR1;
1847       MRI.setType(MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass), S32);
1848 
1849       CCInfo.AllocateReg(Reg);
1850       Info.setWorkItemIDY(ArgDescriptor::createRegister(Reg));
1851     }
1852   }
1853 
1854   if (Info.hasWorkItemIDZ()) {
1855     assert(Info.hasWorkItemIDX() && Info.hasWorkItemIDY());
1856     if (Subtarget->hasPackedTID()) {
1857       Info.setWorkItemIDZ(ArgDescriptor::createRegister(AMDGPU::VGPR0,
1858                                                         0x3ff << 20));
1859     } else {
1860       unsigned Reg = AMDGPU::VGPR2;
1861       MRI.setType(MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass), S32);
1862 
1863       CCInfo.AllocateReg(Reg);
1864       Info.setWorkItemIDZ(ArgDescriptor::createRegister(Reg));
1865     }
1866   }
1867 }
1868 
1869 // Try to allocate a VGPR at the end of the argument list, or if no argument
1870 // VGPRs are left allocating a stack slot.
1871 // If \p Mask is is given it indicates bitfield position in the register.
1872 // If \p Arg is given use it with new ]p Mask instead of allocating new.
1873 static ArgDescriptor allocateVGPR32Input(CCState &CCInfo, unsigned Mask = ~0u,
1874                                          ArgDescriptor Arg = ArgDescriptor()) {
1875   if (Arg.isSet())
1876     return ArgDescriptor::createArg(Arg, Mask);
1877 
1878   ArrayRef<MCPhysReg> ArgVGPRs
1879     = makeArrayRef(AMDGPU::VGPR_32RegClass.begin(), 32);
1880   unsigned RegIdx = CCInfo.getFirstUnallocated(ArgVGPRs);
1881   if (RegIdx == ArgVGPRs.size()) {
1882     // Spill to stack required.
1883     int64_t Offset = CCInfo.AllocateStack(4, Align(4));
1884 
1885     return ArgDescriptor::createStack(Offset, Mask);
1886   }
1887 
1888   unsigned Reg = ArgVGPRs[RegIdx];
1889   Reg = CCInfo.AllocateReg(Reg);
1890   assert(Reg != AMDGPU::NoRegister);
1891 
1892   MachineFunction &MF = CCInfo.getMachineFunction();
1893   Register LiveInVReg = MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
1894   MF.getRegInfo().setType(LiveInVReg, LLT::scalar(32));
1895   return ArgDescriptor::createRegister(Reg, Mask);
1896 }
1897 
1898 static ArgDescriptor allocateSGPR32InputImpl(CCState &CCInfo,
1899                                              const TargetRegisterClass *RC,
1900                                              unsigned NumArgRegs) {
1901   ArrayRef<MCPhysReg> ArgSGPRs = makeArrayRef(RC->begin(), 32);
1902   unsigned RegIdx = CCInfo.getFirstUnallocated(ArgSGPRs);
1903   if (RegIdx == ArgSGPRs.size())
1904     report_fatal_error("ran out of SGPRs for arguments");
1905 
1906   unsigned Reg = ArgSGPRs[RegIdx];
1907   Reg = CCInfo.AllocateReg(Reg);
1908   assert(Reg != AMDGPU::NoRegister);
1909 
1910   MachineFunction &MF = CCInfo.getMachineFunction();
1911   MF.addLiveIn(Reg, RC);
1912   return ArgDescriptor::createRegister(Reg);
1913 }
1914 
1915 // If this has a fixed position, we still should allocate the register in the
1916 // CCInfo state. Technically we could get away with this for values passed
1917 // outside of the normal argument range.
1918 static void allocateFixedSGPRInputImpl(CCState &CCInfo,
1919                                        const TargetRegisterClass *RC,
1920                                        MCRegister Reg) {
1921   Reg = CCInfo.AllocateReg(Reg);
1922   assert(Reg != AMDGPU::NoRegister);
1923   MachineFunction &MF = CCInfo.getMachineFunction();
1924   MF.addLiveIn(Reg, RC);
1925 }
1926 
1927 static void allocateSGPR32Input(CCState &CCInfo, ArgDescriptor &Arg) {
1928   if (Arg) {
1929     allocateFixedSGPRInputImpl(CCInfo, &AMDGPU::SGPR_32RegClass,
1930                                Arg.getRegister());
1931   } else
1932     Arg = allocateSGPR32InputImpl(CCInfo, &AMDGPU::SGPR_32RegClass, 32);
1933 }
1934 
1935 static void allocateSGPR64Input(CCState &CCInfo, ArgDescriptor &Arg) {
1936   if (Arg) {
1937     allocateFixedSGPRInputImpl(CCInfo, &AMDGPU::SGPR_64RegClass,
1938                                Arg.getRegister());
1939   } else
1940     Arg = allocateSGPR32InputImpl(CCInfo, &AMDGPU::SGPR_64RegClass, 16);
1941 }
1942 
1943 /// Allocate implicit function VGPR arguments at the end of allocated user
1944 /// arguments.
1945 void SITargetLowering::allocateSpecialInputVGPRs(
1946   CCState &CCInfo, MachineFunction &MF,
1947   const SIRegisterInfo &TRI, SIMachineFunctionInfo &Info) const {
1948   const unsigned Mask = 0x3ff;
1949   ArgDescriptor Arg;
1950 
1951   if (Info.hasWorkItemIDX()) {
1952     Arg = allocateVGPR32Input(CCInfo, Mask);
1953     Info.setWorkItemIDX(Arg);
1954   }
1955 
1956   if (Info.hasWorkItemIDY()) {
1957     Arg = allocateVGPR32Input(CCInfo, Mask << 10, Arg);
1958     Info.setWorkItemIDY(Arg);
1959   }
1960 
1961   if (Info.hasWorkItemIDZ())
1962     Info.setWorkItemIDZ(allocateVGPR32Input(CCInfo, Mask << 20, Arg));
1963 }
1964 
1965 /// Allocate implicit function VGPR arguments in fixed registers.
1966 void SITargetLowering::allocateSpecialInputVGPRsFixed(
1967   CCState &CCInfo, MachineFunction &MF,
1968   const SIRegisterInfo &TRI, SIMachineFunctionInfo &Info) const {
1969   Register Reg = CCInfo.AllocateReg(AMDGPU::VGPR31);
1970   if (!Reg)
1971     report_fatal_error("failed to allocated VGPR for implicit arguments");
1972 
1973   const unsigned Mask = 0x3ff;
1974   Info.setWorkItemIDX(ArgDescriptor::createRegister(Reg, Mask));
1975   Info.setWorkItemIDY(ArgDescriptor::createRegister(Reg, Mask << 10));
1976   Info.setWorkItemIDZ(ArgDescriptor::createRegister(Reg, Mask << 20));
1977 }
1978 
1979 void SITargetLowering::allocateSpecialInputSGPRs(
1980   CCState &CCInfo,
1981   MachineFunction &MF,
1982   const SIRegisterInfo &TRI,
1983   SIMachineFunctionInfo &Info) const {
1984   auto &ArgInfo = Info.getArgInfo();
1985 
1986   // TODO: Unify handling with private memory pointers.
1987 
1988   if (Info.hasDispatchPtr())
1989     allocateSGPR64Input(CCInfo, ArgInfo.DispatchPtr);
1990 
1991   if (Info.hasQueuePtr())
1992     allocateSGPR64Input(CCInfo, ArgInfo.QueuePtr);
1993 
1994   // Implicit arg ptr takes the place of the kernarg segment pointer. This is a
1995   // constant offset from the kernarg segment.
1996   if (Info.hasImplicitArgPtr())
1997     allocateSGPR64Input(CCInfo, ArgInfo.ImplicitArgPtr);
1998 
1999   if (Info.hasDispatchID())
2000     allocateSGPR64Input(CCInfo, ArgInfo.DispatchID);
2001 
2002   // flat_scratch_init is not applicable for non-kernel functions.
2003 
2004   if (Info.hasWorkGroupIDX())
2005     allocateSGPR32Input(CCInfo, ArgInfo.WorkGroupIDX);
2006 
2007   if (Info.hasWorkGroupIDY())
2008     allocateSGPR32Input(CCInfo, ArgInfo.WorkGroupIDY);
2009 
2010   if (Info.hasWorkGroupIDZ())
2011     allocateSGPR32Input(CCInfo, ArgInfo.WorkGroupIDZ);
2012 }
2013 
2014 // Allocate special inputs passed in user SGPRs.
2015 void SITargetLowering::allocateHSAUserSGPRs(CCState &CCInfo,
2016                                             MachineFunction &MF,
2017                                             const SIRegisterInfo &TRI,
2018                                             SIMachineFunctionInfo &Info) const {
2019   if (Info.hasImplicitBufferPtr()) {
2020     Register ImplicitBufferPtrReg = Info.addImplicitBufferPtr(TRI);
2021     MF.addLiveIn(ImplicitBufferPtrReg, &AMDGPU::SGPR_64RegClass);
2022     CCInfo.AllocateReg(ImplicitBufferPtrReg);
2023   }
2024 
2025   // FIXME: How should these inputs interact with inreg / custom SGPR inputs?
2026   if (Info.hasPrivateSegmentBuffer()) {
2027     Register PrivateSegmentBufferReg = Info.addPrivateSegmentBuffer(TRI);
2028     MF.addLiveIn(PrivateSegmentBufferReg, &AMDGPU::SGPR_128RegClass);
2029     CCInfo.AllocateReg(PrivateSegmentBufferReg);
2030   }
2031 
2032   if (Info.hasDispatchPtr()) {
2033     Register DispatchPtrReg = Info.addDispatchPtr(TRI);
2034     MF.addLiveIn(DispatchPtrReg, &AMDGPU::SGPR_64RegClass);
2035     CCInfo.AllocateReg(DispatchPtrReg);
2036   }
2037 
2038   if (Info.hasQueuePtr()) {
2039     Register QueuePtrReg = Info.addQueuePtr(TRI);
2040     MF.addLiveIn(QueuePtrReg, &AMDGPU::SGPR_64RegClass);
2041     CCInfo.AllocateReg(QueuePtrReg);
2042   }
2043 
2044   if (Info.hasKernargSegmentPtr()) {
2045     MachineRegisterInfo &MRI = MF.getRegInfo();
2046     Register InputPtrReg = Info.addKernargSegmentPtr(TRI);
2047     CCInfo.AllocateReg(InputPtrReg);
2048 
2049     Register VReg = MF.addLiveIn(InputPtrReg, &AMDGPU::SGPR_64RegClass);
2050     MRI.setType(VReg, LLT::pointer(AMDGPUAS::CONSTANT_ADDRESS, 64));
2051   }
2052 
2053   if (Info.hasDispatchID()) {
2054     Register DispatchIDReg = Info.addDispatchID(TRI);
2055     MF.addLiveIn(DispatchIDReg, &AMDGPU::SGPR_64RegClass);
2056     CCInfo.AllocateReg(DispatchIDReg);
2057   }
2058 
2059   if (Info.hasFlatScratchInit() && !getSubtarget()->isAmdPalOS()) {
2060     Register FlatScratchInitReg = Info.addFlatScratchInit(TRI);
2061     MF.addLiveIn(FlatScratchInitReg, &AMDGPU::SGPR_64RegClass);
2062     CCInfo.AllocateReg(FlatScratchInitReg);
2063   }
2064 
2065   // TODO: Add GridWorkGroupCount user SGPRs when used. For now with HSA we read
2066   // these from the dispatch pointer.
2067 }
2068 
2069 // Allocate special input registers that are initialized per-wave.
2070 void SITargetLowering::allocateSystemSGPRs(CCState &CCInfo,
2071                                            MachineFunction &MF,
2072                                            SIMachineFunctionInfo &Info,
2073                                            CallingConv::ID CallConv,
2074                                            bool IsShader) const {
2075   if (Info.hasWorkGroupIDX()) {
2076     Register Reg = Info.addWorkGroupIDX();
2077     MF.addLiveIn(Reg, &AMDGPU::SGPR_32RegClass);
2078     CCInfo.AllocateReg(Reg);
2079   }
2080 
2081   if (Info.hasWorkGroupIDY()) {
2082     Register Reg = Info.addWorkGroupIDY();
2083     MF.addLiveIn(Reg, &AMDGPU::SGPR_32RegClass);
2084     CCInfo.AllocateReg(Reg);
2085   }
2086 
2087   if (Info.hasWorkGroupIDZ()) {
2088     Register Reg = Info.addWorkGroupIDZ();
2089     MF.addLiveIn(Reg, &AMDGPU::SGPR_32RegClass);
2090     CCInfo.AllocateReg(Reg);
2091   }
2092 
2093   if (Info.hasWorkGroupInfo()) {
2094     Register Reg = Info.addWorkGroupInfo();
2095     MF.addLiveIn(Reg, &AMDGPU::SGPR_32RegClass);
2096     CCInfo.AllocateReg(Reg);
2097   }
2098 
2099   if (Info.hasPrivateSegmentWaveByteOffset()) {
2100     // Scratch wave offset passed in system SGPR.
2101     unsigned PrivateSegmentWaveByteOffsetReg;
2102 
2103     if (IsShader) {
2104       PrivateSegmentWaveByteOffsetReg =
2105         Info.getPrivateSegmentWaveByteOffsetSystemSGPR();
2106 
2107       // This is true if the scratch wave byte offset doesn't have a fixed
2108       // location.
2109       if (PrivateSegmentWaveByteOffsetReg == AMDGPU::NoRegister) {
2110         PrivateSegmentWaveByteOffsetReg = findFirstFreeSGPR(CCInfo);
2111         Info.setPrivateSegmentWaveByteOffset(PrivateSegmentWaveByteOffsetReg);
2112       }
2113     } else
2114       PrivateSegmentWaveByteOffsetReg = Info.addPrivateSegmentWaveByteOffset();
2115 
2116     MF.addLiveIn(PrivateSegmentWaveByteOffsetReg, &AMDGPU::SGPR_32RegClass);
2117     CCInfo.AllocateReg(PrivateSegmentWaveByteOffsetReg);
2118   }
2119 }
2120 
2121 static void reservePrivateMemoryRegs(const TargetMachine &TM,
2122                                      MachineFunction &MF,
2123                                      const SIRegisterInfo &TRI,
2124                                      SIMachineFunctionInfo &Info) {
2125   // Now that we've figured out where the scratch register inputs are, see if
2126   // should reserve the arguments and use them directly.
2127   MachineFrameInfo &MFI = MF.getFrameInfo();
2128   bool HasStackObjects = MFI.hasStackObjects();
2129   const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
2130 
2131   // Record that we know we have non-spill stack objects so we don't need to
2132   // check all stack objects later.
2133   if (HasStackObjects)
2134     Info.setHasNonSpillStackObjects(true);
2135 
2136   // Everything live out of a block is spilled with fast regalloc, so it's
2137   // almost certain that spilling will be required.
2138   if (TM.getOptLevel() == CodeGenOpt::None)
2139     HasStackObjects = true;
2140 
2141   // For now assume stack access is needed in any callee functions, so we need
2142   // the scratch registers to pass in.
2143   bool RequiresStackAccess = HasStackObjects || MFI.hasCalls();
2144 
2145   if (!ST.enableFlatScratch()) {
2146     if (RequiresStackAccess && ST.isAmdHsaOrMesa(MF.getFunction())) {
2147       // If we have stack objects, we unquestionably need the private buffer
2148       // resource. For the Code Object V2 ABI, this will be the first 4 user
2149       // SGPR inputs. We can reserve those and use them directly.
2150 
2151       Register PrivateSegmentBufferReg =
2152           Info.getPreloadedReg(AMDGPUFunctionArgInfo::PRIVATE_SEGMENT_BUFFER);
2153       Info.setScratchRSrcReg(PrivateSegmentBufferReg);
2154     } else {
2155       unsigned ReservedBufferReg = TRI.reservedPrivateSegmentBufferReg(MF);
2156       // We tentatively reserve the last registers (skipping the last registers
2157       // which may contain VCC, FLAT_SCR, and XNACK). After register allocation,
2158       // we'll replace these with the ones immediately after those which were
2159       // really allocated. In the prologue copies will be inserted from the
2160       // argument to these reserved registers.
2161 
2162       // Without HSA, relocations are used for the scratch pointer and the
2163       // buffer resource setup is always inserted in the prologue. Scratch wave
2164       // offset is still in an input SGPR.
2165       Info.setScratchRSrcReg(ReservedBufferReg);
2166     }
2167   }
2168 
2169   MachineRegisterInfo &MRI = MF.getRegInfo();
2170 
2171   // For entry functions we have to set up the stack pointer if we use it,
2172   // whereas non-entry functions get this "for free". This means there is no
2173   // intrinsic advantage to using S32 over S34 in cases where we do not have
2174   // calls but do need a frame pointer (i.e. if we are requested to have one
2175   // because frame pointer elimination is disabled). To keep things simple we
2176   // only ever use S32 as the call ABI stack pointer, and so using it does not
2177   // imply we need a separate frame pointer.
2178   //
2179   // Try to use s32 as the SP, but move it if it would interfere with input
2180   // arguments. This won't work with calls though.
2181   //
2182   // FIXME: Move SP to avoid any possible inputs, or find a way to spill input
2183   // registers.
2184   if (!MRI.isLiveIn(AMDGPU::SGPR32)) {
2185     Info.setStackPtrOffsetReg(AMDGPU::SGPR32);
2186   } else {
2187     assert(AMDGPU::isShader(MF.getFunction().getCallingConv()));
2188 
2189     if (MFI.hasCalls())
2190       report_fatal_error("call in graphics shader with too many input SGPRs");
2191 
2192     for (unsigned Reg : AMDGPU::SGPR_32RegClass) {
2193       if (!MRI.isLiveIn(Reg)) {
2194         Info.setStackPtrOffsetReg(Reg);
2195         break;
2196       }
2197     }
2198 
2199     if (Info.getStackPtrOffsetReg() == AMDGPU::SP_REG)
2200       report_fatal_error("failed to find register for SP");
2201   }
2202 
2203   // hasFP should be accurate for entry functions even before the frame is
2204   // finalized, because it does not rely on the known stack size, only
2205   // properties like whether variable sized objects are present.
2206   if (ST.getFrameLowering()->hasFP(MF)) {
2207     Info.setFrameOffsetReg(AMDGPU::SGPR33);
2208   }
2209 }
2210 
2211 bool SITargetLowering::supportSplitCSR(MachineFunction *MF) const {
2212   const SIMachineFunctionInfo *Info = MF->getInfo<SIMachineFunctionInfo>();
2213   return !Info->isEntryFunction();
2214 }
2215 
2216 void SITargetLowering::initializeSplitCSR(MachineBasicBlock *Entry) const {
2217 
2218 }
2219 
2220 void SITargetLowering::insertCopiesSplitCSR(
2221   MachineBasicBlock *Entry,
2222   const SmallVectorImpl<MachineBasicBlock *> &Exits) const {
2223   const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
2224 
2225   const MCPhysReg *IStart = TRI->getCalleeSavedRegsViaCopy(Entry->getParent());
2226   if (!IStart)
2227     return;
2228 
2229   const TargetInstrInfo *TII = Subtarget->getInstrInfo();
2230   MachineRegisterInfo *MRI = &Entry->getParent()->getRegInfo();
2231   MachineBasicBlock::iterator MBBI = Entry->begin();
2232   for (const MCPhysReg *I = IStart; *I; ++I) {
2233     const TargetRegisterClass *RC = nullptr;
2234     if (AMDGPU::SReg_64RegClass.contains(*I))
2235       RC = &AMDGPU::SGPR_64RegClass;
2236     else if (AMDGPU::SReg_32RegClass.contains(*I))
2237       RC = &AMDGPU::SGPR_32RegClass;
2238     else
2239       llvm_unreachable("Unexpected register class in CSRsViaCopy!");
2240 
2241     Register NewVR = MRI->createVirtualRegister(RC);
2242     // Create copy from CSR to a virtual register.
2243     Entry->addLiveIn(*I);
2244     BuildMI(*Entry, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY), NewVR)
2245       .addReg(*I);
2246 
2247     // Insert the copy-back instructions right before the terminator.
2248     for (auto *Exit : Exits)
2249       BuildMI(*Exit, Exit->getFirstTerminator(), DebugLoc(),
2250               TII->get(TargetOpcode::COPY), *I)
2251         .addReg(NewVR);
2252   }
2253 }
2254 
2255 SDValue SITargetLowering::LowerFormalArguments(
2256     SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
2257     const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
2258     SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
2259   const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
2260 
2261   MachineFunction &MF = DAG.getMachineFunction();
2262   const Function &Fn = MF.getFunction();
2263   FunctionType *FType = MF.getFunction().getFunctionType();
2264   SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
2265 
2266   if (Subtarget->isAmdHsaOS() && AMDGPU::isGraphics(CallConv)) {
2267     DiagnosticInfoUnsupported NoGraphicsHSA(
2268         Fn, "unsupported non-compute shaders with HSA", DL.getDebugLoc());
2269     DAG.getContext()->diagnose(NoGraphicsHSA);
2270     return DAG.getEntryNode();
2271   }
2272 
2273   Info->allocateModuleLDSGlobal(Fn.getParent());
2274 
2275   SmallVector<ISD::InputArg, 16> Splits;
2276   SmallVector<CCValAssign, 16> ArgLocs;
2277   BitVector Skipped(Ins.size());
2278   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
2279                  *DAG.getContext());
2280 
2281   bool IsGraphics = AMDGPU::isGraphics(CallConv);
2282   bool IsKernel = AMDGPU::isKernel(CallConv);
2283   bool IsEntryFunc = AMDGPU::isEntryFunctionCC(CallConv);
2284 
2285   if (IsGraphics) {
2286     assert(!Info->hasDispatchPtr() && !Info->hasKernargSegmentPtr() &&
2287            (!Info->hasFlatScratchInit() || Subtarget->enableFlatScratch()) &&
2288            !Info->hasWorkGroupIDX() && !Info->hasWorkGroupIDY() &&
2289            !Info->hasWorkGroupIDZ() && !Info->hasWorkGroupInfo() &&
2290            !Info->hasWorkItemIDX() && !Info->hasWorkItemIDY() &&
2291            !Info->hasWorkItemIDZ());
2292   }
2293 
2294   if (CallConv == CallingConv::AMDGPU_PS) {
2295     processPSInputArgs(Splits, CallConv, Ins, Skipped, FType, Info);
2296 
2297     // At least one interpolation mode must be enabled or else the GPU will
2298     // hang.
2299     //
2300     // Check PSInputAddr instead of PSInputEnable. The idea is that if the user
2301     // set PSInputAddr, the user wants to enable some bits after the compilation
2302     // based on run-time states. Since we can't know what the final PSInputEna
2303     // will look like, so we shouldn't do anything here and the user should take
2304     // responsibility for the correct programming.
2305     //
2306     // Otherwise, the following restrictions apply:
2307     // - At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled.
2308     // - If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be
2309     //   enabled too.
2310     if ((Info->getPSInputAddr() & 0x7F) == 0 ||
2311         ((Info->getPSInputAddr() & 0xF) == 0 && Info->isPSInputAllocated(11))) {
2312       CCInfo.AllocateReg(AMDGPU::VGPR0);
2313       CCInfo.AllocateReg(AMDGPU::VGPR1);
2314       Info->markPSInputAllocated(0);
2315       Info->markPSInputEnabled(0);
2316     }
2317     if (Subtarget->isAmdPalOS()) {
2318       // For isAmdPalOS, the user does not enable some bits after compilation
2319       // based on run-time states; the register values being generated here are
2320       // the final ones set in hardware. Therefore we need to apply the
2321       // workaround to PSInputAddr and PSInputEnable together.  (The case where
2322       // a bit is set in PSInputAddr but not PSInputEnable is where the
2323       // frontend set up an input arg for a particular interpolation mode, but
2324       // nothing uses that input arg. Really we should have an earlier pass
2325       // that removes such an arg.)
2326       unsigned PsInputBits = Info->getPSInputAddr() & Info->getPSInputEnable();
2327       if ((PsInputBits & 0x7F) == 0 ||
2328           ((PsInputBits & 0xF) == 0 && (PsInputBits >> 11 & 1)))
2329         Info->markPSInputEnabled(
2330             countTrailingZeros(Info->getPSInputAddr(), ZB_Undefined));
2331     }
2332   } else if (IsKernel) {
2333     assert(Info->hasWorkGroupIDX() && Info->hasWorkItemIDX());
2334   } else {
2335     Splits.append(Ins.begin(), Ins.end());
2336   }
2337 
2338   if (IsEntryFunc) {
2339     allocateSpecialEntryInputVGPRs(CCInfo, MF, *TRI, *Info);
2340     allocateHSAUserSGPRs(CCInfo, MF, *TRI, *Info);
2341   } else {
2342     // For the fixed ABI, pass workitem IDs in the last argument register.
2343     if (AMDGPUTargetMachine::EnableFixedFunctionABI)
2344       allocateSpecialInputVGPRsFixed(CCInfo, MF, *TRI, *Info);
2345   }
2346 
2347   if (IsKernel) {
2348     analyzeFormalArgumentsCompute(CCInfo, Ins);
2349   } else {
2350     CCAssignFn *AssignFn = CCAssignFnForCall(CallConv, isVarArg);
2351     CCInfo.AnalyzeFormalArguments(Splits, AssignFn);
2352   }
2353 
2354   SmallVector<SDValue, 16> Chains;
2355 
2356   // FIXME: This is the minimum kernel argument alignment. We should improve
2357   // this to the maximum alignment of the arguments.
2358   //
2359   // FIXME: Alignment of explicit arguments totally broken with non-0 explicit
2360   // kern arg offset.
2361   const Align KernelArgBaseAlign = Align(16);
2362 
2363   for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
2364     const ISD::InputArg &Arg = Ins[i];
2365     if (Arg.isOrigArg() && Skipped[Arg.getOrigArgIndex()]) {
2366       InVals.push_back(DAG.getUNDEF(Arg.VT));
2367       continue;
2368     }
2369 
2370     CCValAssign &VA = ArgLocs[ArgIdx++];
2371     MVT VT = VA.getLocVT();
2372 
2373     if (IsEntryFunc && VA.isMemLoc()) {
2374       VT = Ins[i].VT;
2375       EVT MemVT = VA.getLocVT();
2376 
2377       const uint64_t Offset = VA.getLocMemOffset();
2378       Align Alignment = commonAlignment(KernelArgBaseAlign, Offset);
2379 
2380       if (Arg.Flags.isByRef()) {
2381         SDValue Ptr = lowerKernArgParameterPtr(DAG, DL, Chain, Offset);
2382 
2383         const GCNTargetMachine &TM =
2384             static_cast<const GCNTargetMachine &>(getTargetMachine());
2385         if (!TM.isNoopAddrSpaceCast(AMDGPUAS::CONSTANT_ADDRESS,
2386                                     Arg.Flags.getPointerAddrSpace())) {
2387           Ptr = DAG.getAddrSpaceCast(DL, VT, Ptr, AMDGPUAS::CONSTANT_ADDRESS,
2388                                      Arg.Flags.getPointerAddrSpace());
2389         }
2390 
2391         InVals.push_back(Ptr);
2392         continue;
2393       }
2394 
2395       SDValue Arg = lowerKernargMemParameter(
2396         DAG, VT, MemVT, DL, Chain, Offset, Alignment, Ins[i].Flags.isSExt(), &Ins[i]);
2397       Chains.push_back(Arg.getValue(1));
2398 
2399       auto *ParamTy =
2400         dyn_cast<PointerType>(FType->getParamType(Ins[i].getOrigArgIndex()));
2401       if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS &&
2402           ParamTy && (ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
2403                       ParamTy->getAddressSpace() == AMDGPUAS::REGION_ADDRESS)) {
2404         // On SI local pointers are just offsets into LDS, so they are always
2405         // less than 16-bits.  On CI and newer they could potentially be
2406         // real pointers, so we can't guarantee their size.
2407         Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg,
2408                           DAG.getValueType(MVT::i16));
2409       }
2410 
2411       InVals.push_back(Arg);
2412       continue;
2413     } else if (!IsEntryFunc && VA.isMemLoc()) {
2414       SDValue Val = lowerStackParameter(DAG, VA, DL, Chain, Arg);
2415       InVals.push_back(Val);
2416       if (!Arg.Flags.isByVal())
2417         Chains.push_back(Val.getValue(1));
2418       continue;
2419     }
2420 
2421     assert(VA.isRegLoc() && "Parameter must be in a register!");
2422 
2423     Register Reg = VA.getLocReg();
2424     const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
2425     EVT ValVT = VA.getValVT();
2426 
2427     Reg = MF.addLiveIn(Reg, RC);
2428     SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
2429 
2430     if (Arg.Flags.isSRet()) {
2431       // The return object should be reasonably addressable.
2432 
2433       // FIXME: This helps when the return is a real sret. If it is a
2434       // automatically inserted sret (i.e. CanLowerReturn returns false), an
2435       // extra copy is inserted in SelectionDAGBuilder which obscures this.
2436       unsigned NumBits
2437         = 32 - getSubtarget()->getKnownHighZeroBitsForFrameIndex();
2438       Val = DAG.getNode(ISD::AssertZext, DL, VT, Val,
2439         DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(), NumBits)));
2440     }
2441 
2442     // If this is an 8 or 16-bit value, it is really passed promoted
2443     // to 32 bits. Insert an assert[sz]ext to capture this, then
2444     // truncate to the right size.
2445     switch (VA.getLocInfo()) {
2446     case CCValAssign::Full:
2447       break;
2448     case CCValAssign::BCvt:
2449       Val = DAG.getNode(ISD::BITCAST, DL, ValVT, Val);
2450       break;
2451     case CCValAssign::SExt:
2452       Val = DAG.getNode(ISD::AssertSext, DL, VT, Val,
2453                         DAG.getValueType(ValVT));
2454       Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2455       break;
2456     case CCValAssign::ZExt:
2457       Val = DAG.getNode(ISD::AssertZext, DL, VT, Val,
2458                         DAG.getValueType(ValVT));
2459       Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2460       break;
2461     case CCValAssign::AExt:
2462       Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2463       break;
2464     default:
2465       llvm_unreachable("Unknown loc info!");
2466     }
2467 
2468     InVals.push_back(Val);
2469   }
2470 
2471   if (!IsEntryFunc && !AMDGPUTargetMachine::EnableFixedFunctionABI) {
2472     // Special inputs come after user arguments.
2473     allocateSpecialInputVGPRs(CCInfo, MF, *TRI, *Info);
2474   }
2475 
2476   // Start adding system SGPRs.
2477   if (IsEntryFunc) {
2478     allocateSystemSGPRs(CCInfo, MF, *Info, CallConv, IsGraphics);
2479   } else {
2480     CCInfo.AllocateReg(Info->getScratchRSrcReg());
2481     allocateSpecialInputSGPRs(CCInfo, MF, *TRI, *Info);
2482   }
2483 
2484   auto &ArgUsageInfo =
2485     DAG.getPass()->getAnalysis<AMDGPUArgumentUsageInfo>();
2486   ArgUsageInfo.setFuncArgInfo(Fn, Info->getArgInfo());
2487 
2488   unsigned StackArgSize = CCInfo.getNextStackOffset();
2489   Info->setBytesInStackArgArea(StackArgSize);
2490 
2491   return Chains.empty() ? Chain :
2492     DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
2493 }
2494 
2495 // TODO: If return values can't fit in registers, we should return as many as
2496 // possible in registers before passing on stack.
2497 bool SITargetLowering::CanLowerReturn(
2498   CallingConv::ID CallConv,
2499   MachineFunction &MF, bool IsVarArg,
2500   const SmallVectorImpl<ISD::OutputArg> &Outs,
2501   LLVMContext &Context) const {
2502   // Replacing returns with sret/stack usage doesn't make sense for shaders.
2503   // FIXME: Also sort of a workaround for custom vector splitting in LowerReturn
2504   // for shaders. Vector types should be explicitly handled by CC.
2505   if (AMDGPU::isEntryFunctionCC(CallConv))
2506     return true;
2507 
2508   SmallVector<CCValAssign, 16> RVLocs;
2509   CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
2510   return CCInfo.CheckReturn(Outs, CCAssignFnForReturn(CallConv, IsVarArg));
2511 }
2512 
2513 SDValue
2514 SITargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
2515                               bool isVarArg,
2516                               const SmallVectorImpl<ISD::OutputArg> &Outs,
2517                               const SmallVectorImpl<SDValue> &OutVals,
2518                               const SDLoc &DL, SelectionDAG &DAG) const {
2519   MachineFunction &MF = DAG.getMachineFunction();
2520   SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
2521 
2522   if (AMDGPU::isKernel(CallConv)) {
2523     return AMDGPUTargetLowering::LowerReturn(Chain, CallConv, isVarArg, Outs,
2524                                              OutVals, DL, DAG);
2525   }
2526 
2527   bool IsShader = AMDGPU::isShader(CallConv);
2528 
2529   Info->setIfReturnsVoid(Outs.empty());
2530   bool IsWaveEnd = Info->returnsVoid() && IsShader;
2531 
2532   // CCValAssign - represent the assignment of the return value to a location.
2533   SmallVector<CCValAssign, 48> RVLocs;
2534   SmallVector<ISD::OutputArg, 48> Splits;
2535 
2536   // CCState - Info about the registers and stack slots.
2537   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
2538                  *DAG.getContext());
2539 
2540   // Analyze outgoing return values.
2541   CCInfo.AnalyzeReturn(Outs, CCAssignFnForReturn(CallConv, isVarArg));
2542 
2543   SDValue Flag;
2544   SmallVector<SDValue, 48> RetOps;
2545   RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
2546 
2547   // Add return address for callable functions.
2548   if (!Info->isEntryFunction()) {
2549     const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
2550     SDValue ReturnAddrReg = CreateLiveInRegister(
2551       DAG, &AMDGPU::SReg_64RegClass, TRI->getReturnAddressReg(MF), MVT::i64);
2552 
2553     SDValue ReturnAddrVirtualReg = DAG.getRegister(
2554         MF.getRegInfo().createVirtualRegister(&AMDGPU::CCR_SGPR_64RegClass),
2555         MVT::i64);
2556     Chain =
2557         DAG.getCopyToReg(Chain, DL, ReturnAddrVirtualReg, ReturnAddrReg, Flag);
2558     Flag = Chain.getValue(1);
2559     RetOps.push_back(ReturnAddrVirtualReg);
2560   }
2561 
2562   // Copy the result values into the output registers.
2563   for (unsigned I = 0, RealRVLocIdx = 0, E = RVLocs.size(); I != E;
2564        ++I, ++RealRVLocIdx) {
2565     CCValAssign &VA = RVLocs[I];
2566     assert(VA.isRegLoc() && "Can only return in registers!");
2567     // TODO: Partially return in registers if return values don't fit.
2568     SDValue Arg = OutVals[RealRVLocIdx];
2569 
2570     // Copied from other backends.
2571     switch (VA.getLocInfo()) {
2572     case CCValAssign::Full:
2573       break;
2574     case CCValAssign::BCvt:
2575       Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
2576       break;
2577     case CCValAssign::SExt:
2578       Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg);
2579       break;
2580     case CCValAssign::ZExt:
2581       Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg);
2582       break;
2583     case CCValAssign::AExt:
2584       Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg);
2585       break;
2586     default:
2587       llvm_unreachable("Unknown loc info!");
2588     }
2589 
2590     Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag);
2591     Flag = Chain.getValue(1);
2592     RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
2593   }
2594 
2595   // FIXME: Does sret work properly?
2596   if (!Info->isEntryFunction()) {
2597     const SIRegisterInfo *TRI = Subtarget->getRegisterInfo();
2598     const MCPhysReg *I =
2599       TRI->getCalleeSavedRegsViaCopy(&DAG.getMachineFunction());
2600     if (I) {
2601       for (; *I; ++I) {
2602         if (AMDGPU::SReg_64RegClass.contains(*I))
2603           RetOps.push_back(DAG.getRegister(*I, MVT::i64));
2604         else if (AMDGPU::SReg_32RegClass.contains(*I))
2605           RetOps.push_back(DAG.getRegister(*I, MVT::i32));
2606         else
2607           llvm_unreachable("Unexpected register class in CSRsViaCopy!");
2608       }
2609     }
2610   }
2611 
2612   // Update chain and glue.
2613   RetOps[0] = Chain;
2614   if (Flag.getNode())
2615     RetOps.push_back(Flag);
2616 
2617   unsigned Opc = AMDGPUISD::ENDPGM;
2618   if (!IsWaveEnd)
2619     Opc = IsShader ? AMDGPUISD::RETURN_TO_EPILOG : AMDGPUISD::RET_FLAG;
2620   return DAG.getNode(Opc, DL, MVT::Other, RetOps);
2621 }
2622 
2623 SDValue SITargetLowering::LowerCallResult(
2624     SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool IsVarArg,
2625     const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
2626     SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals, bool IsThisReturn,
2627     SDValue ThisVal) const {
2628   CCAssignFn *RetCC = CCAssignFnForReturn(CallConv, IsVarArg);
2629 
2630   // Assign locations to each value returned by this call.
2631   SmallVector<CCValAssign, 16> RVLocs;
2632   CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
2633                  *DAG.getContext());
2634   CCInfo.AnalyzeCallResult(Ins, RetCC);
2635 
2636   // Copy all of the result registers out of their specified physreg.
2637   for (unsigned i = 0; i != RVLocs.size(); ++i) {
2638     CCValAssign VA = RVLocs[i];
2639     SDValue Val;
2640 
2641     if (VA.isRegLoc()) {
2642       Val = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(), VA.getLocVT(), InFlag);
2643       Chain = Val.getValue(1);
2644       InFlag = Val.getValue(2);
2645     } else if (VA.isMemLoc()) {
2646       report_fatal_error("TODO: return values in memory");
2647     } else
2648       llvm_unreachable("unknown argument location type");
2649 
2650     switch (VA.getLocInfo()) {
2651     case CCValAssign::Full:
2652       break;
2653     case CCValAssign::BCvt:
2654       Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
2655       break;
2656     case CCValAssign::ZExt:
2657       Val = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Val,
2658                         DAG.getValueType(VA.getValVT()));
2659       Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2660       break;
2661     case CCValAssign::SExt:
2662       Val = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Val,
2663                         DAG.getValueType(VA.getValVT()));
2664       Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2665       break;
2666     case CCValAssign::AExt:
2667       Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2668       break;
2669     default:
2670       llvm_unreachable("Unknown loc info!");
2671     }
2672 
2673     InVals.push_back(Val);
2674   }
2675 
2676   return Chain;
2677 }
2678 
2679 // Add code to pass special inputs required depending on used features separate
2680 // from the explicit user arguments present in the IR.
2681 void SITargetLowering::passSpecialInputs(
2682     CallLoweringInfo &CLI,
2683     CCState &CCInfo,
2684     const SIMachineFunctionInfo &Info,
2685     SmallVectorImpl<std::pair<unsigned, SDValue>> &RegsToPass,
2686     SmallVectorImpl<SDValue> &MemOpChains,
2687     SDValue Chain) const {
2688   // If we don't have a call site, this was a call inserted by
2689   // legalization. These can never use special inputs.
2690   if (!CLI.CB)
2691     return;
2692 
2693   SelectionDAG &DAG = CLI.DAG;
2694   const SDLoc &DL = CLI.DL;
2695 
2696   const SIRegisterInfo *TRI = Subtarget->getRegisterInfo();
2697   const AMDGPUFunctionArgInfo &CallerArgInfo = Info.getArgInfo();
2698 
2699   const AMDGPUFunctionArgInfo *CalleeArgInfo
2700     = &AMDGPUArgumentUsageInfo::FixedABIFunctionInfo;
2701   if (const Function *CalleeFunc = CLI.CB->getCalledFunction()) {
2702     auto &ArgUsageInfo =
2703       DAG.getPass()->getAnalysis<AMDGPUArgumentUsageInfo>();
2704     CalleeArgInfo = &ArgUsageInfo.lookupFuncArgInfo(*CalleeFunc);
2705   }
2706 
2707   // TODO: Unify with private memory register handling. This is complicated by
2708   // the fact that at least in kernels, the input argument is not necessarily
2709   // in the same location as the input.
2710   AMDGPUFunctionArgInfo::PreloadedValue InputRegs[] = {
2711     AMDGPUFunctionArgInfo::DISPATCH_PTR,
2712     AMDGPUFunctionArgInfo::QUEUE_PTR,
2713     AMDGPUFunctionArgInfo::IMPLICIT_ARG_PTR,
2714     AMDGPUFunctionArgInfo::DISPATCH_ID,
2715     AMDGPUFunctionArgInfo::WORKGROUP_ID_X,
2716     AMDGPUFunctionArgInfo::WORKGROUP_ID_Y,
2717     AMDGPUFunctionArgInfo::WORKGROUP_ID_Z
2718   };
2719 
2720   for (auto InputID : InputRegs) {
2721     const ArgDescriptor *OutgoingArg;
2722     const TargetRegisterClass *ArgRC;
2723     LLT ArgTy;
2724 
2725     std::tie(OutgoingArg, ArgRC, ArgTy) =
2726         CalleeArgInfo->getPreloadedValue(InputID);
2727     if (!OutgoingArg)
2728       continue;
2729 
2730     const ArgDescriptor *IncomingArg;
2731     const TargetRegisterClass *IncomingArgRC;
2732     LLT Ty;
2733     std::tie(IncomingArg, IncomingArgRC, Ty) =
2734         CallerArgInfo.getPreloadedValue(InputID);
2735     assert(IncomingArgRC == ArgRC);
2736 
2737     // All special arguments are ints for now.
2738     EVT ArgVT = TRI->getSpillSize(*ArgRC) == 8 ? MVT::i64 : MVT::i32;
2739     SDValue InputReg;
2740 
2741     if (IncomingArg) {
2742       InputReg = loadInputValue(DAG, ArgRC, ArgVT, DL, *IncomingArg);
2743     } else {
2744       // The implicit arg ptr is special because it doesn't have a corresponding
2745       // input for kernels, and is computed from the kernarg segment pointer.
2746       assert(InputID == AMDGPUFunctionArgInfo::IMPLICIT_ARG_PTR);
2747       InputReg = getImplicitArgPtr(DAG, DL);
2748     }
2749 
2750     if (OutgoingArg->isRegister()) {
2751       RegsToPass.emplace_back(OutgoingArg->getRegister(), InputReg);
2752       if (!CCInfo.AllocateReg(OutgoingArg->getRegister()))
2753         report_fatal_error("failed to allocate implicit input argument");
2754     } else {
2755       unsigned SpecialArgOffset =
2756           CCInfo.AllocateStack(ArgVT.getStoreSize(), Align(4));
2757       SDValue ArgStore = storeStackInputValue(DAG, DL, Chain, InputReg,
2758                                               SpecialArgOffset);
2759       MemOpChains.push_back(ArgStore);
2760     }
2761   }
2762 
2763   // Pack workitem IDs into a single register or pass it as is if already
2764   // packed.
2765   const ArgDescriptor *OutgoingArg;
2766   const TargetRegisterClass *ArgRC;
2767   LLT Ty;
2768 
2769   std::tie(OutgoingArg, ArgRC, Ty) =
2770       CalleeArgInfo->getPreloadedValue(AMDGPUFunctionArgInfo::WORKITEM_ID_X);
2771   if (!OutgoingArg)
2772     std::tie(OutgoingArg, ArgRC, Ty) =
2773         CalleeArgInfo->getPreloadedValue(AMDGPUFunctionArgInfo::WORKITEM_ID_Y);
2774   if (!OutgoingArg)
2775     std::tie(OutgoingArg, ArgRC, Ty) =
2776         CalleeArgInfo->getPreloadedValue(AMDGPUFunctionArgInfo::WORKITEM_ID_Z);
2777   if (!OutgoingArg)
2778     return;
2779 
2780   const ArgDescriptor *IncomingArgX = std::get<0>(
2781       CallerArgInfo.getPreloadedValue(AMDGPUFunctionArgInfo::WORKITEM_ID_X));
2782   const ArgDescriptor *IncomingArgY = std::get<0>(
2783       CallerArgInfo.getPreloadedValue(AMDGPUFunctionArgInfo::WORKITEM_ID_Y));
2784   const ArgDescriptor *IncomingArgZ = std::get<0>(
2785       CallerArgInfo.getPreloadedValue(AMDGPUFunctionArgInfo::WORKITEM_ID_Z));
2786 
2787   SDValue InputReg;
2788   SDLoc SL;
2789 
2790   // If incoming ids are not packed we need to pack them.
2791   if (IncomingArgX && !IncomingArgX->isMasked() && CalleeArgInfo->WorkItemIDX)
2792     InputReg = loadInputValue(DAG, ArgRC, MVT::i32, DL, *IncomingArgX);
2793 
2794   if (IncomingArgY && !IncomingArgY->isMasked() && CalleeArgInfo->WorkItemIDY) {
2795     SDValue Y = loadInputValue(DAG, ArgRC, MVT::i32, DL, *IncomingArgY);
2796     Y = DAG.getNode(ISD::SHL, SL, MVT::i32, Y,
2797                     DAG.getShiftAmountConstant(10, MVT::i32, SL));
2798     InputReg = InputReg.getNode() ?
2799                  DAG.getNode(ISD::OR, SL, MVT::i32, InputReg, Y) : Y;
2800   }
2801 
2802   if (IncomingArgZ && !IncomingArgZ->isMasked() && CalleeArgInfo->WorkItemIDZ) {
2803     SDValue Z = loadInputValue(DAG, ArgRC, MVT::i32, DL, *IncomingArgZ);
2804     Z = DAG.getNode(ISD::SHL, SL, MVT::i32, Z,
2805                     DAG.getShiftAmountConstant(20, MVT::i32, SL));
2806     InputReg = InputReg.getNode() ?
2807                  DAG.getNode(ISD::OR, SL, MVT::i32, InputReg, Z) : Z;
2808   }
2809 
2810   if (!InputReg.getNode()) {
2811     // Workitem ids are already packed, any of present incoming arguments
2812     // will carry all required fields.
2813     ArgDescriptor IncomingArg = ArgDescriptor::createArg(
2814       IncomingArgX ? *IncomingArgX :
2815       IncomingArgY ? *IncomingArgY :
2816                      *IncomingArgZ, ~0u);
2817     InputReg = loadInputValue(DAG, ArgRC, MVT::i32, DL, IncomingArg);
2818   }
2819 
2820   if (OutgoingArg->isRegister()) {
2821     RegsToPass.emplace_back(OutgoingArg->getRegister(), InputReg);
2822     CCInfo.AllocateReg(OutgoingArg->getRegister());
2823   } else {
2824     unsigned SpecialArgOffset = CCInfo.AllocateStack(4, Align(4));
2825     SDValue ArgStore = storeStackInputValue(DAG, DL, Chain, InputReg,
2826                                             SpecialArgOffset);
2827     MemOpChains.push_back(ArgStore);
2828   }
2829 }
2830 
2831 static bool canGuaranteeTCO(CallingConv::ID CC) {
2832   return CC == CallingConv::Fast;
2833 }
2834 
2835 /// Return true if we might ever do TCO for calls with this calling convention.
2836 static bool mayTailCallThisCC(CallingConv::ID CC) {
2837   switch (CC) {
2838   case CallingConv::C:
2839   case CallingConv::AMDGPU_Gfx:
2840     return true;
2841   default:
2842     return canGuaranteeTCO(CC);
2843   }
2844 }
2845 
2846 bool SITargetLowering::isEligibleForTailCallOptimization(
2847     SDValue Callee, CallingConv::ID CalleeCC, bool IsVarArg,
2848     const SmallVectorImpl<ISD::OutputArg> &Outs,
2849     const SmallVectorImpl<SDValue> &OutVals,
2850     const SmallVectorImpl<ISD::InputArg> &Ins, SelectionDAG &DAG) const {
2851   if (!mayTailCallThisCC(CalleeCC))
2852     return false;
2853 
2854   // For a divergent call target, we need to do a waterfall loop over the
2855   // possible callees which precludes us from using a simple jump.
2856   if (Callee->isDivergent())
2857     return false;
2858 
2859   MachineFunction &MF = DAG.getMachineFunction();
2860   const Function &CallerF = MF.getFunction();
2861   CallingConv::ID CallerCC = CallerF.getCallingConv();
2862   const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
2863   const uint32_t *CallerPreserved = TRI->getCallPreservedMask(MF, CallerCC);
2864 
2865   // Kernels aren't callable, and don't have a live in return address so it
2866   // doesn't make sense to do a tail call with entry functions.
2867   if (!CallerPreserved)
2868     return false;
2869 
2870   bool CCMatch = CallerCC == CalleeCC;
2871 
2872   if (DAG.getTarget().Options.GuaranteedTailCallOpt) {
2873     if (canGuaranteeTCO(CalleeCC) && CCMatch)
2874       return true;
2875     return false;
2876   }
2877 
2878   // TODO: Can we handle var args?
2879   if (IsVarArg)
2880     return false;
2881 
2882   for (const Argument &Arg : CallerF.args()) {
2883     if (Arg.hasByValAttr())
2884       return false;
2885   }
2886 
2887   LLVMContext &Ctx = *DAG.getContext();
2888 
2889   // Check that the call results are passed in the same way.
2890   if (!CCState::resultsCompatible(CalleeCC, CallerCC, MF, Ctx, Ins,
2891                                   CCAssignFnForCall(CalleeCC, IsVarArg),
2892                                   CCAssignFnForCall(CallerCC, IsVarArg)))
2893     return false;
2894 
2895   // The callee has to preserve all registers the caller needs to preserve.
2896   if (!CCMatch) {
2897     const uint32_t *CalleePreserved = TRI->getCallPreservedMask(MF, CalleeCC);
2898     if (!TRI->regmaskSubsetEqual(CallerPreserved, CalleePreserved))
2899       return false;
2900   }
2901 
2902   // Nothing more to check if the callee is taking no arguments.
2903   if (Outs.empty())
2904     return true;
2905 
2906   SmallVector<CCValAssign, 16> ArgLocs;
2907   CCState CCInfo(CalleeCC, IsVarArg, MF, ArgLocs, Ctx);
2908 
2909   CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForCall(CalleeCC, IsVarArg));
2910 
2911   const SIMachineFunctionInfo *FuncInfo = MF.getInfo<SIMachineFunctionInfo>();
2912   // If the stack arguments for this call do not fit into our own save area then
2913   // the call cannot be made tail.
2914   // TODO: Is this really necessary?
2915   if (CCInfo.getNextStackOffset() > FuncInfo->getBytesInStackArgArea())
2916     return false;
2917 
2918   const MachineRegisterInfo &MRI = MF.getRegInfo();
2919   return parametersInCSRMatch(MRI, CallerPreserved, ArgLocs, OutVals);
2920 }
2921 
2922 bool SITargetLowering::mayBeEmittedAsTailCall(const CallInst *CI) const {
2923   if (!CI->isTailCall())
2924     return false;
2925 
2926   const Function *ParentFn = CI->getParent()->getParent();
2927   if (AMDGPU::isEntryFunctionCC(ParentFn->getCallingConv()))
2928     return false;
2929   return true;
2930 }
2931 
2932 // The wave scratch offset register is used as the global base pointer.
2933 SDValue SITargetLowering::LowerCall(CallLoweringInfo &CLI,
2934                                     SmallVectorImpl<SDValue> &InVals) const {
2935   SelectionDAG &DAG = CLI.DAG;
2936   const SDLoc &DL = CLI.DL;
2937   SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2938   SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2939   SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2940   SDValue Chain = CLI.Chain;
2941   SDValue Callee = CLI.Callee;
2942   bool &IsTailCall = CLI.IsTailCall;
2943   CallingConv::ID CallConv = CLI.CallConv;
2944   bool IsVarArg = CLI.IsVarArg;
2945   bool IsSibCall = false;
2946   bool IsThisReturn = false;
2947   MachineFunction &MF = DAG.getMachineFunction();
2948 
2949   if (Callee.isUndef() || isNullConstant(Callee)) {
2950     if (!CLI.IsTailCall) {
2951       for (unsigned I = 0, E = CLI.Ins.size(); I != E; ++I)
2952         InVals.push_back(DAG.getUNDEF(CLI.Ins[I].VT));
2953     }
2954 
2955     return Chain;
2956   }
2957 
2958   if (IsVarArg) {
2959     return lowerUnhandledCall(CLI, InVals,
2960                               "unsupported call to variadic function ");
2961   }
2962 
2963   if (!CLI.CB)
2964     report_fatal_error("unsupported libcall legalization");
2965 
2966   if (IsTailCall && MF.getTarget().Options.GuaranteedTailCallOpt) {
2967     return lowerUnhandledCall(CLI, InVals,
2968                               "unsupported required tail call to function ");
2969   }
2970 
2971   if (AMDGPU::isShader(CallConv)) {
2972     // Note the issue is with the CC of the called function, not of the call
2973     // itself.
2974     return lowerUnhandledCall(CLI, InVals,
2975                               "unsupported call to a shader function ");
2976   }
2977 
2978   if (AMDGPU::isShader(MF.getFunction().getCallingConv()) &&
2979       CallConv != CallingConv::AMDGPU_Gfx) {
2980     // Only allow calls with specific calling conventions.
2981     return lowerUnhandledCall(CLI, InVals,
2982                               "unsupported calling convention for call from "
2983                               "graphics shader of function ");
2984   }
2985 
2986   if (IsTailCall) {
2987     IsTailCall = isEligibleForTailCallOptimization(
2988       Callee, CallConv, IsVarArg, Outs, OutVals, Ins, DAG);
2989     if (!IsTailCall && CLI.CB && CLI.CB->isMustTailCall()) {
2990       report_fatal_error("failed to perform tail call elimination on a call "
2991                          "site marked musttail");
2992     }
2993 
2994     bool TailCallOpt = MF.getTarget().Options.GuaranteedTailCallOpt;
2995 
2996     // A sibling call is one where we're under the usual C ABI and not planning
2997     // to change that but can still do a tail call:
2998     if (!TailCallOpt && IsTailCall)
2999       IsSibCall = true;
3000 
3001     if (IsTailCall)
3002       ++NumTailCalls;
3003   }
3004 
3005   const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
3006   SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
3007   SmallVector<SDValue, 8> MemOpChains;
3008 
3009   // Analyze operands of the call, assigning locations to each operand.
3010   SmallVector<CCValAssign, 16> ArgLocs;
3011   CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
3012   CCAssignFn *AssignFn = CCAssignFnForCall(CallConv, IsVarArg);
3013 
3014   if (AMDGPUTargetMachine::EnableFixedFunctionABI &&
3015       CallConv != CallingConv::AMDGPU_Gfx) {
3016     // With a fixed ABI, allocate fixed registers before user arguments.
3017     passSpecialInputs(CLI, CCInfo, *Info, RegsToPass, MemOpChains, Chain);
3018   }
3019 
3020   CCInfo.AnalyzeCallOperands(Outs, AssignFn);
3021 
3022   // Get a count of how many bytes are to be pushed on the stack.
3023   unsigned NumBytes = CCInfo.getNextStackOffset();
3024 
3025   if (IsSibCall) {
3026     // Since we're not changing the ABI to make this a tail call, the memory
3027     // operands are already available in the caller's incoming argument space.
3028     NumBytes = 0;
3029   }
3030 
3031   // FPDiff is the byte offset of the call's argument area from the callee's.
3032   // Stores to callee stack arguments will be placed in FixedStackSlots offset
3033   // by this amount for a tail call. In a sibling call it must be 0 because the
3034   // caller will deallocate the entire stack and the callee still expects its
3035   // arguments to begin at SP+0. Completely unused for non-tail calls.
3036   int32_t FPDiff = 0;
3037   MachineFrameInfo &MFI = MF.getFrameInfo();
3038 
3039   // Adjust the stack pointer for the new arguments...
3040   // These operations are automatically eliminated by the prolog/epilog pass
3041   if (!IsSibCall) {
3042     Chain = DAG.getCALLSEQ_START(Chain, 0, 0, DL);
3043 
3044     if (!Subtarget->enableFlatScratch()) {
3045       SmallVector<SDValue, 4> CopyFromChains;
3046 
3047       // In the HSA case, this should be an identity copy.
3048       SDValue ScratchRSrcReg
3049         = DAG.getCopyFromReg(Chain, DL, Info->getScratchRSrcReg(), MVT::v4i32);
3050       RegsToPass.emplace_back(AMDGPU::SGPR0_SGPR1_SGPR2_SGPR3, ScratchRSrcReg);
3051       CopyFromChains.push_back(ScratchRSrcReg.getValue(1));
3052       Chain = DAG.getTokenFactor(DL, CopyFromChains);
3053     }
3054   }
3055 
3056   MVT PtrVT = MVT::i32;
3057 
3058   // Walk the register/memloc assignments, inserting copies/loads.
3059   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3060     CCValAssign &VA = ArgLocs[i];
3061     SDValue Arg = OutVals[i];
3062 
3063     // Promote the value if needed.
3064     switch (VA.getLocInfo()) {
3065     case CCValAssign::Full:
3066       break;
3067     case CCValAssign::BCvt:
3068       Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
3069       break;
3070     case CCValAssign::ZExt:
3071       Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg);
3072       break;
3073     case CCValAssign::SExt:
3074       Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg);
3075       break;
3076     case CCValAssign::AExt:
3077       Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg);
3078       break;
3079     case CCValAssign::FPExt:
3080       Arg = DAG.getNode(ISD::FP_EXTEND, DL, VA.getLocVT(), Arg);
3081       break;
3082     default:
3083       llvm_unreachable("Unknown loc info!");
3084     }
3085 
3086     if (VA.isRegLoc()) {
3087       RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
3088     } else {
3089       assert(VA.isMemLoc());
3090 
3091       SDValue DstAddr;
3092       MachinePointerInfo DstInfo;
3093 
3094       unsigned LocMemOffset = VA.getLocMemOffset();
3095       int32_t Offset = LocMemOffset;
3096 
3097       SDValue PtrOff = DAG.getConstant(Offset, DL, PtrVT);
3098       MaybeAlign Alignment;
3099 
3100       if (IsTailCall) {
3101         ISD::ArgFlagsTy Flags = Outs[i].Flags;
3102         unsigned OpSize = Flags.isByVal() ?
3103           Flags.getByValSize() : VA.getValVT().getStoreSize();
3104 
3105         // FIXME: We can have better than the minimum byval required alignment.
3106         Alignment =
3107             Flags.isByVal()
3108                 ? Flags.getNonZeroByValAlign()
3109                 : commonAlignment(Subtarget->getStackAlignment(), Offset);
3110 
3111         Offset = Offset + FPDiff;
3112         int FI = MFI.CreateFixedObject(OpSize, Offset, true);
3113 
3114         DstAddr = DAG.getFrameIndex(FI, PtrVT);
3115         DstInfo = MachinePointerInfo::getFixedStack(MF, FI);
3116 
3117         // Make sure any stack arguments overlapping with where we're storing
3118         // are loaded before this eventual operation. Otherwise they'll be
3119         // clobbered.
3120 
3121         // FIXME: Why is this really necessary? This seems to just result in a
3122         // lot of code to copy the stack and write them back to the same
3123         // locations, which are supposed to be immutable?
3124         Chain = addTokenForArgument(Chain, DAG, MFI, FI);
3125       } else {
3126         DstAddr = PtrOff;
3127         DstInfo = MachinePointerInfo::getStack(MF, LocMemOffset);
3128         Alignment =
3129             commonAlignment(Subtarget->getStackAlignment(), LocMemOffset);
3130       }
3131 
3132       if (Outs[i].Flags.isByVal()) {
3133         SDValue SizeNode =
3134             DAG.getConstant(Outs[i].Flags.getByValSize(), DL, MVT::i32);
3135         SDValue Cpy =
3136             DAG.getMemcpy(Chain, DL, DstAddr, Arg, SizeNode,
3137                           Outs[i].Flags.getNonZeroByValAlign(),
3138                           /*isVol = */ false, /*AlwaysInline = */ true,
3139                           /*isTailCall = */ false, DstInfo,
3140                           MachinePointerInfo(AMDGPUAS::PRIVATE_ADDRESS));
3141 
3142         MemOpChains.push_back(Cpy);
3143       } else {
3144         SDValue Store =
3145             DAG.getStore(Chain, DL, Arg, DstAddr, DstInfo, Alignment);
3146         MemOpChains.push_back(Store);
3147       }
3148     }
3149   }
3150 
3151   if (!AMDGPUTargetMachine::EnableFixedFunctionABI &&
3152       CallConv != CallingConv::AMDGPU_Gfx) {
3153     // Copy special input registers after user input arguments.
3154     passSpecialInputs(CLI, CCInfo, *Info, RegsToPass, MemOpChains, Chain);
3155   }
3156 
3157   if (!MemOpChains.empty())
3158     Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
3159 
3160   // Build a sequence of copy-to-reg nodes chained together with token chain
3161   // and flag operands which copy the outgoing args into the appropriate regs.
3162   SDValue InFlag;
3163   for (auto &RegToPass : RegsToPass) {
3164     Chain = DAG.getCopyToReg(Chain, DL, RegToPass.first,
3165                              RegToPass.second, InFlag);
3166     InFlag = Chain.getValue(1);
3167   }
3168 
3169 
3170   SDValue PhysReturnAddrReg;
3171   if (IsTailCall) {
3172     // Since the return is being combined with the call, we need to pass on the
3173     // return address.
3174 
3175     const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
3176     SDValue ReturnAddrReg = CreateLiveInRegister(
3177       DAG, &AMDGPU::SReg_64RegClass, TRI->getReturnAddressReg(MF), MVT::i64);
3178 
3179     PhysReturnAddrReg = DAG.getRegister(TRI->getReturnAddressReg(MF),
3180                                         MVT::i64);
3181     Chain = DAG.getCopyToReg(Chain, DL, PhysReturnAddrReg, ReturnAddrReg, InFlag);
3182     InFlag = Chain.getValue(1);
3183   }
3184 
3185   // We don't usually want to end the call-sequence here because we would tidy
3186   // the frame up *after* the call, however in the ABI-changing tail-call case
3187   // we've carefully laid out the parameters so that when sp is reset they'll be
3188   // in the correct location.
3189   if (IsTailCall && !IsSibCall) {
3190     Chain = DAG.getCALLSEQ_END(Chain,
3191                                DAG.getTargetConstant(NumBytes, DL, MVT::i32),
3192                                DAG.getTargetConstant(0, DL, MVT::i32),
3193                                InFlag, DL);
3194     InFlag = Chain.getValue(1);
3195   }
3196 
3197   std::vector<SDValue> Ops;
3198   Ops.push_back(Chain);
3199   Ops.push_back(Callee);
3200   // Add a redundant copy of the callee global which will not be legalized, as
3201   // we need direct access to the callee later.
3202   if (GlobalAddressSDNode *GSD = dyn_cast<GlobalAddressSDNode>(Callee)) {
3203     const GlobalValue *GV = GSD->getGlobal();
3204     Ops.push_back(DAG.getTargetGlobalAddress(GV, DL, MVT::i64));
3205   } else {
3206     Ops.push_back(DAG.getTargetConstant(0, DL, MVT::i64));
3207   }
3208 
3209   if (IsTailCall) {
3210     // Each tail call may have to adjust the stack by a different amount, so
3211     // this information must travel along with the operation for eventual
3212     // consumption by emitEpilogue.
3213     Ops.push_back(DAG.getTargetConstant(FPDiff, DL, MVT::i32));
3214 
3215     Ops.push_back(PhysReturnAddrReg);
3216   }
3217 
3218   // Add argument registers to the end of the list so that they are known live
3219   // into the call.
3220   for (auto &RegToPass : RegsToPass) {
3221     Ops.push_back(DAG.getRegister(RegToPass.first,
3222                                   RegToPass.second.getValueType()));
3223   }
3224 
3225   // Add a register mask operand representing the call-preserved registers.
3226 
3227   auto *TRI = static_cast<const SIRegisterInfo*>(Subtarget->getRegisterInfo());
3228   const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv);
3229   assert(Mask && "Missing call preserved mask for calling convention");
3230   Ops.push_back(DAG.getRegisterMask(Mask));
3231 
3232   if (InFlag.getNode())
3233     Ops.push_back(InFlag);
3234 
3235   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
3236 
3237   // If we're doing a tall call, use a TC_RETURN here rather than an
3238   // actual call instruction.
3239   if (IsTailCall) {
3240     MFI.setHasTailCall();
3241     return DAG.getNode(AMDGPUISD::TC_RETURN, DL, NodeTys, Ops);
3242   }
3243 
3244   // Returns a chain and a flag for retval copy to use.
3245   SDValue Call = DAG.getNode(AMDGPUISD::CALL, DL, NodeTys, Ops);
3246   Chain = Call.getValue(0);
3247   InFlag = Call.getValue(1);
3248 
3249   uint64_t CalleePopBytes = NumBytes;
3250   Chain = DAG.getCALLSEQ_END(Chain, DAG.getTargetConstant(0, DL, MVT::i32),
3251                              DAG.getTargetConstant(CalleePopBytes, DL, MVT::i32),
3252                              InFlag, DL);
3253   if (!Ins.empty())
3254     InFlag = Chain.getValue(1);
3255 
3256   // Handle result values, copying them out of physregs into vregs that we
3257   // return.
3258   return LowerCallResult(Chain, InFlag, CallConv, IsVarArg, Ins, DL, DAG,
3259                          InVals, IsThisReturn,
3260                          IsThisReturn ? OutVals[0] : SDValue());
3261 }
3262 
3263 // This is identical to the default implementation in ExpandDYNAMIC_STACKALLOC,
3264 // except for applying the wave size scale to the increment amount.
3265 SDValue SITargetLowering::lowerDYNAMIC_STACKALLOCImpl(
3266     SDValue Op, SelectionDAG &DAG) const {
3267   const MachineFunction &MF = DAG.getMachineFunction();
3268   const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
3269 
3270   SDLoc dl(Op);
3271   EVT VT = Op.getValueType();
3272   SDValue Tmp1 = Op;
3273   SDValue Tmp2 = Op.getValue(1);
3274   SDValue Tmp3 = Op.getOperand(2);
3275   SDValue Chain = Tmp1.getOperand(0);
3276 
3277   Register SPReg = Info->getStackPtrOffsetReg();
3278 
3279   // Chain the dynamic stack allocation so that it doesn't modify the stack
3280   // pointer when other instructions are using the stack.
3281   Chain = DAG.getCALLSEQ_START(Chain, 0, 0, dl);
3282 
3283   SDValue Size  = Tmp2.getOperand(1);
3284   SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
3285   Chain = SP.getValue(1);
3286   MaybeAlign Alignment = cast<ConstantSDNode>(Tmp3)->getMaybeAlignValue();
3287   const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
3288   const TargetFrameLowering *TFL = ST.getFrameLowering();
3289   unsigned Opc =
3290     TFL->getStackGrowthDirection() == TargetFrameLowering::StackGrowsUp ?
3291     ISD::ADD : ISD::SUB;
3292 
3293   SDValue ScaledSize = DAG.getNode(
3294       ISD::SHL, dl, VT, Size,
3295       DAG.getConstant(ST.getWavefrontSizeLog2(), dl, MVT::i32));
3296 
3297   Align StackAlign = TFL->getStackAlign();
3298   Tmp1 = DAG.getNode(Opc, dl, VT, SP, ScaledSize); // Value
3299   if (Alignment && *Alignment > StackAlign) {
3300     Tmp1 = DAG.getNode(ISD::AND, dl, VT, Tmp1,
3301                        DAG.getConstant(-(uint64_t)Alignment->value()
3302                                            << ST.getWavefrontSizeLog2(),
3303                                        dl, VT));
3304   }
3305 
3306   Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1);    // Output chain
3307   Tmp2 = DAG.getCALLSEQ_END(
3308       Chain, DAG.getIntPtrConstant(0, dl, true),
3309       DAG.getIntPtrConstant(0, dl, true), SDValue(), dl);
3310 
3311   return DAG.getMergeValues({Tmp1, Tmp2}, dl);
3312 }
3313 
3314 SDValue SITargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
3315                                                   SelectionDAG &DAG) const {
3316   // We only handle constant sizes here to allow non-entry block, static sized
3317   // allocas. A truly dynamic value is more difficult to support because we
3318   // don't know if the size value is uniform or not. If the size isn't uniform,
3319   // we would need to do a wave reduction to get the maximum size to know how
3320   // much to increment the uniform stack pointer.
3321   SDValue Size = Op.getOperand(1);
3322   if (isa<ConstantSDNode>(Size))
3323       return lowerDYNAMIC_STACKALLOCImpl(Op, DAG); // Use "generic" expansion.
3324 
3325   return AMDGPUTargetLowering::LowerDYNAMIC_STACKALLOC(Op, DAG);
3326 }
3327 
3328 Register SITargetLowering::getRegisterByName(const char* RegName, LLT VT,
3329                                              const MachineFunction &MF) const {
3330   Register Reg = StringSwitch<Register>(RegName)
3331     .Case("m0", AMDGPU::M0)
3332     .Case("exec", AMDGPU::EXEC)
3333     .Case("exec_lo", AMDGPU::EXEC_LO)
3334     .Case("exec_hi", AMDGPU::EXEC_HI)
3335     .Case("flat_scratch", AMDGPU::FLAT_SCR)
3336     .Case("flat_scratch_lo", AMDGPU::FLAT_SCR_LO)
3337     .Case("flat_scratch_hi", AMDGPU::FLAT_SCR_HI)
3338     .Default(Register());
3339 
3340   if (Reg == AMDGPU::NoRegister) {
3341     report_fatal_error(Twine("invalid register name \""
3342                              + StringRef(RegName)  + "\"."));
3343 
3344   }
3345 
3346   if (!Subtarget->hasFlatScrRegister() &&
3347        Subtarget->getRegisterInfo()->regsOverlap(Reg, AMDGPU::FLAT_SCR)) {
3348     report_fatal_error(Twine("invalid register \""
3349                              + StringRef(RegName)  + "\" for subtarget."));
3350   }
3351 
3352   switch (Reg) {
3353   case AMDGPU::M0:
3354   case AMDGPU::EXEC_LO:
3355   case AMDGPU::EXEC_HI:
3356   case AMDGPU::FLAT_SCR_LO:
3357   case AMDGPU::FLAT_SCR_HI:
3358     if (VT.getSizeInBits() == 32)
3359       return Reg;
3360     break;
3361   case AMDGPU::EXEC:
3362   case AMDGPU::FLAT_SCR:
3363     if (VT.getSizeInBits() == 64)
3364       return Reg;
3365     break;
3366   default:
3367     llvm_unreachable("missing register type checking");
3368   }
3369 
3370   report_fatal_error(Twine("invalid type for register \""
3371                            + StringRef(RegName) + "\"."));
3372 }
3373 
3374 // If kill is not the last instruction, split the block so kill is always a
3375 // proper terminator.
3376 MachineBasicBlock *
3377 SITargetLowering::splitKillBlock(MachineInstr &MI,
3378                                  MachineBasicBlock *BB) const {
3379   MachineBasicBlock *SplitBB = BB->splitAt(MI, false /*UpdateLiveIns*/);
3380   const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
3381   MI.setDesc(TII->getKillTerminatorFromPseudo(MI.getOpcode()));
3382   return SplitBB;
3383 }
3384 
3385 // Split block \p MBB at \p MI, as to insert a loop. If \p InstInLoop is true,
3386 // \p MI will be the only instruction in the loop body block. Otherwise, it will
3387 // be the first instruction in the remainder block.
3388 //
3389 /// \returns { LoopBody, Remainder }
3390 static std::pair<MachineBasicBlock *, MachineBasicBlock *>
3391 splitBlockForLoop(MachineInstr &MI, MachineBasicBlock &MBB, bool InstInLoop) {
3392   MachineFunction *MF = MBB.getParent();
3393   MachineBasicBlock::iterator I(&MI);
3394 
3395   // To insert the loop we need to split the block. Move everything after this
3396   // point to a new block, and insert a new empty block between the two.
3397   MachineBasicBlock *LoopBB = MF->CreateMachineBasicBlock();
3398   MachineBasicBlock *RemainderBB = MF->CreateMachineBasicBlock();
3399   MachineFunction::iterator MBBI(MBB);
3400   ++MBBI;
3401 
3402   MF->insert(MBBI, LoopBB);
3403   MF->insert(MBBI, RemainderBB);
3404 
3405   LoopBB->addSuccessor(LoopBB);
3406   LoopBB->addSuccessor(RemainderBB);
3407 
3408   // Move the rest of the block into a new block.
3409   RemainderBB->transferSuccessorsAndUpdatePHIs(&MBB);
3410 
3411   if (InstInLoop) {
3412     auto Next = std::next(I);
3413 
3414     // Move instruction to loop body.
3415     LoopBB->splice(LoopBB->begin(), &MBB, I, Next);
3416 
3417     // Move the rest of the block.
3418     RemainderBB->splice(RemainderBB->begin(), &MBB, Next, MBB.end());
3419   } else {
3420     RemainderBB->splice(RemainderBB->begin(), &MBB, I, MBB.end());
3421   }
3422 
3423   MBB.addSuccessor(LoopBB);
3424 
3425   return std::make_pair(LoopBB, RemainderBB);
3426 }
3427 
3428 /// Insert \p MI into a BUNDLE with an S_WAITCNT 0 immediately following it.
3429 void SITargetLowering::bundleInstWithWaitcnt(MachineInstr &MI) const {
3430   MachineBasicBlock *MBB = MI.getParent();
3431   const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
3432   auto I = MI.getIterator();
3433   auto E = std::next(I);
3434 
3435   BuildMI(*MBB, E, MI.getDebugLoc(), TII->get(AMDGPU::S_WAITCNT))
3436     .addImm(0);
3437 
3438   MIBundleBuilder Bundler(*MBB, I, E);
3439   finalizeBundle(*MBB, Bundler.begin());
3440 }
3441 
3442 MachineBasicBlock *
3443 SITargetLowering::emitGWSMemViolTestLoop(MachineInstr &MI,
3444                                          MachineBasicBlock *BB) const {
3445   const DebugLoc &DL = MI.getDebugLoc();
3446 
3447   MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
3448 
3449   MachineBasicBlock *LoopBB;
3450   MachineBasicBlock *RemainderBB;
3451   const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
3452 
3453   // Apparently kill flags are only valid if the def is in the same block?
3454   if (MachineOperand *Src = TII->getNamedOperand(MI, AMDGPU::OpName::data0))
3455     Src->setIsKill(false);
3456 
3457   std::tie(LoopBB, RemainderBB) = splitBlockForLoop(MI, *BB, true);
3458 
3459   MachineBasicBlock::iterator I = LoopBB->end();
3460 
3461   const unsigned EncodedReg = AMDGPU::Hwreg::encodeHwreg(
3462     AMDGPU::Hwreg::ID_TRAPSTS, AMDGPU::Hwreg::OFFSET_MEM_VIOL, 1);
3463 
3464   // Clear TRAP_STS.MEM_VIOL
3465   BuildMI(*LoopBB, LoopBB->begin(), DL, TII->get(AMDGPU::S_SETREG_IMM32_B32))
3466     .addImm(0)
3467     .addImm(EncodedReg);
3468 
3469   bundleInstWithWaitcnt(MI);
3470 
3471   Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
3472 
3473   // Load and check TRAP_STS.MEM_VIOL
3474   BuildMI(*LoopBB, I, DL, TII->get(AMDGPU::S_GETREG_B32), Reg)
3475     .addImm(EncodedReg);
3476 
3477   // FIXME: Do we need to use an isel pseudo that may clobber scc?
3478   BuildMI(*LoopBB, I, DL, TII->get(AMDGPU::S_CMP_LG_U32))
3479     .addReg(Reg, RegState::Kill)
3480     .addImm(0);
3481   BuildMI(*LoopBB, I, DL, TII->get(AMDGPU::S_CBRANCH_SCC1))
3482     .addMBB(LoopBB);
3483 
3484   return RemainderBB;
3485 }
3486 
3487 // Do a v_movrels_b32 or v_movreld_b32 for each unique value of \p IdxReg in the
3488 // wavefront. If the value is uniform and just happens to be in a VGPR, this
3489 // will only do one iteration. In the worst case, this will loop 64 times.
3490 //
3491 // TODO: Just use v_readlane_b32 if we know the VGPR has a uniform value.
3492 static MachineBasicBlock::iterator
3493 emitLoadM0FromVGPRLoop(const SIInstrInfo *TII, MachineRegisterInfo &MRI,
3494                        MachineBasicBlock &OrigBB, MachineBasicBlock &LoopBB,
3495                        const DebugLoc &DL, const MachineOperand &Idx,
3496                        unsigned InitReg, unsigned ResultReg, unsigned PhiReg,
3497                        unsigned InitSaveExecReg, int Offset, bool UseGPRIdxMode,
3498                        Register &SGPRIdxReg) {
3499 
3500   MachineFunction *MF = OrigBB.getParent();
3501   const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>();
3502   const SIRegisterInfo *TRI = ST.getRegisterInfo();
3503   MachineBasicBlock::iterator I = LoopBB.begin();
3504 
3505   const TargetRegisterClass *BoolRC = TRI->getBoolRC();
3506   Register PhiExec = MRI.createVirtualRegister(BoolRC);
3507   Register NewExec = MRI.createVirtualRegister(BoolRC);
3508   Register CurrentIdxReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
3509   Register CondReg = MRI.createVirtualRegister(BoolRC);
3510 
3511   BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiReg)
3512     .addReg(InitReg)
3513     .addMBB(&OrigBB)
3514     .addReg(ResultReg)
3515     .addMBB(&LoopBB);
3516 
3517   BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiExec)
3518     .addReg(InitSaveExecReg)
3519     .addMBB(&OrigBB)
3520     .addReg(NewExec)
3521     .addMBB(&LoopBB);
3522 
3523   // Read the next variant <- also loop target.
3524   BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32), CurrentIdxReg)
3525       .addReg(Idx.getReg(), getUndefRegState(Idx.isUndef()));
3526 
3527   // Compare the just read M0 value to all possible Idx values.
3528   BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_CMP_EQ_U32_e64), CondReg)
3529       .addReg(CurrentIdxReg)
3530       .addReg(Idx.getReg(), 0, Idx.getSubReg());
3531 
3532   // Update EXEC, save the original EXEC value to VCC.
3533   BuildMI(LoopBB, I, DL, TII->get(ST.isWave32() ? AMDGPU::S_AND_SAVEEXEC_B32
3534                                                 : AMDGPU::S_AND_SAVEEXEC_B64),
3535           NewExec)
3536     .addReg(CondReg, RegState::Kill);
3537 
3538   MRI.setSimpleHint(NewExec, CondReg);
3539 
3540   if (UseGPRIdxMode) {
3541     if (Offset == 0) {
3542       SGPRIdxReg = CurrentIdxReg;
3543     } else {
3544       SGPRIdxReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
3545       BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_ADD_I32), SGPRIdxReg)
3546           .addReg(CurrentIdxReg, RegState::Kill)
3547           .addImm(Offset);
3548     }
3549   } else {
3550     // Move index from VCC into M0
3551     if (Offset == 0) {
3552       BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
3553         .addReg(CurrentIdxReg, RegState::Kill);
3554     } else {
3555       BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
3556         .addReg(CurrentIdxReg, RegState::Kill)
3557         .addImm(Offset);
3558     }
3559   }
3560 
3561   // Update EXEC, switch all done bits to 0 and all todo bits to 1.
3562   unsigned Exec = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC;
3563   MachineInstr *InsertPt =
3564     BuildMI(LoopBB, I, DL, TII->get(ST.isWave32() ? AMDGPU::S_XOR_B32_term
3565                                                   : AMDGPU::S_XOR_B64_term), Exec)
3566       .addReg(Exec)
3567       .addReg(NewExec);
3568 
3569   // XXX - s_xor_b64 sets scc to 1 if the result is nonzero, so can we use
3570   // s_cbranch_scc0?
3571 
3572   // Loop back to V_READFIRSTLANE_B32 if there are still variants to cover.
3573   BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
3574     .addMBB(&LoopBB);
3575 
3576   return InsertPt->getIterator();
3577 }
3578 
3579 // This has slightly sub-optimal regalloc when the source vector is killed by
3580 // the read. The register allocator does not understand that the kill is
3581 // per-workitem, so is kept alive for the whole loop so we end up not re-using a
3582 // subregister from it, using 1 more VGPR than necessary. This was saved when
3583 // this was expanded after register allocation.
3584 static MachineBasicBlock::iterator
3585 loadM0FromVGPR(const SIInstrInfo *TII, MachineBasicBlock &MBB, MachineInstr &MI,
3586                unsigned InitResultReg, unsigned PhiReg, int Offset,
3587                bool UseGPRIdxMode, Register &SGPRIdxReg) {
3588   MachineFunction *MF = MBB.getParent();
3589   const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>();
3590   const SIRegisterInfo *TRI = ST.getRegisterInfo();
3591   MachineRegisterInfo &MRI = MF->getRegInfo();
3592   const DebugLoc &DL = MI.getDebugLoc();
3593   MachineBasicBlock::iterator I(&MI);
3594 
3595   const auto *BoolXExecRC = TRI->getRegClass(AMDGPU::SReg_1_XEXECRegClassID);
3596   Register DstReg = MI.getOperand(0).getReg();
3597   Register SaveExec = MRI.createVirtualRegister(BoolXExecRC);
3598   Register TmpExec = MRI.createVirtualRegister(BoolXExecRC);
3599   unsigned Exec = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC;
3600   unsigned MovExecOpc = ST.isWave32() ? AMDGPU::S_MOV_B32 : AMDGPU::S_MOV_B64;
3601 
3602   BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), TmpExec);
3603 
3604   // Save the EXEC mask
3605   BuildMI(MBB, I, DL, TII->get(MovExecOpc), SaveExec)
3606     .addReg(Exec);
3607 
3608   MachineBasicBlock *LoopBB;
3609   MachineBasicBlock *RemainderBB;
3610   std::tie(LoopBB, RemainderBB) = splitBlockForLoop(MI, MBB, false);
3611 
3612   const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
3613 
3614   auto InsPt = emitLoadM0FromVGPRLoop(TII, MRI, MBB, *LoopBB, DL, *Idx,
3615                                       InitResultReg, DstReg, PhiReg, TmpExec,
3616                                       Offset, UseGPRIdxMode, SGPRIdxReg);
3617 
3618   MachineBasicBlock* LandingPad = MF->CreateMachineBasicBlock();
3619   MachineFunction::iterator MBBI(LoopBB);
3620   ++MBBI;
3621   MF->insert(MBBI, LandingPad);
3622   LoopBB->removeSuccessor(RemainderBB);
3623   LandingPad->addSuccessor(RemainderBB);
3624   LoopBB->addSuccessor(LandingPad);
3625   MachineBasicBlock::iterator First = LandingPad->begin();
3626   BuildMI(*LandingPad, First, DL, TII->get(MovExecOpc), Exec)
3627     .addReg(SaveExec);
3628 
3629   return InsPt;
3630 }
3631 
3632 // Returns subreg index, offset
3633 static std::pair<unsigned, int>
3634 computeIndirectRegAndOffset(const SIRegisterInfo &TRI,
3635                             const TargetRegisterClass *SuperRC,
3636                             unsigned VecReg,
3637                             int Offset) {
3638   int NumElts = TRI.getRegSizeInBits(*SuperRC) / 32;
3639 
3640   // Skip out of bounds offsets, or else we would end up using an undefined
3641   // register.
3642   if (Offset >= NumElts || Offset < 0)
3643     return std::make_pair(AMDGPU::sub0, Offset);
3644 
3645   return std::make_pair(SIRegisterInfo::getSubRegFromChannel(Offset), 0);
3646 }
3647 
3648 static void setM0ToIndexFromSGPR(const SIInstrInfo *TII,
3649                                  MachineRegisterInfo &MRI, MachineInstr &MI,
3650                                  int Offset) {
3651   MachineBasicBlock *MBB = MI.getParent();
3652   const DebugLoc &DL = MI.getDebugLoc();
3653   MachineBasicBlock::iterator I(&MI);
3654 
3655   const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
3656 
3657   assert(Idx->getReg() != AMDGPU::NoRegister);
3658 
3659   if (Offset == 0) {
3660     BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0).add(*Idx);
3661   } else {
3662     BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
3663         .add(*Idx)
3664         .addImm(Offset);
3665   }
3666 }
3667 
3668 static Register getIndirectSGPRIdx(const SIInstrInfo *TII,
3669                                    MachineRegisterInfo &MRI, MachineInstr &MI,
3670                                    int Offset) {
3671   MachineBasicBlock *MBB = MI.getParent();
3672   const DebugLoc &DL = MI.getDebugLoc();
3673   MachineBasicBlock::iterator I(&MI);
3674 
3675   const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
3676 
3677   if (Offset == 0)
3678     return Idx->getReg();
3679 
3680   Register Tmp = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
3681   BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_ADD_I32), Tmp)
3682       .add(*Idx)
3683       .addImm(Offset);
3684   return Tmp;
3685 }
3686 
3687 static MachineBasicBlock *emitIndirectSrc(MachineInstr &MI,
3688                                           MachineBasicBlock &MBB,
3689                                           const GCNSubtarget &ST) {
3690   const SIInstrInfo *TII = ST.getInstrInfo();
3691   const SIRegisterInfo &TRI = TII->getRegisterInfo();
3692   MachineFunction *MF = MBB.getParent();
3693   MachineRegisterInfo &MRI = MF->getRegInfo();
3694 
3695   Register Dst = MI.getOperand(0).getReg();
3696   const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
3697   Register SrcReg = TII->getNamedOperand(MI, AMDGPU::OpName::src)->getReg();
3698   int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm();
3699 
3700   const TargetRegisterClass *VecRC = MRI.getRegClass(SrcReg);
3701   const TargetRegisterClass *IdxRC = MRI.getRegClass(Idx->getReg());
3702 
3703   unsigned SubReg;
3704   std::tie(SubReg, Offset)
3705     = computeIndirectRegAndOffset(TRI, VecRC, SrcReg, Offset);
3706 
3707   const bool UseGPRIdxMode = ST.useVGPRIndexMode();
3708 
3709   // Check for a SGPR index.
3710   if (TII->getRegisterInfo().isSGPRClass(IdxRC)) {
3711     MachineBasicBlock::iterator I(&MI);
3712     const DebugLoc &DL = MI.getDebugLoc();
3713 
3714     if (UseGPRIdxMode) {
3715       // TODO: Look at the uses to avoid the copy. This may require rescheduling
3716       // to avoid interfering with other uses, so probably requires a new
3717       // optimization pass.
3718       Register Idx = getIndirectSGPRIdx(TII, MRI, MI, Offset);
3719 
3720       const MCInstrDesc &GPRIDXDesc =
3721           TII->getIndirectGPRIDXPseudo(TRI.getRegSizeInBits(*VecRC), true);
3722       BuildMI(MBB, I, DL, GPRIDXDesc, Dst)
3723           .addReg(SrcReg)
3724           .addReg(Idx)
3725           .addImm(SubReg);
3726     } else {
3727       setM0ToIndexFromSGPR(TII, MRI, MI, Offset);
3728 
3729       BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
3730         .addReg(SrcReg, 0, SubReg)
3731         .addReg(SrcReg, RegState::Implicit);
3732     }
3733 
3734     MI.eraseFromParent();
3735 
3736     return &MBB;
3737   }
3738 
3739   // Control flow needs to be inserted if indexing with a VGPR.
3740   const DebugLoc &DL = MI.getDebugLoc();
3741   MachineBasicBlock::iterator I(&MI);
3742 
3743   Register PhiReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
3744   Register InitReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
3745 
3746   BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), InitReg);
3747 
3748   Register SGPRIdxReg;
3749   auto InsPt = loadM0FromVGPR(TII, MBB, MI, InitReg, PhiReg, Offset,
3750                               UseGPRIdxMode, SGPRIdxReg);
3751 
3752   MachineBasicBlock *LoopBB = InsPt->getParent();
3753 
3754   if (UseGPRIdxMode) {
3755     const MCInstrDesc &GPRIDXDesc =
3756         TII->getIndirectGPRIDXPseudo(TRI.getRegSizeInBits(*VecRC), true);
3757 
3758     BuildMI(*LoopBB, InsPt, DL, GPRIDXDesc, Dst)
3759         .addReg(SrcReg)
3760         .addReg(SGPRIdxReg)
3761         .addImm(SubReg);
3762   } else {
3763     BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
3764       .addReg(SrcReg, 0, SubReg)
3765       .addReg(SrcReg, RegState::Implicit);
3766   }
3767 
3768   MI.eraseFromParent();
3769 
3770   return LoopBB;
3771 }
3772 
3773 static MachineBasicBlock *emitIndirectDst(MachineInstr &MI,
3774                                           MachineBasicBlock &MBB,
3775                                           const GCNSubtarget &ST) {
3776   const SIInstrInfo *TII = ST.getInstrInfo();
3777   const SIRegisterInfo &TRI = TII->getRegisterInfo();
3778   MachineFunction *MF = MBB.getParent();
3779   MachineRegisterInfo &MRI = MF->getRegInfo();
3780 
3781   Register Dst = MI.getOperand(0).getReg();
3782   const MachineOperand *SrcVec = TII->getNamedOperand(MI, AMDGPU::OpName::src);
3783   const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
3784   const MachineOperand *Val = TII->getNamedOperand(MI, AMDGPU::OpName::val);
3785   int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm();
3786   const TargetRegisterClass *VecRC = MRI.getRegClass(SrcVec->getReg());
3787   const TargetRegisterClass *IdxRC = MRI.getRegClass(Idx->getReg());
3788 
3789   // This can be an immediate, but will be folded later.
3790   assert(Val->getReg());
3791 
3792   unsigned SubReg;
3793   std::tie(SubReg, Offset) = computeIndirectRegAndOffset(TRI, VecRC,
3794                                                          SrcVec->getReg(),
3795                                                          Offset);
3796   const bool UseGPRIdxMode = ST.useVGPRIndexMode();
3797 
3798   if (Idx->getReg() == AMDGPU::NoRegister) {
3799     MachineBasicBlock::iterator I(&MI);
3800     const DebugLoc &DL = MI.getDebugLoc();
3801 
3802     assert(Offset == 0);
3803 
3804     BuildMI(MBB, I, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dst)
3805         .add(*SrcVec)
3806         .add(*Val)
3807         .addImm(SubReg);
3808 
3809     MI.eraseFromParent();
3810     return &MBB;
3811   }
3812 
3813   // Check for a SGPR index.
3814   if (TII->getRegisterInfo().isSGPRClass(IdxRC)) {
3815     MachineBasicBlock::iterator I(&MI);
3816     const DebugLoc &DL = MI.getDebugLoc();
3817 
3818     if (UseGPRIdxMode) {
3819       Register Idx = getIndirectSGPRIdx(TII, MRI, MI, Offset);
3820 
3821       const MCInstrDesc &GPRIDXDesc =
3822           TII->getIndirectGPRIDXPseudo(TRI.getRegSizeInBits(*VecRC), false);
3823       BuildMI(MBB, I, DL, GPRIDXDesc, Dst)
3824           .addReg(SrcVec->getReg())
3825           .add(*Val)
3826           .addReg(Idx)
3827           .addImm(SubReg);
3828     } else {
3829       setM0ToIndexFromSGPR(TII, MRI, MI, Offset);
3830 
3831       const MCInstrDesc &MovRelDesc = TII->getIndirectRegWriteMovRelPseudo(
3832           TRI.getRegSizeInBits(*VecRC), 32, false);
3833       BuildMI(MBB, I, DL, MovRelDesc, Dst)
3834           .addReg(SrcVec->getReg())
3835           .add(*Val)
3836           .addImm(SubReg);
3837     }
3838     MI.eraseFromParent();
3839     return &MBB;
3840   }
3841 
3842   // Control flow needs to be inserted if indexing with a VGPR.
3843   if (Val->isReg())
3844     MRI.clearKillFlags(Val->getReg());
3845 
3846   const DebugLoc &DL = MI.getDebugLoc();
3847 
3848   Register PhiReg = MRI.createVirtualRegister(VecRC);
3849 
3850   Register SGPRIdxReg;
3851   auto InsPt = loadM0FromVGPR(TII, MBB, MI, SrcVec->getReg(), PhiReg, Offset,
3852                               UseGPRIdxMode, SGPRIdxReg);
3853   MachineBasicBlock *LoopBB = InsPt->getParent();
3854 
3855   if (UseGPRIdxMode) {
3856     const MCInstrDesc &GPRIDXDesc =
3857         TII->getIndirectGPRIDXPseudo(TRI.getRegSizeInBits(*VecRC), false);
3858 
3859     BuildMI(*LoopBB, InsPt, DL, GPRIDXDesc, Dst)
3860         .addReg(PhiReg)
3861         .add(*Val)
3862         .addReg(SGPRIdxReg)
3863         .addImm(AMDGPU::sub0);
3864   } else {
3865     const MCInstrDesc &MovRelDesc = TII->getIndirectRegWriteMovRelPseudo(
3866         TRI.getRegSizeInBits(*VecRC), 32, false);
3867     BuildMI(*LoopBB, InsPt, DL, MovRelDesc, Dst)
3868         .addReg(PhiReg)
3869         .add(*Val)
3870         .addImm(AMDGPU::sub0);
3871   }
3872 
3873   MI.eraseFromParent();
3874   return LoopBB;
3875 }
3876 
3877 MachineBasicBlock *SITargetLowering::EmitInstrWithCustomInserter(
3878   MachineInstr &MI, MachineBasicBlock *BB) const {
3879 
3880   const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
3881   MachineFunction *MF = BB->getParent();
3882   SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
3883 
3884   switch (MI.getOpcode()) {
3885   case AMDGPU::S_UADDO_PSEUDO:
3886   case AMDGPU::S_USUBO_PSEUDO: {
3887     const DebugLoc &DL = MI.getDebugLoc();
3888     MachineOperand &Dest0 = MI.getOperand(0);
3889     MachineOperand &Dest1 = MI.getOperand(1);
3890     MachineOperand &Src0 = MI.getOperand(2);
3891     MachineOperand &Src1 = MI.getOperand(3);
3892 
3893     unsigned Opc = (MI.getOpcode() == AMDGPU::S_UADDO_PSEUDO)
3894                        ? AMDGPU::S_ADD_I32
3895                        : AMDGPU::S_SUB_I32;
3896     BuildMI(*BB, MI, DL, TII->get(Opc), Dest0.getReg()).add(Src0).add(Src1);
3897 
3898     BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_CSELECT_B64), Dest1.getReg())
3899         .addImm(1)
3900         .addImm(0);
3901 
3902     MI.eraseFromParent();
3903     return BB;
3904   }
3905   case AMDGPU::S_ADD_U64_PSEUDO:
3906   case AMDGPU::S_SUB_U64_PSEUDO: {
3907     MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
3908     const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>();
3909     const SIRegisterInfo *TRI = ST.getRegisterInfo();
3910     const TargetRegisterClass *BoolRC = TRI->getBoolRC();
3911     const DebugLoc &DL = MI.getDebugLoc();
3912 
3913     MachineOperand &Dest = MI.getOperand(0);
3914     MachineOperand &Src0 = MI.getOperand(1);
3915     MachineOperand &Src1 = MI.getOperand(2);
3916 
3917     Register DestSub0 = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
3918     Register DestSub1 = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
3919 
3920     MachineOperand Src0Sub0 = TII->buildExtractSubRegOrImm(
3921         MI, MRI, Src0, BoolRC, AMDGPU::sub0, &AMDGPU::SReg_32RegClass);
3922     MachineOperand Src0Sub1 = TII->buildExtractSubRegOrImm(
3923         MI, MRI, Src0, BoolRC, AMDGPU::sub1, &AMDGPU::SReg_32RegClass);
3924 
3925     MachineOperand Src1Sub0 = TII->buildExtractSubRegOrImm(
3926         MI, MRI, Src1, BoolRC, AMDGPU::sub0, &AMDGPU::SReg_32RegClass);
3927     MachineOperand Src1Sub1 = TII->buildExtractSubRegOrImm(
3928         MI, MRI, Src1, BoolRC, AMDGPU::sub1, &AMDGPU::SReg_32RegClass);
3929 
3930     bool IsAdd = (MI.getOpcode() == AMDGPU::S_ADD_U64_PSEUDO);
3931 
3932     unsigned LoOpc = IsAdd ? AMDGPU::S_ADD_U32 : AMDGPU::S_SUB_U32;
3933     unsigned HiOpc = IsAdd ? AMDGPU::S_ADDC_U32 : AMDGPU::S_SUBB_U32;
3934     BuildMI(*BB, MI, DL, TII->get(LoOpc), DestSub0).add(Src0Sub0).add(Src1Sub0);
3935     BuildMI(*BB, MI, DL, TII->get(HiOpc), DestSub1).add(Src0Sub1).add(Src1Sub1);
3936     BuildMI(*BB, MI, DL, TII->get(TargetOpcode::REG_SEQUENCE), Dest.getReg())
3937         .addReg(DestSub0)
3938         .addImm(AMDGPU::sub0)
3939         .addReg(DestSub1)
3940         .addImm(AMDGPU::sub1);
3941     MI.eraseFromParent();
3942     return BB;
3943   }
3944   case AMDGPU::V_ADD_U64_PSEUDO:
3945   case AMDGPU::V_SUB_U64_PSEUDO: {
3946     MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
3947     const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>();
3948     const SIRegisterInfo *TRI = ST.getRegisterInfo();
3949     const DebugLoc &DL = MI.getDebugLoc();
3950 
3951     bool IsAdd = (MI.getOpcode() == AMDGPU::V_ADD_U64_PSEUDO);
3952 
3953     const auto *CarryRC = TRI->getRegClass(AMDGPU::SReg_1_XEXECRegClassID);
3954 
3955     Register DestSub0 = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
3956     Register DestSub1 = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
3957 
3958     Register CarryReg = MRI.createVirtualRegister(CarryRC);
3959     Register DeadCarryReg = MRI.createVirtualRegister(CarryRC);
3960 
3961     MachineOperand &Dest = MI.getOperand(0);
3962     MachineOperand &Src0 = MI.getOperand(1);
3963     MachineOperand &Src1 = MI.getOperand(2);
3964 
3965     const TargetRegisterClass *Src0RC = Src0.isReg()
3966                                             ? MRI.getRegClass(Src0.getReg())
3967                                             : &AMDGPU::VReg_64RegClass;
3968     const TargetRegisterClass *Src1RC = Src1.isReg()
3969                                             ? MRI.getRegClass(Src1.getReg())
3970                                             : &AMDGPU::VReg_64RegClass;
3971 
3972     const TargetRegisterClass *Src0SubRC =
3973         TRI->getSubRegClass(Src0RC, AMDGPU::sub0);
3974     const TargetRegisterClass *Src1SubRC =
3975         TRI->getSubRegClass(Src1RC, AMDGPU::sub1);
3976 
3977     MachineOperand SrcReg0Sub0 = TII->buildExtractSubRegOrImm(
3978         MI, MRI, Src0, Src0RC, AMDGPU::sub0, Src0SubRC);
3979     MachineOperand SrcReg1Sub0 = TII->buildExtractSubRegOrImm(
3980         MI, MRI, Src1, Src1RC, AMDGPU::sub0, Src1SubRC);
3981 
3982     MachineOperand SrcReg0Sub1 = TII->buildExtractSubRegOrImm(
3983         MI, MRI, Src0, Src0RC, AMDGPU::sub1, Src0SubRC);
3984     MachineOperand SrcReg1Sub1 = TII->buildExtractSubRegOrImm(
3985         MI, MRI, Src1, Src1RC, AMDGPU::sub1, Src1SubRC);
3986 
3987     unsigned LoOpc = IsAdd ? AMDGPU::V_ADD_CO_U32_e64 : AMDGPU::V_SUB_CO_U32_e64;
3988     MachineInstr *LoHalf = BuildMI(*BB, MI, DL, TII->get(LoOpc), DestSub0)
3989                                .addReg(CarryReg, RegState::Define)
3990                                .add(SrcReg0Sub0)
3991                                .add(SrcReg1Sub0)
3992                                .addImm(0); // clamp bit
3993 
3994     unsigned HiOpc = IsAdd ? AMDGPU::V_ADDC_U32_e64 : AMDGPU::V_SUBB_U32_e64;
3995     MachineInstr *HiHalf =
3996         BuildMI(*BB, MI, DL, TII->get(HiOpc), DestSub1)
3997             .addReg(DeadCarryReg, RegState::Define | RegState::Dead)
3998             .add(SrcReg0Sub1)
3999             .add(SrcReg1Sub1)
4000             .addReg(CarryReg, RegState::Kill)
4001             .addImm(0); // clamp bit
4002 
4003     BuildMI(*BB, MI, DL, TII->get(TargetOpcode::REG_SEQUENCE), Dest.getReg())
4004         .addReg(DestSub0)
4005         .addImm(AMDGPU::sub0)
4006         .addReg(DestSub1)
4007         .addImm(AMDGPU::sub1);
4008     TII->legalizeOperands(*LoHalf);
4009     TII->legalizeOperands(*HiHalf);
4010     MI.eraseFromParent();
4011     return BB;
4012   }
4013   case AMDGPU::S_ADD_CO_PSEUDO:
4014   case AMDGPU::S_SUB_CO_PSEUDO: {
4015     // This pseudo has a chance to be selected
4016     // only from uniform add/subcarry node. All the VGPR operands
4017     // therefore assumed to be splat vectors.
4018     MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
4019     const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>();
4020     const SIRegisterInfo *TRI = ST.getRegisterInfo();
4021     MachineBasicBlock::iterator MII = MI;
4022     const DebugLoc &DL = MI.getDebugLoc();
4023     MachineOperand &Dest = MI.getOperand(0);
4024     MachineOperand &CarryDest = MI.getOperand(1);
4025     MachineOperand &Src0 = MI.getOperand(2);
4026     MachineOperand &Src1 = MI.getOperand(3);
4027     MachineOperand &Src2 = MI.getOperand(4);
4028     unsigned Opc = (MI.getOpcode() == AMDGPU::S_ADD_CO_PSEUDO)
4029                        ? AMDGPU::S_ADDC_U32
4030                        : AMDGPU::S_SUBB_U32;
4031     if (Src0.isReg() && TRI->isVectorRegister(MRI, Src0.getReg())) {
4032       Register RegOp0 = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
4033       BuildMI(*BB, MII, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32), RegOp0)
4034           .addReg(Src0.getReg());
4035       Src0.setReg(RegOp0);
4036     }
4037     if (Src1.isReg() && TRI->isVectorRegister(MRI, Src1.getReg())) {
4038       Register RegOp1 = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
4039       BuildMI(*BB, MII, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32), RegOp1)
4040           .addReg(Src1.getReg());
4041       Src1.setReg(RegOp1);
4042     }
4043     Register RegOp2 = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
4044     if (TRI->isVectorRegister(MRI, Src2.getReg())) {
4045       BuildMI(*BB, MII, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32), RegOp2)
4046           .addReg(Src2.getReg());
4047       Src2.setReg(RegOp2);
4048     }
4049 
4050     const TargetRegisterClass *Src2RC = MRI.getRegClass(Src2.getReg());
4051     if (TRI->getRegSizeInBits(*Src2RC) == 64) {
4052       if (ST.hasScalarCompareEq64()) {
4053         BuildMI(*BB, MII, DL, TII->get(AMDGPU::S_CMP_LG_U64))
4054             .addReg(Src2.getReg())
4055             .addImm(0);
4056       } else {
4057         const TargetRegisterClass *SubRC =
4058             TRI->getSubRegClass(Src2RC, AMDGPU::sub0);
4059         MachineOperand Src2Sub0 = TII->buildExtractSubRegOrImm(
4060             MII, MRI, Src2, Src2RC, AMDGPU::sub0, SubRC);
4061         MachineOperand Src2Sub1 = TII->buildExtractSubRegOrImm(
4062             MII, MRI, Src2, Src2RC, AMDGPU::sub1, SubRC);
4063         Register Src2_32 = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
4064 
4065         BuildMI(*BB, MII, DL, TII->get(AMDGPU::S_OR_B32), Src2_32)
4066             .add(Src2Sub0)
4067             .add(Src2Sub1);
4068 
4069         BuildMI(*BB, MII, DL, TII->get(AMDGPU::S_CMP_LG_U32))
4070             .addReg(Src2_32, RegState::Kill)
4071             .addImm(0);
4072       }
4073     } else {
4074       BuildMI(*BB, MII, DL, TII->get(AMDGPU::S_CMPK_LG_U32))
4075           .addReg(Src2.getReg())
4076           .addImm(0);
4077     }
4078 
4079     BuildMI(*BB, MII, DL, TII->get(Opc), Dest.getReg()).add(Src0).add(Src1);
4080 
4081     BuildMI(*BB, MII, DL, TII->get(AMDGPU::COPY), CarryDest.getReg())
4082       .addReg(AMDGPU::SCC);
4083     MI.eraseFromParent();
4084     return BB;
4085   }
4086   case AMDGPU::SI_INIT_M0: {
4087     BuildMI(*BB, MI.getIterator(), MI.getDebugLoc(),
4088             TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
4089         .add(MI.getOperand(0));
4090     MI.eraseFromParent();
4091     return BB;
4092   }
4093   case AMDGPU::GET_GROUPSTATICSIZE: {
4094     assert(getTargetMachine().getTargetTriple().getOS() == Triple::AMDHSA ||
4095            getTargetMachine().getTargetTriple().getOS() == Triple::AMDPAL);
4096     DebugLoc DL = MI.getDebugLoc();
4097     BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_MOV_B32))
4098         .add(MI.getOperand(0))
4099         .addImm(MFI->getLDSSize());
4100     MI.eraseFromParent();
4101     return BB;
4102   }
4103   case AMDGPU::SI_INDIRECT_SRC_V1:
4104   case AMDGPU::SI_INDIRECT_SRC_V2:
4105   case AMDGPU::SI_INDIRECT_SRC_V4:
4106   case AMDGPU::SI_INDIRECT_SRC_V8:
4107   case AMDGPU::SI_INDIRECT_SRC_V16:
4108   case AMDGPU::SI_INDIRECT_SRC_V32:
4109     return emitIndirectSrc(MI, *BB, *getSubtarget());
4110   case AMDGPU::SI_INDIRECT_DST_V1:
4111   case AMDGPU::SI_INDIRECT_DST_V2:
4112   case AMDGPU::SI_INDIRECT_DST_V4:
4113   case AMDGPU::SI_INDIRECT_DST_V8:
4114   case AMDGPU::SI_INDIRECT_DST_V16:
4115   case AMDGPU::SI_INDIRECT_DST_V32:
4116     return emitIndirectDst(MI, *BB, *getSubtarget());
4117   case AMDGPU::SI_KILL_F32_COND_IMM_PSEUDO:
4118   case AMDGPU::SI_KILL_I1_PSEUDO:
4119     return splitKillBlock(MI, BB);
4120   case AMDGPU::V_CNDMASK_B64_PSEUDO: {
4121     MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
4122     const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>();
4123     const SIRegisterInfo *TRI = ST.getRegisterInfo();
4124 
4125     Register Dst = MI.getOperand(0).getReg();
4126     Register Src0 = MI.getOperand(1).getReg();
4127     Register Src1 = MI.getOperand(2).getReg();
4128     const DebugLoc &DL = MI.getDebugLoc();
4129     Register SrcCond = MI.getOperand(3).getReg();
4130 
4131     Register DstLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
4132     Register DstHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
4133     const auto *CondRC = TRI->getRegClass(AMDGPU::SReg_1_XEXECRegClassID);
4134     Register SrcCondCopy = MRI.createVirtualRegister(CondRC);
4135 
4136     BuildMI(*BB, MI, DL, TII->get(AMDGPU::COPY), SrcCondCopy)
4137       .addReg(SrcCond);
4138     BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstLo)
4139       .addImm(0)
4140       .addReg(Src0, 0, AMDGPU::sub0)
4141       .addImm(0)
4142       .addReg(Src1, 0, AMDGPU::sub0)
4143       .addReg(SrcCondCopy);
4144     BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstHi)
4145       .addImm(0)
4146       .addReg(Src0, 0, AMDGPU::sub1)
4147       .addImm(0)
4148       .addReg(Src1, 0, AMDGPU::sub1)
4149       .addReg(SrcCondCopy);
4150 
4151     BuildMI(*BB, MI, DL, TII->get(AMDGPU::REG_SEQUENCE), Dst)
4152       .addReg(DstLo)
4153       .addImm(AMDGPU::sub0)
4154       .addReg(DstHi)
4155       .addImm(AMDGPU::sub1);
4156     MI.eraseFromParent();
4157     return BB;
4158   }
4159   case AMDGPU::SI_BR_UNDEF: {
4160     const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
4161     const DebugLoc &DL = MI.getDebugLoc();
4162     MachineInstr *Br = BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_CBRANCH_SCC1))
4163                            .add(MI.getOperand(0));
4164     Br->getOperand(1).setIsUndef(true); // read undef SCC
4165     MI.eraseFromParent();
4166     return BB;
4167   }
4168   case AMDGPU::ADJCALLSTACKUP:
4169   case AMDGPU::ADJCALLSTACKDOWN: {
4170     const SIMachineFunctionInfo *Info = MF->getInfo<SIMachineFunctionInfo>();
4171     MachineInstrBuilder MIB(*MF, &MI);
4172     MIB.addReg(Info->getStackPtrOffsetReg(), RegState::ImplicitDefine)
4173        .addReg(Info->getStackPtrOffsetReg(), RegState::Implicit);
4174     return BB;
4175   }
4176   case AMDGPU::SI_CALL_ISEL: {
4177     const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
4178     const DebugLoc &DL = MI.getDebugLoc();
4179 
4180     unsigned ReturnAddrReg = TII->getRegisterInfo().getReturnAddressReg(*MF);
4181 
4182     MachineInstrBuilder MIB;
4183     MIB = BuildMI(*BB, MI, DL, TII->get(AMDGPU::SI_CALL), ReturnAddrReg);
4184 
4185     for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I)
4186       MIB.add(MI.getOperand(I));
4187 
4188     MIB.cloneMemRefs(MI);
4189     MI.eraseFromParent();
4190     return BB;
4191   }
4192   case AMDGPU::V_ADD_CO_U32_e32:
4193   case AMDGPU::V_SUB_CO_U32_e32:
4194   case AMDGPU::V_SUBREV_CO_U32_e32: {
4195     // TODO: Define distinct V_*_I32_Pseudo instructions instead.
4196     const DebugLoc &DL = MI.getDebugLoc();
4197     unsigned Opc = MI.getOpcode();
4198 
4199     bool NeedClampOperand = false;
4200     if (TII->pseudoToMCOpcode(Opc) == -1) {
4201       Opc = AMDGPU::getVOPe64(Opc);
4202       NeedClampOperand = true;
4203     }
4204 
4205     auto I = BuildMI(*BB, MI, DL, TII->get(Opc), MI.getOperand(0).getReg());
4206     if (TII->isVOP3(*I)) {
4207       const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>();
4208       const SIRegisterInfo *TRI = ST.getRegisterInfo();
4209       I.addReg(TRI->getVCC(), RegState::Define);
4210     }
4211     I.add(MI.getOperand(1))
4212      .add(MI.getOperand(2));
4213     if (NeedClampOperand)
4214       I.addImm(0); // clamp bit for e64 encoding
4215 
4216     TII->legalizeOperands(*I);
4217 
4218     MI.eraseFromParent();
4219     return BB;
4220   }
4221   case AMDGPU::DS_GWS_INIT:
4222   case AMDGPU::DS_GWS_SEMA_V:
4223   case AMDGPU::DS_GWS_SEMA_BR:
4224   case AMDGPU::DS_GWS_SEMA_P:
4225   case AMDGPU::DS_GWS_SEMA_RELEASE_ALL:
4226   case AMDGPU::DS_GWS_BARRIER:
4227     // A s_waitcnt 0 is required to be the instruction immediately following.
4228     if (getSubtarget()->hasGWSAutoReplay()) {
4229       bundleInstWithWaitcnt(MI);
4230       return BB;
4231     }
4232 
4233     return emitGWSMemViolTestLoop(MI, BB);
4234   case AMDGPU::S_SETREG_B32: {
4235     // Try to optimize cases that only set the denormal mode or rounding mode.
4236     //
4237     // If the s_setreg_b32 fully sets all of the bits in the rounding mode or
4238     // denormal mode to a constant, we can use s_round_mode or s_denorm_mode
4239     // instead.
4240     //
4241     // FIXME: This could be predicates on the immediate, but tablegen doesn't
4242     // allow you to have a no side effect instruction in the output of a
4243     // sideeffecting pattern.
4244     unsigned ID, Offset, Width;
4245     AMDGPU::Hwreg::decodeHwreg(MI.getOperand(1).getImm(), ID, Offset, Width);
4246     if (ID != AMDGPU::Hwreg::ID_MODE)
4247       return BB;
4248 
4249     const unsigned WidthMask = maskTrailingOnes<unsigned>(Width);
4250     const unsigned SetMask = WidthMask << Offset;
4251 
4252     if (getSubtarget()->hasDenormModeInst()) {
4253       unsigned SetDenormOp = 0;
4254       unsigned SetRoundOp = 0;
4255 
4256       // The dedicated instructions can only set the whole denorm or round mode
4257       // at once, not a subset of bits in either.
4258       if (SetMask ==
4259           (AMDGPU::Hwreg::FP_ROUND_MASK | AMDGPU::Hwreg::FP_DENORM_MASK)) {
4260         // If this fully sets both the round and denorm mode, emit the two
4261         // dedicated instructions for these.
4262         SetRoundOp = AMDGPU::S_ROUND_MODE;
4263         SetDenormOp = AMDGPU::S_DENORM_MODE;
4264       } else if (SetMask == AMDGPU::Hwreg::FP_ROUND_MASK) {
4265         SetRoundOp = AMDGPU::S_ROUND_MODE;
4266       } else if (SetMask == AMDGPU::Hwreg::FP_DENORM_MASK) {
4267         SetDenormOp = AMDGPU::S_DENORM_MODE;
4268       }
4269 
4270       if (SetRoundOp || SetDenormOp) {
4271         MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
4272         MachineInstr *Def = MRI.getVRegDef(MI.getOperand(0).getReg());
4273         if (Def && Def->isMoveImmediate() && Def->getOperand(1).isImm()) {
4274           unsigned ImmVal = Def->getOperand(1).getImm();
4275           if (SetRoundOp) {
4276             BuildMI(*BB, MI, MI.getDebugLoc(), TII->get(SetRoundOp))
4277                 .addImm(ImmVal & 0xf);
4278 
4279             // If we also have the denorm mode, get just the denorm mode bits.
4280             ImmVal >>= 4;
4281           }
4282 
4283           if (SetDenormOp) {
4284             BuildMI(*BB, MI, MI.getDebugLoc(), TII->get(SetDenormOp))
4285                 .addImm(ImmVal & 0xf);
4286           }
4287 
4288           MI.eraseFromParent();
4289           return BB;
4290         }
4291       }
4292     }
4293 
4294     // If only FP bits are touched, used the no side effects pseudo.
4295     if ((SetMask & (AMDGPU::Hwreg::FP_ROUND_MASK |
4296                     AMDGPU::Hwreg::FP_DENORM_MASK)) == SetMask)
4297       MI.setDesc(TII->get(AMDGPU::S_SETREG_B32_mode));
4298 
4299     return BB;
4300   }
4301   default:
4302     return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
4303   }
4304 }
4305 
4306 bool SITargetLowering::hasBitPreservingFPLogic(EVT VT) const {
4307   return isTypeLegal(VT.getScalarType());
4308 }
4309 
4310 bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const {
4311   // This currently forces unfolding various combinations of fsub into fma with
4312   // free fneg'd operands. As long as we have fast FMA (controlled by
4313   // isFMAFasterThanFMulAndFAdd), we should perform these.
4314 
4315   // When fma is quarter rate, for f64 where add / sub are at best half rate,
4316   // most of these combines appear to be cycle neutral but save on instruction
4317   // count / code size.
4318   return true;
4319 }
4320 
4321 EVT SITargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &Ctx,
4322                                          EVT VT) const {
4323   if (!VT.isVector()) {
4324     return MVT::i1;
4325   }
4326   return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements());
4327 }
4328 
4329 MVT SITargetLowering::getScalarShiftAmountTy(const DataLayout &, EVT VT) const {
4330   // TODO: Should i16 be used always if legal? For now it would force VALU
4331   // shifts.
4332   return (VT == MVT::i16) ? MVT::i16 : MVT::i32;
4333 }
4334 
4335 LLT SITargetLowering::getPreferredShiftAmountTy(LLT Ty) const {
4336   return (Ty.getScalarSizeInBits() <= 16 && Subtarget->has16BitInsts())
4337              ? Ty.changeElementSize(16)
4338              : Ty.changeElementSize(32);
4339 }
4340 
4341 // Answering this is somewhat tricky and depends on the specific device which
4342 // have different rates for fma or all f64 operations.
4343 //
4344 // v_fma_f64 and v_mul_f64 always take the same number of cycles as each other
4345 // regardless of which device (although the number of cycles differs between
4346 // devices), so it is always profitable for f64.
4347 //
4348 // v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable
4349 // only on full rate devices. Normally, we should prefer selecting v_mad_f32
4350 // which we can always do even without fused FP ops since it returns the same
4351 // result as the separate operations and since it is always full
4352 // rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32
4353 // however does not support denormals, so we do report fma as faster if we have
4354 // a fast fma device and require denormals.
4355 //
4356 bool SITargetLowering::isFMAFasterThanFMulAndFAdd(const MachineFunction &MF,
4357                                                   EVT VT) const {
4358   VT = VT.getScalarType();
4359 
4360   switch (VT.getSimpleVT().SimpleTy) {
4361   case MVT::f32: {
4362     // If mad is not available this depends only on if f32 fma is full rate.
4363     if (!Subtarget->hasMadMacF32Insts())
4364       return Subtarget->hasFastFMAF32();
4365 
4366     // Otherwise f32 mad is always full rate and returns the same result as
4367     // the separate operations so should be preferred over fma.
4368     // However does not support denomals.
4369     if (hasFP32Denormals(MF))
4370       return Subtarget->hasFastFMAF32() || Subtarget->hasDLInsts();
4371 
4372     // If the subtarget has v_fmac_f32, that's just as good as v_mac_f32.
4373     return Subtarget->hasFastFMAF32() && Subtarget->hasDLInsts();
4374   }
4375   case MVT::f64:
4376     return true;
4377   case MVT::f16:
4378     return Subtarget->has16BitInsts() && hasFP64FP16Denormals(MF);
4379   default:
4380     break;
4381   }
4382 
4383   return false;
4384 }
4385 
4386 bool SITargetLowering::isFMADLegal(const SelectionDAG &DAG,
4387                                    const SDNode *N) const {
4388   // TODO: Check future ftz flag
4389   // v_mad_f32/v_mac_f32 do not support denormals.
4390   EVT VT = N->getValueType(0);
4391   if (VT == MVT::f32)
4392     return Subtarget->hasMadMacF32Insts() &&
4393            !hasFP32Denormals(DAG.getMachineFunction());
4394   if (VT == MVT::f16) {
4395     return Subtarget->hasMadF16() &&
4396            !hasFP64FP16Denormals(DAG.getMachineFunction());
4397   }
4398 
4399   return false;
4400 }
4401 
4402 //===----------------------------------------------------------------------===//
4403 // Custom DAG Lowering Operations
4404 //===----------------------------------------------------------------------===//
4405 
4406 // Work around LegalizeDAG doing the wrong thing and fully scalarizing if the
4407 // wider vector type is legal.
4408 SDValue SITargetLowering::splitUnaryVectorOp(SDValue Op,
4409                                              SelectionDAG &DAG) const {
4410   unsigned Opc = Op.getOpcode();
4411   EVT VT = Op.getValueType();
4412   assert(VT == MVT::v4f16 || VT == MVT::v4i16);
4413 
4414   SDValue Lo, Hi;
4415   std::tie(Lo, Hi) = DAG.SplitVectorOperand(Op.getNode(), 0);
4416 
4417   SDLoc SL(Op);
4418   SDValue OpLo = DAG.getNode(Opc, SL, Lo.getValueType(), Lo,
4419                              Op->getFlags());
4420   SDValue OpHi = DAG.getNode(Opc, SL, Hi.getValueType(), Hi,
4421                              Op->getFlags());
4422 
4423   return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(Op), VT, OpLo, OpHi);
4424 }
4425 
4426 // Work around LegalizeDAG doing the wrong thing and fully scalarizing if the
4427 // wider vector type is legal.
4428 SDValue SITargetLowering::splitBinaryVectorOp(SDValue Op,
4429                                               SelectionDAG &DAG) const {
4430   unsigned Opc = Op.getOpcode();
4431   EVT VT = Op.getValueType();
4432   assert(VT == MVT::v4i16 || VT == MVT::v4f16 || VT == MVT::v4f32 ||
4433          VT == MVT::v8f32 || VT == MVT::v16f32 || VT == MVT::v32f32);
4434 
4435   SDValue Lo0, Hi0;
4436   std::tie(Lo0, Hi0) = DAG.SplitVectorOperand(Op.getNode(), 0);
4437   SDValue Lo1, Hi1;
4438   std::tie(Lo1, Hi1) = DAG.SplitVectorOperand(Op.getNode(), 1);
4439 
4440   SDLoc SL(Op);
4441 
4442   SDValue OpLo = DAG.getNode(Opc, SL, Lo0.getValueType(), Lo0, Lo1,
4443                              Op->getFlags());
4444   SDValue OpHi = DAG.getNode(Opc, SL, Hi0.getValueType(), Hi0, Hi1,
4445                              Op->getFlags());
4446 
4447   return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(Op), VT, OpLo, OpHi);
4448 }
4449 
4450 SDValue SITargetLowering::splitTernaryVectorOp(SDValue Op,
4451                                               SelectionDAG &DAG) const {
4452   unsigned Opc = Op.getOpcode();
4453   EVT VT = Op.getValueType();
4454   assert(VT == MVT::v4i16 || VT == MVT::v4f16 || VT == MVT::v4f32 ||
4455          VT == MVT::v8f32 || VT == MVT::v16f32 || VT == MVT::v32f32);
4456 
4457   SDValue Lo0, Hi0;
4458   std::tie(Lo0, Hi0) = DAG.SplitVectorOperand(Op.getNode(), 0);
4459   SDValue Lo1, Hi1;
4460   std::tie(Lo1, Hi1) = DAG.SplitVectorOperand(Op.getNode(), 1);
4461   SDValue Lo2, Hi2;
4462   std::tie(Lo2, Hi2) = DAG.SplitVectorOperand(Op.getNode(), 2);
4463 
4464   SDLoc SL(Op);
4465 
4466   SDValue OpLo = DAG.getNode(Opc, SL, Lo0.getValueType(), Lo0, Lo1, Lo2,
4467                              Op->getFlags());
4468   SDValue OpHi = DAG.getNode(Opc, SL, Hi0.getValueType(), Hi0, Hi1, Hi2,
4469                              Op->getFlags());
4470 
4471   return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(Op), VT, OpLo, OpHi);
4472 }
4473 
4474 
4475 SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
4476   switch (Op.getOpcode()) {
4477   default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
4478   case ISD::BRCOND: return LowerBRCOND(Op, DAG);
4479   case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
4480   case ISD::LOAD: {
4481     SDValue Result = LowerLOAD(Op, DAG);
4482     assert((!Result.getNode() ||
4483             Result.getNode()->getNumValues() == 2) &&
4484            "Load should return a value and a chain");
4485     return Result;
4486   }
4487 
4488   case ISD::FSIN:
4489   case ISD::FCOS:
4490     return LowerTrig(Op, DAG);
4491   case ISD::SELECT: return LowerSELECT(Op, DAG);
4492   case ISD::FDIV: return LowerFDIV(Op, DAG);
4493   case ISD::ATOMIC_CMP_SWAP: return LowerATOMIC_CMP_SWAP(Op, DAG);
4494   case ISD::STORE: return LowerSTORE(Op, DAG);
4495   case ISD::GlobalAddress: {
4496     MachineFunction &MF = DAG.getMachineFunction();
4497     SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
4498     return LowerGlobalAddress(MFI, Op, DAG);
4499   }
4500   case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
4501   case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
4502   case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
4503   case ISD::ADDRSPACECAST: return lowerADDRSPACECAST(Op, DAG);
4504   case ISD::INSERT_SUBVECTOR:
4505     return lowerINSERT_SUBVECTOR(Op, DAG);
4506   case ISD::INSERT_VECTOR_ELT:
4507     return lowerINSERT_VECTOR_ELT(Op, DAG);
4508   case ISD::EXTRACT_VECTOR_ELT:
4509     return lowerEXTRACT_VECTOR_ELT(Op, DAG);
4510   case ISD::VECTOR_SHUFFLE:
4511     return lowerVECTOR_SHUFFLE(Op, DAG);
4512   case ISD::BUILD_VECTOR:
4513     return lowerBUILD_VECTOR(Op, DAG);
4514   case ISD::FP_ROUND:
4515     return lowerFP_ROUND(Op, DAG);
4516   case ISD::TRAP:
4517     return lowerTRAP(Op, DAG);
4518   case ISD::DEBUGTRAP:
4519     return lowerDEBUGTRAP(Op, DAG);
4520   case ISD::FABS:
4521   case ISD::FNEG:
4522   case ISD::FCANONICALIZE:
4523   case ISD::BSWAP:
4524     return splitUnaryVectorOp(Op, DAG);
4525   case ISD::FMINNUM:
4526   case ISD::FMAXNUM:
4527     return lowerFMINNUM_FMAXNUM(Op, DAG);
4528   case ISD::FMA:
4529     return splitTernaryVectorOp(Op, DAG);
4530   case ISD::FP_TO_SINT:
4531   case ISD::FP_TO_UINT:
4532     return LowerFP_TO_INT(Op, DAG);
4533   case ISD::SHL:
4534   case ISD::SRA:
4535   case ISD::SRL:
4536   case ISD::ADD:
4537   case ISD::SUB:
4538   case ISD::MUL:
4539   case ISD::SMIN:
4540   case ISD::SMAX:
4541   case ISD::UMIN:
4542   case ISD::UMAX:
4543   case ISD::FADD:
4544   case ISD::FMUL:
4545   case ISD::FMINNUM_IEEE:
4546   case ISD::FMAXNUM_IEEE:
4547   case ISD::UADDSAT:
4548   case ISD::USUBSAT:
4549   case ISD::SADDSAT:
4550   case ISD::SSUBSAT:
4551     return splitBinaryVectorOp(Op, DAG);
4552   case ISD::SMULO:
4553   case ISD::UMULO:
4554     return lowerXMULO(Op, DAG);
4555   case ISD::DYNAMIC_STACKALLOC:
4556     return LowerDYNAMIC_STACKALLOC(Op, DAG);
4557   }
4558   return SDValue();
4559 }
4560 
4561 // Used for D16: Casts the result of an instruction into the right vector,
4562 // packs values if loads return unpacked values.
4563 static SDValue adjustLoadValueTypeImpl(SDValue Result, EVT LoadVT,
4564                                        const SDLoc &DL,
4565                                        SelectionDAG &DAG, bool Unpacked) {
4566   if (!LoadVT.isVector())
4567     return Result;
4568 
4569   // Cast back to the original packed type or to a larger type that is a
4570   // multiple of 32 bit for D16. Widening the return type is a required for
4571   // legalization.
4572   EVT FittingLoadVT = LoadVT;
4573   if ((LoadVT.getVectorNumElements() % 2) == 1) {
4574     FittingLoadVT =
4575         EVT::getVectorVT(*DAG.getContext(), LoadVT.getVectorElementType(),
4576                          LoadVT.getVectorNumElements() + 1);
4577   }
4578 
4579   if (Unpacked) { // From v2i32/v4i32 back to v2f16/v4f16.
4580     // Truncate to v2i16/v4i16.
4581     EVT IntLoadVT = FittingLoadVT.changeTypeToInteger();
4582 
4583     // Workaround legalizer not scalarizing truncate after vector op
4584     // legalization but not creating intermediate vector trunc.
4585     SmallVector<SDValue, 4> Elts;
4586     DAG.ExtractVectorElements(Result, Elts);
4587     for (SDValue &Elt : Elts)
4588       Elt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i16, Elt);
4589 
4590     // Pad illegal v1i16/v3fi6 to v4i16
4591     if ((LoadVT.getVectorNumElements() % 2) == 1)
4592       Elts.push_back(DAG.getUNDEF(MVT::i16));
4593 
4594     Result = DAG.getBuildVector(IntLoadVT, DL, Elts);
4595 
4596     // Bitcast to original type (v2f16/v4f16).
4597     return DAG.getNode(ISD::BITCAST, DL, FittingLoadVT, Result);
4598   }
4599 
4600   // Cast back to the original packed type.
4601   return DAG.getNode(ISD::BITCAST, DL, FittingLoadVT, Result);
4602 }
4603 
4604 SDValue SITargetLowering::adjustLoadValueType(unsigned Opcode,
4605                                               MemSDNode *M,
4606                                               SelectionDAG &DAG,
4607                                               ArrayRef<SDValue> Ops,
4608                                               bool IsIntrinsic) const {
4609   SDLoc DL(M);
4610 
4611   bool Unpacked = Subtarget->hasUnpackedD16VMem();
4612   EVT LoadVT = M->getValueType(0);
4613 
4614   EVT EquivLoadVT = LoadVT;
4615   if (LoadVT.isVector()) {
4616     if (Unpacked) {
4617       EquivLoadVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32,
4618                                      LoadVT.getVectorNumElements());
4619     } else if ((LoadVT.getVectorNumElements() % 2) == 1) {
4620       // Widen v3f16 to legal type
4621       EquivLoadVT =
4622           EVT::getVectorVT(*DAG.getContext(), LoadVT.getVectorElementType(),
4623                            LoadVT.getVectorNumElements() + 1);
4624     }
4625   }
4626 
4627   // Change from v4f16/v2f16 to EquivLoadVT.
4628   SDVTList VTList = DAG.getVTList(EquivLoadVT, MVT::Other);
4629 
4630   SDValue Load
4631     = DAG.getMemIntrinsicNode(
4632       IsIntrinsic ? (unsigned)ISD::INTRINSIC_W_CHAIN : Opcode, DL,
4633       VTList, Ops, M->getMemoryVT(),
4634       M->getMemOperand());
4635 
4636   SDValue Adjusted = adjustLoadValueTypeImpl(Load, LoadVT, DL, DAG, Unpacked);
4637 
4638   return DAG.getMergeValues({ Adjusted, Load.getValue(1) }, DL);
4639 }
4640 
4641 SDValue SITargetLowering::lowerIntrinsicLoad(MemSDNode *M, bool IsFormat,
4642                                              SelectionDAG &DAG,
4643                                              ArrayRef<SDValue> Ops) const {
4644   SDLoc DL(M);
4645   EVT LoadVT = M->getValueType(0);
4646   EVT EltType = LoadVT.getScalarType();
4647   EVT IntVT = LoadVT.changeTypeToInteger();
4648 
4649   bool IsD16 = IsFormat && (EltType.getSizeInBits() == 16);
4650 
4651   unsigned Opc =
4652       IsFormat ? AMDGPUISD::BUFFER_LOAD_FORMAT : AMDGPUISD::BUFFER_LOAD;
4653 
4654   if (IsD16) {
4655     return adjustLoadValueType(AMDGPUISD::BUFFER_LOAD_FORMAT_D16, M, DAG, Ops);
4656   }
4657 
4658   // Handle BUFFER_LOAD_BYTE/UBYTE/SHORT/USHORT overloaded intrinsics
4659   if (!IsD16 && !LoadVT.isVector() && EltType.getSizeInBits() < 32)
4660     return handleByteShortBufferLoads(DAG, LoadVT, DL, Ops, M);
4661 
4662   if (isTypeLegal(LoadVT)) {
4663     return getMemIntrinsicNode(Opc, DL, M->getVTList(), Ops, IntVT,
4664                                M->getMemOperand(), DAG);
4665   }
4666 
4667   EVT CastVT = getEquivalentMemType(*DAG.getContext(), LoadVT);
4668   SDVTList VTList = DAG.getVTList(CastVT, MVT::Other);
4669   SDValue MemNode = getMemIntrinsicNode(Opc, DL, VTList, Ops, CastVT,
4670                                         M->getMemOperand(), DAG);
4671   return DAG.getMergeValues(
4672       {DAG.getNode(ISD::BITCAST, DL, LoadVT, MemNode), MemNode.getValue(1)},
4673       DL);
4674 }
4675 
4676 static SDValue lowerICMPIntrinsic(const SITargetLowering &TLI,
4677                                   SDNode *N, SelectionDAG &DAG) {
4678   EVT VT = N->getValueType(0);
4679   const auto *CD = cast<ConstantSDNode>(N->getOperand(3));
4680   unsigned CondCode = CD->getZExtValue();
4681   if (!ICmpInst::isIntPredicate(static_cast<ICmpInst::Predicate>(CondCode)))
4682     return DAG.getUNDEF(VT);
4683 
4684   ICmpInst::Predicate IcInput = static_cast<ICmpInst::Predicate>(CondCode);
4685 
4686   SDValue LHS = N->getOperand(1);
4687   SDValue RHS = N->getOperand(2);
4688 
4689   SDLoc DL(N);
4690 
4691   EVT CmpVT = LHS.getValueType();
4692   if (CmpVT == MVT::i16 && !TLI.isTypeLegal(MVT::i16)) {
4693     unsigned PromoteOp = ICmpInst::isSigned(IcInput) ?
4694       ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
4695     LHS = DAG.getNode(PromoteOp, DL, MVT::i32, LHS);
4696     RHS = DAG.getNode(PromoteOp, DL, MVT::i32, RHS);
4697   }
4698 
4699   ISD::CondCode CCOpcode = getICmpCondCode(IcInput);
4700 
4701   unsigned WavefrontSize = TLI.getSubtarget()->getWavefrontSize();
4702   EVT CCVT = EVT::getIntegerVT(*DAG.getContext(), WavefrontSize);
4703 
4704   SDValue SetCC = DAG.getNode(AMDGPUISD::SETCC, DL, CCVT, LHS, RHS,
4705                               DAG.getCondCode(CCOpcode));
4706   if (VT.bitsEq(CCVT))
4707     return SetCC;
4708   return DAG.getZExtOrTrunc(SetCC, DL, VT);
4709 }
4710 
4711 static SDValue lowerFCMPIntrinsic(const SITargetLowering &TLI,
4712                                   SDNode *N, SelectionDAG &DAG) {
4713   EVT VT = N->getValueType(0);
4714   const auto *CD = cast<ConstantSDNode>(N->getOperand(3));
4715 
4716   unsigned CondCode = CD->getZExtValue();
4717   if (!FCmpInst::isFPPredicate(static_cast<FCmpInst::Predicate>(CondCode)))
4718     return DAG.getUNDEF(VT);
4719 
4720   SDValue Src0 = N->getOperand(1);
4721   SDValue Src1 = N->getOperand(2);
4722   EVT CmpVT = Src0.getValueType();
4723   SDLoc SL(N);
4724 
4725   if (CmpVT == MVT::f16 && !TLI.isTypeLegal(CmpVT)) {
4726     Src0 = DAG.getNode(ISD::FP_EXTEND, SL, MVT::f32, Src0);
4727     Src1 = DAG.getNode(ISD::FP_EXTEND, SL, MVT::f32, Src1);
4728   }
4729 
4730   FCmpInst::Predicate IcInput = static_cast<FCmpInst::Predicate>(CondCode);
4731   ISD::CondCode CCOpcode = getFCmpCondCode(IcInput);
4732   unsigned WavefrontSize = TLI.getSubtarget()->getWavefrontSize();
4733   EVT CCVT = EVT::getIntegerVT(*DAG.getContext(), WavefrontSize);
4734   SDValue SetCC = DAG.getNode(AMDGPUISD::SETCC, SL, CCVT, Src0,
4735                               Src1, DAG.getCondCode(CCOpcode));
4736   if (VT.bitsEq(CCVT))
4737     return SetCC;
4738   return DAG.getZExtOrTrunc(SetCC, SL, VT);
4739 }
4740 
4741 static SDValue lowerBALLOTIntrinsic(const SITargetLowering &TLI, SDNode *N,
4742                                     SelectionDAG &DAG) {
4743   EVT VT = N->getValueType(0);
4744   SDValue Src = N->getOperand(1);
4745   SDLoc SL(N);
4746 
4747   if (Src.getOpcode() == ISD::SETCC) {
4748     // (ballot (ISD::SETCC ...)) -> (AMDGPUISD::SETCC ...)
4749     return DAG.getNode(AMDGPUISD::SETCC, SL, VT, Src.getOperand(0),
4750                        Src.getOperand(1), Src.getOperand(2));
4751   }
4752   if (const ConstantSDNode *Arg = dyn_cast<ConstantSDNode>(Src)) {
4753     // (ballot 0) -> 0
4754     if (Arg->isNullValue())
4755       return DAG.getConstant(0, SL, VT);
4756 
4757     // (ballot 1) -> EXEC/EXEC_LO
4758     if (Arg->isOne()) {
4759       Register Exec;
4760       if (VT.getScalarSizeInBits() == 32)
4761         Exec = AMDGPU::EXEC_LO;
4762       else if (VT.getScalarSizeInBits() == 64)
4763         Exec = AMDGPU::EXEC;
4764       else
4765         return SDValue();
4766 
4767       return DAG.getCopyFromReg(DAG.getEntryNode(), SL, Exec, VT);
4768     }
4769   }
4770 
4771   // (ballot (i1 $src)) -> (AMDGPUISD::SETCC (i32 (zext $src)) (i32 0)
4772   // ISD::SETNE)
4773   return DAG.getNode(
4774       AMDGPUISD::SETCC, SL, VT, DAG.getZExtOrTrunc(Src, SL, MVT::i32),
4775       DAG.getConstant(0, SL, MVT::i32), DAG.getCondCode(ISD::SETNE));
4776 }
4777 
4778 void SITargetLowering::ReplaceNodeResults(SDNode *N,
4779                                           SmallVectorImpl<SDValue> &Results,
4780                                           SelectionDAG &DAG) const {
4781   switch (N->getOpcode()) {
4782   case ISD::INSERT_VECTOR_ELT: {
4783     if (SDValue Res = lowerINSERT_VECTOR_ELT(SDValue(N, 0), DAG))
4784       Results.push_back(Res);
4785     return;
4786   }
4787   case ISD::EXTRACT_VECTOR_ELT: {
4788     if (SDValue Res = lowerEXTRACT_VECTOR_ELT(SDValue(N, 0), DAG))
4789       Results.push_back(Res);
4790     return;
4791   }
4792   case ISD::INTRINSIC_WO_CHAIN: {
4793     unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
4794     switch (IID) {
4795     case Intrinsic::amdgcn_cvt_pkrtz: {
4796       SDValue Src0 = N->getOperand(1);
4797       SDValue Src1 = N->getOperand(2);
4798       SDLoc SL(N);
4799       SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_PKRTZ_F16_F32, SL, MVT::i32,
4800                                 Src0, Src1);
4801       Results.push_back(DAG.getNode(ISD::BITCAST, SL, MVT::v2f16, Cvt));
4802       return;
4803     }
4804     case Intrinsic::amdgcn_cvt_pknorm_i16:
4805     case Intrinsic::amdgcn_cvt_pknorm_u16:
4806     case Intrinsic::amdgcn_cvt_pk_i16:
4807     case Intrinsic::amdgcn_cvt_pk_u16: {
4808       SDValue Src0 = N->getOperand(1);
4809       SDValue Src1 = N->getOperand(2);
4810       SDLoc SL(N);
4811       unsigned Opcode;
4812 
4813       if (IID == Intrinsic::amdgcn_cvt_pknorm_i16)
4814         Opcode = AMDGPUISD::CVT_PKNORM_I16_F32;
4815       else if (IID == Intrinsic::amdgcn_cvt_pknorm_u16)
4816         Opcode = AMDGPUISD::CVT_PKNORM_U16_F32;
4817       else if (IID == Intrinsic::amdgcn_cvt_pk_i16)
4818         Opcode = AMDGPUISD::CVT_PK_I16_I32;
4819       else
4820         Opcode = AMDGPUISD::CVT_PK_U16_U32;
4821 
4822       EVT VT = N->getValueType(0);
4823       if (isTypeLegal(VT))
4824         Results.push_back(DAG.getNode(Opcode, SL, VT, Src0, Src1));
4825       else {
4826         SDValue Cvt = DAG.getNode(Opcode, SL, MVT::i32, Src0, Src1);
4827         Results.push_back(DAG.getNode(ISD::BITCAST, SL, MVT::v2i16, Cvt));
4828       }
4829       return;
4830     }
4831     }
4832     break;
4833   }
4834   case ISD::INTRINSIC_W_CHAIN: {
4835     if (SDValue Res = LowerINTRINSIC_W_CHAIN(SDValue(N, 0), DAG)) {
4836       if (Res.getOpcode() == ISD::MERGE_VALUES) {
4837         // FIXME: Hacky
4838         for (unsigned I = 0; I < Res.getNumOperands(); I++) {
4839           Results.push_back(Res.getOperand(I));
4840         }
4841       } else {
4842         Results.push_back(Res);
4843         Results.push_back(Res.getValue(1));
4844       }
4845       return;
4846     }
4847 
4848     break;
4849   }
4850   case ISD::SELECT: {
4851     SDLoc SL(N);
4852     EVT VT = N->getValueType(0);
4853     EVT NewVT = getEquivalentMemType(*DAG.getContext(), VT);
4854     SDValue LHS = DAG.getNode(ISD::BITCAST, SL, NewVT, N->getOperand(1));
4855     SDValue RHS = DAG.getNode(ISD::BITCAST, SL, NewVT, N->getOperand(2));
4856 
4857     EVT SelectVT = NewVT;
4858     if (NewVT.bitsLT(MVT::i32)) {
4859       LHS = DAG.getNode(ISD::ANY_EXTEND, SL, MVT::i32, LHS);
4860       RHS = DAG.getNode(ISD::ANY_EXTEND, SL, MVT::i32, RHS);
4861       SelectVT = MVT::i32;
4862     }
4863 
4864     SDValue NewSelect = DAG.getNode(ISD::SELECT, SL, SelectVT,
4865                                     N->getOperand(0), LHS, RHS);
4866 
4867     if (NewVT != SelectVT)
4868       NewSelect = DAG.getNode(ISD::TRUNCATE, SL, NewVT, NewSelect);
4869     Results.push_back(DAG.getNode(ISD::BITCAST, SL, VT, NewSelect));
4870     return;
4871   }
4872   case ISD::FNEG: {
4873     if (N->getValueType(0) != MVT::v2f16)
4874       break;
4875 
4876     SDLoc SL(N);
4877     SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::i32, N->getOperand(0));
4878 
4879     SDValue Op = DAG.getNode(ISD::XOR, SL, MVT::i32,
4880                              BC,
4881                              DAG.getConstant(0x80008000, SL, MVT::i32));
4882     Results.push_back(DAG.getNode(ISD::BITCAST, SL, MVT::v2f16, Op));
4883     return;
4884   }
4885   case ISD::FABS: {
4886     if (N->getValueType(0) != MVT::v2f16)
4887       break;
4888 
4889     SDLoc SL(N);
4890     SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::i32, N->getOperand(0));
4891 
4892     SDValue Op = DAG.getNode(ISD::AND, SL, MVT::i32,
4893                              BC,
4894                              DAG.getConstant(0x7fff7fff, SL, MVT::i32));
4895     Results.push_back(DAG.getNode(ISD::BITCAST, SL, MVT::v2f16, Op));
4896     return;
4897   }
4898   default:
4899     break;
4900   }
4901 }
4902 
4903 /// Helper function for LowerBRCOND
4904 static SDNode *findUser(SDValue Value, unsigned Opcode) {
4905 
4906   SDNode *Parent = Value.getNode();
4907   for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
4908        I != E; ++I) {
4909 
4910     if (I.getUse().get() != Value)
4911       continue;
4912 
4913     if (I->getOpcode() == Opcode)
4914       return *I;
4915   }
4916   return nullptr;
4917 }
4918 
4919 unsigned SITargetLowering::isCFIntrinsic(const SDNode *Intr) const {
4920   if (Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN) {
4921     switch (cast<ConstantSDNode>(Intr->getOperand(1))->getZExtValue()) {
4922     case Intrinsic::amdgcn_if:
4923       return AMDGPUISD::IF;
4924     case Intrinsic::amdgcn_else:
4925       return AMDGPUISD::ELSE;
4926     case Intrinsic::amdgcn_loop:
4927       return AMDGPUISD::LOOP;
4928     case Intrinsic::amdgcn_end_cf:
4929       llvm_unreachable("should not occur");
4930     default:
4931       return 0;
4932     }
4933   }
4934 
4935   // break, if_break, else_break are all only used as inputs to loop, not
4936   // directly as branch conditions.
4937   return 0;
4938 }
4939 
4940 bool SITargetLowering::shouldEmitFixup(const GlobalValue *GV) const {
4941   const Triple &TT = getTargetMachine().getTargetTriple();
4942   return (GV->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS ||
4943           GV->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS_32BIT) &&
4944          AMDGPU::shouldEmitConstantsToTextSection(TT);
4945 }
4946 
4947 bool SITargetLowering::shouldEmitGOTReloc(const GlobalValue *GV) const {
4948   // FIXME: Either avoid relying on address space here or change the default
4949   // address space for functions to avoid the explicit check.
4950   return (GV->getValueType()->isFunctionTy() ||
4951           !isNonGlobalAddrSpace(GV->getAddressSpace())) &&
4952          !shouldEmitFixup(GV) &&
4953          !getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV);
4954 }
4955 
4956 bool SITargetLowering::shouldEmitPCReloc(const GlobalValue *GV) const {
4957   return !shouldEmitFixup(GV) && !shouldEmitGOTReloc(GV);
4958 }
4959 
4960 bool SITargetLowering::shouldUseLDSConstAddress(const GlobalValue *GV) const {
4961   if (!GV->hasExternalLinkage())
4962     return true;
4963 
4964   const auto OS = getTargetMachine().getTargetTriple().getOS();
4965   return OS == Triple::AMDHSA || OS == Triple::AMDPAL;
4966 }
4967 
4968 /// This transforms the control flow intrinsics to get the branch destination as
4969 /// last parameter, also switches branch target with BR if the need arise
4970 SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
4971                                       SelectionDAG &DAG) const {
4972   SDLoc DL(BRCOND);
4973 
4974   SDNode *Intr = BRCOND.getOperand(1).getNode();
4975   SDValue Target = BRCOND.getOperand(2);
4976   SDNode *BR = nullptr;
4977   SDNode *SetCC = nullptr;
4978 
4979   if (Intr->getOpcode() == ISD::SETCC) {
4980     // As long as we negate the condition everything is fine
4981     SetCC = Intr;
4982     Intr = SetCC->getOperand(0).getNode();
4983 
4984   } else {
4985     // Get the target from BR if we don't negate the condition
4986     BR = findUser(BRCOND, ISD::BR);
4987     assert(BR && "brcond missing unconditional branch user");
4988     Target = BR->getOperand(1);
4989   }
4990 
4991   unsigned CFNode = isCFIntrinsic(Intr);
4992   if (CFNode == 0) {
4993     // This is a uniform branch so we don't need to legalize.
4994     return BRCOND;
4995   }
4996 
4997   bool HaveChain = Intr->getOpcode() == ISD::INTRINSIC_VOID ||
4998                    Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN;
4999 
5000   assert(!SetCC ||
5001         (SetCC->getConstantOperandVal(1) == 1 &&
5002          cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
5003                                                              ISD::SETNE));
5004 
5005   // operands of the new intrinsic call
5006   SmallVector<SDValue, 4> Ops;
5007   if (HaveChain)
5008     Ops.push_back(BRCOND.getOperand(0));
5009 
5010   Ops.append(Intr->op_begin() + (HaveChain ?  2 : 1), Intr->op_end());
5011   Ops.push_back(Target);
5012 
5013   ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end());
5014 
5015   // build the new intrinsic call
5016   SDNode *Result = DAG.getNode(CFNode, DL, DAG.getVTList(Res), Ops).getNode();
5017 
5018   if (!HaveChain) {
5019     SDValue Ops[] =  {
5020       SDValue(Result, 0),
5021       BRCOND.getOperand(0)
5022     };
5023 
5024     Result = DAG.getMergeValues(Ops, DL).getNode();
5025   }
5026 
5027   if (BR) {
5028     // Give the branch instruction our target
5029     SDValue Ops[] = {
5030       BR->getOperand(0),
5031       BRCOND.getOperand(2)
5032     };
5033     SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops);
5034     DAG.ReplaceAllUsesWith(BR, NewBR.getNode());
5035   }
5036 
5037   SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
5038 
5039   // Copy the intrinsic results to registers
5040   for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
5041     SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
5042     if (!CopyToReg)
5043       continue;
5044 
5045     Chain = DAG.getCopyToReg(
5046       Chain, DL,
5047       CopyToReg->getOperand(1),
5048       SDValue(Result, i - 1),
5049       SDValue());
5050 
5051     DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
5052   }
5053 
5054   // Remove the old intrinsic from the chain
5055   DAG.ReplaceAllUsesOfValueWith(
5056     SDValue(Intr, Intr->getNumValues() - 1),
5057     Intr->getOperand(0));
5058 
5059   return Chain;
5060 }
5061 
5062 SDValue SITargetLowering::LowerRETURNADDR(SDValue Op,
5063                                           SelectionDAG &DAG) const {
5064   MVT VT = Op.getSimpleValueType();
5065   SDLoc DL(Op);
5066   // Checking the depth
5067   if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() != 0)
5068     return DAG.getConstant(0, DL, VT);
5069 
5070   MachineFunction &MF = DAG.getMachineFunction();
5071   const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
5072   // Check for kernel and shader functions
5073   if (Info->isEntryFunction())
5074     return DAG.getConstant(0, DL, VT);
5075 
5076   MachineFrameInfo &MFI = MF.getFrameInfo();
5077   // There is a call to @llvm.returnaddress in this function
5078   MFI.setReturnAddressIsTaken(true);
5079 
5080   const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
5081   // Get the return address reg and mark it as an implicit live-in
5082   Register Reg = MF.addLiveIn(TRI->getReturnAddressReg(MF), getRegClassFor(VT, Op.getNode()->isDivergent()));
5083 
5084   return DAG.getCopyFromReg(DAG.getEntryNode(), DL, Reg, VT);
5085 }
5086 
5087 SDValue SITargetLowering::getFPExtOrFPRound(SelectionDAG &DAG,
5088                                             SDValue Op,
5089                                             const SDLoc &DL,
5090                                             EVT VT) const {
5091   return Op.getValueType().bitsLE(VT) ?
5092       DAG.getNode(ISD::FP_EXTEND, DL, VT, Op) :
5093     DAG.getNode(ISD::FP_ROUND, DL, VT, Op,
5094                 DAG.getTargetConstant(0, DL, MVT::i32));
5095 }
5096 
5097 SDValue SITargetLowering::lowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
5098   assert(Op.getValueType() == MVT::f16 &&
5099          "Do not know how to custom lower FP_ROUND for non-f16 type");
5100 
5101   SDValue Src = Op.getOperand(0);
5102   EVT SrcVT = Src.getValueType();
5103   if (SrcVT != MVT::f64)
5104     return Op;
5105 
5106   SDLoc DL(Op);
5107 
5108   SDValue FpToFp16 = DAG.getNode(ISD::FP_TO_FP16, DL, MVT::i32, Src);
5109   SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, MVT::i16, FpToFp16);
5110   return DAG.getNode(ISD::BITCAST, DL, MVT::f16, Trunc);
5111 }
5112 
5113 SDValue SITargetLowering::lowerFMINNUM_FMAXNUM(SDValue Op,
5114                                                SelectionDAG &DAG) const {
5115   EVT VT = Op.getValueType();
5116   const MachineFunction &MF = DAG.getMachineFunction();
5117   const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
5118   bool IsIEEEMode = Info->getMode().IEEE;
5119 
5120   // FIXME: Assert during selection that this is only selected for
5121   // ieee_mode. Currently a combine can produce the ieee version for non-ieee
5122   // mode functions, but this happens to be OK since it's only done in cases
5123   // where there is known no sNaN.
5124   if (IsIEEEMode)
5125     return expandFMINNUM_FMAXNUM(Op.getNode(), DAG);
5126 
5127   if (VT == MVT::v4f16)
5128     return splitBinaryVectorOp(Op, DAG);
5129   return Op;
5130 }
5131 
5132 SDValue SITargetLowering::lowerXMULO(SDValue Op, SelectionDAG &DAG) const {
5133   EVT VT = Op.getValueType();
5134   SDLoc SL(Op);
5135   SDValue LHS = Op.getOperand(0);
5136   SDValue RHS = Op.getOperand(1);
5137   bool isSigned = Op.getOpcode() == ISD::SMULO;
5138 
5139   if (ConstantSDNode *RHSC = isConstOrConstSplat(RHS)) {
5140     const APInt &C = RHSC->getAPIntValue();
5141     // mulo(X, 1 << S) -> { X << S, (X << S) >> S != X }
5142     if (C.isPowerOf2()) {
5143       // smulo(x, signed_min) is same as umulo(x, signed_min).
5144       bool UseArithShift = isSigned && !C.isMinSignedValue();
5145       SDValue ShiftAmt = DAG.getConstant(C.logBase2(), SL, MVT::i32);
5146       SDValue Result = DAG.getNode(ISD::SHL, SL, VT, LHS, ShiftAmt);
5147       SDValue Overflow = DAG.getSetCC(SL, MVT::i1,
5148           DAG.getNode(UseArithShift ? ISD::SRA : ISD::SRL,
5149                       SL, VT, Result, ShiftAmt),
5150           LHS, ISD::SETNE);
5151       return DAG.getMergeValues({ Result, Overflow }, SL);
5152     }
5153   }
5154 
5155   SDValue Result = DAG.getNode(ISD::MUL, SL, VT, LHS, RHS);
5156   SDValue Top = DAG.getNode(isSigned ? ISD::MULHS : ISD::MULHU,
5157                             SL, VT, LHS, RHS);
5158 
5159   SDValue Sign = isSigned
5160     ? DAG.getNode(ISD::SRA, SL, VT, Result,
5161                   DAG.getConstant(VT.getScalarSizeInBits() - 1, SL, MVT::i32))
5162     : DAG.getConstant(0, SL, VT);
5163   SDValue Overflow = DAG.getSetCC(SL, MVT::i1, Top, Sign, ISD::SETNE);
5164 
5165   return DAG.getMergeValues({ Result, Overflow }, SL);
5166 }
5167 
5168 SDValue SITargetLowering::lowerTRAP(SDValue Op, SelectionDAG &DAG) const {
5169   if (!Subtarget->isTrapHandlerEnabled() ||
5170       Subtarget->getTrapHandlerAbi() != GCNSubtarget::TrapHandlerAbi::AMDHSA)
5171     return lowerTrapEndpgm(Op, DAG);
5172 
5173   if (Optional<uint8_t> HsaAbiVer = AMDGPU::getHsaAbiVersion(Subtarget)) {
5174     switch (*HsaAbiVer) {
5175     case ELF::ELFABIVERSION_AMDGPU_HSA_V2:
5176     case ELF::ELFABIVERSION_AMDGPU_HSA_V3:
5177       return lowerTrapHsaQueuePtr(Op, DAG);
5178     case ELF::ELFABIVERSION_AMDGPU_HSA_V4:
5179       return Subtarget->supportsGetDoorbellID() ?
5180           lowerTrapHsa(Op, DAG) : lowerTrapHsaQueuePtr(Op, DAG);
5181     }
5182   }
5183 
5184   llvm_unreachable("Unknown trap handler");
5185 }
5186 
5187 SDValue SITargetLowering::lowerTrapEndpgm(
5188     SDValue Op, SelectionDAG &DAG) const {
5189   SDLoc SL(Op);
5190   SDValue Chain = Op.getOperand(0);
5191   return DAG.getNode(AMDGPUISD::ENDPGM, SL, MVT::Other, Chain);
5192 }
5193 
5194 SDValue SITargetLowering::lowerTrapHsaQueuePtr(
5195     SDValue Op, SelectionDAG &DAG) const {
5196   SDLoc SL(Op);
5197   SDValue Chain = Op.getOperand(0);
5198 
5199   MachineFunction &MF = DAG.getMachineFunction();
5200   SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
5201   Register UserSGPR = Info->getQueuePtrUserSGPR();
5202   assert(UserSGPR != AMDGPU::NoRegister);
5203   SDValue QueuePtr = CreateLiveInRegister(
5204     DAG, &AMDGPU::SReg_64RegClass, UserSGPR, MVT::i64);
5205   SDValue SGPR01 = DAG.getRegister(AMDGPU::SGPR0_SGPR1, MVT::i64);
5206   SDValue ToReg = DAG.getCopyToReg(Chain, SL, SGPR01,
5207                                    QueuePtr, SDValue());
5208 
5209   uint64_t TrapID = static_cast<uint64_t>(GCNSubtarget::TrapID::LLVMAMDHSATrap);
5210   SDValue Ops[] = {
5211     ToReg,
5212     DAG.getTargetConstant(TrapID, SL, MVT::i16),
5213     SGPR01,
5214     ToReg.getValue(1)
5215   };
5216   return DAG.getNode(AMDGPUISD::TRAP, SL, MVT::Other, Ops);
5217 }
5218 
5219 SDValue SITargetLowering::lowerTrapHsa(
5220     SDValue Op, SelectionDAG &DAG) const {
5221   SDLoc SL(Op);
5222   SDValue Chain = Op.getOperand(0);
5223 
5224   uint64_t TrapID = static_cast<uint64_t>(GCNSubtarget::TrapID::LLVMAMDHSATrap);
5225   SDValue Ops[] = {
5226     Chain,
5227     DAG.getTargetConstant(TrapID, SL, MVT::i16)
5228   };
5229   return DAG.getNode(AMDGPUISD::TRAP, SL, MVT::Other, Ops);
5230 }
5231 
5232 SDValue SITargetLowering::lowerDEBUGTRAP(SDValue Op, SelectionDAG &DAG) const {
5233   SDLoc SL(Op);
5234   SDValue Chain = Op.getOperand(0);
5235   MachineFunction &MF = DAG.getMachineFunction();
5236 
5237   if (!Subtarget->isTrapHandlerEnabled() ||
5238       Subtarget->getTrapHandlerAbi() != GCNSubtarget::TrapHandlerAbi::AMDHSA) {
5239     DiagnosticInfoUnsupported NoTrap(MF.getFunction(),
5240                                      "debugtrap handler not supported",
5241                                      Op.getDebugLoc(),
5242                                      DS_Warning);
5243     LLVMContext &Ctx = MF.getFunction().getContext();
5244     Ctx.diagnose(NoTrap);
5245     return Chain;
5246   }
5247 
5248   uint64_t TrapID = static_cast<uint64_t>(GCNSubtarget::TrapID::LLVMAMDHSADebugTrap);
5249   SDValue Ops[] = {
5250     Chain,
5251     DAG.getTargetConstant(TrapID, SL, MVT::i16)
5252   };
5253   return DAG.getNode(AMDGPUISD::TRAP, SL, MVT::Other, Ops);
5254 }
5255 
5256 SDValue SITargetLowering::getSegmentAperture(unsigned AS, const SDLoc &DL,
5257                                              SelectionDAG &DAG) const {
5258   // FIXME: Use inline constants (src_{shared, private}_base) instead.
5259   if (Subtarget->hasApertureRegs()) {
5260     unsigned Offset = AS == AMDGPUAS::LOCAL_ADDRESS ?
5261         AMDGPU::Hwreg::OFFSET_SRC_SHARED_BASE :
5262         AMDGPU::Hwreg::OFFSET_SRC_PRIVATE_BASE;
5263     unsigned WidthM1 = AS == AMDGPUAS::LOCAL_ADDRESS ?
5264         AMDGPU::Hwreg::WIDTH_M1_SRC_SHARED_BASE :
5265         AMDGPU::Hwreg::WIDTH_M1_SRC_PRIVATE_BASE;
5266     unsigned Encoding =
5267         AMDGPU::Hwreg::ID_MEM_BASES << AMDGPU::Hwreg::ID_SHIFT_ |
5268         Offset << AMDGPU::Hwreg::OFFSET_SHIFT_ |
5269         WidthM1 << AMDGPU::Hwreg::WIDTH_M1_SHIFT_;
5270 
5271     SDValue EncodingImm = DAG.getTargetConstant(Encoding, DL, MVT::i16);
5272     SDValue ApertureReg = SDValue(
5273         DAG.getMachineNode(AMDGPU::S_GETREG_B32, DL, MVT::i32, EncodingImm), 0);
5274     SDValue ShiftAmount = DAG.getTargetConstant(WidthM1 + 1, DL, MVT::i32);
5275     return DAG.getNode(ISD::SHL, DL, MVT::i32, ApertureReg, ShiftAmount);
5276   }
5277 
5278   MachineFunction &MF = DAG.getMachineFunction();
5279   SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
5280   Register UserSGPR = Info->getQueuePtrUserSGPR();
5281   assert(UserSGPR != AMDGPU::NoRegister);
5282 
5283   SDValue QueuePtr = CreateLiveInRegister(
5284     DAG, &AMDGPU::SReg_64RegClass, UserSGPR, MVT::i64);
5285 
5286   // Offset into amd_queue_t for group_segment_aperture_base_hi /
5287   // private_segment_aperture_base_hi.
5288   uint32_t StructOffset = (AS == AMDGPUAS::LOCAL_ADDRESS) ? 0x40 : 0x44;
5289 
5290   SDValue Ptr =
5291       DAG.getObjectPtrOffset(DL, QueuePtr, TypeSize::Fixed(StructOffset));
5292 
5293   // TODO: Use custom target PseudoSourceValue.
5294   // TODO: We should use the value from the IR intrinsic call, but it might not
5295   // be available and how do we get it?
5296   MachinePointerInfo PtrInfo(AMDGPUAS::CONSTANT_ADDRESS);
5297   return DAG.getLoad(MVT::i32, DL, QueuePtr.getValue(1), Ptr, PtrInfo,
5298                      commonAlignment(Align(64), StructOffset),
5299                      MachineMemOperand::MODereferenceable |
5300                          MachineMemOperand::MOInvariant);
5301 }
5302 
5303 SDValue SITargetLowering::lowerADDRSPACECAST(SDValue Op,
5304                                              SelectionDAG &DAG) const {
5305   SDLoc SL(Op);
5306   const AddrSpaceCastSDNode *ASC = cast<AddrSpaceCastSDNode>(Op);
5307 
5308   SDValue Src = ASC->getOperand(0);
5309   SDValue FlatNullPtr = DAG.getConstant(0, SL, MVT::i64);
5310 
5311   const AMDGPUTargetMachine &TM =
5312     static_cast<const AMDGPUTargetMachine &>(getTargetMachine());
5313 
5314   // flat -> local/private
5315   if (ASC->getSrcAddressSpace() == AMDGPUAS::FLAT_ADDRESS) {
5316     unsigned DestAS = ASC->getDestAddressSpace();
5317 
5318     if (DestAS == AMDGPUAS::LOCAL_ADDRESS ||
5319         DestAS == AMDGPUAS::PRIVATE_ADDRESS) {
5320       unsigned NullVal = TM.getNullPointerValue(DestAS);
5321       SDValue SegmentNullPtr = DAG.getConstant(NullVal, SL, MVT::i32);
5322       SDValue NonNull = DAG.getSetCC(SL, MVT::i1, Src, FlatNullPtr, ISD::SETNE);
5323       SDValue Ptr = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, Src);
5324 
5325       return DAG.getNode(ISD::SELECT, SL, MVT::i32,
5326                          NonNull, Ptr, SegmentNullPtr);
5327     }
5328   }
5329 
5330   // local/private -> flat
5331   if (ASC->getDestAddressSpace() == AMDGPUAS::FLAT_ADDRESS) {
5332     unsigned SrcAS = ASC->getSrcAddressSpace();
5333 
5334     if (SrcAS == AMDGPUAS::LOCAL_ADDRESS ||
5335         SrcAS == AMDGPUAS::PRIVATE_ADDRESS) {
5336       unsigned NullVal = TM.getNullPointerValue(SrcAS);
5337       SDValue SegmentNullPtr = DAG.getConstant(NullVal, SL, MVT::i32);
5338 
5339       SDValue NonNull
5340         = DAG.getSetCC(SL, MVT::i1, Src, SegmentNullPtr, ISD::SETNE);
5341 
5342       SDValue Aperture = getSegmentAperture(ASC->getSrcAddressSpace(), SL, DAG);
5343       SDValue CvtPtr
5344         = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, Src, Aperture);
5345 
5346       return DAG.getNode(ISD::SELECT, SL, MVT::i64, NonNull,
5347                          DAG.getNode(ISD::BITCAST, SL, MVT::i64, CvtPtr),
5348                          FlatNullPtr);
5349     }
5350   }
5351 
5352   if (ASC->getDestAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS_32BIT &&
5353       Src.getValueType() == MVT::i64)
5354     return DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, Src);
5355 
5356   // global <-> flat are no-ops and never emitted.
5357 
5358   const MachineFunction &MF = DAG.getMachineFunction();
5359   DiagnosticInfoUnsupported InvalidAddrSpaceCast(
5360     MF.getFunction(), "invalid addrspacecast", SL.getDebugLoc());
5361   DAG.getContext()->diagnose(InvalidAddrSpaceCast);
5362 
5363   return DAG.getUNDEF(ASC->getValueType(0));
5364 }
5365 
5366 // This lowers an INSERT_SUBVECTOR by extracting the individual elements from
5367 // the small vector and inserting them into the big vector. That is better than
5368 // the default expansion of doing it via a stack slot. Even though the use of
5369 // the stack slot would be optimized away afterwards, the stack slot itself
5370 // remains.
5371 SDValue SITargetLowering::lowerINSERT_SUBVECTOR(SDValue Op,
5372                                                 SelectionDAG &DAG) const {
5373   SDValue Vec = Op.getOperand(0);
5374   SDValue Ins = Op.getOperand(1);
5375   SDValue Idx = Op.getOperand(2);
5376   EVT VecVT = Vec.getValueType();
5377   EVT InsVT = Ins.getValueType();
5378   EVT EltVT = VecVT.getVectorElementType();
5379   unsigned InsNumElts = InsVT.getVectorNumElements();
5380   unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
5381   SDLoc SL(Op);
5382 
5383   for (unsigned I = 0; I != InsNumElts; ++I) {
5384     SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, Ins,
5385                               DAG.getConstant(I, SL, MVT::i32));
5386     Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, SL, VecVT, Vec, Elt,
5387                       DAG.getConstant(IdxVal + I, SL, MVT::i32));
5388   }
5389   return Vec;
5390 }
5391 
5392 SDValue SITargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op,
5393                                                  SelectionDAG &DAG) const {
5394   SDValue Vec = Op.getOperand(0);
5395   SDValue InsVal = Op.getOperand(1);
5396   SDValue Idx = Op.getOperand(2);
5397   EVT VecVT = Vec.getValueType();
5398   EVT EltVT = VecVT.getVectorElementType();
5399   unsigned VecSize = VecVT.getSizeInBits();
5400   unsigned EltSize = EltVT.getSizeInBits();
5401 
5402 
5403   assert(VecSize <= 64);
5404 
5405   unsigned NumElts = VecVT.getVectorNumElements();
5406   SDLoc SL(Op);
5407   auto KIdx = dyn_cast<ConstantSDNode>(Idx);
5408 
5409   if (NumElts == 4 && EltSize == 16 && KIdx) {
5410     SDValue BCVec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Vec);
5411 
5412     SDValue LoHalf = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BCVec,
5413                                  DAG.getConstant(0, SL, MVT::i32));
5414     SDValue HiHalf = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BCVec,
5415                                  DAG.getConstant(1, SL, MVT::i32));
5416 
5417     SDValue LoVec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i16, LoHalf);
5418     SDValue HiVec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i16, HiHalf);
5419 
5420     unsigned Idx = KIdx->getZExtValue();
5421     bool InsertLo = Idx < 2;
5422     SDValue InsHalf = DAG.getNode(ISD::INSERT_VECTOR_ELT, SL, MVT::v2i16,
5423       InsertLo ? LoVec : HiVec,
5424       DAG.getNode(ISD::BITCAST, SL, MVT::i16, InsVal),
5425       DAG.getConstant(InsertLo ? Idx : (Idx - 2), SL, MVT::i32));
5426 
5427     InsHalf = DAG.getNode(ISD::BITCAST, SL, MVT::i32, InsHalf);
5428 
5429     SDValue Concat = InsertLo ?
5430       DAG.getBuildVector(MVT::v2i32, SL, { InsHalf, HiHalf }) :
5431       DAG.getBuildVector(MVT::v2i32, SL, { LoHalf, InsHalf });
5432 
5433     return DAG.getNode(ISD::BITCAST, SL, VecVT, Concat);
5434   }
5435 
5436   if (isa<ConstantSDNode>(Idx))
5437     return SDValue();
5438 
5439   MVT IntVT = MVT::getIntegerVT(VecSize);
5440 
5441   // Avoid stack access for dynamic indexing.
5442   // v_bfi_b32 (v_bfm_b32 16, (shl idx, 16)), val, vec
5443 
5444   // Create a congruent vector with the target value in each element so that
5445   // the required element can be masked and ORed into the target vector.
5446   SDValue ExtVal = DAG.getNode(ISD::BITCAST, SL, IntVT,
5447                                DAG.getSplatBuildVector(VecVT, SL, InsVal));
5448 
5449   assert(isPowerOf2_32(EltSize));
5450   SDValue ScaleFactor = DAG.getConstant(Log2_32(EltSize), SL, MVT::i32);
5451 
5452   // Convert vector index to bit-index.
5453   SDValue ScaledIdx = DAG.getNode(ISD::SHL, SL, MVT::i32, Idx, ScaleFactor);
5454 
5455   SDValue BCVec = DAG.getNode(ISD::BITCAST, SL, IntVT, Vec);
5456   SDValue BFM = DAG.getNode(ISD::SHL, SL, IntVT,
5457                             DAG.getConstant(0xffff, SL, IntVT),
5458                             ScaledIdx);
5459 
5460   SDValue LHS = DAG.getNode(ISD::AND, SL, IntVT, BFM, ExtVal);
5461   SDValue RHS = DAG.getNode(ISD::AND, SL, IntVT,
5462                             DAG.getNOT(SL, BFM, IntVT), BCVec);
5463 
5464   SDValue BFI = DAG.getNode(ISD::OR, SL, IntVT, LHS, RHS);
5465   return DAG.getNode(ISD::BITCAST, SL, VecVT, BFI);
5466 }
5467 
5468 SDValue SITargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op,
5469                                                   SelectionDAG &DAG) const {
5470   SDLoc SL(Op);
5471 
5472   EVT ResultVT = Op.getValueType();
5473   SDValue Vec = Op.getOperand(0);
5474   SDValue Idx = Op.getOperand(1);
5475   EVT VecVT = Vec.getValueType();
5476   unsigned VecSize = VecVT.getSizeInBits();
5477   EVT EltVT = VecVT.getVectorElementType();
5478   assert(VecSize <= 64);
5479 
5480   DAGCombinerInfo DCI(DAG, AfterLegalizeVectorOps, true, nullptr);
5481 
5482   // Make sure we do any optimizations that will make it easier to fold
5483   // source modifiers before obscuring it with bit operations.
5484 
5485   // XXX - Why doesn't this get called when vector_shuffle is expanded?
5486   if (SDValue Combined = performExtractVectorEltCombine(Op.getNode(), DCI))
5487     return Combined;
5488 
5489   unsigned EltSize = EltVT.getSizeInBits();
5490   assert(isPowerOf2_32(EltSize));
5491 
5492   MVT IntVT = MVT::getIntegerVT(VecSize);
5493   SDValue ScaleFactor = DAG.getConstant(Log2_32(EltSize), SL, MVT::i32);
5494 
5495   // Convert vector index to bit-index (* EltSize)
5496   SDValue ScaledIdx = DAG.getNode(ISD::SHL, SL, MVT::i32, Idx, ScaleFactor);
5497 
5498   SDValue BC = DAG.getNode(ISD::BITCAST, SL, IntVT, Vec);
5499   SDValue Elt = DAG.getNode(ISD::SRL, SL, IntVT, BC, ScaledIdx);
5500 
5501   if (ResultVT == MVT::f16) {
5502     SDValue Result = DAG.getNode(ISD::TRUNCATE, SL, MVT::i16, Elt);
5503     return DAG.getNode(ISD::BITCAST, SL, ResultVT, Result);
5504   }
5505 
5506   return DAG.getAnyExtOrTrunc(Elt, SL, ResultVT);
5507 }
5508 
5509 static bool elementPairIsContiguous(ArrayRef<int> Mask, int Elt) {
5510   assert(Elt % 2 == 0);
5511   return Mask[Elt + 1] == Mask[Elt] + 1 && (Mask[Elt] % 2 == 0);
5512 }
5513 
5514 SDValue SITargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
5515                                               SelectionDAG &DAG) const {
5516   SDLoc SL(Op);
5517   EVT ResultVT = Op.getValueType();
5518   ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
5519 
5520   EVT PackVT = ResultVT.isInteger() ? MVT::v2i16 : MVT::v2f16;
5521   EVT EltVT = PackVT.getVectorElementType();
5522   int SrcNumElts = Op.getOperand(0).getValueType().getVectorNumElements();
5523 
5524   // vector_shuffle <0,1,6,7> lhs, rhs
5525   // -> concat_vectors (extract_subvector lhs, 0), (extract_subvector rhs, 2)
5526   //
5527   // vector_shuffle <6,7,2,3> lhs, rhs
5528   // -> concat_vectors (extract_subvector rhs, 2), (extract_subvector lhs, 2)
5529   //
5530   // vector_shuffle <6,7,0,1> lhs, rhs
5531   // -> concat_vectors (extract_subvector rhs, 2), (extract_subvector lhs, 0)
5532 
5533   // Avoid scalarizing when both halves are reading from consecutive elements.
5534   SmallVector<SDValue, 4> Pieces;
5535   for (int I = 0, N = ResultVT.getVectorNumElements(); I != N; I += 2) {
5536     if (elementPairIsContiguous(SVN->getMask(), I)) {
5537       const int Idx = SVN->getMaskElt(I);
5538       int VecIdx = Idx < SrcNumElts ? 0 : 1;
5539       int EltIdx = Idx < SrcNumElts ? Idx : Idx - SrcNumElts;
5540       SDValue SubVec = DAG.getNode(ISD::EXTRACT_SUBVECTOR, SL,
5541                                     PackVT, SVN->getOperand(VecIdx),
5542                                     DAG.getConstant(EltIdx, SL, MVT::i32));
5543       Pieces.push_back(SubVec);
5544     } else {
5545       const int Idx0 = SVN->getMaskElt(I);
5546       const int Idx1 = SVN->getMaskElt(I + 1);
5547       int VecIdx0 = Idx0 < SrcNumElts ? 0 : 1;
5548       int VecIdx1 = Idx1 < SrcNumElts ? 0 : 1;
5549       int EltIdx0 = Idx0 < SrcNumElts ? Idx0 : Idx0 - SrcNumElts;
5550       int EltIdx1 = Idx1 < SrcNumElts ? Idx1 : Idx1 - SrcNumElts;
5551 
5552       SDValue Vec0 = SVN->getOperand(VecIdx0);
5553       SDValue Elt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
5554                                  Vec0, DAG.getConstant(EltIdx0, SL, MVT::i32));
5555 
5556       SDValue Vec1 = SVN->getOperand(VecIdx1);
5557       SDValue Elt1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
5558                                  Vec1, DAG.getConstant(EltIdx1, SL, MVT::i32));
5559       Pieces.push_back(DAG.getBuildVector(PackVT, SL, { Elt0, Elt1 }));
5560     }
5561   }
5562 
5563   return DAG.getNode(ISD::CONCAT_VECTORS, SL, ResultVT, Pieces);
5564 }
5565 
5566 SDValue SITargetLowering::lowerBUILD_VECTOR(SDValue Op,
5567                                             SelectionDAG &DAG) const {
5568   SDLoc SL(Op);
5569   EVT VT = Op.getValueType();
5570 
5571   if (VT == MVT::v4i16 || VT == MVT::v4f16) {
5572     EVT HalfVT = MVT::getVectorVT(VT.getVectorElementType().getSimpleVT(), 2);
5573 
5574     // Turn into pair of packed build_vectors.
5575     // TODO: Special case for constants that can be materialized with s_mov_b64.
5576     SDValue Lo = DAG.getBuildVector(HalfVT, SL,
5577                                     { Op.getOperand(0), Op.getOperand(1) });
5578     SDValue Hi = DAG.getBuildVector(HalfVT, SL,
5579                                     { Op.getOperand(2), Op.getOperand(3) });
5580 
5581     SDValue CastLo = DAG.getNode(ISD::BITCAST, SL, MVT::i32, Lo);
5582     SDValue CastHi = DAG.getNode(ISD::BITCAST, SL, MVT::i32, Hi);
5583 
5584     SDValue Blend = DAG.getBuildVector(MVT::v2i32, SL, { CastLo, CastHi });
5585     return DAG.getNode(ISD::BITCAST, SL, VT, Blend);
5586   }
5587 
5588   assert(VT == MVT::v2f16 || VT == MVT::v2i16);
5589   assert(!Subtarget->hasVOP3PInsts() && "this should be legal");
5590 
5591   SDValue Lo = Op.getOperand(0);
5592   SDValue Hi = Op.getOperand(1);
5593 
5594   // Avoid adding defined bits with the zero_extend.
5595   if (Hi.isUndef()) {
5596     Lo = DAG.getNode(ISD::BITCAST, SL, MVT::i16, Lo);
5597     SDValue ExtLo = DAG.getNode(ISD::ANY_EXTEND, SL, MVT::i32, Lo);
5598     return DAG.getNode(ISD::BITCAST, SL, VT, ExtLo);
5599   }
5600 
5601   Hi = DAG.getNode(ISD::BITCAST, SL, MVT::i16, Hi);
5602   Hi = DAG.getNode(ISD::ZERO_EXTEND, SL, MVT::i32, Hi);
5603 
5604   SDValue ShlHi = DAG.getNode(ISD::SHL, SL, MVT::i32, Hi,
5605                               DAG.getConstant(16, SL, MVT::i32));
5606   if (Lo.isUndef())
5607     return DAG.getNode(ISD::BITCAST, SL, VT, ShlHi);
5608 
5609   Lo = DAG.getNode(ISD::BITCAST, SL, MVT::i16, Lo);
5610   Lo = DAG.getNode(ISD::ZERO_EXTEND, SL, MVT::i32, Lo);
5611 
5612   SDValue Or = DAG.getNode(ISD::OR, SL, MVT::i32, Lo, ShlHi);
5613   return DAG.getNode(ISD::BITCAST, SL, VT, Or);
5614 }
5615 
5616 bool
5617 SITargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
5618   // We can fold offsets for anything that doesn't require a GOT relocation.
5619   return (GA->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS ||
5620           GA->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS ||
5621           GA->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS_32BIT) &&
5622          !shouldEmitGOTReloc(GA->getGlobal());
5623 }
5624 
5625 static SDValue
5626 buildPCRelGlobalAddress(SelectionDAG &DAG, const GlobalValue *GV,
5627                         const SDLoc &DL, int64_t Offset, EVT PtrVT,
5628                         unsigned GAFlags = SIInstrInfo::MO_NONE) {
5629   assert(isInt<32>(Offset + 4) && "32-bit offset is expected!");
5630   // In order to support pc-relative addressing, the PC_ADD_REL_OFFSET SDNode is
5631   // lowered to the following code sequence:
5632   //
5633   // For constant address space:
5634   //   s_getpc_b64 s[0:1]
5635   //   s_add_u32 s0, s0, $symbol
5636   //   s_addc_u32 s1, s1, 0
5637   //
5638   //   s_getpc_b64 returns the address of the s_add_u32 instruction and then
5639   //   a fixup or relocation is emitted to replace $symbol with a literal
5640   //   constant, which is a pc-relative offset from the encoding of the $symbol
5641   //   operand to the global variable.
5642   //
5643   // For global address space:
5644   //   s_getpc_b64 s[0:1]
5645   //   s_add_u32 s0, s0, $symbol@{gotpc}rel32@lo
5646   //   s_addc_u32 s1, s1, $symbol@{gotpc}rel32@hi
5647   //
5648   //   s_getpc_b64 returns the address of the s_add_u32 instruction and then
5649   //   fixups or relocations are emitted to replace $symbol@*@lo and
5650   //   $symbol@*@hi with lower 32 bits and higher 32 bits of a literal constant,
5651   //   which is a 64-bit pc-relative offset from the encoding of the $symbol
5652   //   operand to the global variable.
5653   //
5654   // What we want here is an offset from the value returned by s_getpc
5655   // (which is the address of the s_add_u32 instruction) to the global
5656   // variable, but since the encoding of $symbol starts 4 bytes after the start
5657   // of the s_add_u32 instruction, we end up with an offset that is 4 bytes too
5658   // small. This requires us to add 4 to the global variable offset in order to
5659   // compute the correct address. Similarly for the s_addc_u32 instruction, the
5660   // encoding of $symbol starts 12 bytes after the start of the s_add_u32
5661   // instruction.
5662   SDValue PtrLo =
5663       DAG.getTargetGlobalAddress(GV, DL, MVT::i32, Offset + 4, GAFlags);
5664   SDValue PtrHi;
5665   if (GAFlags == SIInstrInfo::MO_NONE) {
5666     PtrHi = DAG.getTargetConstant(0, DL, MVT::i32);
5667   } else {
5668     PtrHi =
5669         DAG.getTargetGlobalAddress(GV, DL, MVT::i32, Offset + 12, GAFlags + 1);
5670   }
5671   return DAG.getNode(AMDGPUISD::PC_ADD_REL_OFFSET, DL, PtrVT, PtrLo, PtrHi);
5672 }
5673 
5674 SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
5675                                              SDValue Op,
5676                                              SelectionDAG &DAG) const {
5677   GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
5678   SDLoc DL(GSD);
5679   EVT PtrVT = Op.getValueType();
5680 
5681   const GlobalValue *GV = GSD->getGlobal();
5682   if ((GSD->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS &&
5683        shouldUseLDSConstAddress(GV)) ||
5684       GSD->getAddressSpace() == AMDGPUAS::REGION_ADDRESS ||
5685       GSD->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
5686     if (GSD->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS &&
5687         GV->hasExternalLinkage()) {
5688       Type *Ty = GV->getValueType();
5689       // HIP uses an unsized array `extern __shared__ T s[]` or similar
5690       // zero-sized type in other languages to declare the dynamic shared
5691       // memory which size is not known at the compile time. They will be
5692       // allocated by the runtime and placed directly after the static
5693       // allocated ones. They all share the same offset.
5694       if (DAG.getDataLayout().getTypeAllocSize(Ty).isZero()) {
5695         assert(PtrVT == MVT::i32 && "32-bit pointer is expected.");
5696         // Adjust alignment for that dynamic shared memory array.
5697         MFI->setDynLDSAlign(DAG.getDataLayout(), *cast<GlobalVariable>(GV));
5698         return SDValue(
5699             DAG.getMachineNode(AMDGPU::GET_GROUPSTATICSIZE, DL, PtrVT), 0);
5700       }
5701     }
5702     return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
5703   }
5704 
5705   if (GSD->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) {
5706     SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, GSD->getOffset(),
5707                                             SIInstrInfo::MO_ABS32_LO);
5708     return DAG.getNode(AMDGPUISD::LDS, DL, MVT::i32, GA);
5709   }
5710 
5711   if (shouldEmitFixup(GV))
5712     return buildPCRelGlobalAddress(DAG, GV, DL, GSD->getOffset(), PtrVT);
5713   else if (shouldEmitPCReloc(GV))
5714     return buildPCRelGlobalAddress(DAG, GV, DL, GSD->getOffset(), PtrVT,
5715                                    SIInstrInfo::MO_REL32);
5716 
5717   SDValue GOTAddr = buildPCRelGlobalAddress(DAG, GV, DL, 0, PtrVT,
5718                                             SIInstrInfo::MO_GOTPCREL32);
5719 
5720   Type *Ty = PtrVT.getTypeForEVT(*DAG.getContext());
5721   PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
5722   const DataLayout &DataLayout = DAG.getDataLayout();
5723   Align Alignment = DataLayout.getABITypeAlign(PtrTy);
5724   MachinePointerInfo PtrInfo
5725     = MachinePointerInfo::getGOT(DAG.getMachineFunction());
5726 
5727   return DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), GOTAddr, PtrInfo, Alignment,
5728                      MachineMemOperand::MODereferenceable |
5729                          MachineMemOperand::MOInvariant);
5730 }
5731 
5732 SDValue SITargetLowering::copyToM0(SelectionDAG &DAG, SDValue Chain,
5733                                    const SDLoc &DL, SDValue V) const {
5734   // We can't use S_MOV_B32 directly, because there is no way to specify m0 as
5735   // the destination register.
5736   //
5737   // We can't use CopyToReg, because MachineCSE won't combine COPY instructions,
5738   // so we will end up with redundant moves to m0.
5739   //
5740   // We use a pseudo to ensure we emit s_mov_b32 with m0 as the direct result.
5741 
5742   // A Null SDValue creates a glue result.
5743   SDNode *M0 = DAG.getMachineNode(AMDGPU::SI_INIT_M0, DL, MVT::Other, MVT::Glue,
5744                                   V, Chain);
5745   return SDValue(M0, 0);
5746 }
5747 
5748 SDValue SITargetLowering::lowerImplicitZextParam(SelectionDAG &DAG,
5749                                                  SDValue Op,
5750                                                  MVT VT,
5751                                                  unsigned Offset) const {
5752   SDLoc SL(Op);
5753   SDValue Param = lowerKernargMemParameter(
5754       DAG, MVT::i32, MVT::i32, SL, DAG.getEntryNode(), Offset, Align(4), false);
5755   // The local size values will have the hi 16-bits as zero.
5756   return DAG.getNode(ISD::AssertZext, SL, MVT::i32, Param,
5757                      DAG.getValueType(VT));
5758 }
5759 
5760 static SDValue emitNonHSAIntrinsicError(SelectionDAG &DAG, const SDLoc &DL,
5761                                         EVT VT) {
5762   DiagnosticInfoUnsupported BadIntrin(DAG.getMachineFunction().getFunction(),
5763                                       "non-hsa intrinsic with hsa target",
5764                                       DL.getDebugLoc());
5765   DAG.getContext()->diagnose(BadIntrin);
5766   return DAG.getUNDEF(VT);
5767 }
5768 
5769 static SDValue emitRemovedIntrinsicError(SelectionDAG &DAG, const SDLoc &DL,
5770                                          EVT VT) {
5771   DiagnosticInfoUnsupported BadIntrin(DAG.getMachineFunction().getFunction(),
5772                                       "intrinsic not supported on subtarget",
5773                                       DL.getDebugLoc());
5774   DAG.getContext()->diagnose(BadIntrin);
5775   return DAG.getUNDEF(VT);
5776 }
5777 
5778 static SDValue getBuildDwordsVector(SelectionDAG &DAG, SDLoc DL,
5779                                     ArrayRef<SDValue> Elts) {
5780   assert(!Elts.empty());
5781   MVT Type;
5782   unsigned NumElts;
5783 
5784   if (Elts.size() == 1) {
5785     Type = MVT::f32;
5786     NumElts = 1;
5787   } else if (Elts.size() == 2) {
5788     Type = MVT::v2f32;
5789     NumElts = 2;
5790   } else if (Elts.size() == 3) {
5791     Type = MVT::v3f32;
5792     NumElts = 3;
5793   } else if (Elts.size() <= 4) {
5794     Type = MVT::v4f32;
5795     NumElts = 4;
5796   } else if (Elts.size() <= 8) {
5797     Type = MVT::v8f32;
5798     NumElts = 8;
5799   } else {
5800     assert(Elts.size() <= 16);
5801     Type = MVT::v16f32;
5802     NumElts = 16;
5803   }
5804 
5805   SmallVector<SDValue, 16> VecElts(NumElts);
5806   for (unsigned i = 0; i < Elts.size(); ++i) {
5807     SDValue Elt = Elts[i];
5808     if (Elt.getValueType() != MVT::f32)
5809       Elt = DAG.getBitcast(MVT::f32, Elt);
5810     VecElts[i] = Elt;
5811   }
5812   for (unsigned i = Elts.size(); i < NumElts; ++i)
5813     VecElts[i] = DAG.getUNDEF(MVT::f32);
5814 
5815   if (NumElts == 1)
5816     return VecElts[0];
5817   return DAG.getBuildVector(Type, DL, VecElts);
5818 }
5819 
5820 static SDValue padEltsToUndef(SelectionDAG &DAG, const SDLoc &DL, EVT CastVT,
5821                               SDValue Src, int ExtraElts) {
5822   EVT SrcVT = Src.getValueType();
5823 
5824   SmallVector<SDValue, 8> Elts;
5825 
5826   if (SrcVT.isVector())
5827     DAG.ExtractVectorElements(Src, Elts);
5828   else
5829     Elts.push_back(Src);
5830 
5831   SDValue Undef = DAG.getUNDEF(SrcVT.getScalarType());
5832   while (ExtraElts--)
5833     Elts.push_back(Undef);
5834 
5835   return DAG.getBuildVector(CastVT, DL, Elts);
5836 }
5837 
5838 // Re-construct the required return value for a image load intrinsic.
5839 // This is more complicated due to the optional use TexFailCtrl which means the required
5840 // return type is an aggregate
5841 static SDValue constructRetValue(SelectionDAG &DAG,
5842                                  MachineSDNode *Result,
5843                                  ArrayRef<EVT> ResultTypes,
5844                                  bool IsTexFail, bool Unpacked, bool IsD16,
5845                                  int DMaskPop, int NumVDataDwords,
5846                                  const SDLoc &DL) {
5847   // Determine the required return type. This is the same regardless of IsTexFail flag
5848   EVT ReqRetVT = ResultTypes[0];
5849   int ReqRetNumElts = ReqRetVT.isVector() ? ReqRetVT.getVectorNumElements() : 1;
5850   int NumDataDwords = (!IsD16 || (IsD16 && Unpacked)) ?
5851     ReqRetNumElts : (ReqRetNumElts + 1) / 2;
5852 
5853   int MaskPopDwords = (!IsD16 || (IsD16 && Unpacked)) ?
5854     DMaskPop : (DMaskPop + 1) / 2;
5855 
5856   MVT DataDwordVT = NumDataDwords == 1 ?
5857     MVT::i32 : MVT::getVectorVT(MVT::i32, NumDataDwords);
5858 
5859   MVT MaskPopVT = MaskPopDwords == 1 ?
5860     MVT::i32 : MVT::getVectorVT(MVT::i32, MaskPopDwords);
5861 
5862   SDValue Data(Result, 0);
5863   SDValue TexFail;
5864 
5865   if (DMaskPop > 0 && Data.getValueType() != MaskPopVT) {
5866     SDValue ZeroIdx = DAG.getConstant(0, DL, MVT::i32);
5867     if (MaskPopVT.isVector()) {
5868       Data = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MaskPopVT,
5869                          SDValue(Result, 0), ZeroIdx);
5870     } else {
5871       Data = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MaskPopVT,
5872                          SDValue(Result, 0), ZeroIdx);
5873     }
5874   }
5875 
5876   if (DataDwordVT.isVector())
5877     Data = padEltsToUndef(DAG, DL, DataDwordVT, Data,
5878                           NumDataDwords - MaskPopDwords);
5879 
5880   if (IsD16)
5881     Data = adjustLoadValueTypeImpl(Data, ReqRetVT, DL, DAG, Unpacked);
5882 
5883   EVT LegalReqRetVT = ReqRetVT;
5884   if (!ReqRetVT.isVector()) {
5885     Data = DAG.getNode(ISD::TRUNCATE, DL, ReqRetVT.changeTypeToInteger(), Data);
5886   } else {
5887     // We need to widen the return vector to a legal type
5888     if ((ReqRetVT.getVectorNumElements() % 2) == 1 &&
5889         ReqRetVT.getVectorElementType().getSizeInBits() == 16) {
5890       LegalReqRetVT =
5891           EVT::getVectorVT(*DAG.getContext(), ReqRetVT.getVectorElementType(),
5892                            ReqRetVT.getVectorNumElements() + 1);
5893     }
5894   }
5895   Data = DAG.getNode(ISD::BITCAST, DL, LegalReqRetVT, Data);
5896 
5897   if (IsTexFail) {
5898     TexFail =
5899         DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, SDValue(Result, 0),
5900                     DAG.getConstant(MaskPopDwords, DL, MVT::i32));
5901 
5902     return DAG.getMergeValues({Data, TexFail, SDValue(Result, 1)}, DL);
5903   }
5904 
5905   if (Result->getNumValues() == 1)
5906     return Data;
5907 
5908   return DAG.getMergeValues({Data, SDValue(Result, 1)}, DL);
5909 }
5910 
5911 static bool parseTexFail(SDValue TexFailCtrl, SelectionDAG &DAG, SDValue *TFE,
5912                          SDValue *LWE, bool &IsTexFail) {
5913   auto TexFailCtrlConst = cast<ConstantSDNode>(TexFailCtrl.getNode());
5914 
5915   uint64_t Value = TexFailCtrlConst->getZExtValue();
5916   if (Value) {
5917     IsTexFail = true;
5918   }
5919 
5920   SDLoc DL(TexFailCtrlConst);
5921   *TFE = DAG.getTargetConstant((Value & 0x1) ? 1 : 0, DL, MVT::i32);
5922   Value &= ~(uint64_t)0x1;
5923   *LWE = DAG.getTargetConstant((Value & 0x2) ? 1 : 0, DL, MVT::i32);
5924   Value &= ~(uint64_t)0x2;
5925 
5926   return Value == 0;
5927 }
5928 
5929 static void packImageA16AddressToDwords(SelectionDAG &DAG, SDValue Op,
5930                                         MVT PackVectorVT,
5931                                         SmallVectorImpl<SDValue> &PackedAddrs,
5932                                         unsigned DimIdx, unsigned EndIdx,
5933                                         unsigned NumGradients) {
5934   SDLoc DL(Op);
5935   for (unsigned I = DimIdx; I < EndIdx; I++) {
5936     SDValue Addr = Op.getOperand(I);
5937 
5938     // Gradients are packed with undef for each coordinate.
5939     // In <hi 16 bit>,<lo 16 bit> notation, the registers look like this:
5940     // 1D: undef,dx/dh; undef,dx/dv
5941     // 2D: dy/dh,dx/dh; dy/dv,dx/dv
5942     // 3D: dy/dh,dx/dh; undef,dz/dh; dy/dv,dx/dv; undef,dz/dv
5943     if (((I + 1) >= EndIdx) ||
5944         ((NumGradients / 2) % 2 == 1 && (I == DimIdx + (NumGradients / 2) - 1 ||
5945                                          I == DimIdx + NumGradients - 1))) {
5946       if (Addr.getValueType() != MVT::i16)
5947         Addr = DAG.getBitcast(MVT::i16, Addr);
5948       Addr = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, Addr);
5949     } else {
5950       Addr = DAG.getBuildVector(PackVectorVT, DL, {Addr, Op.getOperand(I + 1)});
5951       I++;
5952     }
5953     Addr = DAG.getBitcast(MVT::f32, Addr);
5954     PackedAddrs.push_back(Addr);
5955   }
5956 }
5957 
5958 SDValue SITargetLowering::lowerImage(SDValue Op,
5959                                      const AMDGPU::ImageDimIntrinsicInfo *Intr,
5960                                      SelectionDAG &DAG, bool WithChain) const {
5961   SDLoc DL(Op);
5962   MachineFunction &MF = DAG.getMachineFunction();
5963   const GCNSubtarget* ST = &MF.getSubtarget<GCNSubtarget>();
5964   const AMDGPU::MIMGBaseOpcodeInfo *BaseOpcode =
5965       AMDGPU::getMIMGBaseOpcodeInfo(Intr->BaseOpcode);
5966   const AMDGPU::MIMGDimInfo *DimInfo = AMDGPU::getMIMGDimInfo(Intr->Dim);
5967   const AMDGPU::MIMGLZMappingInfo *LZMappingInfo =
5968       AMDGPU::getMIMGLZMappingInfo(Intr->BaseOpcode);
5969   const AMDGPU::MIMGMIPMappingInfo *MIPMappingInfo =
5970       AMDGPU::getMIMGMIPMappingInfo(Intr->BaseOpcode);
5971   unsigned IntrOpcode = Intr->BaseOpcode;
5972   bool IsGFX10Plus = AMDGPU::isGFX10Plus(*Subtarget);
5973 
5974   SmallVector<EVT, 3> ResultTypes(Op->values());
5975   SmallVector<EVT, 3> OrigResultTypes(Op->values());
5976   bool IsD16 = false;
5977   bool IsG16 = false;
5978   bool IsA16 = false;
5979   SDValue VData;
5980   int NumVDataDwords;
5981   bool AdjustRetType = false;
5982 
5983   // Offset of intrinsic arguments
5984   const unsigned ArgOffset = WithChain ? 2 : 1;
5985 
5986   unsigned DMask;
5987   unsigned DMaskLanes = 0;
5988 
5989   if (BaseOpcode->Atomic) {
5990     VData = Op.getOperand(2);
5991 
5992     bool Is64Bit = VData.getValueType() == MVT::i64;
5993     if (BaseOpcode->AtomicX2) {
5994       SDValue VData2 = Op.getOperand(3);
5995       VData = DAG.getBuildVector(Is64Bit ? MVT::v2i64 : MVT::v2i32, DL,
5996                                  {VData, VData2});
5997       if (Is64Bit)
5998         VData = DAG.getBitcast(MVT::v4i32, VData);
5999 
6000       ResultTypes[0] = Is64Bit ? MVT::v2i64 : MVT::v2i32;
6001       DMask = Is64Bit ? 0xf : 0x3;
6002       NumVDataDwords = Is64Bit ? 4 : 2;
6003     } else {
6004       DMask = Is64Bit ? 0x3 : 0x1;
6005       NumVDataDwords = Is64Bit ? 2 : 1;
6006     }
6007   } else {
6008     auto *DMaskConst =
6009         cast<ConstantSDNode>(Op.getOperand(ArgOffset + Intr->DMaskIndex));
6010     DMask = DMaskConst->getZExtValue();
6011     DMaskLanes = BaseOpcode->Gather4 ? 4 : countPopulation(DMask);
6012 
6013     if (BaseOpcode->Store) {
6014       VData = Op.getOperand(2);
6015 
6016       MVT StoreVT = VData.getSimpleValueType();
6017       if (StoreVT.getScalarType() == MVT::f16) {
6018         if (!Subtarget->hasD16Images() || !BaseOpcode->HasD16)
6019           return Op; // D16 is unsupported for this instruction
6020 
6021         IsD16 = true;
6022         VData = handleD16VData(VData, DAG, true);
6023       }
6024 
6025       NumVDataDwords = (VData.getValueType().getSizeInBits() + 31) / 32;
6026     } else {
6027       // Work out the num dwords based on the dmask popcount and underlying type
6028       // and whether packing is supported.
6029       MVT LoadVT = ResultTypes[0].getSimpleVT();
6030       if (LoadVT.getScalarType() == MVT::f16) {
6031         if (!Subtarget->hasD16Images() || !BaseOpcode->HasD16)
6032           return Op; // D16 is unsupported for this instruction
6033 
6034         IsD16 = true;
6035       }
6036 
6037       // Confirm that the return type is large enough for the dmask specified
6038       if ((LoadVT.isVector() && LoadVT.getVectorNumElements() < DMaskLanes) ||
6039           (!LoadVT.isVector() && DMaskLanes > 1))
6040           return Op;
6041 
6042       // The sq block of gfx8 and gfx9 do not estimate register use correctly
6043       // for d16 image_gather4, image_gather4_l, and image_gather4_lz
6044       // instructions.
6045       if (IsD16 && !Subtarget->hasUnpackedD16VMem() &&
6046           !(BaseOpcode->Gather4 && Subtarget->hasImageGather4D16Bug()))
6047         NumVDataDwords = (DMaskLanes + 1) / 2;
6048       else
6049         NumVDataDwords = DMaskLanes;
6050 
6051       AdjustRetType = true;
6052     }
6053   }
6054 
6055   unsigned VAddrEnd = ArgOffset + Intr->VAddrEnd;
6056   SmallVector<SDValue, 4> VAddrs;
6057 
6058   // Optimize _L to _LZ when _L is zero
6059   if (LZMappingInfo) {
6060     if (auto *ConstantLod = dyn_cast<ConstantFPSDNode>(
6061             Op.getOperand(ArgOffset + Intr->LodIndex))) {
6062       if (ConstantLod->isZero() || ConstantLod->isNegative()) {
6063         IntrOpcode = LZMappingInfo->LZ;  // set new opcode to _lz variant of _l
6064         VAddrEnd--;                      // remove 'lod'
6065       }
6066     }
6067   }
6068 
6069   // Optimize _mip away, when 'lod' is zero
6070   if (MIPMappingInfo) {
6071     if (auto *ConstantLod = dyn_cast<ConstantSDNode>(
6072             Op.getOperand(ArgOffset + Intr->MipIndex))) {
6073       if (ConstantLod->isNullValue()) {
6074         IntrOpcode = MIPMappingInfo->NONMIP;  // set new opcode to variant without _mip
6075         VAddrEnd--;                           // remove 'mip'
6076       }
6077     }
6078   }
6079 
6080   // Push back extra arguments.
6081   for (unsigned I = Intr->VAddrStart; I < Intr->GradientStart; I++)
6082     VAddrs.push_back(Op.getOperand(ArgOffset + I));
6083 
6084   // Check for 16 bit addresses or derivatives and pack if true.
6085   MVT VAddrVT =
6086       Op.getOperand(ArgOffset + Intr->GradientStart).getSimpleValueType();
6087   MVT VAddrScalarVT = VAddrVT.getScalarType();
6088   MVT PackVectorVT = VAddrScalarVT == MVT::f16 ? MVT::v2f16 : MVT::v2i16;
6089   IsG16 = VAddrScalarVT == MVT::f16 || VAddrScalarVT == MVT::i16;
6090 
6091   VAddrVT = Op.getOperand(ArgOffset + Intr->CoordStart).getSimpleValueType();
6092   VAddrScalarVT = VAddrVT.getScalarType();
6093   IsA16 = VAddrScalarVT == MVT::f16 || VAddrScalarVT == MVT::i16;
6094   if (IsA16 || IsG16) {
6095     if (IsA16) {
6096       if (!ST->hasA16()) {
6097         LLVM_DEBUG(dbgs() << "Failed to lower image intrinsic: Target does not "
6098                              "support 16 bit addresses\n");
6099         return Op;
6100       }
6101       if (!IsG16) {
6102         LLVM_DEBUG(
6103             dbgs() << "Failed to lower image intrinsic: 16 bit addresses "
6104                       "need 16 bit derivatives but got 32 bit derivatives\n");
6105         return Op;
6106       }
6107     } else if (!ST->hasG16()) {
6108       LLVM_DEBUG(dbgs() << "Failed to lower image intrinsic: Target does not "
6109                            "support 16 bit derivatives\n");
6110       return Op;
6111     }
6112 
6113     if (BaseOpcode->Gradients && !IsA16) {
6114       if (!ST->hasG16()) {
6115         LLVM_DEBUG(dbgs() << "Failed to lower image intrinsic: Target does not "
6116                              "support 16 bit derivatives\n");
6117         return Op;
6118       }
6119       // Activate g16
6120       const AMDGPU::MIMGG16MappingInfo *G16MappingInfo =
6121           AMDGPU::getMIMGG16MappingInfo(Intr->BaseOpcode);
6122       IntrOpcode = G16MappingInfo->G16; // set new opcode to variant with _g16
6123     }
6124 
6125     // Don't compress addresses for G16
6126     const int PackEndIdx = IsA16 ? VAddrEnd : (ArgOffset + Intr->CoordStart);
6127     packImageA16AddressToDwords(DAG, Op, PackVectorVT, VAddrs,
6128                                 ArgOffset + Intr->GradientStart, PackEndIdx,
6129                                 Intr->NumGradients);
6130 
6131     if (!IsA16) {
6132       // Add uncompressed address
6133       for (unsigned I = ArgOffset + Intr->CoordStart; I < VAddrEnd; I++)
6134         VAddrs.push_back(Op.getOperand(I));
6135     }
6136   } else {
6137     for (unsigned I = ArgOffset + Intr->GradientStart; I < VAddrEnd; I++)
6138       VAddrs.push_back(Op.getOperand(I));
6139   }
6140 
6141   // If the register allocator cannot place the address registers contiguously
6142   // without introducing moves, then using the non-sequential address encoding
6143   // is always preferable, since it saves VALU instructions and is usually a
6144   // wash in terms of code size or even better.
6145   //
6146   // However, we currently have no way of hinting to the register allocator that
6147   // MIMG addresses should be placed contiguously when it is possible to do so,
6148   // so force non-NSA for the common 2-address case as a heuristic.
6149   //
6150   // SIShrinkInstructions will convert NSA encodings to non-NSA after register
6151   // allocation when possible.
6152   bool UseNSA =
6153       ST->hasFeature(AMDGPU::FeatureNSAEncoding) && VAddrs.size() >= 3;
6154   SDValue VAddr;
6155   if (!UseNSA)
6156     VAddr = getBuildDwordsVector(DAG, DL, VAddrs);
6157 
6158   SDValue True = DAG.getTargetConstant(1, DL, MVT::i1);
6159   SDValue False = DAG.getTargetConstant(0, DL, MVT::i1);
6160   SDValue Unorm;
6161   if (!BaseOpcode->Sampler) {
6162     Unorm = True;
6163   } else {
6164     auto UnormConst =
6165         cast<ConstantSDNode>(Op.getOperand(ArgOffset + Intr->UnormIndex));
6166 
6167     Unorm = UnormConst->getZExtValue() ? True : False;
6168   }
6169 
6170   SDValue TFE;
6171   SDValue LWE;
6172   SDValue TexFail = Op.getOperand(ArgOffset + Intr->TexFailCtrlIndex);
6173   bool IsTexFail = false;
6174   if (!parseTexFail(TexFail, DAG, &TFE, &LWE, IsTexFail))
6175     return Op;
6176 
6177   if (IsTexFail) {
6178     if (!DMaskLanes) {
6179       // Expecting to get an error flag since TFC is on - and dmask is 0
6180       // Force dmask to be at least 1 otherwise the instruction will fail
6181       DMask = 0x1;
6182       DMaskLanes = 1;
6183       NumVDataDwords = 1;
6184     }
6185     NumVDataDwords += 1;
6186     AdjustRetType = true;
6187   }
6188 
6189   // Has something earlier tagged that the return type needs adjusting
6190   // This happens if the instruction is a load or has set TexFailCtrl flags
6191   if (AdjustRetType) {
6192     // NumVDataDwords reflects the true number of dwords required in the return type
6193     if (DMaskLanes == 0 && !BaseOpcode->Store) {
6194       // This is a no-op load. This can be eliminated
6195       SDValue Undef = DAG.getUNDEF(Op.getValueType());
6196       if (isa<MemSDNode>(Op))
6197         return DAG.getMergeValues({Undef, Op.getOperand(0)}, DL);
6198       return Undef;
6199     }
6200 
6201     EVT NewVT = NumVDataDwords > 1 ?
6202                   EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumVDataDwords)
6203                 : MVT::i32;
6204 
6205     ResultTypes[0] = NewVT;
6206     if (ResultTypes.size() == 3) {
6207       // Original result was aggregate type used for TexFailCtrl results
6208       // The actual instruction returns as a vector type which has now been
6209       // created. Remove the aggregate result.
6210       ResultTypes.erase(&ResultTypes[1]);
6211     }
6212   }
6213 
6214   unsigned CPol = cast<ConstantSDNode>(
6215       Op.getOperand(ArgOffset + Intr->CachePolicyIndex))->getZExtValue();
6216   if (BaseOpcode->Atomic)
6217     CPol |= AMDGPU::CPol::GLC; // TODO no-return optimization
6218   if (CPol & ~AMDGPU::CPol::ALL)
6219     return Op;
6220 
6221   SmallVector<SDValue, 26> Ops;
6222   if (BaseOpcode->Store || BaseOpcode->Atomic)
6223     Ops.push_back(VData); // vdata
6224   if (UseNSA)
6225     append_range(Ops, VAddrs);
6226   else
6227     Ops.push_back(VAddr);
6228   Ops.push_back(Op.getOperand(ArgOffset + Intr->RsrcIndex));
6229   if (BaseOpcode->Sampler)
6230     Ops.push_back(Op.getOperand(ArgOffset + Intr->SampIndex));
6231   Ops.push_back(DAG.getTargetConstant(DMask, DL, MVT::i32));
6232   if (IsGFX10Plus)
6233     Ops.push_back(DAG.getTargetConstant(DimInfo->Encoding, DL, MVT::i32));
6234   Ops.push_back(Unorm);
6235   Ops.push_back(DAG.getTargetConstant(CPol, DL, MVT::i32));
6236   Ops.push_back(IsA16 &&  // r128, a16 for gfx9
6237                 ST->hasFeature(AMDGPU::FeatureR128A16) ? True : False);
6238   if (IsGFX10Plus)
6239     Ops.push_back(IsA16 ? True : False);
6240   if (!Subtarget->hasGFX90AInsts()) {
6241     Ops.push_back(TFE); //tfe
6242   } else if (cast<ConstantSDNode>(TFE)->getZExtValue()) {
6243     report_fatal_error("TFE is not supported on this GPU");
6244   }
6245   Ops.push_back(LWE); // lwe
6246   if (!IsGFX10Plus)
6247     Ops.push_back(DimInfo->DA ? True : False);
6248   if (BaseOpcode->HasD16)
6249     Ops.push_back(IsD16 ? True : False);
6250   if (isa<MemSDNode>(Op))
6251     Ops.push_back(Op.getOperand(0)); // chain
6252 
6253   int NumVAddrDwords =
6254       UseNSA ? VAddrs.size() : VAddr.getValueType().getSizeInBits() / 32;
6255   int Opcode = -1;
6256 
6257   if (IsGFX10Plus) {
6258     Opcode = AMDGPU::getMIMGOpcode(IntrOpcode,
6259                                    UseNSA ? AMDGPU::MIMGEncGfx10NSA
6260                                           : AMDGPU::MIMGEncGfx10Default,
6261                                    NumVDataDwords, NumVAddrDwords);
6262   } else {
6263     if (Subtarget->hasGFX90AInsts()) {
6264       Opcode = AMDGPU::getMIMGOpcode(IntrOpcode, AMDGPU::MIMGEncGfx90a,
6265                                      NumVDataDwords, NumVAddrDwords);
6266       if (Opcode == -1)
6267         report_fatal_error(
6268             "requested image instruction is not supported on this GPU");
6269     }
6270     if (Opcode == -1 &&
6271         Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
6272       Opcode = AMDGPU::getMIMGOpcode(IntrOpcode, AMDGPU::MIMGEncGfx8,
6273                                      NumVDataDwords, NumVAddrDwords);
6274     if (Opcode == -1)
6275       Opcode = AMDGPU::getMIMGOpcode(IntrOpcode, AMDGPU::MIMGEncGfx6,
6276                                      NumVDataDwords, NumVAddrDwords);
6277   }
6278   assert(Opcode != -1);
6279 
6280   MachineSDNode *NewNode = DAG.getMachineNode(Opcode, DL, ResultTypes, Ops);
6281   if (auto MemOp = dyn_cast<MemSDNode>(Op)) {
6282     MachineMemOperand *MemRef = MemOp->getMemOperand();
6283     DAG.setNodeMemRefs(NewNode, {MemRef});
6284   }
6285 
6286   if (BaseOpcode->AtomicX2) {
6287     SmallVector<SDValue, 1> Elt;
6288     DAG.ExtractVectorElements(SDValue(NewNode, 0), Elt, 0, 1);
6289     return DAG.getMergeValues({Elt[0], SDValue(NewNode, 1)}, DL);
6290   }
6291   if (BaseOpcode->Store)
6292     return SDValue(NewNode, 0);
6293   return constructRetValue(DAG, NewNode,
6294                            OrigResultTypes, IsTexFail,
6295                            Subtarget->hasUnpackedD16VMem(), IsD16,
6296                            DMaskLanes, NumVDataDwords, DL);
6297 }
6298 
6299 SDValue SITargetLowering::lowerSBuffer(EVT VT, SDLoc DL, SDValue Rsrc,
6300                                        SDValue Offset, SDValue CachePolicy,
6301                                        SelectionDAG &DAG) const {
6302   MachineFunction &MF = DAG.getMachineFunction();
6303 
6304   const DataLayout &DataLayout = DAG.getDataLayout();
6305   Align Alignment =
6306       DataLayout.getABITypeAlign(VT.getTypeForEVT(*DAG.getContext()));
6307 
6308   MachineMemOperand *MMO = MF.getMachineMemOperand(
6309       MachinePointerInfo(),
6310       MachineMemOperand::MOLoad | MachineMemOperand::MODereferenceable |
6311           MachineMemOperand::MOInvariant,
6312       VT.getStoreSize(), Alignment);
6313 
6314   if (!Offset->isDivergent()) {
6315     SDValue Ops[] = {
6316         Rsrc,
6317         Offset, // Offset
6318         CachePolicy
6319     };
6320 
6321     // Widen vec3 load to vec4.
6322     if (VT.isVector() && VT.getVectorNumElements() == 3) {
6323       EVT WidenedVT =
6324           EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(), 4);
6325       auto WidenedOp = DAG.getMemIntrinsicNode(
6326           AMDGPUISD::SBUFFER_LOAD, DL, DAG.getVTList(WidenedVT), Ops, WidenedVT,
6327           MF.getMachineMemOperand(MMO, 0, WidenedVT.getStoreSize()));
6328       auto Subvector = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, WidenedOp,
6329                                    DAG.getVectorIdxConstant(0, DL));
6330       return Subvector;
6331     }
6332 
6333     return DAG.getMemIntrinsicNode(AMDGPUISD::SBUFFER_LOAD, DL,
6334                                    DAG.getVTList(VT), Ops, VT, MMO);
6335   }
6336 
6337   // We have a divergent offset. Emit a MUBUF buffer load instead. We can
6338   // assume that the buffer is unswizzled.
6339   SmallVector<SDValue, 4> Loads;
6340   unsigned NumLoads = 1;
6341   MVT LoadVT = VT.getSimpleVT();
6342   unsigned NumElts = LoadVT.isVector() ? LoadVT.getVectorNumElements() : 1;
6343   assert((LoadVT.getScalarType() == MVT::i32 ||
6344           LoadVT.getScalarType() == MVT::f32));
6345 
6346   if (NumElts == 8 || NumElts == 16) {
6347     NumLoads = NumElts / 4;
6348     LoadVT = MVT::getVectorVT(LoadVT.getScalarType(), 4);
6349   }
6350 
6351   SDVTList VTList = DAG.getVTList({LoadVT, MVT::Glue});
6352   SDValue Ops[] = {
6353       DAG.getEntryNode(),                               // Chain
6354       Rsrc,                                             // rsrc
6355       DAG.getConstant(0, DL, MVT::i32),                 // vindex
6356       {},                                               // voffset
6357       {},                                               // soffset
6358       {},                                               // offset
6359       CachePolicy,                                      // cachepolicy
6360       DAG.getTargetConstant(0, DL, MVT::i1),            // idxen
6361   };
6362 
6363   // Use the alignment to ensure that the required offsets will fit into the
6364   // immediate offsets.
6365   setBufferOffsets(Offset, DAG, &Ops[3],
6366                    NumLoads > 1 ? Align(16 * NumLoads) : Align(4));
6367 
6368   uint64_t InstOffset = cast<ConstantSDNode>(Ops[5])->getZExtValue();
6369   for (unsigned i = 0; i < NumLoads; ++i) {
6370     Ops[5] = DAG.getTargetConstant(InstOffset + 16 * i, DL, MVT::i32);
6371     Loads.push_back(getMemIntrinsicNode(AMDGPUISD::BUFFER_LOAD, DL, VTList, Ops,
6372                                         LoadVT, MMO, DAG));
6373   }
6374 
6375   if (NumElts == 8 || NumElts == 16)
6376     return DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, Loads);
6377 
6378   return Loads[0];
6379 }
6380 
6381 SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
6382                                                   SelectionDAG &DAG) const {
6383   MachineFunction &MF = DAG.getMachineFunction();
6384   auto MFI = MF.getInfo<SIMachineFunctionInfo>();
6385 
6386   EVT VT = Op.getValueType();
6387   SDLoc DL(Op);
6388   unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6389 
6390   // TODO: Should this propagate fast-math-flags?
6391 
6392   switch (IntrinsicID) {
6393   case Intrinsic::amdgcn_implicit_buffer_ptr: {
6394     if (getSubtarget()->isAmdHsaOrMesa(MF.getFunction()))
6395       return emitNonHSAIntrinsicError(DAG, DL, VT);
6396     return getPreloadedValue(DAG, *MFI, VT,
6397                              AMDGPUFunctionArgInfo::IMPLICIT_BUFFER_PTR);
6398   }
6399   case Intrinsic::amdgcn_dispatch_ptr:
6400   case Intrinsic::amdgcn_queue_ptr: {
6401     if (!Subtarget->isAmdHsaOrMesa(MF.getFunction())) {
6402       DiagnosticInfoUnsupported BadIntrin(
6403           MF.getFunction(), "unsupported hsa intrinsic without hsa target",
6404           DL.getDebugLoc());
6405       DAG.getContext()->diagnose(BadIntrin);
6406       return DAG.getUNDEF(VT);
6407     }
6408 
6409     auto RegID = IntrinsicID == Intrinsic::amdgcn_dispatch_ptr ?
6410       AMDGPUFunctionArgInfo::DISPATCH_PTR : AMDGPUFunctionArgInfo::QUEUE_PTR;
6411     return getPreloadedValue(DAG, *MFI, VT, RegID);
6412   }
6413   case Intrinsic::amdgcn_implicitarg_ptr: {
6414     if (MFI->isEntryFunction())
6415       return getImplicitArgPtr(DAG, DL);
6416     return getPreloadedValue(DAG, *MFI, VT,
6417                              AMDGPUFunctionArgInfo::IMPLICIT_ARG_PTR);
6418   }
6419   case Intrinsic::amdgcn_kernarg_segment_ptr: {
6420     if (!AMDGPU::isKernel(MF.getFunction().getCallingConv())) {
6421       // This only makes sense to call in a kernel, so just lower to null.
6422       return DAG.getConstant(0, DL, VT);
6423     }
6424 
6425     return getPreloadedValue(DAG, *MFI, VT,
6426                              AMDGPUFunctionArgInfo::KERNARG_SEGMENT_PTR);
6427   }
6428   case Intrinsic::amdgcn_dispatch_id: {
6429     return getPreloadedValue(DAG, *MFI, VT, AMDGPUFunctionArgInfo::DISPATCH_ID);
6430   }
6431   case Intrinsic::amdgcn_rcp:
6432     return DAG.getNode(AMDGPUISD::RCP, DL, VT, Op.getOperand(1));
6433   case Intrinsic::amdgcn_rsq:
6434     return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
6435   case Intrinsic::amdgcn_rsq_legacy:
6436     if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
6437       return emitRemovedIntrinsicError(DAG, DL, VT);
6438     return SDValue();
6439   case Intrinsic::amdgcn_rcp_legacy:
6440     if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
6441       return emitRemovedIntrinsicError(DAG, DL, VT);
6442     return DAG.getNode(AMDGPUISD::RCP_LEGACY, DL, VT, Op.getOperand(1));
6443   case Intrinsic::amdgcn_rsq_clamp: {
6444     if (Subtarget->getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
6445       return DAG.getNode(AMDGPUISD::RSQ_CLAMP, DL, VT, Op.getOperand(1));
6446 
6447     Type *Type = VT.getTypeForEVT(*DAG.getContext());
6448     APFloat Max = APFloat::getLargest(Type->getFltSemantics());
6449     APFloat Min = APFloat::getLargest(Type->getFltSemantics(), true);
6450 
6451     SDValue Rsq = DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
6452     SDValue Tmp = DAG.getNode(ISD::FMINNUM, DL, VT, Rsq,
6453                               DAG.getConstantFP(Max, DL, VT));
6454     return DAG.getNode(ISD::FMAXNUM, DL, VT, Tmp,
6455                        DAG.getConstantFP(Min, DL, VT));
6456   }
6457   case Intrinsic::r600_read_ngroups_x:
6458     if (Subtarget->isAmdHsaOS())
6459       return emitNonHSAIntrinsicError(DAG, DL, VT);
6460 
6461     return lowerKernargMemParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
6462                                     SI::KernelInputOffsets::NGROUPS_X, Align(4),
6463                                     false);
6464   case Intrinsic::r600_read_ngroups_y:
6465     if (Subtarget->isAmdHsaOS())
6466       return emitNonHSAIntrinsicError(DAG, DL, VT);
6467 
6468     return lowerKernargMemParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
6469                                     SI::KernelInputOffsets::NGROUPS_Y, Align(4),
6470                                     false);
6471   case Intrinsic::r600_read_ngroups_z:
6472     if (Subtarget->isAmdHsaOS())
6473       return emitNonHSAIntrinsicError(DAG, DL, VT);
6474 
6475     return lowerKernargMemParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
6476                                     SI::KernelInputOffsets::NGROUPS_Z, Align(4),
6477                                     false);
6478   case Intrinsic::r600_read_global_size_x:
6479     if (Subtarget->isAmdHsaOS())
6480       return emitNonHSAIntrinsicError(DAG, DL, VT);
6481 
6482     return lowerKernargMemParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
6483                                     SI::KernelInputOffsets::GLOBAL_SIZE_X,
6484                                     Align(4), false);
6485   case Intrinsic::r600_read_global_size_y:
6486     if (Subtarget->isAmdHsaOS())
6487       return emitNonHSAIntrinsicError(DAG, DL, VT);
6488 
6489     return lowerKernargMemParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
6490                                     SI::KernelInputOffsets::GLOBAL_SIZE_Y,
6491                                     Align(4), false);
6492   case Intrinsic::r600_read_global_size_z:
6493     if (Subtarget->isAmdHsaOS())
6494       return emitNonHSAIntrinsicError(DAG, DL, VT);
6495 
6496     return lowerKernargMemParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
6497                                     SI::KernelInputOffsets::GLOBAL_SIZE_Z,
6498                                     Align(4), false);
6499   case Intrinsic::r600_read_local_size_x:
6500     if (Subtarget->isAmdHsaOS())
6501       return emitNonHSAIntrinsicError(DAG, DL, VT);
6502 
6503     return lowerImplicitZextParam(DAG, Op, MVT::i16,
6504                                   SI::KernelInputOffsets::LOCAL_SIZE_X);
6505   case Intrinsic::r600_read_local_size_y:
6506     if (Subtarget->isAmdHsaOS())
6507       return emitNonHSAIntrinsicError(DAG, DL, VT);
6508 
6509     return lowerImplicitZextParam(DAG, Op, MVT::i16,
6510                                   SI::KernelInputOffsets::LOCAL_SIZE_Y);
6511   case Intrinsic::r600_read_local_size_z:
6512     if (Subtarget->isAmdHsaOS())
6513       return emitNonHSAIntrinsicError(DAG, DL, VT);
6514 
6515     return lowerImplicitZextParam(DAG, Op, MVT::i16,
6516                                   SI::KernelInputOffsets::LOCAL_SIZE_Z);
6517   case Intrinsic::amdgcn_workgroup_id_x:
6518     return getPreloadedValue(DAG, *MFI, VT,
6519                              AMDGPUFunctionArgInfo::WORKGROUP_ID_X);
6520   case Intrinsic::amdgcn_workgroup_id_y:
6521     return getPreloadedValue(DAG, *MFI, VT,
6522                              AMDGPUFunctionArgInfo::WORKGROUP_ID_Y);
6523   case Intrinsic::amdgcn_workgroup_id_z:
6524     return getPreloadedValue(DAG, *MFI, VT,
6525                              AMDGPUFunctionArgInfo::WORKGROUP_ID_Z);
6526   case Intrinsic::amdgcn_workitem_id_x:
6527     return loadInputValue(DAG, &AMDGPU::VGPR_32RegClass, MVT::i32,
6528                           SDLoc(DAG.getEntryNode()),
6529                           MFI->getArgInfo().WorkItemIDX);
6530   case Intrinsic::amdgcn_workitem_id_y:
6531     return loadInputValue(DAG, &AMDGPU::VGPR_32RegClass, MVT::i32,
6532                           SDLoc(DAG.getEntryNode()),
6533                           MFI->getArgInfo().WorkItemIDY);
6534   case Intrinsic::amdgcn_workitem_id_z:
6535     return loadInputValue(DAG, &AMDGPU::VGPR_32RegClass, MVT::i32,
6536                           SDLoc(DAG.getEntryNode()),
6537                           MFI->getArgInfo().WorkItemIDZ);
6538   case Intrinsic::amdgcn_wavefrontsize:
6539     return DAG.getConstant(MF.getSubtarget<GCNSubtarget>().getWavefrontSize(),
6540                            SDLoc(Op), MVT::i32);
6541   case Intrinsic::amdgcn_s_buffer_load: {
6542     unsigned CPol = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
6543     if (CPol & ~AMDGPU::CPol::ALL)
6544       return Op;
6545     return lowerSBuffer(VT, DL, Op.getOperand(1), Op.getOperand(2), Op.getOperand(3),
6546                         DAG);
6547   }
6548   case Intrinsic::amdgcn_fdiv_fast:
6549     return lowerFDIV_FAST(Op, DAG);
6550   case Intrinsic::amdgcn_sin:
6551     return DAG.getNode(AMDGPUISD::SIN_HW, DL, VT, Op.getOperand(1));
6552 
6553   case Intrinsic::amdgcn_cos:
6554     return DAG.getNode(AMDGPUISD::COS_HW, DL, VT, Op.getOperand(1));
6555 
6556   case Intrinsic::amdgcn_mul_u24:
6557     return DAG.getNode(AMDGPUISD::MUL_U24, DL, VT, Op.getOperand(1), Op.getOperand(2));
6558   case Intrinsic::amdgcn_mul_i24:
6559     return DAG.getNode(AMDGPUISD::MUL_I24, DL, VT, Op.getOperand(1), Op.getOperand(2));
6560 
6561   case Intrinsic::amdgcn_log_clamp: {
6562     if (Subtarget->getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
6563       return SDValue();
6564 
6565     return emitRemovedIntrinsicError(DAG, DL, VT);
6566   }
6567   case Intrinsic::amdgcn_ldexp:
6568     return DAG.getNode(AMDGPUISD::LDEXP, DL, VT,
6569                        Op.getOperand(1), Op.getOperand(2));
6570 
6571   case Intrinsic::amdgcn_fract:
6572     return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1));
6573 
6574   case Intrinsic::amdgcn_class:
6575     return DAG.getNode(AMDGPUISD::FP_CLASS, DL, VT,
6576                        Op.getOperand(1), Op.getOperand(2));
6577   case Intrinsic::amdgcn_div_fmas:
6578     return DAG.getNode(AMDGPUISD::DIV_FMAS, DL, VT,
6579                        Op.getOperand(1), Op.getOperand(2), Op.getOperand(3),
6580                        Op.getOperand(4));
6581 
6582   case Intrinsic::amdgcn_div_fixup:
6583     return DAG.getNode(AMDGPUISD::DIV_FIXUP, DL, VT,
6584                        Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
6585 
6586   case Intrinsic::amdgcn_div_scale: {
6587     const ConstantSDNode *Param = cast<ConstantSDNode>(Op.getOperand(3));
6588 
6589     // Translate to the operands expected by the machine instruction. The
6590     // first parameter must be the same as the first instruction.
6591     SDValue Numerator = Op.getOperand(1);
6592     SDValue Denominator = Op.getOperand(2);
6593 
6594     // Note this order is opposite of the machine instruction's operations,
6595     // which is s0.f = Quotient, s1.f = Denominator, s2.f = Numerator. The
6596     // intrinsic has the numerator as the first operand to match a normal
6597     // division operation.
6598 
6599     SDValue Src0 = Param->isAllOnesValue() ? Numerator : Denominator;
6600 
6601     return DAG.getNode(AMDGPUISD::DIV_SCALE, DL, Op->getVTList(), Src0,
6602                        Denominator, Numerator);
6603   }
6604   case Intrinsic::amdgcn_icmp: {
6605     // There is a Pat that handles this variant, so return it as-is.
6606     if (Op.getOperand(1).getValueType() == MVT::i1 &&
6607         Op.getConstantOperandVal(2) == 0 &&
6608         Op.getConstantOperandVal(3) == ICmpInst::Predicate::ICMP_NE)
6609       return Op;
6610     return lowerICMPIntrinsic(*this, Op.getNode(), DAG);
6611   }
6612   case Intrinsic::amdgcn_fcmp: {
6613     return lowerFCMPIntrinsic(*this, Op.getNode(), DAG);
6614   }
6615   case Intrinsic::amdgcn_ballot:
6616     return lowerBALLOTIntrinsic(*this, Op.getNode(), DAG);
6617   case Intrinsic::amdgcn_fmed3:
6618     return DAG.getNode(AMDGPUISD::FMED3, DL, VT,
6619                        Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
6620   case Intrinsic::amdgcn_fdot2:
6621     return DAG.getNode(AMDGPUISD::FDOT2, DL, VT,
6622                        Op.getOperand(1), Op.getOperand(2), Op.getOperand(3),
6623                        Op.getOperand(4));
6624   case Intrinsic::amdgcn_fmul_legacy:
6625     return DAG.getNode(AMDGPUISD::FMUL_LEGACY, DL, VT,
6626                        Op.getOperand(1), Op.getOperand(2));
6627   case Intrinsic::amdgcn_sffbh:
6628     return DAG.getNode(AMDGPUISD::FFBH_I32, DL, VT, Op.getOperand(1));
6629   case Intrinsic::amdgcn_sbfe:
6630     return DAG.getNode(AMDGPUISD::BFE_I32, DL, VT,
6631                        Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
6632   case Intrinsic::amdgcn_ubfe:
6633     return DAG.getNode(AMDGPUISD::BFE_U32, DL, VT,
6634                        Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
6635   case Intrinsic::amdgcn_cvt_pkrtz:
6636   case Intrinsic::amdgcn_cvt_pknorm_i16:
6637   case Intrinsic::amdgcn_cvt_pknorm_u16:
6638   case Intrinsic::amdgcn_cvt_pk_i16:
6639   case Intrinsic::amdgcn_cvt_pk_u16: {
6640     // FIXME: Stop adding cast if v2f16/v2i16 are legal.
6641     EVT VT = Op.getValueType();
6642     unsigned Opcode;
6643 
6644     if (IntrinsicID == Intrinsic::amdgcn_cvt_pkrtz)
6645       Opcode = AMDGPUISD::CVT_PKRTZ_F16_F32;
6646     else if (IntrinsicID == Intrinsic::amdgcn_cvt_pknorm_i16)
6647       Opcode = AMDGPUISD::CVT_PKNORM_I16_F32;
6648     else if (IntrinsicID == Intrinsic::amdgcn_cvt_pknorm_u16)
6649       Opcode = AMDGPUISD::CVT_PKNORM_U16_F32;
6650     else if (IntrinsicID == Intrinsic::amdgcn_cvt_pk_i16)
6651       Opcode = AMDGPUISD::CVT_PK_I16_I32;
6652     else
6653       Opcode = AMDGPUISD::CVT_PK_U16_U32;
6654 
6655     if (isTypeLegal(VT))
6656       return DAG.getNode(Opcode, DL, VT, Op.getOperand(1), Op.getOperand(2));
6657 
6658     SDValue Node = DAG.getNode(Opcode, DL, MVT::i32,
6659                                Op.getOperand(1), Op.getOperand(2));
6660     return DAG.getNode(ISD::BITCAST, DL, VT, Node);
6661   }
6662   case Intrinsic::amdgcn_fmad_ftz:
6663     return DAG.getNode(AMDGPUISD::FMAD_FTZ, DL, VT, Op.getOperand(1),
6664                        Op.getOperand(2), Op.getOperand(3));
6665 
6666   case Intrinsic::amdgcn_if_break:
6667     return SDValue(DAG.getMachineNode(AMDGPU::SI_IF_BREAK, DL, VT,
6668                                       Op->getOperand(1), Op->getOperand(2)), 0);
6669 
6670   case Intrinsic::amdgcn_groupstaticsize: {
6671     Triple::OSType OS = getTargetMachine().getTargetTriple().getOS();
6672     if (OS == Triple::AMDHSA || OS == Triple::AMDPAL)
6673       return Op;
6674 
6675     const Module *M = MF.getFunction().getParent();
6676     const GlobalValue *GV =
6677         M->getNamedValue(Intrinsic::getName(Intrinsic::amdgcn_groupstaticsize));
6678     SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, 0,
6679                                             SIInstrInfo::MO_ABS32_LO);
6680     return {DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, GA), 0};
6681   }
6682   case Intrinsic::amdgcn_is_shared:
6683   case Intrinsic::amdgcn_is_private: {
6684     SDLoc SL(Op);
6685     unsigned AS = (IntrinsicID == Intrinsic::amdgcn_is_shared) ?
6686       AMDGPUAS::LOCAL_ADDRESS : AMDGPUAS::PRIVATE_ADDRESS;
6687     SDValue Aperture = getSegmentAperture(AS, SL, DAG);
6688     SDValue SrcVec = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32,
6689                                  Op.getOperand(1));
6690 
6691     SDValue SrcHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, SrcVec,
6692                                 DAG.getConstant(1, SL, MVT::i32));
6693     return DAG.getSetCC(SL, MVT::i1, SrcHi, Aperture, ISD::SETEQ);
6694   }
6695   case Intrinsic::amdgcn_alignbit:
6696     return DAG.getNode(ISD::FSHR, DL, VT,
6697                        Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
6698   case Intrinsic::amdgcn_reloc_constant: {
6699     Module *M = const_cast<Module *>(MF.getFunction().getParent());
6700     const MDNode *Metadata = cast<MDNodeSDNode>(Op.getOperand(1))->getMD();
6701     auto SymbolName = cast<MDString>(Metadata->getOperand(0))->getString();
6702     auto RelocSymbol = cast<GlobalVariable>(
6703         M->getOrInsertGlobal(SymbolName, Type::getInt32Ty(M->getContext())));
6704     SDValue GA = DAG.getTargetGlobalAddress(RelocSymbol, DL, MVT::i32, 0,
6705                                             SIInstrInfo::MO_ABS32_LO);
6706     return {DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, GA), 0};
6707   }
6708   default:
6709     if (const AMDGPU::ImageDimIntrinsicInfo *ImageDimIntr =
6710             AMDGPU::getImageDimIntrinsicInfo(IntrinsicID))
6711       return lowerImage(Op, ImageDimIntr, DAG, false);
6712 
6713     return Op;
6714   }
6715 }
6716 
6717 // This function computes an appropriate offset to pass to
6718 // MachineMemOperand::setOffset() based on the offset inputs to
6719 // an intrinsic.  If any of the offsets are non-contstant or
6720 // if VIndex is non-zero then this function returns 0.  Otherwise,
6721 // it returns the sum of VOffset, SOffset, and Offset.
6722 static unsigned getBufferOffsetForMMO(SDValue VOffset,
6723                                       SDValue SOffset,
6724                                       SDValue Offset,
6725                                       SDValue VIndex = SDValue()) {
6726 
6727   if (!isa<ConstantSDNode>(VOffset) || !isa<ConstantSDNode>(SOffset) ||
6728       !isa<ConstantSDNode>(Offset))
6729     return 0;
6730 
6731   if (VIndex) {
6732     if (!isa<ConstantSDNode>(VIndex) || !cast<ConstantSDNode>(VIndex)->isNullValue())
6733       return 0;
6734   }
6735 
6736   return cast<ConstantSDNode>(VOffset)->getSExtValue() +
6737          cast<ConstantSDNode>(SOffset)->getSExtValue() +
6738          cast<ConstantSDNode>(Offset)->getSExtValue();
6739 }
6740 
6741 SDValue SITargetLowering::lowerRawBufferAtomicIntrin(SDValue Op,
6742                                                      SelectionDAG &DAG,
6743                                                      unsigned NewOpcode) const {
6744   SDLoc DL(Op);
6745 
6746   SDValue VData = Op.getOperand(2);
6747   auto Offsets = splitBufferOffsets(Op.getOperand(4), DAG);
6748   SDValue Ops[] = {
6749     Op.getOperand(0), // Chain
6750     VData,            // vdata
6751     Op.getOperand(3), // rsrc
6752     DAG.getConstant(0, DL, MVT::i32), // vindex
6753     Offsets.first,    // voffset
6754     Op.getOperand(5), // soffset
6755     Offsets.second,   // offset
6756     Op.getOperand(6), // cachepolicy
6757     DAG.getTargetConstant(0, DL, MVT::i1), // idxen
6758   };
6759 
6760   auto *M = cast<MemSDNode>(Op);
6761   M->getMemOperand()->setOffset(getBufferOffsetForMMO(Ops[4], Ops[5], Ops[6]));
6762 
6763   EVT MemVT = VData.getValueType();
6764   return DAG.getMemIntrinsicNode(NewOpcode, DL, Op->getVTList(), Ops, MemVT,
6765                                  M->getMemOperand());
6766 }
6767 
6768 SDValue
6769 SITargetLowering::lowerStructBufferAtomicIntrin(SDValue Op, SelectionDAG &DAG,
6770                                                 unsigned NewOpcode) const {
6771   SDLoc DL(Op);
6772 
6773   SDValue VData = Op.getOperand(2);
6774   auto Offsets = splitBufferOffsets(Op.getOperand(5), DAG);
6775   SDValue Ops[] = {
6776     Op.getOperand(0), // Chain
6777     VData,            // vdata
6778     Op.getOperand(3), // rsrc
6779     Op.getOperand(4), // vindex
6780     Offsets.first,    // voffset
6781     Op.getOperand(6), // soffset
6782     Offsets.second,   // offset
6783     Op.getOperand(7), // cachepolicy
6784     DAG.getTargetConstant(1, DL, MVT::i1), // idxen
6785   };
6786 
6787   auto *M = cast<MemSDNode>(Op);
6788   M->getMemOperand()->setOffset(getBufferOffsetForMMO(Ops[4], Ops[5], Ops[6],
6789                                                       Ops[3]));
6790 
6791   EVT MemVT = VData.getValueType();
6792   return DAG.getMemIntrinsicNode(NewOpcode, DL, Op->getVTList(), Ops, MemVT,
6793                                  M->getMemOperand());
6794 }
6795 
6796 SDValue SITargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op,
6797                                                  SelectionDAG &DAG) const {
6798   unsigned IntrID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6799   SDLoc DL(Op);
6800 
6801   switch (IntrID) {
6802   case Intrinsic::amdgcn_ds_ordered_add:
6803   case Intrinsic::amdgcn_ds_ordered_swap: {
6804     MemSDNode *M = cast<MemSDNode>(Op);
6805     SDValue Chain = M->getOperand(0);
6806     SDValue M0 = M->getOperand(2);
6807     SDValue Value = M->getOperand(3);
6808     unsigned IndexOperand = M->getConstantOperandVal(7);
6809     unsigned WaveRelease = M->getConstantOperandVal(8);
6810     unsigned WaveDone = M->getConstantOperandVal(9);
6811 
6812     unsigned OrderedCountIndex = IndexOperand & 0x3f;
6813     IndexOperand &= ~0x3f;
6814     unsigned CountDw = 0;
6815 
6816     if (Subtarget->getGeneration() >= AMDGPUSubtarget::GFX10) {
6817       CountDw = (IndexOperand >> 24) & 0xf;
6818       IndexOperand &= ~(0xf << 24);
6819 
6820       if (CountDw < 1 || CountDw > 4) {
6821         report_fatal_error(
6822             "ds_ordered_count: dword count must be between 1 and 4");
6823       }
6824     }
6825 
6826     if (IndexOperand)
6827       report_fatal_error("ds_ordered_count: bad index operand");
6828 
6829     if (WaveDone && !WaveRelease)
6830       report_fatal_error("ds_ordered_count: wave_done requires wave_release");
6831 
6832     unsigned Instruction = IntrID == Intrinsic::amdgcn_ds_ordered_add ? 0 : 1;
6833     unsigned ShaderType =
6834         SIInstrInfo::getDSShaderTypeValue(DAG.getMachineFunction());
6835     unsigned Offset0 = OrderedCountIndex << 2;
6836     unsigned Offset1 = WaveRelease | (WaveDone << 1) | (ShaderType << 2) |
6837                        (Instruction << 4);
6838 
6839     if (Subtarget->getGeneration() >= AMDGPUSubtarget::GFX10)
6840       Offset1 |= (CountDw - 1) << 6;
6841 
6842     unsigned Offset = Offset0 | (Offset1 << 8);
6843 
6844     SDValue Ops[] = {
6845       Chain,
6846       Value,
6847       DAG.getTargetConstant(Offset, DL, MVT::i16),
6848       copyToM0(DAG, Chain, DL, M0).getValue(1), // Glue
6849     };
6850     return DAG.getMemIntrinsicNode(AMDGPUISD::DS_ORDERED_COUNT, DL,
6851                                    M->getVTList(), Ops, M->getMemoryVT(),
6852                                    M->getMemOperand());
6853   }
6854   case Intrinsic::amdgcn_ds_fadd: {
6855     MemSDNode *M = cast<MemSDNode>(Op);
6856     unsigned Opc;
6857     switch (IntrID) {
6858     case Intrinsic::amdgcn_ds_fadd:
6859       Opc = ISD::ATOMIC_LOAD_FADD;
6860       break;
6861     }
6862 
6863     return DAG.getAtomic(Opc, SDLoc(Op), M->getMemoryVT(),
6864                          M->getOperand(0), M->getOperand(2), M->getOperand(3),
6865                          M->getMemOperand());
6866   }
6867   case Intrinsic::amdgcn_atomic_inc:
6868   case Intrinsic::amdgcn_atomic_dec:
6869   case Intrinsic::amdgcn_ds_fmin:
6870   case Intrinsic::amdgcn_ds_fmax: {
6871     MemSDNode *M = cast<MemSDNode>(Op);
6872     unsigned Opc;
6873     switch (IntrID) {
6874     case Intrinsic::amdgcn_atomic_inc:
6875       Opc = AMDGPUISD::ATOMIC_INC;
6876       break;
6877     case Intrinsic::amdgcn_atomic_dec:
6878       Opc = AMDGPUISD::ATOMIC_DEC;
6879       break;
6880     case Intrinsic::amdgcn_ds_fmin:
6881       Opc = AMDGPUISD::ATOMIC_LOAD_FMIN;
6882       break;
6883     case Intrinsic::amdgcn_ds_fmax:
6884       Opc = AMDGPUISD::ATOMIC_LOAD_FMAX;
6885       break;
6886     default:
6887       llvm_unreachable("Unknown intrinsic!");
6888     }
6889     SDValue Ops[] = {
6890       M->getOperand(0), // Chain
6891       M->getOperand(2), // Ptr
6892       M->getOperand(3)  // Value
6893     };
6894 
6895     return DAG.getMemIntrinsicNode(Opc, SDLoc(Op), M->getVTList(), Ops,
6896                                    M->getMemoryVT(), M->getMemOperand());
6897   }
6898   case Intrinsic::amdgcn_buffer_load:
6899   case Intrinsic::amdgcn_buffer_load_format: {
6900     unsigned Glc = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
6901     unsigned Slc = cast<ConstantSDNode>(Op.getOperand(6))->getZExtValue();
6902     unsigned IdxEn = 1;
6903     if (auto Idx = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
6904       IdxEn = Idx->getZExtValue() != 0;
6905     SDValue Ops[] = {
6906       Op.getOperand(0), // Chain
6907       Op.getOperand(2), // rsrc
6908       Op.getOperand(3), // vindex
6909       SDValue(),        // voffset -- will be set by setBufferOffsets
6910       SDValue(),        // soffset -- will be set by setBufferOffsets
6911       SDValue(),        // offset -- will be set by setBufferOffsets
6912       DAG.getTargetConstant(Glc | (Slc << 1), DL, MVT::i32), // cachepolicy
6913       DAG.getTargetConstant(IdxEn, DL, MVT::i1), // idxen
6914     };
6915 
6916     unsigned Offset = setBufferOffsets(Op.getOperand(4), DAG, &Ops[3]);
6917     // We don't know the offset if vindex is non-zero, so clear it.
6918     if (IdxEn)
6919       Offset = 0;
6920 
6921     unsigned Opc = (IntrID == Intrinsic::amdgcn_buffer_load) ?
6922         AMDGPUISD::BUFFER_LOAD : AMDGPUISD::BUFFER_LOAD_FORMAT;
6923 
6924     EVT VT = Op.getValueType();
6925     EVT IntVT = VT.changeTypeToInteger();
6926     auto *M = cast<MemSDNode>(Op);
6927     M->getMemOperand()->setOffset(Offset);
6928     EVT LoadVT = Op.getValueType();
6929 
6930     if (LoadVT.getScalarType() == MVT::f16)
6931       return adjustLoadValueType(AMDGPUISD::BUFFER_LOAD_FORMAT_D16,
6932                                  M, DAG, Ops);
6933 
6934     // Handle BUFFER_LOAD_BYTE/UBYTE/SHORT/USHORT overloaded intrinsics
6935     if (LoadVT.getScalarType() == MVT::i8 ||
6936         LoadVT.getScalarType() == MVT::i16)
6937       return handleByteShortBufferLoads(DAG, LoadVT, DL, Ops, M);
6938 
6939     return getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops, IntVT,
6940                                M->getMemOperand(), DAG);
6941   }
6942   case Intrinsic::amdgcn_raw_buffer_load:
6943   case Intrinsic::amdgcn_raw_buffer_load_format: {
6944     const bool IsFormat = IntrID == Intrinsic::amdgcn_raw_buffer_load_format;
6945 
6946     auto Offsets = splitBufferOffsets(Op.getOperand(3), DAG);
6947     SDValue Ops[] = {
6948       Op.getOperand(0), // Chain
6949       Op.getOperand(2), // rsrc
6950       DAG.getConstant(0, DL, MVT::i32), // vindex
6951       Offsets.first,    // voffset
6952       Op.getOperand(4), // soffset
6953       Offsets.second,   // offset
6954       Op.getOperand(5), // cachepolicy, swizzled buffer
6955       DAG.getTargetConstant(0, DL, MVT::i1), // idxen
6956     };
6957 
6958     auto *M = cast<MemSDNode>(Op);
6959     M->getMemOperand()->setOffset(getBufferOffsetForMMO(Ops[3], Ops[4], Ops[5]));
6960     return lowerIntrinsicLoad(M, IsFormat, DAG, Ops);
6961   }
6962   case Intrinsic::amdgcn_struct_buffer_load:
6963   case Intrinsic::amdgcn_struct_buffer_load_format: {
6964     const bool IsFormat = IntrID == Intrinsic::amdgcn_struct_buffer_load_format;
6965 
6966     auto Offsets = splitBufferOffsets(Op.getOperand(4), DAG);
6967     SDValue Ops[] = {
6968       Op.getOperand(0), // Chain
6969       Op.getOperand(2), // rsrc
6970       Op.getOperand(3), // vindex
6971       Offsets.first,    // voffset
6972       Op.getOperand(5), // soffset
6973       Offsets.second,   // offset
6974       Op.getOperand(6), // cachepolicy, swizzled buffer
6975       DAG.getTargetConstant(1, DL, MVT::i1), // idxen
6976     };
6977 
6978     auto *M = cast<MemSDNode>(Op);
6979     M->getMemOperand()->setOffset(getBufferOffsetForMMO(Ops[3], Ops[4], Ops[5],
6980                                                         Ops[2]));
6981     return lowerIntrinsicLoad(cast<MemSDNode>(Op), IsFormat, DAG, Ops);
6982   }
6983   case Intrinsic::amdgcn_tbuffer_load: {
6984     MemSDNode *M = cast<MemSDNode>(Op);
6985     EVT LoadVT = Op.getValueType();
6986 
6987     unsigned Dfmt = cast<ConstantSDNode>(Op.getOperand(7))->getZExtValue();
6988     unsigned Nfmt = cast<ConstantSDNode>(Op.getOperand(8))->getZExtValue();
6989     unsigned Glc = cast<ConstantSDNode>(Op.getOperand(9))->getZExtValue();
6990     unsigned Slc = cast<ConstantSDNode>(Op.getOperand(10))->getZExtValue();
6991     unsigned IdxEn = 1;
6992     if (auto Idx = dyn_cast<ConstantSDNode>(Op.getOperand(3)))
6993       IdxEn = Idx->getZExtValue() != 0;
6994     SDValue Ops[] = {
6995       Op.getOperand(0),  // Chain
6996       Op.getOperand(2),  // rsrc
6997       Op.getOperand(3),  // vindex
6998       Op.getOperand(4),  // voffset
6999       Op.getOperand(5),  // soffset
7000       Op.getOperand(6),  // offset
7001       DAG.getTargetConstant(Dfmt | (Nfmt << 4), DL, MVT::i32), // format
7002       DAG.getTargetConstant(Glc | (Slc << 1), DL, MVT::i32), // cachepolicy
7003       DAG.getTargetConstant(IdxEn, DL, MVT::i1) // idxen
7004     };
7005 
7006     if (LoadVT.getScalarType() == MVT::f16)
7007       return adjustLoadValueType(AMDGPUISD::TBUFFER_LOAD_FORMAT_D16,
7008                                  M, DAG, Ops);
7009     return getMemIntrinsicNode(AMDGPUISD::TBUFFER_LOAD_FORMAT, DL,
7010                                Op->getVTList(), Ops, LoadVT, M->getMemOperand(),
7011                                DAG);
7012   }
7013   case Intrinsic::amdgcn_raw_tbuffer_load: {
7014     MemSDNode *M = cast<MemSDNode>(Op);
7015     EVT LoadVT = Op.getValueType();
7016     auto Offsets = splitBufferOffsets(Op.getOperand(3), DAG);
7017 
7018     SDValue Ops[] = {
7019       Op.getOperand(0),  // Chain
7020       Op.getOperand(2),  // rsrc
7021       DAG.getConstant(0, DL, MVT::i32), // vindex
7022       Offsets.first,     // voffset
7023       Op.getOperand(4),  // soffset
7024       Offsets.second,    // offset
7025       Op.getOperand(5),  // format
7026       Op.getOperand(6),  // cachepolicy, swizzled buffer
7027       DAG.getTargetConstant(0, DL, MVT::i1), // idxen
7028     };
7029 
7030     if (LoadVT.getScalarType() == MVT::f16)
7031       return adjustLoadValueType(AMDGPUISD::TBUFFER_LOAD_FORMAT_D16,
7032                                  M, DAG, Ops);
7033     return getMemIntrinsicNode(AMDGPUISD::TBUFFER_LOAD_FORMAT, DL,
7034                                Op->getVTList(), Ops, LoadVT, M->getMemOperand(),
7035                                DAG);
7036   }
7037   case Intrinsic::amdgcn_struct_tbuffer_load: {
7038     MemSDNode *M = cast<MemSDNode>(Op);
7039     EVT LoadVT = Op.getValueType();
7040     auto Offsets = splitBufferOffsets(Op.getOperand(4), DAG);
7041 
7042     SDValue Ops[] = {
7043       Op.getOperand(0),  // Chain
7044       Op.getOperand(2),  // rsrc
7045       Op.getOperand(3),  // vindex
7046       Offsets.first,     // voffset
7047       Op.getOperand(5),  // soffset
7048       Offsets.second,    // offset
7049       Op.getOperand(6),  // format
7050       Op.getOperand(7),  // cachepolicy, swizzled buffer
7051       DAG.getTargetConstant(1, DL, MVT::i1), // idxen
7052     };
7053 
7054     if (LoadVT.getScalarType() == MVT::f16)
7055       return adjustLoadValueType(AMDGPUISD::TBUFFER_LOAD_FORMAT_D16,
7056                                  M, DAG, Ops);
7057     return getMemIntrinsicNode(AMDGPUISD::TBUFFER_LOAD_FORMAT, DL,
7058                                Op->getVTList(), Ops, LoadVT, M->getMemOperand(),
7059                                DAG);
7060   }
7061   case Intrinsic::amdgcn_buffer_atomic_swap:
7062   case Intrinsic::amdgcn_buffer_atomic_add:
7063   case Intrinsic::amdgcn_buffer_atomic_sub:
7064   case Intrinsic::amdgcn_buffer_atomic_csub:
7065   case Intrinsic::amdgcn_buffer_atomic_smin:
7066   case Intrinsic::amdgcn_buffer_atomic_umin:
7067   case Intrinsic::amdgcn_buffer_atomic_smax:
7068   case Intrinsic::amdgcn_buffer_atomic_umax:
7069   case Intrinsic::amdgcn_buffer_atomic_and:
7070   case Intrinsic::amdgcn_buffer_atomic_or:
7071   case Intrinsic::amdgcn_buffer_atomic_xor:
7072   case Intrinsic::amdgcn_buffer_atomic_fadd: {
7073     unsigned Slc = cast<ConstantSDNode>(Op.getOperand(6))->getZExtValue();
7074     unsigned IdxEn = 1;
7075     if (auto Idx = dyn_cast<ConstantSDNode>(Op.getOperand(4)))
7076       IdxEn = Idx->getZExtValue() != 0;
7077     SDValue Ops[] = {
7078       Op.getOperand(0), // Chain
7079       Op.getOperand(2), // vdata
7080       Op.getOperand(3), // rsrc
7081       Op.getOperand(4), // vindex
7082       SDValue(),        // voffset -- will be set by setBufferOffsets
7083       SDValue(),        // soffset -- will be set by setBufferOffsets
7084       SDValue(),        // offset -- will be set by setBufferOffsets
7085       DAG.getTargetConstant(Slc << 1, DL, MVT::i32), // cachepolicy
7086       DAG.getTargetConstant(IdxEn, DL, MVT::i1), // idxen
7087     };
7088     unsigned Offset = setBufferOffsets(Op.getOperand(5), DAG, &Ops[4]);
7089     // We don't know the offset if vindex is non-zero, so clear it.
7090     if (IdxEn)
7091       Offset = 0;
7092     EVT VT = Op.getValueType();
7093 
7094     auto *M = cast<MemSDNode>(Op);
7095     M->getMemOperand()->setOffset(Offset);
7096     unsigned Opcode = 0;
7097 
7098     switch (IntrID) {
7099     case Intrinsic::amdgcn_buffer_atomic_swap:
7100       Opcode = AMDGPUISD::BUFFER_ATOMIC_SWAP;
7101       break;
7102     case Intrinsic::amdgcn_buffer_atomic_add:
7103       Opcode = AMDGPUISD::BUFFER_ATOMIC_ADD;
7104       break;
7105     case Intrinsic::amdgcn_buffer_atomic_sub:
7106       Opcode = AMDGPUISD::BUFFER_ATOMIC_SUB;
7107       break;
7108     case Intrinsic::amdgcn_buffer_atomic_csub:
7109       Opcode = AMDGPUISD::BUFFER_ATOMIC_CSUB;
7110       break;
7111     case Intrinsic::amdgcn_buffer_atomic_smin:
7112       Opcode = AMDGPUISD::BUFFER_ATOMIC_SMIN;
7113       break;
7114     case Intrinsic::amdgcn_buffer_atomic_umin:
7115       Opcode = AMDGPUISD::BUFFER_ATOMIC_UMIN;
7116       break;
7117     case Intrinsic::amdgcn_buffer_atomic_smax:
7118       Opcode = AMDGPUISD::BUFFER_ATOMIC_SMAX;
7119       break;
7120     case Intrinsic::amdgcn_buffer_atomic_umax:
7121       Opcode = AMDGPUISD::BUFFER_ATOMIC_UMAX;
7122       break;
7123     case Intrinsic::amdgcn_buffer_atomic_and:
7124       Opcode = AMDGPUISD::BUFFER_ATOMIC_AND;
7125       break;
7126     case Intrinsic::amdgcn_buffer_atomic_or:
7127       Opcode = AMDGPUISD::BUFFER_ATOMIC_OR;
7128       break;
7129     case Intrinsic::amdgcn_buffer_atomic_xor:
7130       Opcode = AMDGPUISD::BUFFER_ATOMIC_XOR;
7131       break;
7132     case Intrinsic::amdgcn_buffer_atomic_fadd:
7133       if (!Op.getValue(0).use_empty() && !Subtarget->hasGFX90AInsts()) {
7134         DiagnosticInfoUnsupported
7135           NoFpRet(DAG.getMachineFunction().getFunction(),
7136                   "return versions of fp atomics not supported",
7137                   DL.getDebugLoc(), DS_Error);
7138         DAG.getContext()->diagnose(NoFpRet);
7139         return SDValue();
7140       }
7141       Opcode = AMDGPUISD::BUFFER_ATOMIC_FADD;
7142       break;
7143     default:
7144       llvm_unreachable("unhandled atomic opcode");
7145     }
7146 
7147     return DAG.getMemIntrinsicNode(Opcode, DL, Op->getVTList(), Ops, VT,
7148                                    M->getMemOperand());
7149   }
7150   case Intrinsic::amdgcn_raw_buffer_atomic_fadd:
7151     return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_FADD);
7152   case Intrinsic::amdgcn_struct_buffer_atomic_fadd:
7153     return lowerStructBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_FADD);
7154   case Intrinsic::amdgcn_raw_buffer_atomic_fmin:
7155     return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_FMIN);
7156   case Intrinsic::amdgcn_struct_buffer_atomic_fmin:
7157     return lowerStructBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_FMIN);
7158   case Intrinsic::amdgcn_raw_buffer_atomic_fmax:
7159     return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_FMAX);
7160   case Intrinsic::amdgcn_struct_buffer_atomic_fmax:
7161     return lowerStructBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_FMAX);
7162   case Intrinsic::amdgcn_raw_buffer_atomic_swap:
7163     return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_SWAP);
7164   case Intrinsic::amdgcn_raw_buffer_atomic_add:
7165     return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_ADD);
7166   case Intrinsic::amdgcn_raw_buffer_atomic_sub:
7167     return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_SUB);
7168   case Intrinsic::amdgcn_raw_buffer_atomic_smin:
7169     return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_SMIN);
7170   case Intrinsic::amdgcn_raw_buffer_atomic_umin:
7171     return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_UMIN);
7172   case Intrinsic::amdgcn_raw_buffer_atomic_smax:
7173     return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_SMAX);
7174   case Intrinsic::amdgcn_raw_buffer_atomic_umax:
7175     return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_UMAX);
7176   case Intrinsic::amdgcn_raw_buffer_atomic_and:
7177     return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_AND);
7178   case Intrinsic::amdgcn_raw_buffer_atomic_or:
7179     return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_OR);
7180   case Intrinsic::amdgcn_raw_buffer_atomic_xor:
7181     return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_XOR);
7182   case Intrinsic::amdgcn_raw_buffer_atomic_inc:
7183     return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_INC);
7184   case Intrinsic::amdgcn_raw_buffer_atomic_dec:
7185     return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_DEC);
7186   case Intrinsic::amdgcn_struct_buffer_atomic_swap:
7187     return lowerStructBufferAtomicIntrin(Op, DAG,
7188                                          AMDGPUISD::BUFFER_ATOMIC_SWAP);
7189   case Intrinsic::amdgcn_struct_buffer_atomic_add:
7190     return lowerStructBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_ADD);
7191   case Intrinsic::amdgcn_struct_buffer_atomic_sub:
7192     return lowerStructBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_SUB);
7193   case Intrinsic::amdgcn_struct_buffer_atomic_smin:
7194     return lowerStructBufferAtomicIntrin(Op, DAG,
7195                                          AMDGPUISD::BUFFER_ATOMIC_SMIN);
7196   case Intrinsic::amdgcn_struct_buffer_atomic_umin:
7197     return lowerStructBufferAtomicIntrin(Op, DAG,
7198                                          AMDGPUISD::BUFFER_ATOMIC_UMIN);
7199   case Intrinsic::amdgcn_struct_buffer_atomic_smax:
7200     return lowerStructBufferAtomicIntrin(Op, DAG,
7201                                          AMDGPUISD::BUFFER_ATOMIC_SMAX);
7202   case Intrinsic::amdgcn_struct_buffer_atomic_umax:
7203     return lowerStructBufferAtomicIntrin(Op, DAG,
7204                                          AMDGPUISD::BUFFER_ATOMIC_UMAX);
7205   case Intrinsic::amdgcn_struct_buffer_atomic_and:
7206     return lowerStructBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_AND);
7207   case Intrinsic::amdgcn_struct_buffer_atomic_or:
7208     return lowerStructBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_OR);
7209   case Intrinsic::amdgcn_struct_buffer_atomic_xor:
7210     return lowerStructBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_XOR);
7211   case Intrinsic::amdgcn_struct_buffer_atomic_inc:
7212     return lowerStructBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_INC);
7213   case Intrinsic::amdgcn_struct_buffer_atomic_dec:
7214     return lowerStructBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_DEC);
7215 
7216   case Intrinsic::amdgcn_buffer_atomic_cmpswap: {
7217     unsigned Slc = cast<ConstantSDNode>(Op.getOperand(7))->getZExtValue();
7218     unsigned IdxEn = 1;
7219     if (auto Idx = dyn_cast<ConstantSDNode>(Op.getOperand(5)))
7220       IdxEn = Idx->getZExtValue() != 0;
7221     SDValue Ops[] = {
7222       Op.getOperand(0), // Chain
7223       Op.getOperand(2), // src
7224       Op.getOperand(3), // cmp
7225       Op.getOperand(4), // rsrc
7226       Op.getOperand(5), // vindex
7227       SDValue(),        // voffset -- will be set by setBufferOffsets
7228       SDValue(),        // soffset -- will be set by setBufferOffsets
7229       SDValue(),        // offset -- will be set by setBufferOffsets
7230       DAG.getTargetConstant(Slc << 1, DL, MVT::i32), // cachepolicy
7231       DAG.getTargetConstant(IdxEn, DL, MVT::i1), // idxen
7232     };
7233     unsigned Offset = setBufferOffsets(Op.getOperand(6), DAG, &Ops[5]);
7234     // We don't know the offset if vindex is non-zero, so clear it.
7235     if (IdxEn)
7236       Offset = 0;
7237     EVT VT = Op.getValueType();
7238     auto *M = cast<MemSDNode>(Op);
7239     M->getMemOperand()->setOffset(Offset);
7240 
7241     return DAG.getMemIntrinsicNode(AMDGPUISD::BUFFER_ATOMIC_CMPSWAP, DL,
7242                                    Op->getVTList(), Ops, VT, M->getMemOperand());
7243   }
7244   case Intrinsic::amdgcn_raw_buffer_atomic_cmpswap: {
7245     auto Offsets = splitBufferOffsets(Op.getOperand(5), DAG);
7246     SDValue Ops[] = {
7247       Op.getOperand(0), // Chain
7248       Op.getOperand(2), // src
7249       Op.getOperand(3), // cmp
7250       Op.getOperand(4), // rsrc
7251       DAG.getConstant(0, DL, MVT::i32), // vindex
7252       Offsets.first,    // voffset
7253       Op.getOperand(6), // soffset
7254       Offsets.second,   // offset
7255       Op.getOperand(7), // cachepolicy
7256       DAG.getTargetConstant(0, DL, MVT::i1), // idxen
7257     };
7258     EVT VT = Op.getValueType();
7259     auto *M = cast<MemSDNode>(Op);
7260     M->getMemOperand()->setOffset(getBufferOffsetForMMO(Ops[5], Ops[6], Ops[7]));
7261 
7262     return DAG.getMemIntrinsicNode(AMDGPUISD::BUFFER_ATOMIC_CMPSWAP, DL,
7263                                    Op->getVTList(), Ops, VT, M->getMemOperand());
7264   }
7265   case Intrinsic::amdgcn_struct_buffer_atomic_cmpswap: {
7266     auto Offsets = splitBufferOffsets(Op.getOperand(6), DAG);
7267     SDValue Ops[] = {
7268       Op.getOperand(0), // Chain
7269       Op.getOperand(2), // src
7270       Op.getOperand(3), // cmp
7271       Op.getOperand(4), // rsrc
7272       Op.getOperand(5), // vindex
7273       Offsets.first,    // voffset
7274       Op.getOperand(7), // soffset
7275       Offsets.second,   // offset
7276       Op.getOperand(8), // cachepolicy
7277       DAG.getTargetConstant(1, DL, MVT::i1), // idxen
7278     };
7279     EVT VT = Op.getValueType();
7280     auto *M = cast<MemSDNode>(Op);
7281     M->getMemOperand()->setOffset(getBufferOffsetForMMO(Ops[5], Ops[6], Ops[7],
7282                                                         Ops[4]));
7283 
7284     return DAG.getMemIntrinsicNode(AMDGPUISD::BUFFER_ATOMIC_CMPSWAP, DL,
7285                                    Op->getVTList(), Ops, VT, M->getMemOperand());
7286   }
7287   case Intrinsic::amdgcn_image_bvh_intersect_ray: {
7288     SDLoc DL(Op);
7289     MemSDNode *M = cast<MemSDNode>(Op);
7290     SDValue NodePtr = M->getOperand(2);
7291     SDValue RayExtent = M->getOperand(3);
7292     SDValue RayOrigin = M->getOperand(4);
7293     SDValue RayDir = M->getOperand(5);
7294     SDValue RayInvDir = M->getOperand(6);
7295     SDValue TDescr = M->getOperand(7);
7296 
7297     assert(NodePtr.getValueType() == MVT::i32 ||
7298            NodePtr.getValueType() == MVT::i64);
7299     assert(RayDir.getValueType() == MVT::v4f16 ||
7300            RayDir.getValueType() == MVT::v4f32);
7301 
7302     bool IsA16 = RayDir.getValueType().getVectorElementType() == MVT::f16;
7303     bool Is64 = NodePtr.getValueType() == MVT::i64;
7304     unsigned Opcode = IsA16 ? Is64 ? AMDGPU::IMAGE_BVH64_INTERSECT_RAY_a16_nsa
7305                                    : AMDGPU::IMAGE_BVH_INTERSECT_RAY_a16_nsa
7306                             : Is64 ? AMDGPU::IMAGE_BVH64_INTERSECT_RAY_nsa
7307                                    : AMDGPU::IMAGE_BVH_INTERSECT_RAY_nsa;
7308 
7309     SmallVector<SDValue, 16> Ops;
7310 
7311     auto packLanes = [&DAG, &Ops, &DL] (SDValue Op, bool IsAligned) {
7312       SmallVector<SDValue, 3> Lanes;
7313       DAG.ExtractVectorElements(Op, Lanes, 0, 3);
7314       if (Lanes[0].getValueSizeInBits() == 32) {
7315         for (unsigned I = 0; I < 3; ++I)
7316           Ops.push_back(DAG.getBitcast(MVT::i32, Lanes[I]));
7317       } else {
7318         if (IsAligned) {
7319           Ops.push_back(
7320             DAG.getBitcast(MVT::i32,
7321                            DAG.getBuildVector(MVT::v2f16, DL,
7322                                               { Lanes[0], Lanes[1] })));
7323           Ops.push_back(Lanes[2]);
7324         } else {
7325           SDValue Elt0 = Ops.pop_back_val();
7326           Ops.push_back(
7327             DAG.getBitcast(MVT::i32,
7328                            DAG.getBuildVector(MVT::v2f16, DL,
7329                                               { Elt0, Lanes[0] })));
7330           Ops.push_back(
7331             DAG.getBitcast(MVT::i32,
7332                            DAG.getBuildVector(MVT::v2f16, DL,
7333                                               { Lanes[1], Lanes[2] })));
7334         }
7335       }
7336     };
7337 
7338     if (Is64)
7339       DAG.ExtractVectorElements(DAG.getBitcast(MVT::v2i32, NodePtr), Ops, 0, 2);
7340     else
7341       Ops.push_back(NodePtr);
7342 
7343     Ops.push_back(DAG.getBitcast(MVT::i32, RayExtent));
7344     packLanes(RayOrigin, true);
7345     packLanes(RayDir, true);
7346     packLanes(RayInvDir, false);
7347     Ops.push_back(TDescr);
7348     if (IsA16)
7349       Ops.push_back(DAG.getTargetConstant(1, DL, MVT::i1));
7350     Ops.push_back(M->getChain());
7351 
7352     auto *NewNode = DAG.getMachineNode(Opcode, DL, M->getVTList(), Ops);
7353     MachineMemOperand *MemRef = M->getMemOperand();
7354     DAG.setNodeMemRefs(NewNode, {MemRef});
7355     return SDValue(NewNode, 0);
7356   }
7357   case Intrinsic::amdgcn_global_atomic_fadd:
7358     if (!Op.getValue(0).use_empty() && !Subtarget->hasGFX90AInsts()) {
7359       DiagnosticInfoUnsupported
7360         NoFpRet(DAG.getMachineFunction().getFunction(),
7361                 "return versions of fp atomics not supported",
7362                 DL.getDebugLoc(), DS_Error);
7363       DAG.getContext()->diagnose(NoFpRet);
7364       return SDValue();
7365     }
7366     LLVM_FALLTHROUGH;
7367   case Intrinsic::amdgcn_global_atomic_fmin:
7368   case Intrinsic::amdgcn_global_atomic_fmax:
7369   case Intrinsic::amdgcn_flat_atomic_fadd:
7370   case Intrinsic::amdgcn_flat_atomic_fmin:
7371   case Intrinsic::amdgcn_flat_atomic_fmax: {
7372     MemSDNode *M = cast<MemSDNode>(Op);
7373     SDValue Ops[] = {
7374       M->getOperand(0), // Chain
7375       M->getOperand(2), // Ptr
7376       M->getOperand(3)  // Value
7377     };
7378     unsigned Opcode = 0;
7379     switch (IntrID) {
7380     case Intrinsic::amdgcn_global_atomic_fadd:
7381     case Intrinsic::amdgcn_flat_atomic_fadd: {
7382       EVT VT = Op.getOperand(3).getValueType();
7383       return DAG.getAtomic(ISD::ATOMIC_LOAD_FADD, DL, VT,
7384                            DAG.getVTList(VT, MVT::Other), Ops,
7385                            M->getMemOperand());
7386     }
7387     case Intrinsic::amdgcn_global_atomic_fmin:
7388     case Intrinsic::amdgcn_flat_atomic_fmin: {
7389       Opcode = AMDGPUISD::ATOMIC_LOAD_FMIN;
7390       break;
7391     }
7392     case Intrinsic::amdgcn_global_atomic_fmax:
7393     case Intrinsic::amdgcn_flat_atomic_fmax: {
7394       Opcode = AMDGPUISD::ATOMIC_LOAD_FMAX;
7395       break;
7396     }
7397     default:
7398       llvm_unreachable("unhandled atomic opcode");
7399     }
7400     return DAG.getMemIntrinsicNode(Opcode, SDLoc(Op),
7401                                    M->getVTList(), Ops, M->getMemoryVT(),
7402                                    M->getMemOperand());
7403   }
7404   default:
7405 
7406     if (const AMDGPU::ImageDimIntrinsicInfo *ImageDimIntr =
7407             AMDGPU::getImageDimIntrinsicInfo(IntrID))
7408       return lowerImage(Op, ImageDimIntr, DAG, true);
7409 
7410     return SDValue();
7411   }
7412 }
7413 
7414 // Call DAG.getMemIntrinsicNode for a load, but first widen a dwordx3 type to
7415 // dwordx4 if on SI.
7416 SDValue SITargetLowering::getMemIntrinsicNode(unsigned Opcode, const SDLoc &DL,
7417                                               SDVTList VTList,
7418                                               ArrayRef<SDValue> Ops, EVT MemVT,
7419                                               MachineMemOperand *MMO,
7420                                               SelectionDAG &DAG) const {
7421   EVT VT = VTList.VTs[0];
7422   EVT WidenedVT = VT;
7423   EVT WidenedMemVT = MemVT;
7424   if (!Subtarget->hasDwordx3LoadStores() &&
7425       (WidenedVT == MVT::v3i32 || WidenedVT == MVT::v3f32)) {
7426     WidenedVT = EVT::getVectorVT(*DAG.getContext(),
7427                                  WidenedVT.getVectorElementType(), 4);
7428     WidenedMemVT = EVT::getVectorVT(*DAG.getContext(),
7429                                     WidenedMemVT.getVectorElementType(), 4);
7430     MMO = DAG.getMachineFunction().getMachineMemOperand(MMO, 0, 16);
7431   }
7432 
7433   assert(VTList.NumVTs == 2);
7434   SDVTList WidenedVTList = DAG.getVTList(WidenedVT, VTList.VTs[1]);
7435 
7436   auto NewOp = DAG.getMemIntrinsicNode(Opcode, DL, WidenedVTList, Ops,
7437                                        WidenedMemVT, MMO);
7438   if (WidenedVT != VT) {
7439     auto Extract = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, NewOp,
7440                                DAG.getVectorIdxConstant(0, DL));
7441     NewOp = DAG.getMergeValues({ Extract, SDValue(NewOp.getNode(), 1) }, DL);
7442   }
7443   return NewOp;
7444 }
7445 
7446 SDValue SITargetLowering::handleD16VData(SDValue VData, SelectionDAG &DAG,
7447                                          bool ImageStore) const {
7448   EVT StoreVT = VData.getValueType();
7449 
7450   // No change for f16 and legal vector D16 types.
7451   if (!StoreVT.isVector())
7452     return VData;
7453 
7454   SDLoc DL(VData);
7455   unsigned NumElements = StoreVT.getVectorNumElements();
7456 
7457   if (Subtarget->hasUnpackedD16VMem()) {
7458     // We need to unpack the packed data to store.
7459     EVT IntStoreVT = StoreVT.changeTypeToInteger();
7460     SDValue IntVData = DAG.getNode(ISD::BITCAST, DL, IntStoreVT, VData);
7461 
7462     EVT EquivStoreVT =
7463         EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElements);
7464     SDValue ZExt = DAG.getNode(ISD::ZERO_EXTEND, DL, EquivStoreVT, IntVData);
7465     return DAG.UnrollVectorOp(ZExt.getNode());
7466   }
7467 
7468   // The sq block of gfx8.1 does not estimate register use correctly for d16
7469   // image store instructions. The data operand is computed as if it were not a
7470   // d16 image instruction.
7471   if (ImageStore && Subtarget->hasImageStoreD16Bug()) {
7472     // Bitcast to i16
7473     EVT IntStoreVT = StoreVT.changeTypeToInteger();
7474     SDValue IntVData = DAG.getNode(ISD::BITCAST, DL, IntStoreVT, VData);
7475 
7476     // Decompose into scalars
7477     SmallVector<SDValue, 4> Elts;
7478     DAG.ExtractVectorElements(IntVData, Elts);
7479 
7480     // Group pairs of i16 into v2i16 and bitcast to i32
7481     SmallVector<SDValue, 4> PackedElts;
7482     for (unsigned I = 0; I < Elts.size() / 2; I += 1) {
7483       SDValue Pair =
7484           DAG.getBuildVector(MVT::v2i16, DL, {Elts[I * 2], Elts[I * 2 + 1]});
7485       SDValue IntPair = DAG.getNode(ISD::BITCAST, DL, MVT::i32, Pair);
7486       PackedElts.push_back(IntPair);
7487     }
7488     if ((NumElements % 2) == 1) {
7489       // Handle v3i16
7490       unsigned I = Elts.size() / 2;
7491       SDValue Pair = DAG.getBuildVector(MVT::v2i16, DL,
7492                                         {Elts[I * 2], DAG.getUNDEF(MVT::i16)});
7493       SDValue IntPair = DAG.getNode(ISD::BITCAST, DL, MVT::i32, Pair);
7494       PackedElts.push_back(IntPair);
7495     }
7496 
7497     // Pad using UNDEF
7498     PackedElts.resize(Elts.size(), DAG.getUNDEF(MVT::i32));
7499 
7500     // Build final vector
7501     EVT VecVT =
7502         EVT::getVectorVT(*DAG.getContext(), MVT::i32, PackedElts.size());
7503     return DAG.getBuildVector(VecVT, DL, PackedElts);
7504   }
7505 
7506   if (NumElements == 3) {
7507     EVT IntStoreVT =
7508         EVT::getIntegerVT(*DAG.getContext(), StoreVT.getStoreSizeInBits());
7509     SDValue IntVData = DAG.getNode(ISD::BITCAST, DL, IntStoreVT, VData);
7510 
7511     EVT WidenedStoreVT = EVT::getVectorVT(
7512         *DAG.getContext(), StoreVT.getVectorElementType(), NumElements + 1);
7513     EVT WidenedIntVT = EVT::getIntegerVT(*DAG.getContext(),
7514                                          WidenedStoreVT.getStoreSizeInBits());
7515     SDValue ZExt = DAG.getNode(ISD::ZERO_EXTEND, DL, WidenedIntVT, IntVData);
7516     return DAG.getNode(ISD::BITCAST, DL, WidenedStoreVT, ZExt);
7517   }
7518 
7519   assert(isTypeLegal(StoreVT));
7520   return VData;
7521 }
7522 
7523 SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op,
7524                                               SelectionDAG &DAG) const {
7525   SDLoc DL(Op);
7526   SDValue Chain = Op.getOperand(0);
7527   unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
7528   MachineFunction &MF = DAG.getMachineFunction();
7529 
7530   switch (IntrinsicID) {
7531   case Intrinsic::amdgcn_exp_compr: {
7532     SDValue Src0 = Op.getOperand(4);
7533     SDValue Src1 = Op.getOperand(5);
7534     // Hack around illegal type on SI by directly selecting it.
7535     if (isTypeLegal(Src0.getValueType()))
7536       return SDValue();
7537 
7538     const ConstantSDNode *Done = cast<ConstantSDNode>(Op.getOperand(6));
7539     SDValue Undef = DAG.getUNDEF(MVT::f32);
7540     const SDValue Ops[] = {
7541       Op.getOperand(2), // tgt
7542       DAG.getNode(ISD::BITCAST, DL, MVT::f32, Src0), // src0
7543       DAG.getNode(ISD::BITCAST, DL, MVT::f32, Src1), // src1
7544       Undef, // src2
7545       Undef, // src3
7546       Op.getOperand(7), // vm
7547       DAG.getTargetConstant(1, DL, MVT::i1), // compr
7548       Op.getOperand(3), // en
7549       Op.getOperand(0) // Chain
7550     };
7551 
7552     unsigned Opc = Done->isNullValue() ? AMDGPU::EXP : AMDGPU::EXP_DONE;
7553     return SDValue(DAG.getMachineNode(Opc, DL, Op->getVTList(), Ops), 0);
7554   }
7555   case Intrinsic::amdgcn_s_barrier: {
7556     if (getTargetMachine().getOptLevel() > CodeGenOpt::None) {
7557       const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
7558       unsigned WGSize = ST.getFlatWorkGroupSizes(MF.getFunction()).second;
7559       if (WGSize <= ST.getWavefrontSize())
7560         return SDValue(DAG.getMachineNode(AMDGPU::WAVE_BARRIER, DL, MVT::Other,
7561                                           Op.getOperand(0)), 0);
7562     }
7563     return SDValue();
7564   };
7565   case Intrinsic::amdgcn_tbuffer_store: {
7566     SDValue VData = Op.getOperand(2);
7567     bool IsD16 = (VData.getValueType().getScalarType() == MVT::f16);
7568     if (IsD16)
7569       VData = handleD16VData(VData, DAG);
7570     unsigned Dfmt = cast<ConstantSDNode>(Op.getOperand(8))->getZExtValue();
7571     unsigned Nfmt = cast<ConstantSDNode>(Op.getOperand(9))->getZExtValue();
7572     unsigned Glc = cast<ConstantSDNode>(Op.getOperand(10))->getZExtValue();
7573     unsigned Slc = cast<ConstantSDNode>(Op.getOperand(11))->getZExtValue();
7574     unsigned IdxEn = 1;
7575     if (auto Idx = dyn_cast<ConstantSDNode>(Op.getOperand(4)))
7576       IdxEn = Idx->getZExtValue() != 0;
7577     SDValue Ops[] = {
7578       Chain,
7579       VData,             // vdata
7580       Op.getOperand(3),  // rsrc
7581       Op.getOperand(4),  // vindex
7582       Op.getOperand(5),  // voffset
7583       Op.getOperand(6),  // soffset
7584       Op.getOperand(7),  // offset
7585       DAG.getTargetConstant(Dfmt | (Nfmt << 4), DL, MVT::i32), // format
7586       DAG.getTargetConstant(Glc | (Slc << 1), DL, MVT::i32), // cachepolicy
7587       DAG.getTargetConstant(IdxEn, DL, MVT::i1), // idexen
7588     };
7589     unsigned Opc = IsD16 ? AMDGPUISD::TBUFFER_STORE_FORMAT_D16 :
7590                            AMDGPUISD::TBUFFER_STORE_FORMAT;
7591     MemSDNode *M = cast<MemSDNode>(Op);
7592     return DAG.getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops,
7593                                    M->getMemoryVT(), M->getMemOperand());
7594   }
7595 
7596   case Intrinsic::amdgcn_struct_tbuffer_store: {
7597     SDValue VData = Op.getOperand(2);
7598     bool IsD16 = (VData.getValueType().getScalarType() == MVT::f16);
7599     if (IsD16)
7600       VData = handleD16VData(VData, DAG);
7601     auto Offsets = splitBufferOffsets(Op.getOperand(5), DAG);
7602     SDValue Ops[] = {
7603       Chain,
7604       VData,             // vdata
7605       Op.getOperand(3),  // rsrc
7606       Op.getOperand(4),  // vindex
7607       Offsets.first,     // voffset
7608       Op.getOperand(6),  // soffset
7609       Offsets.second,    // offset
7610       Op.getOperand(7),  // format
7611       Op.getOperand(8),  // cachepolicy, swizzled buffer
7612       DAG.getTargetConstant(1, DL, MVT::i1), // idexen
7613     };
7614     unsigned Opc = IsD16 ? AMDGPUISD::TBUFFER_STORE_FORMAT_D16 :
7615                            AMDGPUISD::TBUFFER_STORE_FORMAT;
7616     MemSDNode *M = cast<MemSDNode>(Op);
7617     return DAG.getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops,
7618                                    M->getMemoryVT(), M->getMemOperand());
7619   }
7620 
7621   case Intrinsic::amdgcn_raw_tbuffer_store: {
7622     SDValue VData = Op.getOperand(2);
7623     bool IsD16 = (VData.getValueType().getScalarType() == MVT::f16);
7624     if (IsD16)
7625       VData = handleD16VData(VData, DAG);
7626     auto Offsets = splitBufferOffsets(Op.getOperand(4), DAG);
7627     SDValue Ops[] = {
7628       Chain,
7629       VData,             // vdata
7630       Op.getOperand(3),  // rsrc
7631       DAG.getConstant(0, DL, MVT::i32), // vindex
7632       Offsets.first,     // voffset
7633       Op.getOperand(5),  // soffset
7634       Offsets.second,    // offset
7635       Op.getOperand(6),  // format
7636       Op.getOperand(7),  // cachepolicy, swizzled buffer
7637       DAG.getTargetConstant(0, DL, MVT::i1), // idexen
7638     };
7639     unsigned Opc = IsD16 ? AMDGPUISD::TBUFFER_STORE_FORMAT_D16 :
7640                            AMDGPUISD::TBUFFER_STORE_FORMAT;
7641     MemSDNode *M = cast<MemSDNode>(Op);
7642     return DAG.getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops,
7643                                    M->getMemoryVT(), M->getMemOperand());
7644   }
7645 
7646   case Intrinsic::amdgcn_buffer_store:
7647   case Intrinsic::amdgcn_buffer_store_format: {
7648     SDValue VData = Op.getOperand(2);
7649     bool IsD16 = (VData.getValueType().getScalarType() == MVT::f16);
7650     if (IsD16)
7651       VData = handleD16VData(VData, DAG);
7652     unsigned Glc = cast<ConstantSDNode>(Op.getOperand(6))->getZExtValue();
7653     unsigned Slc = cast<ConstantSDNode>(Op.getOperand(7))->getZExtValue();
7654     unsigned IdxEn = 1;
7655     if (auto Idx = dyn_cast<ConstantSDNode>(Op.getOperand(4)))
7656       IdxEn = Idx->getZExtValue() != 0;
7657     SDValue Ops[] = {
7658       Chain,
7659       VData,
7660       Op.getOperand(3), // rsrc
7661       Op.getOperand(4), // vindex
7662       SDValue(), // voffset -- will be set by setBufferOffsets
7663       SDValue(), // soffset -- will be set by setBufferOffsets
7664       SDValue(), // offset -- will be set by setBufferOffsets
7665       DAG.getTargetConstant(Glc | (Slc << 1), DL, MVT::i32), // cachepolicy
7666       DAG.getTargetConstant(IdxEn, DL, MVT::i1), // idxen
7667     };
7668     unsigned Offset = setBufferOffsets(Op.getOperand(5), DAG, &Ops[4]);
7669     // We don't know the offset if vindex is non-zero, so clear it.
7670     if (IdxEn)
7671       Offset = 0;
7672     unsigned Opc = IntrinsicID == Intrinsic::amdgcn_buffer_store ?
7673                    AMDGPUISD::BUFFER_STORE : AMDGPUISD::BUFFER_STORE_FORMAT;
7674     Opc = IsD16 ? AMDGPUISD::BUFFER_STORE_FORMAT_D16 : Opc;
7675     MemSDNode *M = cast<MemSDNode>(Op);
7676     M->getMemOperand()->setOffset(Offset);
7677 
7678     // Handle BUFFER_STORE_BYTE/SHORT overloaded intrinsics
7679     EVT VDataType = VData.getValueType().getScalarType();
7680     if (VDataType == MVT::i8 || VDataType == MVT::i16)
7681       return handleByteShortBufferStores(DAG, VDataType, DL, Ops, M);
7682 
7683     return DAG.getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops,
7684                                    M->getMemoryVT(), M->getMemOperand());
7685   }
7686 
7687   case Intrinsic::amdgcn_raw_buffer_store:
7688   case Intrinsic::amdgcn_raw_buffer_store_format: {
7689     const bool IsFormat =
7690         IntrinsicID == Intrinsic::amdgcn_raw_buffer_store_format;
7691 
7692     SDValue VData = Op.getOperand(2);
7693     EVT VDataVT = VData.getValueType();
7694     EVT EltType = VDataVT.getScalarType();
7695     bool IsD16 = IsFormat && (EltType.getSizeInBits() == 16);
7696     if (IsD16) {
7697       VData = handleD16VData(VData, DAG);
7698       VDataVT = VData.getValueType();
7699     }
7700 
7701     if (!isTypeLegal(VDataVT)) {
7702       VData =
7703           DAG.getNode(ISD::BITCAST, DL,
7704                       getEquivalentMemType(*DAG.getContext(), VDataVT), VData);
7705     }
7706 
7707     auto Offsets = splitBufferOffsets(Op.getOperand(4), DAG);
7708     SDValue Ops[] = {
7709       Chain,
7710       VData,
7711       Op.getOperand(3), // rsrc
7712       DAG.getConstant(0, DL, MVT::i32), // vindex
7713       Offsets.first,    // voffset
7714       Op.getOperand(5), // soffset
7715       Offsets.second,   // offset
7716       Op.getOperand(6), // cachepolicy, swizzled buffer
7717       DAG.getTargetConstant(0, DL, MVT::i1), // idxen
7718     };
7719     unsigned Opc =
7720         IsFormat ? AMDGPUISD::BUFFER_STORE_FORMAT : AMDGPUISD::BUFFER_STORE;
7721     Opc = IsD16 ? AMDGPUISD::BUFFER_STORE_FORMAT_D16 : Opc;
7722     MemSDNode *M = cast<MemSDNode>(Op);
7723     M->getMemOperand()->setOffset(getBufferOffsetForMMO(Ops[4], Ops[5], Ops[6]));
7724 
7725     // Handle BUFFER_STORE_BYTE/SHORT overloaded intrinsics
7726     if (!IsD16 && !VDataVT.isVector() && EltType.getSizeInBits() < 32)
7727       return handleByteShortBufferStores(DAG, VDataVT, DL, Ops, M);
7728 
7729     return DAG.getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops,
7730                                    M->getMemoryVT(), M->getMemOperand());
7731   }
7732 
7733   case Intrinsic::amdgcn_struct_buffer_store:
7734   case Intrinsic::amdgcn_struct_buffer_store_format: {
7735     const bool IsFormat =
7736         IntrinsicID == Intrinsic::amdgcn_struct_buffer_store_format;
7737 
7738     SDValue VData = Op.getOperand(2);
7739     EVT VDataVT = VData.getValueType();
7740     EVT EltType = VDataVT.getScalarType();
7741     bool IsD16 = IsFormat && (EltType.getSizeInBits() == 16);
7742 
7743     if (IsD16) {
7744       VData = handleD16VData(VData, DAG);
7745       VDataVT = VData.getValueType();
7746     }
7747 
7748     if (!isTypeLegal(VDataVT)) {
7749       VData =
7750           DAG.getNode(ISD::BITCAST, DL,
7751                       getEquivalentMemType(*DAG.getContext(), VDataVT), VData);
7752     }
7753 
7754     auto Offsets = splitBufferOffsets(Op.getOperand(5), DAG);
7755     SDValue Ops[] = {
7756       Chain,
7757       VData,
7758       Op.getOperand(3), // rsrc
7759       Op.getOperand(4), // vindex
7760       Offsets.first,    // voffset
7761       Op.getOperand(6), // soffset
7762       Offsets.second,   // offset
7763       Op.getOperand(7), // cachepolicy, swizzled buffer
7764       DAG.getTargetConstant(1, DL, MVT::i1), // idxen
7765     };
7766     unsigned Opc = IntrinsicID == Intrinsic::amdgcn_struct_buffer_store ?
7767                    AMDGPUISD::BUFFER_STORE : AMDGPUISD::BUFFER_STORE_FORMAT;
7768     Opc = IsD16 ? AMDGPUISD::BUFFER_STORE_FORMAT_D16 : Opc;
7769     MemSDNode *M = cast<MemSDNode>(Op);
7770     M->getMemOperand()->setOffset(getBufferOffsetForMMO(Ops[4], Ops[5], Ops[6],
7771                                                         Ops[3]));
7772 
7773     // Handle BUFFER_STORE_BYTE/SHORT overloaded intrinsics
7774     EVT VDataType = VData.getValueType().getScalarType();
7775     if (!IsD16 && !VDataVT.isVector() && EltType.getSizeInBits() < 32)
7776       return handleByteShortBufferStores(DAG, VDataType, DL, Ops, M);
7777 
7778     return DAG.getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops,
7779                                    M->getMemoryVT(), M->getMemOperand());
7780   }
7781   case Intrinsic::amdgcn_end_cf:
7782     return SDValue(DAG.getMachineNode(AMDGPU::SI_END_CF, DL, MVT::Other,
7783                                       Op->getOperand(2), Chain), 0);
7784 
7785   default: {
7786     if (const AMDGPU::ImageDimIntrinsicInfo *ImageDimIntr =
7787             AMDGPU::getImageDimIntrinsicInfo(IntrinsicID))
7788       return lowerImage(Op, ImageDimIntr, DAG, true);
7789 
7790     return Op;
7791   }
7792   }
7793 }
7794 
7795 // The raw.(t)buffer and struct.(t)buffer intrinsics have two offset args:
7796 // offset (the offset that is included in bounds checking and swizzling, to be
7797 // split between the instruction's voffset and immoffset fields) and soffset
7798 // (the offset that is excluded from bounds checking and swizzling, to go in
7799 // the instruction's soffset field).  This function takes the first kind of
7800 // offset and figures out how to split it between voffset and immoffset.
7801 std::pair<SDValue, SDValue> SITargetLowering::splitBufferOffsets(
7802     SDValue Offset, SelectionDAG &DAG) const {
7803   SDLoc DL(Offset);
7804   const unsigned MaxImm = 4095;
7805   SDValue N0 = Offset;
7806   ConstantSDNode *C1 = nullptr;
7807 
7808   if ((C1 = dyn_cast<ConstantSDNode>(N0)))
7809     N0 = SDValue();
7810   else if (DAG.isBaseWithConstantOffset(N0)) {
7811     C1 = cast<ConstantSDNode>(N0.getOperand(1));
7812     N0 = N0.getOperand(0);
7813   }
7814 
7815   if (C1) {
7816     unsigned ImmOffset = C1->getZExtValue();
7817     // If the immediate value is too big for the immoffset field, put the value
7818     // and -4096 into the immoffset field so that the value that is copied/added
7819     // for the voffset field is a multiple of 4096, and it stands more chance
7820     // of being CSEd with the copy/add for another similar load/store.
7821     // However, do not do that rounding down to a multiple of 4096 if that is a
7822     // negative number, as it appears to be illegal to have a negative offset
7823     // in the vgpr, even if adding the immediate offset makes it positive.
7824     unsigned Overflow = ImmOffset & ~MaxImm;
7825     ImmOffset -= Overflow;
7826     if ((int32_t)Overflow < 0) {
7827       Overflow += ImmOffset;
7828       ImmOffset = 0;
7829     }
7830     C1 = cast<ConstantSDNode>(DAG.getTargetConstant(ImmOffset, DL, MVT::i32));
7831     if (Overflow) {
7832       auto OverflowVal = DAG.getConstant(Overflow, DL, MVT::i32);
7833       if (!N0)
7834         N0 = OverflowVal;
7835       else {
7836         SDValue Ops[] = { N0, OverflowVal };
7837         N0 = DAG.getNode(ISD::ADD, DL, MVT::i32, Ops);
7838       }
7839     }
7840   }
7841   if (!N0)
7842     N0 = DAG.getConstant(0, DL, MVT::i32);
7843   if (!C1)
7844     C1 = cast<ConstantSDNode>(DAG.getTargetConstant(0, DL, MVT::i32));
7845   return {N0, SDValue(C1, 0)};
7846 }
7847 
7848 // Analyze a combined offset from an amdgcn_buffer_ intrinsic and store the
7849 // three offsets (voffset, soffset and instoffset) into the SDValue[3] array
7850 // pointed to by Offsets.
7851 unsigned SITargetLowering::setBufferOffsets(SDValue CombinedOffset,
7852                                             SelectionDAG &DAG, SDValue *Offsets,
7853                                             Align Alignment) const {
7854   SDLoc DL(CombinedOffset);
7855   if (auto C = dyn_cast<ConstantSDNode>(CombinedOffset)) {
7856     uint32_t Imm = C->getZExtValue();
7857     uint32_t SOffset, ImmOffset;
7858     if (AMDGPU::splitMUBUFOffset(Imm, SOffset, ImmOffset, Subtarget,
7859                                  Alignment)) {
7860       Offsets[0] = DAG.getConstant(0, DL, MVT::i32);
7861       Offsets[1] = DAG.getConstant(SOffset, DL, MVT::i32);
7862       Offsets[2] = DAG.getTargetConstant(ImmOffset, DL, MVT::i32);
7863       return SOffset + ImmOffset;
7864     }
7865   }
7866   if (DAG.isBaseWithConstantOffset(CombinedOffset)) {
7867     SDValue N0 = CombinedOffset.getOperand(0);
7868     SDValue N1 = CombinedOffset.getOperand(1);
7869     uint32_t SOffset, ImmOffset;
7870     int Offset = cast<ConstantSDNode>(N1)->getSExtValue();
7871     if (Offset >= 0 && AMDGPU::splitMUBUFOffset(Offset, SOffset, ImmOffset,
7872                                                 Subtarget, Alignment)) {
7873       Offsets[0] = N0;
7874       Offsets[1] = DAG.getConstant(SOffset, DL, MVT::i32);
7875       Offsets[2] = DAG.getTargetConstant(ImmOffset, DL, MVT::i32);
7876       return 0;
7877     }
7878   }
7879   Offsets[0] = CombinedOffset;
7880   Offsets[1] = DAG.getConstant(0, DL, MVT::i32);
7881   Offsets[2] = DAG.getTargetConstant(0, DL, MVT::i32);
7882   return 0;
7883 }
7884 
7885 // Handle 8 bit and 16 bit buffer loads
7886 SDValue SITargetLowering::handleByteShortBufferLoads(SelectionDAG &DAG,
7887                                                      EVT LoadVT, SDLoc DL,
7888                                                      ArrayRef<SDValue> Ops,
7889                                                      MemSDNode *M) const {
7890   EVT IntVT = LoadVT.changeTypeToInteger();
7891   unsigned Opc = (LoadVT.getScalarType() == MVT::i8) ?
7892          AMDGPUISD::BUFFER_LOAD_UBYTE : AMDGPUISD::BUFFER_LOAD_USHORT;
7893 
7894   SDVTList ResList = DAG.getVTList(MVT::i32, MVT::Other);
7895   SDValue BufferLoad = DAG.getMemIntrinsicNode(Opc, DL, ResList,
7896                                                Ops, IntVT,
7897                                                M->getMemOperand());
7898   SDValue LoadVal = DAG.getNode(ISD::TRUNCATE, DL, IntVT, BufferLoad);
7899   LoadVal = DAG.getNode(ISD::BITCAST, DL, LoadVT, LoadVal);
7900 
7901   return DAG.getMergeValues({LoadVal, BufferLoad.getValue(1)}, DL);
7902 }
7903 
7904 // Handle 8 bit and 16 bit buffer stores
7905 SDValue SITargetLowering::handleByteShortBufferStores(SelectionDAG &DAG,
7906                                                       EVT VDataType, SDLoc DL,
7907                                                       SDValue Ops[],
7908                                                       MemSDNode *M) const {
7909   if (VDataType == MVT::f16)
7910     Ops[1] = DAG.getNode(ISD::BITCAST, DL, MVT::i16, Ops[1]);
7911 
7912   SDValue BufferStoreExt = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, Ops[1]);
7913   Ops[1] = BufferStoreExt;
7914   unsigned Opc = (VDataType == MVT::i8) ? AMDGPUISD::BUFFER_STORE_BYTE :
7915                                  AMDGPUISD::BUFFER_STORE_SHORT;
7916   ArrayRef<SDValue> OpsRef = makeArrayRef(&Ops[0], 9);
7917   return DAG.getMemIntrinsicNode(Opc, DL, M->getVTList(), OpsRef, VDataType,
7918                                      M->getMemOperand());
7919 }
7920 
7921 static SDValue getLoadExtOrTrunc(SelectionDAG &DAG,
7922                                  ISD::LoadExtType ExtType, SDValue Op,
7923                                  const SDLoc &SL, EVT VT) {
7924   if (VT.bitsLT(Op.getValueType()))
7925     return DAG.getNode(ISD::TRUNCATE, SL, VT, Op);
7926 
7927   switch (ExtType) {
7928   case ISD::SEXTLOAD:
7929     return DAG.getNode(ISD::SIGN_EXTEND, SL, VT, Op);
7930   case ISD::ZEXTLOAD:
7931     return DAG.getNode(ISD::ZERO_EXTEND, SL, VT, Op);
7932   case ISD::EXTLOAD:
7933     return DAG.getNode(ISD::ANY_EXTEND, SL, VT, Op);
7934   case ISD::NON_EXTLOAD:
7935     return Op;
7936   }
7937 
7938   llvm_unreachable("invalid ext type");
7939 }
7940 
7941 SDValue SITargetLowering::widenLoad(LoadSDNode *Ld, DAGCombinerInfo &DCI) const {
7942   SelectionDAG &DAG = DCI.DAG;
7943   if (Ld->getAlignment() < 4 || Ld->isDivergent())
7944     return SDValue();
7945 
7946   // FIXME: Constant loads should all be marked invariant.
7947   unsigned AS = Ld->getAddressSpace();
7948   if (AS != AMDGPUAS::CONSTANT_ADDRESS &&
7949       AS != AMDGPUAS::CONSTANT_ADDRESS_32BIT &&
7950       (AS != AMDGPUAS::GLOBAL_ADDRESS || !Ld->isInvariant()))
7951     return SDValue();
7952 
7953   // Don't do this early, since it may interfere with adjacent load merging for
7954   // illegal types. We can avoid losing alignment information for exotic types
7955   // pre-legalize.
7956   EVT MemVT = Ld->getMemoryVT();
7957   if ((MemVT.isSimple() && !DCI.isAfterLegalizeDAG()) ||
7958       MemVT.getSizeInBits() >= 32)
7959     return SDValue();
7960 
7961   SDLoc SL(Ld);
7962 
7963   assert((!MemVT.isVector() || Ld->getExtensionType() == ISD::NON_EXTLOAD) &&
7964          "unexpected vector extload");
7965 
7966   // TODO: Drop only high part of range.
7967   SDValue Ptr = Ld->getBasePtr();
7968   SDValue NewLoad = DAG.getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD,
7969                                 MVT::i32, SL, Ld->getChain(), Ptr,
7970                                 Ld->getOffset(),
7971                                 Ld->getPointerInfo(), MVT::i32,
7972                                 Ld->getAlignment(),
7973                                 Ld->getMemOperand()->getFlags(),
7974                                 Ld->getAAInfo(),
7975                                 nullptr); // Drop ranges
7976 
7977   EVT TruncVT = EVT::getIntegerVT(*DAG.getContext(), MemVT.getSizeInBits());
7978   if (MemVT.isFloatingPoint()) {
7979     assert(Ld->getExtensionType() == ISD::NON_EXTLOAD &&
7980            "unexpected fp extload");
7981     TruncVT = MemVT.changeTypeToInteger();
7982   }
7983 
7984   SDValue Cvt = NewLoad;
7985   if (Ld->getExtensionType() == ISD::SEXTLOAD) {
7986     Cvt = DAG.getNode(ISD::SIGN_EXTEND_INREG, SL, MVT::i32, NewLoad,
7987                       DAG.getValueType(TruncVT));
7988   } else if (Ld->getExtensionType() == ISD::ZEXTLOAD ||
7989              Ld->getExtensionType() == ISD::NON_EXTLOAD) {
7990     Cvt = DAG.getZeroExtendInReg(NewLoad, SL, TruncVT);
7991   } else {
7992     assert(Ld->getExtensionType() == ISD::EXTLOAD);
7993   }
7994 
7995   EVT VT = Ld->getValueType(0);
7996   EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
7997 
7998   DCI.AddToWorklist(Cvt.getNode());
7999 
8000   // We may need to handle exotic cases, such as i16->i64 extloads, so insert
8001   // the appropriate extension from the 32-bit load.
8002   Cvt = getLoadExtOrTrunc(DAG, Ld->getExtensionType(), Cvt, SL, IntVT);
8003   DCI.AddToWorklist(Cvt.getNode());
8004 
8005   // Handle conversion back to floating point if necessary.
8006   Cvt = DAG.getNode(ISD::BITCAST, SL, VT, Cvt);
8007 
8008   return DAG.getMergeValues({ Cvt, NewLoad.getValue(1) }, SL);
8009 }
8010 
8011 SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
8012   SDLoc DL(Op);
8013   LoadSDNode *Load = cast<LoadSDNode>(Op);
8014   ISD::LoadExtType ExtType = Load->getExtensionType();
8015   EVT MemVT = Load->getMemoryVT();
8016 
8017   if (ExtType == ISD::NON_EXTLOAD && MemVT.getSizeInBits() < 32) {
8018     if (MemVT == MVT::i16 && isTypeLegal(MVT::i16))
8019       return SDValue();
8020 
8021     // FIXME: Copied from PPC
8022     // First, load into 32 bits, then truncate to 1 bit.
8023 
8024     SDValue Chain = Load->getChain();
8025     SDValue BasePtr = Load->getBasePtr();
8026     MachineMemOperand *MMO = Load->getMemOperand();
8027 
8028     EVT RealMemVT = (MemVT == MVT::i1) ? MVT::i8 : MVT::i16;
8029 
8030     SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain,
8031                                    BasePtr, RealMemVT, MMO);
8032 
8033     if (!MemVT.isVector()) {
8034       SDValue Ops[] = {
8035         DAG.getNode(ISD::TRUNCATE, DL, MemVT, NewLD),
8036         NewLD.getValue(1)
8037       };
8038 
8039       return DAG.getMergeValues(Ops, DL);
8040     }
8041 
8042     SmallVector<SDValue, 3> Elts;
8043     for (unsigned I = 0, N = MemVT.getVectorNumElements(); I != N; ++I) {
8044       SDValue Elt = DAG.getNode(ISD::SRL, DL, MVT::i32, NewLD,
8045                                 DAG.getConstant(I, DL, MVT::i32));
8046 
8047       Elts.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i1, Elt));
8048     }
8049 
8050     SDValue Ops[] = {
8051       DAG.getBuildVector(MemVT, DL, Elts),
8052       NewLD.getValue(1)
8053     };
8054 
8055     return DAG.getMergeValues(Ops, DL);
8056   }
8057 
8058   if (!MemVT.isVector())
8059     return SDValue();
8060 
8061   assert(Op.getValueType().getVectorElementType() == MVT::i32 &&
8062          "Custom lowering for non-i32 vectors hasn't been implemented.");
8063 
8064   unsigned Alignment = Load->getAlignment();
8065   unsigned AS = Load->getAddressSpace();
8066   if (Subtarget->hasLDSMisalignedBug() &&
8067       AS == AMDGPUAS::FLAT_ADDRESS &&
8068       Alignment < MemVT.getStoreSize() && MemVT.getSizeInBits() > 32) {
8069     return SplitVectorLoad(Op, DAG);
8070   }
8071 
8072   MachineFunction &MF = DAG.getMachineFunction();
8073   SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
8074   // If there is a possibilty that flat instruction access scratch memory
8075   // then we need to use the same legalization rules we use for private.
8076   if (AS == AMDGPUAS::FLAT_ADDRESS &&
8077       !Subtarget->hasMultiDwordFlatScratchAddressing())
8078     AS = MFI->hasFlatScratchInit() ?
8079          AMDGPUAS::PRIVATE_ADDRESS : AMDGPUAS::GLOBAL_ADDRESS;
8080 
8081   unsigned NumElements = MemVT.getVectorNumElements();
8082 
8083   if (AS == AMDGPUAS::CONSTANT_ADDRESS ||
8084       AS == AMDGPUAS::CONSTANT_ADDRESS_32BIT) {
8085     if (!Op->isDivergent() && Alignment >= 4 && NumElements < 32) {
8086       if (MemVT.isPow2VectorType())
8087         return SDValue();
8088       return WidenOrSplitVectorLoad(Op, DAG);
8089     }
8090     // Non-uniform loads will be selected to MUBUF instructions, so they
8091     // have the same legalization requirements as global and private
8092     // loads.
8093     //
8094   }
8095 
8096   if (AS == AMDGPUAS::CONSTANT_ADDRESS ||
8097       AS == AMDGPUAS::CONSTANT_ADDRESS_32BIT ||
8098       AS == AMDGPUAS::GLOBAL_ADDRESS) {
8099     if (Subtarget->getScalarizeGlobalBehavior() && !Op->isDivergent() &&
8100         Load->isSimple() && isMemOpHasNoClobberedMemOperand(Load) &&
8101         Alignment >= 4 && NumElements < 32) {
8102       if (MemVT.isPow2VectorType())
8103         return SDValue();
8104       return WidenOrSplitVectorLoad(Op, DAG);
8105     }
8106     // Non-uniform loads will be selected to MUBUF instructions, so they
8107     // have the same legalization requirements as global and private
8108     // loads.
8109     //
8110   }
8111   if (AS == AMDGPUAS::CONSTANT_ADDRESS ||
8112       AS == AMDGPUAS::CONSTANT_ADDRESS_32BIT ||
8113       AS == AMDGPUAS::GLOBAL_ADDRESS ||
8114       AS == AMDGPUAS::FLAT_ADDRESS) {
8115     if (NumElements > 4)
8116       return SplitVectorLoad(Op, DAG);
8117     // v3 loads not supported on SI.
8118     if (NumElements == 3 && !Subtarget->hasDwordx3LoadStores())
8119       return WidenOrSplitVectorLoad(Op, DAG);
8120 
8121     // v3 and v4 loads are supported for private and global memory.
8122     return SDValue();
8123   }
8124   if (AS == AMDGPUAS::PRIVATE_ADDRESS) {
8125     // Depending on the setting of the private_element_size field in the
8126     // resource descriptor, we can only make private accesses up to a certain
8127     // size.
8128     switch (Subtarget->getMaxPrivateElementSize()) {
8129     case 4: {
8130       SDValue Ops[2];
8131       std::tie(Ops[0], Ops[1]) = scalarizeVectorLoad(Load, DAG);
8132       return DAG.getMergeValues(Ops, DL);
8133     }
8134     case 8:
8135       if (NumElements > 2)
8136         return SplitVectorLoad(Op, DAG);
8137       return SDValue();
8138     case 16:
8139       // Same as global/flat
8140       if (NumElements > 4)
8141         return SplitVectorLoad(Op, DAG);
8142       // v3 loads not supported on SI.
8143       if (NumElements == 3 && !Subtarget->hasDwordx3LoadStores())
8144         return WidenOrSplitVectorLoad(Op, DAG);
8145 
8146       return SDValue();
8147     default:
8148       llvm_unreachable("unsupported private_element_size");
8149     }
8150   } else if (AS == AMDGPUAS::LOCAL_ADDRESS || AS == AMDGPUAS::REGION_ADDRESS) {
8151     // Use ds_read_b128 or ds_read_b96 when possible.
8152     if (Subtarget->hasDS96AndDS128() &&
8153         ((Subtarget->useDS128() && MemVT.getStoreSize() == 16) ||
8154          MemVT.getStoreSize() == 12) &&
8155         allowsMisalignedMemoryAccessesImpl(MemVT.getSizeInBits(), AS,
8156                                            Load->getAlign()))
8157       return SDValue();
8158 
8159     if (NumElements > 2)
8160       return SplitVectorLoad(Op, DAG);
8161 
8162     // SI has a hardware bug in the LDS / GDS boounds checking: if the base
8163     // address is negative, then the instruction is incorrectly treated as
8164     // out-of-bounds even if base + offsets is in bounds. Split vectorized
8165     // loads here to avoid emitting ds_read2_b32. We may re-combine the
8166     // load later in the SILoadStoreOptimizer.
8167     if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS &&
8168         NumElements == 2 && MemVT.getStoreSize() == 8 &&
8169         Load->getAlignment() < 8) {
8170       return SplitVectorLoad(Op, DAG);
8171     }
8172   }
8173 
8174   if (!allowsMemoryAccessForAlignment(*DAG.getContext(), DAG.getDataLayout(),
8175                                       MemVT, *Load->getMemOperand())) {
8176     SDValue Ops[2];
8177     std::tie(Ops[0], Ops[1]) = expandUnalignedLoad(Load, DAG);
8178     return DAG.getMergeValues(Ops, DL);
8179   }
8180 
8181   return SDValue();
8182 }
8183 
8184 SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
8185   EVT VT = Op.getValueType();
8186   assert(VT.getSizeInBits() == 64);
8187 
8188   SDLoc DL(Op);
8189   SDValue Cond = Op.getOperand(0);
8190 
8191   SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
8192   SDValue One = DAG.getConstant(1, DL, MVT::i32);
8193 
8194   SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
8195   SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
8196 
8197   SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
8198   SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
8199 
8200   SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
8201 
8202   SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
8203   SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
8204 
8205   SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
8206 
8207   SDValue Res = DAG.getBuildVector(MVT::v2i32, DL, {Lo, Hi});
8208   return DAG.getNode(ISD::BITCAST, DL, VT, Res);
8209 }
8210 
8211 // Catch division cases where we can use shortcuts with rcp and rsq
8212 // instructions.
8213 SDValue SITargetLowering::lowerFastUnsafeFDIV(SDValue Op,
8214                                               SelectionDAG &DAG) const {
8215   SDLoc SL(Op);
8216   SDValue LHS = Op.getOperand(0);
8217   SDValue RHS = Op.getOperand(1);
8218   EVT VT = Op.getValueType();
8219   const SDNodeFlags Flags = Op->getFlags();
8220 
8221   bool AllowInaccurateRcp = Flags.hasApproximateFuncs();
8222 
8223   // Without !fpmath accuracy information, we can't do more because we don't
8224   // know exactly whether rcp is accurate enough to meet !fpmath requirement.
8225   if (!AllowInaccurateRcp)
8226     return SDValue();
8227 
8228   if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
8229     if (CLHS->isExactlyValue(1.0)) {
8230       // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to
8231       // the CI documentation has a worst case error of 1 ulp.
8232       // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to
8233       // use it as long as we aren't trying to use denormals.
8234       //
8235       // v_rcp_f16 and v_rsq_f16 DO support denormals.
8236 
8237       // 1.0 / sqrt(x) -> rsq(x)
8238 
8239       // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP
8240       // error seems really high at 2^29 ULP.
8241       if (RHS.getOpcode() == ISD::FSQRT)
8242         return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0));
8243 
8244       // 1.0 / x -> rcp(x)
8245       return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
8246     }
8247 
8248     // Same as for 1.0, but expand the sign out of the constant.
8249     if (CLHS->isExactlyValue(-1.0)) {
8250       // -1.0 / x -> rcp (fneg x)
8251       SDValue FNegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
8252       return DAG.getNode(AMDGPUISD::RCP, SL, VT, FNegRHS);
8253     }
8254   }
8255 
8256   // Turn into multiply by the reciprocal.
8257   // x / y -> x * (1.0 / y)
8258   SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
8259   return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip, Flags);
8260 }
8261 
8262 SDValue SITargetLowering::lowerFastUnsafeFDIV64(SDValue Op,
8263                                                 SelectionDAG &DAG) const {
8264   SDLoc SL(Op);
8265   SDValue X = Op.getOperand(0);
8266   SDValue Y = Op.getOperand(1);
8267   EVT VT = Op.getValueType();
8268   const SDNodeFlags Flags = Op->getFlags();
8269 
8270   bool AllowInaccurateDiv = Flags.hasApproximateFuncs() ||
8271                             DAG.getTarget().Options.UnsafeFPMath;
8272   if (!AllowInaccurateDiv)
8273     return SDValue();
8274 
8275   SDValue NegY = DAG.getNode(ISD::FNEG, SL, VT, Y);
8276   SDValue One = DAG.getConstantFP(1.0, SL, VT);
8277 
8278   SDValue R = DAG.getNode(AMDGPUISD::RCP, SL, VT, Y);
8279   SDValue Tmp0 = DAG.getNode(ISD::FMA, SL, VT, NegY, R, One);
8280 
8281   R = DAG.getNode(ISD::FMA, SL, VT, Tmp0, R, R);
8282   SDValue Tmp1 = DAG.getNode(ISD::FMA, SL, VT, NegY, R, One);
8283   R = DAG.getNode(ISD::FMA, SL, VT, Tmp1, R, R);
8284   SDValue Ret = DAG.getNode(ISD::FMUL, SL, VT, X, R);
8285   SDValue Tmp2 = DAG.getNode(ISD::FMA, SL, VT, NegY, Ret, X);
8286   return DAG.getNode(ISD::FMA, SL, VT, Tmp2, R, Ret);
8287 }
8288 
8289 static SDValue getFPBinOp(SelectionDAG &DAG, unsigned Opcode, const SDLoc &SL,
8290                           EVT VT, SDValue A, SDValue B, SDValue GlueChain,
8291                           SDNodeFlags Flags) {
8292   if (GlueChain->getNumValues() <= 1) {
8293     return DAG.getNode(Opcode, SL, VT, A, B, Flags);
8294   }
8295 
8296   assert(GlueChain->getNumValues() == 3);
8297 
8298   SDVTList VTList = DAG.getVTList(VT, MVT::Other, MVT::Glue);
8299   switch (Opcode) {
8300   default: llvm_unreachable("no chain equivalent for opcode");
8301   case ISD::FMUL:
8302     Opcode = AMDGPUISD::FMUL_W_CHAIN;
8303     break;
8304   }
8305 
8306   return DAG.getNode(Opcode, SL, VTList,
8307                      {GlueChain.getValue(1), A, B, GlueChain.getValue(2)},
8308                      Flags);
8309 }
8310 
8311 static SDValue getFPTernOp(SelectionDAG &DAG, unsigned Opcode, const SDLoc &SL,
8312                            EVT VT, SDValue A, SDValue B, SDValue C,
8313                            SDValue GlueChain, SDNodeFlags Flags) {
8314   if (GlueChain->getNumValues() <= 1) {
8315     return DAG.getNode(Opcode, SL, VT, {A, B, C}, Flags);
8316   }
8317 
8318   assert(GlueChain->getNumValues() == 3);
8319 
8320   SDVTList VTList = DAG.getVTList(VT, MVT::Other, MVT::Glue);
8321   switch (Opcode) {
8322   default: llvm_unreachable("no chain equivalent for opcode");
8323   case ISD::FMA:
8324     Opcode = AMDGPUISD::FMA_W_CHAIN;
8325     break;
8326   }
8327 
8328   return DAG.getNode(Opcode, SL, VTList,
8329                      {GlueChain.getValue(1), A, B, C, GlueChain.getValue(2)},
8330                      Flags);
8331 }
8332 
8333 SDValue SITargetLowering::LowerFDIV16(SDValue Op, SelectionDAG &DAG) const {
8334   if (SDValue FastLowered = lowerFastUnsafeFDIV(Op, DAG))
8335     return FastLowered;
8336 
8337   SDLoc SL(Op);
8338   SDValue Src0 = Op.getOperand(0);
8339   SDValue Src1 = Op.getOperand(1);
8340 
8341   SDValue CvtSrc0 = DAG.getNode(ISD::FP_EXTEND, SL, MVT::f32, Src0);
8342   SDValue CvtSrc1 = DAG.getNode(ISD::FP_EXTEND, SL, MVT::f32, Src1);
8343 
8344   SDValue RcpSrc1 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, CvtSrc1);
8345   SDValue Quot = DAG.getNode(ISD::FMUL, SL, MVT::f32, CvtSrc0, RcpSrc1);
8346 
8347   SDValue FPRoundFlag = DAG.getTargetConstant(0, SL, MVT::i32);
8348   SDValue BestQuot = DAG.getNode(ISD::FP_ROUND, SL, MVT::f16, Quot, FPRoundFlag);
8349 
8350   return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f16, BestQuot, Src1, Src0);
8351 }
8352 
8353 // Faster 2.5 ULP division that does not support denormals.
8354 SDValue SITargetLowering::lowerFDIV_FAST(SDValue Op, SelectionDAG &DAG) const {
8355   SDLoc SL(Op);
8356   SDValue LHS = Op.getOperand(1);
8357   SDValue RHS = Op.getOperand(2);
8358 
8359   SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS);
8360 
8361   const APFloat K0Val(BitsToFloat(0x6f800000));
8362   const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32);
8363 
8364   const APFloat K1Val(BitsToFloat(0x2f800000));
8365   const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32);
8366 
8367   const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
8368 
8369   EVT SetCCVT =
8370     getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32);
8371 
8372   SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
8373 
8374   SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One);
8375 
8376   // TODO: Should this propagate fast-math-flags?
8377   r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3);
8378 
8379   // rcp does not support denormals.
8380   SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1);
8381 
8382   SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0);
8383 
8384   return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul);
8385 }
8386 
8387 // Returns immediate value for setting the F32 denorm mode when using the
8388 // S_DENORM_MODE instruction.
8389 static SDValue getSPDenormModeValue(int SPDenormMode, SelectionDAG &DAG,
8390                                     const SDLoc &SL, const GCNSubtarget *ST) {
8391   assert(ST->hasDenormModeInst() && "Requires S_DENORM_MODE");
8392   int DPDenormModeDefault = hasFP64FP16Denormals(DAG.getMachineFunction())
8393                                 ? FP_DENORM_FLUSH_NONE
8394                                 : FP_DENORM_FLUSH_IN_FLUSH_OUT;
8395 
8396   int Mode = SPDenormMode | (DPDenormModeDefault << 2);
8397   return DAG.getTargetConstant(Mode, SL, MVT::i32);
8398 }
8399 
8400 SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
8401   if (SDValue FastLowered = lowerFastUnsafeFDIV(Op, DAG))
8402     return FastLowered;
8403 
8404   // The selection matcher assumes anything with a chain selecting to a
8405   // mayRaiseFPException machine instruction. Since we're introducing a chain
8406   // here, we need to explicitly report nofpexcept for the regular fdiv
8407   // lowering.
8408   SDNodeFlags Flags = Op->getFlags();
8409   Flags.setNoFPExcept(true);
8410 
8411   SDLoc SL(Op);
8412   SDValue LHS = Op.getOperand(0);
8413   SDValue RHS = Op.getOperand(1);
8414 
8415   const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
8416 
8417   SDVTList ScaleVT = DAG.getVTList(MVT::f32, MVT::i1);
8418 
8419   SDValue DenominatorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT,
8420                                           {RHS, RHS, LHS}, Flags);
8421   SDValue NumeratorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT,
8422                                         {LHS, RHS, LHS}, Flags);
8423 
8424   // Denominator is scaled to not be denormal, so using rcp is ok.
8425   SDValue ApproxRcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32,
8426                                   DenominatorScaled, Flags);
8427   SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f32,
8428                                      DenominatorScaled, Flags);
8429 
8430   const unsigned Denorm32Reg = AMDGPU::Hwreg::ID_MODE |
8431                                (4 << AMDGPU::Hwreg::OFFSET_SHIFT_) |
8432                                (1 << AMDGPU::Hwreg::WIDTH_M1_SHIFT_);
8433   const SDValue BitField = DAG.getTargetConstant(Denorm32Reg, SL, MVT::i32);
8434 
8435   const bool HasFP32Denormals = hasFP32Denormals(DAG.getMachineFunction());
8436 
8437   if (!HasFP32Denormals) {
8438     // Note we can't use the STRICT_FMA/STRICT_FMUL for the non-strict FDIV
8439     // lowering. The chain dependence is insufficient, and we need glue. We do
8440     // not need the glue variants in a strictfp function.
8441 
8442     SDVTList BindParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
8443 
8444     SDNode *EnableDenorm;
8445     if (Subtarget->hasDenormModeInst()) {
8446       const SDValue EnableDenormValue =
8447           getSPDenormModeValue(FP_DENORM_FLUSH_NONE, DAG, SL, Subtarget);
8448 
8449       EnableDenorm = DAG.getNode(AMDGPUISD::DENORM_MODE, SL, BindParamVTs,
8450                                  DAG.getEntryNode(), EnableDenormValue).getNode();
8451     } else {
8452       const SDValue EnableDenormValue = DAG.getConstant(FP_DENORM_FLUSH_NONE,
8453                                                         SL, MVT::i32);
8454       EnableDenorm =
8455           DAG.getMachineNode(AMDGPU::S_SETREG_B32, SL, BindParamVTs,
8456                              {EnableDenormValue, BitField, DAG.getEntryNode()});
8457     }
8458 
8459     SDValue Ops[3] = {
8460       NegDivScale0,
8461       SDValue(EnableDenorm, 0),
8462       SDValue(EnableDenorm, 1)
8463     };
8464 
8465     NegDivScale0 = DAG.getMergeValues(Ops, SL);
8466   }
8467 
8468   SDValue Fma0 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, NegDivScale0,
8469                              ApproxRcp, One, NegDivScale0, Flags);
8470 
8471   SDValue Fma1 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, Fma0, ApproxRcp,
8472                              ApproxRcp, Fma0, Flags);
8473 
8474   SDValue Mul = getFPBinOp(DAG, ISD::FMUL, SL, MVT::f32, NumeratorScaled,
8475                            Fma1, Fma1, Flags);
8476 
8477   SDValue Fma2 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, NegDivScale0, Mul,
8478                              NumeratorScaled, Mul, Flags);
8479 
8480   SDValue Fma3 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32,
8481                              Fma2, Fma1, Mul, Fma2, Flags);
8482 
8483   SDValue Fma4 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, NegDivScale0, Fma3,
8484                              NumeratorScaled, Fma3, Flags);
8485 
8486   if (!HasFP32Denormals) {
8487     SDNode *DisableDenorm;
8488     if (Subtarget->hasDenormModeInst()) {
8489       const SDValue DisableDenormValue =
8490           getSPDenormModeValue(FP_DENORM_FLUSH_IN_FLUSH_OUT, DAG, SL, Subtarget);
8491 
8492       DisableDenorm = DAG.getNode(AMDGPUISD::DENORM_MODE, SL, MVT::Other,
8493                                   Fma4.getValue(1), DisableDenormValue,
8494                                   Fma4.getValue(2)).getNode();
8495     } else {
8496       const SDValue DisableDenormValue =
8497           DAG.getConstant(FP_DENORM_FLUSH_IN_FLUSH_OUT, SL, MVT::i32);
8498 
8499       DisableDenorm = DAG.getMachineNode(
8500           AMDGPU::S_SETREG_B32, SL, MVT::Other,
8501           {DisableDenormValue, BitField, Fma4.getValue(1), Fma4.getValue(2)});
8502     }
8503 
8504     SDValue OutputChain = DAG.getNode(ISD::TokenFactor, SL, MVT::Other,
8505                                       SDValue(DisableDenorm, 0), DAG.getRoot());
8506     DAG.setRoot(OutputChain);
8507   }
8508 
8509   SDValue Scale = NumeratorScaled.getValue(1);
8510   SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f32,
8511                              {Fma4, Fma1, Fma3, Scale}, Flags);
8512 
8513   return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f32, Fmas, RHS, LHS, Flags);
8514 }
8515 
8516 SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
8517   if (SDValue FastLowered = lowerFastUnsafeFDIV64(Op, DAG))
8518     return FastLowered;
8519 
8520   SDLoc SL(Op);
8521   SDValue X = Op.getOperand(0);
8522   SDValue Y = Op.getOperand(1);
8523 
8524   const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
8525 
8526   SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1);
8527 
8528   SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X);
8529 
8530   SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0);
8531 
8532   SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0);
8533 
8534   SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One);
8535 
8536   SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp);
8537 
8538   SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One);
8539 
8540   SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X);
8541 
8542   SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1);
8543   SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3);
8544 
8545   SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64,
8546                              NegDivScale0, Mul, DivScale1);
8547 
8548   SDValue Scale;
8549 
8550   if (!Subtarget->hasUsableDivScaleConditionOutput()) {
8551     // Workaround a hardware bug on SI where the condition output from div_scale
8552     // is not usable.
8553 
8554     const SDValue Hi = DAG.getConstant(1, SL, MVT::i32);
8555 
8556     // Figure out if the scale to use for div_fmas.
8557     SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
8558     SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y);
8559     SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0);
8560     SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1);
8561 
8562     SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi);
8563     SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi);
8564 
8565     SDValue Scale0Hi
8566       = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi);
8567     SDValue Scale1Hi
8568       = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi);
8569 
8570     SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ);
8571     SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ);
8572     Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen);
8573   } else {
8574     Scale = DivScale1.getValue(1);
8575   }
8576 
8577   SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64,
8578                              Fma4, Fma3, Mul, Scale);
8579 
8580   return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X);
8581 }
8582 
8583 SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
8584   EVT VT = Op.getValueType();
8585 
8586   if (VT == MVT::f32)
8587     return LowerFDIV32(Op, DAG);
8588 
8589   if (VT == MVT::f64)
8590     return LowerFDIV64(Op, DAG);
8591 
8592   if (VT == MVT::f16)
8593     return LowerFDIV16(Op, DAG);
8594 
8595   llvm_unreachable("Unexpected type for fdiv");
8596 }
8597 
8598 SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
8599   SDLoc DL(Op);
8600   StoreSDNode *Store = cast<StoreSDNode>(Op);
8601   EVT VT = Store->getMemoryVT();
8602 
8603   if (VT == MVT::i1) {
8604     return DAG.getTruncStore(Store->getChain(), DL,
8605        DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
8606        Store->getBasePtr(), MVT::i1, Store->getMemOperand());
8607   }
8608 
8609   assert(VT.isVector() &&
8610          Store->getValue().getValueType().getScalarType() == MVT::i32);
8611 
8612   unsigned AS = Store->getAddressSpace();
8613   if (Subtarget->hasLDSMisalignedBug() &&
8614       AS == AMDGPUAS::FLAT_ADDRESS &&
8615       Store->getAlignment() < VT.getStoreSize() && VT.getSizeInBits() > 32) {
8616     return SplitVectorStore(Op, DAG);
8617   }
8618 
8619   MachineFunction &MF = DAG.getMachineFunction();
8620   SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
8621   // If there is a possibilty that flat instruction access scratch memory
8622   // then we need to use the same legalization rules we use for private.
8623   if (AS == AMDGPUAS::FLAT_ADDRESS &&
8624       !Subtarget->hasMultiDwordFlatScratchAddressing())
8625     AS = MFI->hasFlatScratchInit() ?
8626          AMDGPUAS::PRIVATE_ADDRESS : AMDGPUAS::GLOBAL_ADDRESS;
8627 
8628   unsigned NumElements = VT.getVectorNumElements();
8629   if (AS == AMDGPUAS::GLOBAL_ADDRESS ||
8630       AS == AMDGPUAS::FLAT_ADDRESS) {
8631     if (NumElements > 4)
8632       return SplitVectorStore(Op, DAG);
8633     // v3 stores not supported on SI.
8634     if (NumElements == 3 && !Subtarget->hasDwordx3LoadStores())
8635       return SplitVectorStore(Op, DAG);
8636 
8637     if (!allowsMemoryAccessForAlignment(*DAG.getContext(), DAG.getDataLayout(),
8638                                         VT, *Store->getMemOperand()))
8639       return expandUnalignedStore(Store, DAG);
8640 
8641     return SDValue();
8642   } else if (AS == AMDGPUAS::PRIVATE_ADDRESS) {
8643     switch (Subtarget->getMaxPrivateElementSize()) {
8644     case 4:
8645       return scalarizeVectorStore(Store, DAG);
8646     case 8:
8647       if (NumElements > 2)
8648         return SplitVectorStore(Op, DAG);
8649       return SDValue();
8650     case 16:
8651       if (NumElements > 4 ||
8652           (NumElements == 3 && !Subtarget->enableFlatScratch()))
8653         return SplitVectorStore(Op, DAG);
8654       return SDValue();
8655     default:
8656       llvm_unreachable("unsupported private_element_size");
8657     }
8658   } else if (AS == AMDGPUAS::LOCAL_ADDRESS || AS == AMDGPUAS::REGION_ADDRESS) {
8659     // Use ds_write_b128 or ds_write_b96 when possible.
8660     if (Subtarget->hasDS96AndDS128() &&
8661         ((Subtarget->useDS128() && VT.getStoreSize() == 16) ||
8662          (VT.getStoreSize() == 12)) &&
8663         allowsMisalignedMemoryAccessesImpl(VT.getSizeInBits(), AS,
8664                                            Store->getAlign()))
8665       return SDValue();
8666 
8667     if (NumElements > 2)
8668       return SplitVectorStore(Op, DAG);
8669 
8670     // SI has a hardware bug in the LDS / GDS boounds checking: if the base
8671     // address is negative, then the instruction is incorrectly treated as
8672     // out-of-bounds even if base + offsets is in bounds. Split vectorized
8673     // stores here to avoid emitting ds_write2_b32. We may re-combine the
8674     // store later in the SILoadStoreOptimizer.
8675     if (!Subtarget->hasUsableDSOffset() &&
8676         NumElements == 2 && VT.getStoreSize() == 8 &&
8677         Store->getAlignment() < 8) {
8678       return SplitVectorStore(Op, DAG);
8679     }
8680 
8681     if (!allowsMemoryAccessForAlignment(*DAG.getContext(), DAG.getDataLayout(),
8682                                         VT, *Store->getMemOperand())) {
8683       if (VT.isVector())
8684         return SplitVectorStore(Op, DAG);
8685       return expandUnalignedStore(Store, DAG);
8686     }
8687 
8688     return SDValue();
8689   } else {
8690     llvm_unreachable("unhandled address space");
8691   }
8692 }
8693 
8694 SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
8695   SDLoc DL(Op);
8696   EVT VT = Op.getValueType();
8697   SDValue Arg = Op.getOperand(0);
8698   SDValue TrigVal;
8699 
8700   // Propagate fast-math flags so that the multiply we introduce can be folded
8701   // if Arg is already the result of a multiply by constant.
8702   auto Flags = Op->getFlags();
8703 
8704   SDValue OneOver2Pi = DAG.getConstantFP(0.5 * numbers::inv_pi, DL, VT);
8705 
8706   if (Subtarget->hasTrigReducedRange()) {
8707     SDValue MulVal = DAG.getNode(ISD::FMUL, DL, VT, Arg, OneOver2Pi, Flags);
8708     TrigVal = DAG.getNode(AMDGPUISD::FRACT, DL, VT, MulVal, Flags);
8709   } else {
8710     TrigVal = DAG.getNode(ISD::FMUL, DL, VT, Arg, OneOver2Pi, Flags);
8711   }
8712 
8713   switch (Op.getOpcode()) {
8714   case ISD::FCOS:
8715     return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, TrigVal, Flags);
8716   case ISD::FSIN:
8717     return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, TrigVal, Flags);
8718   default:
8719     llvm_unreachable("Wrong trig opcode");
8720   }
8721 }
8722 
8723 SDValue SITargetLowering::LowerATOMIC_CMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
8724   AtomicSDNode *AtomicNode = cast<AtomicSDNode>(Op);
8725   assert(AtomicNode->isCompareAndSwap());
8726   unsigned AS = AtomicNode->getAddressSpace();
8727 
8728   // No custom lowering required for local address space
8729   if (!AMDGPU::isFlatGlobalAddrSpace(AS))
8730     return Op;
8731 
8732   // Non-local address space requires custom lowering for atomic compare
8733   // and swap; cmp and swap should be in a v2i32 or v2i64 in case of _X2
8734   SDLoc DL(Op);
8735   SDValue ChainIn = Op.getOperand(0);
8736   SDValue Addr = Op.getOperand(1);
8737   SDValue Old = Op.getOperand(2);
8738   SDValue New = Op.getOperand(3);
8739   EVT VT = Op.getValueType();
8740   MVT SimpleVT = VT.getSimpleVT();
8741   MVT VecType = MVT::getVectorVT(SimpleVT, 2);
8742 
8743   SDValue NewOld = DAG.getBuildVector(VecType, DL, {New, Old});
8744   SDValue Ops[] = { ChainIn, Addr, NewOld };
8745 
8746   return DAG.getMemIntrinsicNode(AMDGPUISD::ATOMIC_CMP_SWAP, DL, Op->getVTList(),
8747                                  Ops, VT, AtomicNode->getMemOperand());
8748 }
8749 
8750 //===----------------------------------------------------------------------===//
8751 // Custom DAG optimizations
8752 //===----------------------------------------------------------------------===//
8753 
8754 SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
8755                                                      DAGCombinerInfo &DCI) const {
8756   EVT VT = N->getValueType(0);
8757   EVT ScalarVT = VT.getScalarType();
8758   if (ScalarVT != MVT::f32 && ScalarVT != MVT::f16)
8759     return SDValue();
8760 
8761   SelectionDAG &DAG = DCI.DAG;
8762   SDLoc DL(N);
8763 
8764   SDValue Src = N->getOperand(0);
8765   EVT SrcVT = Src.getValueType();
8766 
8767   // TODO: We could try to match extracting the higher bytes, which would be
8768   // easier if i8 vectors weren't promoted to i32 vectors, particularly after
8769   // types are legalized. v4i8 -> v4f32 is probably the only case to worry
8770   // about in practice.
8771   if (DCI.isAfterLegalizeDAG() && SrcVT == MVT::i32) {
8772     if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
8773       SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, MVT::f32, Src);
8774       DCI.AddToWorklist(Cvt.getNode());
8775 
8776       // For the f16 case, fold to a cast to f32 and then cast back to f16.
8777       if (ScalarVT != MVT::f32) {
8778         Cvt = DAG.getNode(ISD::FP_ROUND, DL, VT, Cvt,
8779                           DAG.getTargetConstant(0, DL, MVT::i32));
8780       }
8781       return Cvt;
8782     }
8783   }
8784 
8785   return SDValue();
8786 }
8787 
8788 // (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2)
8789 
8790 // This is a variant of
8791 // (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2),
8792 //
8793 // The normal DAG combiner will do this, but only if the add has one use since
8794 // that would increase the number of instructions.
8795 //
8796 // This prevents us from seeing a constant offset that can be folded into a
8797 // memory instruction's addressing mode. If we know the resulting add offset of
8798 // a pointer can be folded into an addressing offset, we can replace the pointer
8799 // operand with the add of new constant offset. This eliminates one of the uses,
8800 // and may allow the remaining use to also be simplified.
8801 //
8802 SDValue SITargetLowering::performSHLPtrCombine(SDNode *N,
8803                                                unsigned AddrSpace,
8804                                                EVT MemVT,
8805                                                DAGCombinerInfo &DCI) const {
8806   SDValue N0 = N->getOperand(0);
8807   SDValue N1 = N->getOperand(1);
8808 
8809   // We only do this to handle cases where it's profitable when there are
8810   // multiple uses of the add, so defer to the standard combine.
8811   if ((N0.getOpcode() != ISD::ADD && N0.getOpcode() != ISD::OR) ||
8812       N0->hasOneUse())
8813     return SDValue();
8814 
8815   const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1);
8816   if (!CN1)
8817     return SDValue();
8818 
8819   const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1));
8820   if (!CAdd)
8821     return SDValue();
8822 
8823   // If the resulting offset is too large, we can't fold it into the addressing
8824   // mode offset.
8825   APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue();
8826   Type *Ty = MemVT.getTypeForEVT(*DCI.DAG.getContext());
8827 
8828   AddrMode AM;
8829   AM.HasBaseReg = true;
8830   AM.BaseOffs = Offset.getSExtValue();
8831   if (!isLegalAddressingMode(DCI.DAG.getDataLayout(), AM, Ty, AddrSpace))
8832     return SDValue();
8833 
8834   SelectionDAG &DAG = DCI.DAG;
8835   SDLoc SL(N);
8836   EVT VT = N->getValueType(0);
8837 
8838   SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1);
8839   SDValue COffset = DAG.getConstant(Offset, SL, VT);
8840 
8841   SDNodeFlags Flags;
8842   Flags.setNoUnsignedWrap(N->getFlags().hasNoUnsignedWrap() &&
8843                           (N0.getOpcode() == ISD::OR ||
8844                            N0->getFlags().hasNoUnsignedWrap()));
8845 
8846   return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset, Flags);
8847 }
8848 
8849 /// MemSDNode::getBasePtr() does not work for intrinsics, which needs to offset
8850 /// by the chain and intrinsic ID. Theoretically we would also need to check the
8851 /// specific intrinsic, but they all place the pointer operand first.
8852 static unsigned getBasePtrIndex(const MemSDNode *N) {
8853   switch (N->getOpcode()) {
8854   case ISD::STORE:
8855   case ISD::INTRINSIC_W_CHAIN:
8856   case ISD::INTRINSIC_VOID:
8857     return 2;
8858   default:
8859     return 1;
8860   }
8861 }
8862 
8863 SDValue SITargetLowering::performMemSDNodeCombine(MemSDNode *N,
8864                                                   DAGCombinerInfo &DCI) const {
8865   SelectionDAG &DAG = DCI.DAG;
8866   SDLoc SL(N);
8867 
8868   unsigned PtrIdx = getBasePtrIndex(N);
8869   SDValue Ptr = N->getOperand(PtrIdx);
8870 
8871   // TODO: We could also do this for multiplies.
8872   if (Ptr.getOpcode() == ISD::SHL) {
8873     SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(),  N->getAddressSpace(),
8874                                           N->getMemoryVT(), DCI);
8875     if (NewPtr) {
8876       SmallVector<SDValue, 8> NewOps(N->op_begin(), N->op_end());
8877 
8878       NewOps[PtrIdx] = NewPtr;
8879       return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
8880     }
8881   }
8882 
8883   return SDValue();
8884 }
8885 
8886 static bool bitOpWithConstantIsReducible(unsigned Opc, uint32_t Val) {
8887   return (Opc == ISD::AND && (Val == 0 || Val == 0xffffffff)) ||
8888          (Opc == ISD::OR && (Val == 0xffffffff || Val == 0)) ||
8889          (Opc == ISD::XOR && Val == 0);
8890 }
8891 
8892 // Break up 64-bit bit operation of a constant into two 32-bit and/or/xor. This
8893 // will typically happen anyway for a VALU 64-bit and. This exposes other 32-bit
8894 // integer combine opportunities since most 64-bit operations are decomposed
8895 // this way.  TODO: We won't want this for SALU especially if it is an inline
8896 // immediate.
8897 SDValue SITargetLowering::splitBinaryBitConstantOp(
8898   DAGCombinerInfo &DCI,
8899   const SDLoc &SL,
8900   unsigned Opc, SDValue LHS,
8901   const ConstantSDNode *CRHS) const {
8902   uint64_t Val = CRHS->getZExtValue();
8903   uint32_t ValLo = Lo_32(Val);
8904   uint32_t ValHi = Hi_32(Val);
8905   const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
8906 
8907     if ((bitOpWithConstantIsReducible(Opc, ValLo) ||
8908          bitOpWithConstantIsReducible(Opc, ValHi)) ||
8909         (CRHS->hasOneUse() && !TII->isInlineConstant(CRHS->getAPIntValue()))) {
8910     // If we need to materialize a 64-bit immediate, it will be split up later
8911     // anyway. Avoid creating the harder to understand 64-bit immediate
8912     // materialization.
8913     return splitBinaryBitConstantOpImpl(DCI, SL, Opc, LHS, ValLo, ValHi);
8914   }
8915 
8916   return SDValue();
8917 }
8918 
8919 // Returns true if argument is a boolean value which is not serialized into
8920 // memory or argument and does not require v_cndmask_b32 to be deserialized.
8921 static bool isBoolSGPR(SDValue V) {
8922   if (V.getValueType() != MVT::i1)
8923     return false;
8924   switch (V.getOpcode()) {
8925   default:
8926     break;
8927   case ISD::SETCC:
8928   case AMDGPUISD::FP_CLASS:
8929     return true;
8930   case ISD::AND:
8931   case ISD::OR:
8932   case ISD::XOR:
8933     return isBoolSGPR(V.getOperand(0)) && isBoolSGPR(V.getOperand(1));
8934   }
8935   return false;
8936 }
8937 
8938 // If a constant has all zeroes or all ones within each byte return it.
8939 // Otherwise return 0.
8940 static uint32_t getConstantPermuteMask(uint32_t C) {
8941   // 0xff for any zero byte in the mask
8942   uint32_t ZeroByteMask = 0;
8943   if (!(C & 0x000000ff)) ZeroByteMask |= 0x000000ff;
8944   if (!(C & 0x0000ff00)) ZeroByteMask |= 0x0000ff00;
8945   if (!(C & 0x00ff0000)) ZeroByteMask |= 0x00ff0000;
8946   if (!(C & 0xff000000)) ZeroByteMask |= 0xff000000;
8947   uint32_t NonZeroByteMask = ~ZeroByteMask; // 0xff for any non-zero byte
8948   if ((NonZeroByteMask & C) != NonZeroByteMask)
8949     return 0; // Partial bytes selected.
8950   return C;
8951 }
8952 
8953 // Check if a node selects whole bytes from its operand 0 starting at a byte
8954 // boundary while masking the rest. Returns select mask as in the v_perm_b32
8955 // or -1 if not succeeded.
8956 // Note byte select encoding:
8957 // value 0-3 selects corresponding source byte;
8958 // value 0xc selects zero;
8959 // value 0xff selects 0xff.
8960 static uint32_t getPermuteMask(SelectionDAG &DAG, SDValue V) {
8961   assert(V.getValueSizeInBits() == 32);
8962 
8963   if (V.getNumOperands() != 2)
8964     return ~0;
8965 
8966   ConstantSDNode *N1 = dyn_cast<ConstantSDNode>(V.getOperand(1));
8967   if (!N1)
8968     return ~0;
8969 
8970   uint32_t C = N1->getZExtValue();
8971 
8972   switch (V.getOpcode()) {
8973   default:
8974     break;
8975   case ISD::AND:
8976     if (uint32_t ConstMask = getConstantPermuteMask(C)) {
8977       return (0x03020100 & ConstMask) | (0x0c0c0c0c & ~ConstMask);
8978     }
8979     break;
8980 
8981   case ISD::OR:
8982     if (uint32_t ConstMask = getConstantPermuteMask(C)) {
8983       return (0x03020100 & ~ConstMask) | ConstMask;
8984     }
8985     break;
8986 
8987   case ISD::SHL:
8988     if (C % 8)
8989       return ~0;
8990 
8991     return uint32_t((0x030201000c0c0c0cull << C) >> 32);
8992 
8993   case ISD::SRL:
8994     if (C % 8)
8995       return ~0;
8996 
8997     return uint32_t(0x0c0c0c0c03020100ull >> C);
8998   }
8999 
9000   return ~0;
9001 }
9002 
9003 SDValue SITargetLowering::performAndCombine(SDNode *N,
9004                                             DAGCombinerInfo &DCI) const {
9005   if (DCI.isBeforeLegalize())
9006     return SDValue();
9007 
9008   SelectionDAG &DAG = DCI.DAG;
9009   EVT VT = N->getValueType(0);
9010   SDValue LHS = N->getOperand(0);
9011   SDValue RHS = N->getOperand(1);
9012 
9013 
9014   const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS);
9015   if (VT == MVT::i64 && CRHS) {
9016     if (SDValue Split
9017         = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::AND, LHS, CRHS))
9018       return Split;
9019   }
9020 
9021   if (CRHS && VT == MVT::i32) {
9022     // and (srl x, c), mask => shl (bfe x, nb + c, mask >> nb), nb
9023     // nb = number of trailing zeroes in mask
9024     // It can be optimized out using SDWA for GFX8+ in the SDWA peephole pass,
9025     // given that we are selecting 8 or 16 bit fields starting at byte boundary.
9026     uint64_t Mask = CRHS->getZExtValue();
9027     unsigned Bits = countPopulation(Mask);
9028     if (getSubtarget()->hasSDWA() && LHS->getOpcode() == ISD::SRL &&
9029         (Bits == 8 || Bits == 16) && isShiftedMask_64(Mask) && !(Mask & 1)) {
9030       if (auto *CShift = dyn_cast<ConstantSDNode>(LHS->getOperand(1))) {
9031         unsigned Shift = CShift->getZExtValue();
9032         unsigned NB = CRHS->getAPIntValue().countTrailingZeros();
9033         unsigned Offset = NB + Shift;
9034         if ((Offset & (Bits - 1)) == 0) { // Starts at a byte or word boundary.
9035           SDLoc SL(N);
9036           SDValue BFE = DAG.getNode(AMDGPUISD::BFE_U32, SL, MVT::i32,
9037                                     LHS->getOperand(0),
9038                                     DAG.getConstant(Offset, SL, MVT::i32),
9039                                     DAG.getConstant(Bits, SL, MVT::i32));
9040           EVT NarrowVT = EVT::getIntegerVT(*DAG.getContext(), Bits);
9041           SDValue Ext = DAG.getNode(ISD::AssertZext, SL, VT, BFE,
9042                                     DAG.getValueType(NarrowVT));
9043           SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(LHS), VT, Ext,
9044                                     DAG.getConstant(NB, SDLoc(CRHS), MVT::i32));
9045           return Shl;
9046         }
9047       }
9048     }
9049 
9050     // and (perm x, y, c1), c2 -> perm x, y, permute_mask(c1, c2)
9051     if (LHS.hasOneUse() && LHS.getOpcode() == AMDGPUISD::PERM &&
9052         isa<ConstantSDNode>(LHS.getOperand(2))) {
9053       uint32_t Sel = getConstantPermuteMask(Mask);
9054       if (!Sel)
9055         return SDValue();
9056 
9057       // Select 0xc for all zero bytes
9058       Sel = (LHS.getConstantOperandVal(2) & Sel) | (~Sel & 0x0c0c0c0c);
9059       SDLoc DL(N);
9060       return DAG.getNode(AMDGPUISD::PERM, DL, MVT::i32, LHS.getOperand(0),
9061                          LHS.getOperand(1), DAG.getConstant(Sel, DL, MVT::i32));
9062     }
9063   }
9064 
9065   // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) ->
9066   // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity)
9067   if (LHS.getOpcode() == ISD::SETCC && RHS.getOpcode() == ISD::SETCC) {
9068     ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
9069     ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get();
9070 
9071     SDValue X = LHS.getOperand(0);
9072     SDValue Y = RHS.getOperand(0);
9073     if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X)
9074       return SDValue();
9075 
9076     if (LCC == ISD::SETO) {
9077       if (X != LHS.getOperand(1))
9078         return SDValue();
9079 
9080       if (RCC == ISD::SETUNE) {
9081         const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1));
9082         if (!C1 || !C1->isInfinity() || C1->isNegative())
9083           return SDValue();
9084 
9085         const uint32_t Mask = SIInstrFlags::N_NORMAL |
9086                               SIInstrFlags::N_SUBNORMAL |
9087                               SIInstrFlags::N_ZERO |
9088                               SIInstrFlags::P_ZERO |
9089                               SIInstrFlags::P_SUBNORMAL |
9090                               SIInstrFlags::P_NORMAL;
9091 
9092         static_assert(((~(SIInstrFlags::S_NAN |
9093                           SIInstrFlags::Q_NAN |
9094                           SIInstrFlags::N_INFINITY |
9095                           SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask,
9096                       "mask not equal");
9097 
9098         SDLoc DL(N);
9099         return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
9100                            X, DAG.getConstant(Mask, DL, MVT::i32));
9101       }
9102     }
9103   }
9104 
9105   if (RHS.getOpcode() == ISD::SETCC && LHS.getOpcode() == AMDGPUISD::FP_CLASS)
9106     std::swap(LHS, RHS);
9107 
9108   if (LHS.getOpcode() == ISD::SETCC && RHS.getOpcode() == AMDGPUISD::FP_CLASS &&
9109       RHS.hasOneUse()) {
9110     ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
9111     // and (fcmp seto), (fp_class x, mask) -> fp_class x, mask & ~(p_nan | n_nan)
9112     // and (fcmp setuo), (fp_class x, mask) -> fp_class x, mask & (p_nan | n_nan)
9113     const ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
9114     if ((LCC == ISD::SETO || LCC == ISD::SETUO) && Mask &&
9115         (RHS.getOperand(0) == LHS.getOperand(0) &&
9116          LHS.getOperand(0) == LHS.getOperand(1))) {
9117       const unsigned OrdMask = SIInstrFlags::S_NAN | SIInstrFlags::Q_NAN;
9118       unsigned NewMask = LCC == ISD::SETO ?
9119         Mask->getZExtValue() & ~OrdMask :
9120         Mask->getZExtValue() & OrdMask;
9121 
9122       SDLoc DL(N);
9123       return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1, RHS.getOperand(0),
9124                          DAG.getConstant(NewMask, DL, MVT::i32));
9125     }
9126   }
9127 
9128   if (VT == MVT::i32 &&
9129       (RHS.getOpcode() == ISD::SIGN_EXTEND || LHS.getOpcode() == ISD::SIGN_EXTEND)) {
9130     // and x, (sext cc from i1) => select cc, x, 0
9131     if (RHS.getOpcode() != ISD::SIGN_EXTEND)
9132       std::swap(LHS, RHS);
9133     if (isBoolSGPR(RHS.getOperand(0)))
9134       return DAG.getSelect(SDLoc(N), MVT::i32, RHS.getOperand(0),
9135                            LHS, DAG.getConstant(0, SDLoc(N), MVT::i32));
9136   }
9137 
9138   // and (op x, c1), (op y, c2) -> perm x, y, permute_mask(c1, c2)
9139   const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
9140   if (VT == MVT::i32 && LHS.hasOneUse() && RHS.hasOneUse() &&
9141       N->isDivergent() && TII->pseudoToMCOpcode(AMDGPU::V_PERM_B32_e64) != -1) {
9142     uint32_t LHSMask = getPermuteMask(DAG, LHS);
9143     uint32_t RHSMask = getPermuteMask(DAG, RHS);
9144     if (LHSMask != ~0u && RHSMask != ~0u) {
9145       // Canonicalize the expression in an attempt to have fewer unique masks
9146       // and therefore fewer registers used to hold the masks.
9147       if (LHSMask > RHSMask) {
9148         std::swap(LHSMask, RHSMask);
9149         std::swap(LHS, RHS);
9150       }
9151 
9152       // Select 0xc for each lane used from source operand. Zero has 0xc mask
9153       // set, 0xff have 0xff in the mask, actual lanes are in the 0-3 range.
9154       uint32_t LHSUsedLanes = ~(LHSMask & 0x0c0c0c0c) & 0x0c0c0c0c;
9155       uint32_t RHSUsedLanes = ~(RHSMask & 0x0c0c0c0c) & 0x0c0c0c0c;
9156 
9157       // Check of we need to combine values from two sources within a byte.
9158       if (!(LHSUsedLanes & RHSUsedLanes) &&
9159           // If we select high and lower word keep it for SDWA.
9160           // TODO: teach SDWA to work with v_perm_b32 and remove the check.
9161           !(LHSUsedLanes == 0x0c0c0000 && RHSUsedLanes == 0x00000c0c)) {
9162         // Each byte in each mask is either selector mask 0-3, or has higher
9163         // bits set in either of masks, which can be 0xff for 0xff or 0x0c for
9164         // zero. If 0x0c is in either mask it shall always be 0x0c. Otherwise
9165         // mask which is not 0xff wins. By anding both masks we have a correct
9166         // result except that 0x0c shall be corrected to give 0x0c only.
9167         uint32_t Mask = LHSMask & RHSMask;
9168         for (unsigned I = 0; I < 32; I += 8) {
9169           uint32_t ByteSel = 0xff << I;
9170           if ((LHSMask & ByteSel) == 0x0c || (RHSMask & ByteSel) == 0x0c)
9171             Mask &= (0x0c << I) & 0xffffffff;
9172         }
9173 
9174         // Add 4 to each active LHS lane. It will not affect any existing 0xff
9175         // or 0x0c.
9176         uint32_t Sel = Mask | (LHSUsedLanes & 0x04040404);
9177         SDLoc DL(N);
9178 
9179         return DAG.getNode(AMDGPUISD::PERM, DL, MVT::i32,
9180                            LHS.getOperand(0), RHS.getOperand(0),
9181                            DAG.getConstant(Sel, DL, MVT::i32));
9182       }
9183     }
9184   }
9185 
9186   return SDValue();
9187 }
9188 
9189 SDValue SITargetLowering::performOrCombine(SDNode *N,
9190                                            DAGCombinerInfo &DCI) const {
9191   SelectionDAG &DAG = DCI.DAG;
9192   SDValue LHS = N->getOperand(0);
9193   SDValue RHS = N->getOperand(1);
9194 
9195   EVT VT = N->getValueType(0);
9196   if (VT == MVT::i1) {
9197     // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2)
9198     if (LHS.getOpcode() == AMDGPUISD::FP_CLASS &&
9199         RHS.getOpcode() == AMDGPUISD::FP_CLASS) {
9200       SDValue Src = LHS.getOperand(0);
9201       if (Src != RHS.getOperand(0))
9202         return SDValue();
9203 
9204       const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
9205       const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
9206       if (!CLHS || !CRHS)
9207         return SDValue();
9208 
9209       // Only 10 bits are used.
9210       static const uint32_t MaxMask = 0x3ff;
9211 
9212       uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask;
9213       SDLoc DL(N);
9214       return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
9215                          Src, DAG.getConstant(NewMask, DL, MVT::i32));
9216     }
9217 
9218     return SDValue();
9219   }
9220 
9221   // or (perm x, y, c1), c2 -> perm x, y, permute_mask(c1, c2)
9222   if (isa<ConstantSDNode>(RHS) && LHS.hasOneUse() &&
9223       LHS.getOpcode() == AMDGPUISD::PERM &&
9224       isa<ConstantSDNode>(LHS.getOperand(2))) {
9225     uint32_t Sel = getConstantPermuteMask(N->getConstantOperandVal(1));
9226     if (!Sel)
9227       return SDValue();
9228 
9229     Sel |= LHS.getConstantOperandVal(2);
9230     SDLoc DL(N);
9231     return DAG.getNode(AMDGPUISD::PERM, DL, MVT::i32, LHS.getOperand(0),
9232                        LHS.getOperand(1), DAG.getConstant(Sel, DL, MVT::i32));
9233   }
9234 
9235   // or (op x, c1), (op y, c2) -> perm x, y, permute_mask(c1, c2)
9236   const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
9237   if (VT == MVT::i32 && LHS.hasOneUse() && RHS.hasOneUse() &&
9238       N->isDivergent() && TII->pseudoToMCOpcode(AMDGPU::V_PERM_B32_e64) != -1) {
9239     uint32_t LHSMask = getPermuteMask(DAG, LHS);
9240     uint32_t RHSMask = getPermuteMask(DAG, RHS);
9241     if (LHSMask != ~0u && RHSMask != ~0u) {
9242       // Canonicalize the expression in an attempt to have fewer unique masks
9243       // and therefore fewer registers used to hold the masks.
9244       if (LHSMask > RHSMask) {
9245         std::swap(LHSMask, RHSMask);
9246         std::swap(LHS, RHS);
9247       }
9248 
9249       // Select 0xc for each lane used from source operand. Zero has 0xc mask
9250       // set, 0xff have 0xff in the mask, actual lanes are in the 0-3 range.
9251       uint32_t LHSUsedLanes = ~(LHSMask & 0x0c0c0c0c) & 0x0c0c0c0c;
9252       uint32_t RHSUsedLanes = ~(RHSMask & 0x0c0c0c0c) & 0x0c0c0c0c;
9253 
9254       // Check of we need to combine values from two sources within a byte.
9255       if (!(LHSUsedLanes & RHSUsedLanes) &&
9256           // If we select high and lower word keep it for SDWA.
9257           // TODO: teach SDWA to work with v_perm_b32 and remove the check.
9258           !(LHSUsedLanes == 0x0c0c0000 && RHSUsedLanes == 0x00000c0c)) {
9259         // Kill zero bytes selected by other mask. Zero value is 0xc.
9260         LHSMask &= ~RHSUsedLanes;
9261         RHSMask &= ~LHSUsedLanes;
9262         // Add 4 to each active LHS lane
9263         LHSMask |= LHSUsedLanes & 0x04040404;
9264         // Combine masks
9265         uint32_t Sel = LHSMask | RHSMask;
9266         SDLoc DL(N);
9267 
9268         return DAG.getNode(AMDGPUISD::PERM, DL, MVT::i32,
9269                            LHS.getOperand(0), RHS.getOperand(0),
9270                            DAG.getConstant(Sel, DL, MVT::i32));
9271       }
9272     }
9273   }
9274 
9275   if (VT != MVT::i64 || DCI.isBeforeLegalizeOps())
9276     return SDValue();
9277 
9278   // TODO: This could be a generic combine with a predicate for extracting the
9279   // high half of an integer being free.
9280 
9281   // (or i64:x, (zero_extend i32:y)) ->
9282   //   i64 (bitcast (v2i32 build_vector (or i32:y, lo_32(x)), hi_32(x)))
9283   if (LHS.getOpcode() == ISD::ZERO_EXTEND &&
9284       RHS.getOpcode() != ISD::ZERO_EXTEND)
9285     std::swap(LHS, RHS);
9286 
9287   if (RHS.getOpcode() == ISD::ZERO_EXTEND) {
9288     SDValue ExtSrc = RHS.getOperand(0);
9289     EVT SrcVT = ExtSrc.getValueType();
9290     if (SrcVT == MVT::i32) {
9291       SDLoc SL(N);
9292       SDValue LowLHS, HiBits;
9293       std::tie(LowLHS, HiBits) = split64BitValue(LHS, DAG);
9294       SDValue LowOr = DAG.getNode(ISD::OR, SL, MVT::i32, LowLHS, ExtSrc);
9295 
9296       DCI.AddToWorklist(LowOr.getNode());
9297       DCI.AddToWorklist(HiBits.getNode());
9298 
9299       SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32,
9300                                 LowOr, HiBits);
9301       return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
9302     }
9303   }
9304 
9305   const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
9306   if (CRHS) {
9307     if (SDValue Split
9308           = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::OR, LHS, CRHS))
9309       return Split;
9310   }
9311 
9312   return SDValue();
9313 }
9314 
9315 SDValue SITargetLowering::performXorCombine(SDNode *N,
9316                                             DAGCombinerInfo &DCI) const {
9317   EVT VT = N->getValueType(0);
9318   if (VT != MVT::i64)
9319     return SDValue();
9320 
9321   SDValue LHS = N->getOperand(0);
9322   SDValue RHS = N->getOperand(1);
9323 
9324   const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS);
9325   if (CRHS) {
9326     if (SDValue Split
9327           = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::XOR, LHS, CRHS))
9328       return Split;
9329   }
9330 
9331   return SDValue();
9332 }
9333 
9334 // Instructions that will be lowered with a final instruction that zeros the
9335 // high result bits.
9336 // XXX - probably only need to list legal operations.
9337 static bool fp16SrcZerosHighBits(unsigned Opc) {
9338   switch (Opc) {
9339   case ISD::FADD:
9340   case ISD::FSUB:
9341   case ISD::FMUL:
9342   case ISD::FDIV:
9343   case ISD::FREM:
9344   case ISD::FMA:
9345   case ISD::FMAD:
9346   case ISD::FCANONICALIZE:
9347   case ISD::FP_ROUND:
9348   case ISD::UINT_TO_FP:
9349   case ISD::SINT_TO_FP:
9350   case ISD::FABS:
9351     // Fabs is lowered to a bit operation, but it's an and which will clear the
9352     // high bits anyway.
9353   case ISD::FSQRT:
9354   case ISD::FSIN:
9355   case ISD::FCOS:
9356   case ISD::FPOWI:
9357   case ISD::FPOW:
9358   case ISD::FLOG:
9359   case ISD::FLOG2:
9360   case ISD::FLOG10:
9361   case ISD::FEXP:
9362   case ISD::FEXP2:
9363   case ISD::FCEIL:
9364   case ISD::FTRUNC:
9365   case ISD::FRINT:
9366   case ISD::FNEARBYINT:
9367   case ISD::FROUND:
9368   case ISD::FFLOOR:
9369   case ISD::FMINNUM:
9370   case ISD::FMAXNUM:
9371   case AMDGPUISD::FRACT:
9372   case AMDGPUISD::CLAMP:
9373   case AMDGPUISD::COS_HW:
9374   case AMDGPUISD::SIN_HW:
9375   case AMDGPUISD::FMIN3:
9376   case AMDGPUISD::FMAX3:
9377   case AMDGPUISD::FMED3:
9378   case AMDGPUISD::FMAD_FTZ:
9379   case AMDGPUISD::RCP:
9380   case AMDGPUISD::RSQ:
9381   case AMDGPUISD::RCP_IFLAG:
9382   case AMDGPUISD::LDEXP:
9383     return true;
9384   default:
9385     // fcopysign, select and others may be lowered to 32-bit bit operations
9386     // which don't zero the high bits.
9387     return false;
9388   }
9389 }
9390 
9391 SDValue SITargetLowering::performZeroExtendCombine(SDNode *N,
9392                                                    DAGCombinerInfo &DCI) const {
9393   if (!Subtarget->has16BitInsts() ||
9394       DCI.getDAGCombineLevel() < AfterLegalizeDAG)
9395     return SDValue();
9396 
9397   EVT VT = N->getValueType(0);
9398   if (VT != MVT::i32)
9399     return SDValue();
9400 
9401   SDValue Src = N->getOperand(0);
9402   if (Src.getValueType() != MVT::i16)
9403     return SDValue();
9404 
9405   // (i32 zext (i16 (bitcast f16:$src))) -> fp16_zext $src
9406   // FIXME: It is not universally true that the high bits are zeroed on gfx9.
9407   if (Src.getOpcode() == ISD::BITCAST) {
9408     SDValue BCSrc = Src.getOperand(0);
9409     if (BCSrc.getValueType() == MVT::f16 &&
9410         fp16SrcZerosHighBits(BCSrc.getOpcode()))
9411       return DCI.DAG.getNode(AMDGPUISD::FP16_ZEXT, SDLoc(N), VT, BCSrc);
9412   }
9413 
9414   return SDValue();
9415 }
9416 
9417 SDValue SITargetLowering::performSignExtendInRegCombine(SDNode *N,
9418                                                         DAGCombinerInfo &DCI)
9419                                                         const {
9420   SDValue Src = N->getOperand(0);
9421   auto *VTSign = cast<VTSDNode>(N->getOperand(1));
9422 
9423   if (((Src.getOpcode() == AMDGPUISD::BUFFER_LOAD_UBYTE &&
9424       VTSign->getVT() == MVT::i8) ||
9425       (Src.getOpcode() == AMDGPUISD::BUFFER_LOAD_USHORT &&
9426       VTSign->getVT() == MVT::i16)) &&
9427       Src.hasOneUse()) {
9428     auto *M = cast<MemSDNode>(Src);
9429     SDValue Ops[] = {
9430       Src.getOperand(0), // Chain
9431       Src.getOperand(1), // rsrc
9432       Src.getOperand(2), // vindex
9433       Src.getOperand(3), // voffset
9434       Src.getOperand(4), // soffset
9435       Src.getOperand(5), // offset
9436       Src.getOperand(6),
9437       Src.getOperand(7)
9438     };
9439     // replace with BUFFER_LOAD_BYTE/SHORT
9440     SDVTList ResList = DCI.DAG.getVTList(MVT::i32,
9441                                          Src.getOperand(0).getValueType());
9442     unsigned Opc = (Src.getOpcode() == AMDGPUISD::BUFFER_LOAD_UBYTE) ?
9443                    AMDGPUISD::BUFFER_LOAD_BYTE : AMDGPUISD::BUFFER_LOAD_SHORT;
9444     SDValue BufferLoadSignExt = DCI.DAG.getMemIntrinsicNode(Opc, SDLoc(N),
9445                                                           ResList,
9446                                                           Ops, M->getMemoryVT(),
9447                                                           M->getMemOperand());
9448     return DCI.DAG.getMergeValues({BufferLoadSignExt,
9449                                   BufferLoadSignExt.getValue(1)}, SDLoc(N));
9450   }
9451   return SDValue();
9452 }
9453 
9454 SDValue SITargetLowering::performClassCombine(SDNode *N,
9455                                               DAGCombinerInfo &DCI) const {
9456   SelectionDAG &DAG = DCI.DAG;
9457   SDValue Mask = N->getOperand(1);
9458 
9459   // fp_class x, 0 -> false
9460   if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) {
9461     if (CMask->isNullValue())
9462       return DAG.getConstant(0, SDLoc(N), MVT::i1);
9463   }
9464 
9465   if (N->getOperand(0).isUndef())
9466     return DAG.getUNDEF(MVT::i1);
9467 
9468   return SDValue();
9469 }
9470 
9471 SDValue SITargetLowering::performRcpCombine(SDNode *N,
9472                                             DAGCombinerInfo &DCI) const {
9473   EVT VT = N->getValueType(0);
9474   SDValue N0 = N->getOperand(0);
9475 
9476   if (N0.isUndef())
9477     return N0;
9478 
9479   if (VT == MVT::f32 && (N0.getOpcode() == ISD::UINT_TO_FP ||
9480                          N0.getOpcode() == ISD::SINT_TO_FP)) {
9481     return DCI.DAG.getNode(AMDGPUISD::RCP_IFLAG, SDLoc(N), VT, N0,
9482                            N->getFlags());
9483   }
9484 
9485   if ((VT == MVT::f32 || VT == MVT::f16) && N0.getOpcode() == ISD::FSQRT) {
9486     return DCI.DAG.getNode(AMDGPUISD::RSQ, SDLoc(N), VT,
9487                            N0.getOperand(0), N->getFlags());
9488   }
9489 
9490   return AMDGPUTargetLowering::performRcpCombine(N, DCI);
9491 }
9492 
9493 bool SITargetLowering::isCanonicalized(SelectionDAG &DAG, SDValue Op,
9494                                        unsigned MaxDepth) const {
9495   unsigned Opcode = Op.getOpcode();
9496   if (Opcode == ISD::FCANONICALIZE)
9497     return true;
9498 
9499   if (auto *CFP = dyn_cast<ConstantFPSDNode>(Op)) {
9500     auto F = CFP->getValueAPF();
9501     if (F.isNaN() && F.isSignaling())
9502       return false;
9503     return !F.isDenormal() || denormalsEnabledForType(DAG, Op.getValueType());
9504   }
9505 
9506   // If source is a result of another standard FP operation it is already in
9507   // canonical form.
9508   if (MaxDepth == 0)
9509     return false;
9510 
9511   switch (Opcode) {
9512   // These will flush denorms if required.
9513   case ISD::FADD:
9514   case ISD::FSUB:
9515   case ISD::FMUL:
9516   case ISD::FCEIL:
9517   case ISD::FFLOOR:
9518   case ISD::FMA:
9519   case ISD::FMAD:
9520   case ISD::FSQRT:
9521   case ISD::FDIV:
9522   case ISD::FREM:
9523   case ISD::FP_ROUND:
9524   case ISD::FP_EXTEND:
9525   case AMDGPUISD::FMUL_LEGACY:
9526   case AMDGPUISD::FMAD_FTZ:
9527   case AMDGPUISD::RCP:
9528   case AMDGPUISD::RSQ:
9529   case AMDGPUISD::RSQ_CLAMP:
9530   case AMDGPUISD::RCP_LEGACY:
9531   case AMDGPUISD::RCP_IFLAG:
9532   case AMDGPUISD::DIV_SCALE:
9533   case AMDGPUISD::DIV_FMAS:
9534   case AMDGPUISD::DIV_FIXUP:
9535   case AMDGPUISD::FRACT:
9536   case AMDGPUISD::LDEXP:
9537   case AMDGPUISD::CVT_PKRTZ_F16_F32:
9538   case AMDGPUISD::CVT_F32_UBYTE0:
9539   case AMDGPUISD::CVT_F32_UBYTE1:
9540   case AMDGPUISD::CVT_F32_UBYTE2:
9541   case AMDGPUISD::CVT_F32_UBYTE3:
9542     return true;
9543 
9544   // It can/will be lowered or combined as a bit operation.
9545   // Need to check their input recursively to handle.
9546   case ISD::FNEG:
9547   case ISD::FABS:
9548   case ISD::FCOPYSIGN:
9549     return isCanonicalized(DAG, Op.getOperand(0), MaxDepth - 1);
9550 
9551   case ISD::FSIN:
9552   case ISD::FCOS:
9553   case ISD::FSINCOS:
9554     return Op.getValueType().getScalarType() != MVT::f16;
9555 
9556   case ISD::FMINNUM:
9557   case ISD::FMAXNUM:
9558   case ISD::FMINNUM_IEEE:
9559   case ISD::FMAXNUM_IEEE:
9560   case AMDGPUISD::CLAMP:
9561   case AMDGPUISD::FMED3:
9562   case AMDGPUISD::FMAX3:
9563   case AMDGPUISD::FMIN3: {
9564     // FIXME: Shouldn't treat the generic operations different based these.
9565     // However, we aren't really required to flush the result from
9566     // minnum/maxnum..
9567 
9568     // snans will be quieted, so we only need to worry about denormals.
9569     if (Subtarget->supportsMinMaxDenormModes() ||
9570         denormalsEnabledForType(DAG, Op.getValueType()))
9571       return true;
9572 
9573     // Flushing may be required.
9574     // In pre-GFX9 targets V_MIN_F32 and others do not flush denorms. For such
9575     // targets need to check their input recursively.
9576 
9577     // FIXME: Does this apply with clamp? It's implemented with max.
9578     for (unsigned I = 0, E = Op.getNumOperands(); I != E; ++I) {
9579       if (!isCanonicalized(DAG, Op.getOperand(I), MaxDepth - 1))
9580         return false;
9581     }
9582 
9583     return true;
9584   }
9585   case ISD::SELECT: {
9586     return isCanonicalized(DAG, Op.getOperand(1), MaxDepth - 1) &&
9587            isCanonicalized(DAG, Op.getOperand(2), MaxDepth - 1);
9588   }
9589   case ISD::BUILD_VECTOR: {
9590     for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
9591       SDValue SrcOp = Op.getOperand(i);
9592       if (!isCanonicalized(DAG, SrcOp, MaxDepth - 1))
9593         return false;
9594     }
9595 
9596     return true;
9597   }
9598   case ISD::EXTRACT_VECTOR_ELT:
9599   case ISD::EXTRACT_SUBVECTOR: {
9600     return isCanonicalized(DAG, Op.getOperand(0), MaxDepth - 1);
9601   }
9602   case ISD::INSERT_VECTOR_ELT: {
9603     return isCanonicalized(DAG, Op.getOperand(0), MaxDepth - 1) &&
9604            isCanonicalized(DAG, Op.getOperand(1), MaxDepth - 1);
9605   }
9606   case ISD::UNDEF:
9607     // Could be anything.
9608     return false;
9609 
9610   case ISD::BITCAST: {
9611     // Hack round the mess we make when legalizing extract_vector_elt
9612     SDValue Src = Op.getOperand(0);
9613     if (Src.getValueType() == MVT::i16 &&
9614         Src.getOpcode() == ISD::TRUNCATE) {
9615       SDValue TruncSrc = Src.getOperand(0);
9616       if (TruncSrc.getValueType() == MVT::i32 &&
9617           TruncSrc.getOpcode() == ISD::BITCAST &&
9618           TruncSrc.getOperand(0).getValueType() == MVT::v2f16) {
9619         return isCanonicalized(DAG, TruncSrc.getOperand(0), MaxDepth - 1);
9620       }
9621     }
9622 
9623     return false;
9624   }
9625   case ISD::INTRINSIC_WO_CHAIN: {
9626     unsigned IntrinsicID
9627       = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9628     // TODO: Handle more intrinsics
9629     switch (IntrinsicID) {
9630     case Intrinsic::amdgcn_cvt_pkrtz:
9631     case Intrinsic::amdgcn_cubeid:
9632     case Intrinsic::amdgcn_frexp_mant:
9633     case Intrinsic::amdgcn_fdot2:
9634     case Intrinsic::amdgcn_rcp:
9635     case Intrinsic::amdgcn_rsq:
9636     case Intrinsic::amdgcn_rsq_clamp:
9637     case Intrinsic::amdgcn_rcp_legacy:
9638     case Intrinsic::amdgcn_rsq_legacy:
9639     case Intrinsic::amdgcn_trig_preop:
9640       return true;
9641     default:
9642       break;
9643     }
9644 
9645     LLVM_FALLTHROUGH;
9646   }
9647   default:
9648     return denormalsEnabledForType(DAG, Op.getValueType()) &&
9649            DAG.isKnownNeverSNaN(Op);
9650   }
9651 
9652   llvm_unreachable("invalid operation");
9653 }
9654 
9655 bool SITargetLowering::isCanonicalized(Register Reg, MachineFunction &MF,
9656                                        unsigned MaxDepth) const {
9657   MachineRegisterInfo &MRI = MF.getRegInfo();
9658   MachineInstr *MI = MRI.getVRegDef(Reg);
9659   unsigned Opcode = MI->getOpcode();
9660 
9661   if (Opcode == AMDGPU::G_FCANONICALIZE)
9662     return true;
9663 
9664   if (Opcode == AMDGPU::G_FCONSTANT) {
9665     auto F = MI->getOperand(1).getFPImm()->getValueAPF();
9666     if (F.isNaN() && F.isSignaling())
9667       return false;
9668     return !F.isDenormal() || denormalsEnabledForType(MRI.getType(Reg), MF);
9669   }
9670 
9671   if (MaxDepth == 0)
9672     return false;
9673 
9674   switch (Opcode) {
9675   case AMDGPU::G_FMINNUM_IEEE:
9676   case AMDGPU::G_FMAXNUM_IEEE: {
9677     if (Subtarget->supportsMinMaxDenormModes() ||
9678         denormalsEnabledForType(MRI.getType(Reg), MF))
9679       return true;
9680     for (unsigned I = 1, E = MI->getNumOperands(); I != E; ++I) {
9681       if (!isCanonicalized(MI->getOperand(I).getReg(), MF, MaxDepth - 1))
9682         return false;
9683     }
9684     return true;
9685   }
9686   default:
9687     return denormalsEnabledForType(MRI.getType(Reg), MF) &&
9688            isKnownNeverSNaN(Reg, MRI);
9689   }
9690 
9691   llvm_unreachable("invalid operation");
9692 }
9693 
9694 // Constant fold canonicalize.
9695 SDValue SITargetLowering::getCanonicalConstantFP(
9696   SelectionDAG &DAG, const SDLoc &SL, EVT VT, const APFloat &C) const {
9697   // Flush denormals to 0 if not enabled.
9698   if (C.isDenormal() && !denormalsEnabledForType(DAG, VT))
9699     return DAG.getConstantFP(0.0, SL, VT);
9700 
9701   if (C.isNaN()) {
9702     APFloat CanonicalQNaN = APFloat::getQNaN(C.getSemantics());
9703     if (C.isSignaling()) {
9704       // Quiet a signaling NaN.
9705       // FIXME: Is this supposed to preserve payload bits?
9706       return DAG.getConstantFP(CanonicalQNaN, SL, VT);
9707     }
9708 
9709     // Make sure it is the canonical NaN bitpattern.
9710     //
9711     // TODO: Can we use -1 as the canonical NaN value since it's an inline
9712     // immediate?
9713     if (C.bitcastToAPInt() != CanonicalQNaN.bitcastToAPInt())
9714       return DAG.getConstantFP(CanonicalQNaN, SL, VT);
9715   }
9716 
9717   // Already canonical.
9718   return DAG.getConstantFP(C, SL, VT);
9719 }
9720 
9721 static bool vectorEltWillFoldAway(SDValue Op) {
9722   return Op.isUndef() || isa<ConstantFPSDNode>(Op);
9723 }
9724 
9725 SDValue SITargetLowering::performFCanonicalizeCombine(
9726   SDNode *N,
9727   DAGCombinerInfo &DCI) const {
9728   SelectionDAG &DAG = DCI.DAG;
9729   SDValue N0 = N->getOperand(0);
9730   EVT VT = N->getValueType(0);
9731 
9732   // fcanonicalize undef -> qnan
9733   if (N0.isUndef()) {
9734     APFloat QNaN = APFloat::getQNaN(SelectionDAG::EVTToAPFloatSemantics(VT));
9735     return DAG.getConstantFP(QNaN, SDLoc(N), VT);
9736   }
9737 
9738   if (ConstantFPSDNode *CFP = isConstOrConstSplatFP(N0)) {
9739     EVT VT = N->getValueType(0);
9740     return getCanonicalConstantFP(DAG, SDLoc(N), VT, CFP->getValueAPF());
9741   }
9742 
9743   // fcanonicalize (build_vector x, k) -> build_vector (fcanonicalize x),
9744   //                                                   (fcanonicalize k)
9745   //
9746   // fcanonicalize (build_vector x, undef) -> build_vector (fcanonicalize x), 0
9747 
9748   // TODO: This could be better with wider vectors that will be split to v2f16,
9749   // and to consider uses since there aren't that many packed operations.
9750   if (N0.getOpcode() == ISD::BUILD_VECTOR && VT == MVT::v2f16 &&
9751       isTypeLegal(MVT::v2f16)) {
9752     SDLoc SL(N);
9753     SDValue NewElts[2];
9754     SDValue Lo = N0.getOperand(0);
9755     SDValue Hi = N0.getOperand(1);
9756     EVT EltVT = Lo.getValueType();
9757 
9758     if (vectorEltWillFoldAway(Lo) || vectorEltWillFoldAway(Hi)) {
9759       for (unsigned I = 0; I != 2; ++I) {
9760         SDValue Op = N0.getOperand(I);
9761         if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op)) {
9762           NewElts[I] = getCanonicalConstantFP(DAG, SL, EltVT,
9763                                               CFP->getValueAPF());
9764         } else if (Op.isUndef()) {
9765           // Handled below based on what the other operand is.
9766           NewElts[I] = Op;
9767         } else {
9768           NewElts[I] = DAG.getNode(ISD::FCANONICALIZE, SL, EltVT, Op);
9769         }
9770       }
9771 
9772       // If one half is undef, and one is constant, perfer a splat vector rather
9773       // than the normal qNaN. If it's a register, prefer 0.0 since that's
9774       // cheaper to use and may be free with a packed operation.
9775       if (NewElts[0].isUndef()) {
9776         if (isa<ConstantFPSDNode>(NewElts[1]))
9777           NewElts[0] = isa<ConstantFPSDNode>(NewElts[1]) ?
9778             NewElts[1]: DAG.getConstantFP(0.0f, SL, EltVT);
9779       }
9780 
9781       if (NewElts[1].isUndef()) {
9782         NewElts[1] = isa<ConstantFPSDNode>(NewElts[0]) ?
9783           NewElts[0] : DAG.getConstantFP(0.0f, SL, EltVT);
9784       }
9785 
9786       return DAG.getBuildVector(VT, SL, NewElts);
9787     }
9788   }
9789 
9790   unsigned SrcOpc = N0.getOpcode();
9791 
9792   // If it's free to do so, push canonicalizes further up the source, which may
9793   // find a canonical source.
9794   //
9795   // TODO: More opcodes. Note this is unsafe for the the _ieee minnum/maxnum for
9796   // sNaNs.
9797   if (SrcOpc == ISD::FMINNUM || SrcOpc == ISD::FMAXNUM) {
9798     auto *CRHS = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
9799     if (CRHS && N0.hasOneUse()) {
9800       SDLoc SL(N);
9801       SDValue Canon0 = DAG.getNode(ISD::FCANONICALIZE, SL, VT,
9802                                    N0.getOperand(0));
9803       SDValue Canon1 = getCanonicalConstantFP(DAG, SL, VT, CRHS->getValueAPF());
9804       DCI.AddToWorklist(Canon0.getNode());
9805 
9806       return DAG.getNode(N0.getOpcode(), SL, VT, Canon0, Canon1);
9807     }
9808   }
9809 
9810   return isCanonicalized(DAG, N0) ? N0 : SDValue();
9811 }
9812 
9813 static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) {
9814   switch (Opc) {
9815   case ISD::FMAXNUM:
9816   case ISD::FMAXNUM_IEEE:
9817     return AMDGPUISD::FMAX3;
9818   case ISD::SMAX:
9819     return AMDGPUISD::SMAX3;
9820   case ISD::UMAX:
9821     return AMDGPUISD::UMAX3;
9822   case ISD::FMINNUM:
9823   case ISD::FMINNUM_IEEE:
9824     return AMDGPUISD::FMIN3;
9825   case ISD::SMIN:
9826     return AMDGPUISD::SMIN3;
9827   case ISD::UMIN:
9828     return AMDGPUISD::UMIN3;
9829   default:
9830     llvm_unreachable("Not a min/max opcode");
9831   }
9832 }
9833 
9834 SDValue SITargetLowering::performIntMed3ImmCombine(
9835   SelectionDAG &DAG, const SDLoc &SL,
9836   SDValue Op0, SDValue Op1, bool Signed) const {
9837   ConstantSDNode *K1 = dyn_cast<ConstantSDNode>(Op1);
9838   if (!K1)
9839     return SDValue();
9840 
9841   ConstantSDNode *K0 = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
9842   if (!K0)
9843     return SDValue();
9844 
9845   if (Signed) {
9846     if (K0->getAPIntValue().sge(K1->getAPIntValue()))
9847       return SDValue();
9848   } else {
9849     if (K0->getAPIntValue().uge(K1->getAPIntValue()))
9850       return SDValue();
9851   }
9852 
9853   EVT VT = K0->getValueType(0);
9854   unsigned Med3Opc = Signed ? AMDGPUISD::SMED3 : AMDGPUISD::UMED3;
9855   if (VT == MVT::i32 || (VT == MVT::i16 && Subtarget->hasMed3_16())) {
9856     return DAG.getNode(Med3Opc, SL, VT,
9857                        Op0.getOperand(0), SDValue(K0, 0), SDValue(K1, 0));
9858   }
9859 
9860   // If there isn't a 16-bit med3 operation, convert to 32-bit.
9861   if (VT == MVT::i16) {
9862     MVT NVT = MVT::i32;
9863     unsigned ExtOp = Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
9864 
9865     SDValue Tmp1 = DAG.getNode(ExtOp, SL, NVT, Op0->getOperand(0));
9866     SDValue Tmp2 = DAG.getNode(ExtOp, SL, NVT, Op0->getOperand(1));
9867     SDValue Tmp3 = DAG.getNode(ExtOp, SL, NVT, Op1);
9868 
9869     SDValue Med3 = DAG.getNode(Med3Opc, SL, NVT, Tmp1, Tmp2, Tmp3);
9870     return DAG.getNode(ISD::TRUNCATE, SL, VT, Med3);
9871   }
9872 
9873   return SDValue();
9874 }
9875 
9876 static ConstantFPSDNode *getSplatConstantFP(SDValue Op) {
9877   if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
9878     return C;
9879 
9880   if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(Op)) {
9881     if (ConstantFPSDNode *C = BV->getConstantFPSplatNode())
9882       return C;
9883   }
9884 
9885   return nullptr;
9886 }
9887 
9888 SDValue SITargetLowering::performFPMed3ImmCombine(SelectionDAG &DAG,
9889                                                   const SDLoc &SL,
9890                                                   SDValue Op0,
9891                                                   SDValue Op1) const {
9892   ConstantFPSDNode *K1 = getSplatConstantFP(Op1);
9893   if (!K1)
9894     return SDValue();
9895 
9896   ConstantFPSDNode *K0 = getSplatConstantFP(Op0.getOperand(1));
9897   if (!K0)
9898     return SDValue();
9899 
9900   // Ordered >= (although NaN inputs should have folded away by now).
9901   if (K0->getValueAPF() > K1->getValueAPF())
9902     return SDValue();
9903 
9904   const MachineFunction &MF = DAG.getMachineFunction();
9905   const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
9906 
9907   // TODO: Check IEEE bit enabled?
9908   EVT VT = Op0.getValueType();
9909   if (Info->getMode().DX10Clamp) {
9910     // If dx10_clamp is enabled, NaNs clamp to 0.0. This is the same as the
9911     // hardware fmed3 behavior converting to a min.
9912     // FIXME: Should this be allowing -0.0?
9913     if (K1->isExactlyValue(1.0) && K0->isExactlyValue(0.0))
9914       return DAG.getNode(AMDGPUISD::CLAMP, SL, VT, Op0.getOperand(0));
9915   }
9916 
9917   // med3 for f16 is only available on gfx9+, and not available for v2f16.
9918   if (VT == MVT::f32 || (VT == MVT::f16 && Subtarget->hasMed3_16())) {
9919     // This isn't safe with signaling NaNs because in IEEE mode, min/max on a
9920     // signaling NaN gives a quiet NaN. The quiet NaN input to the min would
9921     // then give the other result, which is different from med3 with a NaN
9922     // input.
9923     SDValue Var = Op0.getOperand(0);
9924     if (!DAG.isKnownNeverSNaN(Var))
9925       return SDValue();
9926 
9927     const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
9928 
9929     if ((!K0->hasOneUse() ||
9930          TII->isInlineConstant(K0->getValueAPF().bitcastToAPInt())) &&
9931         (!K1->hasOneUse() ||
9932          TII->isInlineConstant(K1->getValueAPF().bitcastToAPInt()))) {
9933       return DAG.getNode(AMDGPUISD::FMED3, SL, K0->getValueType(0),
9934                          Var, SDValue(K0, 0), SDValue(K1, 0));
9935     }
9936   }
9937 
9938   return SDValue();
9939 }
9940 
9941 SDValue SITargetLowering::performMinMaxCombine(SDNode *N,
9942                                                DAGCombinerInfo &DCI) const {
9943   SelectionDAG &DAG = DCI.DAG;
9944 
9945   EVT VT = N->getValueType(0);
9946   unsigned Opc = N->getOpcode();
9947   SDValue Op0 = N->getOperand(0);
9948   SDValue Op1 = N->getOperand(1);
9949 
9950   // Only do this if the inner op has one use since this will just increases
9951   // register pressure for no benefit.
9952 
9953   if (Opc != AMDGPUISD::FMIN_LEGACY && Opc != AMDGPUISD::FMAX_LEGACY &&
9954       !VT.isVector() &&
9955       (VT == MVT::i32 || VT == MVT::f32 ||
9956        ((VT == MVT::f16 || VT == MVT::i16) && Subtarget->hasMin3Max3_16()))) {
9957     // max(max(a, b), c) -> max3(a, b, c)
9958     // min(min(a, b), c) -> min3(a, b, c)
9959     if (Op0.getOpcode() == Opc && Op0.hasOneUse()) {
9960       SDLoc DL(N);
9961       return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
9962                          DL,
9963                          N->getValueType(0),
9964                          Op0.getOperand(0),
9965                          Op0.getOperand(1),
9966                          Op1);
9967     }
9968 
9969     // Try commuted.
9970     // max(a, max(b, c)) -> max3(a, b, c)
9971     // min(a, min(b, c)) -> min3(a, b, c)
9972     if (Op1.getOpcode() == Opc && Op1.hasOneUse()) {
9973       SDLoc DL(N);
9974       return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
9975                          DL,
9976                          N->getValueType(0),
9977                          Op0,
9978                          Op1.getOperand(0),
9979                          Op1.getOperand(1));
9980     }
9981   }
9982 
9983   // min(max(x, K0), K1), K0 < K1 -> med3(x, K0, K1)
9984   if (Opc == ISD::SMIN && Op0.getOpcode() == ISD::SMAX && Op0.hasOneUse()) {
9985     if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, true))
9986       return Med3;
9987   }
9988 
9989   if (Opc == ISD::UMIN && Op0.getOpcode() == ISD::UMAX && Op0.hasOneUse()) {
9990     if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, false))
9991       return Med3;
9992   }
9993 
9994   // fminnum(fmaxnum(x, K0), K1), K0 < K1 && !is_snan(x) -> fmed3(x, K0, K1)
9995   if (((Opc == ISD::FMINNUM && Op0.getOpcode() == ISD::FMAXNUM) ||
9996        (Opc == ISD::FMINNUM_IEEE && Op0.getOpcode() == ISD::FMAXNUM_IEEE) ||
9997        (Opc == AMDGPUISD::FMIN_LEGACY &&
9998         Op0.getOpcode() == AMDGPUISD::FMAX_LEGACY)) &&
9999       (VT == MVT::f32 || VT == MVT::f64 ||
10000        (VT == MVT::f16 && Subtarget->has16BitInsts()) ||
10001        (VT == MVT::v2f16 && Subtarget->hasVOP3PInsts())) &&
10002       Op0.hasOneUse()) {
10003     if (SDValue Res = performFPMed3ImmCombine(DAG, SDLoc(N), Op0, Op1))
10004       return Res;
10005   }
10006 
10007   return SDValue();
10008 }
10009 
10010 static bool isClampZeroToOne(SDValue A, SDValue B) {
10011   if (ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A)) {
10012     if (ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B)) {
10013       // FIXME: Should this be allowing -0.0?
10014       return (CA->isExactlyValue(0.0) && CB->isExactlyValue(1.0)) ||
10015              (CA->isExactlyValue(1.0) && CB->isExactlyValue(0.0));
10016     }
10017   }
10018 
10019   return false;
10020 }
10021 
10022 // FIXME: Should only worry about snans for version with chain.
10023 SDValue SITargetLowering::performFMed3Combine(SDNode *N,
10024                                               DAGCombinerInfo &DCI) const {
10025   EVT VT = N->getValueType(0);
10026   // v_med3_f32 and v_max_f32 behave identically wrt denorms, exceptions and
10027   // NaNs. With a NaN input, the order of the operands may change the result.
10028 
10029   SelectionDAG &DAG = DCI.DAG;
10030   SDLoc SL(N);
10031 
10032   SDValue Src0 = N->getOperand(0);
10033   SDValue Src1 = N->getOperand(1);
10034   SDValue Src2 = N->getOperand(2);
10035 
10036   if (isClampZeroToOne(Src0, Src1)) {
10037     // const_a, const_b, x -> clamp is safe in all cases including signaling
10038     // nans.
10039     // FIXME: Should this be allowing -0.0?
10040     return DAG.getNode(AMDGPUISD::CLAMP, SL, VT, Src2);
10041   }
10042 
10043   const MachineFunction &MF = DAG.getMachineFunction();
10044   const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
10045 
10046   // FIXME: dx10_clamp behavior assumed in instcombine. Should we really bother
10047   // handling no dx10-clamp?
10048   if (Info->getMode().DX10Clamp) {
10049     // If NaNs is clamped to 0, we are free to reorder the inputs.
10050 
10051     if (isa<ConstantFPSDNode>(Src0) && !isa<ConstantFPSDNode>(Src1))
10052       std::swap(Src0, Src1);
10053 
10054     if (isa<ConstantFPSDNode>(Src1) && !isa<ConstantFPSDNode>(Src2))
10055       std::swap(Src1, Src2);
10056 
10057     if (isa<ConstantFPSDNode>(Src0) && !isa<ConstantFPSDNode>(Src1))
10058       std::swap(Src0, Src1);
10059 
10060     if (isClampZeroToOne(Src1, Src2))
10061       return DAG.getNode(AMDGPUISD::CLAMP, SL, VT, Src0);
10062   }
10063 
10064   return SDValue();
10065 }
10066 
10067 SDValue SITargetLowering::performCvtPkRTZCombine(SDNode *N,
10068                                                  DAGCombinerInfo &DCI) const {
10069   SDValue Src0 = N->getOperand(0);
10070   SDValue Src1 = N->getOperand(1);
10071   if (Src0.isUndef() && Src1.isUndef())
10072     return DCI.DAG.getUNDEF(N->getValueType(0));
10073   return SDValue();
10074 }
10075 
10076 // Check if EXTRACT_VECTOR_ELT/INSERT_VECTOR_ELT (<n x e>, var-idx) should be
10077 // expanded into a set of cmp/select instructions.
10078 bool SITargetLowering::shouldExpandVectorDynExt(unsigned EltSize,
10079                                                 unsigned NumElem,
10080                                                 bool IsDivergentIdx) {
10081   if (UseDivergentRegisterIndexing)
10082     return false;
10083 
10084   unsigned VecSize = EltSize * NumElem;
10085 
10086   // Sub-dword vectors of size 2 dword or less have better implementation.
10087   if (VecSize <= 64 && EltSize < 32)
10088     return false;
10089 
10090   // Always expand the rest of sub-dword instructions, otherwise it will be
10091   // lowered via memory.
10092   if (EltSize < 32)
10093     return true;
10094 
10095   // Always do this if var-idx is divergent, otherwise it will become a loop.
10096   if (IsDivergentIdx)
10097     return true;
10098 
10099   // Large vectors would yield too many compares and v_cndmask_b32 instructions.
10100   unsigned NumInsts = NumElem /* Number of compares */ +
10101                       ((EltSize + 31) / 32) * NumElem /* Number of cndmasks */;
10102   return NumInsts <= 16;
10103 }
10104 
10105 static bool shouldExpandVectorDynExt(SDNode *N) {
10106   SDValue Idx = N->getOperand(N->getNumOperands() - 1);
10107   if (isa<ConstantSDNode>(Idx))
10108     return false;
10109 
10110   SDValue Vec = N->getOperand(0);
10111   EVT VecVT = Vec.getValueType();
10112   EVT EltVT = VecVT.getVectorElementType();
10113   unsigned EltSize = EltVT.getSizeInBits();
10114   unsigned NumElem = VecVT.getVectorNumElements();
10115 
10116   return SITargetLowering::shouldExpandVectorDynExt(EltSize, NumElem,
10117                                                     Idx->isDivergent());
10118 }
10119 
10120 SDValue SITargetLowering::performExtractVectorEltCombine(
10121   SDNode *N, DAGCombinerInfo &DCI) const {
10122   SDValue Vec = N->getOperand(0);
10123   SelectionDAG &DAG = DCI.DAG;
10124 
10125   EVT VecVT = Vec.getValueType();
10126   EVT EltVT = VecVT.getVectorElementType();
10127 
10128   if ((Vec.getOpcode() == ISD::FNEG ||
10129        Vec.getOpcode() == ISD::FABS) && allUsesHaveSourceMods(N)) {
10130     SDLoc SL(N);
10131     EVT EltVT = N->getValueType(0);
10132     SDValue Idx = N->getOperand(1);
10133     SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
10134                               Vec.getOperand(0), Idx);
10135     return DAG.getNode(Vec.getOpcode(), SL, EltVT, Elt);
10136   }
10137 
10138   // ScalarRes = EXTRACT_VECTOR_ELT ((vector-BINOP Vec1, Vec2), Idx)
10139   //    =>
10140   // Vec1Elt = EXTRACT_VECTOR_ELT(Vec1, Idx)
10141   // Vec2Elt = EXTRACT_VECTOR_ELT(Vec2, Idx)
10142   // ScalarRes = scalar-BINOP Vec1Elt, Vec2Elt
10143   if (Vec.hasOneUse() && DCI.isBeforeLegalize()) {
10144     SDLoc SL(N);
10145     EVT EltVT = N->getValueType(0);
10146     SDValue Idx = N->getOperand(1);
10147     unsigned Opc = Vec.getOpcode();
10148 
10149     switch(Opc) {
10150     default:
10151       break;
10152       // TODO: Support other binary operations.
10153     case ISD::FADD:
10154     case ISD::FSUB:
10155     case ISD::FMUL:
10156     case ISD::ADD:
10157     case ISD::UMIN:
10158     case ISD::UMAX:
10159     case ISD::SMIN:
10160     case ISD::SMAX:
10161     case ISD::FMAXNUM:
10162     case ISD::FMINNUM:
10163     case ISD::FMAXNUM_IEEE:
10164     case ISD::FMINNUM_IEEE: {
10165       SDValue Elt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
10166                                  Vec.getOperand(0), Idx);
10167       SDValue Elt1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
10168                                  Vec.getOperand(1), Idx);
10169 
10170       DCI.AddToWorklist(Elt0.getNode());
10171       DCI.AddToWorklist(Elt1.getNode());
10172       return DAG.getNode(Opc, SL, EltVT, Elt0, Elt1, Vec->getFlags());
10173     }
10174     }
10175   }
10176 
10177   unsigned VecSize = VecVT.getSizeInBits();
10178   unsigned EltSize = EltVT.getSizeInBits();
10179 
10180   // EXTRACT_VECTOR_ELT (<n x e>, var-idx) => n x select (e, const-idx)
10181   if (::shouldExpandVectorDynExt(N)) {
10182     SDLoc SL(N);
10183     SDValue Idx = N->getOperand(1);
10184     SDValue V;
10185     for (unsigned I = 0, E = VecVT.getVectorNumElements(); I < E; ++I) {
10186       SDValue IC = DAG.getVectorIdxConstant(I, SL);
10187       SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, Vec, IC);
10188       if (I == 0)
10189         V = Elt;
10190       else
10191         V = DAG.getSelectCC(SL, Idx, IC, Elt, V, ISD::SETEQ);
10192     }
10193     return V;
10194   }
10195 
10196   if (!DCI.isBeforeLegalize())
10197     return SDValue();
10198 
10199   // Try to turn sub-dword accesses of vectors into accesses of the same 32-bit
10200   // elements. This exposes more load reduction opportunities by replacing
10201   // multiple small extract_vector_elements with a single 32-bit extract.
10202   auto *Idx = dyn_cast<ConstantSDNode>(N->getOperand(1));
10203   if (isa<MemSDNode>(Vec) &&
10204       EltSize <= 16 &&
10205       EltVT.isByteSized() &&
10206       VecSize > 32 &&
10207       VecSize % 32 == 0 &&
10208       Idx) {
10209     EVT NewVT = getEquivalentMemType(*DAG.getContext(), VecVT);
10210 
10211     unsigned BitIndex = Idx->getZExtValue() * EltSize;
10212     unsigned EltIdx = BitIndex / 32;
10213     unsigned LeftoverBitIdx = BitIndex % 32;
10214     SDLoc SL(N);
10215 
10216     SDValue Cast = DAG.getNode(ISD::BITCAST, SL, NewVT, Vec);
10217     DCI.AddToWorklist(Cast.getNode());
10218 
10219     SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Cast,
10220                               DAG.getConstant(EltIdx, SL, MVT::i32));
10221     DCI.AddToWorklist(Elt.getNode());
10222     SDValue Srl = DAG.getNode(ISD::SRL, SL, MVT::i32, Elt,
10223                               DAG.getConstant(LeftoverBitIdx, SL, MVT::i32));
10224     DCI.AddToWorklist(Srl.getNode());
10225 
10226     SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SL, EltVT.changeTypeToInteger(), Srl);
10227     DCI.AddToWorklist(Trunc.getNode());
10228     return DAG.getNode(ISD::BITCAST, SL, EltVT, Trunc);
10229   }
10230 
10231   return SDValue();
10232 }
10233 
10234 SDValue
10235 SITargetLowering::performInsertVectorEltCombine(SDNode *N,
10236                                                 DAGCombinerInfo &DCI) const {
10237   SDValue Vec = N->getOperand(0);
10238   SDValue Idx = N->getOperand(2);
10239   EVT VecVT = Vec.getValueType();
10240   EVT EltVT = VecVT.getVectorElementType();
10241 
10242   // INSERT_VECTOR_ELT (<n x e>, var-idx)
10243   // => BUILD_VECTOR n x select (e, const-idx)
10244   if (!::shouldExpandVectorDynExt(N))
10245     return SDValue();
10246 
10247   SelectionDAG &DAG = DCI.DAG;
10248   SDLoc SL(N);
10249   SDValue Ins = N->getOperand(1);
10250   EVT IdxVT = Idx.getValueType();
10251 
10252   SmallVector<SDValue, 16> Ops;
10253   for (unsigned I = 0, E = VecVT.getVectorNumElements(); I < E; ++I) {
10254     SDValue IC = DAG.getConstant(I, SL, IdxVT);
10255     SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, Vec, IC);
10256     SDValue V = DAG.getSelectCC(SL, Idx, IC, Ins, Elt, ISD::SETEQ);
10257     Ops.push_back(V);
10258   }
10259 
10260   return DAG.getBuildVector(VecVT, SL, Ops);
10261 }
10262 
10263 unsigned SITargetLowering::getFusedOpcode(const SelectionDAG &DAG,
10264                                           const SDNode *N0,
10265                                           const SDNode *N1) const {
10266   EVT VT = N0->getValueType(0);
10267 
10268   // Only do this if we are not trying to support denormals. v_mad_f32 does not
10269   // support denormals ever.
10270   if (((VT == MVT::f32 && !hasFP32Denormals(DAG.getMachineFunction())) ||
10271        (VT == MVT::f16 && !hasFP64FP16Denormals(DAG.getMachineFunction()) &&
10272         getSubtarget()->hasMadF16())) &&
10273        isOperationLegal(ISD::FMAD, VT))
10274     return ISD::FMAD;
10275 
10276   const TargetOptions &Options = DAG.getTarget().Options;
10277   if ((Options.AllowFPOpFusion == FPOpFusion::Fast || Options.UnsafeFPMath ||
10278        (N0->getFlags().hasAllowContract() &&
10279         N1->getFlags().hasAllowContract())) &&
10280       isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), VT)) {
10281     return ISD::FMA;
10282   }
10283 
10284   return 0;
10285 }
10286 
10287 // For a reassociatable opcode perform:
10288 // op x, (op y, z) -> op (op x, z), y, if x and z are uniform
10289 SDValue SITargetLowering::reassociateScalarOps(SDNode *N,
10290                                                SelectionDAG &DAG) const {
10291   EVT VT = N->getValueType(0);
10292   if (VT != MVT::i32 && VT != MVT::i64)
10293     return SDValue();
10294 
10295   unsigned Opc = N->getOpcode();
10296   SDValue Op0 = N->getOperand(0);
10297   SDValue Op1 = N->getOperand(1);
10298 
10299   if (!(Op0->isDivergent() ^ Op1->isDivergent()))
10300     return SDValue();
10301 
10302   if (Op0->isDivergent())
10303     std::swap(Op0, Op1);
10304 
10305   if (Op1.getOpcode() != Opc || !Op1.hasOneUse())
10306     return SDValue();
10307 
10308   SDValue Op2 = Op1.getOperand(1);
10309   Op1 = Op1.getOperand(0);
10310   if (!(Op1->isDivergent() ^ Op2->isDivergent()))
10311     return SDValue();
10312 
10313   if (Op1->isDivergent())
10314     std::swap(Op1, Op2);
10315 
10316   // If either operand is constant this will conflict with
10317   // DAGCombiner::ReassociateOps().
10318   if (DAG.isConstantIntBuildVectorOrConstantInt(Op0) ||
10319       DAG.isConstantIntBuildVectorOrConstantInt(Op1))
10320     return SDValue();
10321 
10322   SDLoc SL(N);
10323   SDValue Add1 = DAG.getNode(Opc, SL, VT, Op0, Op1);
10324   return DAG.getNode(Opc, SL, VT, Add1, Op2);
10325 }
10326 
10327 static SDValue getMad64_32(SelectionDAG &DAG, const SDLoc &SL,
10328                            EVT VT,
10329                            SDValue N0, SDValue N1, SDValue N2,
10330                            bool Signed) {
10331   unsigned MadOpc = Signed ? AMDGPUISD::MAD_I64_I32 : AMDGPUISD::MAD_U64_U32;
10332   SDVTList VTs = DAG.getVTList(MVT::i64, MVT::i1);
10333   SDValue Mad = DAG.getNode(MadOpc, SL, VTs, N0, N1, N2);
10334   return DAG.getNode(ISD::TRUNCATE, SL, VT, Mad);
10335 }
10336 
10337 SDValue SITargetLowering::performAddCombine(SDNode *N,
10338                                             DAGCombinerInfo &DCI) const {
10339   SelectionDAG &DAG = DCI.DAG;
10340   EVT VT = N->getValueType(0);
10341   SDLoc SL(N);
10342   SDValue LHS = N->getOperand(0);
10343   SDValue RHS = N->getOperand(1);
10344 
10345   if ((LHS.getOpcode() == ISD::MUL || RHS.getOpcode() == ISD::MUL)
10346       && Subtarget->hasMad64_32() &&
10347       !VT.isVector() && VT.getScalarSizeInBits() > 32 &&
10348       VT.getScalarSizeInBits() <= 64) {
10349     if (LHS.getOpcode() != ISD::MUL)
10350       std::swap(LHS, RHS);
10351 
10352     SDValue MulLHS = LHS.getOperand(0);
10353     SDValue MulRHS = LHS.getOperand(1);
10354     SDValue AddRHS = RHS;
10355 
10356     // TODO: Maybe restrict if SGPR inputs.
10357     if (numBitsUnsigned(MulLHS, DAG) <= 32 &&
10358         numBitsUnsigned(MulRHS, DAG) <= 32) {
10359       MulLHS = DAG.getZExtOrTrunc(MulLHS, SL, MVT::i32);
10360       MulRHS = DAG.getZExtOrTrunc(MulRHS, SL, MVT::i32);
10361       AddRHS = DAG.getZExtOrTrunc(AddRHS, SL, MVT::i64);
10362       return getMad64_32(DAG, SL, VT, MulLHS, MulRHS, AddRHS, false);
10363     }
10364 
10365     if (numBitsSigned(MulLHS, DAG) < 32 && numBitsSigned(MulRHS, DAG) < 32) {
10366       MulLHS = DAG.getSExtOrTrunc(MulLHS, SL, MVT::i32);
10367       MulRHS = DAG.getSExtOrTrunc(MulRHS, SL, MVT::i32);
10368       AddRHS = DAG.getSExtOrTrunc(AddRHS, SL, MVT::i64);
10369       return getMad64_32(DAG, SL, VT, MulLHS, MulRHS, AddRHS, true);
10370     }
10371 
10372     return SDValue();
10373   }
10374 
10375   if (SDValue V = reassociateScalarOps(N, DAG)) {
10376     return V;
10377   }
10378 
10379   if (VT != MVT::i32 || !DCI.isAfterLegalizeDAG())
10380     return SDValue();
10381 
10382   // add x, zext (setcc) => addcarry x, 0, setcc
10383   // add x, sext (setcc) => subcarry x, 0, setcc
10384   unsigned Opc = LHS.getOpcode();
10385   if (Opc == ISD::ZERO_EXTEND || Opc == ISD::SIGN_EXTEND ||
10386       Opc == ISD::ANY_EXTEND || Opc == ISD::ADDCARRY)
10387     std::swap(RHS, LHS);
10388 
10389   Opc = RHS.getOpcode();
10390   switch (Opc) {
10391   default: break;
10392   case ISD::ZERO_EXTEND:
10393   case ISD::SIGN_EXTEND:
10394   case ISD::ANY_EXTEND: {
10395     auto Cond = RHS.getOperand(0);
10396     // If this won't be a real VOPC output, we would still need to insert an
10397     // extra instruction anyway.
10398     if (!isBoolSGPR(Cond))
10399       break;
10400     SDVTList VTList = DAG.getVTList(MVT::i32, MVT::i1);
10401     SDValue Args[] = { LHS, DAG.getConstant(0, SL, MVT::i32), Cond };
10402     Opc = (Opc == ISD::SIGN_EXTEND) ? ISD::SUBCARRY : ISD::ADDCARRY;
10403     return DAG.getNode(Opc, SL, VTList, Args);
10404   }
10405   case ISD::ADDCARRY: {
10406     // add x, (addcarry y, 0, cc) => addcarry x, y, cc
10407     auto C = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
10408     if (!C || C->getZExtValue() != 0) break;
10409     SDValue Args[] = { LHS, RHS.getOperand(0), RHS.getOperand(2) };
10410     return DAG.getNode(ISD::ADDCARRY, SDLoc(N), RHS->getVTList(), Args);
10411   }
10412   }
10413   return SDValue();
10414 }
10415 
10416 SDValue SITargetLowering::performSubCombine(SDNode *N,
10417                                             DAGCombinerInfo &DCI) const {
10418   SelectionDAG &DAG = DCI.DAG;
10419   EVT VT = N->getValueType(0);
10420 
10421   if (VT != MVT::i32)
10422     return SDValue();
10423 
10424   SDLoc SL(N);
10425   SDValue LHS = N->getOperand(0);
10426   SDValue RHS = N->getOperand(1);
10427 
10428   // sub x, zext (setcc) => subcarry x, 0, setcc
10429   // sub x, sext (setcc) => addcarry x, 0, setcc
10430   unsigned Opc = RHS.getOpcode();
10431   switch (Opc) {
10432   default: break;
10433   case ISD::ZERO_EXTEND:
10434   case ISD::SIGN_EXTEND:
10435   case ISD::ANY_EXTEND: {
10436     auto Cond = RHS.getOperand(0);
10437     // If this won't be a real VOPC output, we would still need to insert an
10438     // extra instruction anyway.
10439     if (!isBoolSGPR(Cond))
10440       break;
10441     SDVTList VTList = DAG.getVTList(MVT::i32, MVT::i1);
10442     SDValue Args[] = { LHS, DAG.getConstant(0, SL, MVT::i32), Cond };
10443     Opc = (Opc == ISD::SIGN_EXTEND) ? ISD::ADDCARRY : ISD::SUBCARRY;
10444     return DAG.getNode(Opc, SL, VTList, Args);
10445   }
10446   }
10447 
10448   if (LHS.getOpcode() == ISD::SUBCARRY) {
10449     // sub (subcarry x, 0, cc), y => subcarry x, y, cc
10450     auto C = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
10451     if (!C || !C->isNullValue())
10452       return SDValue();
10453     SDValue Args[] = { LHS.getOperand(0), RHS, LHS.getOperand(2) };
10454     return DAG.getNode(ISD::SUBCARRY, SDLoc(N), LHS->getVTList(), Args);
10455   }
10456   return SDValue();
10457 }
10458 
10459 SDValue SITargetLowering::performAddCarrySubCarryCombine(SDNode *N,
10460   DAGCombinerInfo &DCI) const {
10461 
10462   if (N->getValueType(0) != MVT::i32)
10463     return SDValue();
10464 
10465   auto C = dyn_cast<ConstantSDNode>(N->getOperand(1));
10466   if (!C || C->getZExtValue() != 0)
10467     return SDValue();
10468 
10469   SelectionDAG &DAG = DCI.DAG;
10470   SDValue LHS = N->getOperand(0);
10471 
10472   // addcarry (add x, y), 0, cc => addcarry x, y, cc
10473   // subcarry (sub x, y), 0, cc => subcarry x, y, cc
10474   unsigned LHSOpc = LHS.getOpcode();
10475   unsigned Opc = N->getOpcode();
10476   if ((LHSOpc == ISD::ADD && Opc == ISD::ADDCARRY) ||
10477       (LHSOpc == ISD::SUB && Opc == ISD::SUBCARRY)) {
10478     SDValue Args[] = { LHS.getOperand(0), LHS.getOperand(1), N->getOperand(2) };
10479     return DAG.getNode(Opc, SDLoc(N), N->getVTList(), Args);
10480   }
10481   return SDValue();
10482 }
10483 
10484 SDValue SITargetLowering::performFAddCombine(SDNode *N,
10485                                              DAGCombinerInfo &DCI) const {
10486   if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
10487     return SDValue();
10488 
10489   SelectionDAG &DAG = DCI.DAG;
10490   EVT VT = N->getValueType(0);
10491 
10492   SDLoc SL(N);
10493   SDValue LHS = N->getOperand(0);
10494   SDValue RHS = N->getOperand(1);
10495 
10496   // These should really be instruction patterns, but writing patterns with
10497   // source modiifiers is a pain.
10498 
10499   // fadd (fadd (a, a), b) -> mad 2.0, a, b
10500   if (LHS.getOpcode() == ISD::FADD) {
10501     SDValue A = LHS.getOperand(0);
10502     if (A == LHS.getOperand(1)) {
10503       unsigned FusedOp = getFusedOpcode(DAG, N, LHS.getNode());
10504       if (FusedOp != 0) {
10505         const SDValue Two = DAG.getConstantFP(2.0, SL, VT);
10506         return DAG.getNode(FusedOp, SL, VT, A, Two, RHS);
10507       }
10508     }
10509   }
10510 
10511   // fadd (b, fadd (a, a)) -> mad 2.0, a, b
10512   if (RHS.getOpcode() == ISD::FADD) {
10513     SDValue A = RHS.getOperand(0);
10514     if (A == RHS.getOperand(1)) {
10515       unsigned FusedOp = getFusedOpcode(DAG, N, RHS.getNode());
10516       if (FusedOp != 0) {
10517         const SDValue Two = DAG.getConstantFP(2.0, SL, VT);
10518         return DAG.getNode(FusedOp, SL, VT, A, Two, LHS);
10519       }
10520     }
10521   }
10522 
10523   return SDValue();
10524 }
10525 
10526 SDValue SITargetLowering::performFSubCombine(SDNode *N,
10527                                              DAGCombinerInfo &DCI) const {
10528   if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
10529     return SDValue();
10530 
10531   SelectionDAG &DAG = DCI.DAG;
10532   SDLoc SL(N);
10533   EVT VT = N->getValueType(0);
10534   assert(!VT.isVector());
10535 
10536   // Try to get the fneg to fold into the source modifier. This undoes generic
10537   // DAG combines and folds them into the mad.
10538   //
10539   // Only do this if we are not trying to support denormals. v_mad_f32 does
10540   // not support denormals ever.
10541   SDValue LHS = N->getOperand(0);
10542   SDValue RHS = N->getOperand(1);
10543   if (LHS.getOpcode() == ISD::FADD) {
10544     // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c)
10545     SDValue A = LHS.getOperand(0);
10546     if (A == LHS.getOperand(1)) {
10547       unsigned FusedOp = getFusedOpcode(DAG, N, LHS.getNode());
10548       if (FusedOp != 0){
10549         const SDValue Two = DAG.getConstantFP(2.0, SL, VT);
10550         SDValue NegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
10551 
10552         return DAG.getNode(FusedOp, SL, VT, A, Two, NegRHS);
10553       }
10554     }
10555   }
10556 
10557   if (RHS.getOpcode() == ISD::FADD) {
10558     // (fsub c, (fadd a, a)) -> mad -2.0, a, c
10559 
10560     SDValue A = RHS.getOperand(0);
10561     if (A == RHS.getOperand(1)) {
10562       unsigned FusedOp = getFusedOpcode(DAG, N, RHS.getNode());
10563       if (FusedOp != 0){
10564         const SDValue NegTwo = DAG.getConstantFP(-2.0, SL, VT);
10565         return DAG.getNode(FusedOp, SL, VT, A, NegTwo, LHS);
10566       }
10567     }
10568   }
10569 
10570   return SDValue();
10571 }
10572 
10573 SDValue SITargetLowering::performFMACombine(SDNode *N,
10574                                             DAGCombinerInfo &DCI) const {
10575   SelectionDAG &DAG = DCI.DAG;
10576   EVT VT = N->getValueType(0);
10577   SDLoc SL(N);
10578 
10579   if (!Subtarget->hasDot7Insts() || VT != MVT::f32)
10580     return SDValue();
10581 
10582   // FMA((F32)S0.x, (F32)S1. x, FMA((F32)S0.y, (F32)S1.y, (F32)z)) ->
10583   //   FDOT2((V2F16)S0, (V2F16)S1, (F32)z))
10584   SDValue Op1 = N->getOperand(0);
10585   SDValue Op2 = N->getOperand(1);
10586   SDValue FMA = N->getOperand(2);
10587 
10588   if (FMA.getOpcode() != ISD::FMA ||
10589       Op1.getOpcode() != ISD::FP_EXTEND ||
10590       Op2.getOpcode() != ISD::FP_EXTEND)
10591     return SDValue();
10592 
10593   // fdot2_f32_f16 always flushes fp32 denormal operand and output to zero,
10594   // regardless of the denorm mode setting. Therefore, unsafe-fp-math/fp-contract
10595   // is sufficient to allow generaing fdot2.
10596   const TargetOptions &Options = DAG.getTarget().Options;
10597   if (Options.AllowFPOpFusion == FPOpFusion::Fast || Options.UnsafeFPMath ||
10598       (N->getFlags().hasAllowContract() &&
10599        FMA->getFlags().hasAllowContract())) {
10600     Op1 = Op1.getOperand(0);
10601     Op2 = Op2.getOperand(0);
10602     if (Op1.getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
10603         Op2.getOpcode() != ISD::EXTRACT_VECTOR_ELT)
10604       return SDValue();
10605 
10606     SDValue Vec1 = Op1.getOperand(0);
10607     SDValue Idx1 = Op1.getOperand(1);
10608     SDValue Vec2 = Op2.getOperand(0);
10609 
10610     SDValue FMAOp1 = FMA.getOperand(0);
10611     SDValue FMAOp2 = FMA.getOperand(1);
10612     SDValue FMAAcc = FMA.getOperand(2);
10613 
10614     if (FMAOp1.getOpcode() != ISD::FP_EXTEND ||
10615         FMAOp2.getOpcode() != ISD::FP_EXTEND)
10616       return SDValue();
10617 
10618     FMAOp1 = FMAOp1.getOperand(0);
10619     FMAOp2 = FMAOp2.getOperand(0);
10620     if (FMAOp1.getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
10621         FMAOp2.getOpcode() != ISD::EXTRACT_VECTOR_ELT)
10622       return SDValue();
10623 
10624     SDValue Vec3 = FMAOp1.getOperand(0);
10625     SDValue Vec4 = FMAOp2.getOperand(0);
10626     SDValue Idx2 = FMAOp1.getOperand(1);
10627 
10628     if (Idx1 != Op2.getOperand(1) || Idx2 != FMAOp2.getOperand(1) ||
10629         // Idx1 and Idx2 cannot be the same.
10630         Idx1 == Idx2)
10631       return SDValue();
10632 
10633     if (Vec1 == Vec2 || Vec3 == Vec4)
10634       return SDValue();
10635 
10636     if (Vec1.getValueType() != MVT::v2f16 || Vec2.getValueType() != MVT::v2f16)
10637       return SDValue();
10638 
10639     if ((Vec1 == Vec3 && Vec2 == Vec4) ||
10640         (Vec1 == Vec4 && Vec2 == Vec3)) {
10641       return DAG.getNode(AMDGPUISD::FDOT2, SL, MVT::f32, Vec1, Vec2, FMAAcc,
10642                          DAG.getTargetConstant(0, SL, MVT::i1));
10643     }
10644   }
10645   return SDValue();
10646 }
10647 
10648 SDValue SITargetLowering::performSetCCCombine(SDNode *N,
10649                                               DAGCombinerInfo &DCI) const {
10650   SelectionDAG &DAG = DCI.DAG;
10651   SDLoc SL(N);
10652 
10653   SDValue LHS = N->getOperand(0);
10654   SDValue RHS = N->getOperand(1);
10655   EVT VT = LHS.getValueType();
10656   ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
10657 
10658   auto CRHS = dyn_cast<ConstantSDNode>(RHS);
10659   if (!CRHS) {
10660     CRHS = dyn_cast<ConstantSDNode>(LHS);
10661     if (CRHS) {
10662       std::swap(LHS, RHS);
10663       CC = getSetCCSwappedOperands(CC);
10664     }
10665   }
10666 
10667   if (CRHS) {
10668     if (VT == MVT::i32 && LHS.getOpcode() == ISD::SIGN_EXTEND &&
10669         isBoolSGPR(LHS.getOperand(0))) {
10670       // setcc (sext from i1 cc), -1, ne|sgt|ult) => not cc => xor cc, -1
10671       // setcc (sext from i1 cc), -1, eq|sle|uge) => cc
10672       // setcc (sext from i1 cc),  0, eq|sge|ule) => not cc => xor cc, -1
10673       // setcc (sext from i1 cc),  0, ne|ugt|slt) => cc
10674       if ((CRHS->isAllOnesValue() &&
10675            (CC == ISD::SETNE || CC == ISD::SETGT || CC == ISD::SETULT)) ||
10676           (CRHS->isNullValue() &&
10677            (CC == ISD::SETEQ || CC == ISD::SETGE || CC == ISD::SETULE)))
10678         return DAG.getNode(ISD::XOR, SL, MVT::i1, LHS.getOperand(0),
10679                            DAG.getConstant(-1, SL, MVT::i1));
10680       if ((CRHS->isAllOnesValue() &&
10681            (CC == ISD::SETEQ || CC == ISD::SETLE || CC == ISD::SETUGE)) ||
10682           (CRHS->isNullValue() &&
10683            (CC == ISD::SETNE || CC == ISD::SETUGT || CC == ISD::SETLT)))
10684         return LHS.getOperand(0);
10685     }
10686 
10687     uint64_t CRHSVal = CRHS->getZExtValue();
10688     if ((CC == ISD::SETEQ || CC == ISD::SETNE) &&
10689         LHS.getOpcode() == ISD::SELECT &&
10690         isa<ConstantSDNode>(LHS.getOperand(1)) &&
10691         isa<ConstantSDNode>(LHS.getOperand(2)) &&
10692         LHS.getConstantOperandVal(1) != LHS.getConstantOperandVal(2) &&
10693         isBoolSGPR(LHS.getOperand(0))) {
10694       // Given CT != FT:
10695       // setcc (select cc, CT, CF), CF, eq => xor cc, -1
10696       // setcc (select cc, CT, CF), CF, ne => cc
10697       // setcc (select cc, CT, CF), CT, ne => xor cc, -1
10698       // setcc (select cc, CT, CF), CT, eq => cc
10699       uint64_t CT = LHS.getConstantOperandVal(1);
10700       uint64_t CF = LHS.getConstantOperandVal(2);
10701 
10702       if ((CF == CRHSVal && CC == ISD::SETEQ) ||
10703           (CT == CRHSVal && CC == ISD::SETNE))
10704         return DAG.getNode(ISD::XOR, SL, MVT::i1, LHS.getOperand(0),
10705                            DAG.getConstant(-1, SL, MVT::i1));
10706       if ((CF == CRHSVal && CC == ISD::SETNE) ||
10707           (CT == CRHSVal && CC == ISD::SETEQ))
10708         return LHS.getOperand(0);
10709     }
10710   }
10711 
10712   if (VT != MVT::f32 && VT != MVT::f64 && (Subtarget->has16BitInsts() &&
10713                                            VT != MVT::f16))
10714     return SDValue();
10715 
10716   // Match isinf/isfinite pattern
10717   // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity))
10718   // (fcmp one (fabs x), inf) -> (fp_class x,
10719   // (p_normal | n_normal | p_subnormal | n_subnormal | p_zero | n_zero)
10720   if ((CC == ISD::SETOEQ || CC == ISD::SETONE) && LHS.getOpcode() == ISD::FABS) {
10721     const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
10722     if (!CRHS)
10723       return SDValue();
10724 
10725     const APFloat &APF = CRHS->getValueAPF();
10726     if (APF.isInfinity() && !APF.isNegative()) {
10727       const unsigned IsInfMask = SIInstrFlags::P_INFINITY |
10728                                  SIInstrFlags::N_INFINITY;
10729       const unsigned IsFiniteMask = SIInstrFlags::N_ZERO |
10730                                     SIInstrFlags::P_ZERO |
10731                                     SIInstrFlags::N_NORMAL |
10732                                     SIInstrFlags::P_NORMAL |
10733                                     SIInstrFlags::N_SUBNORMAL |
10734                                     SIInstrFlags::P_SUBNORMAL;
10735       unsigned Mask = CC == ISD::SETOEQ ? IsInfMask : IsFiniteMask;
10736       return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0),
10737                          DAG.getConstant(Mask, SL, MVT::i32));
10738     }
10739   }
10740 
10741   return SDValue();
10742 }
10743 
10744 SDValue SITargetLowering::performCvtF32UByteNCombine(SDNode *N,
10745                                                      DAGCombinerInfo &DCI) const {
10746   SelectionDAG &DAG = DCI.DAG;
10747   SDLoc SL(N);
10748   unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
10749 
10750   SDValue Src = N->getOperand(0);
10751   SDValue Shift = N->getOperand(0);
10752 
10753   // TODO: Extend type shouldn't matter (assuming legal types).
10754   if (Shift.getOpcode() == ISD::ZERO_EXTEND)
10755     Shift = Shift.getOperand(0);
10756 
10757   if (Shift.getOpcode() == ISD::SRL || Shift.getOpcode() == ISD::SHL) {
10758     // cvt_f32_ubyte1 (shl x,  8) -> cvt_f32_ubyte0 x
10759     // cvt_f32_ubyte3 (shl x, 16) -> cvt_f32_ubyte1 x
10760     // cvt_f32_ubyte0 (srl x, 16) -> cvt_f32_ubyte2 x
10761     // cvt_f32_ubyte1 (srl x, 16) -> cvt_f32_ubyte3 x
10762     // cvt_f32_ubyte0 (srl x,  8) -> cvt_f32_ubyte1 x
10763     if (auto *C = dyn_cast<ConstantSDNode>(Shift.getOperand(1))) {
10764       Shift = DAG.getZExtOrTrunc(Shift.getOperand(0),
10765                                  SDLoc(Shift.getOperand(0)), MVT::i32);
10766 
10767       unsigned ShiftOffset = 8 * Offset;
10768       if (Shift.getOpcode() == ISD::SHL)
10769         ShiftOffset -= C->getZExtValue();
10770       else
10771         ShiftOffset += C->getZExtValue();
10772 
10773       if (ShiftOffset < 32 && (ShiftOffset % 8) == 0) {
10774         return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0 + ShiftOffset / 8, SL,
10775                            MVT::f32, Shift);
10776       }
10777     }
10778   }
10779 
10780   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10781   APInt DemandedBits = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
10782   if (TLI.SimplifyDemandedBits(Src, DemandedBits, DCI)) {
10783     // We simplified Src. If this node is not dead, visit it again so it is
10784     // folded properly.
10785     if (N->getOpcode() != ISD::DELETED_NODE)
10786       DCI.AddToWorklist(N);
10787     return SDValue(N, 0);
10788   }
10789 
10790   // Handle (or x, (srl y, 8)) pattern when known bits are zero.
10791   if (SDValue DemandedSrc =
10792           TLI.SimplifyMultipleUseDemandedBits(Src, DemandedBits, DAG))
10793     return DAG.getNode(N->getOpcode(), SL, MVT::f32, DemandedSrc);
10794 
10795   return SDValue();
10796 }
10797 
10798 SDValue SITargetLowering::performClampCombine(SDNode *N,
10799                                               DAGCombinerInfo &DCI) const {
10800   ConstantFPSDNode *CSrc = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
10801   if (!CSrc)
10802     return SDValue();
10803 
10804   const MachineFunction &MF = DCI.DAG.getMachineFunction();
10805   const APFloat &F = CSrc->getValueAPF();
10806   APFloat Zero = APFloat::getZero(F.getSemantics());
10807   if (F < Zero ||
10808       (F.isNaN() && MF.getInfo<SIMachineFunctionInfo>()->getMode().DX10Clamp)) {
10809     return DCI.DAG.getConstantFP(Zero, SDLoc(N), N->getValueType(0));
10810   }
10811 
10812   APFloat One(F.getSemantics(), "1.0");
10813   if (F > One)
10814     return DCI.DAG.getConstantFP(One, SDLoc(N), N->getValueType(0));
10815 
10816   return SDValue(CSrc, 0);
10817 }
10818 
10819 
10820 SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
10821                                             DAGCombinerInfo &DCI) const {
10822   if (getTargetMachine().getOptLevel() == CodeGenOpt::None)
10823     return SDValue();
10824   switch (N->getOpcode()) {
10825   case ISD::ADD:
10826     return performAddCombine(N, DCI);
10827   case ISD::SUB:
10828     return performSubCombine(N, DCI);
10829   case ISD::ADDCARRY:
10830   case ISD::SUBCARRY:
10831     return performAddCarrySubCarryCombine(N, DCI);
10832   case ISD::FADD:
10833     return performFAddCombine(N, DCI);
10834   case ISD::FSUB:
10835     return performFSubCombine(N, DCI);
10836   case ISD::SETCC:
10837     return performSetCCCombine(N, DCI);
10838   case ISD::FMAXNUM:
10839   case ISD::FMINNUM:
10840   case ISD::FMAXNUM_IEEE:
10841   case ISD::FMINNUM_IEEE:
10842   case ISD::SMAX:
10843   case ISD::SMIN:
10844   case ISD::UMAX:
10845   case ISD::UMIN:
10846   case AMDGPUISD::FMIN_LEGACY:
10847   case AMDGPUISD::FMAX_LEGACY:
10848     return performMinMaxCombine(N, DCI);
10849   case ISD::FMA:
10850     return performFMACombine(N, DCI);
10851   case ISD::AND:
10852     return performAndCombine(N, DCI);
10853   case ISD::OR:
10854     return performOrCombine(N, DCI);
10855   case ISD::XOR:
10856     return performXorCombine(N, DCI);
10857   case ISD::ZERO_EXTEND:
10858     return performZeroExtendCombine(N, DCI);
10859   case ISD::SIGN_EXTEND_INREG:
10860     return performSignExtendInRegCombine(N , DCI);
10861   case AMDGPUISD::FP_CLASS:
10862     return performClassCombine(N, DCI);
10863   case ISD::FCANONICALIZE:
10864     return performFCanonicalizeCombine(N, DCI);
10865   case AMDGPUISD::RCP:
10866     return performRcpCombine(N, DCI);
10867   case AMDGPUISD::FRACT:
10868   case AMDGPUISD::RSQ:
10869   case AMDGPUISD::RCP_LEGACY:
10870   case AMDGPUISD::RCP_IFLAG:
10871   case AMDGPUISD::RSQ_CLAMP:
10872   case AMDGPUISD::LDEXP: {
10873     // FIXME: This is probably wrong. If src is an sNaN, it won't be quieted
10874     SDValue Src = N->getOperand(0);
10875     if (Src.isUndef())
10876       return Src;
10877     break;
10878   }
10879   case ISD::SINT_TO_FP:
10880   case ISD::UINT_TO_FP:
10881     return performUCharToFloatCombine(N, DCI);
10882   case AMDGPUISD::CVT_F32_UBYTE0:
10883   case AMDGPUISD::CVT_F32_UBYTE1:
10884   case AMDGPUISD::CVT_F32_UBYTE2:
10885   case AMDGPUISD::CVT_F32_UBYTE3:
10886     return performCvtF32UByteNCombine(N, DCI);
10887   case AMDGPUISD::FMED3:
10888     return performFMed3Combine(N, DCI);
10889   case AMDGPUISD::CVT_PKRTZ_F16_F32:
10890     return performCvtPkRTZCombine(N, DCI);
10891   case AMDGPUISD::CLAMP:
10892     return performClampCombine(N, DCI);
10893   case ISD::SCALAR_TO_VECTOR: {
10894     SelectionDAG &DAG = DCI.DAG;
10895     EVT VT = N->getValueType(0);
10896 
10897     // v2i16 (scalar_to_vector i16:x) -> v2i16 (bitcast (any_extend i16:x))
10898     if (VT == MVT::v2i16 || VT == MVT::v2f16) {
10899       SDLoc SL(N);
10900       SDValue Src = N->getOperand(0);
10901       EVT EltVT = Src.getValueType();
10902       if (EltVT == MVT::f16)
10903         Src = DAG.getNode(ISD::BITCAST, SL, MVT::i16, Src);
10904 
10905       SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SL, MVT::i32, Src);
10906       return DAG.getNode(ISD::BITCAST, SL, VT, Ext);
10907     }
10908 
10909     break;
10910   }
10911   case ISD::EXTRACT_VECTOR_ELT:
10912     return performExtractVectorEltCombine(N, DCI);
10913   case ISD::INSERT_VECTOR_ELT:
10914     return performInsertVectorEltCombine(N, DCI);
10915   case ISD::LOAD: {
10916     if (SDValue Widended = widenLoad(cast<LoadSDNode>(N), DCI))
10917       return Widended;
10918     LLVM_FALLTHROUGH;
10919   }
10920   default: {
10921     if (!DCI.isBeforeLegalize()) {
10922       if (MemSDNode *MemNode = dyn_cast<MemSDNode>(N))
10923         return performMemSDNodeCombine(MemNode, DCI);
10924     }
10925 
10926     break;
10927   }
10928   }
10929 
10930   return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
10931 }
10932 
10933 /// Helper function for adjustWritemask
10934 static unsigned SubIdx2Lane(unsigned Idx) {
10935   switch (Idx) {
10936   default: return ~0u;
10937   case AMDGPU::sub0: return 0;
10938   case AMDGPU::sub1: return 1;
10939   case AMDGPU::sub2: return 2;
10940   case AMDGPU::sub3: return 3;
10941   case AMDGPU::sub4: return 4; // Possible with TFE/LWE
10942   }
10943 }
10944 
10945 /// Adjust the writemask of MIMG instructions
10946 SDNode *SITargetLowering::adjustWritemask(MachineSDNode *&Node,
10947                                           SelectionDAG &DAG) const {
10948   unsigned Opcode = Node->getMachineOpcode();
10949 
10950   // Subtract 1 because the vdata output is not a MachineSDNode operand.
10951   int D16Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::d16) - 1;
10952   if (D16Idx >= 0 && Node->getConstantOperandVal(D16Idx))
10953     return Node; // not implemented for D16
10954 
10955   SDNode *Users[5] = { nullptr };
10956   unsigned Lane = 0;
10957   unsigned DmaskIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::dmask) - 1;
10958   unsigned OldDmask = Node->getConstantOperandVal(DmaskIdx);
10959   unsigned NewDmask = 0;
10960   unsigned TFEIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::tfe) - 1;
10961   unsigned LWEIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::lwe) - 1;
10962   bool UsesTFC = ((int(TFEIdx) >= 0 && Node->getConstantOperandVal(TFEIdx)) ||
10963                   Node->getConstantOperandVal(LWEIdx)) ? 1 : 0;
10964   unsigned TFCLane = 0;
10965   bool HasChain = Node->getNumValues() > 1;
10966 
10967   if (OldDmask == 0) {
10968     // These are folded out, but on the chance it happens don't assert.
10969     return Node;
10970   }
10971 
10972   unsigned OldBitsSet = countPopulation(OldDmask);
10973   // Work out which is the TFE/LWE lane if that is enabled.
10974   if (UsesTFC) {
10975     TFCLane = OldBitsSet;
10976   }
10977 
10978   // Try to figure out the used register components
10979   for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
10980        I != E; ++I) {
10981 
10982     // Don't look at users of the chain.
10983     if (I.getUse().getResNo() != 0)
10984       continue;
10985 
10986     // Abort if we can't understand the usage
10987     if (!I->isMachineOpcode() ||
10988         I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
10989       return Node;
10990 
10991     // Lane means which subreg of %vgpra_vgprb_vgprc_vgprd is used.
10992     // Note that subregs are packed, i.e. Lane==0 is the first bit set
10993     // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
10994     // set, etc.
10995     Lane = SubIdx2Lane(I->getConstantOperandVal(1));
10996     if (Lane == ~0u)
10997       return Node;
10998 
10999     // Check if the use is for the TFE/LWE generated result at VGPRn+1.
11000     if (UsesTFC && Lane == TFCLane) {
11001       Users[Lane] = *I;
11002     } else {
11003       // Set which texture component corresponds to the lane.
11004       unsigned Comp;
11005       for (unsigned i = 0, Dmask = OldDmask; (i <= Lane) && (Dmask != 0); i++) {
11006         Comp = countTrailingZeros(Dmask);
11007         Dmask &= ~(1 << Comp);
11008       }
11009 
11010       // Abort if we have more than one user per component.
11011       if (Users[Lane])
11012         return Node;
11013 
11014       Users[Lane] = *I;
11015       NewDmask |= 1 << Comp;
11016     }
11017   }
11018 
11019   // Don't allow 0 dmask, as hardware assumes one channel enabled.
11020   bool NoChannels = !NewDmask;
11021   if (NoChannels) {
11022     if (!UsesTFC) {
11023       // No uses of the result and not using TFC. Then do nothing.
11024       return Node;
11025     }
11026     // If the original dmask has one channel - then nothing to do
11027     if (OldBitsSet == 1)
11028       return Node;
11029     // Use an arbitrary dmask - required for the instruction to work
11030     NewDmask = 1;
11031   }
11032   // Abort if there's no change
11033   if (NewDmask == OldDmask)
11034     return Node;
11035 
11036   unsigned BitsSet = countPopulation(NewDmask);
11037 
11038   // Check for TFE or LWE - increase the number of channels by one to account
11039   // for the extra return value
11040   // This will need adjustment for D16 if this is also included in
11041   // adjustWriteMask (this function) but at present D16 are excluded.
11042   unsigned NewChannels = BitsSet + UsesTFC;
11043 
11044   int NewOpcode =
11045       AMDGPU::getMaskedMIMGOp(Node->getMachineOpcode(), NewChannels);
11046   assert(NewOpcode != -1 &&
11047          NewOpcode != static_cast<int>(Node->getMachineOpcode()) &&
11048          "failed to find equivalent MIMG op");
11049 
11050   // Adjust the writemask in the node
11051   SmallVector<SDValue, 12> Ops;
11052   Ops.insert(Ops.end(), Node->op_begin(), Node->op_begin() + DmaskIdx);
11053   Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32));
11054   Ops.insert(Ops.end(), Node->op_begin() + DmaskIdx + 1, Node->op_end());
11055 
11056   MVT SVT = Node->getValueType(0).getVectorElementType().getSimpleVT();
11057 
11058   MVT ResultVT = NewChannels == 1 ?
11059     SVT : MVT::getVectorVT(SVT, NewChannels == 3 ? 4 :
11060                            NewChannels == 5 ? 8 : NewChannels);
11061   SDVTList NewVTList = HasChain ?
11062     DAG.getVTList(ResultVT, MVT::Other) : DAG.getVTList(ResultVT);
11063 
11064 
11065   MachineSDNode *NewNode = DAG.getMachineNode(NewOpcode, SDLoc(Node),
11066                                               NewVTList, Ops);
11067 
11068   if (HasChain) {
11069     // Update chain.
11070     DAG.setNodeMemRefs(NewNode, Node->memoperands());
11071     DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(NewNode, 1));
11072   }
11073 
11074   if (NewChannels == 1) {
11075     assert(Node->hasNUsesOfValue(1, 0));
11076     SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY,
11077                                       SDLoc(Node), Users[Lane]->getValueType(0),
11078                                       SDValue(NewNode, 0));
11079     DAG.ReplaceAllUsesWith(Users[Lane], Copy);
11080     return nullptr;
11081   }
11082 
11083   // Update the users of the node with the new indices
11084   for (unsigned i = 0, Idx = AMDGPU::sub0; i < 5; ++i) {
11085     SDNode *User = Users[i];
11086     if (!User) {
11087       // Handle the special case of NoChannels. We set NewDmask to 1 above, but
11088       // Users[0] is still nullptr because channel 0 doesn't really have a use.
11089       if (i || !NoChannels)
11090         continue;
11091     } else {
11092       SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32);
11093       DAG.UpdateNodeOperands(User, SDValue(NewNode, 0), Op);
11094     }
11095 
11096     switch (Idx) {
11097     default: break;
11098     case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
11099     case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
11100     case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
11101     case AMDGPU::sub3: Idx = AMDGPU::sub4; break;
11102     }
11103   }
11104 
11105   DAG.RemoveDeadNode(Node);
11106   return nullptr;
11107 }
11108 
11109 static bool isFrameIndexOp(SDValue Op) {
11110   if (Op.getOpcode() == ISD::AssertZext)
11111     Op = Op.getOperand(0);
11112 
11113   return isa<FrameIndexSDNode>(Op);
11114 }
11115 
11116 /// Legalize target independent instructions (e.g. INSERT_SUBREG)
11117 /// with frame index operands.
11118 /// LLVM assumes that inputs are to these instructions are registers.
11119 SDNode *SITargetLowering::legalizeTargetIndependentNode(SDNode *Node,
11120                                                         SelectionDAG &DAG) const {
11121   if (Node->getOpcode() == ISD::CopyToReg) {
11122     RegisterSDNode *DestReg = cast<RegisterSDNode>(Node->getOperand(1));
11123     SDValue SrcVal = Node->getOperand(2);
11124 
11125     // Insert a copy to a VReg_1 virtual register so LowerI1Copies doesn't have
11126     // to try understanding copies to physical registers.
11127     if (SrcVal.getValueType() == MVT::i1 && DestReg->getReg().isPhysical()) {
11128       SDLoc SL(Node);
11129       MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
11130       SDValue VReg = DAG.getRegister(
11131         MRI.createVirtualRegister(&AMDGPU::VReg_1RegClass), MVT::i1);
11132 
11133       SDNode *Glued = Node->getGluedNode();
11134       SDValue ToVReg
11135         = DAG.getCopyToReg(Node->getOperand(0), SL, VReg, SrcVal,
11136                          SDValue(Glued, Glued ? Glued->getNumValues() - 1 : 0));
11137       SDValue ToResultReg
11138         = DAG.getCopyToReg(ToVReg, SL, SDValue(DestReg, 0),
11139                            VReg, ToVReg.getValue(1));
11140       DAG.ReplaceAllUsesWith(Node, ToResultReg.getNode());
11141       DAG.RemoveDeadNode(Node);
11142       return ToResultReg.getNode();
11143     }
11144   }
11145 
11146   SmallVector<SDValue, 8> Ops;
11147   for (unsigned i = 0; i < Node->getNumOperands(); ++i) {
11148     if (!isFrameIndexOp(Node->getOperand(i))) {
11149       Ops.push_back(Node->getOperand(i));
11150       continue;
11151     }
11152 
11153     SDLoc DL(Node);
11154     Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL,
11155                                      Node->getOperand(i).getValueType(),
11156                                      Node->getOperand(i)), 0));
11157   }
11158 
11159   return DAG.UpdateNodeOperands(Node, Ops);
11160 }
11161 
11162 /// Fold the instructions after selecting them.
11163 /// Returns null if users were already updated.
11164 SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
11165                                           SelectionDAG &DAG) const {
11166   const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
11167   unsigned Opcode = Node->getMachineOpcode();
11168 
11169   if (TII->isMIMG(Opcode) && !TII->get(Opcode).mayStore() &&
11170       !TII->isGather4(Opcode) &&
11171       AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::dmask) != -1) {
11172     return adjustWritemask(Node, DAG);
11173   }
11174 
11175   if (Opcode == AMDGPU::INSERT_SUBREG ||
11176       Opcode == AMDGPU::REG_SEQUENCE) {
11177     legalizeTargetIndependentNode(Node, DAG);
11178     return Node;
11179   }
11180 
11181   switch (Opcode) {
11182   case AMDGPU::V_DIV_SCALE_F32_e64:
11183   case AMDGPU::V_DIV_SCALE_F64_e64: {
11184     // Satisfy the operand register constraint when one of the inputs is
11185     // undefined. Ordinarily each undef value will have its own implicit_def of
11186     // a vreg, so force these to use a single register.
11187     SDValue Src0 = Node->getOperand(1);
11188     SDValue Src1 = Node->getOperand(3);
11189     SDValue Src2 = Node->getOperand(5);
11190 
11191     if ((Src0.isMachineOpcode() &&
11192          Src0.getMachineOpcode() != AMDGPU::IMPLICIT_DEF) &&
11193         (Src0 == Src1 || Src0 == Src2))
11194       break;
11195 
11196     MVT VT = Src0.getValueType().getSimpleVT();
11197     const TargetRegisterClass *RC =
11198         getRegClassFor(VT, Src0.getNode()->isDivergent());
11199 
11200     MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
11201     SDValue UndefReg = DAG.getRegister(MRI.createVirtualRegister(RC), VT);
11202 
11203     SDValue ImpDef = DAG.getCopyToReg(DAG.getEntryNode(), SDLoc(Node),
11204                                       UndefReg, Src0, SDValue());
11205 
11206     // src0 must be the same register as src1 or src2, even if the value is
11207     // undefined, so make sure we don't violate this constraint.
11208     if (Src0.isMachineOpcode() &&
11209         Src0.getMachineOpcode() == AMDGPU::IMPLICIT_DEF) {
11210       if (Src1.isMachineOpcode() &&
11211           Src1.getMachineOpcode() != AMDGPU::IMPLICIT_DEF)
11212         Src0 = Src1;
11213       else if (Src2.isMachineOpcode() &&
11214                Src2.getMachineOpcode() != AMDGPU::IMPLICIT_DEF)
11215         Src0 = Src2;
11216       else {
11217         assert(Src1.getMachineOpcode() == AMDGPU::IMPLICIT_DEF);
11218         Src0 = UndefReg;
11219         Src1 = UndefReg;
11220       }
11221     } else
11222       break;
11223 
11224     SmallVector<SDValue, 9> Ops(Node->op_begin(), Node->op_end());
11225     Ops[1] = Src0;
11226     Ops[3] = Src1;
11227     Ops[5] = Src2;
11228     Ops.push_back(ImpDef.getValue(1));
11229     return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
11230   }
11231   default:
11232     break;
11233   }
11234 
11235   return Node;
11236 }
11237 
11238 // Any MIMG instructions that use tfe or lwe require an initialization of the
11239 // result register that will be written in the case of a memory access failure.
11240 // The required code is also added to tie this init code to the result of the
11241 // img instruction.
11242 void SITargetLowering::AddIMGInit(MachineInstr &MI) const {
11243   const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
11244   const SIRegisterInfo &TRI = TII->getRegisterInfo();
11245   MachineRegisterInfo &MRI = MI.getMF()->getRegInfo();
11246   MachineBasicBlock &MBB = *MI.getParent();
11247 
11248   MachineOperand *TFE = TII->getNamedOperand(MI, AMDGPU::OpName::tfe);
11249   MachineOperand *LWE = TII->getNamedOperand(MI, AMDGPU::OpName::lwe);
11250   MachineOperand *D16 = TII->getNamedOperand(MI, AMDGPU::OpName::d16);
11251 
11252   if (!TFE && !LWE) // intersect_ray
11253     return;
11254 
11255   unsigned TFEVal = TFE ? TFE->getImm() : 0;
11256   unsigned LWEVal = LWE->getImm();
11257   unsigned D16Val = D16 ? D16->getImm() : 0;
11258 
11259   if (!TFEVal && !LWEVal)
11260     return;
11261 
11262   // At least one of TFE or LWE are non-zero
11263   // We have to insert a suitable initialization of the result value and
11264   // tie this to the dest of the image instruction.
11265 
11266   const DebugLoc &DL = MI.getDebugLoc();
11267 
11268   int DstIdx =
11269       AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::vdata);
11270 
11271   // Calculate which dword we have to initialize to 0.
11272   MachineOperand *MO_Dmask = TII->getNamedOperand(MI, AMDGPU::OpName::dmask);
11273 
11274   // check that dmask operand is found.
11275   assert(MO_Dmask && "Expected dmask operand in instruction");
11276 
11277   unsigned dmask = MO_Dmask->getImm();
11278   // Determine the number of active lanes taking into account the
11279   // Gather4 special case
11280   unsigned ActiveLanes = TII->isGather4(MI) ? 4 : countPopulation(dmask);
11281 
11282   bool Packed = !Subtarget->hasUnpackedD16VMem();
11283 
11284   unsigned InitIdx =
11285       D16Val && Packed ? ((ActiveLanes + 1) >> 1) + 1 : ActiveLanes + 1;
11286 
11287   // Abandon attempt if the dst size isn't large enough
11288   // - this is in fact an error but this is picked up elsewhere and
11289   // reported correctly.
11290   uint32_t DstSize = TRI.getRegSizeInBits(*TII->getOpRegClass(MI, DstIdx)) / 32;
11291   if (DstSize < InitIdx)
11292     return;
11293 
11294   // Create a register for the intialization value.
11295   Register PrevDst = MRI.createVirtualRegister(TII->getOpRegClass(MI, DstIdx));
11296   unsigned NewDst = 0; // Final initialized value will be in here
11297 
11298   // If PRTStrictNull feature is enabled (the default) then initialize
11299   // all the result registers to 0, otherwise just the error indication
11300   // register (VGPRn+1)
11301   unsigned SizeLeft = Subtarget->usePRTStrictNull() ? InitIdx : 1;
11302   unsigned CurrIdx = Subtarget->usePRTStrictNull() ? 0 : (InitIdx - 1);
11303 
11304   BuildMI(MBB, MI, DL, TII->get(AMDGPU::IMPLICIT_DEF), PrevDst);
11305   for (; SizeLeft; SizeLeft--, CurrIdx++) {
11306     NewDst = MRI.createVirtualRegister(TII->getOpRegClass(MI, DstIdx));
11307     // Initialize dword
11308     Register SubReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
11309     BuildMI(MBB, MI, DL, TII->get(AMDGPU::V_MOV_B32_e32), SubReg)
11310       .addImm(0);
11311     // Insert into the super-reg
11312     BuildMI(MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewDst)
11313       .addReg(PrevDst)
11314       .addReg(SubReg)
11315       .addImm(SIRegisterInfo::getSubRegFromChannel(CurrIdx));
11316 
11317     PrevDst = NewDst;
11318   }
11319 
11320   // Add as an implicit operand
11321   MI.addOperand(MachineOperand::CreateReg(NewDst, false, true));
11322 
11323   // Tie the just added implicit operand to the dst
11324   MI.tieOperands(DstIdx, MI.getNumOperands() - 1);
11325 }
11326 
11327 /// Assign the register class depending on the number of
11328 /// bits set in the writemask
11329 void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI,
11330                                                      SDNode *Node) const {
11331   const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
11332 
11333   MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
11334 
11335   if (TII->isVOP3(MI.getOpcode())) {
11336     // Make sure constant bus requirements are respected.
11337     TII->legalizeOperandsVOP3(MRI, MI);
11338 
11339     // Prefer VGPRs over AGPRs in mAI instructions where possible.
11340     // This saves a chain-copy of registers and better ballance register
11341     // use between vgpr and agpr as agpr tuples tend to be big.
11342     if (const MCOperandInfo *OpInfo = MI.getDesc().OpInfo) {
11343       unsigned Opc = MI.getOpcode();
11344       const SIRegisterInfo *TRI = Subtarget->getRegisterInfo();
11345       for (auto I : { AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0),
11346                       AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1) }) {
11347         if (I == -1)
11348           break;
11349         MachineOperand &Op = MI.getOperand(I);
11350         if ((OpInfo[I].RegClass != llvm::AMDGPU::AV_64RegClassID &&
11351              OpInfo[I].RegClass != llvm::AMDGPU::AV_32RegClassID) ||
11352             !Op.getReg().isVirtual() || !TRI->isAGPR(MRI, Op.getReg()))
11353           continue;
11354         auto *Src = MRI.getUniqueVRegDef(Op.getReg());
11355         if (!Src || !Src->isCopy() ||
11356             !TRI->isSGPRReg(MRI, Src->getOperand(1).getReg()))
11357           continue;
11358         auto *RC = TRI->getRegClassForReg(MRI, Op.getReg());
11359         auto *NewRC = TRI->getEquivalentVGPRClass(RC);
11360         // All uses of agpr64 and agpr32 can also accept vgpr except for
11361         // v_accvgpr_read, but we do not produce agpr reads during selection,
11362         // so no use checks are needed.
11363         MRI.setRegClass(Op.getReg(), NewRC);
11364       }
11365     }
11366 
11367     return;
11368   }
11369 
11370   // Replace unused atomics with the no return version.
11371   int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI.getOpcode());
11372   if (NoRetAtomicOp != -1) {
11373     if (!Node->hasAnyUseOfValue(0)) {
11374       int CPolIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
11375                                                AMDGPU::OpName::cpol);
11376       if (CPolIdx != -1) {
11377         MachineOperand &CPol = MI.getOperand(CPolIdx);
11378         CPol.setImm(CPol.getImm() & ~AMDGPU::CPol::GLC);
11379       }
11380       MI.RemoveOperand(0);
11381       MI.setDesc(TII->get(NoRetAtomicOp));
11382       return;
11383     }
11384 
11385     // For mubuf_atomic_cmpswap, we need to have tablegen use an extract_subreg
11386     // instruction, because the return type of these instructions is a vec2 of
11387     // the memory type, so it can be tied to the input operand.
11388     // This means these instructions always have a use, so we need to add a
11389     // special case to check if the atomic has only one extract_subreg use,
11390     // which itself has no uses.
11391     if ((Node->hasNUsesOfValue(1, 0) &&
11392          Node->use_begin()->isMachineOpcode() &&
11393          Node->use_begin()->getMachineOpcode() == AMDGPU::EXTRACT_SUBREG &&
11394          !Node->use_begin()->hasAnyUseOfValue(0))) {
11395       Register Def = MI.getOperand(0).getReg();
11396 
11397       // Change this into a noret atomic.
11398       MI.setDesc(TII->get(NoRetAtomicOp));
11399       MI.RemoveOperand(0);
11400 
11401       // If we only remove the def operand from the atomic instruction, the
11402       // extract_subreg will be left with a use of a vreg without a def.
11403       // So we need to insert an implicit_def to avoid machine verifier
11404       // errors.
11405       BuildMI(*MI.getParent(), MI, MI.getDebugLoc(),
11406               TII->get(AMDGPU::IMPLICIT_DEF), Def);
11407     }
11408     return;
11409   }
11410 
11411   if (TII->isMIMG(MI) && !MI.mayStore())
11412     AddIMGInit(MI);
11413 }
11414 
11415 static SDValue buildSMovImm32(SelectionDAG &DAG, const SDLoc &DL,
11416                               uint64_t Val) {
11417   SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32);
11418   return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0);
11419 }
11420 
11421 MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG,
11422                                                 const SDLoc &DL,
11423                                                 SDValue Ptr) const {
11424   const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
11425 
11426   // Build the half of the subregister with the constants before building the
11427   // full 128-bit register. If we are building multiple resource descriptors,
11428   // this will allow CSEing of the 2-component register.
11429   const SDValue Ops0[] = {
11430     DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32),
11431     buildSMovImm32(DAG, DL, 0),
11432     DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
11433     buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32),
11434     DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32)
11435   };
11436 
11437   SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL,
11438                                                 MVT::v2i32, Ops0), 0);
11439 
11440   // Combine the constants and the pointer.
11441   const SDValue Ops1[] = {
11442     DAG.getTargetConstant(AMDGPU::SGPR_128RegClassID, DL, MVT::i32),
11443     Ptr,
11444     DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32),
11445     SubRegHi,
11446     DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32)
11447   };
11448 
11449   return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1);
11450 }
11451 
11452 /// Return a resource descriptor with the 'Add TID' bit enabled
11453 ///        The TID (Thread ID) is multiplied by the stride value (bits [61:48]
11454 ///        of the resource descriptor) to create an offset, which is added to
11455 ///        the resource pointer.
11456 MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG, const SDLoc &DL,
11457                                            SDValue Ptr, uint32_t RsrcDword1,
11458                                            uint64_t RsrcDword2And3) const {
11459   SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
11460   SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
11461   if (RsrcDword1) {
11462     PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi,
11463                                      DAG.getConstant(RsrcDword1, DL, MVT::i32)),
11464                     0);
11465   }
11466 
11467   SDValue DataLo = buildSMovImm32(DAG, DL,
11468                                   RsrcDword2And3 & UINT64_C(0xFFFFFFFF));
11469   SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32);
11470 
11471   const SDValue Ops[] = {
11472     DAG.getTargetConstant(AMDGPU::SGPR_128RegClassID, DL, MVT::i32),
11473     PtrLo,
11474     DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
11475     PtrHi,
11476     DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32),
11477     DataLo,
11478     DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32),
11479     DataHi,
11480     DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32)
11481   };
11482 
11483   return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
11484 }
11485 
11486 //===----------------------------------------------------------------------===//
11487 //                         SI Inline Assembly Support
11488 //===----------------------------------------------------------------------===//
11489 
11490 std::pair<unsigned, const TargetRegisterClass *>
11491 SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI_,
11492                                                StringRef Constraint,
11493                                                MVT VT) const {
11494   const SIRegisterInfo *TRI = static_cast<const SIRegisterInfo *>(TRI_);
11495 
11496   const TargetRegisterClass *RC = nullptr;
11497   if (Constraint.size() == 1) {
11498     const unsigned BitWidth = VT.getSizeInBits();
11499     switch (Constraint[0]) {
11500     default:
11501       return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
11502     case 's':
11503     case 'r':
11504       switch (BitWidth) {
11505       case 16:
11506         RC = &AMDGPU::SReg_32RegClass;
11507         break;
11508       case 64:
11509         RC = &AMDGPU::SGPR_64RegClass;
11510         break;
11511       default:
11512         RC = SIRegisterInfo::getSGPRClassForBitWidth(BitWidth);
11513         if (!RC)
11514           return std::make_pair(0U, nullptr);
11515         break;
11516       }
11517       break;
11518     case 'v':
11519       switch (BitWidth) {
11520       case 16:
11521         RC = &AMDGPU::VGPR_32RegClass;
11522         break;
11523       default:
11524         RC = TRI->getVGPRClassForBitWidth(BitWidth);
11525         if (!RC)
11526           return std::make_pair(0U, nullptr);
11527         break;
11528       }
11529       break;
11530     case 'a':
11531       if (!Subtarget->hasMAIInsts())
11532         break;
11533       switch (BitWidth) {
11534       case 16:
11535         RC = &AMDGPU::AGPR_32RegClass;
11536         break;
11537       default:
11538         RC = TRI->getAGPRClassForBitWidth(BitWidth);
11539         if (!RC)
11540           return std::make_pair(0U, nullptr);
11541         break;
11542       }
11543       break;
11544     }
11545     // We actually support i128, i16 and f16 as inline parameters
11546     // even if they are not reported as legal
11547     if (RC && (isTypeLegal(VT) || VT.SimpleTy == MVT::i128 ||
11548                VT.SimpleTy == MVT::i16 || VT.SimpleTy == MVT::f16))
11549       return std::make_pair(0U, RC);
11550   }
11551 
11552   if (Constraint.size() > 1) {
11553     if (Constraint[1] == 'v') {
11554       RC = &AMDGPU::VGPR_32RegClass;
11555     } else if (Constraint[1] == 's') {
11556       RC = &AMDGPU::SGPR_32RegClass;
11557     } else if (Constraint[1] == 'a') {
11558       RC = &AMDGPU::AGPR_32RegClass;
11559     }
11560 
11561     if (RC) {
11562       uint32_t Idx;
11563       bool Failed = Constraint.substr(2).getAsInteger(10, Idx);
11564       if (!Failed && Idx < RC->getNumRegs())
11565         return std::make_pair(RC->getRegister(Idx), RC);
11566     }
11567   }
11568 
11569   // FIXME: Returns VS_32 for physical SGPR constraints
11570   return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
11571 }
11572 
11573 static bool isImmConstraint(StringRef Constraint) {
11574   if (Constraint.size() == 1) {
11575     switch (Constraint[0]) {
11576     default: break;
11577     case 'I':
11578     case 'J':
11579     case 'A':
11580     case 'B':
11581     case 'C':
11582       return true;
11583     }
11584   } else if (Constraint == "DA" ||
11585              Constraint == "DB") {
11586     return true;
11587   }
11588   return false;
11589 }
11590 
11591 SITargetLowering::ConstraintType
11592 SITargetLowering::getConstraintType(StringRef Constraint) const {
11593   if (Constraint.size() == 1) {
11594     switch (Constraint[0]) {
11595     default: break;
11596     case 's':
11597     case 'v':
11598     case 'a':
11599       return C_RegisterClass;
11600     }
11601   }
11602   if (isImmConstraint(Constraint)) {
11603     return C_Other;
11604   }
11605   return TargetLowering::getConstraintType(Constraint);
11606 }
11607 
11608 static uint64_t clearUnusedBits(uint64_t Val, unsigned Size) {
11609   if (!AMDGPU::isInlinableIntLiteral(Val)) {
11610     Val = Val & maskTrailingOnes<uint64_t>(Size);
11611   }
11612   return Val;
11613 }
11614 
11615 void SITargetLowering::LowerAsmOperandForConstraint(SDValue Op,
11616                                                     std::string &Constraint,
11617                                                     std::vector<SDValue> &Ops,
11618                                                     SelectionDAG &DAG) const {
11619   if (isImmConstraint(Constraint)) {
11620     uint64_t Val;
11621     if (getAsmOperandConstVal(Op, Val) &&
11622         checkAsmConstraintVal(Op, Constraint, Val)) {
11623       Val = clearUnusedBits(Val, Op.getScalarValueSizeInBits());
11624       Ops.push_back(DAG.getTargetConstant(Val, SDLoc(Op), MVT::i64));
11625     }
11626   } else {
11627     TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
11628   }
11629 }
11630 
11631 bool SITargetLowering::getAsmOperandConstVal(SDValue Op, uint64_t &Val) const {
11632   unsigned Size = Op.getScalarValueSizeInBits();
11633   if (Size > 64)
11634     return false;
11635 
11636   if (Size == 16 && !Subtarget->has16BitInsts())
11637     return false;
11638 
11639   if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
11640     Val = C->getSExtValue();
11641     return true;
11642   }
11643   if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op)) {
11644     Val = C->getValueAPF().bitcastToAPInt().getSExtValue();
11645     return true;
11646   }
11647   if (BuildVectorSDNode *V = dyn_cast<BuildVectorSDNode>(Op)) {
11648     if (Size != 16 || Op.getNumOperands() != 2)
11649       return false;
11650     if (Op.getOperand(0).isUndef() || Op.getOperand(1).isUndef())
11651       return false;
11652     if (ConstantSDNode *C = V->getConstantSplatNode()) {
11653       Val = C->getSExtValue();
11654       return true;
11655     }
11656     if (ConstantFPSDNode *C = V->getConstantFPSplatNode()) {
11657       Val = C->getValueAPF().bitcastToAPInt().getSExtValue();
11658       return true;
11659     }
11660   }
11661 
11662   return false;
11663 }
11664 
11665 bool SITargetLowering::checkAsmConstraintVal(SDValue Op,
11666                                              const std::string &Constraint,
11667                                              uint64_t Val) const {
11668   if (Constraint.size() == 1) {
11669     switch (Constraint[0]) {
11670     case 'I':
11671       return AMDGPU::isInlinableIntLiteral(Val);
11672     case 'J':
11673       return isInt<16>(Val);
11674     case 'A':
11675       return checkAsmConstraintValA(Op, Val);
11676     case 'B':
11677       return isInt<32>(Val);
11678     case 'C':
11679       return isUInt<32>(clearUnusedBits(Val, Op.getScalarValueSizeInBits())) ||
11680              AMDGPU::isInlinableIntLiteral(Val);
11681     default:
11682       break;
11683     }
11684   } else if (Constraint.size() == 2) {
11685     if (Constraint == "DA") {
11686       int64_t HiBits = static_cast<int32_t>(Val >> 32);
11687       int64_t LoBits = static_cast<int32_t>(Val);
11688       return checkAsmConstraintValA(Op, HiBits, 32) &&
11689              checkAsmConstraintValA(Op, LoBits, 32);
11690     }
11691     if (Constraint == "DB") {
11692       return true;
11693     }
11694   }
11695   llvm_unreachable("Invalid asm constraint");
11696 }
11697 
11698 bool SITargetLowering::checkAsmConstraintValA(SDValue Op,
11699                                               uint64_t Val,
11700                                               unsigned MaxSize) const {
11701   unsigned Size = std::min<unsigned>(Op.getScalarValueSizeInBits(), MaxSize);
11702   bool HasInv2Pi = Subtarget->hasInv2PiInlineImm();
11703   if ((Size == 16 && AMDGPU::isInlinableLiteral16(Val, HasInv2Pi)) ||
11704       (Size == 32 && AMDGPU::isInlinableLiteral32(Val, HasInv2Pi)) ||
11705       (Size == 64 && AMDGPU::isInlinableLiteral64(Val, HasInv2Pi))) {
11706     return true;
11707   }
11708   return false;
11709 }
11710 
11711 static int getAlignedAGPRClassID(unsigned UnalignedClassID) {
11712   switch (UnalignedClassID) {
11713   case AMDGPU::VReg_64RegClassID:
11714     return AMDGPU::VReg_64_Align2RegClassID;
11715   case AMDGPU::VReg_96RegClassID:
11716     return AMDGPU::VReg_96_Align2RegClassID;
11717   case AMDGPU::VReg_128RegClassID:
11718     return AMDGPU::VReg_128_Align2RegClassID;
11719   case AMDGPU::VReg_160RegClassID:
11720     return AMDGPU::VReg_160_Align2RegClassID;
11721   case AMDGPU::VReg_192RegClassID:
11722     return AMDGPU::VReg_192_Align2RegClassID;
11723   case AMDGPU::VReg_256RegClassID:
11724     return AMDGPU::VReg_256_Align2RegClassID;
11725   case AMDGPU::VReg_512RegClassID:
11726     return AMDGPU::VReg_512_Align2RegClassID;
11727   case AMDGPU::VReg_1024RegClassID:
11728     return AMDGPU::VReg_1024_Align2RegClassID;
11729   case AMDGPU::AReg_64RegClassID:
11730     return AMDGPU::AReg_64_Align2RegClassID;
11731   case AMDGPU::AReg_96RegClassID:
11732     return AMDGPU::AReg_96_Align2RegClassID;
11733   case AMDGPU::AReg_128RegClassID:
11734     return AMDGPU::AReg_128_Align2RegClassID;
11735   case AMDGPU::AReg_160RegClassID:
11736     return AMDGPU::AReg_160_Align2RegClassID;
11737   case AMDGPU::AReg_192RegClassID:
11738     return AMDGPU::AReg_192_Align2RegClassID;
11739   case AMDGPU::AReg_256RegClassID:
11740     return AMDGPU::AReg_256_Align2RegClassID;
11741   case AMDGPU::AReg_512RegClassID:
11742     return AMDGPU::AReg_512_Align2RegClassID;
11743   case AMDGPU::AReg_1024RegClassID:
11744     return AMDGPU::AReg_1024_Align2RegClassID;
11745   default:
11746     return -1;
11747   }
11748 }
11749 
11750 // Figure out which registers should be reserved for stack access. Only after
11751 // the function is legalized do we know all of the non-spill stack objects or if
11752 // calls are present.
11753 void SITargetLowering::finalizeLowering(MachineFunction &MF) const {
11754   MachineRegisterInfo &MRI = MF.getRegInfo();
11755   SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
11756   const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
11757   const SIRegisterInfo *TRI = Subtarget->getRegisterInfo();
11758   const SIInstrInfo *TII = ST.getInstrInfo();
11759 
11760   if (Info->isEntryFunction()) {
11761     // Callable functions have fixed registers used for stack access.
11762     reservePrivateMemoryRegs(getTargetMachine(), MF, *TRI, *Info);
11763   }
11764 
11765   assert(!TRI->isSubRegister(Info->getScratchRSrcReg(),
11766                              Info->getStackPtrOffsetReg()));
11767   if (Info->getStackPtrOffsetReg() != AMDGPU::SP_REG)
11768     MRI.replaceRegWith(AMDGPU::SP_REG, Info->getStackPtrOffsetReg());
11769 
11770   // We need to worry about replacing the default register with itself in case
11771   // of MIR testcases missing the MFI.
11772   if (Info->getScratchRSrcReg() != AMDGPU::PRIVATE_RSRC_REG)
11773     MRI.replaceRegWith(AMDGPU::PRIVATE_RSRC_REG, Info->getScratchRSrcReg());
11774 
11775   if (Info->getFrameOffsetReg() != AMDGPU::FP_REG)
11776     MRI.replaceRegWith(AMDGPU::FP_REG, Info->getFrameOffsetReg());
11777 
11778   Info->limitOccupancy(MF);
11779 
11780   if (ST.isWave32() && !MF.empty()) {
11781     for (auto &MBB : MF) {
11782       for (auto &MI : MBB) {
11783         TII->fixImplicitOperands(MI);
11784       }
11785     }
11786   }
11787 
11788   // FIXME: This is a hack to fixup AGPR classes to use the properly aligned
11789   // classes if required. Ideally the register class constraints would differ
11790   // per-subtarget, but there's no easy way to achieve that right now. This is
11791   // not a problem for VGPRs because the correctly aligned VGPR class is implied
11792   // from using them as the register class for legal types.
11793   if (ST.needsAlignedVGPRs()) {
11794     for (unsigned I = 0, E = MRI.getNumVirtRegs(); I != E; ++I) {
11795       const Register Reg = Register::index2VirtReg(I);
11796       const TargetRegisterClass *RC = MRI.getRegClassOrNull(Reg);
11797       if (!RC)
11798         continue;
11799       int NewClassID = getAlignedAGPRClassID(RC->getID());
11800       if (NewClassID != -1)
11801         MRI.setRegClass(Reg, TRI->getRegClass(NewClassID));
11802     }
11803   }
11804 
11805   TargetLoweringBase::finalizeLowering(MF);
11806 
11807   // Allocate a VGPR for future SGPR Spill if
11808   // "amdgpu-reserve-vgpr-for-sgpr-spill" option is used
11809   // FIXME: We won't need this hack if we split SGPR allocation from VGPR
11810   if (VGPRReserveforSGPRSpill && TRI->spillSGPRToVGPR() &&
11811       !Info->VGPRReservedForSGPRSpill && !Info->isEntryFunction())
11812     Info->reserveVGPRforSGPRSpills(MF);
11813 }
11814 
11815 void SITargetLowering::computeKnownBitsForFrameIndex(
11816   const int FI, KnownBits &Known, const MachineFunction &MF) const {
11817   TargetLowering::computeKnownBitsForFrameIndex(FI, Known, MF);
11818 
11819   // Set the high bits to zero based on the maximum allowed scratch size per
11820   // wave. We can't use vaddr in MUBUF instructions if we don't know the address
11821   // calculation won't overflow, so assume the sign bit is never set.
11822   Known.Zero.setHighBits(getSubtarget()->getKnownHighZeroBitsForFrameIndex());
11823 }
11824 
11825 static void knownBitsForWorkitemID(const GCNSubtarget &ST, GISelKnownBits &KB,
11826                                    KnownBits &Known, unsigned Dim) {
11827   unsigned MaxValue =
11828       ST.getMaxWorkitemID(KB.getMachineFunction().getFunction(), Dim);
11829   Known.Zero.setHighBits(countLeadingZeros(MaxValue));
11830 }
11831 
11832 void SITargetLowering::computeKnownBitsForTargetInstr(
11833     GISelKnownBits &KB, Register R, KnownBits &Known, const APInt &DemandedElts,
11834     const MachineRegisterInfo &MRI, unsigned Depth) const {
11835   const MachineInstr *MI = MRI.getVRegDef(R);
11836   switch (MI->getOpcode()) {
11837   case AMDGPU::G_INTRINSIC: {
11838     switch (MI->getIntrinsicID()) {
11839     case Intrinsic::amdgcn_workitem_id_x:
11840       knownBitsForWorkitemID(*getSubtarget(), KB, Known, 0);
11841       break;
11842     case Intrinsic::amdgcn_workitem_id_y:
11843       knownBitsForWorkitemID(*getSubtarget(), KB, Known, 1);
11844       break;
11845     case Intrinsic::amdgcn_workitem_id_z:
11846       knownBitsForWorkitemID(*getSubtarget(), KB, Known, 2);
11847       break;
11848     case Intrinsic::amdgcn_mbcnt_lo:
11849     case Intrinsic::amdgcn_mbcnt_hi: {
11850       // These return at most the wavefront size - 1.
11851       unsigned Size = MRI.getType(R).getSizeInBits();
11852       Known.Zero.setHighBits(Size - getSubtarget()->getWavefrontSizeLog2());
11853       break;
11854     }
11855     case Intrinsic::amdgcn_groupstaticsize: {
11856       // We can report everything over the maximum size as 0. We can't report
11857       // based on the actual size because we don't know if it's accurate or not
11858       // at any given point.
11859       Known.Zero.setHighBits(countLeadingZeros(getSubtarget()->getLocalMemorySize()));
11860       break;
11861     }
11862     }
11863     break;
11864   }
11865   case AMDGPU::G_AMDGPU_BUFFER_LOAD_UBYTE:
11866     Known.Zero.setHighBits(24);
11867     break;
11868   case AMDGPU::G_AMDGPU_BUFFER_LOAD_USHORT:
11869     Known.Zero.setHighBits(16);
11870     break;
11871   }
11872 }
11873 
11874 Align SITargetLowering::computeKnownAlignForTargetInstr(
11875   GISelKnownBits &KB, Register R, const MachineRegisterInfo &MRI,
11876   unsigned Depth) const {
11877   const MachineInstr *MI = MRI.getVRegDef(R);
11878   switch (MI->getOpcode()) {
11879   case AMDGPU::G_INTRINSIC:
11880   case AMDGPU::G_INTRINSIC_W_SIDE_EFFECTS: {
11881     // FIXME: Can this move to generic code? What about the case where the call
11882     // site specifies a lower alignment?
11883     Intrinsic::ID IID = MI->getIntrinsicID();
11884     LLVMContext &Ctx = KB.getMachineFunction().getFunction().getContext();
11885     AttributeList Attrs = Intrinsic::getAttributes(Ctx, IID);
11886     if (MaybeAlign RetAlign = Attrs.getRetAlignment())
11887       return *RetAlign;
11888     return Align(1);
11889   }
11890   default:
11891     return Align(1);
11892   }
11893 }
11894 
11895 Align SITargetLowering::getPrefLoopAlignment(MachineLoop *ML) const {
11896   const Align PrefAlign = TargetLowering::getPrefLoopAlignment(ML);
11897   const Align CacheLineAlign = Align(64);
11898 
11899   // Pre-GFX10 target did not benefit from loop alignment
11900   if (!ML || DisableLoopAlignment ||
11901       (getSubtarget()->getGeneration() < AMDGPUSubtarget::GFX10) ||
11902       getSubtarget()->hasInstFwdPrefetchBug())
11903     return PrefAlign;
11904 
11905   // On GFX10 I$ is 4 x 64 bytes cache lines.
11906   // By default prefetcher keeps one cache line behind and reads two ahead.
11907   // We can modify it with S_INST_PREFETCH for larger loops to have two lines
11908   // behind and one ahead.
11909   // Therefor we can benefit from aligning loop headers if loop fits 192 bytes.
11910   // If loop fits 64 bytes it always spans no more than two cache lines and
11911   // does not need an alignment.
11912   // Else if loop is less or equal 128 bytes we do not need to modify prefetch,
11913   // Else if loop is less or equal 192 bytes we need two lines behind.
11914 
11915   const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
11916   const MachineBasicBlock *Header = ML->getHeader();
11917   if (Header->getAlignment() != PrefAlign)
11918     return Header->getAlignment(); // Already processed.
11919 
11920   unsigned LoopSize = 0;
11921   for (const MachineBasicBlock *MBB : ML->blocks()) {
11922     // If inner loop block is aligned assume in average half of the alignment
11923     // size to be added as nops.
11924     if (MBB != Header)
11925       LoopSize += MBB->getAlignment().value() / 2;
11926 
11927     for (const MachineInstr &MI : *MBB) {
11928       LoopSize += TII->getInstSizeInBytes(MI);
11929       if (LoopSize > 192)
11930         return PrefAlign;
11931     }
11932   }
11933 
11934   if (LoopSize <= 64)
11935     return PrefAlign;
11936 
11937   if (LoopSize <= 128)
11938     return CacheLineAlign;
11939 
11940   // If any of parent loops is surrounded by prefetch instructions do not
11941   // insert new for inner loop, which would reset parent's settings.
11942   for (MachineLoop *P = ML->getParentLoop(); P; P = P->getParentLoop()) {
11943     if (MachineBasicBlock *Exit = P->getExitBlock()) {
11944       auto I = Exit->getFirstNonDebugInstr();
11945       if (I != Exit->end() && I->getOpcode() == AMDGPU::S_INST_PREFETCH)
11946         return CacheLineAlign;
11947     }
11948   }
11949 
11950   MachineBasicBlock *Pre = ML->getLoopPreheader();
11951   MachineBasicBlock *Exit = ML->getExitBlock();
11952 
11953   if (Pre && Exit) {
11954     BuildMI(*Pre, Pre->getFirstTerminator(), DebugLoc(),
11955             TII->get(AMDGPU::S_INST_PREFETCH))
11956       .addImm(1); // prefetch 2 lines behind PC
11957 
11958     BuildMI(*Exit, Exit->getFirstNonDebugInstr(), DebugLoc(),
11959             TII->get(AMDGPU::S_INST_PREFETCH))
11960       .addImm(2); // prefetch 1 line behind PC
11961   }
11962 
11963   return CacheLineAlign;
11964 }
11965 
11966 LLVM_ATTRIBUTE_UNUSED
11967 static bool isCopyFromRegOfInlineAsm(const SDNode *N) {
11968   assert(N->getOpcode() == ISD::CopyFromReg);
11969   do {
11970     // Follow the chain until we find an INLINEASM node.
11971     N = N->getOperand(0).getNode();
11972     if (N->getOpcode() == ISD::INLINEASM ||
11973         N->getOpcode() == ISD::INLINEASM_BR)
11974       return true;
11975   } while (N->getOpcode() == ISD::CopyFromReg);
11976   return false;
11977 }
11978 
11979 bool SITargetLowering::isSDNodeSourceOfDivergence(
11980     const SDNode *N, FunctionLoweringInfo *FLI,
11981     LegacyDivergenceAnalysis *KDA) const {
11982   switch (N->getOpcode()) {
11983   case ISD::CopyFromReg: {
11984     const RegisterSDNode *R = cast<RegisterSDNode>(N->getOperand(1));
11985     const MachineRegisterInfo &MRI = FLI->MF->getRegInfo();
11986     const SIRegisterInfo *TRI = Subtarget->getRegisterInfo();
11987     Register Reg = R->getReg();
11988 
11989     // FIXME: Why does this need to consider isLiveIn?
11990     if (Reg.isPhysical() || MRI.isLiveIn(Reg))
11991       return !TRI->isSGPRReg(MRI, Reg);
11992 
11993     if (const Value *V = FLI->getValueFromVirtualReg(R->getReg()))
11994       return KDA->isDivergent(V);
11995 
11996     assert(Reg == FLI->DemoteRegister || isCopyFromRegOfInlineAsm(N));
11997     return !TRI->isSGPRReg(MRI, Reg);
11998   }
11999   case ISD::LOAD: {
12000     const LoadSDNode *L = cast<LoadSDNode>(N);
12001     unsigned AS = L->getAddressSpace();
12002     // A flat load may access private memory.
12003     return AS == AMDGPUAS::PRIVATE_ADDRESS || AS == AMDGPUAS::FLAT_ADDRESS;
12004   }
12005   case ISD::CALLSEQ_END:
12006     return true;
12007   case ISD::INTRINSIC_WO_CHAIN:
12008     return AMDGPU::isIntrinsicSourceOfDivergence(
12009         cast<ConstantSDNode>(N->getOperand(0))->getZExtValue());
12010   case ISD::INTRINSIC_W_CHAIN:
12011     return AMDGPU::isIntrinsicSourceOfDivergence(
12012         cast<ConstantSDNode>(N->getOperand(1))->getZExtValue());
12013   case AMDGPUISD::ATOMIC_CMP_SWAP:
12014   case AMDGPUISD::ATOMIC_INC:
12015   case AMDGPUISD::ATOMIC_DEC:
12016   case AMDGPUISD::ATOMIC_LOAD_FMIN:
12017   case AMDGPUISD::ATOMIC_LOAD_FMAX:
12018   case AMDGPUISD::BUFFER_ATOMIC_SWAP:
12019   case AMDGPUISD::BUFFER_ATOMIC_ADD:
12020   case AMDGPUISD::BUFFER_ATOMIC_SUB:
12021   case AMDGPUISD::BUFFER_ATOMIC_SMIN:
12022   case AMDGPUISD::BUFFER_ATOMIC_UMIN:
12023   case AMDGPUISD::BUFFER_ATOMIC_SMAX:
12024   case AMDGPUISD::BUFFER_ATOMIC_UMAX:
12025   case AMDGPUISD::BUFFER_ATOMIC_AND:
12026   case AMDGPUISD::BUFFER_ATOMIC_OR:
12027   case AMDGPUISD::BUFFER_ATOMIC_XOR:
12028   case AMDGPUISD::BUFFER_ATOMIC_INC:
12029   case AMDGPUISD::BUFFER_ATOMIC_DEC:
12030   case AMDGPUISD::BUFFER_ATOMIC_CMPSWAP:
12031   case AMDGPUISD::BUFFER_ATOMIC_CSUB:
12032   case AMDGPUISD::BUFFER_ATOMIC_FADD:
12033   case AMDGPUISD::BUFFER_ATOMIC_FMIN:
12034   case AMDGPUISD::BUFFER_ATOMIC_FMAX:
12035     // Target-specific read-modify-write atomics are sources of divergence.
12036     return true;
12037   default:
12038     if (auto *A = dyn_cast<AtomicSDNode>(N)) {
12039       // Generic read-modify-write atomics are sources of divergence.
12040       return A->readMem() && A->writeMem();
12041     }
12042     return false;
12043   }
12044 }
12045 
12046 bool SITargetLowering::denormalsEnabledForType(const SelectionDAG &DAG,
12047                                                EVT VT) const {
12048   switch (VT.getScalarType().getSimpleVT().SimpleTy) {
12049   case MVT::f32:
12050     return hasFP32Denormals(DAG.getMachineFunction());
12051   case MVT::f64:
12052   case MVT::f16:
12053     return hasFP64FP16Denormals(DAG.getMachineFunction());
12054   default:
12055     return false;
12056   }
12057 }
12058 
12059 bool SITargetLowering::denormalsEnabledForType(LLT Ty,
12060                                                MachineFunction &MF) const {
12061   switch (Ty.getScalarSizeInBits()) {
12062   case 32:
12063     return hasFP32Denormals(MF);
12064   case 64:
12065   case 16:
12066     return hasFP64FP16Denormals(MF);
12067   default:
12068     return false;
12069   }
12070 }
12071 
12072 bool SITargetLowering::isKnownNeverNaNForTargetNode(SDValue Op,
12073                                                     const SelectionDAG &DAG,
12074                                                     bool SNaN,
12075                                                     unsigned Depth) const {
12076   if (Op.getOpcode() == AMDGPUISD::CLAMP) {
12077     const MachineFunction &MF = DAG.getMachineFunction();
12078     const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
12079 
12080     if (Info->getMode().DX10Clamp)
12081       return true; // Clamped to 0.
12082     return DAG.isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
12083   }
12084 
12085   return AMDGPUTargetLowering::isKnownNeverNaNForTargetNode(Op, DAG,
12086                                                             SNaN, Depth);
12087 }
12088 
12089 // Global FP atomic instructions have a hardcoded FP mode and do not support
12090 // FP32 denormals, and only support v2f16 denormals.
12091 static bool fpModeMatchesGlobalFPAtomicMode(const AtomicRMWInst *RMW) {
12092   const fltSemantics &Flt = RMW->getType()->getScalarType()->getFltSemantics();
12093   auto DenormMode = RMW->getParent()->getParent()->getDenormalMode(Flt);
12094   if (&Flt == &APFloat::IEEEsingle())
12095     return DenormMode == DenormalMode::getPreserveSign();
12096   return DenormMode == DenormalMode::getIEEE();
12097 }
12098 
12099 TargetLowering::AtomicExpansionKind
12100 SITargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *RMW) const {
12101   switch (RMW->getOperation()) {
12102   case AtomicRMWInst::FAdd: {
12103     Type *Ty = RMW->getType();
12104 
12105     // We don't have a way to support 16-bit atomics now, so just leave them
12106     // as-is.
12107     if (Ty->isHalfTy())
12108       return AtomicExpansionKind::None;
12109 
12110     if (!Ty->isFloatTy() && (!Subtarget->hasGFX90AInsts() || !Ty->isDoubleTy()))
12111       return AtomicExpansionKind::CmpXChg;
12112 
12113     // TODO: Do have these for flat. Older targets also had them for buffers.
12114     unsigned AS = RMW->getPointerAddressSpace();
12115 
12116     if ((AS == AMDGPUAS::GLOBAL_ADDRESS || AS == AMDGPUAS::FLAT_ADDRESS) &&
12117          Subtarget->hasAtomicFaddInsts()) {
12118       // The amdgpu-unsafe-fp-atomics attribute enables generation of unsafe
12119       // floating point atomic instructions. May generate more efficient code,
12120       // but may not respect rounding and denormal modes, and may give incorrect
12121       // results for certain memory destinations.
12122       if (!fpModeMatchesGlobalFPAtomicMode(RMW) &&
12123           RMW->getFunction()
12124                   ->getFnAttribute("amdgpu-unsafe-fp-atomics")
12125                   .getValueAsString() != "true")
12126         return AtomicExpansionKind::CmpXChg;
12127 
12128       if (Subtarget->hasGFX90AInsts()) {
12129         auto SSID = RMW->getSyncScopeID();
12130         if (SSID == SyncScope::System ||
12131             SSID == RMW->getContext().getOrInsertSyncScopeID("one-as"))
12132           return AtomicExpansionKind::CmpXChg;
12133 
12134         return (Ty->isFloatTy() && AS == AMDGPUAS::FLAT_ADDRESS) ?
12135           AtomicExpansionKind::CmpXChg : AtomicExpansionKind::None;
12136       }
12137 
12138       if (!Subtarget->hasGFX90AInsts() && AS != AMDGPUAS::GLOBAL_ADDRESS)
12139         return AtomicExpansionKind::CmpXChg;
12140 
12141       return RMW->use_empty() ? AtomicExpansionKind::None :
12142                                 AtomicExpansionKind::CmpXChg;
12143     }
12144 
12145     // DS FP atomics do repect the denormal mode, but the rounding mode is fixed
12146     // to round-to-nearest-even.
12147     // The only exception is DS_ADD_F64 which never flushes regardless of mode.
12148     if (AS == AMDGPUAS::LOCAL_ADDRESS && Subtarget->hasLDSFPAtomics()) {
12149       return (Ty->isDoubleTy() && !fpModeMatchesGlobalFPAtomicMode(RMW)) ?
12150         AtomicExpansionKind::CmpXChg : AtomicExpansionKind::None;
12151     }
12152 
12153     return AtomicExpansionKind::CmpXChg;
12154   }
12155   default:
12156     break;
12157   }
12158 
12159   return AMDGPUTargetLowering::shouldExpandAtomicRMWInIR(RMW);
12160 }
12161 
12162 const TargetRegisterClass *
12163 SITargetLowering::getRegClassFor(MVT VT, bool isDivergent) const {
12164   const TargetRegisterClass *RC = TargetLoweringBase::getRegClassFor(VT, false);
12165   const SIRegisterInfo *TRI = Subtarget->getRegisterInfo();
12166   if (RC == &AMDGPU::VReg_1RegClass && !isDivergent)
12167     return Subtarget->getWavefrontSize() == 64 ? &AMDGPU::SReg_64RegClass
12168                                                : &AMDGPU::SReg_32RegClass;
12169   if (!TRI->isSGPRClass(RC) && !isDivergent)
12170     return TRI->getEquivalentSGPRClass(RC);
12171   else if (TRI->isSGPRClass(RC) && isDivergent)
12172     return TRI->getEquivalentVGPRClass(RC);
12173 
12174   return RC;
12175 }
12176 
12177 // FIXME: This is a workaround for DivergenceAnalysis not understanding always
12178 // uniform values (as produced by the mask results of control flow intrinsics)
12179 // used outside of divergent blocks. The phi users need to also be treated as
12180 // always uniform.
12181 static bool hasCFUser(const Value *V, SmallPtrSet<const Value *, 16> &Visited,
12182                       unsigned WaveSize) {
12183   // FIXME: We asssume we never cast the mask results of a control flow
12184   // intrinsic.
12185   // Early exit if the type won't be consistent as a compile time hack.
12186   IntegerType *IT = dyn_cast<IntegerType>(V->getType());
12187   if (!IT || IT->getBitWidth() != WaveSize)
12188     return false;
12189 
12190   if (!isa<Instruction>(V))
12191     return false;
12192   if (!Visited.insert(V).second)
12193     return false;
12194   bool Result = false;
12195   for (auto U : V->users()) {
12196     if (const IntrinsicInst *Intrinsic = dyn_cast<IntrinsicInst>(U)) {
12197       if (V == U->getOperand(1)) {
12198         switch (Intrinsic->getIntrinsicID()) {
12199         default:
12200           Result = false;
12201           break;
12202         case Intrinsic::amdgcn_if_break:
12203         case Intrinsic::amdgcn_if:
12204         case Intrinsic::amdgcn_else:
12205           Result = true;
12206           break;
12207         }
12208       }
12209       if (V == U->getOperand(0)) {
12210         switch (Intrinsic->getIntrinsicID()) {
12211         default:
12212           Result = false;
12213           break;
12214         case Intrinsic::amdgcn_end_cf:
12215         case Intrinsic::amdgcn_loop:
12216           Result = true;
12217           break;
12218         }
12219       }
12220     } else {
12221       Result = hasCFUser(U, Visited, WaveSize);
12222     }
12223     if (Result)
12224       break;
12225   }
12226   return Result;
12227 }
12228 
12229 bool SITargetLowering::requiresUniformRegister(MachineFunction &MF,
12230                                                const Value *V) const {
12231   if (const CallInst *CI = dyn_cast<CallInst>(V)) {
12232     if (CI->isInlineAsm()) {
12233       // FIXME: This cannot give a correct answer. This should only trigger in
12234       // the case where inline asm returns mixed SGPR and VGPR results, used
12235       // outside the defining block. We don't have a specific result to
12236       // consider, so this assumes if any value is SGPR, the overall register
12237       // also needs to be SGPR.
12238       const SIRegisterInfo *SIRI = Subtarget->getRegisterInfo();
12239       TargetLowering::AsmOperandInfoVector TargetConstraints = ParseConstraints(
12240           MF.getDataLayout(), Subtarget->getRegisterInfo(), *CI);
12241       for (auto &TC : TargetConstraints) {
12242         if (TC.Type == InlineAsm::isOutput) {
12243           ComputeConstraintToUse(TC, SDValue());
12244           unsigned AssignedReg;
12245           const TargetRegisterClass *RC;
12246           std::tie(AssignedReg, RC) = getRegForInlineAsmConstraint(
12247               SIRI, TC.ConstraintCode, TC.ConstraintVT);
12248           if (RC) {
12249             MachineRegisterInfo &MRI = MF.getRegInfo();
12250             if (AssignedReg != 0 && SIRI->isSGPRReg(MRI, AssignedReg))
12251               return true;
12252             else if (SIRI->isSGPRClass(RC))
12253               return true;
12254           }
12255         }
12256       }
12257     }
12258   }
12259   SmallPtrSet<const Value *, 16> Visited;
12260   return hasCFUser(V, Visited, Subtarget->getWavefrontSize());
12261 }
12262 
12263 std::pair<InstructionCost, MVT>
12264 SITargetLowering::getTypeLegalizationCost(const DataLayout &DL,
12265                                           Type *Ty) const {
12266   std::pair<InstructionCost, MVT> Cost =
12267       TargetLoweringBase::getTypeLegalizationCost(DL, Ty);
12268   auto Size = DL.getTypeSizeInBits(Ty);
12269   // Maximum load or store can handle 8 dwords for scalar and 4 for
12270   // vector ALU. Let's assume anything above 8 dwords is expensive
12271   // even if legal.
12272   if (Size <= 256)
12273     return Cost;
12274 
12275   Cost.first = (Size + 255) / 256;
12276   return Cost;
12277 }
12278