LLVM 22.0.0git
SIISelLowering.cpp
Go to the documentation of this file.
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 "AMDGPULaneMaskUtils.h"
18#include "AMDGPUTargetMachine.h"
19#include "GCNSubtarget.h"
22#include "SIRegisterInfo.h"
23#include "llvm/ADT/APInt.h"
25#include "llvm/ADT/Statistic.h"
39#include "llvm/IR/IRBuilder.h"
41#include "llvm/IR/IntrinsicsAMDGPU.h"
42#include "llvm/IR/IntrinsicsR600.h"
43#include "llvm/IR/MDBuilder.h"
46#include "llvm/Support/ModRef.h"
48#include <optional>
49
50using namespace llvm;
51using namespace llvm::SDPatternMatch;
52
53#define DEBUG_TYPE "si-lower"
54
55STATISTIC(NumTailCalls, "Number of tail calls");
56
57static cl::opt<bool>
58 DisableLoopAlignment("amdgpu-disable-loop-alignment",
59 cl::desc("Do not align and prefetch loops"),
60 cl::init(false));
61
63 "amdgpu-use-divergent-register-indexing", cl::Hidden,
64 cl::desc("Use indirect register addressing for divergent indexes"),
65 cl::init(false));
66
69 return Info->getMode().FP32Denormals == DenormalMode::getPreserveSign();
70}
71
74 return Info->getMode().FP64FP16Denormals == DenormalMode::getPreserveSign();
75}
76
77static unsigned findFirstFreeSGPR(CCState &CCInfo) {
78 unsigned NumSGPRs = AMDGPU::SGPR_32RegClass.getNumRegs();
79 for (unsigned Reg = 0; Reg < NumSGPRs; ++Reg) {
80 if (!CCInfo.isAllocated(AMDGPU::SGPR0 + Reg)) {
81 return AMDGPU::SGPR0 + Reg;
82 }
83 }
84 llvm_unreachable("Cannot allocate sgpr");
85}
86
88 const GCNSubtarget &STI)
89 : AMDGPUTargetLowering(TM, STI), Subtarget(&STI) {
90 addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass);
91 addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass);
92
93 addRegisterClass(MVT::i32, &AMDGPU::SReg_32RegClass);
94 addRegisterClass(MVT::f32, &AMDGPU::VGPR_32RegClass);
95
96 addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass);
97
98 const SIRegisterInfo *TRI = STI.getRegisterInfo();
99 const TargetRegisterClass *V64RegClass = TRI->getVGPR64Class();
100
101 addRegisterClass(MVT::f64, V64RegClass);
102 addRegisterClass(MVT::v2f32, V64RegClass);
103 addRegisterClass(MVT::Untyped, V64RegClass);
104
105 addRegisterClass(MVT::v3i32, &AMDGPU::SGPR_96RegClass);
106 addRegisterClass(MVT::v3f32, &AMDGPU::VReg_96RegClass);
107
108 addRegisterClass(MVT::v2i64, &AMDGPU::SGPR_128RegClass);
109 addRegisterClass(MVT::v2f64, &AMDGPU::SGPR_128RegClass);
110
111 addRegisterClass(MVT::v4i32, &AMDGPU::SGPR_128RegClass);
112 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass);
113
114 addRegisterClass(MVT::v5i32, &AMDGPU::SGPR_160RegClass);
115 addRegisterClass(MVT::v5f32, &AMDGPU::VReg_160RegClass);
116
117 addRegisterClass(MVT::v6i32, &AMDGPU::SGPR_192RegClass);
118 addRegisterClass(MVT::v6f32, &AMDGPU::VReg_192RegClass);
119
120 addRegisterClass(MVT::v3i64, &AMDGPU::SGPR_192RegClass);
121 addRegisterClass(MVT::v3f64, &AMDGPU::VReg_192RegClass);
122
123 addRegisterClass(MVT::v7i32, &AMDGPU::SGPR_224RegClass);
124 addRegisterClass(MVT::v7f32, &AMDGPU::VReg_224RegClass);
125
126 addRegisterClass(MVT::v8i32, &AMDGPU::SGPR_256RegClass);
127 addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass);
128
129 addRegisterClass(MVT::v4i64, &AMDGPU::SGPR_256RegClass);
130 addRegisterClass(MVT::v4f64, &AMDGPU::VReg_256RegClass);
131
132 addRegisterClass(MVT::v9i32, &AMDGPU::SGPR_288RegClass);
133 addRegisterClass(MVT::v9f32, &AMDGPU::VReg_288RegClass);
134
135 addRegisterClass(MVT::v10i32, &AMDGPU::SGPR_320RegClass);
136 addRegisterClass(MVT::v10f32, &AMDGPU::VReg_320RegClass);
137
138 addRegisterClass(MVT::v11i32, &AMDGPU::SGPR_352RegClass);
139 addRegisterClass(MVT::v11f32, &AMDGPU::VReg_352RegClass);
140
141 addRegisterClass(MVT::v12i32, &AMDGPU::SGPR_384RegClass);
142 addRegisterClass(MVT::v12f32, &AMDGPU::VReg_384RegClass);
143
144 addRegisterClass(MVT::v16i32, &AMDGPU::SGPR_512RegClass);
145 addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass);
146
147 addRegisterClass(MVT::v8i64, &AMDGPU::SGPR_512RegClass);
148 addRegisterClass(MVT::v8f64, &AMDGPU::VReg_512RegClass);
149
150 addRegisterClass(MVT::v16i64, &AMDGPU::SGPR_1024RegClass);
151 addRegisterClass(MVT::v16f64, &AMDGPU::VReg_1024RegClass);
152
153 if (Subtarget->has16BitInsts()) {
154 if (Subtarget->useRealTrue16Insts()) {
155 addRegisterClass(MVT::i16, &AMDGPU::VGPR_16RegClass);
156 addRegisterClass(MVT::f16, &AMDGPU::VGPR_16RegClass);
157 addRegisterClass(MVT::bf16, &AMDGPU::VGPR_16RegClass);
158 } else {
159 addRegisterClass(MVT::i16, &AMDGPU::SReg_32RegClass);
160 addRegisterClass(MVT::f16, &AMDGPU::SReg_32RegClass);
161 addRegisterClass(MVT::bf16, &AMDGPU::SReg_32RegClass);
162 }
163
164 // Unless there are also VOP3P operations, not operations are really legal.
165 addRegisterClass(MVT::v2i16, &AMDGPU::SReg_32RegClass);
166 addRegisterClass(MVT::v2f16, &AMDGPU::SReg_32RegClass);
167 addRegisterClass(MVT::v2bf16, &AMDGPU::SReg_32RegClass);
168 addRegisterClass(MVT::v4i16, &AMDGPU::SReg_64RegClass);
169 addRegisterClass(MVT::v4f16, &AMDGPU::SReg_64RegClass);
170 addRegisterClass(MVT::v4bf16, &AMDGPU::SReg_64RegClass);
171 addRegisterClass(MVT::v8i16, &AMDGPU::SGPR_128RegClass);
172 addRegisterClass(MVT::v8f16, &AMDGPU::SGPR_128RegClass);
173 addRegisterClass(MVT::v8bf16, &AMDGPU::SGPR_128RegClass);
174 addRegisterClass(MVT::v16i16, &AMDGPU::SGPR_256RegClass);
175 addRegisterClass(MVT::v16f16, &AMDGPU::SGPR_256RegClass);
176 addRegisterClass(MVT::v16bf16, &AMDGPU::SGPR_256RegClass);
177 addRegisterClass(MVT::v32i16, &AMDGPU::SGPR_512RegClass);
178 addRegisterClass(MVT::v32f16, &AMDGPU::SGPR_512RegClass);
179 addRegisterClass(MVT::v32bf16, &AMDGPU::SGPR_512RegClass);
180 }
181
182 addRegisterClass(MVT::v32i32, &AMDGPU::VReg_1024RegClass);
183 addRegisterClass(MVT::v32f32, &AMDGPU::VReg_1024RegClass);
184
185 computeRegisterProperties(Subtarget->getRegisterInfo());
186
187 // The boolean content concept here is too inflexible. Compares only ever
188 // really produce a 1-bit result. Any copy/extend from these will turn into a
189 // select, and zext/1 or sext/-1 are equally cheap. Arbitrarily choose 0/1, as
190 // it's what most targets use.
193
194 // We need to custom lower vector stores from local memory
195 setOperationAction(ISD::LOAD,
196 {MVT::v2i32, MVT::v3i32, MVT::v4i32, MVT::v5i32,
197 MVT::v6i32, MVT::v7i32, MVT::v8i32, MVT::v9i32,
198 MVT::v10i32, MVT::v11i32, MVT::v12i32, MVT::v16i32,
199 MVT::i1, MVT::v32i32},
200 Custom);
201
202 setOperationAction(ISD::STORE,
203 {MVT::v2i32, MVT::v3i32, MVT::v4i32, MVT::v5i32,
204 MVT::v6i32, MVT::v7i32, MVT::v8i32, MVT::v9i32,
205 MVT::v10i32, MVT::v11i32, MVT::v12i32, MVT::v16i32,
206 MVT::i1, MVT::v32i32},
207 Custom);
208
209 if (isTypeLegal(MVT::bf16)) {
210 for (unsigned Opc :
212 ISD::FREM, ISD::FMA, ISD::FMINNUM, ISD::FMAXNUM,
213 ISD::FMINIMUM, ISD::FMAXIMUM, ISD::FSQRT, ISD::FCBRT,
214 ISD::FSIN, ISD::FCOS, ISD::FPOW, ISD::FPOWI,
215 ISD::FLDEXP, ISD::FFREXP, ISD::FLOG, ISD::FLOG2,
216 ISD::FLOG10, ISD::FEXP, ISD::FEXP2, ISD::FEXP10,
217 ISD::FCEIL, ISD::FTRUNC, ISD::FRINT, ISD::FNEARBYINT,
218 ISD::FROUND, ISD::FROUNDEVEN, ISD::FFLOOR, ISD::FCANONICALIZE,
219 ISD::SETCC}) {
220 // FIXME: The promoted to type shouldn't need to be explicit
221 setOperationAction(Opc, MVT::bf16, Promote);
222 AddPromotedToType(Opc, MVT::bf16, MVT::f32);
223 }
224
226
228 AddPromotedToType(ISD::SELECT, MVT::bf16, MVT::i16);
229
230 setOperationAction(ISD::FABS, MVT::bf16, Legal);
231 setOperationAction(ISD::FNEG, MVT::bf16, Legal);
233
234 // We only need to custom lower because we can't specify an action for bf16
235 // sources.
238 }
239
240 setTruncStoreAction(MVT::v2i32, MVT::v2i16, Expand);
241 setTruncStoreAction(MVT::v3i32, MVT::v3i16, Expand);
242 setTruncStoreAction(MVT::v4i32, MVT::v4i16, Expand);
243 setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand);
244 setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand);
245 setTruncStoreAction(MVT::v32i32, MVT::v32i16, Expand);
246 setTruncStoreAction(MVT::v2i32, MVT::v2i8, Expand);
247 setTruncStoreAction(MVT::v4i32, MVT::v4i8, Expand);
248 setTruncStoreAction(MVT::v8i32, MVT::v8i8, Expand);
249 setTruncStoreAction(MVT::v16i32, MVT::v16i8, Expand);
250 setTruncStoreAction(MVT::v32i32, MVT::v32i8, Expand);
251 setTruncStoreAction(MVT::v2i16, MVT::v2i8, Expand);
252 setTruncStoreAction(MVT::v4i16, MVT::v4i8, Expand);
253 setTruncStoreAction(MVT::v8i16, MVT::v8i8, Expand);
254 setTruncStoreAction(MVT::v16i16, MVT::v16i8, Expand);
255 setTruncStoreAction(MVT::v32i16, MVT::v32i8, Expand);
256
257 setTruncStoreAction(MVT::v3i64, MVT::v3i16, Expand);
258 setTruncStoreAction(MVT::v3i64, MVT::v3i32, Expand);
259 setTruncStoreAction(MVT::v4i64, MVT::v4i8, Expand);
260 setTruncStoreAction(MVT::v8i64, MVT::v8i8, Expand);
261 setTruncStoreAction(MVT::v8i64, MVT::v8i16, Expand);
262 setTruncStoreAction(MVT::v8i64, MVT::v8i32, Expand);
263 setTruncStoreAction(MVT::v16i64, MVT::v16i32, Expand);
264
265 setOperationAction(ISD::GlobalAddress, {MVT::i32, MVT::i64}, Custom);
266
270 AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64);
271
272 setOperationAction(ISD::FSQRT, {MVT::f32, MVT::f64}, Custom);
273
275 {MVT::f32, MVT::i32, MVT::i64, MVT::f64, MVT::i1}, Expand);
276
278 setOperationAction(ISD::SETCC, {MVT::v2i1, MVT::v4i1}, Expand);
279 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
280
282 {MVT::v2i32, MVT::v3i32, MVT::v4i32, MVT::v5i32,
283 MVT::v6i32, MVT::v7i32, MVT::v8i32, MVT::v9i32,
284 MVT::v10i32, MVT::v11i32, MVT::v12i32, MVT::v16i32},
285 Expand);
287 {MVT::v2f32, MVT::v3f32, MVT::v4f32, MVT::v5f32,
288 MVT::v6f32, MVT::v7f32, MVT::v8f32, MVT::v9f32,
289 MVT::v10f32, MVT::v11f32, MVT::v12f32, MVT::v16f32},
290 Expand);
291
293 {MVT::v2i1, MVT::v4i1, MVT::v2i8, MVT::v4i8, MVT::v2i16,
294 MVT::v3i16, MVT::v4i16, MVT::Other},
295 Custom);
296
297 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
298 setOperationAction(ISD::BR_CC,
299 {MVT::i1, MVT::i32, MVT::i64, MVT::f32, MVT::f64}, Expand);
300
302
304
306 Expand);
307
308#if 0
310#endif
311
312 // We only support LOAD/STORE and vector manipulation ops for vectors
313 // with > 4 elements.
314 for (MVT VT :
315 {MVT::v8i32, MVT::v8f32, MVT::v9i32, MVT::v9f32, MVT::v10i32,
316 MVT::v10f32, MVT::v11i32, MVT::v11f32, MVT::v12i32, MVT::v12f32,
317 MVT::v16i32, MVT::v16f32, MVT::v2i64, MVT::v2f64, MVT::v4i16,
318 MVT::v4f16, MVT::v4bf16, MVT::v3i64, MVT::v3f64, MVT::v6i32,
319 MVT::v6f32, MVT::v4i64, MVT::v4f64, MVT::v8i64, MVT::v8f64,
320 MVT::v8i16, MVT::v8f16, MVT::v8bf16, MVT::v16i16, MVT::v16f16,
321 MVT::v16bf16, MVT::v16i64, MVT::v16f64, MVT::v32i32, MVT::v32f32,
322 MVT::v32i16, MVT::v32f16, MVT::v32bf16}) {
323 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
324 switch (Op) {
325 case ISD::LOAD:
326 case ISD::STORE:
328 case ISD::BITCAST:
329 case ISD::UNDEF:
333 case ISD::IS_FPCLASS:
334 break;
339 break;
340 default:
342 break;
343 }
344 }
345 }
346
347 setOperationAction(ISD::FP_EXTEND, MVT::v4f32, Expand);
348
349 // TODO: For dynamic 64-bit vector inserts/extracts, should emit a pseudo that
350 // is expanded to avoid having two separate loops in case the index is a VGPR.
351
352 // Most operations are naturally 32-bit vector operations. We only support
353 // load and store of i64 vectors, so promote v2i64 vector operations to v4i32.
354 for (MVT Vec64 : {MVT::v2i64, MVT::v2f64}) {
356 AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v4i32);
357
359 AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v4i32);
360
362 AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v4i32);
363
365 AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v4i32);
366 }
367
368 for (MVT Vec64 : {MVT::v3i64, MVT::v3f64}) {
370 AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v6i32);
371
373 AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v6i32);
374
376 AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v6i32);
377
379 AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v6i32);
380 }
381
382 for (MVT Vec64 : {MVT::v4i64, MVT::v4f64}) {
384 AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v8i32);
385
387 AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v8i32);
388
390 AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v8i32);
391
393 AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v8i32);
394 }
395
396 for (MVT Vec64 : {MVT::v8i64, MVT::v8f64}) {
398 AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v16i32);
399
401 AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v16i32);
402
404 AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v16i32);
405
407 AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v16i32);
408 }
409
410 for (MVT Vec64 : {MVT::v16i64, MVT::v16f64}) {
412 AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v32i32);
413
415 AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v32i32);
416
418 AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v32i32);
419
421 AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v32i32);
422 }
423
425 {MVT::v4i32, MVT::v4f32, MVT::v8i32, MVT::v8f32,
426 MVT::v16i32, MVT::v16f32, MVT::v32i32, MVT::v32f32},
427 Custom);
428
429 if (Subtarget->hasPkMovB32()) {
430 // TODO: 16-bit element vectors should be legal with even aligned elements.
431 // TODO: Can be legal with wider source types than the result with
432 // subregister extracts.
433 setOperationAction(ISD::VECTOR_SHUFFLE, {MVT::v2i32, MVT::v2f32}, Legal);
434 }
435
437 // Prevent SELECT v2i32 from being implemented with the above bitwise ops and
438 // instead lower to cndmask in SITargetLowering::LowerSELECT().
440 // Enable MatchRotate to produce ISD::ROTR, which is later transformed to
441 // alignbit.
442 setOperationAction(ISD::ROTR, MVT::v2i32, Custom);
443
444 setOperationAction(ISD::BUILD_VECTOR, {MVT::v4f16, MVT::v4i16, MVT::v4bf16},
445 Custom);
446
447 // Avoid stack access for these.
448 // TODO: Generalize to more vector types.
450 {MVT::v2i16, MVT::v2f16, MVT::v2bf16, MVT::v2i8, MVT::v4i8,
451 MVT::v8i8, MVT::v4i16, MVT::v4f16, MVT::v4bf16},
452 Custom);
453
454 // Deal with vec3 vector operations when widened to vec4.
456 {MVT::v3i32, MVT::v3f32, MVT::v4i32, MVT::v4f32}, Custom);
457
458 // Deal with vec5/6/7 vector operations when widened to vec8.
460 {MVT::v5i32, MVT::v5f32, MVT::v6i32, MVT::v6f32,
461 MVT::v7i32, MVT::v7f32, MVT::v8i32, MVT::v8f32,
462 MVT::v9i32, MVT::v9f32, MVT::v10i32, MVT::v10f32,
463 MVT::v11i32, MVT::v11f32, MVT::v12i32, MVT::v12f32},
464 Custom);
465
466 // BUFFER/FLAT_ATOMIC_CMP_SWAP on GCN GPUs needs input marshalling,
467 // and output demarshalling
468 setOperationAction(ISD::ATOMIC_CMP_SWAP, {MVT::i32, MVT::i64}, Custom);
469
470 // We can't return success/failure, only the old value,
471 // let LLVM add the comparison
472 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, {MVT::i32, MVT::i64},
473 Expand);
474
475 setOperationAction(ISD::ADDRSPACECAST, {MVT::i32, MVT::i64}, Custom);
476
477 setOperationAction(ISD::BITREVERSE, {MVT::i32, MVT::i64}, Legal);
478
479 // FIXME: This should be narrowed to i32, but that only happens if i64 is
480 // illegal.
481 // FIXME: Should lower sub-i32 bswaps to bit-ops without v_perm_b32.
482 setOperationAction(ISD::BSWAP, {MVT::i64, MVT::i32}, Legal);
483
484 // On SI this is s_memtime and s_memrealtime on VI.
485 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal);
486
487 if (Subtarget->hasSMemRealTime() ||
488 Subtarget->getGeneration() >= AMDGPUSubtarget::GFX11)
489 setOperationAction(ISD::READSTEADYCOUNTER, MVT::i64, Legal);
490 setOperationAction({ISD::TRAP, ISD::DEBUGTRAP}, MVT::Other, Custom);
491
492 if (Subtarget->has16BitInsts()) {
493 setOperationAction({ISD::FPOW, ISD::FPOWI}, MVT::f16, Promote);
494 setOperationAction({ISD::FLOG, ISD::FEXP, ISD::FLOG10}, MVT::f16, Custom);
495 } else {
496 setOperationAction(ISD::FSQRT, MVT::f16, Custom);
497 }
498
499 if (Subtarget->hasMadMacF32Insts())
501
502 if (!Subtarget->hasBFI())
503 // fcopysign can be done in a single instruction with BFI.
504 setOperationAction(ISD::FCOPYSIGN, {MVT::f32, MVT::f64}, Expand);
505
506 if (!Subtarget->hasBCNT(32))
508
509 if (!Subtarget->hasBCNT(64))
511
512 if (Subtarget->hasFFBH())
514
515 if (Subtarget->hasFFBL())
517
518 // We only really have 32-bit BFE instructions (and 16-bit on VI).
519 //
520 // On SI+ there are 64-bit BFEs, but they are scalar only and there isn't any
521 // effort to match them now. We want this to be false for i64 cases when the
522 // extraction isn't restricted to the upper or lower half. Ideally we would
523 // have some pass reduce 64-bit extracts to 32-bit if possible. Extracts that
524 // span the midpoint are probably relatively rare, so don't worry about them
525 // for now.
526 if (Subtarget->hasBFE())
528
529 // Clamp modifier on add/sub
530 if (Subtarget->hasIntClamp())
532
533 if (Subtarget->hasAddNoCarry())
534 setOperationAction({ISD::SADDSAT, ISD::SSUBSAT}, {MVT::i16, MVT::i32},
535 Legal);
536
538 {ISD::FMINNUM, ISD::FMAXNUM, ISD::FMINIMUMNUM, ISD::FMAXIMUMNUM},
539 {MVT::f32, MVT::f64}, Custom);
540
541 // These are really only legal for ieee_mode functions. We should be avoiding
542 // them for functions that don't have ieee_mode enabled, so just say they are
543 // legal.
544 setOperationAction({ISD::FMINNUM_IEEE, ISD::FMAXNUM_IEEE},
545 {MVT::f32, MVT::f64}, Legal);
546
547 if (Subtarget->haveRoundOpsF64())
548 setOperationAction({ISD::FTRUNC, ISD::FCEIL, ISD::FROUNDEVEN}, MVT::f64,
549 Legal);
550 else
551 setOperationAction({ISD::FCEIL, ISD::FTRUNC, ISD::FROUNDEVEN, ISD::FFLOOR},
552 MVT::f64, Custom);
553
554 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
555 setOperationAction({ISD::FLDEXP, ISD::STRICT_FLDEXP}, {MVT::f32, MVT::f64},
556 Legal);
557 setOperationAction(ISD::FFREXP, {MVT::f32, MVT::f64}, Custom);
558
559 setOperationAction({ISD::FSIN, ISD::FCOS, ISD::FDIV}, MVT::f32, Custom);
561
562 setOperationAction(ISD::BF16_TO_FP, {MVT::i16, MVT::f32, MVT::f64}, Expand);
563 setOperationAction(ISD::FP_TO_BF16, {MVT::i16, MVT::f32, MVT::f64}, Expand);
564
565 // Custom lower these because we can't specify a rule based on an illegal
566 // source bf16.
567 setOperationAction({ISD::FP_EXTEND, ISD::STRICT_FP_EXTEND}, MVT::f32, Custom);
568 setOperationAction({ISD::FP_EXTEND, ISD::STRICT_FP_EXTEND}, MVT::f64, Custom);
569
570 if (Subtarget->has16BitInsts()) {
573 MVT::i16, Legal);
574
575 AddPromotedToType(ISD::SIGN_EXTEND, MVT::i16, MVT::i32);
576
578 MVT::i16, Expand);
579
583 ISD::CTPOP},
584 MVT::i16, Promote);
585
586 setOperationAction(ISD::LOAD, MVT::i16, Custom);
587
588 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
589
590 setOperationAction(ISD::FP16_TO_FP, MVT::i16, Promote);
591 AddPromotedToType(ISD::FP16_TO_FP, MVT::i16, MVT::i32);
592 setOperationAction(ISD::FP_TO_FP16, MVT::i16, Promote);
593 AddPromotedToType(ISD::FP_TO_FP16, MVT::i16, MVT::i32);
594
598
600
601 // F16 - Constant Actions.
604
605 // F16 - Load/Store Actions.
606 setOperationAction(ISD::LOAD, MVT::f16, Promote);
607 AddPromotedToType(ISD::LOAD, MVT::f16, MVT::i16);
608 setOperationAction(ISD::STORE, MVT::f16, Promote);
609 AddPromotedToType(ISD::STORE, MVT::f16, MVT::i16);
610
611 // BF16 - Load/Store Actions.
612 setOperationAction(ISD::LOAD, MVT::bf16, Promote);
613 AddPromotedToType(ISD::LOAD, MVT::bf16, MVT::i16);
614 setOperationAction(ISD::STORE, MVT::bf16, Promote);
615 AddPromotedToType(ISD::STORE, MVT::bf16, MVT::i16);
616
617 // F16 - VOP1 Actions.
619 ISD::FSIN, ISD::FROUND},
620 MVT::f16, Custom);
621
622 // BF16 - VOP1 Actions.
623 if (Subtarget->hasBF16TransInsts())
624 setOperationAction({ISD::FCOS, ISD::FSIN, ISD::FDIV}, MVT::bf16, Custom);
625
628
629 // F16 - VOP2 Actions.
630 setOperationAction({ISD::BR_CC, ISD::SELECT_CC}, {MVT::f16, MVT::bf16},
631 Expand);
632 setOperationAction({ISD::FLDEXP, ISD::STRICT_FLDEXP}, MVT::f16, Custom);
633 setOperationAction(ISD::FFREXP, MVT::f16, Custom);
635
636 // F16 - VOP3 Actions.
638 if (STI.hasMadF16())
640
641 for (MVT VT :
642 {MVT::v2i16, MVT::v2f16, MVT::v2bf16, MVT::v4i16, MVT::v4f16,
643 MVT::v4bf16, MVT::v8i16, MVT::v8f16, MVT::v8bf16, MVT::v16i16,
644 MVT::v16f16, MVT::v16bf16, MVT::v32i16, MVT::v32f16}) {
645 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
646 switch (Op) {
647 case ISD::LOAD:
648 case ISD::STORE:
650 case ISD::BITCAST:
651 case ISD::UNDEF:
656 case ISD::IS_FPCLASS:
657 break;
661 break;
662 default:
664 break;
665 }
666 }
667 }
668
669 // v_perm_b32 can handle either of these.
670 setOperationAction(ISD::BSWAP, {MVT::i16, MVT::v2i16}, Legal);
672
673 // XXX - Do these do anything? Vector constants turn into build_vector.
674 setOperationAction(ISD::Constant, {MVT::v2i16, MVT::v2f16}, Legal);
675
676 setOperationAction(ISD::UNDEF, {MVT::v2i16, MVT::v2f16, MVT::v2bf16},
677 Legal);
678
679 setOperationAction(ISD::STORE, MVT::v2i16, Promote);
680 AddPromotedToType(ISD::STORE, MVT::v2i16, MVT::i32);
681 setOperationAction(ISD::STORE, MVT::v2f16, Promote);
682 AddPromotedToType(ISD::STORE, MVT::v2f16, MVT::i32);
683
684 setOperationAction(ISD::LOAD, MVT::v2i16, Promote);
685 AddPromotedToType(ISD::LOAD, MVT::v2i16, MVT::i32);
686 setOperationAction(ISD::LOAD, MVT::v2f16, Promote);
687 AddPromotedToType(ISD::LOAD, MVT::v2f16, MVT::i32);
688
689 setOperationAction(ISD::AND, MVT::v2i16, Promote);
690 AddPromotedToType(ISD::AND, MVT::v2i16, MVT::i32);
691 setOperationAction(ISD::OR, MVT::v2i16, Promote);
692 AddPromotedToType(ISD::OR, MVT::v2i16, MVT::i32);
693 setOperationAction(ISD::XOR, MVT::v2i16, Promote);
694 AddPromotedToType(ISD::XOR, MVT::v2i16, MVT::i32);
695
696 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
697 AddPromotedToType(ISD::LOAD, MVT::v4i16, MVT::v2i32);
698 setOperationAction(ISD::LOAD, MVT::v4f16, Promote);
699 AddPromotedToType(ISD::LOAD, MVT::v4f16, MVT::v2i32);
700 setOperationAction(ISD::LOAD, MVT::v4bf16, Promote);
701 AddPromotedToType(ISD::LOAD, MVT::v4bf16, MVT::v2i32);
702
703 setOperationAction(ISD::STORE, MVT::v4i16, Promote);
704 AddPromotedToType(ISD::STORE, MVT::v4i16, MVT::v2i32);
705 setOperationAction(ISD::STORE, MVT::v4f16, Promote);
706 AddPromotedToType(ISD::STORE, MVT::v4f16, MVT::v2i32);
707 setOperationAction(ISD::STORE, MVT::v4bf16, Promote);
708 AddPromotedToType(ISD::STORE, MVT::v4bf16, MVT::v2i32);
709
710 setOperationAction(ISD::LOAD, MVT::v8i16, Promote);
711 AddPromotedToType(ISD::LOAD, MVT::v8i16, MVT::v4i32);
712 setOperationAction(ISD::LOAD, MVT::v8f16, Promote);
713 AddPromotedToType(ISD::LOAD, MVT::v8f16, MVT::v4i32);
714 setOperationAction(ISD::LOAD, MVT::v8bf16, Promote);
715 AddPromotedToType(ISD::LOAD, MVT::v8bf16, MVT::v4i32);
716
717 setOperationAction(ISD::STORE, MVT::v4i16, Promote);
718 AddPromotedToType(ISD::STORE, MVT::v4i16, MVT::v2i32);
719 setOperationAction(ISD::STORE, MVT::v4f16, Promote);
720 AddPromotedToType(ISD::STORE, MVT::v4f16, MVT::v2i32);
721
722 setOperationAction(ISD::STORE, MVT::v8i16, Promote);
723 AddPromotedToType(ISD::STORE, MVT::v8i16, MVT::v4i32);
724 setOperationAction(ISD::STORE, MVT::v8f16, Promote);
725 AddPromotedToType(ISD::STORE, MVT::v8f16, MVT::v4i32);
726 setOperationAction(ISD::STORE, MVT::v8bf16, Promote);
727 AddPromotedToType(ISD::STORE, MVT::v8bf16, MVT::v4i32);
728
729 setOperationAction(ISD::LOAD, MVT::v16i16, Promote);
730 AddPromotedToType(ISD::LOAD, MVT::v16i16, MVT::v8i32);
731 setOperationAction(ISD::LOAD, MVT::v16f16, Promote);
732 AddPromotedToType(ISD::LOAD, MVT::v16f16, MVT::v8i32);
733 setOperationAction(ISD::LOAD, MVT::v16bf16, Promote);
734 AddPromotedToType(ISD::LOAD, MVT::v16bf16, MVT::v8i32);
735
736 setOperationAction(ISD::STORE, MVT::v16i16, Promote);
737 AddPromotedToType(ISD::STORE, MVT::v16i16, MVT::v8i32);
738 setOperationAction(ISD::STORE, MVT::v16f16, Promote);
739 AddPromotedToType(ISD::STORE, MVT::v16f16, MVT::v8i32);
740 setOperationAction(ISD::STORE, MVT::v16bf16, Promote);
741 AddPromotedToType(ISD::STORE, MVT::v16bf16, MVT::v8i32);
742
743 setOperationAction(ISD::LOAD, MVT::v32i16, Promote);
744 AddPromotedToType(ISD::LOAD, MVT::v32i16, MVT::v16i32);
745 setOperationAction(ISD::LOAD, MVT::v32f16, Promote);
746 AddPromotedToType(ISD::LOAD, MVT::v32f16, MVT::v16i32);
747 setOperationAction(ISD::LOAD, MVT::v32bf16, Promote);
748 AddPromotedToType(ISD::LOAD, MVT::v32bf16, MVT::v16i32);
749
750 setOperationAction(ISD::STORE, MVT::v32i16, Promote);
751 AddPromotedToType(ISD::STORE, MVT::v32i16, MVT::v16i32);
752 setOperationAction(ISD::STORE, MVT::v32f16, Promote);
753 AddPromotedToType(ISD::STORE, MVT::v32f16, MVT::v16i32);
754 setOperationAction(ISD::STORE, MVT::v32bf16, Promote);
755 AddPromotedToType(ISD::STORE, MVT::v32bf16, MVT::v16i32);
756
758 MVT::v2i32, Expand);
759 setOperationAction(ISD::FP_EXTEND, MVT::v2f32, Expand);
760
762 MVT::v4i32, Expand);
763
765 MVT::v8i32, Expand);
766
767 setOperationAction(ISD::BUILD_VECTOR, {MVT::v2i16, MVT::v2f16, MVT::v2bf16},
768 Subtarget->hasVOP3PInsts() ? Legal : Custom);
769
770 setOperationAction(ISD::FNEG, {MVT::v2f16, MVT::v2bf16}, Legal);
771 // This isn't really legal, but this avoids the legalizer unrolling it (and
772 // allows matching fneg (fabs x) patterns)
773 setOperationAction(ISD::FABS, {MVT::v2f16, MVT::v2bf16}, Legal);
774
775 // Can do this in one BFI plus a constant materialize.
777 {MVT::v2f16, MVT::v2bf16, MVT::v4f16, MVT::v4bf16,
778 MVT::v8f16, MVT::v8bf16, MVT::v16f16, MVT::v16bf16,
779 MVT::v32f16, MVT::v32bf16},
780 Custom);
781
783 {ISD::FMAXNUM, ISD::FMINNUM, ISD::FMINIMUMNUM, ISD::FMAXIMUMNUM},
784 MVT::f16, Custom);
785 setOperationAction({ISD::FMAXNUM_IEEE, ISD::FMINNUM_IEEE}, MVT::f16, Legal);
786
787 setOperationAction({ISD::FMINNUM_IEEE, ISD::FMAXNUM_IEEE, ISD::FMINIMUMNUM,
788 ISD::FMAXIMUMNUM},
789 {MVT::v4f16, MVT::v8f16, MVT::v16f16, MVT::v32f16},
790 Custom);
791
792 setOperationAction({ISD::FMINNUM, ISD::FMAXNUM},
793 {MVT::v4f16, MVT::v8f16, MVT::v16f16, MVT::v32f16},
794 Expand);
795
796 for (MVT Vec16 :
797 {MVT::v8i16, MVT::v8f16, MVT::v8bf16, MVT::v16i16, MVT::v16f16,
798 MVT::v16bf16, MVT::v32i16, MVT::v32f16, MVT::v32bf16}) {
801 Vec16, Custom);
803 }
804 }
805
806 if (Subtarget->hasVOP3PInsts()) {
810 MVT::v2i16, Legal);
811
812 setOperationAction({ISD::FADD, ISD::FMUL, ISD::FMA, ISD::FMINNUM_IEEE,
813 ISD::FMAXNUM_IEEE, ISD::FCANONICALIZE},
814 MVT::v2f16, Legal);
815
817 {MVT::v2i16, MVT::v2f16, MVT::v2bf16}, Custom);
818
820 {MVT::v4f16, MVT::v4i16, MVT::v4bf16, MVT::v8f16,
821 MVT::v8i16, MVT::v8bf16, MVT::v16f16, MVT::v16i16,
822 MVT::v16bf16, MVT::v32f16, MVT::v32i16, MVT::v32bf16},
823 Custom);
824
825 for (MVT VT : {MVT::v4i16, MVT::v8i16, MVT::v16i16, MVT::v32i16})
826 // Split vector operations.
831 VT, Custom);
832
833 for (MVT VT : {MVT::v4f16, MVT::v8f16, MVT::v16f16, MVT::v32f16})
834 // Split vector operations.
836 VT, Custom);
837
839 {ISD::FMAXNUM, ISD::FMINNUM, ISD::FMINIMUMNUM, ISD::FMAXIMUMNUM},
840 {MVT::v2f16, MVT::v4f16}, Custom);
841
842 setOperationAction(ISD::FEXP, MVT::v2f16, Custom);
843 setOperationAction(ISD::SELECT, {MVT::v4i16, MVT::v4f16, MVT::v4bf16},
844 Custom);
845
846 if (Subtarget->hasBF16PackedInsts()) {
847 for (MVT VT : {MVT::v4bf16, MVT::v8bf16, MVT::v16bf16, MVT::v32bf16})
848 // Split vector operations.
850 VT, Custom);
851 }
852
853 if (Subtarget->hasPackedFP32Ops()) {
855 MVT::v2f32, Legal);
857 {MVT::v4f32, MVT::v8f32, MVT::v16f32, MVT::v32f32},
858 Custom);
859 }
860 }
861
862 setOperationAction({ISD::FNEG, ISD::FABS}, MVT::v4f16, Custom);
863
864 if (Subtarget->has16BitInsts()) {
866 AddPromotedToType(ISD::SELECT, MVT::v2i16, MVT::i32);
868 AddPromotedToType(ISD::SELECT, MVT::v2f16, MVT::i32);
869 } else {
870 // Legalization hack.
871 setOperationAction(ISD::SELECT, {MVT::v2i16, MVT::v2f16}, Custom);
872
873 setOperationAction({ISD::FNEG, ISD::FABS}, MVT::v2f16, Custom);
874 }
875
877 {MVT::v4i16, MVT::v4f16, MVT::v4bf16, MVT::v2i8, MVT::v4i8,
878 MVT::v8i8, MVT::v8i16, MVT::v8f16, MVT::v8bf16,
879 MVT::v16i16, MVT::v16f16, MVT::v16bf16, MVT::v32i16,
880 MVT::v32f16, MVT::v32bf16},
881 Custom);
882
884
885 if (Subtarget->hasVectorMulU64())
887 else if (Subtarget->hasScalarSMulU64())
889
890 if (Subtarget->hasMad64_32())
892
893 if (Subtarget->hasSafeSmemPrefetch() || Subtarget->hasVmemPrefInsts())
894 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
895
896 if (Subtarget->hasIEEEMinimumMaximumInsts()) {
897 setOperationAction({ISD::FMAXIMUM, ISD::FMINIMUM},
898 {MVT::f16, MVT::f32, MVT::f64, MVT::v2f16}, Legal);
899 } else {
900 // FIXME: For nnan fmaximum, emit the fmaximum3 instead of fmaxnum
901 if (Subtarget->hasMinimum3Maximum3F32())
902 setOperationAction({ISD::FMAXIMUM, ISD::FMINIMUM}, MVT::f32, Legal);
903
904 if (Subtarget->hasMinimum3Maximum3PKF16()) {
905 setOperationAction({ISD::FMAXIMUM, ISD::FMINIMUM}, MVT::v2f16, Legal);
906
907 // If only the vector form is available, we need to widen to a vector.
908 if (!Subtarget->hasMinimum3Maximum3F16())
909 setOperationAction({ISD::FMAXIMUM, ISD::FMINIMUM}, MVT::f16, Custom);
910 }
911 }
912
913 if (Subtarget->hasVOP3PInsts()) {
914 // We want to break these into v2f16 pieces, not scalarize.
915 setOperationAction({ISD::FMINIMUM, ISD::FMAXIMUM},
916 {MVT::v4f16, MVT::v8f16, MVT::v16f16, MVT::v32f16},
917 Custom);
918 }
919
920 if (Subtarget->hasIntMinMax64())
922 Legal);
923
925 {MVT::Other, MVT::f32, MVT::v4f32, MVT::i16, MVT::f16,
926 MVT::bf16, MVT::v2i16, MVT::v2f16, MVT::v2bf16, MVT::i128,
927 MVT::i8},
928 Custom);
929
931 {MVT::v2f16, MVT::v2i16, MVT::v2bf16, MVT::v3f16,
932 MVT::v3i16, MVT::v4f16, MVT::v4i16, MVT::v4bf16,
933 MVT::v8i16, MVT::v8f16, MVT::v8bf16, MVT::Other, MVT::f16,
934 MVT::i16, MVT::bf16, MVT::i8, MVT::i128},
935 Custom);
936
938 {MVT::Other, MVT::v2i16, MVT::v2f16, MVT::v2bf16,
939 MVT::v3i16, MVT::v3f16, MVT::v4f16, MVT::v4i16,
940 MVT::v4bf16, MVT::v8i16, MVT::v8f16, MVT::v8bf16,
941 MVT::f16, MVT::i16, MVT::bf16, MVT::i8, MVT::i128},
942 Custom);
943
944 setOperationAction(ISD::STACKSAVE, MVT::Other, Custom);
946 setOperationAction(ISD::SET_ROUNDING, MVT::Other, Custom);
947 setOperationAction(ISD::GET_FPENV, MVT::i64, Custom);
948 setOperationAction(ISD::SET_FPENV, MVT::i64, Custom);
949
950 // TODO: Could move this to custom lowering, could benefit from combines on
951 // extract of relevant bits.
952 setOperationAction(ISD::GET_FPMODE, MVT::i32, Legal);
953
955
956 if (Subtarget->hasBF16ConversionInsts()) {
957 setOperationAction(ISD::FP_ROUND, {MVT::bf16, MVT::v2bf16}, Custom);
959 }
960
961 if (Subtarget->hasBF16PackedInsts()) {
963 {ISD::FADD, ISD::FMUL, ISD::FMINNUM, ISD::FMAXNUM, ISD::FMA},
964 MVT::v2bf16, Legal);
965 }
966
967 if (Subtarget->hasBF16TransInsts()) {
968 setOperationAction({ISD::FEXP2, ISD::FLOG2, ISD::FSQRT}, MVT::bf16, Legal);
969 }
970
971 if (Subtarget->hasCvtPkF16F32Inst()) {
973 {MVT::v2f16, MVT::v4f16, MVT::v8f16, MVT::v16f16},
974 Custom);
975 }
976
978 ISD::PTRADD,
980 ISD::SUB,
982 ISD::MUL,
983 ISD::FADD,
984 ISD::FSUB,
985 ISD::FDIV,
986 ISD::FMUL,
987 ISD::FMINNUM,
988 ISD::FMAXNUM,
989 ISD::FMINNUM_IEEE,
990 ISD::FMAXNUM_IEEE,
991 ISD::FMINIMUM,
992 ISD::FMAXIMUM,
993 ISD::FMINIMUMNUM,
994 ISD::FMAXIMUMNUM,
995 ISD::FMA,
996 ISD::SMIN,
997 ISD::SMAX,
998 ISD::UMIN,
999 ISD::UMAX,
1000 ISD::SETCC,
1002 ISD::SMIN,
1003 ISD::SMAX,
1004 ISD::UMIN,
1005 ISD::UMAX,
1006 ISD::AND,
1007 ISD::OR,
1008 ISD::XOR,
1009 ISD::SHL,
1010 ISD::SRL,
1011 ISD::SRA,
1012 ISD::FSHR,
1022
1023 if (Subtarget->has16BitInsts() && !Subtarget->hasMed3_16())
1025
1026 // All memory operations. Some folding on the pointer operand is done to help
1027 // matching the constant offsets in the addressing modes.
1028 setTargetDAGCombine({ISD::LOAD,
1029 ISD::STORE,
1030 ISD::ATOMIC_LOAD,
1031 ISD::ATOMIC_STORE,
1032 ISD::ATOMIC_CMP_SWAP,
1033 ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS,
1034 ISD::ATOMIC_SWAP,
1035 ISD::ATOMIC_LOAD_ADD,
1036 ISD::ATOMIC_LOAD_SUB,
1037 ISD::ATOMIC_LOAD_AND,
1038 ISD::ATOMIC_LOAD_OR,
1039 ISD::ATOMIC_LOAD_XOR,
1040 ISD::ATOMIC_LOAD_NAND,
1041 ISD::ATOMIC_LOAD_MIN,
1042 ISD::ATOMIC_LOAD_MAX,
1043 ISD::ATOMIC_LOAD_UMIN,
1044 ISD::ATOMIC_LOAD_UMAX,
1045 ISD::ATOMIC_LOAD_FADD,
1046 ISD::ATOMIC_LOAD_FMIN,
1047 ISD::ATOMIC_LOAD_FMAX,
1048 ISD::ATOMIC_LOAD_UINC_WRAP,
1049 ISD::ATOMIC_LOAD_UDEC_WRAP,
1052
1053 // FIXME: In other contexts we pretend this is a per-function property.
1055
1057}
1058
1059const GCNSubtarget *SITargetLowering::getSubtarget() const { return Subtarget; }
1060
1062 static const MCPhysReg RCRegs[] = {AMDGPU::MODE};
1063 return RCRegs;
1064}
1065
1066//===----------------------------------------------------------------------===//
1067// TargetLowering queries
1068//===----------------------------------------------------------------------===//
1069
1070// v_mad_mix* support a conversion from f16 to f32.
1071//
1072// There is only one special case when denormals are enabled we don't currently,
1073// where this is OK to use.
1074bool SITargetLowering::isFPExtFoldable(const SelectionDAG &DAG, unsigned Opcode,
1075 EVT DestVT, EVT SrcVT) const {
1076 return DestVT.getScalarType() == MVT::f32 &&
1077 ((((Opcode == ISD::FMAD && Subtarget->hasMadMixInsts()) ||
1078 (Opcode == ISD::FMA && Subtarget->hasFmaMixInsts())) &&
1079 SrcVT.getScalarType() == MVT::f16) ||
1080 (Opcode == ISD::FMA && Subtarget->hasFmaMixBF16Insts() &&
1081 SrcVT.getScalarType() == MVT::bf16)) &&
1082 // TODO: This probably only requires no input flushing?
1084}
1085
1087 LLT DestTy, LLT SrcTy) const {
1088 return ((Opcode == TargetOpcode::G_FMAD && Subtarget->hasMadMixInsts()) ||
1089 (Opcode == TargetOpcode::G_FMA && Subtarget->hasFmaMixInsts())) &&
1090 DestTy.getScalarSizeInBits() == 32 &&
1091 SrcTy.getScalarSizeInBits() == 16 &&
1092 // TODO: This probably only requires no input flushing?
1093 denormalModeIsFlushAllF32(*MI.getMF());
1094}
1095
1097 // SI has some legal vector types, but no legal vector operations. Say no
1098 // shuffles are legal in order to prefer scalarizing some vector operations.
1099 return false;
1100}
1101
1103 CallingConv::ID CC,
1104 EVT VT) const {
1106 return TargetLowering::getRegisterTypeForCallingConv(Context, CC, VT);
1107
1108 if (VT.isVector()) {
1109 EVT ScalarVT = VT.getScalarType();
1110 unsigned Size = ScalarVT.getSizeInBits();
1111 if (Size == 16) {
1112 if (Subtarget->has16BitInsts()) {
1113 if (VT.isInteger())
1114 return MVT::v2i16;
1115 return (ScalarVT == MVT::bf16 ? MVT::i32 : MVT::v2f16);
1116 }
1117 return VT.isInteger() ? MVT::i32 : MVT::f32;
1118 }
1119
1120 if (Size < 16)
1121 return Subtarget->has16BitInsts() ? MVT::i16 : MVT::i32;
1122 return Size == 32 ? ScalarVT.getSimpleVT() : MVT::i32;
1123 }
1124
1125 if (VT.getSizeInBits() > 32)
1126 return MVT::i32;
1127
1128 return TargetLowering::getRegisterTypeForCallingConv(Context, CC, VT);
1129}
1130
1132 CallingConv::ID CC,
1133 EVT VT) const {
1135 return TargetLowering::getNumRegistersForCallingConv(Context, CC, VT);
1136
1137 if (VT.isVector()) {
1138 unsigned NumElts = VT.getVectorNumElements();
1139 EVT ScalarVT = VT.getScalarType();
1140 unsigned Size = ScalarVT.getSizeInBits();
1141
1142 // FIXME: Should probably promote 8-bit vectors to i16.
1143 if (Size == 16 && Subtarget->has16BitInsts())
1144 return (NumElts + 1) / 2;
1145
1146 if (Size <= 32)
1147 return NumElts;
1148
1149 if (Size > 32)
1150 return NumElts * ((Size + 31) / 32);
1151 } else if (VT.getSizeInBits() > 32)
1152 return (VT.getSizeInBits() + 31) / 32;
1153
1154 return TargetLowering::getNumRegistersForCallingConv(Context, CC, VT);
1155}
1156
1158 LLVMContext &Context, CallingConv::ID CC, EVT VT, EVT &IntermediateVT,
1159 unsigned &NumIntermediates, MVT &RegisterVT) const {
1160 if (CC != CallingConv::AMDGPU_KERNEL && VT.isVector()) {
1161 unsigned NumElts = VT.getVectorNumElements();
1162 EVT ScalarVT = VT.getScalarType();
1163 unsigned Size = ScalarVT.getSizeInBits();
1164 // FIXME: We should fix the ABI to be the same on targets without 16-bit
1165 // support, but unless we can properly handle 3-vectors, it will be still be
1166 // inconsistent.
1167 if (Size == 16 && Subtarget->has16BitInsts()) {
1168 if (ScalarVT == MVT::bf16) {
1169 RegisterVT = MVT::i32;
1170 IntermediateVT = MVT::v2bf16;
1171 } else {
1172 RegisterVT = VT.isInteger() ? MVT::v2i16 : MVT::v2f16;
1173 IntermediateVT = RegisterVT;
1174 }
1175 NumIntermediates = (NumElts + 1) / 2;
1176 return NumIntermediates;
1177 }
1178
1179 if (Size == 32) {
1180 RegisterVT = ScalarVT.getSimpleVT();
1181 IntermediateVT = RegisterVT;
1182 NumIntermediates = NumElts;
1183 return NumIntermediates;
1184 }
1185
1186 if (Size < 16 && Subtarget->has16BitInsts()) {
1187 // FIXME: Should probably form v2i16 pieces
1188 RegisterVT = MVT::i16;
1189 IntermediateVT = ScalarVT;
1190 NumIntermediates = NumElts;
1191 return NumIntermediates;
1192 }
1193
1194 if (Size != 16 && Size <= 32) {
1195 RegisterVT = MVT::i32;
1196 IntermediateVT = ScalarVT;
1197 NumIntermediates = NumElts;
1198 return NumIntermediates;
1199 }
1200
1201 if (Size > 32) {
1202 RegisterVT = MVT::i32;
1203 IntermediateVT = RegisterVT;
1204 NumIntermediates = NumElts * ((Size + 31) / 32);
1205 return NumIntermediates;
1206 }
1207 }
1208
1210 Context, CC, VT, IntermediateVT, NumIntermediates, RegisterVT);
1211}
1212
1214 const DataLayout &DL, Type *Ty,
1215 unsigned MaxNumLanes) {
1216 assert(MaxNumLanes != 0);
1217
1218 LLVMContext &Ctx = Ty->getContext();
1219 if (auto *VT = dyn_cast<FixedVectorType>(Ty)) {
1220 unsigned NumElts = std::min(MaxNumLanes, VT->getNumElements());
1221 return EVT::getVectorVT(Ctx, TLI.getValueType(DL, VT->getElementType()),
1222 NumElts);
1223 }
1224
1225 return TLI.getValueType(DL, Ty);
1226}
1227
1228// Peek through TFE struct returns to only use the data size.
1230 const DataLayout &DL, Type *Ty,
1231 unsigned MaxNumLanes) {
1232 auto *ST = dyn_cast<StructType>(Ty);
1233 if (!ST)
1234 return memVTFromLoadIntrData(TLI, DL, Ty, MaxNumLanes);
1235
1236 // TFE intrinsics return an aggregate type.
1237 assert(ST->getNumContainedTypes() == 2 &&
1238 ST->getContainedType(1)->isIntegerTy(32));
1239 return memVTFromLoadIntrData(TLI, DL, ST->getContainedType(0), MaxNumLanes);
1240}
1241
1242/// Map address space 7 to MVT::amdgpuBufferFatPointer because that's its
1243/// in-memory representation. This return value is a custom type because there
1244/// is no MVT::i160 and adding one breaks integer promotion logic. While this
1245/// could cause issues during codegen, these address space 7 pointers will be
1246/// rewritten away by then. Therefore, we can return MVT::amdgpuBufferFatPointer
1247/// in order to allow pre-codegen passes that query TargetTransformInfo, often
1248/// for cost modeling, to work. (This also sets us up decently for doing the
1249/// buffer lowering in GlobalISel if SelectionDAG ever goes away.)
1251 if (AMDGPUAS::BUFFER_FAT_POINTER == AS && DL.getPointerSizeInBits(AS) == 160)
1252 return MVT::amdgpuBufferFatPointer;
1254 DL.getPointerSizeInBits(AS) == 192)
1255 return MVT::amdgpuBufferStridedPointer;
1257}
1258/// Similarly, the in-memory representation of a p7 is {p8, i32}, aka
1259/// v8i32 when padding is added.
1260/// The in-memory representation of a p9 is {p8, i32, i32}, which is
1261/// also v8i32 with padding.
1263 if ((AMDGPUAS::BUFFER_FAT_POINTER == AS &&
1264 DL.getPointerSizeInBits(AS) == 160) ||
1266 DL.getPointerSizeInBits(AS) == 192))
1267 return MVT::v8i32;
1269}
1270
1271static unsigned getIntrMemWidth(unsigned IntrID) {
1272 switch (IntrID) {
1273 case Intrinsic::amdgcn_global_load_async_to_lds_b8:
1274 case Intrinsic::amdgcn_cluster_load_async_to_lds_b8:
1275 case Intrinsic::amdgcn_global_store_async_from_lds_b8:
1276 return 8;
1277 case Intrinsic::amdgcn_global_load_async_to_lds_b32:
1278 case Intrinsic::amdgcn_cluster_load_async_to_lds_b32:
1279 case Intrinsic::amdgcn_global_store_async_from_lds_b32:
1280 case Intrinsic::amdgcn_cooperative_atomic_load_32x4B:
1281 case Intrinsic::amdgcn_cooperative_atomic_store_32x4B:
1282 return 32;
1283 case Intrinsic::amdgcn_global_load_async_to_lds_b64:
1284 case Intrinsic::amdgcn_cluster_load_async_to_lds_b64:
1285 case Intrinsic::amdgcn_global_store_async_from_lds_b64:
1286 case Intrinsic::amdgcn_cooperative_atomic_load_16x8B:
1287 case Intrinsic::amdgcn_cooperative_atomic_store_16x8B:
1288 return 64;
1289 case Intrinsic::amdgcn_global_load_async_to_lds_b128:
1290 case Intrinsic::amdgcn_cluster_load_async_to_lds_b128:
1291 case Intrinsic::amdgcn_global_store_async_from_lds_b128:
1292 case Intrinsic::amdgcn_cooperative_atomic_load_8x16B:
1293 case Intrinsic::amdgcn_cooperative_atomic_store_8x16B:
1294 return 128;
1295 default:
1296 llvm_unreachable("Unknown width");
1297 }
1298}
1299
1300static void getCoopAtomicOperandsInfo(const CallInst &CI, bool IsLoad,
1302 Value *OrderingArg = CI.getArgOperand(IsLoad ? 1 : 2);
1303 unsigned Ord = cast<ConstantInt>(OrderingArg)->getZExtValue();
1304 switch (AtomicOrderingCABI(Ord)) {
1307 break;
1310 break;
1313 break;
1314 default:
1316 break;
1317 }
1318
1319 Info.flags =
1321 Info.flags |= MOCooperative;
1322
1323 MDNode *ScopeMD = cast<MDNode>(
1324 cast<MetadataAsValue>(CI.getArgOperand(IsLoad ? 2 : 3))->getMetadata());
1325 StringRef Scope = cast<MDString>(ScopeMD->getOperand(0))->getString();
1326 Info.ssid = CI.getContext().getOrInsertSyncScopeID(Scope);
1327}
1328
1330 const CallInst &CI,
1331 MachineFunction &MF,
1332 unsigned IntrID) const {
1333 Info.flags = MachineMemOperand::MONone;
1334 if (CI.hasMetadata(LLVMContext::MD_invariant_load))
1335 Info.flags |= MachineMemOperand::MOInvariant;
1336 if (CI.hasMetadata(LLVMContext::MD_nontemporal))
1338 Info.flags |= getTargetMMOFlags(CI);
1339
1340 if (const AMDGPU::RsrcIntrinsic *RsrcIntr =
1342 AttributeSet Attr =
1344 MemoryEffects ME = Attr.getMemoryEffects();
1345 if (ME.doesNotAccessMemory())
1346 return false;
1347
1348 // TODO: Should images get their own address space?
1349 Info.fallbackAddressSpace = AMDGPUAS::BUFFER_RESOURCE;
1350
1351 const AMDGPU::MIMGBaseOpcodeInfo *BaseOpcode = nullptr;
1352 if (RsrcIntr->IsImage) {
1353 const AMDGPU::ImageDimIntrinsicInfo *Intr =
1355 BaseOpcode = AMDGPU::getMIMGBaseOpcodeInfo(Intr->BaseOpcode);
1356 Info.align.reset();
1357 }
1358
1359 Value *RsrcArg = CI.getArgOperand(RsrcIntr->RsrcArg);
1360 if (auto *RsrcPtrTy = dyn_cast<PointerType>(RsrcArg->getType())) {
1361 if (RsrcPtrTy->getAddressSpace() == AMDGPUAS::BUFFER_RESOURCE)
1362 // We conservatively set the memory operand of a buffer intrinsic to the
1363 // base resource pointer, so that we can access alias information about
1364 // those pointers. Cases like "this points at the same value
1365 // but with a different offset" are handled in
1366 // areMemAccessesTriviallyDisjoint.
1367 Info.ptrVal = RsrcArg;
1368 }
1369
1370 bool IsSPrefetch = IntrID == Intrinsic::amdgcn_s_buffer_prefetch_data;
1371 if (!IsSPrefetch) {
1372 auto *Aux = cast<ConstantInt>(CI.getArgOperand(CI.arg_size() - 1));
1373 if (Aux->getZExtValue() & AMDGPU::CPol::VOLATILE)
1374 Info.flags |= MachineMemOperand::MOVolatile;
1375 }
1376
1378 if (ME.onlyReadsMemory()) {
1379 if (RsrcIntr->IsImage) {
1380 unsigned MaxNumLanes = 4;
1381
1382 if (!BaseOpcode->Gather4) {
1383 // If this isn't a gather, we may have excess loaded elements in the
1384 // IR type. Check the dmask for the real number of elements loaded.
1385 unsigned DMask =
1386 cast<ConstantInt>(CI.getArgOperand(0))->getZExtValue();
1387 MaxNumLanes = DMask == 0 ? 1 : llvm::popcount(DMask);
1388 }
1389
1390 Info.memVT = memVTFromLoadIntrReturn(*this, MF.getDataLayout(),
1391 CI.getType(), MaxNumLanes);
1392 } else {
1393 Info.memVT =
1395 std::numeric_limits<unsigned>::max());
1396 }
1397
1398 // FIXME: What does alignment mean for an image?
1399 Info.opc = ISD::INTRINSIC_W_CHAIN;
1400 Info.flags |= MachineMemOperand::MOLoad;
1401 } else if (ME.onlyWritesMemory()) {
1402 Info.opc = ISD::INTRINSIC_VOID;
1403
1404 Type *DataTy = CI.getArgOperand(0)->getType();
1405 if (RsrcIntr->IsImage) {
1406 unsigned DMask = cast<ConstantInt>(CI.getArgOperand(1))->getZExtValue();
1407 unsigned DMaskLanes = DMask == 0 ? 1 : llvm::popcount(DMask);
1408 Info.memVT = memVTFromLoadIntrData(*this, MF.getDataLayout(), DataTy,
1409 DMaskLanes);
1410 } else
1411 Info.memVT = getValueType(MF.getDataLayout(), DataTy);
1412
1413 Info.flags |= MachineMemOperand::MOStore;
1414 } else {
1415 // Atomic, NoReturn Sampler or prefetch
1416 Info.opc = CI.getType()->isVoidTy() ? ISD::INTRINSIC_VOID
1418 Info.flags |=
1420
1421 if (!IsSPrefetch)
1422 Info.flags |= MachineMemOperand::MOStore;
1423
1424 switch (IntrID) {
1425 default:
1426 if ((RsrcIntr->IsImage && BaseOpcode->NoReturn) || IsSPrefetch) {
1427 // Fake memory access type for no return sampler intrinsics
1428 Info.memVT = MVT::i32;
1429 } else {
1430 // XXX - Should this be volatile without known ordering?
1431 Info.flags |= MachineMemOperand::MOVolatile;
1432 Info.memVT = MVT::getVT(CI.getArgOperand(0)->getType());
1433 }
1434 break;
1435 case Intrinsic::amdgcn_raw_buffer_load_lds:
1436 case Intrinsic::amdgcn_raw_ptr_buffer_load_lds:
1437 case Intrinsic::amdgcn_struct_buffer_load_lds:
1438 case Intrinsic::amdgcn_struct_ptr_buffer_load_lds: {
1439 unsigned Width = cast<ConstantInt>(CI.getArgOperand(2))->getZExtValue();
1440 Info.memVT = EVT::getIntegerVT(CI.getContext(), Width * 8);
1441 Info.ptrVal = CI.getArgOperand(1);
1442 return true;
1443 }
1444 case Intrinsic::amdgcn_raw_atomic_buffer_load:
1445 case Intrinsic::amdgcn_raw_ptr_atomic_buffer_load:
1446 case Intrinsic::amdgcn_struct_atomic_buffer_load:
1447 case Intrinsic::amdgcn_struct_ptr_atomic_buffer_load: {
1448 Info.memVT =
1450 std::numeric_limits<unsigned>::max());
1451 Info.flags &= ~MachineMemOperand::MOStore;
1452 return true;
1453 }
1454 }
1455 }
1456 return true;
1457 }
1458
1459 switch (IntrID) {
1460 case Intrinsic::amdgcn_ds_ordered_add:
1461 case Intrinsic::amdgcn_ds_ordered_swap: {
1462 Info.opc = ISD::INTRINSIC_W_CHAIN;
1463 Info.memVT = MVT::getVT(CI.getType());
1464 Info.ptrVal = CI.getOperand(0);
1465 Info.align.reset();
1467
1468 const ConstantInt *Vol = cast<ConstantInt>(CI.getOperand(4));
1469 if (!Vol->isZero())
1470 Info.flags |= MachineMemOperand::MOVolatile;
1471
1472 return true;
1473 }
1474 case Intrinsic::amdgcn_ds_add_gs_reg_rtn:
1475 case Intrinsic::amdgcn_ds_sub_gs_reg_rtn: {
1476 Info.opc = ISD::INTRINSIC_W_CHAIN;
1477 Info.memVT = MVT::getVT(CI.getOperand(0)->getType());
1478 Info.ptrVal = nullptr;
1479 Info.fallbackAddressSpace = AMDGPUAS::STREAMOUT_REGISTER;
1481 return true;
1482 }
1483 case Intrinsic::amdgcn_ds_append:
1484 case Intrinsic::amdgcn_ds_consume: {
1485 Info.opc = ISD::INTRINSIC_W_CHAIN;
1486 Info.memVT = MVT::getVT(CI.getType());
1487 Info.ptrVal = CI.getOperand(0);
1488 Info.align.reset();
1490
1491 const ConstantInt *Vol = cast<ConstantInt>(CI.getOperand(1));
1492 if (!Vol->isZero())
1493 Info.flags |= MachineMemOperand::MOVolatile;
1494
1495 return true;
1496 }
1497 case Intrinsic::amdgcn_ds_atomic_async_barrier_arrive_b64:
1498 case Intrinsic::amdgcn_ds_atomic_barrier_arrive_rtn_b64: {
1499 Info.opc = (IntrID == Intrinsic::amdgcn_ds_atomic_barrier_arrive_rtn_b64)
1502 Info.memVT = MVT::getVT(CI.getType());
1503 Info.ptrVal = CI.getOperand(0);
1504 Info.memVT = MVT::i64;
1505 Info.size = 8;
1506 Info.align.reset();
1508 return true;
1509 }
1510 case Intrinsic::amdgcn_global_atomic_csub: {
1511 Info.opc = ISD::INTRINSIC_W_CHAIN;
1512 Info.memVT = MVT::getVT(CI.getType());
1513 Info.ptrVal = CI.getOperand(0);
1514 Info.align.reset();
1517 return true;
1518 }
1519 case Intrinsic::amdgcn_image_bvh_dual_intersect_ray:
1520 case Intrinsic::amdgcn_image_bvh_intersect_ray:
1521 case Intrinsic::amdgcn_image_bvh8_intersect_ray: {
1522 Info.opc = ISD::INTRINSIC_W_CHAIN;
1523 Info.memVT =
1524 MVT::getVT(IntrID == Intrinsic::amdgcn_image_bvh_intersect_ray
1525 ? CI.getType()
1527 ->getElementType(0)); // XXX: what is correct VT?
1528
1529 Info.fallbackAddressSpace = AMDGPUAS::BUFFER_RESOURCE;
1530 Info.align.reset();
1531 Info.flags |=
1533 return true;
1534 }
1535 case Intrinsic::amdgcn_global_atomic_fmin_num:
1536 case Intrinsic::amdgcn_global_atomic_fmax_num:
1537 case Intrinsic::amdgcn_global_atomic_ordered_add_b64:
1538 case Intrinsic::amdgcn_flat_atomic_fmin_num:
1539 case Intrinsic::amdgcn_flat_atomic_fmax_num:
1540 case Intrinsic::amdgcn_atomic_cond_sub_u32: {
1541 Info.opc = ISD::INTRINSIC_W_CHAIN;
1542 Info.memVT = MVT::getVT(CI.getType());
1543 Info.ptrVal = CI.getOperand(0);
1544 Info.align.reset();
1548 return true;
1549 }
1550 case Intrinsic::amdgcn_flat_load_monitor_b32:
1551 case Intrinsic::amdgcn_flat_load_monitor_b64:
1552 case Intrinsic::amdgcn_flat_load_monitor_b128:
1553 case Intrinsic::amdgcn_global_load_monitor_b32:
1554 case Intrinsic::amdgcn_global_load_monitor_b64:
1555 case Intrinsic::amdgcn_global_load_monitor_b128:
1556 case Intrinsic::amdgcn_cluster_load_b32:
1557 case Intrinsic::amdgcn_cluster_load_b64:
1558 case Intrinsic::amdgcn_cluster_load_b128:
1559 case Intrinsic::amdgcn_ds_load_tr6_b96:
1560 case Intrinsic::amdgcn_ds_load_tr4_b64:
1561 case Intrinsic::amdgcn_ds_load_tr8_b64:
1562 case Intrinsic::amdgcn_ds_load_tr16_b128:
1563 case Intrinsic::amdgcn_global_load_tr6_b96:
1564 case Intrinsic::amdgcn_global_load_tr4_b64:
1565 case Intrinsic::amdgcn_global_load_tr_b64:
1566 case Intrinsic::amdgcn_global_load_tr_b128:
1567 case Intrinsic::amdgcn_ds_read_tr4_b64:
1568 case Intrinsic::amdgcn_ds_read_tr6_b96:
1569 case Intrinsic::amdgcn_ds_read_tr8_b64:
1570 case Intrinsic::amdgcn_ds_read_tr16_b64: {
1571 Info.opc = ISD::INTRINSIC_W_CHAIN;
1572 Info.memVT = MVT::getVT(CI.getType());
1573 Info.ptrVal = CI.getOperand(0);
1574 Info.align.reset();
1575 Info.flags |= MachineMemOperand::MOLoad;
1576 return true;
1577 }
1578 case Intrinsic::amdgcn_cooperative_atomic_load_32x4B:
1579 case Intrinsic::amdgcn_cooperative_atomic_load_16x8B:
1580 case Intrinsic::amdgcn_cooperative_atomic_load_8x16B: {
1581 Info.opc = ISD::INTRINSIC_W_CHAIN;
1582 Info.memVT = EVT::getIntegerVT(CI.getContext(), getIntrMemWidth(IntrID));
1583 Info.ptrVal = CI.getOperand(0);
1584 Info.align.reset();
1585 getCoopAtomicOperandsInfo(CI, /*IsLoad=*/true, Info);
1586 return true;
1587 }
1588 case Intrinsic::amdgcn_cooperative_atomic_store_32x4B:
1589 case Intrinsic::amdgcn_cooperative_atomic_store_16x8B:
1590 case Intrinsic::amdgcn_cooperative_atomic_store_8x16B: {
1591 Info.opc = ISD::INTRINSIC_VOID;
1592 Info.memVT = EVT::getIntegerVT(CI.getContext(), getIntrMemWidth(IntrID));
1593 Info.ptrVal = CI.getArgOperand(0);
1594 Info.align.reset();
1595 getCoopAtomicOperandsInfo(CI, /*IsLoad=*/false, Info);
1596 return true;
1597 }
1598 case Intrinsic::amdgcn_ds_gws_init:
1599 case Intrinsic::amdgcn_ds_gws_barrier:
1600 case Intrinsic::amdgcn_ds_gws_sema_v:
1601 case Intrinsic::amdgcn_ds_gws_sema_br:
1602 case Intrinsic::amdgcn_ds_gws_sema_p:
1603 case Intrinsic::amdgcn_ds_gws_sema_release_all: {
1604 Info.opc = ISD::INTRINSIC_VOID;
1605
1606 const GCNTargetMachine &TM =
1607 static_cast<const GCNTargetMachine &>(getTargetMachine());
1608
1610 Info.ptrVal = MFI->getGWSPSV(TM);
1611
1612 // This is an abstract access, but we need to specify a type and size.
1613 Info.memVT = MVT::i32;
1614 Info.size = 4;
1615 Info.align = Align(4);
1616
1617 if (IntrID == Intrinsic::amdgcn_ds_gws_barrier)
1618 Info.flags |= MachineMemOperand::MOLoad;
1619 else
1620 Info.flags |= MachineMemOperand::MOStore;
1621 return true;
1622 }
1623 case Intrinsic::amdgcn_global_load_async_to_lds_b8:
1624 case Intrinsic::amdgcn_global_load_async_to_lds_b32:
1625 case Intrinsic::amdgcn_global_load_async_to_lds_b64:
1626 case Intrinsic::amdgcn_global_load_async_to_lds_b128:
1627 case Intrinsic::amdgcn_cluster_load_async_to_lds_b8:
1628 case Intrinsic::amdgcn_cluster_load_async_to_lds_b32:
1629 case Intrinsic::amdgcn_cluster_load_async_to_lds_b64:
1630 case Intrinsic::amdgcn_cluster_load_async_to_lds_b128: {
1631 Info.opc = ISD::INTRINSIC_VOID;
1632 Info.memVT = EVT::getIntegerVT(CI.getContext(), getIntrMemWidth(IntrID));
1633 Info.ptrVal = CI.getArgOperand(1);
1635 return true;
1636 }
1637 case Intrinsic::amdgcn_global_store_async_from_lds_b8:
1638 case Intrinsic::amdgcn_global_store_async_from_lds_b32:
1639 case Intrinsic::amdgcn_global_store_async_from_lds_b64:
1640 case Intrinsic::amdgcn_global_store_async_from_lds_b128: {
1641 Info.opc = ISD::INTRINSIC_VOID;
1642 Info.memVT = EVT::getIntegerVT(CI.getContext(), getIntrMemWidth(IntrID));
1643 Info.ptrVal = CI.getArgOperand(0);
1645 return true;
1646 }
1647 case Intrinsic::amdgcn_load_to_lds:
1648 case Intrinsic::amdgcn_global_load_lds: {
1649 Info.opc = ISD::INTRINSIC_VOID;
1650 unsigned Width = cast<ConstantInt>(CI.getArgOperand(2))->getZExtValue();
1651 Info.memVT = EVT::getIntegerVT(CI.getContext(), Width * 8);
1652 Info.ptrVal = CI.getArgOperand(1);
1654 auto *Aux = cast<ConstantInt>(CI.getArgOperand(CI.arg_size() - 1));
1655 if (Aux->getZExtValue() & AMDGPU::CPol::VOLATILE)
1656 Info.flags |= MachineMemOperand::MOVolatile;
1657 return true;
1658 }
1659 case Intrinsic::amdgcn_ds_bvh_stack_rtn:
1660 case Intrinsic::amdgcn_ds_bvh_stack_push4_pop1_rtn:
1661 case Intrinsic::amdgcn_ds_bvh_stack_push8_pop1_rtn:
1662 case Intrinsic::amdgcn_ds_bvh_stack_push8_pop2_rtn: {
1663 Info.opc = ISD::INTRINSIC_W_CHAIN;
1664
1665 const GCNTargetMachine &TM =
1666 static_cast<const GCNTargetMachine &>(getTargetMachine());
1667
1669 Info.ptrVal = MFI->getGWSPSV(TM);
1670
1671 // This is an abstract access, but we need to specify a type and size.
1672 Info.memVT = MVT::i32;
1673 Info.size = 4;
1674 Info.align = Align(4);
1675
1677 return true;
1678 }
1679 case Intrinsic::amdgcn_s_prefetch_data:
1680 case Intrinsic::amdgcn_flat_prefetch:
1681 case Intrinsic::amdgcn_global_prefetch: {
1682 Info.opc = ISD::INTRINSIC_VOID;
1683 Info.memVT = EVT::getIntegerVT(CI.getContext(), 8);
1684 Info.ptrVal = CI.getArgOperand(0);
1685 Info.flags |= MachineMemOperand::MOLoad;
1686 return true;
1687 }
1688 default:
1689 return false;
1690 }
1691}
1692
1694 const CallInst &I, SmallVectorImpl<SDValue> &Ops, SelectionDAG &DAG) const {
1696 case Intrinsic::amdgcn_addrspacecast_nonnull: {
1697 // The DAG's ValueType loses the addrspaces.
1698 // Add them as 2 extra Constant operands "from" and "to".
1699 unsigned SrcAS = I.getOperand(0)->getType()->getPointerAddressSpace();
1700 unsigned DstAS = I.getType()->getPointerAddressSpace();
1701 Ops.push_back(DAG.getTargetConstant(SrcAS, SDLoc(), MVT::i32));
1702 Ops.push_back(DAG.getTargetConstant(DstAS, SDLoc(), MVT::i32));
1703 break;
1704 }
1705 default:
1706 break;
1707 }
1708}
1709
1712 Type *&AccessTy) const {
1713 Value *Ptr = nullptr;
1714 switch (II->getIntrinsicID()) {
1715 case Intrinsic::amdgcn_atomic_cond_sub_u32:
1716 case Intrinsic::amdgcn_cluster_load_b128:
1717 case Intrinsic::amdgcn_cluster_load_b64:
1718 case Intrinsic::amdgcn_cluster_load_b32:
1719 case Intrinsic::amdgcn_ds_append:
1720 case Intrinsic::amdgcn_ds_consume:
1721 case Intrinsic::amdgcn_ds_load_tr8_b64:
1722 case Intrinsic::amdgcn_ds_load_tr16_b128:
1723 case Intrinsic::amdgcn_ds_load_tr4_b64:
1724 case Intrinsic::amdgcn_ds_load_tr6_b96:
1725 case Intrinsic::amdgcn_ds_read_tr4_b64:
1726 case Intrinsic::amdgcn_ds_read_tr6_b96:
1727 case Intrinsic::amdgcn_ds_read_tr8_b64:
1728 case Intrinsic::amdgcn_ds_read_tr16_b64:
1729 case Intrinsic::amdgcn_ds_ordered_add:
1730 case Intrinsic::amdgcn_ds_ordered_swap:
1731 case Intrinsic::amdgcn_ds_atomic_async_barrier_arrive_b64:
1732 case Intrinsic::amdgcn_ds_atomic_barrier_arrive_rtn_b64:
1733 case Intrinsic::amdgcn_flat_atomic_fmax_num:
1734 case Intrinsic::amdgcn_flat_atomic_fmin_num:
1735 case Intrinsic::amdgcn_flat_load_monitor_b128:
1736 case Intrinsic::amdgcn_flat_load_monitor_b32:
1737 case Intrinsic::amdgcn_flat_load_monitor_b64:
1738 case Intrinsic::amdgcn_global_atomic_csub:
1739 case Intrinsic::amdgcn_global_atomic_fmax_num:
1740 case Intrinsic::amdgcn_global_atomic_fmin_num:
1741 case Intrinsic::amdgcn_global_atomic_ordered_add_b64:
1742 case Intrinsic::amdgcn_global_load_monitor_b128:
1743 case Intrinsic::amdgcn_global_load_monitor_b32:
1744 case Intrinsic::amdgcn_global_load_monitor_b64:
1745 case Intrinsic::amdgcn_global_load_tr_b64:
1746 case Intrinsic::amdgcn_global_load_tr_b128:
1747 case Intrinsic::amdgcn_global_load_tr4_b64:
1748 case Intrinsic::amdgcn_global_load_tr6_b96:
1749 case Intrinsic::amdgcn_global_store_async_from_lds_b8:
1750 case Intrinsic::amdgcn_global_store_async_from_lds_b32:
1751 case Intrinsic::amdgcn_global_store_async_from_lds_b64:
1752 case Intrinsic::amdgcn_global_store_async_from_lds_b128:
1753 Ptr = II->getArgOperand(0);
1754 break;
1755 case Intrinsic::amdgcn_load_to_lds:
1756 case Intrinsic::amdgcn_global_load_lds:
1757 case Intrinsic::amdgcn_global_load_async_to_lds_b8:
1758 case Intrinsic::amdgcn_global_load_async_to_lds_b32:
1759 case Intrinsic::amdgcn_global_load_async_to_lds_b64:
1760 case Intrinsic::amdgcn_global_load_async_to_lds_b128:
1761 case Intrinsic::amdgcn_cluster_load_async_to_lds_b8:
1762 case Intrinsic::amdgcn_cluster_load_async_to_lds_b32:
1763 case Intrinsic::amdgcn_cluster_load_async_to_lds_b64:
1764 case Intrinsic::amdgcn_cluster_load_async_to_lds_b128:
1765 Ptr = II->getArgOperand(1);
1766 break;
1767 default:
1768 return false;
1769 }
1770 AccessTy = II->getType();
1771 Ops.push_back(Ptr);
1772 return true;
1773}
1774
1776 unsigned AddrSpace) const {
1777 if (!Subtarget->hasFlatInstOffsets()) {
1778 // Flat instructions do not have offsets, and only have the register
1779 // address.
1780 return AM.BaseOffs == 0 && AM.Scale == 0;
1781 }
1782
1783 decltype(SIInstrFlags::FLAT) FlatVariant =
1787
1788 return AM.Scale == 0 &&
1789 (AM.BaseOffs == 0 || Subtarget->getInstrInfo()->isLegalFLATOffset(
1790 AM.BaseOffs, AddrSpace, FlatVariant));
1791}
1792
1794 if (Subtarget->hasFlatGlobalInsts())
1796
1797 if (!Subtarget->hasAddr64() || Subtarget->useFlatForGlobal()) {
1798 // Assume the we will use FLAT for all global memory accesses
1799 // on VI.
1800 // FIXME: This assumption is currently wrong. On VI we still use
1801 // MUBUF instructions for the r + i addressing mode. As currently
1802 // implemented, the MUBUF instructions only work on buffer < 4GB.
1803 // It may be possible to support > 4GB buffers with MUBUF instructions,
1804 // by setting the stride value in the resource descriptor which would
1805 // increase the size limit to (stride * 4GB). However, this is risky,
1806 // because it has never been validated.
1808 }
1809
1810 return isLegalMUBUFAddressingMode(AM);
1811}
1812
1813bool SITargetLowering::isLegalMUBUFAddressingMode(const AddrMode &AM) const {
1814 // MUBUF / MTBUF instructions have a 12-bit unsigned byte offset, and
1815 // additionally can do r + r + i with addr64. 32-bit has more addressing
1816 // mode options. Depending on the resource constant, it can also do
1817 // (i64 r0) + (i32 r1) * (i14 i).
1818 //
1819 // Private arrays end up using a scratch buffer most of the time, so also
1820 // assume those use MUBUF instructions. Scratch loads / stores are currently
1821 // implemented as mubuf instructions with offen bit set, so slightly
1822 // different than the normal addr64.
1823 const SIInstrInfo *TII = Subtarget->getInstrInfo();
1824 if (!TII->isLegalMUBUFImmOffset(AM.BaseOffs))
1825 return false;
1826
1827 // FIXME: Since we can split immediate into soffset and immediate offset,
1828 // would it make sense to allow any immediate?
1829
1830 switch (AM.Scale) {
1831 case 0: // r + i or just i, depending on HasBaseReg.
1832 return true;
1833 case 1:
1834 return true; // We have r + r or r + i.
1835 case 2:
1836 if (AM.HasBaseReg) {
1837 // Reject 2 * r + r.
1838 return false;
1839 }
1840
1841 // Allow 2 * r as r + r
1842 // Or 2 * r + i is allowed as r + r + i.
1843 return true;
1844 default: // Don't allow n * r
1845 return false;
1846 }
1847}
1848
1850 const AddrMode &AM, Type *Ty,
1851 unsigned AS,
1852 Instruction *I) const {
1853 // No global is ever allowed as a base.
1854 if (AM.BaseGV)
1855 return false;
1856
1857 if (AS == AMDGPUAS::GLOBAL_ADDRESS)
1858 return isLegalGlobalAddressingMode(AM);
1859
1860 if (AS == AMDGPUAS::CONSTANT_ADDRESS ||
1864 // If the offset isn't a multiple of 4, it probably isn't going to be
1865 // correctly aligned.
1866 // FIXME: Can we get the real alignment here?
1867 if (AM.BaseOffs % 4 != 0)
1868 return isLegalMUBUFAddressingMode(AM);
1869
1870 if (!Subtarget->hasScalarSubwordLoads()) {
1871 // There are no SMRD extloads, so if we have to do a small type access we
1872 // will use a MUBUF load.
1873 // FIXME?: We also need to do this if unaligned, but we don't know the
1874 // alignment here.
1875 if (Ty->isSized() && DL.getTypeStoreSize(Ty) < 4)
1876 return isLegalGlobalAddressingMode(AM);
1877 }
1878
1879 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) {
1880 // SMRD instructions have an 8-bit, dword offset on SI.
1881 if (!isUInt<8>(AM.BaseOffs / 4))
1882 return false;
1883 } else if (Subtarget->getGeneration() == AMDGPUSubtarget::SEA_ISLANDS) {
1884 // On CI+, this can also be a 32-bit literal constant offset. If it fits
1885 // in 8-bits, it can use a smaller encoding.
1886 if (!isUInt<32>(AM.BaseOffs / 4))
1887 return false;
1888 } else if (Subtarget->getGeneration() < AMDGPUSubtarget::GFX9) {
1889 // On VI, these use the SMEM format and the offset is 20-bit in bytes.
1890 if (!isUInt<20>(AM.BaseOffs))
1891 return false;
1892 } else if (Subtarget->getGeneration() < AMDGPUSubtarget::GFX12) {
1893 // On GFX9 the offset is signed 21-bit in bytes (but must not be negative
1894 // for S_BUFFER_* instructions).
1895 if (!isInt<21>(AM.BaseOffs))
1896 return false;
1897 } else {
1898 // On GFX12, all offsets are signed 24-bit in bytes.
1899 if (!isInt<24>(AM.BaseOffs))
1900 return false;
1901 }
1902
1903 if ((AS == AMDGPUAS::CONSTANT_ADDRESS ||
1905 AM.BaseOffs < 0) {
1906 // Scalar (non-buffer) loads can only use a negative offset if
1907 // soffset+offset is non-negative. Since the compiler can only prove that
1908 // in a few special cases, it is safer to claim that negative offsets are
1909 // not supported.
1910 return false;
1911 }
1912
1913 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
1914 return true;
1915
1916 if (AM.Scale == 1 && AM.HasBaseReg)
1917 return true;
1918
1919 return false;
1920 }
1921
1922 if (AS == AMDGPUAS::PRIVATE_ADDRESS)
1923 return Subtarget->enableFlatScratch()
1925 : isLegalMUBUFAddressingMode(AM);
1926
1927 if (AS == AMDGPUAS::LOCAL_ADDRESS ||
1928 (AS == AMDGPUAS::REGION_ADDRESS && Subtarget->hasGDS())) {
1929 // Basic, single offset DS instructions allow a 16-bit unsigned immediate
1930 // field.
1931 // XXX - If doing a 4-byte aligned 8-byte type access, we effectively have
1932 // an 8-bit dword offset but we don't know the alignment here.
1933 if (!isUInt<16>(AM.BaseOffs))
1934 return false;
1935
1936 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
1937 return true;
1938
1939 if (AM.Scale == 1 && AM.HasBaseReg)
1940 return true;
1941
1942 return false;
1943 }
1944
1946 // For an unknown address space, this usually means that this is for some
1947 // reason being used for pure arithmetic, and not based on some addressing
1948 // computation. We don't have instructions that compute pointers with any
1949 // addressing modes, so treat them as having no offset like flat
1950 // instructions.
1952 }
1953
1954 // Assume a user alias of global for unknown address spaces.
1955 return isLegalGlobalAddressingMode(AM);
1956}
1957
1959 const MachineFunction &MF) const {
1961 return (MemVT.getSizeInBits() <= 4 * 32);
1962 if (AS == AMDGPUAS::PRIVATE_ADDRESS) {
1963 unsigned MaxPrivateBits = 8 * getSubtarget()->getMaxPrivateElementSize();
1964 return (MemVT.getSizeInBits() <= MaxPrivateBits);
1965 }
1967 return (MemVT.getSizeInBits() <= 2 * 32);
1968 return true;
1969}
1970
1972 unsigned Size, unsigned AddrSpace, Align Alignment,
1973 MachineMemOperand::Flags Flags, unsigned *IsFast) const {
1974 if (IsFast)
1975 *IsFast = 0;
1976
1977 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS ||
1978 AddrSpace == AMDGPUAS::REGION_ADDRESS) {
1979 // Check if alignment requirements for ds_read/write instructions are
1980 // disabled.
1981 if (!Subtarget->hasUnalignedDSAccessEnabled() && Alignment < Align(4))
1982 return false;
1983
1984 Align RequiredAlignment(
1985 PowerOf2Ceil(divideCeil(Size, 8))); // Natural alignment.
1986 if (Subtarget->hasLDSMisalignedBug() && Size > 32 &&
1987 Alignment < RequiredAlignment)
1988 return false;
1989
1990 // Either, the alignment requirements are "enabled", or there is an
1991 // unaligned LDS access related hardware bug though alignment requirements
1992 // are "disabled". In either case, we need to check for proper alignment
1993 // requirements.
1994 //
1995 switch (Size) {
1996 case 64:
1997 // SI has a hardware bug in the LDS / GDS bounds checking: if the base
1998 // address is negative, then the instruction is incorrectly treated as
1999 // out-of-bounds even if base + offsets is in bounds. Split vectorized
2000 // loads here to avoid emitting ds_read2_b32. We may re-combine the
2001 // load later in the SILoadStoreOptimizer.
2002 if (!Subtarget->hasUsableDSOffset() && Alignment < Align(8))
2003 return false;
2004
2005 // 8 byte accessing via ds_read/write_b64 require 8-byte alignment, but we
2006 // can do a 4 byte aligned, 8 byte access in a single operation using
2007 // ds_read2/write2_b32 with adjacent offsets.
2008 RequiredAlignment = Align(4);
2009
2010 if (Subtarget->hasUnalignedDSAccessEnabled()) {
2011 // We will either select ds_read_b64/ds_write_b64 or ds_read2_b32/
2012 // ds_write2_b32 depending on the alignment. In either case with either
2013 // alignment there is no faster way of doing this.
2014
2015 // The numbers returned here and below are not additive, it is a 'speed
2016 // rank'. They are just meant to be compared to decide if a certain way
2017 // of lowering an operation is faster than another. For that purpose
2018 // naturally aligned operation gets it bitsize to indicate that "it
2019 // operates with a speed comparable to N-bit wide load". With the full
2020 // alignment ds128 is slower than ds96 for example. If underaligned it
2021 // is comparable to a speed of a single dword access, which would then
2022 // mean 32 < 128 and it is faster to issue a wide load regardless.
2023 // 1 is simply "slow, don't do it". I.e. comparing an aligned load to a
2024 // wider load which will not be aligned anymore the latter is slower.
2025 if (IsFast)
2026 *IsFast = (Alignment >= RequiredAlignment) ? 64
2027 : (Alignment < Align(4)) ? 32
2028 : 1;
2029 return true;
2030 }
2031
2032 break;
2033 case 96:
2034 if (!Subtarget->hasDS96AndDS128())
2035 return false;
2036
2037 // 12 byte accessing via ds_read/write_b96 require 16-byte alignment on
2038 // gfx8 and older.
2039
2040 if (Subtarget->hasUnalignedDSAccessEnabled()) {
2041 // Naturally aligned access is fastest. However, also report it is Fast
2042 // if memory is aligned less than DWORD. A narrow load or store will be
2043 // be equally slow as a single ds_read_b96/ds_write_b96, but there will
2044 // be more of them, so overall we will pay less penalty issuing a single
2045 // instruction.
2046
2047 // See comment on the values above.
2048 if (IsFast)
2049 *IsFast = (Alignment >= RequiredAlignment) ? 96
2050 : (Alignment < Align(4)) ? 32
2051 : 1;
2052 return true;
2053 }
2054
2055 break;
2056 case 128:
2057 if (!Subtarget->hasDS96AndDS128() || !Subtarget->useDS128())
2058 return false;
2059
2060 // 16 byte accessing via ds_read/write_b128 require 16-byte alignment on
2061 // gfx8 and older, but we can do a 8 byte aligned, 16 byte access in a
2062 // single operation using ds_read2/write2_b64.
2063 RequiredAlignment = Align(8);
2064
2065 if (Subtarget->hasUnalignedDSAccessEnabled()) {
2066 // Naturally aligned access is fastest. However, also report it is Fast
2067 // if memory is aligned less than DWORD. A narrow load or store will be
2068 // be equally slow as a single ds_read_b128/ds_write_b128, but there
2069 // will be more of them, so overall we will pay less penalty issuing a
2070 // single instruction.
2071
2072 // See comment on the values above.
2073 if (IsFast)
2074 *IsFast = (Alignment >= RequiredAlignment) ? 128
2075 : (Alignment < Align(4)) ? 32
2076 : 1;
2077 return true;
2078 }
2079
2080 break;
2081 default:
2082 if (Size > 32)
2083 return false;
2084
2085 break;
2086 }
2087
2088 // See comment on the values above.
2089 // Note that we have a single-dword or sub-dword here, so if underaligned
2090 // it is a slowest possible access, hence returned value is 0.
2091 if (IsFast)
2092 *IsFast = (Alignment >= RequiredAlignment) ? Size : 0;
2093
2094 return Alignment >= RequiredAlignment ||
2095 Subtarget->hasUnalignedDSAccessEnabled();
2096 }
2097
2098 // FIXME: We have to be conservative here and assume that flat operations
2099 // will access scratch. If we had access to the IR function, then we
2100 // could determine if any private memory was used in the function.
2101 if (AddrSpace == AMDGPUAS::PRIVATE_ADDRESS ||
2102 AddrSpace == AMDGPUAS::FLAT_ADDRESS) {
2103 bool AlignedBy4 = Alignment >= Align(4);
2104 if (Subtarget->hasUnalignedScratchAccessEnabled()) {
2105 if (IsFast)
2106 *IsFast = AlignedBy4 ? Size : 1;
2107 return true;
2108 }
2109
2110 if (IsFast)
2111 *IsFast = AlignedBy4;
2112
2113 return AlignedBy4;
2114 }
2115
2116 // So long as they are correct, wide global memory operations perform better
2117 // than multiple smaller memory ops -- even when misaligned
2118 if (AMDGPU::isExtendedGlobalAddrSpace(AddrSpace)) {
2119 if (IsFast)
2120 *IsFast = Size;
2121
2122 return Alignment >= Align(4) ||
2123 Subtarget->hasUnalignedBufferAccessEnabled();
2124 }
2125
2126 // Ensure robust out-of-bounds guarantees for buffer accesses are met if
2127 // RelaxedBufferOOBMode is disabled. Normally hardware will ensure proper
2128 // out-of-bounds behavior, but in the edge case where an access starts
2129 // out-of-bounds and then enter in-bounds, the entire access would be treated
2130 // as out-of-bounds. Prevent misaligned memory accesses by requiring the
2131 // natural alignment of buffer accesses.
2132 if (AddrSpace == AMDGPUAS::BUFFER_FAT_POINTER ||
2133 AddrSpace == AMDGPUAS::BUFFER_RESOURCE ||
2134 AddrSpace == AMDGPUAS::BUFFER_STRIDED_POINTER) {
2135 if (!Subtarget->hasRelaxedBufferOOBMode() &&
2136 Alignment < Align(PowerOf2Ceil(divideCeil(Size, 8))))
2137 return false;
2138 }
2139
2140 // Smaller than dword value must be aligned.
2141 if (Size < 32)
2142 return false;
2143
2144 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
2145 // byte-address are ignored, thus forcing Dword alignment.
2146 // This applies to private, global, and constant memory.
2147 if (IsFast)
2148 *IsFast = 1;
2149
2150 return Size >= 32 && Alignment >= Align(4);
2151}
2152
2154 EVT VT, unsigned AddrSpace, Align Alignment, MachineMemOperand::Flags Flags,
2155 unsigned *IsFast) const {
2157 Alignment, Flags, IsFast);
2158}
2159
2161 LLVMContext &Context, const MemOp &Op,
2162 const AttributeList &FuncAttributes) const {
2163 // FIXME: Should account for address space here.
2164
2165 // The default fallback uses the private pointer size as a guess for a type to
2166 // use. Make sure we switch these to 64-bit accesses.
2167
2168 if (Op.size() >= 16 &&
2169 Op.isDstAligned(Align(4))) // XXX: Should only do for global
2170 return MVT::v4i32;
2171
2172 if (Op.size() >= 8 && Op.isDstAligned(Align(4)))
2173 return MVT::v2i32;
2174
2175 // Use the default.
2176 return MVT::Other;
2177}
2178
2180 const MemSDNode *MemNode = cast<MemSDNode>(N);
2181 return MemNode->getMemOperand()->getFlags() & MONoClobber;
2182}
2183
2188
2190 unsigned DestAS) const {
2191 if (SrcAS == AMDGPUAS::FLAT_ADDRESS) {
2192 if (DestAS == AMDGPUAS::PRIVATE_ADDRESS &&
2193 Subtarget->hasGloballyAddressableScratch()) {
2194 // Flat -> private requires subtracting src_flat_scratch_base_lo.
2195 return false;
2196 }
2197
2198 // Flat -> private/local is a simple truncate.
2199 // Flat -> global is no-op
2200 return true;
2201 }
2202
2203 const GCNTargetMachine &TM =
2204 static_cast<const GCNTargetMachine &>(getTargetMachine());
2205 return TM.isNoopAddrSpaceCast(SrcAS, DestAS);
2206}
2207
2215
2217 Type *Ty) const {
2218 // FIXME: Could be smarter if called for vector constants.
2219 return true;
2220}
2221
2223 unsigned Index) const {
2225 return false;
2226
2227 // TODO: Add more cases that are cheap.
2228 return Index == 0;
2229}
2230
2231bool SITargetLowering::isExtractVecEltCheap(EVT VT, unsigned Index) const {
2232 // TODO: This should be more aggressive, particular for 16-bit element
2233 // vectors. However there are some mixed improvements and regressions.
2234 EVT EltTy = VT.getVectorElementType();
2235 return EltTy.getSizeInBits() % 32 == 0;
2236}
2237
2239 if (Subtarget->has16BitInsts() && VT == MVT::i16) {
2240 switch (Op) {
2241 case ISD::LOAD:
2242 case ISD::STORE:
2243 return true;
2244 default:
2245 return false;
2246 }
2247 }
2248
2249 // SimplifySetCC uses this function to determine whether or not it should
2250 // create setcc with i1 operands. We don't have instructions for i1 setcc.
2251 if (VT == MVT::i1 && Op == ISD::SETCC)
2252 return false;
2253
2255}
2256
2257SDValue SITargetLowering::lowerKernArgParameterPtr(SelectionDAG &DAG,
2258 const SDLoc &SL,
2259 SDValue Chain,
2260 uint64_t Offset) const {
2261 const DataLayout &DL = DAG.getDataLayout();
2265
2266 auto [InputPtrReg, RC, ArgTy] =
2267 Info->getPreloadedValue(AMDGPUFunctionArgInfo::KERNARG_SEGMENT_PTR);
2268
2269 // We may not have the kernarg segment argument if we have no kernel
2270 // arguments.
2271 if (!InputPtrReg)
2272 return DAG.getConstant(Offset, SL, PtrVT);
2273
2275 SDValue BasePtr = DAG.getCopyFromReg(
2276 Chain, SL, MRI.getLiveInVirtReg(InputPtrReg->getRegister()), PtrVT);
2277
2278 return DAG.getObjectPtrOffset(SL, BasePtr, TypeSize::getFixed(Offset));
2279}
2280
2281SDValue SITargetLowering::getImplicitArgPtr(SelectionDAG &DAG,
2282 const SDLoc &SL) const {
2285 return lowerKernArgParameterPtr(DAG, SL, DAG.getEntryNode(), Offset);
2286}
2287
2288SDValue SITargetLowering::getLDSKernelId(SelectionDAG &DAG,
2289 const SDLoc &SL) const {
2290
2292 std::optional<uint32_t> KnownSize =
2294 if (KnownSize.has_value())
2295 return DAG.getConstant(*KnownSize, SL, MVT::i32);
2296 return SDValue();
2297}
2298
2299SDValue SITargetLowering::convertArgType(SelectionDAG &DAG, EVT VT, EVT MemVT,
2300 const SDLoc &SL, SDValue Val,
2301 bool Signed,
2302 const ISD::InputArg *Arg) const {
2303 // First, if it is a widened vector, narrow it.
2304 if (VT.isVector() &&
2306 EVT NarrowedVT =
2309 Val = DAG.getNode(ISD::EXTRACT_SUBVECTOR, SL, NarrowedVT, Val,
2310 DAG.getConstant(0, SL, MVT::i32));
2311 }
2312
2313 // Then convert the vector elements or scalar value.
2314 if (Arg && (Arg->Flags.isSExt() || Arg->Flags.isZExt()) && VT.bitsLT(MemVT)) {
2315 unsigned Opc = Arg->Flags.isZExt() ? ISD::AssertZext : ISD::AssertSext;
2316 Val = DAG.getNode(Opc, SL, MemVT, Val, DAG.getValueType(VT));
2317 }
2318
2319 if (MemVT.isFloatingPoint())
2320 Val = getFPExtOrFPRound(DAG, Val, SL, VT);
2321 else if (Signed)
2322 Val = DAG.getSExtOrTrunc(Val, SL, VT);
2323 else
2324 Val = DAG.getZExtOrTrunc(Val, SL, VT);
2325
2326 return Val;
2327}
2328
2329SDValue SITargetLowering::lowerKernargMemParameter(
2330 SelectionDAG &DAG, EVT VT, EVT MemVT, const SDLoc &SL, SDValue Chain,
2331 uint64_t Offset, Align Alignment, bool Signed,
2332 const ISD::InputArg *Arg) const {
2333 MachinePointerInfo PtrInfo(AMDGPUAS::CONSTANT_ADDRESS);
2334
2335 // Try to avoid using an extload by loading earlier than the argument address,
2336 // and extracting the relevant bits. The load should hopefully be merged with
2337 // the previous argument.
2338 if (MemVT.getStoreSize() < 4 && Alignment < 4) {
2339 // TODO: Handle align < 4 and size >= 4 (can happen with packed structs).
2340 int64_t AlignDownOffset = alignDown(Offset, 4);
2341 int64_t OffsetDiff = Offset - AlignDownOffset;
2342
2343 EVT IntVT = MemVT.changeTypeToInteger();
2344
2345 // TODO: If we passed in the base kernel offset we could have a better
2346 // alignment than 4, but we don't really need it.
2347 SDValue Ptr = lowerKernArgParameterPtr(DAG, SL, Chain, AlignDownOffset);
2348 SDValue Load = DAG.getLoad(MVT::i32, SL, Chain, Ptr, PtrInfo, Align(4),
2351
2352 SDValue ShiftAmt = DAG.getConstant(OffsetDiff * 8, SL, MVT::i32);
2353 SDValue Extract = DAG.getNode(ISD::SRL, SL, MVT::i32, Load, ShiftAmt);
2354
2355 SDValue ArgVal = DAG.getNode(ISD::TRUNCATE, SL, IntVT, Extract);
2356 ArgVal = DAG.getNode(ISD::BITCAST, SL, MemVT, ArgVal);
2357 ArgVal = convertArgType(DAG, VT, MemVT, SL, ArgVal, Signed, Arg);
2358
2359 return DAG.getMergeValues({ArgVal, Load.getValue(1)}, SL);
2360 }
2361
2362 SDValue Ptr = lowerKernArgParameterPtr(DAG, SL, Chain, Offset);
2363 SDValue Load = DAG.getLoad(MemVT, SL, Chain, Ptr, PtrInfo, Alignment,
2366
2367 SDValue Val = convertArgType(DAG, VT, MemVT, SL, Load, Signed, Arg);
2368 return DAG.getMergeValues({Val, Load.getValue(1)}, SL);
2369}
2370
2371/// Coerce an argument which was passed in a different ABI type to the original
2372/// expected value type.
2373SDValue SITargetLowering::convertABITypeToValueType(SelectionDAG &DAG,
2374 SDValue Val,
2375 CCValAssign &VA,
2376 const SDLoc &SL) const {
2377 EVT ValVT = VA.getValVT();
2378
2379 // If this is an 8 or 16-bit value, it is really passed promoted
2380 // to 32 bits. Insert an assert[sz]ext to capture this, then
2381 // truncate to the right size.
2382 switch (VA.getLocInfo()) {
2383 case CCValAssign::Full:
2384 return Val;
2385 case CCValAssign::BCvt:
2386 return DAG.getNode(ISD::BITCAST, SL, ValVT, Val);
2387 case CCValAssign::SExt:
2388 Val = DAG.getNode(ISD::AssertSext, SL, VA.getLocVT(), Val,
2389 DAG.getValueType(ValVT));
2390 return DAG.getNode(ISD::TRUNCATE, SL, ValVT, Val);
2391 case CCValAssign::ZExt:
2392 Val = DAG.getNode(ISD::AssertZext, SL, VA.getLocVT(), Val,
2393 DAG.getValueType(ValVT));
2394 return DAG.getNode(ISD::TRUNCATE, SL, ValVT, Val);
2395 case CCValAssign::AExt:
2396 return DAG.getNode(ISD::TRUNCATE, SL, ValVT, Val);
2397 default:
2398 llvm_unreachable("Unknown loc info!");
2399 }
2400}
2401
2402SDValue SITargetLowering::lowerStackParameter(SelectionDAG &DAG,
2403 CCValAssign &VA, const SDLoc &SL,
2404 SDValue Chain,
2405 const ISD::InputArg &Arg) const {
2406 MachineFunction &MF = DAG.getMachineFunction();
2407 MachineFrameInfo &MFI = MF.getFrameInfo();
2408
2409 if (Arg.Flags.isByVal()) {
2410 unsigned Size = Arg.Flags.getByValSize();
2411 int FrameIdx = MFI.CreateFixedObject(Size, VA.getLocMemOffset(), false);
2412 return DAG.getFrameIndex(FrameIdx, MVT::i32);
2413 }
2414
2415 unsigned ArgOffset = VA.getLocMemOffset();
2416 unsigned ArgSize = VA.getValVT().getStoreSize();
2417
2418 int FI = MFI.CreateFixedObject(ArgSize, ArgOffset, true);
2419
2420 // Create load nodes to retrieve arguments from the stack.
2421 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
2422
2423 // For NON_EXTLOAD, generic code in getLoad assert(ValVT == MemVT)
2425 MVT MemVT = VA.getValVT();
2426
2427 switch (VA.getLocInfo()) {
2428 default:
2429 break;
2430 case CCValAssign::BCvt:
2431 MemVT = VA.getLocVT();
2432 break;
2433 case CCValAssign::SExt:
2434 ExtType = ISD::SEXTLOAD;
2435 break;
2436 case CCValAssign::ZExt:
2437 ExtType = ISD::ZEXTLOAD;
2438 break;
2439 case CCValAssign::AExt:
2440 ExtType = ISD::EXTLOAD;
2441 break;
2442 }
2443
2444 SDValue ArgValue = DAG.getExtLoad(
2445 ExtType, SL, VA.getLocVT(), Chain, FIN,
2447
2448 SDValue ConvertedVal = convertABITypeToValueType(DAG, ArgValue, VA, SL);
2449 if (ConvertedVal == ArgValue)
2450 return ConvertedVal;
2451
2452 return DAG.getMergeValues({ConvertedVal, ArgValue.getValue(1)}, SL);
2453}
2454
2455SDValue SITargetLowering::lowerWorkGroupId(
2456 SelectionDAG &DAG, const SIMachineFunctionInfo &MFI, EVT VT,
2459 AMDGPUFunctionArgInfo::PreloadedValue ClusterWorkGroupIdPV) const {
2460 if (!Subtarget->hasClusters())
2461 return getPreloadedValue(DAG, MFI, VT, WorkGroupIdPV);
2462
2463 // Clusters are supported. Return the global position in the grid. If clusters
2464 // are enabled, WorkGroupIdPV returns the cluster ID not the workgroup ID.
2465
2466 // WorkGroupIdXYZ = ClusterId == 0 ?
2467 // ClusterIdXYZ :
2468 // ClusterIdXYZ * (ClusterMaxIdXYZ + 1) + ClusterWorkGroupIdXYZ
2469 SDValue ClusterIdXYZ = getPreloadedValue(DAG, MFI, VT, WorkGroupIdPV);
2470 SDLoc SL(ClusterIdXYZ);
2471 SDValue ClusterMaxIdXYZ = getPreloadedValue(DAG, MFI, VT, ClusterMaxIdPV);
2472 SDValue One = DAG.getConstant(1, SL, VT);
2473 SDValue ClusterSizeXYZ = DAG.getNode(ISD::ADD, SL, VT, ClusterMaxIdXYZ, One);
2474 SDValue ClusterWorkGroupIdXYZ =
2475 getPreloadedValue(DAG, MFI, VT, ClusterWorkGroupIdPV);
2476 SDValue GlobalIdXYZ =
2477 DAG.getNode(ISD::ADD, SL, VT, ClusterWorkGroupIdXYZ,
2478 DAG.getNode(ISD::MUL, SL, VT, ClusterIdXYZ, ClusterSizeXYZ));
2479
2480 switch (MFI.getClusterDims().getKind()) {
2483 return GlobalIdXYZ;
2485 return ClusterIdXYZ;
2487 using namespace AMDGPU::Hwreg;
2488 SDValue ClusterIdField =
2489 DAG.getTargetConstant(HwregEncoding::encode(ID_IB_STS2, 6, 4), SL, VT);
2490 SDNode *GetReg =
2491 DAG.getMachineNode(AMDGPU::S_GETREG_B32_const, SL, VT, ClusterIdField);
2492 SDValue ClusterId(GetReg, 0);
2493 SDValue Zero = DAG.getConstant(0, SL, VT);
2494 return DAG.getNode(ISD::SELECT_CC, SL, VT, ClusterId, Zero, ClusterIdXYZ,
2495 GlobalIdXYZ, DAG.getCondCode(ISD::SETEQ));
2496 }
2497 }
2498
2499 llvm_unreachable("nothing should reach here");
2500}
2501
2502SDValue SITargetLowering::getPreloadedValue(
2503 SelectionDAG &DAG, const SIMachineFunctionInfo &MFI, EVT VT,
2505 const ArgDescriptor *Reg = nullptr;
2506 const TargetRegisterClass *RC;
2507 LLT Ty;
2508
2510 const ArgDescriptor WorkGroupIDX =
2511 ArgDescriptor::createRegister(AMDGPU::TTMP9);
2512 // If GridZ is not programmed in an entry function then the hardware will set
2513 // it to all zeros, so there is no need to mask the GridY value in the low
2514 // order bits.
2515 const ArgDescriptor WorkGroupIDY = ArgDescriptor::createRegister(
2516 AMDGPU::TTMP7,
2517 AMDGPU::isEntryFunctionCC(CC) && !MFI.hasWorkGroupIDZ() ? ~0u : 0xFFFFu);
2518 const ArgDescriptor WorkGroupIDZ =
2519 ArgDescriptor::createRegister(AMDGPU::TTMP7, 0xFFFF0000u);
2520 const ArgDescriptor ClusterWorkGroupIDX =
2521 ArgDescriptor::createRegister(AMDGPU::TTMP6, 0x0000000Fu);
2522 const ArgDescriptor ClusterWorkGroupIDY =
2523 ArgDescriptor::createRegister(AMDGPU::TTMP6, 0x000000F0u);
2524 const ArgDescriptor ClusterWorkGroupIDZ =
2525 ArgDescriptor::createRegister(AMDGPU::TTMP6, 0x00000F00u);
2526 const ArgDescriptor ClusterWorkGroupMaxIDX =
2527 ArgDescriptor::createRegister(AMDGPU::TTMP6, 0x0000F000u);
2528 const ArgDescriptor ClusterWorkGroupMaxIDY =
2529 ArgDescriptor::createRegister(AMDGPU::TTMP6, 0x000F0000u);
2530 const ArgDescriptor ClusterWorkGroupMaxIDZ =
2531 ArgDescriptor::createRegister(AMDGPU::TTMP6, 0x00F00000u);
2532 const ArgDescriptor ClusterWorkGroupMaxFlatID =
2533 ArgDescriptor::createRegister(AMDGPU::TTMP6, 0x0F000000u);
2534
2535 auto LoadConstant = [&](unsigned N) {
2536 return DAG.getConstant(N, SDLoc(), VT);
2537 };
2538
2539 if (Subtarget->hasArchitectedSGPRs() &&
2541 AMDGPU::ClusterDimsAttr ClusterDims = MFI.getClusterDims();
2542 bool HasFixedDims = ClusterDims.isFixedDims();
2543
2544 switch (PVID) {
2546 Reg = &WorkGroupIDX;
2547 RC = &AMDGPU::SReg_32RegClass;
2548 Ty = LLT::scalar(32);
2549 break;
2551 Reg = &WorkGroupIDY;
2552 RC = &AMDGPU::SReg_32RegClass;
2553 Ty = LLT::scalar(32);
2554 break;
2556 Reg = &WorkGroupIDZ;
2557 RC = &AMDGPU::SReg_32RegClass;
2558 Ty = LLT::scalar(32);
2559 break;
2561 if (HasFixedDims && ClusterDims.getDims()[0] == 1)
2562 return LoadConstant(0);
2563 Reg = &ClusterWorkGroupIDX;
2564 RC = &AMDGPU::SReg_32RegClass;
2565 Ty = LLT::scalar(32);
2566 break;
2568 if (HasFixedDims && ClusterDims.getDims()[1] == 1)
2569 return LoadConstant(0);
2570 Reg = &ClusterWorkGroupIDY;
2571 RC = &AMDGPU::SReg_32RegClass;
2572 Ty = LLT::scalar(32);
2573 break;
2575 if (HasFixedDims && ClusterDims.getDims()[2] == 1)
2576 return LoadConstant(0);
2577 Reg = &ClusterWorkGroupIDZ;
2578 RC = &AMDGPU::SReg_32RegClass;
2579 Ty = LLT::scalar(32);
2580 break;
2582 if (HasFixedDims)
2583 return LoadConstant(ClusterDims.getDims()[0] - 1);
2584 Reg = &ClusterWorkGroupMaxIDX;
2585 RC = &AMDGPU::SReg_32RegClass;
2586 Ty = LLT::scalar(32);
2587 break;
2589 if (HasFixedDims)
2590 return LoadConstant(ClusterDims.getDims()[1] - 1);
2591 Reg = &ClusterWorkGroupMaxIDY;
2592 RC = &AMDGPU::SReg_32RegClass;
2593 Ty = LLT::scalar(32);
2594 break;
2596 if (HasFixedDims)
2597 return LoadConstant(ClusterDims.getDims()[2] - 1);
2598 Reg = &ClusterWorkGroupMaxIDZ;
2599 RC = &AMDGPU::SReg_32RegClass;
2600 Ty = LLT::scalar(32);
2601 break;
2603 Reg = &ClusterWorkGroupMaxFlatID;
2604 RC = &AMDGPU::SReg_32RegClass;
2605 Ty = LLT::scalar(32);
2606 break;
2607 default:
2608 break;
2609 }
2610 }
2611
2612 if (!Reg)
2613 std::tie(Reg, RC, Ty) = MFI.getPreloadedValue(PVID);
2614 if (!Reg) {
2616 // It's possible for a kernarg intrinsic call to appear in a kernel with
2617 // no allocated segment, in which case we do not add the user sgpr
2618 // argument, so just return null.
2619 return DAG.getConstant(0, SDLoc(), VT);
2620 }
2621
2622 // It's undefined behavior if a function marked with the amdgpu-no-*
2623 // attributes uses the corresponding intrinsic.
2624 return DAG.getPOISON(VT);
2625 }
2626
2627 return loadInputValue(DAG, RC, VT, SDLoc(DAG.getEntryNode()), *Reg);
2628}
2629
2631 CallingConv::ID CallConv,
2632 ArrayRef<ISD::InputArg> Ins, BitVector &Skipped,
2633 FunctionType *FType,
2635 for (unsigned I = 0, E = Ins.size(), PSInputNum = 0; I != E; ++I) {
2636 const ISD::InputArg *Arg = &Ins[I];
2637
2638 assert((!Arg->VT.isVector() || Arg->VT.getScalarSizeInBits() == 16) &&
2639 "vector type argument should have been split");
2640
2641 // First check if it's a PS input addr.
2642 if (CallConv == CallingConv::AMDGPU_PS && !Arg->Flags.isInReg() &&
2643 PSInputNum <= 15) {
2644 bool SkipArg = !Arg->Used && !Info->isPSInputAllocated(PSInputNum);
2645
2646 // Inconveniently only the first part of the split is marked as isSplit,
2647 // so skip to the end. We only want to increment PSInputNum once for the
2648 // entire split argument.
2649 if (Arg->Flags.isSplit()) {
2650 while (!Arg->Flags.isSplitEnd()) {
2651 assert((!Arg->VT.isVector() || Arg->VT.getScalarSizeInBits() == 16) &&
2652 "unexpected vector split in ps argument type");
2653 if (!SkipArg)
2654 Splits.push_back(*Arg);
2655 Arg = &Ins[++I];
2656 }
2657 }
2658
2659 if (SkipArg) {
2660 // We can safely skip PS inputs.
2661 Skipped.set(Arg->getOrigArgIndex());
2662 ++PSInputNum;
2663 continue;
2664 }
2665
2666 Info->markPSInputAllocated(PSInputNum);
2667 if (Arg->Used)
2668 Info->markPSInputEnabled(PSInputNum);
2669
2670 ++PSInputNum;
2671 }
2672
2673 Splits.push_back(*Arg);
2674 }
2675}
2676
2677// Allocate special inputs passed in VGPRs.
2679 CCState &CCInfo, MachineFunction &MF, const SIRegisterInfo &TRI,
2680 SIMachineFunctionInfo &Info) const {
2681 const LLT S32 = LLT::scalar(32);
2683
2684 if (Info.hasWorkItemIDX()) {
2685 Register Reg = AMDGPU::VGPR0;
2686 MRI.setType(MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass), S32);
2687
2688 CCInfo.AllocateReg(Reg);
2689 unsigned Mask =
2690 (Subtarget->hasPackedTID() && Info.hasWorkItemIDY()) ? 0x3ff : ~0u;
2691 Info.setWorkItemIDX(ArgDescriptor::createRegister(Reg, Mask));
2692 }
2693
2694 if (Info.hasWorkItemIDY()) {
2695 assert(Info.hasWorkItemIDX());
2696 if (Subtarget->hasPackedTID()) {
2697 Info.setWorkItemIDY(
2698 ArgDescriptor::createRegister(AMDGPU::VGPR0, 0x3ff << 10));
2699 } else {
2700 unsigned Reg = AMDGPU::VGPR1;
2701 MRI.setType(MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass), S32);
2702
2703 CCInfo.AllocateReg(Reg);
2704 Info.setWorkItemIDY(ArgDescriptor::createRegister(Reg));
2705 }
2706 }
2707
2708 if (Info.hasWorkItemIDZ()) {
2709 assert(Info.hasWorkItemIDX() && Info.hasWorkItemIDY());
2710 if (Subtarget->hasPackedTID()) {
2711 Info.setWorkItemIDZ(
2712 ArgDescriptor::createRegister(AMDGPU::VGPR0, 0x3ff << 20));
2713 } else {
2714 unsigned Reg = AMDGPU::VGPR2;
2715 MRI.setType(MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass), S32);
2716
2717 CCInfo.AllocateReg(Reg);
2718 Info.setWorkItemIDZ(ArgDescriptor::createRegister(Reg));
2719 }
2720 }
2721}
2722
2723// Try to allocate a VGPR at the end of the argument list, or if no argument
2724// VGPRs are left allocating a stack slot.
2725// If \p Mask is is given it indicates bitfield position in the register.
2726// If \p Arg is given use it with new ]p Mask instead of allocating new.
2727static ArgDescriptor allocateVGPR32Input(CCState &CCInfo, unsigned Mask = ~0u,
2728 ArgDescriptor Arg = ArgDescriptor()) {
2729 if (Arg.isSet())
2730 return ArgDescriptor::createArg(Arg, Mask);
2731
2732 ArrayRef<MCPhysReg> ArgVGPRs = ArrayRef(AMDGPU::VGPR_32RegClass.begin(), 32);
2733 unsigned RegIdx = CCInfo.getFirstUnallocated(ArgVGPRs);
2734 if (RegIdx == ArgVGPRs.size()) {
2735 // Spill to stack required.
2736 int64_t Offset = CCInfo.AllocateStack(4, Align(4));
2737
2738 return ArgDescriptor::createStack(Offset, Mask);
2739 }
2740
2741 unsigned Reg = ArgVGPRs[RegIdx];
2742 Reg = CCInfo.AllocateReg(Reg);
2743 assert(Reg != AMDGPU::NoRegister);
2744
2745 MachineFunction &MF = CCInfo.getMachineFunction();
2746 Register LiveInVReg = MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
2747 MF.getRegInfo().setType(LiveInVReg, LLT::scalar(32));
2748 return ArgDescriptor::createRegister(Reg, Mask);
2749}
2750
2752 const TargetRegisterClass *RC,
2753 unsigned NumArgRegs) {
2754 ArrayRef<MCPhysReg> ArgSGPRs = ArrayRef(RC->begin(), 32);
2755 unsigned RegIdx = CCInfo.getFirstUnallocated(ArgSGPRs);
2756 if (RegIdx == ArgSGPRs.size())
2757 report_fatal_error("ran out of SGPRs for arguments");
2758
2759 unsigned Reg = ArgSGPRs[RegIdx];
2760 Reg = CCInfo.AllocateReg(Reg);
2761 assert(Reg != AMDGPU::NoRegister);
2762
2763 MachineFunction &MF = CCInfo.getMachineFunction();
2764 MF.addLiveIn(Reg, RC);
2766}
2767
2768// If this has a fixed position, we still should allocate the register in the
2769// CCInfo state. Technically we could get away with this for values passed
2770// outside of the normal argument range.
2772 const TargetRegisterClass *RC,
2773 MCRegister Reg) {
2774 Reg = CCInfo.AllocateReg(Reg);
2775 assert(Reg != AMDGPU::NoRegister);
2776 MachineFunction &MF = CCInfo.getMachineFunction();
2777 MF.addLiveIn(Reg, RC);
2778}
2779
2780static void allocateSGPR32Input(CCState &CCInfo, ArgDescriptor &Arg) {
2781 if (Arg) {
2782 allocateFixedSGPRInputImpl(CCInfo, &AMDGPU::SGPR_32RegClass,
2783 Arg.getRegister());
2784 } else
2785 Arg = allocateSGPR32InputImpl(CCInfo, &AMDGPU::SGPR_32RegClass, 32);
2786}
2787
2788static void allocateSGPR64Input(CCState &CCInfo, ArgDescriptor &Arg) {
2789 if (Arg) {
2790 allocateFixedSGPRInputImpl(CCInfo, &AMDGPU::SGPR_64RegClass,
2791 Arg.getRegister());
2792 } else
2793 Arg = allocateSGPR32InputImpl(CCInfo, &AMDGPU::SGPR_64RegClass, 16);
2794}
2795
2796/// Allocate implicit function VGPR arguments at the end of allocated user
2797/// arguments.
2799 CCState &CCInfo, MachineFunction &MF, const SIRegisterInfo &TRI,
2800 SIMachineFunctionInfo &Info) const {
2801 const unsigned Mask = 0x3ff;
2802 ArgDescriptor Arg;
2803
2804 if (Info.hasWorkItemIDX()) {
2805 Arg = allocateVGPR32Input(CCInfo, Mask);
2806 Info.setWorkItemIDX(Arg);
2807 }
2808
2809 if (Info.hasWorkItemIDY()) {
2810 Arg = allocateVGPR32Input(CCInfo, Mask << 10, Arg);
2811 Info.setWorkItemIDY(Arg);
2812 }
2813
2814 if (Info.hasWorkItemIDZ())
2815 Info.setWorkItemIDZ(allocateVGPR32Input(CCInfo, Mask << 20, Arg));
2816}
2817
2818/// Allocate implicit function VGPR arguments in fixed registers.
2820 CCState &CCInfo, MachineFunction &MF, const SIRegisterInfo &TRI,
2821 SIMachineFunctionInfo &Info) const {
2822 Register Reg = CCInfo.AllocateReg(AMDGPU::VGPR31);
2823 if (!Reg)
2824 report_fatal_error("failed to allocate VGPR for implicit arguments");
2825
2826 const unsigned Mask = 0x3ff;
2827 Info.setWorkItemIDX(ArgDescriptor::createRegister(Reg, Mask));
2828 Info.setWorkItemIDY(ArgDescriptor::createRegister(Reg, Mask << 10));
2829 Info.setWorkItemIDZ(ArgDescriptor::createRegister(Reg, Mask << 20));
2830}
2831
2833 CCState &CCInfo, MachineFunction &MF, const SIRegisterInfo &TRI,
2834 SIMachineFunctionInfo &Info) const {
2835 auto &ArgInfo = Info.getArgInfo();
2836 const GCNUserSGPRUsageInfo &UserSGPRInfo = Info.getUserSGPRInfo();
2837
2838 // TODO: Unify handling with private memory pointers.
2839 if (UserSGPRInfo.hasDispatchPtr())
2840 allocateSGPR64Input(CCInfo, ArgInfo.DispatchPtr);
2841
2842 if (UserSGPRInfo.hasQueuePtr())
2843 allocateSGPR64Input(CCInfo, ArgInfo.QueuePtr);
2844
2845 // Implicit arg ptr takes the place of the kernarg segment pointer. This is a
2846 // constant offset from the kernarg segment.
2847 if (Info.hasImplicitArgPtr())
2848 allocateSGPR64Input(CCInfo, ArgInfo.ImplicitArgPtr);
2849
2850 if (UserSGPRInfo.hasDispatchID())
2851 allocateSGPR64Input(CCInfo, ArgInfo.DispatchID);
2852
2853 // flat_scratch_init is not applicable for non-kernel functions.
2854
2855 if (Info.hasWorkGroupIDX())
2856 allocateSGPR32Input(CCInfo, ArgInfo.WorkGroupIDX);
2857
2858 if (Info.hasWorkGroupIDY())
2859 allocateSGPR32Input(CCInfo, ArgInfo.WorkGroupIDY);
2860
2861 if (Info.hasWorkGroupIDZ())
2862 allocateSGPR32Input(CCInfo, ArgInfo.WorkGroupIDZ);
2863
2864 if (Info.hasLDSKernelId())
2865 allocateSGPR32Input(CCInfo, ArgInfo.LDSKernelId);
2866}
2867
2868// Allocate special inputs passed in user SGPRs.
2870 MachineFunction &MF,
2871 const SIRegisterInfo &TRI,
2872 SIMachineFunctionInfo &Info) const {
2873 const GCNUserSGPRUsageInfo &UserSGPRInfo = Info.getUserSGPRInfo();
2874 if (UserSGPRInfo.hasImplicitBufferPtr()) {
2875 Register ImplicitBufferPtrReg = Info.addImplicitBufferPtr(TRI);
2876 MF.addLiveIn(ImplicitBufferPtrReg, &AMDGPU::SGPR_64RegClass);
2877 CCInfo.AllocateReg(ImplicitBufferPtrReg);
2878 }
2879
2880 // FIXME: How should these inputs interact with inreg / custom SGPR inputs?
2881 if (UserSGPRInfo.hasPrivateSegmentBuffer()) {
2882 Register PrivateSegmentBufferReg = Info.addPrivateSegmentBuffer(TRI);
2883 MF.addLiveIn(PrivateSegmentBufferReg, &AMDGPU::SGPR_128RegClass);
2884 CCInfo.AllocateReg(PrivateSegmentBufferReg);
2885 }
2886
2887 if (UserSGPRInfo.hasDispatchPtr()) {
2888 Register DispatchPtrReg = Info.addDispatchPtr(TRI);
2889 MF.addLiveIn(DispatchPtrReg, &AMDGPU::SGPR_64RegClass);
2890 CCInfo.AllocateReg(DispatchPtrReg);
2891 }
2892
2893 if (UserSGPRInfo.hasQueuePtr()) {
2894 Register QueuePtrReg = Info.addQueuePtr(TRI);
2895 MF.addLiveIn(QueuePtrReg, &AMDGPU::SGPR_64RegClass);
2896 CCInfo.AllocateReg(QueuePtrReg);
2897 }
2898
2899 if (UserSGPRInfo.hasKernargSegmentPtr()) {
2901 Register InputPtrReg = Info.addKernargSegmentPtr(TRI);
2902 CCInfo.AllocateReg(InputPtrReg);
2903
2904 Register VReg = MF.addLiveIn(InputPtrReg, &AMDGPU::SGPR_64RegClass);
2905 MRI.setType(VReg, LLT::pointer(AMDGPUAS::CONSTANT_ADDRESS, 64));
2906 }
2907
2908 if (UserSGPRInfo.hasDispatchID()) {
2909 Register DispatchIDReg = Info.addDispatchID(TRI);
2910 MF.addLiveIn(DispatchIDReg, &AMDGPU::SGPR_64RegClass);
2911 CCInfo.AllocateReg(DispatchIDReg);
2912 }
2913
2914 if (UserSGPRInfo.hasFlatScratchInit() && !getSubtarget()->isAmdPalOS()) {
2915 Register FlatScratchInitReg = Info.addFlatScratchInit(TRI);
2916 MF.addLiveIn(FlatScratchInitReg, &AMDGPU::SGPR_64RegClass);
2917 CCInfo.AllocateReg(FlatScratchInitReg);
2918 }
2919
2920 if (UserSGPRInfo.hasPrivateSegmentSize()) {
2921 Register PrivateSegmentSizeReg = Info.addPrivateSegmentSize(TRI);
2922 MF.addLiveIn(PrivateSegmentSizeReg, &AMDGPU::SGPR_32RegClass);
2923 CCInfo.AllocateReg(PrivateSegmentSizeReg);
2924 }
2925
2926 // TODO: Add GridWorkGroupCount user SGPRs when used. For now with HSA we read
2927 // these from the dispatch pointer.
2928}
2929
2930// Allocate pre-loaded kernel arguemtns. Arguments to be preloading must be
2931// sequential starting from the first argument.
2933 CCState &CCInfo, SmallVectorImpl<CCValAssign> &ArgLocs,
2935 const SIRegisterInfo &TRI, SIMachineFunctionInfo &Info) const {
2936 Function &F = MF.getFunction();
2937 unsigned LastExplicitArgOffset = Subtarget->getExplicitKernelArgOffset();
2938 GCNUserSGPRUsageInfo &SGPRInfo = Info.getUserSGPRInfo();
2939 bool InPreloadSequence = true;
2940 unsigned InIdx = 0;
2941 bool AlignedForImplictArgs = false;
2942 unsigned ImplicitArgOffset = 0;
2943 for (auto &Arg : F.args()) {
2944 if (!InPreloadSequence || !Arg.hasInRegAttr())
2945 break;
2946
2947 unsigned ArgIdx = Arg.getArgNo();
2948 // Don't preload non-original args or parts not in the current preload
2949 // sequence.
2950 if (InIdx < Ins.size() &&
2951 (!Ins[InIdx].isOrigArg() || Ins[InIdx].getOrigArgIndex() != ArgIdx))
2952 break;
2953
2954 for (; InIdx < Ins.size() && Ins[InIdx].isOrigArg() &&
2955 Ins[InIdx].getOrigArgIndex() == ArgIdx;
2956 InIdx++) {
2957 assert(ArgLocs[ArgIdx].isMemLoc());
2958 auto &ArgLoc = ArgLocs[InIdx];
2959 const Align KernelArgBaseAlign = Align(16);
2960 unsigned ArgOffset = ArgLoc.getLocMemOffset();
2961 Align Alignment = commonAlignment(KernelArgBaseAlign, ArgOffset);
2962 unsigned NumAllocSGPRs =
2963 alignTo(ArgLoc.getLocVT().getFixedSizeInBits(), 32) / 32;
2964
2965 // Fix alignment for hidden arguments.
2966 if (Arg.hasAttribute("amdgpu-hidden-argument")) {
2967 if (!AlignedForImplictArgs) {
2968 ImplicitArgOffset =
2969 alignTo(LastExplicitArgOffset,
2970 Subtarget->getAlignmentForImplicitArgPtr()) -
2971 LastExplicitArgOffset;
2972 AlignedForImplictArgs = true;
2973 }
2974 ArgOffset += ImplicitArgOffset;
2975 }
2976
2977 // Arg is preloaded into the previous SGPR.
2978 if (ArgLoc.getLocVT().getStoreSize() < 4 && Alignment < 4) {
2979 assert(InIdx >= 1 && "No previous SGPR");
2980 Info.getArgInfo().PreloadKernArgs[InIdx].Regs.push_back(
2981 Info.getArgInfo().PreloadKernArgs[InIdx - 1].Regs[0]);
2982 continue;
2983 }
2984
2985 unsigned Padding = ArgOffset - LastExplicitArgOffset;
2986 unsigned PaddingSGPRs = alignTo(Padding, 4) / 4;
2987 // Check for free user SGPRs for preloading.
2988 if (PaddingSGPRs + NumAllocSGPRs > SGPRInfo.getNumFreeUserSGPRs()) {
2989 InPreloadSequence = false;
2990 break;
2991 }
2992
2993 // Preload this argument.
2994 const TargetRegisterClass *RC =
2995 TRI.getSGPRClassForBitWidth(NumAllocSGPRs * 32);
2996 SmallVectorImpl<MCRegister> *PreloadRegs =
2997 Info.addPreloadedKernArg(TRI, RC, NumAllocSGPRs, InIdx, PaddingSGPRs);
2998
2999 if (PreloadRegs->size() > 1)
3000 RC = &AMDGPU::SGPR_32RegClass;
3001 for (auto &Reg : *PreloadRegs) {
3002 assert(Reg);
3003 MF.addLiveIn(Reg, RC);
3004 CCInfo.AllocateReg(Reg);
3005 }
3006
3007 LastExplicitArgOffset = NumAllocSGPRs * 4 + ArgOffset;
3008 }
3009 }
3010}
3011
3013 const SIRegisterInfo &TRI,
3014 SIMachineFunctionInfo &Info) const {
3015 // Always allocate this last since it is a synthetic preload.
3016 if (Info.hasLDSKernelId()) {
3017 Register Reg = Info.addLDSKernelId();
3018 MF.addLiveIn(Reg, &AMDGPU::SGPR_32RegClass);
3019 CCInfo.AllocateReg(Reg);
3020 }
3021}
3022
3023// Allocate special input registers that are initialized per-wave.
3026 CallingConv::ID CallConv,
3027 bool IsShader) const {
3028 bool HasArchitectedSGPRs = Subtarget->hasArchitectedSGPRs();
3029 if (Subtarget->hasUserSGPRInit16Bug() && !IsShader) {
3030 // Note: user SGPRs are handled by the front-end for graphics shaders
3031 // Pad up the used user SGPRs with dead inputs.
3032
3033 // TODO: NumRequiredSystemSGPRs computation should be adjusted appropriately
3034 // before enabling architected SGPRs for workgroup IDs.
3035 assert(!HasArchitectedSGPRs && "Unhandled feature for the subtarget");
3036
3037 unsigned CurrentUserSGPRs = Info.getNumUserSGPRs();
3038 // Note we do not count the PrivateSegmentWaveByteOffset. We do not want to
3039 // rely on it to reach 16 since if we end up having no stack usage, it will
3040 // not really be added.
3041 unsigned NumRequiredSystemSGPRs =
3042 Info.hasWorkGroupIDX() + Info.hasWorkGroupIDY() +
3043 Info.hasWorkGroupIDZ() + Info.hasWorkGroupInfo();
3044 for (unsigned i = NumRequiredSystemSGPRs + CurrentUserSGPRs; i < 16; ++i) {
3045 Register Reg = Info.addReservedUserSGPR();
3046 MF.addLiveIn(Reg, &AMDGPU::SGPR_32RegClass);
3047 CCInfo.AllocateReg(Reg);
3048 }
3049 }
3050
3051 if (!HasArchitectedSGPRs) {
3052 if (Info.hasWorkGroupIDX()) {
3053 Register Reg = Info.addWorkGroupIDX();
3054 MF.addLiveIn(Reg, &AMDGPU::SGPR_32RegClass);
3055 CCInfo.AllocateReg(Reg);
3056 }
3057
3058 if (Info.hasWorkGroupIDY()) {
3059 Register Reg = Info.addWorkGroupIDY();
3060 MF.addLiveIn(Reg, &AMDGPU::SGPR_32RegClass);
3061 CCInfo.AllocateReg(Reg);
3062 }
3063
3064 if (Info.hasWorkGroupIDZ()) {
3065 Register Reg = Info.addWorkGroupIDZ();
3066 MF.addLiveIn(Reg, &AMDGPU::SGPR_32RegClass);
3067 CCInfo.AllocateReg(Reg);
3068 }
3069 }
3070
3071 if (Info.hasWorkGroupInfo()) {
3072 Register Reg = Info.addWorkGroupInfo();
3073 MF.addLiveIn(Reg, &AMDGPU::SGPR_32RegClass);
3074 CCInfo.AllocateReg(Reg);
3075 }
3076
3077 if (Info.hasPrivateSegmentWaveByteOffset()) {
3078 // Scratch wave offset passed in system SGPR.
3079 unsigned PrivateSegmentWaveByteOffsetReg;
3080
3081 if (IsShader) {
3082 PrivateSegmentWaveByteOffsetReg =
3083 Info.getPrivateSegmentWaveByteOffsetSystemSGPR();
3084
3085 // This is true if the scratch wave byte offset doesn't have a fixed
3086 // location.
3087 if (PrivateSegmentWaveByteOffsetReg == AMDGPU::NoRegister) {
3088 PrivateSegmentWaveByteOffsetReg = findFirstFreeSGPR(CCInfo);
3089 Info.setPrivateSegmentWaveByteOffset(PrivateSegmentWaveByteOffsetReg);
3090 }
3091 } else
3092 PrivateSegmentWaveByteOffsetReg = Info.addPrivateSegmentWaveByteOffset();
3093
3094 MF.addLiveIn(PrivateSegmentWaveByteOffsetReg, &AMDGPU::SGPR_32RegClass);
3095 CCInfo.AllocateReg(PrivateSegmentWaveByteOffsetReg);
3096 }
3097
3098 assert(!Subtarget->hasUserSGPRInit16Bug() || IsShader ||
3099 Info.getNumPreloadedSGPRs() >= 16);
3100}
3101
3103 MachineFunction &MF,
3104 const SIRegisterInfo &TRI,
3106 // Now that we've figured out where the scratch register inputs are, see if
3107 // should reserve the arguments and use them directly.
3108 MachineFrameInfo &MFI = MF.getFrameInfo();
3109 bool HasStackObjects = MFI.hasStackObjects();
3110 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
3111
3112 // Record that we know we have non-spill stack objects so we don't need to
3113 // check all stack objects later.
3114 if (HasStackObjects)
3115 Info.setHasNonSpillStackObjects(true);
3116
3117 // Everything live out of a block is spilled with fast regalloc, so it's
3118 // almost certain that spilling will be required.
3119 if (TM.getOptLevel() == CodeGenOptLevel::None)
3120 HasStackObjects = true;
3121
3122 // For now assume stack access is needed in any callee functions, so we need
3123 // the scratch registers to pass in.
3124 bool RequiresStackAccess = HasStackObjects || MFI.hasCalls();
3125
3126 if (!ST.enableFlatScratch()) {
3127 if (RequiresStackAccess && ST.isAmdHsaOrMesa(MF.getFunction())) {
3128 // If we have stack objects, we unquestionably need the private buffer
3129 // resource. For the Code Object V2 ABI, this will be the first 4 user
3130 // SGPR inputs. We can reserve those and use them directly.
3131
3132 Register PrivateSegmentBufferReg =
3134 Info.setScratchRSrcReg(PrivateSegmentBufferReg);
3135 } else {
3136 unsigned ReservedBufferReg = TRI.reservedPrivateSegmentBufferReg(MF);
3137 // We tentatively reserve the last registers (skipping the last registers
3138 // which may contain VCC, FLAT_SCR, and XNACK). After register allocation,
3139 // we'll replace these with the ones immediately after those which were
3140 // really allocated. In the prologue copies will be inserted from the
3141 // argument to these reserved registers.
3142
3143 // Without HSA, relocations are used for the scratch pointer and the
3144 // buffer resource setup is always inserted in the prologue. Scratch wave
3145 // offset is still in an input SGPR.
3146 Info.setScratchRSrcReg(ReservedBufferReg);
3147 }
3148 }
3149
3151
3152 // For entry functions we have to set up the stack pointer if we use it,
3153 // whereas non-entry functions get this "for free". This means there is no
3154 // intrinsic advantage to using S32 over S34 in cases where we do not have
3155 // calls but do need a frame pointer (i.e. if we are requested to have one
3156 // because frame pointer elimination is disabled). To keep things simple we
3157 // only ever use S32 as the call ABI stack pointer, and so using it does not
3158 // imply we need a separate frame pointer.
3159 //
3160 // Try to use s32 as the SP, but move it if it would interfere with input
3161 // arguments. This won't work with calls though.
3162 //
3163 // FIXME: Move SP to avoid any possible inputs, or find a way to spill input
3164 // registers.
3165 if (!MRI.isLiveIn(AMDGPU::SGPR32)) {
3166 Info.setStackPtrOffsetReg(AMDGPU::SGPR32);
3167 } else {
3169
3170 if (MFI.hasCalls())
3171 report_fatal_error("call in graphics shader with too many input SGPRs");
3172
3173 for (unsigned Reg : AMDGPU::SGPR_32RegClass) {
3174 if (!MRI.isLiveIn(Reg)) {
3175 Info.setStackPtrOffsetReg(Reg);
3176 break;
3177 }
3178 }
3179
3180 if (Info.getStackPtrOffsetReg() == AMDGPU::SP_REG)
3181 report_fatal_error("failed to find register for SP");
3182 }
3183
3184 // hasFP should be accurate for entry functions even before the frame is
3185 // finalized, because it does not rely on the known stack size, only
3186 // properties like whether variable sized objects are present.
3187 if (ST.getFrameLowering()->hasFP(MF)) {
3188 Info.setFrameOffsetReg(AMDGPU::SGPR33);
3189 }
3190}
3191
3194 return !Info->isEntryFunction();
3195}
3196
3198
3200 MachineBasicBlock *Entry,
3201 const SmallVectorImpl<MachineBasicBlock *> &Exits) const {
3203
3204 const MCPhysReg *IStart = TRI->getCalleeSavedRegsViaCopy(Entry->getParent());
3205 if (!IStart)
3206 return;
3207
3208 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
3209 MachineRegisterInfo *MRI = &Entry->getParent()->getRegInfo();
3210 MachineBasicBlock::iterator MBBI = Entry->begin();
3211 for (const MCPhysReg *I = IStart; *I; ++I) {
3212 const TargetRegisterClass *RC = nullptr;
3213 if (AMDGPU::SReg_64RegClass.contains(*I))
3214 RC = &AMDGPU::SGPR_64RegClass;
3215 else if (AMDGPU::SReg_32RegClass.contains(*I))
3216 RC = &AMDGPU::SGPR_32RegClass;
3217 else
3218 llvm_unreachable("Unexpected register class in CSRsViaCopy!");
3219
3220 Register NewVR = MRI->createVirtualRegister(RC);
3221 // Create copy from CSR to a virtual register.
3222 Entry->addLiveIn(*I);
3223 BuildMI(*Entry, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY), NewVR)
3224 .addReg(*I);
3225
3226 // Insert the copy-back instructions right before the terminator.
3227 for (auto *Exit : Exits)
3228 BuildMI(*Exit, Exit->getFirstTerminator(), DebugLoc(),
3229 TII->get(TargetOpcode::COPY), *I)
3230 .addReg(NewVR);
3231 }
3232}
3233
3235 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
3236 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
3237 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
3239
3241 const Function &Fn = MF.getFunction();
3244 bool IsError = false;
3245
3246 if (Subtarget->isAmdHsaOS() && AMDGPU::isGraphics(CallConv)) {
3248 Fn, "unsupported non-compute shaders with HSA", DL.getDebugLoc()));
3249 IsError = true;
3250 }
3251
3254 BitVector Skipped(Ins.size());
3255 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
3256 *DAG.getContext());
3257
3258 bool IsGraphics = AMDGPU::isGraphics(CallConv);
3259 bool IsKernel = AMDGPU::isKernel(CallConv);
3260 bool IsEntryFunc = AMDGPU::isEntryFunctionCC(CallConv);
3261
3262 if (IsGraphics) {
3263 const GCNUserSGPRUsageInfo &UserSGPRInfo = Info->getUserSGPRInfo();
3264 assert(!UserSGPRInfo.hasDispatchPtr() &&
3265 !UserSGPRInfo.hasKernargSegmentPtr() && !Info->hasWorkGroupInfo() &&
3266 !Info->hasLDSKernelId() && !Info->hasWorkItemIDX() &&
3267 !Info->hasWorkItemIDY() && !Info->hasWorkItemIDZ());
3268 (void)UserSGPRInfo;
3269 if (!Subtarget->enableFlatScratch())
3270 assert(!UserSGPRInfo.hasFlatScratchInit());
3271 if ((CallConv != CallingConv::AMDGPU_CS &&
3272 CallConv != CallingConv::AMDGPU_Gfx &&
3273 CallConv != CallingConv::AMDGPU_Gfx_WholeWave) ||
3274 !Subtarget->hasArchitectedSGPRs())
3275 assert(!Info->hasWorkGroupIDX() && !Info->hasWorkGroupIDY() &&
3276 !Info->hasWorkGroupIDZ());
3277 }
3278
3279 bool IsWholeWaveFunc = Info->isWholeWaveFunction();
3280
3281 if (CallConv == CallingConv::AMDGPU_PS) {
3282 processPSInputArgs(Splits, CallConv, Ins, Skipped, FType, Info);
3283
3284 // At least one interpolation mode must be enabled or else the GPU will
3285 // hang.
3286 //
3287 // Check PSInputAddr instead of PSInputEnable. The idea is that if the user
3288 // set PSInputAddr, the user wants to enable some bits after the compilation
3289 // based on run-time states. Since we can't know what the final PSInputEna
3290 // will look like, so we shouldn't do anything here and the user should take
3291 // responsibility for the correct programming.
3292 //
3293 // Otherwise, the following restrictions apply:
3294 // - At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled.
3295 // - If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be
3296 // enabled too.
3297 if ((Info->getPSInputAddr() & 0x7F) == 0 ||
3298 ((Info->getPSInputAddr() & 0xF) == 0 && Info->isPSInputAllocated(11))) {
3299 CCInfo.AllocateReg(AMDGPU::VGPR0);
3300 CCInfo.AllocateReg(AMDGPU::VGPR1);
3301 Info->markPSInputAllocated(0);
3302 Info->markPSInputEnabled(0);
3303 }
3304 if (Subtarget->isAmdPalOS()) {
3305 // For isAmdPalOS, the user does not enable some bits after compilation
3306 // based on run-time states; the register values being generated here are
3307 // the final ones set in hardware. Therefore we need to apply the
3308 // workaround to PSInputAddr and PSInputEnable together. (The case where
3309 // a bit is set in PSInputAddr but not PSInputEnable is where the
3310 // frontend set up an input arg for a particular interpolation mode, but
3311 // nothing uses that input arg. Really we should have an earlier pass
3312 // that removes such an arg.)
3313 unsigned PsInputBits = Info->getPSInputAddr() & Info->getPSInputEnable();
3314 if ((PsInputBits & 0x7F) == 0 ||
3315 ((PsInputBits & 0xF) == 0 && (PsInputBits >> 11 & 1)))
3316 Info->markPSInputEnabled(llvm::countr_zero(Info->getPSInputAddr()));
3317 }
3318 } else if (IsKernel) {
3319 assert(Info->hasWorkGroupIDX() && Info->hasWorkItemIDX());
3320 } else {
3321 Splits.append(IsWholeWaveFunc ? std::next(Ins.begin()) : Ins.begin(),
3322 Ins.end());
3323 }
3324
3325 if (IsKernel)
3326 analyzeFormalArgumentsCompute(CCInfo, Ins);
3327
3328 if (IsEntryFunc) {
3329 allocateSpecialEntryInputVGPRs(CCInfo, MF, *TRI, *Info);
3330 allocateHSAUserSGPRs(CCInfo, MF, *TRI, *Info);
3331 if (IsKernel && Subtarget->hasKernargPreload())
3332 allocatePreloadKernArgSGPRs(CCInfo, ArgLocs, Ins, MF, *TRI, *Info);
3333
3334 allocateLDSKernelId(CCInfo, MF, *TRI, *Info);
3335 } else if (!IsGraphics) {
3336 // For the fixed ABI, pass workitem IDs in the last argument register.
3337 allocateSpecialInputVGPRsFixed(CCInfo, MF, *TRI, *Info);
3338
3339 // FIXME: Sink this into allocateSpecialInputSGPRs
3340 if (!Subtarget->enableFlatScratch())
3341 CCInfo.AllocateReg(Info->getScratchRSrcReg());
3342
3343 allocateSpecialInputSGPRs(CCInfo, MF, *TRI, *Info);
3344 }
3345
3346 if (!IsKernel) {
3347 CCAssignFn *AssignFn = CCAssignFnForCall(CallConv, isVarArg);
3348 CCInfo.AnalyzeFormalArguments(Splits, AssignFn);
3349
3350 // This assumes the registers are allocated by CCInfo in ascending order
3351 // with no gaps.
3352 Info->setNumWaveDispatchSGPRs(
3353 CCInfo.getFirstUnallocated(AMDGPU::SGPR_32RegClass.getRegisters()));
3354 Info->setNumWaveDispatchVGPRs(
3355 CCInfo.getFirstUnallocated(AMDGPU::VGPR_32RegClass.getRegisters()));
3356 } else if (Info->getNumKernargPreloadedSGPRs()) {
3357 Info->setNumWaveDispatchSGPRs(Info->getNumUserSGPRs());
3358 }
3359
3361
3362 if (IsWholeWaveFunc) {
3364 {MVT::i1, MVT::Other}, Chain);
3365 InVals.push_back(Setup.getValue(0));
3366 Chains.push_back(Setup.getValue(1));
3367 }
3368
3369 // FIXME: This is the minimum kernel argument alignment. We should improve
3370 // this to the maximum alignment of the arguments.
3371 //
3372 // FIXME: Alignment of explicit arguments totally broken with non-0 explicit
3373 // kern arg offset.
3374 const Align KernelArgBaseAlign = Align(16);
3375
3376 for (unsigned i = IsWholeWaveFunc ? 1 : 0, e = Ins.size(), ArgIdx = 0; i != e;
3377 ++i) {
3378 const ISD::InputArg &Arg = Ins[i];
3379 if ((Arg.isOrigArg() && Skipped[Arg.getOrigArgIndex()]) || IsError) {
3380 InVals.push_back(DAG.getPOISON(Arg.VT));
3381 continue;
3382 }
3383
3384 CCValAssign &VA = ArgLocs[ArgIdx++];
3385 MVT VT = VA.getLocVT();
3386
3387 if (IsEntryFunc && VA.isMemLoc()) {
3388 VT = Ins[i].VT;
3389 EVT MemVT = VA.getLocVT();
3390
3391 const uint64_t Offset = VA.getLocMemOffset();
3392 Align Alignment = commonAlignment(KernelArgBaseAlign, Offset);
3393
3394 if (Arg.Flags.isByRef()) {
3395 SDValue Ptr = lowerKernArgParameterPtr(DAG, DL, Chain, Offset);
3396
3397 const GCNTargetMachine &TM =
3398 static_cast<const GCNTargetMachine &>(getTargetMachine());
3399 if (!TM.isNoopAddrSpaceCast(AMDGPUAS::CONSTANT_ADDRESS,
3400 Arg.Flags.getPointerAddrSpace())) {
3403 }
3404
3405 InVals.push_back(Ptr);
3406 continue;
3407 }
3408
3409 SDValue NewArg;
3410 if (Arg.isOrigArg() && Info->getArgInfo().PreloadKernArgs.count(i)) {
3411 if (MemVT.getStoreSize() < 4 && Alignment < 4) {
3412 // In this case the argument is packed into the previous preload SGPR.
3413 int64_t AlignDownOffset = alignDown(Offset, 4);
3414 int64_t OffsetDiff = Offset - AlignDownOffset;
3415 EVT IntVT = MemVT.changeTypeToInteger();
3416
3417 const SIMachineFunctionInfo *Info =
3420 Register Reg =
3421 Info->getArgInfo().PreloadKernArgs.find(i)->getSecond().Regs[0];
3422
3423 assert(Reg);
3424 Register VReg = MRI.getLiveInVirtReg(Reg);
3425 SDValue Copy = DAG.getCopyFromReg(Chain, DL, VReg, MVT::i32);
3426
3427 SDValue ShiftAmt = DAG.getConstant(OffsetDiff * 8, DL, MVT::i32);
3428 SDValue Extract = DAG.getNode(ISD::SRL, DL, MVT::i32, Copy, ShiftAmt);
3429
3430 SDValue ArgVal = DAG.getNode(ISD::TRUNCATE, DL, IntVT, Extract);
3431 ArgVal = DAG.getNode(ISD::BITCAST, DL, MemVT, ArgVal);
3432 NewArg = convertArgType(DAG, VT, MemVT, DL, ArgVal,
3433 Ins[i].Flags.isSExt(), &Ins[i]);
3434
3435 NewArg = DAG.getMergeValues({NewArg, Copy.getValue(1)}, DL);
3436 } else {
3437 const SIMachineFunctionInfo *Info =
3440 const SmallVectorImpl<MCRegister> &PreloadRegs =
3441 Info->getArgInfo().PreloadKernArgs.find(i)->getSecond().Regs;
3442
3443 SDValue Copy;
3444 if (PreloadRegs.size() == 1) {
3445 Register VReg = MRI.getLiveInVirtReg(PreloadRegs[0]);
3446 const TargetRegisterClass *RC = MRI.getRegClass(VReg);
3447 NewArg = DAG.getCopyFromReg(
3448 Chain, DL, VReg,
3450 TRI->getRegSizeInBits(*RC)));
3451
3452 } else {
3453 // If the kernarg alignment does not match the alignment of the SGPR
3454 // tuple RC that can accommodate this argument, it will be built up
3455 // via copies from from the individual SGPRs that the argument was
3456 // preloaded to.
3458 for (auto Reg : PreloadRegs) {
3459 Register VReg = MRI.getLiveInVirtReg(Reg);
3460 Copy = DAG.getCopyFromReg(Chain, DL, VReg, MVT::i32);
3461 Elts.push_back(Copy);
3462 }
3463 NewArg =
3464 DAG.getBuildVector(EVT::getVectorVT(*DAG.getContext(), MVT::i32,
3465 PreloadRegs.size()),
3466 DL, Elts);
3467 }
3468
3469 // If the argument was preloaded to multiple consecutive 32-bit
3470 // registers because of misalignment between addressable SGPR tuples
3471 // and the argument size, we can still assume that because of kernarg
3472 // segment alignment restrictions that NewArg's size is the same as
3473 // MemVT and just do a bitcast. If MemVT is less than 32-bits we add a
3474 // truncate since we cannot preload to less than a single SGPR and the
3475 // MemVT may be smaller.
3476 EVT MemVTInt =
3478 if (MemVT.bitsLT(NewArg.getSimpleValueType()))
3479 NewArg = DAG.getNode(ISD::TRUNCATE, DL, MemVTInt, NewArg);
3480
3481 NewArg = DAG.getBitcast(MemVT, NewArg);
3482 NewArg = convertArgType(DAG, VT, MemVT, DL, NewArg,
3483 Ins[i].Flags.isSExt(), &Ins[i]);
3484 NewArg = DAG.getMergeValues({NewArg, Chain}, DL);
3485 }
3486 } else {
3487 // Hidden arguments that are in the kernel signature must be preloaded
3488 // to user SGPRs. Print a diagnostic error if a hidden argument is in
3489 // the argument list and is not preloaded.
3490 if (Arg.isOrigArg()) {
3491 Argument *OrigArg = Fn.getArg(Arg.getOrigArgIndex());
3492 if (OrigArg->hasAttribute("amdgpu-hidden-argument")) {
3494 *OrigArg->getParent(),
3495 "hidden argument in kernel signature was not preloaded",
3496 DL.getDebugLoc()));
3497 }
3498 }
3499
3500 NewArg =
3501 lowerKernargMemParameter(DAG, VT, MemVT, DL, Chain, Offset,
3502 Alignment, Ins[i].Flags.isSExt(), &Ins[i]);
3503 }
3504 Chains.push_back(NewArg.getValue(1));
3505
3506 auto *ParamTy =
3507 dyn_cast<PointerType>(FType->getParamType(Ins[i].getOrigArgIndex()));
3508 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS &&
3509 ParamTy &&
3510 (ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
3511 ParamTy->getAddressSpace() == AMDGPUAS::REGION_ADDRESS)) {
3512 // On SI local pointers are just offsets into LDS, so they are always
3513 // less than 16-bits. On CI and newer they could potentially be
3514 // real pointers, so we can't guarantee their size.
3515 NewArg = DAG.getNode(ISD::AssertZext, DL, NewArg.getValueType(), NewArg,
3516 DAG.getValueType(MVT::i16));
3517 }
3518
3519 InVals.push_back(NewArg);
3520 continue;
3521 }
3522 if (!IsEntryFunc && VA.isMemLoc()) {
3523 SDValue Val = lowerStackParameter(DAG, VA, DL, Chain, Arg);
3524 InVals.push_back(Val);
3525 if (!Arg.Flags.isByVal())
3526 Chains.push_back(Val.getValue(1));
3527 continue;
3528 }
3529
3530 assert(VA.isRegLoc() && "Parameter must be in a register!");
3531
3532 Register Reg = VA.getLocReg();
3533 const TargetRegisterClass *RC = nullptr;
3534 if (AMDGPU::VGPR_32RegClass.contains(Reg))
3535 RC = &AMDGPU::VGPR_32RegClass;
3536 else if (AMDGPU::SGPR_32RegClass.contains(Reg))
3537 RC = &AMDGPU::SGPR_32RegClass;
3538 else
3539 llvm_unreachable("Unexpected register class in LowerFormalArguments!");
3540
3541 Reg = MF.addLiveIn(Reg, RC);
3542 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
3543
3544 if (Arg.Flags.isSRet()) {
3545 // The return object should be reasonably addressable.
3546
3547 // FIXME: This helps when the return is a real sret. If it is a
3548 // automatically inserted sret (i.e. CanLowerReturn returns false), an
3549 // extra copy is inserted in SelectionDAGBuilder which obscures this.
3550 unsigned NumBits =
3552 Val = DAG.getNode(
3553 ISD::AssertZext, DL, VT, Val,
3554 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(), NumBits)));
3555 }
3556
3557 Val = convertABITypeToValueType(DAG, Val, VA, DL);
3558 InVals.push_back(Val);
3559 }
3560
3561 // Start adding system SGPRs.
3562 if (IsEntryFunc)
3563 allocateSystemSGPRs(CCInfo, MF, *Info, CallConv, IsGraphics);
3564
3565 // DAG.getPass() returns nullptr when using new pass manager.
3566 // TODO: Use DAG.getMFAM() to access analysis result.
3567 if (DAG.getPass()) {
3568 auto &ArgUsageInfo = DAG.getPass()->getAnalysis<AMDGPUArgumentUsageInfo>();
3569 ArgUsageInfo.setFuncArgInfo(Fn, Info->getArgInfo());
3570 }
3571
3572 unsigned StackArgSize = CCInfo.getStackSize();
3573 Info->setBytesInStackArgArea(StackArgSize);
3574
3575 return Chains.empty() ? Chain
3576 : DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
3577}
3578
3579// TODO: If return values can't fit in registers, we should return as many as
3580// possible in registers before passing on stack.
3582 CallingConv::ID CallConv, MachineFunction &MF, bool IsVarArg,
3583 const SmallVectorImpl<ISD::OutputArg> &Outs, LLVMContext &Context,
3584 const Type *RetTy) const {
3585 // Replacing returns with sret/stack usage doesn't make sense for shaders.
3586 // FIXME: Also sort of a workaround for custom vector splitting in LowerReturn
3587 // for shaders. Vector types should be explicitly handled by CC.
3588 if (AMDGPU::isEntryFunctionCC(CallConv))
3589 return true;
3590
3592 CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
3593 if (!CCInfo.CheckReturn(Outs, CCAssignFnForReturn(CallConv, IsVarArg)))
3594 return false;
3595
3596 // We must use the stack if return would require unavailable registers.
3597 unsigned MaxNumVGPRs = Subtarget->getMaxNumVGPRs(MF);
3598 unsigned TotalNumVGPRs = Subtarget->getAddressableNumArchVGPRs();
3599 for (unsigned i = MaxNumVGPRs; i < TotalNumVGPRs; ++i)
3600 if (CCInfo.isAllocated(AMDGPU::VGPR_32RegClass.getRegister(i)))
3601 return false;
3602
3603 return true;
3604}
3605
3606SDValue
3608 bool isVarArg,
3610 const SmallVectorImpl<SDValue> &OutVals,
3611 const SDLoc &DL, SelectionDAG &DAG) const {
3615
3616 if (AMDGPU::isKernel(CallConv)) {
3617 return AMDGPUTargetLowering::LowerReturn(Chain, CallConv, isVarArg, Outs,
3618 OutVals, DL, DAG);
3619 }
3620
3621 bool IsShader = AMDGPU::isShader(CallConv);
3622
3623 Info->setIfReturnsVoid(Outs.empty());
3624 bool IsWaveEnd = Info->returnsVoid() && IsShader;
3625
3626 // CCValAssign - represent the assignment of the return value to a location.
3628
3629 // CCState - Info about the registers and stack slots.
3630 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
3631 *DAG.getContext());
3632
3633 // Analyze outgoing return values.
3634 CCInfo.AnalyzeReturn(Outs, CCAssignFnForReturn(CallConv, isVarArg));
3635
3636 SDValue Glue;
3638 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
3639
3640 SDValue ReadFirstLane =
3641 DAG.getTargetConstant(Intrinsic::amdgcn_readfirstlane, DL, MVT::i32);
3642 // Copy the result values into the output registers.
3643 for (unsigned I = 0, RealRVLocIdx = 0, E = RVLocs.size(); I != E;
3644 ++I, ++RealRVLocIdx) {
3645 CCValAssign &VA = RVLocs[I];
3646 assert(VA.isRegLoc() && "Can only return in registers!");
3647 // TODO: Partially return in registers if return values don't fit.
3648 SDValue Arg = OutVals[RealRVLocIdx];
3649
3650 // Copied from other backends.
3651 switch (VA.getLocInfo()) {
3652 case CCValAssign::Full:
3653 break;
3654 case CCValAssign::BCvt:
3655 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
3656 break;
3657 case CCValAssign::SExt:
3658 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg);
3659 break;
3660 case CCValAssign::ZExt:
3661 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg);
3662 break;
3663 case CCValAssign::AExt:
3664 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg);
3665 break;
3666 default:
3667 llvm_unreachable("Unknown loc info!");
3668 }
3669 if (TRI->isSGPRPhysReg(VA.getLocReg()))
3671 ReadFirstLane, Arg);
3672 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Glue);
3673 Glue = Chain.getValue(1);
3674 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
3675 }
3676
3677 // FIXME: Does sret work properly?
3678 if (!Info->isEntryFunction()) {
3679 const SIRegisterInfo *TRI = Subtarget->getRegisterInfo();
3680 const MCPhysReg *I =
3681 TRI->getCalleeSavedRegsViaCopy(&DAG.getMachineFunction());
3682 if (I) {
3683 for (; *I; ++I) {
3684 if (AMDGPU::SReg_64RegClass.contains(*I))
3685 RetOps.push_back(DAG.getRegister(*I, MVT::i64));
3686 else if (AMDGPU::SReg_32RegClass.contains(*I))
3687 RetOps.push_back(DAG.getRegister(*I, MVT::i32));
3688 else
3689 llvm_unreachable("Unexpected register class in CSRsViaCopy!");
3690 }
3691 }
3692 }
3693
3694 // Update chain and glue.
3695 RetOps[0] = Chain;
3696 if (Glue.getNode())
3697 RetOps.push_back(Glue);
3698
3699 unsigned Opc = AMDGPUISD::ENDPGM;
3700 if (!IsWaveEnd)
3701 Opc = Info->isWholeWaveFunction() ? AMDGPUISD::WHOLE_WAVE_RETURN
3702 : IsShader ? AMDGPUISD::RETURN_TO_EPILOG
3704 return DAG.getNode(Opc, DL, MVT::Other, RetOps);
3705}
3706
3708 SDValue Chain, SDValue InGlue, CallingConv::ID CallConv, bool IsVarArg,
3709 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
3710 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals, bool IsThisReturn,
3711 SDValue ThisVal) const {
3712 CCAssignFn *RetCC = CCAssignFnForReturn(CallConv, IsVarArg);
3713
3714 // Assign locations to each value returned by this call.
3716 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
3717 *DAG.getContext());
3718 CCInfo.AnalyzeCallResult(Ins, RetCC);
3719
3720 // Copy all of the result registers out of their specified physreg.
3721 for (CCValAssign VA : RVLocs) {
3722 SDValue Val;
3723
3724 if (VA.isRegLoc()) {
3725 Val =
3726 DAG.getCopyFromReg(Chain, DL, VA.getLocReg(), VA.getLocVT(), InGlue);
3727 Chain = Val.getValue(1);
3728 InGlue = Val.getValue(2);
3729 } else if (VA.isMemLoc()) {
3730 report_fatal_error("TODO: return values in memory");
3731 } else
3732 llvm_unreachable("unknown argument location type");
3733
3734 switch (VA.getLocInfo()) {
3735 case CCValAssign::Full:
3736 break;
3737 case CCValAssign::BCvt:
3738 Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
3739 break;
3740 case CCValAssign::ZExt:
3741 Val = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Val,
3742 DAG.getValueType(VA.getValVT()));
3743 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
3744 break;
3745 case CCValAssign::SExt:
3746 Val = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Val,
3747 DAG.getValueType(VA.getValVT()));
3748 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
3749 break;
3750 case CCValAssign::AExt:
3751 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
3752 break;
3753 default:
3754 llvm_unreachable("Unknown loc info!");
3755 }
3756
3757 InVals.push_back(Val);
3758 }
3759
3760 return Chain;
3761}
3762
3763// Add code to pass special inputs required depending on used features separate
3764// from the explicit user arguments present in the IR.
3766 CallLoweringInfo &CLI, CCState &CCInfo, const SIMachineFunctionInfo &Info,
3767 SmallVectorImpl<std::pair<unsigned, SDValue>> &RegsToPass,
3768 SmallVectorImpl<SDValue> &MemOpChains, SDValue Chain) const {
3769 // If we don't have a call site, this was a call inserted by
3770 // legalization. These can never use special inputs.
3771 if (!CLI.CB)
3772 return;
3773
3774 SelectionDAG &DAG = CLI.DAG;
3775 const SDLoc &DL = CLI.DL;
3776 const Function &F = DAG.getMachineFunction().getFunction();
3777
3778 const SIRegisterInfo *TRI = Subtarget->getRegisterInfo();
3779 const AMDGPUFunctionArgInfo &CallerArgInfo = Info.getArgInfo();
3780
3781 const AMDGPUFunctionArgInfo *CalleeArgInfo =
3783 if (const Function *CalleeFunc = CLI.CB->getCalledFunction()) {
3784 // DAG.getPass() returns nullptr when using new pass manager.
3785 // TODO: Use DAG.getMFAM() to access analysis result.
3786 if (DAG.getPass()) {
3787 auto &ArgUsageInfo =
3789 CalleeArgInfo = &ArgUsageInfo.lookupFuncArgInfo(*CalleeFunc);
3790 }
3791 }
3792
3793 // TODO: Unify with private memory register handling. This is complicated by
3794 // the fact that at least in kernels, the input argument is not necessarily
3795 // in the same location as the input.
3796 // clang-format off
3797 static constexpr std::pair<AMDGPUFunctionArgInfo::PreloadedValue,
3798 std::array<StringLiteral, 2>> ImplicitAttrs[] = {
3799 {AMDGPUFunctionArgInfo::DISPATCH_PTR, {"amdgpu-no-dispatch-ptr", ""}},
3800 {AMDGPUFunctionArgInfo::QUEUE_PTR, {"amdgpu-no-queue-ptr", ""}},
3801 {AMDGPUFunctionArgInfo::IMPLICIT_ARG_PTR, {"amdgpu-no-implicitarg-ptr", ""}},
3802 {AMDGPUFunctionArgInfo::DISPATCH_ID, {"amdgpu-no-dispatch-id", ""}},
3803 {AMDGPUFunctionArgInfo::WORKGROUP_ID_X, {"amdgpu-no-workgroup-id-x", "amdgpu-no-cluster-id-x"}},
3804 {AMDGPUFunctionArgInfo::WORKGROUP_ID_Y, {"amdgpu-no-workgroup-id-y", "amdgpu-no-cluster-id-y"}},
3805 {AMDGPUFunctionArgInfo::WORKGROUP_ID_Z, {"amdgpu-no-workgroup-id-z", "amdgpu-no-cluster-id-z"}},
3806 {AMDGPUFunctionArgInfo::LDS_KERNEL_ID, {"amdgpu-no-lds-kernel-id", ""}},
3807 };
3808 // clang-format on
3809
3810 for (auto [InputID, Attrs] : ImplicitAttrs) {
3811 // If the callee does not use the attribute value, skip copying the value.
3812 if (all_of(Attrs, [&](StringRef Attr) {
3813 return Attr.empty() || CLI.CB->hasFnAttr(Attr);
3814 }))
3815 continue;
3816
3817 const auto [OutgoingArg, ArgRC, ArgTy] =
3818 CalleeArgInfo->getPreloadedValue(InputID);
3819 if (!OutgoingArg)
3820 continue;
3821
3822 const auto [IncomingArg, IncomingArgRC, Ty] =
3823 CallerArgInfo.getPreloadedValue(InputID);
3824 assert(IncomingArgRC == ArgRC);
3825
3826 // All special arguments are ints for now.
3827 EVT ArgVT = TRI->getSpillSize(*ArgRC) == 8 ? MVT::i64 : MVT::i32;
3828 SDValue InputReg;
3829
3830 if (IncomingArg) {
3831 InputReg = loadInputValue(DAG, ArgRC, ArgVT, DL, *IncomingArg);
3832 } else if (InputID == AMDGPUFunctionArgInfo::IMPLICIT_ARG_PTR) {
3833 // The implicit arg ptr is special because it doesn't have a corresponding
3834 // input for kernels, and is computed from the kernarg segment pointer.
3835 InputReg = getImplicitArgPtr(DAG, DL);
3836 } else if (InputID == AMDGPUFunctionArgInfo::LDS_KERNEL_ID) {
3837 std::optional<uint32_t> Id =
3839 if (Id.has_value()) {
3840 InputReg = DAG.getConstant(*Id, DL, ArgVT);
3841 } else {
3842 InputReg = DAG.getPOISON(ArgVT);
3843 }
3844 } else {
3845 // We may have proven the input wasn't needed, although the ABI is
3846 // requiring it. We just need to allocate the register appropriately.
3847 InputReg = DAG.getPOISON(ArgVT);
3848 }
3849
3850 if (OutgoingArg->isRegister()) {
3851 RegsToPass.emplace_back(OutgoingArg->getRegister(), InputReg);
3852 if (!CCInfo.AllocateReg(OutgoingArg->getRegister()))
3853 report_fatal_error("failed to allocate implicit input argument");
3854 } else {
3855 unsigned SpecialArgOffset =
3856 CCInfo.AllocateStack(ArgVT.getStoreSize(), Align(4));
3857 SDValue ArgStore =
3858 storeStackInputValue(DAG, DL, Chain, InputReg, SpecialArgOffset);
3859 MemOpChains.push_back(ArgStore);
3860 }
3861 }
3862
3863 // Pack workitem IDs into a single register or pass it as is if already
3864 // packed.
3865
3866 auto [OutgoingArg, ArgRC, Ty] =
3868 if (!OutgoingArg)
3869 std::tie(OutgoingArg, ArgRC, Ty) =
3871 if (!OutgoingArg)
3872 std::tie(OutgoingArg, ArgRC, Ty) =
3874 if (!OutgoingArg)
3875 return;
3876
3877 const ArgDescriptor *IncomingArgX = std::get<0>(
3879 const ArgDescriptor *IncomingArgY = std::get<0>(
3881 const ArgDescriptor *IncomingArgZ = std::get<0>(
3883
3884 SDValue InputReg;
3885 SDLoc SL;
3886
3887 const bool NeedWorkItemIDX = !CLI.CB->hasFnAttr("amdgpu-no-workitem-id-x");
3888 const bool NeedWorkItemIDY = !CLI.CB->hasFnAttr("amdgpu-no-workitem-id-y");
3889 const bool NeedWorkItemIDZ = !CLI.CB->hasFnAttr("amdgpu-no-workitem-id-z");
3890
3891 // If incoming ids are not packed we need to pack them.
3892 if (IncomingArgX && !IncomingArgX->isMasked() && CalleeArgInfo->WorkItemIDX &&
3893 NeedWorkItemIDX) {
3894 if (Subtarget->getMaxWorkitemID(F, 0) != 0) {
3895 InputReg = loadInputValue(DAG, ArgRC, MVT::i32, DL, *IncomingArgX);
3896 } else {
3897 InputReg = DAG.getConstant(0, DL, MVT::i32);
3898 }
3899 }
3900
3901 if (IncomingArgY && !IncomingArgY->isMasked() && CalleeArgInfo->WorkItemIDY &&
3902 NeedWorkItemIDY && Subtarget->getMaxWorkitemID(F, 1) != 0) {
3903 SDValue Y = loadInputValue(DAG, ArgRC, MVT::i32, DL, *IncomingArgY);
3904 Y = DAG.getNode(ISD::SHL, SL, MVT::i32, Y,
3905 DAG.getShiftAmountConstant(10, MVT::i32, SL));
3906 InputReg = InputReg.getNode()
3907 ? DAG.getNode(ISD::OR, SL, MVT::i32, InputReg, Y)
3908 : Y;
3909 }
3910
3911 if (IncomingArgZ && !IncomingArgZ->isMasked() && CalleeArgInfo->WorkItemIDZ &&
3912 NeedWorkItemIDZ && Subtarget->getMaxWorkitemID(F, 2) != 0) {
3913 SDValue Z = loadInputValue(DAG, ArgRC, MVT::i32, DL, *IncomingArgZ);
3914 Z = DAG.getNode(ISD::SHL, SL, MVT::i32, Z,
3915 DAG.getShiftAmountConstant(20, MVT::i32, SL));
3916 InputReg = InputReg.getNode()
3917 ? DAG.getNode(ISD::OR, SL, MVT::i32, InputReg, Z)
3918 : Z;
3919 }
3920
3921 if (!InputReg && (NeedWorkItemIDX || NeedWorkItemIDY || NeedWorkItemIDZ)) {
3922 if (!IncomingArgX && !IncomingArgY && !IncomingArgZ) {
3923 // We're in a situation where the outgoing function requires the workitem
3924 // ID, but the calling function does not have it (e.g a graphics function
3925 // calling a C calling convention function). This is illegal, but we need
3926 // to produce something.
3927 InputReg = DAG.getPOISON(MVT::i32);
3928 } else {
3929 // Workitem ids are already packed, any of present incoming arguments
3930 // will carry all required fields.
3931 ArgDescriptor IncomingArg =
3932 ArgDescriptor::createArg(IncomingArgX ? *IncomingArgX
3933 : IncomingArgY ? *IncomingArgY
3934 : *IncomingArgZ,
3935 ~0u);
3936 InputReg = loadInputValue(DAG, ArgRC, MVT::i32, DL, IncomingArg);
3937 }
3938 }
3939
3940 if (OutgoingArg->isRegister()) {
3941 if (InputReg)
3942 RegsToPass.emplace_back(OutgoingArg->getRegister(), InputReg);
3943
3944 CCInfo.AllocateReg(OutgoingArg->getRegister());
3945 } else {
3946 unsigned SpecialArgOffset = CCInfo.AllocateStack(4, Align(4));
3947 if (InputReg) {
3948 SDValue ArgStore =
3949 storeStackInputValue(DAG, DL, Chain, InputReg, SpecialArgOffset);
3950 MemOpChains.push_back(ArgStore);
3951 }
3952 }
3953}
3954
3956 SDValue Callee, CallingConv::ID CalleeCC, bool IsVarArg,
3958 const SmallVectorImpl<SDValue> &OutVals,
3959 const SmallVectorImpl<ISD::InputArg> &Ins, SelectionDAG &DAG) const {
3960 if (AMDGPU::isChainCC(CalleeCC))
3961 return true;
3962
3963 if (!AMDGPU::mayTailCallThisCC(CalleeCC))
3964 return false;
3965
3966 // For a divergent call target, we need to do a waterfall loop over the
3967 // possible callees which precludes us from using a simple jump.
3968 if (Callee->isDivergent())
3969 return false;
3970
3972 const Function &CallerF = MF.getFunction();
3973 CallingConv::ID CallerCC = CallerF.getCallingConv();
3975 const uint32_t *CallerPreserved = TRI->getCallPreservedMask(MF, CallerCC);
3976
3977 // Kernels aren't callable, and don't have a live in return address so it
3978 // doesn't make sense to do a tail call with entry functions.
3979 if (!CallerPreserved)
3980 return false;
3981
3982 bool CCMatch = CallerCC == CalleeCC;
3983
3985 if (AMDGPU::canGuaranteeTCO(CalleeCC) && CCMatch)
3986 return true;
3987 return false;
3988 }
3989
3990 // TODO: Can we handle var args?
3991 if (IsVarArg)
3992 return false;
3993
3994 for (const Argument &Arg : CallerF.args()) {
3995 if (Arg.hasByValAttr())
3996 return false;
3997 }
3998
3999 LLVMContext &Ctx = *DAG.getContext();
4000
4001 // Check that the call results are passed in the same way.
4002 if (!CCState::resultsCompatible(CalleeCC, CallerCC, MF, Ctx, Ins,
4003 CCAssignFnForCall(CalleeCC, IsVarArg),
4004 CCAssignFnForCall(CallerCC, IsVarArg)))
4005 return false;
4006
4007 // The callee has to preserve all registers the caller needs to preserve.
4008 if (!CCMatch) {
4009 const uint32_t *CalleePreserved = TRI->getCallPreservedMask(MF, CalleeCC);
4010 if (!TRI->regmaskSubsetEqual(CallerPreserved, CalleePreserved))
4011 return false;
4012 }
4013
4014 // Nothing more to check if the callee is taking no arguments.
4015 if (Outs.empty())
4016 return true;
4017
4019 CCState CCInfo(CalleeCC, IsVarArg, MF, ArgLocs, Ctx);
4020
4021 // FIXME: We are not allocating special input registers, so we will be
4022 // deciding based on incorrect register assignments.
4023 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForCall(CalleeCC, IsVarArg));
4024
4025 const SIMachineFunctionInfo *FuncInfo = MF.getInfo<SIMachineFunctionInfo>();
4026 // If the stack arguments for this call do not fit into our own save area then
4027 // the call cannot be made tail.
4028 // TODO: Is this really necessary?
4029 if (CCInfo.getStackSize() > FuncInfo->getBytesInStackArgArea())
4030 return false;
4031
4032 for (const auto &[CCVA, ArgVal] : zip_equal(ArgLocs, OutVals)) {
4033 // FIXME: What about inreg arguments that end up passed in memory?
4034 if (!CCVA.isRegLoc())
4035 continue;
4036
4037 // If we are passing an argument in an SGPR, and the value is divergent,
4038 // this call requires a waterfall loop.
4039 if (ArgVal->isDivergent() && TRI->isSGPRPhysReg(CCVA.getLocReg())) {
4040 LLVM_DEBUG(
4041 dbgs() << "Cannot tail call due to divergent outgoing argument in "
4042 << printReg(CCVA.getLocReg(), TRI) << '\n');
4043 return false;
4044 }
4045 }
4046
4047 const MachineRegisterInfo &MRI = MF.getRegInfo();
4048 return parametersInCSRMatch(MRI, CallerPreserved, ArgLocs, OutVals);
4049}
4050
4052 if (!CI->isTailCall())
4053 return false;
4054
4055 const Function *ParentFn = CI->getParent()->getParent();
4057 return false;
4058 return true;
4059}
4060
4061namespace {
4062// Chain calls have special arguments that we need to handle. These are
4063// tagging along at the end of the arguments list(s), after the SGPR and VGPR
4064// arguments (index 0 and 1 respectively).
4065enum ChainCallArgIdx {
4066 Exec = 2,
4067 Flags,
4068 NumVGPRs,
4069 FallbackExec,
4070 FallbackCallee
4071};
4072} // anonymous namespace
4073
4074// The wave scratch offset register is used as the global base pointer.
4076 SmallVectorImpl<SDValue> &InVals) const {
4077 CallingConv::ID CallConv = CLI.CallConv;
4078 bool IsChainCallConv = AMDGPU::isChainCC(CallConv);
4079
4080 SelectionDAG &DAG = CLI.DAG;
4081
4082 const SDLoc &DL = CLI.DL;
4083 SDValue Chain = CLI.Chain;
4084 SDValue Callee = CLI.Callee;
4085
4086 llvm::SmallVector<SDValue, 6> ChainCallSpecialArgs;
4087 bool UsesDynamicVGPRs = false;
4088 if (IsChainCallConv) {
4089 // The last arguments should be the value that we need to put in EXEC,
4090 // followed by the flags and any other arguments with special meanings.
4091 // Pop them out of CLI.Outs and CLI.OutVals before we do any processing so
4092 // we don't treat them like the "real" arguments.
4093 auto RequestedExecIt =
4094 llvm::find_if(CLI.Outs, [](const ISD::OutputArg &Arg) {
4095 return Arg.OrigArgIndex == 2;
4096 });
4097 assert(RequestedExecIt != CLI.Outs.end() && "No node for EXEC");
4098
4099 size_t SpecialArgsBeginIdx = RequestedExecIt - CLI.Outs.begin();
4100 CLI.OutVals.erase(CLI.OutVals.begin() + SpecialArgsBeginIdx,
4101 CLI.OutVals.end());
4102 CLI.Outs.erase(RequestedExecIt, CLI.Outs.end());
4103
4104 assert(CLI.Outs.back().OrigArgIndex < 2 &&
4105 "Haven't popped all the special args");
4106
4107 TargetLowering::ArgListEntry RequestedExecArg =
4108 CLI.Args[ChainCallArgIdx::Exec];
4109 if (!RequestedExecArg.Ty->isIntegerTy(Subtarget->getWavefrontSize()))
4110 return lowerUnhandledCall(CLI, InVals, "Invalid value for EXEC");
4111
4112 // Convert constants into TargetConstants, so they become immediate operands
4113 // instead of being selected into S_MOV.
4114 auto PushNodeOrTargetConstant = [&](TargetLowering::ArgListEntry Arg) {
4115 if (const auto *ArgNode = dyn_cast<ConstantSDNode>(Arg.Node)) {
4116 ChainCallSpecialArgs.push_back(DAG.getTargetConstant(
4117 ArgNode->getAPIntValue(), DL, ArgNode->getValueType(0)));
4118 } else
4119 ChainCallSpecialArgs.push_back(Arg.Node);
4120 };
4121
4122 PushNodeOrTargetConstant(RequestedExecArg);
4123
4124 // Process any other special arguments depending on the value of the flags.
4125 TargetLowering::ArgListEntry Flags = CLI.Args[ChainCallArgIdx::Flags];
4126
4127 const APInt &FlagsValue = cast<ConstantSDNode>(Flags.Node)->getAPIntValue();
4128 if (FlagsValue.isZero()) {
4129 if (CLI.Args.size() > ChainCallArgIdx::Flags + 1)
4130 return lowerUnhandledCall(CLI, InVals,
4131 "no additional args allowed if flags == 0");
4132 } else if (FlagsValue.isOneBitSet(0)) {
4133 if (CLI.Args.size() != ChainCallArgIdx::FallbackCallee + 1) {
4134 return lowerUnhandledCall(CLI, InVals, "expected 3 additional args");
4135 }
4136
4137 if (!Subtarget->isWave32()) {
4138 return lowerUnhandledCall(
4139 CLI, InVals, "dynamic VGPR mode is only supported for wave32");
4140 }
4141
4142 UsesDynamicVGPRs = true;
4143 std::for_each(CLI.Args.begin() + ChainCallArgIdx::NumVGPRs,
4144 CLI.Args.end(), PushNodeOrTargetConstant);
4145 }
4146 }
4147
4149 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
4151 bool &IsTailCall = CLI.IsTailCall;
4152 bool IsVarArg = CLI.IsVarArg;
4153 bool IsSibCall = false;
4155
4156 if (Callee.isUndef() || isNullConstant(Callee)) {
4157 if (!CLI.IsTailCall) {
4158 for (ISD::InputArg &Arg : CLI.Ins)
4159 InVals.push_back(DAG.getPOISON(Arg.VT));
4160 }
4161
4162 return Chain;
4163 }
4164
4165 if (IsVarArg) {
4166 return lowerUnhandledCall(CLI, InVals,
4167 "unsupported call to variadic function ");
4168 }
4169
4170 if (!CLI.CB)
4171 return lowerUnhandledCall(CLI, InVals, "unsupported libcall legalization");
4172
4173 if (IsTailCall && MF.getTarget().Options.GuaranteedTailCallOpt) {
4174 return lowerUnhandledCall(CLI, InVals,
4175 "unsupported required tail call to function ");
4176 }
4177
4178 if (IsTailCall) {
4179 IsTailCall = isEligibleForTailCallOptimization(Callee, CallConv, IsVarArg,
4180 Outs, OutVals, Ins, DAG);
4181 if (!IsTailCall &&
4182 ((CLI.CB && CLI.CB->isMustTailCall()) || IsChainCallConv)) {
4183 report_fatal_error("failed to perform tail call elimination on a call "
4184 "site marked musttail or on llvm.amdgcn.cs.chain");
4185 }
4186
4187 bool TailCallOpt = MF.getTarget().Options.GuaranteedTailCallOpt;
4188
4189 // A sibling call is one where we're under the usual C ABI and not planning
4190 // to change that but can still do a tail call:
4191 if (!TailCallOpt && IsTailCall)
4192 IsSibCall = true;
4193
4194 if (IsTailCall)
4195 ++NumTailCalls;
4196 }
4197
4200 SmallVector<SDValue, 8> MemOpChains;
4201
4202 // Analyze operands of the call, assigning locations to each operand.
4204 CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
4205 CCAssignFn *AssignFn = CCAssignFnForCall(CallConv, IsVarArg);
4206
4207 if (CallConv != CallingConv::AMDGPU_Gfx && !AMDGPU::isChainCC(CallConv) &&
4209 // With a fixed ABI, allocate fixed registers before user arguments.
4210 passSpecialInputs(CLI, CCInfo, *Info, RegsToPass, MemOpChains, Chain);
4211 }
4212
4213 CCInfo.AnalyzeCallOperands(Outs, AssignFn);
4214
4215 // Get a count of how many bytes are to be pushed on the stack.
4216 unsigned NumBytes = CCInfo.getStackSize();
4217
4218 if (IsSibCall) {
4219 // Since we're not changing the ABI to make this a tail call, the memory
4220 // operands are already available in the caller's incoming argument space.
4221 NumBytes = 0;
4222 }
4223
4224 // FPDiff is the byte offset of the call's argument area from the callee's.
4225 // Stores to callee stack arguments will be placed in FixedStackSlots offset
4226 // by this amount for a tail call. In a sibling call it must be 0 because the
4227 // caller will deallocate the entire stack and the callee still expects its
4228 // arguments to begin at SP+0. Completely unused for non-tail calls.
4229 int32_t FPDiff = 0;
4230 MachineFrameInfo &MFI = MF.getFrameInfo();
4231 auto *TRI = Subtarget->getRegisterInfo();
4232
4233 // Adjust the stack pointer for the new arguments...
4234 // These operations are automatically eliminated by the prolog/epilog pass
4235 if (!IsSibCall)
4236 Chain = DAG.getCALLSEQ_START(Chain, 0, 0, DL);
4237
4238 if (!IsSibCall || IsChainCallConv) {
4239 if (!Subtarget->enableFlatScratch()) {
4240 SmallVector<SDValue, 4> CopyFromChains;
4241
4242 // In the HSA case, this should be an identity copy.
4243 SDValue ScratchRSrcReg =
4244 DAG.getCopyFromReg(Chain, DL, Info->getScratchRSrcReg(), MVT::v4i32);
4245 RegsToPass.emplace_back(IsChainCallConv
4246 ? AMDGPU::SGPR48_SGPR49_SGPR50_SGPR51
4247 : AMDGPU::SGPR0_SGPR1_SGPR2_SGPR3,
4248 ScratchRSrcReg);
4249 CopyFromChains.push_back(ScratchRSrcReg.getValue(1));
4250 Chain = DAG.getTokenFactor(DL, CopyFromChains);
4251 }
4252 }
4253
4254 const unsigned NumSpecialInputs = RegsToPass.size();
4255
4256 MVT PtrVT = MVT::i32;
4257
4258 // Walk the register/memloc assignments, inserting copies/loads.
4259 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
4260 CCValAssign &VA = ArgLocs[i];
4261 SDValue Arg = OutVals[i];
4262
4263 // Promote the value if needed.
4264 switch (VA.getLocInfo()) {
4265 case CCValAssign::Full:
4266 break;
4267 case CCValAssign::BCvt:
4268 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
4269 break;
4270 case CCValAssign::ZExt:
4271 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg);
4272 break;
4273 case CCValAssign::SExt:
4274 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg);
4275 break;
4276 case CCValAssign::AExt:
4277 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg);
4278 break;
4279 case CCValAssign::FPExt:
4280 Arg = DAG.getNode(ISD::FP_EXTEND, DL, VA.getLocVT(), Arg);
4281 break;
4282 default:
4283 llvm_unreachable("Unknown loc info!");
4284 }
4285
4286 if (VA.isRegLoc()) {
4287 RegsToPass.push_back(std::pair(VA.getLocReg(), Arg));
4288 } else {
4289 assert(VA.isMemLoc());
4290
4291 SDValue DstAddr;
4292 MachinePointerInfo DstInfo;
4293
4294 unsigned LocMemOffset = VA.getLocMemOffset();
4295 int32_t Offset = LocMemOffset;
4296
4297 SDValue PtrOff = DAG.getConstant(Offset, DL, PtrVT);
4298 MaybeAlign Alignment;
4299
4300 if (IsTailCall) {
4301 ISD::ArgFlagsTy Flags = Outs[i].Flags;
4302 unsigned OpSize = Flags.isByVal() ? Flags.getByValSize()
4303 : VA.getValVT().getStoreSize();
4304
4305 // FIXME: We can have better than the minimum byval required alignment.
4306 Alignment =
4307 Flags.isByVal()
4308 ? Flags.getNonZeroByValAlign()
4309 : commonAlignment(Subtarget->getStackAlignment(), Offset);
4310
4311 Offset = Offset + FPDiff;
4312 int FI = MFI.CreateFixedObject(OpSize, Offset, true);
4313
4314 DstAddr = DAG.getFrameIndex(FI, PtrVT);
4315 DstInfo = MachinePointerInfo::getFixedStack(MF, FI);
4316
4317 // Make sure any stack arguments overlapping with where we're storing
4318 // are loaded before this eventual operation. Otherwise they'll be
4319 // clobbered.
4320
4321 // FIXME: Why is this really necessary? This seems to just result in a
4322 // lot of code to copy the stack and write them back to the same
4323 // locations, which are supposed to be immutable?
4324 Chain = addTokenForArgument(Chain, DAG, MFI, FI);
4325 } else {
4326 // Stores to the argument stack area are relative to the stack pointer.
4327 SDValue SP = DAG.getCopyFromReg(Chain, DL, Info->getStackPtrOffsetReg(),
4328 MVT::i32);
4329 DstAddr = DAG.getNode(ISD::ADD, DL, MVT::i32, SP, PtrOff);
4330 DstInfo = MachinePointerInfo::getStack(MF, LocMemOffset);
4331 Alignment =
4332 commonAlignment(Subtarget->getStackAlignment(), LocMemOffset);
4333 }
4334
4335 if (Outs[i].Flags.isByVal()) {
4336 SDValue SizeNode =
4337 DAG.getConstant(Outs[i].Flags.getByValSize(), DL, MVT::i32);
4338 SDValue Cpy =
4339 DAG.getMemcpy(Chain, DL, DstAddr, Arg, SizeNode,
4340 Outs[i].Flags.getNonZeroByValAlign(),
4341 /*isVol = */ false, /*AlwaysInline = */ true,
4342 /*CI=*/nullptr, std::nullopt, DstInfo,
4344
4345 MemOpChains.push_back(Cpy);
4346 } else {
4347 SDValue Store =
4348 DAG.getStore(Chain, DL, Arg, DstAddr, DstInfo, Alignment);
4349 MemOpChains.push_back(Store);
4350 }
4351 }
4352 }
4353
4354 if (!MemOpChains.empty())
4355 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
4356
4357 SDValue ReadFirstLaneID =
4358 DAG.getTargetConstant(Intrinsic::amdgcn_readfirstlane, DL, MVT::i32);
4359
4360 SDValue TokenGlue;
4361 if (CLI.ConvergenceControlToken) {
4362 TokenGlue = DAG.getNode(ISD::CONVERGENCECTRL_GLUE, DL, MVT::Glue,
4364 }
4365
4366 // Build a sequence of copy-to-reg nodes chained together with token chain
4367 // and flag operands which copy the outgoing args into the appropriate regs.
4368 SDValue InGlue;
4369
4370 unsigned ArgIdx = 0;
4371 for (auto [Reg, Val] : RegsToPass) {
4372 if (ArgIdx++ >= NumSpecialInputs &&
4373 (IsChainCallConv || !Val->isDivergent()) && TRI->isSGPRPhysReg(Reg)) {
4374 // For chain calls, the inreg arguments are required to be
4375 // uniform. Speculatively Insert a readfirstlane in case we cannot prove
4376 // they are uniform.
4377 //
4378 // For other calls, if an inreg arguments is known to be uniform,
4379 // speculatively insert a readfirstlane in case it is in a VGPR.
4380 //
4381 // FIXME: We need to execute this in a waterfall loop if it is a divergent
4382 // value, so let that continue to produce invalid code.
4383
4384 SmallVector<SDValue, 3> ReadfirstlaneArgs({ReadFirstLaneID, Val});
4385 if (TokenGlue)
4386 ReadfirstlaneArgs.push_back(TokenGlue);
4388 ReadfirstlaneArgs);
4389 }
4390
4391 Chain = DAG.getCopyToReg(Chain, DL, Reg, Val, InGlue);
4392 InGlue = Chain.getValue(1);
4393 }
4394
4395 // We don't usually want to end the call-sequence here because we would tidy
4396 // the frame up *after* the call, however in the ABI-changing tail-call case
4397 // we've carefully laid out the parameters so that when sp is reset they'll be
4398 // in the correct location.
4399 if (IsTailCall && !IsSibCall) {
4400 Chain = DAG.getCALLSEQ_END(Chain, NumBytes, 0, InGlue, DL);
4401 InGlue = Chain.getValue(1);
4402 }
4403
4404 std::vector<SDValue> Ops({Chain});
4405
4406 // Add a redundant copy of the callee global which will not be legalized, as
4407 // we need direct access to the callee later.
4409 const GlobalValue *GV = GSD->getGlobal();
4410 Ops.push_back(Callee);
4411 Ops.push_back(DAG.getTargetGlobalAddress(GV, DL, MVT::i64));
4412 } else {
4413 if (IsTailCall) {
4414 // isEligibleForTailCallOptimization considered whether the call target is
4415 // divergent, but we may still end up with a uniform value in a VGPR.
4416 // Insert a readfirstlane just in case.
4417 SDValue ReadFirstLaneID =
4418 DAG.getTargetConstant(Intrinsic::amdgcn_readfirstlane, DL, MVT::i32);
4419
4420 SmallVector<SDValue, 3> ReadfirstlaneArgs({ReadFirstLaneID, Callee});
4421 if (TokenGlue)
4422 ReadfirstlaneArgs.push_back(TokenGlue); // Wire up convergence token.
4423 Callee = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, Callee.getValueType(),
4424 ReadfirstlaneArgs);
4425 }
4426
4427 Ops.push_back(Callee);
4428 Ops.push_back(DAG.getTargetConstant(0, DL, MVT::i64));
4429 }
4430
4431 if (IsTailCall) {
4432 // Each tail call may have to adjust the stack by a different amount, so
4433 // this information must travel along with the operation for eventual
4434 // consumption by emitEpilogue.
4435 Ops.push_back(DAG.getTargetConstant(FPDiff, DL, MVT::i32));
4436 }
4437
4438 if (IsChainCallConv)
4439 llvm::append_range(Ops, ChainCallSpecialArgs);
4440
4441 // Add argument registers to the end of the list so that they are known live
4442 // into the call.
4443 for (auto &[Reg, Val] : RegsToPass)
4444 Ops.push_back(DAG.getRegister(Reg, Val.getValueType()));
4445
4446 // Add a register mask operand representing the call-preserved registers.
4447 const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv);
4448 assert(Mask && "Missing call preserved mask for calling convention");
4449 Ops.push_back(DAG.getRegisterMask(Mask));
4450
4451 if (SDValue Token = CLI.ConvergenceControlToken) {
4453 GlueOps.push_back(Token);
4454 if (InGlue)
4455 GlueOps.push_back(InGlue);
4456
4457 InGlue = SDValue(DAG.getMachineNode(TargetOpcode::CONVERGENCECTRL_GLUE, DL,
4458 MVT::Glue, GlueOps),
4459 0);
4460 }
4461
4462 if (InGlue)
4463 Ops.push_back(InGlue);
4464
4465 // If we're doing a tall call, use a TC_RETURN here rather than an
4466 // actual call instruction.
4467 if (IsTailCall) {
4468 MFI.setHasTailCall();
4469 unsigned OPC = AMDGPUISD::TC_RETURN;
4470 switch (CallConv) {
4473 break;
4476 OPC = UsesDynamicVGPRs ? AMDGPUISD::TC_RETURN_CHAIN_DVGPR
4478 break;
4479 }
4480
4481 // If the caller is a whole wave function, we need to use a special opcode
4482 // so we can patch up EXEC.
4483 if (Info->isWholeWaveFunction())
4485
4486 return DAG.getNode(OPC, DL, MVT::Other, Ops);
4487 }
4488
4489 // Returns a chain and a flag for retval copy to use.
4490 SDValue Call = DAG.getNode(AMDGPUISD::CALL, DL, {MVT::Other, MVT::Glue}, Ops);
4491 Chain = Call.getValue(0);
4492 InGlue = Call.getValue(1);
4493
4494 uint64_t CalleePopBytes = NumBytes;
4495 Chain = DAG.getCALLSEQ_END(Chain, 0, CalleePopBytes, InGlue, DL);
4496 if (!Ins.empty())
4497 InGlue = Chain.getValue(1);
4498
4499 // Handle result values, copying them out of physregs into vregs that we
4500 // return.
4501 return LowerCallResult(Chain, InGlue, CallConv, IsVarArg, Ins, DL, DAG,
4502 InVals, /*IsThisReturn=*/false, SDValue());
4503}
4504
4505// This is similar to the default implementation in ExpandDYNAMIC_STACKALLOC,
4506// except for:
4507// 1. Stack growth direction(default: downwards, AMDGPU: upwards), and
4508// 2. Scale size where, scale = wave-reduction(alloca-size) * wave-size
4510 SelectionDAG &DAG) const {
4511 const MachineFunction &MF = DAG.getMachineFunction();
4513
4514 SDLoc dl(Op);
4515 EVT VT = Op.getValueType();
4516 SDValue Chain = Op.getOperand(0);
4517 Register SPReg = Info->getStackPtrOffsetReg();
4518
4519 // Chain the dynamic stack allocation so that it doesn't modify the stack
4520 // pointer when other instructions are using the stack.
4521 Chain = DAG.getCALLSEQ_START(Chain, 0, 0, dl);
4522
4523 SDValue Size = Op.getOperand(1);
4524 SDValue BaseAddr = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
4525 Align Alignment = cast<ConstantSDNode>(Op.getOperand(2))->getAlignValue();
4526
4527 const TargetFrameLowering *TFL = Subtarget->getFrameLowering();
4529 "Stack grows upwards for AMDGPU");
4530
4531 Chain = BaseAddr.getValue(1);
4532 Align StackAlign = TFL->getStackAlign();
4533 if (Alignment > StackAlign) {
4534 uint64_t ScaledAlignment = Alignment.value()
4535 << Subtarget->getWavefrontSizeLog2();
4536 uint64_t StackAlignMask = ScaledAlignment - 1;
4537 SDValue TmpAddr = DAG.getNode(ISD::ADD, dl, VT, BaseAddr,
4538 DAG.getConstant(StackAlignMask, dl, VT));
4539 BaseAddr = DAG.getNode(ISD::AND, dl, VT, TmpAddr,
4540 DAG.getSignedConstant(-ScaledAlignment, dl, VT));
4541 }
4542
4543 assert(Size.getValueType() == MVT::i32 && "Size must be 32-bit");
4544 SDValue NewSP;
4546 // For constant sized alloca, scale alloca size by wave-size
4547 SDValue ScaledSize = DAG.getNode(
4548 ISD::SHL, dl, VT, Size,
4549 DAG.getConstant(Subtarget->getWavefrontSizeLog2(), dl, MVT::i32));
4550 NewSP = DAG.getNode(ISD::ADD, dl, VT, BaseAddr, ScaledSize); // Value
4551 } else {
4552 // For dynamic sized alloca, perform wave-wide reduction to get max of
4553 // alloca size(divergent) and then scale it by wave-size
4554 SDValue WaveReduction =
4555 DAG.getTargetConstant(Intrinsic::amdgcn_wave_reduce_umax, dl, MVT::i32);
4556 Size = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32, WaveReduction,
4557 Size, DAG.getConstant(0, dl, MVT::i32));
4558 SDValue ScaledSize = DAG.getNode(
4559 ISD::SHL, dl, VT, Size,
4560 DAG.getConstant(Subtarget->getWavefrontSizeLog2(), dl, MVT::i32));
4561 NewSP =
4562 DAG.getNode(ISD::ADD, dl, VT, BaseAddr, ScaledSize); // Value in vgpr.
4563 SDValue ReadFirstLaneID =
4564 DAG.getTargetConstant(Intrinsic::amdgcn_readfirstlane, dl, MVT::i32);
4565 NewSP = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32, ReadFirstLaneID,
4566 NewSP);
4567 }
4568
4569 Chain = DAG.getCopyToReg(Chain, dl, SPReg, NewSP); // Output chain
4570 SDValue CallSeqEnd = DAG.getCALLSEQ_END(Chain, 0, 0, SDValue(), dl);
4571
4572 return DAG.getMergeValues({BaseAddr, CallSeqEnd}, dl);
4573}
4574
4576 if (Op.getValueType() != MVT::i32)
4577 return Op; // Defer to cannot select error.
4578
4580 SDLoc SL(Op);
4581
4582 SDValue CopyFromSP = DAG.getCopyFromReg(Op->getOperand(0), SL, SP, MVT::i32);
4583
4584 // Convert from wave uniform to swizzled vector address. This should protect
4585 // from any edge cases where the stacksave result isn't directly used with
4586 // stackrestore.
4587 SDValue VectorAddress =
4588 DAG.getNode(AMDGPUISD::WAVE_ADDRESS, SL, MVT::i32, CopyFromSP);
4589 return DAG.getMergeValues({VectorAddress, CopyFromSP.getValue(1)}, SL);
4590}
4591
4593 SelectionDAG &DAG) const {
4594 SDLoc SL(Op);
4595 assert(Op.getValueType() == MVT::i32);
4596
4597 uint32_t BothRoundHwReg =
4599 SDValue GetRoundBothImm = DAG.getTargetConstant(BothRoundHwReg, SL, MVT::i32);
4600
4601 SDValue IntrinID =
4602 DAG.getTargetConstant(Intrinsic::amdgcn_s_getreg, SL, MVT::i32);
4603 SDValue GetReg = DAG.getNode(ISD::INTRINSIC_W_CHAIN, SL, Op->getVTList(),
4604 Op.getOperand(0), IntrinID, GetRoundBothImm);
4605
4606 // There are two rounding modes, one for f32 and one for f64/f16. We only
4607 // report in the standard value range if both are the same.
4608 //
4609 // The raw values also differ from the expected FLT_ROUNDS values. Nearest
4610 // ties away from zero is not supported, and the other values are rotated by
4611 // 1.
4612 //
4613 // If the two rounding modes are not the same, report a target defined value.
4614
4615 // Mode register rounding mode fields:
4616 //
4617 // [1:0] Single-precision round mode.
4618 // [3:2] Double/Half-precision round mode.
4619 //
4620 // 0=nearest even; 1= +infinity; 2= -infinity, 3= toward zero.
4621 //
4622 // Hardware Spec
4623 // Toward-0 3 0
4624 // Nearest Even 0 1
4625 // +Inf 1 2
4626 // -Inf 2 3
4627 // NearestAway0 N/A 4
4628 //
4629 // We have to handle 16 permutations of a 4-bit value, so we create a 64-bit
4630 // table we can index by the raw hardware mode.
4631 //
4632 // (trunc (FltRoundConversionTable >> MODE.fp_round)) & 0xf
4633
4634 SDValue BitTable =
4636
4637 SDValue Two = DAG.getConstant(2, SL, MVT::i32);
4638 SDValue RoundModeTimesNumBits =
4639 DAG.getNode(ISD::SHL, SL, MVT::i32, GetReg, Two);
4640
4641 // TODO: We could possibly avoid a 64-bit shift and use a simpler table if we
4642 // knew only one mode was demanded.
4643 SDValue TableValue =
4644 DAG.getNode(ISD::SRL, SL, MVT::i64, BitTable, RoundModeTimesNumBits);
4645 SDValue TruncTable = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, TableValue);
4646
4647 SDValue EntryMask = DAG.getConstant(0xf, SL, MVT::i32);
4648 SDValue TableEntry =
4649 DAG.getNode(ISD::AND, SL, MVT::i32, TruncTable, EntryMask);
4650
4651 // There's a gap in the 4-bit encoded table and actual enum values, so offset
4652 // if it's an extended value.
4653 SDValue Four = DAG.getConstant(4, SL, MVT::i32);
4654 SDValue IsStandardValue =
4655 DAG.getSetCC(SL, MVT::i1, TableEntry, Four, ISD::SETULT);
4656 SDValue EnumOffset = DAG.getNode(ISD::ADD, SL, MVT::i32, TableEntry, Four);
4657 SDValue Result = DAG.getNode(ISD::SELECT, SL, MVT::i32, IsStandardValue,
4658 TableEntry, EnumOffset);
4659
4660 return DAG.getMergeValues({Result, GetReg.getValue(1)}, SL);
4661}
4662
4664 SelectionDAG &DAG) const {
4665 SDLoc SL(Op);
4666
4667 SDValue NewMode = Op.getOperand(1);
4668 assert(NewMode.getValueType() == MVT::i32);
4669
4670 // Index a table of 4-bit entries mapping from the C FLT_ROUNDS values to the
4671 // hardware MODE.fp_round values.
4672 if (auto *ConstMode = dyn_cast<ConstantSDNode>(NewMode)) {
4673 uint32_t ClampedVal = std::min(
4674 static_cast<uint32_t>(ConstMode->getZExtValue()),
4676 NewMode = DAG.getConstant(
4677 AMDGPU::decodeFltRoundToHWConversionTable(ClampedVal), SL, MVT::i32);
4678 } else {
4679 // If we know the input can only be one of the supported standard modes in
4680 // the range 0-3, we can use a simplified mapping to hardware values.
4681 KnownBits KB = DAG.computeKnownBits(NewMode);
4682 const bool UseReducedTable = KB.countMinLeadingZeros() >= 30;
4683 // The supported standard values are 0-3. The extended values start at 8. We
4684 // need to offset by 4 if the value is in the extended range.
4685
4686 if (UseReducedTable) {
4687 // Truncate to the low 32-bits.
4688 SDValue BitTable = DAG.getConstant(
4689 AMDGPU::FltRoundToHWConversionTable & 0xffff, SL, MVT::i32);
4690
4691 SDValue Two = DAG.getConstant(2, SL, MVT::i32);
4692 SDValue RoundModeTimesNumBits =
4693 DAG.getNode(ISD::SHL, SL, MVT::i32, NewMode, Two);
4694
4695 NewMode =
4696 DAG.getNode(ISD::SRL, SL, MVT::i32, BitTable, RoundModeTimesNumBits);
4697
4698 // TODO: SimplifyDemandedBits on the setreg source here can likely reduce
4699 // the table extracted bits into inline immediates.
4700 } else {
4701 // table_index = umin(value, value - 4)
4702 // MODE.fp_round = (bit_table >> (table_index << 2)) & 0xf
4703 SDValue BitTable =
4705
4706 SDValue Four = DAG.getConstant(4, SL, MVT::i32);
4707 SDValue OffsetEnum = DAG.getNode(ISD::SUB, SL, MVT::i32, NewMode, Four);
4708 SDValue IndexVal =
4709 DAG.getNode(ISD::UMIN, SL, MVT::i32, NewMode, OffsetEnum);
4710
4711 SDValue Two = DAG.getConstant(2, SL, MVT::i32);
4712 SDValue RoundModeTimesNumBits =
4713 DAG.getNode(ISD::SHL, SL, MVT::i32, IndexVal, Two);
4714
4715 SDValue TableValue =
4716 DAG.getNode(ISD::SRL, SL, MVT::i64, BitTable, RoundModeTimesNumBits);
4717 SDValue TruncTable = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, TableValue);
4718
4719 // No need to mask out the high bits since the setreg will ignore them
4720 // anyway.
4721 NewMode = TruncTable;
4722 }
4723
4724 // Insert a readfirstlane in case the value is a VGPR. We could do this
4725 // earlier and keep more operations scalar, but that interferes with
4726 // combining the source.
4727 SDValue ReadFirstLaneID =
4728 DAG.getTargetConstant(Intrinsic::amdgcn_readfirstlane, SL, MVT::i32);
4729 NewMode = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SL, MVT::i32,
4730 ReadFirstLaneID, NewMode);
4731 }
4732
4733 // N.B. The setreg will be later folded into s_round_mode on supported
4734 // targets.
4735 SDValue IntrinID =
4736 DAG.getTargetConstant(Intrinsic::amdgcn_s_setreg, SL, MVT::i32);
4737 uint32_t BothRoundHwReg =
4739 SDValue RoundBothImm = DAG.getTargetConstant(BothRoundHwReg, SL, MVT::i32);
4740
4741 SDValue SetReg =
4742 DAG.getNode(ISD::INTRINSIC_VOID, SL, Op->getVTList(), Op.getOperand(0),
4743 IntrinID, RoundBothImm, NewMode);
4744
4745 return SetReg;
4746}
4747
4749 if (Op->isDivergent() &&
4750 (!Subtarget->hasVmemPrefInsts() || !Op.getConstantOperandVal(4)))
4751 // Cannot do I$ prefetch with divergent pointer.
4752 return SDValue();
4753
4754 switch (cast<MemSDNode>(Op)->getAddressSpace()) {
4758 break;
4760 if (Subtarget->hasSafeSmemPrefetch())
4761 break;
4762 [[fallthrough]];
4763 default:
4764 return SDValue();
4765 }
4766
4767 // I$ prefetch
4768 if (!Subtarget->hasSafeSmemPrefetch() && !Op.getConstantOperandVal(4))
4769 return SDValue();
4770
4771 return Op;
4772}
4773
4774// Work around DAG legality rules only based on the result type.
4776 bool IsStrict = Op.getOpcode() == ISD::STRICT_FP_EXTEND;
4777 SDValue Src = Op.getOperand(IsStrict ? 1 : 0);
4778 EVT SrcVT = Src.getValueType();
4779
4780 if (SrcVT.getScalarType() != MVT::bf16)
4781 return Op;
4782
4783 SDLoc SL(Op);
4784 SDValue BitCast =
4785 DAG.getNode(ISD::BITCAST, SL, SrcVT.changeTypeToInteger(), Src);
4786
4787 EVT DstVT = Op.getValueType();
4788 if (IsStrict)
4789 llvm_unreachable("Need STRICT_BF16_TO_FP");
4790
4791 return DAG.getNode(ISD::BF16_TO_FP, SL, DstVT, BitCast);
4792}
4793
4795 SDLoc SL(Op);
4796 if (Op.getValueType() != MVT::i64)
4797 return Op;
4798
4799 uint32_t ModeHwReg =
4801 SDValue ModeHwRegImm = DAG.getTargetConstant(ModeHwReg, SL, MVT::i32);
4802 uint32_t TrapHwReg =
4804 SDValue TrapHwRegImm = DAG.getTargetConstant(TrapHwReg, SL, MVT::i32);
4805
4806 SDVTList VTList = DAG.getVTList(MVT::i32, MVT::Other);
4807 SDValue IntrinID =
4808 DAG.getTargetConstant(Intrinsic::amdgcn_s_getreg, SL, MVT::i32);
4809 SDValue GetModeReg = DAG.getNode(ISD::INTRINSIC_W_CHAIN, SL, VTList,
4810 Op.getOperand(0), IntrinID, ModeHwRegImm);
4811 SDValue GetTrapReg = DAG.getNode(ISD::INTRINSIC_W_CHAIN, SL, VTList,
4812 Op.getOperand(0), IntrinID, TrapHwRegImm);
4813 SDValue TokenReg =
4814 DAG.getNode(ISD::TokenFactor, SL, MVT::Other, GetModeReg.getValue(1),
4815 GetTrapReg.getValue(1));
4816
4817 SDValue CvtPtr =
4818 DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, GetModeReg, GetTrapReg);
4819 SDValue Result = DAG.getNode(ISD::BITCAST, SL, MVT::i64, CvtPtr);
4820
4821 return DAG.getMergeValues({Result, TokenReg}, SL);
4822}
4823
4825 SDLoc SL(Op);
4826 if (Op.getOperand(1).getValueType() != MVT::i64)
4827 return Op;
4828
4829 SDValue Input = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op.getOperand(1));
4830 SDValue NewModeReg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Input,
4831 DAG.getConstant(0, SL, MVT::i32));
4832 SDValue NewTrapReg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Input,
4833 DAG.getConstant(1, SL, MVT::i32));
4834
4835 SDValue ReadFirstLaneID =
4836 DAG.getTargetConstant(Intrinsic::amdgcn_readfirstlane, SL, MVT::i32);
4837 NewModeReg = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SL, MVT::i32,
4838 ReadFirstLaneID, NewModeReg);
4839 NewTrapReg = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SL, MVT::i32,
4840 ReadFirstLaneID, NewTrapReg);
4841
4842 unsigned ModeHwReg =
4844 SDValue ModeHwRegImm = DAG.getTargetConstant(ModeHwReg, SL, MVT::i32);
4845 unsigned TrapHwReg =
4847 SDValue TrapHwRegImm = DAG.getTargetConstant(TrapHwReg, SL, MVT::i32);
4848
4849 SDValue IntrinID =
4850 DAG.getTargetConstant(Intrinsic::amdgcn_s_setreg, SL, MVT::i32);
4851 SDValue SetModeReg =
4852 DAG.getNode(ISD::INTRINSIC_VOID, SL, MVT::Other, Op.getOperand(0),
4853 IntrinID, ModeHwRegImm, NewModeReg);
4854 SDValue SetTrapReg =
4855 DAG.getNode(ISD::INTRINSIC_VOID, SL, MVT::Other, Op.getOperand(0),
4856 IntrinID, TrapHwRegImm, NewTrapReg);
4857 return DAG.getNode(ISD::TokenFactor, SL, MVT::Other, SetTrapReg, SetModeReg);
4858}
4859
4861 const MachineFunction &MF) const {
4862 const Function &Fn = MF.getFunction();
4863
4865 .Case("m0", AMDGPU::M0)
4866 .Case("exec", AMDGPU::EXEC)
4867 .Case("exec_lo", AMDGPU::EXEC_LO)
4868 .Case("exec_hi", AMDGPU::EXEC_HI)
4869 .Case("flat_scratch", AMDGPU::FLAT_SCR)
4870 .Case("flat_scratch_lo", AMDGPU::FLAT_SCR_LO)
4871 .Case("flat_scratch_hi", AMDGPU::FLAT_SCR_HI)
4872 .Default(Register());
4873 if (!Reg)
4874 return Reg;
4875
4876 if (!Subtarget->hasFlatScrRegister() &&
4877 Subtarget->getRegisterInfo()->regsOverlap(Reg, AMDGPU::FLAT_SCR)) {
4878 Fn.getContext().emitError(Twine("invalid register \"" + StringRef(RegName) +
4879 "\" for subtarget."));
4880 }
4881
4882 switch (Reg) {
4883 case AMDGPU::M0:
4884 case AMDGPU::EXEC_LO:
4885 case AMDGPU::EXEC_HI:
4886 case AMDGPU::FLAT_SCR_LO:
4887 case AMDGPU::FLAT_SCR_HI:
4888 if (VT.getSizeInBits() == 32)
4889 return Reg;
4890 break;
4891 case AMDGPU::EXEC:
4892 case AMDGPU::FLAT_SCR:
4893 if (VT.getSizeInBits() == 64)
4894 return Reg;
4895 break;
4896 default:
4897 llvm_unreachable("missing register type checking");
4898 }
4899
4901 Twine("invalid type for register \"" + StringRef(RegName) + "\"."));
4902}
4903
4904// If kill is not the last instruction, split the block so kill is always a
4905// proper terminator.
4908 MachineBasicBlock *BB) const {
4909 MachineBasicBlock *SplitBB = BB->splitAt(MI, /*UpdateLiveIns=*/true);
4911 MI.setDesc(TII->getKillTerminatorFromPseudo(MI.getOpcode()));
4912 return SplitBB;
4913}
4914
4915// Split block \p MBB at \p MI, as to insert a loop. If \p InstInLoop is true,
4916// \p MI will be the only instruction in the loop body block. Otherwise, it will
4917// be the first instruction in the remainder block.
4918//
4919/// \returns { LoopBody, Remainder }
4920static std::pair<MachineBasicBlock *, MachineBasicBlock *>
4922 MachineFunction *MF = MBB.getParent();
4924
4925 // To insert the loop we need to split the block. Move everything after this
4926 // point to a new block, and insert a new empty block between the two.
4928 MachineBasicBlock *RemainderBB = MF->CreateMachineBasicBlock();
4930 ++MBBI;
4931
4932 MF->insert(MBBI, LoopBB);
4933 MF->insert(MBBI, RemainderBB);
4934
4935 LoopBB->addSuccessor(LoopBB);
4936 LoopBB->addSuccessor(RemainderBB);
4937
4938 // Move the rest of the block into a new block.
4939 RemainderBB->transferSuccessorsAndUpdatePHIs(&MBB);
4940
4941 if (InstInLoop) {
4942 auto Next = std::next(I);
4943
4944 // Move instruction to loop body.
4945 LoopBB->splice(LoopBB->begin(), &MBB, I, Next);
4946
4947 // Move the rest of the block.
4948 RemainderBB->splice(RemainderBB->begin(), &MBB, Next, MBB.end());
4949 } else {
4950 RemainderBB->splice(RemainderBB->begin(), &MBB, I, MBB.end());
4951 }
4952
4953 MBB.addSuccessor(LoopBB);
4954
4955 return std::pair(LoopBB, RemainderBB);
4956}
4957
4958/// Insert \p MI into a BUNDLE with an S_WAITCNT 0 immediately following it.
4960 MachineBasicBlock *MBB = MI.getParent();
4962 auto I = MI.getIterator();
4963 auto E = std::next(I);
4964
4965 // clang-format off
4966 BuildMI(*MBB, E, MI.getDebugLoc(), TII->get(AMDGPU::S_WAITCNT))
4967 .addImm(0);
4968 // clang-format on
4969
4970 MIBundleBuilder Bundler(*MBB, I, E);
4971 finalizeBundle(*MBB, Bundler.begin());
4972}
4973
4976 MachineBasicBlock *BB) const {
4977 const DebugLoc &DL = MI.getDebugLoc();
4978
4980
4982
4983 // Apparently kill flags are only valid if the def is in the same block?
4984 if (MachineOperand *Src = TII->getNamedOperand(MI, AMDGPU::OpName::data0))
4985 Src->setIsKill(false);
4986
4987 auto [LoopBB, RemainderBB] = splitBlockForLoop(MI, *BB, true);
4988
4989 MachineBasicBlock::iterator I = LoopBB->end();
4990
4991 const unsigned EncodedReg = AMDGPU::Hwreg::HwregEncoding::encode(
4993
4994 // Clear TRAP_STS.MEM_VIOL
4995 BuildMI(*LoopBB, LoopBB->begin(), DL, TII->get(AMDGPU::S_SETREG_IMM32_B32))
4996 .addImm(0)
4997 .addImm(EncodedReg);
4998
5000
5001 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
5002
5003 // Load and check TRAP_STS.MEM_VIOL
5004 BuildMI(*LoopBB, I, DL, TII->get(AMDGPU::S_GETREG_B32), Reg)
5005 .addImm(EncodedReg);
5006
5007 // FIXME: Do we need to use an isel pseudo that may clobber scc?
5008 BuildMI(*LoopBB, I, DL, TII->get(AMDGPU::S_CMP_LG_U32))
5009 .addReg(Reg, RegState::Kill)
5010 .addImm(0);
5011 // clang-format off
5012 BuildMI(*LoopBB, I, DL, TII->get(AMDGPU::S_CBRANCH_SCC1))
5013 .addMBB(LoopBB);
5014 // clang-format on
5015
5016 return RemainderBB;
5017}
5018
5019// Do a v_movrels_b32 or v_movreld_b32 for each unique value of \p IdxReg in the
5020// wavefront. If the value is uniform and just happens to be in a VGPR, this
5021// will only do one iteration. In the worst case, this will loop 64 times.
5022//
5023// TODO: Just use v_readlane_b32 if we know the VGPR has a uniform value.
5026 MachineBasicBlock &OrigBB, MachineBasicBlock &LoopBB,
5027 const DebugLoc &DL, const MachineOperand &Idx,
5028 unsigned InitReg, unsigned ResultReg, unsigned PhiReg,
5029 unsigned InitSaveExecReg, int Offset, bool UseGPRIdxMode,
5030 Register &SGPRIdxReg) {
5031
5032 MachineFunction *MF = OrigBB.getParent();
5033 const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>();
5034 const SIRegisterInfo *TRI = ST.getRegisterInfo();
5037
5038 const TargetRegisterClass *BoolRC = TRI->getBoolRC();
5039 Register PhiExec = MRI.createVirtualRegister(BoolRC);
5040 Register NewExec = MRI.createVirtualRegister(BoolRC);
5041 Register CurrentIdxReg =
5042 MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
5043 Register CondReg = MRI.createVirtualRegister(BoolRC);
5044
5045 BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiReg)
5046 .addReg(InitReg)
5047 .addMBB(&OrigBB)
5048 .addReg(ResultReg)
5049 .addMBB(&LoopBB);
5050
5051 BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiExec)
5052 .addReg(InitSaveExecReg)
5053 .addMBB(&OrigBB)
5054 .addReg(NewExec)
5055 .addMBB(&LoopBB);
5056
5057 // Read the next variant <- also loop target.
5058 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32), CurrentIdxReg)
5059 .addReg(Idx.getReg(), getUndefRegState(Idx.isUndef()));
5060
5061 // Compare the just read M0 value to all possible Idx values.
5062 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_CMP_EQ_U32_e64), CondReg)
5063 .addReg(CurrentIdxReg)
5064 .addReg(Idx.getReg(), 0, Idx.getSubReg());
5065
5066 // Update EXEC, save the original EXEC value to VCC.
5067 BuildMI(LoopBB, I, DL, TII->get(LMC.AndSaveExecOpc), NewExec)
5068 .addReg(CondReg, RegState::Kill);
5069
5070 MRI.setSimpleHint(NewExec, CondReg);
5071
5072 if (UseGPRIdxMode) {
5073 if (Offset == 0) {
5074 SGPRIdxReg = CurrentIdxReg;
5075 } else {
5076 SGPRIdxReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
5077 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_ADD_I32), SGPRIdxReg)
5078 .addReg(CurrentIdxReg, RegState::Kill)
5079 .addImm(Offset);
5080 }
5081 } else {
5082 // Move index from VCC into M0
5083 if (Offset == 0) {
5084 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::COPY), AMDGPU::M0)
5085 .addReg(CurrentIdxReg, RegState::Kill);
5086 } else {
5087 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
5088 .addReg(CurrentIdxReg, RegState::Kill)
5089 .addImm(Offset);
5090 }
5091 }
5092
5093 // Update EXEC, switch all done bits to 0 and all todo bits to 1.
5094 MachineInstr *InsertPt =
5095 BuildMI(LoopBB, I, DL, TII->get(LMC.XorTermOpc), LMC.ExecReg)
5096 .addReg(LMC.ExecReg)
5097 .addReg(NewExec);
5098
5099 // XXX - s_xor_b64 sets scc to 1 if the result is nonzero, so can we use
5100 // s_cbranch_scc0?
5101
5102 // Loop back to V_READFIRSTLANE_B32 if there are still variants to cover.
5103 // clang-format off
5104 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
5105 .addMBB(&LoopBB);
5106 // clang-format on
5107
5108 return InsertPt->getIterator();
5109}
5110
5111// This has slightly sub-optimal regalloc when the source vector is killed by
5112// the read. The register allocator does not understand that the kill is
5113// per-workitem, so is kept alive for the whole loop so we end up not re-using a
5114// subregister from it, using 1 more VGPR than necessary. This was saved when
5115// this was expanded after register allocation.
5118 unsigned InitResultReg, unsigned PhiReg, int Offset,
5119 bool UseGPRIdxMode, Register &SGPRIdxReg) {
5120 MachineFunction *MF = MBB.getParent();
5121 const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>();
5122 const SIRegisterInfo *TRI = ST.getRegisterInfo();
5124 const DebugLoc &DL = MI.getDebugLoc();
5126
5127 const auto *BoolXExecRC = TRI->getWaveMaskRegClass();
5128 Register DstReg = MI.getOperand(0).getReg();
5129 Register SaveExec = MRI.createVirtualRegister(BoolXExecRC);
5130 Register TmpExec = MRI.createVirtualRegister(BoolXExecRC);
5132
5133 BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), TmpExec);
5134
5135 // Save the EXEC mask
5136 // clang-format off
5137 BuildMI(MBB, I, DL, TII->get(LMC.MovOpc), SaveExec)
5138 .addReg(LMC.ExecReg);
5139 // clang-format on
5140
5141 auto [LoopBB, RemainderBB] = splitBlockForLoop(MI, MBB, false);
5142
5143 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
5144
5145 auto InsPt = emitLoadM0FromVGPRLoop(TII, MRI, MBB, *LoopBB, DL, *Idx,
5146 InitResultReg, DstReg, PhiReg, TmpExec,
5147 Offset, UseGPRIdxMode, SGPRIdxReg);
5148
5149 MachineBasicBlock *LandingPad = MF->CreateMachineBasicBlock();
5151 ++MBBI;
5152 MF->insert(MBBI, LandingPad);
5153 LoopBB->removeSuccessor(RemainderBB);
5154 LandingPad->addSuccessor(RemainderBB);
5155 LoopBB->addSuccessor(LandingPad);
5156 MachineBasicBlock::iterator First = LandingPad->begin();
5157 // clang-format off
5158 BuildMI(*LandingPad, First, DL, TII->get(LMC.MovOpc), LMC.ExecReg)
5159 .addReg(SaveExec);
5160 // clang-format on
5161
5162 return InsPt;
5163}
5164
5165// Returns subreg index, offset
5166static std::pair<unsigned, int>
5168 const TargetRegisterClass *SuperRC, unsigned VecReg,
5169 int Offset) {
5170 int NumElts = TRI.getRegSizeInBits(*SuperRC) / 32;
5171
5172 // Skip out of bounds offsets, or else we would end up using an undefined
5173 // register.
5174 if (Offset >= NumElts || Offset < 0)
5175 return std::pair(AMDGPU::sub0, Offset);
5176
5177 return std::pair(SIRegisterInfo::getSubRegFromChannel(Offset), 0);
5178}
5179
5182 int Offset) {
5183 MachineBasicBlock *MBB = MI.getParent();
5184 const DebugLoc &DL = MI.getDebugLoc();
5186
5187 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
5188
5189 assert(Idx->getReg() != AMDGPU::NoRegister);
5190
5191 if (Offset == 0) {
5192 // clang-format off
5193 BuildMI(*MBB, I, DL, TII->get(AMDGPU::COPY), AMDGPU::M0)
5194 .add(*Idx);
5195 // clang-format on
5196 } else {
5197 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
5198 .add(*Idx)
5199 .addImm(Offset);
5200 }
5201}
5202
5205 int Offset) {
5206 MachineBasicBlock *MBB = MI.getParent();
5207 const DebugLoc &DL = MI.getDebugLoc();
5209
5210 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
5211
5212 if (Offset == 0)
5213 return Idx->getReg();
5214
5215 Register Tmp = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
5216 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_ADD_I32), Tmp)
5217 .add(*Idx)
5218 .addImm(Offset);
5219 return Tmp;
5220}
5221
5224 const GCNSubtarget &ST) {
5225 const SIInstrInfo *TII = ST.getInstrInfo();
5226 const SIRegisterInfo &TRI = TII->getRegisterInfo();
5227 MachineFunction *MF = MBB.getParent();
5229
5230 Register Dst = MI.getOperand(0).getReg();
5231 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
5232 Register SrcReg = TII->getNamedOperand(MI, AMDGPU::OpName::src)->getReg();
5233 int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm();
5234
5235 const TargetRegisterClass *VecRC = MRI.getRegClass(SrcReg);
5236 const TargetRegisterClass *IdxRC = MRI.getRegClass(Idx->getReg());
5237
5238 unsigned SubReg;
5239 std::tie(SubReg, Offset) =
5240 computeIndirectRegAndOffset(TRI, VecRC, SrcReg, Offset);
5241
5242 const bool UseGPRIdxMode = ST.useVGPRIndexMode();
5243
5244 // Check for a SGPR index.
5245 if (TII->getRegisterInfo().isSGPRClass(IdxRC)) {
5247 const DebugLoc &DL = MI.getDebugLoc();
5248
5249 if (UseGPRIdxMode) {
5250 // TODO: Look at the uses to avoid the copy. This may require rescheduling
5251 // to avoid interfering with other uses, so probably requires a new
5252 // optimization pass.
5254
5255 const MCInstrDesc &GPRIDXDesc =
5256 TII->getIndirectGPRIDXPseudo(TRI.getRegSizeInBits(*VecRC), true);
5257 BuildMI(MBB, I, DL, GPRIDXDesc, Dst)
5258 .addReg(SrcReg)
5259 .addReg(Idx)
5260 .addImm(SubReg);
5261 } else {
5263
5264 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
5265 .addReg(SrcReg, 0, SubReg)
5266 .addReg(SrcReg, RegState::Implicit);
5267 }
5268
5269 MI.eraseFromParent();
5270
5271 return &MBB;
5272 }
5273
5274 // Control flow needs to be inserted if indexing with a VGPR.
5275 const DebugLoc &DL = MI.getDebugLoc();
5277
5278 Register PhiReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
5279 Register InitReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
5280
5281 BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), InitReg);
5282
5283 Register SGPRIdxReg;
5284 auto InsPt = loadM0FromVGPR(TII, MBB, MI, InitReg, PhiReg, Offset,
5285 UseGPRIdxMode, SGPRIdxReg);
5286
5287 MachineBasicBlock *LoopBB = InsPt->getParent();
5288
5289 if (UseGPRIdxMode) {
5290 const MCInstrDesc &GPRIDXDesc =
5291 TII->getIndirectGPRIDXPseudo(TRI.getRegSizeInBits(*VecRC), true);
5292
5293 BuildMI(*LoopBB, InsPt, DL, GPRIDXDesc, Dst)
5294 .addReg(SrcReg)
5295 .addReg(SGPRIdxReg)
5296 .addImm(SubReg);
5297 } else {
5298 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
5299 .addReg(SrcReg, 0, SubReg)
5300 .addReg(SrcReg, RegState::Implicit);
5301 }
5302
5303 MI.eraseFromParent();
5304
5305 return LoopBB;
5306}
5307
5310 const GCNSubtarget &ST) {
5311 const SIInstrInfo *TII = ST.getInstrInfo();
5312 const SIRegisterInfo &TRI = TII->getRegisterInfo();
5313 MachineFunction *MF = MBB.getParent();
5315
5316 Register Dst = MI.getOperand(0).getReg();
5317 const MachineOperand *SrcVec = TII->getNamedOperand(MI, AMDGPU::OpName::src);
5318 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
5319 const MachineOperand *Val = TII->getNamedOperand(MI, AMDGPU::OpName::val);
5320 int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm();
5321 const TargetRegisterClass *VecRC = MRI.getRegClass(SrcVec->getReg());
5322 const TargetRegisterClass *IdxRC = MRI.getRegClass(Idx->getReg());
5323
5324 // This can be an immediate, but will be folded later.
5325 assert(Val->getReg());
5326
5327 unsigned SubReg;
5328 std::tie(SubReg, Offset) =
5329 computeIndirectRegAndOffset(TRI, VecRC, SrcVec->getReg(), Offset);
5330 const bool UseGPRIdxMode = ST.useVGPRIndexMode();
5331
5332 if (Idx->getReg() == AMDGPU::NoRegister) {
5334 const DebugLoc &DL = MI.getDebugLoc();
5335
5336 assert(Offset == 0);
5337
5338 BuildMI(MBB, I, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dst)
5339 .add(*SrcVec)
5340 .add(*Val)
5341 .addImm(SubReg);
5342
5343 MI.eraseFromParent();
5344 return &MBB;
5345 }
5346
5347 // Check for a SGPR index.
5348 if (TII->getRegisterInfo().isSGPRClass(IdxRC)) {
5350 const DebugLoc &DL = MI.getDebugLoc();
5351
5352 if (UseGPRIdxMode) {
5354
5355 const MCInstrDesc &GPRIDXDesc =
5356 TII->getIndirectGPRIDXPseudo(TRI.getRegSizeInBits(*VecRC), false);
5357 BuildMI(MBB, I, DL, GPRIDXDesc, Dst)
5358 .addReg(SrcVec->getReg())
5359 .add(*Val)
5360 .addReg(Idx)
5361 .addImm(SubReg);
5362 } else {
5364
5365 const MCInstrDesc &MovRelDesc = TII->getIndirectRegWriteMovRelPseudo(
5366 TRI.getRegSizeInBits(*VecRC), 32, false);
5367 BuildMI(MBB, I, DL, MovRelDesc, Dst)
5368 .addReg(SrcVec->getReg())
5369 .add(*Val)
5370 .addImm(SubReg);
5371 }
5372 MI.eraseFromParent();
5373 return &MBB;
5374 }
5375
5376 // Control flow needs to be inserted if indexing with a VGPR.
5377 if (Val->isReg())
5378 MRI.clearKillFlags(Val->getReg());
5379
5380 const DebugLoc &DL = MI.getDebugLoc();
5381
5382 Register PhiReg = MRI.createVirtualRegister(VecRC);
5383
5384 Register SGPRIdxReg;
5385 auto InsPt = loadM0FromVGPR(TII, MBB, MI, SrcVec->getReg(), PhiReg, Offset,
5386 UseGPRIdxMode, SGPRIdxReg);
5387 MachineBasicBlock *LoopBB = InsPt->getParent();
5388
5389 if (UseGPRIdxMode) {
5390 const MCInstrDesc &GPRIDXDesc =
5391 TII->getIndirectGPRIDXPseudo(TRI.getRegSizeInBits(*VecRC), false);
5392
5393 BuildMI(*LoopBB, InsPt, DL, GPRIDXDesc, Dst)
5394 .addReg(PhiReg)
5395 .add(*Val)
5396 .addReg(SGPRIdxReg)
5397 .addImm(SubReg);
5398 } else {
5399 const MCInstrDesc &MovRelDesc = TII->getIndirectRegWriteMovRelPseudo(
5400 TRI.getRegSizeInBits(*VecRC), 32, false);
5401 BuildMI(*LoopBB, InsPt, DL, MovRelDesc, Dst)
5402 .addReg(PhiReg)
5403 .add(*Val)
5404 .addImm(SubReg);
5405 }
5406
5407 MI.eraseFromParent();
5408 return LoopBB;
5409}
5410
5412 MachineBasicBlock *BB) {
5413 // For targets older than GFX12, we emit a sequence of 32-bit operations.
5414 // For GFX12, we emit s_add_u64 and s_sub_u64.
5415 MachineFunction *MF = BB->getParent();
5416 const SIInstrInfo *TII = MF->getSubtarget<GCNSubtarget>().getInstrInfo();
5417 const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>();
5419 const DebugLoc &DL = MI.getDebugLoc();
5420 MachineOperand &Dest = MI.getOperand(0);
5421 MachineOperand &Src0 = MI.getOperand(1);
5422 MachineOperand &Src1 = MI.getOperand(2);
5423 bool IsAdd = (MI.getOpcode() == AMDGPU::S_ADD_U64_PSEUDO);
5424 if (ST.hasScalarAddSub64()) {
5425 unsigned Opc = IsAdd ? AMDGPU::S_ADD_U64 : AMDGPU::S_SUB_U64;
5426 // clang-format off
5427 BuildMI(*BB, MI, DL, TII->get(Opc), Dest.getReg())
5428 .add(Src0)
5429 .add(Src1);
5430 // clang-format on
5431 } else {
5432 const SIRegisterInfo *TRI = ST.getRegisterInfo();
5433 const TargetRegisterClass *BoolRC = TRI->getBoolRC();
5434
5435 Register DestSub0 = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
5436 Register DestSub1 = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
5437
5438 MachineOperand Src0Sub0 = TII->buildExtractSubRegOrImm(
5439 MI, MRI, Src0, BoolRC, AMDGPU::sub0, &AMDGPU::SReg_32RegClass);
5440 MachineOperand Src0Sub1 = TII->buildExtractSubRegOrImm(
5441 MI, MRI, Src0, BoolRC, AMDGPU::sub1, &AMDGPU::SReg_32RegClass);
5442
5443 MachineOperand Src1Sub0 = TII->buildExtractSubRegOrImm(
5444 MI, MRI, Src1, BoolRC, AMDGPU::sub0, &AMDGPU::SReg_32RegClass);
5445 MachineOperand Src1Sub1 = TII->buildExtractSubRegOrImm(
5446 MI, MRI, Src1, BoolRC, AMDGPU::sub1, &AMDGPU::SReg_32RegClass);
5447
5448 unsigned LoOpc = IsAdd ? AMDGPU::S_ADD_U32 : AMDGPU::S_SUB_U32;
5449 unsigned HiOpc = IsAdd ? AMDGPU::S_ADDC_U32 : AMDGPU::S_SUBB_U32;
5450 BuildMI(*BB, MI, DL, TII->get(LoOpc), DestSub0).add(Src0Sub0).add(Src1Sub0);
5451 BuildMI(*BB, MI, DL, TII->get(HiOpc), DestSub1).add(Src0Sub1).add(Src1Sub1);
5452 BuildMI(*BB, MI, DL, TII->get(TargetOpcode::REG_SEQUENCE), Dest.getReg())
5453 .addReg(DestSub0)
5454 .addImm(AMDGPU::sub0)
5455 .addReg(DestSub1)
5456 .addImm(AMDGPU::sub1);
5457 }
5458 MI.eraseFromParent();
5459 return BB;
5460}
5461
5463 switch (Opc) {
5464 case AMDGPU::S_MIN_U32:
5465 return std::numeric_limits<uint32_t>::max();
5466 case AMDGPU::S_MIN_I32:
5467 return std::numeric_limits<int32_t>::max();
5468 case AMDGPU::S_MAX_U32:
5469 return std::numeric_limits<uint32_t>::min();
5470 case AMDGPU::S_MAX_I32:
5471 return std::numeric_limits<int32_t>::min();
5472 case AMDGPU::S_ADD_I32:
5473 case AMDGPU::S_SUB_I32:
5474 case AMDGPU::S_OR_B32:
5475 case AMDGPU::S_XOR_B32:
5476 return std::numeric_limits<uint32_t>::min();
5477 case AMDGPU::S_AND_B32:
5478 return std::numeric_limits<uint32_t>::max();
5479 default:
5481 "Unexpected opcode in getIdentityValueFor32BitWaveReduction");
5482 }
5483}
5484
5486 switch (Opc) {
5487 case AMDGPU::V_CMP_LT_U64_e64: // umin.u64
5488 return std::numeric_limits<uint64_t>::max();
5489 case AMDGPU::V_CMP_LT_I64_e64: // min.i64
5490 return std::numeric_limits<int64_t>::max();
5491 case AMDGPU::V_CMP_GT_U64_e64: // umax.u64
5492 return std::numeric_limits<uint64_t>::min();
5493 case AMDGPU::V_CMP_GT_I64_e64: // max.i64
5494 return std::numeric_limits<int64_t>::min();
5495 case AMDGPU::S_ADD_U64_PSEUDO:
5496 case AMDGPU::S_SUB_U64_PSEUDO:
5497 case AMDGPU::S_OR_B64:
5498 case AMDGPU::S_XOR_B64:
5499 return std::numeric_limits<uint64_t>::min();
5500 case AMDGPU::S_AND_B64:
5501 return std::numeric_limits<uint64_t>::max();
5502 default:
5504 "Unexpected opcode in getIdentityValueFor64BitWaveReduction");
5505 }
5506}
5507
5508static bool is32bitWaveReduceOperation(unsigned Opc) {
5509 return Opc == AMDGPU::S_MIN_U32 || Opc == AMDGPU::S_MIN_I32 ||
5510 Opc == AMDGPU::S_MAX_U32 || Opc == AMDGPU::S_MAX_I32 ||
5511 Opc == AMDGPU::S_ADD_I32 || Opc == AMDGPU::S_SUB_I32 ||
5512 Opc == AMDGPU::S_AND_B32 || Opc == AMDGPU::S_OR_B32 ||
5513 Opc == AMDGPU::S_XOR_B32;
5514}
5515
5518 const GCNSubtarget &ST,
5519 unsigned Opc) {
5521 const SIRegisterInfo *TRI = ST.getRegisterInfo();
5522 const DebugLoc &DL = MI.getDebugLoc();
5523 const SIInstrInfo *TII = ST.getInstrInfo();
5524
5525 // Reduction operations depend on whether the input operand is SGPR or VGPR.
5526 Register SrcReg = MI.getOperand(1).getReg();
5527 bool isSGPR = TRI->isSGPRClass(MRI.getRegClass(SrcReg));
5528 Register DstReg = MI.getOperand(0).getReg();
5529 MachineBasicBlock *RetBB = nullptr;
5530 if (isSGPR) {
5531 switch (Opc) {
5532 case AMDGPU::S_MIN_U32:
5533 case AMDGPU::S_MIN_I32:
5534 case AMDGPU::S_MAX_U32:
5535 case AMDGPU::S_MAX_I32:
5536 case AMDGPU::S_AND_B32:
5537 case AMDGPU::S_OR_B32: {
5538 // Idempotent operations.
5539 BuildMI(BB, MI, DL, TII->get(AMDGPU::S_MOV_B32), DstReg).addReg(SrcReg);
5540 RetBB = &BB;
5541 break;
5542 }
5543 case AMDGPU::V_CMP_LT_U64_e64: // umin
5544 case AMDGPU::V_CMP_LT_I64_e64: // min
5545 case AMDGPU::V_CMP_GT_U64_e64: // umax
5546 case AMDGPU::V_CMP_GT_I64_e64: // max
5547 case AMDGPU::S_AND_B64:
5548 case AMDGPU::S_OR_B64: {
5549 // Idempotent operations.
5550 BuildMI(BB, MI, DL, TII->get(AMDGPU::S_MOV_B64), DstReg).addReg(SrcReg);
5551 RetBB = &BB;
5552 break;
5553 }
5554 case AMDGPU::S_XOR_B32:
5555 case AMDGPU::S_XOR_B64:
5556 case AMDGPU::S_ADD_I32:
5557 case AMDGPU::S_ADD_U64_PSEUDO:
5558 case AMDGPU::S_SUB_I32:
5559 case AMDGPU::S_SUB_U64_PSEUDO: {
5560 const TargetRegisterClass *WaveMaskRegClass = TRI->getWaveMaskRegClass();
5561 const TargetRegisterClass *DstRegClass = MRI.getRegClass(DstReg);
5562 Register ExecMask = MRI.createVirtualRegister(WaveMaskRegClass);
5563 Register NumActiveLanes =
5564 MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
5565
5566 bool IsWave32 = ST.isWave32();
5567 unsigned MovOpc = IsWave32 ? AMDGPU::S_MOV_B32 : AMDGPU::S_MOV_B64;
5568 MCRegister ExecReg = IsWave32 ? AMDGPU::EXEC_LO : AMDGPU::EXEC;
5569 unsigned BitCountOpc =
5570 IsWave32 ? AMDGPU::S_BCNT1_I32_B32 : AMDGPU::S_BCNT1_I32_B64;
5571
5572 BuildMI(BB, MI, DL, TII->get(MovOpc), ExecMask).addReg(ExecReg);
5573
5574 auto NewAccumulator =
5575 BuildMI(BB, MI, DL, TII->get(BitCountOpc), NumActiveLanes)
5576 .addReg(ExecMask);
5577
5578 switch (Opc) {
5579 case AMDGPU::S_XOR_B32:
5580 case AMDGPU::S_XOR_B64: {
5581 // Performing an XOR operation on a uniform value
5582 // depends on the parity of the number of active lanes.
5583 // For even parity, the result will be 0, for odd
5584 // parity the result will be the same as the input value.
5585 Register ParityRegister =
5586 MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
5587
5588 BuildMI(BB, MI, DL, TII->get(AMDGPU::S_AND_B32), ParityRegister)
5589 .addReg(NewAccumulator->getOperand(0).getReg())
5590 .addImm(1)
5591 .setOperandDead(3); // Dead scc
5592 if (Opc == AMDGPU::S_XOR_B32) {
5593 BuildMI(BB, MI, DL, TII->get(AMDGPU::S_MUL_I32), DstReg)
5594 .addReg(SrcReg)
5595 .addReg(ParityRegister);
5596 } else {
5597 Register DestSub0 =
5598 MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
5599 Register DestSub1 =
5600 MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
5601
5602 const TargetRegisterClass *SrcRC = MRI.getRegClass(SrcReg);
5603 const TargetRegisterClass *SrcSubRC =
5604 TRI->getSubRegisterClass(SrcRC, AMDGPU::sub0);
5605
5606 MachineOperand Op1L = TII->buildExtractSubRegOrImm(
5607 MI, MRI, MI.getOperand(1), SrcRC, AMDGPU::sub0, SrcSubRC);
5608 MachineOperand Op1H = TII->buildExtractSubRegOrImm(
5609 MI, MRI, MI.getOperand(1), SrcRC, AMDGPU::sub1, SrcSubRC);
5610
5611 BuildMI(BB, MI, DL, TII->get(AMDGPU::S_MUL_I32), DestSub0)
5612 .add(Op1L)
5613 .addReg(ParityRegister);
5614
5615 BuildMI(BB, MI, DL, TII->get(AMDGPU::S_MUL_I32), DestSub1)
5616 .add(Op1H)
5617 .addReg(ParityRegister);
5618
5619 BuildMI(BB, MI, DL, TII->get(TargetOpcode::REG_SEQUENCE), DstReg)
5620 .addReg(DestSub0)
5621 .addImm(AMDGPU::sub0)
5622 .addReg(DestSub1)
5623 .addImm(AMDGPU::sub1);
5624 }
5625 break;
5626 }
5627 case AMDGPU::S_SUB_I32: {
5628 Register NegatedVal = MRI.createVirtualRegister(DstRegClass);
5629
5630 // Take the negation of the source operand.
5631 BuildMI(BB, MI, DL, TII->get(AMDGPU::S_SUB_I32), NegatedVal)
5632 .addImm(0)
5633 .addReg(SrcReg);
5634 BuildMI(BB, MI, DL, TII->get(AMDGPU::S_MUL_I32), DstReg)
5635 .addReg(NegatedVal)
5636 .addReg(NewAccumulator->getOperand(0).getReg());
5637 break;
5638 }
5639 case AMDGPU::S_ADD_I32: {
5640 BuildMI(BB, MI, DL, TII->get(AMDGPU::S_MUL_I32), DstReg)
5641 .addReg(SrcReg)
5642 .addReg(NewAccumulator->getOperand(0).getReg());
5643 break;
5644 }
5645 case AMDGPU::S_ADD_U64_PSEUDO:
5646 case AMDGPU::S_SUB_U64_PSEUDO: {
5647 Register DestSub0 = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
5648 Register DestSub1 = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
5649 Register Op1H_Op0L_Reg =
5650 MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
5651 Register Op1L_Op0H_Reg =
5652 MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
5653 Register CarryReg = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
5654 Register AddReg = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
5655 Register NegatedValLo =
5656 MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
5657 Register NegatedValHi =
5658 MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
5659
5660 const TargetRegisterClass *Src1RC = MRI.getRegClass(SrcReg);
5661 const TargetRegisterClass *Src1SubRC =
5662 TRI->getSubRegisterClass(Src1RC, AMDGPU::sub0);
5663
5664 MachineOperand Op1L = TII->buildExtractSubRegOrImm(
5665 MI, MRI, MI.getOperand(1), Src1RC, AMDGPU::sub0, Src1SubRC);
5666 MachineOperand Op1H = TII->buildExtractSubRegOrImm(
5667 MI, MRI, MI.getOperand(1), Src1RC, AMDGPU::sub1, Src1SubRC);
5668
5669 if (Opc == AMDGPU::S_SUB_U64_PSEUDO) {
5670 BuildMI(BB, MI, DL, TII->get(AMDGPU::S_SUB_I32), NegatedValLo)
5671 .addImm(0)
5672 .addReg(NewAccumulator->getOperand(0).getReg())
5673 .setOperandDead(3); // Dead scc
5674 BuildMI(BB, MI, DL, TII->get(AMDGPU::S_ASHR_I32), NegatedValHi)
5675 .addReg(NegatedValLo)
5676 .addImm(31)
5677 .setOperandDead(3); // Dead scc
5678 BuildMI(BB, MI, DL, TII->get(AMDGPU::S_MUL_I32), Op1L_Op0H_Reg)
5679 .add(Op1L)
5680 .addReg(NegatedValHi);
5681 }
5682 Register LowOpcode = Opc == AMDGPU::S_SUB_U64_PSEUDO
5683 ? NegatedValLo
5684 : NewAccumulator->getOperand(0).getReg();
5685 BuildMI(BB, MI, DL, TII->get(AMDGPU::S_MUL_I32), DestSub0)
5686 .add(Op1L)
5687 .addReg(LowOpcode);
5688 BuildMI(BB, MI, DL, TII->get(AMDGPU::S_MUL_HI_U32), CarryReg)
5689 .add(Op1L)
5690 .addReg(LowOpcode);
5691 BuildMI(BB, MI, DL, TII->get(AMDGPU::S_MUL_I32), Op1H_Op0L_Reg)
5692 .add(Op1H)
5693 .addReg(LowOpcode);
5694
5695 Register HiVal = Opc == AMDGPU::S_SUB_U64_PSEUDO ? AddReg : DestSub1;
5696 BuildMI(BB, MI, DL, TII->get(AMDGPU::S_ADD_U32), HiVal)
5697 .addReg(CarryReg)
5698 .addReg(Op1H_Op0L_Reg)
5699 .setOperandDead(3); // Dead scc
5700
5701 if (Opc == AMDGPU::S_SUB_U64_PSEUDO) {
5702 BuildMI(BB, MI, DL, TII->get(AMDGPU::S_ADD_U32), DestSub1)
5703 .addReg(HiVal)
5704 .addReg(Op1L_Op0H_Reg)
5705 .setOperandDead(3); // Dead scc
5706 }
5707 BuildMI(BB, MI, DL, TII->get(TargetOpcode::REG_SEQUENCE), DstReg)
5708 .addReg(DestSub0)
5709 .addImm(AMDGPU::sub0)
5710 .addReg(DestSub1)
5711 .addImm(AMDGPU::sub1);
5712 break;
5713 }
5714 }
5715 RetBB = &BB;
5716 }
5717 }
5718 } else {
5719 // TODO: Implement DPP Strategy and switch based on immediate strategy
5720 // operand. For now, for all the cases (default, Iterative and DPP we use
5721 // iterative approach by default.)
5722
5723 // To reduce the VGPR using iterative approach, we need to iterate
5724 // over all the active lanes. Lowering consists of ComputeLoop,
5725 // which iterate over only active lanes. We use copy of EXEC register
5726 // as induction variable and every active lane modifies it using bitset0
5727 // so that we will get the next active lane for next iteration.
5729 Register SrcReg = MI.getOperand(1).getReg();
5730 bool is32BitOpc = is32bitWaveReduceOperation(Opc);
5731
5732 // Create Control flow for loop
5733 // Split MI's Machine Basic block into For loop
5734 auto [ComputeLoop, ComputeEnd] = splitBlockForLoop(MI, BB, true);
5735
5736 // Create virtual registers required for lowering.
5737 const TargetRegisterClass *WaveMaskRegClass = TRI->getWaveMaskRegClass();
5738 const TargetRegisterClass *DstRegClass = MRI.getRegClass(DstReg);
5739 Register LoopIterator = MRI.createVirtualRegister(WaveMaskRegClass);
5740 Register IdentityValReg = MRI.createVirtualRegister(DstRegClass);
5741 Register AccumulatorReg = MRI.createVirtualRegister(DstRegClass);
5742 Register ActiveBitsReg = MRI.createVirtualRegister(WaveMaskRegClass);
5743 Register NewActiveBitsReg = MRI.createVirtualRegister(WaveMaskRegClass);
5744 Register FF1Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
5745 Register LaneValueReg = MRI.createVirtualRegister(DstRegClass);
5746
5747 bool IsWave32 = ST.isWave32();
5748 unsigned MovOpcForExec = IsWave32 ? AMDGPU::S_MOV_B32 : AMDGPU::S_MOV_B64;
5749 unsigned ExecReg = IsWave32 ? AMDGPU::EXEC_LO : AMDGPU::EXEC;
5750
5751 // Create initial values of induction variable from Exec, Accumulator and
5752 // insert branch instr to newly created ComputeBlock
5753 BuildMI(BB, I, DL, TII->get(MovOpcForExec), LoopIterator).addReg(ExecReg);
5754 if (is32BitOpc) {
5756 BuildMI(BB, I, DL, TII->get(AMDGPU::S_MOV_B32), IdentityValReg)
5757 .addImm(IdentityValue);
5758 } else {
5760 BuildMI(BB, I, DL, TII->get(AMDGPU::S_MOV_B64_IMM_PSEUDO), IdentityValReg)
5761 .addImm(IdentityValue);
5762 }
5763 // clang-format off
5764 BuildMI(BB, I, DL, TII->get(AMDGPU::S_BRANCH))
5765 .addMBB(ComputeLoop);
5766 // clang-format on
5767
5768 // Start constructing ComputeLoop
5769 I = ComputeLoop->begin();
5770 auto Accumulator =
5771 BuildMI(*ComputeLoop, I, DL, TII->get(AMDGPU::PHI), AccumulatorReg)
5772 .addReg(IdentityValReg)
5773 .addMBB(&BB);
5774 auto ActiveBits =
5775 BuildMI(*ComputeLoop, I, DL, TII->get(AMDGPU::PHI), ActiveBitsReg)
5776 .addReg(LoopIterator)
5777 .addMBB(&BB);
5778
5779 I = ComputeLoop->end();
5780 MachineInstr *NewAccumulator;
5781 // Perform the computations
5782 unsigned SFFOpc = IsWave32 ? AMDGPU::S_FF1_I32_B32 : AMDGPU::S_FF1_I32_B64;
5783 BuildMI(*ComputeLoop, I, DL, TII->get(SFFOpc), FF1Reg)
5784 .addReg(ActiveBitsReg);
5785 if (is32BitOpc) {
5786 BuildMI(*ComputeLoop, I, DL, TII->get(AMDGPU::V_READLANE_B32),
5787 LaneValueReg)
5788 .addReg(SrcReg)
5789 .addReg(FF1Reg);
5790 NewAccumulator = BuildMI(*ComputeLoop, I, DL, TII->get(Opc), DstReg)
5791 .addReg(Accumulator->getOperand(0).getReg())
5792 .addReg(LaneValueReg);
5793 } else {
5794 Register LaneValueLoReg =
5795 MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
5796 Register LaneValueHiReg =
5797 MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
5798 Register LaneValReg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
5799 const TargetRegisterClass *SrcRC = MRI.getRegClass(SrcReg);
5800 const TargetRegisterClass *SrcSubRC =
5801 TRI->getSubRegisterClass(SrcRC, AMDGPU::sub0);
5802 MachineOperand Op1L = TII->buildExtractSubRegOrImm(
5803 MI, MRI, MI.getOperand(1), SrcRC, AMDGPU::sub0, SrcSubRC);
5804 MachineOperand Op1H = TII->buildExtractSubRegOrImm(
5805 MI, MRI, MI.getOperand(1), SrcRC, AMDGPU::sub1, SrcSubRC);
5806 // lane value input should be in an sgpr
5807 BuildMI(*ComputeLoop, I, DL, TII->get(AMDGPU::V_READLANE_B32),
5808 LaneValueLoReg)
5809 .add(Op1L)
5810 .addReg(FF1Reg);
5811 BuildMI(*ComputeLoop, I, DL, TII->get(AMDGPU::V_READLANE_B32),
5812 LaneValueHiReg)
5813 .add(Op1H)
5814 .addReg(FF1Reg);
5815 auto LaneValue = BuildMI(*ComputeLoop, I, DL,
5816 TII->get(TargetOpcode::REG_SEQUENCE), LaneValReg)
5817 .addReg(LaneValueLoReg)
5818 .addImm(AMDGPU::sub0)
5819 .addReg(LaneValueHiReg)
5820 .addImm(AMDGPU::sub1);
5821 switch (Opc) {
5822 case AMDGPU::S_OR_B64:
5823 case AMDGPU::S_AND_B64:
5824 case AMDGPU::S_XOR_B64: {
5825 NewAccumulator = BuildMI(*ComputeLoop, I, DL, TII->get(Opc), DstReg)
5826 .addReg(Accumulator->getOperand(0).getReg())
5827 .addReg(LaneValue->getOperand(0).getReg())
5828 .setOperandDead(3); // Dead scc
5829 break;
5830 }
5831 case AMDGPU::V_CMP_GT_I64_e64:
5832 case AMDGPU::V_CMP_GT_U64_e64:
5833 case AMDGPU::V_CMP_LT_I64_e64:
5834 case AMDGPU::V_CMP_LT_U64_e64: {
5835 Register LaneMaskReg = MRI.createVirtualRegister(WaveMaskRegClass);
5836 Register ComparisonResultReg =
5837 MRI.createVirtualRegister(WaveMaskRegClass);
5838 const TargetRegisterClass *VregClass = TRI->getVGPR64Class();
5839 const TargetRegisterClass *VSubRegClass =
5840 TRI->getSubRegisterClass(VregClass, AMDGPU::sub0);
5841 Register AccumulatorVReg = MRI.createVirtualRegister(VregClass);
5842 MachineOperand SrcReg0Sub0 =
5843 TII->buildExtractSubRegOrImm(MI, MRI, Accumulator->getOperand(0),
5844 VregClass, AMDGPU::sub0, VSubRegClass);
5845 MachineOperand SrcReg0Sub1 =
5846 TII->buildExtractSubRegOrImm(MI, MRI, Accumulator->getOperand(0),
5847 VregClass, AMDGPU::sub1, VSubRegClass);
5848 BuildMI(*ComputeLoop, I, DL, TII->get(TargetOpcode::REG_SEQUENCE),
5849 AccumulatorVReg)
5850 .add(SrcReg0Sub0)
5851 .addImm(AMDGPU::sub0)
5852 .add(SrcReg0Sub1)
5853 .addImm(AMDGPU::sub1);
5854 BuildMI(*ComputeLoop, I, DL, TII->get(Opc), LaneMaskReg)
5855 .addReg(LaneValue->getOperand(0).getReg())
5856 .addReg(AccumulatorVReg);
5857
5858 unsigned AndOpc = IsWave32 ? AMDGPU::S_AND_B32 : AMDGPU::S_AND_B64;
5859 BuildMI(*ComputeLoop, I, DL, TII->get(AndOpc), ComparisonResultReg)
5860 .addReg(LaneMaskReg)
5861 .addReg(ActiveBitsReg);
5862
5863 NewAccumulator = BuildMI(*ComputeLoop, I, DL,
5864 TII->get(AMDGPU::S_CSELECT_B64), DstReg)
5865 .addReg(LaneValue->getOperand(0).getReg())
5866 .addReg(Accumulator->getOperand(0).getReg());
5867 break;
5868 }
5869 case AMDGPU::S_ADD_U64_PSEUDO:
5870 case AMDGPU::S_SUB_U64_PSEUDO: {
5871 NewAccumulator = BuildMI(*ComputeLoop, I, DL, TII->get(Opc), DstReg)
5872 .addReg(Accumulator->getOperand(0).getReg())
5873 .addReg(LaneValue->getOperand(0).getReg());
5874 ComputeLoop = Expand64BitScalarArithmetic(*NewAccumulator, ComputeLoop);
5875 break;
5876 }
5877 }
5878 }
5879 // Manipulate the iterator to get the next active lane
5880 unsigned BITSETOpc =
5881 IsWave32 ? AMDGPU::S_BITSET0_B32 : AMDGPU::S_BITSET0_B64;
5882 BuildMI(*ComputeLoop, I, DL, TII->get(BITSETOpc), NewActiveBitsReg)
5883 .addReg(FF1Reg)
5884 .addReg(ActiveBitsReg);
5885
5886 // Add phi nodes
5887 Accumulator.addReg(DstReg).addMBB(ComputeLoop);
5888 ActiveBits.addReg(NewActiveBitsReg).addMBB(ComputeLoop);
5889
5890 // Creating branching
5891 unsigned CMPOpc = IsWave32 ? AMDGPU::S_CMP_LG_U32 : AMDGPU::S_CMP_LG_U64;
5892 BuildMI(*ComputeLoop, I, DL, TII->get(CMPOpc))
5893 .addReg(NewActiveBitsReg)
5894 .addImm(0);
5895 BuildMI(*ComputeLoop, I, DL, TII->get(AMDGPU::S_CBRANCH_SCC1))
5896 .addMBB(ComputeLoop);
5897
5898 RetBB = ComputeEnd;
5899 }
5900 MI.eraseFromParent();
5901 return RetBB;
5902}
5903
5906 MachineBasicBlock *BB) const {
5907 MachineFunction *MF = BB->getParent();
5909 const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>();
5911 const SIRegisterInfo *TRI = Subtarget->getRegisterInfo();
5913 const DebugLoc &DL = MI.getDebugLoc();
5914
5915 switch (MI.getOpcode()) {
5916 case AMDGPU::WAVE_REDUCE_UMIN_PSEUDO_U32:
5917 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::S_MIN_U32);
5918 case AMDGPU::WAVE_REDUCE_UMIN_PSEUDO_U64:
5919 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::V_CMP_LT_U64_e64);
5920 case AMDGPU::WAVE_REDUCE_MIN_PSEUDO_I32:
5921 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::S_MIN_I32);
5922 case AMDGPU::WAVE_REDUCE_MIN_PSEUDO_I64:
5923 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::V_CMP_LT_I64_e64);
5924 case AMDGPU::WAVE_REDUCE_UMAX_PSEUDO_U32:
5925 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::S_MAX_U32);
5926 case AMDGPU::WAVE_REDUCE_UMAX_PSEUDO_U64:
5927 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::V_CMP_GT_U64_e64);
5928 case AMDGPU::WAVE_REDUCE_MAX_PSEUDO_I32:
5929 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::S_MAX_I32);
5930 case AMDGPU::WAVE_REDUCE_MAX_PSEUDO_I64:
5931 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::V_CMP_GT_I64_e64);
5932 case AMDGPU::WAVE_REDUCE_ADD_PSEUDO_I32:
5933 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::S_ADD_I32);
5934 case AMDGPU::WAVE_REDUCE_ADD_PSEUDO_U64:
5935 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::S_ADD_U64_PSEUDO);
5936 case AMDGPU::WAVE_REDUCE_SUB_PSEUDO_I32:
5937 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::S_SUB_I32);
5938 case AMDGPU::WAVE_REDUCE_SUB_PSEUDO_U64:
5939 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::S_SUB_U64_PSEUDO);
5940 case AMDGPU::WAVE_REDUCE_AND_PSEUDO_B32:
5941 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::S_AND_B32);
5942 case AMDGPU::WAVE_REDUCE_AND_PSEUDO_B64:
5943 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::S_AND_B64);
5944 case AMDGPU::WAVE_REDUCE_OR_PSEUDO_B32:
5945 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::S_OR_B32);
5946 case AMDGPU::WAVE_REDUCE_OR_PSEUDO_B64:
5947 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::S_OR_B64);
5948 case AMDGPU::WAVE_REDUCE_XOR_PSEUDO_B32:
5949 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::S_XOR_B32);
5950 case AMDGPU::WAVE_REDUCE_XOR_PSEUDO_B64:
5951 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::S_XOR_B64);
5952 case AMDGPU::S_UADDO_PSEUDO:
5953 case AMDGPU::S_USUBO_PSEUDO: {
5954 MachineOperand &Dest0 = MI.getOperand(0);
5955 MachineOperand &Dest1 = MI.getOperand(1);
5956 MachineOperand &Src0 = MI.getOperand(2);
5957 MachineOperand &Src1 = MI.getOperand(3);
5958
5959 unsigned Opc = (MI.getOpcode() == AMDGPU::S_UADDO_PSEUDO)
5960 ? AMDGPU::S_ADD_U32
5961 : AMDGPU::S_SUB_U32;
5962 // clang-format off
5963 BuildMI(*BB, MI, DL, TII->get(Opc), Dest0.getReg())
5964 .add(Src0)
5965 .add(Src1);
5966 // clang-format on
5967
5968 unsigned SelOpc =
5969 Subtarget->isWave64() ? AMDGPU::S_CSELECT_B64 : AMDGPU::S_CSELECT_B32;
5970 BuildMI(*BB, MI, DL, TII->get(SelOpc), Dest1.getReg()).addImm(-1).addImm(0);
5971
5972 MI.eraseFromParent();
5973 return BB;
5974 }
5975 case AMDGPU::S_ADD_U64_PSEUDO:
5976 case AMDGPU::S_SUB_U64_PSEUDO: {
5977 return Expand64BitScalarArithmetic(MI, BB);
5978 }
5979 case AMDGPU::V_ADD_U64_PSEUDO:
5980 case AMDGPU::V_SUB_U64_PSEUDO: {
5981 bool IsAdd = (MI.getOpcode() == AMDGPU::V_ADD_U64_PSEUDO);
5982
5983 MachineOperand &Dest = MI.getOperand(0);
5984 MachineOperand &Src0 = MI.getOperand(1);
5985 MachineOperand &Src1 = MI.getOperand(2);
5986
5987 if (ST.hasAddSubU64Insts()) {
5988 auto I = BuildMI(*BB, MI, DL,
5989 TII->get(IsAdd ? AMDGPU::V_ADD_U64_e64
5990 : AMDGPU::V_SUB_U64_e64),
5991 Dest.getReg())
5992 .add(Src0)
5993 .add(Src1)
5994 .addImm(0); // clamp
5995 TII->legalizeOperands(*I);
5996 MI.eraseFromParent();
5997 return BB;
5998 }
5999
6000 if (IsAdd && ST.hasLshlAddU64Inst()) {
6001 auto Add = BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_LSHL_ADD_U64_e64),
6002 Dest.getReg())
6003 .add(Src0)
6004 .addImm(0)
6005 .add(Src1);
6006 TII->legalizeOperands(*Add);
6007 MI.eraseFromParent();
6008 return BB;
6009 }
6010
6011 const auto *CarryRC = TRI->getWaveMaskRegClass();
6012
6013 Register DestSub0 = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
6014 Register DestSub1 = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
6015
6016 Register CarryReg = MRI.createVirtualRegister(CarryRC);
6017 Register DeadCarryReg = MRI.createVirtualRegister(CarryRC);
6018
6019 const TargetRegisterClass *Src0RC = Src0.isReg()
6020 ? MRI.getRegClass(Src0.getReg())
6021 : &AMDGPU::VReg_64RegClass;
6022 const TargetRegisterClass *Src1RC = Src1.isReg()
6023 ? MRI.getRegClass(Src1.getReg())
6024 : &AMDGPU::VReg_64RegClass;
6025
6026 const TargetRegisterClass *Src0SubRC =
6027 TRI->getSubRegisterClass(Src0RC, AMDGPU::sub0);
6028 const TargetRegisterClass *Src1SubRC =
6029 TRI->getSubRegisterClass(Src1RC, AMDGPU::sub1);
6030
6031 MachineOperand SrcReg0Sub0 = TII->buildExtractSubRegOrImm(
6032 MI, MRI, Src0, Src0RC, AMDGPU::sub0, Src0SubRC);
6033 MachineOperand SrcReg1Sub0 = TII->buildExtractSubRegOrImm(
6034 MI, MRI, Src1, Src1RC, AMDGPU::sub0, Src1SubRC);
6035
6036 MachineOperand SrcReg0Sub1 = TII->buildExtractSubRegOrImm(
6037 MI, MRI, Src0, Src0RC, AMDGPU::sub1, Src0SubRC);
6038 MachineOperand SrcReg1Sub1 = TII->buildExtractSubRegOrImm(
6039 MI, MRI, Src1, Src1RC, AMDGPU::sub1, Src1SubRC);
6040
6041 unsigned LoOpc =
6042 IsAdd ? AMDGPU::V_ADD_CO_U32_e64 : AMDGPU::V_SUB_CO_U32_e64;
6043 MachineInstr *LoHalf = BuildMI(*BB, MI, DL, TII->get(LoOpc), DestSub0)
6044 .addReg(CarryReg, RegState::Define)
6045 .add(SrcReg0Sub0)
6046 .add(SrcReg1Sub0)
6047 .addImm(0); // clamp bit
6048
6049 unsigned HiOpc = IsAdd ? AMDGPU::V_ADDC_U32_e64 : AMDGPU::V_SUBB_U32_e64;
6050 MachineInstr *HiHalf =
6051 BuildMI(*BB, MI, DL, TII->get(HiOpc), DestSub1)
6052 .addReg(DeadCarryReg, RegState::Define | RegState::Dead)
6053 .add(SrcReg0Sub1)
6054 .add(SrcReg1Sub1)
6055 .addReg(CarryReg, RegState::Kill)
6056 .addImm(0); // clamp bit
6057
6058 BuildMI(*BB, MI, DL, TII->get(TargetOpcode::REG_SEQUENCE), Dest.getReg())
6059 .addReg(DestSub0)
6060 .addImm(AMDGPU::sub0)
6061 .addReg(DestSub1)
6062 .addImm(AMDGPU::sub1);
6063 TII->legalizeOperands(*LoHalf);
6064 TII->legalizeOperands(*HiHalf);
6065 MI.eraseFromParent();
6066 return BB;
6067 }
6068 case AMDGPU::S_ADD_CO_PSEUDO:
6069 case AMDGPU::S_SUB_CO_PSEUDO: {
6070 // This pseudo has a chance to be selected
6071 // only from uniform add/subcarry node. All the VGPR operands
6072 // therefore assumed to be splat vectors.
6074 MachineOperand &Dest = MI.getOperand(0);
6075 MachineOperand &CarryDest = MI.getOperand(1);
6076 MachineOperand &Src0 = MI.getOperand(2);
6077 MachineOperand &Src1 = MI.getOperand(3);
6078 MachineOperand &Src2 = MI.getOperand(4);
6079 if (Src0.isReg() && TRI->isVectorRegister(MRI, Src0.getReg())) {
6080 Register RegOp0 = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
6081 BuildMI(*BB, MII, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32), RegOp0)
6082 .addReg(Src0.getReg());
6083 Src0.setReg(RegOp0);
6084 }
6085 if (Src1.isReg() && TRI->isVectorRegister(MRI, Src1.getReg())) {
6086 Register RegOp1 = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
6087 BuildMI(*BB, MII, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32), RegOp1)
6088 .addReg(Src1.getReg());
6089 Src1.setReg(RegOp1);
6090 }
6091 Register RegOp2 = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
6092 if (TRI->isVectorRegister(MRI, Src2.getReg())) {
6093 BuildMI(*BB, MII, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32), RegOp2)
6094 .addReg(Src2.getReg());
6095 Src2.setReg(RegOp2);
6096 }
6097
6098 if (ST.isWave64()) {
6099 if (ST.hasScalarCompareEq64()) {
6100 BuildMI(*BB, MII, DL, TII->get(AMDGPU::S_CMP_LG_U64))
6101 .addReg(Src2.getReg())
6102 .addImm(0);
6103 } else {
6104 const TargetRegisterClass *Src2RC = MRI.getRegClass(Src2.getReg());
6105 const TargetRegisterClass *SubRC =
6106 TRI->getSubRegisterClass(Src2RC, AMDGPU::sub0);
6107 MachineOperand Src2Sub0 = TII->buildExtractSubRegOrImm(
6108 MII, MRI, Src2, Src2RC, AMDGPU::sub0, SubRC);
6109 MachineOperand Src2Sub1 = TII->buildExtractSubRegOrImm(
6110 MII, MRI, Src2, Src2RC, AMDGPU::sub1, SubRC);
6111 Register Src2_32 = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
6112
6113 BuildMI(*BB, MII, DL, TII->get(AMDGPU::S_OR_B32), Src2_32)
6114 .add(Src2Sub0)
6115 .add(Src2Sub1);
6116
6117 BuildMI(*BB, MII, DL, TII->get(AMDGPU::S_CMP_LG_U32))
6118 .addReg(Src2_32, RegState::Kill)
6119 .addImm(0);
6120 }
6121 } else {
6122 BuildMI(*BB, MII, DL, TII->get(AMDGPU::S_CMP_LG_U32))
6123 .addReg(Src2.getReg())
6124 .addImm(0);
6125 }
6126
6127 unsigned Opc = MI.getOpcode() == AMDGPU::S_ADD_CO_PSEUDO
6128 ? AMDGPU::S_ADDC_U32
6129 : AMDGPU::S_SUBB_U32;
6130
6131 BuildMI(*BB, MII, DL, TII->get(Opc), Dest.getReg()).add(Src0).add(Src1);
6132
6133 unsigned SelOpc =
6134 ST.isWave64() ? AMDGPU::S_CSELECT_B64 : AMDGPU::S_CSELECT_B32;
6135
6136 BuildMI(*BB, MII, DL, TII->get(SelOpc), CarryDest.getReg())
6137 .addImm(-1)
6138 .addImm(0);
6139
6140 MI.eraseFromParent();
6141 return BB;
6142 }
6143 case AMDGPU::SI_INIT_M0: {
6144 MachineOperand &M0Init = MI.getOperand(0);
6145 BuildMI(*BB, MI.getIterator(), MI.getDebugLoc(),
6146 TII->get(M0Init.isReg() ? AMDGPU::COPY : AMDGPU::S_MOV_B32),
6147 AMDGPU::M0)
6148 .add(M0Init);
6149 MI.eraseFromParent();
6150 return BB;
6151 }
6152 case AMDGPU::S_BARRIER_SIGNAL_ISFIRST_IMM: {
6153 // Set SCC to true, in case the barrier instruction gets converted to a NOP.
6154 BuildMI(*BB, MI.getIterator(), MI.getDebugLoc(),
6155 TII->get(AMDGPU::S_CMP_EQ_U32))
6156 .addImm(0)
6157 .addImm(0);
6158 return BB;
6159 }
6160 case AMDGPU::GET_GROUPSTATICSIZE: {
6161 assert(getTargetMachine().getTargetTriple().getOS() == Triple::AMDHSA ||
6162 getTargetMachine().getTargetTriple().getOS() == Triple::AMDPAL);
6163 BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_MOV_B32))
6164 .add(MI.getOperand(0))
6165 .addImm(MFI->getLDSSize());
6166 MI.eraseFromParent();
6167 return BB;
6168 }
6169 case AMDGPU::GET_SHADERCYCLESHILO: {
6171 // The algorithm is:
6172 //
6173 // hi1 = getreg(SHADER_CYCLES_HI)
6174 // lo1 = getreg(SHADER_CYCLES_LO)
6175 // hi2 = getreg(SHADER_CYCLES_HI)
6176 //
6177 // If hi1 == hi2 then there was no overflow and the result is hi2:lo1.
6178 // Otherwise there was overflow and the result is hi2:0. In both cases the
6179 // result should represent the actual time at some point during the sequence
6180 // of three getregs.
6181 using namespace AMDGPU::Hwreg;
6182 Register RegHi1 = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
6183 BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_GETREG_B32), RegHi1)
6184 .addImm(HwregEncoding::encode(ID_SHADER_CYCLES_HI, 0, 32));
6185 Register RegLo1 = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
6186 BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_GETREG_B32), RegLo1)
6187 .addImm(HwregEncoding::encode(ID_SHADER_CYCLES, 0, 32));
6188 Register RegHi2 = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
6189 BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_GETREG_B32), RegHi2)
6190 .addImm(HwregEncoding::encode(ID_SHADER_CYCLES_HI, 0, 32));
6191 BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_CMP_EQ_U32))
6192 .addReg(RegHi1)
6193 .addReg(RegHi2);
6194 Register RegLo = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
6195 BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_CSELECT_B32), RegLo)
6196 .addReg(RegLo1)
6197 .addImm(0);
6198 BuildMI(*BB, MI, DL, TII->get(AMDGPU::REG_SEQUENCE))
6199 .add(MI.getOperand(0))
6200 .addReg(RegLo)
6201 .addImm(AMDGPU::sub0)
6202 .addReg(RegHi2)
6203 .addImm(AMDGPU::sub1);
6204 MI.eraseFromParent();
6205 return BB;
6206 }
6207 case AMDGPU::SI_INDIRECT_SRC_V1:
6208 case AMDGPU::SI_INDIRECT_SRC_V2:
6209 case AMDGPU::SI_INDIRECT_SRC_V4:
6210 case AMDGPU::SI_INDIRECT_SRC_V8:
6211 case AMDGPU::SI_INDIRECT_SRC_V9:
6212 case AMDGPU::SI_INDIRECT_SRC_V10:
6213 case AMDGPU::SI_INDIRECT_SRC_V11:
6214 case AMDGPU::SI_INDIRECT_SRC_V12:
6215 case AMDGPU::SI_INDIRECT_SRC_V16:
6216 case AMDGPU::SI_INDIRECT_SRC_V32:
6217 return emitIndirectSrc(MI, *BB, *getSubtarget());
6218 case AMDGPU::SI_INDIRECT_DST_V1:
6219 case AMDGPU::SI_INDIRECT_DST_V2:
6220 case AMDGPU::SI_INDIRECT_DST_V4:
6221 case AMDGPU::SI_INDIRECT_DST_V8:
6222 case AMDGPU::SI_INDIRECT_DST_V9:
6223 case AMDGPU::SI_INDIRECT_DST_V10:
6224 case AMDGPU::SI_INDIRECT_DST_V11:
6225 case AMDGPU::SI_INDIRECT_DST_V12:
6226 case AMDGPU::SI_INDIRECT_DST_V16:
6227 case AMDGPU::SI_INDIRECT_DST_V32:
6228 return emitIndirectDst(MI, *BB, *getSubtarget());
6229 case AMDGPU::SI_KILL_F32_COND_IMM_PSEUDO:
6230 case AMDGPU::SI_KILL_I1_PSEUDO:
6231 return splitKillBlock(MI, BB);
6232 case AMDGPU::V_CNDMASK_B64_PSEUDO: {
6233 Register Dst = MI.getOperand(0).getReg();
6234 const MachineOperand &Src0 = MI.getOperand(1);
6235 const MachineOperand &Src1 = MI.getOperand(2);
6236 Register SrcCond = MI.getOperand(3).getReg();
6237
6238 Register DstLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
6239 Register DstHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
6240 const auto *CondRC = TRI->getWaveMaskRegClass();
6241 Register SrcCondCopy = MRI.createVirtualRegister(CondRC);
6242
6243 const TargetRegisterClass *Src0RC = Src0.isReg()
6244 ? MRI.getRegClass(Src0.getReg())
6245 : &AMDGPU::VReg_64RegClass;
6246 const TargetRegisterClass *Src1RC = Src1.isReg()
6247 ? MRI.getRegClass(Src1.getReg())
6248 : &AMDGPU::VReg_64RegClass;
6249
6250 const TargetRegisterClass *Src0SubRC =
6251 TRI->getSubRegisterClass(Src0RC, AMDGPU::sub0);
6252 const TargetRegisterClass *Src1SubRC =
6253 TRI->getSubRegisterClass(Src1RC, AMDGPU::sub1);
6254
6255 MachineOperand Src0Sub0 = TII->buildExtractSubRegOrImm(
6256 MI, MRI, Src0, Src0RC, AMDGPU::sub0, Src0SubRC);
6257 MachineOperand Src1Sub0 = TII->buildExtractSubRegOrImm(
6258 MI, MRI, Src1, Src1RC, AMDGPU::sub0, Src1SubRC);
6259
6260 MachineOperand Src0Sub1 = TII->buildExtractSubRegOrImm(
6261 MI, MRI, Src0, Src0RC, AMDGPU::sub1, Src0SubRC);
6262 MachineOperand Src1Sub1 = TII->buildExtractSubRegOrImm(
6263 MI, MRI, Src1, Src1RC, AMDGPU::sub1, Src1SubRC);
6264
6265 BuildMI(*BB, MI, DL, TII->get(AMDGPU::COPY), SrcCondCopy).addReg(SrcCond);
6266 BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstLo)
6267 .addImm(0)
6268 .add(Src0Sub0)
6269 .addImm(0)
6270 .add(Src1Sub0)
6271 .addReg(SrcCondCopy);
6272 BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstHi)
6273 .addImm(0)
6274 .add(Src0Sub1)
6275 .addImm(0)
6276 .add(Src1Sub1)
6277 .addReg(SrcCondCopy);
6278
6279 BuildMI(*BB, MI, DL, TII->get(AMDGPU::REG_SEQUENCE), Dst)
6280 .addReg(DstLo)
6281 .addImm(AMDGPU::sub0)
6282 .addReg(DstHi)
6283 .addImm(AMDGPU::sub1);
6284 MI.eraseFromParent();
6285 return BB;
6286 }
6287 case AMDGPU::SI_BR_UNDEF: {
6288 MachineInstr *Br = BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_CBRANCH_SCC1))
6289 .add(MI.getOperand(0));
6290 Br->getOperand(1).setIsUndef(); // read undef SCC
6291 MI.eraseFromParent();
6292 return BB;
6293 }
6294 case AMDGPU::ADJCALLSTACKUP:
6295 case AMDGPU::ADJCALLSTACKDOWN: {
6297 MachineInstrBuilder MIB(*MF, &MI);
6298 MIB.addReg(Info->getStackPtrOffsetReg(), RegState::ImplicitDefine)
6299 .addReg(Info->getStackPtrOffsetReg(), RegState::Implicit);
6300 return BB;
6301 }
6302 case AMDGPU::SI_CALL_ISEL: {
6303 unsigned ReturnAddrReg = TII->getRegisterInfo().getReturnAddressReg(*MF);
6304
6306 MIB = BuildMI(*BB, MI, DL, TII->get(AMDGPU::SI_CALL), ReturnAddrReg);
6307
6308 for (const MachineOperand &MO : MI.operands())
6309 MIB.add(MO);
6310
6311 MIB.cloneMemRefs(MI);
6312 MI.eraseFromParent();
6313 return BB;
6314 }
6315 case AMDGPU::V_ADD_CO_U32_e32:
6316 case AMDGPU::V_SUB_CO_U32_e32:
6317 case AMDGPU::V_SUBREV_CO_U32_e32: {
6318 // TODO: Define distinct V_*_I32_Pseudo instructions instead.
6319 unsigned Opc = MI.getOpcode();
6320
6321 bool NeedClampOperand = false;
6322 if (TII->pseudoToMCOpcode(Opc) == -1) {
6324 NeedClampOperand = true;
6325 }
6326
6327 auto I = BuildMI(*BB, MI, DL, TII->get(Opc), MI.getOperand(0).getReg());
6328 if (TII->isVOP3(*I)) {
6329 I.addReg(TRI->getVCC(), RegState::Define);
6330 }
6331 I.add(MI.getOperand(1)).add(MI.getOperand(2));
6332 if (NeedClampOperand)
6333 I.addImm(0); // clamp bit for e64 encoding
6334
6335 TII->legalizeOperands(*I);
6336
6337 MI.eraseFromParent();
6338 return BB;
6339 }
6340 case AMDGPU::V_ADDC_U32_e32:
6341 case AMDGPU::V_SUBB_U32_e32:
6342 case AMDGPU::V_SUBBREV_U32_e32:
6343 // These instructions have an implicit use of vcc which counts towards the
6344 // constant bus limit.
6345 TII->legalizeOperands(MI);
6346 return BB;
6347 case AMDGPU::DS_GWS_INIT:
6348 case AMDGPU::DS_GWS_SEMA_BR:
6349 case AMDGPU::DS_GWS_BARRIER:
6350 TII->enforceOperandRCAlignment(MI, AMDGPU::OpName::data0);
6351 [[fallthrough]];
6352 case AMDGPU::DS_GWS_SEMA_V:
6353 case AMDGPU::DS_GWS_SEMA_P:
6354 case AMDGPU::DS_GWS_SEMA_RELEASE_ALL:
6355 // A s_waitcnt 0 is required to be the instruction immediately following.
6356 if (getSubtarget()->hasGWSAutoReplay()) {
6358 return BB;
6359 }
6360
6361 return emitGWSMemViolTestLoop(MI, BB);
6362 case AMDGPU::S_SETREG_B32: {
6363 // Try to optimize cases that only set the denormal mode or rounding mode.
6364 //
6365 // If the s_setreg_b32 fully sets all of the bits in the rounding mode or
6366 // denormal mode to a constant, we can use s_round_mode or s_denorm_mode
6367 // instead.
6368 //
6369 // FIXME: This could be predicates on the immediate, but tablegen doesn't
6370 // allow you to have a no side effect instruction in the output of a
6371 // sideeffecting pattern.
6372 auto [ID, Offset, Width] =
6373 AMDGPU::Hwreg::HwregEncoding::decode(MI.getOperand(1).getImm());
6375 return BB;
6376
6377 const unsigned WidthMask = maskTrailingOnes<unsigned>(Width);
6378 const unsigned SetMask = WidthMask << Offset;
6379
6380 if (getSubtarget()->hasDenormModeInst()) {
6381 unsigned SetDenormOp = 0;
6382 unsigned SetRoundOp = 0;
6383
6384 // The dedicated instructions can only set the whole denorm or round mode
6385 // at once, not a subset of bits in either.
6386 if (SetMask ==
6388 // If this fully sets both the round and denorm mode, emit the two
6389 // dedicated instructions for these.
6390 SetRoundOp = AMDGPU::S_ROUND_MODE;
6391 SetDenormOp = AMDGPU::S_DENORM_MODE;
6392 } else if (SetMask == AMDGPU::Hwreg::FP_ROUND_MASK) {
6393 SetRoundOp = AMDGPU::S_ROUND_MODE;
6394 } else if (SetMask == AMDGPU::Hwreg::FP_DENORM_MASK) {
6395 SetDenormOp = AMDGPU::S_DENORM_MODE;
6396 }
6397
6398 if (SetRoundOp || SetDenormOp) {
6399 MachineInstr *Def = MRI.getVRegDef(MI.getOperand(0).getReg());
6400 if (Def && Def->isMoveImmediate() && Def->getOperand(1).isImm()) {
6401 unsigned ImmVal = Def->getOperand(1).getImm();
6402 if (SetRoundOp) {
6403 BuildMI(*BB, MI, MI.getDebugLoc(), TII->get(SetRoundOp))
6404 .addImm(ImmVal & 0xf);
6405
6406 // If we also have the denorm mode, get just the denorm mode bits.
6407 ImmVal >>= 4;
6408 }
6409
6410 if (SetDenormOp) {
6411 BuildMI(*BB, MI, MI.getDebugLoc(), TII->get(SetDenormOp))
6412 .addImm(ImmVal & 0xf);
6413 }
6414
6415 MI.eraseFromParent();
6416 return BB;
6417 }
6418 }
6419 }
6420
6421 // If only FP bits are touched, used the no side effects pseudo.
6422 if ((SetMask & (AMDGPU::Hwreg::FP_ROUND_MASK |
6423 AMDGPU::Hwreg::FP_DENORM_MASK)) == SetMask)
6424 MI.setDesc(TII->get(AMDGPU::S_SETREG_B32_mode));
6425
6426 return BB;
6427 }
6428 case AMDGPU::S_INVERSE_BALLOT_U32:
6429 case AMDGPU::S_INVERSE_BALLOT_U64:
6430 // These opcodes only exist to let SIFixSGPRCopies insert a readfirstlane if
6431 // necessary. After that they are equivalent to a COPY.
6432 MI.setDesc(TII->get(AMDGPU::COPY));
6433 return BB;
6434 case AMDGPU::ENDPGM_TRAP: {
6435 if (BB->succ_empty() && std::next(MI.getIterator()) == BB->end()) {
6436 MI.setDesc(TII->get(AMDGPU::S_ENDPGM));
6437 MI.addOperand(MachineOperand::CreateImm(0));
6438 return BB;
6439 }
6440
6441 // We need a block split to make the real endpgm a terminator. We also don't
6442 // want to break phis in successor blocks, so we can't just delete to the
6443 // end of the block.
6444
6445 MachineBasicBlock *SplitBB = BB->splitAt(MI, false /*UpdateLiveIns*/);
6447 MF->push_back(TrapBB);
6448 // clang-format off
6449 BuildMI(*TrapBB, TrapBB->end(), DL, TII->get(AMDGPU::S_ENDPGM))
6450 .addImm(0);
6451 BuildMI(*BB, &MI, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
6452 .addMBB(TrapBB);
6453 // clang-format on
6454
6455 BB->addSuccessor(TrapBB);
6456 MI.eraseFromParent();
6457 return SplitBB;
6458 }
6459 case AMDGPU::SIMULATED_TRAP: {
6460 assert(Subtarget->hasPrivEnabledTrap2NopBug());
6461 MachineBasicBlock *SplitBB =
6462 TII->insertSimulatedTrap(MRI, *BB, MI, MI.getDebugLoc());
6463 MI.eraseFromParent();
6464 return SplitBB;
6465 }
6466 case AMDGPU::SI_TCRETURN_GFX_WholeWave:
6467 case AMDGPU::SI_WHOLE_WAVE_FUNC_RETURN: {
6469
6470 // During ISel, it's difficult to propagate the original EXEC mask to use as
6471 // an input to SI_WHOLE_WAVE_FUNC_RETURN. Set it up here instead.
6472 MachineInstr *Setup = TII->getWholeWaveFunctionSetup(*BB->getParent());
6473 assert(Setup && "Couldn't find SI_SETUP_WHOLE_WAVE_FUNC");
6474 Register OriginalExec = Setup->getOperand(0).getReg();
6475 MF->getRegInfo().clearKillFlags(OriginalExec);
6476 MI.getOperand(0).setReg(OriginalExec);
6477 return BB;
6478 }
6479 default:
6480 if (TII->isImage(MI) || TII->isMUBUF(MI)) {
6481 if (!MI.mayStore())
6483 return BB;
6484 }
6486 }
6487}
6488
6490 // This currently forces unfolding various combinations of fsub into fma with
6491 // free fneg'd operands. As long as we have fast FMA (controlled by
6492 // isFMAFasterThanFMulAndFAdd), we should perform these.
6493
6494 // When fma is quarter rate, for f64 where add / sub are at best half rate,
6495 // most of these combines appear to be cycle neutral but save on instruction
6496 // count / code size.
6497 return true;
6498}
6499
6501
6503 EVT VT) const {
6504 if (!VT.isVector()) {
6505 return MVT::i1;
6506 }
6507 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements());
6508}
6509
6511 // TODO: Should i16 be used always if legal? For now it would force VALU
6512 // shifts.
6513 return (VT == MVT::i16) ? MVT::i16 : MVT::i32;
6514}
6515
6517 return (Ty.getScalarSizeInBits() <= 16 && Subtarget->has16BitInsts())
6518 ? Ty.changeElementSize(16)
6519 : Ty.changeElementSize(32);
6520}
6521
6522// Answering this is somewhat tricky and depends on the specific device which
6523// have different rates for fma or all f64 operations.
6524//
6525// v_fma_f64 and v_mul_f64 always take the same number of cycles as each other
6526// regardless of which device (although the number of cycles differs between
6527// devices), so it is always profitable for f64.
6528//
6529// v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable
6530// only on full rate devices. Normally, we should prefer selecting v_mad_f32
6531// which we can always do even without fused FP ops since it returns the same
6532// result as the separate operations and since it is always full
6533// rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32
6534// however does not support denormals, so we do report fma as faster if we have
6535// a fast fma device and require denormals.
6536//
6538 EVT VT) const {
6539 VT = VT.getScalarType();
6540
6541 switch (VT.getSimpleVT().SimpleTy) {
6542 case MVT::f32: {
6543 // If mad is not available this depends only on if f32 fma is full rate.
6544 if (!Subtarget->hasMadMacF32Insts())
6545 return Subtarget->hasFastFMAF32();
6546
6547 // Otherwise f32 mad is always full rate and returns the same result as
6548 // the separate operations so should be preferred over fma.
6549 // However does not support denormals.
6551 return Subtarget->hasFastFMAF32() || Subtarget->hasDLInsts();
6552
6553 // If the subtarget has v_fmac_f32, that's just as good as v_mac_f32.
6554 return Subtarget->hasFastFMAF32() && Subtarget->hasDLInsts();
6555 }
6556 case MVT::f64:
6557 return true;
6558 case MVT::f16:
6559 case MVT::bf16:
6560 return Subtarget->has16BitInsts() && !denormalModeIsFlushAllF64F16(MF);
6561 default:
6562 break;
6563 }
6564
6565 return false;
6566}
6567
6569 LLT Ty) const {
6570 switch (Ty.getScalarSizeInBits()) {
6571 case 16:
6572 return isFMAFasterThanFMulAndFAdd(MF, MVT::f16);
6573 case 32:
6574 return isFMAFasterThanFMulAndFAdd(MF, MVT::f32);
6575 case 64:
6576 return isFMAFasterThanFMulAndFAdd(MF, MVT::f64);
6577 default:
6578 break;
6579 }
6580
6581 return false;
6582}
6583
6585 if (!Ty.isScalar())
6586 return false;
6587
6588 if (Ty.getScalarSizeInBits() == 16)
6589 return Subtarget->hasMadF16() && denormalModeIsFlushAllF64F16(*MI.getMF());
6590 if (Ty.getScalarSizeInBits() == 32)
6591 return Subtarget->hasMadMacF32Insts() &&
6592 denormalModeIsFlushAllF32(*MI.getMF());
6593
6594 return false;
6595}
6596
6598 const SDNode *N) const {
6599 // TODO: Check future ftz flag
6600 // v_mad_f32/v_mac_f32 do not support denormals.
6601 EVT VT = N->getValueType(0);
6602 if (VT == MVT::f32)
6603 return Subtarget->hasMadMacF32Insts() &&
6605 if (VT == MVT::f16) {
6606 return Subtarget->hasMadF16() &&
6608 }
6609
6610 return false;
6611}
6612
6613//===----------------------------------------------------------------------===//
6614// Custom DAG Lowering Operations
6615//===----------------------------------------------------------------------===//
6616
6617// Work around LegalizeDAG doing the wrong thing and fully scalarizing if the
6618// wider vector type is legal.
6620 SelectionDAG &DAG) const {
6621 unsigned Opc = Op.getOpcode();
6622 EVT VT = Op.getValueType();
6623 assert(VT == MVT::v4i16 || VT == MVT::v4f16 || VT == MVT::v4bf16 ||
6624 VT == MVT::v4f32 || VT == MVT::v8i16 || VT == MVT::v8f16 ||
6625 VT == MVT::v8bf16 || VT == MVT::v16i16 || VT == MVT::v16f16 ||
6626 VT == MVT::v16bf16 || VT == MVT::v8f32 || VT == MVT::v16f32 ||
6627 VT == MVT::v32f32 || VT == MVT::v32i16 || VT == MVT::v32f16 ||
6628 VT == MVT::v32bf16);
6629
6630 auto [Lo, Hi] = DAG.SplitVectorOperand(Op.getNode(), 0);
6631
6632 SDLoc SL(Op);
6633 SDValue OpLo = DAG.getNode(Opc, SL, Lo.getValueType(), Lo, Op->getFlags());
6634 SDValue OpHi = DAG.getNode(Opc, SL, Hi.getValueType(), Hi, Op->getFlags());
6635
6636 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(Op), VT, OpLo, OpHi);
6637}
6638
6639// Enable lowering of ROTR for vxi32 types. This is a workaround for a
6640// regression whereby extra unnecessary instructions were added to codegen
6641// for rotr operations, casued by legalising v2i32 or. This resulted in extra
6642// instructions to extract the result from the vector.
6644 [[maybe_unused]] EVT VT = Op.getValueType();
6645
6646 assert((VT == MVT::v2i32 || VT == MVT::v4i32 || VT == MVT::v8i32 ||
6647 VT == MVT::v16i32) &&
6648 "Unexpected ValueType.");
6649
6650 return DAG.UnrollVectorOp(Op.getNode());
6651}
6652
6653// Work around LegalizeDAG doing the wrong thing and fully scalarizing if the
6654// wider vector type is legal.
6656 SelectionDAG &DAG) const {
6657 unsigned Opc = Op.getOpcode();
6658 EVT VT = Op.getValueType();
6659 assert(VT == MVT::v4i16 || VT == MVT::v4f16 || VT == MVT::v4bf16 ||
6660 VT == MVT::v4f32 || VT == MVT::v8i16 || VT == MVT::v8f16 ||
6661 VT == MVT::v8bf16 || VT == MVT::v16i16 || VT == MVT::v16f16 ||
6662 VT == MVT::v16bf16 || VT == MVT::v8f32 || VT == MVT::v16f32 ||
6663 VT == MVT::v32f32 || VT == MVT::v32i16 || VT == MVT::v32f16 ||
6664 VT == MVT::v32bf16);
6665
6666 auto [Lo0, Hi0] = DAG.SplitVectorOperand(Op.getNode(), 0);
6667 auto [Lo1, Hi1] = DAG.SplitVectorOperand(Op.getNode(), 1);
6668
6669 SDLoc SL(Op);
6670
6671 SDValue OpLo =
6672 DAG.getNode(Opc, SL, Lo0.getValueType(), Lo0, Lo1, Op->getFlags());
6673 SDValue OpHi =
6674 DAG.getNode(Opc, SL, Hi0.getValueType(), Hi0, Hi1, Op->getFlags());
6675
6676 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(Op), VT, OpLo, OpHi);
6677}
6678
6680 SelectionDAG &DAG) const {
6681 unsigned Opc = Op.getOpcode();
6682 EVT VT = Op.getValueType();
6683 assert(VT == MVT::v4i16 || VT == MVT::v4f16 || VT == MVT::v8i16 ||
6684 VT == MVT::v8f16 || VT == MVT::v4f32 || VT == MVT::v16i16 ||
6685 VT == MVT::v16f16 || VT == MVT::v8f32 || VT == MVT::v16f32 ||
6686 VT == MVT::v32f32 || VT == MVT::v32f16 || VT == MVT::v32i16 ||
6687 VT == MVT::v4bf16 || VT == MVT::v8bf16 || VT == MVT::v16bf16 ||
6688 VT == MVT::v32bf16);
6689
6690 SDValue Op0 = Op.getOperand(0);
6691 auto [Lo0, Hi0] = Op0.getValueType().isVector()
6692 ? DAG.SplitVectorOperand(Op.getNode(), 0)
6693 : std::pair(Op0, Op0);
6694
6695 auto [Lo1, Hi1] = DAG.SplitVectorOperand(Op.getNode(), 1);
6696 auto [Lo2, Hi2] = DAG.SplitVectorOperand(Op.getNode(), 2);
6697
6698 SDLoc SL(Op);
6699 auto ResVT = DAG.GetSplitDestVTs(VT);
6700
6701 SDValue OpLo =
6702 DAG.getNode(Opc, SL, ResVT.first, Lo0, Lo1, Lo2, Op->getFlags());
6703 SDValue OpHi =
6704 DAG.getNode(Opc, SL, ResVT.second, Hi0, Hi1, Hi2, Op->getFlags());
6705
6706 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(Op), VT, OpLo, OpHi);
6707}
6708
6710 switch (Op.getOpcode()) {
6711 default:
6713 case ISD::BRCOND:
6714 return LowerBRCOND(Op, DAG);
6715 case ISD::RETURNADDR:
6716 return LowerRETURNADDR(Op, DAG);
6717 case ISD::LOAD: {
6718 SDValue Result = LowerLOAD(Op, DAG);
6719 assert((!Result.getNode() || Result.getNode()->getNumValues() == 2) &&
6720 "Load should return a value and a chain");
6721 return Result;
6722 }
6723 case ISD::FSQRT: {
6724 EVT VT = Op.getValueType();
6725 if (VT == MVT::f32)
6726 return lowerFSQRTF32(Op, DAG);
6727 if (VT == MVT::f64)
6728 return lowerFSQRTF64(Op, DAG);
6729 return SDValue();
6730 }
6731 case ISD::FSIN:
6732 case ISD::FCOS:
6733 return LowerTrig(Op, DAG);
6734 case ISD::SELECT:
6735 return LowerSELECT(Op, DAG);
6736 case ISD::FDIV:
6737 return LowerFDIV(Op, DAG);
6738 case ISD::FFREXP:
6739 return LowerFFREXP(Op, DAG);
6740 case ISD::ATOMIC_CMP_SWAP:
6741 return LowerATOMIC_CMP_SWAP(Op, DAG);
6742 case ISD::STORE:
6743 return LowerSTORE(Op, DAG);
6744 case ISD::GlobalAddress: {
6747 return LowerGlobalAddress(MFI, Op, DAG);
6748 }
6750 return LowerINTRINSIC_WO_CHAIN(Op, DAG);
6752 return LowerINTRINSIC_W_CHAIN(Op, DAG);
6754 return LowerINTRINSIC_VOID(Op, DAG);
6755 case ISD::ADDRSPACECAST:
6756 return lowerADDRSPACECAST(Op, DAG);
6758 return lowerINSERT_SUBVECTOR(Op, DAG);
6760 return lowerINSERT_VECTOR_ELT(Op, DAG);
6762 return lowerEXTRACT_VECTOR_ELT(Op, DAG);
6764 return lowerVECTOR_SHUFFLE(Op, DAG);
6766 return lowerSCALAR_TO_VECTOR(Op, DAG);
6767 case ISD::BUILD_VECTOR:
6768 return lowerBUILD_VECTOR(Op, DAG);
6769 case ISD::FP_ROUND:
6771 return lowerFP_ROUND(Op, DAG);
6772 case ISD::TRAP:
6773 return lowerTRAP(Op, DAG);
6774 case ISD::DEBUGTRAP:
6775 return lowerDEBUGTRAP(Op, DAG);
6776 case ISD::ABS:
6777 case ISD::FABS:
6778 case ISD::FNEG:
6779 case ISD::FCANONICALIZE:
6780 case ISD::BSWAP:
6781 return splitUnaryVectorOp(Op, DAG);
6782 case ISD::FMINNUM:
6783 case ISD::FMAXNUM:
6784 return lowerFMINNUM_FMAXNUM(Op, DAG);
6785 case ISD::FMINIMUMNUM:
6786 case ISD::FMAXIMUMNUM:
6787 return lowerFMINIMUMNUM_FMAXIMUMNUM(Op, DAG);
6788 case ISD::FMINIMUM:
6789 case ISD::FMAXIMUM:
6790 return lowerFMINIMUM_FMAXIMUM(Op, DAG);
6791 case ISD::FLDEXP:
6792 case ISD::STRICT_FLDEXP:
6793 return lowerFLDEXP(Op, DAG);
6794 case ISD::FMA:
6795 return splitTernaryVectorOp(Op, DAG);
6796 case ISD::FP_TO_SINT:
6797 case ISD::FP_TO_UINT:
6798 return LowerFP_TO_INT(Op, DAG);
6799 case ISD::SHL:
6800 case ISD::SRA:
6801 case ISD::SRL:
6802 case ISD::ADD:
6803 case ISD::SUB:
6804 case ISD::SMIN:
6805 case ISD::SMAX:
6806 case ISD::UMIN:
6807 case ISD::UMAX:
6808 case ISD::FADD:
6809 case ISD::FMUL:
6810 case ISD::FMINNUM_IEEE:
6811 case ISD::FMAXNUM_IEEE:
6812 case ISD::UADDSAT:
6813 case ISD::USUBSAT:
6814 case ISD::SADDSAT:
6815 case ISD::SSUBSAT:
6816 return splitBinaryVectorOp(Op, DAG);
6817 case ISD::FCOPYSIGN:
6818 return lowerFCOPYSIGN(Op, DAG);
6819 case ISD::MUL:
6820 return lowerMUL(Op, DAG);
6821 case ISD::SMULO:
6822 case ISD::UMULO:
6823 return lowerXMULO(Op, DAG);
6824 case ISD::SMUL_LOHI:
6825 case ISD::UMUL_LOHI:
6826 return lowerXMUL_LOHI(Op, DAG);
6827 case ISD::DYNAMIC_STACKALLOC:
6828 return LowerDYNAMIC_STACKALLOC(Op, DAG);
6829 case ISD::STACKSAVE:
6830 return LowerSTACKSAVE(Op, DAG);
6831 case ISD::GET_ROUNDING:
6832 return lowerGET_ROUNDING(Op, DAG);
6833 case ISD::SET_ROUNDING:
6834 return lowerSET_ROUNDING(Op, DAG);
6835 case ISD::PREFETCH:
6836 return lowerPREFETCH(Op, DAG);
6837 case ISD::FP_EXTEND:
6839 return lowerFP_EXTEND(Op, DAG);
6840 case ISD::GET_FPENV:
6841 return lowerGET_FPENV(Op, DAG);
6842 case ISD::SET_FPENV:
6843 return lowerSET_FPENV(Op, DAG);
6844 case ISD::ROTR:
6845 return lowerROTR(Op, DAG);
6846 }
6847 return SDValue();
6848}
6849
6850// Used for D16: Casts the result of an instruction into the right vector,
6851// packs values if loads return unpacked values.
6853 const SDLoc &DL, SelectionDAG &DAG,
6854 bool Unpacked) {
6855 if (!LoadVT.isVector())
6856 return Result;
6857
6858 // Cast back to the original packed type or to a larger type that is a
6859 // multiple of 32 bit for D16. Widening the return type is a required for
6860 // legalization.
6861 EVT FittingLoadVT = LoadVT;
6862 if ((LoadVT.getVectorNumElements() % 2) == 1) {
6863 FittingLoadVT =
6865 LoadVT.getVectorNumElements() + 1);
6866 }
6867
6868 if (Unpacked) { // From v2i32/v4i32 back to v2f16/v4f16.
6869 // Truncate to v2i16/v4i16.
6870 EVT IntLoadVT = FittingLoadVT.changeTypeToInteger();
6871
6872 // Workaround legalizer not scalarizing truncate after vector op
6873 // legalization but not creating intermediate vector trunc.
6875 DAG.ExtractVectorElements(Result, Elts);
6876 for (SDValue &Elt : Elts)
6877 Elt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i16, Elt);
6878
6879 // Pad illegal v1i16/v3fi6 to v4i16
6880 if ((LoadVT.getVectorNumElements() % 2) == 1)
6881 Elts.push_back(DAG.getPOISON(MVT::i16));
6882
6883 Result = DAG.getBuildVector(IntLoadVT, DL, Elts);
6884
6885 // Bitcast to original type (v2f16/v4f16).
6886 return DAG.getNode(ISD::BITCAST, DL, FittingLoadVT, Result);
6887 }
6888
6889 // Cast back to the original packed type.
6890 return DAG.getNode(ISD::BITCAST, DL, FittingLoadVT, Result);
6891}
6892
6893SDValue SITargetLowering::adjustLoadValueType(unsigned Opcode, MemSDNode *M,
6894 SelectionDAG &DAG,
6896 bool IsIntrinsic) const {
6897 SDLoc DL(M);
6898
6899 bool Unpacked = Subtarget->hasUnpackedD16VMem();
6900 EVT LoadVT = M->getValueType(0);
6901
6902 EVT EquivLoadVT = LoadVT;
6903 if (LoadVT.isVector()) {
6904 if (Unpacked) {
6905 EquivLoadVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32,
6906 LoadVT.getVectorNumElements());
6907 } else if ((LoadVT.getVectorNumElements() % 2) == 1) {
6908 // Widen v3f16 to legal type
6909 EquivLoadVT =
6911 LoadVT.getVectorNumElements() + 1);
6912 }
6913 }
6914
6915 // Change from v4f16/v2f16 to EquivLoadVT.
6916 SDVTList VTList = DAG.getVTList(EquivLoadVT, MVT::Other);
6917
6919 IsIntrinsic ? (unsigned)ISD::INTRINSIC_W_CHAIN : Opcode, DL, VTList, Ops,
6920 M->getMemoryVT(), M->getMemOperand());
6921
6922 SDValue Adjusted = adjustLoadValueTypeImpl(Load, LoadVT, DL, DAG, Unpacked);
6923
6924 return DAG.getMergeValues({Adjusted, Load.getValue(1)}, DL);
6925}
6926
6927SDValue SITargetLowering::lowerIntrinsicLoad(MemSDNode *M, bool IsFormat,
6928 SelectionDAG &DAG,
6929 ArrayRef<SDValue> Ops) const {
6930 SDLoc DL(M);
6931 EVT LoadVT = M->getValueType(0);
6932 EVT EltType = LoadVT.getScalarType();
6933 EVT IntVT = LoadVT.changeTypeToInteger();
6934
6935 bool IsD16 = IsFormat && (EltType.getSizeInBits() == 16);
6936
6937 assert(M->getNumValues() == 2 || M->getNumValues() == 3);
6938 bool IsTFE = M->getNumValues() == 3;
6939
6940 unsigned Opc = IsFormat ? (IsTFE ? AMDGPUISD::BUFFER_LOAD_FORMAT_TFE
6942 : IsTFE ? AMDGPUISD::BUFFER_LOAD_TFE
6943 : AMDGPUISD::BUFFER_LOAD;
6944
6945 if (IsD16) {
6946 return adjustLoadValueType(AMDGPUISD::BUFFER_LOAD_FORMAT_D16, M, DAG, Ops);
6947 }
6948
6949 // Handle BUFFER_LOAD_BYTE/UBYTE/SHORT/USHORT overloaded intrinsics
6950 if (!IsD16 && !LoadVT.isVector() && EltType.getSizeInBits() < 32)
6951 return handleByteShortBufferLoads(DAG, LoadVT, DL, Ops, M->getMemOperand(),
6952 IsTFE);
6953
6954 if (isTypeLegal(LoadVT)) {
6955 return getMemIntrinsicNode(Opc, DL, M->getVTList(), Ops, IntVT,
6956 M->getMemOperand(), DAG);
6957 }
6958
6959 EVT CastVT = getEquivalentMemType(*DAG.getContext(), LoadVT);
6960 SDVTList VTList = DAG.getVTList(CastVT, MVT::Other);
6961 SDValue MemNode = getMemIntrinsicNode(Opc, DL, VTList, Ops, CastVT,
6962 M->getMemOperand(), DAG);
6963 return DAG.getMergeValues(
6964 {DAG.getNode(ISD::BITCAST, DL, LoadVT, MemNode), MemNode.getValue(1)},
6965 DL);
6966}
6967
6969 SelectionDAG &DAG) {
6970 EVT VT = N->getValueType(0);
6971 unsigned CondCode = N->getConstantOperandVal(3);
6972 if (!ICmpInst::isIntPredicate(static_cast<ICmpInst::Predicate>(CondCode)))
6973 return DAG.getPOISON(VT);
6974
6975 ICmpInst::Predicate IcInput = static_cast<ICmpInst::Predicate>(CondCode);
6976
6977 SDValue LHS = N->getOperand(1);
6978 SDValue RHS = N->getOperand(2);
6979
6980 SDLoc DL(N);
6981
6982 EVT CmpVT = LHS.getValueType();
6983 if (CmpVT == MVT::i16 && !TLI.isTypeLegal(MVT::i16)) {
6984 unsigned PromoteOp =
6986 LHS = DAG.getNode(PromoteOp, DL, MVT::i32, LHS);
6987 RHS = DAG.getNode(PromoteOp, DL, MVT::i32, RHS);
6988 }
6989
6990 ISD::CondCode CCOpcode = getICmpCondCode(IcInput);
6991
6992 unsigned WavefrontSize = TLI.getSubtarget()->getWavefrontSize();
6993 EVT CCVT = EVT::getIntegerVT(*DAG.getContext(), WavefrontSize);
6994
6995 SDValue SetCC = DAG.getNode(AMDGPUISD::SETCC, DL, CCVT, LHS, RHS,
6996 DAG.getCondCode(CCOpcode));
6997 if (VT.bitsEq(CCVT))
6998 return SetCC;
6999 return DAG.getZExtOrTrunc(SetCC, DL, VT);
7000}
7001
7003 SelectionDAG &DAG) {
7004 EVT VT = N->getValueType(0);
7005
7006 unsigned CondCode = N->getConstantOperandVal(3);
7007 if (!FCmpInst::isFPPredicate(static_cast<FCmpInst::Predicate>(CondCode)))
7008 return DAG.getPOISON(VT);
7009
7010 SDValue Src0 = N->getOperand(1);
7011 SDValue Src1 = N->getOperand(2);
7012 EVT CmpVT = Src0.getValueType();
7013 SDLoc SL(N);
7014
7015 if (CmpVT == MVT::f16 && !TLI.isTypeLegal(CmpVT)) {
7016 Src0 = DAG.getNode(ISD::FP_EXTEND, SL, MVT::f32, Src0);
7017 Src1 = DAG.getNode(ISD::FP_EXTEND, SL, MVT::f32, Src1);
7018 }
7019
7020 FCmpInst::Predicate IcInput = static_cast<FCmpInst::Predicate>(CondCode);
7021 ISD::CondCode CCOpcode = getFCmpCondCode(IcInput);
7022 unsigned WavefrontSize = TLI.getSubtarget()->getWavefrontSize();
7023 EVT CCVT = EVT::getIntegerVT(*DAG.getContext(), WavefrontSize);
7024 SDValue SetCC = DAG.getNode(AMDGPUISD::SETCC, SL, CCVT, Src0, Src1,
7025 DAG.getCondCode(CCOpcode));
7026 if (VT.bitsEq(CCVT))
7027 return SetCC;
7028 return DAG.getZExtOrTrunc(SetCC, SL, VT);
7029}
7030
7032 SelectionDAG &DAG) {
7033 EVT VT = N->getValueType(0);
7034 SDValue Src = N->getOperand(1);
7035 SDLoc SL(N);
7036
7037 if (Src.getOpcode() == ISD::SETCC) {
7038 // (ballot (ISD::SETCC ...)) -> (AMDGPUISD::SETCC ...)
7039 return DAG.getNode(AMDGPUISD::SETCC, SL, VT, Src.getOperand(0),
7040 Src.getOperand(1), Src.getOperand(2));
7041 }
7042 if (const ConstantSDNode *Arg = dyn_cast<ConstantSDNode>(Src)) {
7043 // (ballot 0) -> 0
7044 if (Arg->isZero())
7045 return DAG.getConstant(0, SL, VT);
7046
7047 // (ballot 1) -> EXEC/EXEC_LO
7048 if (Arg->isOne()) {
7049 Register Exec;
7050 if (VT.getScalarSizeInBits() == 32)
7051 Exec = AMDGPU::EXEC_LO;
7052 else if (VT.getScalarSizeInBits() == 64)
7053 Exec = AMDGPU::EXEC;
7054 else
7055 return SDValue();
7056
7057 return DAG.getCopyFromReg(DAG.getEntryNode(), SL, Exec, VT);
7058 }
7059 }
7060
7061 // (ballot (i1 $src)) -> (AMDGPUISD::SETCC (i32 (zext $src)) (i32 0)
7062 // ISD::SETNE)
7063 return DAG.getNode(
7064 AMDGPUISD::SETCC, SL, VT, DAG.getZExtOrTrunc(Src, SL, MVT::i32),
7065 DAG.getConstant(0, SL, MVT::i32), DAG.getCondCode(ISD::SETNE));
7066}
7067
7069 SelectionDAG &DAG) {
7070 EVT VT = N->getValueType(0);
7071 unsigned ValSize = VT.getSizeInBits();
7072 unsigned IID = N->getConstantOperandVal(0);
7073 bool IsPermLane16 = IID == Intrinsic::amdgcn_permlane16 ||
7074 IID == Intrinsic::amdgcn_permlanex16;
7075 bool IsSetInactive = IID == Intrinsic::amdgcn_set_inactive ||
7076 IID == Intrinsic::amdgcn_set_inactive_chain_arg;
7077 SDLoc SL(N);
7078 MVT IntVT = MVT::getIntegerVT(ValSize);
7079 const GCNSubtarget *ST = TLI.getSubtarget();
7080 unsigned SplitSize = 32;
7081 if (IID == Intrinsic::amdgcn_update_dpp && (ValSize % 64 == 0) &&
7082 ST->hasDPALU_DPP() &&
7083 AMDGPU::isLegalDPALU_DPPControl(*ST, N->getConstantOperandVal(3)))
7084 SplitSize = 64;
7085
7086 auto createLaneOp = [&DAG, &SL, N, IID](SDValue Src0, SDValue Src1,
7087 SDValue Src2, MVT ValT) -> SDValue {
7088 SmallVector<SDValue, 8> Operands;
7089 switch (IID) {
7090 case Intrinsic::amdgcn_permlane16:
7091 case Intrinsic::amdgcn_permlanex16:
7092 case Intrinsic::amdgcn_update_dpp:
7093 Operands.push_back(N->getOperand(6));
7094 Operands.push_back(N->getOperand(5));
7095 Operands.push_back(N->getOperand(4));
7096 [[fallthrough]];
7097 case Intrinsic::amdgcn_writelane:
7098 Operands.push_back(Src2);
7099 [[fallthrough]];
7100 case Intrinsic::amdgcn_readlane:
7101 case Intrinsic::amdgcn_set_inactive:
7102 case Intrinsic::amdgcn_set_inactive_chain_arg:
7103 case Intrinsic::amdgcn_mov_dpp8:
7104 Operands.push_back(Src1);
7105 [[fallthrough]];
7106 case Intrinsic::amdgcn_readfirstlane:
7107 case Intrinsic::amdgcn_permlane64:
7108 Operands.push_back(Src0);
7109 break;
7110 default:
7111 llvm_unreachable("unhandled lane op");
7112 }
7113
7114 Operands.push_back(DAG.getTargetConstant(IID, SL, MVT::i32));
7115 std::reverse(Operands.begin(), Operands.end());
7116
7117 if (SDNode *GL = N->getGluedNode()) {
7118 assert(GL->getOpcode() == ISD::CONVERGENCECTRL_GLUE);
7119 GL = GL->getOperand(0).getNode();
7120 Operands.push_back(DAG.getNode(ISD::CONVERGENCECTRL_GLUE, SL, MVT::Glue,
7121 SDValue(GL, 0)));
7122 }
7123
7124 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SL, ValT, Operands);
7125 };
7126
7127 SDValue Src0 = N->getOperand(1);
7128 SDValue Src1, Src2;
7129 if (IID == Intrinsic::amdgcn_readlane || IID == Intrinsic::amdgcn_writelane ||
7130 IID == Intrinsic::amdgcn_mov_dpp8 ||
7131 IID == Intrinsic::amdgcn_update_dpp || IsSetInactive || IsPermLane16) {
7132 Src1 = N->getOperand(2);
7133 if (IID == Intrinsic::amdgcn_writelane ||
7134 IID == Intrinsic::amdgcn_update_dpp || IsPermLane16)
7135 Src2 = N->getOperand(3);
7136 }
7137
7138 if (ValSize == SplitSize) {
7139 // Already legal
7140 return SDValue();
7141 }
7142
7143 if (ValSize < 32) {
7144 bool IsFloat = VT.isFloatingPoint();
7145 Src0 = DAG.getAnyExtOrTrunc(IsFloat ? DAG.getBitcast(IntVT, Src0) : Src0,
7146 SL, MVT::i32);
7147
7148 if (IID == Intrinsic::amdgcn_update_dpp || IsSetInactive || IsPermLane16) {
7149 Src1 = DAG.getAnyExtOrTrunc(IsFloat ? DAG.getBitcast(IntVT, Src1) : Src1,
7150 SL, MVT::i32);
7151 }
7152
7153 if (IID == Intrinsic::amdgcn_writelane) {
7154 Src2 = DAG.getAnyExtOrTrunc(IsFloat ? DAG.getBitcast(IntVT, Src2) : Src2,
7155 SL, MVT::i32);
7156 }
7157
7158 SDValue LaneOp = createLaneOp(Src0, Src1, Src2, MVT::i32);
7159 SDValue Trunc = DAG.getAnyExtOrTrunc(LaneOp, SL, IntVT);
7160 return IsFloat ? DAG.getBitcast(VT, Trunc) : Trunc;
7161 }
7162
7163 if (ValSize % SplitSize != 0)
7164 return SDValue();
7165
7166 auto unrollLaneOp = [&DAG, &SL](SDNode *N) -> SDValue {
7167 EVT VT = N->getValueType(0);
7168 unsigned NE = VT.getVectorNumElements();
7169 EVT EltVT = VT.getVectorElementType();
7171 unsigned NumOperands = N->getNumOperands();
7172 SmallVector<SDValue, 4> Operands(NumOperands);
7173 SDNode *GL = N->getGluedNode();
7174
7175 // only handle convergencectrl_glue
7176 assert(!GL || GL->getOpcode() == ISD::CONVERGENCECTRL_GLUE);
7177
7178 for (unsigned i = 0; i != NE; ++i) {
7179 for (unsigned j = 0, e = GL ? NumOperands - 1 : NumOperands; j != e;
7180 ++j) {
7181 SDValue Operand = N->getOperand(j);
7182 EVT OperandVT = Operand.getValueType();
7183 if (OperandVT.isVector()) {
7184 // A vector operand; extract a single element.
7185 EVT OperandEltVT = OperandVT.getVectorElementType();
7186 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, OperandEltVT,
7187 Operand, DAG.getVectorIdxConstant(i, SL));
7188 } else {
7189 // A scalar operand; just use it as is.
7190 Operands[j] = Operand;
7191 }
7192 }
7193
7194 if (GL)
7195 Operands[NumOperands - 1] =
7196 DAG.getNode(ISD::CONVERGENCECTRL_GLUE, SL, MVT::Glue,
7197 SDValue(GL->getOperand(0).getNode(), 0));
7198
7199 Scalars.push_back(DAG.getNode(N->getOpcode(), SL, EltVT, Operands));
7200 }
7201
7202 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NE);
7203 return DAG.getBuildVector(VecVT, SL, Scalars);
7204 };
7205
7206 if (VT.isVector()) {
7207 switch (MVT::SimpleValueType EltTy =
7209 case MVT::i32:
7210 case MVT::f32:
7211 if (SplitSize == 32) {
7212 SDValue LaneOp = createLaneOp(Src0, Src1, Src2, VT.getSimpleVT());
7213 return unrollLaneOp(LaneOp.getNode());
7214 }
7215 [[fallthrough]];
7216 case MVT::i16:
7217 case MVT::f16:
7218 case MVT::bf16: {
7219 unsigned SubVecNumElt =
7220 SplitSize / VT.getVectorElementType().getSizeInBits();
7221 MVT SubVecVT = MVT::getVectorVT(EltTy, SubVecNumElt);
7223 SDValue Src0SubVec, Src1SubVec, Src2SubVec;
7224 for (unsigned i = 0, EltIdx = 0; i < ValSize / SplitSize; i++) {
7225 Src0SubVec = DAG.getNode(ISD::EXTRACT_SUBVECTOR, SL, SubVecVT, Src0,
7226 DAG.getConstant(EltIdx, SL, MVT::i32));
7227
7228 if (IID == Intrinsic::amdgcn_update_dpp || IsSetInactive ||
7229 IsPermLane16)
7230 Src1SubVec = DAG.getNode(ISD::EXTRACT_SUBVECTOR, SL, SubVecVT, Src1,
7231 DAG.getConstant(EltIdx, SL, MVT::i32));
7232
7233 if (IID == Intrinsic::amdgcn_writelane)
7234 Src2SubVec = DAG.getNode(ISD::EXTRACT_SUBVECTOR, SL, SubVecVT, Src2,
7235 DAG.getConstant(EltIdx, SL, MVT::i32));
7236
7237 Pieces.push_back(
7238 IID == Intrinsic::amdgcn_update_dpp || IsSetInactive || IsPermLane16
7239 ? createLaneOp(Src0SubVec, Src1SubVec, Src2, SubVecVT)
7240 : createLaneOp(Src0SubVec, Src1, Src2SubVec, SubVecVT));
7241 EltIdx += SubVecNumElt;
7242 }
7243 return DAG.getNode(ISD::CONCAT_VECTORS, SL, VT, Pieces);
7244 }
7245 default:
7246 // Handle all other cases by bitcasting to i32 vectors
7247 break;
7248 }
7249 }
7250
7251 MVT VecVT =
7252 MVT::getVectorVT(MVT::getIntegerVT(SplitSize), ValSize / SplitSize);
7253 Src0 = DAG.getBitcast(VecVT, Src0);
7254
7255 if (IID == Intrinsic::amdgcn_update_dpp || IsSetInactive || IsPermLane16)
7256 Src1 = DAG.getBitcast(VecVT, Src1);
7257
7258 if (IID == Intrinsic::amdgcn_writelane)
7259 Src2 = DAG.getBitcast(VecVT, Src2);
7260
7261 SDValue LaneOp = createLaneOp(Src0, Src1, Src2, VecVT);
7262 SDValue UnrolledLaneOp = unrollLaneOp(LaneOp.getNode());
7263 return DAG.getBitcast(VT, UnrolledLaneOp);
7264}
7265
7268 SelectionDAG &DAG) const {
7269 switch (N->getOpcode()) {
7271 if (SDValue Res = lowerINSERT_VECTOR_ELT(SDValue(N, 0), DAG))
7272 Results.push_back(Res);
7273 return;
7274 }
7276 if (SDValue Res = lowerEXTRACT_VECTOR_ELT(SDValue(N, 0), DAG))
7277 Results.push_back(Res);
7278 return;
7279 }
7281 unsigned IID = N->getConstantOperandVal(0);
7282 switch (IID) {
7283 case Intrinsic::amdgcn_make_buffer_rsrc:
7284 Results.push_back(lowerPointerAsRsrcIntrin(N, DAG));
7285 return;
7286 case Intrinsic::amdgcn_cvt_pkrtz: {
7287 SDValue Src0 = N->getOperand(1);
7288 SDValue Src1 = N->getOperand(2);
7289 SDLoc SL(N);
7290 SDValue Cvt =
7291 DAG.getNode(AMDGPUISD::CVT_PKRTZ_F16_F32, SL, MVT::i32, Src0, Src1);
7292 Results.push_back(DAG.getNode(ISD::BITCAST, SL, MVT::v2f16, Cvt));
7293 return;
7294 }
7295 case Intrinsic::amdgcn_cvt_pknorm_i16:
7296 case Intrinsic::amdgcn_cvt_pknorm_u16:
7297 case Intrinsic::amdgcn_cvt_pk_i16:
7298 case Intrinsic::amdgcn_cvt_pk_u16: {
7299 SDValue Src0 = N->getOperand(1);
7300 SDValue Src1 = N->getOperand(2);
7301 SDLoc SL(N);
7302 unsigned Opcode;
7303
7304 if (IID == Intrinsic::amdgcn_cvt_pknorm_i16)
7306 else if (IID == Intrinsic::amdgcn_cvt_pknorm_u16)
7308 else if (IID == Intrinsic::amdgcn_cvt_pk_i16)
7310 else
7312
7313 EVT VT = N->getValueType(0);
7314 if (isTypeLegal(VT))
7315 Results.push_back(DAG.getNode(Opcode, SL, VT, Src0, Src1));
7316 else {
7317 SDValue Cvt = DAG.getNode(Opcode, SL, MVT::i32, Src0, Src1);
7318 Results.push_back(DAG.getNode(ISD::BITCAST, SL, MVT::v2i16, Cvt));
7319 }
7320 return;
7321 }
7322 case Intrinsic::amdgcn_s_buffer_load: {
7323 // Lower llvm.amdgcn.s.buffer.load.(i8, u8) intrinsics. First, we generate
7324 // s_buffer_load_u8 for signed and unsigned load instructions. Next, DAG
7325 // combiner tries to merge the s_buffer_load_u8 with a sext instruction
7326 // (performSignExtendInRegCombine()) and it replaces s_buffer_load_u8 with
7327 // s_buffer_load_i8.
7328 if (!Subtarget->hasScalarSubwordLoads())
7329 return;
7330 SDValue Op = SDValue(N, 0);
7331 SDValue Rsrc = Op.getOperand(1);
7332 SDValue Offset = Op.getOperand(2);
7333 SDValue CachePolicy = Op.getOperand(3);
7334 EVT VT = Op.getValueType();
7335 assert(VT == MVT::i8 && "Expected 8-bit s_buffer_load intrinsics.\n");
7336 SDLoc DL(Op);
7338 const DataLayout &DataLayout = DAG.getDataLayout();
7339 Align Alignment =
7345 VT.getStoreSize(), Alignment);
7346 SDValue LoadVal;
7347 if (!Offset->isDivergent()) {
7348 SDValue Ops[] = {Rsrc, // source register
7349 Offset, CachePolicy};
7350 SDValue BufferLoad =
7352 DAG.getVTList(MVT::i32), Ops, VT, MMO);
7353 LoadVal = DAG.getNode(ISD::TRUNCATE, DL, VT, BufferLoad);
7354 } else {
7355 SDValue Ops[] = {
7356 DAG.getEntryNode(), // Chain
7357 Rsrc, // rsrc
7358 DAG.getConstant(0, DL, MVT::i32), // vindex
7359 {}, // voffset
7360 {}, // soffset
7361 {}, // offset
7362 CachePolicy, // cachepolicy
7363 DAG.getTargetConstant(0, DL, MVT::i1), // idxen
7364 };
7365 setBufferOffsets(Offset, DAG, &Ops[3], Align(4));
7366 LoadVal = handleByteShortBufferLoads(DAG, VT, DL, Ops, MMO);
7367 }
7368 Results.push_back(LoadVal);
7369 return;
7370 }
7371 case Intrinsic::amdgcn_dead: {
7372 for (unsigned I = 0, E = N->getNumValues(); I < E; ++I)
7373 Results.push_back(DAG.getPOISON(N->getValueType(I)));
7374 return;
7375 }
7376 }
7377 break;
7378 }
7380 if (SDValue Res = LowerINTRINSIC_W_CHAIN(SDValue(N, 0), DAG)) {
7381 if (Res.getOpcode() == ISD::MERGE_VALUES) {
7382 // FIXME: Hacky
7383 for (unsigned I = 0; I < Res.getNumOperands(); I++) {
7384 Results.push_back(Res.getOperand(I));
7385 }
7386 } else {
7387 Results.push_back(Res);
7388 Results.push_back(Res.getValue(1));
7389 }
7390 return;
7391 }
7392
7393 break;
7394 }
7395 case ISD::SELECT: {
7396 SDLoc SL(N);
7397 EVT VT = N->getValueType(0);
7398 EVT NewVT = getEquivalentMemType(*DAG.getContext(), VT);
7399 SDValue LHS = DAG.getNode(ISD::BITCAST, SL, NewVT, N->getOperand(1));
7400 SDValue RHS = DAG.getNode(ISD::BITCAST, SL, NewVT, N->getOperand(2));
7401
7402 EVT SelectVT = NewVT;
7403 if (NewVT.bitsLT(MVT::i32)) {
7404 LHS = DAG.getNode(ISD::ANY_EXTEND, SL, MVT::i32, LHS);
7405 RHS = DAG.getNode(ISD::ANY_EXTEND, SL, MVT::i32, RHS);
7406 SelectVT = MVT::i32;
7407 }
7408
7409 SDValue NewSelect =
7410 DAG.getNode(ISD::SELECT, SL, SelectVT, N->getOperand(0), LHS, RHS);
7411
7412 if (NewVT != SelectVT)
7413 NewSelect = DAG.getNode(ISD::TRUNCATE, SL, NewVT, NewSelect);
7414 Results.push_back(DAG.getNode(ISD::BITCAST, SL, VT, NewSelect));
7415 return;
7416 }
7417 case ISD::FNEG: {
7418 if (N->getValueType(0) != MVT::v2f16)
7419 break;
7420
7421 SDLoc SL(N);
7422 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::i32, N->getOperand(0));
7423
7424 SDValue Op = DAG.getNode(ISD::XOR, SL, MVT::i32, BC,
7425 DAG.getConstant(0x80008000, SL, MVT::i32));
7426 Results.push_back(DAG.getNode(ISD::BITCAST, SL, MVT::v2f16, Op));
7427 return;
7428 }
7429 case ISD::FABS: {
7430 if (N->getValueType(0) != MVT::v2f16)
7431 break;
7432
7433 SDLoc SL(N);
7434 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::i32, N->getOperand(0));
7435
7436 SDValue Op = DAG.getNode(ISD::AND, SL, MVT::i32, BC,
7437 DAG.getConstant(0x7fff7fff, SL, MVT::i32));
7438 Results.push_back(DAG.getNode(ISD::BITCAST, SL, MVT::v2f16, Op));
7439 return;
7440 }
7441 case ISD::FSQRT: {
7442 if (N->getValueType(0) != MVT::f16)
7443 break;
7444 Results.push_back(lowerFSQRTF16(SDValue(N, 0), DAG));
7445 break;
7446 }
7447 default:
7449 break;
7450 }
7451}
7452
7453/// Helper function for LowerBRCOND
7454static SDNode *findUser(SDValue Value, unsigned Opcode) {
7455
7456 for (SDUse &U : Value->uses()) {
7457 if (U.get() != Value)
7458 continue;
7459
7460 if (U.getUser()->getOpcode() == Opcode)
7461 return U.getUser();
7462 }
7463 return nullptr;
7464}
7465
7466unsigned SITargetLowering::isCFIntrinsic(const SDNode *Intr) const {
7467 if (Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN) {
7468 switch (Intr->getConstantOperandVal(1)) {
7469 case Intrinsic::amdgcn_if:
7470 return AMDGPUISD::IF;
7471 case Intrinsic::amdgcn_else:
7472 return AMDGPUISD::ELSE;
7473 case Intrinsic::amdgcn_loop:
7474 return AMDGPUISD::LOOP;
7475 case Intrinsic::amdgcn_end_cf:
7476 llvm_unreachable("should not occur");
7477 default:
7478 return 0;
7479 }
7480 }
7481
7482 // break, if_break, else_break are all only used as inputs to loop, not
7483 // directly as branch conditions.
7484 return 0;
7485}
7486
7493
7495 if (Subtarget->isAmdPalOS() || Subtarget->isMesa3DOS())
7496 return false;
7497
7498 // FIXME: Either avoid relying on address space here or change the default
7499 // address space for functions to avoid the explicit check.
7500 return (GV->getValueType()->isFunctionTy() ||
7503}
7504
7506 return !shouldEmitFixup(GV) && !shouldEmitGOTReloc(GV);
7507}
7508
7510 if (!GV->hasExternalLinkage())
7511 return true;
7512
7513 const auto OS = getTargetMachine().getTargetTriple().getOS();
7514 return OS == Triple::AMDHSA || OS == Triple::AMDPAL;
7515}
7516
7517/// This transforms the control flow intrinsics to get the branch destination as
7518/// last parameter, also switches branch target with BR if the need arise
7519SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND, SelectionDAG &DAG) const {
7520 SDLoc DL(BRCOND);
7521
7522 SDNode *Intr = BRCOND.getOperand(1).getNode();
7523 SDValue Target = BRCOND.getOperand(2);
7524 SDNode *BR = nullptr;
7525 SDNode *SetCC = nullptr;
7526
7527 switch (Intr->getOpcode()) {
7528 case ISD::SETCC: {
7529 // As long as we negate the condition everything is fine
7530 SetCC = Intr;
7531 Intr = SetCC->getOperand(0).getNode();
7532 break;
7533 }
7534 case ISD::XOR: {
7535 // Similar to SETCC, if we have (xor c, -1), we will be fine.
7536 SDValue LHS = Intr->getOperand(0);
7537 SDValue RHS = Intr->getOperand(1);
7538 if (auto *C = dyn_cast<ConstantSDNode>(RHS); C && C->getZExtValue()) {
7539 Intr = LHS.getNode();
7540 break;
7541 }
7542 [[fallthrough]];
7543 }
7544 default: {
7545 // Get the target from BR if we don't negate the condition
7546 BR = findUser(BRCOND, ISD::BR);
7547 assert(BR && "brcond missing unconditional branch user");
7548 Target = BR->getOperand(1);
7549 }
7550 }
7551
7552 unsigned CFNode = isCFIntrinsic(Intr);
7553 if (CFNode == 0) {
7554 // This is a uniform branch so we don't need to legalize.
7555 return BRCOND;
7556 }
7557
7558 bool HaveChain = Intr->getOpcode() == ISD::INTRINSIC_VOID ||
7560
7561 assert(!SetCC ||
7562 (SetCC->getConstantOperandVal(1) == 1 &&
7563 cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
7564 ISD::SETNE));
7565
7566 // operands of the new intrinsic call
7568 if (HaveChain)
7569 Ops.push_back(BRCOND.getOperand(0));
7570
7571 Ops.append(Intr->op_begin() + (HaveChain ? 2 : 1), Intr->op_end());
7572 Ops.push_back(Target);
7573
7574 ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end());
7575
7576 // build the new intrinsic call
7577 SDNode *Result = DAG.getNode(CFNode, DL, DAG.getVTList(Res), Ops).getNode();
7578
7579 if (!HaveChain) {
7580 SDValue Ops[] = {SDValue(Result, 0), BRCOND.getOperand(0)};
7581
7583 }
7584
7585 if (BR) {
7586 // Give the branch instruction our target
7587 SDValue Ops[] = {BR->getOperand(0), BRCOND.getOperand(2)};
7588 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops);
7589 DAG.ReplaceAllUsesWith(BR, NewBR.getNode());
7590 }
7591
7592 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
7593
7594 // Copy the intrinsic results to registers
7595 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
7596 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
7597 if (!CopyToReg)
7598 continue;
7599
7600 Chain = DAG.getCopyToReg(Chain, DL, CopyToReg->getOperand(1),
7601 SDValue(Result, i - 1), SDValue());
7602
7603 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
7604 }
7605
7606 // Remove the old intrinsic from the chain
7607 DAG.ReplaceAllUsesOfValueWith(SDValue(Intr, Intr->getNumValues() - 1),
7608 Intr->getOperand(0));
7609
7610 return Chain;
7611}
7612
7613SDValue SITargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const {
7614 MVT VT = Op.getSimpleValueType();
7615 SDLoc DL(Op);
7616 // Checking the depth
7617 if (Op.getConstantOperandVal(0) != 0)
7618 return DAG.getConstant(0, DL, VT);
7619
7620 MachineFunction &MF = DAG.getMachineFunction();
7621 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
7622 // Check for kernel and shader functions
7623 if (Info->isEntryFunction())
7624 return DAG.getConstant(0, DL, VT);
7625
7626 MachineFrameInfo &MFI = MF.getFrameInfo();
7627 // There is a call to @llvm.returnaddress in this function
7628 MFI.setReturnAddressIsTaken(true);
7629
7630 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
7631 // Get the return address reg and mark it as an implicit live-in
7632 Register Reg = MF.addLiveIn(TRI->getReturnAddressReg(MF),
7633 getRegClassFor(VT, Op.getNode()->isDivergent()));
7634
7635 return DAG.getCopyFromReg(DAG.getEntryNode(), DL, Reg, VT);
7636}
7637
7638SDValue SITargetLowering::getFPExtOrFPRound(SelectionDAG &DAG, SDValue Op,
7639 const SDLoc &DL, EVT VT) const {
7640 return Op.getValueType().bitsLE(VT)
7641 ? DAG.getNode(ISD::FP_EXTEND, DL, VT, Op)
7642 : DAG.getNode(ISD::FP_ROUND, DL, VT, Op,
7643 DAG.getTargetConstant(0, DL, MVT::i32));
7644}
7645
7646SDValue SITargetLowering::splitFP_ROUNDVectorOp(SDValue Op,
7647 SelectionDAG &DAG) const {
7648 EVT DstVT = Op.getValueType();
7649 unsigned NumElts = DstVT.getVectorNumElements();
7650 assert(NumElts > 2 && isPowerOf2_32(NumElts));
7651
7652 auto [Lo, Hi] = DAG.SplitVectorOperand(Op.getNode(), 0);
7653
7654 SDLoc DL(Op);
7655 unsigned Opc = Op.getOpcode();
7656 SDValue Flags = Op.getOperand(1);
7657 EVT HalfDstVT =
7658 EVT::getVectorVT(*DAG.getContext(), DstVT.getScalarType(), NumElts / 2);
7659 SDValue OpLo = DAG.getNode(Opc, DL, HalfDstVT, Lo, Flags);
7660 SDValue OpHi = DAG.getNode(Opc, DL, HalfDstVT, Hi, Flags);
7661
7662 return DAG.getNode(ISD::CONCAT_VECTORS, DL, DstVT, OpLo, OpHi);
7663}
7664
7665SDValue SITargetLowering::lowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
7666 SDValue Src = Op.getOperand(0);
7667 EVT SrcVT = Src.getValueType();
7668 EVT DstVT = Op.getValueType();
7669
7670 if (DstVT.isVector() && DstVT.getScalarType() == MVT::f16) {
7671 assert(Subtarget->hasCvtPkF16F32Inst() && "support v_cvt_pk_f16_f32");
7672 if (SrcVT.getScalarType() != MVT::f32)
7673 return SDValue();
7674 return SrcVT == MVT::v2f32 ? Op : splitFP_ROUNDVectorOp(Op, DAG);
7675 }
7676
7677 if (SrcVT.getScalarType() != MVT::f64)
7678 return Op;
7679
7680 SDLoc DL(Op);
7681 if (DstVT == MVT::f16) {
7682 // TODO: Handle strictfp
7683 if (Op.getOpcode() != ISD::FP_ROUND)
7684 return Op;
7685
7686 if (!Subtarget->has16BitInsts()) {
7687 SDValue FpToFp16 = DAG.getNode(ISD::FP_TO_FP16, DL, MVT::i32, Src);
7688 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, MVT::i16, FpToFp16);
7689 return DAG.getNode(ISD::BITCAST, DL, MVT::f16, Trunc);
7690 }
7691 if (Op->getFlags().hasApproximateFuncs()) {
7692 SDValue Flags = Op.getOperand(1);
7693 SDValue Src32 = DAG.getNode(ISD::FP_ROUND, DL, MVT::f32, Src, Flags);
7694 return DAG.getNode(ISD::FP_ROUND, DL, MVT::f16, Src32, Flags);
7695 }
7696 SDValue FpToFp16 = LowerF64ToF16Safe(Src, DL, DAG);
7697 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, MVT::i16, FpToFp16);
7698 return DAG.getNode(ISD::BITCAST, DL, MVT::f16, Trunc);
7699 }
7700
7701 assert(DstVT.getScalarType() == MVT::bf16 &&
7702 "custom lower FP_ROUND for f16 or bf16");
7703 assert(Subtarget->hasBF16ConversionInsts() && "f32 -> bf16 is legal");
7704
7705 // Round-inexact-to-odd f64 to f32, then do the final rounding using the
7706 // hardware f32 -> bf16 instruction.
7707 EVT F32VT = SrcVT.isVector() ? SrcVT.changeVectorElementType(MVT::f32) :
7708 MVT::f32;
7709 SDValue Rod = expandRoundInexactToOdd(F32VT, Src, DL, DAG);
7710 return DAG.getNode(ISD::FP_ROUND, DL, DstVT, Rod,
7711 DAG.getTargetConstant(0, DL, MVT::i32));
7712}
7713
7714SDValue SITargetLowering::lowerFMINNUM_FMAXNUM(SDValue Op,
7715 SelectionDAG &DAG) const {
7716 EVT VT = Op.getValueType();
7717 const MachineFunction &MF = DAG.getMachineFunction();
7718 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
7719 bool IsIEEEMode = Info->getMode().IEEE;
7720
7721 // FIXME: Assert during selection that this is only selected for
7722 // ieee_mode. Currently a combine can produce the ieee version for non-ieee
7723 // mode functions, but this happens to be OK since it's only done in cases
7724 // where there is known no sNaN.
7725 if (IsIEEEMode)
7726 return expandFMINNUM_FMAXNUM(Op.getNode(), DAG);
7727
7728 if (VT == MVT::v4f16 || VT == MVT::v8f16 || VT == MVT::v16f16 ||
7729 VT == MVT::v16bf16)
7730 return splitBinaryVectorOp(Op, DAG);
7731 return Op;
7732}
7733
7734SDValue
7735SITargetLowering::lowerFMINIMUMNUM_FMAXIMUMNUM(SDValue Op,
7736 SelectionDAG &DAG) const {
7737 EVT VT = Op.getValueType();
7738 const MachineFunction &MF = DAG.getMachineFunction();
7739 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
7740 bool IsIEEEMode = Info->getMode().IEEE;
7741
7742 if (IsIEEEMode)
7743 return expandFMINIMUMNUM_FMAXIMUMNUM(Op.getNode(), DAG);
7744
7745 if (VT == MVT::v4f16 || VT == MVT::v8f16 || VT == MVT::v16f16 ||
7746 VT == MVT::v16bf16)
7747 return splitBinaryVectorOp(Op, DAG);
7748 return Op;
7749}
7750
7751SDValue SITargetLowering::lowerFMINIMUM_FMAXIMUM(SDValue Op,
7752 SelectionDAG &DAG) const {
7753 EVT VT = Op.getValueType();
7754 if (VT.isVector())
7755 return splitBinaryVectorOp(Op, DAG);
7756
7757 assert(!Subtarget->hasIEEEMinimumMaximumInsts() &&
7758 !Subtarget->hasMinimum3Maximum3F16() &&
7759 Subtarget->hasMinimum3Maximum3PKF16() && VT == MVT::f16 &&
7760 "should not need to widen f16 minimum/maximum to v2f16");
7761
7762 // Widen f16 operation to v2f16
7763
7764 // fminimum f16:x, f16:y ->
7765 // extract_vector_elt (fminimum (v2f16 (scalar_to_vector x))
7766 // (v2f16 (scalar_to_vector y))), 0
7767 SDLoc SL(Op);
7768 SDValue WideSrc0 =
7769 DAG.getNode(ISD::SCALAR_TO_VECTOR, SL, MVT::v2f16, Op.getOperand(0));
7770 SDValue WideSrc1 =
7771 DAG.getNode(ISD::SCALAR_TO_VECTOR, SL, MVT::v2f16, Op.getOperand(1));
7772
7773 SDValue Widened =
7774 DAG.getNode(Op.getOpcode(), SL, MVT::v2f16, WideSrc0, WideSrc1);
7775
7776 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::f16, Widened,
7777 DAG.getConstant(0, SL, MVT::i32));
7778}
7779
7780SDValue SITargetLowering::lowerFLDEXP(SDValue Op, SelectionDAG &DAG) const {
7781 bool IsStrict = Op.getOpcode() == ISD::STRICT_FLDEXP;
7782 EVT VT = Op.getValueType();
7783 assert(VT == MVT::f16);
7784
7785 SDValue Exp = Op.getOperand(IsStrict ? 2 : 1);
7786 EVT ExpVT = Exp.getValueType();
7787 if (ExpVT == MVT::i16)
7788 return Op;
7789
7790 SDLoc DL(Op);
7791
7792 // Correct the exponent type for f16 to i16.
7793 // Clamp the range of the exponent to the instruction's range.
7794
7795 // TODO: This should be a generic narrowing legalization, and can easily be
7796 // for GlobalISel.
7797
7798 SDValue MinExp = DAG.getSignedConstant(minIntN(16), DL, ExpVT);
7799 SDValue ClampMin = DAG.getNode(ISD::SMAX, DL, ExpVT, Exp, MinExp);
7800
7801 SDValue MaxExp = DAG.getSignedConstant(maxIntN(16), DL, ExpVT);
7802 SDValue Clamp = DAG.getNode(ISD::SMIN, DL, ExpVT, ClampMin, MaxExp);
7803
7804 SDValue TruncExp = DAG.getNode(ISD::TRUNCATE, DL, MVT::i16, Clamp);
7805
7806 if (IsStrict) {
7807 return DAG.getNode(ISD::STRICT_FLDEXP, DL, {VT, MVT::Other},
7808 {Op.getOperand(0), Op.getOperand(1), TruncExp});
7809 }
7810
7811 return DAG.getNode(ISD::FLDEXP, DL, VT, Op.getOperand(0), TruncExp);
7812}
7813
7815 switch (Op->getOpcode()) {
7816 case ISD::SRA:
7817 case ISD::SMIN:
7818 case ISD::SMAX:
7819 return ISD::SIGN_EXTEND;
7820 case ISD::SRL:
7821 case ISD::UMIN:
7822 case ISD::UMAX:
7823 return ISD::ZERO_EXTEND;
7824 case ISD::ADD:
7825 case ISD::SUB:
7826 case ISD::AND:
7827 case ISD::OR:
7828 case ISD::XOR:
7829 case ISD::SHL:
7830 case ISD::SELECT:
7831 case ISD::MUL:
7832 // operation result won't be influenced by garbage high bits.
7833 // TODO: are all of those cases correct, and are there more?
7834 return ISD::ANY_EXTEND;
7835 case ISD::SETCC: {
7836 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7838 }
7839 default:
7840 llvm_unreachable("unexpected opcode!");
7841 }
7842}
7843
7844SDValue SITargetLowering::promoteUniformOpToI32(SDValue Op,
7845 DAGCombinerInfo &DCI) const {
7846 const unsigned Opc = Op.getOpcode();
7847 assert(Opc == ISD::ADD || Opc == ISD::SUB || Opc == ISD::SHL ||
7848 Opc == ISD::SRL || Opc == ISD::SRA || Opc == ISD::AND ||
7849 Opc == ISD::OR || Opc == ISD::XOR || Opc == ISD::MUL ||
7850 Opc == ISD::SETCC || Opc == ISD::SELECT || Opc == ISD::SMIN ||
7851 Opc == ISD::SMAX || Opc == ISD::UMIN || Opc == ISD::UMAX);
7852
7853 EVT OpTy = (Opc != ISD::SETCC) ? Op.getValueType()
7854 : Op->getOperand(0).getValueType();
7855 auto ExtTy = OpTy.changeElementType(MVT::i32);
7856
7857 if (DCI.isBeforeLegalizeOps() ||
7858 isNarrowingProfitable(Op.getNode(), ExtTy, OpTy))
7859 return SDValue();
7860
7861 auto &DAG = DCI.DAG;
7862
7863 SDLoc DL(Op);
7864 SDValue LHS;
7865 SDValue RHS;
7866 if (Opc == ISD::SELECT) {
7867 LHS = Op->getOperand(1);
7868 RHS = Op->getOperand(2);
7869 } else {
7870 LHS = Op->getOperand(0);
7871 RHS = Op->getOperand(1);
7872 }
7873
7874 const unsigned ExtOp = getExtOpcodeForPromotedOp(Op);
7875 LHS = DAG.getNode(ExtOp, DL, ExtTy, {LHS});
7876
7877 // Special case: for shifts, the RHS always needs a zext.
7878 if (Opc == ISD::SHL || Opc == ISD::SRL || Opc == ISD::SRA)
7879 RHS = DAG.getNode(ISD::ZERO_EXTEND, DL, ExtTy, {RHS});
7880 else
7881 RHS = DAG.getNode(ExtOp, DL, ExtTy, {RHS});
7882
7883 // setcc always return i1/i1 vec so no need to truncate after.
7884 if (Opc == ISD::SETCC) {
7885 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7886 return DAG.getSetCC(DL, Op.getValueType(), LHS, RHS, CC);
7887 }
7888
7889 // For other ops, we extend the operation's return type as well so we need to
7890 // truncate back to the original type.
7891 SDValue NewVal;
7892 if (Opc == ISD::SELECT)
7893 NewVal = DAG.getNode(ISD::SELECT, DL, ExtTy, {Op->getOperand(0), LHS, RHS});
7894 else
7895 NewVal = DAG.getNode(Opc, DL, ExtTy, {LHS, RHS});
7896
7897 return DAG.getZExtOrTrunc(NewVal, DL, OpTy);
7898}
7899
7900SDValue SITargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
7901 SDValue Mag = Op.getOperand(0);
7902 EVT MagVT = Mag.getValueType();
7903
7904 if (MagVT.getVectorNumElements() > 2)
7905 return splitBinaryVectorOp(Op, DAG);
7906
7907 SDValue Sign = Op.getOperand(1);
7908 EVT SignVT = Sign.getValueType();
7909
7910 if (MagVT == SignVT)
7911 return Op;
7912
7913 // fcopysign v2f16:mag, v2f32:sign ->
7914 // fcopysign v2f16:mag, bitcast (trunc (bitcast sign to v2i32) to v2i16)
7915
7916 SDLoc SL(Op);
7917 SDValue SignAsInt32 = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Sign);
7918 SDValue SignAsInt16 = DAG.getNode(ISD::TRUNCATE, SL, MVT::v2i16, SignAsInt32);
7919
7920 SDValue SignAsHalf16 = DAG.getNode(ISD::BITCAST, SL, MagVT, SignAsInt16);
7921
7922 return DAG.getNode(ISD::FCOPYSIGN, SL, MagVT, Mag, SignAsHalf16);
7923}
7924
7925// Custom lowering for vector multiplications and s_mul_u64.
7926SDValue SITargetLowering::lowerMUL(SDValue Op, SelectionDAG &DAG) const {
7927 EVT VT = Op.getValueType();
7928
7929 // Split vector operands.
7930 if (VT.isVector())
7931 return splitBinaryVectorOp(Op, DAG);
7932
7933 assert(VT == MVT::i64 && "The following code is a special for s_mul_u64");
7934
7935 // There are four ways to lower s_mul_u64:
7936 //
7937 // 1. If all the operands are uniform, then we lower it as it is.
7938 //
7939 // 2. If the operands are divergent, then we have to split s_mul_u64 in 32-bit
7940 // multiplications because there is not a vector equivalent of s_mul_u64.
7941 //
7942 // 3. If the cost model decides that it is more efficient to use vector
7943 // registers, then we have to split s_mul_u64 in 32-bit multiplications.
7944 // This happens in splitScalarSMULU64() in SIInstrInfo.cpp .
7945 //
7946 // 4. If the cost model decides to use vector registers and both of the
7947 // operands are zero-extended/sign-extended from 32-bits, then we split the
7948 // s_mul_u64 in two 32-bit multiplications. The problem is that it is not
7949 // possible to check if the operands are zero-extended or sign-extended in
7950 // SIInstrInfo.cpp. For this reason, here, we replace s_mul_u64 with
7951 // s_mul_u64_u32_pseudo if both operands are zero-extended and we replace
7952 // s_mul_u64 with s_mul_i64_i32_pseudo if both operands are sign-extended.
7953 // If the cost model decides that we have to use vector registers, then
7954 // splitScalarSMulPseudo() (in SIInstrInfo.cpp) split s_mul_u64_u32/
7955 // s_mul_i64_i32_pseudo in two vector multiplications. If the cost model
7956 // decides that we should use scalar registers, then s_mul_u64_u32_pseudo/
7957 // s_mul_i64_i32_pseudo is lowered as s_mul_u64 in expandPostRAPseudo() in
7958 // SIInstrInfo.cpp .
7959
7960 if (Op->isDivergent())
7961 return SDValue();
7962
7963 SDValue Op0 = Op.getOperand(0);
7964 SDValue Op1 = Op.getOperand(1);
7965 // If all the operands are zero-enteted to 32-bits, then we replace s_mul_u64
7966 // with s_mul_u64_u32_pseudo. If all the operands are sign-extended to
7967 // 32-bits, then we replace s_mul_u64 with s_mul_i64_i32_pseudo.
7968 KnownBits Op0KnownBits = DAG.computeKnownBits(Op0);
7969 unsigned Op0LeadingZeros = Op0KnownBits.countMinLeadingZeros();
7970 KnownBits Op1KnownBits = DAG.computeKnownBits(Op1);
7971 unsigned Op1LeadingZeros = Op1KnownBits.countMinLeadingZeros();
7972 SDLoc SL(Op);
7973 if (Op0LeadingZeros >= 32 && Op1LeadingZeros >= 32)
7974 return SDValue(
7975 DAG.getMachineNode(AMDGPU::S_MUL_U64_U32_PSEUDO, SL, VT, Op0, Op1), 0);
7976 unsigned Op0SignBits = DAG.ComputeNumSignBits(Op0);
7977 unsigned Op1SignBits = DAG.ComputeNumSignBits(Op1);
7978 if (Op0SignBits >= 33 && Op1SignBits >= 33)
7979 return SDValue(
7980 DAG.getMachineNode(AMDGPU::S_MUL_I64_I32_PSEUDO, SL, VT, Op0, Op1), 0);
7981 // If all the operands are uniform, then we lower s_mul_u64 as it is.
7982 return Op;
7983}
7984
7985SDValue SITargetLowering::lowerXMULO(SDValue Op, SelectionDAG &DAG) const {
7986 EVT VT = Op.getValueType();
7987 SDLoc SL(Op);
7988 SDValue LHS = Op.getOperand(0);
7989 SDValue RHS = Op.getOperand(1);
7990 bool isSigned = Op.getOpcode() == ISD::SMULO;
7991
7992 if (ConstantSDNode *RHSC = isConstOrConstSplat(RHS)) {
7993 const APInt &C = RHSC->getAPIntValue();
7994 // mulo(X, 1 << S) -> { X << S, (X << S) >> S != X }
7995 if (C.isPowerOf2()) {
7996 // smulo(x, signed_min) is same as umulo(x, signed_min).
7997 bool UseArithShift = isSigned && !C.isMinSignedValue();
7998 SDValue ShiftAmt = DAG.getConstant(C.logBase2(), SL, MVT::i32);
7999 SDValue Result = DAG.getNode(ISD::SHL, SL, VT, LHS, ShiftAmt);
8000 SDValue Overflow =
8001 DAG.getSetCC(SL, MVT::i1,
8002 DAG.getNode(UseArithShift ? ISD::SRA : ISD::SRL, SL, VT,
8003 Result, ShiftAmt),
8004 LHS, ISD::SETNE);
8005 return DAG.getMergeValues({Result, Overflow}, SL);
8006 }
8007 }
8008
8009 SDValue Result = DAG.getNode(ISD::MUL, SL, VT, LHS, RHS);
8010 SDValue Top =
8011 DAG.getNode(isSigned ? ISD::MULHS : ISD::MULHU, SL, VT, LHS, RHS);
8012
8013 SDValue Sign = isSigned
8014 ? DAG.getNode(ISD::SRA, SL, VT, Result,
8015 DAG.getConstant(VT.getScalarSizeInBits() - 1,
8016 SL, MVT::i32))
8017 : DAG.getConstant(0, SL, VT);
8018 SDValue Overflow = DAG.getSetCC(SL, MVT::i1, Top, Sign, ISD::SETNE);
8019
8020 return DAG.getMergeValues({Result, Overflow}, SL);
8021}
8022
8023SDValue SITargetLowering::lowerXMUL_LOHI(SDValue Op, SelectionDAG &DAG) const {
8024 if (Op->isDivergent()) {
8025 // Select to V_MAD_[IU]64_[IU]32.
8026 return Op;
8027 }
8028 if (Subtarget->hasSMulHi()) {
8029 // Expand to S_MUL_I32 + S_MUL_HI_[IU]32.
8030 return SDValue();
8031 }
8032 // The multiply is uniform but we would have to use V_MUL_HI_[IU]32 to
8033 // calculate the high part, so we might as well do the whole thing with
8034 // V_MAD_[IU]64_[IU]32.
8035 return Op;
8036}
8037
8038SDValue SITargetLowering::lowerTRAP(SDValue Op, SelectionDAG &DAG) const {
8039 if (!Subtarget->isTrapHandlerEnabled() ||
8040 Subtarget->getTrapHandlerAbi() != GCNSubtarget::TrapHandlerAbi::AMDHSA)
8041 return lowerTrapEndpgm(Op, DAG);
8042
8043 return Subtarget->supportsGetDoorbellID() ? lowerTrapHsa(Op, DAG)
8044 : lowerTrapHsaQueuePtr(Op, DAG);
8045}
8046
8047SDValue SITargetLowering::lowerTrapEndpgm(SDValue Op, SelectionDAG &DAG) const {
8048 SDLoc SL(Op);
8049 SDValue Chain = Op.getOperand(0);
8050 return DAG.getNode(AMDGPUISD::ENDPGM_TRAP, SL, MVT::Other, Chain);
8051}
8052
8053SDValue
8054SITargetLowering::loadImplicitKernelArgument(SelectionDAG &DAG, MVT VT,
8055 const SDLoc &DL, Align Alignment,
8056 ImplicitParameter Param) const {
8057 MachineFunction &MF = DAG.getMachineFunction();
8058 uint64_t Offset = getImplicitParameterOffset(MF, Param);
8059 SDValue Ptr = lowerKernArgParameterPtr(DAG, DL, DAG.getEntryNode(), Offset);
8060 MachinePointerInfo PtrInfo(AMDGPUAS::CONSTANT_ADDRESS);
8061 return DAG.getLoad(VT, DL, DAG.getEntryNode(), Ptr, PtrInfo, Alignment,
8064}
8065
8066SDValue SITargetLowering::lowerTrapHsaQueuePtr(SDValue Op,
8067 SelectionDAG &DAG) const {
8068 SDLoc SL(Op);
8069 SDValue Chain = Op.getOperand(0);
8070
8071 SDValue QueuePtr;
8072 // For code object version 5, QueuePtr is passed through implicit kernarg.
8073 const Module *M = DAG.getMachineFunction().getFunction().getParent();
8075 QueuePtr =
8076 loadImplicitKernelArgument(DAG, MVT::i64, SL, Align(8), QUEUE_PTR);
8077 } else {
8078 MachineFunction &MF = DAG.getMachineFunction();
8079 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
8080 Register UserSGPR = Info->getQueuePtrUserSGPR();
8081
8082 if (UserSGPR == AMDGPU::NoRegister) {
8083 // We probably are in a function incorrectly marked with
8084 // amdgpu-no-queue-ptr. This is undefined. We don't want to delete the
8085 // trap, so just use a null pointer.
8086 QueuePtr = DAG.getConstant(0, SL, MVT::i64);
8087 } else {
8088 QueuePtr = CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, UserSGPR,
8089 MVT::i64);
8090 }
8091 }
8092
8093 SDValue SGPR01 = DAG.getRegister(AMDGPU::SGPR0_SGPR1, MVT::i64);
8094 SDValue ToReg = DAG.getCopyToReg(Chain, SL, SGPR01, QueuePtr, SDValue());
8095
8096 uint64_t TrapID = static_cast<uint64_t>(GCNSubtarget::TrapID::LLVMAMDHSATrap);
8097 SDValue Ops[] = {ToReg, DAG.getTargetConstant(TrapID, SL, MVT::i16), SGPR01,
8098 ToReg.getValue(1)};
8099 return DAG.getNode(AMDGPUISD::TRAP, SL, MVT::Other, Ops);
8100}
8101
8102SDValue SITargetLowering::lowerTrapHsa(SDValue Op, SelectionDAG &DAG) const {
8103 SDLoc SL(Op);
8104 SDValue Chain = Op.getOperand(0);
8105
8106 // We need to simulate the 's_trap 2' instruction on targets that run in
8107 // PRIV=1 (where it is treated as a nop).
8108 if (Subtarget->hasPrivEnabledTrap2NopBug())
8109 return DAG.getNode(AMDGPUISD::SIMULATED_TRAP, SL, MVT::Other, Chain);
8110
8111 uint64_t TrapID = static_cast<uint64_t>(GCNSubtarget::TrapID::LLVMAMDHSATrap);
8112 SDValue Ops[] = {Chain, DAG.getTargetConstant(TrapID, SL, MVT::i16)};
8113 return DAG.getNode(AMDGPUISD::TRAP, SL, MVT::Other, Ops);
8114}
8115
8116SDValue SITargetLowering::lowerDEBUGTRAP(SDValue Op, SelectionDAG &DAG) const {
8117 SDLoc SL(Op);
8118 SDValue Chain = Op.getOperand(0);
8119 MachineFunction &MF = DAG.getMachineFunction();
8120
8121 if (!Subtarget->isTrapHandlerEnabled() ||
8122 Subtarget->getTrapHandlerAbi() != GCNSubtarget::TrapHandlerAbi::AMDHSA) {
8123 LLVMContext &Ctx = MF.getFunction().getContext();
8124 Ctx.diagnose(DiagnosticInfoUnsupported(MF.getFunction(),
8125 "debugtrap handler not supported",
8126 Op.getDebugLoc(), DS_Warning));
8127 return Chain;
8128 }
8129
8130 uint64_t TrapID =
8131 static_cast<uint64_t>(GCNSubtarget::TrapID::LLVMAMDHSADebugTrap);
8132 SDValue Ops[] = {Chain, DAG.getTargetConstant(TrapID, SL, MVT::i16)};
8133 return DAG.getNode(AMDGPUISD::TRAP, SL, MVT::Other, Ops);
8134}
8135
8136SDValue SITargetLowering::getSegmentAperture(unsigned AS, const SDLoc &DL,
8137 SelectionDAG &DAG) const {
8138 if (Subtarget->hasApertureRegs()) {
8139 const unsigned ApertureRegNo = (AS == AMDGPUAS::LOCAL_ADDRESS)
8140 ? AMDGPU::SRC_SHARED_BASE
8141 : AMDGPU::SRC_PRIVATE_BASE;
8142 assert((ApertureRegNo != AMDGPU::SRC_PRIVATE_BASE ||
8143 !Subtarget->hasGloballyAddressableScratch()) &&
8144 "Cannot use src_private_base with globally addressable scratch!");
8145 // Note: this feature (register) is broken. When used as a 32-bit operand,
8146 // it returns a wrong value (all zeroes?). The real value is in the upper 32
8147 // bits.
8148 //
8149 // To work around the issue, emit a 64 bit copy from this register
8150 // then extract the high bits. Note that this shouldn't even result in a
8151 // shift being emitted and simply become a pair of registers (e.g.):
8152 // s_mov_b64 s[6:7], src_shared_base
8153 // v_mov_b32_e32 v1, s7
8154 SDValue Copy =
8155 DAG.getCopyFromReg(DAG.getEntryNode(), DL, ApertureRegNo, MVT::v2i32);
8156 return DAG.getExtractVectorElt(DL, MVT::i32, Copy, 1);
8157 }
8158
8159 // For code object version 5, private_base and shared_base are passed through
8160 // implicit kernargs.
8161 const Module *M = DAG.getMachineFunction().getFunction().getParent();
8165 return loadImplicitKernelArgument(DAG, MVT::i32, DL, Align(4), Param);
8166 }
8167
8168 MachineFunction &MF = DAG.getMachineFunction();
8169 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
8170 Register UserSGPR = Info->getQueuePtrUserSGPR();
8171 if (UserSGPR == AMDGPU::NoRegister) {
8172 // We probably are in a function incorrectly marked with
8173 // amdgpu-no-queue-ptr. This is undefined.
8174 return DAG.getPOISON(MVT::i32);
8175 }
8176
8177 SDValue QueuePtr =
8178 CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, UserSGPR, MVT::i64);
8179
8180 // Offset into amd_queue_t for group_segment_aperture_base_hi /
8181 // private_segment_aperture_base_hi.
8182 uint32_t StructOffset = (AS == AMDGPUAS::LOCAL_ADDRESS) ? 0x40 : 0x44;
8183
8184 SDValue Ptr =
8185 DAG.getObjectPtrOffset(DL, QueuePtr, TypeSize::getFixed(StructOffset));
8186
8187 // TODO: Use custom target PseudoSourceValue.
8188 // TODO: We should use the value from the IR intrinsic call, but it might not
8189 // be available and how do we get it?
8190 MachinePointerInfo PtrInfo(AMDGPUAS::CONSTANT_ADDRESS);
8191 return DAG.getLoad(MVT::i32, DL, QueuePtr.getValue(1), Ptr, PtrInfo,
8192 commonAlignment(Align(64), StructOffset),
8195}
8196
8197/// Return true if the value is a known valid address, such that a null check is
8198/// not necessary.
8200 const AMDGPUTargetMachine &TM, unsigned AddrSpace) {
8202 return true;
8203
8204 if (auto *ConstVal = dyn_cast<ConstantSDNode>(Val))
8205 return ConstVal->getSExtValue() != TM.getNullPointerValue(AddrSpace);
8206
8207 // TODO: Search through arithmetic, handle arguments and loads
8208 // marked nonnull.
8209 return false;
8210}
8211
8212SDValue SITargetLowering::lowerADDRSPACECAST(SDValue Op,
8213 SelectionDAG &DAG) const {
8214 SDLoc SL(Op);
8215
8216 const AMDGPUTargetMachine &TM =
8217 static_cast<const AMDGPUTargetMachine &>(getTargetMachine());
8218
8219 unsigned DestAS, SrcAS;
8220 SDValue Src;
8221 bool IsNonNull = false;
8222 if (const auto *ASC = dyn_cast<AddrSpaceCastSDNode>(Op)) {
8223 SrcAS = ASC->getSrcAddressSpace();
8224 Src = ASC->getOperand(0);
8225 DestAS = ASC->getDestAddressSpace();
8226 } else {
8227 assert(Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
8228 Op.getConstantOperandVal(0) ==
8229 Intrinsic::amdgcn_addrspacecast_nonnull);
8230 Src = Op->getOperand(1);
8231 SrcAS = Op->getConstantOperandVal(2);
8232 DestAS = Op->getConstantOperandVal(3);
8233 IsNonNull = true;
8234 }
8235
8236 SDValue FlatNullPtr = DAG.getConstant(0, SL, MVT::i64);
8237
8238 // flat -> local/private
8239 if (SrcAS == AMDGPUAS::FLAT_ADDRESS) {
8240 if (DestAS == AMDGPUAS::LOCAL_ADDRESS ||
8241 DestAS == AMDGPUAS::PRIVATE_ADDRESS) {
8242 SDValue Ptr = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, Src);
8243
8244 if (DestAS == AMDGPUAS::PRIVATE_ADDRESS &&
8245 Subtarget->hasGloballyAddressableScratch()) {
8246 // flat -> private with globally addressable scratch: subtract
8247 // src_flat_scratch_base_lo.
8248 SDValue FlatScratchBaseLo(
8249 DAG.getMachineNode(
8250 AMDGPU::S_MOV_B32, SL, MVT::i32,
8251 DAG.getRegister(AMDGPU::SRC_FLAT_SCRATCH_BASE_LO, MVT::i32)),
8252 0);
8253 Ptr = DAG.getNode(ISD::SUB, SL, MVT::i32, Ptr, FlatScratchBaseLo);
8254 }
8255
8256 if (IsNonNull || isKnownNonNull(Op, DAG, TM, SrcAS))
8257 return Ptr;
8258
8259 unsigned NullVal = TM.getNullPointerValue(DestAS);
8260 SDValue SegmentNullPtr = DAG.getConstant(NullVal, SL, MVT::i32);
8261 SDValue NonNull = DAG.getSetCC(SL, MVT::i1, Src, FlatNullPtr, ISD::SETNE);
8262
8263 return DAG.getNode(ISD::SELECT, SL, MVT::i32, NonNull, Ptr,
8264 SegmentNullPtr);
8265 }
8266 }
8267
8268 // local/private -> flat
8269 if (DestAS == AMDGPUAS::FLAT_ADDRESS) {
8270 if (SrcAS == AMDGPUAS::LOCAL_ADDRESS ||
8271 SrcAS == AMDGPUAS::PRIVATE_ADDRESS) {
8272 SDValue CvtPtr;
8273 if (SrcAS == AMDGPUAS::PRIVATE_ADDRESS &&
8274 Subtarget->hasGloballyAddressableScratch()) {
8275 // For wave32: Addr = (TID[4:0] << 52) + FLAT_SCRATCH_BASE + privateAddr
8276 // For wave64: Addr = (TID[5:0] << 51) + FLAT_SCRATCH_BASE + privateAddr
8277 SDValue AllOnes = DAG.getSignedTargetConstant(-1, SL, MVT::i32);
8278 SDValue ThreadID = DAG.getConstant(0, SL, MVT::i32);
8279 ThreadID = DAG.getNode(
8280 ISD::INTRINSIC_WO_CHAIN, SL, MVT::i32,
8281 DAG.getTargetConstant(Intrinsic::amdgcn_mbcnt_lo, SL, MVT::i32),
8282 AllOnes, ThreadID);
8283 if (Subtarget->isWave64())
8284 ThreadID = DAG.getNode(
8285 ISD::INTRINSIC_WO_CHAIN, SL, MVT::i32,
8286 DAG.getTargetConstant(Intrinsic::amdgcn_mbcnt_hi, SL, MVT::i32),
8287 AllOnes, ThreadID);
8288 SDValue ShAmt = DAG.getShiftAmountConstant(
8289 57 - 32 - Subtarget->getWavefrontSizeLog2(), MVT::i32, SL);
8290 SDValue SrcHi = DAG.getNode(ISD::SHL, SL, MVT::i32, ThreadID, ShAmt);
8291 CvtPtr = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, Src, SrcHi);
8292 CvtPtr = DAG.getNode(ISD::BITCAST, SL, MVT::i64, CvtPtr);
8293 // Accessing src_flat_scratch_base_lo as a 64-bit operand gives the full
8294 // 64-bit hi:lo value.
8295 SDValue FlatScratchBase = {
8296 DAG.getMachineNode(
8297 AMDGPU::S_MOV_B64, SL, MVT::i64,
8298 DAG.getRegister(AMDGPU::SRC_FLAT_SCRATCH_BASE, MVT::i64)),
8299 0};
8300 CvtPtr = DAG.getNode(ISD::ADD, SL, MVT::i64, CvtPtr, FlatScratchBase);
8301 } else {
8302 SDValue Aperture = getSegmentAperture(SrcAS, SL, DAG);
8303 CvtPtr = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, Src, Aperture);
8304 CvtPtr = DAG.getNode(ISD::BITCAST, SL, MVT::i64, CvtPtr);
8305 }
8306
8307 if (IsNonNull || isKnownNonNull(Op, DAG, TM, SrcAS))
8308 return CvtPtr;
8309
8310 unsigned NullVal = TM.getNullPointerValue(SrcAS);
8311 SDValue SegmentNullPtr = DAG.getConstant(NullVal, SL, MVT::i32);
8312
8313 SDValue NonNull =
8314 DAG.getSetCC(SL, MVT::i1, Src, SegmentNullPtr, ISD::SETNE);
8315
8316 return DAG.getNode(ISD::SELECT, SL, MVT::i64, NonNull, CvtPtr,
8317 FlatNullPtr);
8318 }
8319 }
8320
8321 if (SrcAS == AMDGPUAS::CONSTANT_ADDRESS_32BIT &&
8322 Op.getValueType() == MVT::i64) {
8323 const SIMachineFunctionInfo *Info =
8324 DAG.getMachineFunction().getInfo<SIMachineFunctionInfo>();
8325 SDValue Hi = DAG.getConstant(Info->get32BitAddressHighBits(), SL, MVT::i32);
8326 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, Src, Hi);
8327 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
8328 }
8329
8330 if (DestAS == AMDGPUAS::CONSTANT_ADDRESS_32BIT &&
8331 Src.getValueType() == MVT::i64)
8332 return DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, Src);
8333
8334 // global <-> flat are no-ops and never emitted.
8335
8336 // Invalid casts are poison.
8337 return DAG.getPOISON(Op->getValueType(0));
8338}
8339
8340// This lowers an INSERT_SUBVECTOR by extracting the individual elements from
8341// the small vector and inserting them into the big vector. That is better than
8342// the default expansion of doing it via a stack slot. Even though the use of
8343// the stack slot would be optimized away afterwards, the stack slot itself
8344// remains.
8345SDValue SITargetLowering::lowerINSERT_SUBVECTOR(SDValue Op,
8346 SelectionDAG &DAG) const {
8347 SDValue Vec = Op.getOperand(0);
8348 SDValue Ins = Op.getOperand(1);
8349 SDValue Idx = Op.getOperand(2);
8350 EVT VecVT = Vec.getValueType();
8351 EVT InsVT = Ins.getValueType();
8352 EVT EltVT = VecVT.getVectorElementType();
8353 unsigned InsNumElts = InsVT.getVectorNumElements();
8354 unsigned IdxVal = Idx->getAsZExtVal();
8355 SDLoc SL(Op);
8356
8357 if (EltVT.getScalarSizeInBits() == 16 && IdxVal % 2 == 0) {
8358 // Insert 32-bit registers at a time.
8359 assert(InsNumElts % 2 == 0 && "expect legal vector types");
8360
8361 unsigned VecNumElts = VecVT.getVectorNumElements();
8362 EVT NewVecVT =
8363 EVT::getVectorVT(*DAG.getContext(), MVT::i32, VecNumElts / 2);
8364 EVT NewInsVT = InsNumElts == 2 ? MVT::i32
8366 MVT::i32, InsNumElts / 2);
8367
8368 Vec = DAG.getNode(ISD::BITCAST, SL, NewVecVT, Vec);
8369 Ins = DAG.getNode(ISD::BITCAST, SL, NewInsVT, Ins);
8370
8371 for (unsigned I = 0; I != InsNumElts / 2; ++I) {
8372 SDValue Elt;
8373 if (InsNumElts == 2) {
8374 Elt = Ins;
8375 } else {
8376 Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Ins,
8377 DAG.getConstant(I, SL, MVT::i32));
8378 }
8379 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, SL, NewVecVT, Vec, Elt,
8380 DAG.getConstant(IdxVal / 2 + I, SL, MVT::i32));
8381 }
8382
8383 return DAG.getNode(ISD::BITCAST, SL, VecVT, Vec);
8384 }
8385
8386 for (unsigned I = 0; I != InsNumElts; ++I) {
8387 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, Ins,
8388 DAG.getConstant(I, SL, MVT::i32));
8389 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, SL, VecVT, Vec, Elt,
8390 DAG.getConstant(IdxVal + I, SL, MVT::i32));
8391 }
8392 return Vec;
8393}
8394
8395SDValue SITargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op,
8396 SelectionDAG &DAG) const {
8397 SDValue Vec = Op.getOperand(0);
8398 SDValue InsVal = Op.getOperand(1);
8399 SDValue Idx = Op.getOperand(2);
8400 EVT VecVT = Vec.getValueType();
8401 EVT EltVT = VecVT.getVectorElementType();
8402 unsigned VecSize = VecVT.getSizeInBits();
8403 unsigned EltSize = EltVT.getSizeInBits();
8404 SDLoc SL(Op);
8405
8406 // Specially handle the case of v4i16 with static indexing.
8407 unsigned NumElts = VecVT.getVectorNumElements();
8408 auto *KIdx = dyn_cast<ConstantSDNode>(Idx);
8409 if (NumElts == 4 && EltSize == 16 && KIdx) {
8410 SDValue BCVec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Vec);
8411
8412 SDValue LoHalf = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BCVec,
8413 DAG.getConstant(0, SL, MVT::i32));
8414 SDValue HiHalf = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BCVec,
8415 DAG.getConstant(1, SL, MVT::i32));
8416
8417 SDValue LoVec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i16, LoHalf);
8418 SDValue HiVec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i16, HiHalf);
8419
8420 unsigned Idx = KIdx->getZExtValue();
8421 bool InsertLo = Idx < 2;
8422 SDValue InsHalf = DAG.getNode(
8423 ISD::INSERT_VECTOR_ELT, SL, MVT::v2i16, InsertLo ? LoVec : HiVec,
8424 DAG.getNode(ISD::BITCAST, SL, MVT::i16, InsVal),
8425 DAG.getConstant(InsertLo ? Idx : (Idx - 2), SL, MVT::i32));
8426
8427 InsHalf = DAG.getNode(ISD::BITCAST, SL, MVT::i32, InsHalf);
8428
8429 SDValue Concat =
8430 InsertLo ? DAG.getBuildVector(MVT::v2i32, SL, {InsHalf, HiHalf})
8431 : DAG.getBuildVector(MVT::v2i32, SL, {LoHalf, InsHalf});
8432
8433 return DAG.getNode(ISD::BITCAST, SL, VecVT, Concat);
8434 }
8435
8436 // Static indexing does not lower to stack access, and hence there is no need
8437 // for special custom lowering to avoid stack access.
8438 if (isa<ConstantSDNode>(Idx))
8439 return SDValue();
8440
8441 // Avoid stack access for dynamic indexing by custom lowering to
8442 // v_bfi_b32 (v_bfm_b32 16, (shl idx, 16)), val, vec
8443
8444 assert(VecSize <= 64 && "Expected target vector size to be <= 64 bits");
8445
8446 MVT IntVT = MVT::getIntegerVT(VecSize);
8447
8448 // Convert vector index to bit-index and get the required bit mask.
8449 assert(isPowerOf2_32(EltSize));
8450 const auto EltMask = maskTrailingOnes<uint64_t>(EltSize);
8451 SDValue ScaleFactor = DAG.getConstant(Log2_32(EltSize), SL, MVT::i32);
8452 SDValue ScaledIdx = DAG.getNode(ISD::SHL, SL, MVT::i32, Idx, ScaleFactor);
8453 SDValue BFM = DAG.getNode(ISD::SHL, SL, IntVT,
8454 DAG.getConstant(EltMask, SL, IntVT), ScaledIdx);
8455
8456 // 1. Create a congruent vector with the target value in each element.
8457 SDValue ExtVal = DAG.getNode(ISD::BITCAST, SL, IntVT,
8458 DAG.getSplatBuildVector(VecVT, SL, InsVal));
8459
8460 // 2. Mask off all other indices except the required index within (1).
8461 SDValue LHS = DAG.getNode(ISD::AND, SL, IntVT, BFM, ExtVal);
8462
8463 // 3. Mask off the required index within the target vector.
8464 SDValue BCVec = DAG.getNode(ISD::BITCAST, SL, IntVT, Vec);
8465 SDValue RHS =
8466 DAG.getNode(ISD::AND, SL, IntVT, DAG.getNOT(SL, BFM, IntVT), BCVec);
8467
8468 // 4. Get (2) and (3) ORed into the target vector.
8469 SDValue BFI =
8470 DAG.getNode(ISD::OR, SL, IntVT, LHS, RHS, SDNodeFlags::Disjoint);
8471
8472 return DAG.getNode(ISD::BITCAST, SL, VecVT, BFI);
8473}
8474
8475SDValue SITargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op,
8476 SelectionDAG &DAG) const {
8477 SDLoc SL(Op);
8478
8479 EVT ResultVT = Op.getValueType();
8480 SDValue Vec = Op.getOperand(0);
8481 SDValue Idx = Op.getOperand(1);
8482 EVT VecVT = Vec.getValueType();
8483 unsigned VecSize = VecVT.getSizeInBits();
8484 EVT EltVT = VecVT.getVectorElementType();
8485
8486 DAGCombinerInfo DCI(DAG, AfterLegalizeVectorOps, true, nullptr);
8487
8488 // Make sure we do any optimizations that will make it easier to fold
8489 // source modifiers before obscuring it with bit operations.
8490
8491 // XXX - Why doesn't this get called when vector_shuffle is expanded?
8492 if (SDValue Combined = performExtractVectorEltCombine(Op.getNode(), DCI))
8493 return Combined;
8494
8495 if (VecSize == 128 || VecSize == 256 || VecSize == 512) {
8496 SDValue Lo, Hi;
8497 auto [LoVT, HiVT] = DAG.GetSplitDestVTs(VecVT);
8498
8499 if (VecSize == 128) {
8500 SDValue V2 = DAG.getBitcast(MVT::v2i64, Vec);
8501 Lo = DAG.getBitcast(LoVT,
8502 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i64, V2,
8503 DAG.getConstant(0, SL, MVT::i32)));
8504 Hi = DAG.getBitcast(HiVT,
8505 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i64, V2,
8506 DAG.getConstant(1, SL, MVT::i32)));
8507 } else if (VecSize == 256) {
8508 SDValue V2 = DAG.getBitcast(MVT::v4i64, Vec);
8509 SDValue Parts[4];
8510 for (unsigned P = 0; P < 4; ++P) {
8511 Parts[P] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i64, V2,
8512 DAG.getConstant(P, SL, MVT::i32));
8513 }
8514
8515 Lo = DAG.getBitcast(LoVT, DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i64,
8516 Parts[0], Parts[1]));
8517 Hi = DAG.getBitcast(HiVT, DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i64,
8518 Parts[2], Parts[3]));
8519 } else {
8520 assert(VecSize == 512);
8521
8522 SDValue V2 = DAG.getBitcast(MVT::v8i64, Vec);
8523 SDValue Parts[8];
8524 for (unsigned P = 0; P < 8; ++P) {
8525 Parts[P] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i64, V2,
8526 DAG.getConstant(P, SL, MVT::i32));
8527 }
8528
8529 Lo = DAG.getBitcast(LoVT,
8530 DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v4i64,
8531 Parts[0], Parts[1], Parts[2], Parts[3]));
8532 Hi = DAG.getBitcast(HiVT,
8533 DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v4i64,
8534 Parts[4], Parts[5], Parts[6], Parts[7]));
8535 }
8536
8537 EVT IdxVT = Idx.getValueType();
8538 unsigned NElem = VecVT.getVectorNumElements();
8539 assert(isPowerOf2_32(NElem));
8540 SDValue IdxMask = DAG.getConstant(NElem / 2 - 1, SL, IdxVT);
8541 SDValue NewIdx = DAG.getNode(ISD::AND, SL, IdxVT, Idx, IdxMask);
8542 SDValue Half = DAG.getSelectCC(SL, Idx, IdxMask, Hi, Lo, ISD::SETUGT);
8543 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, Half, NewIdx);
8544 }
8545
8546 assert(VecSize <= 64);
8547
8548 MVT IntVT = MVT::getIntegerVT(VecSize);
8549
8550 // If Vec is just a SCALAR_TO_VECTOR, then use the scalar integer directly.
8551 SDValue VecBC = peekThroughBitcasts(Vec);
8552 if (VecBC.getOpcode() == ISD::SCALAR_TO_VECTOR) {
8553 SDValue Src = VecBC.getOperand(0);
8554 Src = DAG.getBitcast(Src.getValueType().changeTypeToInteger(), Src);
8555 Vec = DAG.getAnyExtOrTrunc(Src, SL, IntVT);
8556 }
8557
8558 unsigned EltSize = EltVT.getSizeInBits();
8559 assert(isPowerOf2_32(EltSize));
8560
8561 SDValue ScaleFactor = DAG.getConstant(Log2_32(EltSize), SL, MVT::i32);
8562
8563 // Convert vector index to bit-index (* EltSize)
8564 SDValue ScaledIdx = DAG.getNode(ISD::SHL, SL, MVT::i32, Idx, ScaleFactor);
8565
8566 SDValue BC = DAG.getNode(ISD::BITCAST, SL, IntVT, Vec);
8567 SDValue Elt = DAG.getNode(ISD::SRL, SL, IntVT, BC, ScaledIdx);
8568
8569 if (ResultVT == MVT::f16 || ResultVT == MVT::bf16) {
8570 SDValue Result = DAG.getNode(ISD::TRUNCATE, SL, MVT::i16, Elt);
8571 return DAG.getNode(ISD::BITCAST, SL, ResultVT, Result);
8572 }
8573
8574 return DAG.getAnyExtOrTrunc(Elt, SL, ResultVT);
8575}
8576
8577static bool elementPairIsContiguous(ArrayRef<int> Mask, int Elt) {
8578 assert(Elt % 2 == 0);
8579 return Mask[Elt + 1] == Mask[Elt] + 1 && (Mask[Elt] % 2 == 0);
8580}
8581
8582static bool elementPairIsOddToEven(ArrayRef<int> Mask, int Elt) {
8583 assert(Elt % 2 == 0);
8584 return Mask[Elt] >= 0 && Mask[Elt + 1] >= 0 && (Mask[Elt] & 1) &&
8585 !(Mask[Elt + 1] & 1);
8586}
8587
8588SDValue SITargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
8589 SelectionDAG &DAG) const {
8590 SDLoc SL(Op);
8591 EVT ResultVT = Op.getValueType();
8592 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
8593 MVT EltVT = ResultVT.getVectorElementType().getSimpleVT();
8594 const int NewSrcNumElts = 2;
8595 MVT PackVT = MVT::getVectorVT(EltVT, NewSrcNumElts);
8596 int SrcNumElts = Op.getOperand(0).getValueType().getVectorNumElements();
8597
8598 // Break up the shuffle into registers sized pieces.
8599 //
8600 // We're trying to form sub-shuffles that the register allocation pipeline
8601 // won't be able to figure out, like how to use v_pk_mov_b32 to do a register
8602 // blend or 16-bit op_sel. It should be able to figure out how to reassemble a
8603 // pair of copies into a consecutive register copy, so use the ordinary
8604 // extract_vector_elt lowering unless we can use the shuffle.
8605 //
8606 // TODO: This is a bit of hack, and we should probably always use
8607 // extract_subvector for the largest possible subvector we can (or at least
8608 // use it for PackVT aligned pieces). However we have worse support for
8609 // combines on them don't directly treat extract_subvector / insert_subvector
8610 // as legal. The DAG scheduler also ends up doing a worse job with the
8611 // extract_subvectors.
8612 const bool ShouldUseConsecutiveExtract = EltVT.getSizeInBits() == 16;
8613
8614 // vector_shuffle <0,1,6,7> lhs, rhs
8615 // -> concat_vectors (extract_subvector lhs, 0), (extract_subvector rhs, 2)
8616 //
8617 // vector_shuffle <6,7,2,3> lhs, rhs
8618 // -> concat_vectors (extract_subvector rhs, 2), (extract_subvector lhs, 2)
8619 //
8620 // vector_shuffle <6,7,0,1> lhs, rhs
8621 // -> concat_vectors (extract_subvector rhs, 2), (extract_subvector lhs, 0)
8622
8623 // Avoid scalarizing when both halves are reading from consecutive elements.
8624
8625 // If we're treating 2 element shuffles as legal, also create odd-to-even
8626 // shuffles of neighboring pairs.
8627 //
8628 // vector_shuffle <3,2,7,6> lhs, rhs
8629 // -> concat_vectors vector_shuffle <1, 0> (extract_subvector lhs, 0)
8630 // vector_shuffle <1, 0> (extract_subvector rhs, 2)
8631
8633 for (int I = 0, N = ResultVT.getVectorNumElements(); I != N; I += 2) {
8634 if (ShouldUseConsecutiveExtract &&
8636 const int Idx = SVN->getMaskElt(I);
8637 int VecIdx = Idx < SrcNumElts ? 0 : 1;
8638 int EltIdx = Idx < SrcNumElts ? Idx : Idx - SrcNumElts;
8639 SDValue SubVec = DAG.getNode(ISD::EXTRACT_SUBVECTOR, SL, PackVT,
8640 SVN->getOperand(VecIdx),
8641 DAG.getConstant(EltIdx, SL, MVT::i32));
8642 Pieces.push_back(SubVec);
8643 } else if (elementPairIsOddToEven(SVN->getMask(), I) &&
8645 int Idx0 = SVN->getMaskElt(I);
8646 int Idx1 = SVN->getMaskElt(I + 1);
8647
8648 SDValue SrcOp0 = SVN->getOperand(0);
8649 SDValue SrcOp1 = SrcOp0;
8650 if (Idx0 >= SrcNumElts) {
8651 SrcOp0 = SVN->getOperand(1);
8652 Idx0 -= SrcNumElts;
8653 }
8654
8655 if (Idx1 >= SrcNumElts) {
8656 SrcOp1 = SVN->getOperand(1);
8657 Idx1 -= SrcNumElts;
8658 }
8659
8660 int AlignedIdx0 = Idx0 & ~(NewSrcNumElts - 1);
8661 int AlignedIdx1 = Idx1 & ~(NewSrcNumElts - 1);
8662
8663 // Extract nearest even aligned piece.
8664 SDValue SubVec0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, SL, PackVT, SrcOp0,
8665 DAG.getConstant(AlignedIdx0, SL, MVT::i32));
8666 SDValue SubVec1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, SL, PackVT, SrcOp1,
8667 DAG.getConstant(AlignedIdx1, SL, MVT::i32));
8668
8669 int NewMaskIdx0 = Idx0 - AlignedIdx0;
8670 int NewMaskIdx1 = Idx1 - AlignedIdx1;
8671
8672 SDValue Result0 = SubVec0;
8673 SDValue Result1 = SubVec0;
8674
8675 if (SubVec0 != SubVec1) {
8676 NewMaskIdx1 += NewSrcNumElts;
8677 Result1 = SubVec1;
8678 } else {
8679 Result1 = DAG.getPOISON(PackVT);
8680 }
8681
8682 SDValue Shuf = DAG.getVectorShuffle(PackVT, SL, Result0, Result1,
8683 {NewMaskIdx0, NewMaskIdx1});
8684 Pieces.push_back(Shuf);
8685 } else {
8686 const int Idx0 = SVN->getMaskElt(I);
8687 const int Idx1 = SVN->getMaskElt(I + 1);
8688 int VecIdx0 = Idx0 < SrcNumElts ? 0 : 1;
8689 int VecIdx1 = Idx1 < SrcNumElts ? 0 : 1;
8690 int EltIdx0 = Idx0 < SrcNumElts ? Idx0 : Idx0 - SrcNumElts;
8691 int EltIdx1 = Idx1 < SrcNumElts ? Idx1 : Idx1 - SrcNumElts;
8692
8693 SDValue Vec0 = SVN->getOperand(VecIdx0);
8694 SDValue Elt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, Vec0,
8695 DAG.getSignedConstant(EltIdx0, SL, MVT::i32));
8696
8697 SDValue Vec1 = SVN->getOperand(VecIdx1);
8698 SDValue Elt1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, Vec1,
8699 DAG.getSignedConstant(EltIdx1, SL, MVT::i32));
8700 Pieces.push_back(DAG.getBuildVector(PackVT, SL, {Elt0, Elt1}));
8701 }
8702 }
8703
8704 return DAG.getNode(ISD::CONCAT_VECTORS, SL, ResultVT, Pieces);
8705}
8706
8707SDValue SITargetLowering::lowerSCALAR_TO_VECTOR(SDValue Op,
8708 SelectionDAG &DAG) const {
8709 SDValue SVal = Op.getOperand(0);
8710 EVT ResultVT = Op.getValueType();
8711 EVT SValVT = SVal.getValueType();
8712 SDValue UndefVal = DAG.getPOISON(SValVT);
8713 SDLoc SL(Op);
8714
8716 VElts.push_back(SVal);
8717 for (int I = 1, E = ResultVT.getVectorNumElements(); I < E; ++I)
8718 VElts.push_back(UndefVal);
8719
8720 return DAG.getBuildVector(ResultVT, SL, VElts);
8721}
8722
8723SDValue SITargetLowering::lowerBUILD_VECTOR(SDValue Op,
8724 SelectionDAG &DAG) const {
8725 SDLoc SL(Op);
8726 EVT VT = Op.getValueType();
8727
8728 if (VT == MVT::v2f16 || VT == MVT::v2i16 || VT == MVT::v2bf16) {
8729 assert(!Subtarget->hasVOP3PInsts() && "this should be legal");
8730
8731 SDValue Lo = Op.getOperand(0);
8732 SDValue Hi = Op.getOperand(1);
8733
8734 // Avoid adding defined bits with the zero_extend.
8735 if (Hi.isUndef()) {
8736 Lo = DAG.getNode(ISD::BITCAST, SL, MVT::i16, Lo);
8737 SDValue ExtLo = DAG.getNode(ISD::ANY_EXTEND, SL, MVT::i32, Lo);
8738 return DAG.getNode(ISD::BITCAST, SL, VT, ExtLo);
8739 }
8740
8741 Hi = DAG.getNode(ISD::BITCAST, SL, MVT::i16, Hi);
8742 Hi = DAG.getNode(ISD::ZERO_EXTEND, SL, MVT::i32, Hi);
8743
8744 SDValue ShlHi = DAG.getNode(ISD::SHL, SL, MVT::i32, Hi,
8745 DAG.getConstant(16, SL, MVT::i32));
8746 if (Lo.isUndef())
8747 return DAG.getNode(ISD::BITCAST, SL, VT, ShlHi);
8748
8749 Lo = DAG.getNode(ISD::BITCAST, SL, MVT::i16, Lo);
8750 Lo = DAG.getNode(ISD::ZERO_EXTEND, SL, MVT::i32, Lo);
8751
8752 SDValue Or =
8753 DAG.getNode(ISD::OR, SL, MVT::i32, Lo, ShlHi, SDNodeFlags::Disjoint);
8754 return DAG.getNode(ISD::BITCAST, SL, VT, Or);
8755 }
8756
8757 // Split into 2-element chunks.
8758 const unsigned NumParts = VT.getVectorNumElements() / 2;
8759 EVT PartVT = MVT::getVectorVT(VT.getVectorElementType().getSimpleVT(), 2);
8760 MVT PartIntVT = MVT::getIntegerVT(PartVT.getSizeInBits());
8761
8763 for (unsigned P = 0; P < NumParts; ++P) {
8764 SDValue Vec = DAG.getBuildVector(
8765 PartVT, SL, {Op.getOperand(P * 2), Op.getOperand(P * 2 + 1)});
8766 Casts.push_back(DAG.getNode(ISD::BITCAST, SL, PartIntVT, Vec));
8767 }
8768
8769 SDValue Blend =
8770 DAG.getBuildVector(MVT::getVectorVT(PartIntVT, NumParts), SL, Casts);
8771 return DAG.getNode(ISD::BITCAST, SL, VT, Blend);
8772}
8773
8775 const GlobalAddressSDNode *GA) const {
8776 // OSes that use ELF REL relocations (instead of RELA) can only store a
8777 // 32-bit addend in the instruction, so it is not safe to allow offset folding
8778 // which can create arbitrary 64-bit addends. (This is only a problem for
8779 // R_AMDGPU_*32_HI relocations since other relocation types are unaffected by
8780 // the high 32 bits of the addend.)
8781 //
8782 // This should be kept in sync with how HasRelocationAddend is initialized in
8783 // the constructor of ELFAMDGPUAsmBackend.
8784 if (!Subtarget->isAmdHsaOS())
8785 return false;
8786
8787 // We can fold offsets for anything that doesn't require a GOT relocation.
8788 return (GA->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS ||
8792}
8793
8794static SDValue
8796 const SDLoc &DL, int64_t Offset, EVT PtrVT,
8797 unsigned GAFlags = SIInstrInfo::MO_NONE) {
8798 assert(isInt<32>(Offset + 4) && "32-bit offset is expected!");
8799 // In order to support pc-relative addressing, the PC_ADD_REL_OFFSET SDNode is
8800 // lowered to the following code sequence:
8801 //
8802 // For constant address space:
8803 // s_getpc_b64 s[0:1]
8804 // s_add_u32 s0, s0, $symbol
8805 // s_addc_u32 s1, s1, 0
8806 //
8807 // s_getpc_b64 returns the address of the s_add_u32 instruction and then
8808 // a fixup or relocation is emitted to replace $symbol with a literal
8809 // constant, which is a pc-relative offset from the encoding of the $symbol
8810 // operand to the global variable.
8811 //
8812 // For global address space:
8813 // s_getpc_b64 s[0:1]
8814 // s_add_u32 s0, s0, $symbol@{gotpc}rel32@lo
8815 // s_addc_u32 s1, s1, $symbol@{gotpc}rel32@hi
8816 //
8817 // s_getpc_b64 returns the address of the s_add_u32 instruction and then
8818 // fixups or relocations are emitted to replace $symbol@*@lo and
8819 // $symbol@*@hi with lower 32 bits and higher 32 bits of a literal constant,
8820 // which is a 64-bit pc-relative offset from the encoding of the $symbol
8821 // operand to the global variable.
8822 if (((const GCNSubtarget &)DAG.getSubtarget()).has64BitLiterals()) {
8823 assert(GAFlags != SIInstrInfo::MO_NONE);
8824
8825 SDValue Ptr =
8826 DAG.getTargetGlobalAddress(GV, DL, MVT::i64, Offset, GAFlags + 2);
8827 return DAG.getNode(AMDGPUISD::PC_ADD_REL_OFFSET64, DL, PtrVT, Ptr);
8828 }
8829
8830 SDValue PtrLo = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, Offset, GAFlags);
8831 SDValue PtrHi;
8832 if (GAFlags == SIInstrInfo::MO_NONE)
8833 PtrHi = DAG.getTargetConstant(0, DL, MVT::i32);
8834 else
8835 PtrHi = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, Offset, GAFlags + 1);
8836 return DAG.getNode(AMDGPUISD::PC_ADD_REL_OFFSET, DL, PtrVT, PtrLo, PtrHi);
8837}
8838
8839SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
8840 SDValue Op,
8841 SelectionDAG &DAG) const {
8842 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
8843 SDLoc DL(GSD);
8844 EVT PtrVT = Op.getValueType();
8845
8846 const GlobalValue *GV = GSD->getGlobal();
8852 GV->hasExternalLinkage()) {
8853 Type *Ty = GV->getValueType();
8854 // HIP uses an unsized array `extern __shared__ T s[]` or similar
8855 // zero-sized type in other languages to declare the dynamic shared
8856 // memory which size is not known at the compile time. They will be
8857 // allocated by the runtime and placed directly after the static
8858 // allocated ones. They all share the same offset.
8859 if (DAG.getDataLayout().getTypeAllocSize(Ty).isZero()) {
8860 assert(PtrVT == MVT::i32 && "32-bit pointer is expected.");
8861 // Adjust alignment for that dynamic shared memory array.
8864 MFI->setUsesDynamicLDS(true);
8865 return SDValue(
8866 DAG.getMachineNode(AMDGPU::GET_GROUPSTATICSIZE, DL, PtrVT), 0);
8867 }
8868 }
8870 }
8871
8873 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, GSD->getOffset(),
8875 return DAG.getNode(AMDGPUISD::LDS, DL, MVT::i32, GA);
8876 }
8877
8878 if (Subtarget->isAmdPalOS() || Subtarget->isMesa3DOS()) {
8879 if (Subtarget->has64BitLiterals()) {
8881 GV, DL, MVT::i64, GSD->getOffset(), SIInstrInfo::MO_ABS64);
8882 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B64, DL, MVT::i64, Addr),
8883 0);
8884 }
8885
8886 SDValue AddrLo = DAG.getTargetGlobalAddress(
8887 GV, DL, MVT::i32, GSD->getOffset(), SIInstrInfo::MO_ABS32_LO);
8888 AddrLo = {DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, AddrLo), 0};
8889
8890 SDValue AddrHi = DAG.getTargetGlobalAddress(
8891 GV, DL, MVT::i32, GSD->getOffset(), SIInstrInfo::MO_ABS32_HI);
8892 AddrHi = {DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, AddrHi), 0};
8893
8894 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, AddrLo, AddrHi);
8895 }
8896
8897 if (shouldEmitFixup(GV))
8898 return buildPCRelGlobalAddress(DAG, GV, DL, GSD->getOffset(), PtrVT);
8899
8900 if (shouldEmitPCReloc(GV))
8901 return buildPCRelGlobalAddress(DAG, GV, DL, GSD->getOffset(), PtrVT,
8903
8904 SDValue GOTAddr = buildPCRelGlobalAddress(DAG, GV, DL, 0, PtrVT,
8906 PointerType *PtrTy =
8908 const DataLayout &DataLayout = DAG.getDataLayout();
8909 Align Alignment = DataLayout.getABITypeAlign(PtrTy);
8910 MachinePointerInfo PtrInfo =
8912
8913 return DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), GOTAddr, PtrInfo, Alignment,
8916}
8917
8919 const SDLoc &DL, SDValue V) const {
8920 // We can't use S_MOV_B32 directly, because there is no way to specify m0 as
8921 // the destination register.
8922 //
8923 // We can't use CopyToReg, because MachineCSE won't combine COPY instructions,
8924 // so we will end up with redundant moves to m0.
8925 //
8926 // We use a pseudo to ensure we emit s_mov_b32 with m0 as the direct result.
8927
8928 // A Null SDValue creates a glue result.
8929 SDNode *M0 = DAG.getMachineNode(AMDGPU::SI_INIT_M0, DL, MVT::Other, MVT::Glue,
8930 V, Chain);
8931 return SDValue(M0, 0);
8932}
8933
8934SDValue SITargetLowering::lowerImplicitZextParam(SelectionDAG &DAG, SDValue Op,
8935 MVT VT,
8936 unsigned Offset) const {
8937 SDLoc SL(Op);
8938 SDValue Param = lowerKernargMemParameter(
8939 DAG, MVT::i32, MVT::i32, SL, DAG.getEntryNode(), Offset, Align(4), false);
8940 // The local size values will have the hi 16-bits as zero.
8941 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, Param,
8942 DAG.getValueType(VT));
8943}
8944
8946 EVT VT) {
8949 "non-hsa intrinsic with hsa target", DL.getDebugLoc()));
8950 return DAG.getPOISON(VT);
8951}
8952
8954 EVT VT) {
8957 "intrinsic not supported on subtarget", DL.getDebugLoc()));
8958 return DAG.getPOISON(VT);
8959}
8960
8962 ArrayRef<SDValue> Elts) {
8963 assert(!Elts.empty());
8964 MVT Type;
8965 unsigned NumElts = Elts.size();
8966
8967 if (NumElts <= 12) {
8968 Type = MVT::getVectorVT(MVT::f32, NumElts);
8969 } else {
8970 assert(Elts.size() <= 16);
8971 Type = MVT::v16f32;
8972 NumElts = 16;
8973 }
8974
8975 SmallVector<SDValue, 16> VecElts(NumElts);
8976 for (unsigned i = 0; i < Elts.size(); ++i) {
8977 SDValue Elt = Elts[i];
8978 if (Elt.getValueType() != MVT::f32)
8979 Elt = DAG.getBitcast(MVT::f32, Elt);
8980 VecElts[i] = Elt;
8981 }
8982 for (unsigned i = Elts.size(); i < NumElts; ++i)
8983 VecElts[i] = DAG.getPOISON(MVT::f32);
8984
8985 if (NumElts == 1)
8986 return VecElts[0];
8987 return DAG.getBuildVector(Type, DL, VecElts);
8988}
8989
8990static SDValue padEltsToUndef(SelectionDAG &DAG, const SDLoc &DL, EVT CastVT,
8991 SDValue Src, int ExtraElts) {
8992 EVT SrcVT = Src.getValueType();
8993
8995
8996 if (SrcVT.isVector())
8997 DAG.ExtractVectorElements(Src, Elts);
8998 else
8999 Elts.push_back(Src);
9000
9001 SDValue Undef = DAG.getPOISON(SrcVT.getScalarType());
9002 while (ExtraElts--)
9003 Elts.push_back(Undef);
9004
9005 return DAG.getBuildVector(CastVT, DL, Elts);
9006}
9007
9008// Re-construct the required return value for a image load intrinsic.
9009// This is more complicated due to the optional use TexFailCtrl which means the
9010// required return type is an aggregate
9012 ArrayRef<EVT> ResultTypes, bool IsTexFail,
9013 bool Unpacked, bool IsD16, int DMaskPop,
9014 int NumVDataDwords, bool IsAtomicPacked16Bit,
9015 const SDLoc &DL) {
9016 // Determine the required return type. This is the same regardless of
9017 // IsTexFail flag
9018 EVT ReqRetVT = ResultTypes[0];
9019 int ReqRetNumElts = ReqRetVT.isVector() ? ReqRetVT.getVectorNumElements() : 1;
9020 int NumDataDwords = ((IsD16 && !Unpacked) || IsAtomicPacked16Bit)
9021 ? (ReqRetNumElts + 1) / 2
9022 : ReqRetNumElts;
9023
9024 int MaskPopDwords = (!IsD16 || Unpacked) ? DMaskPop : (DMaskPop + 1) / 2;
9025
9026 MVT DataDwordVT =
9027 NumDataDwords == 1 ? MVT::i32 : MVT::getVectorVT(MVT::i32, NumDataDwords);
9028
9029 MVT MaskPopVT =
9030 MaskPopDwords == 1 ? MVT::i32 : MVT::getVectorVT(MVT::i32, MaskPopDwords);
9031
9032 SDValue Data(Result, 0);
9033 SDValue TexFail;
9034
9035 if (DMaskPop > 0 && Data.getValueType() != MaskPopVT) {
9036 SDValue ZeroIdx = DAG.getConstant(0, DL, MVT::i32);
9037 if (MaskPopVT.isVector()) {
9038 Data = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MaskPopVT,
9039 SDValue(Result, 0), ZeroIdx);
9040 } else {
9041 Data = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MaskPopVT,
9042 SDValue(Result, 0), ZeroIdx);
9043 }
9044 }
9045
9046 if (DataDwordVT.isVector() && !IsAtomicPacked16Bit)
9047 Data = padEltsToUndef(DAG, DL, DataDwordVT, Data,
9048 NumDataDwords - MaskPopDwords);
9049
9050 if (IsD16)
9051 Data = adjustLoadValueTypeImpl(Data, ReqRetVT, DL, DAG, Unpacked);
9052
9053 EVT LegalReqRetVT = ReqRetVT;
9054 if (!ReqRetVT.isVector()) {
9055 if (!Data.getValueType().isInteger())
9056 Data = DAG.getNode(ISD::BITCAST, DL,
9057 Data.getValueType().changeTypeToInteger(), Data);
9058 Data = DAG.getNode(ISD::TRUNCATE, DL, ReqRetVT.changeTypeToInteger(), Data);
9059 } else {
9060 // We need to widen the return vector to a legal type
9061 if ((ReqRetVT.getVectorNumElements() % 2) == 1 &&
9062 ReqRetVT.getVectorElementType().getSizeInBits() == 16) {
9063 LegalReqRetVT =
9065 ReqRetVT.getVectorNumElements() + 1);
9066 }
9067 }
9068 Data = DAG.getNode(ISD::BITCAST, DL, LegalReqRetVT, Data);
9069
9070 if (IsTexFail) {
9071 TexFail =
9072 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, SDValue(Result, 0),
9073 DAG.getConstant(MaskPopDwords, DL, MVT::i32));
9074
9075 return DAG.getMergeValues({Data, TexFail, SDValue(Result, 1)}, DL);
9076 }
9077
9078 if (Result->getNumValues() == 1)
9079 return Data;
9080
9081 return DAG.getMergeValues({Data, SDValue(Result, 1)}, DL);
9082}
9083
9084static bool parseTexFail(SDValue TexFailCtrl, SelectionDAG &DAG, SDValue *TFE,
9085 SDValue *LWE, bool &IsTexFail) {
9086 auto *TexFailCtrlConst = cast<ConstantSDNode>(TexFailCtrl.getNode());
9087
9088 uint64_t Value = TexFailCtrlConst->getZExtValue();
9089 if (Value) {
9090 IsTexFail = true;
9091 }
9092
9093 SDLoc DL(TexFailCtrlConst);
9094 *TFE = DAG.getTargetConstant((Value & 0x1) ? 1 : 0, DL, MVT::i32);
9095 Value &= ~(uint64_t)0x1;
9096 *LWE = DAG.getTargetConstant((Value & 0x2) ? 1 : 0, DL, MVT::i32);
9097 Value &= ~(uint64_t)0x2;
9098
9099 return Value == 0;
9100}
9101
9103 MVT PackVectorVT,
9104 SmallVectorImpl<SDValue> &PackedAddrs,
9105 unsigned DimIdx, unsigned EndIdx,
9106 unsigned NumGradients) {
9107 SDLoc DL(Op);
9108 for (unsigned I = DimIdx; I < EndIdx; I++) {
9109 SDValue Addr = Op.getOperand(I);
9110
9111 // Gradients are packed with undef for each coordinate.
9112 // In <hi 16 bit>,<lo 16 bit> notation, the registers look like this:
9113 // 1D: undef,dx/dh; undef,dx/dv
9114 // 2D: dy/dh,dx/dh; dy/dv,dx/dv
9115 // 3D: dy/dh,dx/dh; undef,dz/dh; dy/dv,dx/dv; undef,dz/dv
9116 if (((I + 1) >= EndIdx) ||
9117 ((NumGradients / 2) % 2 == 1 && (I == DimIdx + (NumGradients / 2) - 1 ||
9118 I == DimIdx + NumGradients - 1))) {
9119 if (Addr.getValueType() != MVT::i16)
9120 Addr = DAG.getBitcast(MVT::i16, Addr);
9121 Addr = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, Addr);
9122 } else {
9123 Addr = DAG.getBuildVector(PackVectorVT, DL, {Addr, Op.getOperand(I + 1)});
9124 I++;
9125 }
9126 Addr = DAG.getBitcast(MVT::f32, Addr);
9127 PackedAddrs.push_back(Addr);
9128 }
9129}
9130
9131SDValue SITargetLowering::lowerImage(SDValue Op,
9133 SelectionDAG &DAG, bool WithChain) const {
9134 SDLoc DL(Op);
9135 MachineFunction &MF = DAG.getMachineFunction();
9136 const GCNSubtarget *ST = &MF.getSubtarget<GCNSubtarget>();
9137 const AMDGPU::MIMGBaseOpcodeInfo *BaseOpcode =
9139 const AMDGPU::MIMGDimInfo *DimInfo = AMDGPU::getMIMGDimInfo(Intr->Dim);
9140 unsigned IntrOpcode = Intr->BaseOpcode;
9141 bool IsGFX10Plus = AMDGPU::isGFX10Plus(*Subtarget);
9142 bool IsGFX11Plus = AMDGPU::isGFX11Plus(*Subtarget);
9143 bool IsGFX12Plus = AMDGPU::isGFX12Plus(*Subtarget);
9144
9145 SmallVector<EVT, 3> ResultTypes(Op->values());
9146 SmallVector<EVT, 3> OrigResultTypes(Op->values());
9147 bool IsD16 = false;
9148 bool IsG16 = false;
9149 bool IsA16 = false;
9150 SDValue VData;
9151 int NumVDataDwords = 0;
9152 bool AdjustRetType = false;
9153 bool IsAtomicPacked16Bit = false;
9154
9155 // Offset of intrinsic arguments
9156 const unsigned ArgOffset = WithChain ? 2 : 1;
9157
9158 unsigned DMask;
9159 unsigned DMaskLanes = 0;
9160
9161 if (BaseOpcode->Atomic) {
9162 VData = Op.getOperand(2);
9163
9164 IsAtomicPacked16Bit =
9165 (Intr->BaseOpcode == AMDGPU::IMAGE_ATOMIC_PK_ADD_F16 ||
9166 Intr->BaseOpcode == AMDGPU::IMAGE_ATOMIC_PK_ADD_BF16);
9167
9168 bool Is64Bit = VData.getValueSizeInBits() == 64;
9169 if (BaseOpcode->AtomicX2) {
9170 SDValue VData2 = Op.getOperand(3);
9171 VData = DAG.getBuildVector(Is64Bit ? MVT::v2i64 : MVT::v2i32, DL,
9172 {VData, VData2});
9173 if (Is64Bit)
9174 VData = DAG.getBitcast(MVT::v4i32, VData);
9175
9176 ResultTypes[0] = Is64Bit ? MVT::v2i64 : MVT::v2i32;
9177 DMask = Is64Bit ? 0xf : 0x3;
9178 NumVDataDwords = Is64Bit ? 4 : 2;
9179 } else {
9180 DMask = Is64Bit ? 0x3 : 0x1;
9181 NumVDataDwords = Is64Bit ? 2 : 1;
9182 }
9183 } else {
9184 DMask = Op->getConstantOperandVal(ArgOffset + Intr->DMaskIndex);
9185 DMaskLanes = BaseOpcode->Gather4 ? 4 : llvm::popcount(DMask);
9186
9187 if (BaseOpcode->Store) {
9188 VData = Op.getOperand(2);
9189
9190 MVT StoreVT = VData.getSimpleValueType();
9191 if (StoreVT.getScalarType() == MVT::f16) {
9192 if (!Subtarget->hasD16Images() || !BaseOpcode->HasD16)
9193 return Op; // D16 is unsupported for this instruction
9194
9195 IsD16 = true;
9196 VData = handleD16VData(VData, DAG, true);
9197 }
9198
9199 NumVDataDwords = (VData.getValueType().getSizeInBits() + 31) / 32;
9200 } else if (!BaseOpcode->NoReturn) {
9201 // Work out the num dwords based on the dmask popcount and underlying type
9202 // and whether packing is supported.
9203 MVT LoadVT = ResultTypes[0].getSimpleVT();
9204 if (LoadVT.getScalarType() == MVT::f16) {
9205 if (!Subtarget->hasD16Images() || !BaseOpcode->HasD16)
9206 return Op; // D16 is unsupported for this instruction
9207
9208 IsD16 = true;
9209 }
9210
9211 // Confirm that the return type is large enough for the dmask specified
9212 if ((LoadVT.isVector() && LoadVT.getVectorNumElements() < DMaskLanes) ||
9213 (!LoadVT.isVector() && DMaskLanes > 1))
9214 return Op;
9215
9216 // The sq block of gfx8 and gfx9 do not estimate register use correctly
9217 // for d16 image_gather4, image_gather4_l, and image_gather4_lz
9218 // instructions.
9219 if (IsD16 && !Subtarget->hasUnpackedD16VMem() &&
9220 !(BaseOpcode->Gather4 && Subtarget->hasImageGather4D16Bug()))
9221 NumVDataDwords = (DMaskLanes + 1) / 2;
9222 else
9223 NumVDataDwords = DMaskLanes;
9224
9225 AdjustRetType = true;
9226 }
9227 }
9228
9229 unsigned VAddrEnd = ArgOffset + Intr->VAddrEnd;
9231
9232 // Check for 16 bit addresses or derivatives and pack if true.
9233 MVT VAddrVT =
9234 Op.getOperand(ArgOffset + Intr->GradientStart).getSimpleValueType();
9235 MVT VAddrScalarVT = VAddrVT.getScalarType();
9236 MVT GradPackVectorVT = VAddrScalarVT == MVT::f16 ? MVT::v2f16 : MVT::v2i16;
9237 IsG16 = VAddrScalarVT == MVT::f16 || VAddrScalarVT == MVT::i16;
9238
9239 VAddrVT = Op.getOperand(ArgOffset + Intr->CoordStart).getSimpleValueType();
9240 VAddrScalarVT = VAddrVT.getScalarType();
9241 MVT AddrPackVectorVT = VAddrScalarVT == MVT::f16 ? MVT::v2f16 : MVT::v2i16;
9242 IsA16 = VAddrScalarVT == MVT::f16 || VAddrScalarVT == MVT::i16;
9243
9244 // Push back extra arguments.
9245 for (unsigned I = Intr->VAddrStart; I < Intr->GradientStart; I++) {
9246 if (IsA16 && (Op.getOperand(ArgOffset + I).getValueType() == MVT::f16)) {
9247 assert(I == Intr->BiasIndex && "Got unexpected 16-bit extra argument");
9248 // Special handling of bias when A16 is on. Bias is of type half but
9249 // occupies full 32-bit.
9250 SDValue Bias = DAG.getBuildVector(
9251 MVT::v2f16, DL,
9252 {Op.getOperand(ArgOffset + I), DAG.getPOISON(MVT::f16)});
9253 VAddrs.push_back(Bias);
9254 } else {
9255 assert((!IsA16 || Intr->NumBiasArgs == 0 || I != Intr->BiasIndex) &&
9256 "Bias needs to be converted to 16 bit in A16 mode");
9257 VAddrs.push_back(Op.getOperand(ArgOffset + I));
9258 }
9259 }
9260
9261 if (BaseOpcode->Gradients && !ST->hasG16() && (IsA16 != IsG16)) {
9262 // 16 bit gradients are supported, but are tied to the A16 control
9263 // so both gradients and addresses must be 16 bit
9264 LLVM_DEBUG(
9265 dbgs() << "Failed to lower image intrinsic: 16 bit addresses "
9266 "require 16 bit args for both gradients and addresses");
9267 return Op;
9268 }
9269
9270 if (IsA16) {
9271 if (!ST->hasA16()) {
9272 LLVM_DEBUG(dbgs() << "Failed to lower image intrinsic: Target does not "
9273 "support 16 bit addresses\n");
9274 return Op;
9275 }
9276 }
9277
9278 // We've dealt with incorrect input so we know that if IsA16, IsG16
9279 // are set then we have to compress/pack operands (either address,
9280 // gradient or both)
9281 // In the case where a16 and gradients are tied (no G16 support) then we
9282 // have already verified that both IsA16 and IsG16 are true
9283 if (BaseOpcode->Gradients && IsG16 && ST->hasG16()) {
9284 // Activate g16
9285 const AMDGPU::MIMGG16MappingInfo *G16MappingInfo =
9287 IntrOpcode = G16MappingInfo->G16; // set new opcode to variant with _g16
9288 }
9289
9290 // Add gradients (packed or unpacked)
9291 if (IsG16) {
9292 // Pack the gradients
9293 // const int PackEndIdx = IsA16 ? VAddrEnd : (ArgOffset + Intr->CoordStart);
9294 packImage16bitOpsToDwords(DAG, Op, GradPackVectorVT, VAddrs,
9295 ArgOffset + Intr->GradientStart,
9296 ArgOffset + Intr->CoordStart, Intr->NumGradients);
9297 } else {
9298 for (unsigned I = ArgOffset + Intr->GradientStart;
9299 I < ArgOffset + Intr->CoordStart; I++)
9300 VAddrs.push_back(Op.getOperand(I));
9301 }
9302
9303 // Add addresses (packed or unpacked)
9304 if (IsA16) {
9305 packImage16bitOpsToDwords(DAG, Op, AddrPackVectorVT, VAddrs,
9306 ArgOffset + Intr->CoordStart, VAddrEnd,
9307 0 /* No gradients */);
9308 } else {
9309 // Add uncompressed address
9310 for (unsigned I = ArgOffset + Intr->CoordStart; I < VAddrEnd; I++)
9311 VAddrs.push_back(Op.getOperand(I));
9312 }
9313
9314 // If the register allocator cannot place the address registers contiguously
9315 // without introducing moves, then using the non-sequential address encoding
9316 // is always preferable, since it saves VALU instructions and is usually a
9317 // wash in terms of code size or even better.
9318 //
9319 // However, we currently have no way of hinting to the register allocator that
9320 // MIMG addresses should be placed contiguously when it is possible to do so,
9321 // so force non-NSA for the common 2-address case as a heuristic.
9322 //
9323 // SIShrinkInstructions will convert NSA encodings to non-NSA after register
9324 // allocation when possible.
9325 //
9326 // Partial NSA is allowed on GFX11+ where the final register is a contiguous
9327 // set of the remaining addresses.
9328 const unsigned NSAMaxSize = ST->getNSAMaxSize(BaseOpcode->Sampler);
9329 const bool HasPartialNSAEncoding = ST->hasPartialNSAEncoding();
9330 const bool UseNSA = ST->hasNSAEncoding() &&
9331 VAddrs.size() >= ST->getNSAThreshold(MF) &&
9332 (VAddrs.size() <= NSAMaxSize || HasPartialNSAEncoding);
9333 const bool UsePartialNSA =
9334 UseNSA && HasPartialNSAEncoding && VAddrs.size() > NSAMaxSize;
9335
9336 SDValue VAddr;
9337 if (UsePartialNSA) {
9338 VAddr = getBuildDwordsVector(DAG, DL,
9339 ArrayRef(VAddrs).drop_front(NSAMaxSize - 1));
9340 } else if (!UseNSA) {
9341 VAddr = getBuildDwordsVector(DAG, DL, VAddrs);
9342 }
9343
9344 SDValue True = DAG.getTargetConstant(1, DL, MVT::i1);
9345 SDValue False = DAG.getTargetConstant(0, DL, MVT::i1);
9346 SDValue Unorm;
9347 if (!BaseOpcode->Sampler) {
9348 Unorm = True;
9349 } else {
9350 uint64_t UnormConst =
9351 Op.getConstantOperandVal(ArgOffset + Intr->UnormIndex);
9352
9353 Unorm = UnormConst ? True : False;
9354 }
9355
9356 SDValue TFE;
9357 SDValue LWE;
9358 SDValue TexFail = Op.getOperand(ArgOffset + Intr->TexFailCtrlIndex);
9359 bool IsTexFail = false;
9360 if (!parseTexFail(TexFail, DAG, &TFE, &LWE, IsTexFail))
9361 return Op;
9362
9363 if (IsTexFail) {
9364 if (!DMaskLanes) {
9365 // Expecting to get an error flag since TFC is on - and dmask is 0
9366 // Force dmask to be at least 1 otherwise the instruction will fail
9367 DMask = 0x1;
9368 DMaskLanes = 1;
9369 NumVDataDwords = 1;
9370 }
9371 NumVDataDwords += 1;
9372 AdjustRetType = true;
9373 }
9374
9375 // Has something earlier tagged that the return type needs adjusting
9376 // This happens if the instruction is a load or has set TexFailCtrl flags
9377 if (AdjustRetType) {
9378 // NumVDataDwords reflects the true number of dwords required in the return
9379 // type
9380 if (DMaskLanes == 0 && !BaseOpcode->Store) {
9381 // This is a no-op load. This can be eliminated
9382 SDValue Undef = DAG.getPOISON(Op.getValueType());
9383 if (isa<MemSDNode>(Op))
9384 return DAG.getMergeValues({Undef, Op.getOperand(0)}, DL);
9385 return Undef;
9386 }
9387
9388 EVT NewVT = NumVDataDwords > 1 ? EVT::getVectorVT(*DAG.getContext(),
9389 MVT::i32, NumVDataDwords)
9390 : MVT::i32;
9391
9392 ResultTypes[0] = NewVT;
9393 if (ResultTypes.size() == 3) {
9394 // Original result was aggregate type used for TexFailCtrl results
9395 // The actual instruction returns as a vector type which has now been
9396 // created. Remove the aggregate result.
9397 ResultTypes.erase(&ResultTypes[1]);
9398 }
9399 }
9400
9401 unsigned CPol = Op.getConstantOperandVal(ArgOffset + Intr->CachePolicyIndex);
9402 if (BaseOpcode->Atomic)
9403 CPol |= AMDGPU::CPol::GLC; // TODO no-return optimization
9404 if (CPol & ~((IsGFX12Plus ? AMDGPU::CPol::ALL : AMDGPU::CPol::ALL_pregfx12) |
9406 return Op;
9407
9409 if (BaseOpcode->Store || BaseOpcode->Atomic)
9410 Ops.push_back(VData); // vdata
9411 if (UsePartialNSA) {
9412 append_range(Ops, ArrayRef(VAddrs).take_front(NSAMaxSize - 1));
9413 Ops.push_back(VAddr);
9414 } else if (UseNSA)
9415 append_range(Ops, VAddrs);
9416 else
9417 Ops.push_back(VAddr);
9418 SDValue Rsrc = Op.getOperand(ArgOffset + Intr->RsrcIndex);
9419 EVT RsrcVT = Rsrc.getValueType();
9420 if (RsrcVT != MVT::v4i32 && RsrcVT != MVT::v8i32)
9421 return Op;
9422 Ops.push_back(Rsrc);
9423 if (BaseOpcode->Sampler) {
9424 SDValue Samp = Op.getOperand(ArgOffset + Intr->SampIndex);
9425 if (Samp.getValueType() != MVT::v4i32)
9426 return Op;
9427 Ops.push_back(Samp);
9428 }
9429 Ops.push_back(DAG.getTargetConstant(DMask, DL, MVT::i32));
9430 if (IsGFX10Plus)
9431 Ops.push_back(DAG.getTargetConstant(DimInfo->Encoding, DL, MVT::i32));
9432 if (!IsGFX12Plus || BaseOpcode->Sampler || BaseOpcode->MSAA)
9433 Ops.push_back(Unorm);
9434 Ops.push_back(DAG.getTargetConstant(CPol, DL, MVT::i32));
9435 Ops.push_back(IsA16 && // r128, a16 for gfx9
9436 ST->hasFeature(AMDGPU::FeatureR128A16)
9437 ? True
9438 : False);
9439 if (IsGFX10Plus)
9440 Ops.push_back(IsA16 ? True : False);
9441
9442 if (!Subtarget->hasGFX90AInsts())
9443 Ops.push_back(TFE); // tfe
9444 else if (TFE->getAsZExtVal()) {
9445 DAG.getContext()->diagnose(DiagnosticInfoUnsupported(
9447 "TFE is not supported on this GPU", DL.getDebugLoc()));
9448 }
9449
9450 if (!IsGFX12Plus || BaseOpcode->Sampler || BaseOpcode->MSAA)
9451 Ops.push_back(LWE); // lwe
9452 if (!IsGFX10Plus)
9453 Ops.push_back(DimInfo->DA ? True : False);
9454 if (BaseOpcode->HasD16)
9455 Ops.push_back(IsD16 ? True : False);
9456 if (isa<MemSDNode>(Op))
9457 Ops.push_back(Op.getOperand(0)); // chain
9458
9459 int NumVAddrDwords =
9460 UseNSA ? VAddrs.size() : VAddr.getValueType().getSizeInBits() / 32;
9461 int Opcode = -1;
9462
9463 if (IsGFX12Plus) {
9464 Opcode = AMDGPU::getMIMGOpcode(IntrOpcode, AMDGPU::MIMGEncGfx12,
9465 NumVDataDwords, NumVAddrDwords);
9466 } else if (IsGFX11Plus) {
9467 Opcode = AMDGPU::getMIMGOpcode(IntrOpcode,
9468 UseNSA ? AMDGPU::MIMGEncGfx11NSA
9469 : AMDGPU::MIMGEncGfx11Default,
9470 NumVDataDwords, NumVAddrDwords);
9471 } else if (IsGFX10Plus) {
9472 Opcode = AMDGPU::getMIMGOpcode(IntrOpcode,
9473 UseNSA ? AMDGPU::MIMGEncGfx10NSA
9474 : AMDGPU::MIMGEncGfx10Default,
9475 NumVDataDwords, NumVAddrDwords);
9476 } else {
9477 if (Subtarget->hasGFX90AInsts()) {
9478 Opcode = AMDGPU::getMIMGOpcode(IntrOpcode, AMDGPU::MIMGEncGfx90a,
9479 NumVDataDwords, NumVAddrDwords);
9480 if (Opcode == -1) {
9481 DAG.getContext()->diagnose(DiagnosticInfoUnsupported(
9483 "requested image instruction is not supported on this GPU",
9484 DL.getDebugLoc()));
9485
9486 unsigned Idx = 0;
9487 SmallVector<SDValue, 3> RetValues(OrigResultTypes.size());
9488 for (EVT VT : OrigResultTypes) {
9489 if (VT == MVT::Other)
9490 RetValues[Idx++] = Op.getOperand(0); // Chain
9491 else
9492 RetValues[Idx++] = DAG.getPOISON(VT);
9493 }
9494
9495 return DAG.getMergeValues(RetValues, DL);
9496 }
9497 }
9498 if (Opcode == -1 &&
9499 Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
9500 Opcode = AMDGPU::getMIMGOpcode(IntrOpcode, AMDGPU::MIMGEncGfx8,
9501 NumVDataDwords, NumVAddrDwords);
9502 if (Opcode == -1)
9503 Opcode = AMDGPU::getMIMGOpcode(IntrOpcode, AMDGPU::MIMGEncGfx6,
9504 NumVDataDwords, NumVAddrDwords);
9505 }
9506 if (Opcode == -1)
9507 return Op;
9508
9509 MachineSDNode *NewNode = DAG.getMachineNode(Opcode, DL, ResultTypes, Ops);
9510 if (auto *MemOp = dyn_cast<MemSDNode>(Op)) {
9511 MachineMemOperand *MemRef = MemOp->getMemOperand();
9512 DAG.setNodeMemRefs(NewNode, {MemRef});
9513 }
9514
9515 if (BaseOpcode->AtomicX2) {
9517 DAG.ExtractVectorElements(SDValue(NewNode, 0), Elt, 0, 1);
9518 return DAG.getMergeValues({Elt[0], SDValue(NewNode, 1)}, DL);
9519 }
9520 if (BaseOpcode->NoReturn)
9521 return SDValue(NewNode, 0);
9522 return constructRetValue(DAG, NewNode, OrigResultTypes, IsTexFail,
9523 Subtarget->hasUnpackedD16VMem(), IsD16, DMaskLanes,
9524 NumVDataDwords, IsAtomicPacked16Bit, DL);
9525}
9526
9527SDValue SITargetLowering::lowerSBuffer(EVT VT, SDLoc DL, SDValue Rsrc,
9528 SDValue Offset, SDValue CachePolicy,
9529 SelectionDAG &DAG) const {
9530 MachineFunction &MF = DAG.getMachineFunction();
9531
9532 const DataLayout &DataLayout = DAG.getDataLayout();
9533 Align Alignment =
9534 DataLayout.getABITypeAlign(VT.getTypeForEVT(*DAG.getContext()));
9535
9536 MachineMemOperand *MMO = MF.getMachineMemOperand(
9537 MachinePointerInfo(),
9540 VT.getStoreSize(), Alignment);
9541
9542 if (!Offset->isDivergent()) {
9543 SDValue Ops[] = {Rsrc, Offset, CachePolicy};
9544
9545 // Lower llvm.amdgcn.s.buffer.load.{i16, u16} intrinsics. Initially, the
9546 // s_buffer_load_u16 instruction is emitted for both signed and unsigned
9547 // loads. Later, DAG combiner tries to combine s_buffer_load_u16 with sext
9548 // and generates s_buffer_load_i16 (performSignExtendInRegCombine).
9549 if (VT == MVT::i16 && Subtarget->hasScalarSubwordLoads()) {
9550 SDValue BufferLoad =
9552 DAG.getVTList(MVT::i32), Ops, VT, MMO);
9553 return DAG.getNode(ISD::TRUNCATE, DL, VT, BufferLoad);
9554 }
9555
9556 // Widen vec3 load to vec4.
9557 if (VT.isVector() && VT.getVectorNumElements() == 3 &&
9558 !Subtarget->hasScalarDwordx3Loads()) {
9559 EVT WidenedVT =
9561 auto WidenedOp = DAG.getMemIntrinsicNode(
9562 AMDGPUISD::SBUFFER_LOAD, DL, DAG.getVTList(WidenedVT), Ops, WidenedVT,
9563 MF.getMachineMemOperand(MMO, 0, WidenedVT.getStoreSize()));
9564 auto Subvector = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, WidenedOp,
9565 DAG.getVectorIdxConstant(0, DL));
9566 return Subvector;
9567 }
9568
9570 DAG.getVTList(VT), Ops, VT, MMO);
9571 }
9572
9573 // We have a divergent offset. Emit a MUBUF buffer load instead. We can
9574 // assume that the buffer is unswizzled.
9575 SDValue Ops[] = {
9576 DAG.getEntryNode(), // Chain
9577 Rsrc, // rsrc
9578 DAG.getConstant(0, DL, MVT::i32), // vindex
9579 {}, // voffset
9580 {}, // soffset
9581 {}, // offset
9582 CachePolicy, // cachepolicy
9583 DAG.getTargetConstant(0, DL, MVT::i1), // idxen
9584 };
9585 if (VT == MVT::i16 && Subtarget->hasScalarSubwordLoads()) {
9586 setBufferOffsets(Offset, DAG, &Ops[3], Align(4));
9587 return handleByteShortBufferLoads(DAG, VT, DL, Ops, MMO);
9588 }
9589
9591 unsigned NumLoads = 1;
9592 MVT LoadVT = VT.getSimpleVT();
9593 unsigned NumElts = LoadVT.isVector() ? LoadVT.getVectorNumElements() : 1;
9594 assert((LoadVT.getScalarType() == MVT::i32 ||
9595 LoadVT.getScalarType() == MVT::f32));
9596
9597 if (NumElts == 8 || NumElts == 16) {
9598 NumLoads = NumElts / 4;
9599 LoadVT = MVT::getVectorVT(LoadVT.getScalarType(), 4);
9600 }
9601
9602 SDVTList VTList = DAG.getVTList({LoadVT, MVT::Other});
9603
9604 // Use the alignment to ensure that the required offsets will fit into the
9605 // immediate offsets.
9606 setBufferOffsets(Offset, DAG, &Ops[3],
9607 NumLoads > 1 ? Align(16 * NumLoads) : Align(4));
9608
9609 uint64_t InstOffset = Ops[5]->getAsZExtVal();
9610 for (unsigned i = 0; i < NumLoads; ++i) {
9611 Ops[5] = DAG.getTargetConstant(InstOffset + 16 * i, DL, MVT::i32);
9612 Loads.push_back(getMemIntrinsicNode(AMDGPUISD::BUFFER_LOAD, DL, VTList, Ops,
9613 LoadVT, MMO, DAG));
9614 }
9615
9616 if (NumElts == 8 || NumElts == 16)
9617 return DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, Loads);
9618
9619 return Loads[0];
9620}
9621
9622SDValue SITargetLowering::lowerWaveID(SelectionDAG &DAG, SDValue Op) const {
9623 // With architected SGPRs, waveIDinGroup is in TTMP8[29:25].
9624 if (!Subtarget->hasArchitectedSGPRs())
9625 return {};
9626 SDLoc SL(Op);
9627 MVT VT = MVT::i32;
9628 SDValue TTMP8 = DAG.getCopyFromReg(DAG.getEntryNode(), SL, AMDGPU::TTMP8, VT);
9629 return DAG.getNode(AMDGPUISD::BFE_U32, SL, VT, TTMP8,
9630 DAG.getConstant(25, SL, VT), DAG.getConstant(5, SL, VT));
9631}
9632
9633SDValue SITargetLowering::lowerConstHwRegRead(SelectionDAG &DAG, SDValue Op,
9634 AMDGPU::Hwreg::Id HwReg,
9635 unsigned LowBit,
9636 unsigned Width) const {
9637 SDLoc SL(Op);
9638 using namespace AMDGPU::Hwreg;
9639 return {DAG.getMachineNode(
9640 AMDGPU::S_GETREG_B32_const, SL, MVT::i32,
9641 DAG.getTargetConstant(HwregEncoding::encode(HwReg, LowBit, Width),
9642 SL, MVT::i32)),
9643 0};
9644}
9645
9646SDValue SITargetLowering::lowerWorkitemID(SelectionDAG &DAG, SDValue Op,
9647 unsigned Dim,
9648 const ArgDescriptor &Arg) const {
9649 SDLoc SL(Op);
9650 MachineFunction &MF = DAG.getMachineFunction();
9651 unsigned MaxID = Subtarget->getMaxWorkitemID(MF.getFunction(), Dim);
9652 if (MaxID == 0)
9653 return DAG.getConstant(0, SL, MVT::i32);
9654
9655 // It's undefined behavior if a function marked with the amdgpu-no-*
9656 // attributes uses the corresponding intrinsic.
9657 if (!Arg)
9658 return DAG.getPOISON(Op->getValueType(0));
9659
9660 SDValue Val = loadInputValue(DAG, &AMDGPU::VGPR_32RegClass, MVT::i32,
9661 SDLoc(DAG.getEntryNode()), Arg);
9662
9663 // Don't bother inserting AssertZext for packed IDs since we're emitting the
9664 // masking operations anyway.
9665 //
9666 // TODO: We could assert the top bit is 0 for the source copy.
9667 if (Arg.isMasked())
9668 return Val;
9669
9670 // Preserve the known bits after expansion to a copy.
9671 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), llvm::bit_width(MaxID));
9672 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, Val,
9673 DAG.getValueType(SmallVT));
9674}
9675
9676SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
9677 SelectionDAG &DAG) const {
9678 MachineFunction &MF = DAG.getMachineFunction();
9679 auto *MFI = MF.getInfo<SIMachineFunctionInfo>();
9680
9681 EVT VT = Op.getValueType();
9682 SDLoc DL(Op);
9683 unsigned IntrinsicID = Op.getConstantOperandVal(0);
9684
9685 // TODO: Should this propagate fast-math-flags?
9686
9687 switch (IntrinsicID) {
9688 case Intrinsic::amdgcn_implicit_buffer_ptr: {
9689 if (getSubtarget()->isAmdHsaOrMesa(MF.getFunction()))
9690 return emitNonHSAIntrinsicError(DAG, DL, VT);
9691 return getPreloadedValue(DAG, *MFI, VT,
9693 }
9694 case Intrinsic::amdgcn_dispatch_ptr:
9695 case Intrinsic::amdgcn_queue_ptr: {
9696 if (!Subtarget->isAmdHsaOrMesa(MF.getFunction())) {
9697 DAG.getContext()->diagnose(DiagnosticInfoUnsupported(
9698 MF.getFunction(), "unsupported hsa intrinsic without hsa target",
9699 DL.getDebugLoc()));
9700 return DAG.getPOISON(VT);
9701 }
9702
9703 auto RegID = IntrinsicID == Intrinsic::amdgcn_dispatch_ptr
9706 return getPreloadedValue(DAG, *MFI, VT, RegID);
9707 }
9708 case Intrinsic::amdgcn_implicitarg_ptr: {
9709 if (MFI->isEntryFunction())
9710 return getImplicitArgPtr(DAG, DL);
9711 return getPreloadedValue(DAG, *MFI, VT,
9713 }
9714 case Intrinsic::amdgcn_kernarg_segment_ptr: {
9716 // This only makes sense to call in a kernel, so just lower to null.
9717 return DAG.getConstant(0, DL, VT);
9718 }
9719
9720 return getPreloadedValue(DAG, *MFI, VT,
9722 }
9723 case Intrinsic::amdgcn_dispatch_id: {
9724 return getPreloadedValue(DAG, *MFI, VT, AMDGPUFunctionArgInfo::DISPATCH_ID);
9725 }
9726 case Intrinsic::amdgcn_rcp:
9727 return DAG.getNode(AMDGPUISD::RCP, DL, VT, Op.getOperand(1));
9728 case Intrinsic::amdgcn_rsq:
9729 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
9730 case Intrinsic::amdgcn_rsq_legacy:
9731 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
9732 return emitRemovedIntrinsicError(DAG, DL, VT);
9733 return SDValue();
9734 case Intrinsic::amdgcn_rcp_legacy:
9735 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
9736 return emitRemovedIntrinsicError(DAG, DL, VT);
9737 return DAG.getNode(AMDGPUISD::RCP_LEGACY, DL, VT, Op.getOperand(1));
9738 case Intrinsic::amdgcn_rsq_clamp: {
9739 if (Subtarget->getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
9740 return DAG.getNode(AMDGPUISD::RSQ_CLAMP, DL, VT, Op.getOperand(1));
9741
9742 Type *Type = VT.getTypeForEVT(*DAG.getContext());
9743 APFloat Max = APFloat::getLargest(Type->getFltSemantics());
9744 APFloat Min = APFloat::getLargest(Type->getFltSemantics(), true);
9745
9746 SDValue Rsq = DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
9747 SDValue Tmp =
9748 DAG.getNode(ISD::FMINNUM, DL, VT, Rsq, DAG.getConstantFP(Max, DL, VT));
9749 return DAG.getNode(ISD::FMAXNUM, DL, VT, Tmp,
9750 DAG.getConstantFP(Min, DL, VT));
9751 }
9752 case Intrinsic::r600_read_ngroups_x:
9753 if (Subtarget->isAmdHsaOS())
9754 return emitNonHSAIntrinsicError(DAG, DL, VT);
9755
9756 return lowerKernargMemParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
9758 false);
9759 case Intrinsic::r600_read_ngroups_y:
9760 if (Subtarget->isAmdHsaOS())
9761 return emitNonHSAIntrinsicError(DAG, DL, VT);
9762
9763 return lowerKernargMemParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
9765 false);
9766 case Intrinsic::r600_read_ngroups_z:
9767 if (Subtarget->isAmdHsaOS())
9768 return emitNonHSAIntrinsicError(DAG, DL, VT);
9769
9770 return lowerKernargMemParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
9772 false);
9773 case Intrinsic::r600_read_local_size_x:
9774 if (Subtarget->isAmdHsaOS())
9775 return emitNonHSAIntrinsicError(DAG, DL, VT);
9776
9777 return lowerImplicitZextParam(DAG, Op, MVT::i16,
9779 case Intrinsic::r600_read_local_size_y:
9780 if (Subtarget->isAmdHsaOS())
9781 return emitNonHSAIntrinsicError(DAG, DL, VT);
9782
9783 return lowerImplicitZextParam(DAG, Op, MVT::i16,
9785 case Intrinsic::r600_read_local_size_z:
9786 if (Subtarget->isAmdHsaOS())
9787 return emitNonHSAIntrinsicError(DAG, DL, VT);
9788
9789 return lowerImplicitZextParam(DAG, Op, MVT::i16,
9791 case Intrinsic::amdgcn_workgroup_id_x:
9792 return lowerWorkGroupId(DAG, *MFI, VT,
9796 case Intrinsic::amdgcn_workgroup_id_y:
9797 return lowerWorkGroupId(DAG, *MFI, VT,
9801 case Intrinsic::amdgcn_workgroup_id_z:
9802 return lowerWorkGroupId(DAG, *MFI, VT,
9806 case Intrinsic::amdgcn_cluster_id_x:
9807 return Subtarget->hasClusters()
9808 ? getPreloadedValue(DAG, *MFI, VT,
9810 : DAG.getPOISON(VT);
9811 case Intrinsic::amdgcn_cluster_id_y:
9812 return Subtarget->hasClusters()
9813 ? getPreloadedValue(DAG, *MFI, VT,
9815 : DAG.getPOISON(VT);
9816 case Intrinsic::amdgcn_cluster_id_z:
9817 return Subtarget->hasClusters()
9818 ? getPreloadedValue(DAG, *MFI, VT,
9820 : DAG.getPOISON(VT);
9821 case Intrinsic::amdgcn_cluster_workgroup_id_x:
9822 return Subtarget->hasClusters()
9823 ? getPreloadedValue(
9824 DAG, *MFI, VT,
9826 : DAG.getPOISON(VT);
9827 case Intrinsic::amdgcn_cluster_workgroup_id_y:
9828 return Subtarget->hasClusters()
9829 ? getPreloadedValue(
9830 DAG, *MFI, VT,
9832 : DAG.getPOISON(VT);
9833 case Intrinsic::amdgcn_cluster_workgroup_id_z:
9834 return Subtarget->hasClusters()
9835 ? getPreloadedValue(
9836 DAG, *MFI, VT,
9838 : DAG.getPOISON(VT);
9839 case Intrinsic::amdgcn_cluster_workgroup_flat_id:
9840 return Subtarget->hasClusters()
9841 ? lowerConstHwRegRead(DAG, Op, AMDGPU::Hwreg::ID_IB_STS2, 21, 4)
9842 : SDValue();
9843 case Intrinsic::amdgcn_cluster_workgroup_max_id_x:
9844 return Subtarget->hasClusters()
9845 ? getPreloadedValue(
9846 DAG, *MFI, VT,
9848 : DAG.getPOISON(VT);
9849 case Intrinsic::amdgcn_cluster_workgroup_max_id_y:
9850 return Subtarget->hasClusters()
9851 ? getPreloadedValue(
9852 DAG, *MFI, VT,
9854 : DAG.getPOISON(VT);
9855 case Intrinsic::amdgcn_cluster_workgroup_max_id_z:
9856 return Subtarget->hasClusters()
9857 ? getPreloadedValue(
9858 DAG, *MFI, VT,
9860 : DAG.getPOISON(VT);
9861 case Intrinsic::amdgcn_cluster_workgroup_max_flat_id:
9862 return Subtarget->hasClusters()
9863 ? getPreloadedValue(
9864 DAG, *MFI, VT,
9866 : DAG.getPOISON(VT);
9867 case Intrinsic::amdgcn_wave_id:
9868 return lowerWaveID(DAG, Op);
9869 case Intrinsic::amdgcn_lds_kernel_id: {
9870 if (MFI->isEntryFunction())
9871 return getLDSKernelId(DAG, DL);
9872 return getPreloadedValue(DAG, *MFI, VT,
9874 }
9875 case Intrinsic::amdgcn_workitem_id_x:
9876 return lowerWorkitemID(DAG, Op, 0, MFI->getArgInfo().WorkItemIDX);
9877 case Intrinsic::amdgcn_workitem_id_y:
9878 return lowerWorkitemID(DAG, Op, 1, MFI->getArgInfo().WorkItemIDY);
9879 case Intrinsic::amdgcn_workitem_id_z:
9880 return lowerWorkitemID(DAG, Op, 2, MFI->getArgInfo().WorkItemIDZ);
9881 case Intrinsic::amdgcn_wavefrontsize:
9882 return DAG.getConstant(MF.getSubtarget<GCNSubtarget>().getWavefrontSize(),
9883 SDLoc(Op), MVT::i32);
9884 case Intrinsic::amdgcn_s_buffer_load: {
9885 unsigned CPol = Op.getConstantOperandVal(3);
9886 // s_buffer_load, because of how it's optimized, can't be volatile
9887 // so reject ones with the volatile bit set.
9888 if (CPol & ~((Subtarget->getGeneration() >= AMDGPUSubtarget::GFX12)
9891 return Op;
9892 return lowerSBuffer(VT, DL, Op.getOperand(1), Op.getOperand(2),
9893 Op.getOperand(3), DAG);
9894 }
9895 case Intrinsic::amdgcn_fdiv_fast:
9896 return lowerFDIV_FAST(Op, DAG);
9897 case Intrinsic::amdgcn_sin:
9898 return DAG.getNode(AMDGPUISD::SIN_HW, DL, VT, Op.getOperand(1));
9899
9900 case Intrinsic::amdgcn_cos:
9901 return DAG.getNode(AMDGPUISD::COS_HW, DL, VT, Op.getOperand(1));
9902
9903 case Intrinsic::amdgcn_mul_u24:
9904 return DAG.getNode(AMDGPUISD::MUL_U24, DL, VT, Op.getOperand(1),
9905 Op.getOperand(2));
9906 case Intrinsic::amdgcn_mul_i24:
9907 return DAG.getNode(AMDGPUISD::MUL_I24, DL, VT, Op.getOperand(1),
9908 Op.getOperand(2));
9909
9910 case Intrinsic::amdgcn_log_clamp: {
9911 if (Subtarget->getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
9912 return SDValue();
9913
9914 return emitRemovedIntrinsicError(DAG, DL, VT);
9915 }
9916 case Intrinsic::amdgcn_fract:
9917 return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1));
9918
9919 case Intrinsic::amdgcn_class:
9920 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, VT, Op.getOperand(1),
9921 Op.getOperand(2));
9922 case Intrinsic::amdgcn_div_fmas:
9923 return DAG.getNode(AMDGPUISD::DIV_FMAS, DL, VT, Op.getOperand(1),
9924 Op.getOperand(2), Op.getOperand(3), Op.getOperand(4));
9925
9926 case Intrinsic::amdgcn_div_fixup:
9927 return DAG.getNode(AMDGPUISD::DIV_FIXUP, DL, VT, Op.getOperand(1),
9928 Op.getOperand(2), Op.getOperand(3));
9929
9930 case Intrinsic::amdgcn_div_scale: {
9931 const ConstantSDNode *Param = cast<ConstantSDNode>(Op.getOperand(3));
9932
9933 // Translate to the operands expected by the machine instruction. The
9934 // first parameter must be the same as the first instruction.
9935 SDValue Numerator = Op.getOperand(1);
9936 SDValue Denominator = Op.getOperand(2);
9937
9938 // Note this order is opposite of the machine instruction's operations,
9939 // which is s0.f = Quotient, s1.f = Denominator, s2.f = Numerator. The
9940 // intrinsic has the numerator as the first operand to match a normal
9941 // division operation.
9942
9943 SDValue Src0 = Param->isAllOnes() ? Numerator : Denominator;
9944
9945 return DAG.getNode(AMDGPUISD::DIV_SCALE, DL, Op->getVTList(), Src0,
9946 Denominator, Numerator);
9947 }
9948 case Intrinsic::amdgcn_icmp: {
9949 // There is a Pat that handles this variant, so return it as-is.
9950 if (Op.getOperand(1).getValueType() == MVT::i1 &&
9951 Op.getConstantOperandVal(2) == 0 &&
9952 Op.getConstantOperandVal(3) == ICmpInst::Predicate::ICMP_NE)
9953 return Op;
9954 return lowerICMPIntrinsic(*this, Op.getNode(), DAG);
9955 }
9956 case Intrinsic::amdgcn_fcmp: {
9957 return lowerFCMPIntrinsic(*this, Op.getNode(), DAG);
9958 }
9959 case Intrinsic::amdgcn_ballot:
9960 return lowerBALLOTIntrinsic(*this, Op.getNode(), DAG);
9961 case Intrinsic::amdgcn_fmed3:
9962 return DAG.getNode(AMDGPUISD::FMED3, DL, VT, Op.getOperand(1),
9963 Op.getOperand(2), Op.getOperand(3));
9964 case Intrinsic::amdgcn_fdot2:
9965 return DAG.getNode(AMDGPUISD::FDOT2, DL, VT, Op.getOperand(1),
9966 Op.getOperand(2), Op.getOperand(3), Op.getOperand(4));
9967 case Intrinsic::amdgcn_fmul_legacy:
9968 return DAG.getNode(AMDGPUISD::FMUL_LEGACY, DL, VT, Op.getOperand(1),
9969 Op.getOperand(2));
9970 case Intrinsic::amdgcn_sffbh:
9971 return DAG.getNode(AMDGPUISD::FFBH_I32, DL, VT, Op.getOperand(1));
9972 case Intrinsic::amdgcn_sbfe:
9973 return DAG.getNode(AMDGPUISD::BFE_I32, DL, VT, Op.getOperand(1),
9974 Op.getOperand(2), Op.getOperand(3));
9975 case Intrinsic::amdgcn_ubfe:
9976 return DAG.getNode(AMDGPUISD::BFE_U32, DL, VT, Op.getOperand(1),
9977 Op.getOperand(2), Op.getOperand(3));
9978 case Intrinsic::amdgcn_cvt_pkrtz:
9979 case Intrinsic::amdgcn_cvt_pknorm_i16:
9980 case Intrinsic::amdgcn_cvt_pknorm_u16:
9981 case Intrinsic::amdgcn_cvt_pk_i16:
9982 case Intrinsic::amdgcn_cvt_pk_u16: {
9983 // FIXME: Stop adding cast if v2f16/v2i16 are legal.
9984 EVT VT = Op.getValueType();
9985 unsigned Opcode;
9986
9987 if (IntrinsicID == Intrinsic::amdgcn_cvt_pkrtz)
9989 else if (IntrinsicID == Intrinsic::amdgcn_cvt_pknorm_i16)
9991 else if (IntrinsicID == Intrinsic::amdgcn_cvt_pknorm_u16)
9993 else if (IntrinsicID == Intrinsic::amdgcn_cvt_pk_i16)
9995 else
9997
9998 if (isTypeLegal(VT))
9999 return DAG.getNode(Opcode, DL, VT, Op.getOperand(1), Op.getOperand(2));
10000
10001 SDValue Node =
10002 DAG.getNode(Opcode, DL, MVT::i32, Op.getOperand(1), Op.getOperand(2));
10003 return DAG.getNode(ISD::BITCAST, DL, VT, Node);
10004 }
10005 case Intrinsic::amdgcn_fmad_ftz:
10006 return DAG.getNode(AMDGPUISD::FMAD_FTZ, DL, VT, Op.getOperand(1),
10007 Op.getOperand(2), Op.getOperand(3));
10008
10009 case Intrinsic::amdgcn_if_break:
10010 return SDValue(DAG.getMachineNode(AMDGPU::SI_IF_BREAK, DL, VT,
10011 Op->getOperand(1), Op->getOperand(2)),
10012 0);
10013
10014 case Intrinsic::amdgcn_groupstaticsize: {
10016 if (OS == Triple::AMDHSA || OS == Triple::AMDPAL)
10017 return Op;
10018
10019 const Module *M = MF.getFunction().getParent();
10020 const GlobalValue *GV =
10021 Intrinsic::getDeclarationIfExists(M, Intrinsic::amdgcn_groupstaticsize);
10022 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, 0,
10024 return {DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, GA), 0};
10025 }
10026 case Intrinsic::amdgcn_is_shared:
10027 case Intrinsic::amdgcn_is_private: {
10028 SDLoc SL(Op);
10029 SDValue SrcVec =
10030 DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
10031 SDValue SrcHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, SrcVec,
10032 DAG.getConstant(1, SL, MVT::i32));
10033
10034 unsigned AS = (IntrinsicID == Intrinsic::amdgcn_is_shared)
10036 : AMDGPUAS::PRIVATE_ADDRESS;
10037 if (AS == AMDGPUAS::PRIVATE_ADDRESS &&
10038 Subtarget->hasGloballyAddressableScratch()) {
10039 SDValue FlatScratchBaseHi(
10040 DAG.getMachineNode(
10041 AMDGPU::S_MOV_B32, DL, MVT::i32,
10042 DAG.getRegister(AMDGPU::SRC_FLAT_SCRATCH_BASE_HI, MVT::i32)),
10043 0);
10044 // Test bits 63..58 against the aperture address.
10045 return DAG.getSetCC(
10046 SL, MVT::i1,
10047 DAG.getNode(ISD::XOR, SL, MVT::i32, SrcHi, FlatScratchBaseHi),
10048 DAG.getConstant(1u << 26, SL, MVT::i32), ISD::SETULT);
10049 }
10050
10051 SDValue Aperture = getSegmentAperture(AS, SL, DAG);
10052 return DAG.getSetCC(SL, MVT::i1, SrcHi, Aperture, ISD::SETEQ);
10053 }
10054 case Intrinsic::amdgcn_perm:
10055 return DAG.getNode(AMDGPUISD::PERM, DL, MVT::i32, Op.getOperand(1),
10056 Op.getOperand(2), Op.getOperand(3));
10057 case Intrinsic::amdgcn_reloc_constant: {
10058 Module *M = MF.getFunction().getParent();
10059 const MDNode *Metadata = cast<MDNodeSDNode>(Op.getOperand(1))->getMD();
10060 auto SymbolName = cast<MDString>(Metadata->getOperand(0))->getString();
10061 auto *RelocSymbol = cast<GlobalVariable>(
10062 M->getOrInsertGlobal(SymbolName, Type::getInt32Ty(M->getContext())));
10063 SDValue GA = DAG.getTargetGlobalAddress(RelocSymbol, DL, MVT::i32, 0,
10065 return {DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, GA), 0};
10066 }
10067 case Intrinsic::amdgcn_swmmac_f16_16x16x32_f16:
10068 case Intrinsic::amdgcn_swmmac_bf16_16x16x32_bf16:
10069 case Intrinsic::amdgcn_swmmac_f32_16x16x32_bf16:
10070 case Intrinsic::amdgcn_swmmac_f32_16x16x32_f16:
10071 case Intrinsic::amdgcn_swmmac_f32_16x16x32_fp8_fp8:
10072 case Intrinsic::amdgcn_swmmac_f32_16x16x32_fp8_bf8:
10073 case Intrinsic::amdgcn_swmmac_f32_16x16x32_bf8_fp8:
10074 case Intrinsic::amdgcn_swmmac_f32_16x16x32_bf8_bf8: {
10075 if (Op.getOperand(4).getValueType() == MVT::i32)
10076 return SDValue();
10077
10078 SDLoc SL(Op);
10079 auto IndexKeyi32 = DAG.getAnyExtOrTrunc(Op.getOperand(4), SL, MVT::i32);
10080 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SL, Op.getValueType(),
10081 Op.getOperand(0), Op.getOperand(1), Op.getOperand(2),
10082 Op.getOperand(3), IndexKeyi32);
10083 }
10084 case Intrinsic::amdgcn_swmmac_f32_16x16x128_fp8_fp8:
10085 case Intrinsic::amdgcn_swmmac_f32_16x16x128_fp8_bf8:
10086 case Intrinsic::amdgcn_swmmac_f32_16x16x128_bf8_fp8:
10087 case Intrinsic::amdgcn_swmmac_f32_16x16x128_bf8_bf8:
10088 case Intrinsic::amdgcn_swmmac_f16_16x16x128_fp8_fp8:
10089 case Intrinsic::amdgcn_swmmac_f16_16x16x128_fp8_bf8:
10090 case Intrinsic::amdgcn_swmmac_f16_16x16x128_bf8_fp8:
10091 case Intrinsic::amdgcn_swmmac_f16_16x16x128_bf8_bf8: {
10092 if (Op.getOperand(4).getValueType() == MVT::i64)
10093 return SDValue();
10094
10095 SDLoc SL(Op);
10096 auto IndexKeyi64 = DAG.getAnyExtOrTrunc(Op.getOperand(4), SL, MVT::i64);
10097 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SL, Op.getValueType(),
10098 {Op.getOperand(0), Op.getOperand(1), Op.getOperand(2),
10099 Op.getOperand(3), IndexKeyi64, Op.getOperand(5),
10100 Op.getOperand(6)});
10101 }
10102 case Intrinsic::amdgcn_swmmac_f16_16x16x64_f16:
10103 case Intrinsic::amdgcn_swmmac_bf16_16x16x64_bf16:
10104 case Intrinsic::amdgcn_swmmac_f32_16x16x64_bf16:
10105 case Intrinsic::amdgcn_swmmac_bf16f32_16x16x64_bf16:
10106 case Intrinsic::amdgcn_swmmac_f32_16x16x64_f16:
10107 case Intrinsic::amdgcn_swmmac_i32_16x16x128_iu8: {
10108 EVT IndexKeyTy = IntrinsicID == Intrinsic::amdgcn_swmmac_i32_16x16x128_iu8
10109 ? MVT::i64
10110 : MVT::i32;
10111 if (Op.getOperand(6).getValueType() == IndexKeyTy)
10112 return SDValue();
10113
10114 SDLoc SL(Op);
10115 auto IndexKey = DAG.getAnyExtOrTrunc(Op.getOperand(6), SL, IndexKeyTy);
10116 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SL, Op.getValueType(),
10117 {Op.getOperand(0), Op.getOperand(1), Op.getOperand(2),
10118 Op.getOperand(3), Op.getOperand(4), Op.getOperand(5),
10119 IndexKey, Op.getOperand(7),
10120 Op.getOperand(8)}); // No clamp operand
10121 }
10122 case Intrinsic::amdgcn_swmmac_i32_16x16x32_iu4:
10123 case Intrinsic::amdgcn_swmmac_i32_16x16x32_iu8:
10124 case Intrinsic::amdgcn_swmmac_i32_16x16x64_iu4: {
10125 if (Op.getOperand(6).getValueType() == MVT::i32)
10126 return SDValue();
10127
10128 SDLoc SL(Op);
10129 auto IndexKeyi32 = DAG.getAnyExtOrTrunc(Op.getOperand(6), SL, MVT::i32);
10130 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SL, Op.getValueType(),
10131 {Op.getOperand(0), Op.getOperand(1), Op.getOperand(2),
10132 Op.getOperand(3), Op.getOperand(4), Op.getOperand(5),
10133 IndexKeyi32, Op.getOperand(7)});
10134 }
10135 case Intrinsic::amdgcn_addrspacecast_nonnull:
10136 return lowerADDRSPACECAST(Op, DAG);
10137 case Intrinsic::amdgcn_readlane:
10138 case Intrinsic::amdgcn_readfirstlane:
10139 case Intrinsic::amdgcn_writelane:
10140 case Intrinsic::amdgcn_permlane16:
10141 case Intrinsic::amdgcn_permlanex16:
10142 case Intrinsic::amdgcn_permlane64:
10143 case Intrinsic::amdgcn_set_inactive:
10144 case Intrinsic::amdgcn_set_inactive_chain_arg:
10145 case Intrinsic::amdgcn_mov_dpp8:
10146 case Intrinsic::amdgcn_update_dpp:
10147 return lowerLaneOp(*this, Op.getNode(), DAG);
10148 case Intrinsic::amdgcn_dead: {
10150 for (const EVT ValTy : Op.getNode()->values())
10151 Poisons.push_back(DAG.getPOISON(ValTy));
10152 return DAG.getMergeValues(Poisons, SDLoc(Op));
10153 }
10154 default:
10155 if (const AMDGPU::ImageDimIntrinsicInfo *ImageDimIntr =
10157 return lowerImage(Op, ImageDimIntr, DAG, false);
10158
10159 return Op;
10160 }
10161}
10162
10163// On targets not supporting constant in soffset field, turn zero to
10164// SGPR_NULL to avoid generating an extra s_mov with zero.
10166 const GCNSubtarget *Subtarget) {
10167 if (Subtarget->hasRestrictedSOffset() && isNullConstant(SOffset))
10168 return DAG.getRegister(AMDGPU::SGPR_NULL, MVT::i32);
10169 return SOffset;
10170}
10171
10172SDValue SITargetLowering::lowerRawBufferAtomicIntrin(SDValue Op,
10173 SelectionDAG &DAG,
10174 unsigned NewOpcode) const {
10175 SDLoc DL(Op);
10176
10177 SDValue VData = Op.getOperand(2);
10178 SDValue Rsrc = bufferRsrcPtrToVector(Op.getOperand(3), DAG);
10179 auto [VOffset, Offset] = splitBufferOffsets(Op.getOperand(4), DAG);
10180 auto SOffset = selectSOffset(Op.getOperand(5), DAG, Subtarget);
10181 SDValue Ops[] = {
10182 Op.getOperand(0), // Chain
10183 VData, // vdata
10184 Rsrc, // rsrc
10185 DAG.getConstant(0, DL, MVT::i32), // vindex
10186 VOffset, // voffset
10187 SOffset, // soffset
10188 Offset, // offset
10189 Op.getOperand(6), // cachepolicy
10190 DAG.getTargetConstant(0, DL, MVT::i1), // idxen
10191 };
10192
10193 auto *M = cast<MemSDNode>(Op);
10194
10195 EVT MemVT = VData.getValueType();
10196 return DAG.getMemIntrinsicNode(NewOpcode, DL, Op->getVTList(), Ops, MemVT,
10197 M->getMemOperand());
10198}
10199
10200SDValue
10201SITargetLowering::lowerStructBufferAtomicIntrin(SDValue Op, SelectionDAG &DAG,
10202 unsigned NewOpcode) const {
10203 SDLoc DL(Op);
10204
10205 SDValue VData = Op.getOperand(2);
10206 SDValue Rsrc = bufferRsrcPtrToVector(Op.getOperand(3), DAG);
10207 auto [VOffset, Offset] = splitBufferOffsets(Op.getOperand(5), DAG);
10208 auto SOffset = selectSOffset(Op.getOperand(6), DAG, Subtarget);
10209 SDValue Ops[] = {
10210 Op.getOperand(0), // Chain
10211 VData, // vdata
10212 Rsrc, // rsrc
10213 Op.getOperand(4), // vindex
10214 VOffset, // voffset
10215 SOffset, // soffset
10216 Offset, // offset
10217 Op.getOperand(7), // cachepolicy
10218 DAG.getTargetConstant(1, DL, MVT::i1), // idxen
10219 };
10220
10221 auto *M = cast<MemSDNode>(Op);
10222
10223 EVT MemVT = VData.getValueType();
10224 return DAG.getMemIntrinsicNode(NewOpcode, DL, Op->getVTList(), Ops, MemVT,
10225 M->getMemOperand());
10226}
10227
10228SDValue SITargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op,
10229 SelectionDAG &DAG) const {
10230 unsigned IntrID = Op.getConstantOperandVal(1);
10231 SDLoc DL(Op);
10232
10233 switch (IntrID) {
10234 case Intrinsic::amdgcn_ds_ordered_add:
10235 case Intrinsic::amdgcn_ds_ordered_swap: {
10236 MemSDNode *M = cast<MemSDNode>(Op);
10237 SDValue Chain = M->getOperand(0);
10238 SDValue M0 = M->getOperand(2);
10239 SDValue Value = M->getOperand(3);
10240 unsigned IndexOperand = M->getConstantOperandVal(7);
10241 unsigned WaveRelease = M->getConstantOperandVal(8);
10242 unsigned WaveDone = M->getConstantOperandVal(9);
10243
10244 unsigned OrderedCountIndex = IndexOperand & 0x3f;
10245 IndexOperand &= ~0x3f;
10246 unsigned CountDw = 0;
10247
10248 if (Subtarget->getGeneration() >= AMDGPUSubtarget::GFX10) {
10249 CountDw = (IndexOperand >> 24) & 0xf;
10250 IndexOperand &= ~(0xf << 24);
10251
10252 if (CountDw < 1 || CountDw > 4) {
10253 const Function &Fn = DAG.getMachineFunction().getFunction();
10254 DAG.getContext()->diagnose(DiagnosticInfoUnsupported(
10255 Fn, "ds_ordered_count: dword count must be between 1 and 4",
10256 DL.getDebugLoc()));
10257 CountDw = 1;
10258 }
10259 }
10260
10261 if (IndexOperand) {
10262 const Function &Fn = DAG.getMachineFunction().getFunction();
10263 DAG.getContext()->diagnose(DiagnosticInfoUnsupported(
10264 Fn, "ds_ordered_count: bad index operand", DL.getDebugLoc()));
10265 }
10266
10267 if (WaveDone && !WaveRelease) {
10268 // TODO: Move this to IR verifier
10269 const Function &Fn = DAG.getMachineFunction().getFunction();
10270 DAG.getContext()->diagnose(DiagnosticInfoUnsupported(
10271 Fn, "ds_ordered_count: wave_done requires wave_release",
10272 DL.getDebugLoc()));
10273 }
10274
10275 unsigned Instruction = IntrID == Intrinsic::amdgcn_ds_ordered_add ? 0 : 1;
10276 unsigned ShaderType =
10278 unsigned Offset0 = OrderedCountIndex << 2;
10279 unsigned Offset1 = WaveRelease | (WaveDone << 1) | (Instruction << 4);
10280
10281 if (Subtarget->getGeneration() >= AMDGPUSubtarget::GFX10)
10282 Offset1 |= (CountDw - 1) << 6;
10283
10284 if (Subtarget->getGeneration() < AMDGPUSubtarget::GFX11)
10285 Offset1 |= ShaderType << 2;
10286
10287 unsigned Offset = Offset0 | (Offset1 << 8);
10288
10289 SDValue Ops[] = {
10290 Chain, Value, DAG.getTargetConstant(Offset, DL, MVT::i16),
10291 copyToM0(DAG, Chain, DL, M0).getValue(1), // Glue
10292 };
10294 M->getVTList(), Ops, M->getMemoryVT(),
10295 M->getMemOperand());
10296 }
10297 case Intrinsic::amdgcn_raw_buffer_load:
10298 case Intrinsic::amdgcn_raw_ptr_buffer_load:
10299 case Intrinsic::amdgcn_raw_atomic_buffer_load:
10300 case Intrinsic::amdgcn_raw_ptr_atomic_buffer_load:
10301 case Intrinsic::amdgcn_raw_buffer_load_format:
10302 case Intrinsic::amdgcn_raw_ptr_buffer_load_format: {
10303 const bool IsFormat =
10304 IntrID == Intrinsic::amdgcn_raw_buffer_load_format ||
10305 IntrID == Intrinsic::amdgcn_raw_ptr_buffer_load_format;
10306
10307 SDValue Rsrc = bufferRsrcPtrToVector(Op.getOperand(2), DAG);
10308 auto [VOffset, Offset] = splitBufferOffsets(Op.getOperand(3), DAG);
10309 auto SOffset = selectSOffset(Op.getOperand(4), DAG, Subtarget);
10310 SDValue Ops[] = {
10311 Op.getOperand(0), // Chain
10312 Rsrc, // rsrc
10313 DAG.getConstant(0, DL, MVT::i32), // vindex
10314 VOffset, // voffset
10315 SOffset, // soffset
10316 Offset, // offset
10317 Op.getOperand(5), // cachepolicy, swizzled buffer
10318 DAG.getTargetConstant(0, DL, MVT::i1), // idxen
10319 };
10320
10321 auto *M = cast<MemSDNode>(Op);
10322 return lowerIntrinsicLoad(M, IsFormat, DAG, Ops);
10323 }
10324 case Intrinsic::amdgcn_struct_buffer_load:
10325 case Intrinsic::amdgcn_struct_ptr_buffer_load:
10326 case Intrinsic::amdgcn_struct_buffer_load_format:
10327 case Intrinsic::amdgcn_struct_ptr_buffer_load_format:
10328 case Intrinsic::amdgcn_struct_atomic_buffer_load:
10329 case Intrinsic::amdgcn_struct_ptr_atomic_buffer_load: {
10330 const bool IsFormat =
10331 IntrID == Intrinsic::amdgcn_struct_buffer_load_format ||
10332 IntrID == Intrinsic::amdgcn_struct_ptr_buffer_load_format;
10333
10334 SDValue Rsrc = bufferRsrcPtrToVector(Op.getOperand(2), DAG);
10335 auto [VOffset, Offset] = splitBufferOffsets(Op.getOperand(4), DAG);
10336 auto SOffset = selectSOffset(Op.getOperand(5), DAG, Subtarget);
10337 SDValue Ops[] = {
10338 Op.getOperand(0), // Chain
10339 Rsrc, // rsrc
10340 Op.getOperand(3), // vindex
10341 VOffset, // voffset
10342 SOffset, // soffset
10343 Offset, // offset
10344 Op.getOperand(6), // cachepolicy, swizzled buffer
10345 DAG.getTargetConstant(1, DL, MVT::i1), // idxen
10346 };
10347
10348 return lowerIntrinsicLoad(cast<MemSDNode>(Op), IsFormat, DAG, Ops);
10349 }
10350 case Intrinsic::amdgcn_raw_tbuffer_load:
10351 case Intrinsic::amdgcn_raw_ptr_tbuffer_load: {
10352 MemSDNode *M = cast<MemSDNode>(Op);
10353 EVT LoadVT = Op.getValueType();
10354 SDValue Rsrc = bufferRsrcPtrToVector(Op.getOperand(2), DAG);
10355 auto [VOffset, Offset] = splitBufferOffsets(Op.getOperand(3), DAG);
10356 auto SOffset = selectSOffset(Op.getOperand(4), DAG, Subtarget);
10357
10358 SDValue Ops[] = {
10359 Op.getOperand(0), // Chain
10360 Rsrc, // rsrc
10361 DAG.getConstant(0, DL, MVT::i32), // vindex
10362 VOffset, // voffset
10363 SOffset, // soffset
10364 Offset, // offset
10365 Op.getOperand(5), // format
10366 Op.getOperand(6), // cachepolicy, swizzled buffer
10367 DAG.getTargetConstant(0, DL, MVT::i1), // idxen
10368 };
10369
10370 if (LoadVT.getScalarType() == MVT::f16)
10371 return adjustLoadValueType(AMDGPUISD::TBUFFER_LOAD_FORMAT_D16, M, DAG,
10372 Ops);
10373 return getMemIntrinsicNode(AMDGPUISD::TBUFFER_LOAD_FORMAT, DL,
10374 Op->getVTList(), Ops, LoadVT, M->getMemOperand(),
10375 DAG);
10376 }
10377 case Intrinsic::amdgcn_struct_tbuffer_load:
10378 case Intrinsic::amdgcn_struct_ptr_tbuffer_load: {
10379 MemSDNode *M = cast<MemSDNode>(Op);
10380 EVT LoadVT = Op.getValueType();
10381 SDValue Rsrc = bufferRsrcPtrToVector(Op.getOperand(2), DAG);
10382 auto [VOffset, Offset] = splitBufferOffsets(Op.getOperand(4), DAG);
10383 auto SOffset = selectSOffset(Op.getOperand(5), DAG, Subtarget);
10384
10385 SDValue Ops[] = {
10386 Op.getOperand(0), // Chain
10387 Rsrc, // rsrc
10388 Op.getOperand(3), // vindex
10389 VOffset, // voffset
10390 SOffset, // soffset
10391 Offset, // offset
10392 Op.getOperand(6), // format
10393 Op.getOperand(7), // cachepolicy, swizzled buffer
10394 DAG.getTargetConstant(1, DL, MVT::i1), // idxen
10395 };
10396
10397 if (LoadVT.getScalarType() == MVT::f16)
10398 return adjustLoadValueType(AMDGPUISD::TBUFFER_LOAD_FORMAT_D16, M, DAG,
10399 Ops);
10400 return getMemIntrinsicNode(AMDGPUISD::TBUFFER_LOAD_FORMAT, DL,
10401 Op->getVTList(), Ops, LoadVT, M->getMemOperand(),
10402 DAG);
10403 }
10404 case Intrinsic::amdgcn_raw_buffer_atomic_fadd:
10405 case Intrinsic::amdgcn_raw_ptr_buffer_atomic_fadd:
10406 return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_FADD);
10407 case Intrinsic::amdgcn_struct_buffer_atomic_fadd:
10408 case Intrinsic::amdgcn_struct_ptr_buffer_atomic_fadd:
10409 return lowerStructBufferAtomicIntrin(Op, DAG,
10411 case Intrinsic::amdgcn_raw_buffer_atomic_fmin:
10412 case Intrinsic::amdgcn_raw_ptr_buffer_atomic_fmin:
10413 return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_FMIN);
10414 case Intrinsic::amdgcn_struct_buffer_atomic_fmin:
10415 case Intrinsic::amdgcn_struct_ptr_buffer_atomic_fmin:
10416 return lowerStructBufferAtomicIntrin(Op, DAG,
10418 case Intrinsic::amdgcn_raw_buffer_atomic_fmax:
10419 case Intrinsic::amdgcn_raw_ptr_buffer_atomic_fmax:
10420 return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_FMAX);
10421 case Intrinsic::amdgcn_struct_buffer_atomic_fmax:
10422 case Intrinsic::amdgcn_struct_ptr_buffer_atomic_fmax:
10423 return lowerStructBufferAtomicIntrin(Op, DAG,
10425 case Intrinsic::amdgcn_raw_buffer_atomic_swap:
10426 case Intrinsic::amdgcn_raw_ptr_buffer_atomic_swap:
10427 return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_SWAP);
10428 case Intrinsic::amdgcn_raw_buffer_atomic_add:
10429 case Intrinsic::amdgcn_raw_ptr_buffer_atomic_add:
10430 return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_ADD);
10431 case Intrinsic::amdgcn_raw_buffer_atomic_sub:
10432 case Intrinsic::amdgcn_raw_ptr_buffer_atomic_sub:
10433 return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_SUB);
10434 case Intrinsic::amdgcn_raw_buffer_atomic_smin:
10435 case Intrinsic::amdgcn_raw_ptr_buffer_atomic_smin:
10436 return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_SMIN);
10437 case Intrinsic::amdgcn_raw_buffer_atomic_umin:
10438 case Intrinsic::amdgcn_raw_ptr_buffer_atomic_umin:
10439 return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_UMIN);
10440 case Intrinsic::amdgcn_raw_buffer_atomic_smax:
10441 case Intrinsic::amdgcn_raw_ptr_buffer_atomic_smax:
10442 return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_SMAX);
10443 case Intrinsic::amdgcn_raw_buffer_atomic_umax:
10444 case Intrinsic::amdgcn_raw_ptr_buffer_atomic_umax:
10445 return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_UMAX);
10446 case Intrinsic::amdgcn_raw_buffer_atomic_and:
10447 case Intrinsic::amdgcn_raw_ptr_buffer_atomic_and:
10448 return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_AND);
10449 case Intrinsic::amdgcn_raw_buffer_atomic_or:
10450 case Intrinsic::amdgcn_raw_ptr_buffer_atomic_or:
10451 return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_OR);
10452 case Intrinsic::amdgcn_raw_buffer_atomic_xor:
10453 case Intrinsic::amdgcn_raw_ptr_buffer_atomic_xor:
10454 return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_XOR);
10455 case Intrinsic::amdgcn_raw_buffer_atomic_inc:
10456 case Intrinsic::amdgcn_raw_ptr_buffer_atomic_inc:
10457 return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_INC);
10458 case Intrinsic::amdgcn_raw_buffer_atomic_dec:
10459 case Intrinsic::amdgcn_raw_ptr_buffer_atomic_dec:
10460 return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_DEC);
10461 case Intrinsic::amdgcn_raw_buffer_atomic_cond_sub_u32:
10462 return lowerRawBufferAtomicIntrin(Op, DAG,
10464 case Intrinsic::amdgcn_struct_buffer_atomic_swap:
10465 case Intrinsic::amdgcn_struct_ptr_buffer_atomic_swap:
10466 return lowerStructBufferAtomicIntrin(Op, DAG,
10468 case Intrinsic::amdgcn_struct_buffer_atomic_add:
10469 case Intrinsic::amdgcn_struct_ptr_buffer_atomic_add:
10470 return lowerStructBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_ADD);
10471 case Intrinsic::amdgcn_struct_buffer_atomic_sub:
10472 case Intrinsic::amdgcn_struct_ptr_buffer_atomic_sub:
10473 return lowerStructBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_SUB);
10474 case Intrinsic::amdgcn_struct_buffer_atomic_smin:
10475 case Intrinsic::amdgcn_struct_ptr_buffer_atomic_smin:
10476 return lowerStructBufferAtomicIntrin(Op, DAG,
10478 case Intrinsic::amdgcn_struct_buffer_atomic_umin:
10479 case Intrinsic::amdgcn_struct_ptr_buffer_atomic_umin:
10480 return lowerStructBufferAtomicIntrin(Op, DAG,
10482 case Intrinsic::amdgcn_struct_buffer_atomic_smax:
10483 case Intrinsic::amdgcn_struct_ptr_buffer_atomic_smax:
10484 return lowerStructBufferAtomicIntrin(Op, DAG,
10486 case Intrinsic::amdgcn_struct_buffer_atomic_umax:
10487 case Intrinsic::amdgcn_struct_ptr_buffer_atomic_umax:
10488 return lowerStructBufferAtomicIntrin(Op, DAG,
10490 case Intrinsic::amdgcn_struct_buffer_atomic_and:
10491 case Intrinsic::amdgcn_struct_ptr_buffer_atomic_and:
10492 return lowerStructBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_AND);
10493 case Intrinsic::amdgcn_struct_buffer_atomic_or:
10494 case Intrinsic::amdgcn_struct_ptr_buffer_atomic_or:
10495 return lowerStructBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_OR);
10496 case Intrinsic::amdgcn_struct_buffer_atomic_xor:
10497 case Intrinsic::amdgcn_struct_ptr_buffer_atomic_xor:
10498 return lowerStructBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_XOR);
10499 case Intrinsic::amdgcn_struct_buffer_atomic_inc:
10500 case Intrinsic::amdgcn_struct_ptr_buffer_atomic_inc:
10501 return lowerStructBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_INC);
10502 case Intrinsic::amdgcn_struct_buffer_atomic_dec:
10503 case Intrinsic::amdgcn_struct_ptr_buffer_atomic_dec:
10504 return lowerStructBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_DEC);
10505 case Intrinsic::amdgcn_struct_buffer_atomic_cond_sub_u32:
10506 return lowerStructBufferAtomicIntrin(Op, DAG,
10508
10509 case Intrinsic::amdgcn_raw_buffer_atomic_cmpswap:
10510 case Intrinsic::amdgcn_raw_ptr_buffer_atomic_cmpswap: {
10511 SDValue Rsrc = bufferRsrcPtrToVector(Op.getOperand(4), DAG);
10512 auto [VOffset, Offset] = splitBufferOffsets(Op.getOperand(5), DAG);
10513 auto SOffset = selectSOffset(Op.getOperand(6), DAG, Subtarget);
10514 SDValue Ops[] = {
10515 Op.getOperand(0), // Chain
10516 Op.getOperand(2), // src
10517 Op.getOperand(3), // cmp
10518 Rsrc, // rsrc
10519 DAG.getConstant(0, DL, MVT::i32), // vindex
10520 VOffset, // voffset
10521 SOffset, // soffset
10522 Offset, // offset
10523 Op.getOperand(7), // cachepolicy
10524 DAG.getTargetConstant(0, DL, MVT::i1), // idxen
10525 };
10526 EVT VT = Op.getValueType();
10527 auto *M = cast<MemSDNode>(Op);
10528
10530 Op->getVTList(), Ops, VT,
10531 M->getMemOperand());
10532 }
10533 case Intrinsic::amdgcn_struct_buffer_atomic_cmpswap:
10534 case Intrinsic::amdgcn_struct_ptr_buffer_atomic_cmpswap: {
10535 SDValue Rsrc = bufferRsrcPtrToVector(Op->getOperand(4), DAG);
10536 auto [VOffset, Offset] = splitBufferOffsets(Op.getOperand(6), DAG);
10537 auto SOffset = selectSOffset(Op.getOperand(7), DAG, Subtarget);
10538 SDValue Ops[] = {
10539 Op.getOperand(0), // Chain
10540 Op.getOperand(2), // src
10541 Op.getOperand(3), // cmp
10542 Rsrc, // rsrc
10543 Op.getOperand(5), // vindex
10544 VOffset, // voffset
10545 SOffset, // soffset
10546 Offset, // offset
10547 Op.getOperand(8), // cachepolicy
10548 DAG.getTargetConstant(1, DL, MVT::i1), // idxen
10549 };
10550 EVT VT = Op.getValueType();
10551 auto *M = cast<MemSDNode>(Op);
10552
10554 Op->getVTList(), Ops, VT,
10555 M->getMemOperand());
10556 }
10557 case Intrinsic::amdgcn_image_bvh_dual_intersect_ray:
10558 case Intrinsic::amdgcn_image_bvh8_intersect_ray: {
10559 MemSDNode *M = cast<MemSDNode>(Op);
10560 SDValue NodePtr = M->getOperand(2);
10561 SDValue RayExtent = M->getOperand(3);
10562 SDValue InstanceMask = M->getOperand(4);
10563 SDValue RayOrigin = M->getOperand(5);
10564 SDValue RayDir = M->getOperand(6);
10565 SDValue Offsets = M->getOperand(7);
10566 SDValue TDescr = M->getOperand(8);
10567
10568 assert(NodePtr.getValueType() == MVT::i64);
10569 assert(RayDir.getValueType() == MVT::v3f32);
10570
10571 if (!Subtarget->hasBVHDualAndBVH8Insts()) {
10572 emitRemovedIntrinsicError(DAG, DL, Op.getValueType());
10573 return SDValue();
10574 }
10575
10576 bool IsBVH8 = IntrID == Intrinsic::amdgcn_image_bvh8_intersect_ray;
10577 const unsigned NumVDataDwords = 10;
10578 const unsigned NumVAddrDwords = IsBVH8 ? 11 : 12;
10579 int Opcode = AMDGPU::getMIMGOpcode(
10580 IsBVH8 ? AMDGPU::IMAGE_BVH8_INTERSECT_RAY
10581 : AMDGPU::IMAGE_BVH_DUAL_INTERSECT_RAY,
10582 AMDGPU::MIMGEncGfx12, NumVDataDwords, NumVAddrDwords);
10583 assert(Opcode != -1);
10584
10586 Ops.push_back(NodePtr);
10587 Ops.push_back(DAG.getBuildVector(
10588 MVT::v2i32, DL,
10589 {DAG.getBitcast(MVT::i32, RayExtent),
10590 DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, InstanceMask)}));
10591 Ops.push_back(RayOrigin);
10592 Ops.push_back(RayDir);
10593 Ops.push_back(Offsets);
10594 Ops.push_back(TDescr);
10595 Ops.push_back(M->getChain());
10596
10597 auto *NewNode = DAG.getMachineNode(Opcode, DL, M->getVTList(), Ops);
10598 MachineMemOperand *MemRef = M->getMemOperand();
10599 DAG.setNodeMemRefs(NewNode, {MemRef});
10600 return SDValue(NewNode, 0);
10601 }
10602 case Intrinsic::amdgcn_image_bvh_intersect_ray: {
10603 MemSDNode *M = cast<MemSDNode>(Op);
10604 SDValue NodePtr = M->getOperand(2);
10605 SDValue RayExtent = M->getOperand(3);
10606 SDValue RayOrigin = M->getOperand(4);
10607 SDValue RayDir = M->getOperand(5);
10608 SDValue RayInvDir = M->getOperand(6);
10609 SDValue TDescr = M->getOperand(7);
10610
10611 assert(NodePtr.getValueType() == MVT::i32 ||
10612 NodePtr.getValueType() == MVT::i64);
10613 assert(RayDir.getValueType() == MVT::v3f16 ||
10614 RayDir.getValueType() == MVT::v3f32);
10615
10616 if (!Subtarget->hasGFX10_AEncoding()) {
10617 emitRemovedIntrinsicError(DAG, DL, Op.getValueType());
10618 return SDValue();
10619 }
10620
10621 const bool IsGFX11 = AMDGPU::isGFX11(*Subtarget);
10622 const bool IsGFX11Plus = AMDGPU::isGFX11Plus(*Subtarget);
10623 const bool IsGFX12Plus = AMDGPU::isGFX12Plus(*Subtarget);
10624 const bool IsA16 = RayDir.getValueType().getVectorElementType() == MVT::f16;
10625 const bool Is64 = NodePtr.getValueType() == MVT::i64;
10626 const unsigned NumVDataDwords = 4;
10627 const unsigned NumVAddrDwords = IsA16 ? (Is64 ? 9 : 8) : (Is64 ? 12 : 11);
10628 const unsigned NumVAddrs = IsGFX11Plus ? (IsA16 ? 4 : 5) : NumVAddrDwords;
10629 const bool UseNSA = (Subtarget->hasNSAEncoding() &&
10630 NumVAddrs <= Subtarget->getNSAMaxSize()) ||
10631 IsGFX12Plus;
10632 const unsigned BaseOpcodes[2][2] = {
10633 {AMDGPU::IMAGE_BVH_INTERSECT_RAY, AMDGPU::IMAGE_BVH_INTERSECT_RAY_a16},
10634 {AMDGPU::IMAGE_BVH64_INTERSECT_RAY,
10635 AMDGPU::IMAGE_BVH64_INTERSECT_RAY_a16}};
10636 int Opcode;
10637 if (UseNSA) {
10638 Opcode = AMDGPU::getMIMGOpcode(BaseOpcodes[Is64][IsA16],
10639 IsGFX12Plus ? AMDGPU::MIMGEncGfx12
10640 : IsGFX11 ? AMDGPU::MIMGEncGfx11NSA
10641 : AMDGPU::MIMGEncGfx10NSA,
10642 NumVDataDwords, NumVAddrDwords);
10643 } else {
10644 assert(!IsGFX12Plus);
10645 Opcode = AMDGPU::getMIMGOpcode(BaseOpcodes[Is64][IsA16],
10646 IsGFX11 ? AMDGPU::MIMGEncGfx11Default
10647 : AMDGPU::MIMGEncGfx10Default,
10648 NumVDataDwords, NumVAddrDwords);
10649 }
10650 assert(Opcode != -1);
10651
10653
10654 auto packLanes = [&DAG, &Ops, &DL](SDValue Op, bool IsAligned) {
10656 DAG.ExtractVectorElements(Op, Lanes, 0, 3);
10657 if (Lanes[0].getValueSizeInBits() == 32) {
10658 for (unsigned I = 0; I < 3; ++I)
10659 Ops.push_back(DAG.getBitcast(MVT::i32, Lanes[I]));
10660 } else {
10661 if (IsAligned) {
10662 Ops.push_back(DAG.getBitcast(
10663 MVT::i32,
10664 DAG.getBuildVector(MVT::v2f16, DL, {Lanes[0], Lanes[1]})));
10665 Ops.push_back(Lanes[2]);
10666 } else {
10667 SDValue Elt0 = Ops.pop_back_val();
10668 Ops.push_back(DAG.getBitcast(
10669 MVT::i32, DAG.getBuildVector(MVT::v2f16, DL, {Elt0, Lanes[0]})));
10670 Ops.push_back(DAG.getBitcast(
10671 MVT::i32,
10672 DAG.getBuildVector(MVT::v2f16, DL, {Lanes[1], Lanes[2]})));
10673 }
10674 }
10675 };
10676
10677 if (UseNSA && IsGFX11Plus) {
10678 Ops.push_back(NodePtr);
10679 Ops.push_back(DAG.getBitcast(MVT::i32, RayExtent));
10680 Ops.push_back(RayOrigin);
10681 if (IsA16) {
10682 SmallVector<SDValue, 3> DirLanes, InvDirLanes, MergedLanes;
10683 DAG.ExtractVectorElements(RayDir, DirLanes, 0, 3);
10684 DAG.ExtractVectorElements(RayInvDir, InvDirLanes, 0, 3);
10685 for (unsigned I = 0; I < 3; ++I) {
10686 MergedLanes.push_back(DAG.getBitcast(
10687 MVT::i32, DAG.getBuildVector(MVT::v2f16, DL,
10688 {DirLanes[I], InvDirLanes[I]})));
10689 }
10690 Ops.push_back(DAG.getBuildVector(MVT::v3i32, DL, MergedLanes));
10691 } else {
10692 Ops.push_back(RayDir);
10693 Ops.push_back(RayInvDir);
10694 }
10695 } else {
10696 if (Is64)
10697 DAG.ExtractVectorElements(DAG.getBitcast(MVT::v2i32, NodePtr), Ops, 0,
10698 2);
10699 else
10700 Ops.push_back(NodePtr);
10701
10702 Ops.push_back(DAG.getBitcast(MVT::i32, RayExtent));
10703 packLanes(RayOrigin, true);
10704 packLanes(RayDir, true);
10705 packLanes(RayInvDir, false);
10706 }
10707
10708 if (!UseNSA) {
10709 // Build a single vector containing all the operands so far prepared.
10710 if (NumVAddrDwords > 12) {
10711 SDValue Undef = DAG.getPOISON(MVT::i32);
10712 Ops.append(16 - Ops.size(), Undef);
10713 }
10714 assert(Ops.size() >= 8 && Ops.size() <= 12);
10715 SDValue MergedOps =
10716 DAG.getBuildVector(MVT::getVectorVT(MVT::i32, Ops.size()), DL, Ops);
10717 Ops.clear();
10718 Ops.push_back(MergedOps);
10719 }
10720
10721 Ops.push_back(TDescr);
10722 Ops.push_back(DAG.getTargetConstant(IsA16, DL, MVT::i1));
10723 Ops.push_back(M->getChain());
10724
10725 auto *NewNode = DAG.getMachineNode(Opcode, DL, M->getVTList(), Ops);
10726 MachineMemOperand *MemRef = M->getMemOperand();
10727 DAG.setNodeMemRefs(NewNode, {MemRef});
10728 return SDValue(NewNode, 0);
10729 }
10730 case Intrinsic::amdgcn_global_atomic_fmin_num:
10731 case Intrinsic::amdgcn_global_atomic_fmax_num:
10732 case Intrinsic::amdgcn_flat_atomic_fmin_num:
10733 case Intrinsic::amdgcn_flat_atomic_fmax_num: {
10734 MemSDNode *M = cast<MemSDNode>(Op);
10735 SDValue Ops[] = {
10736 M->getOperand(0), // Chain
10737 M->getOperand(2), // Ptr
10738 M->getOperand(3) // Value
10739 };
10740 unsigned Opcode = 0;
10741 switch (IntrID) {
10742 case Intrinsic::amdgcn_global_atomic_fmin_num:
10743 case Intrinsic::amdgcn_flat_atomic_fmin_num: {
10744 Opcode = ISD::ATOMIC_LOAD_FMIN;
10745 break;
10746 }
10747 case Intrinsic::amdgcn_global_atomic_fmax_num:
10748 case Intrinsic::amdgcn_flat_atomic_fmax_num: {
10749 Opcode = ISD::ATOMIC_LOAD_FMAX;
10750 break;
10751 }
10752 default:
10753 llvm_unreachable("unhandled atomic opcode");
10754 }
10755 return DAG.getAtomic(Opcode, SDLoc(Op), M->getMemoryVT(), M->getVTList(),
10756 Ops, M->getMemOperand());
10757 }
10758 case Intrinsic::amdgcn_s_get_barrier_state:
10759 case Intrinsic::amdgcn_s_get_named_barrier_state: {
10760 SDValue Chain = Op->getOperand(0);
10762 unsigned Opc;
10763
10764 if (isa<ConstantSDNode>(Op->getOperand(2))) {
10765 uint64_t BarID = cast<ConstantSDNode>(Op->getOperand(2))->getZExtValue();
10766 if (IntrID == Intrinsic::amdgcn_s_get_named_barrier_state)
10767 BarID = (BarID >> 4) & 0x3F;
10768 Opc = AMDGPU::S_GET_BARRIER_STATE_IMM;
10769 SDValue K = DAG.getTargetConstant(BarID, DL, MVT::i32);
10770 Ops.push_back(K);
10771 Ops.push_back(Chain);
10772 } else {
10773 Opc = AMDGPU::S_GET_BARRIER_STATE_M0;
10774 if (IntrID == Intrinsic::amdgcn_s_get_named_barrier_state) {
10775 SDValue M0Val;
10776 M0Val = DAG.getNode(ISD::SRL, DL, MVT::i32, Op->getOperand(2),
10777 DAG.getShiftAmountConstant(4, MVT::i32, DL));
10778 M0Val = SDValue(
10779 DAG.getMachineNode(AMDGPU::S_AND_B32, DL, MVT::i32, M0Val,
10780 DAG.getTargetConstant(0x3F, DL, MVT::i32)),
10781 0);
10782 Ops.push_back(copyToM0(DAG, Chain, DL, M0Val).getValue(0));
10783 } else
10784 Ops.push_back(copyToM0(DAG, Chain, DL, Op->getOperand(2)).getValue(0));
10785 }
10786
10787 auto *NewMI = DAG.getMachineNode(Opc, DL, Op->getVTList(), Ops);
10788 return SDValue(NewMI, 0);
10789 }
10790 case Intrinsic::amdgcn_cooperative_atomic_load_32x4B:
10791 case Intrinsic::amdgcn_cooperative_atomic_load_16x8B:
10792 case Intrinsic::amdgcn_cooperative_atomic_load_8x16B: {
10793 MemIntrinsicSDNode *MII = cast<MemIntrinsicSDNode>(Op);
10794 SDValue Chain = Op->getOperand(0);
10795 SDValue Ptr = Op->getOperand(2);
10796 EVT VT = Op->getValueType(0);
10797 return DAG.getAtomicLoad(ISD::NON_EXTLOAD, DL, MII->getMemoryVT(), VT,
10798 Chain, Ptr, MII->getMemOperand());
10799 }
10800 default:
10801
10802 if (const AMDGPU::ImageDimIntrinsicInfo *ImageDimIntr =
10804 return lowerImage(Op, ImageDimIntr, DAG, true);
10805
10806 return SDValue();
10807 }
10808}
10809
10810// Call DAG.getMemIntrinsicNode for a load, but first widen a dwordx3 type to
10811// dwordx4 if on SI and handle TFE loads.
10812SDValue SITargetLowering::getMemIntrinsicNode(unsigned Opcode, const SDLoc &DL,
10813 SDVTList VTList,
10814 ArrayRef<SDValue> Ops, EVT MemVT,
10815 MachineMemOperand *MMO,
10816 SelectionDAG &DAG) const {
10817 LLVMContext &C = *DAG.getContext();
10818 MachineFunction &MF = DAG.getMachineFunction();
10819 EVT VT = VTList.VTs[0];
10820
10821 assert(VTList.NumVTs == 2 || VTList.NumVTs == 3);
10822 bool IsTFE = VTList.NumVTs == 3;
10823 if (IsTFE) {
10824 unsigned NumValueDWords = divideCeil(VT.getSizeInBits(), 32);
10825 unsigned NumOpDWords = NumValueDWords + 1;
10826 EVT OpDWordsVT = EVT::getVectorVT(C, MVT::i32, NumOpDWords);
10827 SDVTList OpDWordsVTList = DAG.getVTList(OpDWordsVT, VTList.VTs[2]);
10828 MachineMemOperand *OpDWordsMMO =
10829 MF.getMachineMemOperand(MMO, 0, NumOpDWords * 4);
10830 SDValue Op = getMemIntrinsicNode(Opcode, DL, OpDWordsVTList, Ops,
10831 OpDWordsVT, OpDWordsMMO, DAG);
10832 SDValue Status = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Op,
10833 DAG.getVectorIdxConstant(NumValueDWords, DL));
10834 SDValue ZeroIdx = DAG.getVectorIdxConstant(0, DL);
10835 SDValue ValueDWords =
10836 NumValueDWords == 1
10837 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Op, ZeroIdx)
10839 EVT::getVectorVT(C, MVT::i32, NumValueDWords), Op,
10840 ZeroIdx);
10841 SDValue Value = DAG.getNode(ISD::BITCAST, DL, VT, ValueDWords);
10842 return DAG.getMergeValues({Value, Status, SDValue(Op.getNode(), 1)}, DL);
10843 }
10844
10845 if (!Subtarget->hasDwordx3LoadStores() &&
10846 (VT == MVT::v3i32 || VT == MVT::v3f32)) {
10847 EVT WidenedVT = EVT::getVectorVT(C, VT.getVectorElementType(), 4);
10848 EVT WidenedMemVT = EVT::getVectorVT(C, MemVT.getVectorElementType(), 4);
10849 MachineMemOperand *WidenedMMO = MF.getMachineMemOperand(MMO, 0, 16);
10850 SDVTList WidenedVTList = DAG.getVTList(WidenedVT, VTList.VTs[1]);
10851 SDValue Op = DAG.getMemIntrinsicNode(Opcode, DL, WidenedVTList, Ops,
10852 WidenedMemVT, WidenedMMO);
10854 DAG.getVectorIdxConstant(0, DL));
10855 return DAG.getMergeValues({Value, SDValue(Op.getNode(), 1)}, DL);
10856 }
10857
10858 return DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops, MemVT, MMO);
10859}
10860
10861SDValue SITargetLowering::handleD16VData(SDValue VData, SelectionDAG &DAG,
10862 bool ImageStore) const {
10863 EVT StoreVT = VData.getValueType();
10864
10865 // No change for f16 and legal vector D16 types.
10866 if (!StoreVT.isVector())
10867 return VData;
10868
10869 SDLoc DL(VData);
10870 unsigned NumElements = StoreVT.getVectorNumElements();
10871
10872 if (Subtarget->hasUnpackedD16VMem()) {
10873 // We need to unpack the packed data to store.
10874 EVT IntStoreVT = StoreVT.changeTypeToInteger();
10875 SDValue IntVData = DAG.getNode(ISD::BITCAST, DL, IntStoreVT, VData);
10876
10877 EVT EquivStoreVT =
10878 EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElements);
10879 SDValue ZExt = DAG.getNode(ISD::ZERO_EXTEND, DL, EquivStoreVT, IntVData);
10880 return DAG.UnrollVectorOp(ZExt.getNode());
10881 }
10882
10883 // The sq block of gfx8.1 does not estimate register use correctly for d16
10884 // image store instructions. The data operand is computed as if it were not a
10885 // d16 image instruction.
10886 if (ImageStore && Subtarget->hasImageStoreD16Bug()) {
10887 // Bitcast to i16
10888 EVT IntStoreVT = StoreVT.changeTypeToInteger();
10889 SDValue IntVData = DAG.getNode(ISD::BITCAST, DL, IntStoreVT, VData);
10890
10891 // Decompose into scalars
10893 DAG.ExtractVectorElements(IntVData, Elts);
10894
10895 // Group pairs of i16 into v2i16 and bitcast to i32
10896 SmallVector<SDValue, 4> PackedElts;
10897 for (unsigned I = 0; I < Elts.size() / 2; I += 1) {
10898 SDValue Pair =
10899 DAG.getBuildVector(MVT::v2i16, DL, {Elts[I * 2], Elts[I * 2 + 1]});
10900 SDValue IntPair = DAG.getNode(ISD::BITCAST, DL, MVT::i32, Pair);
10901 PackedElts.push_back(IntPair);
10902 }
10903 if ((NumElements % 2) == 1) {
10904 // Handle v3i16
10905 unsigned I = Elts.size() / 2;
10906 SDValue Pair = DAG.getBuildVector(MVT::v2i16, DL,
10907 {Elts[I * 2], DAG.getPOISON(MVT::i16)});
10908 SDValue IntPair = DAG.getNode(ISD::BITCAST, DL, MVT::i32, Pair);
10909 PackedElts.push_back(IntPair);
10910 }
10911
10912 // Pad using UNDEF
10913 PackedElts.resize(Elts.size(), DAG.getPOISON(MVT::i32));
10914
10915 // Build final vector
10916 EVT VecVT =
10917 EVT::getVectorVT(*DAG.getContext(), MVT::i32, PackedElts.size());
10918 return DAG.getBuildVector(VecVT, DL, PackedElts);
10919 }
10920
10921 if (NumElements == 3) {
10922 EVT IntStoreVT =
10924 SDValue IntVData = DAG.getNode(ISD::BITCAST, DL, IntStoreVT, VData);
10925
10926 EVT WidenedStoreVT = EVT::getVectorVT(
10927 *DAG.getContext(), StoreVT.getVectorElementType(), NumElements + 1);
10928 EVT WidenedIntVT = EVT::getIntegerVT(*DAG.getContext(),
10929 WidenedStoreVT.getStoreSizeInBits());
10930 SDValue ZExt = DAG.getNode(ISD::ZERO_EXTEND, DL, WidenedIntVT, IntVData);
10931 return DAG.getNode(ISD::BITCAST, DL, WidenedStoreVT, ZExt);
10932 }
10933
10934 assert(isTypeLegal(StoreVT));
10935 return VData;
10936}
10937
10938SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op,
10939 SelectionDAG &DAG) const {
10940 SDLoc DL(Op);
10941 SDValue Chain = Op.getOperand(0);
10942 unsigned IntrinsicID = Op.getConstantOperandVal(1);
10943 MachineFunction &MF = DAG.getMachineFunction();
10944
10945 switch (IntrinsicID) {
10946 case Intrinsic::amdgcn_exp_compr: {
10947 if (!Subtarget->hasCompressedExport()) {
10948 DAG.getContext()->diagnose(DiagnosticInfoUnsupported(
10950 "intrinsic not supported on subtarget", DL.getDebugLoc()));
10951 }
10952 SDValue Src0 = Op.getOperand(4);
10953 SDValue Src1 = Op.getOperand(5);
10954 // Hack around illegal type on SI by directly selecting it.
10955 if (isTypeLegal(Src0.getValueType()))
10956 return SDValue();
10957
10958 const ConstantSDNode *Done = cast<ConstantSDNode>(Op.getOperand(6));
10959 SDValue Undef = DAG.getPOISON(MVT::f32);
10960 const SDValue Ops[] = {
10961 Op.getOperand(2), // tgt
10962 DAG.getNode(ISD::BITCAST, DL, MVT::f32, Src0), // src0
10963 DAG.getNode(ISD::BITCAST, DL, MVT::f32, Src1), // src1
10964 Undef, // src2
10965 Undef, // src3
10966 Op.getOperand(7), // vm
10967 DAG.getTargetConstant(1, DL, MVT::i1), // compr
10968 Op.getOperand(3), // en
10969 Op.getOperand(0) // Chain
10970 };
10971
10972 unsigned Opc = Done->isZero() ? AMDGPU::EXP : AMDGPU::EXP_DONE;
10973 return SDValue(DAG.getMachineNode(Opc, DL, Op->getVTList(), Ops), 0);
10974 }
10975
10976 case Intrinsic::amdgcn_struct_tbuffer_store:
10977 case Intrinsic::amdgcn_struct_ptr_tbuffer_store: {
10978 SDValue VData = Op.getOperand(2);
10979 bool IsD16 = (VData.getValueType().getScalarType() == MVT::f16);
10980 if (IsD16)
10981 VData = handleD16VData(VData, DAG);
10982 SDValue Rsrc = bufferRsrcPtrToVector(Op.getOperand(3), DAG);
10983 auto [VOffset, Offset] = splitBufferOffsets(Op.getOperand(5), DAG);
10984 auto SOffset = selectSOffset(Op.getOperand(6), DAG, Subtarget);
10985 SDValue Ops[] = {
10986 Chain,
10987 VData, // vdata
10988 Rsrc, // rsrc
10989 Op.getOperand(4), // vindex
10990 VOffset, // voffset
10991 SOffset, // soffset
10992 Offset, // offset
10993 Op.getOperand(7), // format
10994 Op.getOperand(8), // cachepolicy, swizzled buffer
10995 DAG.getTargetConstant(1, DL, MVT::i1), // idxen
10996 };
10997 unsigned Opc = IsD16 ? AMDGPUISD::TBUFFER_STORE_FORMAT_D16
10999 MemSDNode *M = cast<MemSDNode>(Op);
11000 return DAG.getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops,
11001 M->getMemoryVT(), M->getMemOperand());
11002 }
11003
11004 case Intrinsic::amdgcn_raw_tbuffer_store:
11005 case Intrinsic::amdgcn_raw_ptr_tbuffer_store: {
11006 SDValue VData = Op.getOperand(2);
11007 bool IsD16 = (VData.getValueType().getScalarType() == MVT::f16);
11008 if (IsD16)
11009 VData = handleD16VData(VData, DAG);
11010 SDValue Rsrc = bufferRsrcPtrToVector(Op.getOperand(3), DAG);
11011 auto [VOffset, Offset] = splitBufferOffsets(Op.getOperand(4), DAG);
11012 auto SOffset = selectSOffset(Op.getOperand(5), DAG, Subtarget);
11013 SDValue Ops[] = {
11014 Chain,
11015 VData, // vdata
11016 Rsrc, // rsrc
11017 DAG.getConstant(0, DL, MVT::i32), // vindex
11018 VOffset, // voffset
11019 SOffset, // soffset
11020 Offset, // offset
11021 Op.getOperand(6), // format
11022 Op.getOperand(7), // cachepolicy, swizzled buffer
11023 DAG.getTargetConstant(0, DL, MVT::i1), // idxen
11024 };
11025 unsigned Opc = IsD16 ? AMDGPUISD::TBUFFER_STORE_FORMAT_D16
11027 MemSDNode *M = cast<MemSDNode>(Op);
11028 return DAG.getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops,
11029 M->getMemoryVT(), M->getMemOperand());
11030 }
11031
11032 case Intrinsic::amdgcn_raw_buffer_store:
11033 case Intrinsic::amdgcn_raw_ptr_buffer_store:
11034 case Intrinsic::amdgcn_raw_buffer_store_format:
11035 case Intrinsic::amdgcn_raw_ptr_buffer_store_format: {
11036 const bool IsFormat =
11037 IntrinsicID == Intrinsic::amdgcn_raw_buffer_store_format ||
11038 IntrinsicID == Intrinsic::amdgcn_raw_ptr_buffer_store_format;
11039
11040 SDValue VData = Op.getOperand(2);
11041 EVT VDataVT = VData.getValueType();
11042 EVT EltType = VDataVT.getScalarType();
11043 bool IsD16 = IsFormat && (EltType.getSizeInBits() == 16);
11044 if (IsD16) {
11045 VData = handleD16VData(VData, DAG);
11046 VDataVT = VData.getValueType();
11047 }
11048
11049 if (!isTypeLegal(VDataVT)) {
11050 VData =
11051 DAG.getNode(ISD::BITCAST, DL,
11052 getEquivalentMemType(*DAG.getContext(), VDataVT), VData);
11053 }
11054
11055 SDValue Rsrc = bufferRsrcPtrToVector(Op.getOperand(3), DAG);
11056 auto [VOffset, Offset] = splitBufferOffsets(Op.getOperand(4), DAG);
11057 auto SOffset = selectSOffset(Op.getOperand(5), DAG, Subtarget);
11058 SDValue Ops[] = {
11059 Chain,
11060 VData,
11061 Rsrc,
11062 DAG.getConstant(0, DL, MVT::i32), // vindex
11063 VOffset, // voffset
11064 SOffset, // soffset
11065 Offset, // offset
11066 Op.getOperand(6), // cachepolicy, swizzled buffer
11067 DAG.getTargetConstant(0, DL, MVT::i1), // idxen
11068 };
11069 unsigned Opc =
11072 MemSDNode *M = cast<MemSDNode>(Op);
11073
11074 // Handle BUFFER_STORE_BYTE/SHORT overloaded intrinsics
11075 if (!IsD16 && !VDataVT.isVector() && EltType.getSizeInBits() < 32)
11076 return handleByteShortBufferStores(DAG, VDataVT, DL, Ops, M);
11077
11078 return DAG.getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops,
11079 M->getMemoryVT(), M->getMemOperand());
11080 }
11081
11082 case Intrinsic::amdgcn_struct_buffer_store:
11083 case Intrinsic::amdgcn_struct_ptr_buffer_store:
11084 case Intrinsic::amdgcn_struct_buffer_store_format:
11085 case Intrinsic::amdgcn_struct_ptr_buffer_store_format: {
11086 const bool IsFormat =
11087 IntrinsicID == Intrinsic::amdgcn_struct_buffer_store_format ||
11088 IntrinsicID == Intrinsic::amdgcn_struct_ptr_buffer_store_format;
11089
11090 SDValue VData = Op.getOperand(2);
11091 EVT VDataVT = VData.getValueType();
11092 EVT EltType = VDataVT.getScalarType();
11093 bool IsD16 = IsFormat && (EltType.getSizeInBits() == 16);
11094
11095 if (IsD16) {
11096 VData = handleD16VData(VData, DAG);
11097 VDataVT = VData.getValueType();
11098 }
11099
11100 if (!isTypeLegal(VDataVT)) {
11101 VData =
11102 DAG.getNode(ISD::BITCAST, DL,
11103 getEquivalentMemType(*DAG.getContext(), VDataVT), VData);
11104 }
11105
11106 auto Rsrc = bufferRsrcPtrToVector(Op.getOperand(3), DAG);
11107 auto [VOffset, Offset] = splitBufferOffsets(Op.getOperand(5), DAG);
11108 auto SOffset = selectSOffset(Op.getOperand(6), DAG, Subtarget);
11109 SDValue Ops[] = {
11110 Chain,
11111 VData,
11112 Rsrc,
11113 Op.getOperand(4), // vindex
11114 VOffset, // voffset
11115 SOffset, // soffset
11116 Offset, // offset
11117 Op.getOperand(7), // cachepolicy, swizzled buffer
11118 DAG.getTargetConstant(1, DL, MVT::i1), // idxen
11119 };
11120 unsigned Opc =
11123 MemSDNode *M = cast<MemSDNode>(Op);
11124
11125 // Handle BUFFER_STORE_BYTE/SHORT overloaded intrinsics
11126 EVT VDataType = VData.getValueType().getScalarType();
11127 if (!IsD16 && !VDataVT.isVector() && EltType.getSizeInBits() < 32)
11128 return handleByteShortBufferStores(DAG, VDataType, DL, Ops, M);
11129
11130 return DAG.getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops,
11131 M->getMemoryVT(), M->getMemOperand());
11132 }
11133 case Intrinsic::amdgcn_raw_buffer_load_lds:
11134 case Intrinsic::amdgcn_raw_ptr_buffer_load_lds:
11135 case Intrinsic::amdgcn_struct_buffer_load_lds:
11136 case Intrinsic::amdgcn_struct_ptr_buffer_load_lds: {
11137 if (!Subtarget->hasVMemToLDSLoad())
11138 return SDValue();
11139 unsigned Opc;
11140 bool HasVIndex =
11141 IntrinsicID == Intrinsic::amdgcn_struct_buffer_load_lds ||
11142 IntrinsicID == Intrinsic::amdgcn_struct_ptr_buffer_load_lds;
11143 unsigned OpOffset = HasVIndex ? 1 : 0;
11144 SDValue VOffset = Op.getOperand(5 + OpOffset);
11145 bool HasVOffset = !isNullConstant(VOffset);
11146 unsigned Size = Op->getConstantOperandVal(4);
11147
11148 switch (Size) {
11149 default:
11150 return SDValue();
11151 case 1:
11152 Opc = HasVIndex ? HasVOffset ? AMDGPU::BUFFER_LOAD_UBYTE_LDS_BOTHEN
11153 : AMDGPU::BUFFER_LOAD_UBYTE_LDS_IDXEN
11154 : HasVOffset ? AMDGPU::BUFFER_LOAD_UBYTE_LDS_OFFEN
11155 : AMDGPU::BUFFER_LOAD_UBYTE_LDS_OFFSET;
11156 break;
11157 case 2:
11158 Opc = HasVIndex ? HasVOffset ? AMDGPU::BUFFER_LOAD_USHORT_LDS_BOTHEN
11159 : AMDGPU::BUFFER_LOAD_USHORT_LDS_IDXEN
11160 : HasVOffset ? AMDGPU::BUFFER_LOAD_USHORT_LDS_OFFEN
11161 : AMDGPU::BUFFER_LOAD_USHORT_LDS_OFFSET;
11162 break;
11163 case 4:
11164 Opc = HasVIndex ? HasVOffset ? AMDGPU::BUFFER_LOAD_DWORD_LDS_BOTHEN
11165 : AMDGPU::BUFFER_LOAD_DWORD_LDS_IDXEN
11166 : HasVOffset ? AMDGPU::BUFFER_LOAD_DWORD_LDS_OFFEN
11167 : AMDGPU::BUFFER_LOAD_DWORD_LDS_OFFSET;
11168 break;
11169 case 12:
11170 if (!Subtarget->hasLDSLoadB96_B128())
11171 return SDValue();
11172 Opc = HasVIndex ? HasVOffset ? AMDGPU::BUFFER_LOAD_DWORDX3_LDS_BOTHEN
11173 : AMDGPU::BUFFER_LOAD_DWORDX3_LDS_IDXEN
11174 : HasVOffset ? AMDGPU::BUFFER_LOAD_DWORDX3_LDS_OFFEN
11175 : AMDGPU::BUFFER_LOAD_DWORDX3_LDS_OFFSET;
11176 break;
11177 case 16:
11178 if (!Subtarget->hasLDSLoadB96_B128())
11179 return SDValue();
11180 Opc = HasVIndex ? HasVOffset ? AMDGPU::BUFFER_LOAD_DWORDX4_LDS_BOTHEN
11181 : AMDGPU::BUFFER_LOAD_DWORDX4_LDS_IDXEN
11182 : HasVOffset ? AMDGPU::BUFFER_LOAD_DWORDX4_LDS_OFFEN
11183 : AMDGPU::BUFFER_LOAD_DWORDX4_LDS_OFFSET;
11184 break;
11185 }
11186
11187 SDValue M0Val = copyToM0(DAG, Chain, DL, Op.getOperand(3));
11188
11190
11191 if (HasVIndex && HasVOffset)
11192 Ops.push_back(DAG.getBuildVector(MVT::v2i32, DL,
11193 {Op.getOperand(5), // VIndex
11194 VOffset}));
11195 else if (HasVIndex)
11196 Ops.push_back(Op.getOperand(5));
11197 else if (HasVOffset)
11198 Ops.push_back(VOffset);
11199
11200 SDValue Rsrc = bufferRsrcPtrToVector(Op.getOperand(2), DAG);
11201 Ops.push_back(Rsrc);
11202 Ops.push_back(Op.getOperand(6 + OpOffset)); // soffset
11203 Ops.push_back(Op.getOperand(7 + OpOffset)); // imm offset
11204 bool IsGFX12Plus = AMDGPU::isGFX12Plus(*Subtarget);
11205 unsigned Aux = Op.getConstantOperandVal(8 + OpOffset);
11206 Ops.push_back(DAG.getTargetConstant(
11207 Aux & (IsGFX12Plus ? AMDGPU::CPol::ALL : AMDGPU::CPol::ALL_pregfx12),
11208 DL, MVT::i8)); // cpol
11209 Ops.push_back(DAG.getTargetConstant(
11210 Aux & (IsGFX12Plus ? AMDGPU::CPol::SWZ : AMDGPU::CPol::SWZ_pregfx12)
11211 ? 1
11212 : 0,
11213 DL, MVT::i8)); // swz
11214 Ops.push_back(M0Val.getValue(0)); // Chain
11215 Ops.push_back(M0Val.getValue(1)); // Glue
11216
11217 auto *M = cast<MemSDNode>(Op);
11218 MachineMemOperand *LoadMMO = M->getMemOperand();
11219 // Don't set the offset value here because the pointer points to the base of
11220 // the buffer.
11221 MachinePointerInfo LoadPtrI = LoadMMO->getPointerInfo();
11222
11223 MachinePointerInfo StorePtrI = LoadPtrI;
11224 LoadPtrI.V = PoisonValue::get(
11228
11229 auto F = LoadMMO->getFlags() &
11231 LoadMMO =
11233 LoadMMO->getBaseAlign(), LoadMMO->getAAInfo());
11234
11235 MachineMemOperand *StoreMMO = MF.getMachineMemOperand(
11236 StorePtrI, F | MachineMemOperand::MOStore, sizeof(int32_t),
11237 LoadMMO->getBaseAlign(), LoadMMO->getAAInfo());
11238
11239 auto *Load = DAG.getMachineNode(Opc, DL, M->getVTList(), Ops);
11240 DAG.setNodeMemRefs(Load, {LoadMMO, StoreMMO});
11241
11242 return SDValue(Load, 0);
11243 }
11244 // Buffers are handled by LowerBufferFatPointers, and we're going to go
11245 // for "trust me" that the remaining cases are global pointers until
11246 // such time as we can put two mem operands on an intrinsic.
11247 case Intrinsic::amdgcn_load_to_lds:
11248 case Intrinsic::amdgcn_global_load_lds: {
11249 if (!Subtarget->hasVMemToLDSLoad())
11250 return SDValue();
11251
11252 unsigned Opc;
11253 unsigned Size = Op->getConstantOperandVal(4);
11254 switch (Size) {
11255 default:
11256 return SDValue();
11257 case 1:
11258 Opc = AMDGPU::GLOBAL_LOAD_LDS_UBYTE;
11259 break;
11260 case 2:
11261 Opc = AMDGPU::GLOBAL_LOAD_LDS_USHORT;
11262 break;
11263 case 4:
11264 Opc = AMDGPU::GLOBAL_LOAD_LDS_DWORD;
11265 break;
11266 case 12:
11267 if (!Subtarget->hasLDSLoadB96_B128())
11268 return SDValue();
11269 Opc = AMDGPU::GLOBAL_LOAD_LDS_DWORDX3;
11270 break;
11271 case 16:
11272 if (!Subtarget->hasLDSLoadB96_B128())
11273 return SDValue();
11274 Opc = AMDGPU::GLOBAL_LOAD_LDS_DWORDX4;
11275 break;
11276 }
11277
11278 SDValue M0Val = copyToM0(DAG, Chain, DL, Op.getOperand(3));
11279
11281
11282 SDValue Addr = Op.getOperand(2); // Global ptr
11283 SDValue VOffset;
11284 // Try to split SAddr and VOffset. Global and LDS pointers share the same
11285 // immediate offset, so we cannot use a regular SelectGlobalSAddr().
11286 if (Addr->isDivergent() && Addr->isAnyAdd()) {
11287 SDValue LHS = Addr.getOperand(0);
11288 SDValue RHS = Addr.getOperand(1);
11289
11290 if (LHS->isDivergent())
11291 std::swap(LHS, RHS);
11292
11293 if (!LHS->isDivergent() && RHS.getOpcode() == ISD::ZERO_EXTEND &&
11294 RHS.getOperand(0).getValueType() == MVT::i32) {
11295 // add (i64 sgpr), (zero_extend (i32 vgpr))
11296 Addr = LHS;
11297 VOffset = RHS.getOperand(0);
11298 }
11299 }
11300
11301 Ops.push_back(Addr);
11302 if (!Addr->isDivergent()) {
11304 if (!VOffset)
11305 VOffset =
11306 SDValue(DAG.getMachineNode(AMDGPU::V_MOV_B32_e32, DL, MVT::i32,
11307 DAG.getTargetConstant(0, DL, MVT::i32)),
11308 0);
11309 Ops.push_back(VOffset);
11310 }
11311
11312 Ops.push_back(Op.getOperand(5)); // Offset
11313
11314 unsigned Aux = Op.getConstantOperandVal(6);
11315 Ops.push_back(DAG.getTargetConstant(Aux & ~AMDGPU::CPol::VIRTUAL_BITS, DL,
11316 MVT::i32)); // CPol
11317
11318 Ops.push_back(M0Val.getValue(0)); // Chain
11319 Ops.push_back(M0Val.getValue(1)); // Glue
11320
11321 auto *M = cast<MemSDNode>(Op);
11322 MachineMemOperand *LoadMMO = M->getMemOperand();
11323 MachinePointerInfo LoadPtrI = LoadMMO->getPointerInfo();
11324 LoadPtrI.Offset = Op->getConstantOperandVal(5);
11325 MachinePointerInfo StorePtrI = LoadPtrI;
11326 LoadPtrI.V = PoisonValue::get(
11330 auto F = LoadMMO->getFlags() &
11332 LoadMMO =
11334 LoadMMO->getBaseAlign(), LoadMMO->getAAInfo());
11335 MachineMemOperand *StoreMMO = MF.getMachineMemOperand(
11336 StorePtrI, F | MachineMemOperand::MOStore, sizeof(int32_t), Align(4),
11337 LoadMMO->getAAInfo());
11338
11339 auto *Load = DAG.getMachineNode(Opc, DL, Op->getVTList(), Ops);
11340 DAG.setNodeMemRefs(Load, {LoadMMO, StoreMMO});
11341
11342 return SDValue(Load, 0);
11343 }
11344 case Intrinsic::amdgcn_end_cf:
11345 return SDValue(DAG.getMachineNode(AMDGPU::SI_END_CF, DL, MVT::Other,
11346 Op->getOperand(2), Chain),
11347 0);
11348 case Intrinsic::amdgcn_s_barrier_init:
11349 case Intrinsic::amdgcn_s_barrier_signal_var: {
11350 // these two intrinsics have two operands: barrier pointer and member count
11351 SDValue Chain = Op->getOperand(0);
11353 SDValue BarOp = Op->getOperand(2);
11354 SDValue CntOp = Op->getOperand(3);
11355 SDValue M0Val;
11356 unsigned Opc = IntrinsicID == Intrinsic::amdgcn_s_barrier_init
11357 ? AMDGPU::S_BARRIER_INIT_M0
11358 : AMDGPU::S_BARRIER_SIGNAL_M0;
11359 // extract the BarrierID from bits 4-9 of BarOp
11360 SDValue BarID;
11361 BarID = DAG.getNode(ISD::SRL, DL, MVT::i32, BarOp,
11362 DAG.getShiftAmountConstant(4, MVT::i32, DL));
11363 BarID =
11364 SDValue(DAG.getMachineNode(AMDGPU::S_AND_B32, DL, MVT::i32, BarID,
11365 DAG.getTargetConstant(0x3F, DL, MVT::i32)),
11366 0);
11367 // Member count should be put into M0[ShAmt:+6]
11368 // Barrier ID should be put into M0[5:0]
11369 M0Val =
11370 SDValue(DAG.getMachineNode(AMDGPU::S_AND_B32, DL, MVT::i32, CntOp,
11371 DAG.getTargetConstant(0x3F, DL, MVT::i32)),
11372 0);
11373 constexpr unsigned ShAmt = 16;
11374 M0Val = DAG.getNode(ISD::SHL, DL, MVT::i32, CntOp,
11375 DAG.getShiftAmountConstant(ShAmt, MVT::i32, DL));
11376
11377 M0Val = SDValue(
11378 DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, M0Val, BarID), 0);
11379
11380 Ops.push_back(copyToM0(DAG, Chain, DL, M0Val).getValue(0));
11381
11382 auto *NewMI = DAG.getMachineNode(Opc, DL, Op->getVTList(), Ops);
11383 return SDValue(NewMI, 0);
11384 }
11385 case Intrinsic::amdgcn_s_barrier_join: {
11386 // these three intrinsics have one operand: barrier pointer
11387 SDValue Chain = Op->getOperand(0);
11389 SDValue BarOp = Op->getOperand(2);
11390 unsigned Opc;
11391
11392 if (isa<ConstantSDNode>(BarOp)) {
11393 uint64_t BarVal = cast<ConstantSDNode>(BarOp)->getZExtValue();
11394 Opc = AMDGPU::S_BARRIER_JOIN_IMM;
11395
11396 // extract the BarrierID from bits 4-9 of the immediate
11397 unsigned BarID = (BarVal >> 4) & 0x3F;
11398 SDValue K = DAG.getTargetConstant(BarID, DL, MVT::i32);
11399 Ops.push_back(K);
11400 Ops.push_back(Chain);
11401 } else {
11402 Opc = AMDGPU::S_BARRIER_JOIN_M0;
11403
11404 // extract the BarrierID from bits 4-9 of BarOp, copy to M0[5:0]
11405 SDValue M0Val;
11406 M0Val = DAG.getNode(ISD::SRL, DL, MVT::i32, BarOp,
11407 DAG.getShiftAmountConstant(4, MVT::i32, DL));
11408 M0Val =
11409 SDValue(DAG.getMachineNode(AMDGPU::S_AND_B32, DL, MVT::i32, M0Val,
11410 DAG.getTargetConstant(0x3F, DL, MVT::i32)),
11411 0);
11412 Ops.push_back(copyToM0(DAG, Chain, DL, M0Val).getValue(0));
11413 }
11414
11415 auto *NewMI = DAG.getMachineNode(Opc, DL, Op->getVTList(), Ops);
11416 return SDValue(NewMI, 0);
11417 }
11418 case Intrinsic::amdgcn_s_prefetch_data: {
11419 // For non-global address space preserve the chain and remove the call.
11421 return Op.getOperand(0);
11422 return Op;
11423 }
11424 case Intrinsic::amdgcn_s_buffer_prefetch_data: {
11425 SDValue Ops[] = {
11426 Chain, bufferRsrcPtrToVector(Op.getOperand(2), DAG),
11427 Op.getOperand(3), // offset
11428 Op.getOperand(4), // length
11429 };
11430
11431 MemSDNode *M = cast<MemSDNode>(Op);
11433 Op->getVTList(), Ops, M->getMemoryVT(),
11434 M->getMemOperand());
11435 }
11436 case Intrinsic::amdgcn_cooperative_atomic_store_32x4B:
11437 case Intrinsic::amdgcn_cooperative_atomic_store_16x8B:
11438 case Intrinsic::amdgcn_cooperative_atomic_store_8x16B: {
11439 MemIntrinsicSDNode *MII = cast<MemIntrinsicSDNode>(Op);
11440 SDValue Chain = Op->getOperand(0);
11441 SDValue Ptr = Op->getOperand(2);
11442 SDValue Val = Op->getOperand(3);
11443 return DAG.getAtomic(ISD::ATOMIC_STORE, DL, MII->getMemoryVT(), Chain, Val,
11444 Ptr, MII->getMemOperand());
11445 }
11446 default: {
11447 if (const AMDGPU::ImageDimIntrinsicInfo *ImageDimIntr =
11449 return lowerImage(Op, ImageDimIntr, DAG, true);
11450
11451 return Op;
11452 }
11453 }
11454}
11455
11456// Return whether the operation has NoUnsignedWrap property.
11457static bool isNoUnsignedWrap(SDValue Addr) {
11458 return (Addr.getOpcode() == ISD::ADD &&
11459 Addr->getFlags().hasNoUnsignedWrap()) ||
11460 Addr->getOpcode() == ISD::OR;
11461}
11462
11464 EVT PtrVT) const {
11465 return PtrVT == MVT::i64;
11466}
11467
11469 EVT PtrVT) const {
11470 return true;
11471}
11472
11473// The raw.(t)buffer and struct.(t)buffer intrinsics have two offset args:
11474// offset (the offset that is included in bounds checking and swizzling, to be
11475// split between the instruction's voffset and immoffset fields) and soffset
11476// (the offset that is excluded from bounds checking and swizzling, to go in
11477// the instruction's soffset field). This function takes the first kind of
11478// offset and figures out how to split it between voffset and immoffset.
11479std::pair<SDValue, SDValue>
11480SITargetLowering::splitBufferOffsets(SDValue Offset, SelectionDAG &DAG) const {
11481 SDLoc DL(Offset);
11482 const unsigned MaxImm = SIInstrInfo::getMaxMUBUFImmOffset(*Subtarget);
11483 SDValue N0 = Offset;
11484 ConstantSDNode *C1 = nullptr;
11485
11486 if ((C1 = dyn_cast<ConstantSDNode>(N0)))
11487 N0 = SDValue();
11488 else if (DAG.isBaseWithConstantOffset(N0)) {
11489 // On GFX1250+, voffset and immoffset are zero-extended from 32 bits before
11490 // being added, so we can only safely match a 32-bit addition with no
11491 // unsigned overflow.
11492 bool CheckNUW = AMDGPU::isGFX1250(*Subtarget);
11493 if (!CheckNUW || isNoUnsignedWrap(N0)) {
11494 C1 = cast<ConstantSDNode>(N0.getOperand(1));
11495 N0 = N0.getOperand(0);
11496 }
11497 }
11498
11499 if (C1) {
11500 unsigned ImmOffset = C1->getZExtValue();
11501 // If the immediate value is too big for the immoffset field, put only bits
11502 // that would normally fit in the immoffset field. The remaining value that
11503 // is copied/added for the voffset field is a large power of 2, and it
11504 // stands more chance of being CSEd with the copy/add for another similar
11505 // load/store.
11506 // However, do not do that rounding down if that is a negative
11507 // number, as it appears to be illegal to have a negative offset in the
11508 // vgpr, even if adding the immediate offset makes it positive.
11509 unsigned Overflow = ImmOffset & ~MaxImm;
11510 ImmOffset -= Overflow;
11511 if ((int32_t)Overflow < 0) {
11512 Overflow += ImmOffset;
11513 ImmOffset = 0;
11514 }
11515 C1 = cast<ConstantSDNode>(DAG.getTargetConstant(ImmOffset, DL, MVT::i32));
11516 if (Overflow) {
11517 auto OverflowVal = DAG.getConstant(Overflow, DL, MVT::i32);
11518 if (!N0)
11519 N0 = OverflowVal;
11520 else {
11521 SDValue Ops[] = {N0, OverflowVal};
11522 N0 = DAG.getNode(ISD::ADD, DL, MVT::i32, Ops);
11523 }
11524 }
11525 }
11526 if (!N0)
11527 N0 = DAG.getConstant(0, DL, MVT::i32);
11528 if (!C1)
11529 C1 = cast<ConstantSDNode>(DAG.getTargetConstant(0, DL, MVT::i32));
11530 return {N0, SDValue(C1, 0)};
11531}
11532
11533// Analyze a combined offset from an amdgcn_s_buffer_load intrinsic and store
11534// the three offsets (voffset, soffset and instoffset) into the SDValue[3] array
11535// pointed to by Offsets.
11536void SITargetLowering::setBufferOffsets(SDValue CombinedOffset,
11537 SelectionDAG &DAG, SDValue *Offsets,
11538 Align Alignment) const {
11539 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
11540 SDLoc DL(CombinedOffset);
11541 if (auto *C = dyn_cast<ConstantSDNode>(CombinedOffset)) {
11542 uint32_t Imm = C->getZExtValue();
11543 uint32_t SOffset, ImmOffset;
11544 if (TII->splitMUBUFOffset(Imm, SOffset, ImmOffset, Alignment)) {
11545 Offsets[0] = DAG.getConstant(0, DL, MVT::i32);
11546 Offsets[1] = DAG.getConstant(SOffset, DL, MVT::i32);
11547 Offsets[2] = DAG.getTargetConstant(ImmOffset, DL, MVT::i32);
11548 return;
11549 }
11550 }
11551 if (DAG.isBaseWithConstantOffset(CombinedOffset)) {
11552 SDValue N0 = CombinedOffset.getOperand(0);
11553 SDValue N1 = CombinedOffset.getOperand(1);
11554 uint32_t SOffset, ImmOffset;
11555 int Offset = cast<ConstantSDNode>(N1)->getSExtValue();
11556 if (Offset >= 0 &&
11557 TII->splitMUBUFOffset(Offset, SOffset, ImmOffset, Alignment)) {
11558 Offsets[0] = N0;
11559 Offsets[1] = DAG.getConstant(SOffset, DL, MVT::i32);
11560 Offsets[2] = DAG.getTargetConstant(ImmOffset, DL, MVT::i32);
11561 return;
11562 }
11563 }
11564
11565 SDValue SOffsetZero = Subtarget->hasRestrictedSOffset()
11566 ? DAG.getRegister(AMDGPU::SGPR_NULL, MVT::i32)
11567 : DAG.getConstant(0, DL, MVT::i32);
11568
11569 Offsets[0] = CombinedOffset;
11570 Offsets[1] = SOffsetZero;
11571 Offsets[2] = DAG.getTargetConstant(0, DL, MVT::i32);
11572}
11573
11574SDValue SITargetLowering::bufferRsrcPtrToVector(SDValue MaybePointer,
11575 SelectionDAG &DAG) const {
11576 if (!MaybePointer.getValueType().isScalarInteger())
11577 return MaybePointer;
11578
11579 SDValue Rsrc = DAG.getBitcast(MVT::v4i32, MaybePointer);
11580 return Rsrc;
11581}
11582
11583// Wrap a global or flat pointer into a buffer intrinsic using the flags
11584// specified in the intrinsic.
11585SDValue SITargetLowering::lowerPointerAsRsrcIntrin(SDNode *Op,
11586 SelectionDAG &DAG) const {
11587 SDLoc Loc(Op);
11588
11589 SDValue Pointer = Op->getOperand(1);
11590 SDValue Stride = Op->getOperand(2);
11591 SDValue NumRecords = Op->getOperand(3);
11592 SDValue Flags = Op->getOperand(4);
11593
11594 SDValue ExtStride = DAG.getAnyExtOrTrunc(Stride, Loc, MVT::i32);
11595 SDValue Rsrc;
11596
11597 if (Subtarget->has45BitNumRecordsBufferResource()) {
11598 SDValue Zero = DAG.getConstant(0, Loc, MVT::i32);
11599 // Build the lower 64-bit value, which has a 57-bit base and the lower 7-bit
11600 // num_records.
11601 SDValue ExtPointer = DAG.getAnyExtOrTrunc(Pointer, Loc, MVT::i64);
11602 SDValue NumRecordsLHS =
11603 DAG.getNode(ISD::SHL, Loc, MVT::i64, NumRecords,
11604 DAG.getShiftAmountConstant(57, MVT::i32, Loc));
11605 SDValue LowHalf =
11606 DAG.getNode(ISD::OR, Loc, MVT::i64, ExtPointer, NumRecordsLHS);
11607
11608 // Build the higher 64-bit value, which has the higher 38-bit num_records,
11609 // 6-bit zero (omit), 16-bit stride and scale and 4-bit flag.
11610 SDValue NumRecordsRHS =
11611 DAG.getNode(ISD::SRL, Loc, MVT::i64, NumRecords,
11612 DAG.getShiftAmountConstant(7, MVT::i32, Loc));
11613 SDValue ShiftedStride =
11614 DAG.getNode(ISD::SHL, Loc, MVT::i32, ExtStride,
11615 DAG.getShiftAmountConstant(12, MVT::i32, Loc));
11616 SDValue ExtShiftedStrideVec =
11617 DAG.getNode(ISD::BUILD_VECTOR, Loc, MVT::v2i32, Zero, ShiftedStride);
11618 SDValue ExtShiftedStride =
11619 DAG.getNode(ISD::BITCAST, Loc, MVT::i64, ExtShiftedStrideVec);
11620 SDValue ShiftedFlags =
11621 DAG.getNode(ISD::SHL, Loc, MVT::i32, Flags,
11622 DAG.getShiftAmountConstant(28, MVT::i32, Loc));
11623 SDValue ExtShiftedFlagsVec =
11624 DAG.getNode(ISD::BUILD_VECTOR, Loc, MVT::v2i32, Zero, ShiftedFlags);
11625 SDValue ExtShiftedFlags =
11626 DAG.getNode(ISD::BITCAST, Loc, MVT::i64, ExtShiftedFlagsVec);
11627 SDValue CombinedFields =
11628 DAG.getNode(ISD::OR, Loc, MVT::i64, NumRecordsRHS, ExtShiftedStride);
11629 SDValue HighHalf =
11630 DAG.getNode(ISD::OR, Loc, MVT::i64, CombinedFields, ExtShiftedFlags);
11631
11632 Rsrc = DAG.getNode(ISD::BUILD_VECTOR, Loc, MVT::v2i64, LowHalf, HighHalf);
11633 } else {
11634 NumRecords = DAG.getAnyExtOrTrunc(NumRecords, Loc, MVT::i32);
11635 auto [LowHalf, HighHalf] =
11636 DAG.SplitScalar(Pointer, Loc, MVT::i32, MVT::i32);
11637 SDValue Mask = DAG.getConstant(0x0000ffff, Loc, MVT::i32);
11638 SDValue Masked = DAG.getNode(ISD::AND, Loc, MVT::i32, HighHalf, Mask);
11639 SDValue ShiftedStride =
11640 DAG.getNode(ISD::SHL, Loc, MVT::i32, ExtStride,
11641 DAG.getShiftAmountConstant(16, MVT::i32, Loc));
11642 SDValue NewHighHalf =
11643 DAG.getNode(ISD::OR, Loc, MVT::i32, Masked, ShiftedStride);
11644
11645 Rsrc = DAG.getNode(ISD::BUILD_VECTOR, Loc, MVT::v4i32, LowHalf, NewHighHalf,
11646 NumRecords, Flags);
11647 }
11648
11649 SDValue RsrcPtr = DAG.getNode(ISD::BITCAST, Loc, MVT::i128, Rsrc);
11650 return RsrcPtr;
11651}
11652
11653// Handle 8 bit and 16 bit buffer loads
11654SDValue SITargetLowering::handleByteShortBufferLoads(SelectionDAG &DAG,
11655 EVT LoadVT, SDLoc DL,
11657 MachineMemOperand *MMO,
11658 bool IsTFE) const {
11659 EVT IntVT = LoadVT.changeTypeToInteger();
11660
11661 if (IsTFE) {
11662 unsigned Opc = (LoadVT.getScalarType() == MVT::i8)
11665 MachineFunction &MF = DAG.getMachineFunction();
11666 MachineMemOperand *OpMMO = MF.getMachineMemOperand(MMO, 0, 8);
11667 SDVTList VTs = DAG.getVTList(MVT::v2i32, MVT::Other);
11668 SDValue Op = getMemIntrinsicNode(Opc, DL, VTs, Ops, MVT::v2i32, OpMMO, DAG);
11669 SDValue Status = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Op,
11670 DAG.getConstant(1, DL, MVT::i32));
11671 SDValue Data = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Op,
11672 DAG.getConstant(0, DL, MVT::i32));
11673 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, IntVT, Data);
11674 SDValue Value = DAG.getNode(ISD::BITCAST, DL, LoadVT, Trunc);
11675 return DAG.getMergeValues({Value, Status, SDValue(Op.getNode(), 1)}, DL);
11676 }
11677
11678 unsigned Opc = LoadVT.getScalarType() == MVT::i8
11681
11682 SDVTList ResList = DAG.getVTList(MVT::i32, MVT::Other);
11683 SDValue BufferLoad =
11684 DAG.getMemIntrinsicNode(Opc, DL, ResList, Ops, IntVT, MMO);
11685 SDValue LoadVal = DAG.getNode(ISD::TRUNCATE, DL, IntVT, BufferLoad);
11686 LoadVal = DAG.getNode(ISD::BITCAST, DL, LoadVT, LoadVal);
11687
11688 return DAG.getMergeValues({LoadVal, BufferLoad.getValue(1)}, DL);
11689}
11690
11691// Handle 8 bit and 16 bit buffer stores
11692SDValue SITargetLowering::handleByteShortBufferStores(SelectionDAG &DAG,
11693 EVT VDataType, SDLoc DL,
11694 SDValue Ops[],
11695 MemSDNode *M) const {
11696 if (VDataType == MVT::f16 || VDataType == MVT::bf16)
11697 Ops[1] = DAG.getNode(ISD::BITCAST, DL, MVT::i16, Ops[1]);
11698
11699 SDValue BufferStoreExt = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, Ops[1]);
11700 Ops[1] = BufferStoreExt;
11701 unsigned Opc = (VDataType == MVT::i8) ? AMDGPUISD::BUFFER_STORE_BYTE
11702 : AMDGPUISD::BUFFER_STORE_SHORT;
11703 ArrayRef<SDValue> OpsRef = ArrayRef(&Ops[0], 9);
11704 return DAG.getMemIntrinsicNode(Opc, DL, M->getVTList(), OpsRef, VDataType,
11705 M->getMemOperand());
11706}
11707
11709 SDValue Op, const SDLoc &SL, EVT VT) {
11710 if (VT.bitsLT(Op.getValueType()))
11711 return DAG.getNode(ISD::TRUNCATE, SL, VT, Op);
11712
11713 switch (ExtType) {
11714 case ISD::SEXTLOAD:
11715 return DAG.getNode(ISD::SIGN_EXTEND, SL, VT, Op);
11716 case ISD::ZEXTLOAD:
11717 return DAG.getNode(ISD::ZERO_EXTEND, SL, VT, Op);
11718 case ISD::EXTLOAD:
11719 return DAG.getNode(ISD::ANY_EXTEND, SL, VT, Op);
11720 case ISD::NON_EXTLOAD:
11721 return Op;
11722 }
11723
11724 llvm_unreachable("invalid ext type");
11725}
11726
11727// Try to turn 8 and 16-bit scalar loads into SMEM eligible 32-bit loads.
11728// TODO: Skip this on GFX12 which does have scalar sub-dword loads.
11729SDValue SITargetLowering::widenLoad(LoadSDNode *Ld,
11730 DAGCombinerInfo &DCI) const {
11731 SelectionDAG &DAG = DCI.DAG;
11732 if (Ld->getAlign() < Align(4) || Ld->isDivergent())
11733 return SDValue();
11734
11735 // FIXME: Constant loads should all be marked invariant.
11736 unsigned AS = Ld->getAddressSpace();
11737 if (AS != AMDGPUAS::CONSTANT_ADDRESS &&
11739 (AS != AMDGPUAS::GLOBAL_ADDRESS || !Ld->isInvariant()))
11740 return SDValue();
11741
11742 // Don't do this early, since it may interfere with adjacent load merging for
11743 // illegal types. We can avoid losing alignment information for exotic types
11744 // pre-legalize.
11745 EVT MemVT = Ld->getMemoryVT();
11746 if ((MemVT.isSimple() && !DCI.isAfterLegalizeDAG()) ||
11747 MemVT.getSizeInBits() >= 32)
11748 return SDValue();
11749
11750 SDLoc SL(Ld);
11751
11752 assert((!MemVT.isVector() || Ld->getExtensionType() == ISD::NON_EXTLOAD) &&
11753 "unexpected vector extload");
11754
11755 // TODO: Drop only high part of range.
11756 SDValue Ptr = Ld->getBasePtr();
11757 SDValue NewLoad = DAG.getLoad(
11758 ISD::UNINDEXED, ISD::NON_EXTLOAD, MVT::i32, SL, Ld->getChain(), Ptr,
11759 Ld->getOffset(), Ld->getPointerInfo(), MVT::i32, Ld->getAlign(),
11760 Ld->getMemOperand()->getFlags(), Ld->getAAInfo(),
11761 nullptr); // Drop ranges
11762
11763 EVT TruncVT = EVT::getIntegerVT(*DAG.getContext(), MemVT.getSizeInBits());
11764 if (MemVT.isFloatingPoint()) {
11766 "unexpected fp extload");
11767 TruncVT = MemVT.changeTypeToInteger();
11768 }
11769
11770 SDValue Cvt = NewLoad;
11771 if (Ld->getExtensionType() == ISD::SEXTLOAD) {
11772 Cvt = DAG.getNode(ISD::SIGN_EXTEND_INREG, SL, MVT::i32, NewLoad,
11773 DAG.getValueType(TruncVT));
11774 } else if (Ld->getExtensionType() == ISD::ZEXTLOAD ||
11776 Cvt = DAG.getZeroExtendInReg(NewLoad, SL, TruncVT);
11777 } else {
11779 }
11780
11781 EVT VT = Ld->getValueType(0);
11782 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
11783
11784 DCI.AddToWorklist(Cvt.getNode());
11785
11786 // We may need to handle exotic cases, such as i16->i64 extloads, so insert
11787 // the appropriate extension from the 32-bit load.
11788 Cvt = getLoadExtOrTrunc(DAG, Ld->getExtensionType(), Cvt, SL, IntVT);
11789 DCI.AddToWorklist(Cvt.getNode());
11790
11791 // Handle conversion back to floating point if necessary.
11792 Cvt = DAG.getNode(ISD::BITCAST, SL, VT, Cvt);
11793
11794 return DAG.getMergeValues({Cvt, NewLoad.getValue(1)}, SL);
11795}
11796
11798 const SIMachineFunctionInfo &Info) {
11799 // TODO: Should check if the address can definitely not access stack.
11800 if (Info.isEntryFunction())
11801 return Info.getUserSGPRInfo().hasFlatScratchInit();
11802 return true;
11803}
11804
11805SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
11806 SDLoc DL(Op);
11807 LoadSDNode *Load = cast<LoadSDNode>(Op);
11808 ISD::LoadExtType ExtType = Load->getExtensionType();
11809 EVT MemVT = Load->getMemoryVT();
11810 MachineMemOperand *MMO = Load->getMemOperand();
11811
11812 if (ExtType == ISD::NON_EXTLOAD && MemVT.getSizeInBits() < 32) {
11813 if (MemVT == MVT::i16 && isTypeLegal(MVT::i16))
11814 return SDValue();
11815
11816 // FIXME: Copied from PPC
11817 // First, load into 32 bits, then truncate to 1 bit.
11818
11819 SDValue Chain = Load->getChain();
11820 SDValue BasePtr = Load->getBasePtr();
11821
11822 EVT RealMemVT = (MemVT == MVT::i1) ? MVT::i8 : MVT::i16;
11823
11824 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain, BasePtr,
11825 RealMemVT, MMO);
11826
11827 if (!MemVT.isVector()) {
11828 SDValue Ops[] = {DAG.getNode(ISD::TRUNCATE, DL, MemVT, NewLD),
11829 NewLD.getValue(1)};
11830
11831 return DAG.getMergeValues(Ops, DL);
11832 }
11833
11835 for (unsigned I = 0, N = MemVT.getVectorNumElements(); I != N; ++I) {
11836 SDValue Elt = DAG.getNode(ISD::SRL, DL, MVT::i32, NewLD,
11837 DAG.getConstant(I, DL, MVT::i32));
11838
11839 Elts.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i1, Elt));
11840 }
11841
11842 SDValue Ops[] = {DAG.getBuildVector(MemVT, DL, Elts), NewLD.getValue(1)};
11843
11844 return DAG.getMergeValues(Ops, DL);
11845 }
11846
11847 if (!MemVT.isVector())
11848 return SDValue();
11849
11850 assert(Op.getValueType().getVectorElementType() == MVT::i32 &&
11851 "Custom lowering for non-i32 vectors hasn't been implemented.");
11852
11853 Align Alignment = Load->getAlign();
11854 unsigned AS = Load->getAddressSpace();
11855 if (Subtarget->hasLDSMisalignedBug() && AS == AMDGPUAS::FLAT_ADDRESS &&
11856 Alignment.value() < MemVT.getStoreSize() && MemVT.getSizeInBits() > 32) {
11857 return SplitVectorLoad(Op, DAG);
11858 }
11859
11860 MachineFunction &MF = DAG.getMachineFunction();
11861 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
11862 // If there is a possibility that flat instruction access scratch memory
11863 // then we need to use the same legalization rules we use for private.
11864 if (AS == AMDGPUAS::FLAT_ADDRESS &&
11865 !Subtarget->hasMultiDwordFlatScratchAddressing())
11866 AS = addressMayBeAccessedAsPrivate(Load->getMemOperand(), *MFI)
11869
11870 unsigned NumElements = MemVT.getVectorNumElements();
11871
11872 if (AS == AMDGPUAS::CONSTANT_ADDRESS ||
11874 (AS == AMDGPUAS::GLOBAL_ADDRESS &&
11875 Subtarget->getScalarizeGlobalBehavior() && Load->isSimple() &&
11877 if ((!Op->isDivergent() || AMDGPU::isUniformMMO(MMO)) &&
11878 Alignment >= Align(4) && NumElements < 32) {
11879 if (MemVT.isPow2VectorType() ||
11880 (Subtarget->hasScalarDwordx3Loads() && NumElements == 3))
11881 return SDValue();
11882 return WidenOrSplitVectorLoad(Op, DAG);
11883 }
11884 // Non-uniform loads will be selected to MUBUF instructions, so they
11885 // have the same legalization requirements as global and private
11886 // loads.
11887 //
11888 }
11889 if (AS == AMDGPUAS::CONSTANT_ADDRESS ||
11892 if (NumElements > 4)
11893 return SplitVectorLoad(Op, DAG);
11894 // v3 loads not supported on SI.
11895 if (NumElements == 3 && !Subtarget->hasDwordx3LoadStores())
11896 return WidenOrSplitVectorLoad(Op, DAG);
11897
11898 // v3 and v4 loads are supported for private and global memory.
11899 return SDValue();
11900 }
11901 if (AS == AMDGPUAS::PRIVATE_ADDRESS) {
11902 // Depending on the setting of the private_element_size field in the
11903 // resource descriptor, we can only make private accesses up to a certain
11904 // size.
11905 switch (Subtarget->getMaxPrivateElementSize()) {
11906 case 4: {
11907 auto [Op0, Op1] = scalarizeVectorLoad(Load, DAG);
11908 return DAG.getMergeValues({Op0, Op1}, DL);
11909 }
11910 case 8:
11911 if (NumElements > 2)
11912 return SplitVectorLoad(Op, DAG);
11913 return SDValue();
11914 case 16:
11915 // Same as global/flat
11916 if (NumElements > 4)
11917 return SplitVectorLoad(Op, DAG);
11918 // v3 loads not supported on SI.
11919 if (NumElements == 3 && !Subtarget->hasDwordx3LoadStores())
11920 return WidenOrSplitVectorLoad(Op, DAG);
11921
11922 return SDValue();
11923 default:
11924 llvm_unreachable("unsupported private_element_size");
11925 }
11926 } else if (AS == AMDGPUAS::LOCAL_ADDRESS || AS == AMDGPUAS::REGION_ADDRESS) {
11927 unsigned Fast = 0;
11928 auto Flags = Load->getMemOperand()->getFlags();
11930 Load->getAlign(), Flags, &Fast) &&
11931 Fast > 1)
11932 return SDValue();
11933
11934 if (MemVT.isVector())
11935 return SplitVectorLoad(Op, DAG);
11936 }
11937
11939 MemVT, *Load->getMemOperand())) {
11940 auto [Op0, Op1] = expandUnalignedLoad(Load, DAG);
11941 return DAG.getMergeValues({Op0, Op1}, DL);
11942 }
11943
11944 return SDValue();
11945}
11946
11947SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
11948 EVT VT = Op.getValueType();
11949 if (VT.getSizeInBits() == 128 || VT.getSizeInBits() == 256 ||
11950 VT.getSizeInBits() == 512)
11951 return splitTernaryVectorOp(Op, DAG);
11952
11953 assert(VT.getSizeInBits() == 64);
11954
11955 SDLoc DL(Op);
11956 SDValue Cond = DAG.getFreeze(Op.getOperand(0));
11957
11958 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
11959 SDValue One = DAG.getConstant(1, DL, MVT::i32);
11960
11961 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
11962 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
11963
11964 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
11965 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
11966
11967 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
11968
11969 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
11970 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
11971
11972 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
11973
11974 SDValue Res = DAG.getBuildVector(MVT::v2i32, DL, {Lo, Hi});
11975 return DAG.getNode(ISD::BITCAST, DL, VT, Res);
11976}
11977
11978// Catch division cases where we can use shortcuts with rcp and rsq
11979// instructions.
11980SDValue SITargetLowering::lowerFastUnsafeFDIV(SDValue Op,
11981 SelectionDAG &DAG) const {
11982 SDLoc SL(Op);
11983 SDValue LHS = Op.getOperand(0);
11984 SDValue RHS = Op.getOperand(1);
11985 EVT VT = Op.getValueType();
11986 const SDNodeFlags Flags = Op->getFlags();
11987
11988 bool AllowInaccurateRcp = Flags.hasApproximateFuncs();
11989
11990 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
11991 // Without !fpmath accuracy information, we can't do more because we don't
11992 // know exactly whether rcp is accurate enough to meet !fpmath requirement.
11993 // f16 is always accurate enough
11994 if (!AllowInaccurateRcp && VT != MVT::f16 && VT != MVT::bf16)
11995 return SDValue();
11996
11997 if (CLHS->isExactlyValue(1.0)) {
11998 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to
11999 // the CI documentation has a worst case error of 1 ulp.
12000 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to
12001 // use it as long as we aren't trying to use denormals.
12002 //
12003 // v_rcp_f16 and v_rsq_f16 DO support denormals and 0.51ulp.
12004
12005 // 1.0 / sqrt(x) -> rsq(x)
12006
12007 // XXX - Is afn sufficient to do this for f64? The maximum ULP
12008 // error seems really high at 2^29 ULP.
12009 // 1.0 / x -> rcp(x)
12010 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
12011 }
12012
12013 // Same as for 1.0, but expand the sign out of the constant.
12014 if (CLHS->isExactlyValue(-1.0)) {
12015 // -1.0 / x -> rcp (fneg x)
12016 SDValue FNegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
12017 return DAG.getNode(AMDGPUISD::RCP, SL, VT, FNegRHS);
12018 }
12019 }
12020
12021 // For f16 and bf16 require afn or arcp.
12022 // For f32 require afn.
12023 if (!AllowInaccurateRcp &&
12024 ((VT != MVT::f16 && VT != MVT::bf16) || !Flags.hasAllowReciprocal()))
12025 return SDValue();
12026
12027 // Turn into multiply by the reciprocal.
12028 // x / y -> x * (1.0 / y)
12029 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
12030 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip, Flags);
12031}
12032
12033SDValue SITargetLowering::lowerFastUnsafeFDIV64(SDValue Op,
12034 SelectionDAG &DAG) const {
12035 SDLoc SL(Op);
12036 SDValue X = Op.getOperand(0);
12037 SDValue Y = Op.getOperand(1);
12038 EVT VT = Op.getValueType();
12039 const SDNodeFlags Flags = Op->getFlags();
12040
12041 bool AllowInaccurateDiv = Flags.hasApproximateFuncs();
12042 if (!AllowInaccurateDiv)
12043 return SDValue();
12044
12045 SDValue NegY = DAG.getNode(ISD::FNEG, SL, VT, Y);
12046 SDValue One = DAG.getConstantFP(1.0, SL, VT);
12047
12048 SDValue R = DAG.getNode(AMDGPUISD::RCP, SL, VT, Y);
12049 SDValue Tmp0 = DAG.getNode(ISD::FMA, SL, VT, NegY, R, One);
12050
12051 R = DAG.getNode(ISD::FMA, SL, VT, Tmp0, R, R);
12052 SDValue Tmp1 = DAG.getNode(ISD::FMA, SL, VT, NegY, R, One);
12053 R = DAG.getNode(ISD::FMA, SL, VT, Tmp1, R, R);
12054 SDValue Ret = DAG.getNode(ISD::FMUL, SL, VT, X, R);
12055 SDValue Tmp2 = DAG.getNode(ISD::FMA, SL, VT, NegY, Ret, X);
12056 return DAG.getNode(ISD::FMA, SL, VT, Tmp2, R, Ret);
12057}
12058
12059static SDValue getFPBinOp(SelectionDAG &DAG, unsigned Opcode, const SDLoc &SL,
12060 EVT VT, SDValue A, SDValue B, SDValue GlueChain,
12061 SDNodeFlags Flags) {
12062 if (GlueChain->getNumValues() <= 1) {
12063 return DAG.getNode(Opcode, SL, VT, A, B, Flags);
12064 }
12065
12066 assert(GlueChain->getNumValues() == 3);
12067
12068 SDVTList VTList = DAG.getVTList(VT, MVT::Other, MVT::Glue);
12069 switch (Opcode) {
12070 default:
12071 llvm_unreachable("no chain equivalent for opcode");
12072 case ISD::FMUL:
12073 Opcode = AMDGPUISD::FMUL_W_CHAIN;
12074 break;
12075 }
12076
12077 return DAG.getNode(Opcode, SL, VTList,
12078 {GlueChain.getValue(1), A, B, GlueChain.getValue(2)},
12079 Flags);
12080}
12081
12082static SDValue getFPTernOp(SelectionDAG &DAG, unsigned Opcode, const SDLoc &SL,
12083 EVT VT, SDValue A, SDValue B, SDValue C,
12084 SDValue GlueChain, SDNodeFlags Flags) {
12085 if (GlueChain->getNumValues() <= 1) {
12086 return DAG.getNode(Opcode, SL, VT, {A, B, C}, Flags);
12087 }
12088
12089 assert(GlueChain->getNumValues() == 3);
12090
12091 SDVTList VTList = DAG.getVTList(VT, MVT::Other, MVT::Glue);
12092 switch (Opcode) {
12093 default:
12094 llvm_unreachable("no chain equivalent for opcode");
12095 case ISD::FMA:
12096 Opcode = AMDGPUISD::FMA_W_CHAIN;
12097 break;
12098 }
12099
12100 return DAG.getNode(Opcode, SL, VTList,
12101 {GlueChain.getValue(1), A, B, C, GlueChain.getValue(2)},
12102 Flags);
12103}
12104
12105SDValue SITargetLowering::LowerFDIV16(SDValue Op, SelectionDAG &DAG) const {
12106 if (SDValue FastLowered = lowerFastUnsafeFDIV(Op, DAG))
12107 return FastLowered;
12108
12109 SDLoc SL(Op);
12110 EVT VT = Op.getValueType();
12111 SDValue LHS = Op.getOperand(0);
12112 SDValue RHS = Op.getOperand(1);
12113
12114 SDValue LHSExt = DAG.getNode(ISD::FP_EXTEND, SL, MVT::f32, LHS);
12115 SDValue RHSExt = DAG.getNode(ISD::FP_EXTEND, SL, MVT::f32, RHS);
12116
12117 if (VT == MVT::bf16) {
12118 SDValue ExtDiv =
12119 DAG.getNode(ISD::FDIV, SL, MVT::f32, LHSExt, RHSExt, Op->getFlags());
12120 return DAG.getNode(ISD::FP_ROUND, SL, MVT::bf16, ExtDiv,
12121 DAG.getTargetConstant(0, SL, MVT::i32));
12122 }
12123
12124 assert(VT == MVT::f16);
12125
12126 // a32.u = opx(V_CVT_F32_F16, a.u); // CVT to F32
12127 // b32.u = opx(V_CVT_F32_F16, b.u); // CVT to F32
12128 // r32.u = opx(V_RCP_F32, b32.u); // rcp = 1 / d
12129 // q32.u = opx(V_MUL_F32, a32.u, r32.u); // q = n * rcp
12130 // e32.u = opx(V_MAD_F32, (b32.u^_neg32), q32.u, a32.u); // err = -d * q + n
12131 // q32.u = opx(V_MAD_F32, e32.u, r32.u, q32.u); // q = n * rcp
12132 // e32.u = opx(V_MAD_F32, (b32.u^_neg32), q32.u, a32.u); // err = -d * q + n
12133 // tmp.u = opx(V_MUL_F32, e32.u, r32.u);
12134 // tmp.u = opx(V_AND_B32, tmp.u, 0xff800000)
12135 // q32.u = opx(V_ADD_F32, tmp.u, q32.u);
12136 // q16.u = opx(V_CVT_F16_F32, q32.u);
12137 // q16.u = opx(V_DIV_FIXUP_F16, q16.u, b.u, a.u); // q = touchup(q, d, n)
12138
12139 // We will use ISD::FMA on targets that don't support ISD::FMAD.
12140 unsigned FMADOpCode =
12142 SDValue NegRHSExt = DAG.getNode(ISD::FNEG, SL, MVT::f32, RHSExt);
12143 SDValue Rcp =
12144 DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, RHSExt, Op->getFlags());
12145 SDValue Quot =
12146 DAG.getNode(ISD::FMUL, SL, MVT::f32, LHSExt, Rcp, Op->getFlags());
12147 SDValue Err = DAG.getNode(FMADOpCode, SL, MVT::f32, NegRHSExt, Quot, LHSExt,
12148 Op->getFlags());
12149 Quot = DAG.getNode(FMADOpCode, SL, MVT::f32, Err, Rcp, Quot, Op->getFlags());
12150 Err = DAG.getNode(FMADOpCode, SL, MVT::f32, NegRHSExt, Quot, LHSExt,
12151 Op->getFlags());
12152 SDValue Tmp = DAG.getNode(ISD::FMUL, SL, MVT::f32, Err, Rcp, Op->getFlags());
12153 SDValue TmpCast = DAG.getNode(ISD::BITCAST, SL, MVT::i32, Tmp);
12154 TmpCast = DAG.getNode(ISD::AND, SL, MVT::i32, TmpCast,
12155 DAG.getConstant(0xff800000, SL, MVT::i32));
12156 Tmp = DAG.getNode(ISD::BITCAST, SL, MVT::f32, TmpCast);
12157 Quot = DAG.getNode(ISD::FADD, SL, MVT::f32, Tmp, Quot, Op->getFlags());
12158 SDValue RDst = DAG.getNode(ISD::FP_ROUND, SL, MVT::f16, Quot,
12159 DAG.getTargetConstant(0, SL, MVT::i32));
12160 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f16, RDst, RHS, LHS,
12161 Op->getFlags());
12162}
12163
12164// Faster 2.5 ULP division that does not support denormals.
12165SDValue SITargetLowering::lowerFDIV_FAST(SDValue Op, SelectionDAG &DAG) const {
12166 SDNodeFlags Flags = Op->getFlags();
12167 SDLoc SL(Op);
12168 SDValue LHS = Op.getOperand(1);
12169 SDValue RHS = Op.getOperand(2);
12170
12171 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS, Flags);
12172
12173 const APFloat K0Val(0x1p+96f);
12174 const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32);
12175
12176 const APFloat K1Val(0x1p-32f);
12177 const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32);
12178
12179 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
12180
12181 EVT SetCCVT =
12182 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32);
12183
12184 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
12185
12186 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One, Flags);
12187
12188 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3, Flags);
12189
12190 // rcp does not support denormals.
12191 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1, Flags);
12192
12193 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0, Flags);
12194
12195 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul, Flags);
12196}
12197
12198// Returns immediate value for setting the F32 denorm mode when using the
12199// S_DENORM_MODE instruction.
12202 const GCNSubtarget *ST) {
12203 assert(ST->hasDenormModeInst() && "Requires S_DENORM_MODE");
12204 uint32_t DPDenormModeDefault = Info->getMode().fpDenormModeDPValue();
12205 uint32_t Mode = SPDenormMode | (DPDenormModeDefault << 2);
12206 return DAG.getTargetConstant(Mode, SDLoc(), MVT::i32);
12207}
12208
12209SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
12210 if (SDValue FastLowered = lowerFastUnsafeFDIV(Op, DAG))
12211 return FastLowered;
12212
12213 // The selection matcher assumes anything with a chain selecting to a
12214 // mayRaiseFPException machine instruction. Since we're introducing a chain
12215 // here, we need to explicitly report nofpexcept for the regular fdiv
12216 // lowering.
12217 SDNodeFlags Flags = Op->getFlags();
12218 Flags.setNoFPExcept(true);
12219
12220 SDLoc SL(Op);
12221 SDValue LHS = Op.getOperand(0);
12222 SDValue RHS = Op.getOperand(1);
12223
12224 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
12225
12226 SDVTList ScaleVT = DAG.getVTList(MVT::f32, MVT::i1);
12227
12228 SDValue DenominatorScaled =
12229 DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, {RHS, RHS, LHS}, Flags);
12230 SDValue NumeratorScaled =
12231 DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, {LHS, RHS, LHS}, Flags);
12232
12233 // Denominator is scaled to not be denormal, so using rcp is ok.
12234 SDValue ApproxRcp =
12235 DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, DenominatorScaled, Flags);
12236 SDValue NegDivScale0 =
12237 DAG.getNode(ISD::FNEG, SL, MVT::f32, DenominatorScaled, Flags);
12238
12239 using namespace AMDGPU::Hwreg;
12240 const unsigned Denorm32Reg = HwregEncoding::encode(ID_MODE, 4, 2);
12241 const SDValue BitField = DAG.getTargetConstant(Denorm32Reg, SL, MVT::i32);
12242
12243 const MachineFunction &MF = DAG.getMachineFunction();
12244 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
12245 const DenormalMode DenormMode = Info->getMode().FP32Denormals;
12246
12247 const bool PreservesDenormals = DenormMode == DenormalMode::getIEEE();
12248 const bool HasDynamicDenormals =
12249 (DenormMode.Input == DenormalMode::Dynamic) ||
12250 (DenormMode.Output == DenormalMode::Dynamic);
12251
12252 SDValue SavedDenormMode;
12253
12254 if (!PreservesDenormals) {
12255 // Note we can't use the STRICT_FMA/STRICT_FMUL for the non-strict FDIV
12256 // lowering. The chain dependence is insufficient, and we need glue. We do
12257 // not need the glue variants in a strictfp function.
12258
12259 SDVTList BindParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
12260
12261 SDValue Glue = DAG.getEntryNode();
12262 if (HasDynamicDenormals) {
12263 SDNode *GetReg = DAG.getMachineNode(AMDGPU::S_GETREG_B32, SL,
12264 DAG.getVTList(MVT::i32, MVT::Glue),
12265 {BitField, Glue});
12266 SavedDenormMode = SDValue(GetReg, 0);
12267
12268 Glue = DAG.getMergeValues(
12269 {DAG.getEntryNode(), SDValue(GetReg, 0), SDValue(GetReg, 1)}, SL);
12270 }
12271
12272 SDNode *EnableDenorm;
12273 if (Subtarget->hasDenormModeInst()) {
12274 const SDValue EnableDenormValue =
12276
12277 EnableDenorm = DAG.getNode(AMDGPUISD::DENORM_MODE, SL, BindParamVTs, Glue,
12278 EnableDenormValue)
12279 .getNode();
12280 } else {
12281 const SDValue EnableDenormValue =
12282 DAG.getConstant(FP_DENORM_FLUSH_NONE, SL, MVT::i32);
12283 EnableDenorm = DAG.getMachineNode(AMDGPU::S_SETREG_B32, SL, BindParamVTs,
12284 {EnableDenormValue, BitField, Glue});
12285 }
12286
12287 SDValue Ops[3] = {NegDivScale0, SDValue(EnableDenorm, 0),
12288 SDValue(EnableDenorm, 1)};
12289
12290 NegDivScale0 = DAG.getMergeValues(Ops, SL);
12291 }
12292
12293 SDValue Fma0 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, NegDivScale0,
12294 ApproxRcp, One, NegDivScale0, Flags);
12295
12296 SDValue Fma1 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, Fma0, ApproxRcp,
12297 ApproxRcp, Fma0, Flags);
12298
12299 SDValue Mul = getFPBinOp(DAG, ISD::FMUL, SL, MVT::f32, NumeratorScaled, Fma1,
12300 Fma1, Flags);
12301
12302 SDValue Fma2 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, NegDivScale0, Mul,
12303 NumeratorScaled, Mul, Flags);
12304
12305 SDValue Fma3 =
12306 getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, Fma2, Fma1, Mul, Fma2, Flags);
12307
12308 SDValue Fma4 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, NegDivScale0, Fma3,
12309 NumeratorScaled, Fma3, Flags);
12310
12311 if (!PreservesDenormals) {
12312 SDNode *DisableDenorm;
12313 if (!HasDynamicDenormals && Subtarget->hasDenormModeInst()) {
12314 const SDValue DisableDenormValue = getSPDenormModeValue(
12315 FP_DENORM_FLUSH_IN_FLUSH_OUT, DAG, Info, Subtarget);
12316
12317 SDVTList BindParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
12318 DisableDenorm =
12319 DAG.getNode(AMDGPUISD::DENORM_MODE, SL, BindParamVTs,
12320 Fma4.getValue(1), DisableDenormValue, Fma4.getValue(2))
12321 .getNode();
12322 } else {
12323 assert(HasDynamicDenormals == (bool)SavedDenormMode);
12324 const SDValue DisableDenormValue =
12325 HasDynamicDenormals
12326 ? SavedDenormMode
12327 : DAG.getConstant(FP_DENORM_FLUSH_IN_FLUSH_OUT, SL, MVT::i32);
12328
12329 DisableDenorm = DAG.getMachineNode(
12330 AMDGPU::S_SETREG_B32, SL, MVT::Other,
12331 {DisableDenormValue, BitField, Fma4.getValue(1), Fma4.getValue(2)});
12332 }
12333
12334 SDValue OutputChain = DAG.getNode(ISD::TokenFactor, SL, MVT::Other,
12335 SDValue(DisableDenorm, 0), DAG.getRoot());
12336 DAG.setRoot(OutputChain);
12337 }
12338
12339 SDValue Scale = NumeratorScaled.getValue(1);
12340 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f32,
12341 {Fma4, Fma1, Fma3, Scale}, Flags);
12342
12343 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f32, Fmas, RHS, LHS, Flags);
12344}
12345
12346SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
12347 if (SDValue FastLowered = lowerFastUnsafeFDIV64(Op, DAG))
12348 return FastLowered;
12349
12350 SDLoc SL(Op);
12351 SDValue X = Op.getOperand(0);
12352 SDValue Y = Op.getOperand(1);
12353
12354 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
12355
12356 SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1);
12357
12358 SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X);
12359
12360 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0);
12361
12362 SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0);
12363
12364 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One);
12365
12366 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp);
12367
12368 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One);
12369
12370 SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X);
12371
12372 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1);
12373 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3);
12374
12375 SDValue Fma4 =
12376 DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Mul, DivScale1);
12377
12378 SDValue Scale;
12379
12380 if (!Subtarget->hasUsableDivScaleConditionOutput()) {
12381 // Workaround a hardware bug on SI where the condition output from div_scale
12382 // is not usable.
12383
12384 const SDValue Hi = DAG.getConstant(1, SL, MVT::i32);
12385
12386 // Figure out if the scale to use for div_fmas.
12387 SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
12388 SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y);
12389 SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0);
12390 SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1);
12391
12392 SDValue NumHi =
12393 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi);
12394 SDValue DenHi =
12395 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi);
12396
12397 SDValue Scale0Hi =
12398 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi);
12399 SDValue Scale1Hi =
12400 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi);
12401
12402 SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ);
12403 SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ);
12404 Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen);
12405 } else {
12406 Scale = DivScale1.getValue(1);
12407 }
12408
12409 SDValue Fmas =
12410 DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64, Fma4, Fma3, Mul, Scale);
12411
12412 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X);
12413}
12414
12415SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
12416 EVT VT = Op.getValueType();
12417
12418 if (VT == MVT::f32)
12419 return LowerFDIV32(Op, DAG);
12420
12421 if (VT == MVT::f64)
12422 return LowerFDIV64(Op, DAG);
12423
12424 if (VT == MVT::f16 || VT == MVT::bf16)
12425 return LowerFDIV16(Op, DAG);
12426
12427 llvm_unreachable("Unexpected type for fdiv");
12428}
12429
12430SDValue SITargetLowering::LowerFFREXP(SDValue Op, SelectionDAG &DAG) const {
12431 SDLoc dl(Op);
12432 SDValue Val = Op.getOperand(0);
12433 EVT VT = Val.getValueType();
12434 EVT ResultExpVT = Op->getValueType(1);
12435 EVT InstrExpVT = VT == MVT::f16 ? MVT::i16 : MVT::i32;
12436
12437 SDValue Mant = DAG.getNode(
12439 DAG.getTargetConstant(Intrinsic::amdgcn_frexp_mant, dl, MVT::i32), Val);
12440
12441 SDValue Exp = DAG.getNode(
12442 ISD::INTRINSIC_WO_CHAIN, dl, InstrExpVT,
12443 DAG.getTargetConstant(Intrinsic::amdgcn_frexp_exp, dl, MVT::i32), Val);
12444
12445 if (Subtarget->hasFractBug()) {
12446 SDValue Fabs = DAG.getNode(ISD::FABS, dl, VT, Val);
12447 SDValue Inf =
12449
12450 SDValue IsFinite = DAG.getSetCC(dl, MVT::i1, Fabs, Inf, ISD::SETOLT);
12451 SDValue Zero = DAG.getConstant(0, dl, InstrExpVT);
12452 Exp = DAG.getNode(ISD::SELECT, dl, InstrExpVT, IsFinite, Exp, Zero);
12453 Mant = DAG.getNode(ISD::SELECT, dl, VT, IsFinite, Mant, Val);
12454 }
12455
12456 SDValue CastExp = DAG.getSExtOrTrunc(Exp, dl, ResultExpVT);
12457 return DAG.getMergeValues({Mant, CastExp}, dl);
12458}
12459
12460SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
12461 SDLoc DL(Op);
12462 StoreSDNode *Store = cast<StoreSDNode>(Op);
12463 EVT VT = Store->getMemoryVT();
12464
12465 if (VT == MVT::i1) {
12466 return DAG.getTruncStore(
12467 Store->getChain(), DL,
12468 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
12469 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
12470 }
12471
12472 assert(VT.isVector() &&
12473 Store->getValue().getValueType().getScalarType() == MVT::i32);
12474
12475 unsigned AS = Store->getAddressSpace();
12476 if (Subtarget->hasLDSMisalignedBug() && AS == AMDGPUAS::FLAT_ADDRESS &&
12477 Store->getAlign().value() < VT.getStoreSize() &&
12478 VT.getSizeInBits() > 32) {
12479 return SplitVectorStore(Op, DAG);
12480 }
12481
12482 MachineFunction &MF = DAG.getMachineFunction();
12483 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
12484 // If there is a possibility that flat instruction access scratch memory
12485 // then we need to use the same legalization rules we use for private.
12486 if (AS == AMDGPUAS::FLAT_ADDRESS &&
12487 !Subtarget->hasMultiDwordFlatScratchAddressing())
12488 AS = addressMayBeAccessedAsPrivate(Store->getMemOperand(), *MFI)
12491
12492 unsigned NumElements = VT.getVectorNumElements();
12494 if (NumElements > 4)
12495 return SplitVectorStore(Op, DAG);
12496 // v3 stores not supported on SI.
12497 if (NumElements == 3 && !Subtarget->hasDwordx3LoadStores())
12498 return SplitVectorStore(Op, DAG);
12499
12501 VT, *Store->getMemOperand()))
12502 return expandUnalignedStore(Store, DAG);
12503
12504 return SDValue();
12505 }
12506 if (AS == AMDGPUAS::PRIVATE_ADDRESS) {
12507 switch (Subtarget->getMaxPrivateElementSize()) {
12508 case 4:
12509 return scalarizeVectorStore(Store, DAG);
12510 case 8:
12511 if (NumElements > 2)
12512 return SplitVectorStore(Op, DAG);
12513 return SDValue();
12514 case 16:
12515 if (NumElements > 4 ||
12516 (NumElements == 3 && !Subtarget->enableFlatScratch()))
12517 return SplitVectorStore(Op, DAG);
12518 return SDValue();
12519 default:
12520 llvm_unreachable("unsupported private_element_size");
12521 }
12522 } else if (AS == AMDGPUAS::LOCAL_ADDRESS || AS == AMDGPUAS::REGION_ADDRESS) {
12523 unsigned Fast = 0;
12524 auto Flags = Store->getMemOperand()->getFlags();
12526 Store->getAlign(), Flags, &Fast) &&
12527 Fast > 1)
12528 return SDValue();
12529
12530 if (VT.isVector())
12531 return SplitVectorStore(Op, DAG);
12532
12533 return expandUnalignedStore(Store, DAG);
12534 }
12535
12536 // Probably an invalid store. If so we'll end up emitting a selection error.
12537 return SDValue();
12538}
12539
12540// Avoid the full correct expansion for f32 sqrt when promoting from f16.
12541SDValue SITargetLowering::lowerFSQRTF16(SDValue Op, SelectionDAG &DAG) const {
12542 SDLoc SL(Op);
12543 assert(!Subtarget->has16BitInsts());
12544 SDNodeFlags Flags = Op->getFlags();
12545 SDValue Ext =
12546 DAG.getNode(ISD::FP_EXTEND, SL, MVT::f32, Op.getOperand(0), Flags);
12547
12548 SDValue SqrtID = DAG.getTargetConstant(Intrinsic::amdgcn_sqrt, SL, MVT::i32);
12549 SDValue Sqrt =
12550 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SL, MVT::f32, SqrtID, Ext, Flags);
12551
12552 return DAG.getNode(ISD::FP_ROUND, SL, MVT::f16, Sqrt,
12553 DAG.getTargetConstant(0, SL, MVT::i32), Flags);
12554}
12555
12556SDValue SITargetLowering::lowerFSQRTF32(SDValue Op, SelectionDAG &DAG) const {
12557 SDLoc DL(Op);
12558 SDNodeFlags Flags = Op->getFlags();
12559 MVT VT = Op.getValueType().getSimpleVT();
12560 const SDValue X = Op.getOperand(0);
12561
12562 if (allowApproxFunc(DAG, Flags)) {
12563 // Instruction is 1ulp but ignores denormals.
12564 return DAG.getNode(
12566 DAG.getTargetConstant(Intrinsic::amdgcn_sqrt, DL, MVT::i32), X, Flags);
12567 }
12568
12569 SDValue ScaleThreshold = DAG.getConstantFP(0x1.0p-96f, DL, VT);
12570 SDValue NeedScale = DAG.getSetCC(DL, MVT::i1, X, ScaleThreshold, ISD::SETOLT);
12571
12572 SDValue ScaleUpFactor = DAG.getConstantFP(0x1.0p+32f, DL, VT);
12573
12574 SDValue ScaledX = DAG.getNode(ISD::FMUL, DL, VT, X, ScaleUpFactor, Flags);
12575
12576 SDValue SqrtX =
12577 DAG.getNode(ISD::SELECT, DL, VT, NeedScale, ScaledX, X, Flags);
12578
12579 SDValue SqrtS;
12580 if (needsDenormHandlingF32(DAG, X, Flags)) {
12581 SDValue SqrtID =
12582 DAG.getTargetConstant(Intrinsic::amdgcn_sqrt, DL, MVT::i32);
12583 SqrtS = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT, SqrtID, SqrtX, Flags);
12584
12585 SDValue SqrtSAsInt = DAG.getNode(ISD::BITCAST, DL, MVT::i32, SqrtS);
12586 SDValue SqrtSNextDownInt =
12587 DAG.getNode(ISD::ADD, DL, MVT::i32, SqrtSAsInt,
12588 DAG.getAllOnesConstant(DL, MVT::i32));
12589 SDValue SqrtSNextDown = DAG.getNode(ISD::BITCAST, DL, VT, SqrtSNextDownInt);
12590
12591 SDValue NegSqrtSNextDown =
12592 DAG.getNode(ISD::FNEG, DL, VT, SqrtSNextDown, Flags);
12593
12594 SDValue SqrtVP =
12595 DAG.getNode(ISD::FMA, DL, VT, NegSqrtSNextDown, SqrtS, SqrtX, Flags);
12596
12597 SDValue SqrtSNextUpInt = DAG.getNode(ISD::ADD, DL, MVT::i32, SqrtSAsInt,
12598 DAG.getConstant(1, DL, MVT::i32));
12599 SDValue SqrtSNextUp = DAG.getNode(ISD::BITCAST, DL, VT, SqrtSNextUpInt);
12600
12601 SDValue NegSqrtSNextUp = DAG.getNode(ISD::FNEG, DL, VT, SqrtSNextUp, Flags);
12602 SDValue SqrtVS =
12603 DAG.getNode(ISD::FMA, DL, VT, NegSqrtSNextUp, SqrtS, SqrtX, Flags);
12604
12605 SDValue Zero = DAG.getConstantFP(0.0f, DL, VT);
12606 SDValue SqrtVPLE0 = DAG.getSetCC(DL, MVT::i1, SqrtVP, Zero, ISD::SETOLE);
12607
12608 SqrtS = DAG.getNode(ISD::SELECT, DL, VT, SqrtVPLE0, SqrtSNextDown, SqrtS,
12609 Flags);
12610
12611 SDValue SqrtVPVSGT0 = DAG.getSetCC(DL, MVT::i1, SqrtVS, Zero, ISD::SETOGT);
12612 SqrtS = DAG.getNode(ISD::SELECT, DL, VT, SqrtVPVSGT0, SqrtSNextUp, SqrtS,
12613 Flags);
12614 } else {
12615 SDValue SqrtR = DAG.getNode(AMDGPUISD::RSQ, DL, VT, SqrtX, Flags);
12616
12617 SqrtS = DAG.getNode(ISD::FMUL, DL, VT, SqrtX, SqrtR, Flags);
12618
12619 SDValue Half = DAG.getConstantFP(0.5f, DL, VT);
12620 SDValue SqrtH = DAG.getNode(ISD::FMUL, DL, VT, SqrtR, Half, Flags);
12621 SDValue NegSqrtH = DAG.getNode(ISD::FNEG, DL, VT, SqrtH, Flags);
12622
12623 SDValue SqrtE = DAG.getNode(ISD::FMA, DL, VT, NegSqrtH, SqrtS, Half, Flags);
12624 SqrtH = DAG.getNode(ISD::FMA, DL, VT, SqrtH, SqrtE, SqrtH, Flags);
12625 SqrtS = DAG.getNode(ISD::FMA, DL, VT, SqrtS, SqrtE, SqrtS, Flags);
12626
12627 SDValue NegSqrtS = DAG.getNode(ISD::FNEG, DL, VT, SqrtS, Flags);
12628 SDValue SqrtD =
12629 DAG.getNode(ISD::FMA, DL, VT, NegSqrtS, SqrtS, SqrtX, Flags);
12630 SqrtS = DAG.getNode(ISD::FMA, DL, VT, SqrtD, SqrtH, SqrtS, Flags);
12631 }
12632
12633 SDValue ScaleDownFactor = DAG.getConstantFP(0x1.0p-16f, DL, VT);
12634
12635 SDValue ScaledDown =
12636 DAG.getNode(ISD::FMUL, DL, VT, SqrtS, ScaleDownFactor, Flags);
12637
12638 SqrtS = DAG.getNode(ISD::SELECT, DL, VT, NeedScale, ScaledDown, SqrtS, Flags);
12639 SDValue IsZeroOrInf =
12640 DAG.getNode(ISD::IS_FPCLASS, DL, MVT::i1, SqrtX,
12641 DAG.getTargetConstant(fcZero | fcPosInf, DL, MVT::i32));
12642
12643 return DAG.getNode(ISD::SELECT, DL, VT, IsZeroOrInf, SqrtX, SqrtS, Flags);
12644}
12645
12646SDValue SITargetLowering::lowerFSQRTF64(SDValue Op, SelectionDAG &DAG) const {
12647 // For double type, the SQRT and RSQ instructions don't have required
12648 // precision, we apply Goldschmidt's algorithm to improve the result:
12649 //
12650 // y0 = rsq(x)
12651 // g0 = x * y0
12652 // h0 = 0.5 * y0
12653 //
12654 // r0 = 0.5 - h0 * g0
12655 // g1 = g0 * r0 + g0
12656 // h1 = h0 * r0 + h0
12657 //
12658 // r1 = 0.5 - h1 * g1 => d0 = x - g1 * g1
12659 // g2 = g1 * r1 + g1 g2 = d0 * h1 + g1
12660 // h2 = h1 * r1 + h1
12661 //
12662 // r2 = 0.5 - h2 * g2 => d1 = x - g2 * g2
12663 // g3 = g2 * r2 + g2 g3 = d1 * h1 + g2
12664 //
12665 // sqrt(x) = g3
12666
12667 SDNodeFlags Flags = Op->getFlags();
12668
12669 SDLoc DL(Op);
12670
12671 SDValue X = Op.getOperand(0);
12672 SDValue ScaleConstant = DAG.getConstantFP(0x1.0p-767, DL, MVT::f64);
12673
12674 SDValue Scaling = DAG.getSetCC(DL, MVT::i1, X, ScaleConstant, ISD::SETOLT);
12675
12676 SDValue ZeroInt = DAG.getConstant(0, DL, MVT::i32);
12677
12678 // Scale up input if it is too small.
12679 SDValue ScaleUpFactor = DAG.getConstant(256, DL, MVT::i32);
12680 SDValue ScaleUp =
12681 DAG.getNode(ISD::SELECT, DL, MVT::i32, Scaling, ScaleUpFactor, ZeroInt);
12682 SDValue SqrtX = DAG.getNode(ISD::FLDEXP, DL, MVT::f64, X, ScaleUp, Flags);
12683
12684 SDValue SqrtY = DAG.getNode(AMDGPUISD::RSQ, DL, MVT::f64, SqrtX);
12685
12686 SDValue SqrtS0 = DAG.getNode(ISD::FMUL, DL, MVT::f64, SqrtX, SqrtY);
12687
12688 SDValue Half = DAG.getConstantFP(0.5, DL, MVT::f64);
12689 SDValue SqrtH0 = DAG.getNode(ISD::FMUL, DL, MVT::f64, SqrtY, Half);
12690
12691 SDValue NegSqrtH0 = DAG.getNode(ISD::FNEG, DL, MVT::f64, SqrtH0);
12692 SDValue SqrtR0 = DAG.getNode(ISD::FMA, DL, MVT::f64, NegSqrtH0, SqrtS0, Half);
12693
12694 SDValue SqrtH1 = DAG.getNode(ISD::FMA, DL, MVT::f64, SqrtH0, SqrtR0, SqrtH0);
12695
12696 SDValue SqrtS1 = DAG.getNode(ISD::FMA, DL, MVT::f64, SqrtS0, SqrtR0, SqrtS0);
12697
12698 SDValue NegSqrtS1 = DAG.getNode(ISD::FNEG, DL, MVT::f64, SqrtS1);
12699 SDValue SqrtD0 =
12700 DAG.getNode(ISD::FMA, DL, MVT::f64, NegSqrtS1, SqrtS1, SqrtX);
12701
12702 SDValue SqrtS2 = DAG.getNode(ISD::FMA, DL, MVT::f64, SqrtD0, SqrtH1, SqrtS1);
12703
12704 SDValue NegSqrtS2 = DAG.getNode(ISD::FNEG, DL, MVT::f64, SqrtS2);
12705 SDValue SqrtD1 =
12706 DAG.getNode(ISD::FMA, DL, MVT::f64, NegSqrtS2, SqrtS2, SqrtX);
12707
12708 SDValue SqrtRet = DAG.getNode(ISD::FMA, DL, MVT::f64, SqrtD1, SqrtH1, SqrtS2);
12709
12710 SDValue ScaleDownFactor = DAG.getSignedConstant(-128, DL, MVT::i32);
12711 SDValue ScaleDown =
12712 DAG.getNode(ISD::SELECT, DL, MVT::i32, Scaling, ScaleDownFactor, ZeroInt);
12713 SqrtRet = DAG.getNode(ISD::FLDEXP, DL, MVT::f64, SqrtRet, ScaleDown, Flags);
12714
12715 // TODO: Switch to fcmp oeq 0 for finite only. Can't fully remove this check
12716 // with finite only or nsz because rsq(+/-0) = +/-inf
12717
12718 // TODO: Check for DAZ and expand to subnormals
12719 SDValue IsZeroOrInf =
12720 DAG.getNode(ISD::IS_FPCLASS, DL, MVT::i1, SqrtX,
12721 DAG.getTargetConstant(fcZero | fcPosInf, DL, MVT::i32));
12722
12723 // If x is +INF, +0, or -0, use its original value
12724 return DAG.getNode(ISD::SELECT, DL, MVT::f64, IsZeroOrInf, SqrtX, SqrtRet,
12725 Flags);
12726}
12727
12728SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
12729 SDLoc DL(Op);
12730 EVT VT = Op.getValueType();
12731 SDValue Arg = Op.getOperand(0);
12732 SDValue TrigVal;
12733
12734 // Propagate fast-math flags so that the multiply we introduce can be folded
12735 // if Arg is already the result of a multiply by constant.
12736 auto Flags = Op->getFlags();
12737
12738 SDValue OneOver2Pi = DAG.getConstantFP(0.5 * numbers::inv_pi, DL, VT);
12739
12740 if (Subtarget->hasTrigReducedRange()) {
12741 SDValue MulVal = DAG.getNode(ISD::FMUL, DL, VT, Arg, OneOver2Pi, Flags);
12742 TrigVal = DAG.getNode(AMDGPUISD::FRACT, DL, VT, MulVal, Flags);
12743 } else {
12744 TrigVal = DAG.getNode(ISD::FMUL, DL, VT, Arg, OneOver2Pi, Flags);
12745 }
12746
12747 switch (Op.getOpcode()) {
12748 case ISD::FCOS:
12749 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, TrigVal, Flags);
12750 case ISD::FSIN:
12751 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, TrigVal, Flags);
12752 default:
12753 llvm_unreachable("Wrong trig opcode");
12754 }
12755}
12756
12757SDValue SITargetLowering::LowerATOMIC_CMP_SWAP(SDValue Op,
12758 SelectionDAG &DAG) const {
12759 AtomicSDNode *AtomicNode = cast<AtomicSDNode>(Op);
12760 assert(AtomicNode->isCompareAndSwap());
12761 unsigned AS = AtomicNode->getAddressSpace();
12762
12763 // No custom lowering required for local address space
12765 return Op;
12766
12767 // Non-local address space requires custom lowering for atomic compare
12768 // and swap; cmp and swap should be in a v2i32 or v2i64 in case of _X2
12769 SDLoc DL(Op);
12770 SDValue ChainIn = Op.getOperand(0);
12771 SDValue Addr = Op.getOperand(1);
12772 SDValue Old = Op.getOperand(2);
12773 SDValue New = Op.getOperand(3);
12774 EVT VT = Op.getValueType();
12775 MVT SimpleVT = VT.getSimpleVT();
12776 MVT VecType = MVT::getVectorVT(SimpleVT, 2);
12777
12778 SDValue NewOld = DAG.getBuildVector(VecType, DL, {New, Old});
12779 SDValue Ops[] = {ChainIn, Addr, NewOld};
12780
12782 Op->getVTList(), Ops, VT,
12783 AtomicNode->getMemOperand());
12784}
12785
12786//===----------------------------------------------------------------------===//
12787// Custom DAG optimizations
12788//===----------------------------------------------------------------------===//
12789
12790SDValue
12791SITargetLowering::performUCharToFloatCombine(SDNode *N,
12792 DAGCombinerInfo &DCI) const {
12793 EVT VT = N->getValueType(0);
12794 EVT ScalarVT = VT.getScalarType();
12795 if (ScalarVT != MVT::f32 && ScalarVT != MVT::f16)
12796 return SDValue();
12797
12798 SelectionDAG &DAG = DCI.DAG;
12799 SDLoc DL(N);
12800
12801 SDValue Src = N->getOperand(0);
12802 EVT SrcVT = Src.getValueType();
12803
12804 // TODO: We could try to match extracting the higher bytes, which would be
12805 // easier if i8 vectors weren't promoted to i32 vectors, particularly after
12806 // types are legalized. v4i8 -> v4f32 is probably the only case to worry
12807 // about in practice.
12808 if (DCI.isAfterLegalizeDAG() && SrcVT == MVT::i32) {
12809 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
12810 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, MVT::f32, Src);
12811 DCI.AddToWorklist(Cvt.getNode());
12812
12813 // For the f16 case, fold to a cast to f32 and then cast back to f16.
12814 if (ScalarVT != MVT::f32) {
12815 Cvt = DAG.getNode(ISD::FP_ROUND, DL, VT, Cvt,
12816 DAG.getTargetConstant(0, DL, MVT::i32));
12817 }
12818 return Cvt;
12819 }
12820 }
12821
12822 return SDValue();
12823}
12824
12825SDValue SITargetLowering::performFCopySignCombine(SDNode *N,
12826 DAGCombinerInfo &DCI) const {
12827 SDValue MagnitudeOp = N->getOperand(0);
12828 SDValue SignOp = N->getOperand(1);
12829
12830 // The generic combine for fcopysign + fp cast is too conservative with
12831 // vectors, and also gets confused by the splitting we will perform here, so
12832 // peek through FP casts.
12833 if (SignOp.getOpcode() == ISD::FP_EXTEND ||
12834 SignOp.getOpcode() == ISD::FP_ROUND)
12835 SignOp = SignOp.getOperand(0);
12836
12837 SelectionDAG &DAG = DCI.DAG;
12838 SDLoc DL(N);
12839 EVT SignVT = SignOp.getValueType();
12840
12841 // f64 fcopysign is really an f32 copysign on the high bits, so replace the
12842 // lower half with a copy.
12843 // fcopysign f64:x, _:y -> x.lo32, (fcopysign (f32 x.hi32), _:y)
12844 EVT MagVT = MagnitudeOp.getValueType();
12845
12846 unsigned NumElts = MagVT.isVector() ? MagVT.getVectorNumElements() : 1;
12847
12848 if (MagVT.getScalarType() == MVT::f64) {
12849 EVT F32VT = MagVT.isVector()
12850 ? EVT::getVectorVT(*DAG.getContext(), MVT::f32, 2 * NumElts)
12851 : MVT::v2f32;
12852
12853 SDValue MagAsVector = DAG.getNode(ISD::BITCAST, DL, F32VT, MagnitudeOp);
12854
12856 for (unsigned I = 0; I != NumElts; ++I) {
12857 SDValue MagLo =
12858 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, MagAsVector,
12859 DAG.getConstant(2 * I, DL, MVT::i32));
12860 SDValue MagHi =
12861 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, MagAsVector,
12862 DAG.getConstant(2 * I + 1, DL, MVT::i32));
12863
12864 SDValue SignOpElt =
12865 MagVT.isVector()
12867 SignOp, DAG.getConstant(I, DL, MVT::i32))
12868 : SignOp;
12869
12870 SDValue HiOp =
12871 DAG.getNode(ISD::FCOPYSIGN, DL, MVT::f32, MagHi, SignOpElt);
12872
12873 SDValue Vector =
12874 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2f32, MagLo, HiOp);
12875
12876 SDValue NewElt = DAG.getNode(ISD::BITCAST, DL, MVT::f64, Vector);
12877 NewElts.push_back(NewElt);
12878 }
12879
12880 if (NewElts.size() == 1)
12881 return NewElts[0];
12882
12883 return DAG.getNode(ISD::BUILD_VECTOR, DL, MagVT, NewElts);
12884 }
12885
12886 if (SignVT.getScalarType() != MVT::f64)
12887 return SDValue();
12888
12889 // Reduce width of sign operand, we only need the highest bit.
12890 //
12891 // fcopysign f64:x, f64:y ->
12892 // fcopysign f64:x, (extract_vector_elt (bitcast f64:y to v2f32), 1)
12893 // TODO: In some cases it might make sense to go all the way to f16.
12894
12895 EVT F32VT = MagVT.isVector()
12896 ? EVT::getVectorVT(*DAG.getContext(), MVT::f32, 2 * NumElts)
12897 : MVT::v2f32;
12898
12899 SDValue SignAsVector = DAG.getNode(ISD::BITCAST, DL, F32VT, SignOp);
12900
12901 SmallVector<SDValue, 8> F32Signs;
12902 for (unsigned I = 0; I != NumElts; ++I) {
12903 // Take sign from odd elements of cast vector
12904 SDValue SignAsF32 =
12905 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, SignAsVector,
12906 DAG.getConstant(2 * I + 1, DL, MVT::i32));
12907 F32Signs.push_back(SignAsF32);
12908 }
12909
12910 SDValue NewSign =
12911 NumElts == 1
12912 ? F32Signs.back()
12914 EVT::getVectorVT(*DAG.getContext(), MVT::f32, NumElts),
12915 F32Signs);
12916
12917 return DAG.getNode(ISD::FCOPYSIGN, DL, N->getValueType(0), N->getOperand(0),
12918 NewSign);
12919}
12920
12921// (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2)
12922// (shl (or x, c1), c2) -> add (shl x, c2), (shl c1, c2) iff x and c1 share no
12923// bits
12924
12925// This is a variant of
12926// (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2),
12927//
12928// The normal DAG combiner will do this, but only if the add has one use since
12929// that would increase the number of instructions.
12930//
12931// This prevents us from seeing a constant offset that can be folded into a
12932// memory instruction's addressing mode. If we know the resulting add offset of
12933// a pointer can be folded into an addressing offset, we can replace the pointer
12934// operand with the add of new constant offset. This eliminates one of the uses,
12935// and may allow the remaining use to also be simplified.
12936//
12937SDValue SITargetLowering::performSHLPtrCombine(SDNode *N, unsigned AddrSpace,
12938 EVT MemVT,
12939 DAGCombinerInfo &DCI) const {
12940 SDValue N0 = N->getOperand(0);
12941 SDValue N1 = N->getOperand(1);
12942
12943 // We only do this to handle cases where it's profitable when there are
12944 // multiple uses of the add, so defer to the standard combine.
12945 if ((!N0->isAnyAdd() && N0.getOpcode() != ISD::OR) || N0->hasOneUse())
12946 return SDValue();
12947
12948 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1);
12949 if (!CN1)
12950 return SDValue();
12951
12952 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1));
12953 if (!CAdd)
12954 return SDValue();
12955
12956 SelectionDAG &DAG = DCI.DAG;
12957
12958 if (N0->getOpcode() == ISD::OR &&
12959 !DAG.haveNoCommonBitsSet(N0.getOperand(0), N0.getOperand(1)))
12960 return SDValue();
12961
12962 // If the resulting offset is too large, we can't fold it into the
12963 // addressing mode offset.
12964 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue();
12965 Type *Ty = MemVT.getTypeForEVT(*DCI.DAG.getContext());
12966
12967 AddrMode AM;
12968 AM.HasBaseReg = true;
12969 AM.BaseOffs = Offset.getSExtValue();
12970 if (!isLegalAddressingMode(DCI.DAG.getDataLayout(), AM, Ty, AddrSpace))
12971 return SDValue();
12972
12973 SDLoc SL(N);
12974 EVT VT = N->getValueType(0);
12975
12976 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1);
12977 SDValue COffset = DAG.getConstant(Offset, SL, VT);
12978
12979 SDNodeFlags Flags;
12980 Flags.setNoUnsignedWrap(
12981 N->getFlags().hasNoUnsignedWrap() &&
12982 (N0.getOpcode() == ISD::OR || N0->getFlags().hasNoUnsignedWrap()));
12983
12984 // Use ISD::ADD even if the original operation was ISD::PTRADD, since we can't
12985 // be sure that the new left operand is a proper base pointer.
12986 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset, Flags);
12987}
12988
12989/// MemSDNode::getBasePtr() does not work for intrinsics, which needs to offset
12990/// by the chain and intrinsic ID. Theoretically we would also need to check the
12991/// specific intrinsic, but they all place the pointer operand first.
12992static unsigned getBasePtrIndex(const MemSDNode *N) {
12993 switch (N->getOpcode()) {
12994 case ISD::STORE:
12997 return 2;
12998 default:
12999 return 1;
13000 }
13001}
13002
13003SDValue SITargetLowering::performMemSDNodeCombine(MemSDNode *N,
13004 DAGCombinerInfo &DCI) const {
13005 SelectionDAG &DAG = DCI.DAG;
13006
13007 unsigned PtrIdx = getBasePtrIndex(N);
13008 SDValue Ptr = N->getOperand(PtrIdx);
13009
13010 // TODO: We could also do this for multiplies.
13011 if (Ptr.getOpcode() == ISD::SHL) {
13012 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), N->getAddressSpace(),
13013 N->getMemoryVT(), DCI);
13014 if (NewPtr) {
13015 SmallVector<SDValue, 8> NewOps(N->ops());
13016
13017 NewOps[PtrIdx] = NewPtr;
13018 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
13019 }
13020 }
13021
13022 return SDValue();
13023}
13024
13025static bool bitOpWithConstantIsReducible(unsigned Opc, uint32_t Val) {
13026 return (Opc == ISD::AND && (Val == 0 || Val == 0xffffffff)) ||
13027 (Opc == ISD::OR && (Val == 0xffffffff || Val == 0)) ||
13028 (Opc == ISD::XOR && Val == 0);
13029}
13030
13031// Break up 64-bit bit operation of a constant into two 32-bit and/or/xor. This
13032// will typically happen anyway for a VALU 64-bit and. This exposes other 32-bit
13033// integer combine opportunities since most 64-bit operations are decomposed
13034// this way. TODO: We won't want this for SALU especially if it is an inline
13035// immediate.
13036SDValue SITargetLowering::splitBinaryBitConstantOp(
13037 DAGCombinerInfo &DCI, const SDLoc &SL, unsigned Opc, SDValue LHS,
13038 const ConstantSDNode *CRHS) const {
13039 uint64_t Val = CRHS->getZExtValue();
13040 uint32_t ValLo = Lo_32(Val);
13041 uint32_t ValHi = Hi_32(Val);
13042 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
13043
13044 if ((bitOpWithConstantIsReducible(Opc, ValLo) ||
13046 (CRHS->hasOneUse() && !TII->isInlineConstant(CRHS->getAPIntValue()))) {
13047 // We have 64-bit scalar and/or/xor, but do not have vector forms.
13048 if (Subtarget->has64BitLiterals() && CRHS->hasOneUse() &&
13049 !CRHS->user_begin()->isDivergent())
13050 return SDValue();
13051
13052 // If we need to materialize a 64-bit immediate, it will be split up later
13053 // anyway. Avoid creating the harder to understand 64-bit immediate
13054 // materialization.
13055 return splitBinaryBitConstantOpImpl(DCI, SL, Opc, LHS, ValLo, ValHi);
13056 }
13057
13058 return SDValue();
13059}
13060
13062 if (V.getValueType() != MVT::i1)
13063 return false;
13064 switch (V.getOpcode()) {
13065 default:
13066 break;
13067 case ISD::SETCC:
13068 case ISD::IS_FPCLASS:
13070 return true;
13071 case ISD::AND:
13072 case ISD::OR:
13073 case ISD::XOR:
13074 return isBoolSGPR(V.getOperand(0)) && isBoolSGPR(V.getOperand(1));
13075 case ISD::SADDO:
13076 case ISD::UADDO:
13077 case ISD::SSUBO:
13078 case ISD::USUBO:
13079 case ISD::SMULO:
13080 case ISD::UMULO:
13081 return V.getResNo() == 1;
13083 unsigned IntrinsicID = V.getConstantOperandVal(0);
13084 switch (IntrinsicID) {
13085 case Intrinsic::amdgcn_is_shared:
13086 case Intrinsic::amdgcn_is_private:
13087 return true;
13088 default:
13089 return false;
13090 }
13091
13092 return false;
13093 }
13094 }
13095 return false;
13096}
13097
13098// If a constant has all zeroes or all ones within each byte return it.
13099// Otherwise return 0.
13101 // 0xff for any zero byte in the mask
13102 uint32_t ZeroByteMask = 0;
13103 if (!(C & 0x000000ff))
13104 ZeroByteMask |= 0x000000ff;
13105 if (!(C & 0x0000ff00))
13106 ZeroByteMask |= 0x0000ff00;
13107 if (!(C & 0x00ff0000))
13108 ZeroByteMask |= 0x00ff0000;
13109 if (!(C & 0xff000000))
13110 ZeroByteMask |= 0xff000000;
13111 uint32_t NonZeroByteMask = ~ZeroByteMask; // 0xff for any non-zero byte
13112 if ((NonZeroByteMask & C) != NonZeroByteMask)
13113 return 0; // Partial bytes selected.
13114 return C;
13115}
13116
13117// Check if a node selects whole bytes from its operand 0 starting at a byte
13118// boundary while masking the rest. Returns select mask as in the v_perm_b32
13119// or -1 if not succeeded.
13120// Note byte select encoding:
13121// value 0-3 selects corresponding source byte;
13122// value 0xc selects zero;
13123// value 0xff selects 0xff.
13125 assert(V.getValueSizeInBits() == 32);
13126
13127 if (V.getNumOperands() != 2)
13128 return ~0;
13129
13130 ConstantSDNode *N1 = dyn_cast<ConstantSDNode>(V.getOperand(1));
13131 if (!N1)
13132 return ~0;
13133
13134 uint32_t C = N1->getZExtValue();
13135
13136 switch (V.getOpcode()) {
13137 default:
13138 break;
13139 case ISD::AND:
13140 if (uint32_t ConstMask = getConstantPermuteMask(C))
13141 return (0x03020100 & ConstMask) | (0x0c0c0c0c & ~ConstMask);
13142 break;
13143
13144 case ISD::OR:
13145 if (uint32_t ConstMask = getConstantPermuteMask(C))
13146 return (0x03020100 & ~ConstMask) | ConstMask;
13147 break;
13148
13149 case ISD::SHL:
13150 if (C % 8)
13151 return ~0;
13152
13153 return uint32_t((0x030201000c0c0c0cull << C) >> 32);
13154
13155 case ISD::SRL:
13156 if (C % 8)
13157 return ~0;
13158
13159 return uint32_t(0x0c0c0c0c03020100ull >> C);
13160 }
13161
13162 return ~0;
13163}
13164
13165SDValue SITargetLowering::performAndCombine(SDNode *N,
13166 DAGCombinerInfo &DCI) const {
13167 if (DCI.isBeforeLegalize())
13168 return SDValue();
13169
13170 SelectionDAG &DAG = DCI.DAG;
13171 EVT VT = N->getValueType(0);
13172 SDValue LHS = N->getOperand(0);
13173 SDValue RHS = N->getOperand(1);
13174
13175 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS);
13176 if (VT == MVT::i64 && CRHS) {
13177 if (SDValue Split =
13178 splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::AND, LHS, CRHS))
13179 return Split;
13180 }
13181
13182 if (CRHS && VT == MVT::i32) {
13183 // and (srl x, c), mask => shl (bfe x, nb + c, mask >> nb), nb
13184 // nb = number of trailing zeroes in mask
13185 // It can be optimized out using SDWA for GFX8+ in the SDWA peephole pass,
13186 // given that we are selecting 8 or 16 bit fields starting at byte boundary.
13187 uint64_t Mask = CRHS->getZExtValue();
13188 unsigned Bits = llvm::popcount(Mask);
13189 if (getSubtarget()->hasSDWA() && LHS->getOpcode() == ISD::SRL &&
13190 (Bits == 8 || Bits == 16) && isShiftedMask_64(Mask) && !(Mask & 1)) {
13191 if (auto *CShift = dyn_cast<ConstantSDNode>(LHS->getOperand(1))) {
13192 unsigned Shift = CShift->getZExtValue();
13193 unsigned NB = CRHS->getAPIntValue().countr_zero();
13194 unsigned Offset = NB + Shift;
13195 if ((Offset & (Bits - 1)) == 0) { // Starts at a byte or word boundary.
13196 SDLoc SL(N);
13197 SDValue BFE =
13198 DAG.getNode(AMDGPUISD::BFE_U32, SL, MVT::i32, LHS->getOperand(0),
13199 DAG.getConstant(Offset, SL, MVT::i32),
13200 DAG.getConstant(Bits, SL, MVT::i32));
13201 EVT NarrowVT = EVT::getIntegerVT(*DAG.getContext(), Bits);
13202 SDValue Ext = DAG.getNode(ISD::AssertZext, SL, VT, BFE,
13203 DAG.getValueType(NarrowVT));
13204 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(LHS), VT, Ext,
13205 DAG.getConstant(NB, SDLoc(CRHS), MVT::i32));
13206 return Shl;
13207 }
13208 }
13209 }
13210
13211 // and (perm x, y, c1), c2 -> perm x, y, permute_mask(c1, c2)
13212 if (LHS.hasOneUse() && LHS.getOpcode() == AMDGPUISD::PERM &&
13213 isa<ConstantSDNode>(LHS.getOperand(2))) {
13214 uint32_t Sel = getConstantPermuteMask(Mask);
13215 if (!Sel)
13216 return SDValue();
13217
13218 // Select 0xc for all zero bytes
13219 Sel = (LHS.getConstantOperandVal(2) & Sel) | (~Sel & 0x0c0c0c0c);
13220 SDLoc DL(N);
13221 return DAG.getNode(AMDGPUISD::PERM, DL, MVT::i32, LHS.getOperand(0),
13222 LHS.getOperand(1), DAG.getConstant(Sel, DL, MVT::i32));
13223 }
13224 }
13225
13226 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) ->
13227 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity)
13228 if (LHS.getOpcode() == ISD::SETCC && RHS.getOpcode() == ISD::SETCC) {
13229 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
13230 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get();
13231
13232 SDValue X = LHS.getOperand(0);
13233 SDValue Y = RHS.getOperand(0);
13234 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X ||
13235 !isTypeLegal(X.getValueType()))
13236 return SDValue();
13237
13238 if (LCC == ISD::SETO) {
13239 if (X != LHS.getOperand(1))
13240 return SDValue();
13241
13242 if (RCC == ISD::SETUNE) {
13243 const ConstantFPSDNode *C1 =
13244 dyn_cast<ConstantFPSDNode>(RHS.getOperand(1));
13245 if (!C1 || !C1->isInfinity() || C1->isNegative())
13246 return SDValue();
13247
13248 const uint32_t Mask = SIInstrFlags::N_NORMAL |
13252
13253 static_assert(
13256 0x3ff) == Mask,
13257 "mask not equal");
13258
13259 SDLoc DL(N);
13260 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1, X,
13261 DAG.getConstant(Mask, DL, MVT::i32));
13262 }
13263 }
13264 }
13265
13266 if (RHS.getOpcode() == ISD::SETCC && LHS.getOpcode() == AMDGPUISD::FP_CLASS)
13267 std::swap(LHS, RHS);
13268
13269 if (LHS.getOpcode() == ISD::SETCC && RHS.getOpcode() == AMDGPUISD::FP_CLASS &&
13270 RHS.hasOneUse()) {
13271 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
13272 // and (fcmp seto), (fp_class x, mask) -> fp_class x, mask & ~(p_nan |
13273 // n_nan) and (fcmp setuo), (fp_class x, mask) -> fp_class x, mask & (p_nan
13274 // | n_nan)
13275 const ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
13276 if ((LCC == ISD::SETO || LCC == ISD::SETUO) && Mask &&
13277 (RHS.getOperand(0) == LHS.getOperand(0) &&
13278 LHS.getOperand(0) == LHS.getOperand(1))) {
13279 const unsigned OrdMask = SIInstrFlags::S_NAN | SIInstrFlags::Q_NAN;
13280 unsigned NewMask = LCC == ISD::SETO ? Mask->getZExtValue() & ~OrdMask
13281 : Mask->getZExtValue() & OrdMask;
13282
13283 SDLoc DL(N);
13284 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1, RHS.getOperand(0),
13285 DAG.getConstant(NewMask, DL, MVT::i32));
13286 }
13287 }
13288
13289 if (VT == MVT::i32 && (RHS.getOpcode() == ISD::SIGN_EXTEND ||
13290 LHS.getOpcode() == ISD::SIGN_EXTEND)) {
13291 // and x, (sext cc from i1) => select cc, x, 0
13292 if (RHS.getOpcode() != ISD::SIGN_EXTEND)
13293 std::swap(LHS, RHS);
13294 if (isBoolSGPR(RHS.getOperand(0)))
13295 return DAG.getSelect(SDLoc(N), MVT::i32, RHS.getOperand(0), LHS,
13296 DAG.getConstant(0, SDLoc(N), MVT::i32));
13297 }
13298
13299 // and (op x, c1), (op y, c2) -> perm x, y, permute_mask(c1, c2)
13300 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
13301 if (VT == MVT::i32 && LHS.hasOneUse() && RHS.hasOneUse() &&
13302 N->isDivergent() && TII->pseudoToMCOpcode(AMDGPU::V_PERM_B32_e64) != -1) {
13303 uint32_t LHSMask = getPermuteMask(LHS);
13304 uint32_t RHSMask = getPermuteMask(RHS);
13305 if (LHSMask != ~0u && RHSMask != ~0u) {
13306 // Canonicalize the expression in an attempt to have fewer unique masks
13307 // and therefore fewer registers used to hold the masks.
13308 if (LHSMask > RHSMask) {
13309 std::swap(LHSMask, RHSMask);
13310 std::swap(LHS, RHS);
13311 }
13312
13313 // Select 0xc for each lane used from source operand. Zero has 0xc mask
13314 // set, 0xff have 0xff in the mask, actual lanes are in the 0-3 range.
13315 uint32_t LHSUsedLanes = ~(LHSMask & 0x0c0c0c0c) & 0x0c0c0c0c;
13316 uint32_t RHSUsedLanes = ~(RHSMask & 0x0c0c0c0c) & 0x0c0c0c0c;
13317
13318 // Check of we need to combine values from two sources within a byte.
13319 if (!(LHSUsedLanes & RHSUsedLanes) &&
13320 // If we select high and lower word keep it for SDWA.
13321 // TODO: teach SDWA to work with v_perm_b32 and remove the check.
13322 !(LHSUsedLanes == 0x0c0c0000 && RHSUsedLanes == 0x00000c0c)) {
13323 // Each byte in each mask is either selector mask 0-3, or has higher
13324 // bits set in either of masks, which can be 0xff for 0xff or 0x0c for
13325 // zero. If 0x0c is in either mask it shall always be 0x0c. Otherwise
13326 // mask which is not 0xff wins. By anding both masks we have a correct
13327 // result except that 0x0c shall be corrected to give 0x0c only.
13328 uint32_t Mask = LHSMask & RHSMask;
13329 for (unsigned I = 0; I < 32; I += 8) {
13330 uint32_t ByteSel = 0xff << I;
13331 if ((LHSMask & ByteSel) == 0x0c || (RHSMask & ByteSel) == 0x0c)
13332 Mask &= (0x0c << I) & 0xffffffff;
13333 }
13334
13335 // Add 4 to each active LHS lane. It will not affect any existing 0xff
13336 // or 0x0c.
13337 uint32_t Sel = Mask | (LHSUsedLanes & 0x04040404);
13338 SDLoc DL(N);
13339
13340 return DAG.getNode(AMDGPUISD::PERM, DL, MVT::i32, LHS.getOperand(0),
13341 RHS.getOperand(0),
13342 DAG.getConstant(Sel, DL, MVT::i32));
13343 }
13344 }
13345 }
13346
13347 return SDValue();
13348}
13349
13350// A key component of v_perm is a mapping between byte position of the src
13351// operands, and the byte position of the dest. To provide such, we need: 1. the
13352// node that provides x byte of the dest of the OR, and 2. the byte of the node
13353// used to provide that x byte. calculateByteProvider finds which node provides
13354// a certain byte of the dest of the OR, and calculateSrcByte takes that node,
13355// and finds an ultimate src and byte position For example: The supported
13356// LoadCombine pattern for vector loads is as follows
13357// t1
13358// or
13359// / \
13360// t2 t3
13361// zext shl
13362// | | \
13363// t4 t5 16
13364// or anyext
13365// / \ |
13366// t6 t7 t8
13367// srl shl or
13368// / | / \ / \
13369// t9 t10 t11 t12 t13 t14
13370// trunc* 8 trunc* 8 and and
13371// | | / | | \
13372// t15 t16 t17 t18 t19 t20
13373// trunc* 255 srl -256
13374// | / \
13375// t15 t15 16
13376//
13377// *In this example, the truncs are from i32->i16
13378//
13379// calculateByteProvider would find t6, t7, t13, and t14 for bytes 0-3
13380// respectively. calculateSrcByte would find (given node) -> ultimate src &
13381// byteposition: t6 -> t15 & 1, t7 -> t16 & 0, t13 -> t15 & 0, t14 -> t15 & 3.
13382// After finding the mapping, we can combine the tree into vperm t15, t16,
13383// 0x05000407
13384
13385// Find the source and byte position from a node.
13386// \p DestByte is the byte position of the dest of the or that the src
13387// ultimately provides. \p SrcIndex is the byte of the src that maps to this
13388// dest of the or byte. \p Depth tracks how many recursive iterations we have
13389// performed.
13390static const std::optional<ByteProvider<SDValue>>
13391calculateSrcByte(const SDValue Op, uint64_t DestByte, uint64_t SrcIndex = 0,
13392 unsigned Depth = 0) {
13393 // We may need to recursively traverse a series of SRLs
13394 if (Depth >= 6)
13395 return std::nullopt;
13396
13397 if (Op.getValueSizeInBits() < 8)
13398 return std::nullopt;
13399
13400 if (Op.getValueType().isVector())
13401 return ByteProvider<SDValue>::getSrc(Op, DestByte, SrcIndex);
13402
13403 switch (Op->getOpcode()) {
13404 case ISD::TRUNCATE: {
13405 return calculateSrcByte(Op->getOperand(0), DestByte, SrcIndex, Depth + 1);
13406 }
13407
13408 case ISD::SIGN_EXTEND:
13409 case ISD::ZERO_EXTEND:
13411 SDValue NarrowOp = Op->getOperand(0);
13412 auto NarrowVT = NarrowOp.getValueType();
13413 if (Op->getOpcode() == ISD::SIGN_EXTEND_INREG) {
13414 auto *VTSign = cast<VTSDNode>(Op->getOperand(1));
13415 NarrowVT = VTSign->getVT();
13416 }
13417 if (!NarrowVT.isByteSized())
13418 return std::nullopt;
13419 uint64_t NarrowByteWidth = NarrowVT.getStoreSize();
13420
13421 if (SrcIndex >= NarrowByteWidth)
13422 return std::nullopt;
13423 return calculateSrcByte(Op->getOperand(0), DestByte, SrcIndex, Depth + 1);
13424 }
13425
13426 case ISD::SRA:
13427 case ISD::SRL: {
13428 auto *ShiftOp = dyn_cast<ConstantSDNode>(Op->getOperand(1));
13429 if (!ShiftOp)
13430 return std::nullopt;
13431
13432 uint64_t BitShift = ShiftOp->getZExtValue();
13433
13434 if (BitShift % 8 != 0)
13435 return std::nullopt;
13436
13437 SrcIndex += BitShift / 8;
13438
13439 return calculateSrcByte(Op->getOperand(0), DestByte, SrcIndex, Depth + 1);
13440 }
13441
13442 default: {
13443 return ByteProvider<SDValue>::getSrc(Op, DestByte, SrcIndex);
13444 }
13445 }
13446 llvm_unreachable("fully handled switch");
13447}
13448
13449// For a byte position in the result of an Or, traverse the tree and find the
13450// node (and the byte of the node) which ultimately provides this {Or,
13451// BytePosition}. \p Op is the operand we are currently examining. \p Index is
13452// the byte position of the Op that corresponds with the originally requested
13453// byte of the Or \p Depth tracks how many recursive iterations we have
13454// performed. \p StartingIndex is the originally requested byte of the Or
13455static const std::optional<ByteProvider<SDValue>>
13456calculateByteProvider(const SDValue &Op, unsigned Index, unsigned Depth,
13457 unsigned StartingIndex = 0) {
13458 // Finding Src tree of RHS of or typically requires at least 1 additional
13459 // depth
13460 if (Depth > 6)
13461 return std::nullopt;
13462
13463 unsigned BitWidth = Op.getScalarValueSizeInBits();
13464 if (BitWidth % 8 != 0)
13465 return std::nullopt;
13466 if (Index > BitWidth / 8 - 1)
13467 return std::nullopt;
13468
13469 bool IsVec = Op.getValueType().isVector();
13470 switch (Op.getOpcode()) {
13471 case ISD::OR: {
13472 if (IsVec)
13473 return std::nullopt;
13474
13475 auto RHS = calculateByteProvider(Op.getOperand(1), Index, Depth + 1,
13476 StartingIndex);
13477 if (!RHS)
13478 return std::nullopt;
13479 auto LHS = calculateByteProvider(Op.getOperand(0), Index, Depth + 1,
13480 StartingIndex);
13481 if (!LHS)
13482 return std::nullopt;
13483 // A well formed Or will have two ByteProviders for each byte, one of which
13484 // is constant zero
13485 if (!LHS->isConstantZero() && !RHS->isConstantZero())
13486 return std::nullopt;
13487 if (!LHS || LHS->isConstantZero())
13488 return RHS;
13489 if (!RHS || RHS->isConstantZero())
13490 return LHS;
13491 return std::nullopt;
13492 }
13493
13494 case ISD::AND: {
13495 if (IsVec)
13496 return std::nullopt;
13497
13498 auto *BitMaskOp = dyn_cast<ConstantSDNode>(Op->getOperand(1));
13499 if (!BitMaskOp)
13500 return std::nullopt;
13501
13502 uint32_t BitMask = BitMaskOp->getZExtValue();
13503 // Bits we expect for our StartingIndex
13504 uint32_t IndexMask = 0xFF << (Index * 8);
13505
13506 if ((IndexMask & BitMask) != IndexMask) {
13507 // If the result of the and partially provides the byte, then it
13508 // is not well formatted
13509 if (IndexMask & BitMask)
13510 return std::nullopt;
13512 }
13513
13514 return calculateSrcByte(Op->getOperand(0), StartingIndex, Index);
13515 }
13516
13517 case ISD::FSHR: {
13518 if (IsVec)
13519 return std::nullopt;
13520
13521 // fshr(X,Y,Z): (X << (BW - (Z % BW))) | (Y >> (Z % BW))
13522 auto *ShiftOp = dyn_cast<ConstantSDNode>(Op->getOperand(2));
13523 if (!ShiftOp || Op.getValueType().isVector())
13524 return std::nullopt;
13525
13526 uint64_t BitsProvided = Op.getValueSizeInBits();
13527 if (BitsProvided % 8 != 0)
13528 return std::nullopt;
13529
13530 uint64_t BitShift = ShiftOp->getAPIntValue().urem(BitsProvided);
13531 if (BitShift % 8)
13532 return std::nullopt;
13533
13534 uint64_t ConcatSizeInBytes = BitsProvided / 4;
13535 uint64_t ByteShift = BitShift / 8;
13536
13537 uint64_t NewIndex = (Index + ByteShift) % ConcatSizeInBytes;
13538 uint64_t BytesProvided = BitsProvided / 8;
13539 SDValue NextOp = Op.getOperand(NewIndex >= BytesProvided ? 0 : 1);
13540 NewIndex %= BytesProvided;
13541 return calculateByteProvider(NextOp, NewIndex, Depth + 1, StartingIndex);
13542 }
13543
13544 case ISD::SRA:
13545 case ISD::SRL: {
13546 if (IsVec)
13547 return std::nullopt;
13548
13549 auto *ShiftOp = dyn_cast<ConstantSDNode>(Op->getOperand(1));
13550 if (!ShiftOp)
13551 return std::nullopt;
13552
13553 uint64_t BitShift = ShiftOp->getZExtValue();
13554 if (BitShift % 8)
13555 return std::nullopt;
13556
13557 auto BitsProvided = Op.getScalarValueSizeInBits();
13558 if (BitsProvided % 8 != 0)
13559 return std::nullopt;
13560
13561 uint64_t BytesProvided = BitsProvided / 8;
13562 uint64_t ByteShift = BitShift / 8;
13563 // The dest of shift will have good [0 : (BytesProvided - ByteShift)] bytes.
13564 // If the byte we are trying to provide (as tracked by index) falls in this
13565 // range, then the SRL provides the byte. The byte of interest of the src of
13566 // the SRL is Index + ByteShift
13567 return BytesProvided - ByteShift > Index
13568 ? calculateSrcByte(Op->getOperand(0), StartingIndex,
13569 Index + ByteShift)
13571 }
13572
13573 case ISD::SHL: {
13574 if (IsVec)
13575 return std::nullopt;
13576
13577 auto *ShiftOp = dyn_cast<ConstantSDNode>(Op->getOperand(1));
13578 if (!ShiftOp)
13579 return std::nullopt;
13580
13581 uint64_t BitShift = ShiftOp->getZExtValue();
13582 if (BitShift % 8 != 0)
13583 return std::nullopt;
13584 uint64_t ByteShift = BitShift / 8;
13585
13586 // If we are shifting by an amount greater than (or equal to)
13587 // the index we are trying to provide, then it provides 0s. If not,
13588 // then this bytes are not definitively 0s, and the corresponding byte
13589 // of interest is Index - ByteShift of the src
13590 return Index < ByteShift
13592 : calculateByteProvider(Op.getOperand(0), Index - ByteShift,
13593 Depth + 1, StartingIndex);
13594 }
13595 case ISD::ANY_EXTEND:
13596 case ISD::SIGN_EXTEND:
13597 case ISD::ZERO_EXTEND:
13599 case ISD::AssertZext:
13600 case ISD::AssertSext: {
13601 if (IsVec)
13602 return std::nullopt;
13603
13604 SDValue NarrowOp = Op->getOperand(0);
13605 unsigned NarrowBitWidth = NarrowOp.getValueSizeInBits();
13606 if (Op->getOpcode() == ISD::SIGN_EXTEND_INREG ||
13607 Op->getOpcode() == ISD::AssertZext ||
13608 Op->getOpcode() == ISD::AssertSext) {
13609 auto *VTSign = cast<VTSDNode>(Op->getOperand(1));
13610 NarrowBitWidth = VTSign->getVT().getSizeInBits();
13611 }
13612 if (NarrowBitWidth % 8 != 0)
13613 return std::nullopt;
13614 uint64_t NarrowByteWidth = NarrowBitWidth / 8;
13615
13616 if (Index >= NarrowByteWidth)
13617 return Op.getOpcode() == ISD::ZERO_EXTEND
13618 ? std::optional<ByteProvider<SDValue>>(
13620 : std::nullopt;
13621 return calculateByteProvider(NarrowOp, Index, Depth + 1, StartingIndex);
13622 }
13623
13624 case ISD::TRUNCATE: {
13625 if (IsVec)
13626 return std::nullopt;
13627
13628 uint64_t NarrowByteWidth = BitWidth / 8;
13629
13630 if (NarrowByteWidth >= Index) {
13631 return calculateByteProvider(Op.getOperand(0), Index, Depth + 1,
13632 StartingIndex);
13633 }
13634
13635 return std::nullopt;
13636 }
13637
13638 case ISD::CopyFromReg: {
13639 if (BitWidth / 8 > Index)
13640 return calculateSrcByte(Op, StartingIndex, Index);
13641
13642 return std::nullopt;
13643 }
13644
13645 case ISD::LOAD: {
13646 auto *L = cast<LoadSDNode>(Op.getNode());
13647
13648 unsigned NarrowBitWidth = L->getMemoryVT().getSizeInBits();
13649 if (NarrowBitWidth % 8 != 0)
13650 return std::nullopt;
13651 uint64_t NarrowByteWidth = NarrowBitWidth / 8;
13652
13653 // If the width of the load does not reach byte we are trying to provide for
13654 // and it is not a ZEXTLOAD, then the load does not provide for the byte in
13655 // question
13656 if (Index >= NarrowByteWidth) {
13657 return L->getExtensionType() == ISD::ZEXTLOAD
13658 ? std::optional<ByteProvider<SDValue>>(
13660 : std::nullopt;
13661 }
13662
13663 if (NarrowByteWidth > Index) {
13664 return calculateSrcByte(Op, StartingIndex, Index);
13665 }
13666
13667 return std::nullopt;
13668 }
13669
13670 case ISD::BSWAP: {
13671 if (IsVec)
13672 return std::nullopt;
13673
13674 return calculateByteProvider(Op->getOperand(0), BitWidth / 8 - Index - 1,
13675 Depth + 1, StartingIndex);
13676 }
13677
13679 auto *IdxOp = dyn_cast<ConstantSDNode>(Op->getOperand(1));
13680 if (!IdxOp)
13681 return std::nullopt;
13682 auto VecIdx = IdxOp->getZExtValue();
13683 auto ScalarSize = Op.getScalarValueSizeInBits();
13684 if (ScalarSize < 32)
13685 Index = ScalarSize == 8 ? VecIdx : VecIdx * 2 + Index;
13686 return calculateSrcByte(ScalarSize >= 32 ? Op : Op.getOperand(0),
13687 StartingIndex, Index);
13688 }
13689
13690 case AMDGPUISD::PERM: {
13691 if (IsVec)
13692 return std::nullopt;
13693
13694 auto *PermMask = dyn_cast<ConstantSDNode>(Op->getOperand(2));
13695 if (!PermMask)
13696 return std::nullopt;
13697
13698 auto IdxMask =
13699 (PermMask->getZExtValue() & (0xFF << (Index * 8))) >> (Index * 8);
13700 if (IdxMask > 0x07 && IdxMask != 0x0c)
13701 return std::nullopt;
13702
13703 auto NextOp = Op.getOperand(IdxMask > 0x03 ? 0 : 1);
13704 auto NextIndex = IdxMask > 0x03 ? IdxMask % 4 : IdxMask;
13705
13706 return IdxMask != 0x0c ? calculateSrcByte(NextOp, StartingIndex, NextIndex)
13709 }
13710
13711 default: {
13712 return std::nullopt;
13713 }
13714 }
13715
13716 llvm_unreachable("fully handled switch");
13717}
13718
13719// Returns true if the Operand is a scalar and is 16 bits
13720static bool isExtendedFrom16Bits(SDValue &Operand) {
13721
13722 switch (Operand.getOpcode()) {
13723 case ISD::ANY_EXTEND:
13724 case ISD::SIGN_EXTEND:
13725 case ISD::ZERO_EXTEND: {
13726 auto OpVT = Operand.getOperand(0).getValueType();
13727 return !OpVT.isVector() && OpVT.getSizeInBits() == 16;
13728 }
13729 case ISD::LOAD: {
13730 LoadSDNode *L = cast<LoadSDNode>(Operand.getNode());
13731 auto ExtType = cast<LoadSDNode>(L)->getExtensionType();
13732 if (ExtType == ISD::ZEXTLOAD || ExtType == ISD::SEXTLOAD ||
13733 ExtType == ISD::EXTLOAD) {
13734 auto MemVT = L->getMemoryVT();
13735 return !MemVT.isVector() && MemVT.getSizeInBits() == 16;
13736 }
13737 return L->getMemoryVT().getSizeInBits() == 16;
13738 }
13739 default:
13740 return false;
13741 }
13742}
13743
13744// Returns true if the mask matches consecutive bytes, and the first byte
13745// begins at a power of 2 byte offset from 0th byte
13746static bool addresses16Bits(int Mask) {
13747 int Low8 = Mask & 0xff;
13748 int Hi8 = (Mask & 0xff00) >> 8;
13749
13750 assert(Low8 < 8 && Hi8 < 8);
13751 // Are the bytes contiguous in the order of increasing addresses.
13752 bool IsConsecutive = (Hi8 - Low8 == 1);
13753 // Is the first byte at location that is aligned for 16 bit instructions.
13754 // A counter example is taking 2 consecutive bytes starting at the 8th bit.
13755 // In this case, we still need code to extract the 16 bit operand, so it
13756 // is better to use i8 v_perm
13757 bool Is16Aligned = !(Low8 % 2);
13758
13759 return IsConsecutive && Is16Aligned;
13760}
13761
13762// Do not lower into v_perm if the operands are actually 16 bit
13763// and the selected bits (based on PermMask) correspond with two
13764// easily addressable 16 bit operands.
13766 SDValue &OtherOp) {
13767 int Low16 = PermMask & 0xffff;
13768 int Hi16 = (PermMask & 0xffff0000) >> 16;
13769
13770 auto TempOp = peekThroughBitcasts(Op);
13771 auto TempOtherOp = peekThroughBitcasts(OtherOp);
13772
13773 auto OpIs16Bit =
13774 TempOtherOp.getValueSizeInBits() == 16 || isExtendedFrom16Bits(TempOp);
13775 if (!OpIs16Bit)
13776 return true;
13777
13778 auto OtherOpIs16Bit = TempOtherOp.getValueSizeInBits() == 16 ||
13779 isExtendedFrom16Bits(TempOtherOp);
13780 if (!OtherOpIs16Bit)
13781 return true;
13782
13783 // Do we cleanly address both
13784 return !addresses16Bits(Low16) || !addresses16Bits(Hi16);
13785}
13786
13788 unsigned DWordOffset) {
13789 SDValue Ret;
13790
13791 auto TypeSize = Src.getValueSizeInBits().getFixedValue();
13792 // ByteProvider must be at least 8 bits
13793 assert(Src.getValueSizeInBits().isKnownMultipleOf(8));
13794
13795 if (TypeSize <= 32)
13796 return DAG.getBitcastedAnyExtOrTrunc(Src, SL, MVT::i32);
13797
13798 if (Src.getValueType().isVector()) {
13799 auto ScalarTySize = Src.getScalarValueSizeInBits();
13800 auto ScalarTy = Src.getValueType().getScalarType();
13801 if (ScalarTySize == 32) {
13802 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Src,
13803 DAG.getConstant(DWordOffset, SL, MVT::i32));
13804 }
13805 if (ScalarTySize > 32) {
13806 Ret = DAG.getNode(
13807 ISD::EXTRACT_VECTOR_ELT, SL, ScalarTy, Src,
13808 DAG.getConstant(DWordOffset / (ScalarTySize / 32), SL, MVT::i32));
13809 auto ShiftVal = 32 * (DWordOffset % (ScalarTySize / 32));
13810 if (ShiftVal)
13811 Ret = DAG.getNode(ISD::SRL, SL, Ret.getValueType(), Ret,
13812 DAG.getConstant(ShiftVal, SL, MVT::i32));
13813 return DAG.getBitcastedAnyExtOrTrunc(Ret, SL, MVT::i32);
13814 }
13815
13816 assert(ScalarTySize < 32);
13817 auto NumElements = TypeSize / ScalarTySize;
13818 auto Trunc32Elements = (ScalarTySize * NumElements) / 32;
13819 auto NormalizedTrunc = Trunc32Elements * 32 / ScalarTySize;
13820 auto NumElementsIn32 = 32 / ScalarTySize;
13821 auto NumAvailElements = DWordOffset < Trunc32Elements
13822 ? NumElementsIn32
13823 : NumElements - NormalizedTrunc;
13824
13826 DAG.ExtractVectorElements(Src, VecSrcs, DWordOffset * NumElementsIn32,
13827 NumAvailElements);
13828
13829 Ret = DAG.getBuildVector(
13830 MVT::getVectorVT(MVT::getIntegerVT(ScalarTySize), NumAvailElements), SL,
13831 VecSrcs);
13832 return Ret = DAG.getBitcastedAnyExtOrTrunc(Ret, SL, MVT::i32);
13833 }
13834
13835 /// Scalar Type
13836 auto ShiftVal = 32 * DWordOffset;
13837 Ret = DAG.getNode(ISD::SRL, SL, Src.getValueType(), Src,
13838 DAG.getConstant(ShiftVal, SL, MVT::i32));
13839 return DAG.getBitcastedAnyExtOrTrunc(Ret, SL, MVT::i32);
13840}
13841
13843 SelectionDAG &DAG = DCI.DAG;
13844 [[maybe_unused]] EVT VT = N->getValueType(0);
13846
13847 // VT is known to be MVT::i32, so we need to provide 4 bytes.
13848 assert(VT == MVT::i32);
13849 for (int i = 0; i < 4; i++) {
13850 // Find the ByteProvider that provides the ith byte of the result of OR
13851 std::optional<ByteProvider<SDValue>> P =
13852 calculateByteProvider(SDValue(N, 0), i, 0, /*StartingIndex = */ i);
13853 // TODO support constantZero
13854 if (!P || P->isConstantZero())
13855 return SDValue();
13856
13857 PermNodes.push_back(*P);
13858 }
13859 if (PermNodes.size() != 4)
13860 return SDValue();
13861
13862 std::pair<unsigned, unsigned> FirstSrc(0, PermNodes[0].SrcOffset / 4);
13863 std::optional<std::pair<unsigned, unsigned>> SecondSrc;
13864 uint64_t PermMask = 0x00000000;
13865 for (size_t i = 0; i < PermNodes.size(); i++) {
13866 auto PermOp = PermNodes[i];
13867 // Since the mask is applied to Src1:Src2, Src1 bytes must be offset
13868 // by sizeof(Src2) = 4
13869 int SrcByteAdjust = 4;
13870
13871 // If the Src uses a byte from a different DWORD, then it corresponds
13872 // with a difference source
13873 if (!PermOp.hasSameSrc(PermNodes[FirstSrc.first]) ||
13874 ((PermOp.SrcOffset / 4) != FirstSrc.second)) {
13875 if (SecondSrc)
13876 if (!PermOp.hasSameSrc(PermNodes[SecondSrc->first]) ||
13877 ((PermOp.SrcOffset / 4) != SecondSrc->second))
13878 return SDValue();
13879
13880 // Set the index of the second distinct Src node
13881 SecondSrc = {i, PermNodes[i].SrcOffset / 4};
13882 assert(!(PermNodes[SecondSrc->first].Src->getValueSizeInBits() % 8));
13883 SrcByteAdjust = 0;
13884 }
13885 assert((PermOp.SrcOffset % 4) + SrcByteAdjust < 8);
13887 PermMask |= ((PermOp.SrcOffset % 4) + SrcByteAdjust) << (i * 8);
13888 }
13889 SDLoc DL(N);
13890 SDValue Op = *PermNodes[FirstSrc.first].Src;
13891 Op = getDWordFromOffset(DAG, DL, Op, FirstSrc.second);
13892 assert(Op.getValueSizeInBits() == 32);
13893
13894 // Check that we are not just extracting the bytes in order from an op
13895 if (!SecondSrc) {
13896 int Low16 = PermMask & 0xffff;
13897 int Hi16 = (PermMask & 0xffff0000) >> 16;
13898
13899 bool WellFormedLow = (Low16 == 0x0504) || (Low16 == 0x0100);
13900 bool WellFormedHi = (Hi16 == 0x0706) || (Hi16 == 0x0302);
13901
13902 // The perm op would really just produce Op. So combine into Op
13903 if (WellFormedLow && WellFormedHi)
13904 return DAG.getBitcast(MVT::getIntegerVT(32), Op);
13905 }
13906
13907 SDValue OtherOp = SecondSrc ? *PermNodes[SecondSrc->first].Src : Op;
13908
13909 if (SecondSrc) {
13910 OtherOp = getDWordFromOffset(DAG, DL, OtherOp, SecondSrc->second);
13911 assert(OtherOp.getValueSizeInBits() == 32);
13912 }
13913
13914 if (hasNon16BitAccesses(PermMask, Op, OtherOp)) {
13915
13916 assert(Op.getValueType().isByteSized() &&
13917 OtherOp.getValueType().isByteSized());
13918
13919 // If the ultimate src is less than 32 bits, then we will only be
13920 // using bytes 0: Op.getValueSizeInBytes() - 1 in the or.
13921 // CalculateByteProvider would not have returned Op as source if we
13922 // used a byte that is outside its ValueType. Thus, we are free to
13923 // ANY_EXTEND as the extended bits are dont-cares.
13924 Op = DAG.getBitcastedAnyExtOrTrunc(Op, DL, MVT::i32);
13925 OtherOp = DAG.getBitcastedAnyExtOrTrunc(OtherOp, DL, MVT::i32);
13926
13927 return DAG.getNode(AMDGPUISD::PERM, DL, MVT::i32, Op, OtherOp,
13928 DAG.getConstant(PermMask, DL, MVT::i32));
13929 }
13930 return SDValue();
13931}
13932
13933SDValue SITargetLowering::performOrCombine(SDNode *N,
13934 DAGCombinerInfo &DCI) const {
13935 SelectionDAG &DAG = DCI.DAG;
13936 SDValue LHS = N->getOperand(0);
13937 SDValue RHS = N->getOperand(1);
13938
13939 EVT VT = N->getValueType(0);
13940 if (VT == MVT::i1) {
13941 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2)
13942 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS &&
13943 RHS.getOpcode() == AMDGPUISD::FP_CLASS) {
13944 SDValue Src = LHS.getOperand(0);
13945 if (Src != RHS.getOperand(0))
13946 return SDValue();
13947
13948 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
13949 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
13950 if (!CLHS || !CRHS)
13951 return SDValue();
13952
13953 // Only 10 bits are used.
13954 static const uint32_t MaxMask = 0x3ff;
13955
13956 uint32_t NewMask =
13957 (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask;
13958 SDLoc DL(N);
13959 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1, Src,
13960 DAG.getConstant(NewMask, DL, MVT::i32));
13961 }
13962
13963 return SDValue();
13964 }
13965
13966 // or (perm x, y, c1), c2 -> perm x, y, permute_mask(c1, c2)
13968 LHS.getOpcode() == AMDGPUISD::PERM &&
13969 isa<ConstantSDNode>(LHS.getOperand(2))) {
13970 uint32_t Sel = getConstantPermuteMask(N->getConstantOperandVal(1));
13971 if (!Sel)
13972 return SDValue();
13973
13974 Sel |= LHS.getConstantOperandVal(2);
13975 SDLoc DL(N);
13976 return DAG.getNode(AMDGPUISD::PERM, DL, MVT::i32, LHS.getOperand(0),
13977 LHS.getOperand(1), DAG.getConstant(Sel, DL, MVT::i32));
13978 }
13979
13980 // or (op x, c1), (op y, c2) -> perm x, y, permute_mask(c1, c2)
13981 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
13982 if (VT == MVT::i32 && LHS.hasOneUse() && RHS.hasOneUse() &&
13983 N->isDivergent() && TII->pseudoToMCOpcode(AMDGPU::V_PERM_B32_e64) != -1) {
13984
13985 // If all the uses of an or need to extract the individual elements, do not
13986 // attempt to lower into v_perm
13987 auto usesCombinedOperand = [](SDNode *OrUse) {
13988 // If we have any non-vectorized use, then it is a candidate for v_perm
13989 if (OrUse->getOpcode() != ISD::BITCAST ||
13990 !OrUse->getValueType(0).isVector())
13991 return true;
13992
13993 // If we have any non-vectorized use, then it is a candidate for v_perm
13994 for (auto *VUser : OrUse->users()) {
13995 if (!VUser->getValueType(0).isVector())
13996 return true;
13997
13998 // If the use of a vector is a store, then combining via a v_perm
13999 // is beneficial.
14000 // TODO -- whitelist more uses
14001 for (auto VectorwiseOp : {ISD::STORE, ISD::CopyToReg, ISD::CopyFromReg})
14002 if (VUser->getOpcode() == VectorwiseOp)
14003 return true;
14004 }
14005 return false;
14006 };
14007
14008 if (!any_of(N->users(), usesCombinedOperand))
14009 return SDValue();
14010
14011 uint32_t LHSMask = getPermuteMask(LHS);
14012 uint32_t RHSMask = getPermuteMask(RHS);
14013
14014 if (LHSMask != ~0u && RHSMask != ~0u) {
14015 // Canonicalize the expression in an attempt to have fewer unique masks
14016 // and therefore fewer registers used to hold the masks.
14017 if (LHSMask > RHSMask) {
14018 std::swap(LHSMask, RHSMask);
14019 std::swap(LHS, RHS);
14020 }
14021
14022 // Select 0xc for each lane used from source operand. Zero has 0xc mask
14023 // set, 0xff have 0xff in the mask, actual lanes are in the 0-3 range.
14024 uint32_t LHSUsedLanes = ~(LHSMask & 0x0c0c0c0c) & 0x0c0c0c0c;
14025 uint32_t RHSUsedLanes = ~(RHSMask & 0x0c0c0c0c) & 0x0c0c0c0c;
14026
14027 // Check of we need to combine values from two sources within a byte.
14028 if (!(LHSUsedLanes & RHSUsedLanes) &&
14029 // If we select high and lower word keep it for SDWA.
14030 // TODO: teach SDWA to work with v_perm_b32 and remove the check.
14031 !(LHSUsedLanes == 0x0c0c0000 && RHSUsedLanes == 0x00000c0c)) {
14032 // Kill zero bytes selected by other mask. Zero value is 0xc.
14033 LHSMask &= ~RHSUsedLanes;
14034 RHSMask &= ~LHSUsedLanes;
14035 // Add 4 to each active LHS lane
14036 LHSMask |= LHSUsedLanes & 0x04040404;
14037 // Combine masks
14038 uint32_t Sel = LHSMask | RHSMask;
14039 SDLoc DL(N);
14040
14041 return DAG.getNode(AMDGPUISD::PERM, DL, MVT::i32, LHS.getOperand(0),
14042 RHS.getOperand(0),
14043 DAG.getConstant(Sel, DL, MVT::i32));
14044 }
14045 }
14046 if (LHSMask == ~0u || RHSMask == ~0u) {
14047 if (SDValue Perm = matchPERM(N, DCI))
14048 return Perm;
14049 }
14050 }
14051
14052 // Detect identity v2i32 OR and replace with identity source node.
14053 // Specifically an Or that has operands constructed from the same source node
14054 // via extract_vector_elt and build_vector. I.E.
14055 // v2i32 or(
14056 // v2i32 build_vector(
14057 // i32 extract_elt(%IdentitySrc, 0),
14058 // i32 0
14059 // ),
14060 // v2i32 build_vector(
14061 // i32 0,
14062 // i32 extract_elt(%IdentitySrc, 1)
14063 // ) )
14064 // =>
14065 // v2i32 %IdentitySrc
14066
14067 if (VT == MVT::v2i32 && LHS->getOpcode() == ISD::BUILD_VECTOR &&
14068 RHS->getOpcode() == ISD::BUILD_VECTOR) {
14069
14070 ConstantSDNode *LC = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
14071 ConstantSDNode *RC = dyn_cast<ConstantSDNode>(RHS->getOperand(0));
14072
14073 // Test for and normalise build vectors.
14074 if (LC && RC && LC->getZExtValue() == 0 && RC->getZExtValue() == 0) {
14075
14076 // Get the extract_vector_element operands.
14077 SDValue LEVE = LHS->getOperand(0);
14078 SDValue REVE = RHS->getOperand(1);
14079
14080 if (LEVE->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
14082 // Check that different elements from the same vector are
14083 // extracted.
14084 if (LEVE->getOperand(0) == REVE->getOperand(0) &&
14085 LEVE->getOperand(1) != REVE->getOperand(1)) {
14086 SDValue IdentitySrc = LEVE.getOperand(0);
14087 return IdentitySrc;
14088 }
14089 }
14090 }
14091 }
14092
14093 if (VT != MVT::i64 || DCI.isBeforeLegalizeOps())
14094 return SDValue();
14095
14096 // TODO: This could be a generic combine with a predicate for extracting the
14097 // high half of an integer being free.
14098
14099 // (or i64:x, (zero_extend i32:y)) ->
14100 // i64 (bitcast (v2i32 build_vector (or i32:y, lo_32(x)), hi_32(x)))
14101 if (LHS.getOpcode() == ISD::ZERO_EXTEND &&
14102 RHS.getOpcode() != ISD::ZERO_EXTEND)
14103 std::swap(LHS, RHS);
14104
14105 if (RHS.getOpcode() == ISD::ZERO_EXTEND) {
14106 SDValue ExtSrc = RHS.getOperand(0);
14107 EVT SrcVT = ExtSrc.getValueType();
14108 if (SrcVT == MVT::i32) {
14109 SDLoc SL(N);
14110 auto [LowLHS, HiBits] = split64BitValue(LHS, DAG);
14111 SDValue LowOr = DAG.getNode(ISD::OR, SL, MVT::i32, LowLHS, ExtSrc);
14112
14113 DCI.AddToWorklist(LowOr.getNode());
14114 DCI.AddToWorklist(HiBits.getNode());
14115
14116 SDValue Vec =
14117 DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, LowOr, HiBits);
14118 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
14119 }
14120 }
14121
14122 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
14123 if (CRHS) {
14124 if (SDValue Split = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::OR,
14125 N->getOperand(0), CRHS))
14126 return Split;
14127 }
14128
14129 return SDValue();
14130}
14131
14132SDValue SITargetLowering::performXorCombine(SDNode *N,
14133 DAGCombinerInfo &DCI) const {
14134 if (SDValue RV = reassociateScalarOps(N, DCI.DAG))
14135 return RV;
14136
14137 SDValue LHS = N->getOperand(0);
14138 SDValue RHS = N->getOperand(1);
14139
14140 const ConstantSDNode *CRHS = isConstOrConstSplat(RHS);
14141 SelectionDAG &DAG = DCI.DAG;
14142
14143 EVT VT = N->getValueType(0);
14144 if (CRHS && VT == MVT::i64) {
14145 if (SDValue Split =
14146 splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::XOR, LHS, CRHS))
14147 return Split;
14148 }
14149
14150 // v2i32 (xor (vselect cc, x, y), K) ->
14151 // (v2i32 svelect cc, (xor x, K), (xor y, K)) This enables the xor to be
14152 // replaced with source modifiers when the select is lowered to CNDMASK.
14153 unsigned Opc = LHS.getOpcode();
14154 if (((Opc == ISD::VSELECT && VT == MVT::v2i32) ||
14155 (Opc == ISD::SELECT && VT == MVT::i64)) &&
14156 CRHS && CRHS->getAPIntValue().isSignMask()) {
14157 SDValue CC = LHS->getOperand(0);
14158 SDValue TRUE = LHS->getOperand(1);
14159 SDValue FALSE = LHS->getOperand(2);
14160 SDValue XTrue = DAG.getNode(ISD::XOR, SDLoc(N), VT, TRUE, RHS);
14161 SDValue XFalse = DAG.getNode(ISD::XOR, SDLoc(N), VT, FALSE, RHS);
14162 SDValue XSelect =
14163 DAG.getNode(ISD::VSELECT, SDLoc(N), VT, CC, XTrue, XFalse);
14164 return XSelect;
14165 }
14166
14167 // Make sure to apply the 64-bit constant splitting fold before trying to fold
14168 // fneg-like xors into 64-bit select.
14169 if (LHS.getOpcode() == ISD::SELECT && VT == MVT::i32) {
14170 // This looks like an fneg, try to fold as a source modifier.
14171 if (CRHS && CRHS->getAPIntValue().isSignMask() &&
14173 // xor (select c, a, b), 0x80000000 ->
14174 // bitcast (select c, (fneg (bitcast a)), (fneg (bitcast b)))
14175 SDLoc DL(N);
14176 SDValue CastLHS =
14177 DAG.getNode(ISD::BITCAST, DL, MVT::f32, LHS->getOperand(1));
14178 SDValue CastRHS =
14179 DAG.getNode(ISD::BITCAST, DL, MVT::f32, LHS->getOperand(2));
14180 SDValue FNegLHS = DAG.getNode(ISD::FNEG, DL, MVT::f32, CastLHS);
14181 SDValue FNegRHS = DAG.getNode(ISD::FNEG, DL, MVT::f32, CastRHS);
14182 SDValue NewSelect = DAG.getNode(ISD::SELECT, DL, MVT::f32,
14183 LHS->getOperand(0), FNegLHS, FNegRHS);
14184 return DAG.getNode(ISD::BITCAST, DL, VT, NewSelect);
14185 }
14186 }
14187
14188 return SDValue();
14189}
14190
14191SDValue SITargetLowering::performZeroExtendCombine(SDNode *N,
14192 DAGCombinerInfo &DCI) const {
14193 if (!Subtarget->has16BitInsts() ||
14194 DCI.getDAGCombineLevel() < AfterLegalizeDAG)
14195 return SDValue();
14196
14197 EVT VT = N->getValueType(0);
14198 if (VT != MVT::i32)
14199 return SDValue();
14200
14201 SDValue Src = N->getOperand(0);
14202 if (Src.getValueType() != MVT::i16)
14203 return SDValue();
14204
14205 return SDValue();
14206}
14207
14208SDValue
14209SITargetLowering::performSignExtendInRegCombine(SDNode *N,
14210 DAGCombinerInfo &DCI) const {
14211 SDValue Src = N->getOperand(0);
14212 auto *VTSign = cast<VTSDNode>(N->getOperand(1));
14213
14214 // Combine s_buffer_load_u8 or s_buffer_load_u16 with sext and replace them
14215 // with s_buffer_load_i8 and s_buffer_load_i16 respectively.
14216 if (((Src.getOpcode() == AMDGPUISD::SBUFFER_LOAD_UBYTE &&
14217 VTSign->getVT() == MVT::i8) ||
14218 (Src.getOpcode() == AMDGPUISD::SBUFFER_LOAD_USHORT &&
14219 VTSign->getVT() == MVT::i16))) {
14220 assert(Subtarget->hasScalarSubwordLoads() &&
14221 "s_buffer_load_{u8, i8} are supported "
14222 "in GFX12 (or newer) architectures.");
14223 EVT VT = Src.getValueType();
14224 unsigned Opc = (Src.getOpcode() == AMDGPUISD::SBUFFER_LOAD_UBYTE)
14227 SDLoc DL(N);
14228 SDVTList ResList = DCI.DAG.getVTList(MVT::i32);
14229 SDValue Ops[] = {
14230 Src.getOperand(0), // source register
14231 Src.getOperand(1), // offset
14232 Src.getOperand(2) // cachePolicy
14233 };
14234 auto *M = cast<MemSDNode>(Src);
14235 SDValue BufferLoad = DCI.DAG.getMemIntrinsicNode(
14236 Opc, DL, ResList, Ops, M->getMemoryVT(), M->getMemOperand());
14237 SDValue LoadVal = DCI.DAG.getNode(ISD::TRUNCATE, DL, VT, BufferLoad);
14238 return LoadVal;
14239 }
14240 if (((Src.getOpcode() == AMDGPUISD::BUFFER_LOAD_UBYTE &&
14241 VTSign->getVT() == MVT::i8) ||
14242 (Src.getOpcode() == AMDGPUISD::BUFFER_LOAD_USHORT &&
14243 VTSign->getVT() == MVT::i16)) &&
14244 Src.hasOneUse()) {
14245 auto *M = cast<MemSDNode>(Src);
14246 SDValue Ops[] = {Src.getOperand(0), // Chain
14247 Src.getOperand(1), // rsrc
14248 Src.getOperand(2), // vindex
14249 Src.getOperand(3), // voffset
14250 Src.getOperand(4), // soffset
14251 Src.getOperand(5), // offset
14252 Src.getOperand(6), Src.getOperand(7)};
14253 // replace with BUFFER_LOAD_BYTE/SHORT
14254 SDVTList ResList =
14255 DCI.DAG.getVTList(MVT::i32, Src.getOperand(0).getValueType());
14256 unsigned Opc = (Src.getOpcode() == AMDGPUISD::BUFFER_LOAD_UBYTE)
14259 SDValue BufferLoadSignExt = DCI.DAG.getMemIntrinsicNode(
14260 Opc, SDLoc(N), ResList, Ops, M->getMemoryVT(), M->getMemOperand());
14261 return DCI.DAG.getMergeValues(
14262 {BufferLoadSignExt, BufferLoadSignExt.getValue(1)}, SDLoc(N));
14263 }
14264 return SDValue();
14265}
14266
14267SDValue SITargetLowering::performClassCombine(SDNode *N,
14268 DAGCombinerInfo &DCI) const {
14269 SelectionDAG &DAG = DCI.DAG;
14270 SDValue Mask = N->getOperand(1);
14271
14272 // fp_class x, 0 -> false
14273 if (isNullConstant(Mask))
14274 return DAG.getConstant(0, SDLoc(N), MVT::i1);
14275
14276 if (N->getOperand(0).isUndef())
14277 return DAG.getUNDEF(MVT::i1);
14278
14279 return SDValue();
14280}
14281
14282SDValue SITargetLowering::performRcpCombine(SDNode *N,
14283 DAGCombinerInfo &DCI) const {
14284 EVT VT = N->getValueType(0);
14285 SDValue N0 = N->getOperand(0);
14286
14287 if (N0.isUndef()) {
14288 return DCI.DAG.getConstantFP(APFloat::getQNaN(VT.getFltSemantics()),
14289 SDLoc(N), VT);
14290 }
14291
14292 if (VT == MVT::f32 && (N0.getOpcode() == ISD::UINT_TO_FP ||
14293 N0.getOpcode() == ISD::SINT_TO_FP)) {
14294 return DCI.DAG.getNode(AMDGPUISD::RCP_IFLAG, SDLoc(N), VT, N0,
14295 N->getFlags());
14296 }
14297
14298 // TODO: Could handle f32 + amdgcn.sqrt but probably never reaches here.
14299 if ((VT == MVT::f16 && N0.getOpcode() == ISD::FSQRT) &&
14300 N->getFlags().hasAllowContract() && N0->getFlags().hasAllowContract()) {
14301 return DCI.DAG.getNode(AMDGPUISD::RSQ, SDLoc(N), VT, N0.getOperand(0),
14302 N->getFlags());
14303 }
14304
14306}
14307
14309 unsigned MaxDepth) const {
14310 unsigned Opcode = Op.getOpcode();
14311 if (Opcode == ISD::FCANONICALIZE)
14312 return true;
14313
14314 if (auto *CFP = dyn_cast<ConstantFPSDNode>(Op)) {
14315 const auto &F = CFP->getValueAPF();
14316 if (F.isNaN() && F.isSignaling())
14317 return false;
14318 if (!F.isDenormal())
14319 return true;
14320
14321 DenormalMode Mode =
14322 DAG.getMachineFunction().getDenormalMode(F.getSemantics());
14323 return Mode == DenormalMode::getIEEE();
14324 }
14325
14326 // If source is a result of another standard FP operation it is already in
14327 // canonical form.
14328 if (MaxDepth == 0)
14329 return false;
14330
14331 switch (Opcode) {
14332 // These will flush denorms if required.
14333 case ISD::FADD:
14334 case ISD::FSUB:
14335 case ISD::FMUL:
14336 case ISD::FCEIL:
14337 case ISD::FFLOOR:
14338 case ISD::FMA:
14339 case ISD::FMAD:
14340 case ISD::FSQRT:
14341 case ISD::FDIV:
14342 case ISD::FREM:
14343 case ISD::FP_ROUND:
14344 case ISD::FP_EXTEND:
14345 case ISD::FP16_TO_FP:
14346 case ISD::FP_TO_FP16:
14347 case ISD::BF16_TO_FP:
14348 case ISD::FP_TO_BF16:
14349 case ISD::FLDEXP:
14352 case AMDGPUISD::RCP:
14353 case AMDGPUISD::RSQ:
14357 case AMDGPUISD::LOG:
14358 case AMDGPUISD::EXP:
14362 case AMDGPUISD::FRACT:
14369 case AMDGPUISD::SIN_HW:
14370 case AMDGPUISD::COS_HW:
14371 return true;
14372
14373 // It can/will be lowered or combined as a bit operation.
14374 // Need to check their input recursively to handle.
14375 case ISD::FNEG:
14376 case ISD::FABS:
14377 case ISD::FCOPYSIGN:
14378 return isCanonicalized(DAG, Op.getOperand(0), MaxDepth - 1);
14379
14380 case ISD::AND:
14381 if (Op.getValueType() == MVT::i32) {
14382 // Be careful as we only know it is a bitcast floating point type. It
14383 // could be f32, v2f16, we have no way of knowing. Luckily the constant
14384 // value that we optimize for, which comes up in fp32 to bf16 conversions,
14385 // is valid to optimize for all types.
14386 if (auto *RHS = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
14387 if (RHS->getZExtValue() == 0xffff0000) {
14388 return isCanonicalized(DAG, Op.getOperand(0), MaxDepth - 1);
14389 }
14390 }
14391 }
14392 break;
14393
14394 case ISD::FSIN:
14395 case ISD::FCOS:
14396 case ISD::FSINCOS:
14397 return Op.getValueType().getScalarType() != MVT::f16;
14398
14399 case ISD::FMINNUM:
14400 case ISD::FMAXNUM:
14401 case ISD::FMINNUM_IEEE:
14402 case ISD::FMAXNUM_IEEE:
14403 case ISD::FMINIMUM:
14404 case ISD::FMAXIMUM:
14405 case ISD::FMINIMUMNUM:
14406 case ISD::FMAXIMUMNUM:
14407 case AMDGPUISD::CLAMP:
14408 case AMDGPUISD::FMED3:
14409 case AMDGPUISD::FMAX3:
14410 case AMDGPUISD::FMIN3:
14412 case AMDGPUISD::FMINIMUM3: {
14413 // FIXME: Shouldn't treat the generic operations different based these.
14414 // However, we aren't really required to flush the result from
14415 // minnum/maxnum..
14416
14417 // snans will be quieted, so we only need to worry about denormals.
14418 if (Subtarget->supportsMinMaxDenormModes() ||
14419 // FIXME: denormalsEnabledForType is broken for dynamic
14420 denormalsEnabledForType(DAG, Op.getValueType()))
14421 return true;
14422
14423 // Flushing may be required.
14424 // In pre-GFX9 targets V_MIN_F32 and others do not flush denorms. For such
14425 // targets need to check their input recursively.
14426
14427 // FIXME: Does this apply with clamp? It's implemented with max.
14428 for (unsigned I = 0, E = Op.getNumOperands(); I != E; ++I) {
14429 if (!isCanonicalized(DAG, Op.getOperand(I), MaxDepth - 1))
14430 return false;
14431 }
14432
14433 return true;
14434 }
14435 case ISD::SELECT: {
14436 return isCanonicalized(DAG, Op.getOperand(1), MaxDepth - 1) &&
14437 isCanonicalized(DAG, Op.getOperand(2), MaxDepth - 1);
14438 }
14439 case ISD::BUILD_VECTOR: {
14440 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
14441 SDValue SrcOp = Op.getOperand(i);
14442 if (!isCanonicalized(DAG, SrcOp, MaxDepth - 1))
14443 return false;
14444 }
14445
14446 return true;
14447 }
14450 return isCanonicalized(DAG, Op.getOperand(0), MaxDepth - 1);
14451 }
14453 return isCanonicalized(DAG, Op.getOperand(0), MaxDepth - 1) &&
14454 isCanonicalized(DAG, Op.getOperand(1), MaxDepth - 1);
14455 }
14456 case ISD::UNDEF:
14457 // Could be anything.
14458 return false;
14459
14460 case ISD::BITCAST:
14461 // TODO: This is incorrect as it loses track of the operand's type. We may
14462 // end up effectively bitcasting from f32 to v2f16 or vice versa, and the
14463 // same bits that are canonicalized in one type need not be in the other.
14464 return isCanonicalized(DAG, Op.getOperand(0), MaxDepth - 1);
14465 case ISD::TRUNCATE: {
14466 // Hack round the mess we make when legalizing extract_vector_elt
14467 if (Op.getValueType() == MVT::i16) {
14468 SDValue TruncSrc = Op.getOperand(0);
14469 if (TruncSrc.getValueType() == MVT::i32 &&
14470 TruncSrc.getOpcode() == ISD::BITCAST &&
14471 TruncSrc.getOperand(0).getValueType() == MVT::v2f16) {
14472 return isCanonicalized(DAG, TruncSrc.getOperand(0), MaxDepth - 1);
14473 }
14474 }
14475 return false;
14476 }
14478 unsigned IntrinsicID = Op.getConstantOperandVal(0);
14479 // TODO: Handle more intrinsics
14480 switch (IntrinsicID) {
14481 case Intrinsic::amdgcn_cvt_pkrtz:
14482 case Intrinsic::amdgcn_cubeid:
14483 case Intrinsic::amdgcn_frexp_mant:
14484 case Intrinsic::amdgcn_fdot2:
14485 case Intrinsic::amdgcn_rcp:
14486 case Intrinsic::amdgcn_rsq:
14487 case Intrinsic::amdgcn_rsq_clamp:
14488 case Intrinsic::amdgcn_rcp_legacy:
14489 case Intrinsic::amdgcn_rsq_legacy:
14490 case Intrinsic::amdgcn_trig_preop:
14491 case Intrinsic::amdgcn_tanh:
14492 case Intrinsic::amdgcn_log:
14493 case Intrinsic::amdgcn_exp2:
14494 case Intrinsic::amdgcn_sqrt:
14495 return true;
14496 default:
14497 break;
14498 }
14499
14500 break;
14501 }
14502 default:
14503 break;
14504 }
14505
14506 // FIXME: denormalsEnabledForType is broken for dynamic
14507 return denormalsEnabledForType(DAG, Op.getValueType()) &&
14508 DAG.isKnownNeverSNaN(Op);
14509}
14510
14512 unsigned MaxDepth) const {
14513 const MachineRegisterInfo &MRI = MF.getRegInfo();
14514 MachineInstr *MI = MRI.getVRegDef(Reg);
14515 unsigned Opcode = MI->getOpcode();
14516
14517 if (Opcode == AMDGPU::G_FCANONICALIZE)
14518 return true;
14519
14520 std::optional<FPValueAndVReg> FCR;
14521 // Constant splat (can be padded with undef) or scalar constant.
14523 if (FCR->Value.isSignaling())
14524 return false;
14525 if (!FCR->Value.isDenormal())
14526 return true;
14527
14528 DenormalMode Mode = MF.getDenormalMode(FCR->Value.getSemantics());
14529 return Mode == DenormalMode::getIEEE();
14530 }
14531
14532 if (MaxDepth == 0)
14533 return false;
14534
14535 switch (Opcode) {
14536 case AMDGPU::G_FADD:
14537 case AMDGPU::G_FSUB:
14538 case AMDGPU::G_FMUL:
14539 case AMDGPU::G_FCEIL:
14540 case AMDGPU::G_FFLOOR:
14541 case AMDGPU::G_FRINT:
14542 case AMDGPU::G_FNEARBYINT:
14543 case AMDGPU::G_INTRINSIC_FPTRUNC_ROUND:
14544 case AMDGPU::G_INTRINSIC_TRUNC:
14545 case AMDGPU::G_INTRINSIC_ROUNDEVEN:
14546 case AMDGPU::G_FMA:
14547 case AMDGPU::G_FMAD:
14548 case AMDGPU::G_FSQRT:
14549 case AMDGPU::G_FDIV:
14550 case AMDGPU::G_FREM:
14551 case AMDGPU::G_FPOW:
14552 case AMDGPU::G_FPEXT:
14553 case AMDGPU::G_FLOG:
14554 case AMDGPU::G_FLOG2:
14555 case AMDGPU::G_FLOG10:
14556 case AMDGPU::G_FPTRUNC:
14557 case AMDGPU::G_AMDGPU_RCP_IFLAG:
14558 case AMDGPU::G_AMDGPU_CVT_F32_UBYTE0:
14559 case AMDGPU::G_AMDGPU_CVT_F32_UBYTE1:
14560 case AMDGPU::G_AMDGPU_CVT_F32_UBYTE2:
14561 case AMDGPU::G_AMDGPU_CVT_F32_UBYTE3:
14562 return true;
14563 case AMDGPU::G_FNEG:
14564 case AMDGPU::G_FABS:
14565 case AMDGPU::G_FCOPYSIGN:
14566 return isCanonicalized(MI->getOperand(1).getReg(), MF, MaxDepth - 1);
14567 case AMDGPU::G_FMINNUM:
14568 case AMDGPU::G_FMAXNUM:
14569 case AMDGPU::G_FMINNUM_IEEE:
14570 case AMDGPU::G_FMAXNUM_IEEE:
14571 case AMDGPU::G_FMINIMUM:
14572 case AMDGPU::G_FMAXIMUM:
14573 case AMDGPU::G_FMINIMUMNUM:
14574 case AMDGPU::G_FMAXIMUMNUM: {
14575 if (Subtarget->supportsMinMaxDenormModes() ||
14576 // FIXME: denormalsEnabledForType is broken for dynamic
14577 denormalsEnabledForType(MRI.getType(Reg), MF))
14578 return true;
14579
14580 [[fallthrough]];
14581 }
14582 case AMDGPU::G_BUILD_VECTOR:
14583 for (const MachineOperand &MO : llvm::drop_begin(MI->operands()))
14584 if (!isCanonicalized(MO.getReg(), MF, MaxDepth - 1))
14585 return false;
14586 return true;
14587 case AMDGPU::G_INTRINSIC:
14588 case AMDGPU::G_INTRINSIC_CONVERGENT:
14589 switch (cast<GIntrinsic>(MI)->getIntrinsicID()) {
14590 case Intrinsic::amdgcn_fmul_legacy:
14591 case Intrinsic::amdgcn_fmad_ftz:
14592 case Intrinsic::amdgcn_sqrt:
14593 case Intrinsic::amdgcn_fmed3:
14594 case Intrinsic::amdgcn_sin:
14595 case Intrinsic::amdgcn_cos:
14596 case Intrinsic::amdgcn_log:
14597 case Intrinsic::amdgcn_exp2:
14598 case Intrinsic::amdgcn_log_clamp:
14599 case Intrinsic::amdgcn_rcp:
14600 case Intrinsic::amdgcn_rcp_legacy:
14601 case Intrinsic::amdgcn_rsq:
14602 case Intrinsic::amdgcn_rsq_clamp:
14603 case Intrinsic::amdgcn_rsq_legacy:
14604 case Intrinsic::amdgcn_div_scale:
14605 case Intrinsic::amdgcn_div_fmas:
14606 case Intrinsic::amdgcn_div_fixup:
14607 case Intrinsic::amdgcn_fract:
14608 case Intrinsic::amdgcn_cvt_pkrtz:
14609 case Intrinsic::amdgcn_cubeid:
14610 case Intrinsic::amdgcn_cubema:
14611 case Intrinsic::amdgcn_cubesc:
14612 case Intrinsic::amdgcn_cubetc:
14613 case Intrinsic::amdgcn_frexp_mant:
14614 case Intrinsic::amdgcn_fdot2:
14615 case Intrinsic::amdgcn_trig_preop:
14616 case Intrinsic::amdgcn_tanh:
14617 return true;
14618 default:
14619 break;
14620 }
14621
14622 [[fallthrough]];
14623 default:
14624 return false;
14625 }
14626
14627 llvm_unreachable("invalid operation");
14628}
14629
14630// Constant fold canonicalize.
14631SDValue SITargetLowering::getCanonicalConstantFP(SelectionDAG &DAG,
14632 const SDLoc &SL, EVT VT,
14633 const APFloat &C) const {
14634 // Flush denormals to 0 if not enabled.
14635 if (C.isDenormal()) {
14636 DenormalMode Mode =
14637 DAG.getMachineFunction().getDenormalMode(C.getSemantics());
14638 if (Mode == DenormalMode::getPreserveSign()) {
14639 return DAG.getConstantFP(
14640 APFloat::getZero(C.getSemantics(), C.isNegative()), SL, VT);
14641 }
14642
14643 if (Mode != DenormalMode::getIEEE())
14644 return SDValue();
14645 }
14646
14647 if (C.isNaN()) {
14648 APFloat CanonicalQNaN = APFloat::getQNaN(C.getSemantics());
14649 if (C.isSignaling()) {
14650 // Quiet a signaling NaN.
14651 // FIXME: Is this supposed to preserve payload bits?
14652 return DAG.getConstantFP(CanonicalQNaN, SL, VT);
14653 }
14654
14655 // Make sure it is the canonical NaN bitpattern.
14656 //
14657 // TODO: Can we use -1 as the canonical NaN value since it's an inline
14658 // immediate?
14659 if (C.bitcastToAPInt() != CanonicalQNaN.bitcastToAPInt())
14660 return DAG.getConstantFP(CanonicalQNaN, SL, VT);
14661 }
14662
14663 // Already canonical.
14664 return DAG.getConstantFP(C, SL, VT);
14665}
14666
14668 return Op.isUndef() || isa<ConstantFPSDNode>(Op);
14669}
14670
14671SDValue
14672SITargetLowering::performFCanonicalizeCombine(SDNode *N,
14673 DAGCombinerInfo &DCI) const {
14674 SelectionDAG &DAG = DCI.DAG;
14675 SDValue N0 = N->getOperand(0);
14676 EVT VT = N->getValueType(0);
14677
14678 // fcanonicalize undef -> qnan
14679 if (N0.isUndef()) {
14681 return DAG.getConstantFP(QNaN, SDLoc(N), VT);
14682 }
14683
14684 if (ConstantFPSDNode *CFP = isConstOrConstSplatFP(N0)) {
14685 EVT VT = N->getValueType(0);
14686 return getCanonicalConstantFP(DAG, SDLoc(N), VT, CFP->getValueAPF());
14687 }
14688
14689 // fcanonicalize (build_vector x, k) -> build_vector (fcanonicalize x),
14690 // (fcanonicalize k)
14691 //
14692 // fcanonicalize (build_vector x, undef) -> build_vector (fcanonicalize x), 0
14693
14694 // TODO: This could be better with wider vectors that will be split to v2f16,
14695 // and to consider uses since there aren't that many packed operations.
14696 if (N0.getOpcode() == ISD::BUILD_VECTOR && VT == MVT::v2f16 &&
14697 isTypeLegal(MVT::v2f16)) {
14698 SDLoc SL(N);
14699 SDValue NewElts[2];
14700 SDValue Lo = N0.getOperand(0);
14701 SDValue Hi = N0.getOperand(1);
14702 EVT EltVT = Lo.getValueType();
14703
14705 for (unsigned I = 0; I != 2; ++I) {
14706 SDValue Op = N0.getOperand(I);
14707 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op)) {
14708 NewElts[I] =
14709 getCanonicalConstantFP(DAG, SL, EltVT, CFP->getValueAPF());
14710 } else if (Op.isUndef()) {
14711 // Handled below based on what the other operand is.
14712 NewElts[I] = Op;
14713 } else {
14714 NewElts[I] = DAG.getNode(ISD::FCANONICALIZE, SL, EltVT, Op);
14715 }
14716 }
14717
14718 // If one half is undef, and one is constant, prefer a splat vector rather
14719 // than the normal qNaN. If it's a register, prefer 0.0 since that's
14720 // cheaper to use and may be free with a packed operation.
14721 if (NewElts[0].isUndef()) {
14722 if (isa<ConstantFPSDNode>(NewElts[1]))
14723 NewElts[0] = isa<ConstantFPSDNode>(NewElts[1])
14724 ? NewElts[1]
14725 : DAG.getConstantFP(0.0f, SL, EltVT);
14726 }
14727
14728 if (NewElts[1].isUndef()) {
14729 NewElts[1] = isa<ConstantFPSDNode>(NewElts[0])
14730 ? NewElts[0]
14731 : DAG.getConstantFP(0.0f, SL, EltVT);
14732 }
14733
14734 return DAG.getBuildVector(VT, SL, NewElts);
14735 }
14736 }
14737
14738 return SDValue();
14739}
14740
14741static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) {
14742 switch (Opc) {
14743 case ISD::FMAXNUM:
14744 case ISD::FMAXNUM_IEEE:
14745 case ISD::FMAXIMUMNUM:
14746 return AMDGPUISD::FMAX3;
14747 case ISD::FMAXIMUM:
14748 return AMDGPUISD::FMAXIMUM3;
14749 case ISD::SMAX:
14750 return AMDGPUISD::SMAX3;
14751 case ISD::UMAX:
14752 return AMDGPUISD::UMAX3;
14753 case ISD::FMINNUM:
14754 case ISD::FMINNUM_IEEE:
14755 case ISD::FMINIMUMNUM:
14756 return AMDGPUISD::FMIN3;
14757 case ISD::FMINIMUM:
14758 return AMDGPUISD::FMINIMUM3;
14759 case ISD::SMIN:
14760 return AMDGPUISD::SMIN3;
14761 case ISD::UMIN:
14762 return AMDGPUISD::UMIN3;
14763 default:
14764 llvm_unreachable("Not a min/max opcode");
14765 }
14766}
14767
14768SDValue SITargetLowering::performIntMed3ImmCombine(SelectionDAG &DAG,
14769 const SDLoc &SL, SDValue Src,
14770 SDValue MinVal,
14771 SDValue MaxVal,
14772 bool Signed) const {
14773
14774 // med3 comes from
14775 // min(max(x, K0), K1), K0 < K1
14776 // max(min(x, K0), K1), K1 < K0
14777 //
14778 // "MinVal" and "MaxVal" respectively refer to the rhs of the
14779 // min/max op.
14780 ConstantSDNode *MinK = dyn_cast<ConstantSDNode>(MinVal);
14781 ConstantSDNode *MaxK = dyn_cast<ConstantSDNode>(MaxVal);
14782
14783 if (!MinK || !MaxK)
14784 return SDValue();
14785
14786 if (Signed) {
14787 if (MaxK->getAPIntValue().sge(MinK->getAPIntValue()))
14788 return SDValue();
14789 } else {
14790 if (MaxK->getAPIntValue().uge(MinK->getAPIntValue()))
14791 return SDValue();
14792 }
14793
14794 EVT VT = MinK->getValueType(0);
14795 unsigned Med3Opc = Signed ? AMDGPUISD::SMED3 : AMDGPUISD::UMED3;
14796 if (VT == MVT::i32 || (VT == MVT::i16 && Subtarget->hasMed3_16()))
14797 return DAG.getNode(Med3Opc, SL, VT, Src, MaxVal, MinVal);
14798
14799 // Note: we could also extend to i32 and use i32 med3 if i16 med3 is
14800 // not available, but this is unlikely to be profitable as constants
14801 // will often need to be materialized & extended, especially on
14802 // pre-GFX10 where VOP3 instructions couldn't take literal operands.
14803 return SDValue();
14804}
14805
14808 return C;
14809
14811 if (ConstantFPSDNode *C = BV->getConstantFPSplatNode())
14812 return C;
14813 }
14814
14815 return nullptr;
14816}
14817
14818SDValue SITargetLowering::performFPMed3ImmCombine(SelectionDAG &DAG,
14819 const SDLoc &SL, SDValue Op0,
14820 SDValue Op1) const {
14821 ConstantFPSDNode *K1 = getSplatConstantFP(Op1);
14822 if (!K1)
14823 return SDValue();
14824
14825 ConstantFPSDNode *K0 = getSplatConstantFP(Op0.getOperand(1));
14826 if (!K0)
14827 return SDValue();
14828
14829 // Ordered >= (although NaN inputs should have folded away by now).
14830 if (K0->getValueAPF() > K1->getValueAPF())
14831 return SDValue();
14832
14833 // med3 with a nan input acts like
14834 // v_min_f32(v_min_f32(S0.f32, S1.f32), S2.f32)
14835 //
14836 // So the result depends on whether the IEEE mode bit is enabled or not with a
14837 // signaling nan input.
14838 // ieee=1
14839 // s0 snan: yields s2
14840 // s1 snan: yields s2
14841 // s2 snan: qnan
14842
14843 // s0 qnan: min(s1, s2)
14844 // s1 qnan: min(s0, s2)
14845 // s2 qnan: min(s0, s1)
14846
14847 // ieee=0
14848 // s0 snan: min(s1, s2)
14849 // s1 snan: min(s0, s2)
14850 // s2 snan: qnan
14851
14852 // s0 qnan: min(s1, s2)
14853 // s1 qnan: min(s0, s2)
14854 // s2 qnan: min(s0, s1)
14855 const MachineFunction &MF = DAG.getMachineFunction();
14856 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
14857
14858 // TODO: Check IEEE bit enabled. We can form fmed3 with IEEE=0 regardless of
14859 // whether the input is a signaling nan if op0 is fmaximum or fmaximumnum. We
14860 // can only form if op0 is fmaxnum_ieee if IEEE=1.
14861 EVT VT = Op0.getValueType();
14862 if (Info->getMode().DX10Clamp) {
14863 // If dx10_clamp is enabled, NaNs clamp to 0.0. This is the same as the
14864 // hardware fmed3 behavior converting to a min.
14865 // FIXME: Should this be allowing -0.0?
14866 if (K1->isExactlyValue(1.0) && K0->isExactlyValue(0.0))
14867 return DAG.getNode(AMDGPUISD::CLAMP, SL, VT, Op0.getOperand(0));
14868 }
14869
14870 // med3 for f16 is only available on gfx9+, and not available for v2f16.
14871 if (VT == MVT::f32 || (VT == MVT::f16 && Subtarget->hasMed3_16())) {
14872 // This isn't safe with signaling NaNs because in IEEE mode, min/max on a
14873 // signaling NaN gives a quiet NaN. The quiet NaN input to the min would
14874 // then give the other result, which is different from med3 with a NaN
14875 // input.
14876 SDValue Var = Op0.getOperand(0);
14877 if (!DAG.isKnownNeverSNaN(Var))
14878 return SDValue();
14879
14880 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
14881
14882 if ((!K0->hasOneUse() || TII->isInlineConstant(K0->getValueAPF())) &&
14883 (!K1->hasOneUse() || TII->isInlineConstant(K1->getValueAPF()))) {
14884 return DAG.getNode(AMDGPUISD::FMED3, SL, K0->getValueType(0), Var,
14885 SDValue(K0, 0), SDValue(K1, 0));
14886 }
14887 }
14888
14889 return SDValue();
14890}
14891
14892/// \return true if the subtarget supports minimum3 and maximum3 with the given
14893/// base min/max opcode \p Opc for type \p VT.
14894static bool supportsMin3Max3(const GCNSubtarget &Subtarget, unsigned Opc,
14895 EVT VT) {
14896 switch (Opc) {
14897 case ISD::FMINNUM:
14898 case ISD::FMAXNUM:
14899 case ISD::FMINNUM_IEEE:
14900 case ISD::FMAXNUM_IEEE:
14901 case ISD::FMINIMUMNUM:
14902 case ISD::FMAXIMUMNUM:
14905 return (VT == MVT::f32) || (VT == MVT::f16 && Subtarget.hasMin3Max3_16()) ||
14906 (VT == MVT::v2f16 && Subtarget.hasMin3Max3PKF16());
14907 case ISD::FMINIMUM:
14908 case ISD::FMAXIMUM:
14909 return (VT == MVT::f32 && Subtarget.hasMinimum3Maximum3F32()) ||
14910 (VT == MVT::f16 && Subtarget.hasMinimum3Maximum3F16()) ||
14911 (VT == MVT::v2f16 && Subtarget.hasMinimum3Maximum3PKF16());
14912 case ISD::SMAX:
14913 case ISD::SMIN:
14914 case ISD::UMAX:
14915 case ISD::UMIN:
14916 return (VT == MVT::i32) || (VT == MVT::i16 && Subtarget.hasMin3Max3_16());
14917 default:
14918 return false;
14919 }
14920
14921 llvm_unreachable("not a min/max opcode");
14922}
14923
14924SDValue SITargetLowering::performMinMaxCombine(SDNode *N,
14925 DAGCombinerInfo &DCI) const {
14926 SelectionDAG &DAG = DCI.DAG;
14927
14928 EVT VT = N->getValueType(0);
14929 unsigned Opc = N->getOpcode();
14930 SDValue Op0 = N->getOperand(0);
14931 SDValue Op1 = N->getOperand(1);
14932
14933 // Only do this if the inner op has one use since this will just increases
14934 // register pressure for no benefit.
14935
14936 if (supportsMin3Max3(*Subtarget, Opc, VT)) {
14937 // max(max(a, b), c) -> max3(a, b, c)
14938 // min(min(a, b), c) -> min3(a, b, c)
14939 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) {
14940 SDLoc DL(N);
14941 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc), DL, N->getValueType(0),
14942 Op0.getOperand(0), Op0.getOperand(1), Op1);
14943 }
14944
14945 // Try commuted.
14946 // max(a, max(b, c)) -> max3(a, b, c)
14947 // min(a, min(b, c)) -> min3(a, b, c)
14948 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) {
14949 SDLoc DL(N);
14950 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc), DL, N->getValueType(0),
14951 Op0, Op1.getOperand(0), Op1.getOperand(1));
14952 }
14953 }
14954
14955 // min(max(x, K0), K1), K0 < K1 -> med3(x, K0, K1)
14956 // max(min(x, K0), K1), K1 < K0 -> med3(x, K1, K0)
14957 if (Opc == ISD::SMIN && Op0.getOpcode() == ISD::SMAX && Op0.hasOneUse()) {
14958 if (SDValue Med3 = performIntMed3ImmCombine(
14959 DAG, SDLoc(N), Op0->getOperand(0), Op1, Op0->getOperand(1), true))
14960 return Med3;
14961 }
14962 if (Opc == ISD::SMAX && Op0.getOpcode() == ISD::SMIN && Op0.hasOneUse()) {
14963 if (SDValue Med3 = performIntMed3ImmCombine(
14964 DAG, SDLoc(N), Op0->getOperand(0), Op0->getOperand(1), Op1, true))
14965 return Med3;
14966 }
14967
14968 if (Opc == ISD::UMIN && Op0.getOpcode() == ISD::UMAX && Op0.hasOneUse()) {
14969 if (SDValue Med3 = performIntMed3ImmCombine(
14970 DAG, SDLoc(N), Op0->getOperand(0), Op1, Op0->getOperand(1), false))
14971 return Med3;
14972 }
14973 if (Opc == ISD::UMAX && Op0.getOpcode() == ISD::UMIN && Op0.hasOneUse()) {
14974 if (SDValue Med3 = performIntMed3ImmCombine(
14975 DAG, SDLoc(N), Op0->getOperand(0), Op0->getOperand(1), Op1, false))
14976 return Med3;
14977 }
14978
14979 // if !is_snan(x):
14980 // fminnum(fmaxnum(x, K0), K1), K0 < K1 -> fmed3(x, K0, K1)
14981 // fminnum_ieee(fmaxnum_ieee(x, K0), K1), K0 < K1 -> fmed3(x, K0, K1)
14982 // fminnumnum(fmaxnumnum(x, K0), K1), K0 < K1 -> fmed3(x, K0, K1)
14983 // fmin_legacy(fmax_legacy(x, K0), K1), K0 < K1 -> fmed3(x, K0, K1)
14984 if (((Opc == ISD::FMINNUM && Op0.getOpcode() == ISD::FMAXNUM) ||
14985 (Opc == ISD::FMINNUM_IEEE && Op0.getOpcode() == ISD::FMAXNUM_IEEE) ||
14986 (Opc == ISD::FMINIMUMNUM && Op0.getOpcode() == ISD::FMAXIMUMNUM) ||
14988 Op0.getOpcode() == AMDGPUISD::FMAX_LEGACY)) &&
14989 (VT == MVT::f32 || VT == MVT::f64 ||
14990 (VT == MVT::f16 && Subtarget->has16BitInsts()) ||
14991 (VT == MVT::bf16 && Subtarget->hasBF16PackedInsts()) ||
14992 (VT == MVT::v2bf16 && Subtarget->hasBF16PackedInsts()) ||
14993 (VT == MVT::v2f16 && Subtarget->hasVOP3PInsts())) &&
14994 Op0.hasOneUse()) {
14995 if (SDValue Res = performFPMed3ImmCombine(DAG, SDLoc(N), Op0, Op1))
14996 return Res;
14997 }
14998
14999 // Prefer fminnum_ieee over fminimum. For gfx950, minimum/maximum are legal
15000 // for some types, but at a higher cost since it's implemented with a 3
15001 // operand form.
15002 const SDNodeFlags Flags = N->getFlags();
15003 if ((Opc == ISD::FMINIMUM || Opc == ISD::FMAXIMUM) &&
15004 !Subtarget->hasIEEEMinimumMaximumInsts() && Flags.hasNoNaNs()) {
15005 unsigned NewOpc =
15006 Opc == ISD::FMINIMUM ? ISD::FMINNUM_IEEE : ISD::FMAXNUM_IEEE;
15007 return DAG.getNode(NewOpc, SDLoc(N), VT, Op0, Op1, Flags);
15008 }
15009
15010 return SDValue();
15011}
15012
15016 // FIXME: Should this be allowing -0.0?
15017 return (CA->isExactlyValue(0.0) && CB->isExactlyValue(1.0)) ||
15018 (CA->isExactlyValue(1.0) && CB->isExactlyValue(0.0));
15019 }
15020 }
15021
15022 return false;
15023}
15024
15025// FIXME: Should only worry about snans for version with chain.
15026SDValue SITargetLowering::performFMed3Combine(SDNode *N,
15027 DAGCombinerInfo &DCI) const {
15028 EVT VT = N->getValueType(0);
15029 // v_med3_f32 and v_max_f32 behave identically wrt denorms, exceptions and
15030 // NaNs. With a NaN input, the order of the operands may change the result.
15031
15032 SelectionDAG &DAG = DCI.DAG;
15033 SDLoc SL(N);
15034
15035 SDValue Src0 = N->getOperand(0);
15036 SDValue Src1 = N->getOperand(1);
15037 SDValue Src2 = N->getOperand(2);
15038
15039 if (isClampZeroToOne(Src0, Src1)) {
15040 // const_a, const_b, x -> clamp is safe in all cases including signaling
15041 // nans.
15042 // FIXME: Should this be allowing -0.0?
15043 return DAG.getNode(AMDGPUISD::CLAMP, SL, VT, Src2);
15044 }
15045
15046 const MachineFunction &MF = DAG.getMachineFunction();
15047 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
15048
15049 // FIXME: dx10_clamp behavior assumed in instcombine. Should we really bother
15050 // handling no dx10-clamp?
15051 if (Info->getMode().DX10Clamp) {
15052 // If NaNs is clamped to 0, we are free to reorder the inputs.
15053
15054 if (isa<ConstantFPSDNode>(Src0) && !isa<ConstantFPSDNode>(Src1))
15055 std::swap(Src0, Src1);
15056
15057 if (isa<ConstantFPSDNode>(Src1) && !isa<ConstantFPSDNode>(Src2))
15058 std::swap(Src1, Src2);
15059
15060 if (isa<ConstantFPSDNode>(Src0) && !isa<ConstantFPSDNode>(Src1))
15061 std::swap(Src0, Src1);
15062
15063 if (isClampZeroToOne(Src1, Src2))
15064 return DAG.getNode(AMDGPUISD::CLAMP, SL, VT, Src0);
15065 }
15066
15067 return SDValue();
15068}
15069
15070SDValue SITargetLowering::performCvtPkRTZCombine(SDNode *N,
15071 DAGCombinerInfo &DCI) const {
15072 SDValue Src0 = N->getOperand(0);
15073 SDValue Src1 = N->getOperand(1);
15074 if (Src0.isUndef() && Src1.isUndef())
15075 return DCI.DAG.getUNDEF(N->getValueType(0));
15076 return SDValue();
15077}
15078
15079// Check if EXTRACT_VECTOR_ELT/INSERT_VECTOR_ELT (<n x e>, var-idx) should be
15080// expanded into a set of cmp/select instructions.
15082 unsigned NumElem,
15083 bool IsDivergentIdx,
15084 const GCNSubtarget *Subtarget) {
15086 return false;
15087
15088 unsigned VecSize = EltSize * NumElem;
15089
15090 // Sub-dword vectors of size 2 dword or less have better implementation.
15091 if (VecSize <= 64 && EltSize < 32)
15092 return false;
15093
15094 // Always expand the rest of sub-dword instructions, otherwise it will be
15095 // lowered via memory.
15096 if (EltSize < 32)
15097 return true;
15098
15099 // Always do this if var-idx is divergent, otherwise it will become a loop.
15100 if (IsDivergentIdx)
15101 return true;
15102
15103 // Large vectors would yield too many compares and v_cndmask_b32 instructions.
15104 unsigned NumInsts = NumElem /* Number of compares */ +
15105 ((EltSize + 31) / 32) * NumElem /* Number of cndmasks */;
15106
15107 // On some architectures (GFX9) movrel is not available and it's better
15108 // to expand.
15109 if (Subtarget->useVGPRIndexMode())
15110 return NumInsts <= 16;
15111
15112 // If movrel is available, use it instead of expanding for vector of 8
15113 // elements.
15114 if (Subtarget->hasMovrel())
15115 return NumInsts <= 15;
15116
15117 return true;
15118}
15119
15121 SDValue Idx = N->getOperand(N->getNumOperands() - 1);
15122 if (isa<ConstantSDNode>(Idx))
15123 return false;
15124
15125 SDValue Vec = N->getOperand(0);
15126 EVT VecVT = Vec.getValueType();
15127 EVT EltVT = VecVT.getVectorElementType();
15128 unsigned EltSize = EltVT.getSizeInBits();
15129 unsigned NumElem = VecVT.getVectorNumElements();
15130
15132 EltSize, NumElem, Idx->isDivergent(), getSubtarget());
15133}
15134
15135SDValue
15136SITargetLowering::performExtractVectorEltCombine(SDNode *N,
15137 DAGCombinerInfo &DCI) const {
15138 SDValue Vec = N->getOperand(0);
15139 SelectionDAG &DAG = DCI.DAG;
15140
15141 EVT VecVT = Vec.getValueType();
15142 EVT VecEltVT = VecVT.getVectorElementType();
15143 EVT ResVT = N->getValueType(0);
15144
15145 unsigned VecSize = VecVT.getSizeInBits();
15146 unsigned VecEltSize = VecEltVT.getSizeInBits();
15147
15148 if ((Vec.getOpcode() == ISD::FNEG || Vec.getOpcode() == ISD::FABS) &&
15150 SDLoc SL(N);
15151 SDValue Idx = N->getOperand(1);
15152 SDValue Elt =
15153 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, ResVT, Vec.getOperand(0), Idx);
15154 return DAG.getNode(Vec.getOpcode(), SL, ResVT, Elt);
15155 }
15156
15157 // (extract_vector_element (and {y0, y1}, (build_vector 0x1f, 0x1f)), index)
15158 // -> (and (extract_vector_element {y0, y1}, index), 0x1f)
15159 // There are optimisations to transform 64-bit shifts into 32-bit shifts
15160 // depending on the shift operand. See e.g. performSraCombine().
15161 // This combine ensures that the optimisation is compatible with v2i32
15162 // legalised AND.
15163 if (VecVT == MVT::v2i32 && Vec->getOpcode() == ISD::AND &&
15164 Vec->getOperand(1)->getOpcode() == ISD::BUILD_VECTOR) {
15165
15167 if (!C || C->getZExtValue() != 0x1f)
15168 return SDValue();
15169
15170 SDLoc SL(N);
15171 SDValue AndMask = DAG.getConstant(0x1f, SL, MVT::i32);
15172 SDValue EVE = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32,
15173 Vec->getOperand(0), N->getOperand(1));
15174 SDValue A = DAG.getNode(ISD::AND, SL, MVT::i32, EVE, AndMask);
15175 DAG.ReplaceAllUsesWith(N, A.getNode());
15176 }
15177
15178 // ScalarRes = EXTRACT_VECTOR_ELT ((vector-BINOP Vec1, Vec2), Idx)
15179 // =>
15180 // Vec1Elt = EXTRACT_VECTOR_ELT(Vec1, Idx)
15181 // Vec2Elt = EXTRACT_VECTOR_ELT(Vec2, Idx)
15182 // ScalarRes = scalar-BINOP Vec1Elt, Vec2Elt
15183 if (Vec.hasOneUse() && DCI.isBeforeLegalize() && VecEltVT == ResVT) {
15184 SDLoc SL(N);
15185 SDValue Idx = N->getOperand(1);
15186 unsigned Opc = Vec.getOpcode();
15187
15188 switch (Opc) {
15189 default:
15190 break;
15191 // TODO: Support other binary operations.
15192 case ISD::FADD:
15193 case ISD::FSUB:
15194 case ISD::FMUL:
15195 case ISD::ADD:
15196 case ISD::UMIN:
15197 case ISD::UMAX:
15198 case ISD::SMIN:
15199 case ISD::SMAX:
15200 case ISD::FMAXNUM:
15201 case ISD::FMINNUM:
15202 case ISD::FMAXNUM_IEEE:
15203 case ISD::FMINNUM_IEEE:
15204 case ISD::FMAXIMUM:
15205 case ISD::FMINIMUM: {
15206 SDValue Elt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, ResVT,
15207 Vec.getOperand(0), Idx);
15208 SDValue Elt1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, ResVT,
15209 Vec.getOperand(1), Idx);
15210
15211 DCI.AddToWorklist(Elt0.getNode());
15212 DCI.AddToWorklist(Elt1.getNode());
15213 return DAG.getNode(Opc, SL, ResVT, Elt0, Elt1, Vec->getFlags());
15214 }
15215 }
15216 }
15217
15218 // EXTRACT_VECTOR_ELT (<n x e>, var-idx) => n x select (e, const-idx)
15220 SDLoc SL(N);
15221 SDValue Idx = N->getOperand(1);
15222 SDValue V;
15223 for (unsigned I = 0, E = VecVT.getVectorNumElements(); I < E; ++I) {
15224 SDValue IC = DAG.getVectorIdxConstant(I, SL);
15225 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, ResVT, Vec, IC);
15226 if (I == 0)
15227 V = Elt;
15228 else
15229 V = DAG.getSelectCC(SL, Idx, IC, Elt, V, ISD::SETEQ);
15230 }
15231 return V;
15232 }
15233
15234 if (!DCI.isBeforeLegalize())
15235 return SDValue();
15236
15237 // Try to turn sub-dword accesses of vectors into accesses of the same 32-bit
15238 // elements. This exposes more load reduction opportunities by replacing
15239 // multiple small extract_vector_elements with a single 32-bit extract.
15240 auto *Idx = dyn_cast<ConstantSDNode>(N->getOperand(1));
15241 if (isa<MemSDNode>(Vec) && VecEltSize <= 16 && VecEltVT.isByteSized() &&
15242 VecSize > 32 && VecSize % 32 == 0 && Idx) {
15243 EVT NewVT = getEquivalentMemType(*DAG.getContext(), VecVT);
15244
15245 unsigned BitIndex = Idx->getZExtValue() * VecEltSize;
15246 unsigned EltIdx = BitIndex / 32;
15247 unsigned LeftoverBitIdx = BitIndex % 32;
15248 SDLoc SL(N);
15249
15250 SDValue Cast = DAG.getNode(ISD::BITCAST, SL, NewVT, Vec);
15251 DCI.AddToWorklist(Cast.getNode());
15252
15253 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Cast,
15254 DAG.getConstant(EltIdx, SL, MVT::i32));
15255 DCI.AddToWorklist(Elt.getNode());
15256 SDValue Srl = DAG.getNode(ISD::SRL, SL, MVT::i32, Elt,
15257 DAG.getConstant(LeftoverBitIdx, SL, MVT::i32));
15258 DCI.AddToWorklist(Srl.getNode());
15259
15260 EVT VecEltAsIntVT = VecEltVT.changeTypeToInteger();
15261 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SL, VecEltAsIntVT, Srl);
15262 DCI.AddToWorklist(Trunc.getNode());
15263
15264 if (VecEltVT == ResVT) {
15265 return DAG.getNode(ISD::BITCAST, SL, VecEltVT, Trunc);
15266 }
15267
15268 assert(ResVT.isScalarInteger());
15269 return DAG.getAnyExtOrTrunc(Trunc, SL, ResVT);
15270 }
15271
15272 return SDValue();
15273}
15274
15275SDValue
15276SITargetLowering::performInsertVectorEltCombine(SDNode *N,
15277 DAGCombinerInfo &DCI) const {
15278 SDValue Vec = N->getOperand(0);
15279 SDValue Idx = N->getOperand(2);
15280 EVT VecVT = Vec.getValueType();
15281 EVT EltVT = VecVT.getVectorElementType();
15282
15283 // INSERT_VECTOR_ELT (<n x e>, var-idx)
15284 // => BUILD_VECTOR n x select (e, const-idx)
15286 return SDValue();
15287
15288 SelectionDAG &DAG = DCI.DAG;
15289 SDLoc SL(N);
15290 SDValue Ins = N->getOperand(1);
15291 EVT IdxVT = Idx.getValueType();
15292
15294 for (unsigned I = 0, E = VecVT.getVectorNumElements(); I < E; ++I) {
15295 SDValue IC = DAG.getConstant(I, SL, IdxVT);
15296 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, Vec, IC);
15297 SDValue V = DAG.getSelectCC(SL, Idx, IC, Ins, Elt, ISD::SETEQ);
15298 Ops.push_back(V);
15299 }
15300
15301 return DAG.getBuildVector(VecVT, SL, Ops);
15302}
15303
15304/// Return the source of an fp_extend from f16 to f32, or a converted FP
15305/// constant.
15307 if (Src.getOpcode() == ISD::FP_EXTEND &&
15308 Src.getOperand(0).getValueType() == MVT::f16) {
15309 return Src.getOperand(0);
15310 }
15311
15312 if (auto *CFP = dyn_cast<ConstantFPSDNode>(Src)) {
15313 APFloat Val = CFP->getValueAPF();
15314 bool LosesInfo = true;
15316 if (!LosesInfo)
15317 return DAG.getConstantFP(Val, SDLoc(Src), MVT::f16);
15318 }
15319
15320 return SDValue();
15321}
15322
15323SDValue SITargetLowering::performFPRoundCombine(SDNode *N,
15324 DAGCombinerInfo &DCI) const {
15325 assert(Subtarget->has16BitInsts() && !Subtarget->hasMed3_16() &&
15326 "combine only useful on gfx8");
15327
15328 SDValue TruncSrc = N->getOperand(0);
15329 EVT VT = N->getValueType(0);
15330 if (VT != MVT::f16)
15331 return SDValue();
15332
15333 if (TruncSrc.getOpcode() != AMDGPUISD::FMED3 ||
15334 TruncSrc.getValueType() != MVT::f32 || !TruncSrc.hasOneUse())
15335 return SDValue();
15336
15337 SelectionDAG &DAG = DCI.DAG;
15338 SDLoc SL(N);
15339
15340 // Optimize f16 fmed3 pattern performed on f32. On gfx8 there is no f16 fmed3,
15341 // and expanding it with min/max saves 1 instruction vs. casting to f32 and
15342 // casting back.
15343
15344 // fptrunc (f32 (fmed3 (fpext f16:a, fpext f16:b, fpext f16:c))) =>
15345 // fmin(fmax(a, b), fmax(fmin(a, b), c))
15346 SDValue A = strictFPExtFromF16(DAG, TruncSrc.getOperand(0));
15347 if (!A)
15348 return SDValue();
15349
15350 SDValue B = strictFPExtFromF16(DAG, TruncSrc.getOperand(1));
15351 if (!B)
15352 return SDValue();
15353
15354 SDValue C = strictFPExtFromF16(DAG, TruncSrc.getOperand(2));
15355 if (!C)
15356 return SDValue();
15357
15358 // This changes signaling nan behavior. If an input is a signaling nan, it
15359 // would have been quieted by the fpext originally. We don't care because
15360 // these are unconstrained ops. If we needed to insert quieting canonicalizes
15361 // we would be worse off than just doing the promotion.
15362 SDValue A1 = DAG.getNode(ISD::FMINNUM_IEEE, SL, VT, A, B);
15363 SDValue B1 = DAG.getNode(ISD::FMAXNUM_IEEE, SL, VT, A, B);
15364 SDValue C1 = DAG.getNode(ISD::FMAXNUM_IEEE, SL, VT, A1, C);
15365 return DAG.getNode(ISD::FMINNUM_IEEE, SL, VT, B1, C1);
15366}
15367
15368unsigned SITargetLowering::getFusedOpcode(const SelectionDAG &DAG,
15369 const SDNode *N0,
15370 const SDNode *N1) const {
15371 EVT VT = N0->getValueType(0);
15372
15373 // Only do this if we are not trying to support denormals. v_mad_f32 does not
15374 // support denormals ever.
15375 if (((VT == MVT::f32 &&
15377 (VT == MVT::f16 && Subtarget->hasMadF16() &&
15380 return ISD::FMAD;
15381
15382 const TargetOptions &Options = DAG.getTarget().Options;
15383 if ((Options.AllowFPOpFusion == FPOpFusion::Fast ||
15384 (N0->getFlags().hasAllowContract() &&
15385 N1->getFlags().hasAllowContract())) &&
15387 return ISD::FMA;
15388 }
15389
15390 return 0;
15391}
15392
15393// For a reassociatable opcode perform:
15394// op x, (op y, z) -> op (op x, z), y, if x and z are uniform
15395SDValue SITargetLowering::reassociateScalarOps(SDNode *N,
15396 SelectionDAG &DAG) const {
15397 EVT VT = N->getValueType(0);
15398 if (VT != MVT::i32 && VT != MVT::i64)
15399 return SDValue();
15400
15401 if (DAG.isBaseWithConstantOffset(SDValue(N, 0)))
15402 return SDValue();
15403
15404 unsigned Opc = N->getOpcode();
15405 SDValue Op0 = N->getOperand(0);
15406 SDValue Op1 = N->getOperand(1);
15407
15408 if (!(Op0->isDivergent() ^ Op1->isDivergent()))
15409 return SDValue();
15410
15411 if (Op0->isDivergent())
15412 std::swap(Op0, Op1);
15413
15414 if (Op1.getOpcode() != Opc || !Op1.hasOneUse())
15415 return SDValue();
15416
15417 SDValue Op2 = Op1.getOperand(1);
15418 Op1 = Op1.getOperand(0);
15419 if (!(Op1->isDivergent() ^ Op2->isDivergent()))
15420 return SDValue();
15421
15422 if (Op1->isDivergent())
15423 std::swap(Op1, Op2);
15424
15425 SDLoc SL(N);
15426 SDValue Add1 = DAG.getNode(Opc, SL, VT, Op0, Op1);
15427 return DAG.getNode(Opc, SL, VT, Add1, Op2);
15428}
15429
15430static SDValue getMad64_32(SelectionDAG &DAG, const SDLoc &SL, EVT VT,
15431 SDValue N0, SDValue N1, SDValue N2, bool Signed) {
15433 SDVTList VTs = DAG.getVTList(MVT::i64, MVT::i1);
15434 SDValue Mad = DAG.getNode(MadOpc, SL, VTs, N0, N1, N2);
15435 return DAG.getNode(ISD::TRUNCATE, SL, VT, Mad);
15436}
15437
15438// Fold
15439// y = lshr i64 x, 32
15440// res = add (mul i64 y, Const), x where "Const" is a 64-bit constant
15441// with Const.hi == -1
15442// To
15443// res = mad_u64_u32 y.lo ,Const.lo, x.lo
15445 SDValue MulLHS, SDValue MulRHS,
15446 SDValue AddRHS) {
15447 if (MulRHS.getOpcode() == ISD::SRL)
15448 std::swap(MulLHS, MulRHS);
15449
15450 if (MulLHS.getValueType() != MVT::i64 || MulLHS.getOpcode() != ISD::SRL)
15451 return SDValue();
15452
15453 ConstantSDNode *ShiftVal = dyn_cast<ConstantSDNode>(MulLHS.getOperand(1));
15454 if (!ShiftVal || ShiftVal->getAsZExtVal() != 32 ||
15455 MulLHS.getOperand(0) != AddRHS)
15456 return SDValue();
15457
15459 if (!Const || Hi_32(Const->getZExtValue()) != uint32_t(-1))
15460 return SDValue();
15461
15462 SDValue ConstMul =
15463 DAG.getConstant(Lo_32(Const->getZExtValue()), SL, MVT::i32);
15464 return getMad64_32(DAG, SL, MVT::i64,
15465 DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, MulLHS), ConstMul,
15466 DAG.getZeroExtendInReg(AddRHS, SL, MVT::i32), false);
15467}
15468
15469// Fold (add (mul x, y), z) --> (mad_[iu]64_[iu]32 x, y, z) plus high
15470// multiplies, if any.
15471//
15472// Full 64-bit multiplies that feed into an addition are lowered here instead
15473// of using the generic expansion. The generic expansion ends up with
15474// a tree of ADD nodes that prevents us from using the "add" part of the
15475// MAD instruction. The expansion produced here results in a chain of ADDs
15476// instead of a tree.
15477SDValue SITargetLowering::tryFoldToMad64_32(SDNode *N,
15478 DAGCombinerInfo &DCI) const {
15479 assert(N->isAnyAdd());
15480
15481 SelectionDAG &DAG = DCI.DAG;
15482 EVT VT = N->getValueType(0);
15483 SDLoc SL(N);
15484 SDValue LHS = N->getOperand(0);
15485 SDValue RHS = N->getOperand(1);
15486
15487 if (VT.isVector())
15488 return SDValue();
15489
15490 // S_MUL_HI_[IU]32 was added in gfx9, which allows us to keep the overall
15491 // result in scalar registers for uniform values.
15492 if (!N->isDivergent() && Subtarget->hasSMulHi())
15493 return SDValue();
15494
15495 unsigned NumBits = VT.getScalarSizeInBits();
15496 if (NumBits <= 32 || NumBits > 64)
15497 return SDValue();
15498
15499 if (LHS.getOpcode() != ISD::MUL) {
15500 assert(RHS.getOpcode() == ISD::MUL);
15501 std::swap(LHS, RHS);
15502 }
15503
15504 // Avoid the fold if it would unduly increase the number of multiplies due to
15505 // multiple uses, except on hardware with full-rate multiply-add (which is
15506 // part of full-rate 64-bit ops).
15507 if (!Subtarget->hasFullRate64Ops()) {
15508 unsigned NumUsers = 0;
15509 for (SDNode *User : LHS->users()) {
15510 // There is a use that does not feed into addition, so the multiply can't
15511 // be removed. We prefer MUL + ADD + ADDC over MAD + MUL.
15512 if (!User->isAnyAdd())
15513 return SDValue();
15514
15515 // We prefer 2xMAD over MUL + 2xADD + 2xADDC (code density), and prefer
15516 // MUL + 3xADD + 3xADDC over 3xMAD.
15517 ++NumUsers;
15518 if (NumUsers >= 3)
15519 return SDValue();
15520 }
15521 }
15522
15523 SDValue MulLHS = LHS.getOperand(0);
15524 SDValue MulRHS = LHS.getOperand(1);
15525 SDValue AddRHS = RHS;
15526
15527 if (SDValue FoldedMAD = tryFoldMADwithSRL(DAG, SL, MulLHS, MulRHS, AddRHS))
15528 return FoldedMAD;
15529
15530 // Always check whether operands are small unsigned values, since that
15531 // knowledge is useful in more cases. Check for small signed values only if
15532 // doing so can unlock a shorter code sequence.
15533 bool MulLHSUnsigned32 = numBitsUnsigned(MulLHS, DAG) <= 32;
15534 bool MulRHSUnsigned32 = numBitsUnsigned(MulRHS, DAG) <= 32;
15535
15536 bool MulSignedLo = false;
15537 if (!MulLHSUnsigned32 || !MulRHSUnsigned32) {
15538 MulSignedLo =
15539 numBitsSigned(MulLHS, DAG) <= 32 && numBitsSigned(MulRHS, DAG) <= 32;
15540 }
15541
15542 // The operands and final result all have the same number of bits. If
15543 // operands need to be extended, they can be extended with garbage. The
15544 // resulting garbage in the high bits of the mad_[iu]64_[iu]32 result is
15545 // truncated away in the end.
15546 if (VT != MVT::i64) {
15547 MulLHS = DAG.getNode(ISD::ANY_EXTEND, SL, MVT::i64, MulLHS);
15548 MulRHS = DAG.getNode(ISD::ANY_EXTEND, SL, MVT::i64, MulRHS);
15549 AddRHS = DAG.getNode(ISD::ANY_EXTEND, SL, MVT::i64, AddRHS);
15550 }
15551
15552 // The basic code generated is conceptually straightforward. Pseudo code:
15553 //
15554 // accum = mad_64_32 lhs.lo, rhs.lo, accum
15555 // accum.hi = add (mul lhs.hi, rhs.lo), accum.hi
15556 // accum.hi = add (mul lhs.lo, rhs.hi), accum.hi
15557 //
15558 // The second and third lines are optional, depending on whether the factors
15559 // are {sign,zero}-extended or not.
15560 //
15561 // The actual DAG is noisier than the pseudo code, but only due to
15562 // instructions that disassemble values into low and high parts, and
15563 // assemble the final result.
15564 SDValue One = DAG.getConstant(1, SL, MVT::i32);
15565
15566 auto MulLHSLo = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, MulLHS);
15567 auto MulRHSLo = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, MulRHS);
15568 SDValue Accum =
15569 getMad64_32(DAG, SL, MVT::i64, MulLHSLo, MulRHSLo, AddRHS, MulSignedLo);
15570
15571 if (!MulSignedLo && (!MulLHSUnsigned32 || !MulRHSUnsigned32)) {
15572 auto [AccumLo, AccumHi] = DAG.SplitScalar(Accum, SL, MVT::i32, MVT::i32);
15573
15574 if (!MulLHSUnsigned32) {
15575 auto MulLHSHi =
15576 DAG.getNode(ISD::EXTRACT_ELEMENT, SL, MVT::i32, MulLHS, One);
15577 SDValue MulHi = DAG.getNode(ISD::MUL, SL, MVT::i32, MulLHSHi, MulRHSLo);
15578 AccumHi = DAG.getNode(ISD::ADD, SL, MVT::i32, MulHi, AccumHi);
15579 }
15580
15581 if (!MulRHSUnsigned32) {
15582 auto MulRHSHi =
15583 DAG.getNode(ISD::EXTRACT_ELEMENT, SL, MVT::i32, MulRHS, One);
15584 SDValue MulHi = DAG.getNode(ISD::MUL, SL, MVT::i32, MulLHSLo, MulRHSHi);
15585 AccumHi = DAG.getNode(ISD::ADD, SL, MVT::i32, MulHi, AccumHi);
15586 }
15587
15588 Accum = DAG.getBuildVector(MVT::v2i32, SL, {AccumLo, AccumHi});
15589 Accum = DAG.getBitcast(MVT::i64, Accum);
15590 }
15591
15592 if (VT != MVT::i64)
15593 Accum = DAG.getNode(ISD::TRUNCATE, SL, VT, Accum);
15594 return Accum;
15595}
15596
15597SDValue
15598SITargetLowering::foldAddSub64WithZeroLowBitsTo32(SDNode *N,
15599 DAGCombinerInfo &DCI) const {
15600 SDValue RHS = N->getOperand(1);
15601 auto *CRHS = dyn_cast<ConstantSDNode>(RHS);
15602 if (!CRHS)
15603 return SDValue();
15604
15605 // TODO: Worth using computeKnownBits? Maybe expensive since it's so
15606 // common.
15607 uint64_t Val = CRHS->getZExtValue();
15608 if (countr_zero(Val) >= 32) {
15609 SelectionDAG &DAG = DCI.DAG;
15610 SDLoc SL(N);
15611 SDValue LHS = N->getOperand(0);
15612
15613 // Avoid carry machinery if we know the low half of the add does not
15614 // contribute to the final result.
15615 //
15616 // add i64:x, K if computeTrailingZeros(K) >= 32
15617 // => build_pair (add x.hi, K.hi), x.lo
15618
15619 // Breaking the 64-bit add here with this strange constant is unlikely
15620 // to interfere with addressing mode patterns.
15621
15622 SDValue Hi = getHiHalf64(LHS, DAG);
15623 SDValue ConstHi32 = DAG.getConstant(Hi_32(Val), SL, MVT::i32);
15624 unsigned Opcode = N->getOpcode();
15625 if (Opcode == ISD::PTRADD)
15626 Opcode = ISD::ADD;
15627 SDValue AddHi =
15628 DAG.getNode(Opcode, SL, MVT::i32, Hi, ConstHi32, N->getFlags());
15629
15630 SDValue Lo = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, LHS);
15631 return DAG.getNode(ISD::BUILD_PAIR, SL, MVT::i64, Lo, AddHi);
15632 }
15633
15634 return SDValue();
15635}
15636
15637// Collect the ultimate src of each of the mul node's operands, and confirm
15638// each operand is 8 bytes.
15639static std::optional<ByteProvider<SDValue>>
15640handleMulOperand(const SDValue &MulOperand) {
15641 auto Byte0 = calculateByteProvider(MulOperand, 0, 0);
15642 if (!Byte0 || Byte0->isConstantZero()) {
15643 return std::nullopt;
15644 }
15645 auto Byte1 = calculateByteProvider(MulOperand, 1, 0);
15646 if (Byte1 && !Byte1->isConstantZero()) {
15647 return std::nullopt;
15648 }
15649 return Byte0;
15650}
15651
15652static unsigned addPermMasks(unsigned First, unsigned Second) {
15653 unsigned FirstCs = First & 0x0c0c0c0c;
15654 unsigned SecondCs = Second & 0x0c0c0c0c;
15655 unsigned FirstNoCs = First & ~0x0c0c0c0c;
15656 unsigned SecondNoCs = Second & ~0x0c0c0c0c;
15657
15658 assert((FirstCs & 0xFF) | (SecondCs & 0xFF));
15659 assert((FirstCs & 0xFF00) | (SecondCs & 0xFF00));
15660 assert((FirstCs & 0xFF0000) | (SecondCs & 0xFF0000));
15661 assert((FirstCs & 0xFF000000) | (SecondCs & 0xFF000000));
15662
15663 return (FirstNoCs | SecondNoCs) | (FirstCs & SecondCs);
15664}
15665
15666struct DotSrc {
15668 int64_t PermMask;
15670};
15671
15675 SmallVectorImpl<DotSrc> &Src1s, int Step) {
15676
15677 assert(Src0.Src.has_value() && Src1.Src.has_value());
15678 // Src0s and Src1s are empty, just place arbitrarily.
15679 if (Step == 0) {
15680 Src0s.push_back({*Src0.Src, ((Src0.SrcOffset % 4) << 24) + 0x0c0c0c,
15681 Src0.SrcOffset / 4});
15682 Src1s.push_back({*Src1.Src, ((Src1.SrcOffset % 4) << 24) + 0x0c0c0c,
15683 Src1.SrcOffset / 4});
15684 return;
15685 }
15686
15687 for (int BPI = 0; BPI < 2; BPI++) {
15688 std::pair<ByteProvider<SDValue>, ByteProvider<SDValue>> BPP = {Src0, Src1};
15689 if (BPI == 1) {
15690 BPP = {Src1, Src0};
15691 }
15692 unsigned ZeroMask = 0x0c0c0c0c;
15693 unsigned FMask = 0xFF << (8 * (3 - Step));
15694
15695 unsigned FirstMask =
15696 (BPP.first.SrcOffset % 4) << (8 * (3 - Step)) | (ZeroMask & ~FMask);
15697 unsigned SecondMask =
15698 (BPP.second.SrcOffset % 4) << (8 * (3 - Step)) | (ZeroMask & ~FMask);
15699 // Attempt to find Src vector which contains our SDValue, if so, add our
15700 // perm mask to the existing one. If we are unable to find a match for the
15701 // first SDValue, attempt to find match for the second.
15702 int FirstGroup = -1;
15703 for (int I = 0; I < 2; I++) {
15704 SmallVectorImpl<DotSrc> &Srcs = I == 0 ? Src0s : Src1s;
15705 auto MatchesFirst = [&BPP](DotSrc &IterElt) {
15706 return IterElt.SrcOp == *BPP.first.Src &&
15707 (IterElt.DWordOffset == (BPP.first.SrcOffset / 4));
15708 };
15709
15710 auto *Match = llvm::find_if(Srcs, MatchesFirst);
15711 if (Match != Srcs.end()) {
15712 Match->PermMask = addPermMasks(FirstMask, Match->PermMask);
15713 FirstGroup = I;
15714 break;
15715 }
15716 }
15717 if (FirstGroup != -1) {
15718 SmallVectorImpl<DotSrc> &Srcs = FirstGroup == 1 ? Src0s : Src1s;
15719 auto MatchesSecond = [&BPP](DotSrc &IterElt) {
15720 return IterElt.SrcOp == *BPP.second.Src &&
15721 (IterElt.DWordOffset == (BPP.second.SrcOffset / 4));
15722 };
15723 auto *Match = llvm::find_if(Srcs, MatchesSecond);
15724 if (Match != Srcs.end()) {
15725 Match->PermMask = addPermMasks(SecondMask, Match->PermMask);
15726 } else
15727 Srcs.push_back({*BPP.second.Src, SecondMask, BPP.second.SrcOffset / 4});
15728 return;
15729 }
15730 }
15731
15732 // If we have made it here, then we could not find a match in Src0s or Src1s
15733 // for either Src0 or Src1, so just place them arbitrarily.
15734
15735 unsigned ZeroMask = 0x0c0c0c0c;
15736 unsigned FMask = 0xFF << (8 * (3 - Step));
15737
15738 Src0s.push_back(
15739 {*Src0.Src,
15740 ((Src0.SrcOffset % 4) << (8 * (3 - Step)) | (ZeroMask & ~FMask)),
15741 Src0.SrcOffset / 4});
15742 Src1s.push_back(
15743 {*Src1.Src,
15744 ((Src1.SrcOffset % 4) << (8 * (3 - Step)) | (ZeroMask & ~FMask)),
15745 Src1.SrcOffset / 4});
15746}
15747
15749 SmallVectorImpl<DotSrc> &Srcs, bool IsSigned,
15750 bool IsAny) {
15751
15752 // If we just have one source, just permute it accordingly.
15753 if (Srcs.size() == 1) {
15754 auto *Elt = Srcs.begin();
15755 auto EltOp = getDWordFromOffset(DAG, SL, Elt->SrcOp, Elt->DWordOffset);
15756
15757 // v_perm will produce the original value
15758 if (Elt->PermMask == 0x3020100)
15759 return EltOp;
15760
15761 return DAG.getNode(AMDGPUISD::PERM, SL, MVT::i32, EltOp, EltOp,
15762 DAG.getConstant(Elt->PermMask, SL, MVT::i32));
15763 }
15764
15765 auto *FirstElt = Srcs.begin();
15766 auto *SecondElt = std::next(FirstElt);
15767
15769
15770 // If we have multiple sources in the chain, combine them via perms (using
15771 // calculated perm mask) and Ors.
15772 while (true) {
15773 auto FirstMask = FirstElt->PermMask;
15774 auto SecondMask = SecondElt->PermMask;
15775
15776 unsigned FirstCs = FirstMask & 0x0c0c0c0c;
15777 unsigned FirstPlusFour = FirstMask | 0x04040404;
15778 // 0x0c + 0x04 = 0x10, so anding with 0x0F will produced 0x00 for any
15779 // original 0x0C.
15780 FirstMask = (FirstPlusFour & 0x0F0F0F0F) | FirstCs;
15781
15782 auto PermMask = addPermMasks(FirstMask, SecondMask);
15783 auto FirstVal =
15784 getDWordFromOffset(DAG, SL, FirstElt->SrcOp, FirstElt->DWordOffset);
15785 auto SecondVal =
15786 getDWordFromOffset(DAG, SL, SecondElt->SrcOp, SecondElt->DWordOffset);
15787
15788 Perms.push_back(DAG.getNode(AMDGPUISD::PERM, SL, MVT::i32, FirstVal,
15789 SecondVal,
15790 DAG.getConstant(PermMask, SL, MVT::i32)));
15791
15792 FirstElt = std::next(SecondElt);
15793 if (FirstElt == Srcs.end())
15794 break;
15795
15796 SecondElt = std::next(FirstElt);
15797 // If we only have a FirstElt, then just combine that into the cumulative
15798 // source node.
15799 if (SecondElt == Srcs.end()) {
15800 auto EltOp =
15801 getDWordFromOffset(DAG, SL, FirstElt->SrcOp, FirstElt->DWordOffset);
15802
15803 Perms.push_back(
15804 DAG.getNode(AMDGPUISD::PERM, SL, MVT::i32, EltOp, EltOp,
15805 DAG.getConstant(FirstElt->PermMask, SL, MVT::i32)));
15806 break;
15807 }
15808 }
15809
15810 assert(Perms.size() == 1 || Perms.size() == 2);
15811 return Perms.size() == 2
15812 ? DAG.getNode(ISD::OR, SL, MVT::i32, Perms[0], Perms[1])
15813 : Perms[0];
15814}
15815
15816static void fixMasks(SmallVectorImpl<DotSrc> &Srcs, unsigned ChainLength) {
15817 for (auto &[EntryVal, EntryMask, EntryOffset] : Srcs) {
15818 EntryMask = EntryMask >> ((4 - ChainLength) * 8);
15819 auto ZeroMask = ChainLength == 2 ? 0x0c0c0000 : 0x0c000000;
15820 EntryMask += ZeroMask;
15821 }
15822}
15823
15824static bool isMul(const SDValue Op) {
15825 auto Opcode = Op.getOpcode();
15826
15827 return (Opcode == ISD::MUL || Opcode == AMDGPUISD::MUL_U24 ||
15828 Opcode == AMDGPUISD::MUL_I24);
15829}
15830
15831static std::optional<bool>
15833 ByteProvider<SDValue> &Src1, const SDValue &S0Op,
15834 const SDValue &S1Op, const SelectionDAG &DAG) {
15835 // If we both ops are i8s (pre legalize-dag), then the signedness semantics
15836 // of the dot4 is irrelevant.
15837 if (S0Op.getValueSizeInBits() == 8 && S1Op.getValueSizeInBits() == 8)
15838 return false;
15839
15840 auto Known0 = DAG.computeKnownBits(S0Op, 0);
15841 bool S0IsUnsigned = Known0.countMinLeadingZeros() > 0;
15842 bool S0IsSigned = Known0.countMinLeadingOnes() > 0;
15843 auto Known1 = DAG.computeKnownBits(S1Op, 0);
15844 bool S1IsUnsigned = Known1.countMinLeadingZeros() > 0;
15845 bool S1IsSigned = Known1.countMinLeadingOnes() > 0;
15846
15847 assert(!(S0IsUnsigned && S0IsSigned));
15848 assert(!(S1IsUnsigned && S1IsSigned));
15849
15850 // There are 9 possible permutations of
15851 // {S0IsUnsigned, S0IsSigned, S1IsUnsigned, S1IsSigned}
15852
15853 // In two permutations, the sign bits are known to be the same for both Ops,
15854 // so simply return Signed / Unsigned corresponding to the MSB
15855
15856 if ((S0IsUnsigned && S1IsUnsigned) || (S0IsSigned && S1IsSigned))
15857 return S0IsSigned;
15858
15859 // In another two permutations, the sign bits are known to be opposite. In
15860 // this case return std::nullopt to indicate a bad match.
15861
15862 if ((S0IsUnsigned && S1IsSigned) || (S0IsSigned && S1IsUnsigned))
15863 return std::nullopt;
15864
15865 // In the remaining five permutations, we don't know the value of the sign
15866 // bit for at least one Op. Since we have a valid ByteProvider, we know that
15867 // the upper bits must be extension bits. Thus, the only ways for the sign
15868 // bit to be unknown is if it was sign extended from unknown value, or if it
15869 // was any extended. In either case, it is correct to use the signed
15870 // version of the signedness semantics of dot4
15871
15872 // In two of such permutations, we known the sign bit is set for
15873 // one op, and the other is unknown. It is okay to used signed version of
15874 // dot4.
15875 if ((S0IsSigned && !(S1IsSigned || S1IsUnsigned)) ||
15876 ((S1IsSigned && !(S0IsSigned || S0IsUnsigned))))
15877 return true;
15878
15879 // In one such permutation, we don't know either of the sign bits. It is okay
15880 // to used the signed version of dot4.
15881 if ((!(S1IsSigned || S1IsUnsigned) && !(S0IsSigned || S0IsUnsigned)))
15882 return true;
15883
15884 // In two of such permutations, we known the sign bit is unset for
15885 // one op, and the other is unknown. Return std::nullopt to indicate a
15886 // bad match.
15887 if ((S0IsUnsigned && !(S1IsSigned || S1IsUnsigned)) ||
15888 ((S1IsUnsigned && !(S0IsSigned || S0IsUnsigned))))
15889 return std::nullopt;
15890
15891 llvm_unreachable("Fully covered condition");
15892}
15893
15894SDValue SITargetLowering::performAddCombine(SDNode *N,
15895 DAGCombinerInfo &DCI) const {
15896 SelectionDAG &DAG = DCI.DAG;
15897 EVT VT = N->getValueType(0);
15898 SDLoc SL(N);
15899 SDValue LHS = N->getOperand(0);
15900 SDValue RHS = N->getOperand(1);
15901
15902 if (LHS.getOpcode() == ISD::MUL || RHS.getOpcode() == ISD::MUL) {
15903 if (Subtarget->hasMad64_32()) {
15904 if (SDValue Folded = tryFoldToMad64_32(N, DCI))
15905 return Folded;
15906 }
15907 }
15908
15909 if (SDValue V = reassociateScalarOps(N, DAG)) {
15910 return V;
15911 }
15912
15913 if (VT == MVT::i64) {
15914 if (SDValue Folded = foldAddSub64WithZeroLowBitsTo32(N, DCI))
15915 return Folded;
15916 }
15917
15918 if ((isMul(LHS) || isMul(RHS)) && Subtarget->hasDot7Insts() &&
15919 (Subtarget->hasDot1Insts() || Subtarget->hasDot8Insts())) {
15920 SDValue TempNode(N, 0);
15921 std::optional<bool> IsSigned;
15925
15926 // Match the v_dot4 tree, while collecting src nodes.
15927 int ChainLength = 0;
15928 for (int I = 0; I < 4; I++) {
15929 auto MulIdx = isMul(LHS) ? 0 : isMul(RHS) ? 1 : -1;
15930 if (MulIdx == -1)
15931 break;
15932 auto Src0 = handleMulOperand(TempNode->getOperand(MulIdx)->getOperand(0));
15933 if (!Src0)
15934 break;
15935 auto Src1 = handleMulOperand(TempNode->getOperand(MulIdx)->getOperand(1));
15936 if (!Src1)
15937 break;
15938
15939 auto IterIsSigned = checkDot4MulSignedness(
15940 TempNode->getOperand(MulIdx), *Src0, *Src1,
15941 TempNode->getOperand(MulIdx)->getOperand(0),
15942 TempNode->getOperand(MulIdx)->getOperand(1), DAG);
15943 if (!IterIsSigned)
15944 break;
15945 if (!IsSigned)
15946 IsSigned = *IterIsSigned;
15947 if (*IterIsSigned != *IsSigned)
15948 break;
15949 placeSources(*Src0, *Src1, Src0s, Src1s, I);
15950 auto AddIdx = 1 - MulIdx;
15951 // Allow the special case where add (add (mul24, 0), mul24) became ->
15952 // add (mul24, mul24).
15953 if (I == 2 && isMul(TempNode->getOperand(AddIdx))) {
15954 Src2s.push_back(TempNode->getOperand(AddIdx));
15955 auto Src0 =
15956 handleMulOperand(TempNode->getOperand(AddIdx)->getOperand(0));
15957 if (!Src0)
15958 break;
15959 auto Src1 =
15960 handleMulOperand(TempNode->getOperand(AddIdx)->getOperand(1));
15961 if (!Src1)
15962 break;
15963 auto IterIsSigned = checkDot4MulSignedness(
15964 TempNode->getOperand(AddIdx), *Src0, *Src1,
15965 TempNode->getOperand(AddIdx)->getOperand(0),
15966 TempNode->getOperand(AddIdx)->getOperand(1), DAG);
15967 if (!IterIsSigned)
15968 break;
15969 assert(IsSigned);
15970 if (*IterIsSigned != *IsSigned)
15971 break;
15972 placeSources(*Src0, *Src1, Src0s, Src1s, I + 1);
15973 Src2s.push_back(DAG.getConstant(0, SL, MVT::i32));
15974 ChainLength = I + 2;
15975 break;
15976 }
15977
15978 TempNode = TempNode->getOperand(AddIdx);
15979 Src2s.push_back(TempNode);
15980 ChainLength = I + 1;
15981 if (TempNode->getNumOperands() < 2)
15982 break;
15983 LHS = TempNode->getOperand(0);
15984 RHS = TempNode->getOperand(1);
15985 }
15986
15987 if (ChainLength < 2)
15988 return SDValue();
15989
15990 // Masks were constructed with assumption that we would find a chain of
15991 // length 4. If not, then we need to 0 out the MSB bits (via perm mask of
15992 // 0x0c) so they do not affect dot calculation.
15993 if (ChainLength < 4) {
15994 fixMasks(Src0s, ChainLength);
15995 fixMasks(Src1s, ChainLength);
15996 }
15997
15998 SDValue Src0, Src1;
15999
16000 // If we are just using a single source for both, and have permuted the
16001 // bytes consistently, we can just use the sources without permuting
16002 // (commutation).
16003 bool UseOriginalSrc = false;
16004 if (ChainLength == 4 && Src0s.size() == 1 && Src1s.size() == 1 &&
16005 Src0s.begin()->PermMask == Src1s.begin()->PermMask &&
16006 Src0s.begin()->SrcOp.getValueSizeInBits() >= 32 &&
16007 Src1s.begin()->SrcOp.getValueSizeInBits() >= 32) {
16008 SmallVector<unsigned, 4> SrcBytes;
16009 auto Src0Mask = Src0s.begin()->PermMask;
16010 SrcBytes.push_back(Src0Mask & 0xFF000000);
16011 bool UniqueEntries = true;
16012 for (auto I = 1; I < 4; I++) {
16013 auto NextByte = Src0Mask & (0xFF << ((3 - I) * 8));
16014
16015 if (is_contained(SrcBytes, NextByte)) {
16016 UniqueEntries = false;
16017 break;
16018 }
16019 SrcBytes.push_back(NextByte);
16020 }
16021
16022 if (UniqueEntries) {
16023 UseOriginalSrc = true;
16024
16025 auto *FirstElt = Src0s.begin();
16026 auto FirstEltOp =
16027 getDWordFromOffset(DAG, SL, FirstElt->SrcOp, FirstElt->DWordOffset);
16028
16029 auto *SecondElt = Src1s.begin();
16030 auto SecondEltOp = getDWordFromOffset(DAG, SL, SecondElt->SrcOp,
16031 SecondElt->DWordOffset);
16032
16033 Src0 = DAG.getBitcastedAnyExtOrTrunc(FirstEltOp, SL,
16034 MVT::getIntegerVT(32));
16035 Src1 = DAG.getBitcastedAnyExtOrTrunc(SecondEltOp, SL,
16036 MVT::getIntegerVT(32));
16037 }
16038 }
16039
16040 if (!UseOriginalSrc) {
16041 Src0 = resolveSources(DAG, SL, Src0s, false, true);
16042 Src1 = resolveSources(DAG, SL, Src1s, false, true);
16043 }
16044
16045 assert(IsSigned);
16046 SDValue Src2 =
16047 DAG.getExtOrTrunc(*IsSigned, Src2s[ChainLength - 1], SL, MVT::i32);
16048
16049 SDValue IID = DAG.getTargetConstant(*IsSigned ? Intrinsic::amdgcn_sdot4
16050 : Intrinsic::amdgcn_udot4,
16051 SL, MVT::i64);
16052
16053 assert(!VT.isVector());
16054 auto Dot = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SL, MVT::i32, IID, Src0,
16055 Src1, Src2, DAG.getTargetConstant(0, SL, MVT::i1));
16056
16057 return DAG.getExtOrTrunc(*IsSigned, Dot, SL, VT);
16058 }
16059
16060 if (VT != MVT::i32 || !DCI.isAfterLegalizeDAG())
16061 return SDValue();
16062
16063 // add x, zext (setcc) => uaddo_carry x, 0, setcc
16064 // add x, sext (setcc) => usubo_carry x, 0, setcc
16065 unsigned Opc = LHS.getOpcode();
16068 std::swap(RHS, LHS);
16069
16070 Opc = RHS.getOpcode();
16071 switch (Opc) {
16072 default:
16073 break;
16074 case ISD::ZERO_EXTEND:
16075 case ISD::SIGN_EXTEND:
16076 case ISD::ANY_EXTEND: {
16077 auto Cond = RHS.getOperand(0);
16078 // If this won't be a real VOPC output, we would still need to insert an
16079 // extra instruction anyway.
16080 if (!isBoolSGPR(Cond))
16081 break;
16082 SDVTList VTList = DAG.getVTList(MVT::i32, MVT::i1);
16083 SDValue Args[] = {LHS, DAG.getConstant(0, SL, MVT::i32), Cond};
16085 return DAG.getNode(Opc, SL, VTList, Args);
16086 }
16087 case ISD::UADDO_CARRY: {
16088 // add x, (uaddo_carry y, 0, cc) => uaddo_carry x, y, cc
16089 if (!isNullConstant(RHS.getOperand(1)))
16090 break;
16091 SDValue Args[] = {LHS, RHS.getOperand(0), RHS.getOperand(2)};
16092 return DAG.getNode(ISD::UADDO_CARRY, SDLoc(N), RHS->getVTList(), Args);
16093 }
16094 }
16095 return SDValue();
16096}
16097
16098SDValue SITargetLowering::performPtrAddCombine(SDNode *N,
16099 DAGCombinerInfo &DCI) const {
16100 SelectionDAG &DAG = DCI.DAG;
16101 SDLoc DL(N);
16102 EVT VT = N->getValueType(0);
16103 SDValue N0 = N->getOperand(0);
16104 SDValue N1 = N->getOperand(1);
16105
16106 // The following folds transform PTRADDs into regular arithmetic in cases
16107 // where the PTRADD wouldn't be folded as an immediate offset into memory
16108 // instructions anyway. They are target-specific in that other targets might
16109 // prefer to not lose information about the pointer arithmetic.
16110
16111 // Fold (ptradd x, shl(0 - v, k)) -> sub(x, shl(v, k)).
16112 // Adapted from DAGCombiner::visitADDLikeCommutative.
16113 SDValue V, K;
16114 if (sd_match(N1, m_Shl(m_Neg(m_Value(V)), m_Value(K)))) {
16115 SDNodeFlags ShlFlags = N1->getFlags();
16116 // If the original shl is NUW and NSW, the first k+1 bits of 0-v are all 0,
16117 // so v is either 0 or the first k+1 bits of v are all 1 -> NSW can be
16118 // preserved.
16119 SDNodeFlags NewShlFlags =
16120 ShlFlags.hasNoUnsignedWrap() && ShlFlags.hasNoSignedWrap()
16122 : SDNodeFlags();
16123 SDValue Inner = DAG.getNode(ISD::SHL, DL, VT, V, K, NewShlFlags);
16124 DCI.AddToWorklist(Inner.getNode());
16125 return DAG.getNode(ISD::SUB, DL, VT, N0, Inner);
16126 }
16127
16128 // Fold into Mad64 if the right-hand side is a MUL. Analogous to a fold in
16129 // performAddCombine.
16130 if (N1.getOpcode() == ISD::MUL) {
16131 if (Subtarget->hasMad64_32()) {
16132 if (SDValue Folded = tryFoldToMad64_32(N, DCI))
16133 return Folded;
16134 }
16135 }
16136
16137 // If the 32 low bits of the constant are all zero, there is nothing to fold
16138 // into an immediate offset, so it's better to eliminate the unnecessary
16139 // addition for the lower 32 bits than to preserve the PTRADD.
16140 // Analogous to a fold in performAddCombine.
16141 if (VT == MVT::i64) {
16142 if (SDValue Folded = foldAddSub64WithZeroLowBitsTo32(N, DCI))
16143 return Folded;
16144 }
16145
16146 if (N1.getOpcode() != ISD::ADD || !N1.hasOneUse())
16147 return SDValue();
16148
16149 SDValue X = N0;
16150 SDValue Y = N1.getOperand(0);
16151 SDValue Z = N1.getOperand(1);
16152 bool YIsConstant = DAG.isConstantIntBuildVectorOrConstantInt(Y);
16153 bool ZIsConstant = DAG.isConstantIntBuildVectorOrConstantInt(Z);
16154
16155 if (!YIsConstant && !ZIsConstant && !X->isDivergent() &&
16156 Y->isDivergent() != Z->isDivergent()) {
16157 // Reassociate (ptradd x, (add y, z)) -> (ptradd (ptradd x, y), z) if x and
16158 // y are uniform and z isn't.
16159 // Reassociate (ptradd x, (add y, z)) -> (ptradd (ptradd x, z), y) if x and
16160 // z are uniform and y isn't.
16161 // The goal is to push uniform operands up in the computation, so that they
16162 // can be handled with scalar operations. We can't use reassociateScalarOps
16163 // for this since it requires two identical commutative operations to
16164 // reassociate.
16165 if (Y->isDivergent())
16166 std::swap(Y, Z);
16167 // If both additions in the original were NUW, reassociation preserves that.
16168 SDNodeFlags ReassocFlags =
16169 (N->getFlags() & N1->getFlags()) & SDNodeFlags::NoUnsignedWrap;
16170 SDValue UniformInner = DAG.getMemBasePlusOffset(X, Y, DL, ReassocFlags);
16171 DCI.AddToWorklist(UniformInner.getNode());
16172 return DAG.getMemBasePlusOffset(UniformInner, Z, DL, ReassocFlags);
16173 }
16174
16175 return SDValue();
16176}
16177
16178SDValue SITargetLowering::performSubCombine(SDNode *N,
16179 DAGCombinerInfo &DCI) const {
16180 SelectionDAG &DAG = DCI.DAG;
16181 EVT VT = N->getValueType(0);
16182
16183 if (VT == MVT::i64) {
16184 if (SDValue Folded = foldAddSub64WithZeroLowBitsTo32(N, DCI))
16185 return Folded;
16186 }
16187
16188 if (VT != MVT::i32)
16189 return SDValue();
16190
16191 SDLoc SL(N);
16192 SDValue LHS = N->getOperand(0);
16193 SDValue RHS = N->getOperand(1);
16194
16195 // sub x, zext (setcc) => usubo_carry x, 0, setcc
16196 // sub x, sext (setcc) => uaddo_carry x, 0, setcc
16197 unsigned Opc = RHS.getOpcode();
16198 switch (Opc) {
16199 default:
16200 break;
16201 case ISD::ZERO_EXTEND:
16202 case ISD::SIGN_EXTEND:
16203 case ISD::ANY_EXTEND: {
16204 auto Cond = RHS.getOperand(0);
16205 // If this won't be a real VOPC output, we would still need to insert an
16206 // extra instruction anyway.
16207 if (!isBoolSGPR(Cond))
16208 break;
16209 SDVTList VTList = DAG.getVTList(MVT::i32, MVT::i1);
16210 SDValue Args[] = {LHS, DAG.getConstant(0, SL, MVT::i32), Cond};
16212 return DAG.getNode(Opc, SL, VTList, Args);
16213 }
16214 }
16215
16216 if (LHS.getOpcode() == ISD::USUBO_CARRY) {
16217 // sub (usubo_carry x, 0, cc), y => usubo_carry x, y, cc
16218 if (!isNullConstant(LHS.getOperand(1)))
16219 return SDValue();
16220 SDValue Args[] = {LHS.getOperand(0), RHS, LHS.getOperand(2)};
16221 return DAG.getNode(ISD::USUBO_CARRY, SDLoc(N), LHS->getVTList(), Args);
16222 }
16223 return SDValue();
16224}
16225
16226SDValue
16227SITargetLowering::performAddCarrySubCarryCombine(SDNode *N,
16228 DAGCombinerInfo &DCI) const {
16229
16230 if (N->getValueType(0) != MVT::i32)
16231 return SDValue();
16232
16233 if (!isNullConstant(N->getOperand(1)))
16234 return SDValue();
16235
16236 SelectionDAG &DAG = DCI.DAG;
16237 SDValue LHS = N->getOperand(0);
16238
16239 // uaddo_carry (add x, y), 0, cc => uaddo_carry x, y, cc
16240 // usubo_carry (sub x, y), 0, cc => usubo_carry x, y, cc
16241 unsigned LHSOpc = LHS.getOpcode();
16242 unsigned Opc = N->getOpcode();
16243 if ((LHSOpc == ISD::ADD && Opc == ISD::UADDO_CARRY) ||
16244 (LHSOpc == ISD::SUB && Opc == ISD::USUBO_CARRY)) {
16245 SDValue Args[] = {LHS.getOperand(0), LHS.getOperand(1), N->getOperand(2)};
16246 return DAG.getNode(Opc, SDLoc(N), N->getVTList(), Args);
16247 }
16248 return SDValue();
16249}
16250
16251SDValue SITargetLowering::performFAddCombine(SDNode *N,
16252 DAGCombinerInfo &DCI) const {
16253 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
16254 return SDValue();
16255
16256 SelectionDAG &DAG = DCI.DAG;
16257 EVT VT = N->getValueType(0);
16258
16259 SDLoc SL(N);
16260 SDValue LHS = N->getOperand(0);
16261 SDValue RHS = N->getOperand(1);
16262
16263 // These should really be instruction patterns, but writing patterns with
16264 // source modifiers is a pain.
16265
16266 // fadd (fadd (a, a), b) -> mad 2.0, a, b
16267 if (LHS.getOpcode() == ISD::FADD) {
16268 SDValue A = LHS.getOperand(0);
16269 if (A == LHS.getOperand(1)) {
16270 unsigned FusedOp = getFusedOpcode(DAG, N, LHS.getNode());
16271 if (FusedOp != 0) {
16272 const SDValue Two = DAG.getConstantFP(2.0, SL, VT);
16273 return DAG.getNode(FusedOp, SL, VT, A, Two, RHS);
16274 }
16275 }
16276 }
16277
16278 // fadd (b, fadd (a, a)) -> mad 2.0, a, b
16279 if (RHS.getOpcode() == ISD::FADD) {
16280 SDValue A = RHS.getOperand(0);
16281 if (A == RHS.getOperand(1)) {
16282 unsigned FusedOp = getFusedOpcode(DAG, N, RHS.getNode());
16283 if (FusedOp != 0) {
16284 const SDValue Two = DAG.getConstantFP(2.0, SL, VT);
16285 return DAG.getNode(FusedOp, SL, VT, A, Two, LHS);
16286 }
16287 }
16288 }
16289
16290 return SDValue();
16291}
16292
16293SDValue SITargetLowering::performFSubCombine(SDNode *N,
16294 DAGCombinerInfo &DCI) const {
16295 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
16296 return SDValue();
16297
16298 SelectionDAG &DAG = DCI.DAG;
16299 SDLoc SL(N);
16300 EVT VT = N->getValueType(0);
16301 assert(!VT.isVector());
16302
16303 // Try to get the fneg to fold into the source modifier. This undoes generic
16304 // DAG combines and folds them into the mad.
16305 //
16306 // Only do this if we are not trying to support denormals. v_mad_f32 does
16307 // not support denormals ever.
16308 SDValue LHS = N->getOperand(0);
16309 SDValue RHS = N->getOperand(1);
16310 if (LHS.getOpcode() == ISD::FADD) {
16311 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c)
16312 SDValue A = LHS.getOperand(0);
16313 if (A == LHS.getOperand(1)) {
16314 unsigned FusedOp = getFusedOpcode(DAG, N, LHS.getNode());
16315 if (FusedOp != 0) {
16316 const SDValue Two = DAG.getConstantFP(2.0, SL, VT);
16317 SDValue NegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
16318
16319 return DAG.getNode(FusedOp, SL, VT, A, Two, NegRHS);
16320 }
16321 }
16322 }
16323
16324 if (RHS.getOpcode() == ISD::FADD) {
16325 // (fsub c, (fadd a, a)) -> mad -2.0, a, c
16326
16327 SDValue A = RHS.getOperand(0);
16328 if (A == RHS.getOperand(1)) {
16329 unsigned FusedOp = getFusedOpcode(DAG, N, RHS.getNode());
16330 if (FusedOp != 0) {
16331 const SDValue NegTwo = DAG.getConstantFP(-2.0, SL, VT);
16332 return DAG.getNode(FusedOp, SL, VT, A, NegTwo, LHS);
16333 }
16334 }
16335 }
16336
16337 return SDValue();
16338}
16339
16340SDValue SITargetLowering::performFDivCombine(SDNode *N,
16341 DAGCombinerInfo &DCI) const {
16342 SelectionDAG &DAG = DCI.DAG;
16343 SDLoc SL(N);
16344 EVT VT = N->getValueType(0);
16345 if ((VT != MVT::f16 && VT != MVT::bf16) || !Subtarget->has16BitInsts())
16346 return SDValue();
16347
16348 SDValue LHS = N->getOperand(0);
16349 SDValue RHS = N->getOperand(1);
16350
16351 SDNodeFlags Flags = N->getFlags();
16352 SDNodeFlags RHSFlags = RHS->getFlags();
16353 if (!Flags.hasAllowContract() || !RHSFlags.hasAllowContract() ||
16354 !RHS->hasOneUse())
16355 return SDValue();
16356
16357 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
16358 bool IsNegative = false;
16359 if (CLHS->isExactlyValue(1.0) ||
16360 (IsNegative = CLHS->isExactlyValue(-1.0))) {
16361 // fdiv contract 1.0, (sqrt contract x) -> rsq for f16
16362 // fdiv contract -1.0, (sqrt contract x) -> fneg(rsq) for f16
16363 if (RHS.getOpcode() == ISD::FSQRT) {
16364 // TODO: Or in RHS flags, somehow missing from SDNodeFlags
16365 SDValue Rsq =
16366 DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0), Flags);
16367 return IsNegative ? DAG.getNode(ISD::FNEG, SL, VT, Rsq, Flags) : Rsq;
16368 }
16369 }
16370 }
16371
16372 return SDValue();
16373}
16374
16375SDValue SITargetLowering::performFMulCombine(SDNode *N,
16376 DAGCombinerInfo &DCI) const {
16377 SelectionDAG &DAG = DCI.DAG;
16378 EVT VT = N->getValueType(0);
16379 EVT ScalarVT = VT.getScalarType();
16380 EVT IntVT = VT.changeElementType(MVT::i32);
16381
16382 if (!N->isDivergent() && getSubtarget()->hasSALUFloatInsts() &&
16383 (ScalarVT == MVT::f32 || ScalarVT == MVT::f16)) {
16384 // Prefer to use s_mul_f16/f32 instead of v_ldexp_f16/f32.
16385 return SDValue();
16386 }
16387
16388 SDValue LHS = N->getOperand(0);
16389 SDValue RHS = N->getOperand(1);
16390
16391 // It is cheaper to realize i32 inline constants as compared against
16392 // materializing f16 or f64 (or even non-inline f32) values,
16393 // possible via ldexp usage, as shown below :
16394 //
16395 // Given : A = 2^a & B = 2^b ; where a and b are integers.
16396 // fmul x, (select y, A, B) -> ldexp( x, (select i32 y, a, b) )
16397 // fmul x, (select y, -A, -B) -> ldexp( (fneg x), (select i32 y, a, b) )
16398 if ((ScalarVT == MVT::f64 || ScalarVT == MVT::f32 || ScalarVT == MVT::f16) &&
16399 (RHS.hasOneUse() && RHS.getOpcode() == ISD::SELECT)) {
16400 const ConstantFPSDNode *TrueNode = isConstOrConstSplatFP(RHS.getOperand(1));
16401 if (!TrueNode)
16402 return SDValue();
16403 const ConstantFPSDNode *FalseNode =
16404 isConstOrConstSplatFP(RHS.getOperand(2));
16405 if (!FalseNode)
16406 return SDValue();
16407
16408 if (TrueNode->isNegative() != FalseNode->isNegative())
16409 return SDValue();
16410
16411 // For f32, only non-inline constants should be transformed.
16412 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
16413 if (ScalarVT == MVT::f32 &&
16414 TII->isInlineConstant(TrueNode->getValueAPF()) &&
16415 TII->isInlineConstant(FalseNode->getValueAPF()))
16416 return SDValue();
16417
16418 int TrueNodeExpVal = TrueNode->getValueAPF().getExactLog2Abs();
16419 if (TrueNodeExpVal == INT_MIN)
16420 return SDValue();
16421 int FalseNodeExpVal = FalseNode->getValueAPF().getExactLog2Abs();
16422 if (FalseNodeExpVal == INT_MIN)
16423 return SDValue();
16424
16425 SDLoc SL(N);
16426 SDValue SelectNode =
16427 DAG.getNode(ISD::SELECT, SL, IntVT, RHS.getOperand(0),
16428 DAG.getSignedConstant(TrueNodeExpVal, SL, IntVT),
16429 DAG.getSignedConstant(FalseNodeExpVal, SL, IntVT));
16430
16431 LHS = TrueNode->isNegative()
16432 ? DAG.getNode(ISD::FNEG, SL, VT, LHS, LHS->getFlags())
16433 : LHS;
16434
16435 return DAG.getNode(ISD::FLDEXP, SL, VT, LHS, SelectNode, N->getFlags());
16436 }
16437
16438 return SDValue();
16439}
16440
16441SDValue SITargetLowering::performFMACombine(SDNode *N,
16442 DAGCombinerInfo &DCI) const {
16443 SelectionDAG &DAG = DCI.DAG;
16444 EVT VT = N->getValueType(0);
16445 SDLoc SL(N);
16446
16447 if (!Subtarget->hasDot10Insts() || VT != MVT::f32)
16448 return SDValue();
16449
16450 // FMA((F32)S0.x, (F32)S1. x, FMA((F32)S0.y, (F32)S1.y, (F32)z)) ->
16451 // FDOT2((V2F16)S0, (V2F16)S1, (F32)z))
16452 SDValue Op1 = N->getOperand(0);
16453 SDValue Op2 = N->getOperand(1);
16454 SDValue FMA = N->getOperand(2);
16455
16456 if (FMA.getOpcode() != ISD::FMA || Op1.getOpcode() != ISD::FP_EXTEND ||
16457 Op2.getOpcode() != ISD::FP_EXTEND)
16458 return SDValue();
16459
16460 // fdot2_f32_f16 always flushes fp32 denormal operand and output to zero,
16461 // regardless of the denorm mode setting. Therefore,
16462 // fp-contract is sufficient to allow generating fdot2.
16463 const TargetOptions &Options = DAG.getTarget().Options;
16464 if (Options.AllowFPOpFusion == FPOpFusion::Fast ||
16465 (N->getFlags().hasAllowContract() &&
16466 FMA->getFlags().hasAllowContract())) {
16467 Op1 = Op1.getOperand(0);
16468 Op2 = Op2.getOperand(0);
16469 if (Op1.getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
16471 return SDValue();
16472
16473 SDValue Vec1 = Op1.getOperand(0);
16474 SDValue Idx1 = Op1.getOperand(1);
16475 SDValue Vec2 = Op2.getOperand(0);
16476
16477 SDValue FMAOp1 = FMA.getOperand(0);
16478 SDValue FMAOp2 = FMA.getOperand(1);
16479 SDValue FMAAcc = FMA.getOperand(2);
16480
16481 if (FMAOp1.getOpcode() != ISD::FP_EXTEND ||
16482 FMAOp2.getOpcode() != ISD::FP_EXTEND)
16483 return SDValue();
16484
16485 FMAOp1 = FMAOp1.getOperand(0);
16486 FMAOp2 = FMAOp2.getOperand(0);
16487 if (FMAOp1.getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
16489 return SDValue();
16490
16491 SDValue Vec3 = FMAOp1.getOperand(0);
16492 SDValue Vec4 = FMAOp2.getOperand(0);
16493 SDValue Idx2 = FMAOp1.getOperand(1);
16494
16495 if (Idx1 != Op2.getOperand(1) || Idx2 != FMAOp2.getOperand(1) ||
16496 // Idx1 and Idx2 cannot be the same.
16497 Idx1 == Idx2)
16498 return SDValue();
16499
16500 if (Vec1 == Vec2 || Vec3 == Vec4)
16501 return SDValue();
16502
16503 if (Vec1.getValueType() != MVT::v2f16 || Vec2.getValueType() != MVT::v2f16)
16504 return SDValue();
16505
16506 if ((Vec1 == Vec3 && Vec2 == Vec4) || (Vec1 == Vec4 && Vec2 == Vec3)) {
16507 return DAG.getNode(AMDGPUISD::FDOT2, SL, MVT::f32, Vec1, Vec2, FMAAcc,
16508 DAG.getTargetConstant(0, SL, MVT::i1));
16509 }
16510 }
16511 return SDValue();
16512}
16513
16514SDValue SITargetLowering::performSetCCCombine(SDNode *N,
16515 DAGCombinerInfo &DCI) const {
16516 SelectionDAG &DAG = DCI.DAG;
16517 SDLoc SL(N);
16518
16519 SDValue LHS = N->getOperand(0);
16520 SDValue RHS = N->getOperand(1);
16521 EVT VT = LHS.getValueType();
16522 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
16523
16524 auto *CRHS = dyn_cast<ConstantSDNode>(RHS);
16525 if (!CRHS) {
16527 if (CRHS) {
16528 std::swap(LHS, RHS);
16529 CC = getSetCCSwappedOperands(CC);
16530 }
16531 }
16532
16533 if (CRHS) {
16534 if (VT == MVT::i32 && LHS.getOpcode() == ISD::SIGN_EXTEND &&
16535 isBoolSGPR(LHS.getOperand(0))) {
16536 // setcc (sext from i1 cc), -1, ne|sgt|ult) => not cc => xor cc, -1
16537 // setcc (sext from i1 cc), -1, eq|sle|uge) => cc
16538 // setcc (sext from i1 cc), 0, eq|sge|ule) => not cc => xor cc, -1
16539 // setcc (sext from i1 cc), 0, ne|ugt|slt) => cc
16540 if ((CRHS->isAllOnes() &&
16541 (CC == ISD::SETNE || CC == ISD::SETGT || CC == ISD::SETULT)) ||
16542 (CRHS->isZero() &&
16543 (CC == ISD::SETEQ || CC == ISD::SETGE || CC == ISD::SETULE)))
16544 return DAG.getNode(ISD::XOR, SL, MVT::i1, LHS.getOperand(0),
16545 DAG.getAllOnesConstant(SL, MVT::i1));
16546 if ((CRHS->isAllOnes() &&
16547 (CC == ISD::SETEQ || CC == ISD::SETLE || CC == ISD::SETUGE)) ||
16548 (CRHS->isZero() &&
16549 (CC == ISD::SETNE || CC == ISD::SETUGT || CC == ISD::SETLT)))
16550 return LHS.getOperand(0);
16551 }
16552
16553 const APInt &CRHSVal = CRHS->getAPIntValue();
16554 if ((CC == ISD::SETEQ || CC == ISD::SETNE) &&
16555 LHS.getOpcode() == ISD::SELECT &&
16556 isa<ConstantSDNode>(LHS.getOperand(1)) &&
16557 isa<ConstantSDNode>(LHS.getOperand(2)) &&
16558 LHS.getConstantOperandVal(1) != LHS.getConstantOperandVal(2) &&
16559 isBoolSGPR(LHS.getOperand(0))) {
16560 // Given CT != FT:
16561 // setcc (select cc, CT, CF), CF, eq => xor cc, -1
16562 // setcc (select cc, CT, CF), CF, ne => cc
16563 // setcc (select cc, CT, CF), CT, ne => xor cc, -1
16564 // setcc (select cc, CT, CF), CT, eq => cc
16565 const APInt &CT = LHS.getConstantOperandAPInt(1);
16566 const APInt &CF = LHS.getConstantOperandAPInt(2);
16567
16568 if ((CF == CRHSVal && CC == ISD::SETEQ) ||
16569 (CT == CRHSVal && CC == ISD::SETNE))
16570 return DAG.getNode(ISD::XOR, SL, MVT::i1, LHS.getOperand(0),
16571 DAG.getAllOnesConstant(SL, MVT::i1));
16572 if ((CF == CRHSVal && CC == ISD::SETNE) ||
16573 (CT == CRHSVal && CC == ISD::SETEQ))
16574 return LHS.getOperand(0);
16575 }
16576 }
16577
16578 // Eliminate setcc by using carryout from add/sub instruction
16579
16580 // LHS = ADD i64 RHS, Z LHSlo = UADDO i32 RHSlo, Zlo
16581 // setcc LHS ult RHS -> LHSHi = UADDO_CARRY i32 RHShi, Zhi
16582 // similarly for subtraction
16583
16584 // LHS = ADD i64 Y, 1 LHSlo = UADDO i32 Ylo, 1
16585 // setcc LHS eq 0 -> LHSHi = UADDO_CARRY i32 Yhi, 0
16586
16587 if (VT == MVT::i64 && ((CC == ISD::SETULT &&
16589 (CC == ISD::SETUGT &&
16591 (CC == ISD::SETEQ && CRHS && CRHS->isZero() &&
16592 sd_match(LHS, m_Add(m_Value(), m_One()))))) {
16593 bool IsAdd = LHS.getOpcode() == ISD::ADD;
16594
16595 SDValue Op0 = LHS.getOperand(0);
16596 SDValue Op1 = LHS.getOperand(1);
16597
16598 SDValue Op0Lo = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, Op0);
16599 SDValue Op1Lo = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, Op1);
16600
16601 SDValue Op0Hi = getHiHalf64(Op0, DAG);
16602 SDValue Op1Hi = getHiHalf64(Op1, DAG);
16603
16604 SDValue NodeLo =
16605 DAG.getNode(IsAdd ? ISD::UADDO : ISD::USUBO, SL,
16606 DAG.getVTList(MVT::i32, MVT::i1), {Op0Lo, Op1Lo});
16607
16608 SDValue CarryInHi = NodeLo.getValue(1);
16609 SDValue NodeHi = DAG.getNode(IsAdd ? ISD::UADDO_CARRY : ISD::USUBO_CARRY,
16610 SL, DAG.getVTList(MVT::i32, MVT::i1),
16611 {Op0Hi, Op1Hi, CarryInHi});
16612
16613 SDValue ResultLo = NodeLo.getValue(0);
16614 SDValue ResultHi = NodeHi.getValue(0);
16615
16616 SDValue JoinedResult =
16617 DAG.getBuildVector(MVT::v2i32, SL, {ResultLo, ResultHi});
16618
16619 SDValue Result = DAG.getNode(ISD::BITCAST, SL, VT, JoinedResult);
16620 SDValue Overflow = NodeHi.getValue(1);
16621 DCI.CombineTo(LHS.getNode(), Result);
16622 return Overflow;
16623 }
16624
16625 if (VT != MVT::f32 && VT != MVT::f64 &&
16626 (!Subtarget->has16BitInsts() || VT != MVT::f16))
16627 return SDValue();
16628
16629 // Match isinf/isfinite pattern
16630 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity))
16631 // (fcmp one (fabs x), inf) -> (fp_class x,
16632 // (p_normal | n_normal | p_subnormal | n_subnormal | p_zero | n_zero)
16633 if ((CC == ISD::SETOEQ || CC == ISD::SETONE) &&
16634 LHS.getOpcode() == ISD::FABS) {
16635 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
16636 if (!CRHS)
16637 return SDValue();
16638
16639 const APFloat &APF = CRHS->getValueAPF();
16640 if (APF.isInfinity() && !APF.isNegative()) {
16641 const unsigned IsInfMask =
16643 const unsigned IsFiniteMask =
16647 unsigned Mask = CC == ISD::SETOEQ ? IsInfMask : IsFiniteMask;
16648 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0),
16649 DAG.getConstant(Mask, SL, MVT::i32));
16650 }
16651 }
16652
16653 return SDValue();
16654}
16655
16656SDValue
16657SITargetLowering::performCvtF32UByteNCombine(SDNode *N,
16658 DAGCombinerInfo &DCI) const {
16659 SelectionDAG &DAG = DCI.DAG;
16660 SDLoc SL(N);
16661 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
16662
16663 SDValue Src = N->getOperand(0);
16664 SDValue Shift = N->getOperand(0);
16665
16666 // TODO: Extend type shouldn't matter (assuming legal types).
16667 if (Shift.getOpcode() == ISD::ZERO_EXTEND)
16668 Shift = Shift.getOperand(0);
16669
16670 if (Shift.getOpcode() == ISD::SRL || Shift.getOpcode() == ISD::SHL) {
16671 // cvt_f32_ubyte1 (shl x, 8) -> cvt_f32_ubyte0 x
16672 // cvt_f32_ubyte3 (shl x, 16) -> cvt_f32_ubyte1 x
16673 // cvt_f32_ubyte0 (srl x, 16) -> cvt_f32_ubyte2 x
16674 // cvt_f32_ubyte1 (srl x, 16) -> cvt_f32_ubyte3 x
16675 // cvt_f32_ubyte0 (srl x, 8) -> cvt_f32_ubyte1 x
16676 if (auto *C = dyn_cast<ConstantSDNode>(Shift.getOperand(1))) {
16677 SDValue Shifted = DAG.getZExtOrTrunc(
16678 Shift.getOperand(0), SDLoc(Shift.getOperand(0)), MVT::i32);
16679
16680 unsigned ShiftOffset = 8 * Offset;
16681 if (Shift.getOpcode() == ISD::SHL)
16682 ShiftOffset -= C->getZExtValue();
16683 else
16684 ShiftOffset += C->getZExtValue();
16685
16686 if (ShiftOffset < 32 && (ShiftOffset % 8) == 0) {
16687 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0 + ShiftOffset / 8, SL,
16688 MVT::f32, Shifted);
16689 }
16690 }
16691 }
16692
16693 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16694 APInt DemandedBits = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
16695 if (TLI.SimplifyDemandedBits(Src, DemandedBits, DCI)) {
16696 // We simplified Src. If this node is not dead, visit it again so it is
16697 // folded properly.
16698 if (N->getOpcode() != ISD::DELETED_NODE)
16699 DCI.AddToWorklist(N);
16700 return SDValue(N, 0);
16701 }
16702
16703 // Handle (or x, (srl y, 8)) pattern when known bits are zero.
16704 if (SDValue DemandedSrc =
16705 TLI.SimplifyMultipleUseDemandedBits(Src, DemandedBits, DAG))
16706 return DAG.getNode(N->getOpcode(), SL, MVT::f32, DemandedSrc);
16707
16708 return SDValue();
16709}
16710
16711SDValue SITargetLowering::performClampCombine(SDNode *N,
16712 DAGCombinerInfo &DCI) const {
16713 ConstantFPSDNode *CSrc = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
16714 if (!CSrc)
16715 return SDValue();
16716
16717 const MachineFunction &MF = DCI.DAG.getMachineFunction();
16718 const APFloat &F = CSrc->getValueAPF();
16719 APFloat Zero = APFloat::getZero(F.getSemantics());
16720 if (F < Zero ||
16721 (F.isNaN() && MF.getInfo<SIMachineFunctionInfo>()->getMode().DX10Clamp)) {
16722 return DCI.DAG.getConstantFP(Zero, SDLoc(N), N->getValueType(0));
16723 }
16724
16725 APFloat One(F.getSemantics(), "1.0");
16726 if (F > One)
16727 return DCI.DAG.getConstantFP(One, SDLoc(N), N->getValueType(0));
16728
16729 return SDValue(CSrc, 0);
16730}
16731
16732SDValue SITargetLowering::performSelectCombine(SDNode *N,
16733 DAGCombinerInfo &DCI) const {
16734
16735 // Try to fold CMP + SELECT patterns with shared constants (both FP and
16736 // integer).
16737 // Detect when CMP and SELECT use the same constant and fold them to avoid
16738 // loading the constant twice. Specifically handles patterns like:
16739 // %cmp = icmp eq i32 %val, 4242
16740 // %sel = select i1 %cmp, i32 4242, i32 %other
16741 // It can be optimized to reuse %val instead of 4242 in select.
16742 SDValue Cond = N->getOperand(0);
16743 SDValue TrueVal = N->getOperand(1);
16744 SDValue FalseVal = N->getOperand(2);
16745
16746 // Check if condition is a comparison.
16747 if (Cond.getOpcode() != ISD::SETCC)
16748 return SDValue();
16749
16750 SDValue LHS = Cond.getOperand(0);
16751 SDValue RHS = Cond.getOperand(1);
16752 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
16753
16754 bool isFloatingPoint = LHS.getValueType().isFloatingPoint();
16755 bool isInteger = LHS.getValueType().isInteger();
16756
16757 // Handle simple floating-point and integer types only.
16758 if (!isFloatingPoint && !isInteger)
16759 return SDValue();
16760
16761 bool isEquality = CC == (isFloatingPoint ? ISD::SETOEQ : ISD::SETEQ);
16762 bool isNonEquality = CC == (isFloatingPoint ? ISD::SETONE : ISD::SETNE);
16763 if (!isEquality && !isNonEquality)
16764 return SDValue();
16765
16766 SDValue ArgVal, ConstVal;
16767 if ((isFloatingPoint && isa<ConstantFPSDNode>(RHS)) ||
16768 (isInteger && isa<ConstantSDNode>(RHS))) {
16769 ConstVal = RHS;
16770 ArgVal = LHS;
16771 } else if ((isFloatingPoint && isa<ConstantFPSDNode>(LHS)) ||
16772 (isInteger && isa<ConstantSDNode>(LHS))) {
16773 ConstVal = LHS;
16774 ArgVal = RHS;
16775 } else {
16776 return SDValue();
16777 }
16778
16779 // Skip optimization for inlinable immediates.
16780 if (isFloatingPoint) {
16781 const APFloat &Val = cast<ConstantFPSDNode>(ConstVal)->getValueAPF();
16782 if (!Val.isNormal() || Subtarget->getInstrInfo()->isInlineConstant(Val))
16783 return SDValue();
16784 } else {
16786 cast<ConstantSDNode>(ConstVal)->getSExtValue()))
16787 return SDValue();
16788 }
16789
16790 // For equality and non-equality comparisons, patterns:
16791 // select (setcc x, const), const, y -> select (setcc x, const), x, y
16792 // select (setccinv x, const), y, const -> select (setccinv x, const), y, x
16793 if (!(isEquality && TrueVal == ConstVal) &&
16794 !(isNonEquality && FalseVal == ConstVal))
16795 return SDValue();
16796
16797 SDValue SelectLHS = (isEquality && TrueVal == ConstVal) ? ArgVal : TrueVal;
16798 SDValue SelectRHS =
16799 (isNonEquality && FalseVal == ConstVal) ? ArgVal : FalseVal;
16800 return DCI.DAG.getNode(ISD::SELECT, SDLoc(N), N->getValueType(0), Cond,
16801 SelectLHS, SelectRHS);
16802}
16803
16805 DAGCombinerInfo &DCI) const {
16806 switch (N->getOpcode()) {
16807 case ISD::ADD:
16808 case ISD::SUB:
16809 case ISD::SHL:
16810 case ISD::SRL:
16811 case ISD::SRA:
16812 case ISD::AND:
16813 case ISD::OR:
16814 case ISD::XOR:
16815 case ISD::MUL:
16816 case ISD::SETCC:
16817 case ISD::SELECT:
16818 case ISD::SMIN:
16819 case ISD::SMAX:
16820 case ISD::UMIN:
16821 case ISD::UMAX:
16822 if (auto Res = promoteUniformOpToI32(SDValue(N, 0), DCI))
16823 return Res;
16824 break;
16825 default:
16826 break;
16827 }
16828
16829 if (getTargetMachine().getOptLevel() == CodeGenOptLevel::None)
16830 return SDValue();
16831
16832 switch (N->getOpcode()) {
16833 case ISD::ADD:
16834 return performAddCombine(N, DCI);
16835 case ISD::PTRADD:
16836 return performPtrAddCombine(N, DCI);
16837 case ISD::SUB:
16838 return performSubCombine(N, DCI);
16839 case ISD::UADDO_CARRY:
16840 case ISD::USUBO_CARRY:
16841 return performAddCarrySubCarryCombine(N, DCI);
16842 case ISD::FADD:
16843 return performFAddCombine(N, DCI);
16844 case ISD::FSUB:
16845 return performFSubCombine(N, DCI);
16846 case ISD::FDIV:
16847 return performFDivCombine(N, DCI);
16848 case ISD::FMUL:
16849 return performFMulCombine(N, DCI);
16850 case ISD::SETCC:
16851 return performSetCCCombine(N, DCI);
16852 case ISD::SELECT:
16853 if (auto Res = performSelectCombine(N, DCI))
16854 return Res;
16855 break;
16856 case ISD::FMAXNUM:
16857 case ISD::FMINNUM:
16858 case ISD::FMAXNUM_IEEE:
16859 case ISD::FMINNUM_IEEE:
16860 case ISD::FMAXIMUM:
16861 case ISD::FMINIMUM:
16862 case ISD::FMAXIMUMNUM:
16863 case ISD::FMINIMUMNUM:
16864 case ISD::SMAX:
16865 case ISD::SMIN:
16866 case ISD::UMAX:
16867 case ISD::UMIN:
16870 return performMinMaxCombine(N, DCI);
16871 case ISD::FMA:
16872 return performFMACombine(N, DCI);
16873 case ISD::AND:
16874 return performAndCombine(N, DCI);
16875 case ISD::OR:
16876 return performOrCombine(N, DCI);
16877 case ISD::FSHR: {
16879 if (N->getValueType(0) == MVT::i32 && N->isDivergent() &&
16880 TII->pseudoToMCOpcode(AMDGPU::V_PERM_B32_e64) != -1) {
16881 return matchPERM(N, DCI);
16882 }
16883 break;
16884 }
16885 case ISD::XOR:
16886 return performXorCombine(N, DCI);
16887 case ISD::ZERO_EXTEND:
16888 return performZeroExtendCombine(N, DCI);
16890 return performSignExtendInRegCombine(N, DCI);
16892 return performClassCombine(N, DCI);
16893 case ISD::FCANONICALIZE:
16894 return performFCanonicalizeCombine(N, DCI);
16895 case AMDGPUISD::RCP:
16896 return performRcpCombine(N, DCI);
16897 case ISD::FLDEXP:
16898 case AMDGPUISD::FRACT:
16899 case AMDGPUISD::RSQ:
16902 case AMDGPUISD::RSQ_CLAMP: {
16903 // FIXME: This is probably wrong. If src is an sNaN, it won't be quieted
16904 SDValue Src = N->getOperand(0);
16905 if (Src.isUndef())
16906 return Src;
16907 break;
16908 }
16909 case ISD::SINT_TO_FP:
16910 case ISD::UINT_TO_FP:
16911 return performUCharToFloatCombine(N, DCI);
16912 case ISD::FCOPYSIGN:
16913 return performFCopySignCombine(N, DCI);
16918 return performCvtF32UByteNCombine(N, DCI);
16919 case AMDGPUISD::FMED3:
16920 return performFMed3Combine(N, DCI);
16922 return performCvtPkRTZCombine(N, DCI);
16923 case AMDGPUISD::CLAMP:
16924 return performClampCombine(N, DCI);
16925 case ISD::SCALAR_TO_VECTOR: {
16926 SelectionDAG &DAG = DCI.DAG;
16927 EVT VT = N->getValueType(0);
16928
16929 // v2i16 (scalar_to_vector i16:x) -> v2i16 (bitcast (any_extend i16:x))
16930 if (VT == MVT::v2i16 || VT == MVT::v2f16 || VT == MVT::v2bf16) {
16931 SDLoc SL(N);
16932 SDValue Src = N->getOperand(0);
16933 EVT EltVT = Src.getValueType();
16934 if (EltVT != MVT::i16)
16935 Src = DAG.getNode(ISD::BITCAST, SL, MVT::i16, Src);
16936
16937 SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SL, MVT::i32, Src);
16938 return DAG.getNode(ISD::BITCAST, SL, VT, Ext);
16939 }
16940
16941 break;
16942 }
16944 return performExtractVectorEltCombine(N, DCI);
16946 return performInsertVectorEltCombine(N, DCI);
16947 case ISD::FP_ROUND:
16948 return performFPRoundCombine(N, DCI);
16949 case ISD::LOAD: {
16950 if (SDValue Widened = widenLoad(cast<LoadSDNode>(N), DCI))
16951 return Widened;
16952 [[fallthrough]];
16953 }
16954 default: {
16955 if (!DCI.isBeforeLegalize()) {
16956 if (MemSDNode *MemNode = dyn_cast<MemSDNode>(N))
16957 return performMemSDNodeCombine(MemNode, DCI);
16958 }
16959
16960 break;
16961 }
16962 }
16963
16965}
16966
16967/// Helper function for adjustWritemask
16968static unsigned SubIdx2Lane(unsigned Idx) {
16969 switch (Idx) {
16970 default:
16971 return ~0u;
16972 case AMDGPU::sub0:
16973 return 0;
16974 case AMDGPU::sub1:
16975 return 1;
16976 case AMDGPU::sub2:
16977 return 2;
16978 case AMDGPU::sub3:
16979 return 3;
16980 case AMDGPU::sub4:
16981 return 4; // Possible with TFE/LWE
16982 }
16983}
16984
16985/// Adjust the writemask of MIMG, VIMAGE or VSAMPLE instructions
16986SDNode *SITargetLowering::adjustWritemask(MachineSDNode *&Node,
16987 SelectionDAG &DAG) const {
16988 unsigned Opcode = Node->getMachineOpcode();
16989
16990 // Subtract 1 because the vdata output is not a MachineSDNode operand.
16991 int D16Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::d16) - 1;
16992 if (D16Idx >= 0 && Node->getConstantOperandVal(D16Idx))
16993 return Node; // not implemented for D16
16994
16995 SDNode *Users[5] = {nullptr};
16996 unsigned Lane = 0;
16997 unsigned DmaskIdx =
16998 AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::dmask) - 1;
16999 unsigned OldDmask = Node->getConstantOperandVal(DmaskIdx);
17000 unsigned NewDmask = 0;
17001 unsigned TFEIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::tfe) - 1;
17002 unsigned LWEIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::lwe) - 1;
17003 bool UsesTFC = (int(TFEIdx) >= 0 && Node->getConstantOperandVal(TFEIdx)) ||
17004 (int(LWEIdx) >= 0 && Node->getConstantOperandVal(LWEIdx));
17005 unsigned TFCLane = 0;
17006 bool HasChain = Node->getNumValues() > 1;
17007
17008 if (OldDmask == 0) {
17009 // These are folded out, but on the chance it happens don't assert.
17010 return Node;
17011 }
17012
17013 unsigned OldBitsSet = llvm::popcount(OldDmask);
17014 // Work out which is the TFE/LWE lane if that is enabled.
17015 if (UsesTFC) {
17016 TFCLane = OldBitsSet;
17017 }
17018
17019 // Try to figure out the used register components
17020 for (SDUse &Use : Node->uses()) {
17021
17022 // Don't look at users of the chain.
17023 if (Use.getResNo() != 0)
17024 continue;
17025
17026 SDNode *User = Use.getUser();
17027
17028 // Abort if we can't understand the usage
17029 if (!User->isMachineOpcode() ||
17030 User->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
17031 return Node;
17032
17033 // Lane means which subreg of %vgpra_vgprb_vgprc_vgprd is used.
17034 // Note that subregs are packed, i.e. Lane==0 is the first bit set
17035 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
17036 // set, etc.
17037 Lane = SubIdx2Lane(User->getConstantOperandVal(1));
17038 if (Lane == ~0u)
17039 return Node;
17040
17041 // Check if the use is for the TFE/LWE generated result at VGPRn+1.
17042 if (UsesTFC && Lane == TFCLane) {
17043 Users[Lane] = User;
17044 } else {
17045 // Set which texture component corresponds to the lane.
17046 unsigned Comp;
17047 for (unsigned i = 0, Dmask = OldDmask; (i <= Lane) && (Dmask != 0); i++) {
17048 Comp = llvm::countr_zero(Dmask);
17049 Dmask &= ~(1 << Comp);
17050 }
17051
17052 // Abort if we have more than one user per component.
17053 if (Users[Lane])
17054 return Node;
17055
17056 Users[Lane] = User;
17057 NewDmask |= 1 << Comp;
17058 }
17059 }
17060
17061 // Don't allow 0 dmask, as hardware assumes one channel enabled.
17062 bool NoChannels = !NewDmask;
17063 if (NoChannels) {
17064 if (!UsesTFC) {
17065 // No uses of the result and not using TFC. Then do nothing.
17066 return Node;
17067 }
17068 // If the original dmask has one channel - then nothing to do
17069 if (OldBitsSet == 1)
17070 return Node;
17071 // Use an arbitrary dmask - required for the instruction to work
17072 NewDmask = 1;
17073 }
17074 // Abort if there's no change
17075 if (NewDmask == OldDmask)
17076 return Node;
17077
17078 unsigned BitsSet = llvm::popcount(NewDmask);
17079
17080 // Check for TFE or LWE - increase the number of channels by one to account
17081 // for the extra return value
17082 // This will need adjustment for D16 if this is also included in
17083 // adjustWriteMask (this function) but at present D16 are excluded.
17084 unsigned NewChannels = BitsSet + UsesTFC;
17085
17086 int NewOpcode =
17087 AMDGPU::getMaskedMIMGOp(Node->getMachineOpcode(), NewChannels);
17088 assert(NewOpcode != -1 &&
17089 NewOpcode != static_cast<int>(Node->getMachineOpcode()) &&
17090 "failed to find equivalent MIMG op");
17091
17092 // Adjust the writemask in the node
17094 llvm::append_range(Ops, Node->ops().take_front(DmaskIdx));
17095 Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32));
17096 llvm::append_range(Ops, Node->ops().drop_front(DmaskIdx + 1));
17097
17098 MVT SVT = Node->getValueType(0).getVectorElementType().getSimpleVT();
17099
17100 MVT ResultVT = NewChannels == 1
17101 ? SVT
17102 : MVT::getVectorVT(SVT, NewChannels == 3 ? 4
17103 : NewChannels == 5 ? 8
17104 : NewChannels);
17105 SDVTList NewVTList =
17106 HasChain ? DAG.getVTList(ResultVT, MVT::Other) : DAG.getVTList(ResultVT);
17107
17108 MachineSDNode *NewNode =
17109 DAG.getMachineNode(NewOpcode, SDLoc(Node), NewVTList, Ops);
17110
17111 if (HasChain) {
17112 // Update chain.
17113 DAG.setNodeMemRefs(NewNode, Node->memoperands());
17114 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(NewNode, 1));
17115 }
17116
17117 if (NewChannels == 1) {
17118 assert(Node->hasNUsesOfValue(1, 0));
17119 SDNode *Copy =
17120 DAG.getMachineNode(TargetOpcode::COPY, SDLoc(Node),
17121 Users[Lane]->getValueType(0), SDValue(NewNode, 0));
17122 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
17123 return nullptr;
17124 }
17125
17126 // Update the users of the node with the new indices
17127 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 5; ++i) {
17128 SDNode *User = Users[i];
17129 if (!User) {
17130 // Handle the special case of NoChannels. We set NewDmask to 1 above, but
17131 // Users[0] is still nullptr because channel 0 doesn't really have a use.
17132 if (i || !NoChannels)
17133 continue;
17134 } else {
17135 SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32);
17136 SDNode *NewUser = DAG.UpdateNodeOperands(User, SDValue(NewNode, 0), Op);
17137 if (NewUser != User) {
17138 DAG.ReplaceAllUsesWith(SDValue(User, 0), SDValue(NewUser, 0));
17139 DAG.RemoveDeadNode(User);
17140 }
17141 }
17142
17143 switch (Idx) {
17144 default:
17145 break;
17146 case AMDGPU::sub0:
17147 Idx = AMDGPU::sub1;
17148 break;
17149 case AMDGPU::sub1:
17150 Idx = AMDGPU::sub2;
17151 break;
17152 case AMDGPU::sub2:
17153 Idx = AMDGPU::sub3;
17154 break;
17155 case AMDGPU::sub3:
17156 Idx = AMDGPU::sub4;
17157 break;
17158 }
17159 }
17160
17161 DAG.RemoveDeadNode(Node);
17162 return nullptr;
17163}
17164
17166 if (Op.getOpcode() == ISD::AssertZext)
17167 Op = Op.getOperand(0);
17168
17169 return isa<FrameIndexSDNode>(Op);
17170}
17171
17172/// Legalize target independent instructions (e.g. INSERT_SUBREG)
17173/// with frame index operands.
17174/// LLVM assumes that inputs are to these instructions are registers.
17175SDNode *
17177 SelectionDAG &DAG) const {
17178 if (Node->getOpcode() == ISD::CopyToReg) {
17179 RegisterSDNode *DestReg = cast<RegisterSDNode>(Node->getOperand(1));
17180 SDValue SrcVal = Node->getOperand(2);
17181
17182 // Insert a copy to a VReg_1 virtual register so LowerI1Copies doesn't have
17183 // to try understanding copies to physical registers.
17184 if (SrcVal.getValueType() == MVT::i1 && DestReg->getReg().isPhysical()) {
17185 SDLoc SL(Node);
17187 SDValue VReg = DAG.getRegister(
17188 MRI.createVirtualRegister(&AMDGPU::VReg_1RegClass), MVT::i1);
17189
17190 SDNode *Glued = Node->getGluedNode();
17191 SDValue ToVReg = DAG.getCopyToReg(
17192 Node->getOperand(0), SL, VReg, SrcVal,
17193 SDValue(Glued, Glued ? Glued->getNumValues() - 1 : 0));
17194 SDValue ToResultReg = DAG.getCopyToReg(ToVReg, SL, SDValue(DestReg, 0),
17195 VReg, ToVReg.getValue(1));
17196 DAG.ReplaceAllUsesWith(Node, ToResultReg.getNode());
17197 DAG.RemoveDeadNode(Node);
17198 return ToResultReg.getNode();
17199 }
17200 }
17201
17203 for (unsigned i = 0; i < Node->getNumOperands(); ++i) {
17204 if (!isFrameIndexOp(Node->getOperand(i))) {
17205 Ops.push_back(Node->getOperand(i));
17206 continue;
17207 }
17208
17209 SDLoc DL(Node);
17210 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL,
17211 Node->getOperand(i).getValueType(),
17212 Node->getOperand(i)),
17213 0));
17214 }
17215
17216 return DAG.UpdateNodeOperands(Node, Ops);
17217}
17218
17219/// Fold the instructions after selecting them.
17220/// Returns null if users were already updated.
17222 SelectionDAG &DAG) const {
17224 unsigned Opcode = Node->getMachineOpcode();
17225
17226 if (TII->isImage(Opcode) && !TII->get(Opcode).mayStore() &&
17227 !TII->isGather4(Opcode) &&
17228 AMDGPU::hasNamedOperand(Opcode, AMDGPU::OpName::dmask)) {
17229 return adjustWritemask(Node, DAG);
17230 }
17231
17232 if (Opcode == AMDGPU::INSERT_SUBREG || Opcode == AMDGPU::REG_SEQUENCE) {
17234 return Node;
17235 }
17236
17237 switch (Opcode) {
17238 case AMDGPU::V_DIV_SCALE_F32_e64:
17239 case AMDGPU::V_DIV_SCALE_F64_e64: {
17240 // Satisfy the operand register constraint when one of the inputs is
17241 // undefined. Ordinarily each undef value will have its own implicit_def of
17242 // a vreg, so force these to use a single register.
17243 SDValue Src0 = Node->getOperand(1);
17244 SDValue Src1 = Node->getOperand(3);
17245 SDValue Src2 = Node->getOperand(5);
17246
17247 if ((Src0.isMachineOpcode() &&
17248 Src0.getMachineOpcode() != AMDGPU::IMPLICIT_DEF) &&
17249 (Src0 == Src1 || Src0 == Src2))
17250 break;
17251
17252 MVT VT = Src0.getValueType().getSimpleVT();
17253 const TargetRegisterClass *RC =
17254 getRegClassFor(VT, Src0.getNode()->isDivergent());
17255
17257 SDValue UndefReg = DAG.getRegister(MRI.createVirtualRegister(RC), VT);
17258
17259 SDValue ImpDef = DAG.getCopyToReg(DAG.getEntryNode(), SDLoc(Node), UndefReg,
17260 Src0, SDValue());
17261
17262 // src0 must be the same register as src1 or src2, even if the value is
17263 // undefined, so make sure we don't violate this constraint.
17264 if (Src0.isMachineOpcode() &&
17265 Src0.getMachineOpcode() == AMDGPU::IMPLICIT_DEF) {
17266 if (Src1.isMachineOpcode() &&
17267 Src1.getMachineOpcode() != AMDGPU::IMPLICIT_DEF)
17268 Src0 = Src1;
17269 else if (Src2.isMachineOpcode() &&
17270 Src2.getMachineOpcode() != AMDGPU::IMPLICIT_DEF)
17271 Src0 = Src2;
17272 else {
17273 assert(Src1.getMachineOpcode() == AMDGPU::IMPLICIT_DEF);
17274 Src0 = UndefReg;
17275 Src1 = UndefReg;
17276 }
17277 } else
17278 break;
17279
17281 Ops[1] = Src0;
17282 Ops[3] = Src1;
17283 Ops[5] = Src2;
17284 Ops.push_back(ImpDef.getValue(1));
17285 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
17286 }
17287 default:
17288 break;
17289 }
17290
17291 return Node;
17292}
17293
17294// Any MIMG instructions that use tfe or lwe require an initialization of the
17295// result register that will be written in the case of a memory access failure.
17296// The required code is also added to tie this init code to the result of the
17297// img instruction.
17300 const SIRegisterInfo &TRI = TII->getRegisterInfo();
17301 MachineRegisterInfo &MRI = MI.getMF()->getRegInfo();
17302 MachineBasicBlock &MBB = *MI.getParent();
17303
17304 int DstIdx =
17305 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::vdata);
17306 unsigned InitIdx = 0;
17307
17308 if (TII->isImage(MI)) {
17309 MachineOperand *TFE = TII->getNamedOperand(MI, AMDGPU::OpName::tfe);
17310 MachineOperand *LWE = TII->getNamedOperand(MI, AMDGPU::OpName::lwe);
17311 MachineOperand *D16 = TII->getNamedOperand(MI, AMDGPU::OpName::d16);
17312
17313 if (!TFE && !LWE) // intersect_ray
17314 return;
17315
17316 unsigned TFEVal = TFE ? TFE->getImm() : 0;
17317 unsigned LWEVal = LWE ? LWE->getImm() : 0;
17318 unsigned D16Val = D16 ? D16->getImm() : 0;
17319
17320 if (!TFEVal && !LWEVal)
17321 return;
17322
17323 // At least one of TFE or LWE are non-zero
17324 // We have to insert a suitable initialization of the result value and
17325 // tie this to the dest of the image instruction.
17326
17327 // Calculate which dword we have to initialize to 0.
17328 MachineOperand *MO_Dmask = TII->getNamedOperand(MI, AMDGPU::OpName::dmask);
17329
17330 // check that dmask operand is found.
17331 assert(MO_Dmask && "Expected dmask operand in instruction");
17332
17333 unsigned dmask = MO_Dmask->getImm();
17334 // Determine the number of active lanes taking into account the
17335 // Gather4 special case
17336 unsigned ActiveLanes = TII->isGather4(MI) ? 4 : llvm::popcount(dmask);
17337
17338 bool Packed = !Subtarget->hasUnpackedD16VMem();
17339
17340 InitIdx = D16Val && Packed ? ((ActiveLanes + 1) >> 1) + 1 : ActiveLanes + 1;
17341
17342 // Abandon attempt if the dst size isn't large enough
17343 // - this is in fact an error but this is picked up elsewhere and
17344 // reported correctly.
17345 uint32_t DstSize =
17346 TRI.getRegSizeInBits(*TII->getOpRegClass(MI, DstIdx)) / 32;
17347 if (DstSize < InitIdx)
17348 return;
17349 } else if (TII->isMUBUF(MI) && AMDGPU::getMUBUFTfe(MI.getOpcode())) {
17350 InitIdx = TRI.getRegSizeInBits(*TII->getOpRegClass(MI, DstIdx)) / 32;
17351 } else {
17352 return;
17353 }
17354
17355 const DebugLoc &DL = MI.getDebugLoc();
17356
17357 // Create a register for the initialization value.
17358 Register PrevDst = MRI.cloneVirtualRegister(MI.getOperand(DstIdx).getReg());
17359 unsigned NewDst = 0; // Final initialized value will be in here
17360
17361 // If PRTStrictNull feature is enabled (the default) then initialize
17362 // all the result registers to 0, otherwise just the error indication
17363 // register (VGPRn+1)
17364 unsigned SizeLeft = Subtarget->usePRTStrictNull() ? InitIdx : 1;
17365 unsigned CurrIdx = Subtarget->usePRTStrictNull() ? 0 : (InitIdx - 1);
17366
17367 BuildMI(MBB, MI, DL, TII->get(AMDGPU::IMPLICIT_DEF), PrevDst);
17368 for (; SizeLeft; SizeLeft--, CurrIdx++) {
17369 NewDst = MRI.createVirtualRegister(TII->getOpRegClass(MI, DstIdx));
17370 // Initialize dword
17371 Register SubReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
17372 // clang-format off
17373 BuildMI(MBB, MI, DL, TII->get(AMDGPU::V_MOV_B32_e32), SubReg)
17374 .addImm(0);
17375 // clang-format on
17376 // Insert into the super-reg
17377 BuildMI(MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewDst)
17378 .addReg(PrevDst)
17379 .addReg(SubReg)
17381
17382 PrevDst = NewDst;
17383 }
17384
17385 // Add as an implicit operand
17386 MI.addOperand(MachineOperand::CreateReg(NewDst, false, true));
17387
17388 // Tie the just added implicit operand to the dst
17389 MI.tieOperands(DstIdx, MI.getNumOperands() - 1);
17390}
17391
17392/// Assign the register class depending on the number of
17393/// bits set in the writemask
17395 SDNode *Node) const {
17397
17398 MachineFunction *MF = MI.getParent()->getParent();
17400
17401 if (TII->isVOP3(MI.getOpcode())) {
17402 // Make sure constant bus requirements are respected.
17403 TII->legalizeOperandsVOP3(MRI, MI);
17404
17405 if (TII->isMAI(MI)) {
17406 // The ordinary src0, src1, src2 were legalized above.
17407 //
17408 // We have to also legalize the appended v_mfma_ld_scale_b32 operands,
17409 // as a separate instruction.
17410 int Src0Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
17411 AMDGPU::OpName::scale_src0);
17412 if (Src0Idx != -1) {
17413 int Src1Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
17414 AMDGPU::OpName::scale_src1);
17415 if (TII->usesConstantBus(MRI, MI, Src0Idx) &&
17416 TII->usesConstantBus(MRI, MI, Src1Idx))
17417 TII->legalizeOpWithMove(MI, Src1Idx);
17418 }
17419 }
17420
17421 return;
17422 }
17423
17424 if (TII->isImage(MI))
17425 TII->enforceOperandRCAlignment(MI, AMDGPU::OpName::vaddr);
17426}
17427
17429 uint64_t Val) {
17430 SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32);
17431 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0);
17432}
17433
17435 const SDLoc &DL,
17436 SDValue Ptr) const {
17438
17439 // Build the half of the subregister with the constants before building the
17440 // full 128-bit register. If we are building multiple resource descriptors,
17441 // this will allow CSEing of the 2-component register.
17442 const SDValue Ops0[] = {
17443 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32),
17444 buildSMovImm32(DAG, DL, 0),
17445 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
17446 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32),
17447 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32)};
17448
17449 SDValue SubRegHi = SDValue(
17450 DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v2i32, Ops0), 0);
17451
17452 // Combine the constants and the pointer.
17453 const SDValue Ops1[] = {
17454 DAG.getTargetConstant(AMDGPU::SGPR_128RegClassID, DL, MVT::i32), Ptr,
17455 DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32), SubRegHi,
17456 DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32)};
17457
17458 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1);
17459}
17460
17461/// Return a resource descriptor with the 'Add TID' bit enabled
17462/// The TID (Thread ID) is multiplied by the stride value (bits [61:48]
17463/// of the resource descriptor) to create an offset, which is added to
17464/// the resource pointer.
17466 SDValue Ptr, uint32_t RsrcDword1,
17467 uint64_t RsrcDword2And3) const {
17468 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
17469 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
17470 if (RsrcDword1) {
17471 PtrHi =
17472 SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi,
17473 DAG.getConstant(RsrcDword1, DL, MVT::i32)),
17474 0);
17475 }
17476
17477 SDValue DataLo =
17478 buildSMovImm32(DAG, DL, RsrcDword2And3 & UINT64_C(0xFFFFFFFF));
17479 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32);
17480
17481 const SDValue Ops[] = {
17482 DAG.getTargetConstant(AMDGPU::SGPR_128RegClassID, DL, MVT::i32),
17483 PtrLo,
17484 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
17485 PtrHi,
17486 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32),
17487 DataLo,
17488 DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32),
17489 DataHi,
17490 DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32)};
17491
17492 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
17493}
17494
17495//===----------------------------------------------------------------------===//
17496// SI Inline Assembly Support
17497//===----------------------------------------------------------------------===//
17498
17499std::pair<unsigned, const TargetRegisterClass *>
17501 StringRef Constraint,
17502 MVT VT) const {
17503 const SIRegisterInfo *TRI = static_cast<const SIRegisterInfo *>(TRI_);
17504
17505 const TargetRegisterClass *RC = nullptr;
17506 if (Constraint.size() == 1) {
17507 // Check if we cannot determine the bit size of the given value type. This
17508 // can happen, for example, in this situation where we have an empty struct
17509 // (size 0): `call void asm "", "v"({} poison)`-
17510 if (VT == MVT::Other)
17511 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
17512 const unsigned BitWidth = VT.getSizeInBits();
17513 switch (Constraint[0]) {
17514 default:
17515 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
17516 case 's':
17517 case 'r':
17518 switch (BitWidth) {
17519 case 16:
17520 RC = &AMDGPU::SReg_32RegClass;
17521 break;
17522 case 64:
17523 RC = &AMDGPU::SGPR_64RegClass;
17524 break;
17525 default:
17527 if (!RC)
17528 return std::pair(0U, nullptr);
17529 break;
17530 }
17531 break;
17532 case 'v':
17533 switch (BitWidth) {
17534 case 16:
17535 RC = Subtarget->useRealTrue16Insts() ? &AMDGPU::VGPR_16RegClass
17536 : &AMDGPU::VGPR_32_Lo256RegClass;
17537 break;
17538 default:
17539 RC = Subtarget->has1024AddressableVGPRs()
17540 ? TRI->getAlignedLo256VGPRClassForBitWidth(BitWidth)
17541 : TRI->getVGPRClassForBitWidth(BitWidth);
17542 if (!RC)
17543 return std::pair(0U, nullptr);
17544 break;
17545 }
17546 break;
17547 case 'a':
17548 if (!Subtarget->hasMAIInsts())
17549 break;
17550 switch (BitWidth) {
17551 case 16:
17552 RC = &AMDGPU::AGPR_32RegClass;
17553 break;
17554 default:
17555 RC = TRI->getAGPRClassForBitWidth(BitWidth);
17556 if (!RC)
17557 return std::pair(0U, nullptr);
17558 break;
17559 }
17560 break;
17561 }
17562 } else if (Constraint == "VA" && Subtarget->hasGFX90AInsts()) {
17563 const unsigned BitWidth = VT.getSizeInBits();
17564 switch (BitWidth) {
17565 case 16:
17566 RC = &AMDGPU::AV_32RegClass;
17567 break;
17568 default:
17569 RC = TRI->getVectorSuperClassForBitWidth(BitWidth);
17570 if (!RC)
17571 return std::pair(0U, nullptr);
17572 break;
17573 }
17574 }
17575
17576 // We actually support i128, i16 and f16 as inline parameters
17577 // even if they are not reported as legal
17578 if (RC && (isTypeLegal(VT) || VT.SimpleTy == MVT::i128 ||
17579 VT.SimpleTy == MVT::i16 || VT.SimpleTy == MVT::f16))
17580 return std::pair(0U, RC);
17581
17582 auto [Kind, Idx, NumRegs] = AMDGPU::parseAsmConstraintPhysReg(Constraint);
17583 if (Kind != '\0') {
17584 if (Kind == 'v') {
17585 RC = &AMDGPU::VGPR_32_Lo256RegClass;
17586 } else if (Kind == 's') {
17587 RC = &AMDGPU::SGPR_32RegClass;
17588 } else if (Kind == 'a') {
17589 RC = &AMDGPU::AGPR_32RegClass;
17590 }
17591
17592 if (RC) {
17593 if (NumRegs > 1) {
17594 if (Idx >= RC->getNumRegs() || Idx + NumRegs - 1 >= RC->getNumRegs())
17595 return std::pair(0U, nullptr);
17596
17597 uint32_t Width = NumRegs * 32;
17598 // Prohibit constraints for register ranges with a width that does not
17599 // match the required type.
17600 if (VT.SimpleTy != MVT::Other && Width != VT.getSizeInBits())
17601 return std::pair(0U, nullptr);
17602
17603 MCRegister Reg = RC->getRegister(Idx);
17605 RC = TRI->getVGPRClassForBitWidth(Width);
17606 else if (SIRegisterInfo::isSGPRClass(RC))
17607 RC = TRI->getSGPRClassForBitWidth(Width);
17608 else if (SIRegisterInfo::isAGPRClass(RC))
17609 RC = TRI->getAGPRClassForBitWidth(Width);
17610 if (RC) {
17611 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0, RC);
17612 if (!Reg) {
17613 // The register class does not contain the requested register,
17614 // e.g., because it is an SGPR pair that would violate alignment
17615 // requirements.
17616 return std::pair(0U, nullptr);
17617 }
17618 return std::pair(Reg, RC);
17619 }
17620 }
17621
17622 // Check for lossy scalar/vector conversions.
17623 if (VT.isVector() && VT.getSizeInBits() != 32)
17624 return std::pair(0U, nullptr);
17625 if (Idx < RC->getNumRegs())
17626 return std::pair(RC->getRegister(Idx), RC);
17627 return std::pair(0U, nullptr);
17628 }
17629 }
17630
17631 auto Ret = TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
17632 if (Ret.first)
17633 Ret.second = TRI->getPhysRegBaseClass(Ret.first);
17634
17635 return Ret;
17636}
17637
17638static bool isImmConstraint(StringRef Constraint) {
17639 if (Constraint.size() == 1) {
17640 switch (Constraint[0]) {
17641 default:
17642 break;
17643 case 'I':
17644 case 'J':
17645 case 'A':
17646 case 'B':
17647 case 'C':
17648 return true;
17649 }
17650 } else if (Constraint == "DA" || Constraint == "DB") {
17651 return true;
17652 }
17653 return false;
17654}
17655
17658 if (Constraint.size() == 1) {
17659 switch (Constraint[0]) {
17660 default:
17661 break;
17662 case 's':
17663 case 'v':
17664 case 'a':
17665 return C_RegisterClass;
17666 }
17667 } else if (Constraint.size() == 2) {
17668 if (Constraint == "VA")
17669 return C_RegisterClass;
17670 }
17671 if (isImmConstraint(Constraint)) {
17672 return C_Other;
17673 }
17674 return TargetLowering::getConstraintType(Constraint);
17675}
17676
17677static uint64_t clearUnusedBits(uint64_t Val, unsigned Size) {
17679 Val = Val & maskTrailingOnes<uint64_t>(Size);
17680 }
17681 return Val;
17682}
17683
17685 StringRef Constraint,
17686 std::vector<SDValue> &Ops,
17687 SelectionDAG &DAG) const {
17688 if (isImmConstraint(Constraint)) {
17689 uint64_t Val;
17690 if (getAsmOperandConstVal(Op, Val) &&
17691 checkAsmConstraintVal(Op, Constraint, Val)) {
17692 Val = clearUnusedBits(Val, Op.getScalarValueSizeInBits());
17693 Ops.push_back(DAG.getTargetConstant(Val, SDLoc(Op), MVT::i64));
17694 }
17695 } else {
17697 }
17698}
17699
17701 unsigned Size = Op.getScalarValueSizeInBits();
17702 if (Size > 64)
17703 return false;
17704
17705 if (Size == 16 && !Subtarget->has16BitInsts())
17706 return false;
17707
17709 Val = C->getSExtValue();
17710 return true;
17711 }
17713 Val = C->getValueAPF().bitcastToAPInt().getSExtValue();
17714 return true;
17715 }
17717 if (Size != 16 || Op.getNumOperands() != 2)
17718 return false;
17719 if (Op.getOperand(0).isUndef() || Op.getOperand(1).isUndef())
17720 return false;
17721 if (ConstantSDNode *C = V->getConstantSplatNode()) {
17722 Val = C->getSExtValue();
17723 return true;
17724 }
17725 if (ConstantFPSDNode *C = V->getConstantFPSplatNode()) {
17726 Val = C->getValueAPF().bitcastToAPInt().getSExtValue();
17727 return true;
17728 }
17729 }
17730
17731 return false;
17732}
17733
17735 uint64_t Val) const {
17736 if (Constraint.size() == 1) {
17737 switch (Constraint[0]) {
17738 case 'I':
17740 case 'J':
17741 return isInt<16>(Val);
17742 case 'A':
17743 return checkAsmConstraintValA(Op, Val);
17744 case 'B':
17745 return isInt<32>(Val);
17746 case 'C':
17747 return isUInt<32>(clearUnusedBits(Val, Op.getScalarValueSizeInBits())) ||
17749 default:
17750 break;
17751 }
17752 } else if (Constraint.size() == 2) {
17753 if (Constraint == "DA") {
17754 int64_t HiBits = static_cast<int32_t>(Val >> 32);
17755 int64_t LoBits = static_cast<int32_t>(Val);
17756 return checkAsmConstraintValA(Op, HiBits, 32) &&
17757 checkAsmConstraintValA(Op, LoBits, 32);
17758 }
17759 if (Constraint == "DB") {
17760 return true;
17761 }
17762 }
17763 llvm_unreachable("Invalid asm constraint");
17764}
17765
17767 unsigned MaxSize) const {
17768 unsigned Size = std::min<unsigned>(Op.getScalarValueSizeInBits(), MaxSize);
17769 bool HasInv2Pi = Subtarget->hasInv2PiInlineImm();
17770 if (Size == 16) {
17771 MVT VT = Op.getSimpleValueType();
17772 switch (VT.SimpleTy) {
17773 default:
17774 return false;
17775 case MVT::i16:
17776 return AMDGPU::isInlinableLiteralI16(Val, HasInv2Pi);
17777 case MVT::f16:
17778 return AMDGPU::isInlinableLiteralFP16(Val, HasInv2Pi);
17779 case MVT::bf16:
17780 return AMDGPU::isInlinableLiteralBF16(Val, HasInv2Pi);
17781 case MVT::v2i16:
17782 return AMDGPU::getInlineEncodingV2I16(Val).has_value();
17783 case MVT::v2f16:
17784 return AMDGPU::getInlineEncodingV2F16(Val).has_value();
17785 case MVT::v2bf16:
17786 return AMDGPU::getInlineEncodingV2BF16(Val).has_value();
17787 }
17788 }
17789 if ((Size == 32 && AMDGPU::isInlinableLiteral32(Val, HasInv2Pi)) ||
17790 (Size == 64 && AMDGPU::isInlinableLiteral64(Val, HasInv2Pi)))
17791 return true;
17792 return false;
17793}
17794
17795static int getAlignedAGPRClassID(unsigned UnalignedClassID) {
17796 switch (UnalignedClassID) {
17797 case AMDGPU::VReg_64RegClassID:
17798 return AMDGPU::VReg_64_Align2RegClassID;
17799 case AMDGPU::VReg_96RegClassID:
17800 return AMDGPU::VReg_96_Align2RegClassID;
17801 case AMDGPU::VReg_128RegClassID:
17802 return AMDGPU::VReg_128_Align2RegClassID;
17803 case AMDGPU::VReg_160RegClassID:
17804 return AMDGPU::VReg_160_Align2RegClassID;
17805 case AMDGPU::VReg_192RegClassID:
17806 return AMDGPU::VReg_192_Align2RegClassID;
17807 case AMDGPU::VReg_224RegClassID:
17808 return AMDGPU::VReg_224_Align2RegClassID;
17809 case AMDGPU::VReg_256RegClassID:
17810 return AMDGPU::VReg_256_Align2RegClassID;
17811 case AMDGPU::VReg_288RegClassID:
17812 return AMDGPU::VReg_288_Align2RegClassID;
17813 case AMDGPU::VReg_320RegClassID:
17814 return AMDGPU::VReg_320_Align2RegClassID;
17815 case AMDGPU::VReg_352RegClassID:
17816 return AMDGPU::VReg_352_Align2RegClassID;
17817 case AMDGPU::VReg_384RegClassID:
17818 return AMDGPU::VReg_384_Align2RegClassID;
17819 case AMDGPU::VReg_512RegClassID:
17820 return AMDGPU::VReg_512_Align2RegClassID;
17821 case AMDGPU::VReg_1024RegClassID:
17822 return AMDGPU::VReg_1024_Align2RegClassID;
17823 case AMDGPU::AReg_64RegClassID:
17824 return AMDGPU::AReg_64_Align2RegClassID;
17825 case AMDGPU::AReg_96RegClassID:
17826 return AMDGPU::AReg_96_Align2RegClassID;
17827 case AMDGPU::AReg_128RegClassID:
17828 return AMDGPU::AReg_128_Align2RegClassID;
17829 case AMDGPU::AReg_160RegClassID:
17830 return AMDGPU::AReg_160_Align2RegClassID;
17831 case AMDGPU::AReg_192RegClassID:
17832 return AMDGPU::AReg_192_Align2RegClassID;
17833 case AMDGPU::AReg_256RegClassID:
17834 return AMDGPU::AReg_256_Align2RegClassID;
17835 case AMDGPU::AReg_512RegClassID:
17836 return AMDGPU::AReg_512_Align2RegClassID;
17837 case AMDGPU::AReg_1024RegClassID:
17838 return AMDGPU::AReg_1024_Align2RegClassID;
17839 default:
17840 return -1;
17841 }
17842}
17843
17844// Figure out which registers should be reserved for stack access. Only after
17845// the function is legalized do we know all of the non-spill stack objects or if
17846// calls are present.
17850 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
17851 const SIRegisterInfo *TRI = Subtarget->getRegisterInfo();
17852 const SIInstrInfo *TII = ST.getInstrInfo();
17853
17854 if (Info->isEntryFunction()) {
17855 // Callable functions have fixed registers used for stack access.
17857 }
17858
17859 // TODO: Move this logic to getReservedRegs()
17860 // Reserve the SGPR(s) to save/restore EXEC for WWM spill/copy handling.
17861 unsigned MaxNumSGPRs = ST.getMaxNumSGPRs(MF);
17862 Register SReg = ST.isWave32()
17863 ? AMDGPU::SGPR_32RegClass.getRegister(MaxNumSGPRs - 1)
17864 : TRI->getAlignedHighSGPRForRC(MF, /*Align=*/2,
17865 &AMDGPU::SGPR_64RegClass);
17866 Info->setSGPRForEXECCopy(SReg);
17867
17868 assert(!TRI->isSubRegister(Info->getScratchRSrcReg(),
17869 Info->getStackPtrOffsetReg()));
17870 if (Info->getStackPtrOffsetReg() != AMDGPU::SP_REG)
17871 MRI.replaceRegWith(AMDGPU::SP_REG, Info->getStackPtrOffsetReg());
17872
17873 // We need to worry about replacing the default register with itself in case
17874 // of MIR testcases missing the MFI.
17875 if (Info->getScratchRSrcReg() != AMDGPU::PRIVATE_RSRC_REG)
17876 MRI.replaceRegWith(AMDGPU::PRIVATE_RSRC_REG, Info->getScratchRSrcReg());
17877
17878 if (Info->getFrameOffsetReg() != AMDGPU::FP_REG)
17879 MRI.replaceRegWith(AMDGPU::FP_REG, Info->getFrameOffsetReg());
17880
17881 Info->limitOccupancy(MF);
17882
17883 if (ST.isWave32() && !MF.empty()) {
17884 for (auto &MBB : MF) {
17885 for (auto &MI : MBB) {
17886 TII->fixImplicitOperands(MI);
17887 }
17888 }
17889 }
17890
17891 // FIXME: This is a hack to fixup AGPR classes to use the properly aligned
17892 // classes if required. Ideally the register class constraints would differ
17893 // per-subtarget, but there's no easy way to achieve that right now. This is
17894 // not a problem for VGPRs because the correctly aligned VGPR class is implied
17895 // from using them as the register class for legal types.
17896 if (ST.needsAlignedVGPRs()) {
17897 for (unsigned I = 0, E = MRI.getNumVirtRegs(); I != E; ++I) {
17898 const Register Reg = Register::index2VirtReg(I);
17899 const TargetRegisterClass *RC = MRI.getRegClassOrNull(Reg);
17900 if (!RC)
17901 continue;
17902 int NewClassID = getAlignedAGPRClassID(RC->getID());
17903 if (NewClassID != -1)
17904 MRI.setRegClass(Reg, TRI->getRegClass(NewClassID));
17905 }
17906 }
17907
17909}
17910
17912 KnownBits &Known,
17913 const APInt &DemandedElts,
17914 const SelectionDAG &DAG,
17915 unsigned Depth) const {
17916 Known.resetAll();
17917 unsigned Opc = Op.getOpcode();
17918 switch (Opc) {
17920 unsigned IID = Op.getConstantOperandVal(0);
17921 switch (IID) {
17922 case Intrinsic::amdgcn_mbcnt_lo:
17923 case Intrinsic::amdgcn_mbcnt_hi: {
17924 const GCNSubtarget &ST =
17926 // Wave64 mbcnt_lo returns at most 32 + src1. Otherwise these return at
17927 // most 31 + src1.
17928 Known.Zero.setBitsFrom(
17929 IID == Intrinsic::amdgcn_mbcnt_lo ? ST.getWavefrontSizeLog2() : 5);
17930 KnownBits Known2 = DAG.computeKnownBits(Op.getOperand(2), Depth + 1);
17931 Known = KnownBits::add(Known, Known2);
17932 return;
17933 }
17934 }
17935 break;
17936 }
17937 }
17939 Op, Known, DemandedElts, DAG, Depth);
17940}
17941
17943 const int FI, KnownBits &Known, const MachineFunction &MF) const {
17945
17946 // Set the high bits to zero based on the maximum allowed scratch size per
17947 // wave. We can't use vaddr in MUBUF instructions if we don't know the address
17948 // calculation won't overflow, so assume the sign bit is never set.
17949 Known.Zero.setHighBits(getSubtarget()->getKnownHighZeroBitsForFrameIndex());
17950}
17951
17953 GISelValueTracking &VT, KnownBits &Known,
17954 unsigned Dim) {
17955 unsigned MaxValue =
17956 ST.getMaxWorkitemID(VT.getMachineFunction().getFunction(), Dim);
17957 Known.Zero.setHighBits(llvm::countl_zero(MaxValue));
17958}
17959
17961 KnownBits &Known, const APInt &DemandedElts,
17962 unsigned BFEWidth, bool SExt, unsigned Depth) {
17964 const MachineOperand &Src1 = MI.getOperand(2);
17965
17966 unsigned Src1Cst = 0;
17967 if (Src1.isImm()) {
17968 Src1Cst = Src1.getImm();
17969 } else if (Src1.isReg()) {
17970 auto Cst = getIConstantVRegValWithLookThrough(Src1.getReg(), MRI);
17971 if (!Cst)
17972 return;
17973 Src1Cst = Cst->Value.getZExtValue();
17974 } else {
17975 return;
17976 }
17977
17978 // Offset is at bits [4:0] for 32 bit, [5:0] for 64 bit.
17979 // Width is always [22:16].
17980 const unsigned Offset =
17981 Src1Cst & maskTrailingOnes<unsigned>((BFEWidth == 32) ? 5 : 6);
17982 const unsigned Width = (Src1Cst >> 16) & maskTrailingOnes<unsigned>(6);
17983
17984 if (Width >= BFEWidth) // Ill-formed.
17985 return;
17986
17987 VT.computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
17988 Depth + 1);
17989
17990 Known = Known.extractBits(Width, Offset);
17991
17992 if (SExt)
17993 Known = Known.sext(BFEWidth);
17994 else
17995 Known = Known.zext(BFEWidth);
17996}
17997
17999 GISelValueTracking &VT, Register R, KnownBits &Known,
18000 const APInt &DemandedElts, const MachineRegisterInfo &MRI,
18001 unsigned Depth) const {
18002 Known.resetAll();
18003 const MachineInstr *MI = MRI.getVRegDef(R);
18004 switch (MI->getOpcode()) {
18005 case AMDGPU::S_BFE_I32:
18006 return knownBitsForSBFE(*MI, VT, Known, DemandedElts, /*Width=*/32,
18007 /*SExt=*/true, Depth);
18008 case AMDGPU::S_BFE_U32:
18009 return knownBitsForSBFE(*MI, VT, Known, DemandedElts, /*Width=*/32,
18010 /*SExt=*/false, Depth);
18011 case AMDGPU::S_BFE_I64:
18012 return knownBitsForSBFE(*MI, VT, Known, DemandedElts, /*Width=*/64,
18013 /*SExt=*/true, Depth);
18014 case AMDGPU::S_BFE_U64:
18015 return knownBitsForSBFE(*MI, VT, Known, DemandedElts, /*Width=*/64,
18016 /*SExt=*/false, Depth);
18017 case AMDGPU::G_INTRINSIC:
18018 case AMDGPU::G_INTRINSIC_CONVERGENT: {
18019 Intrinsic::ID IID = cast<GIntrinsic>(MI)->getIntrinsicID();
18020 switch (IID) {
18021 case Intrinsic::amdgcn_workitem_id_x:
18022 knownBitsForWorkitemID(*getSubtarget(), VT, Known, 0);
18023 break;
18024 case Intrinsic::amdgcn_workitem_id_y:
18025 knownBitsForWorkitemID(*getSubtarget(), VT, Known, 1);
18026 break;
18027 case Intrinsic::amdgcn_workitem_id_z:
18028 knownBitsForWorkitemID(*getSubtarget(), VT, Known, 2);
18029 break;
18030 case Intrinsic::amdgcn_mbcnt_lo:
18031 case Intrinsic::amdgcn_mbcnt_hi: {
18032 // Wave64 mbcnt_lo returns at most 32 + src1. Otherwise these return at
18033 // most 31 + src1.
18034 Known.Zero.setBitsFrom(IID == Intrinsic::amdgcn_mbcnt_lo
18035 ? getSubtarget()->getWavefrontSizeLog2()
18036 : 5);
18037 KnownBits Known2;
18038 VT.computeKnownBitsImpl(MI->getOperand(3).getReg(), Known2, DemandedElts,
18039 Depth + 1);
18040 Known = KnownBits::add(Known, Known2);
18041 break;
18042 }
18043 case Intrinsic::amdgcn_groupstaticsize: {
18044 // We can report everything over the maximum size as 0. We can't report
18045 // based on the actual size because we don't know if it's accurate or not
18046 // at any given point.
18047 Known.Zero.setHighBits(
18048 llvm::countl_zero(getSubtarget()->getAddressableLocalMemorySize()));
18049 break;
18050 }
18051 }
18052 break;
18053 }
18054 case AMDGPU::G_AMDGPU_BUFFER_LOAD_UBYTE:
18055 Known.Zero.setHighBits(24);
18056 break;
18057 case AMDGPU::G_AMDGPU_BUFFER_LOAD_USHORT:
18058 Known.Zero.setHighBits(16);
18059 break;
18060 case AMDGPU::G_AMDGPU_SMED3:
18061 case AMDGPU::G_AMDGPU_UMED3: {
18062 auto [Dst, Src0, Src1, Src2] = MI->getFirst4Regs();
18063
18064 KnownBits Known2;
18065 VT.computeKnownBitsImpl(Src2, Known2, DemandedElts, Depth + 1);
18066 if (Known2.isUnknown())
18067 break;
18068
18069 KnownBits Known1;
18070 VT.computeKnownBitsImpl(Src1, Known1, DemandedElts, Depth + 1);
18071 if (Known1.isUnknown())
18072 break;
18073
18074 KnownBits Known0;
18075 VT.computeKnownBitsImpl(Src0, Known0, DemandedElts, Depth + 1);
18076 if (Known0.isUnknown())
18077 break;
18078
18079 // TODO: Handle LeadZero/LeadOne from UMIN/UMAX handling.
18080 Known.Zero = Known0.Zero & Known1.Zero & Known2.Zero;
18081 Known.One = Known0.One & Known1.One & Known2.One;
18082 break;
18083 }
18084 }
18085}
18086
18089 unsigned Depth) const {
18090 const MachineInstr *MI = MRI.getVRegDef(R);
18091 if (auto *GI = dyn_cast<GIntrinsic>(MI)) {
18092 // FIXME: Can this move to generic code? What about the case where the call
18093 // site specifies a lower alignment?
18094 Intrinsic::ID IID = GI->getIntrinsicID();
18096 AttributeList Attrs =
18097 Intrinsic::getAttributes(Ctx, IID, Intrinsic::getType(Ctx, IID));
18098 if (MaybeAlign RetAlign = Attrs.getRetAlignment())
18099 return *RetAlign;
18100 }
18101 return Align(1);
18102}
18103
18106 const Align CacheLineAlign = Align(64);
18107
18108 // Pre-GFX10 target did not benefit from loop alignment
18109 if (!ML || DisableLoopAlignment || !getSubtarget()->hasInstPrefetch() ||
18110 getSubtarget()->hasInstFwdPrefetchBug())
18111 return PrefAlign;
18112
18113 // On GFX10 I$ is 4 x 64 bytes cache lines.
18114 // By default prefetcher keeps one cache line behind and reads two ahead.
18115 // We can modify it with S_INST_PREFETCH for larger loops to have two lines
18116 // behind and one ahead.
18117 // Therefor we can benefit from aligning loop headers if loop fits 192 bytes.
18118 // If loop fits 64 bytes it always spans no more than two cache lines and
18119 // does not need an alignment.
18120 // Else if loop is less or equal 128 bytes we do not need to modify prefetch,
18121 // Else if loop is less or equal 192 bytes we need two lines behind.
18122
18124 const MachineBasicBlock *Header = ML->getHeader();
18125 if (Header->getAlignment() != PrefAlign)
18126 return Header->getAlignment(); // Already processed.
18127
18128 unsigned LoopSize = 0;
18129 for (const MachineBasicBlock *MBB : ML->blocks()) {
18130 // If inner loop block is aligned assume in average half of the alignment
18131 // size to be added as nops.
18132 if (MBB != Header)
18133 LoopSize += MBB->getAlignment().value() / 2;
18134
18135 for (const MachineInstr &MI : *MBB) {
18136 LoopSize += TII->getInstSizeInBytes(MI);
18137 if (LoopSize > 192)
18138 return PrefAlign;
18139 }
18140 }
18141
18142 if (LoopSize <= 64)
18143 return PrefAlign;
18144
18145 if (LoopSize <= 128)
18146 return CacheLineAlign;
18147
18148 // If any of parent loops is surrounded by prefetch instructions do not
18149 // insert new for inner loop, which would reset parent's settings.
18150 for (MachineLoop *P = ML->getParentLoop(); P; P = P->getParentLoop()) {
18151 if (MachineBasicBlock *Exit = P->getExitBlock()) {
18152 auto I = Exit->getFirstNonDebugInstr();
18153 if (I != Exit->end() && I->getOpcode() == AMDGPU::S_INST_PREFETCH)
18154 return CacheLineAlign;
18155 }
18156 }
18157
18158 MachineBasicBlock *Pre = ML->getLoopPreheader();
18159 MachineBasicBlock *Exit = ML->getExitBlock();
18160
18161 if (Pre && Exit) {
18162 auto PreTerm = Pre->getFirstTerminator();
18163 if (PreTerm == Pre->begin() ||
18164 std::prev(PreTerm)->getOpcode() != AMDGPU::S_INST_PREFETCH)
18165 BuildMI(*Pre, PreTerm, DebugLoc(), TII->get(AMDGPU::S_INST_PREFETCH))
18166 .addImm(1); // prefetch 2 lines behind PC
18167
18168 auto ExitHead = Exit->getFirstNonDebugInstr();
18169 if (ExitHead == Exit->end() ||
18170 ExitHead->getOpcode() != AMDGPU::S_INST_PREFETCH)
18171 BuildMI(*Exit, ExitHead, DebugLoc(), TII->get(AMDGPU::S_INST_PREFETCH))
18172 .addImm(2); // prefetch 1 line behind PC
18173 }
18174
18175 return CacheLineAlign;
18176}
18177
18178[[maybe_unused]]
18179static bool isCopyFromRegOfInlineAsm(const SDNode *N) {
18180 assert(N->getOpcode() == ISD::CopyFromReg);
18181 do {
18182 // Follow the chain until we find an INLINEASM node.
18183 N = N->getOperand(0).getNode();
18184 if (N->getOpcode() == ISD::INLINEASM || N->getOpcode() == ISD::INLINEASM_BR)
18185 return true;
18186 } while (N->getOpcode() == ISD::CopyFromReg);
18187 return false;
18188}
18189
18192 UniformityInfo *UA) const {
18193 switch (N->getOpcode()) {
18194 case ISD::CopyFromReg: {
18195 const RegisterSDNode *R = cast<RegisterSDNode>(N->getOperand(1));
18196 const MachineRegisterInfo &MRI = FLI->MF->getRegInfo();
18197 const SIRegisterInfo *TRI = Subtarget->getRegisterInfo();
18198 Register Reg = R->getReg();
18199
18200 // FIXME: Why does this need to consider isLiveIn?
18201 if (Reg.isPhysical() || MRI.isLiveIn(Reg))
18202 return !TRI->isSGPRReg(MRI, Reg);
18203
18204 if (const Value *V = FLI->getValueFromVirtualReg(R->getReg()))
18205 return UA->isDivergent(V);
18206
18208 return !TRI->isSGPRReg(MRI, Reg);
18209 }
18210 case ISD::LOAD: {
18211 const LoadSDNode *L = cast<LoadSDNode>(N);
18212 unsigned AS = L->getAddressSpace();
18213 // A flat load may access private memory.
18215 }
18216 case ISD::CALLSEQ_END:
18217 return true;
18219 return AMDGPU::isIntrinsicSourceOfDivergence(N->getConstantOperandVal(0));
18221 return AMDGPU::isIntrinsicSourceOfDivergence(N->getConstantOperandVal(1));
18240 // Target-specific read-modify-write atomics are sources of divergence.
18241 return true;
18242 default:
18243 if (auto *A = dyn_cast<AtomicSDNode>(N)) {
18244 // Generic read-modify-write atomics are sources of divergence.
18245 return A->readMem() && A->writeMem();
18246 }
18247 return false;
18248 }
18249}
18250
18252 EVT VT) const {
18253 switch (VT.getScalarType().getSimpleVT().SimpleTy) {
18254 case MVT::f32:
18256 case MVT::f64:
18257 case MVT::f16:
18259 default:
18260 return false;
18261 }
18262}
18263
18265 LLT Ty, const MachineFunction &MF) const {
18266 switch (Ty.getScalarSizeInBits()) {
18267 case 32:
18268 return !denormalModeIsFlushAllF32(MF);
18269 case 64:
18270 case 16:
18271 return !denormalModeIsFlushAllF64F16(MF);
18272 default:
18273 return false;
18274 }
18275}
18276
18278 const APInt &DemandedElts,
18279 const SelectionDAG &DAG,
18280 bool SNaN,
18281 unsigned Depth) const {
18282 if (Op.getOpcode() == AMDGPUISD::CLAMP) {
18283 const MachineFunction &MF = DAG.getMachineFunction();
18285
18286 if (Info->getMode().DX10Clamp)
18287 return true; // Clamped to 0.
18288 return DAG.isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
18289 }
18290
18292 DAG, SNaN, Depth);
18293}
18294
18295// On older subtargets, global FP atomic instructions have a hardcoded FP mode
18296// and do not support FP32 denormals, and only support v2f16/f64 denormals.
18298 if (RMW->hasMetadata("amdgpu.ignore.denormal.mode"))
18299 return true;
18300
18302 auto DenormMode = RMW->getFunction()->getDenormalMode(Flt);
18303 if (DenormMode == DenormalMode::getPreserveSign())
18304 return true;
18305
18306 // TODO: Remove this.
18307 return RMW->getFunction()
18308 ->getFnAttribute("amdgpu-unsafe-fp-atomics")
18309 .getValueAsBool();
18310}
18311
18313 LLVMContext &Ctx = RMW->getContext();
18314 StringRef MemScope =
18315 Ctx.getSyncScopeName(RMW->getSyncScopeID()).value_or("system");
18316
18317 return OptimizationRemark(DEBUG_TYPE, "Passed", RMW)
18318 << "Hardware instruction generated for atomic "
18319 << RMW->getOperationName(RMW->getOperation())
18320 << " operation at memory scope " << MemScope;
18321}
18322
18323static bool isV2F16OrV2BF16(Type *Ty) {
18324 if (auto *VT = dyn_cast<FixedVectorType>(Ty)) {
18325 Type *EltTy = VT->getElementType();
18326 return VT->getNumElements() == 2 &&
18327 (EltTy->isHalfTy() || EltTy->isBFloatTy());
18328 }
18329
18330 return false;
18331}
18332
18333static bool isV2F16(Type *Ty) {
18335 return VT && VT->getNumElements() == 2 && VT->getElementType()->isHalfTy();
18336}
18337
18338static bool isV2BF16(Type *Ty) {
18340 return VT && VT->getNumElements() == 2 && VT->getElementType()->isBFloatTy();
18341}
18342
18343/// \return true if atomicrmw integer ops work for the type.
18344static bool isAtomicRMWLegalIntTy(Type *Ty) {
18345 if (auto *IT = dyn_cast<IntegerType>(Ty)) {
18346 unsigned BW = IT->getBitWidth();
18347 return BW == 32 || BW == 64;
18348 }
18349
18350 return false;
18351}
18352
18353/// \return true if this atomicrmw xchg type can be selected.
18354static bool isAtomicRMWLegalXChgTy(const AtomicRMWInst *RMW) {
18355 Type *Ty = RMW->getType();
18356 if (isAtomicRMWLegalIntTy(Ty))
18357 return true;
18358
18359 if (PointerType *PT = dyn_cast<PointerType>(Ty)) {
18360 const DataLayout &DL = RMW->getFunction()->getParent()->getDataLayout();
18361 unsigned BW = DL.getPointerSizeInBits(PT->getAddressSpace());
18362 return BW == 32 || BW == 64;
18363 }
18364
18365 if (Ty->isFloatTy() || Ty->isDoubleTy())
18366 return true;
18367
18369 return VT->getNumElements() == 2 &&
18370 VT->getElementType()->getPrimitiveSizeInBits() == 16;
18371 }
18372
18373 return false;
18374}
18375
18376/// \returns true if it's valid to emit a native instruction for \p RMW, based
18377/// on the properties of the target memory.
18378static bool globalMemoryFPAtomicIsLegal(const GCNSubtarget &Subtarget,
18379 const AtomicRMWInst *RMW,
18380 bool HasSystemScope) {
18381 // The remote/fine-grained access logic is different from the integer
18382 // atomics. Without AgentScopeFineGrainedRemoteMemoryAtomics support,
18383 // fine-grained access does not work, even for a device local allocation.
18384 //
18385 // With AgentScopeFineGrainedRemoteMemoryAtomics, system scoped device local
18386 // allocations work.
18387 if (HasSystemScope) {
18389 RMW->hasMetadata("amdgpu.no.remote.memory"))
18390 return true;
18391 if (Subtarget.hasEmulatedSystemScopeAtomics())
18392 return true;
18394 return true;
18395
18396 return RMW->hasMetadata("amdgpu.no.fine.grained.memory");
18397}
18398
18399/// \return Action to perform on AtomicRMWInsts for integer operations.
18406
18407/// Return if a flat address space atomicrmw can access private memory.
18409 const MDNode *MD = I->getMetadata(LLVMContext::MD_noalias_addrspace);
18410 return !MD ||
18412}
18413
18421
18424 unsigned AS = RMW->getPointerAddressSpace();
18425 if (AS == AMDGPUAS::PRIVATE_ADDRESS)
18427
18428 // 64-bit flat atomics that dynamically reside in private memory will silently
18429 // be dropped.
18430 //
18431 // Note that we will emit a new copy of the original atomic in the expansion,
18432 // which will be incrementally relegalized.
18433 const DataLayout &DL = RMW->getFunction()->getDataLayout();
18434 if (AS == AMDGPUAS::FLAT_ADDRESS &&
18435 DL.getTypeSizeInBits(RMW->getType()) == 64 &&
18438
18439 auto ReportUnsafeHWInst = [=](TargetLowering::AtomicExpansionKind Kind) {
18441 ORE.emit([=]() {
18442 return emitAtomicRMWLegalRemark(RMW) << " due to an unsafe request.";
18443 });
18444 return Kind;
18445 };
18446
18447 auto SSID = RMW->getSyncScopeID();
18448 bool HasSystemScope =
18449 SSID == SyncScope::System ||
18450 SSID == RMW->getContext().getOrInsertSyncScopeID("one-as");
18451
18452 auto Op = RMW->getOperation();
18453 switch (Op) {
18455 // PCIe supports add and xchg for system atomics.
18456 return isAtomicRMWLegalXChgTy(RMW)
18459 case AtomicRMWInst::Add:
18460 // PCIe supports add and xchg for system atomics.
18462 case AtomicRMWInst::Sub:
18463 case AtomicRMWInst::And:
18464 case AtomicRMWInst::Or:
18465 case AtomicRMWInst::Xor:
18466 case AtomicRMWInst::Max:
18467 case AtomicRMWInst::Min:
18474 if (Subtarget->hasEmulatedSystemScopeAtomics())
18476
18477 // On most subtargets, for atomicrmw operations other than add/xchg,
18478 // whether or not the instructions will behave correctly depends on where
18479 // the address physically resides and what interconnect is used in the
18480 // system configuration. On some some targets the instruction will nop,
18481 // and in others synchronization will only occur at degraded device scope.
18482 //
18483 // If the allocation is known local to the device, the instructions should
18484 // work correctly.
18485 if (RMW->hasMetadata("amdgpu.no.remote.memory"))
18487
18488 // If fine-grained remote memory works at device scope, we don't need to
18489 // do anything.
18490 if (!HasSystemScope &&
18491 Subtarget->supportsAgentScopeFineGrainedRemoteMemoryAtomics())
18493
18494 // If we are targeting a remote allocated address, it depends what kind of
18495 // allocation the address belongs to.
18496 //
18497 // If the allocation is fine-grained (in host memory, or in PCIe peer
18498 // device memory), the operation will fail depending on the target.
18499 //
18500 // Note fine-grained host memory access does work on APUs or if XGMI is
18501 // used, but we do not know if we are targeting an APU or the system
18502 // configuration from the ISA version/target-cpu.
18503 if (RMW->hasMetadata("amdgpu.no.fine.grained.memory"))
18505
18508 // Atomic sub/or/xor do not work over PCI express, but atomic add
18509 // does. InstCombine transforms these with 0 to or, so undo that.
18510 if (Constant *ConstVal = dyn_cast<Constant>(RMW->getValOperand());
18511 ConstVal && ConstVal->isNullValue())
18513 }
18514
18515 // If the allocation could be in remote, fine-grained memory, the rmw
18516 // instructions may fail. cmpxchg should work, so emit that. On some
18517 // system configurations, PCIe atomics aren't supported so cmpxchg won't
18518 // even work, so you're out of luck anyway.
18519
18520 // In summary:
18521 //
18522 // Cases that may fail:
18523 // - fine-grained pinned host memory
18524 // - fine-grained migratable host memory
18525 // - fine-grained PCIe peer device
18526 //
18527 // Cases that should work, but may be treated overly conservatively.
18528 // - fine-grained host memory on an APU
18529 // - fine-grained XGMI peer device
18531 }
18532
18534 }
18535 case AtomicRMWInst::FAdd: {
18536 Type *Ty = RMW->getType();
18537
18538 // TODO: Handle REGION_ADDRESS
18539 if (AS == AMDGPUAS::LOCAL_ADDRESS) {
18540 // DS F32 FP atomics do respect the denormal mode, but the rounding mode
18541 // is fixed to round-to-nearest-even.
18542 //
18543 // F64 / PK_F16 / PK_BF16 never flush and are also fixed to
18544 // round-to-nearest-even.
18545 //
18546 // We ignore the rounding mode problem, even in strictfp. The C++ standard
18547 // suggests it is OK if the floating-point mode may not match the calling
18548 // thread.
18549 if (Ty->isFloatTy()) {
18550 return Subtarget->hasLDSFPAtomicAddF32() ? AtomicExpansionKind::None
18552 }
18553
18554 if (Ty->isDoubleTy()) {
18555 // Ignores denormal mode, but we don't consider flushing mandatory.
18556 return Subtarget->hasLDSFPAtomicAddF64() ? AtomicExpansionKind::None
18558 }
18559
18560 if (Subtarget->hasAtomicDsPkAdd16Insts() && isV2F16OrV2BF16(Ty))
18562
18564 }
18565
18566 // LDS atomics respect the denormal mode from the mode register.
18567 //
18568 // Traditionally f32 global/buffer memory atomics would unconditionally
18569 // flush denormals, but newer targets do not flush. f64/f16/bf16 cases never
18570 // flush.
18571 //
18572 // On targets with flat atomic fadd, denormals would flush depending on
18573 // whether the target address resides in LDS or global memory. We consider
18574 // this flat-maybe-flush as will-flush.
18575 if (Ty->isFloatTy() &&
18576 !Subtarget->hasMemoryAtomicFaddF32DenormalSupport() &&
18579
18580 // FIXME: These ReportUnsafeHWInsts are imprecise. Some of these cases are
18581 // safe. The message phrasing also should be better.
18582 if (globalMemoryFPAtomicIsLegal(*Subtarget, RMW, HasSystemScope)) {
18583 if (AS == AMDGPUAS::FLAT_ADDRESS) {
18584 // gfx942, gfx12
18585 if (Subtarget->hasAtomicFlatPkAdd16Insts() && isV2F16OrV2BF16(Ty))
18586 return ReportUnsafeHWInst(AtomicExpansionKind::None);
18587 } else if (AMDGPU::isExtendedGlobalAddrSpace(AS)) {
18588 // gfx90a, gfx942, gfx12
18589 if (Subtarget->hasAtomicBufferGlobalPkAddF16Insts() && isV2F16(Ty))
18590 return ReportUnsafeHWInst(AtomicExpansionKind::None);
18591
18592 // gfx942, gfx12
18593 if (Subtarget->hasAtomicGlobalPkAddBF16Inst() && isV2BF16(Ty))
18594 return ReportUnsafeHWInst(AtomicExpansionKind::None);
18595 } else if (AS == AMDGPUAS::BUFFER_FAT_POINTER) {
18596 // gfx90a, gfx942, gfx12
18597 if (Subtarget->hasAtomicBufferGlobalPkAddF16Insts() && isV2F16(Ty))
18598 return ReportUnsafeHWInst(AtomicExpansionKind::None);
18599
18600 // While gfx90a/gfx942 supports v2bf16 for global/flat, it does not for
18601 // buffer. gfx12 does have the buffer version.
18602 if (Subtarget->hasAtomicBufferPkAddBF16Inst() && isV2BF16(Ty))
18603 return ReportUnsafeHWInst(AtomicExpansionKind::None);
18604 }
18605
18606 // global and flat atomic fadd f64: gfx90a, gfx942.
18607 if (Subtarget->hasFlatBufferGlobalAtomicFaddF64Inst() && Ty->isDoubleTy())
18608 return ReportUnsafeHWInst(AtomicExpansionKind::None);
18609
18610 if (AS != AMDGPUAS::FLAT_ADDRESS) {
18611 if (Ty->isFloatTy()) {
18612 // global/buffer atomic fadd f32 no-rtn: gfx908, gfx90a, gfx942,
18613 // gfx11+.
18614 if (RMW->use_empty() && Subtarget->hasAtomicFaddNoRtnInsts())
18615 return ReportUnsafeHWInst(AtomicExpansionKind::None);
18616 // global/buffer atomic fadd f32 rtn: gfx90a, gfx942, gfx11+.
18617 if (!RMW->use_empty() && Subtarget->hasAtomicFaddRtnInsts())
18618 return ReportUnsafeHWInst(AtomicExpansionKind::None);
18619 } else {
18620 // gfx908
18621 if (RMW->use_empty() &&
18622 Subtarget->hasAtomicBufferGlobalPkAddF16NoRtnInsts() &&
18623 isV2F16(Ty))
18624 return ReportUnsafeHWInst(AtomicExpansionKind::None);
18625 }
18626 }
18627
18628 // flat atomic fadd f32: gfx942, gfx11+.
18629 if (AS == AMDGPUAS::FLAT_ADDRESS && Ty->isFloatTy()) {
18630 if (Subtarget->hasFlatAtomicFaddF32Inst())
18631 return ReportUnsafeHWInst(AtomicExpansionKind::None);
18632
18633 // If it is in flat address space, and the type is float, we will try to
18634 // expand it, if the target supports global and lds atomic fadd. The
18635 // reason we need that is, in the expansion, we emit the check of
18636 // address space. If it is in global address space, we emit the global
18637 // atomic fadd; if it is in shared address space, we emit the LDS atomic
18638 // fadd.
18639 if (Subtarget->hasLDSFPAtomicAddF32()) {
18640 if (RMW->use_empty() && Subtarget->hasAtomicFaddNoRtnInsts())
18642 if (!RMW->use_empty() && Subtarget->hasAtomicFaddRtnInsts())
18644 }
18645 }
18646 }
18647
18649 }
18651 case AtomicRMWInst::FMax: {
18652 Type *Ty = RMW->getType();
18653
18654 // LDS float and double fmin/fmax were always supported.
18655 if (AS == AMDGPUAS::LOCAL_ADDRESS) {
18656 return Ty->isFloatTy() || Ty->isDoubleTy() ? AtomicExpansionKind::None
18658 }
18659
18660 if (globalMemoryFPAtomicIsLegal(*Subtarget, RMW, HasSystemScope)) {
18661 // For flat and global cases:
18662 // float, double in gfx7. Manual claims denormal support.
18663 // Removed in gfx8.
18664 // float, double restored in gfx10.
18665 // double removed again in gfx11, so only f32 for gfx11/gfx12.
18666 //
18667 // For gfx9, gfx90a and gfx942 support f64 for global (same as fadd), but
18668 // no f32.
18669 if (AS == AMDGPUAS::FLAT_ADDRESS) {
18670 if (Subtarget->hasAtomicFMinFMaxF32FlatInsts() && Ty->isFloatTy())
18671 return ReportUnsafeHWInst(AtomicExpansionKind::None);
18672 if (Subtarget->hasAtomicFMinFMaxF64FlatInsts() && Ty->isDoubleTy())
18673 return ReportUnsafeHWInst(AtomicExpansionKind::None);
18674 } else if (AMDGPU::isExtendedGlobalAddrSpace(AS) ||
18676 if (Subtarget->hasAtomicFMinFMaxF32GlobalInsts() && Ty->isFloatTy())
18677 return ReportUnsafeHWInst(AtomicExpansionKind::None);
18678 if (Subtarget->hasAtomicFMinFMaxF64GlobalInsts() && Ty->isDoubleTy())
18679 return ReportUnsafeHWInst(AtomicExpansionKind::None);
18680 }
18681 }
18682
18684 }
18687 default:
18689 }
18690
18691 llvm_unreachable("covered atomicrmw op switch");
18692}
18693
18700
18707
18710 unsigned AddrSpace = CmpX->getPointerAddressSpace();
18711 if (AddrSpace == AMDGPUAS::PRIVATE_ADDRESS)
18713
18714 if (AddrSpace != AMDGPUAS::FLAT_ADDRESS || !flatInstrMayAccessPrivate(CmpX))
18716
18717 const DataLayout &DL = CmpX->getDataLayout();
18718
18719 Type *ValTy = CmpX->getNewValOperand()->getType();
18720
18721 // If a 64-bit flat atomic may alias private, we need to avoid using the
18722 // atomic in the private case.
18723 return DL.getTypeSizeInBits(ValTy) == 64 ? AtomicExpansionKind::CustomExpand
18725}
18726
18727const TargetRegisterClass *
18728SITargetLowering::getRegClassFor(MVT VT, bool isDivergent) const {
18730 const SIRegisterInfo *TRI = Subtarget->getRegisterInfo();
18731 if (RC == &AMDGPU::VReg_1RegClass && !isDivergent)
18732 return Subtarget->isWave64() ? &AMDGPU::SReg_64RegClass
18733 : &AMDGPU::SReg_32RegClass;
18734 if (!TRI->isSGPRClass(RC) && !isDivergent)
18735 return TRI->getEquivalentSGPRClass(RC);
18736 if (TRI->isSGPRClass(RC) && isDivergent)
18737 return TRI->getEquivalentVGPRClass(RC);
18738
18739 return RC;
18740}
18741
18742// FIXME: This is a workaround for DivergenceAnalysis not understanding always
18743// uniform values (as produced by the mask results of control flow intrinsics)
18744// used outside of divergent blocks. The phi users need to also be treated as
18745// always uniform.
18746//
18747// FIXME: DA is no longer in-use. Does this still apply to UniformityAnalysis?
18748static bool hasCFUser(const Value *V, SmallPtrSet<const Value *, 16> &Visited,
18749 unsigned WaveSize) {
18750 // FIXME: We assume we never cast the mask results of a control flow
18751 // intrinsic.
18752 // Early exit if the type won't be consistent as a compile time hack.
18753 IntegerType *IT = dyn_cast<IntegerType>(V->getType());
18754 if (!IT || IT->getBitWidth() != WaveSize)
18755 return false;
18756
18757 if (!isa<Instruction>(V))
18758 return false;
18759 if (!Visited.insert(V).second)
18760 return false;
18761 bool Result = false;
18762 for (const auto *U : V->users()) {
18764 if (V == U->getOperand(1)) {
18765 switch (Intrinsic->getIntrinsicID()) {
18766 default:
18767 Result = false;
18768 break;
18769 case Intrinsic::amdgcn_if_break:
18770 case Intrinsic::amdgcn_if:
18771 case Intrinsic::amdgcn_else:
18772 Result = true;
18773 break;
18774 }
18775 }
18776 if (V == U->getOperand(0)) {
18777 switch (Intrinsic->getIntrinsicID()) {
18778 default:
18779 Result = false;
18780 break;
18781 case Intrinsic::amdgcn_end_cf:
18782 case Intrinsic::amdgcn_loop:
18783 Result = true;
18784 break;
18785 }
18786 }
18787 } else {
18788 Result = hasCFUser(U, Visited, WaveSize);
18789 }
18790 if (Result)
18791 break;
18792 }
18793 return Result;
18794}
18795
18797 const Value *V) const {
18798 if (const CallInst *CI = dyn_cast<CallInst>(V)) {
18799 if (CI->isInlineAsm()) {
18800 // FIXME: This cannot give a correct answer. This should only trigger in
18801 // the case where inline asm returns mixed SGPR and VGPR results, used
18802 // outside the defining block. We don't have a specific result to
18803 // consider, so this assumes if any value is SGPR, the overall register
18804 // also needs to be SGPR.
18805 const SIRegisterInfo *SIRI = Subtarget->getRegisterInfo();
18807 MF.getDataLayout(), Subtarget->getRegisterInfo(), *CI);
18808 for (auto &TC : TargetConstraints) {
18809 if (TC.Type == InlineAsm::isOutput) {
18811 const TargetRegisterClass *RC =
18812 getRegForInlineAsmConstraint(SIRI, TC.ConstraintCode,
18813 TC.ConstraintVT)
18814 .second;
18815 if (RC && SIRI->isSGPRClass(RC))
18816 return true;
18817 }
18818 }
18819 }
18820 }
18822 return hasCFUser(V, Visited, Subtarget->getWavefrontSize());
18823}
18824
18826 for (SDUse &Use : N->uses()) {
18828 if (getBasePtrIndex(M) == Use.getOperandNo())
18829 return true;
18830 }
18831 }
18832 return false;
18833}
18834
18836 SDValue N1) const {
18837 if (!N0.hasOneUse())
18838 return false;
18839 // Take care of the opportunity to keep N0 uniform
18840 if (N0->isDivergent() || !N1->isDivergent())
18841 return true;
18842 // Check if we have a good chance to form the memory access pattern with the
18843 // base and offset
18844 return (DAG.isBaseWithConstantOffset(N0) &&
18846}
18847
18849 Register N0, Register N1) const {
18850 return MRI.hasOneNonDBGUse(N0); // FIXME: handle regbanks
18851}
18852
18855 // Propagate metadata set by AMDGPUAnnotateUniformValues to the MMO of a load.
18857 if (I.getMetadata("amdgpu.noclobber"))
18858 Flags |= MONoClobber;
18859 if (I.getMetadata("amdgpu.last.use"))
18860 Flags |= MOLastUse;
18861 return Flags;
18862}
18863
18865 Instruction *AI) const {
18866 // Given: atomicrmw fadd ptr %addr, float %val ordering
18867 //
18868 // With this expansion we produce the following code:
18869 // [...]
18870 // %is.shared = call i1 @llvm.amdgcn.is.shared(ptr %addr)
18871 // br i1 %is.shared, label %atomicrmw.shared, label %atomicrmw.check.private
18872 //
18873 // atomicrmw.shared:
18874 // %cast.shared = addrspacecast ptr %addr to ptr addrspace(3)
18875 // %loaded.shared = atomicrmw fadd ptr addrspace(3) %cast.shared,
18876 // float %val ordering
18877 // br label %atomicrmw.phi
18878 //
18879 // atomicrmw.check.private:
18880 // %is.private = call i1 @llvm.amdgcn.is.private(ptr %int8ptr)
18881 // br i1 %is.private, label %atomicrmw.private, label %atomicrmw.global
18882 //
18883 // atomicrmw.private:
18884 // %cast.private = addrspacecast ptr %addr to ptr addrspace(5)
18885 // %loaded.private = load float, ptr addrspace(5) %cast.private
18886 // %val.new = fadd float %loaded.private, %val
18887 // store float %val.new, ptr addrspace(5) %cast.private
18888 // br label %atomicrmw.phi
18889 //
18890 // atomicrmw.global:
18891 // %cast.global = addrspacecast ptr %addr to ptr addrspace(1)
18892 // %loaded.global = atomicrmw fadd ptr addrspace(1) %cast.global,
18893 // float %val ordering
18894 // br label %atomicrmw.phi
18895 //
18896 // atomicrmw.phi:
18897 // %loaded.phi = phi float [ %loaded.shared, %atomicrmw.shared ],
18898 // [ %loaded.private, %atomicrmw.private ],
18899 // [ %loaded.global, %atomicrmw.global ]
18900 // br label %atomicrmw.end
18901 //
18902 // atomicrmw.end:
18903 // [...]
18904 //
18905 //
18906 // For 64-bit atomics which may reside in private memory, we perform a simpler
18907 // version that only inserts the private check, and uses the flat operation.
18908
18909 IRBuilder<> Builder(AI);
18910 LLVMContext &Ctx = Builder.getContext();
18911
18912 auto *RMW = dyn_cast<AtomicRMWInst>(AI);
18913 const unsigned PtrOpIdx = RMW ? AtomicRMWInst::getPointerOperandIndex()
18915 Value *Addr = AI->getOperand(PtrOpIdx);
18916
18917 /// TODO: Only need to check private, then emit flat-known-not private (no
18918 /// need for shared block, or cast to global).
18920
18921 Align Alignment;
18922 if (RMW)
18923 Alignment = RMW->getAlign();
18924 else if (CX)
18925 Alignment = CX->getAlign();
18926 else
18927 llvm_unreachable("unhandled atomic operation");
18928
18929 // FullFlatEmulation is true if we need to issue the private, shared, and
18930 // global cases.
18931 //
18932 // If this is false, we are only dealing with the flat-targeting-private case,
18933 // where we only insert a check for private and still use the flat instruction
18934 // for global and shared.
18935
18936 bool FullFlatEmulation =
18937 RMW && RMW->getOperation() == AtomicRMWInst::FAdd &&
18938 ((Subtarget->hasAtomicFaddInsts() && RMW->getType()->isFloatTy()) ||
18939 (Subtarget->hasFlatBufferGlobalAtomicFaddF64Inst() &&
18940 RMW->getType()->isDoubleTy()));
18941
18942 // If the return value isn't used, do not introduce a false use in the phi.
18943 bool ReturnValueIsUsed = !AI->use_empty();
18944
18945 BasicBlock *BB = Builder.GetInsertBlock();
18946 Function *F = BB->getParent();
18947 BasicBlock *ExitBB =
18948 BB->splitBasicBlock(Builder.GetInsertPoint(), "atomicrmw.end");
18949 BasicBlock *SharedBB = nullptr;
18950
18951 BasicBlock *CheckPrivateBB = BB;
18952 if (FullFlatEmulation) {
18953 SharedBB = BasicBlock::Create(Ctx, "atomicrmw.shared", F, ExitBB);
18954 CheckPrivateBB =
18955 BasicBlock::Create(Ctx, "atomicrmw.check.private", F, ExitBB);
18956 }
18957
18958 BasicBlock *PrivateBB =
18959 BasicBlock::Create(Ctx, "atomicrmw.private", F, ExitBB);
18960 BasicBlock *GlobalBB = BasicBlock::Create(Ctx, "atomicrmw.global", F, ExitBB);
18961 BasicBlock *PhiBB = BasicBlock::Create(Ctx, "atomicrmw.phi", F, ExitBB);
18962
18963 std::prev(BB->end())->eraseFromParent();
18964 Builder.SetInsertPoint(BB);
18965
18966 Value *LoadedShared = nullptr;
18967 if (FullFlatEmulation) {
18968 CallInst *IsShared = Builder.CreateIntrinsic(Intrinsic::amdgcn_is_shared,
18969 {Addr}, nullptr, "is.shared");
18970 Builder.CreateCondBr(IsShared, SharedBB, CheckPrivateBB);
18971 Builder.SetInsertPoint(SharedBB);
18972 Value *CastToLocal = Builder.CreateAddrSpaceCast(
18974
18975 Instruction *Clone = AI->clone();
18976 Clone->insertInto(SharedBB, SharedBB->end());
18977 Clone->getOperandUse(PtrOpIdx).set(CastToLocal);
18978 LoadedShared = Clone;
18979
18980 Builder.CreateBr(PhiBB);
18981 Builder.SetInsertPoint(CheckPrivateBB);
18982 }
18983
18984 CallInst *IsPrivate = Builder.CreateIntrinsic(Intrinsic::amdgcn_is_private,
18985 {Addr}, nullptr, "is.private");
18986 Builder.CreateCondBr(IsPrivate, PrivateBB, GlobalBB);
18987
18988 Builder.SetInsertPoint(PrivateBB);
18989
18990 Value *CastToPrivate = Builder.CreateAddrSpaceCast(
18992
18993 Value *LoadedPrivate;
18994 if (RMW) {
18995 LoadedPrivate = Builder.CreateAlignedLoad(
18996 RMW->getType(), CastToPrivate, RMW->getAlign(), "loaded.private");
18997
18998 Value *NewVal = buildAtomicRMWValue(RMW->getOperation(), Builder,
18999 LoadedPrivate, RMW->getValOperand());
19000
19001 Builder.CreateAlignedStore(NewVal, CastToPrivate, RMW->getAlign());
19002 } else {
19003 auto [ResultLoad, Equal] =
19004 buildCmpXchgValue(Builder, CastToPrivate, CX->getCompareOperand(),
19005 CX->getNewValOperand(), CX->getAlign());
19006
19007 Value *Insert = Builder.CreateInsertValue(PoisonValue::get(CX->getType()),
19008 ResultLoad, 0);
19009 LoadedPrivate = Builder.CreateInsertValue(Insert, Equal, 1);
19010 }
19011
19012 Builder.CreateBr(PhiBB);
19013
19014 Builder.SetInsertPoint(GlobalBB);
19015
19016 // Continue using a flat instruction if we only emitted the check for private.
19017 Instruction *LoadedGlobal = AI;
19018 if (FullFlatEmulation) {
19019 Value *CastToGlobal = Builder.CreateAddrSpaceCast(
19021 AI->getOperandUse(PtrOpIdx).set(CastToGlobal);
19022 }
19023
19024 AI->removeFromParent();
19025 AI->insertInto(GlobalBB, GlobalBB->end());
19026
19027 // The new atomicrmw may go through another round of legalization later.
19028 if (!FullFlatEmulation) {
19029 // We inserted the runtime check already, make sure we do not try to
19030 // re-expand this.
19031 // TODO: Should union with any existing metadata.
19032 MDBuilder MDB(F->getContext());
19033 MDNode *RangeNotPrivate =
19036 LoadedGlobal->setMetadata(LLVMContext::MD_noalias_addrspace,
19037 RangeNotPrivate);
19038 }
19039
19040 Builder.CreateBr(PhiBB);
19041
19042 Builder.SetInsertPoint(PhiBB);
19043
19044 if (ReturnValueIsUsed) {
19045 PHINode *Loaded = Builder.CreatePHI(AI->getType(), 3);
19046 AI->replaceAllUsesWith(Loaded);
19047 if (FullFlatEmulation)
19048 Loaded->addIncoming(LoadedShared, SharedBB);
19049 Loaded->addIncoming(LoadedPrivate, PrivateBB);
19050 Loaded->addIncoming(LoadedGlobal, GlobalBB);
19051 Loaded->takeName(AI);
19052 }
19053
19054 Builder.CreateBr(ExitBB);
19055}
19056
19058 unsigned PtrOpIdx) {
19059 Value *PtrOp = I->getOperand(PtrOpIdx);
19062
19063 Type *FlatPtr = PointerType::get(I->getContext(), AMDGPUAS::FLAT_ADDRESS);
19064 Value *ASCast = CastInst::CreatePointerCast(PtrOp, FlatPtr, "scratch.ascast",
19065 I->getIterator());
19066 I->setOperand(PtrOpIdx, ASCast);
19067}
19068
19071
19074
19077 if (const auto *ConstVal = dyn_cast<Constant>(AI->getValOperand());
19078 ConstVal && ConstVal->isNullValue()) {
19079 // atomicrmw or %ptr, 0 -> atomicrmw add %ptr, 0
19081
19082 // We may still need the private-alias-flat handling below.
19083
19084 // TODO: Skip this for cases where we cannot access remote memory.
19085 }
19086 }
19087
19088 // The non-flat expansions should only perform the de-canonicalization of
19089 // identity values.
19091 return;
19092
19094}
19095
19102
19106
19108 "Expand Atomic Load only handles SCRATCH -> FLAT conversion");
19109}
19110
19112 if (SI->getPointerAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS)
19113 return convertScratchAtomicToFlatAtomic(SI, SI->getPointerOperandIndex());
19114
19116 "Expand Atomic Store only handles SCRATCH -> FLAT conversion");
19117}
19118
19119LoadInst *
19121 IRBuilder<> Builder(AI);
19122 auto Order = AI->getOrdering();
19123
19124 // The optimization removes store aspect of the atomicrmw. Therefore, cache
19125 // must be flushed if the atomic ordering had a release semantics. This is
19126 // not necessary a fence, a release fence just coincides to do that flush.
19127 // Avoid replacing of an atomicrmw with a release semantics.
19128 if (isReleaseOrStronger(Order))
19129 return nullptr;
19130
19131 LoadInst *LI = Builder.CreateAlignedLoad(
19132 AI->getType(), AI->getPointerOperand(), AI->getAlign());
19133 LI->setAtomic(Order, AI->getSyncScopeID());
19134 LI->copyMetadata(*AI);
19135 LI->takeName(AI);
19136 AI->replaceAllUsesWith(LI);
19137 AI->eraseFromParent();
19138 return LI;
19139}
static bool isMul(MachineInstr *MI)
unsigned SubReg
unsigned const MachineRegisterInfo * MRI
return SDValue()
static unsigned getIntrinsicID(const SDNode *N)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static constexpr std::pair< ImplicitArgumentMask, StringLiteral > ImplicitAttrs[]
static bool allUsesHaveSourceMods(MachineInstr &MI, MachineRegisterInfo &MRI, unsigned CostThreshold=4)
Contains the definition of a TargetInstrInfo class that is common to all AMD GPUs.
static bool isNoUnsignedWrap(MachineInstr *Addr)
static bool parseTexFail(uint64_t TexFailCtrl, bool &TFE, bool &LWE, bool &IsTexFail)
static void packImage16bitOpsToDwords(MachineIRBuilder &B, MachineInstr &MI, SmallVectorImpl< Register > &PackedAddrs, unsigned ArgOffset, const AMDGPU::ImageDimIntrinsicInfo *Intr, bool IsA16, bool IsG16)
Turn a set of s16 typed registers in AddrRegs into a dword sized vector with s16 typed elements.
constexpr LLT S32
static bool isKnownNonNull(Register Val, MachineRegisterInfo &MRI, const AMDGPUTargetMachine &TM, unsigned AddrSpace)
Return true if the value is a known valid address, such that a null check is not necessary.
Provides AMDGPU specific target descriptions.
The AMDGPU TargetMachine interface definition for hw codegen targets.
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
static cl::opt< ITMode > IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT), cl::values(clEnumValN(DefaultIT, "arm-default-it", "Generate any type of IT block"), clEnumValN(RestrictedIT, "arm-restrict-it", "Disallow complex IT blocks")))
Function Alias Analysis Results
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Analysis containing CSE Info
Definition CSEInfo.cpp:27
static std::optional< SDByteProvider > calculateByteProvider(SDValue Op, unsigned Index, unsigned Depth, std::optional< uint64_t > VectorIndex, unsigned StartingIndex=0)
dxil translate DXIL Translate Metadata
static bool isSigned(unsigned int Opcode)
Utilities for dealing with flags related to floating point properties and mode controls.
AMD GCN specific subclass of TargetSubtarget.
Provides analysis for querying information about KnownBits during GISel passes.
#define DEBUG_TYPE
Declares convenience wrapper classes for interpreting MachineInstr instances as specific generic oper...
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
iv Induction Variable Users
Definition IVUsers.cpp:48
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define RegName(no)
static LVOptions Options
Definition LVOptions.cpp:25
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
Contains matchers for matching SSA Machine Instructions.
Machine Check Debug Module
static bool isUndef(const MachineInstr &MI)
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
static unsigned getAddressSpace(const Value *V, unsigned MaxLookup)
uint64_t IntrinsicInst * II
#define P(N)
static constexpr MCPhysReg SPReg
const SmallVectorImpl< MachineOperand > & Cond
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
Contains matchers for matching SelectionDAG nodes and values.
static void r0(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E, int I, uint32_t *Buf)
Definition SHA1.cpp:39
static void r3(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E, int I, uint32_t *Buf)
Definition SHA1.cpp:57
static void r2(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E, int I, uint32_t *Buf)
Definition SHA1.cpp:51
static void r1(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E, int I, uint32_t *Buf)
Definition SHA1.cpp:45
#define FP_DENORM_FLUSH_NONE
Definition SIDefines.h:1257
#define FP_DENORM_FLUSH_IN_FLUSH_OUT
Definition SIDefines.h:1254
static void reservePrivateMemoryRegs(const TargetMachine &TM, MachineFunction &MF, const SIRegisterInfo &TRI, SIMachineFunctionInfo &Info)
static SDValue adjustLoadValueTypeImpl(SDValue Result, EVT LoadVT, const SDLoc &DL, SelectionDAG &DAG, bool Unpacked)
static MachineBasicBlock * emitIndirectSrc(MachineInstr &MI, MachineBasicBlock &MBB, const GCNSubtarget &ST)
static bool denormalModeIsFlushAllF64F16(const MachineFunction &MF)
static bool isAtomicRMWLegalIntTy(Type *Ty)
static void knownBitsForWorkitemID(const GCNSubtarget &ST, GISelValueTracking &VT, KnownBits &Known, unsigned Dim)
static bool flatInstrMayAccessPrivate(const Instruction *I)
Return if a flat address space atomicrmw can access private memory.
static std::pair< unsigned, int > computeIndirectRegAndOffset(const SIRegisterInfo &TRI, const TargetRegisterClass *SuperRC, unsigned VecReg, int Offset)
static bool denormalModeIsFlushAllF32(const MachineFunction &MF)
static bool addresses16Bits(int Mask)
static bool isClampZeroToOne(SDValue A, SDValue B)
static bool supportsMin3Max3(const GCNSubtarget &Subtarget, unsigned Opc, EVT VT)
static unsigned findFirstFreeSGPR(CCState &CCInfo)
static uint32_t getPermuteMask(SDValue V)
static SDValue lowerLaneOp(const SITargetLowering &TLI, SDNode *N, SelectionDAG &DAG)
static int getAlignedAGPRClassID(unsigned UnalignedClassID)
static void processPSInputArgs(SmallVectorImpl< ISD::InputArg > &Splits, CallingConv::ID CallConv, ArrayRef< ISD::InputArg > Ins, BitVector &Skipped, FunctionType *FType, SIMachineFunctionInfo *Info)
static void getCoopAtomicOperandsInfo(const CallInst &CI, bool IsLoad, TargetLoweringBase::IntrinsicInfo &Info)
static uint64_t getIdentityValueFor64BitWaveReduction(unsigned Opc)
static SDValue selectSOffset(SDValue SOffset, SelectionDAG &DAG, const GCNSubtarget *Subtarget)
static SDValue getLoadExtOrTrunc(SelectionDAG &DAG, ISD::LoadExtType ExtType, SDValue Op, const SDLoc &SL, EVT VT)
static bool globalMemoryFPAtomicIsLegal(const GCNSubtarget &Subtarget, const AtomicRMWInst *RMW, bool HasSystemScope)
static void fixMasks(SmallVectorImpl< DotSrc > &Srcs, unsigned ChainLength)
static bool is32bitWaveReduceOperation(unsigned Opc)
static TargetLowering::AtomicExpansionKind atomicSupportedIfLegalIntType(const AtomicRMWInst *RMW)
static SDValue strictFPExtFromF16(SelectionDAG &DAG, SDValue Src)
Return the source of an fp_extend from f16 to f32, or a converted FP constant.
static bool isAtomicRMWLegalXChgTy(const AtomicRMWInst *RMW)
static bool bitOpWithConstantIsReducible(unsigned Opc, uint32_t Val)
static void convertScratchAtomicToFlatAtomic(Instruction *I, unsigned PtrOpIdx)
static bool isCopyFromRegOfInlineAsm(const SDNode *N)
static bool elementPairIsOddToEven(ArrayRef< int > Mask, int Elt)
static cl::opt< bool > DisableLoopAlignment("amdgpu-disable-loop-alignment", cl::desc("Do not align and prefetch loops"), cl::init(false))
static SDValue getDWordFromOffset(SelectionDAG &DAG, SDLoc SL, SDValue Src, unsigned DWordOffset)
static MachineBasicBlock::iterator loadM0FromVGPR(const SIInstrInfo *TII, MachineBasicBlock &MBB, MachineInstr &MI, unsigned InitResultReg, unsigned PhiReg, int Offset, bool UseGPRIdxMode, Register &SGPRIdxReg)
static bool isImmConstraint(StringRef Constraint)
static SDValue padEltsToUndef(SelectionDAG &DAG, const SDLoc &DL, EVT CastVT, SDValue Src, int ExtraElts)
static SDValue lowerICMPIntrinsic(const SITargetLowering &TLI, SDNode *N, SelectionDAG &DAG)
static bool hasCFUser(const Value *V, SmallPtrSet< const Value *, 16 > &Visited, unsigned WaveSize)
static OptimizationRemark emitAtomicRMWLegalRemark(const AtomicRMWInst *RMW)
static unsigned SubIdx2Lane(unsigned Idx)
Helper function for adjustWritemask.
static TargetLowering::AtomicExpansionKind getPrivateAtomicExpansionKind(const GCNSubtarget &STI)
static bool addressMayBeAccessedAsPrivate(const MachineMemOperand *MMO, const SIMachineFunctionInfo &Info)
static MachineBasicBlock * lowerWaveReduce(MachineInstr &MI, MachineBasicBlock &BB, const GCNSubtarget &ST, unsigned Opc)
static bool elementPairIsContiguous(ArrayRef< int > Mask, int Elt)
static bool isV2BF16(Type *Ty)
static ArgDescriptor allocateSGPR32InputImpl(CCState &CCInfo, const TargetRegisterClass *RC, unsigned NumArgRegs)
static SDValue getMad64_32(SelectionDAG &DAG, const SDLoc &SL, EVT VT, SDValue N0, SDValue N1, SDValue N2, bool Signed)
static SDValue resolveSources(SelectionDAG &DAG, SDLoc SL, SmallVectorImpl< DotSrc > &Srcs, bool IsSigned, bool IsAny)
static bool hasNon16BitAccesses(uint64_t PermMask, SDValue &Op, SDValue &OtherOp)
static void placeSources(ByteProvider< SDValue > &Src0, ByteProvider< SDValue > &Src1, SmallVectorImpl< DotSrc > &Src0s, SmallVectorImpl< DotSrc > &Src1s, int Step)
static EVT memVTFromLoadIntrReturn(const SITargetLowering &TLI, const DataLayout &DL, Type *Ty, unsigned MaxNumLanes)
static MachineBasicBlock::iterator emitLoadM0FromVGPRLoop(const SIInstrInfo *TII, MachineRegisterInfo &MRI, MachineBasicBlock &OrigBB, MachineBasicBlock &LoopBB, const DebugLoc &DL, const MachineOperand &Idx, unsigned InitReg, unsigned ResultReg, unsigned PhiReg, unsigned InitSaveExecReg, int Offset, bool UseGPRIdxMode, Register &SGPRIdxReg)
static SDValue matchPERM(SDNode *N, TargetLowering::DAGCombinerInfo &DCI)
static bool isFrameIndexOp(SDValue Op)
static ConstantFPSDNode * getSplatConstantFP(SDValue Op)
static void allocateSGPR32Input(CCState &CCInfo, ArgDescriptor &Arg)
static void knownBitsForSBFE(const MachineInstr &MI, GISelValueTracking &VT, KnownBits &Known, const APInt &DemandedElts, unsigned BFEWidth, bool SExt, unsigned Depth)
static bool isExtendedFrom16Bits(SDValue &Operand)
static std::optional< bool > checkDot4MulSignedness(const SDValue &N, ByteProvider< SDValue > &Src0, ByteProvider< SDValue > &Src1, const SDValue &S0Op, const SDValue &S1Op, const SelectionDAG &DAG)
static bool vectorEltWillFoldAway(SDValue Op)
static SDValue getSPDenormModeValue(uint32_t SPDenormMode, SelectionDAG &DAG, const SIMachineFunctionInfo *Info, const GCNSubtarget *ST)
static uint32_t getConstantPermuteMask(uint32_t C)
static MachineBasicBlock * emitIndirectDst(MachineInstr &MI, MachineBasicBlock &MBB, const GCNSubtarget &ST)
static void setM0ToIndexFromSGPR(const SIInstrInfo *TII, MachineRegisterInfo &MRI, MachineInstr &MI, int Offset)
static ArgDescriptor allocateVGPR32Input(CCState &CCInfo, unsigned Mask=~0u, ArgDescriptor Arg=ArgDescriptor())
static MachineBasicBlock * Expand64BitScalarArithmetic(MachineInstr &MI, MachineBasicBlock *BB)
static std::pair< MachineBasicBlock *, MachineBasicBlock * > splitBlockForLoop(MachineInstr &MI, MachineBasicBlock &MBB, bool InstInLoop)
static uint32_t getIdentityValueFor32BitWaveReduction(unsigned Opc)
static unsigned getBasePtrIndex(const MemSDNode *N)
MemSDNode::getBasePtr() does not work for intrinsics, which needs to offset by the chain and intrinsi...
static void allocateFixedSGPRInputImpl(CCState &CCInfo, const TargetRegisterClass *RC, MCRegister Reg)
static SDValue constructRetValue(SelectionDAG &DAG, MachineSDNode *Result, ArrayRef< EVT > ResultTypes, bool IsTexFail, bool Unpacked, bool IsD16, int DMaskPop, int NumVDataDwords, bool IsAtomicPacked16Bit, const SDLoc &DL)
static std::optional< ByteProvider< SDValue > > handleMulOperand(const SDValue &MulOperand)
static SDValue lowerFCMPIntrinsic(const SITargetLowering &TLI, SDNode *N, SelectionDAG &DAG)
static Register getIndirectSGPRIdx(const SIInstrInfo *TII, MachineRegisterInfo &MRI, MachineInstr &MI, int Offset)
static SDValue emitNonHSAIntrinsicError(SelectionDAG &DAG, const SDLoc &DL, EVT VT)
static EVT memVTFromLoadIntrData(const SITargetLowering &TLI, const DataLayout &DL, Type *Ty, unsigned MaxNumLanes)
static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc)
static unsigned getExtOpcodeForPromotedOp(SDValue Op)
static SDValue lowerBALLOTIntrinsic(const SITargetLowering &TLI, SDNode *N, SelectionDAG &DAG)
static SDValue buildSMovImm32(SelectionDAG &DAG, const SDLoc &DL, uint64_t Val)
static SDValue tryFoldMADwithSRL(SelectionDAG &DAG, const SDLoc &SL, SDValue MulLHS, SDValue MulRHS, SDValue AddRHS)
static unsigned getIntrMemWidth(unsigned IntrID)
static SDValue getBuildDwordsVector(SelectionDAG &DAG, SDLoc DL, ArrayRef< SDValue > Elts)
static SDNode * findUser(SDValue Value, unsigned Opcode)
Helper function for LowerBRCOND.
static unsigned addPermMasks(unsigned First, unsigned Second)
static uint64_t clearUnusedBits(uint64_t Val, unsigned Size)
static SDValue getFPTernOp(SelectionDAG &DAG, unsigned Opcode, const SDLoc &SL, EVT VT, SDValue A, SDValue B, SDValue C, SDValue GlueChain, SDNodeFlags Flags)
static bool isV2F16OrV2BF16(Type *Ty)
static bool atomicIgnoresDenormalModeOrFPModeIsFTZ(const AtomicRMWInst *RMW)
static SDValue emitRemovedIntrinsicError(SelectionDAG &DAG, const SDLoc &DL, EVT VT)
static SDValue getFPBinOp(SelectionDAG &DAG, unsigned Opcode, const SDLoc &SL, EVT VT, SDValue A, SDValue B, SDValue GlueChain, SDNodeFlags Flags)
static SDValue buildPCRelGlobalAddress(SelectionDAG &DAG, const GlobalValue *GV, const SDLoc &DL, int64_t Offset, EVT PtrVT, unsigned GAFlags=SIInstrInfo::MO_NONE)
static cl::opt< bool > UseDivergentRegisterIndexing("amdgpu-use-divergent-register-indexing", cl::Hidden, cl::desc("Use indirect register addressing for divergent indexes"), cl::init(false))
static const std::optional< ByteProvider< SDValue > > calculateSrcByte(const SDValue Op, uint64_t DestByte, uint64_t SrcIndex=0, unsigned Depth=0)
static bool isV2F16(Type *Ty)
static void allocateSGPR64Input(CCState &CCInfo, ArgDescriptor &Arg)
SI DAG Lowering interface definition.
Interface definition for SIRegisterInfo.
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:480
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:114
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
LLVM IR instance of the generic uniformity analysis.
static constexpr int Concat[]
Value * RHS
Value * LHS
The Input class is used to parse a yaml document into in-memory structs and vectors.
static const AMDGPUFunctionArgInfo FixedABIFunctionInfo
void setFuncArgInfo(const Function &F, const AMDGPUFunctionArgInfo &ArgInfo)
static std::optional< uint32_t > getLDSKernelIdMetadata(const Function &F)
void setDynLDSAlign(const Function &F, const GlobalVariable &GV)
unsigned getWavefrontSize() const
static unsigned numBitsSigned(SDValue Op, SelectionDAG &DAG)
SDValue SplitVectorLoad(SDValue Op, SelectionDAG &DAG) const
Split a vector load into 2 loads of half the vector.
void analyzeFormalArgumentsCompute(CCState &State, const SmallVectorImpl< ISD::InputArg > &Ins) const
The SelectionDAGBuilder will automatically promote function arguments with illegal types.
SDValue LowerF64ToF16Safe(SDValue Src, const SDLoc &DL, SelectionDAG &DAG) const
SDValue storeStackInputValue(SelectionDAG &DAG, const SDLoc &SL, SDValue Chain, SDValue ArgVal, int64_t Offset) const
void computeKnownBitsForTargetNode(const SDValue Op, KnownBits &Known, const APInt &DemandedElts, const SelectionDAG &DAG, unsigned Depth=0) const override
Determine which of the bits specified in Mask are known to be either zero or one and return them in t...
SDValue splitBinaryBitConstantOpImpl(DAGCombinerInfo &DCI, const SDLoc &SL, unsigned Opc, SDValue LHS, uint32_t ValLo, uint32_t ValHi) const
Split the 64-bit value LHS into two 32-bit components, and perform the binary operation Opc to it wit...
SDValue lowerUnhandledCall(CallLoweringInfo &CLI, SmallVectorImpl< SDValue > &InVals, StringRef Reason) const
SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override
This callback is invoked for operations that are unsupported by the target, which are registered to u...
SDValue addTokenForArgument(SDValue Chain, SelectionDAG &DAG, MachineFrameInfo &MFI, int ClobberedFI) const
bool isKnownNeverNaNForTargetNode(SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG, bool SNaN=false, unsigned Depth=0) const override
If SNaN is false,.
static bool needsDenormHandlingF32(const SelectionDAG &DAG, SDValue Src, SDNodeFlags Flags)
uint32_t getImplicitParameterOffset(const MachineFunction &MF, const ImplicitParameter Param) const
Helper function that returns the byte offset of the given type of implicit parameter.
SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const
virtual SDValue LowerGlobalAddress(AMDGPUMachineFunction *MFI, SDValue Op, SelectionDAG &DAG) const
SDValue loadInputValue(SelectionDAG &DAG, const TargetRegisterClass *RC, EVT VT, const SDLoc &SL, const ArgDescriptor &Arg) const
static EVT getEquivalentMemType(LLVMContext &Context, EVT VT)
SDValue CreateLiveInRegister(SelectionDAG &DAG, const TargetRegisterClass *RC, Register Reg, EVT VT, const SDLoc &SL, bool RawReg=false) const
Helper function that adds Reg to the LiveIn list of the DAG's MachineFunction.
SDValue SplitVectorStore(SDValue Op, SelectionDAG &DAG) const
Split a vector store into 2 stores of half the vector.
std::pair< SDValue, SDValue > split64BitValue(SDValue Op, SelectionDAG &DAG) const
Return 64-bit value Op as two 32-bit integers.
AMDGPUTargetLowering(const TargetMachine &TM, const AMDGPUSubtarget &STI)
static CCAssignFn * CCAssignFnForReturn(CallingConv::ID CC, bool IsVarArg)
static CCAssignFn * CCAssignFnForCall(CallingConv::ID CC, bool IsVarArg)
Selects the correct CCAssignFn for a given CallingConvention value.
static unsigned numBitsUnsigned(SDValue Op, SelectionDAG &DAG)
static bool allowApproxFunc(const SelectionDAG &DAG, SDNodeFlags Flags)
SDValue LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool isVarArg, const SmallVectorImpl< ISD::OutputArg > &Outs, const SmallVectorImpl< SDValue > &OutVals, const SDLoc &DL, SelectionDAG &DAG) const override
This hook must be implemented to lower outgoing return values, described by the Outs array,...
void ReplaceNodeResults(SDNode *N, SmallVectorImpl< SDValue > &Results, SelectionDAG &DAG) const override
This callback is invoked when a node result type is illegal for the target, and the operation was reg...
SDValue performRcpCombine(SDNode *N, DAGCombinerInfo &DCI) const
static bool shouldFoldFNegIntoSrc(SDNode *FNeg, SDValue FNegSrc)
SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const override
This method will be invoked for all target nodes and for any target-independent nodes that the target...
SDValue WidenOrSplitVectorLoad(SDValue Op, SelectionDAG &DAG) const
Widen a suitably aligned v3 load.
SDValue getHiHalf64(SDValue Op, SelectionDAG &DAG) const
bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const override
Returns true if a cast between SrcAS and DestAS is a noop.
const std::array< unsigned, 3 > & getDims() const
static const LaneMaskConstants & get(const GCNSubtarget &ST)
static constexpr roundingMode rmNearestTiesToEven
Definition APFloat.h:344
static const fltSemantics & IEEEhalf()
Definition APFloat.h:294
static APFloat getQNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for QNaN values.
Definition APFloat.h:1102
LLVM_ABI opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition APFloat.cpp:6060
LLVM_READONLY int getExactLog2Abs() const
Definition APFloat.h:1479
bool isNegative() const
Definition APFloat.h:1431
bool isNormal() const
Definition APFloat.h:1435
APInt bitcastToAPInt() const
Definition APFloat.h:1335
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1120
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition APFloat.h:1080
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
Definition APFloat.h:1061
bool isInfinity() const
Definition APFloat.h:1428
Class for arbitrary precision integers.
Definition APInt.h:78
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition APInt.h:1391
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition APInt.h:1385
static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit)
Get a value with a block of bits set.
Definition APInt.h:258
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:380
bool isSignMask() const
Check if the APInt's value is returned by getSignMask.
Definition APInt.h:466
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition APInt.h:1639
bool isOneBitSet(unsigned BitNo) const
Determine if this APInt Value only has the specified bit set.
Definition APInt.h:366
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition APInt.h:296
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition APInt.h:1237
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1221
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
LLVM_ABI bool hasAttribute(Attribute::AttrKind Kind) const
Check if an argument has a given attribute.
Definition Function.cpp:339
const Function * getParent() const
Definition Argument.h:44
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition ArrayRef.h:147
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:142
An instruction that atomically checks whether a specified value is in a memory location,...
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
Align getAlign() const
Return the alignment of the memory that is being allocated by the instruction.
static unsigned getPointerOperandIndex()
an instruction that atomically reads a memory location, combines it with another value,...
Align getAlign() const
Return the alignment of the memory that is being allocated by the instruction.
static unsigned getPointerOperandIndex()
BinOp
This enumeration lists the possible modifications atomicrmw can make.
@ Add
*p = old + v
@ FAdd
*p = old + v
@ Min
*p = old <signed v ? old : v
@ Sub
*p = old - v
@ And
*p = old & v
@ Xor
*p = old ^ v
@ FSub
*p = old - v
@ UIncWrap
Increment one up to a maximum value.
@ Max
*p = old >signed v ? old : v
@ UMin
*p = old <unsigned v ? old : v
@ FMin
*p = minnum(old, v) minnum matches the behavior of llvm.minnum.
@ UMax
*p = old >unsigned v ? old : v
@ FMax
*p = maxnum(old, v) maxnum matches the behavior of llvm.maxnum.
@ UDecWrap
Decrement one until a minimum value or zero.
@ Nand
*p = ~(old & v)
Value * getPointerOperand()
void setOperation(BinOp Operation)
BinOp getOperation() const
SyncScope::ID getSyncScopeID() const
Returns the synchronization scope ID of this rmw instruction.
static LLVM_ABI StringRef getOperationName(BinOp Op)
AtomicOrdering getOrdering() const
Returns the ordering constraint of this rmw instruction.
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
bool isCompareAndSwap() const
Returns true if this SDNode represents cmpxchg atomic operation, false otherwise.
This class holds the attributes for a particular argument, parameter, function, or return value.
Definition Attributes.h:361
LLVM_ABI MemoryEffects getMemoryEffects() const
LLVM_ABI bool getValueAsBool() const
Return the attribute's value as a boolean.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator end()
Definition BasicBlock.h:472
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition BasicBlock.h:206
LLVM_ABI BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="", bool Before=false)
Split the basic block into two basic blocks at the specified instruction.
BitVector & set()
Definition BitVector.h:370
A "pseudo-class" with methods for operating on BUILD_VECTORs.
Represents known origin of an individual byte in combine pattern.
static ByteProvider getConstantZero()
static ByteProvider getSrc(std::optional< ISelOp > Val, int64_t ByteOffset, int64_t VectorOffset)
std::optional< ISelOp > Src
CCState - This class holds information needed while lowering arguments and return values.
MachineFunction & getMachineFunction() const
unsigned getFirstUnallocated(ArrayRef< MCPhysReg > Regs) const
getFirstUnallocated - Return the index of the first unallocated register in the set,...
static LLVM_ABI bool resultsCompatible(CallingConv::ID CalleeCC, CallingConv::ID CallerCC, MachineFunction &MF, LLVMContext &C, const SmallVectorImpl< ISD::InputArg > &Ins, CCAssignFn CalleeFn, CCAssignFn CallerFn)
Returns true if the results of the two calling conventions are compatible.
LLVM_ABI void AnalyzeCallResult(const SmallVectorImpl< ISD::InputArg > &Ins, CCAssignFn Fn)
AnalyzeCallResult - Analyze the return values of a call, incorporating info about the passed values i...
MCRegister AllocateReg(MCPhysReg Reg)
AllocateReg - Attempt to allocate one register.
LLVM_ABI bool CheckReturn(const SmallVectorImpl< ISD::OutputArg > &Outs, CCAssignFn Fn)
CheckReturn - Analyze the return values of a function, returning true if the return can be performed ...
LLVM_ABI void AnalyzeReturn(const SmallVectorImpl< ISD::OutputArg > &Outs, CCAssignFn Fn)
AnalyzeReturn - Analyze the returned values of a return, incorporating info about the result values i...
int64_t AllocateStack(unsigned Size, Align Alignment)
AllocateStack - Allocate a chunk of stack space with the specified size and alignment.
LLVM_ABI void AnalyzeCallOperands(const SmallVectorImpl< ISD::OutputArg > &Outs, CCAssignFn Fn)
AnalyzeCallOperands - Analyze the outgoing arguments to a call, incorporating info about the passed v...
uint64_t getStackSize() const
Returns the size of the currently allocated portion of the stack.
bool isAllocated(MCRegister Reg) const
isAllocated - Return true if the specified register (or an alias) is allocated.
LLVM_ABI void AnalyzeFormalArguments(const SmallVectorImpl< ISD::InputArg > &Ins, CCAssignFn Fn)
AnalyzeFormalArguments - Analyze an array of argument values, incorporating info about the formals in...
CCValAssign - Represent assignment of one arg/retval to a location.
Register getLocReg() const
LocInfo getLocInfo() const
int64_t getLocMemOffset() const
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
bool hasFnAttr(Attribute::AttrKind Kind) const
Determine whether this call has the given attribute.
LLVM_ABI bool isMustTailCall() const
Tests if this call site must be tail call optimized.
Value * getArgOperand(unsigned i) const
unsigned arg_size() const
This class represents a function call, abstracting a target machine's calling convention.
bool isTailCall() const
static LLVM_ABI CastInst * CreatePointerCast(Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Create a BitCast, AddrSpaceCast or a PtrToInt cast instruction.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:676
@ ICMP_NE
not equal
Definition InstrTypes.h:698
bool isSigned() const
Definition InstrTypes.h:930
static bool isFPPredicate(Predicate P)
Definition InstrTypes.h:770
static bool isIntPredicate(Predicate P)
Definition InstrTypes.h:776
const APFloat & getValueAPF() const
bool isExactlyValue(double V) const
We don't rely on operator== working on double values, as it returns true for things that are clearly ...
bool isNegative() const
Return true if the value is negative.
bool isInfinity() const
Return true if the value is an infinity.
This is the shared class of boolean and integer constants.
Definition Constants.h:87
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition Constants.h:214
uint64_t getZExtValue() const
const APInt & getAPIntValue() const
This is an important base class in LLVM.
Definition Constant.h:43
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
LLVM_ABI Align getABITypeAlign(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
bool isBigEndian() const
Definition DataLayout.h:208
TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
A debug info location.
Definition DebugLoc.h:124
Diagnostic information for unsupported feature in backend.
Class to represent fixed width SIMD vectors.
unsigned getNumElements() const
FunctionLoweringInfo - This contains information that is global to a function that is used when lower...
Register DemoteRegister
DemoteRegister - if CanLowerReturn is false, DemoteRegister is a vreg allocated to hold a pointer to ...
const Value * getValueFromVirtualReg(Register Vreg)
This method is called from TargetLowerinInfo::isSDNodeSourceOfDivergence to get the Value correspondi...
Class to represent function types.
Type * getParamType(unsigned i) const
Parameter type accessors.
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition Function.h:209
const DataLayout & getDataLayout() const
Get the data layout of the module this function belongs to.
Definition Function.cpp:363
iterator_range< arg_iterator > args()
Definition Function.h:890
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition Function.cpp:762
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition Function.h:270
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition Function.cpp:359
DenormalMode getDenormalMode(const fltSemantics &FPType) const
Returns the denormal handling type for the default rounding mode of the function.
Definition Function.cpp:803
Argument * getArg(unsigned i) const
Definition Function.h:884
bool hasMinimum3Maximum3F32() const
bool supportsAgentScopeFineGrainedRemoteMemoryAtomics() const
const SIInstrInfo * getInstrInfo() const override
bool hasMadF16() const
bool hasMin3Max3PKF16() const
const SIRegisterInfo * getRegisterInfo() const override
bool hasMinimum3Maximum3PKF16() const
bool hasGloballyAddressableScratch() const
bool hasMinimum3Maximum3F16() const
bool hasRestrictedSOffset() const
bool hasMin3Max3_16() const
bool hasEmulatedSystemScopeAtomics() const
unsigned getKnownHighZeroBitsForFrameIndex() const
Return the number of high bits known to be zero for a frame index.
bool hasShaderCyclesHiLoRegisters() const
unsigned getMaxPrivateElementSize(bool ForBufferRSrc=false) const
bool hasPrivateSegmentBuffer() const
const MachineFunction & getMachineFunction() const
void computeKnownBitsImpl(Register R, KnownBits &Known, const APInt &DemandedElts, unsigned Depth=0)
bool isDivergent(ConstValueRefT V) const
Whether V is divergent at its definition.
LLVM_ABI unsigned getAddressSpace() const
const GlobalValue * getGlobal() const
bool hasExternalLinkage() const
unsigned getAddressSpace() const
Module * getParent()
Get the module that this global value is contained inside of...
Type * getValueType() const
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2788
LLVM_ABI Instruction * clone() const
Create a copy of 'this' instruction that is identical in all ways except the following:
LLVM_ABI void removeFromParent()
This method unlinks 'this' from the containing basic block, but does not delete it.
bool hasMetadata() const
Return true if this instruction has any metadata attached to it.
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
LLVM_ABI void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
LLVM_ABI void copyMetadata(const Instruction &SrcInst, ArrayRef< unsigned > WL=ArrayRef< unsigned >())
Copy metadata from SrcInst to this instruction.
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
LLVM_ABI InstListType::iterator insertInto(BasicBlock *ParentBB, InstListType::iterator It)
Inserts an unlinked instruction into ParentBB at position It and returns the iterator of the inserted...
Class to represent integer types.
A wrapper class for inspecting calls to intrinsic functions.
constexpr unsigned getScalarSizeInBits() const
static constexpr LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
static constexpr LLT pointer(unsigned AddressSpace, unsigned SizeInBits)
Get a low-level pointer in the given address space.
constexpr TypeSize getSizeInBits() const
Returns the total size of the type. Must only be called on sized types.
constexpr LLT changeElementSize(unsigned NewEltSize) const
If this type is a vector, return a vector with the same number of elements but the new element size.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
LLVM_ABI void emitError(const Instruction *I, const Twine &ErrorStr)
emitError - Emit an error message to the currently installed error handler with optional location inf...
LLVM_ABI void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
LLVM_ABI SyncScope::ID getOrInsertSyncScopeID(StringRef SSN)
getOrInsertSyncScopeID - Maps synchronization scope name to synchronization scope ID.
An instruction for reading from memory.
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
void setAtomic(AtomicOrdering Ordering, SyncScope::ID SSID=SyncScope::System)
Sets the ordering constraint and the synchronization scope ID of this load instruction.
static unsigned getPointerOperandIndex()
This class is used to represent ISD::LOAD nodes.
const SDValue & getBasePtr() const
const SDValue & getOffset() const
ISD::LoadExtType getExtensionType() const
Return whether this is a plain node, or one of the varieties of value-extending loads.
Describe properties that are true of each instruction in the target description file.
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:33
LLVM_ABI MDNode * createRange(const APInt &Lo, const APInt &Hi)
Return metadata describing the range [Lo, Hi).
Definition MDBuilder.cpp:96
Metadata node.
Definition Metadata.h:1078
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1442
Helper class for constructing bundles of MachineInstrs.
MachineBasicBlock::instr_iterator begin() const
Return an iterator to the first bundled instruction.
Machine Value Type.
SimpleValueType SimpleTy
uint64_t getScalarSizeInBits() const
bool bitsLE(MVT VT) const
Return true if this has no more bits than VT.
unsigned getVectorNumElements() const
bool isVector() const
Return true if this is a vector value type.
bool isScalableVector() const
Return true if this is a vector value type where the runtime length is machine dependent.
static LLVM_ABI MVT getVT(Type *Ty, bool HandleUnknown=false)
Return the value type corresponding to the specified type.
TypeSize getSizeInBits() const
Returns the size of the specified MVT in bits.
bool isPow2VectorType() const
Returns true if the given vector is a power of 2.
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
static MVT getVectorVT(MVT VT, unsigned NumElements)
static MVT getIntegerVT(unsigned BitWidth)
MVT getScalarType() const
If this is a vector, return the element type, otherwise return this.
LLVM_ABI void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB)
Transfers all the successors, as in transferSuccessors, and update PHI operands in the successor bloc...
LLVM_ABI iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
LLVM_ABI void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
LLVM_ABI MachineBasicBlock * splitAt(MachineInstr &SplitInst, bool UpdateLiveIns=true, LiveIntervals *LIS=nullptr)
Split a basic block into 2 pieces at SplitPoint.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
MachineInstrBundleIterator< MachineInstr > iterator
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
LLVM_ABI int CreateFixedObject(uint64_t Size, int64_t SPOffset, bool IsImmutable, bool isAliased=false)
Create a new object at a fixed location on the stack.
bool hasCalls() const
Return true if the current function has any function calls.
void setHasTailCall(bool V=true)
void setReturnAddressIsTaken(bool s)
bool hasStackObjects() const
Return true if there are any stack objects in this function.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, LLT MemTy, Align base_alignment, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr, SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
DenormalMode getDenormalMode(const fltSemantics &FPType) const
Returns the denormal handling type for the default rounding mode of the function.
void push_back(MachineBasicBlock *MBB)
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
const DataLayout & getDataLayout() const
Return the DataLayout attached to the Module associated to this MF.
Function & getFunction()
Return the LLVM function that this machine code represents.
BasicBlockListType::iterator iterator
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
Register addLiveIn(MCRegister PReg, const TargetRegisterClass *RC)
addLiveIn - Add the specified physical register as a live-in value and create a corresponding virtual...
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineInstr - Allocate a new MachineInstr.
void insert(iterator MBBI, MachineBasicBlock *MBB)
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
const MachineInstrBuilder & setOperandDead(unsigned OpIdx) const
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
const MachineInstrBuilder & cloneMemRefs(const MachineInstr &OtherMI) const
Representation of each machine instruction.
const MachineOperand & getOperand(unsigned i) const
A description of a memory reference used in the backend.
Flags
Flags values. These may be or'd together.
@ MOVolatile
The memory access is volatile.
@ MODereferenceable
The memory access is dereferenceable (i.e., doesn't trap).
@ MOLoad
The memory access reads data.
@ MONonTemporal
The memory access is non-temporal.
@ MOInvariant
The memory access always returns the same value (or traps).
@ MOStore
The memory access writes data.
const MachinePointerInfo & getPointerInfo() const
Flags getFlags() const
Return the raw flags of the source value,.
AAMDNodes getAAInfo() const
Return the AA tags for the memory reference.
Align getBaseAlign() const
Return the minimum known alignment in bytes of the base address, without the offset.
MachineOperand class - Representation of each machine instruction operand.
unsigned getSubReg() const
int64_t getImm() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
LLVM_ABI void setReg(Register Reg)
Change the register this operand corresponds to.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
static MachineOperand CreateImm(int64_t Val)
void setIsUndef(bool Val=true)
Register getReg() const
getReg - Returns the register number.
static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI void clearKillFlags(Register Reg) const
clearKillFlags - Iterate over all the uses of the given register and clear the kill flag from the Mac...
LLVM_ABI void setType(Register VReg, LLT Ty)
Set the low-level type of VReg to Ty.
An SDNode that represents everything that will be needed to construct a MachineInstr.
This is an abstract virtual class for memory operations.
unsigned getAddressSpace() const
Return the address space for the associated pointer.
Align getAlign() const
AAMDNodes getAAInfo() const
Returns the AA info that describes the dereference.
MachineMemOperand * getMemOperand() const
Return a MachineMemOperand object describing the memory reference performed by operation.
const MachinePointerInfo & getPointerInfo() const
const SDValue & getChain() const
bool isInvariant() const
EVT getMemoryVT() const
Return the type of the in-memory value.
bool onlyWritesMemory() const
Whether this function only (at most) writes memory.
Definition ModRef.h:221
bool doesNotAccessMemory() const
Whether this function accesses no memory.
Definition ModRef.h:215
bool onlyReadsMemory() const
Whether this function only (at most) reads memory.
Definition ModRef.h:218
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition Module.h:278
The optimization diagnostic interface.
LLVM_ABI void emit(DiagnosticInfoOptimizationBase &OptDiag)
Output the remark via the diagnostic handler and to the optimization record file.
Diagnostic information for applied optimization remarks.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
static LLVM_ABI PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Wrapper class representing virtual and physical registers.
Definition Register.h:19
static Register index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
Definition Register.h:67
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:78
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
Represents one node in the SelectionDAG.
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
bool isDivergent() const
bool hasOneUse() const
Return true if there is exactly one use of this node.
value_iterator value_end() const
SDNodeFlags getFlags() const
uint64_t getAsZExtVal() const
Helper method returns the zero-extended integer value of a ConstantSDNode.
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
SDVTList getVTList() const
const SDValue & getOperand(unsigned Num) const
uint64_t getConstantOperandVal(unsigned Num) const
Helper method returns the integer value of a ConstantSDNode operand.
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
user_iterator user_begin() const
Provide iteration support to walk over all users of an SDNode.
op_iterator op_end() const
bool isAnyAdd() const
Returns true if the node type is ADD or PTRADD.
value_iterator value_begin() const
op_iterator op_begin() const
Represents a use of a SDNode.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
bool isUndef() const
SDNode * getNode() const
get the SDNode which holds the desired result
bool hasOneUse() const
Return true if there is exactly one node using value ResNo of Node.
SDValue getValue(unsigned R) const
EVT getValueType() const
Return the ValueType of the referenced return value.
bool isMachineOpcode() const
TypeSize getValueSizeInBits() const
Returns the size of the value in bits.
const SDValue & getOperand(unsigned i) const
MVT getSimpleValueType() const
Return the simple ValueType of the referenced return value.
unsigned getMachineOpcode() const
unsigned getOpcode() const
static unsigned getMaxMUBUFImmOffset(const GCNSubtarget &ST)
static unsigned getDSShaderTypeValue(const MachineFunction &MF)
This class keeps track of the SPI_SP_INPUT_ADDR config register, which tells the hardware which inter...
AMDGPU::ClusterDimsAttr getClusterDims() const
std::tuple< const ArgDescriptor *, const TargetRegisterClass *, LLT > getPreloadedValue(AMDGPUFunctionArgInfo::PreloadedValue Value) const
const AMDGPUGWSResourcePseudoSourceValue * getGWSPSV(const AMDGPUTargetMachine &TM)
static unsigned getSubRegFromChannel(unsigned Channel, unsigned NumRegs=1)
static LLVM_READONLY const TargetRegisterClass * getSGPRClassForBitWidth(unsigned BitWidth)
static bool isVGPRClass(const TargetRegisterClass *RC)
static bool isSGPRClass(const TargetRegisterClass *RC)
static bool isAGPRClass(const TargetRegisterClass *RC)
bool isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const override
Return true if folding a constant offset with the given GlobalAddress is legal.
bool isTypeDesirableForOp(unsigned Op, EVT VT) const override
Return true if the target has native support for the specified value type and it is 'desirable' to us...
SDNode * PostISelFolding(MachineSDNode *N, SelectionDAG &DAG) const override
Fold the instructions after selecting them.
SDValue splitTernaryVectorOp(SDValue Op, SelectionDAG &DAG) const
MachineSDNode * wrapAddr64Rsrc(SelectionDAG &DAG, const SDLoc &DL, SDValue Ptr) const
bool isFMAFasterThanFMulAndFAdd(const MachineFunction &MF, EVT VT) const override
Return true if an FMA operation is faster than a pair of fmul and fadd instructions.
SDValue lowerGET_ROUNDING(SDValue Op, SelectionDAG &DAG) const
bool requiresUniformRegister(MachineFunction &MF, const Value *V) const override
Allows target to decide about the register class of the specific value that is live outside the defin...
bool isFMADLegal(const SelectionDAG &DAG, const SDNode *N) const override
Returns true if be combined with to form an ISD::FMAD.
AtomicExpansionKind shouldExpandAtomicStoreInIR(StoreInst *SI) const override
Returns how the given (atomic) store should be expanded by the IR-level AtomicExpand pass into.
void bundleInstWithWaitcnt(MachineInstr &MI) const
Insert MI into a BUNDLE with an S_WAITCNT 0 immediately following it.
SDValue lowerROTR(SDValue Op, SelectionDAG &DAG) const
MVT getScalarShiftAmountTy(const DataLayout &, EVT) const override
Return the type to use for a scalar shift opcode, given the shifted amount type.
SDValue LowerCall(CallLoweringInfo &CLI, SmallVectorImpl< SDValue > &InVals) const override
This hook must be implemented to lower calls into the specified DAG.
MVT getPointerTy(const DataLayout &DL, unsigned AS) const override
Map address space 7 to MVT::amdgpuBufferFatPointer because that's its in-memory representation.
bool denormalsEnabledForType(const SelectionDAG &DAG, EVT VT) const
void insertCopiesSplitCSR(MachineBasicBlock *Entry, const SmallVectorImpl< MachineBasicBlock * > &Exits) const override
Insert explicit copies in entry and exit blocks.
EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context, EVT VT) const override
Return the ValueType of the result of SETCC operations.
SDNode * legalizeTargetIndependentNode(SDNode *Node, SelectionDAG &DAG) const
Legalize target independent instructions (e.g.
bool allowsMisalignedMemoryAccessesImpl(unsigned Size, unsigned AddrSpace, Align Alignment, MachineMemOperand::Flags Flags=MachineMemOperand::MONone, unsigned *IsFast=nullptr) const
TargetLoweringBase::LegalizeTypeAction getPreferredVectorAction(MVT VT) const override
Return the preferred vector type legalization action.
SDValue lowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const
const GCNSubtarget * getSubtarget() const
bool enableAggressiveFMAFusion(EVT VT) const override
Return true if target always benefits from combining into FMA for a given value type.
bool shouldEmitGOTReloc(const GlobalValue *GV) const
bool isCanonicalized(SelectionDAG &DAG, SDValue Op, unsigned MaxDepth=5) const
void CollectTargetIntrinsicOperands(const CallInst &I, SmallVectorImpl< SDValue > &Ops, SelectionDAG &DAG) const override
SDValue splitUnaryVectorOp(SDValue Op, SelectionDAG &DAG) const
SDValue lowerGET_FPENV(SDValue Op, SelectionDAG &DAG) const
void allocateSpecialInputSGPRs(CCState &CCInfo, MachineFunction &MF, const SIRegisterInfo &TRI, SIMachineFunctionInfo &Info) const
void allocateLDSKernelId(CCState &CCInfo, MachineFunction &MF, const SIRegisterInfo &TRI, SIMachineFunctionInfo &Info) const
SDValue LowerSTACKSAVE(SDValue Op, SelectionDAG &DAG) const
bool isReassocProfitable(SelectionDAG &DAG, SDValue N0, SDValue N1) const override
void allocateHSAUserSGPRs(CCState &CCInfo, MachineFunction &MF, const SIRegisterInfo &TRI, SIMachineFunctionInfo &Info) const
ArrayRef< MCPhysReg > getRoundingControlRegisters() const override
Returns a 0 terminated array of rounding control registers that can be attached into strict FP call.
ConstraintType getConstraintType(StringRef Constraint) const override
Given a constraint, return the type of constraint it is for this target.
SDValue LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg, const SmallVectorImpl< ISD::OutputArg > &Outs, const SmallVectorImpl< SDValue > &OutVals, const SDLoc &DL, SelectionDAG &DAG) const override
This hook must be implemented to lower outgoing return values, described by the Outs array,...
const TargetRegisterClass * getRegClassFor(MVT VT, bool isDivergent) const override
Return the register class that should be used for the specified value type.
void AddMemOpInit(MachineInstr &MI) const
MachineMemOperand::Flags getTargetMMOFlags(const Instruction &I) const override
This callback is used to inspect load/store instructions and add target-specific MachineMemOperand fl...
bool isLegalGlobalAddressingMode(const AddrMode &AM) const
void computeKnownBitsForFrameIndex(int FrameIdx, KnownBits &Known, const MachineFunction &MF) const override
Determine which of the bits of FrameIndex FIOp are known to be 0.
bool shouldConvertConstantLoadToIntImm(const APInt &Imm, Type *Ty) const override
Return true if it is beneficial to convert a load of a constant to just the constant itself.
Align getPrefLoopAlignment(MachineLoop *ML) const override
Return the preferred loop alignment.
std::pair< unsigned, const TargetRegisterClass * > getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const override
Given a physical register constraint (e.g.
void emitExpandAtomicStore(StoreInst *SI) const override
Perform a atomic store using a target-specific way.
AtomicExpansionKind shouldExpandAtomicLoadInIR(LoadInst *LI) const override
Returns how the given (atomic) load should be expanded by the IR-level AtomicExpand pass.
Align computeKnownAlignForTargetInstr(GISelValueTracking &Analysis, Register R, const MachineRegisterInfo &MRI, unsigned Depth=0) const override
Determine the known alignment for the pointer value R.
bool getAsmOperandConstVal(SDValue Op, uint64_t &Val) const
bool isShuffleMaskLegal(ArrayRef< int >, EVT) const override
Targets can use this to indicate that they only support some VECTOR_SHUFFLE operations,...
void emitExpandAtomicLoad(LoadInst *LI) const override
Perform a atomic load using a target-specific way.
EVT getOptimalMemOpType(LLVMContext &Context, const MemOp &Op, const AttributeList &FuncAttributes) const override
Returns the target specific optimal type for load and store operations as a result of memset,...
void ReplaceNodeResults(SDNode *N, SmallVectorImpl< SDValue > &Results, SelectionDAG &DAG) const override
This callback is invoked when a node result type is illegal for the target, and the operation was reg...
Register getRegisterByName(const char *RegName, LLT VT, const MachineFunction &MF) const override
Return the register ID of the name passed in.
void LowerAsmOperandForConstraint(SDValue Op, StringRef Constraint, std::vector< SDValue > &Ops, SelectionDAG &DAG) const override
Lower the specified operand into the Ops vector.
LLT getPreferredShiftAmountTy(LLT Ty) const override
Return the preferred type to use for a shift opcode, given the shifted amount type is ShiftValueTy.
bool isLegalAddressingMode(const DataLayout &DL, const AddrMode &AM, Type *Ty, unsigned AS, Instruction *I=nullptr) const override
Return true if the addressing mode represented by AM is legal for this target, for a load/store of th...
SDValue lowerSET_FPENV(SDValue Op, SelectionDAG &DAG) const
bool shouldPreservePtrArith(const Function &F, EVT PtrVT) const override
True if target has some particular form of dealing with pointer arithmetic semantics for pointers wit...
void computeKnownBitsForTargetNode(const SDValue Op, KnownBits &Known, const APInt &DemandedElts, const SelectionDAG &DAG, unsigned Depth=0) const override
Determine which of the bits specified in Mask are known to be either zero or one and return them in t...
SDValue lowerSET_ROUNDING(SDValue Op, SelectionDAG &DAG) const
void allocateSpecialInputVGPRsFixed(CCState &CCInfo, MachineFunction &MF, const SIRegisterInfo &TRI, SIMachineFunctionInfo &Info) const
Allocate implicit function VGPR arguments in fixed registers.
LoadInst * lowerIdempotentRMWIntoFencedLoad(AtomicRMWInst *AI) const override
On some platforms, an AtomicRMW that never actually modifies the value (such as fetch_add of 0) can b...
MachineBasicBlock * emitGWSMemViolTestLoop(MachineInstr &MI, MachineBasicBlock *BB) const
bool getAddrModeArguments(const IntrinsicInst *I, SmallVectorImpl< Value * > &Ops, Type *&AccessTy) const override
CodeGenPrepare sinks address calculations into the same BB as Load/Store instructions reading the add...
bool checkAsmConstraintValA(SDValue Op, uint64_t Val, unsigned MaxSize=64) const
AtomicExpansionKind shouldExpandAtomicRMWInIR(AtomicRMWInst *) const override
Returns how the IR-level AtomicExpand pass should expand the given AtomicRMW, if at all.
bool shouldEmitFixup(const GlobalValue *GV) const
MachineBasicBlock * splitKillBlock(MachineInstr &MI, MachineBasicBlock *BB) const
void emitExpandAtomicCmpXchg(AtomicCmpXchgInst *CI) const override
Perform a cmpxchg expansion using a target-specific method.
bool canTransformPtrArithOutOfBounds(const Function &F, EVT PtrVT) const override
True if the target allows transformations of in-bounds pointer arithmetic that cause out-of-bounds in...
bool hasMemSDNodeUser(SDNode *N) const
bool isSDNodeSourceOfDivergence(const SDNode *N, FunctionLoweringInfo *FLI, UniformityInfo *UA) const override
MachineBasicBlock * EmitInstrWithCustomInserter(MachineInstr &MI, MachineBasicBlock *BB) const override
This method should be implemented by targets that mark instructions with the 'usesCustomInserter' fla...
bool isEligibleForTailCallOptimization(SDValue Callee, CallingConv::ID CalleeCC, bool isVarArg, const SmallVectorImpl< ISD::OutputArg > &Outs, const SmallVectorImpl< SDValue > &OutVals, const SmallVectorImpl< ISD::InputArg > &Ins, SelectionDAG &DAG) const
bool isMemOpHasNoClobberedMemOperand(const SDNode *N) const
bool isLegalFlatAddressingMode(const AddrMode &AM, unsigned AddrSpace) const
SDValue LowerCallResult(SDValue Chain, SDValue InGlue, CallingConv::ID CallConv, bool isVarArg, const SmallVectorImpl< ISD::InputArg > &Ins, const SDLoc &DL, SelectionDAG &DAG, SmallVectorImpl< SDValue > &InVals, bool isThisReturn, SDValue ThisVal) const
SDValue LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool isVarArg, const SmallVectorImpl< ISD::InputArg > &Ins, const SDLoc &DL, SelectionDAG &DAG, SmallVectorImpl< SDValue > &InVals) const override
This hook must be implemented to lower the incoming (formal) arguments, described by the Ins array,...
SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const override
This method will be invoked for all target nodes and for any target-independent nodes that the target...
AtomicExpansionKind shouldExpandAtomicCmpXchgInIR(AtomicCmpXchgInst *AI) const override
Returns how the given atomic cmpxchg should be expanded by the IR-level AtomicExpand pass.
bool isFPExtFoldable(const SelectionDAG &DAG, unsigned Opcode, EVT DestVT, EVT SrcVT) const override
Return true if an fpext operation input to an Opcode operation is free (for instance,...
void AdjustInstrPostInstrSelection(MachineInstr &MI, SDNode *Node) const override
Assign the register class depending on the number of bits set in the writemask.
MVT getRegisterTypeForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT) const override
Certain combinations of ABIs, Targets and features require that types are legal for some operations a...
void allocateSpecialInputVGPRs(CCState &CCInfo, MachineFunction &MF, const SIRegisterInfo &TRI, SIMachineFunctionInfo &Info) const
Allocate implicit function VGPR arguments at the end of allocated user arguments.
void finalizeLowering(MachineFunction &MF) const override
Execute target specific actions to finalize target lowering.
static bool isNonGlobalAddrSpace(unsigned AS)
void emitExpandAtomicAddrSpacePredicate(Instruction *AI) const
MachineSDNode * buildRSRC(SelectionDAG &DAG, const SDLoc &DL, SDValue Ptr, uint32_t RsrcDword1, uint64_t RsrcDword2And3) const
Return a resource descriptor with the 'Add TID' bit enabled The TID (Thread ID) is multiplied by the ...
unsigned getNumRegistersForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT) const override
Certain targets require unusual breakdowns of certain types.
bool mayBeEmittedAsTailCall(const CallInst *) const override
Return true if the target may be able emit the call instruction as a tail call.
void passSpecialInputs(CallLoweringInfo &CLI, CCState &CCInfo, const SIMachineFunctionInfo &Info, SmallVectorImpl< std::pair< unsigned, SDValue > > &RegsToPass, SmallVectorImpl< SDValue > &MemOpChains, SDValue Chain) const
SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override
This callback is invoked for operations that are unsupported by the target, which are registered to u...
bool checkAsmConstraintVal(SDValue Op, StringRef Constraint, uint64_t Val) const
bool isKnownNeverNaNForTargetNode(SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG, bool SNaN=false, unsigned Depth=0) const override
If SNaN is false,.
void emitExpandAtomicRMW(AtomicRMWInst *AI) const override
Perform a atomicrmw expansion using a target-specific way.
static bool shouldExpandVectorDynExt(unsigned EltSize, unsigned NumElem, bool IsDivergentIdx, const GCNSubtarget *Subtarget)
Check if EXTRACT_VECTOR_ELT/INSERT_VECTOR_ELT (<n x e>, var-idx) should be expanded into a set of cmp...
bool shouldUseLDSConstAddress(const GlobalValue *GV) const
bool supportSplitCSR(MachineFunction *MF) const override
Return true if the target supports that a subset of CSRs for the given machine function is handled ex...
bool isExtractVecEltCheap(EVT VT, unsigned Index) const override
Return true if extraction of a scalar element from the given vector type at the given index is cheap.
SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
bool allowsMisalignedMemoryAccesses(LLT Ty, unsigned AddrSpace, Align Alignment, MachineMemOperand::Flags Flags=MachineMemOperand::MONone, unsigned *IsFast=nullptr) const override
LLT handling variant.
bool isExtractSubvectorCheap(EVT ResVT, EVT SrcVT, unsigned Index) const override
Return true if EXTRACT_SUBVECTOR is cheap for extracting this result type from this source type with ...
bool canMergeStoresTo(unsigned AS, EVT MemVT, const MachineFunction &MF) const override
Returns if it's reasonable to merge stores to MemVT size.
SDValue lowerPREFETCH(SDValue Op, SelectionDAG &DAG) const
SITargetLowering(const TargetMachine &tm, const GCNSubtarget &STI)
void computeKnownBitsForTargetInstr(GISelValueTracking &Analysis, Register R, KnownBits &Known, const APInt &DemandedElts, const MachineRegisterInfo &MRI, unsigned Depth=0) const override
Determine which of the bits specified in Mask are known to be either zero or one and return them in t...
bool isFreeAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const override
Returns true if a cast from SrcAS to DestAS is "cheap", such that e.g.
bool shouldEmitPCReloc(const GlobalValue *GV) const
void initializeSplitCSR(MachineBasicBlock *Entry) const override
Perform necessary initialization to handle a subset of CSRs explicitly via copies.
void allocateSpecialEntryInputVGPRs(CCState &CCInfo, MachineFunction &MF, const SIRegisterInfo &TRI, SIMachineFunctionInfo &Info) const
void allocatePreloadKernArgSGPRs(CCState &CCInfo, SmallVectorImpl< CCValAssign > &ArgLocs, const SmallVectorImpl< ISD::InputArg > &Ins, MachineFunction &MF, const SIRegisterInfo &TRI, SIMachineFunctionInfo &Info) const
SDValue copyToM0(SelectionDAG &DAG, SDValue Chain, const SDLoc &DL, SDValue V) const
bool getTgtMemIntrinsic(IntrinsicInfo &, const CallInst &, MachineFunction &MF, unsigned IntrinsicID) const override
Given an intrinsic, checks if on the target the intrinsic will need to map to a MemIntrinsicNode (tou...
SDValue splitBinaryVectorOp(SDValue Op, SelectionDAG &DAG) const
unsigned getVectorTypeBreakdownForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT, EVT &IntermediateVT, unsigned &NumIntermediates, MVT &RegisterVT) const override
Certain targets such as MIPS require that some types such as vectors are always broken down into scal...
MVT getPointerMemTy(const DataLayout &DL, unsigned AS) const override
Similarly, the in-memory representation of a p7 is {p8, i32}, aka v8i32 when padding is added.
void allocateSystemSGPRs(CCState &CCInfo, MachineFunction &MF, SIMachineFunctionInfo &Info, CallingConv::ID CallConv, bool IsShader) const
bool CanLowerReturn(CallingConv::ID CallConv, MachineFunction &MF, bool isVarArg, const SmallVectorImpl< ISD::OutputArg > &Outs, LLVMContext &Context, const Type *RetTy) const override
This hook should be implemented to check whether the return values described by the Outs array can fi...
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
LLVM_ABI SDValue getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, EVT MemVT, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
SDValue getTargetGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT, int64_t offset=0, unsigned TargetFlags=0)
SDValue getExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT, unsigned Opcode)
Convert Op, which must be of integer type, to the integer type VT, by either any/sign/zero-extending ...
SDValue getExtractVectorElt(const SDLoc &DL, EVT VT, SDValue Vec, unsigned Idx)
Extract element at Idx from Vec.
const SDValue & getRoot() const
Return the root tag of the SelectionDAG.
LLVM_ABI SDValue getAddrSpaceCast(const SDLoc &dl, EVT VT, SDValue Ptr, unsigned SrcAS, unsigned DestAS)
Return an AddrSpaceCastSDNode.
bool isKnownNeverSNaN(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
const TargetSubtargetInfo & getSubtarget() const
SDValue getCopyToReg(SDValue Chain, const SDLoc &dl, Register Reg, SDValue N)
const Pass * getPass() const
LLVM_ABI SDValue getMergeValues(ArrayRef< SDValue > Ops, const SDLoc &dl)
Create a MERGE_VALUES node from the given operands.
LLVM_ABI SDVTList getVTList(EVT VT)
Return an SDVTList that represents the list of values specified.
LLVM_ABI SDValue getShiftAmountConstant(uint64_t Val, EVT VT, const SDLoc &DL)
LLVM_ABI SDValue getAllOnesConstant(const SDLoc &DL, EVT VT, bool IsTarget=false, bool IsOpaque=false)
LLVM_ABI MachineSDNode * getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT)
These are used for target selectors to create a new node with specified return type(s),...
LLVM_ABI void ExtractVectorElements(SDValue Op, SmallVectorImpl< SDValue > &Args, unsigned Start=0, unsigned Count=0, EVT EltVT=EVT())
Append the extracted elements from Start to Count out of the vector Op in Args.
LLVM_ABI SDValue getAtomicLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT MemVT, EVT VT, SDValue Chain, SDValue Ptr, MachineMemOperand *MMO)
LLVM_ABI SDValue getFreeze(SDValue V)
Return a freeze using the SDLoc of the value operand.
LLVM_ABI bool isConstantIntBuildVectorOrConstantInt(SDValue N, bool AllowOpaques=true) const
Test whether the given value is a constant int or similar node.
SDValue getSetCC(const SDLoc &DL, EVT VT, SDValue LHS, SDValue RHS, ISD::CondCode Cond, SDValue Chain=SDValue(), bool IsSignaling=false)
Helper function to make it easier to build SetCC's if you just have an ISD::CondCode instead of an SD...
LLVM_ABI SDValue UnrollVectorOp(SDNode *N, unsigned ResNE=0)
Utility function used by legalize and lowering to "unroll" a vector operation by splitting out the sc...
LLVM_ABI SDValue getConstantFP(double Val, const SDLoc &DL, EVT VT, bool isTarget=false)
Create a ConstantFPSDNode wrapping a constant value.
LLVM_ABI bool haveNoCommonBitsSet(SDValue A, SDValue B) const
Return true if A and B have no common bits set.
LLVM_ABI SDValue getRegister(Register Reg, EVT VT)
LLVM_ABI SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr)
Loads are not normal binary operators: their result type is not determined by their operands,...
LLVM_ABI SDValue getMemIntrinsicNode(unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef< SDValue > Ops, EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags Flags=MachineMemOperand::MOLoad|MachineMemOperand::MOStore, LocationSize Size=LocationSize::precise(0), const AAMDNodes &AAInfo=AAMDNodes())
Creates a MemIntrinsicNode that may produce a result and takes a list of operands.
LLVM_ABI SDValue getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDValue Chain, SDValue Ptr, SDValue Val, MachineMemOperand *MMO)
Gets a node for an atomic op, produces result (if relevant) and chain and takes 2 operands.
LLVM_ABI SDValue getMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI, std::optional< bool > OverrideTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo=AAMDNodes(), BatchAAResults *BatchAA=nullptr)
std::pair< SDValue, SDValue > SplitVectorOperand(const SDNode *N, unsigned OpNo)
Split the node's operand with EXTRACT_SUBVECTOR and return the low/high part.
LLVM_ABI SDValue getNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a bitwise NOT operation as (XOR Val, -1).
const TargetLowering & getTargetLoweringInfo() const
LLVM_ABI std::pair< EVT, EVT > GetSplitDestVTs(const EVT &VT) const
Compute the VTs needed for the low/hi parts of a type which is split (or expanded) into two not neces...
SDValue getUNDEF(EVT VT)
Return an UNDEF node. UNDEF does not have a useful SDLoc.
SDValue getCALLSEQ_END(SDValue Chain, SDValue Op1, SDValue Op2, SDValue InGlue, const SDLoc &DL)
Return a new CALLSEQ_END node, which always must have a glue result (to ensure it's not CSE'd).
SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef< SDValue > Ops)
Return an ISD::BUILD_VECTOR node.
LLVM_ABI SDValue getBitcastedAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
LLVM_ABI SDValue getBitcast(EVT VT, SDValue V)
Return a bitcast using the SDLoc of the value operand, and casting to the provided type.
SDValue getCopyFromReg(SDValue Chain, const SDLoc &dl, Register Reg, EVT VT)
SDValue getSelect(const SDLoc &DL, EVT VT, SDValue Cond, SDValue LHS, SDValue RHS, SDNodeFlags Flags=SDNodeFlags())
Helper function to make it easier to build Select's if you just have operands and don't want to check...
LLVM_ABI void setNodeMemRefs(MachineSDNode *N, ArrayRef< MachineMemOperand * > NewMemRefs)
Mutate the specified machine node's memory references to the provided list.
LLVM_ABI SDValue getZeroExtendInReg(SDValue Op, const SDLoc &DL, EVT VT)
Return the expression required to zero extend the Op value assuming it was the smaller SrcTy value.
const DataLayout & getDataLayout() const
LLVM_ABI SDValue getTokenFactor(const SDLoc &DL, SmallVectorImpl< SDValue > &Vals)
Creates a new TokenFactor containing Vals.
LLVM_ABI SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
LLVM_ABI SDValue getMemBasePlusOffset(SDValue Base, TypeSize Offset, const SDLoc &DL, const SDNodeFlags Flags=SDNodeFlags())
Returns sum of the base pointer and offset.
SDValue getSignedTargetConstant(int64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
LLVM_ABI SDValue getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, EVT SVT, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
LLVM_ABI void ReplaceAllUsesWith(SDValue From, SDValue To)
Modify anything using 'From' to use 'To' instead.
LLVM_ABI SDValue getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
Helper function to build ISD::STORE nodes.
LLVM_ABI SDValue getSignedConstant(int64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
SDValue getCALLSEQ_START(SDValue Chain, uint64_t InSize, uint64_t OutSize, const SDLoc &DL)
Return a new CALLSEQ_START node, that starts new call frame, in which InSize bytes are set up inside ...
LLVM_ABI void RemoveDeadNode(SDNode *N)
Remove the specified node from the system.
LLVM_ABI SDValue getTargetExtractSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand)
A convenience function for creating TargetInstrInfo::EXTRACT_SUBREG nodes.
SDValue getSelectCC(const SDLoc &DL, SDValue LHS, SDValue RHS, SDValue True, SDValue False, ISD::CondCode Cond, SDNodeFlags Flags=SDNodeFlags())
Helper function to make it easier to build SelectCC's if you just have an ISD::CondCode instead of an...
LLVM_ABI SDValue getSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either sign-extending or trunca...
const TargetMachine & getTarget() const
LLVM_ABI SDValue getAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either any-extending or truncat...
LLVM_ABI SDValue getValueType(EVT)
LLVM_ABI SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
LLVM_ABI bool isKnownNeverNaN(SDValue Op, const APInt &DemandedElts, bool SNaN=false, unsigned Depth=0) const
Test whether the given SDValue (or all elements of it, if it is a vector) is known to never be NaN in...
SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
LLVM_ABI unsigned ComputeNumSignBits(SDValue Op, unsigned Depth=0) const
Return the number of times the sign bit of the register is replicated into the other bits.
LLVM_ABI bool isBaseWithConstantOffset(SDValue Op) const
Return true if the specified operand is an ISD::ADD with a ConstantSDNode on the right-hand side,...
LLVM_ABI SDValue getVectorIdxConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
LLVM_ABI void ReplaceAllUsesOfValueWith(SDValue From, SDValue To)
Replace any uses of From with To, leaving uses of other values produced by From.getNode() alone.
MachineFunction & getMachineFunction() const
SDValue getPOISON(EVT VT)
Return a POISON node. POISON does not have a useful SDLoc.
SDValue getSplatBuildVector(EVT VT, const SDLoc &DL, SDValue Op)
Return a splat ISD::BUILD_VECTOR node, consisting of Op splatted to all elements.
LLVM_ABI SDValue getFrameIndex(int FI, EVT VT, bool isTarget=false)
LLVM_ABI KnownBits computeKnownBits(SDValue Op, unsigned Depth=0) const
Determine which bits of Op are known to be either zero or one and return them in Known.
LLVM_ABI SDValue getRegisterMask(const uint32_t *RegMask)
LLVM_ABI SDValue getZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either zero-extending or trunca...
LLVM_ABI SDValue getCondCode(ISD::CondCode Cond)
LLVM_ABI bool MaskedValueIsZero(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if 'Op & Mask' is known to be zero.
SDValue getObjectPtrOffset(const SDLoc &SL, SDValue Ptr, TypeSize Offset)
Create an add instruction with appropriate flags when used for addressing some offset of an object.
LLVMContext * getContext() const
const SDValue & setRoot(SDValue N)
Set the current root tag of the SelectionDAG.
LLVM_ABI SDNode * UpdateNodeOperands(SDNode *N, SDValue Op)
Mutate the specified node in-place to have the specified operands.
SDValue getEntryNode() const
Return the token chain corresponding to the entry of the function.
LLVM_ABI std::pair< SDValue, SDValue > SplitScalar(const SDValue &N, const SDLoc &DL, const EVT &LoVT, const EVT &HiVT)
Split the scalar node with EXTRACT_ELEMENT using the provided VTs and return the low/high part.
LLVM_ABI SDValue getVectorShuffle(EVT VT, const SDLoc &dl, SDValue N1, SDValue N2, ArrayRef< int > Mask)
Return an ISD::VECTOR_SHUFFLE node.
int getMaskElt(unsigned Idx) const
ArrayRef< int > getMask() const
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void resize(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:143
constexpr size_t size() const
size - Get the string size.
Definition StringRef.h:146
A switch()-like statement whose cases are string literals.
StringSwitch & Case(StringLiteral S, T Value)
Information about stack frame layout on the target.
Align getStackAlign() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
StackDirection getStackGrowthDirection() const
getStackGrowthDirection - Return the direction the stack grows
TargetInstrInfo - Interface to description of machine instruction set.
Type * Ty
Same as OrigTy, or partially legalized for soft float libcalls.
void setBooleanVectorContents(BooleanContent Ty)
Specify how the target extends the result of a vector boolean value from a vector of i1 to a wider ty...
void setOperationAction(unsigned Op, MVT VT, LegalizeAction Action)
Indicate that the specified operation does not work with the specified type and indicate what to do a...
virtual void finalizeLowering(MachineFunction &MF) const
Execute target specific actions to finalize target lowering.
EVT getValueType(const DataLayout &DL, Type *Ty, bool AllowUnknown=false) const
Return the EVT corresponding to this LLVM type.
virtual const TargetRegisterClass * getRegClassFor(MVT VT, bool isDivergent=false) const
Return the register class that should be used for the specified value type.
const TargetMachine & getTargetMachine() const
virtual unsigned getNumRegistersForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT) const
Certain targets require unusual breakdowns of certain types.
virtual MVT getRegisterTypeForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT) const
Certain combinations of ABIs, Targets and features require that types are legal for some operations a...
LegalizeTypeAction
This enum indicates whether a types are legal for a target, and if not, what action should be used to...
void setHasExtractBitsInsn(bool hasExtractInsn=true)
Tells the code generator that the target has BitExtract instructions.
virtual TargetLoweringBase::LegalizeTypeAction getPreferredVectorAction(MVT VT) const
Return the preferred vector type legalization action.
virtual unsigned getVectorTypeBreakdownForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT, EVT &IntermediateVT, unsigned &NumIntermediates, MVT &RegisterVT) const
Certain targets such as MIPS require that some types such as vectors are always broken down into scal...
Register getStackPointerRegisterToSaveRestore() const
If a physical register, this specifies the register that llvm.savestack/llvm.restorestack should save...
void setBooleanContents(BooleanContent Ty)
Specify how the target extends the result of integer and floating point boolean values from i1 to a w...
virtual Align getPrefLoopAlignment(MachineLoop *ML=nullptr) const
Return the preferred loop alignment.
void computeRegisterProperties(const TargetRegisterInfo *TRI)
Once all of the register classes are added, this allows us to compute derived properties we expose.
void addRegisterClass(MVT VT, const TargetRegisterClass *RC)
Add the specified register class as an available regclass for the specified value type.
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
virtual MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
bool isOperationLegal(unsigned Op, EVT VT) const
Return true if the specified operation is legal on this target.
void setTruncStoreAction(MVT ValVT, MVT MemVT, LegalizeAction Action)
Indicate that the specified truncating store does not work with the specified type and indicate what ...
bool isOperationLegalOrCustom(unsigned Op, EVT VT, bool LegalOnly=false) const
Return true if the specified operation is legal on this target or can be made legal with custom lower...
virtual bool isNarrowingProfitable(SDNode *N, EVT SrcVT, EVT DestVT) const
Return true if it's profitable to narrow operations of type SrcVT to DestVT.
void setStackPointerRegisterToSaveRestore(Register R)
If set to a physical register, this specifies the register that llvm.savestack/llvm....
void AddPromotedToType(unsigned Opc, MVT OrigVT, MVT DestVT)
If Opc/OrigVT is specified as being promoted, the promotion code defaults to trying a larger integer/...
AtomicExpansionKind
Enum that specifies what an atomic load/AtomicRMWInst is expanded to, if at all.
void setTargetDAGCombine(ArrayRef< ISD::NodeType > NTs)
Targets should invoke this method for each target independent node that they want to provide a custom...
bool allowsMemoryAccessForAlignment(LLVMContext &Context, const DataLayout &DL, EVT VT, unsigned AddrSpace=0, Align Alignment=Align(1), MachineMemOperand::Flags Flags=MachineMemOperand::MONone, unsigned *Fast=nullptr) const
This function returns true if the memory access is aligned or if the target allows this specific unal...
virtual MVT getPointerMemTy(const DataLayout &DL, uint32_t AS=0) const
Return the in-memory pointer type for the given address space, defaults to the pointer type from the ...
void setSchedulingPreference(Sched::Preference Pref)
Specify the target scheduling preference.
virtual void computeKnownBitsForFrameIndex(int FIOp, KnownBits &Known, const MachineFunction &MF) const
Determine which of the bits of FrameIndex FIOp are known to be 0.
SDValue scalarizeVectorStore(StoreSDNode *ST, SelectionDAG &DAG) const
std::vector< AsmOperandInfo > AsmOperandInfoVector
SDValue SimplifyMultipleUseDemandedBits(SDValue Op, const APInt &DemandedBits, const APInt &DemandedElts, SelectionDAG &DAG, unsigned Depth=0) const
More limited version of SimplifyDemandedBits that can be used to "lookthrough" ops that don't contrib...
SDValue expandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG) const
Expands an unaligned store to 2 half-size stores for integer values, and possibly more for vectors.
virtual ConstraintType getConstraintType(StringRef Constraint) const
Given a constraint, return the type of constraint it is for this target.
bool parametersInCSRMatch(const MachineRegisterInfo &MRI, const uint32_t *CallerPreservedMask, const SmallVectorImpl< CCValAssign > &ArgLocs, const SmallVectorImpl< SDValue > &OutVals) const
Check whether parameters to a call that are passed in callee saved registers are the same as from the...
std::pair< SDValue, SDValue > expandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG) const
Expands an unaligned load to 2 half-size loads for an integer, and possibly more for vectors.
SDValue expandFMINIMUMNUM_FMAXIMUMNUM(SDNode *N, SelectionDAG &DAG) const
Expand fminimumnum/fmaximumnum into multiple comparison with selects.
virtual bool isTypeDesirableForOp(unsigned, EVT VT) const
Return true if the target has native support for the specified value type and it is 'desirable' to us...
std::pair< SDValue, SDValue > scalarizeVectorLoad(LoadSDNode *LD, SelectionDAG &DAG) const
Turn load of vector type into a load of the individual elements.
virtual std::pair< unsigned, const TargetRegisterClass * > getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const
Given a physical register constraint (e.g.
bool SimplifyDemandedBits(SDValue Op, const APInt &DemandedBits, const APInt &DemandedElts, KnownBits &Known, TargetLoweringOpt &TLO, unsigned Depth=0, bool AssumeSingleUse=false) const
Look at Op.
TargetLowering(const TargetLowering &)=delete
virtual MachineBasicBlock * EmitInstrWithCustomInserter(MachineInstr &MI, MachineBasicBlock *MBB) const
This method should be implemented by targets that mark instructions with the 'usesCustomInserter' fla...
virtual AsmOperandInfoVector ParseConstraints(const DataLayout &DL, const TargetRegisterInfo *TRI, const CallBase &Call) const
Split up the constraint string from the inline assembly value into the specific constraints and their...
SDValue expandRoundInexactToOdd(EVT ResultVT, SDValue Op, const SDLoc &DL, SelectionDAG &DAG) const
Truncate Op to ResultVT.
virtual void ComputeConstraintToUse(AsmOperandInfo &OpInfo, SDValue Op, SelectionDAG *DAG=nullptr) const
Determines the constraint code and constraint type to use for the specific AsmOperandInfo,...
virtual void LowerAsmOperandForConstraint(SDValue Op, StringRef Constraint, std::vector< SDValue > &Ops, SelectionDAG &DAG) const
Lower the specified operand into the Ops vector.
SDValue expandFMINNUM_FMAXNUM(SDNode *N, SelectionDAG &DAG) const
Expand fminnum/fmaxnum into fminnum_ieee/fmaxnum_ieee with quieted inputs.
Primary interface to the complete machine description for the target machine.
const Triple & getTargetTriple() const
bool shouldAssumeDSOLocal(const GlobalValue *GV) const
TargetOptions Options
unsigned GuaranteedTailCallOpt
GuaranteedTailCallOpt - This flag is enabled when -tailcallopt is specified on the commandline.
unsigned getNumRegs() const
Return the number of registers in this class.
unsigned getID() const
Return the register class ID number.
MCRegister getRegister(unsigned i) const
Return the specified register in the class.
iterator begin() const
begin/end - Return all of the registers in this class.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:47
OSType getOS() const
Get the parsed operating system type of this triple.
Definition Triple.h:421
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition TypeSize.h:344
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:297
bool isBFloatTy() const
Return true if this is 'bfloat', a 16-bit bfloat type.
Definition Type.h:145
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:352
bool isHalfTy() const
Return true if this is 'half', a 16-bit IEEE fp type.
Definition Type.h:142
bool isFunctionTy() const
True if this is an instance of FunctionType.
Definition Type.h:258
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:240
LLVM_ABI const fltSemantics & getFltSemantics() const
Definition Type.cpp:107
bool isVoidTy() const
Return true if this is 'void'.
Definition Type.h:139
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM_ABI void set(Value *Val)
Definition Value.h:905
User * getUser() const
Returns the User that contains this Use.
Definition Use.h:61
LLVM_ABI unsigned getOperandNo() const
Return the operand # of this use in its User.
Definition Use.cpp:35
const Use & getOperandUse(unsigned i) const
Definition User.h:245
Value * getOperand(unsigned i) const
Definition User.h:232
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition Value.h:439
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:546
iterator_range< user_iterator > users()
Definition Value.h:426
bool use_empty() const
Definition Value.h:346
LLVM_ABI LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.cpp:1099
iterator_range< use_iterator > uses()
Definition Value.h:380
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
Definition Value.cpp:396
Type * getElementType() const
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:201
constexpr bool isZero() const
Definition TypeSize.h:154
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:123
CallInst * Call
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ CONSTANT_ADDRESS_32BIT
Address space for 32-bit constant memory.
@ BUFFER_STRIDED_POINTER
Address space for 192-bit fat buffer pointers with an additional index.
@ REGION_ADDRESS
Address space for region memory. (GDS)
@ LOCAL_ADDRESS
Address space for local memory.
@ STREAMOUT_REGISTER
Internal address spaces. Can be freely renumbered.
@ CONSTANT_ADDRESS
Address space for constant memory (VTX2).
@ FLAT_ADDRESS
Address space for flat memory.
@ GLOBAL_ADDRESS
Address space for global memory (RAT0, VTX0).
@ BUFFER_FAT_POINTER
Address space for 160-bit buffer fat pointers.
@ PRIVATE_ADDRESS
Address space for private memory.
@ BUFFER_RESOURCE
Address space for 128-bit buffer resources.
@ CLAMP
CLAMP value between 0.0 and 1.0.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
constexpr char SymbolName[]
Key for Kernel::Metadata::mSymbolName.
bool isInlinableLiteralBF16(int16_t Literal, bool HasInv2Pi)
LLVM_READONLY const MIMGG16MappingInfo * getMIMGG16MappingInfo(unsigned G)
LLVM_READONLY int getGlobalSaddrOp(uint16_t Opcode)
bool isInlinableLiteralFP16(int16_t Literal, bool HasInv2Pi)
int getMIMGOpcode(unsigned BaseOpcode, unsigned MIMGEncoding, unsigned VDataDwords, unsigned VAddrDwords)
LLVM_READNONE constexpr bool isShader(CallingConv::ID CC)
bool shouldEmitConstantsToTextSection(const Triple &TT)
bool isFlatGlobalAddrSpace(unsigned AS)
const uint64_t FltRoundToHWConversionTable
bool isGFX12Plus(const MCSubtargetInfo &STI)
unsigned getNSAMaxSize(const MCSubtargetInfo &STI, bool HasSampler)
bool isGFX11(const MCSubtargetInfo &STI)
bool hasValueInRangeLikeMetadata(const MDNode &MD, int64_t Val)
Checks if Val is inside MD, a !range-like metadata.
LLVM_READNONE bool isLegalDPALU_DPPControl(const MCSubtargetInfo &ST, unsigned DC)
LLVM_READNONE constexpr bool mayTailCallThisCC(CallingConv::ID CC)
Return true if we might ever do TCO for calls with this calling convention.
unsigned getAMDHSACodeObjectVersion(const Module &M)
LLVM_READONLY bool hasNamedOperand(uint64_t Opcode, OpName NamedIdx)
LLVM_READNONE constexpr bool isKernel(CallingConv::ID CC)
LLVM_READNONE constexpr bool isEntryFunctionCC(CallingConv::ID CC)
bool isInlinableLiteral32(int32_t Literal, bool HasInv2Pi)
LLVM_READNONE constexpr bool isCompute(CallingConv::ID CC)
bool isIntrinsicSourceOfDivergence(unsigned IntrID)
LLVM_READNONE bool isInlinableIntLiteral(int64_t Literal)
Is this literal inlinable, and not one of the values intended for floating point values.
bool getMUBUFTfe(unsigned Opc)
bool isGFX11Plus(const MCSubtargetInfo &STI)
std::optional< unsigned > getInlineEncodingV2F16(uint32_t Literal)
std::tuple< char, unsigned, unsigned > parseAsmConstraintPhysReg(StringRef Constraint)
Returns a valid charcode or 0 in the first entry if this is a valid physical register constraint.
bool isGFX10Plus(const MCSubtargetInfo &STI)
LLVM_READONLY int getVOPe64(uint16_t Opcode)
bool isUniformMMO(const MachineMemOperand *MMO)
std::optional< unsigned > getInlineEncodingV2I16(uint32_t Literal)
uint32_t decodeFltRoundToHWConversionTable(uint32_t FltRounds)
Read the hardware rounding mode equivalent of a AMDGPUFltRounds value.
bool isGFX1250(const MCSubtargetInfo &STI)
bool isExtendedGlobalAddrSpace(unsigned AS)
LLVM_READONLY const MIMGDimInfo * getMIMGDimInfo(unsigned DimEnum)
std::optional< unsigned > getInlineEncodingV2BF16(uint32_t Literal)
LLVM_READONLY const MIMGBaseOpcodeInfo * getMIMGBaseOpcodeInfo(unsigned BaseOpcode)
LLVM_READNONE constexpr bool isChainCC(CallingConv::ID CC)
int getMaskedMIMGOp(unsigned Opc, unsigned NewChannels)
const ImageDimIntrinsicInfo * getImageDimIntrinsicInfo(unsigned Intr)
bool isInlinableLiteralI16(int32_t Literal, bool HasInv2Pi)
LLVM_READNONE constexpr bool canGuaranteeTCO(CallingConv::ID CC)
LLVM_READNONE constexpr bool isGraphics(CallingConv::ID CC)
bool isInlinableLiteral64(int64_t Literal, bool HasInv2Pi)
Is this literal inlinable.
const RsrcIntrinsic * lookupRsrcIntrinsic(unsigned Intr)
const uint64_t FltRoundConversionTable
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ AMDGPU_CS
Used for Mesa/AMDPAL compute shaders.
@ AMDGPU_KERNEL
Used for AMDGPU code object kernels.
@ MaxID
The highest possible ID. Must be some 2^k - 1.
@ AMDGPU_Gfx
Used for AMD graphics targets.
@ AMDGPU_CS_ChainPreserve
Used on AMDGPUs to give the middle-end more control over argument placement.
@ AMDGPU_CS_Chain
Used on AMDGPUs to give the middle-end more control over argument placement.
@ AMDGPU_PS
Used for Mesa/AMDPAL pixel shaders.
@ Fast
Attempts to make calls as fast as possible (e.g.
Definition CallingConv.h:41
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
@ SETCC
SetCC operator - This evaluates to a true value iff the condition is true.
Definition ISDOpcodes.h:807
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition ISDOpcodes.h:256
@ CTLZ_ZERO_UNDEF
Definition ISDOpcodes.h:780
@ DELETED_NODE
DELETED_NODE - This is an illegal value that is used to catch errors.
Definition ISDOpcodes.h:45
@ SMUL_LOHI
SMUL_LOHI/UMUL_LOHI - Multiply two integers of type iN, producing a signed/unsigned value of type i[2...
Definition ISDOpcodes.h:270
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition ISDOpcodes.h:593
@ BSWAP
Byte Swap and Counting operators.
Definition ISDOpcodes.h:771
@ FMAD
FMAD - Perform a * b + c, while getting the same result as the separately rounded operations.
Definition ISDOpcodes.h:515
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:259
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition ISDOpcodes.h:841
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition ISDOpcodes.h:511
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition ISDOpcodes.h:215
@ GlobalAddress
Definition ISDOpcodes.h:88
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition ISDOpcodes.h:868
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition ISDOpcodes.h:577
@ FADD
Simple binary floating point operators.
Definition ISDOpcodes.h:410
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition ISDOpcodes.h:744
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition ISDOpcodes.h:249
@ BUILTIN_OP_END
BUILTIN_OP_END - This must be the last enum value in this list.
@ SIGN_EXTEND
Conversion operators.
Definition ISDOpcodes.h:832
@ SCALAR_TO_VECTOR
SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a scalar value into element 0 of the...
Definition ISDOpcodes.h:662
@ CTTZ_ZERO_UNDEF
Bit counting operators with an undefined result for zero inputs.
Definition ISDOpcodes.h:779
@ SSUBO
Same for subtraction.
Definition ISDOpcodes.h:347
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition ISDOpcodes.h:534
@ IS_FPCLASS
Performs a check of floating point class property, defined by IEEE-754.
Definition ISDOpcodes.h:541
@ SSUBSAT
RESULT = [US]SUBSAT(LHS, RHS) - Perform saturation subtraction on 2 integers with the same bit width ...
Definition ISDOpcodes.h:369
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition ISDOpcodes.h:784
@ UNDEF
UNDEF - An undefined node.
Definition ISDOpcodes.h:228
@ EXTRACT_ELEMENT
EXTRACT_ELEMENT - This is used to get the lower or upper (determined by a Constant,...
Definition ISDOpcodes.h:242
@ CopyFromReg
CopyFromReg - This node indicates that the input value is a virtual or physical register that is defi...
Definition ISDOpcodes.h:225
@ SADDO
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition ISDOpcodes.h:343
@ GET_ROUNDING
Returns current rounding mode: -1 Undefined 0 Round to 0 1 Round to nearest, ties to even 2 Round to ...
Definition ISDOpcodes.h:958
@ MULHU
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition ISDOpcodes.h:701
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:762
@ VECTOR_SHUFFLE
VECTOR_SHUFFLE(VEC1, VEC2) - Returns a vector, of the same type as VEC1/VEC2.
Definition ISDOpcodes.h:642
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition ISDOpcodes.h:607
@ EXTRACT_VECTOR_ELT
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition ISDOpcodes.h:569
@ CopyToReg
CopyToReg - This node has three operands: a chain, a register number to set to this value,...
Definition ISDOpcodes.h:219
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition ISDOpcodes.h:838
@ SELECT_CC
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition ISDOpcodes.h:799
@ SMULO
Same for multiplication.
Definition ISDOpcodes.h:351
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition ISDOpcodes.h:876
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition ISDOpcodes.h:724
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition ISDOpcodes.h:793
@ UADDO_CARRY
Carry-using nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:323
@ STRICT_FP_ROUND
X = STRICT_FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision ...
Definition ISDOpcodes.h:493
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:914
@ STRICT_FP_EXTEND
X = STRICT_FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition ISDOpcodes.h:498
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:736
@ INTRINSIC_WO_CHAIN
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition ISDOpcodes.h:200
@ INSERT_VECTOR_ELT
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL.
Definition ISDOpcodes.h:558
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition ISDOpcodes.h:53
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition ISDOpcodes.h:947
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:844
@ SHL_PARTS
SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded integer shift operations.
Definition ISDOpcodes.h:821
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition ISDOpcodes.h:62
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition ISDOpcodes.h:527
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition ISDOpcodes.h:360
@ INTRINSIC_W_CHAIN
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition ISDOpcodes.h:208
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition ISDOpcodes.h:549
LLVM_ABI CondCode getSetCCSwappedOperands(CondCode Operation)
Return the operation corresponding to (Y op X) when given the operation for (X op Y).
bool isSignedIntSetCC(CondCode Code)
Return true if this is a setcc instruction that performs a signed comparison when used with integer o...
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out,...
LoadExtType
LoadExtType enum - This enum defines the three variants of LOADEXT (load with extension).
This namespace contains an enum with a value for every intrinsic/builtin function known by LLVM.
LLVM_ABI Function * getDeclarationIfExists(const Module *M, ID id)
Look up the Function declaration of the intrinsic id in the Module M and return it if it exists.
LLVM_ABI AttributeSet getFnAttributes(LLVMContext &C, ID id)
Return the function attributes for an intrinsic.
LLVM_ABI AttributeList getAttributes(LLVMContext &C, ID id, FunctionType *FT)
Return the attributes for an intrinsic.
LLVM_ABI FunctionType * getType(LLVMContext &Context, ID id, ArrayRef< Type * > Tys={})
Return the function type for an intrinsic.
BinaryOp_match< SpecificConstantMatch, SrcTy, TargetOpcode::G_SUB > m_Neg(const SrcTy &&Src)
Matches a register negated by a G_SUB.
bool mi_match(Reg R, const MachineRegisterInfo &MRI, Pattern &&P)
GFCstOrSplatGFCstMatch m_GFCstOrSplat(std::optional< FPValueAndVReg > &FPValReg)
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
@ Implicit
Not emitted register (e.g. carry, or temporary result).
@ Dead
Unused definition.
@ Define
Register definition.
@ Kill
The last use of a register.
@ Undef
Value of the register doesn't matter.
bool sd_match(SDNode *N, const SelectionDAG *DAG, Pattern &&P)
Offsets
Offsets in bytes from the start of the input buffer.
@ System
Synchronized with respect to all concurrently executing threads.
Definition LLVMContext.h:58
initializer< Ty > init(const Ty &Val)
constexpr double inv_pi
@ User
could "use" a pointer
NodeAddr< UseNode * > Use
Definition RDFGraph.h:385
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
GenericUniformityInfo< SSAContext > UniformityInfo
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:316
@ Offset
Definition DWP.cpp:477
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred)
getICmpCondCode - Return the ISD condition code corresponding to the given LLVM IR integer condition ...
Definition Analysis.cpp:241
LLVM_ABI void finalizeBundle(MachineBasicBlock &MBB, MachineBasicBlock::instr_iterator FirstMI, MachineBasicBlock::instr_iterator LastMI)
finalizeBundle - Finalize a machine instruction bundle which includes a sequence of instructions star...
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1725
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
detail::zippy< detail::zip_first, T, U, Args... > zip_equal(T &&t, U &&u, Args &&...args)
zip iterator that assumes that all iteratees have the same length.
Definition STLExtras.h:839
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:165
LLVM_ABI bool isNullConstant(SDValue V)
Returns true if V is a constant integer zero.
std::pair< Value *, Value * > buildCmpXchgValue(IRBuilderBase &Builder, Value *Ptr, Value *Cmp, Value *Val, Align Alignment)
Emit IR to implement the given cmpxchg operation on values in registers, returning the new value.
LLVM_ABI SDValue peekThroughBitcasts(SDValue V)
Return the non-bitcasted source operand of V if it exists.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
@ Done
Definition Threading.h:60
bool CCAssignFn(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, Type *OrigTy, CCState &State)
CCAssignFn - This function assigns a location for Val, updating State to reflect the change.
constexpr int64_t minIntN(int64_t N)
Gets the minimum value for a N-bit signed integer.
Definition MathExtras.h:223
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition bit.h:303
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2136
constexpr T alignDown(U Value, V Align, W Skew=0)
Returns the largest unsigned integer less than or equal to Value and is Skew mod Align.
Definition MathExtras.h:546
MemoryEffectsBase< IRMemLocation > MemoryEffects
Summary of how a function affects memory in the program.
Definition ModRef.h:296
constexpr int popcount(T Value) noexcept
Count the number of set bits in a value.
Definition bit.h:154
LLVM_ABI ConstantFPSDNode * isConstOrConstSplatFP(SDValue N, bool AllowUndefs=false)
Returns the SDNode if it is a constant splat BuildVector or constant float.
uint64_t PowerOf2Ceil(uint64_t A)
Returns the power of two which is greater than or equal to the given value.
Definition MathExtras.h:385
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:202
constexpr bool isShiftedMask_64(uint64_t Value)
Return true if the argument contains a non-empty sequence of ones with the remainder zero (64 bit ver...
Definition MathExtras.h:273
bool isReleaseOrStronger(AtomicOrdering AO)
static const MachineMemOperand::Flags MONoClobber
Mark the MMO of a uniform load if there are no potentially clobbering stores on any path from the sta...
Definition SIInstrInfo.h:44
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1732
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:331
AtomicOrderingCABI
Atomic ordering for C11 / C++11's memory models.
int countl_zero(T Val)
Count number of 0's from the most significant bit to the least stopping at the first 1.
Definition bit.h:236
bool isBoolSGPR(SDValue V)
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:279
constexpr uint32_t Hi_32(uint64_t Value)
Return the high 32 bits of a 64 bit value.
Definition MathExtras.h:150
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:189
ISD::CondCode getFCmpCondCode(FCmpInst::Predicate Pred)
getFCmpCondCode - Return the ISD condition code corresponding to the given LLVM IR floating-point con...
Definition Analysis.cpp:207
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
constexpr uint32_t Lo_32(uint64_t Value)
Return the low 32 bits of a 64 bit value.
Definition MathExtras.h:155
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
static const MachineMemOperand::Flags MOCooperative
Mark the MMO of cooperative load/store atomics.
Definition SIInstrInfo.h:52
Value * buildAtomicRMWValue(AtomicRMWInst::BinOp Op, IRBuilderBase &Builder, Value *Loaded, Value *Val)
Emit IR to implement the given atomicrmw operation on values in registers, returning the new value.
constexpr T divideCeil(U Numerator, V Denominator)
Returns the integer ceil(Numerator / Denominator).
Definition MathExtras.h:394
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:71
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
unsigned getUndefRegState(bool B)
@ AfterLegalizeDAG
Definition DAGCombine.h:19
@ AfterLegalizeVectorOps
Definition DAGCombine.h:18
@ Or
Bitwise or logical OR of integers.
@ Mul
Product of integers.
@ Add
Sum of integers.
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
FunctionAddr VTableAddr Next
Definition InstrProf.h:141
DWARFExpression::Operation Op
unsigned M0(unsigned Val)
Definition VE.h:376
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI ConstantSDNode * isConstOrConstSplat(SDValue N, bool AllowUndefs=false, bool AllowTruncation=false)
Returns the SDNode if it is a constant splat BuildVector or constant int.
constexpr int64_t maxIntN(int64_t N)
Gets the maximum value for a N-bit signed integer.
Definition MathExtras.h:232
constexpr unsigned BitWidth
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI std::optional< ValueAndVReg > getIConstantVRegValWithLookThrough(Register VReg, const MachineRegisterInfo &MRI, bool LookThroughInstrs=true)
If VReg is defined by a statically evaluable chain of instructions rooted on a G_CONSTANT returns its...
Definition Utils.cpp:433
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1758
static const MachineMemOperand::Flags MOLastUse
Mark the MMO of a load as the last use.
Definition SIInstrInfo.h:48
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1897
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition Alignment.h:201
constexpr T maskTrailingOnes(unsigned N)
Create a bitmask with the N right-most bits set to 1, and all other bits set to 0.
Definition MathExtras.h:77
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:869
#define N
int64_t DWordOffset
int64_t PermMask
std::tuple< const ArgDescriptor *, const TargetRegisterClass *, LLT > getPreloadedValue(PreloadedValue Value) const
static constexpr uint64_t encode(Fields... Values)
static std::tuple< typename Fields::ValueType... > decode(uint64_t Encoded)
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77
static ArgDescriptor createStack(unsigned Offset, unsigned Mask=~0u)
MCRegister getRegister() const
static ArgDescriptor createArg(const ArgDescriptor &Arg, unsigned Mask)
static ArgDescriptor createRegister(Register Reg, unsigned Mask=~0u)
Helper struct shared between Function Specialization and SCCP Solver.
Definition SCCPSolver.h:42
Represent subnormal handling kind for floating point instruction inputs and outputs.
@ Dynamic
Denormals have unknown treatment.
static constexpr DenormalMode getPreserveSign()
static constexpr DenormalMode getIEEE()
Extended Value Type.
Definition ValueTypes.h:35
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
Definition ValueTypes.h:395
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition ValueTypes.h:137
static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements, bool IsScalable=false)
Returns the EVT that represents a vector NumElements in length, where each element is of type VT.
Definition ValueTypes.h:74
EVT changeTypeToInteger() const
Return the type converted to an equivalently sized integer or vector with integer element type.
Definition ValueTypes.h:121
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition ValueTypes.h:300
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition ValueTypes.h:147
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:373
bool isByteSized() const
Return true if the bit size is a multiple of 8.
Definition ValueTypes.h:243
EVT changeElementType(EVT EltVT) const
Return a VT for a type whose attributes match ourselves with the exception of the element type that i...
Definition ValueTypes.h:113
uint64_t getScalarSizeInBits() const
Definition ValueTypes.h:385
bool isPow2VectorType() const
Returns true if the given vector is a power of 2.
Definition ValueTypes.h:470
TypeSize getStoreSizeInBits() const
Return the number of bits overwritten by a store of the specified value type.
Definition ValueTypes.h:412
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:316
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition ValueTypes.h:65
bool isVector() const
Return true if this is a vector value type.
Definition ValueTypes.h:168
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition ValueTypes.h:323
bool bitsEq(EVT VT) const
Return true if this has the same number of bits as VT.
Definition ValueTypes.h:256
LLVM_ABI Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition ValueTypes.h:328
bool isScalarInteger() const
Return true if this is an integer, but not a vector.
Definition ValueTypes.h:157
EVT changeVectorElementType(EVT EltVT) const
Return a VT for a vector type whose attributes match ourselves with the exception of the element type...
Definition ValueTypes.h:102
LLVM_ABI const fltSemantics & getFltSemantics() const
Returns an APFloat semantics tag appropriate for the value type.
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition ValueTypes.h:336
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition ValueTypes.h:152
unsigned getPointerAddrSpace() const
unsigned getByValSize() const
InputArg - This struct carries flags and type information about a single incoming (formal) argument o...
MVT VT
Legalized type of this argument part.
unsigned getOrigArgIndex() const
OutputArg - This struct carries flags and a value for a single outgoing (actual) argument or outgoing...
bool isUnknown() const
Returns true if we don't know any bits.
Definition KnownBits.h:66
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition KnownBits.h:172
void resetAll()
Resets the known state of all bits.
Definition KnownBits.h:74
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition KnownBits.h:225
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition KnownBits.h:180
static KnownBits add(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from addition of LHS and RHS.
Definition KnownBits.h:347
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition KnownBits.h:248
This class contains a discriminated union of information about pointers in memory operands,...
static LLVM_ABI MachinePointerInfo getStack(MachineFunction &MF, int64_t Offset, uint8_t ID=0)
Stack pointer relative access.
int64_t Offset
Offset - This is an offset from the base Value*.
PointerUnion< const Value *, const PseudoSourceValue * > V
This is the IR pointer value for the access, or it is null if unknown.
static LLVM_ABI MachinePointerInfo getGOT(MachineFunction &MF)
Return a MachinePointerInfo record that refers to a GOT entry.
static LLVM_ABI MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition Alignment.h:106
These are IR-level optimization flags that may be propagated to SDNodes.
bool hasNoUnsignedWrap() const
bool hasAllowContract() const
bool hasNoSignedWrap() const
This represents a list of ValueType's that has been intern'd by a SelectionDAG.
unsigned int NumVTs
This represents an addressing mode of: BaseGV + BaseOffs + BaseReg + Scale*ScaleReg + ScalableOffset*...
This structure contains all information that is necessary for lowering calls.
SmallVector< ISD::InputArg, 32 > Ins
SmallVector< ISD::OutputArg, 32 > Outs