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"
19#include "AMDGPUTargetMachine.h"
20#include "GCNSubtarget.h"
23#include "SIRegisterInfo.h"
24#include "llvm/ADT/APInt.h"
26#include "llvm/ADT/Statistic.h"
42#include "llvm/IR/IRBuilder.h"
44#include "llvm/IR/IntrinsicsAMDGPU.h"
45#include "llvm/IR/IntrinsicsR600.h"
46#include "llvm/IR/MDBuilder.h"
49#include "llvm/Support/ModRef.h"
51#include <optional>
52
53using namespace llvm;
54using namespace llvm::SDPatternMatch;
55
56#define DEBUG_TYPE "si-lower"
57
58STATISTIC(NumTailCalls, "Number of tail calls");
59
60static cl::opt<bool>
61 DisableLoopAlignment("amdgpu-disable-loop-alignment",
62 cl::desc("Do not align and prefetch loops"),
63 cl::init(false));
64
66 "amdgpu-use-divergent-register-indexing", cl::Hidden,
67 cl::desc("Use indirect register addressing for divergent indexes"),
68 cl::init(false));
69
72 return Info->getMode().FP32Denormals == DenormalMode::getPreserveSign();
73}
74
77 return Info->getMode().FP64FP16Denormals == DenormalMode::getPreserveSign();
78}
79
80static unsigned findFirstFreeSGPR(CCState &CCInfo) {
81 unsigned NumSGPRs = AMDGPU::SGPR_32RegClass.getNumRegs();
82 for (unsigned Reg = 0; Reg < NumSGPRs; ++Reg) {
83 if (!CCInfo.isAllocated(AMDGPU::SGPR0 + Reg)) {
84 return AMDGPU::SGPR0 + Reg;
85 }
86 }
87 llvm_unreachable("Cannot allocate sgpr");
88}
89
91 const GCNSubtarget &STI)
92 : AMDGPUTargetLowering(TM, STI, STI), Subtarget(&STI) {
93 addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass);
94 addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass);
95
96 addRegisterClass(MVT::i32, &AMDGPU::SReg_32RegClass);
97
98 const SIRegisterInfo *TRI = STI.getRegisterInfo();
99 const TargetRegisterClass *V32RegClass =
100 TRI->getDefaultVectorSuperClassForBitWidth(32);
101 addRegisterClass(MVT::f32, V32RegClass);
102
103 addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass);
104
105 const TargetRegisterClass *V64RegClass =
106 TRI->getDefaultVectorSuperClassForBitWidth(64);
107
108 addRegisterClass(MVT::f64, V64RegClass);
109 addRegisterClass(MVT::v2f32, V64RegClass);
110 addRegisterClass(MVT::Untyped, V64RegClass);
111
112 addRegisterClass(MVT::v3i32, &AMDGPU::SGPR_96RegClass);
113 addRegisterClass(MVT::v3f32, TRI->getDefaultVectorSuperClassForBitWidth(96));
114
115 addRegisterClass(MVT::v2i64, &AMDGPU::SGPR_128RegClass);
116 addRegisterClass(MVT::v2f64, &AMDGPU::SGPR_128RegClass);
117
118 addRegisterClass(MVT::v4i32, &AMDGPU::SGPR_128RegClass);
119 addRegisterClass(MVT::v4f32, TRI->getDefaultVectorSuperClassForBitWidth(128));
120
121 addRegisterClass(MVT::v5i32, &AMDGPU::SGPR_160RegClass);
122 addRegisterClass(MVT::v5f32, TRI->getDefaultVectorSuperClassForBitWidth(160));
123
124 addRegisterClass(MVT::v6i32, &AMDGPU::SGPR_192RegClass);
125 addRegisterClass(MVT::v6f32, TRI->getDefaultVectorSuperClassForBitWidth(192));
126
127 addRegisterClass(MVT::v3i64, &AMDGPU::SGPR_192RegClass);
128 addRegisterClass(MVT::v3f64, TRI->getDefaultVectorSuperClassForBitWidth(192));
129
130 addRegisterClass(MVT::v7i32, &AMDGPU::SGPR_224RegClass);
131 addRegisterClass(MVT::v7f32, TRI->getDefaultVectorSuperClassForBitWidth(224));
132
133 addRegisterClass(MVT::v8i32, &AMDGPU::SGPR_256RegClass);
134 addRegisterClass(MVT::v8f32, TRI->getDefaultVectorSuperClassForBitWidth(256));
135
136 addRegisterClass(MVT::v4i64, &AMDGPU::SGPR_256RegClass);
137 addRegisterClass(MVT::v4f64, TRI->getDefaultVectorSuperClassForBitWidth(256));
138
139 addRegisterClass(MVT::v9i32, &AMDGPU::SGPR_288RegClass);
140 addRegisterClass(MVT::v9f32, TRI->getDefaultVectorSuperClassForBitWidth(288));
141
142 addRegisterClass(MVT::v10i32, &AMDGPU::SGPR_320RegClass);
143 addRegisterClass(MVT::v10f32,
144 TRI->getDefaultVectorSuperClassForBitWidth(320));
145
146 addRegisterClass(MVT::v11i32, &AMDGPU::SGPR_352RegClass);
147 addRegisterClass(MVT::v11f32,
148 TRI->getDefaultVectorSuperClassForBitWidth(352));
149
150 addRegisterClass(MVT::v12i32, &AMDGPU::SGPR_384RegClass);
151 addRegisterClass(MVT::v12f32,
152 TRI->getDefaultVectorSuperClassForBitWidth(384));
153
154 addRegisterClass(MVT::v16i32, &AMDGPU::SGPR_512RegClass);
155 addRegisterClass(MVT::v16f32,
156 TRI->getDefaultVectorSuperClassForBitWidth(512));
157
158 addRegisterClass(MVT::v8i64, &AMDGPU::SGPR_512RegClass);
159 addRegisterClass(MVT::v8f64, TRI->getDefaultVectorSuperClassForBitWidth(512));
160
161 addRegisterClass(MVT::v16i64, &AMDGPU::SGPR_1024RegClass);
162 addRegisterClass(MVT::v16f64,
163 TRI->getDefaultVectorSuperClassForBitWidth(1024));
164
165 if (Subtarget->has16BitInsts()) {
166 if (Subtarget->useRealTrue16Insts()) {
167 addRegisterClass(MVT::i16, &AMDGPU::VGPR_16RegClass);
168 addRegisterClass(MVT::f16, &AMDGPU::VGPR_16RegClass);
169 addRegisterClass(MVT::bf16, &AMDGPU::VGPR_16RegClass);
170 } else {
171 addRegisterClass(MVT::i16, &AMDGPU::SReg_32RegClass);
172 addRegisterClass(MVT::f16, &AMDGPU::SReg_32RegClass);
173 addRegisterClass(MVT::bf16, &AMDGPU::SReg_32RegClass);
174 }
175
176 // Unless there are also VOP3P operations, not operations are really legal.
177 addRegisterClass(MVT::v2i16, &AMDGPU::SReg_32RegClass);
178 addRegisterClass(MVT::v2f16, &AMDGPU::SReg_32RegClass);
179 addRegisterClass(MVT::v2bf16, &AMDGPU::SReg_32RegClass);
180 addRegisterClass(MVT::v4i16, &AMDGPU::SReg_64RegClass);
181 addRegisterClass(MVT::v4f16, &AMDGPU::SReg_64RegClass);
182 addRegisterClass(MVT::v4bf16, &AMDGPU::SReg_64RegClass);
183 addRegisterClass(MVT::v8i16, &AMDGPU::SGPR_128RegClass);
184 addRegisterClass(MVT::v8f16, &AMDGPU::SGPR_128RegClass);
185 addRegisterClass(MVT::v8bf16, &AMDGPU::SGPR_128RegClass);
186 addRegisterClass(MVT::v16i16, &AMDGPU::SGPR_256RegClass);
187 addRegisterClass(MVT::v16f16, &AMDGPU::SGPR_256RegClass);
188 addRegisterClass(MVT::v16bf16, &AMDGPU::SGPR_256RegClass);
189 addRegisterClass(MVT::v32i16, &AMDGPU::SGPR_512RegClass);
190 addRegisterClass(MVT::v32f16, &AMDGPU::SGPR_512RegClass);
191 addRegisterClass(MVT::v32bf16, &AMDGPU::SGPR_512RegClass);
192 }
193
194 addRegisterClass(MVT::v32i32, &AMDGPU::VReg_1024RegClass);
195 addRegisterClass(MVT::v32f32,
196 TRI->getDefaultVectorSuperClassForBitWidth(1024));
197
198 computeRegisterProperties(Subtarget->getRegisterInfo());
199
200 // The boolean content concept here is too inflexible. Compares only ever
201 // really produce a 1-bit result. Any copy/extend from these will turn into a
202 // select, and zext/1 or sext/-1 are equally cheap. Arbitrarily choose 0/1, as
203 // it's what most targets use.
206
207 // We need to custom lower vector stores from local memory
209 {MVT::v2i32, MVT::v3i32, MVT::v4i32, MVT::v5i32,
210 MVT::v6i32, MVT::v7i32, MVT::v8i32, MVT::v9i32,
211 MVT::v10i32, MVT::v11i32, MVT::v12i32, MVT::v16i32,
212 MVT::i1, MVT::v32i32},
213 Custom);
214
216 {MVT::v2i32, MVT::v3i32, MVT::v4i32, MVT::v5i32,
217 MVT::v6i32, MVT::v7i32, MVT::v8i32, MVT::v9i32,
218 MVT::v10i32, MVT::v11i32, MVT::v12i32, MVT::v16i32,
219 MVT::i1, MVT::v32i32},
220 Custom);
221
222 if (isTypeLegal(MVT::bf16)) {
223 for (unsigned Opc :
232 ISD::SETCC}) {
233 // FIXME: The promoted to type shouldn't need to be explicit
234 setOperationAction(Opc, MVT::bf16, Promote);
235 AddPromotedToType(Opc, MVT::bf16, MVT::f32);
236 }
237
239
241 AddPromotedToType(ISD::SELECT, MVT::bf16, MVT::i16);
242
246
247 // We only need to custom lower because we can't specify an action for bf16
248 // sources.
251 }
252
253 setTruncStoreAction(MVT::v2i32, MVT::v2i16, Expand);
254 setTruncStoreAction(MVT::v3i32, MVT::v3i16, Expand);
255 setTruncStoreAction(MVT::v4i32, MVT::v4i16, Expand);
256 setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand);
257 setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand);
258 setTruncStoreAction(MVT::v32i32, MVT::v32i16, Expand);
259 setTruncStoreAction(MVT::v2i32, MVT::v2i8, Expand);
260 setTruncStoreAction(MVT::v4i32, MVT::v4i8, Expand);
261 setTruncStoreAction(MVT::v8i32, MVT::v8i8, Expand);
262 setTruncStoreAction(MVT::v16i32, MVT::v16i8, Expand);
263 setTruncStoreAction(MVT::v32i32, MVT::v32i8, Expand);
264 setTruncStoreAction(MVT::v2i16, MVT::v2i8, Expand);
265 setTruncStoreAction(MVT::v4i16, MVT::v4i8, Expand);
266 setTruncStoreAction(MVT::v8i16, MVT::v8i8, Expand);
267 setTruncStoreAction(MVT::v16i16, MVT::v16i8, Expand);
268 setTruncStoreAction(MVT::v32i16, MVT::v32i8, Expand);
269
270 setTruncStoreAction(MVT::v3i64, MVT::v3i16, Expand);
271 setTruncStoreAction(MVT::v3i64, MVT::v3i32, Expand);
272 setTruncStoreAction(MVT::v4i64, MVT::v4i8, Expand);
273 setTruncStoreAction(MVT::v8i64, MVT::v8i8, Expand);
274 setTruncStoreAction(MVT::v8i64, MVT::v8i16, Expand);
275 setTruncStoreAction(MVT::v8i64, MVT::v8i32, Expand);
276 setTruncStoreAction(MVT::v16i64, MVT::v16i32, Expand);
277
278 setOperationAction(ISD::GlobalAddress, {MVT::i32, MVT::i64}, Custom);
279 setOperationAction(ISD::ExternalSymbol, {MVT::i32, MVT::i64}, Custom);
280
284 AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64);
285
286 setOperationAction(ISD::FSQRT, {MVT::f32, MVT::f64}, Custom);
287
289 {MVT::f32, MVT::i32, MVT::i64, MVT::f64, MVT::i1}, Expand);
290
292 setOperationAction(ISD::SETCC, {MVT::v2i1, MVT::v4i1}, Expand);
293 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
294
296 {MVT::v2i32, MVT::v3i32, MVT::v4i32, MVT::v5i32,
297 MVT::v6i32, MVT::v7i32, MVT::v8i32, MVT::v9i32,
298 MVT::v10i32, MVT::v11i32, MVT::v12i32, MVT::v16i32},
299 Expand);
301 {MVT::v2f32, MVT::v3f32, MVT::v4f32, MVT::v5f32,
302 MVT::v6f32, MVT::v7f32, MVT::v8f32, MVT::v9f32,
303 MVT::v10f32, MVT::v11f32, MVT::v12f32, MVT::v16f32},
304 Expand);
305
307 {MVT::v2i1, MVT::v4i1, MVT::v2i8, MVT::v4i8, MVT::v2i16,
308 MVT::v3i16, MVT::v4i16, MVT::Other},
309 Custom);
310
313 {MVT::i1, MVT::i32, MVT::i64, MVT::f32, MVT::f64}, Expand);
314
316
318
320 Expand);
321
322#if 0
324#endif
325
326 // We only support LOAD/STORE and vector manipulation ops for vectors
327 // with > 4 elements.
328 for (MVT VT :
329 {MVT::v8i32, MVT::v8f32, MVT::v9i32, MVT::v9f32, MVT::v10i32,
330 MVT::v10f32, MVT::v11i32, MVT::v11f32, MVT::v12i32, MVT::v12f32,
331 MVT::v16i32, MVT::v16f32, MVT::v2i64, MVT::v2f64, MVT::v4i16,
332 MVT::v4f16, MVT::v4bf16, MVT::v3i64, MVT::v3f64, MVT::v6i32,
333 MVT::v6f32, MVT::v4i64, MVT::v4f64, MVT::v8i64, MVT::v8f64,
334 MVT::v8i16, MVT::v8f16, MVT::v8bf16, MVT::v16i16, MVT::v16f16,
335 MVT::v16bf16, MVT::v16i64, MVT::v16f64, MVT::v32i32, MVT::v32f32,
336 MVT::v32i16, MVT::v32f16, MVT::v32bf16}) {
337 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
338 switch (Op) {
339 case ISD::LOAD:
340 case ISD::STORE:
342 case ISD::BITCAST:
343 case ISD::UNDEF:
347 case ISD::IS_FPCLASS:
348 break;
353 break;
354 default:
356 break;
357 }
358 }
359 }
360
362
363 // TODO: For dynamic 64-bit vector inserts/extracts, should emit a pseudo that
364 // is expanded to avoid having two separate loops in case the index is a VGPR.
365
366 // Most operations are naturally 32-bit vector operations. We only support
367 // load and store of i64 vectors, so promote v2i64 vector operations to v4i32.
368 for (MVT Vec64 : {MVT::v2i64, MVT::v2f64}) {
370 AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v4i32);
371
373 AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v4i32);
374
376 AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v4i32);
377
379 AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v4i32);
380 }
381
382 for (MVT Vec64 : {MVT::v3i64, MVT::v3f64}) {
384 AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v6i32);
385
387 AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v6i32);
388
390 AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v6i32);
391
393 AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v6i32);
394 }
395
396 for (MVT Vec64 : {MVT::v4i64, MVT::v4f64}) {
398 AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v8i32);
399
401 AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v8i32);
402
404 AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v8i32);
405
407 AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v8i32);
408 }
409
410 for (MVT Vec64 : {MVT::v8i64, MVT::v8f64}) {
412 AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v16i32);
413
415 AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v16i32);
416
418 AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v16i32);
419
421 AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v16i32);
422 }
423
424 for (MVT Vec64 : {MVT::v16i64, MVT::v16f64}) {
426 AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v32i32);
427
429 AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v32i32);
430
432 AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v32i32);
433
435 AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v32i32);
436 }
437
439 {MVT::v4i32, MVT::v4f32, MVT::v8i32, MVT::v8f32,
440 MVT::v16i32, MVT::v16f32, MVT::v32i32, MVT::v32f32},
441 Custom);
442
443 if (Subtarget->hasPkMovB32()) {
444 // TODO: 16-bit element vectors should be legal with even aligned elements.
445 // TODO: Can be legal with wider source types than the result with
446 // subregister extracts.
447 setOperationAction(ISD::VECTOR_SHUFFLE, {MVT::v2i32, MVT::v2f32}, Legal);
448 }
449
451 // Prevent SELECT v2i32 from being implemented with the above bitwise ops and
452 // instead lower to cndmask in SITargetLowering::LowerSELECT().
454 // Enable MatchRotate to produce ISD::ROTR, which is later transformed to
455 // alignbit.
456 setOperationAction(ISD::ROTR, MVT::v2i32, Custom);
457
458 setOperationAction(ISD::BUILD_VECTOR, {MVT::v4f16, MVT::v4i16, MVT::v4bf16},
459 Custom);
460
461 // Avoid stack access for these.
462 // TODO: Generalize to more vector types.
464 {MVT::v2i16, MVT::v2f16, MVT::v2bf16, MVT::v2i8, MVT::v4i8,
465 MVT::v8i8, MVT::v4i16, MVT::v4f16, MVT::v4bf16},
466 Custom);
467
468 // Deal with vec3 vector operations when widened to vec4.
470 {MVT::v3i32, MVT::v3f32, MVT::v4i32, MVT::v4f32}, Custom);
471
472 // Deal with vec5/6/7 vector operations when widened to vec8.
474 {MVT::v5i32, MVT::v5f32, MVT::v6i32, MVT::v6f32,
475 MVT::v7i32, MVT::v7f32, MVT::v8i32, MVT::v8f32,
476 MVT::v9i32, MVT::v9f32, MVT::v10i32, MVT::v10f32,
477 MVT::v11i32, MVT::v11f32, MVT::v12i32, MVT::v12f32},
478 Custom);
479
480 // BUFFER/FLAT_ATOMIC_CMP_SWAP on GCN GPUs needs input marshalling,
481 // and output demarshalling
482 setOperationAction(ISD::ATOMIC_CMP_SWAP, {MVT::i32, MVT::i64}, Custom);
483
484 // We can't return success/failure, only the old value,
485 // let LLVM add the comparison
487 Expand);
488
489 setOperationAction(ISD::ADDRSPACECAST, {MVT::i32, MVT::i64}, Custom);
490
491 setOperationAction(ISD::BITREVERSE, {MVT::i32, MVT::i64}, Legal);
492
493 // FIXME: This should be narrowed to i32, but that only happens if i64 is
494 // illegal.
495 // FIXME: Should lower sub-i32 bswaps to bit-ops without v_perm_b32.
496 setOperationAction(ISD::BSWAP, {MVT::i64, MVT::i32}, Legal);
497
498 // On SI this is s_memtime and s_memrealtime on VI.
500
501 if (Subtarget->hasSMemRealTime() ||
502 Subtarget->getGeneration() >= AMDGPUSubtarget::GFX11)
505
506 if (Subtarget->has16BitInsts()) {
509 } else {
511 }
512
513 if (Subtarget->hasMadMacF32Insts())
515
518
519 // We only really have 32-bit BFE instructions (and 16-bit on VI).
520 //
521 // On SI+ there are 64-bit BFEs, but they are scalar only and there isn't any
522 // effort to match them now. We want this to be false for i64 cases when the
523 // extraction isn't restricted to the upper or lower half. Ideally we would
524 // have some pass reduce 64-bit extracts to 32-bit if possible. Extracts that
525 // span the midpoint are probably relatively rare, so don't worry about them
526 // for now.
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
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.
545 {MVT::f32, MVT::f64}, Legal);
546
547 if (Subtarget->haveRoundOpsF64())
549 Legal);
550 else
552 MVT::f64, Custom);
553
555 setOperationAction({ISD::FLDEXP, ISD::STRICT_FLDEXP}, {MVT::f32, MVT::f64},
556 Legal);
557 setOperationAction(ISD::FFREXP, {MVT::f32, MVT::f64}, Custom);
558
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.
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
587
588 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
589
591 AddPromotedToType(ISD::FP16_TO_FP, MVT::i16, MVT::i32);
593 AddPromotedToType(ISD::FP_TO_FP16, MVT::i16, MVT::i32);
594
598
600
601 // F16 - Constant Actions.
604
605 // F16 - Load/Store Actions.
607 AddPromotedToType(ISD::LOAD, MVT::f16, MVT::i16);
609 AddPromotedToType(ISD::STORE, MVT::f16, MVT::i16);
610
611 // BF16 - Load/Store Actions.
613 AddPromotedToType(ISD::LOAD, MVT::bf16, MVT::i16);
615 AddPromotedToType(ISD::STORE, MVT::bf16, MVT::i16);
616
617 // F16 - VOP1 Actions.
620 MVT::f16, Custom);
621
622 // BF16 - VOP1 Actions.
623 if (Subtarget->hasBF16TransInsts())
625
628
629 // F16 - VOP2 Actions.
630 setOperationAction({ISD::BR_CC, ISD::SELECT_CC}, {MVT::f16, MVT::bf16},
631 Expand);
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
680 AddPromotedToType(ISD::STORE, MVT::v2i16, MVT::i32);
682 AddPromotedToType(ISD::STORE, MVT::v2f16, MVT::i32);
683
685 AddPromotedToType(ISD::LOAD, MVT::v2i16, MVT::i32);
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
697 AddPromotedToType(ISD::LOAD, MVT::v4i16, MVT::v2i32);
699 AddPromotedToType(ISD::LOAD, MVT::v4f16, MVT::v2i32);
700 setOperationAction(ISD::LOAD, MVT::v4bf16, Promote);
701 AddPromotedToType(ISD::LOAD, MVT::v4bf16, MVT::v2i32);
702
704 AddPromotedToType(ISD::STORE, MVT::v4i16, MVT::v2i32);
706 AddPromotedToType(ISD::STORE, MVT::v4f16, MVT::v2i32);
708 AddPromotedToType(ISD::STORE, MVT::v4bf16, MVT::v2i32);
709
711 AddPromotedToType(ISD::LOAD, MVT::v8i16, MVT::v4i32);
713 AddPromotedToType(ISD::LOAD, MVT::v8f16, MVT::v4i32);
714 setOperationAction(ISD::LOAD, MVT::v8bf16, Promote);
715 AddPromotedToType(ISD::LOAD, MVT::v8bf16, MVT::v4i32);
716
718 AddPromotedToType(ISD::STORE, MVT::v4i16, MVT::v2i32);
720 AddPromotedToType(ISD::STORE, MVT::v4f16, MVT::v2i32);
721
723 AddPromotedToType(ISD::STORE, MVT::v8i16, MVT::v4i32);
725 AddPromotedToType(ISD::STORE, MVT::v8f16, MVT::v4i32);
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
737 AddPromotedToType(ISD::STORE, MVT::v16i16, MVT::v8i32);
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
751 AddPromotedToType(ISD::STORE, MVT::v32i16, MVT::v16i32);
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);
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
784 MVT::f16, Custom);
786
789 {MVT::v4f16, MVT::v8f16, MVT::v16f16, MVT::v32f16},
790 Custom);
791
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
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
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
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
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())
895
896 if (Subtarget->hasIEEEMinimumMaximumInsts()) {
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())
903
904 if (Subtarget->hasMinimum3Maximum3PKF16()) {
906
907 // If only the vector form is available, we need to widen to a vector.
908 if (!Subtarget->hasMinimum3Maximum3F16())
910 }
911 }
912
913 if (Subtarget->hasVOP3PInsts()) {
914 // We want to break these into v2f16 pieces, not scalarize.
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
949
950 // TODO: Could move this to custom lowering, could benefit from combines on
951 // extract of relevant bits.
953
955
956 if (Subtarget->hasBF16ConversionInsts()) {
957 setOperationAction(ISD::FP_ROUND, {MVT::bf16, MVT::v2bf16}, Custom);
959 }
960
961 if (Subtarget->hasBF16PackedInsts()) {
964 MVT::v2bf16, Legal);
965 }
966
967 if (Subtarget->hasBF16TransInsts()) {
969 }
970
971 if (Subtarget->hasCvtPkF16F32Inst()) {
973 {MVT::v2f16, MVT::v4f16, MVT::v8f16, MVT::v16f16},
974 Custom);
975 }
976
980 ISD::SUB,
982 ISD::MUL,
983 ISD::FADD,
984 ISD::FSUB,
985 ISD::FDIV,
986 ISD::FMUL,
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.
1029 ISD::STORE,
1054
1055 // FIXME: In other contexts we pretend this is a per-function property.
1057
1059}
1060
1061const GCNSubtarget *SITargetLowering::getSubtarget() const { return Subtarget; }
1062
1064 static const MCPhysReg RCRegs[] = {AMDGPU::MODE};
1065 return RCRegs;
1066}
1067
1068//===----------------------------------------------------------------------===//
1069// TargetLowering queries
1070//===----------------------------------------------------------------------===//
1071
1072// v_mad_mix* support a conversion from f16 to f32.
1073//
1074// There is only one special case when denormals are enabled we don't currently,
1075// where this is OK to use.
1076bool SITargetLowering::isFPExtFoldable(const SelectionDAG &DAG, unsigned Opcode,
1077 EVT DestVT, EVT SrcVT) const {
1078 return DestVT.getScalarType() == MVT::f32 &&
1079 ((((Opcode == ISD::FMAD && Subtarget->hasMadMixInsts()) ||
1080 (Opcode == ISD::FMA && Subtarget->hasFmaMixInsts())) &&
1081 SrcVT.getScalarType() == MVT::f16) ||
1082 (Opcode == ISD::FMA && Subtarget->hasFmaMixBF16Insts() &&
1083 SrcVT.getScalarType() == MVT::bf16)) &&
1084 // TODO: This probably only requires no input flushing?
1086}
1087
1089 LLT DestTy, LLT SrcTy) const {
1090 return ((Opcode == TargetOpcode::G_FMAD && Subtarget->hasMadMixInsts()) ||
1091 (Opcode == TargetOpcode::G_FMA && Subtarget->hasFmaMixInsts())) &&
1092 DestTy.getScalarSizeInBits() == 32 &&
1093 SrcTy.getScalarSizeInBits() == 16 &&
1094 // TODO: This probably only requires no input flushing?
1095 denormalModeIsFlushAllF32(*MI.getMF());
1096}
1097
1099 // SI has some legal vector types, but no legal vector operations. Say no
1100 // shuffles are legal in order to prefer scalarizing some vector operations.
1101 return false;
1102}
1103
1105 CallingConv::ID CC,
1106 EVT VT) const {
1108 return TargetLowering::getRegisterTypeForCallingConv(Context, CC, VT);
1109
1110 if (VT.isVector()) {
1111 EVT ScalarVT = VT.getScalarType();
1112 unsigned Size = ScalarVT.getSizeInBits();
1113 if (Size == 16) {
1114 if (Subtarget->has16BitInsts()) {
1115 if (VT.isInteger())
1116 return MVT::v2i16;
1117 return (ScalarVT == MVT::bf16 ? MVT::i32 : MVT::v2f16);
1118 }
1119 return VT.isInteger() ? MVT::i32 : MVT::f32;
1120 }
1121
1122 if (Size < 16)
1123 return Subtarget->has16BitInsts() ? MVT::i16 : MVT::i32;
1124 return Size == 32 ? ScalarVT.getSimpleVT() : MVT::i32;
1125 }
1126
1127 if (VT.getSizeInBits() > 32)
1128 return MVT::i32;
1129
1130 return TargetLowering::getRegisterTypeForCallingConv(Context, CC, VT);
1131}
1132
1134 CallingConv::ID CC,
1135 EVT VT) const {
1137 return TargetLowering::getNumRegistersForCallingConv(Context, CC, VT);
1138
1139 if (VT.isVector()) {
1140 unsigned NumElts = VT.getVectorNumElements();
1141 EVT ScalarVT = VT.getScalarType();
1142 unsigned Size = ScalarVT.getSizeInBits();
1143
1144 // FIXME: Should probably promote 8-bit vectors to i16.
1145 if (Size == 16 && Subtarget->has16BitInsts())
1146 return (NumElts + 1) / 2;
1147
1148 if (Size <= 32)
1149 return NumElts;
1150
1151 if (Size > 32)
1152 return NumElts * ((Size + 31) / 32);
1153 } else if (VT.getSizeInBits() > 32)
1154 return (VT.getSizeInBits() + 31) / 32;
1155
1156 return TargetLowering::getNumRegistersForCallingConv(Context, CC, VT);
1157}
1158
1160 LLVMContext &Context, CallingConv::ID CC, EVT VT, EVT &IntermediateVT,
1161 unsigned &NumIntermediates, MVT &RegisterVT) const {
1162 if (CC != CallingConv::AMDGPU_KERNEL && VT.isVector()) {
1163 unsigned NumElts = VT.getVectorNumElements();
1164 EVT ScalarVT = VT.getScalarType();
1165 unsigned Size = ScalarVT.getSizeInBits();
1166 // FIXME: We should fix the ABI to be the same on targets without 16-bit
1167 // support, but unless we can properly handle 3-vectors, it will be still be
1168 // inconsistent.
1169 if (Size == 16 && Subtarget->has16BitInsts()) {
1170 if (ScalarVT == MVT::bf16) {
1171 RegisterVT = MVT::i32;
1172 IntermediateVT = MVT::v2bf16;
1173 } else {
1174 RegisterVT = VT.isInteger() ? MVT::v2i16 : MVT::v2f16;
1175 IntermediateVT = RegisterVT;
1176 }
1177 NumIntermediates = (NumElts + 1) / 2;
1178 return NumIntermediates;
1179 }
1180
1181 if (Size == 32) {
1182 RegisterVT = ScalarVT.getSimpleVT();
1183 IntermediateVT = RegisterVT;
1184 NumIntermediates = NumElts;
1185 return NumIntermediates;
1186 }
1187
1188 if (Size < 16 && Subtarget->has16BitInsts()) {
1189 // FIXME: Should probably form v2i16 pieces
1190 RegisterVT = MVT::i16;
1191 IntermediateVT = ScalarVT;
1192 NumIntermediates = NumElts;
1193 return NumIntermediates;
1194 }
1195
1196 if (Size != 16 && Size <= 32) {
1197 RegisterVT = MVT::i32;
1198 IntermediateVT = ScalarVT;
1199 NumIntermediates = NumElts;
1200 return NumIntermediates;
1201 }
1202
1203 if (Size > 32) {
1204 RegisterVT = MVT::i32;
1205 IntermediateVT = RegisterVT;
1206 NumIntermediates = NumElts * ((Size + 31) / 32);
1207 return NumIntermediates;
1208 }
1209 }
1210
1212 Context, CC, VT, IntermediateVT, NumIntermediates, RegisterVT);
1213}
1214
1216 const DataLayout &DL, Type *Ty,
1217 unsigned MaxNumLanes) {
1218 assert(MaxNumLanes != 0);
1219
1220 LLVMContext &Ctx = Ty->getContext();
1221 if (auto *VT = dyn_cast<FixedVectorType>(Ty)) {
1222 unsigned NumElts = std::min(MaxNumLanes, VT->getNumElements());
1223 return EVT::getVectorVT(Ctx, TLI.getValueType(DL, VT->getElementType()),
1224 NumElts);
1225 }
1226
1227 return TLI.getValueType(DL, Ty);
1228}
1229
1230// Peek through TFE struct returns to only use the data size.
1232 const DataLayout &DL, Type *Ty,
1233 unsigned MaxNumLanes) {
1234 auto *ST = dyn_cast<StructType>(Ty);
1235 if (!ST)
1236 return memVTFromLoadIntrData(TLI, DL, Ty, MaxNumLanes);
1237
1238 // TFE intrinsics return an aggregate type.
1239 assert(ST->getNumContainedTypes() == 2 &&
1240 ST->getContainedType(1)->isIntegerTy(32));
1241 return memVTFromLoadIntrData(TLI, DL, ST->getContainedType(0), MaxNumLanes);
1242}
1243
1244/// Map address space 7 to MVT::amdgpuBufferFatPointer because that's its
1245/// in-memory representation. This return value is a custom type because there
1246/// is no MVT::i160 and adding one breaks integer promotion logic. While this
1247/// could cause issues during codegen, these address space 7 pointers will be
1248/// rewritten away by then. Therefore, we can return MVT::amdgpuBufferFatPointer
1249/// in order to allow pre-codegen passes that query TargetTransformInfo, often
1250/// for cost modeling, to work. (This also sets us up decently for doing the
1251/// buffer lowering in GlobalISel if SelectionDAG ever goes away.)
1253 if (AMDGPUAS::BUFFER_FAT_POINTER == AS && DL.getPointerSizeInBits(AS) == 160)
1254 return MVT::amdgpuBufferFatPointer;
1256 DL.getPointerSizeInBits(AS) == 192)
1257 return MVT::amdgpuBufferStridedPointer;
1259}
1260/// Similarly, the in-memory representation of a p7 is {p8, i32}, aka
1261/// v8i32 when padding is added.
1262/// The in-memory representation of a p9 is {p8, i32, i32}, which is
1263/// also v8i32 with padding.
1265 if ((AMDGPUAS::BUFFER_FAT_POINTER == AS &&
1266 DL.getPointerSizeInBits(AS) == 160) ||
1268 DL.getPointerSizeInBits(AS) == 192))
1269 return MVT::v8i32;
1271}
1272
1273static unsigned getIntrMemWidth(unsigned IntrID) {
1274 switch (IntrID) {
1275 case Intrinsic::amdgcn_global_load_async_to_lds_b8:
1276 case Intrinsic::amdgcn_cluster_load_async_to_lds_b8:
1277 case Intrinsic::amdgcn_global_store_async_from_lds_b8:
1278 return 8;
1279 case Intrinsic::amdgcn_global_load_async_to_lds_b32:
1280 case Intrinsic::amdgcn_cluster_load_async_to_lds_b32:
1281 case Intrinsic::amdgcn_global_store_async_from_lds_b32:
1282 case Intrinsic::amdgcn_cooperative_atomic_load_32x4B:
1283 case Intrinsic::amdgcn_cooperative_atomic_store_32x4B:
1284 return 32;
1285 case Intrinsic::amdgcn_global_load_async_to_lds_b64:
1286 case Intrinsic::amdgcn_cluster_load_async_to_lds_b64:
1287 case Intrinsic::amdgcn_global_store_async_from_lds_b64:
1288 case Intrinsic::amdgcn_cooperative_atomic_load_16x8B:
1289 case Intrinsic::amdgcn_cooperative_atomic_store_16x8B:
1290 return 64;
1291 case Intrinsic::amdgcn_global_load_async_to_lds_b128:
1292 case Intrinsic::amdgcn_cluster_load_async_to_lds_b128:
1293 case Intrinsic::amdgcn_global_store_async_from_lds_b128:
1294 case Intrinsic::amdgcn_cooperative_atomic_load_8x16B:
1295 case Intrinsic::amdgcn_cooperative_atomic_store_8x16B:
1296 return 128;
1297 default:
1298 llvm_unreachable("Unknown width");
1299 }
1300}
1301
1302static void getCoopAtomicOperandsInfo(const CallBase &CI, bool IsLoad,
1304 Value *OrderingArg = CI.getArgOperand(IsLoad ? 1 : 2);
1305 unsigned Ord = cast<ConstantInt>(OrderingArg)->getZExtValue();
1306 switch (AtomicOrderingCABI(Ord)) {
1309 break;
1312 break;
1315 break;
1316 default:
1318 break;
1319 }
1320
1321 Info.flags =
1323 Info.flags |= MOCooperative;
1324
1325 MDNode *ScopeMD = cast<MDNode>(
1326 cast<MetadataAsValue>(CI.getArgOperand(IsLoad ? 2 : 3))->getMetadata());
1327 StringRef Scope = cast<MDString>(ScopeMD->getOperand(0))->getString();
1328 Info.ssid = CI.getContext().getOrInsertSyncScopeID(Scope);
1329}
1330
1332 const CallBase &CI,
1333 MachineFunction &MF,
1334 unsigned IntrID) const {
1335 Info.flags = MachineMemOperand::MONone;
1336 if (CI.hasMetadata(LLVMContext::MD_invariant_load))
1337 Info.flags |= MachineMemOperand::MOInvariant;
1338 if (CI.hasMetadata(LLVMContext::MD_nontemporal))
1340 Info.flags |= getTargetMMOFlags(CI);
1341
1342 if (const AMDGPU::RsrcIntrinsic *RsrcIntr =
1344 AttributeSet Attr =
1346 MemoryEffects ME = Attr.getMemoryEffects();
1347 if (ME.doesNotAccessMemory())
1348 return false;
1349
1350 // TODO: Should images get their own address space?
1351 Info.fallbackAddressSpace = AMDGPUAS::BUFFER_RESOURCE;
1352
1353 const AMDGPU::MIMGBaseOpcodeInfo *BaseOpcode = nullptr;
1354 if (RsrcIntr->IsImage) {
1355 const AMDGPU::ImageDimIntrinsicInfo *Intr =
1357 BaseOpcode = AMDGPU::getMIMGBaseOpcodeInfo(Intr->BaseOpcode);
1358 Info.align.reset();
1359 }
1360
1361 Value *RsrcArg = CI.getArgOperand(RsrcIntr->RsrcArg);
1362 if (auto *RsrcPtrTy = dyn_cast<PointerType>(RsrcArg->getType())) {
1363 if (RsrcPtrTy->getAddressSpace() == AMDGPUAS::BUFFER_RESOURCE)
1364 // We conservatively set the memory operand of a buffer intrinsic to the
1365 // base resource pointer, so that we can access alias information about
1366 // those pointers. Cases like "this points at the same value
1367 // but with a different offset" are handled in
1368 // areMemAccessesTriviallyDisjoint.
1369 Info.ptrVal = RsrcArg;
1370 }
1371
1372 bool IsSPrefetch = IntrID == Intrinsic::amdgcn_s_buffer_prefetch_data;
1373 if (!IsSPrefetch) {
1374 auto *Aux = cast<ConstantInt>(CI.getArgOperand(CI.arg_size() - 1));
1375 if (Aux->getZExtValue() & AMDGPU::CPol::VOLATILE)
1376 Info.flags |= MachineMemOperand::MOVolatile;
1377 }
1378
1380 if (ME.onlyReadsMemory()) {
1381 if (RsrcIntr->IsImage) {
1382 unsigned MaxNumLanes = 4;
1383
1384 if (!BaseOpcode->Gather4) {
1385 // If this isn't a gather, we may have excess loaded elements in the
1386 // IR type. Check the dmask for the real number of elements loaded.
1387 unsigned DMask =
1388 cast<ConstantInt>(CI.getArgOperand(0))->getZExtValue();
1389 MaxNumLanes = DMask == 0 ? 1 : llvm::popcount(DMask);
1390 }
1391
1392 Info.memVT = memVTFromLoadIntrReturn(*this, MF.getDataLayout(),
1393 CI.getType(), MaxNumLanes);
1394 } else {
1395 Info.memVT =
1397 std::numeric_limits<unsigned>::max());
1398 }
1399
1400 // FIXME: What does alignment mean for an image?
1401 Info.opc = ISD::INTRINSIC_W_CHAIN;
1402 Info.flags |= MachineMemOperand::MOLoad;
1403 } else if (ME.onlyWritesMemory()) {
1404 Info.opc = ISD::INTRINSIC_VOID;
1405
1406 Type *DataTy = CI.getArgOperand(0)->getType();
1407 if (RsrcIntr->IsImage) {
1408 unsigned DMask = cast<ConstantInt>(CI.getArgOperand(1))->getZExtValue();
1409 unsigned DMaskLanes = DMask == 0 ? 1 : llvm::popcount(DMask);
1410 Info.memVT = memVTFromLoadIntrData(*this, MF.getDataLayout(), DataTy,
1411 DMaskLanes);
1412 } else
1413 Info.memVT = getValueType(MF.getDataLayout(), DataTy);
1414
1415 Info.flags |= MachineMemOperand::MOStore;
1416 } else {
1417 // Atomic, NoReturn Sampler or prefetch
1418 Info.opc = CI.getType()->isVoidTy() ? ISD::INTRINSIC_VOID
1420 Info.flags |=
1422
1423 if (!IsSPrefetch)
1424 Info.flags |= MachineMemOperand::MOStore;
1425
1426 switch (IntrID) {
1427 default:
1428 if ((RsrcIntr->IsImage && BaseOpcode->NoReturn) || IsSPrefetch) {
1429 // Fake memory access type for no return sampler intrinsics
1430 Info.memVT = MVT::i32;
1431 } else {
1432 // XXX - Should this be volatile without known ordering?
1433 Info.flags |= MachineMemOperand::MOVolatile;
1434 Info.memVT = MVT::getVT(CI.getArgOperand(0)->getType());
1435 }
1436 break;
1437 case Intrinsic::amdgcn_raw_buffer_load_lds:
1438 case Intrinsic::amdgcn_raw_ptr_buffer_load_lds:
1439 case Intrinsic::amdgcn_struct_buffer_load_lds:
1440 case Intrinsic::amdgcn_struct_ptr_buffer_load_lds: {
1441 unsigned Width = cast<ConstantInt>(CI.getArgOperand(2))->getZExtValue();
1442 Info.memVT = EVT::getIntegerVT(CI.getContext(), Width * 8);
1443 Info.ptrVal = CI.getArgOperand(1);
1444 return true;
1445 }
1446 case Intrinsic::amdgcn_raw_atomic_buffer_load:
1447 case Intrinsic::amdgcn_raw_ptr_atomic_buffer_load:
1448 case Intrinsic::amdgcn_struct_atomic_buffer_load:
1449 case Intrinsic::amdgcn_struct_ptr_atomic_buffer_load: {
1450 Info.memVT =
1452 std::numeric_limits<unsigned>::max());
1453 Info.flags &= ~MachineMemOperand::MOStore;
1454 return true;
1455 }
1456 }
1457 }
1458 return true;
1459 }
1460
1461 switch (IntrID) {
1462 case Intrinsic::amdgcn_ds_ordered_add:
1463 case Intrinsic::amdgcn_ds_ordered_swap: {
1464 Info.opc = ISD::INTRINSIC_W_CHAIN;
1465 Info.memVT = MVT::getVT(CI.getType());
1466 Info.ptrVal = CI.getOperand(0);
1467 Info.align.reset();
1469
1470 const ConstantInt *Vol = cast<ConstantInt>(CI.getOperand(4));
1471 if (!Vol->isZero())
1472 Info.flags |= MachineMemOperand::MOVolatile;
1473
1474 return true;
1475 }
1476 case Intrinsic::amdgcn_ds_add_gs_reg_rtn:
1477 case Intrinsic::amdgcn_ds_sub_gs_reg_rtn: {
1478 Info.opc = ISD::INTRINSIC_W_CHAIN;
1479 Info.memVT = MVT::getVT(CI.getOperand(0)->getType());
1480 Info.ptrVal = nullptr;
1481 Info.fallbackAddressSpace = AMDGPUAS::STREAMOUT_REGISTER;
1483 return true;
1484 }
1485 case Intrinsic::amdgcn_ds_append:
1486 case Intrinsic::amdgcn_ds_consume: {
1487 Info.opc = ISD::INTRINSIC_W_CHAIN;
1488 Info.memVT = MVT::getVT(CI.getType());
1489 Info.ptrVal = CI.getOperand(0);
1490 Info.align.reset();
1492
1493 const ConstantInt *Vol = cast<ConstantInt>(CI.getOperand(1));
1494 if (!Vol->isZero())
1495 Info.flags |= MachineMemOperand::MOVolatile;
1496
1497 return true;
1498 }
1499 case Intrinsic::amdgcn_ds_atomic_async_barrier_arrive_b64:
1500 case Intrinsic::amdgcn_ds_atomic_barrier_arrive_rtn_b64: {
1501 Info.opc = (IntrID == Intrinsic::amdgcn_ds_atomic_barrier_arrive_rtn_b64)
1504 Info.memVT = MVT::getVT(CI.getType());
1505 Info.ptrVal = CI.getOperand(0);
1506 Info.memVT = MVT::i64;
1507 Info.size = 8;
1508 Info.align.reset();
1510 return true;
1511 }
1512 case Intrinsic::amdgcn_image_bvh_dual_intersect_ray:
1513 case Intrinsic::amdgcn_image_bvh_intersect_ray:
1514 case Intrinsic::amdgcn_image_bvh8_intersect_ray: {
1515 Info.opc = ISD::INTRINSIC_W_CHAIN;
1516 Info.memVT =
1517 MVT::getVT(IntrID == Intrinsic::amdgcn_image_bvh_intersect_ray
1518 ? CI.getType()
1520 ->getElementType(0)); // XXX: what is correct VT?
1521
1522 Info.fallbackAddressSpace = AMDGPUAS::BUFFER_RESOURCE;
1523 Info.align.reset();
1524 Info.flags |=
1526 return true;
1527 }
1528 case Intrinsic::amdgcn_global_atomic_fmin_num:
1529 case Intrinsic::amdgcn_global_atomic_fmax_num:
1530 case Intrinsic::amdgcn_global_atomic_ordered_add_b64:
1531 case Intrinsic::amdgcn_flat_atomic_fmin_num:
1532 case Intrinsic::amdgcn_flat_atomic_fmax_num: {
1533 Info.opc = ISD::INTRINSIC_W_CHAIN;
1534 Info.memVT = MVT::getVT(CI.getType());
1535 Info.ptrVal = CI.getOperand(0);
1536 Info.align.reset();
1540 return true;
1541 }
1542 case Intrinsic::amdgcn_flat_load_monitor_b32:
1543 case Intrinsic::amdgcn_flat_load_monitor_b64:
1544 case Intrinsic::amdgcn_flat_load_monitor_b128:
1545 case Intrinsic::amdgcn_global_load_monitor_b32:
1546 case Intrinsic::amdgcn_global_load_monitor_b64:
1547 case Intrinsic::amdgcn_global_load_monitor_b128:
1548 case Intrinsic::amdgcn_cluster_load_b32:
1549 case Intrinsic::amdgcn_cluster_load_b64:
1550 case Intrinsic::amdgcn_cluster_load_b128:
1551 case Intrinsic::amdgcn_ds_load_tr6_b96:
1552 case Intrinsic::amdgcn_ds_load_tr4_b64:
1553 case Intrinsic::amdgcn_ds_load_tr8_b64:
1554 case Intrinsic::amdgcn_ds_load_tr16_b128:
1555 case Intrinsic::amdgcn_global_load_tr6_b96:
1556 case Intrinsic::amdgcn_global_load_tr4_b64:
1557 case Intrinsic::amdgcn_global_load_tr_b64:
1558 case Intrinsic::amdgcn_global_load_tr_b128:
1559 case Intrinsic::amdgcn_ds_read_tr4_b64:
1560 case Intrinsic::amdgcn_ds_read_tr6_b96:
1561 case Intrinsic::amdgcn_ds_read_tr8_b64:
1562 case Intrinsic::amdgcn_ds_read_tr16_b64: {
1563 Info.opc = ISD::INTRINSIC_W_CHAIN;
1564 Info.memVT = MVT::getVT(CI.getType());
1565 Info.ptrVal = CI.getOperand(0);
1566 Info.align.reset();
1567 Info.flags |= MachineMemOperand::MOLoad;
1568 return true;
1569 }
1570 case Intrinsic::amdgcn_cooperative_atomic_load_32x4B:
1571 case Intrinsic::amdgcn_cooperative_atomic_load_16x8B:
1572 case Intrinsic::amdgcn_cooperative_atomic_load_8x16B: {
1573 Info.opc = ISD::INTRINSIC_W_CHAIN;
1574 Info.memVT = EVT::getIntegerVT(CI.getContext(), getIntrMemWidth(IntrID));
1575 Info.ptrVal = CI.getOperand(0);
1576 Info.align.reset();
1577 getCoopAtomicOperandsInfo(CI, /*IsLoad=*/true, Info);
1578 return true;
1579 }
1580 case Intrinsic::amdgcn_cooperative_atomic_store_32x4B:
1581 case Intrinsic::amdgcn_cooperative_atomic_store_16x8B:
1582 case Intrinsic::amdgcn_cooperative_atomic_store_8x16B: {
1583 Info.opc = ISD::INTRINSIC_VOID;
1584 Info.memVT = EVT::getIntegerVT(CI.getContext(), getIntrMemWidth(IntrID));
1585 Info.ptrVal = CI.getArgOperand(0);
1586 Info.align.reset();
1587 getCoopAtomicOperandsInfo(CI, /*IsLoad=*/false, Info);
1588 return true;
1589 }
1590 case Intrinsic::amdgcn_ds_gws_init:
1591 case Intrinsic::amdgcn_ds_gws_barrier:
1592 case Intrinsic::amdgcn_ds_gws_sema_v:
1593 case Intrinsic::amdgcn_ds_gws_sema_br:
1594 case Intrinsic::amdgcn_ds_gws_sema_p:
1595 case Intrinsic::amdgcn_ds_gws_sema_release_all: {
1596 Info.opc = ISD::INTRINSIC_VOID;
1597
1598 const GCNTargetMachine &TM =
1599 static_cast<const GCNTargetMachine &>(getTargetMachine());
1600
1602 Info.ptrVal = MFI->getGWSPSV(TM);
1603
1604 // This is an abstract access, but we need to specify a type and size.
1605 Info.memVT = MVT::i32;
1606 Info.size = 4;
1607 Info.align = Align(4);
1608
1609 if (IntrID == Intrinsic::amdgcn_ds_gws_barrier)
1610 Info.flags |= MachineMemOperand::MOLoad;
1611 else
1612 Info.flags |= MachineMemOperand::MOStore;
1613 return true;
1614 }
1615 case Intrinsic::amdgcn_global_load_async_to_lds_b8:
1616 case Intrinsic::amdgcn_global_load_async_to_lds_b32:
1617 case Intrinsic::amdgcn_global_load_async_to_lds_b64:
1618 case Intrinsic::amdgcn_global_load_async_to_lds_b128:
1619 case Intrinsic::amdgcn_cluster_load_async_to_lds_b8:
1620 case Intrinsic::amdgcn_cluster_load_async_to_lds_b32:
1621 case Intrinsic::amdgcn_cluster_load_async_to_lds_b64:
1622 case Intrinsic::amdgcn_cluster_load_async_to_lds_b128: {
1623 Info.opc = ISD::INTRINSIC_VOID;
1624 Info.memVT = EVT::getIntegerVT(CI.getContext(), getIntrMemWidth(IntrID));
1625 Info.ptrVal = CI.getArgOperand(1);
1627 return true;
1628 }
1629 case Intrinsic::amdgcn_global_store_async_from_lds_b8:
1630 case Intrinsic::amdgcn_global_store_async_from_lds_b32:
1631 case Intrinsic::amdgcn_global_store_async_from_lds_b64:
1632 case Intrinsic::amdgcn_global_store_async_from_lds_b128: {
1633 Info.opc = ISD::INTRINSIC_VOID;
1634 Info.memVT = EVT::getIntegerVT(CI.getContext(), getIntrMemWidth(IntrID));
1635 Info.ptrVal = CI.getArgOperand(0);
1637 return true;
1638 }
1639 case Intrinsic::amdgcn_load_to_lds:
1640 case Intrinsic::amdgcn_global_load_lds: {
1641 Info.opc = ISD::INTRINSIC_VOID;
1642 unsigned Width = cast<ConstantInt>(CI.getArgOperand(2))->getZExtValue();
1643 Info.memVT = EVT::getIntegerVT(CI.getContext(), Width * 8);
1644 Info.ptrVal = CI.getArgOperand(1);
1646 auto *Aux = cast<ConstantInt>(CI.getArgOperand(CI.arg_size() - 1));
1647 if (Aux->getZExtValue() & AMDGPU::CPol::VOLATILE)
1648 Info.flags |= MachineMemOperand::MOVolatile;
1649 return true;
1650 }
1651 case Intrinsic::amdgcn_ds_bvh_stack_rtn:
1652 case Intrinsic::amdgcn_ds_bvh_stack_push4_pop1_rtn:
1653 case Intrinsic::amdgcn_ds_bvh_stack_push8_pop1_rtn:
1654 case Intrinsic::amdgcn_ds_bvh_stack_push8_pop2_rtn: {
1655 Info.opc = ISD::INTRINSIC_W_CHAIN;
1656
1657 const GCNTargetMachine &TM =
1658 static_cast<const GCNTargetMachine &>(getTargetMachine());
1659
1661 Info.ptrVal = MFI->getGWSPSV(TM);
1662
1663 // This is an abstract access, but we need to specify a type and size.
1664 Info.memVT = MVT::i32;
1665 Info.size = 4;
1666 Info.align = Align(4);
1667
1669 return true;
1670 }
1671 case Intrinsic::amdgcn_s_prefetch_data:
1672 case Intrinsic::amdgcn_flat_prefetch:
1673 case Intrinsic::amdgcn_global_prefetch: {
1674 Info.opc = ISD::INTRINSIC_VOID;
1675 Info.memVT = EVT::getIntegerVT(CI.getContext(), 8);
1676 Info.ptrVal = CI.getArgOperand(0);
1677 Info.flags |= MachineMemOperand::MOLoad;
1678 return true;
1679 }
1680 default:
1681 return false;
1682 }
1683}
1684
1686 const CallInst &I, SmallVectorImpl<SDValue> &Ops, SelectionDAG &DAG) const {
1688 case Intrinsic::amdgcn_addrspacecast_nonnull: {
1689 // The DAG's ValueType loses the addrspaces.
1690 // Add them as 2 extra Constant operands "from" and "to".
1691 unsigned SrcAS = I.getOperand(0)->getType()->getPointerAddressSpace();
1692 unsigned DstAS = I.getType()->getPointerAddressSpace();
1693 Ops.push_back(DAG.getTargetConstant(SrcAS, SDLoc(), MVT::i32));
1694 Ops.push_back(DAG.getTargetConstant(DstAS, SDLoc(), MVT::i32));
1695 break;
1696 }
1697 default:
1698 break;
1699 }
1700}
1701
1704 Type *&AccessTy) const {
1705 Value *Ptr = nullptr;
1706 switch (II->getIntrinsicID()) {
1707 case Intrinsic::amdgcn_cluster_load_b128:
1708 case Intrinsic::amdgcn_cluster_load_b64:
1709 case Intrinsic::amdgcn_cluster_load_b32:
1710 case Intrinsic::amdgcn_ds_append:
1711 case Intrinsic::amdgcn_ds_consume:
1712 case Intrinsic::amdgcn_ds_load_tr8_b64:
1713 case Intrinsic::amdgcn_ds_load_tr16_b128:
1714 case Intrinsic::amdgcn_ds_load_tr4_b64:
1715 case Intrinsic::amdgcn_ds_load_tr6_b96:
1716 case Intrinsic::amdgcn_ds_read_tr4_b64:
1717 case Intrinsic::amdgcn_ds_read_tr6_b96:
1718 case Intrinsic::amdgcn_ds_read_tr8_b64:
1719 case Intrinsic::amdgcn_ds_read_tr16_b64:
1720 case Intrinsic::amdgcn_ds_ordered_add:
1721 case Intrinsic::amdgcn_ds_ordered_swap:
1722 case Intrinsic::amdgcn_ds_atomic_async_barrier_arrive_b64:
1723 case Intrinsic::amdgcn_ds_atomic_barrier_arrive_rtn_b64:
1724 case Intrinsic::amdgcn_flat_atomic_fmax_num:
1725 case Intrinsic::amdgcn_flat_atomic_fmin_num:
1726 case Intrinsic::amdgcn_flat_load_monitor_b128:
1727 case Intrinsic::amdgcn_flat_load_monitor_b32:
1728 case Intrinsic::amdgcn_flat_load_monitor_b64:
1729 case Intrinsic::amdgcn_global_atomic_fmax_num:
1730 case Intrinsic::amdgcn_global_atomic_fmin_num:
1731 case Intrinsic::amdgcn_global_atomic_ordered_add_b64:
1732 case Intrinsic::amdgcn_global_load_monitor_b128:
1733 case Intrinsic::amdgcn_global_load_monitor_b32:
1734 case Intrinsic::amdgcn_global_load_monitor_b64:
1735 case Intrinsic::amdgcn_global_load_tr_b64:
1736 case Intrinsic::amdgcn_global_load_tr_b128:
1737 case Intrinsic::amdgcn_global_load_tr4_b64:
1738 case Intrinsic::amdgcn_global_load_tr6_b96:
1739 case Intrinsic::amdgcn_global_store_async_from_lds_b8:
1740 case Intrinsic::amdgcn_global_store_async_from_lds_b32:
1741 case Intrinsic::amdgcn_global_store_async_from_lds_b64:
1742 case Intrinsic::amdgcn_global_store_async_from_lds_b128:
1743 Ptr = II->getArgOperand(0);
1744 break;
1745 case Intrinsic::amdgcn_load_to_lds:
1746 case Intrinsic::amdgcn_global_load_lds:
1747 case Intrinsic::amdgcn_global_load_async_to_lds_b8:
1748 case Intrinsic::amdgcn_global_load_async_to_lds_b32:
1749 case Intrinsic::amdgcn_global_load_async_to_lds_b64:
1750 case Intrinsic::amdgcn_global_load_async_to_lds_b128:
1751 case Intrinsic::amdgcn_cluster_load_async_to_lds_b8:
1752 case Intrinsic::amdgcn_cluster_load_async_to_lds_b32:
1753 case Intrinsic::amdgcn_cluster_load_async_to_lds_b64:
1754 case Intrinsic::amdgcn_cluster_load_async_to_lds_b128:
1755 Ptr = II->getArgOperand(1);
1756 break;
1757 default:
1758 return false;
1759 }
1760 AccessTy = II->getType();
1761 Ops.push_back(Ptr);
1762 return true;
1763}
1764
1766 unsigned AddrSpace) const {
1767 if (!Subtarget->hasFlatInstOffsets()) {
1768 // Flat instructions do not have offsets, and only have the register
1769 // address.
1770 return AM.BaseOffs == 0 && AM.Scale == 0;
1771 }
1772
1773 decltype(SIInstrFlags::FLAT) FlatVariant =
1777
1778 return AM.Scale == 0 &&
1779 (AM.BaseOffs == 0 || Subtarget->getInstrInfo()->isLegalFLATOffset(
1780 AM.BaseOffs, AddrSpace, FlatVariant));
1781}
1782
1784 if (Subtarget->hasFlatGlobalInsts())
1786
1787 if (!Subtarget->hasAddr64() || Subtarget->useFlatForGlobal()) {
1788 // Assume the we will use FLAT for all global memory accesses
1789 // on VI.
1790 // FIXME: This assumption is currently wrong. On VI we still use
1791 // MUBUF instructions for the r + i addressing mode. As currently
1792 // implemented, the MUBUF instructions only work on buffer < 4GB.
1793 // It may be possible to support > 4GB buffers with MUBUF instructions,
1794 // by setting the stride value in the resource descriptor which would
1795 // increase the size limit to (stride * 4GB). However, this is risky,
1796 // because it has never been validated.
1798 }
1799
1800 return isLegalMUBUFAddressingMode(AM);
1801}
1802
1803bool SITargetLowering::isLegalMUBUFAddressingMode(const AddrMode &AM) const {
1804 // MUBUF / MTBUF instructions have a 12-bit unsigned byte offset, and
1805 // additionally can do r + r + i with addr64. 32-bit has more addressing
1806 // mode options. Depending on the resource constant, it can also do
1807 // (i64 r0) + (i32 r1) * (i14 i).
1808 //
1809 // Private arrays end up using a scratch buffer most of the time, so also
1810 // assume those use MUBUF instructions. Scratch loads / stores are currently
1811 // implemented as mubuf instructions with offen bit set, so slightly
1812 // different than the normal addr64.
1813 const SIInstrInfo *TII = Subtarget->getInstrInfo();
1814 if (!TII->isLegalMUBUFImmOffset(AM.BaseOffs))
1815 return false;
1816
1817 // FIXME: Since we can split immediate into soffset and immediate offset,
1818 // would it make sense to allow any immediate?
1819
1820 switch (AM.Scale) {
1821 case 0: // r + i or just i, depending on HasBaseReg.
1822 return true;
1823 case 1:
1824 return true; // We have r + r or r + i.
1825 case 2:
1826 if (AM.HasBaseReg) {
1827 // Reject 2 * r + r.
1828 return false;
1829 }
1830
1831 // Allow 2 * r as r + r
1832 // Or 2 * r + i is allowed as r + r + i.
1833 return true;
1834 default: // Don't allow n * r
1835 return false;
1836 }
1837}
1838
1840 const AddrMode &AM, Type *Ty,
1841 unsigned AS,
1842 Instruction *I) const {
1843 // No global is ever allowed as a base.
1844 if (AM.BaseGV)
1845 return false;
1846
1847 if (AS == AMDGPUAS::GLOBAL_ADDRESS)
1848 return isLegalGlobalAddressingMode(AM);
1849
1850 if (AS == AMDGPUAS::CONSTANT_ADDRESS ||
1854 // If the offset isn't a multiple of 4, it probably isn't going to be
1855 // correctly aligned.
1856 // FIXME: Can we get the real alignment here?
1857 if (AM.BaseOffs % 4 != 0)
1858 return isLegalMUBUFAddressingMode(AM);
1859
1860 if (!Subtarget->hasScalarSubwordLoads()) {
1861 // There are no SMRD extloads, so if we have to do a small type access we
1862 // will use a MUBUF load.
1863 // FIXME?: We also need to do this if unaligned, but we don't know the
1864 // alignment here.
1865 if (Ty->isSized() && DL.getTypeStoreSize(Ty) < 4)
1866 return isLegalGlobalAddressingMode(AM);
1867 }
1868
1869 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) {
1870 // SMRD instructions have an 8-bit, dword offset on SI.
1871 if (!isUInt<8>(AM.BaseOffs / 4))
1872 return false;
1873 } else if (Subtarget->getGeneration() == AMDGPUSubtarget::SEA_ISLANDS) {
1874 // On CI+, this can also be a 32-bit literal constant offset. If it fits
1875 // in 8-bits, it can use a smaller encoding.
1876 if (!isUInt<32>(AM.BaseOffs / 4))
1877 return false;
1878 } else if (Subtarget->getGeneration() < AMDGPUSubtarget::GFX9) {
1879 // On VI, these use the SMEM format and the offset is 20-bit in bytes.
1880 if (!isUInt<20>(AM.BaseOffs))
1881 return false;
1882 } else if (Subtarget->getGeneration() < AMDGPUSubtarget::GFX12) {
1883 // On GFX9 the offset is signed 21-bit in bytes (but must not be negative
1884 // for S_BUFFER_* instructions).
1885 if (!isInt<21>(AM.BaseOffs))
1886 return false;
1887 } else {
1888 // On GFX12, all offsets are signed 24-bit in bytes.
1889 if (!isInt<24>(AM.BaseOffs))
1890 return false;
1891 }
1892
1893 if ((AS == AMDGPUAS::CONSTANT_ADDRESS ||
1895 AM.BaseOffs < 0) {
1896 // Scalar (non-buffer) loads can only use a negative offset if
1897 // soffset+offset is non-negative. Since the compiler can only prove that
1898 // in a few special cases, it is safer to claim that negative offsets are
1899 // not supported.
1900 return false;
1901 }
1902
1903 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
1904 return true;
1905
1906 if (AM.Scale == 1 && AM.HasBaseReg)
1907 return true;
1908
1909 return false;
1910 }
1911
1912 if (AS == AMDGPUAS::PRIVATE_ADDRESS)
1913 return Subtarget->enableFlatScratch()
1915 : isLegalMUBUFAddressingMode(AM);
1916
1917 if (AS == AMDGPUAS::LOCAL_ADDRESS ||
1918 (AS == AMDGPUAS::REGION_ADDRESS && Subtarget->hasGDS())) {
1919 // Basic, single offset DS instructions allow a 16-bit unsigned immediate
1920 // field.
1921 // XXX - If doing a 4-byte aligned 8-byte type access, we effectively have
1922 // an 8-bit dword offset but we don't know the alignment here.
1923 if (!isUInt<16>(AM.BaseOffs))
1924 return false;
1925
1926 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
1927 return true;
1928
1929 if (AM.Scale == 1 && AM.HasBaseReg)
1930 return true;
1931
1932 return false;
1933 }
1934
1936 // For an unknown address space, this usually means that this is for some
1937 // reason being used for pure arithmetic, and not based on some addressing
1938 // computation. We don't have instructions that compute pointers with any
1939 // addressing modes, so treat them as having no offset like flat
1940 // instructions.
1942 }
1943
1944 // Assume a user alias of global for unknown address spaces.
1945 return isLegalGlobalAddressingMode(AM);
1946}
1947
1949 const MachineFunction &MF) const {
1951 return (MemVT.getSizeInBits() <= 4 * 32);
1952 if (AS == AMDGPUAS::PRIVATE_ADDRESS) {
1953 unsigned MaxPrivateBits = 8 * getSubtarget()->getMaxPrivateElementSize();
1954 return (MemVT.getSizeInBits() <= MaxPrivateBits);
1955 }
1957 return (MemVT.getSizeInBits() <= 2 * 32);
1958 return true;
1959}
1960
1962 unsigned Size, unsigned AddrSpace, Align Alignment,
1963 MachineMemOperand::Flags Flags, unsigned *IsFast) const {
1964 if (IsFast)
1965 *IsFast = 0;
1966
1967 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS ||
1968 AddrSpace == AMDGPUAS::REGION_ADDRESS) {
1969 // Check if alignment requirements for ds_read/write instructions are
1970 // disabled.
1971 if (!Subtarget->hasUnalignedDSAccessEnabled() && Alignment < Align(4))
1972 return false;
1973
1974 Align RequiredAlignment(
1975 PowerOf2Ceil(divideCeil(Size, 8))); // Natural alignment.
1976 if (Subtarget->hasLDSMisalignedBug() && Size > 32 &&
1977 Alignment < RequiredAlignment)
1978 return false;
1979
1980 // Either, the alignment requirements are "enabled", or there is an
1981 // unaligned LDS access related hardware bug though alignment requirements
1982 // are "disabled". In either case, we need to check for proper alignment
1983 // requirements.
1984 //
1985 switch (Size) {
1986 case 64:
1987 // SI has a hardware bug in the LDS / GDS bounds checking: if the base
1988 // address is negative, then the instruction is incorrectly treated as
1989 // out-of-bounds even if base + offsets is in bounds. Split vectorized
1990 // loads here to avoid emitting ds_read2_b32. We may re-combine the
1991 // load later in the SILoadStoreOptimizer.
1992 if (!Subtarget->hasUsableDSOffset() && Alignment < Align(8))
1993 return false;
1994
1995 // 8 byte accessing via ds_read/write_b64 require 8-byte alignment, but we
1996 // can do a 4 byte aligned, 8 byte access in a single operation using
1997 // ds_read2/write2_b32 with adjacent offsets.
1998 RequiredAlignment = Align(4);
1999
2000 if (Subtarget->hasUnalignedDSAccessEnabled()) {
2001 // We will either select ds_read_b64/ds_write_b64 or ds_read2_b32/
2002 // ds_write2_b32 depending on the alignment. In either case with either
2003 // alignment there is no faster way of doing this.
2004
2005 // The numbers returned here and below are not additive, it is a 'speed
2006 // rank'. They are just meant to be compared to decide if a certain way
2007 // of lowering an operation is faster than another. For that purpose
2008 // naturally aligned operation gets it bitsize to indicate that "it
2009 // operates with a speed comparable to N-bit wide load". With the full
2010 // alignment ds128 is slower than ds96 for example. If underaligned it
2011 // is comparable to a speed of a single dword access, which would then
2012 // mean 32 < 128 and it is faster to issue a wide load regardless.
2013 // 1 is simply "slow, don't do it". I.e. comparing an aligned load to a
2014 // wider load which will not be aligned anymore the latter is slower.
2015 if (IsFast)
2016 *IsFast = (Alignment >= RequiredAlignment) ? 64
2017 : (Alignment < Align(4)) ? 32
2018 : 1;
2019 return true;
2020 }
2021
2022 break;
2023 case 96:
2024 if (!Subtarget->hasDS96AndDS128())
2025 return false;
2026
2027 // 12 byte accessing via ds_read/write_b96 require 16-byte alignment on
2028 // gfx8 and older.
2029
2030 if (Subtarget->hasUnalignedDSAccessEnabled()) {
2031 // Naturally aligned access is fastest. However, also report it is Fast
2032 // if memory is aligned less than DWORD. A narrow load or store will be
2033 // be equally slow as a single ds_read_b96/ds_write_b96, but there will
2034 // be more of them, so overall we will pay less penalty issuing a single
2035 // instruction.
2036
2037 // See comment on the values above.
2038 if (IsFast)
2039 *IsFast = (Alignment >= RequiredAlignment) ? 96
2040 : (Alignment < Align(4)) ? 32
2041 : 1;
2042 return true;
2043 }
2044
2045 break;
2046 case 128:
2047 if (!Subtarget->hasDS96AndDS128() || !Subtarget->useDS128())
2048 return false;
2049
2050 // 16 byte accessing via ds_read/write_b128 require 16-byte alignment on
2051 // gfx8 and older, but we can do a 8 byte aligned, 16 byte access in a
2052 // single operation using ds_read2/write2_b64.
2053 RequiredAlignment = Align(8);
2054
2055 if (Subtarget->hasUnalignedDSAccessEnabled()) {
2056 // Naturally aligned access is fastest. However, also report it is Fast
2057 // if memory is aligned less than DWORD. A narrow load or store will be
2058 // be equally slow as a single ds_read_b128/ds_write_b128, but there
2059 // will be more of them, so overall we will pay less penalty issuing a
2060 // single instruction.
2061
2062 // See comment on the values above.
2063 if (IsFast)
2064 *IsFast = (Alignment >= RequiredAlignment) ? 128
2065 : (Alignment < Align(4)) ? 32
2066 : 1;
2067 return true;
2068 }
2069
2070 break;
2071 default:
2072 if (Size > 32)
2073 return false;
2074
2075 break;
2076 }
2077
2078 // See comment on the values above.
2079 // Note that we have a single-dword or sub-dword here, so if underaligned
2080 // it is a slowest possible access, hence returned value is 0.
2081 if (IsFast)
2082 *IsFast = (Alignment >= RequiredAlignment) ? Size : 0;
2083
2084 return Alignment >= RequiredAlignment ||
2085 Subtarget->hasUnalignedDSAccessEnabled();
2086 }
2087
2088 // FIXME: We have to be conservative here and assume that flat operations
2089 // will access scratch. If we had access to the IR function, then we
2090 // could determine if any private memory was used in the function.
2091 if (AddrSpace == AMDGPUAS::PRIVATE_ADDRESS ||
2092 AddrSpace == AMDGPUAS::FLAT_ADDRESS) {
2093 bool AlignedBy4 = Alignment >= Align(4);
2094 if (Subtarget->hasUnalignedScratchAccessEnabled()) {
2095 if (IsFast)
2096 *IsFast = AlignedBy4 ? Size : 1;
2097 return true;
2098 }
2099
2100 if (IsFast)
2101 *IsFast = AlignedBy4;
2102
2103 return AlignedBy4;
2104 }
2105
2106 // So long as they are correct, wide global memory operations perform better
2107 // than multiple smaller memory ops -- even when misaligned
2108 if (AMDGPU::isExtendedGlobalAddrSpace(AddrSpace)) {
2109 if (IsFast)
2110 *IsFast = Size;
2111
2112 return Alignment >= Align(4) ||
2113 Subtarget->hasUnalignedBufferAccessEnabled();
2114 }
2115
2116 // Ensure robust out-of-bounds guarantees for buffer accesses are met if
2117 // RelaxedBufferOOBMode is disabled. Normally hardware will ensure proper
2118 // out-of-bounds behavior, but in the edge case where an access starts
2119 // out-of-bounds and then enter in-bounds, the entire access would be treated
2120 // as out-of-bounds. Prevent misaligned memory accesses by requiring the
2121 // natural alignment of buffer accesses.
2122 if (AddrSpace == AMDGPUAS::BUFFER_FAT_POINTER ||
2123 AddrSpace == AMDGPUAS::BUFFER_RESOURCE ||
2124 AddrSpace == AMDGPUAS::BUFFER_STRIDED_POINTER) {
2125 if (!Subtarget->hasRelaxedBufferOOBMode() &&
2126 Alignment < Align(PowerOf2Ceil(divideCeil(Size, 8))))
2127 return false;
2128 }
2129
2130 // Smaller than dword value must be aligned.
2131 if (Size < 32)
2132 return false;
2133
2134 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
2135 // byte-address are ignored, thus forcing Dword alignment.
2136 // This applies to private, global, and constant memory.
2137 if (IsFast)
2138 *IsFast = 1;
2139
2140 return Size >= 32 && Alignment >= Align(4);
2141}
2142
2144 EVT VT, unsigned AddrSpace, Align Alignment, MachineMemOperand::Flags Flags,
2145 unsigned *IsFast) const {
2147 Alignment, Flags, IsFast);
2148}
2149
2151 LLVMContext &Context, const MemOp &Op,
2152 const AttributeList &FuncAttributes) const {
2153 // FIXME: Should account for address space here.
2154
2155 // The default fallback uses the private pointer size as a guess for a type to
2156 // use. Make sure we switch these to 64-bit accesses.
2157
2158 if (Op.size() >= 16 &&
2159 Op.isDstAligned(Align(4))) // XXX: Should only do for global
2160 return MVT::v4i32;
2161
2162 if (Op.size() >= 8 && Op.isDstAligned(Align(4)))
2163 return MVT::v2i32;
2164
2165 // Use the default.
2166 return MVT::Other;
2167}
2168
2170 const MemSDNode *MemNode = cast<MemSDNode>(N);
2171 return MemNode->getMemOperand()->getFlags() & MONoClobber;
2172}
2173
2178
2180 unsigned DestAS) const {
2181 if (SrcAS == AMDGPUAS::FLAT_ADDRESS) {
2182 if (DestAS == AMDGPUAS::PRIVATE_ADDRESS &&
2183 Subtarget->hasGloballyAddressableScratch()) {
2184 // Flat -> private requires subtracting src_flat_scratch_base_lo.
2185 return false;
2186 }
2187
2188 // Flat -> private/local is a simple truncate.
2189 // Flat -> global is no-op
2190 return true;
2191 }
2192
2193 const GCNTargetMachine &TM =
2194 static_cast<const GCNTargetMachine &>(getTargetMachine());
2195 return TM.isNoopAddrSpaceCast(SrcAS, DestAS);
2196}
2197
2205
2207 Type *Ty) const {
2208 // FIXME: Could be smarter if called for vector constants.
2209 return true;
2210}
2211
2213 unsigned Index) const {
2215 return false;
2216
2217 // TODO: Add more cases that are cheap.
2218 return Index == 0;
2219}
2220
2221bool SITargetLowering::isExtractVecEltCheap(EVT VT, unsigned Index) const {
2222 // TODO: This should be more aggressive, particular for 16-bit element
2223 // vectors. However there are some mixed improvements and regressions.
2224 EVT EltTy = VT.getVectorElementType();
2225 return EltTy.getSizeInBits() % 32 == 0;
2226}
2227
2229 if (Subtarget->has16BitInsts() && VT == MVT::i16) {
2230 switch (Op) {
2231 case ISD::LOAD:
2232 case ISD::STORE:
2233 return true;
2234 default:
2235 return false;
2236 }
2237 }
2238
2239 // SimplifySetCC uses this function to determine whether or not it should
2240 // create setcc with i1 operands. We don't have instructions for i1 setcc.
2241 if (VT == MVT::i1 && Op == ISD::SETCC)
2242 return false;
2243
2245}
2246
2249 // This isn't really a constant pool but close enough.
2252 return PtrInfo;
2253}
2254
2255SDValue SITargetLowering::lowerKernArgParameterPtr(SelectionDAG &DAG,
2256 const SDLoc &SL,
2257 SDValue Chain,
2258 uint64_t Offset) const {
2259 const DataLayout &DL = DAG.getDataLayout();
2263
2264 auto [InputPtrReg, RC, ArgTy] =
2265 Info->getPreloadedValue(AMDGPUFunctionArgInfo::KERNARG_SEGMENT_PTR);
2266
2267 // We may not have the kernarg segment argument if we have no kernel
2268 // arguments.
2269 if (!InputPtrReg)
2270 return DAG.getConstant(Offset, SL, PtrVT);
2271
2273 SDValue BasePtr = DAG.getCopyFromReg(
2274 Chain, SL, MRI.getLiveInVirtReg(InputPtrReg->getRegister()), PtrVT);
2275
2276 return DAG.getObjectPtrOffset(SL, BasePtr, TypeSize::getFixed(Offset));
2277}
2278
2279SDValue SITargetLowering::getImplicitArgPtr(SelectionDAG &DAG,
2280 const SDLoc &SL) const {
2283 return lowerKernArgParameterPtr(DAG, SL, DAG.getEntryNode(), Offset);
2284}
2285
2286SDValue SITargetLowering::getLDSKernelId(SelectionDAG &DAG,
2287 const SDLoc &SL) const {
2288
2290 std::optional<uint32_t> KnownSize =
2292 if (KnownSize.has_value())
2293 return DAG.getConstant(*KnownSize, SL, MVT::i32);
2294 return SDValue();
2295}
2296
2297SDValue SITargetLowering::convertArgType(SelectionDAG &DAG, EVT VT, EVT MemVT,
2298 const SDLoc &SL, SDValue Val,
2299 bool Signed,
2300 const ISD::InputArg *Arg) const {
2301 // First, if it is a widened vector, narrow it.
2302 if (VT.isVector() &&
2304 EVT NarrowedVT =
2307 Val = DAG.getNode(ISD::EXTRACT_SUBVECTOR, SL, NarrowedVT, Val,
2308 DAG.getConstant(0, SL, MVT::i32));
2309 }
2310
2311 // Then convert the vector elements or scalar value.
2312 if (Arg && (Arg->Flags.isSExt() || Arg->Flags.isZExt()) && VT.bitsLT(MemVT)) {
2313 unsigned Opc = Arg->Flags.isZExt() ? ISD::AssertZext : ISD::AssertSext;
2314 Val = DAG.getNode(Opc, SL, MemVT, Val, DAG.getValueType(VT));
2315 }
2316
2317 if (MemVT.isFloatingPoint())
2318 Val = getFPExtOrFPRound(DAG, Val, SL, VT);
2319 else if (Signed)
2320 Val = DAG.getSExtOrTrunc(Val, SL, VT);
2321 else
2322 Val = DAG.getZExtOrTrunc(Val, SL, VT);
2323
2324 return Val;
2325}
2326
2327SDValue SITargetLowering::lowerKernargMemParameter(
2328 SelectionDAG &DAG, EVT VT, EVT MemVT, const SDLoc &SL, SDValue Chain,
2329 uint64_t Offset, Align Alignment, bool Signed,
2330 const ISD::InputArg *Arg) const {
2331
2332 MachinePointerInfo PtrInfo =
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,
2349 PtrInfo.getWithOffset(AlignDownOffset), Align(4),
2352
2353 SDValue ShiftAmt = DAG.getConstant(OffsetDiff * 8, SL, MVT::i32);
2354 SDValue Extract = DAG.getNode(ISD::SRL, SL, MVT::i32, Load, ShiftAmt);
2355
2356 SDValue ArgVal = DAG.getNode(ISD::TRUNCATE, SL, IntVT, Extract);
2357 ArgVal = DAG.getNode(ISD::BITCAST, SL, MemVT, ArgVal);
2358 ArgVal = convertArgType(DAG, VT, MemVT, SL, ArgVal, Signed, Arg);
2359
2360 return DAG.getMergeValues({ArgVal, Load.getValue(1)}, SL);
2361 }
2362
2363 SDValue Ptr = lowerKernArgParameterPtr(DAG, SL, Chain, Offset);
2364 SDValue Load = DAG.getLoad(
2365 MemVT, SL, Chain, Ptr, PtrInfo.getWithOffset(Offset), Alignment,
2367
2368 SDValue Val = convertArgType(DAG, VT, MemVT, SL, Load, Signed, Arg);
2369 return DAG.getMergeValues({Val, Load.getValue(1)}, SL);
2370}
2371
2372/// Coerce an argument which was passed in a different ABI type to the original
2373/// expected value type.
2374SDValue SITargetLowering::convertABITypeToValueType(SelectionDAG &DAG,
2375 SDValue Val,
2376 CCValAssign &VA,
2377 const SDLoc &SL) const {
2378 EVT ValVT = VA.getValVT();
2379
2380 // If this is an 8 or 16-bit value, it is really passed promoted
2381 // to 32 bits. Insert an assert[sz]ext to capture this, then
2382 // truncate to the right size.
2383 switch (VA.getLocInfo()) {
2384 case CCValAssign::Full:
2385 return Val;
2386 case CCValAssign::BCvt:
2387 return DAG.getNode(ISD::BITCAST, SL, ValVT, Val);
2388 case CCValAssign::SExt:
2389 Val = DAG.getNode(ISD::AssertSext, SL, VA.getLocVT(), Val,
2390 DAG.getValueType(ValVT));
2391 return DAG.getNode(ISD::TRUNCATE, SL, ValVT, Val);
2392 case CCValAssign::ZExt:
2393 Val = DAG.getNode(ISD::AssertZext, SL, VA.getLocVT(), Val,
2394 DAG.getValueType(ValVT));
2395 return DAG.getNode(ISD::TRUNCATE, SL, ValVT, Val);
2396 case CCValAssign::AExt:
2397 return DAG.getNode(ISD::TRUNCATE, SL, ValVT, Val);
2398 default:
2399 llvm_unreachable("Unknown loc info!");
2400 }
2401}
2402
2403SDValue SITargetLowering::lowerStackParameter(SelectionDAG &DAG,
2404 CCValAssign &VA, const SDLoc &SL,
2405 SDValue Chain,
2406 const ISD::InputArg &Arg) const {
2407 MachineFunction &MF = DAG.getMachineFunction();
2408 MachineFrameInfo &MFI = MF.getFrameInfo();
2409
2410 if (Arg.Flags.isByVal()) {
2411 unsigned Size = Arg.Flags.getByValSize();
2412 int FrameIdx = MFI.CreateFixedObject(Size, VA.getLocMemOffset(), false);
2413 return DAG.getFrameIndex(FrameIdx, MVT::i32);
2414 }
2415
2416 unsigned ArgOffset = VA.getLocMemOffset();
2417 unsigned ArgSize = VA.getValVT().getStoreSize();
2418
2419 int FI = MFI.CreateFixedObject(ArgSize, ArgOffset, true);
2420
2421 // Create load nodes to retrieve arguments from the stack.
2422 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
2423
2424 // For NON_EXTLOAD, generic code in getLoad assert(ValVT == MemVT)
2426 MVT MemVT = VA.getValVT();
2427
2428 switch (VA.getLocInfo()) {
2429 default:
2430 break;
2431 case CCValAssign::BCvt:
2432 MemVT = VA.getLocVT();
2433 break;
2434 case CCValAssign::SExt:
2435 ExtType = ISD::SEXTLOAD;
2436 break;
2437 case CCValAssign::ZExt:
2438 ExtType = ISD::ZEXTLOAD;
2439 break;
2440 case CCValAssign::AExt:
2441 ExtType = ISD::EXTLOAD;
2442 break;
2443 }
2444
2445 SDValue ArgValue = DAG.getExtLoad(
2446 ExtType, SL, VA.getLocVT(), Chain, FIN,
2448
2449 SDValue ConvertedVal = convertABITypeToValueType(DAG, ArgValue, VA, SL);
2450 if (ConvertedVal == ArgValue)
2451 return ConvertedVal;
2452
2453 return DAG.getMergeValues({ConvertedVal, ArgValue.getValue(1)}, SL);
2454}
2455
2456SDValue SITargetLowering::lowerWorkGroupId(
2457 SelectionDAG &DAG, const SIMachineFunctionInfo &MFI, EVT VT,
2460 AMDGPUFunctionArgInfo::PreloadedValue ClusterWorkGroupIdPV) const {
2461 if (!Subtarget->hasClusters())
2462 return getPreloadedValue(DAG, MFI, VT, WorkGroupIdPV);
2463
2464 // Clusters are supported. Return the global position in the grid. If clusters
2465 // are enabled, WorkGroupIdPV returns the cluster ID not the workgroup ID.
2466
2467 // WorkGroupIdXYZ = ClusterId == 0 ?
2468 // ClusterIdXYZ :
2469 // ClusterIdXYZ * (ClusterMaxIdXYZ + 1) + ClusterWorkGroupIdXYZ
2470 SDValue ClusterIdXYZ = getPreloadedValue(DAG, MFI, VT, WorkGroupIdPV);
2471 SDLoc SL(ClusterIdXYZ);
2472 SDValue ClusterMaxIdXYZ = getPreloadedValue(DAG, MFI, VT, ClusterMaxIdPV);
2473 SDValue One = DAG.getConstant(1, SL, VT);
2474 SDValue ClusterSizeXYZ = DAG.getNode(ISD::ADD, SL, VT, ClusterMaxIdXYZ, One);
2475 SDValue ClusterWorkGroupIdXYZ =
2476 getPreloadedValue(DAG, MFI, VT, ClusterWorkGroupIdPV);
2477 SDValue GlobalIdXYZ =
2478 DAG.getNode(ISD::ADD, SL, VT, ClusterWorkGroupIdXYZ,
2479 DAG.getNode(ISD::MUL, SL, VT, ClusterIdXYZ, ClusterSizeXYZ));
2480
2481 switch (MFI.getClusterDims().getKind()) {
2484 return GlobalIdXYZ;
2486 return ClusterIdXYZ;
2488 using namespace AMDGPU::Hwreg;
2489 SDValue ClusterIdField =
2490 DAG.getTargetConstant(HwregEncoding::encode(ID_IB_STS2, 6, 4), SL, VT);
2491 SDNode *GetReg =
2492 DAG.getMachineNode(AMDGPU::S_GETREG_B32_const, SL, VT, ClusterIdField);
2493 SDValue ClusterId(GetReg, 0);
2494 SDValue Zero = DAG.getConstant(0, SL, VT);
2495 return DAG.getNode(ISD::SELECT_CC, SL, VT, ClusterId, Zero, ClusterIdXYZ,
2496 GlobalIdXYZ, DAG.getCondCode(ISD::SETEQ));
2497 }
2498 }
2499
2500 llvm_unreachable("nothing should reach here");
2501}
2502
2503SDValue SITargetLowering::getPreloadedValue(
2504 SelectionDAG &DAG, const SIMachineFunctionInfo &MFI, EVT VT,
2506 const ArgDescriptor *Reg = nullptr;
2507 const TargetRegisterClass *RC;
2508 LLT Ty;
2509
2511 const ArgDescriptor WorkGroupIDX =
2512 ArgDescriptor::createRegister(AMDGPU::TTMP9);
2513 // If GridZ is not programmed in an entry function then the hardware will set
2514 // it to all zeros, so there is no need to mask the GridY value in the low
2515 // order bits.
2516 const ArgDescriptor WorkGroupIDY = ArgDescriptor::createRegister(
2517 AMDGPU::TTMP7,
2518 AMDGPU::isEntryFunctionCC(CC) && !MFI.hasWorkGroupIDZ() ? ~0u : 0xFFFFu);
2519 const ArgDescriptor WorkGroupIDZ =
2520 ArgDescriptor::createRegister(AMDGPU::TTMP7, 0xFFFF0000u);
2521 const ArgDescriptor ClusterWorkGroupIDX =
2522 ArgDescriptor::createRegister(AMDGPU::TTMP6, 0x0000000Fu);
2523 const ArgDescriptor ClusterWorkGroupIDY =
2524 ArgDescriptor::createRegister(AMDGPU::TTMP6, 0x000000F0u);
2525 const ArgDescriptor ClusterWorkGroupIDZ =
2526 ArgDescriptor::createRegister(AMDGPU::TTMP6, 0x00000F00u);
2527 const ArgDescriptor ClusterWorkGroupMaxIDX =
2528 ArgDescriptor::createRegister(AMDGPU::TTMP6, 0x0000F000u);
2529 const ArgDescriptor ClusterWorkGroupMaxIDY =
2530 ArgDescriptor::createRegister(AMDGPU::TTMP6, 0x000F0000u);
2531 const ArgDescriptor ClusterWorkGroupMaxIDZ =
2532 ArgDescriptor::createRegister(AMDGPU::TTMP6, 0x00F00000u);
2533 const ArgDescriptor ClusterWorkGroupMaxFlatID =
2534 ArgDescriptor::createRegister(AMDGPU::TTMP6, 0x0F000000u);
2535
2536 auto LoadConstant = [&](unsigned N) {
2537 return DAG.getConstant(N, SDLoc(), VT);
2538 };
2539
2540 if (Subtarget->hasArchitectedSGPRs() &&
2542 AMDGPU::ClusterDimsAttr ClusterDims = MFI.getClusterDims();
2543 bool HasFixedDims = ClusterDims.isFixedDims();
2544
2545 switch (PVID) {
2547 Reg = &WorkGroupIDX;
2548 RC = &AMDGPU::SReg_32RegClass;
2549 Ty = LLT::scalar(32);
2550 break;
2552 Reg = &WorkGroupIDY;
2553 RC = &AMDGPU::SReg_32RegClass;
2554 Ty = LLT::scalar(32);
2555 break;
2557 Reg = &WorkGroupIDZ;
2558 RC = &AMDGPU::SReg_32RegClass;
2559 Ty = LLT::scalar(32);
2560 break;
2562 if (HasFixedDims && ClusterDims.getDims()[0] == 1)
2563 return LoadConstant(0);
2564 Reg = &ClusterWorkGroupIDX;
2565 RC = &AMDGPU::SReg_32RegClass;
2566 Ty = LLT::scalar(32);
2567 break;
2569 if (HasFixedDims && ClusterDims.getDims()[1] == 1)
2570 return LoadConstant(0);
2571 Reg = &ClusterWorkGroupIDY;
2572 RC = &AMDGPU::SReg_32RegClass;
2573 Ty = LLT::scalar(32);
2574 break;
2576 if (HasFixedDims && ClusterDims.getDims()[2] == 1)
2577 return LoadConstant(0);
2578 Reg = &ClusterWorkGroupIDZ;
2579 RC = &AMDGPU::SReg_32RegClass;
2580 Ty = LLT::scalar(32);
2581 break;
2583 if (HasFixedDims)
2584 return LoadConstant(ClusterDims.getDims()[0] - 1);
2585 Reg = &ClusterWorkGroupMaxIDX;
2586 RC = &AMDGPU::SReg_32RegClass;
2587 Ty = LLT::scalar(32);
2588 break;
2590 if (HasFixedDims)
2591 return LoadConstant(ClusterDims.getDims()[1] - 1);
2592 Reg = &ClusterWorkGroupMaxIDY;
2593 RC = &AMDGPU::SReg_32RegClass;
2594 Ty = LLT::scalar(32);
2595 break;
2597 if (HasFixedDims)
2598 return LoadConstant(ClusterDims.getDims()[2] - 1);
2599 Reg = &ClusterWorkGroupMaxIDZ;
2600 RC = &AMDGPU::SReg_32RegClass;
2601 Ty = LLT::scalar(32);
2602 break;
2604 Reg = &ClusterWorkGroupMaxFlatID;
2605 RC = &AMDGPU::SReg_32RegClass;
2606 Ty = LLT::scalar(32);
2607 break;
2608 default:
2609 break;
2610 }
2611 }
2612
2613 if (!Reg)
2614 std::tie(Reg, RC, Ty) = MFI.getPreloadedValue(PVID);
2615 if (!Reg) {
2617 // It's possible for a kernarg intrinsic call to appear in a kernel with
2618 // no allocated segment, in which case we do not add the user sgpr
2619 // argument, so just return null.
2620 return DAG.getConstant(0, SDLoc(), VT);
2621 }
2622
2623 // It's undefined behavior if a function marked with the amdgpu-no-*
2624 // attributes uses the corresponding intrinsic.
2625 return DAG.getPOISON(VT);
2626 }
2627
2628 return loadInputValue(DAG, RC, VT, SDLoc(DAG.getEntryNode()), *Reg);
2629}
2630
2632 CallingConv::ID CallConv,
2633 ArrayRef<ISD::InputArg> Ins, BitVector &Skipped,
2634 FunctionType *FType,
2636 for (unsigned I = 0, E = Ins.size(), PSInputNum = 0; I != E; ++I) {
2637 const ISD::InputArg *Arg = &Ins[I];
2638
2639 assert((!Arg->VT.isVector() || Arg->VT.getScalarSizeInBits() == 16) &&
2640 "vector type argument should have been split");
2641
2642 // First check if it's a PS input addr.
2643 if (CallConv == CallingConv::AMDGPU_PS && !Arg->Flags.isInReg() &&
2644 PSInputNum <= 15) {
2645 bool SkipArg = !Arg->Used && !Info->isPSInputAllocated(PSInputNum);
2646
2647 // Inconveniently only the first part of the split is marked as isSplit,
2648 // so skip to the end. We only want to increment PSInputNum once for the
2649 // entire split argument.
2650 if (Arg->Flags.isSplit()) {
2651 while (!Arg->Flags.isSplitEnd()) {
2652 assert((!Arg->VT.isVector() || Arg->VT.getScalarSizeInBits() == 16) &&
2653 "unexpected vector split in ps argument type");
2654 if (!SkipArg)
2655 Splits.push_back(*Arg);
2656 Arg = &Ins[++I];
2657 }
2658 }
2659
2660 if (SkipArg) {
2661 // We can safely skip PS inputs.
2662 Skipped.set(Arg->getOrigArgIndex());
2663 ++PSInputNum;
2664 continue;
2665 }
2666
2667 Info->markPSInputAllocated(PSInputNum);
2668 if (Arg->Used)
2669 Info->markPSInputEnabled(PSInputNum);
2670
2671 ++PSInputNum;
2672 }
2673
2674 Splits.push_back(*Arg);
2675 }
2676}
2677
2678// Allocate special inputs passed in VGPRs.
2680 CCState &CCInfo, MachineFunction &MF, const SIRegisterInfo &TRI,
2681 SIMachineFunctionInfo &Info) const {
2682 const LLT S32 = LLT::scalar(32);
2684
2685 if (Info.hasWorkItemIDX()) {
2686 Register Reg = AMDGPU::VGPR0;
2687 MRI.setType(MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass), S32);
2688
2689 CCInfo.AllocateReg(Reg);
2690 unsigned Mask =
2691 (Subtarget->hasPackedTID() && Info.hasWorkItemIDY()) ? 0x3ff : ~0u;
2692 Info.setWorkItemIDX(ArgDescriptor::createRegister(Reg, Mask));
2693 }
2694
2695 if (Info.hasWorkItemIDY()) {
2696 assert(Info.hasWorkItemIDX());
2697 if (Subtarget->hasPackedTID()) {
2698 Info.setWorkItemIDY(
2699 ArgDescriptor::createRegister(AMDGPU::VGPR0, 0x3ff << 10));
2700 } else {
2701 unsigned Reg = AMDGPU::VGPR1;
2702 MRI.setType(MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass), S32);
2703
2704 CCInfo.AllocateReg(Reg);
2705 Info.setWorkItemIDY(ArgDescriptor::createRegister(Reg));
2706 }
2707 }
2708
2709 if (Info.hasWorkItemIDZ()) {
2710 assert(Info.hasWorkItemIDX() && Info.hasWorkItemIDY());
2711 if (Subtarget->hasPackedTID()) {
2712 Info.setWorkItemIDZ(
2713 ArgDescriptor::createRegister(AMDGPU::VGPR0, 0x3ff << 20));
2714 } else {
2715 unsigned Reg = AMDGPU::VGPR2;
2716 MRI.setType(MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass), S32);
2717
2718 CCInfo.AllocateReg(Reg);
2719 Info.setWorkItemIDZ(ArgDescriptor::createRegister(Reg));
2720 }
2721 }
2722}
2723
2724// Try to allocate a VGPR at the end of the argument list, or if no argument
2725// VGPRs are left allocating a stack slot.
2726// If \p Mask is is given it indicates bitfield position in the register.
2727// If \p Arg is given use it with new ]p Mask instead of allocating new.
2728static ArgDescriptor allocateVGPR32Input(CCState &CCInfo, unsigned Mask = ~0u,
2729 ArgDescriptor Arg = ArgDescriptor()) {
2730 if (Arg.isSet())
2731 return ArgDescriptor::createArg(Arg, Mask);
2732
2733 ArrayRef<MCPhysReg> ArgVGPRs = ArrayRef(AMDGPU::VGPR_32RegClass.begin(), 32);
2734 unsigned RegIdx = CCInfo.getFirstUnallocated(ArgVGPRs);
2735 if (RegIdx == ArgVGPRs.size()) {
2736 // Spill to stack required.
2737 int64_t Offset = CCInfo.AllocateStack(4, Align(4));
2738
2739 return ArgDescriptor::createStack(Offset, Mask);
2740 }
2741
2742 unsigned Reg = ArgVGPRs[RegIdx];
2743 Reg = CCInfo.AllocateReg(Reg);
2744 assert(Reg != AMDGPU::NoRegister);
2745
2746 MachineFunction &MF = CCInfo.getMachineFunction();
2747 Register LiveInVReg = MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
2748 MF.getRegInfo().setType(LiveInVReg, LLT::scalar(32));
2749 return ArgDescriptor::createRegister(Reg, Mask);
2750}
2751
2753 const TargetRegisterClass *RC,
2754 unsigned NumArgRegs) {
2755 ArrayRef<MCPhysReg> ArgSGPRs = ArrayRef(RC->begin(), 32);
2756 unsigned RegIdx = CCInfo.getFirstUnallocated(ArgSGPRs);
2757 if (RegIdx == ArgSGPRs.size())
2758 report_fatal_error("ran out of SGPRs for arguments");
2759
2760 unsigned Reg = ArgSGPRs[RegIdx];
2761 Reg = CCInfo.AllocateReg(Reg);
2762 assert(Reg != AMDGPU::NoRegister);
2763
2764 MachineFunction &MF = CCInfo.getMachineFunction();
2765 MF.addLiveIn(Reg, RC);
2767}
2768
2769// If this has a fixed position, we still should allocate the register in the
2770// CCInfo state. Technically we could get away with this for values passed
2771// outside of the normal argument range.
2773 const TargetRegisterClass *RC,
2774 MCRegister Reg) {
2775 Reg = CCInfo.AllocateReg(Reg);
2776 assert(Reg != AMDGPU::NoRegister);
2777 MachineFunction &MF = CCInfo.getMachineFunction();
2778 MF.addLiveIn(Reg, RC);
2779}
2780
2781static void allocateSGPR32Input(CCState &CCInfo, ArgDescriptor &Arg) {
2782 if (Arg) {
2783 allocateFixedSGPRInputImpl(CCInfo, &AMDGPU::SGPR_32RegClass,
2784 Arg.getRegister());
2785 } else
2786 Arg = allocateSGPR32InputImpl(CCInfo, &AMDGPU::SGPR_32RegClass, 32);
2787}
2788
2789static void allocateSGPR64Input(CCState &CCInfo, ArgDescriptor &Arg) {
2790 if (Arg) {
2791 allocateFixedSGPRInputImpl(CCInfo, &AMDGPU::SGPR_64RegClass,
2792 Arg.getRegister());
2793 } else
2794 Arg = allocateSGPR32InputImpl(CCInfo, &AMDGPU::SGPR_64RegClass, 16);
2795}
2796
2797/// Allocate implicit function VGPR arguments at the end of allocated user
2798/// arguments.
2800 CCState &CCInfo, MachineFunction &MF, const SIRegisterInfo &TRI,
2801 SIMachineFunctionInfo &Info) const {
2802 const unsigned Mask = 0x3ff;
2803 ArgDescriptor Arg;
2804
2805 if (Info.hasWorkItemIDX()) {
2806 Arg = allocateVGPR32Input(CCInfo, Mask);
2807 Info.setWorkItemIDX(Arg);
2808 }
2809
2810 if (Info.hasWorkItemIDY()) {
2811 Arg = allocateVGPR32Input(CCInfo, Mask << 10, Arg);
2812 Info.setWorkItemIDY(Arg);
2813 }
2814
2815 if (Info.hasWorkItemIDZ())
2816 Info.setWorkItemIDZ(allocateVGPR32Input(CCInfo, Mask << 20, Arg));
2817}
2818
2819/// Allocate implicit function VGPR arguments in fixed registers.
2821 CCState &CCInfo, MachineFunction &MF, const SIRegisterInfo &TRI,
2822 SIMachineFunctionInfo &Info) const {
2823 Register Reg = CCInfo.AllocateReg(AMDGPU::VGPR31);
2824 if (!Reg)
2825 report_fatal_error("failed to allocate VGPR for implicit arguments");
2826
2827 const unsigned Mask = 0x3ff;
2828 Info.setWorkItemIDX(ArgDescriptor::createRegister(Reg, Mask));
2829 Info.setWorkItemIDY(ArgDescriptor::createRegister(Reg, Mask << 10));
2830 Info.setWorkItemIDZ(ArgDescriptor::createRegister(Reg, Mask << 20));
2831}
2832
2834 CCState &CCInfo, MachineFunction &MF, const SIRegisterInfo &TRI,
2835 SIMachineFunctionInfo &Info) const {
2836 auto &ArgInfo = Info.getArgInfo();
2837 const GCNUserSGPRUsageInfo &UserSGPRInfo = Info.getUserSGPRInfo();
2838
2839 // TODO: Unify handling with private memory pointers.
2840 if (UserSGPRInfo.hasDispatchPtr())
2841 allocateSGPR64Input(CCInfo, ArgInfo.DispatchPtr);
2842
2843 if (UserSGPRInfo.hasQueuePtr())
2844 allocateSGPR64Input(CCInfo, ArgInfo.QueuePtr);
2845
2846 // Implicit arg ptr takes the place of the kernarg segment pointer. This is a
2847 // constant offset from the kernarg segment.
2848 if (Info.hasImplicitArgPtr())
2849 allocateSGPR64Input(CCInfo, ArgInfo.ImplicitArgPtr);
2850
2851 if (UserSGPRInfo.hasDispatchID())
2852 allocateSGPR64Input(CCInfo, ArgInfo.DispatchID);
2853
2854 // flat_scratch_init is not applicable for non-kernel functions.
2855
2856 if (Info.hasWorkGroupIDX())
2857 allocateSGPR32Input(CCInfo, ArgInfo.WorkGroupIDX);
2858
2859 if (Info.hasWorkGroupIDY())
2860 allocateSGPR32Input(CCInfo, ArgInfo.WorkGroupIDY);
2861
2862 if (Info.hasWorkGroupIDZ())
2863 allocateSGPR32Input(CCInfo, ArgInfo.WorkGroupIDZ);
2864
2865 if (Info.hasLDSKernelId())
2866 allocateSGPR32Input(CCInfo, ArgInfo.LDSKernelId);
2867}
2868
2869// Allocate special inputs passed in user SGPRs.
2871 MachineFunction &MF,
2872 const SIRegisterInfo &TRI,
2873 SIMachineFunctionInfo &Info) const {
2874 const GCNUserSGPRUsageInfo &UserSGPRInfo = Info.getUserSGPRInfo();
2875 if (UserSGPRInfo.hasImplicitBufferPtr()) {
2876 Register ImplicitBufferPtrReg = Info.addImplicitBufferPtr(TRI);
2877 MF.addLiveIn(ImplicitBufferPtrReg, &AMDGPU::SGPR_64RegClass);
2878 CCInfo.AllocateReg(ImplicitBufferPtrReg);
2879 }
2880
2881 // FIXME: How should these inputs interact with inreg / custom SGPR inputs?
2882 if (UserSGPRInfo.hasPrivateSegmentBuffer()) {
2883 Register PrivateSegmentBufferReg = Info.addPrivateSegmentBuffer(TRI);
2884 MF.addLiveIn(PrivateSegmentBufferReg, &AMDGPU::SGPR_128RegClass);
2885 CCInfo.AllocateReg(PrivateSegmentBufferReg);
2886 }
2887
2888 if (UserSGPRInfo.hasDispatchPtr()) {
2889 Register DispatchPtrReg = Info.addDispatchPtr(TRI);
2890 MF.addLiveIn(DispatchPtrReg, &AMDGPU::SGPR_64RegClass);
2891 CCInfo.AllocateReg(DispatchPtrReg);
2892 }
2893
2894 if (UserSGPRInfo.hasQueuePtr()) {
2895 Register QueuePtrReg = Info.addQueuePtr(TRI);
2896 MF.addLiveIn(QueuePtrReg, &AMDGPU::SGPR_64RegClass);
2897 CCInfo.AllocateReg(QueuePtrReg);
2898 }
2899
2900 if (UserSGPRInfo.hasKernargSegmentPtr()) {
2902 Register InputPtrReg = Info.addKernargSegmentPtr(TRI);
2903 CCInfo.AllocateReg(InputPtrReg);
2904
2905 Register VReg = MF.addLiveIn(InputPtrReg, &AMDGPU::SGPR_64RegClass);
2906 MRI.setType(VReg, LLT::pointer(AMDGPUAS::CONSTANT_ADDRESS, 64));
2907 }
2908
2909 if (UserSGPRInfo.hasDispatchID()) {
2910 Register DispatchIDReg = Info.addDispatchID(TRI);
2911 MF.addLiveIn(DispatchIDReg, &AMDGPU::SGPR_64RegClass);
2912 CCInfo.AllocateReg(DispatchIDReg);
2913 }
2914
2915 if (UserSGPRInfo.hasFlatScratchInit() && !getSubtarget()->isAmdPalOS()) {
2916 Register FlatScratchInitReg = Info.addFlatScratchInit(TRI);
2917 MF.addLiveIn(FlatScratchInitReg, &AMDGPU::SGPR_64RegClass);
2918 CCInfo.AllocateReg(FlatScratchInitReg);
2919 }
2920
2921 if (UserSGPRInfo.hasPrivateSegmentSize()) {
2922 Register PrivateSegmentSizeReg = Info.addPrivateSegmentSize(TRI);
2923 MF.addLiveIn(PrivateSegmentSizeReg, &AMDGPU::SGPR_32RegClass);
2924 CCInfo.AllocateReg(PrivateSegmentSizeReg);
2925 }
2926
2927 // TODO: Add GridWorkGroupCount user SGPRs when used. For now with HSA we read
2928 // these from the dispatch pointer.
2929}
2930
2931// Allocate pre-loaded kernel arguemtns. Arguments to be preloading must be
2932// sequential starting from the first argument.
2934 CCState &CCInfo, SmallVectorImpl<CCValAssign> &ArgLocs,
2936 const SIRegisterInfo &TRI, SIMachineFunctionInfo &Info) const {
2937 Function &F = MF.getFunction();
2938 unsigned LastExplicitArgOffset = Subtarget->getExplicitKernelArgOffset();
2939 GCNUserSGPRUsageInfo &SGPRInfo = Info.getUserSGPRInfo();
2940 bool InPreloadSequence = true;
2941 unsigned InIdx = 0;
2942 bool AlignedForImplictArgs = false;
2943 unsigned ImplicitArgOffset = 0;
2944 for (auto &Arg : F.args()) {
2945 if (!InPreloadSequence || !Arg.hasInRegAttr())
2946 break;
2947
2948 unsigned ArgIdx = Arg.getArgNo();
2949 // Don't preload non-original args or parts not in the current preload
2950 // sequence.
2951 if (InIdx < Ins.size() &&
2952 (!Ins[InIdx].isOrigArg() || Ins[InIdx].getOrigArgIndex() != ArgIdx))
2953 break;
2954
2955 for (; InIdx < Ins.size() && Ins[InIdx].isOrigArg() &&
2956 Ins[InIdx].getOrigArgIndex() == ArgIdx;
2957 InIdx++) {
2958 assert(ArgLocs[ArgIdx].isMemLoc());
2959 auto &ArgLoc = ArgLocs[InIdx];
2960 const Align KernelArgBaseAlign = Align(16);
2961 unsigned ArgOffset = ArgLoc.getLocMemOffset();
2962 Align Alignment = commonAlignment(KernelArgBaseAlign, ArgOffset);
2963 unsigned NumAllocSGPRs =
2964 alignTo(ArgLoc.getLocVT().getFixedSizeInBits(), 32) / 32;
2965
2966 // Fix alignment for hidden arguments.
2967 if (Arg.hasAttribute("amdgpu-hidden-argument")) {
2968 if (!AlignedForImplictArgs) {
2969 ImplicitArgOffset =
2970 alignTo(LastExplicitArgOffset,
2971 Subtarget->getAlignmentForImplicitArgPtr()) -
2972 LastExplicitArgOffset;
2973 AlignedForImplictArgs = true;
2974 }
2975 ArgOffset += ImplicitArgOffset;
2976 }
2977
2978 // Arg is preloaded into the previous SGPR.
2979 if (ArgLoc.getLocVT().getStoreSize() < 4 && Alignment < 4) {
2980 assert(InIdx >= 1 && "No previous SGPR");
2981 Info.getArgInfo().PreloadKernArgs[InIdx].Regs.push_back(
2982 Info.getArgInfo().PreloadKernArgs[InIdx - 1].Regs[0]);
2983 continue;
2984 }
2985
2986 unsigned Padding = ArgOffset - LastExplicitArgOffset;
2987 unsigned PaddingSGPRs = alignTo(Padding, 4) / 4;
2988 // Check for free user SGPRs for preloading.
2989 if (PaddingSGPRs + NumAllocSGPRs > SGPRInfo.getNumFreeUserSGPRs()) {
2990 InPreloadSequence = false;
2991 break;
2992 }
2993
2994 // Preload this argument.
2995 const TargetRegisterClass *RC =
2996 TRI.getSGPRClassForBitWidth(NumAllocSGPRs * 32);
2997 SmallVectorImpl<MCRegister> *PreloadRegs =
2998 Info.addPreloadedKernArg(TRI, RC, NumAllocSGPRs, InIdx, PaddingSGPRs);
2999
3000 if (PreloadRegs->size() > 1)
3001 RC = &AMDGPU::SGPR_32RegClass;
3002 for (auto &Reg : *PreloadRegs) {
3003 assert(Reg);
3004 MF.addLiveIn(Reg, RC);
3005 CCInfo.AllocateReg(Reg);
3006 }
3007
3008 LastExplicitArgOffset = NumAllocSGPRs * 4 + ArgOffset;
3009 }
3010 }
3011}
3012
3014 const SIRegisterInfo &TRI,
3015 SIMachineFunctionInfo &Info) const {
3016 // Always allocate this last since it is a synthetic preload.
3017 if (Info.hasLDSKernelId()) {
3018 Register Reg = Info.addLDSKernelId();
3019 MF.addLiveIn(Reg, &AMDGPU::SGPR_32RegClass);
3020 CCInfo.AllocateReg(Reg);
3021 }
3022}
3023
3024// Allocate special input registers that are initialized per-wave.
3027 CallingConv::ID CallConv,
3028 bool IsShader) const {
3029 bool HasArchitectedSGPRs = Subtarget->hasArchitectedSGPRs();
3030 if (Subtarget->hasUserSGPRInit16Bug() && !IsShader) {
3031 // Note: user SGPRs are handled by the front-end for graphics shaders
3032 // Pad up the used user SGPRs with dead inputs.
3033
3034 // TODO: NumRequiredSystemSGPRs computation should be adjusted appropriately
3035 // before enabling architected SGPRs for workgroup IDs.
3036 assert(!HasArchitectedSGPRs && "Unhandled feature for the subtarget");
3037
3038 unsigned CurrentUserSGPRs = Info.getNumUserSGPRs();
3039 // Note we do not count the PrivateSegmentWaveByteOffset. We do not want to
3040 // rely on it to reach 16 since if we end up having no stack usage, it will
3041 // not really be added.
3042 unsigned NumRequiredSystemSGPRs =
3043 Info.hasWorkGroupIDX() + Info.hasWorkGroupIDY() +
3044 Info.hasWorkGroupIDZ() + Info.hasWorkGroupInfo();
3045 for (unsigned i = NumRequiredSystemSGPRs + CurrentUserSGPRs; i < 16; ++i) {
3046 Register Reg = Info.addReservedUserSGPR();
3047 MF.addLiveIn(Reg, &AMDGPU::SGPR_32RegClass);
3048 CCInfo.AllocateReg(Reg);
3049 }
3050 }
3051
3052 if (!HasArchitectedSGPRs) {
3053 if (Info.hasWorkGroupIDX()) {
3054 Register Reg = Info.addWorkGroupIDX();
3055 MF.addLiveIn(Reg, &AMDGPU::SGPR_32RegClass);
3056 CCInfo.AllocateReg(Reg);
3057 }
3058
3059 if (Info.hasWorkGroupIDY()) {
3060 Register Reg = Info.addWorkGroupIDY();
3061 MF.addLiveIn(Reg, &AMDGPU::SGPR_32RegClass);
3062 CCInfo.AllocateReg(Reg);
3063 }
3064
3065 if (Info.hasWorkGroupIDZ()) {
3066 Register Reg = Info.addWorkGroupIDZ();
3067 MF.addLiveIn(Reg, &AMDGPU::SGPR_32RegClass);
3068 CCInfo.AllocateReg(Reg);
3069 }
3070 }
3071
3072 if (Info.hasWorkGroupInfo()) {
3073 Register Reg = Info.addWorkGroupInfo();
3074 MF.addLiveIn(Reg, &AMDGPU::SGPR_32RegClass);
3075 CCInfo.AllocateReg(Reg);
3076 }
3077
3078 if (Info.hasPrivateSegmentWaveByteOffset()) {
3079 // Scratch wave offset passed in system SGPR.
3080 unsigned PrivateSegmentWaveByteOffsetReg;
3081
3082 if (IsShader) {
3083 PrivateSegmentWaveByteOffsetReg =
3084 Info.getPrivateSegmentWaveByteOffsetSystemSGPR();
3085
3086 // This is true if the scratch wave byte offset doesn't have a fixed
3087 // location.
3088 if (PrivateSegmentWaveByteOffsetReg == AMDGPU::NoRegister) {
3089 PrivateSegmentWaveByteOffsetReg = findFirstFreeSGPR(CCInfo);
3090 Info.setPrivateSegmentWaveByteOffset(PrivateSegmentWaveByteOffsetReg);
3091 }
3092 } else
3093 PrivateSegmentWaveByteOffsetReg = Info.addPrivateSegmentWaveByteOffset();
3094
3095 MF.addLiveIn(PrivateSegmentWaveByteOffsetReg, &AMDGPU::SGPR_32RegClass);
3096 CCInfo.AllocateReg(PrivateSegmentWaveByteOffsetReg);
3097 }
3098
3099 assert(!Subtarget->hasUserSGPRInit16Bug() || IsShader ||
3100 Info.getNumPreloadedSGPRs() >= 16);
3101}
3102
3104 MachineFunction &MF,
3105 const SIRegisterInfo &TRI,
3107 // Now that we've figured out where the scratch register inputs are, see if
3108 // should reserve the arguments and use them directly.
3109 MachineFrameInfo &MFI = MF.getFrameInfo();
3110 bool HasStackObjects = MFI.hasStackObjects();
3111 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
3112
3113 // Record that we know we have non-spill stack objects so we don't need to
3114 // check all stack objects later.
3115 if (HasStackObjects)
3116 Info.setHasNonSpillStackObjects(true);
3117
3118 // Everything live out of a block is spilled with fast regalloc, so it's
3119 // almost certain that spilling will be required.
3121 HasStackObjects = true;
3122
3123 // For now assume stack access is needed in any callee functions, so we need
3124 // the scratch registers to pass in.
3125 bool RequiresStackAccess = HasStackObjects || MFI.hasCalls();
3126
3127 if (!ST.enableFlatScratch()) {
3128 if (RequiresStackAccess && ST.isAmdHsaOrMesa(MF.getFunction())) {
3129 // If we have stack objects, we unquestionably need the private buffer
3130 // resource. For the Code Object V2 ABI, this will be the first 4 user
3131 // SGPR inputs. We can reserve those and use them directly.
3132
3133 Register PrivateSegmentBufferReg =
3135 Info.setScratchRSrcReg(PrivateSegmentBufferReg);
3136 } else {
3137 unsigned ReservedBufferReg = TRI.reservedPrivateSegmentBufferReg(MF);
3138 // We tentatively reserve the last registers (skipping the last registers
3139 // which may contain VCC, FLAT_SCR, and XNACK). After register allocation,
3140 // we'll replace these with the ones immediately after those which were
3141 // really allocated. In the prologue copies will be inserted from the
3142 // argument to these reserved registers.
3143
3144 // Without HSA, relocations are used for the scratch pointer and the
3145 // buffer resource setup is always inserted in the prologue. Scratch wave
3146 // offset is still in an input SGPR.
3147 Info.setScratchRSrcReg(ReservedBufferReg);
3148 }
3149 }
3150
3152
3153 // For entry functions we have to set up the stack pointer if we use it,
3154 // whereas non-entry functions get this "for free". This means there is no
3155 // intrinsic advantage to using S32 over S34 in cases where we do not have
3156 // calls but do need a frame pointer (i.e. if we are requested to have one
3157 // because frame pointer elimination is disabled). To keep things simple we
3158 // only ever use S32 as the call ABI stack pointer, and so using it does not
3159 // imply we need a separate frame pointer.
3160 //
3161 // Try to use s32 as the SP, but move it if it would interfere with input
3162 // arguments. This won't work with calls though.
3163 //
3164 // FIXME: Move SP to avoid any possible inputs, or find a way to spill input
3165 // registers.
3166 if (!MRI.isLiveIn(AMDGPU::SGPR32)) {
3167 Info.setStackPtrOffsetReg(AMDGPU::SGPR32);
3168 } else {
3170
3171 if (MFI.hasCalls())
3172 report_fatal_error("call in graphics shader with too many input SGPRs");
3173
3174 for (unsigned Reg : AMDGPU::SGPR_32RegClass) {
3175 if (!MRI.isLiveIn(Reg)) {
3176 Info.setStackPtrOffsetReg(Reg);
3177 break;
3178 }
3179 }
3180
3181 if (Info.getStackPtrOffsetReg() == AMDGPU::SP_REG)
3182 report_fatal_error("failed to find register for SP");
3183 }
3184
3185 // hasFP should be accurate for entry functions even before the frame is
3186 // finalized, because it does not rely on the known stack size, only
3187 // properties like whether variable sized objects are present.
3188 if (ST.getFrameLowering()->hasFP(MF)) {
3189 Info.setFrameOffsetReg(AMDGPU::SGPR33);
3190 }
3191}
3192
3195 return !Info->isEntryFunction();
3196}
3197
3199
3201 MachineBasicBlock *Entry,
3202 const SmallVectorImpl<MachineBasicBlock *> &Exits) const {
3204
3205 const MCPhysReg *IStart = TRI->getCalleeSavedRegsViaCopy(Entry->getParent());
3206 if (!IStart)
3207 return;
3208
3209 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
3210 MachineRegisterInfo *MRI = &Entry->getParent()->getRegInfo();
3211 MachineBasicBlock::iterator MBBI = Entry->begin();
3212 for (const MCPhysReg *I = IStart; *I; ++I) {
3213 const TargetRegisterClass *RC = nullptr;
3214 if (AMDGPU::SReg_64RegClass.contains(*I))
3215 RC = &AMDGPU::SGPR_64RegClass;
3216 else if (AMDGPU::SReg_32RegClass.contains(*I))
3217 RC = &AMDGPU::SGPR_32RegClass;
3218 else
3219 llvm_unreachable("Unexpected register class in CSRsViaCopy!");
3220
3221 Register NewVR = MRI->createVirtualRegister(RC);
3222 // Create copy from CSR to a virtual register.
3223 Entry->addLiveIn(*I);
3224 BuildMI(*Entry, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY), NewVR)
3225 .addReg(*I);
3226
3227 // Insert the copy-back instructions right before the terminator.
3228 for (auto *Exit : Exits)
3229 BuildMI(*Exit, Exit->getFirstTerminator(), DebugLoc(),
3230 TII->get(TargetOpcode::COPY), *I)
3231 .addReg(NewVR);
3232 }
3233}
3234
3236 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
3237 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
3238 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
3240
3242 const Function &Fn = MF.getFunction();
3245 bool IsError = false;
3246
3247 if (Subtarget->isAmdHsaOS() && AMDGPU::isGraphics(CallConv)) {
3249 Fn, "unsupported non-compute shaders with HSA", DL.getDebugLoc()));
3250 IsError = true;
3251 }
3252
3255 BitVector Skipped(Ins.size());
3256 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
3257 *DAG.getContext());
3258
3259 bool IsGraphics = AMDGPU::isGraphics(CallConv);
3260 bool IsKernel = AMDGPU::isKernel(CallConv);
3261 bool IsEntryFunc = AMDGPU::isEntryFunctionCC(CallConv);
3262
3263 if (IsGraphics) {
3264 const GCNUserSGPRUsageInfo &UserSGPRInfo = Info->getUserSGPRInfo();
3265 assert(!UserSGPRInfo.hasDispatchPtr() &&
3266 !UserSGPRInfo.hasKernargSegmentPtr() && !Info->hasWorkGroupInfo() &&
3267 !Info->hasLDSKernelId() && !Info->hasWorkItemIDX() &&
3268 !Info->hasWorkItemIDY() && !Info->hasWorkItemIDZ());
3269 (void)UserSGPRInfo;
3270 if (!Subtarget->enableFlatScratch())
3271 assert(!UserSGPRInfo.hasFlatScratchInit());
3272 if ((CallConv != CallingConv::AMDGPU_CS &&
3273 CallConv != CallingConv::AMDGPU_Gfx &&
3274 CallConv != CallingConv::AMDGPU_Gfx_WholeWave) ||
3275 !Subtarget->hasArchitectedSGPRs())
3276 assert(!Info->hasWorkGroupIDX() && !Info->hasWorkGroupIDY() &&
3277 !Info->hasWorkGroupIDZ());
3278 }
3279
3280 bool IsWholeWaveFunc = Info->isWholeWaveFunction();
3281
3282 if (CallConv == CallingConv::AMDGPU_PS) {
3283 processPSInputArgs(Splits, CallConv, Ins, Skipped, FType, Info);
3284
3285 // At least one interpolation mode must be enabled or else the GPU will
3286 // hang.
3287 //
3288 // Check PSInputAddr instead of PSInputEnable. The idea is that if the user
3289 // set PSInputAddr, the user wants to enable some bits after the compilation
3290 // based on run-time states. Since we can't know what the final PSInputEna
3291 // will look like, so we shouldn't do anything here and the user should take
3292 // responsibility for the correct programming.
3293 //
3294 // Otherwise, the following restrictions apply:
3295 // - At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled.
3296 // - If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be
3297 // enabled too.
3298 if ((Info->getPSInputAddr() & 0x7F) == 0 ||
3299 ((Info->getPSInputAddr() & 0xF) == 0 && Info->isPSInputAllocated(11))) {
3300 CCInfo.AllocateReg(AMDGPU::VGPR0);
3301 CCInfo.AllocateReg(AMDGPU::VGPR1);
3302 Info->markPSInputAllocated(0);
3303 Info->markPSInputEnabled(0);
3304 }
3305 if (Subtarget->isAmdPalOS()) {
3306 // For isAmdPalOS, the user does not enable some bits after compilation
3307 // based on run-time states; the register values being generated here are
3308 // the final ones set in hardware. Therefore we need to apply the
3309 // workaround to PSInputAddr and PSInputEnable together. (The case where
3310 // a bit is set in PSInputAddr but not PSInputEnable is where the
3311 // frontend set up an input arg for a particular interpolation mode, but
3312 // nothing uses that input arg. Really we should have an earlier pass
3313 // that removes such an arg.)
3314 unsigned PsInputBits = Info->getPSInputAddr() & Info->getPSInputEnable();
3315 if ((PsInputBits & 0x7F) == 0 ||
3316 ((PsInputBits & 0xF) == 0 && (PsInputBits >> 11 & 1)))
3317 Info->markPSInputEnabled(llvm::countr_zero(Info->getPSInputAddr()));
3318 }
3319 } else if (IsKernel) {
3320 assert(Info->hasWorkGroupIDX() && Info->hasWorkItemIDX());
3321 } else {
3322 Splits.append(IsWholeWaveFunc ? std::next(Ins.begin()) : Ins.begin(),
3323 Ins.end());
3324 }
3325
3326 if (IsKernel)
3327 analyzeFormalArgumentsCompute(CCInfo, Ins);
3328
3329 if (IsEntryFunc) {
3330 allocateSpecialEntryInputVGPRs(CCInfo, MF, *TRI, *Info);
3331 allocateHSAUserSGPRs(CCInfo, MF, *TRI, *Info);
3332 if (IsKernel && Subtarget->hasKernargPreload())
3333 allocatePreloadKernArgSGPRs(CCInfo, ArgLocs, Ins, MF, *TRI, *Info);
3334
3335 allocateLDSKernelId(CCInfo, MF, *TRI, *Info);
3336 } else if (!IsGraphics) {
3337 // For the fixed ABI, pass workitem IDs in the last argument register.
3338 allocateSpecialInputVGPRsFixed(CCInfo, MF, *TRI, *Info);
3339
3340 // FIXME: Sink this into allocateSpecialInputSGPRs
3341 if (!Subtarget->enableFlatScratch())
3342 CCInfo.AllocateReg(Info->getScratchRSrcReg());
3343
3344 allocateSpecialInputSGPRs(CCInfo, MF, *TRI, *Info);
3345 }
3346
3347 if (!IsKernel) {
3348 CCAssignFn *AssignFn = CCAssignFnForCall(CallConv, isVarArg);
3349 CCInfo.AnalyzeFormalArguments(Splits, AssignFn);
3350
3351 // This assumes the registers are allocated by CCInfo in ascending order
3352 // with no gaps.
3353 Info->setNumWaveDispatchSGPRs(
3354 CCInfo.getFirstUnallocated(AMDGPU::SGPR_32RegClass.getRegisters()));
3355 Info->setNumWaveDispatchVGPRs(
3356 CCInfo.getFirstUnallocated(AMDGPU::VGPR_32RegClass.getRegisters()));
3357 } else if (Info->getNumKernargPreloadedSGPRs()) {
3358 Info->setNumWaveDispatchSGPRs(Info->getNumUserSGPRs());
3359 }
3360
3362
3363 if (IsWholeWaveFunc) {
3364 SDValue Setup = DAG.getNode(AMDGPUISD::WHOLE_WAVE_SETUP, DL,
3365 {MVT::i1, MVT::Other}, Chain);
3366 InVals.push_back(Setup.getValue(0));
3367 Chains.push_back(Setup.getValue(1));
3368 }
3369
3370 // FIXME: This is the minimum kernel argument alignment. We should improve
3371 // this to the maximum alignment of the arguments.
3372 //
3373 // FIXME: Alignment of explicit arguments totally broken with non-0 explicit
3374 // kern arg offset.
3375 const Align KernelArgBaseAlign = Align(16);
3376
3377 for (unsigned i = IsWholeWaveFunc ? 1 : 0, e = Ins.size(), ArgIdx = 0; i != e;
3378 ++i) {
3379 const ISD::InputArg &Arg = Ins[i];
3380 if ((Arg.isOrigArg() && Skipped[Arg.getOrigArgIndex()]) || IsError) {
3381 InVals.push_back(DAG.getPOISON(Arg.VT));
3382 continue;
3383 }
3384
3385 CCValAssign &VA = ArgLocs[ArgIdx++];
3386 MVT VT = VA.getLocVT();
3387
3388 if (IsEntryFunc && VA.isMemLoc()) {
3389 VT = Ins[i].VT;
3390 EVT MemVT = VA.getLocVT();
3391
3392 const uint64_t Offset = VA.getLocMemOffset();
3393 Align Alignment = commonAlignment(KernelArgBaseAlign, Offset);
3394
3395 if (Arg.Flags.isByRef()) {
3396 SDValue Ptr = lowerKernArgParameterPtr(DAG, DL, Chain, Offset);
3397
3398 const GCNTargetMachine &TM =
3399 static_cast<const GCNTargetMachine &>(getTargetMachine());
3400 if (!TM.isNoopAddrSpaceCast(AMDGPUAS::CONSTANT_ADDRESS,
3401 Arg.Flags.getPointerAddrSpace())) {
3404 }
3405
3406 InVals.push_back(Ptr);
3407 continue;
3408 }
3409
3410 SDValue NewArg;
3411 if (Arg.isOrigArg() && Info->getArgInfo().PreloadKernArgs.count(i)) {
3412 if (MemVT.getStoreSize() < 4 && Alignment < 4) {
3413 // In this case the argument is packed into the previous preload SGPR.
3414 int64_t AlignDownOffset = alignDown(Offset, 4);
3415 int64_t OffsetDiff = Offset - AlignDownOffset;
3416 EVT IntVT = MemVT.changeTypeToInteger();
3417
3418 const SIMachineFunctionInfo *Info =
3421 Register Reg =
3422 Info->getArgInfo().PreloadKernArgs.find(i)->getSecond().Regs[0];
3423
3424 assert(Reg);
3425 Register VReg = MRI.getLiveInVirtReg(Reg);
3426 SDValue Copy = DAG.getCopyFromReg(Chain, DL, VReg, MVT::i32);
3427
3428 SDValue ShiftAmt = DAG.getConstant(OffsetDiff * 8, DL, MVT::i32);
3429 SDValue Extract = DAG.getNode(ISD::SRL, DL, MVT::i32, Copy, ShiftAmt);
3430
3431 SDValue ArgVal = DAG.getNode(ISD::TRUNCATE, DL, IntVT, Extract);
3432 ArgVal = DAG.getNode(ISD::BITCAST, DL, MemVT, ArgVal);
3433 NewArg = convertArgType(DAG, VT, MemVT, DL, ArgVal,
3434 Ins[i].Flags.isSExt(), &Ins[i]);
3435
3436 NewArg = DAG.getMergeValues({NewArg, Copy.getValue(1)}, DL);
3437 } else {
3438 const SIMachineFunctionInfo *Info =
3441 const SmallVectorImpl<MCRegister> &PreloadRegs =
3442 Info->getArgInfo().PreloadKernArgs.find(i)->getSecond().Regs;
3443
3444 SDValue Copy;
3445 if (PreloadRegs.size() == 1) {
3446 Register VReg = MRI.getLiveInVirtReg(PreloadRegs[0]);
3447 const TargetRegisterClass *RC = MRI.getRegClass(VReg);
3448 NewArg = DAG.getCopyFromReg(
3449 Chain, DL, VReg,
3451 TRI->getRegSizeInBits(*RC)));
3452
3453 } else {
3454 // If the kernarg alignment does not match the alignment of the SGPR
3455 // tuple RC that can accommodate this argument, it will be built up
3456 // via copies from from the individual SGPRs that the argument was
3457 // preloaded to.
3459 for (auto Reg : PreloadRegs) {
3460 Register VReg = MRI.getLiveInVirtReg(Reg);
3461 Copy = DAG.getCopyFromReg(Chain, DL, VReg, MVT::i32);
3462 Elts.push_back(Copy);
3463 }
3464 NewArg =
3465 DAG.getBuildVector(EVT::getVectorVT(*DAG.getContext(), MVT::i32,
3466 PreloadRegs.size()),
3467 DL, Elts);
3468 }
3469
3470 // If the argument was preloaded to multiple consecutive 32-bit
3471 // registers because of misalignment between addressable SGPR tuples
3472 // and the argument size, we can still assume that because of kernarg
3473 // segment alignment restrictions that NewArg's size is the same as
3474 // MemVT and just do a bitcast. If MemVT is less than 32-bits we add a
3475 // truncate since we cannot preload to less than a single SGPR and the
3476 // MemVT may be smaller.
3477 EVT MemVTInt =
3479 if (MemVT.bitsLT(NewArg.getSimpleValueType()))
3480 NewArg = DAG.getNode(ISD::TRUNCATE, DL, MemVTInt, NewArg);
3481
3482 NewArg = DAG.getBitcast(MemVT, NewArg);
3483 NewArg = convertArgType(DAG, VT, MemVT, DL, NewArg,
3484 Ins[i].Flags.isSExt(), &Ins[i]);
3485 NewArg = DAG.getMergeValues({NewArg, Chain}, DL);
3486 }
3487 } else {
3488 // Hidden arguments that are in the kernel signature must be preloaded
3489 // to user SGPRs. Print a diagnostic error if a hidden argument is in
3490 // the argument list and is not preloaded.
3491 if (Arg.isOrigArg()) {
3492 Argument *OrigArg = Fn.getArg(Arg.getOrigArgIndex());
3493 if (OrigArg->hasAttribute("amdgpu-hidden-argument")) {
3495 *OrigArg->getParent(),
3496 "hidden argument in kernel signature was not preloaded",
3497 DL.getDebugLoc()));
3498 }
3499 }
3500
3501 NewArg =
3502 lowerKernargMemParameter(DAG, VT, MemVT, DL, Chain, Offset,
3503 Alignment, Ins[i].Flags.isSExt(), &Ins[i]);
3504 }
3505 Chains.push_back(NewArg.getValue(1));
3506
3507 auto *ParamTy =
3508 dyn_cast<PointerType>(FType->getParamType(Ins[i].getOrigArgIndex()));
3509 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS &&
3510 ParamTy &&
3511 (ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
3512 ParamTy->getAddressSpace() == AMDGPUAS::REGION_ADDRESS)) {
3513 // On SI local pointers are just offsets into LDS, so they are always
3514 // less than 16-bits. On CI and newer they could potentially be
3515 // real pointers, so we can't guarantee their size.
3516 NewArg = DAG.getNode(ISD::AssertZext, DL, NewArg.getValueType(), NewArg,
3517 DAG.getValueType(MVT::i16));
3518 }
3519
3520 InVals.push_back(NewArg);
3521 continue;
3522 }
3523 if (!IsEntryFunc && VA.isMemLoc()) {
3524 SDValue Val = lowerStackParameter(DAG, VA, DL, Chain, Arg);
3525 InVals.push_back(Val);
3526 if (!Arg.Flags.isByVal())
3527 Chains.push_back(Val.getValue(1));
3528 continue;
3529 }
3530
3531 assert(VA.isRegLoc() && "Parameter must be in a register!");
3532
3533 Register Reg = VA.getLocReg();
3534 const TargetRegisterClass *RC = nullptr;
3535 if (AMDGPU::VGPR_32RegClass.contains(Reg))
3536 RC = &AMDGPU::VGPR_32RegClass;
3537 else if (AMDGPU::SGPR_32RegClass.contains(Reg))
3538 RC = &AMDGPU::SGPR_32RegClass;
3539 else
3540 llvm_unreachable("Unexpected register class in LowerFormalArguments!");
3541
3542 Reg = MF.addLiveIn(Reg, RC);
3543 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
3544
3545 if (Arg.Flags.isSRet()) {
3546 // The return object should be reasonably addressable.
3547
3548 // FIXME: This helps when the return is a real sret. If it is a
3549 // automatically inserted sret (i.e. CanLowerReturn returns false), an
3550 // extra copy is inserted in SelectionDAGBuilder which obscures this.
3551 unsigned NumBits =
3553 Val = DAG.getNode(
3554 ISD::AssertZext, DL, VT, Val,
3555 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(), NumBits)));
3556 }
3557
3558 Val = convertABITypeToValueType(DAG, Val, VA, DL);
3559 InVals.push_back(Val);
3560 }
3561
3562 // Start adding system SGPRs.
3563 if (IsEntryFunc)
3564 allocateSystemSGPRs(CCInfo, MF, *Info, CallConv, IsGraphics);
3565
3566 if (DAG.getPass()) {
3567 auto &ArgUsageInfo =
3569 ArgUsageInfo.getArgUsageInfo().setFuncArgInfo(Fn, Info->getArgInfo());
3570 } else if (auto *MFAM = DAG.getMFAM()) {
3571 Module &M = *MF.getFunction().getParent();
3572 auto *ArgUsageInfo =
3574 .getCachedResult<AMDGPUArgumentUsageAnalysis>(M);
3575 if (ArgUsageInfo)
3576 ArgUsageInfo->setFuncArgInfo(Fn, Info->getArgInfo());
3577 }
3578
3579 unsigned StackArgSize = CCInfo.getStackSize();
3580 Info->setBytesInStackArgArea(StackArgSize);
3581
3582 return Chains.empty() ? Chain
3583 : DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
3584}
3585
3586// TODO: If return values can't fit in registers, we should return as many as
3587// possible in registers before passing on stack.
3589 CallingConv::ID CallConv, MachineFunction &MF, bool IsVarArg,
3590 const SmallVectorImpl<ISD::OutputArg> &Outs, LLVMContext &Context,
3591 const Type *RetTy) const {
3592 // Replacing returns with sret/stack usage doesn't make sense for shaders.
3593 // FIXME: Also sort of a workaround for custom vector splitting in LowerReturn
3594 // for shaders. Vector types should be explicitly handled by CC.
3595 if (AMDGPU::isEntryFunctionCC(CallConv))
3596 return true;
3597
3599 CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
3600 if (!CCInfo.CheckReturn(Outs, CCAssignFnForReturn(CallConv, IsVarArg)))
3601 return false;
3602
3603 // We must use the stack if return would require unavailable registers.
3604 unsigned MaxNumVGPRs = Subtarget->getMaxNumVGPRs(MF);
3605 unsigned TotalNumVGPRs = Subtarget->getAddressableNumArchVGPRs();
3606 for (unsigned i = MaxNumVGPRs; i < TotalNumVGPRs; ++i)
3607 if (CCInfo.isAllocated(AMDGPU::VGPR_32RegClass.getRegister(i)))
3608 return false;
3609
3610 return true;
3611}
3612
3613SDValue
3615 bool isVarArg,
3617 const SmallVectorImpl<SDValue> &OutVals,
3618 const SDLoc &DL, SelectionDAG &DAG) const {
3622
3623 if (AMDGPU::isKernel(CallConv)) {
3624 return AMDGPUTargetLowering::LowerReturn(Chain, CallConv, isVarArg, Outs,
3625 OutVals, DL, DAG);
3626 }
3627
3628 bool IsShader = AMDGPU::isShader(CallConv);
3629
3630 Info->setIfReturnsVoid(Outs.empty());
3631 bool IsWaveEnd = Info->returnsVoid() && IsShader;
3632
3633 // CCValAssign - represent the assignment of the return value to a location.
3635
3636 // CCState - Info about the registers and stack slots.
3637 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
3638 *DAG.getContext());
3639
3640 // Analyze outgoing return values.
3641 CCInfo.AnalyzeReturn(Outs, CCAssignFnForReturn(CallConv, isVarArg));
3642
3643 SDValue Glue;
3645 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
3646
3647 SDValue ReadFirstLane =
3648 DAG.getTargetConstant(Intrinsic::amdgcn_readfirstlane, DL, MVT::i32);
3649 // Copy the result values into the output registers.
3650 for (unsigned I = 0, RealRVLocIdx = 0, E = RVLocs.size(); I != E;
3651 ++I, ++RealRVLocIdx) {
3652 CCValAssign &VA = RVLocs[I];
3653 assert(VA.isRegLoc() && "Can only return in registers!");
3654 // TODO: Partially return in registers if return values don't fit.
3655 SDValue Arg = OutVals[RealRVLocIdx];
3656
3657 // Copied from other backends.
3658 switch (VA.getLocInfo()) {
3659 case CCValAssign::Full:
3660 break;
3661 case CCValAssign::BCvt:
3662 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
3663 break;
3664 case CCValAssign::SExt:
3665 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg);
3666 break;
3667 case CCValAssign::ZExt:
3668 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg);
3669 break;
3670 case CCValAssign::AExt:
3671 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg);
3672 break;
3673 default:
3674 llvm_unreachable("Unknown loc info!");
3675 }
3676 if (TRI->isSGPRPhysReg(VA.getLocReg()))
3678 ReadFirstLane, Arg);
3679 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Glue);
3680 Glue = Chain.getValue(1);
3681 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
3682 }
3683
3684 // FIXME: Does sret work properly?
3685 if (!Info->isEntryFunction()) {
3686 const SIRegisterInfo *TRI = Subtarget->getRegisterInfo();
3687 const MCPhysReg *I =
3688 TRI->getCalleeSavedRegsViaCopy(&DAG.getMachineFunction());
3689 if (I) {
3690 for (; *I; ++I) {
3691 if (AMDGPU::SReg_64RegClass.contains(*I))
3692 RetOps.push_back(DAG.getRegister(*I, MVT::i64));
3693 else if (AMDGPU::SReg_32RegClass.contains(*I))
3694 RetOps.push_back(DAG.getRegister(*I, MVT::i32));
3695 else
3696 llvm_unreachable("Unexpected register class in CSRsViaCopy!");
3697 }
3698 }
3699 }
3700
3701 // Update chain and glue.
3702 RetOps[0] = Chain;
3703 if (Glue.getNode())
3704 RetOps.push_back(Glue);
3705
3706 unsigned Opc = AMDGPUISD::ENDPGM;
3707 if (!IsWaveEnd)
3708 Opc = Info->isWholeWaveFunction() ? AMDGPUISD::WHOLE_WAVE_RETURN
3709 : IsShader ? AMDGPUISD::RETURN_TO_EPILOG
3710 : AMDGPUISD::RET_GLUE;
3711 return DAG.getNode(Opc, DL, MVT::Other, RetOps);
3712}
3713
3715 SDValue Chain, SDValue InGlue, CallingConv::ID CallConv, bool IsVarArg,
3716 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
3717 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals, bool IsThisReturn,
3718 SDValue ThisVal) const {
3719 CCAssignFn *RetCC = CCAssignFnForReturn(CallConv, IsVarArg);
3720
3721 // Assign locations to each value returned by this call.
3723 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
3724 *DAG.getContext());
3725 CCInfo.AnalyzeCallResult(Ins, RetCC);
3726
3727 // Copy all of the result registers out of their specified physreg.
3728 for (CCValAssign VA : RVLocs) {
3729 SDValue Val;
3730
3731 if (VA.isRegLoc()) {
3732 Val =
3733 DAG.getCopyFromReg(Chain, DL, VA.getLocReg(), VA.getLocVT(), InGlue);
3734 Chain = Val.getValue(1);
3735 InGlue = Val.getValue(2);
3736 } else if (VA.isMemLoc()) {
3737 report_fatal_error("TODO: return values in memory");
3738 } else
3739 llvm_unreachable("unknown argument location type");
3740
3741 switch (VA.getLocInfo()) {
3742 case CCValAssign::Full:
3743 break;
3744 case CCValAssign::BCvt:
3745 Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
3746 break;
3747 case CCValAssign::ZExt:
3748 Val = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Val,
3749 DAG.getValueType(VA.getValVT()));
3750 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
3751 break;
3752 case CCValAssign::SExt:
3753 Val = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Val,
3754 DAG.getValueType(VA.getValVT()));
3755 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
3756 break;
3757 case CCValAssign::AExt:
3758 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
3759 break;
3760 default:
3761 llvm_unreachable("Unknown loc info!");
3762 }
3763
3764 InVals.push_back(Val);
3765 }
3766
3767 return Chain;
3768}
3769
3770// Add code to pass special inputs required depending on used features separate
3771// from the explicit user arguments present in the IR.
3773 CallLoweringInfo &CLI, CCState &CCInfo, const SIMachineFunctionInfo &Info,
3774 SmallVectorImpl<std::pair<unsigned, SDValue>> &RegsToPass,
3775 SmallVectorImpl<SDValue> &MemOpChains, SDValue Chain) const {
3776 // If we don't have a call site, this was a call inserted by
3777 // legalization. These can never use special inputs.
3778 if (!CLI.CB)
3779 return;
3780
3781 SelectionDAG &DAG = CLI.DAG;
3782 const SDLoc &DL = CLI.DL;
3783 const Function &F = DAG.getMachineFunction().getFunction();
3784
3785 const SIRegisterInfo *TRI = Subtarget->getRegisterInfo();
3786 const AMDGPUFunctionArgInfo &CallerArgInfo = Info.getArgInfo();
3787
3788 const AMDGPUFunctionArgInfo *CalleeArgInfo =
3790 if (const Function *CalleeFunc = CLI.CB->getCalledFunction()) {
3791 if (DAG.getPass()) {
3792 auto &ArgUsageInfo =
3794 CalleeArgInfo =
3795 &ArgUsageInfo.getArgUsageInfo().lookupFuncArgInfo(*CalleeFunc);
3796 } else if (auto *MFAM = DAG.getMFAM()) {
3798 auto *ArgUsageInfo =
3800 DAG.getMachineFunction())
3801 .getCachedResult<AMDGPUArgumentUsageAnalysis>(M);
3802 if (ArgUsageInfo)
3803 CalleeArgInfo = &ArgUsageInfo->lookupFuncArgInfo(*CalleeFunc);
3804 }
3805 }
3806
3807 // TODO: Unify with private memory register handling. This is complicated by
3808 // the fact that at least in kernels, the input argument is not necessarily
3809 // in the same location as the input.
3810 // clang-format off
3811 static constexpr std::pair<AMDGPUFunctionArgInfo::PreloadedValue,
3812 std::array<StringLiteral, 2>> ImplicitAttrs[] = {
3813 {AMDGPUFunctionArgInfo::DISPATCH_PTR, {"amdgpu-no-dispatch-ptr", ""}},
3814 {AMDGPUFunctionArgInfo::QUEUE_PTR, {"amdgpu-no-queue-ptr", ""}},
3815 {AMDGPUFunctionArgInfo::IMPLICIT_ARG_PTR, {"amdgpu-no-implicitarg-ptr", ""}},
3816 {AMDGPUFunctionArgInfo::DISPATCH_ID, {"amdgpu-no-dispatch-id", ""}},
3817 {AMDGPUFunctionArgInfo::WORKGROUP_ID_X, {"amdgpu-no-workgroup-id-x", "amdgpu-no-cluster-id-x"}},
3818 {AMDGPUFunctionArgInfo::WORKGROUP_ID_Y, {"amdgpu-no-workgroup-id-y", "amdgpu-no-cluster-id-y"}},
3819 {AMDGPUFunctionArgInfo::WORKGROUP_ID_Z, {"amdgpu-no-workgroup-id-z", "amdgpu-no-cluster-id-z"}},
3820 {AMDGPUFunctionArgInfo::LDS_KERNEL_ID, {"amdgpu-no-lds-kernel-id", ""}},
3821 };
3822 // clang-format on
3823
3824 for (auto [InputID, Attrs] : ImplicitAttrs) {
3825 // If the callee does not use the attribute value, skip copying the value.
3826 if (all_of(Attrs, [&](StringRef Attr) {
3827 return Attr.empty() || CLI.CB->hasFnAttr(Attr);
3828 }))
3829 continue;
3830
3831 const auto [OutgoingArg, ArgRC, ArgTy] =
3832 CalleeArgInfo->getPreloadedValue(InputID);
3833 if (!OutgoingArg)
3834 continue;
3835
3836 const auto [IncomingArg, IncomingArgRC, Ty] =
3837 CallerArgInfo.getPreloadedValue(InputID);
3838 assert(IncomingArgRC == ArgRC);
3839
3840 // All special arguments are ints for now.
3841 EVT ArgVT = TRI->getSpillSize(*ArgRC) == 8 ? MVT::i64 : MVT::i32;
3842 SDValue InputReg;
3843
3844 if (IncomingArg) {
3845 InputReg = loadInputValue(DAG, ArgRC, ArgVT, DL, *IncomingArg);
3846 } else if (InputID == AMDGPUFunctionArgInfo::IMPLICIT_ARG_PTR) {
3847 // The implicit arg ptr is special because it doesn't have a corresponding
3848 // input for kernels, and is computed from the kernarg segment pointer.
3849 InputReg = getImplicitArgPtr(DAG, DL);
3850 } else if (InputID == AMDGPUFunctionArgInfo::LDS_KERNEL_ID) {
3851 std::optional<uint32_t> Id =
3853 if (Id.has_value()) {
3854 InputReg = DAG.getConstant(*Id, DL, ArgVT);
3855 } else {
3856 InputReg = DAG.getPOISON(ArgVT);
3857 }
3858 } else {
3859 // We may have proven the input wasn't needed, although the ABI is
3860 // requiring it. We just need to allocate the register appropriately.
3861 InputReg = DAG.getPOISON(ArgVT);
3862 }
3863
3864 if (OutgoingArg->isRegister()) {
3865 RegsToPass.emplace_back(OutgoingArg->getRegister(), InputReg);
3866 if (!CCInfo.AllocateReg(OutgoingArg->getRegister()))
3867 report_fatal_error("failed to allocate implicit input argument");
3868 } else {
3869 unsigned SpecialArgOffset =
3870 CCInfo.AllocateStack(ArgVT.getStoreSize(), Align(4));
3871 SDValue ArgStore =
3872 storeStackInputValue(DAG, DL, Chain, InputReg, SpecialArgOffset);
3873 MemOpChains.push_back(ArgStore);
3874 }
3875 }
3876
3877 // Pack workitem IDs into a single register or pass it as is if already
3878 // packed.
3879
3880 auto [OutgoingArg, ArgRC, Ty] =
3882 if (!OutgoingArg)
3883 std::tie(OutgoingArg, ArgRC, Ty) =
3885 if (!OutgoingArg)
3886 std::tie(OutgoingArg, ArgRC, Ty) =
3888 if (!OutgoingArg)
3889 return;
3890
3891 const ArgDescriptor *IncomingArgX = std::get<0>(
3893 const ArgDescriptor *IncomingArgY = std::get<0>(
3895 const ArgDescriptor *IncomingArgZ = std::get<0>(
3897
3898 SDValue InputReg;
3899 SDLoc SL;
3900
3901 const bool NeedWorkItemIDX = !CLI.CB->hasFnAttr("amdgpu-no-workitem-id-x");
3902 const bool NeedWorkItemIDY = !CLI.CB->hasFnAttr("amdgpu-no-workitem-id-y");
3903 const bool NeedWorkItemIDZ = !CLI.CB->hasFnAttr("amdgpu-no-workitem-id-z");
3904
3905 // If incoming ids are not packed we need to pack them.
3906 if (IncomingArgX && !IncomingArgX->isMasked() && CalleeArgInfo->WorkItemIDX &&
3907 NeedWorkItemIDX) {
3908 if (Subtarget->getMaxWorkitemID(F, 0) != 0) {
3909 InputReg = loadInputValue(DAG, ArgRC, MVT::i32, DL, *IncomingArgX);
3910 } else {
3911 InputReg = DAG.getConstant(0, DL, MVT::i32);
3912 }
3913 }
3914
3915 if (IncomingArgY && !IncomingArgY->isMasked() && CalleeArgInfo->WorkItemIDY &&
3916 NeedWorkItemIDY && Subtarget->getMaxWorkitemID(F, 1) != 0) {
3917 SDValue Y = loadInputValue(DAG, ArgRC, MVT::i32, DL, *IncomingArgY);
3918 Y = DAG.getNode(ISD::SHL, SL, MVT::i32, Y,
3919 DAG.getShiftAmountConstant(10, MVT::i32, SL));
3920 InputReg = InputReg.getNode()
3921 ? DAG.getNode(ISD::OR, SL, MVT::i32, InputReg, Y)
3922 : Y;
3923 }
3924
3925 if (IncomingArgZ && !IncomingArgZ->isMasked() && CalleeArgInfo->WorkItemIDZ &&
3926 NeedWorkItemIDZ && Subtarget->getMaxWorkitemID(F, 2) != 0) {
3927 SDValue Z = loadInputValue(DAG, ArgRC, MVT::i32, DL, *IncomingArgZ);
3928 Z = DAG.getNode(ISD::SHL, SL, MVT::i32, Z,
3929 DAG.getShiftAmountConstant(20, MVT::i32, SL));
3930 InputReg = InputReg.getNode()
3931 ? DAG.getNode(ISD::OR, SL, MVT::i32, InputReg, Z)
3932 : Z;
3933 }
3934
3935 if (!InputReg && (NeedWorkItemIDX || NeedWorkItemIDY || NeedWorkItemIDZ)) {
3936 if (!IncomingArgX && !IncomingArgY && !IncomingArgZ) {
3937 // We're in a situation where the outgoing function requires the workitem
3938 // ID, but the calling function does not have it (e.g a graphics function
3939 // calling a C calling convention function). This is illegal, but we need
3940 // to produce something.
3941 InputReg = DAG.getPOISON(MVT::i32);
3942 } else {
3943 // Workitem ids are already packed, any of present incoming arguments
3944 // will carry all required fields.
3945 ArgDescriptor IncomingArg =
3946 ArgDescriptor::createArg(IncomingArgX ? *IncomingArgX
3947 : IncomingArgY ? *IncomingArgY
3948 : *IncomingArgZ,
3949 ~0u);
3950 InputReg = loadInputValue(DAG, ArgRC, MVT::i32, DL, IncomingArg);
3951 }
3952 }
3953
3954 if (OutgoingArg->isRegister()) {
3955 if (InputReg)
3956 RegsToPass.emplace_back(OutgoingArg->getRegister(), InputReg);
3957
3958 CCInfo.AllocateReg(OutgoingArg->getRegister());
3959 } else {
3960 unsigned SpecialArgOffset = CCInfo.AllocateStack(4, Align(4));
3961 if (InputReg) {
3962 SDValue ArgStore =
3963 storeStackInputValue(DAG, DL, Chain, InputReg, SpecialArgOffset);
3964 MemOpChains.push_back(ArgStore);
3965 }
3966 }
3967}
3968
3970 SDValue Callee, CallingConv::ID CalleeCC, bool IsVarArg,
3972 const SmallVectorImpl<SDValue> &OutVals,
3973 const SmallVectorImpl<ISD::InputArg> &Ins, SelectionDAG &DAG) const {
3974 if (AMDGPU::isChainCC(CalleeCC))
3975 return true;
3976
3977 if (!AMDGPU::mayTailCallThisCC(CalleeCC))
3978 return false;
3979
3980 // For a divergent call target, we need to do a waterfall loop over the
3981 // possible callees which precludes us from using a simple jump.
3982 if (Callee->isDivergent())
3983 return false;
3984
3986 const Function &CallerF = MF.getFunction();
3987 CallingConv::ID CallerCC = CallerF.getCallingConv();
3989 const uint32_t *CallerPreserved = TRI->getCallPreservedMask(MF, CallerCC);
3990
3991 // Kernels aren't callable, and don't have a live in return address so it
3992 // doesn't make sense to do a tail call with entry functions.
3993 if (!CallerPreserved)
3994 return false;
3995
3996 bool CCMatch = CallerCC == CalleeCC;
3997
3999 if (AMDGPU::canGuaranteeTCO(CalleeCC) && CCMatch)
4000 return true;
4001 return false;
4002 }
4003
4004 // TODO: Can we handle var args?
4005 if (IsVarArg)
4006 return false;
4007
4008 for (const Argument &Arg : CallerF.args()) {
4009 if (Arg.hasByValAttr())
4010 return false;
4011 }
4012
4013 LLVMContext &Ctx = *DAG.getContext();
4014
4015 // Check that the call results are passed in the same way.
4016 if (!CCState::resultsCompatible(CalleeCC, CallerCC, MF, Ctx, Ins,
4017 CCAssignFnForCall(CalleeCC, IsVarArg),
4018 CCAssignFnForCall(CallerCC, IsVarArg)))
4019 return false;
4020
4021 // The callee has to preserve all registers the caller needs to preserve.
4022 if (!CCMatch) {
4023 const uint32_t *CalleePreserved = TRI->getCallPreservedMask(MF, CalleeCC);
4024 if (!TRI->regmaskSubsetEqual(CallerPreserved, CalleePreserved))
4025 return false;
4026 }
4027
4028 // Nothing more to check if the callee is taking no arguments.
4029 if (Outs.empty())
4030 return true;
4031
4033 CCState CCInfo(CalleeCC, IsVarArg, MF, ArgLocs, Ctx);
4034
4035 // FIXME: We are not allocating special input registers, so we will be
4036 // deciding based on incorrect register assignments.
4037 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForCall(CalleeCC, IsVarArg));
4038
4039 const SIMachineFunctionInfo *FuncInfo = MF.getInfo<SIMachineFunctionInfo>();
4040 // If the stack arguments for this call do not fit into our own save area then
4041 // the call cannot be made tail.
4042 // TODO: Is this really necessary?
4043 if (CCInfo.getStackSize() > FuncInfo->getBytesInStackArgArea())
4044 return false;
4045
4046 for (const auto &[CCVA, ArgVal] : zip_equal(ArgLocs, OutVals)) {
4047 // FIXME: What about inreg arguments that end up passed in memory?
4048 if (!CCVA.isRegLoc())
4049 continue;
4050
4051 // If we are passing an argument in an SGPR, and the value is divergent,
4052 // this call requires a waterfall loop.
4053 if (ArgVal->isDivergent() && TRI->isSGPRPhysReg(CCVA.getLocReg())) {
4054 LLVM_DEBUG(
4055 dbgs() << "Cannot tail call due to divergent outgoing argument in "
4056 << printReg(CCVA.getLocReg(), TRI) << '\n');
4057 return false;
4058 }
4059 }
4060
4061 const MachineRegisterInfo &MRI = MF.getRegInfo();
4062 return parametersInCSRMatch(MRI, CallerPreserved, ArgLocs, OutVals);
4063}
4064
4066 if (!CI->isTailCall())
4067 return false;
4068
4069 const Function *ParentFn = CI->getFunction();
4071 return false;
4072 return true;
4073}
4074
4075namespace {
4076// Chain calls have special arguments that we need to handle. These are
4077// tagging along at the end of the arguments list(s), after the SGPR and VGPR
4078// arguments (index 0 and 1 respectively).
4079enum ChainCallArgIdx {
4080 Exec = 2,
4081 Flags,
4082 NumVGPRs,
4083 FallbackExec,
4084 FallbackCallee
4085};
4086} // anonymous namespace
4087
4088// The wave scratch offset register is used as the global base pointer.
4090 SmallVectorImpl<SDValue> &InVals) const {
4091 CallingConv::ID CallConv = CLI.CallConv;
4092 bool IsChainCallConv = AMDGPU::isChainCC(CallConv);
4093
4094 SelectionDAG &DAG = CLI.DAG;
4095
4096 const SDLoc &DL = CLI.DL;
4097 SDValue Chain = CLI.Chain;
4098 SDValue Callee = CLI.Callee;
4099
4100 llvm::SmallVector<SDValue, 6> ChainCallSpecialArgs;
4101 bool UsesDynamicVGPRs = false;
4102 if (IsChainCallConv) {
4103 // The last arguments should be the value that we need to put in EXEC,
4104 // followed by the flags and any other arguments with special meanings.
4105 // Pop them out of CLI.Outs and CLI.OutVals before we do any processing so
4106 // we don't treat them like the "real" arguments.
4107 auto RequestedExecIt =
4108 llvm::find_if(CLI.Outs, [](const ISD::OutputArg &Arg) {
4109 return Arg.OrigArgIndex == 2;
4110 });
4111 assert(RequestedExecIt != CLI.Outs.end() && "No node for EXEC");
4112
4113 size_t SpecialArgsBeginIdx = RequestedExecIt - CLI.Outs.begin();
4114 CLI.OutVals.erase(CLI.OutVals.begin() + SpecialArgsBeginIdx,
4115 CLI.OutVals.end());
4116 CLI.Outs.erase(RequestedExecIt, CLI.Outs.end());
4117
4118 assert(CLI.Outs.back().OrigArgIndex < 2 &&
4119 "Haven't popped all the special args");
4120
4121 TargetLowering::ArgListEntry RequestedExecArg =
4122 CLI.Args[ChainCallArgIdx::Exec];
4123 if (!RequestedExecArg.Ty->isIntegerTy(Subtarget->getWavefrontSize()))
4124 return lowerUnhandledCall(CLI, InVals, "Invalid value for EXEC");
4125
4126 // Convert constants into TargetConstants, so they become immediate operands
4127 // instead of being selected into S_MOV.
4128 auto PushNodeOrTargetConstant = [&](TargetLowering::ArgListEntry Arg) {
4129 if (const auto *ArgNode = dyn_cast<ConstantSDNode>(Arg.Node)) {
4130 ChainCallSpecialArgs.push_back(DAG.getTargetConstant(
4131 ArgNode->getAPIntValue(), DL, ArgNode->getValueType(0)));
4132 } else
4133 ChainCallSpecialArgs.push_back(Arg.Node);
4134 };
4135
4136 PushNodeOrTargetConstant(RequestedExecArg);
4137
4138 // Process any other special arguments depending on the value of the flags.
4139 TargetLowering::ArgListEntry Flags = CLI.Args[ChainCallArgIdx::Flags];
4140
4141 const APInt &FlagsValue = cast<ConstantSDNode>(Flags.Node)->getAPIntValue();
4142 if (FlagsValue.isZero()) {
4143 if (CLI.Args.size() > ChainCallArgIdx::Flags + 1)
4144 return lowerUnhandledCall(CLI, InVals,
4145 "no additional args allowed if flags == 0");
4146 } else if (FlagsValue.isOneBitSet(0)) {
4147 if (CLI.Args.size() != ChainCallArgIdx::FallbackCallee + 1) {
4148 return lowerUnhandledCall(CLI, InVals, "expected 3 additional args");
4149 }
4150
4151 if (!Subtarget->isWave32()) {
4152 return lowerUnhandledCall(
4153 CLI, InVals, "dynamic VGPR mode is only supported for wave32");
4154 }
4155
4156 UsesDynamicVGPRs = true;
4157 std::for_each(CLI.Args.begin() + ChainCallArgIdx::NumVGPRs,
4158 CLI.Args.end(), PushNodeOrTargetConstant);
4159 }
4160 }
4161
4163 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
4165 bool &IsTailCall = CLI.IsTailCall;
4166 bool IsVarArg = CLI.IsVarArg;
4167 bool IsSibCall = false;
4169
4170 if (Callee.isUndef() || isNullConstant(Callee)) {
4171 if (!CLI.IsTailCall) {
4172 for (ISD::InputArg &Arg : CLI.Ins)
4173 InVals.push_back(DAG.getPOISON(Arg.VT));
4174 }
4175
4176 return Chain;
4177 }
4178
4179 if (IsVarArg) {
4180 return lowerUnhandledCall(CLI, InVals,
4181 "unsupported call to variadic function ");
4182 }
4183
4184 if (!CLI.CB)
4185 return lowerUnhandledCall(CLI, InVals, "unsupported libcall legalization");
4186
4187 if (IsTailCall && MF.getTarget().Options.GuaranteedTailCallOpt) {
4188 return lowerUnhandledCall(CLI, InVals,
4189 "unsupported required tail call to function ");
4190 }
4191
4192 if (IsTailCall) {
4193 IsTailCall = isEligibleForTailCallOptimization(Callee, CallConv, IsVarArg,
4194 Outs, OutVals, Ins, DAG);
4195 if (!IsTailCall &&
4196 ((CLI.CB && CLI.CB->isMustTailCall()) || IsChainCallConv)) {
4197 report_fatal_error("failed to perform tail call elimination on a call "
4198 "site marked musttail or on llvm.amdgcn.cs.chain");
4199 }
4200
4201 bool TailCallOpt = MF.getTarget().Options.GuaranteedTailCallOpt;
4202
4203 // A sibling call is one where we're under the usual C ABI and not planning
4204 // to change that but can still do a tail call:
4205 if (!TailCallOpt && IsTailCall)
4206 IsSibCall = true;
4207
4208 if (IsTailCall)
4209 ++NumTailCalls;
4210 }
4211
4214 SmallVector<SDValue, 8> MemOpChains;
4215
4216 // Analyze operands of the call, assigning locations to each operand.
4218 CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
4219 CCAssignFn *AssignFn = CCAssignFnForCall(CallConv, IsVarArg);
4220
4221 if (CallConv != CallingConv::AMDGPU_Gfx && !AMDGPU::isChainCC(CallConv) &&
4223 // With a fixed ABI, allocate fixed registers before user arguments.
4224 passSpecialInputs(CLI, CCInfo, *Info, RegsToPass, MemOpChains, Chain);
4225 }
4226
4227 CCInfo.AnalyzeCallOperands(Outs, AssignFn);
4228
4229 // Get a count of how many bytes are to be pushed on the stack.
4230 unsigned NumBytes = CCInfo.getStackSize();
4231
4232 if (IsSibCall) {
4233 // Since we're not changing the ABI to make this a tail call, the memory
4234 // operands are already available in the caller's incoming argument space.
4235 NumBytes = 0;
4236 }
4237
4238 // FPDiff is the byte offset of the call's argument area from the callee's.
4239 // Stores to callee stack arguments will be placed in FixedStackSlots offset
4240 // by this amount for a tail call. In a sibling call it must be 0 because the
4241 // caller will deallocate the entire stack and the callee still expects its
4242 // arguments to begin at SP+0. Completely unused for non-tail calls.
4243 int32_t FPDiff = 0;
4244 MachineFrameInfo &MFI = MF.getFrameInfo();
4245 auto *TRI = Subtarget->getRegisterInfo();
4246
4247 // Adjust the stack pointer for the new arguments...
4248 // These operations are automatically eliminated by the prolog/epilog pass
4249 if (!IsSibCall)
4250 Chain = DAG.getCALLSEQ_START(Chain, 0, 0, DL);
4251
4252 if (!IsSibCall || IsChainCallConv) {
4253 if (!Subtarget->enableFlatScratch()) {
4254 SmallVector<SDValue, 4> CopyFromChains;
4255
4256 // In the HSA case, this should be an identity copy.
4257 SDValue ScratchRSrcReg =
4258 DAG.getCopyFromReg(Chain, DL, Info->getScratchRSrcReg(), MVT::v4i32);
4259 RegsToPass.emplace_back(IsChainCallConv
4260 ? AMDGPU::SGPR48_SGPR49_SGPR50_SGPR51
4261 : AMDGPU::SGPR0_SGPR1_SGPR2_SGPR3,
4262 ScratchRSrcReg);
4263 CopyFromChains.push_back(ScratchRSrcReg.getValue(1));
4264 Chain = DAG.getTokenFactor(DL, CopyFromChains);
4265 }
4266 }
4267
4268 const unsigned NumSpecialInputs = RegsToPass.size();
4269
4270 MVT PtrVT = MVT::i32;
4271
4272 // Walk the register/memloc assignments, inserting copies/loads.
4273 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
4274 CCValAssign &VA = ArgLocs[i];
4275 SDValue Arg = OutVals[i];
4276
4277 // Promote the value if needed.
4278 switch (VA.getLocInfo()) {
4279 case CCValAssign::Full:
4280 break;
4281 case CCValAssign::BCvt:
4282 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
4283 break;
4284 case CCValAssign::ZExt:
4285 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg);
4286 break;
4287 case CCValAssign::SExt:
4288 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg);
4289 break;
4290 case CCValAssign::AExt:
4291 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg);
4292 break;
4293 case CCValAssign::FPExt:
4294 Arg = DAG.getNode(ISD::FP_EXTEND, DL, VA.getLocVT(), Arg);
4295 break;
4296 default:
4297 llvm_unreachable("Unknown loc info!");
4298 }
4299
4300 if (VA.isRegLoc()) {
4301 RegsToPass.push_back(std::pair(VA.getLocReg(), Arg));
4302 } else {
4303 assert(VA.isMemLoc());
4304
4305 SDValue DstAddr;
4306 MachinePointerInfo DstInfo;
4307
4308 unsigned LocMemOffset = VA.getLocMemOffset();
4309 int32_t Offset = LocMemOffset;
4310
4311 SDValue PtrOff = DAG.getConstant(Offset, DL, PtrVT);
4312 MaybeAlign Alignment;
4313
4314 if (IsTailCall) {
4315 ISD::ArgFlagsTy Flags = Outs[i].Flags;
4316 unsigned OpSize = Flags.isByVal() ? Flags.getByValSize()
4317 : VA.getValVT().getStoreSize();
4318
4319 // FIXME: We can have better than the minimum byval required alignment.
4320 Alignment =
4321 Flags.isByVal()
4322 ? Flags.getNonZeroByValAlign()
4323 : commonAlignment(Subtarget->getStackAlignment(), Offset);
4324
4325 Offset = Offset + FPDiff;
4326 int FI = MFI.CreateFixedObject(OpSize, Offset, true);
4327
4328 DstAddr = DAG.getFrameIndex(FI, PtrVT);
4329 DstInfo = MachinePointerInfo::getFixedStack(MF, FI);
4330
4331 // Make sure any stack arguments overlapping with where we're storing
4332 // are loaded before this eventual operation. Otherwise they'll be
4333 // clobbered.
4334
4335 // FIXME: Why is this really necessary? This seems to just result in a
4336 // lot of code to copy the stack and write them back to the same
4337 // locations, which are supposed to be immutable?
4338 Chain = addTokenForArgument(Chain, DAG, MFI, FI);
4339 } else {
4340 // Stores to the argument stack area are relative to the stack pointer.
4341 SDValue SP = DAG.getCopyFromReg(Chain, DL, Info->getStackPtrOffsetReg(),
4342 MVT::i32);
4343 DstAddr = DAG.getNode(ISD::ADD, DL, MVT::i32, SP, PtrOff);
4344 DstInfo = MachinePointerInfo::getStack(MF, LocMemOffset);
4345 Alignment =
4346 commonAlignment(Subtarget->getStackAlignment(), LocMemOffset);
4347 }
4348
4349 if (Outs[i].Flags.isByVal()) {
4350 SDValue SizeNode =
4351 DAG.getConstant(Outs[i].Flags.getByValSize(), DL, MVT::i32);
4352 SDValue Cpy =
4353 DAG.getMemcpy(Chain, DL, DstAddr, Arg, SizeNode,
4354 Outs[i].Flags.getNonZeroByValAlign(),
4355 /*isVol = */ false, /*AlwaysInline = */ true,
4356 /*CI=*/nullptr, std::nullopt, DstInfo,
4358
4359 MemOpChains.push_back(Cpy);
4360 } else {
4361 SDValue Store =
4362 DAG.getStore(Chain, DL, Arg, DstAddr, DstInfo, Alignment);
4363 MemOpChains.push_back(Store);
4364 }
4365 }
4366 }
4367
4368 if (!MemOpChains.empty())
4369 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
4370
4371 SDValue ReadFirstLaneID =
4372 DAG.getTargetConstant(Intrinsic::amdgcn_readfirstlane, DL, MVT::i32);
4373
4374 SDValue TokenGlue;
4375 if (CLI.ConvergenceControlToken) {
4376 TokenGlue = DAG.getNode(ISD::CONVERGENCECTRL_GLUE, DL, MVT::Glue,
4378 }
4379
4380 // Build a sequence of copy-to-reg nodes chained together with token chain
4381 // and flag operands which copy the outgoing args into the appropriate regs.
4382 SDValue InGlue;
4383
4384 unsigned ArgIdx = 0;
4385 for (auto [Reg, Val] : RegsToPass) {
4386 if (ArgIdx++ >= NumSpecialInputs &&
4387 (IsChainCallConv || !Val->isDivergent()) && TRI->isSGPRPhysReg(Reg)) {
4388 // For chain calls, the inreg arguments are required to be
4389 // uniform. Speculatively Insert a readfirstlane in case we cannot prove
4390 // they are uniform.
4391 //
4392 // For other calls, if an inreg arguments is known to be uniform,
4393 // speculatively insert a readfirstlane in case it is in a VGPR.
4394 //
4395 // FIXME: We need to execute this in a waterfall loop if it is a divergent
4396 // value, so let that continue to produce invalid code.
4397
4398 SmallVector<SDValue, 3> ReadfirstlaneArgs({ReadFirstLaneID, Val});
4399 if (TokenGlue)
4400 ReadfirstlaneArgs.push_back(TokenGlue);
4402 ReadfirstlaneArgs);
4403 }
4404
4405 Chain = DAG.getCopyToReg(Chain, DL, Reg, Val, InGlue);
4406 InGlue = Chain.getValue(1);
4407 }
4408
4409 // We don't usually want to end the call-sequence here because we would tidy
4410 // the frame up *after* the call, however in the ABI-changing tail-call case
4411 // we've carefully laid out the parameters so that when sp is reset they'll be
4412 // in the correct location.
4413 if (IsTailCall && !IsSibCall) {
4414 Chain = DAG.getCALLSEQ_END(Chain, NumBytes, 0, InGlue, DL);
4415 InGlue = Chain.getValue(1);
4416 }
4417
4418 std::vector<SDValue> Ops({Chain});
4419
4420 // Add a redundant copy of the callee global which will not be legalized, as
4421 // we need direct access to the callee later.
4423 const GlobalValue *GV = GSD->getGlobal();
4424 Ops.push_back(Callee);
4425 Ops.push_back(DAG.getTargetGlobalAddress(GV, DL, MVT::i64));
4426 } else {
4427 if (IsTailCall) {
4428 // isEligibleForTailCallOptimization considered whether the call target is
4429 // divergent, but we may still end up with a uniform value in a VGPR.
4430 // Insert a readfirstlane just in case.
4431 SDValue ReadFirstLaneID =
4432 DAG.getTargetConstant(Intrinsic::amdgcn_readfirstlane, DL, MVT::i32);
4433
4434 SmallVector<SDValue, 3> ReadfirstlaneArgs({ReadFirstLaneID, Callee});
4435 if (TokenGlue)
4436 ReadfirstlaneArgs.push_back(TokenGlue); // Wire up convergence token.
4437 Callee = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, Callee.getValueType(),
4438 ReadfirstlaneArgs);
4439 }
4440
4441 Ops.push_back(Callee);
4442 Ops.push_back(DAG.getTargetConstant(0, DL, MVT::i64));
4443 }
4444
4445 if (IsTailCall) {
4446 // Each tail call may have to adjust the stack by a different amount, so
4447 // this information must travel along with the operation for eventual
4448 // consumption by emitEpilogue.
4449 Ops.push_back(DAG.getTargetConstant(FPDiff, DL, MVT::i32));
4450 }
4451
4452 if (IsChainCallConv)
4453 llvm::append_range(Ops, ChainCallSpecialArgs);
4454
4455 // Add argument registers to the end of the list so that they are known live
4456 // into the call.
4457 for (auto &[Reg, Val] : RegsToPass)
4458 Ops.push_back(DAG.getRegister(Reg, Val.getValueType()));
4459
4460 // Add a register mask operand representing the call-preserved registers.
4461 const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv);
4462 assert(Mask && "Missing call preserved mask for calling convention");
4463 Ops.push_back(DAG.getRegisterMask(Mask));
4464
4465 if (SDValue Token = CLI.ConvergenceControlToken) {
4467 GlueOps.push_back(Token);
4468 if (InGlue)
4469 GlueOps.push_back(InGlue);
4470
4471 InGlue = SDValue(DAG.getMachineNode(TargetOpcode::CONVERGENCECTRL_GLUE, DL,
4472 MVT::Glue, GlueOps),
4473 0);
4474 }
4475
4476 if (InGlue)
4477 Ops.push_back(InGlue);
4478
4479 // If we're doing a tall call, use a TC_RETURN here rather than an
4480 // actual call instruction.
4481 if (IsTailCall) {
4482 MFI.setHasTailCall();
4483 unsigned OPC = AMDGPUISD::TC_RETURN;
4484 switch (CallConv) {
4486 OPC = AMDGPUISD::TC_RETURN_GFX;
4487 break;
4490 OPC = UsesDynamicVGPRs ? AMDGPUISD::TC_RETURN_CHAIN_DVGPR
4491 : AMDGPUISD::TC_RETURN_CHAIN;
4492 break;
4493 }
4494
4495 // If the caller is a whole wave function, we need to use a special opcode
4496 // so we can patch up EXEC.
4497 if (Info->isWholeWaveFunction())
4498 OPC = AMDGPUISD::TC_RETURN_GFX_WholeWave;
4499
4500 return DAG.getNode(OPC, DL, MVT::Other, Ops);
4501 }
4502
4503 // Returns a chain and a flag for retval copy to use.
4504 SDValue Call = DAG.getNode(AMDGPUISD::CALL, DL, {MVT::Other, MVT::Glue}, Ops);
4505 Chain = Call.getValue(0);
4506 InGlue = Call.getValue(1);
4507
4508 uint64_t CalleePopBytes = NumBytes;
4509 Chain = DAG.getCALLSEQ_END(Chain, 0, CalleePopBytes, InGlue, DL);
4510 if (!Ins.empty())
4511 InGlue = Chain.getValue(1);
4512
4513 // Handle result values, copying them out of physregs into vregs that we
4514 // return.
4515 return LowerCallResult(Chain, InGlue, CallConv, IsVarArg, Ins, DL, DAG,
4516 InVals, /*IsThisReturn=*/false, SDValue());
4517}
4518
4519// This is similar to the default implementation in ExpandDYNAMIC_STACKALLOC,
4520// except for:
4521// 1. Stack growth direction(default: downwards, AMDGPU: upwards), and
4522// 2. Scale size where, scale = wave-reduction(alloca-size) * wave-size
4524 SelectionDAG &DAG) const {
4525 const MachineFunction &MF = DAG.getMachineFunction();
4527
4528 SDLoc dl(Op);
4529 EVT VT = Op.getValueType();
4530 SDValue Chain = Op.getOperand(0);
4531 Register SPReg = Info->getStackPtrOffsetReg();
4532
4533 // Chain the dynamic stack allocation so that it doesn't modify the stack
4534 // pointer when other instructions are using the stack.
4535 Chain = DAG.getCALLSEQ_START(Chain, 0, 0, dl);
4536
4537 SDValue Size = Op.getOperand(1);
4538 SDValue BaseAddr = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
4539 Align Alignment = cast<ConstantSDNode>(Op.getOperand(2))->getAlignValue();
4540
4541 const TargetFrameLowering *TFL = Subtarget->getFrameLowering();
4543 "Stack grows upwards for AMDGPU");
4544
4545 Chain = BaseAddr.getValue(1);
4546 Align StackAlign = TFL->getStackAlign();
4547 if (Alignment > StackAlign) {
4548 uint64_t ScaledAlignment = Alignment.value()
4549 << Subtarget->getWavefrontSizeLog2();
4550 uint64_t StackAlignMask = ScaledAlignment - 1;
4551 SDValue TmpAddr = DAG.getNode(ISD::ADD, dl, VT, BaseAddr,
4552 DAG.getConstant(StackAlignMask, dl, VT));
4553 BaseAddr = DAG.getNode(ISD::AND, dl, VT, TmpAddr,
4554 DAG.getSignedConstant(-ScaledAlignment, dl, VT));
4555 }
4556
4557 assert(Size.getValueType() == MVT::i32 && "Size must be 32-bit");
4558 SDValue NewSP;
4560 // For constant sized alloca, scale alloca size by wave-size
4561 SDValue ScaledSize = DAG.getNode(
4562 ISD::SHL, dl, VT, Size,
4563 DAG.getConstant(Subtarget->getWavefrontSizeLog2(), dl, MVT::i32));
4564 NewSP = DAG.getNode(ISD::ADD, dl, VT, BaseAddr, ScaledSize); // Value
4565 } else {
4566 // For dynamic sized alloca, perform wave-wide reduction to get max of
4567 // alloca size(divergent) and then scale it by wave-size
4568 SDValue WaveReduction =
4569 DAG.getTargetConstant(Intrinsic::amdgcn_wave_reduce_umax, dl, MVT::i32);
4570 Size = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32, WaveReduction,
4571 Size, DAG.getConstant(0, dl, MVT::i32));
4572 SDValue ScaledSize = DAG.getNode(
4573 ISD::SHL, dl, VT, Size,
4574 DAG.getConstant(Subtarget->getWavefrontSizeLog2(), dl, MVT::i32));
4575 NewSP =
4576 DAG.getNode(ISD::ADD, dl, VT, BaseAddr, ScaledSize); // Value in vgpr.
4577 SDValue ReadFirstLaneID =
4578 DAG.getTargetConstant(Intrinsic::amdgcn_readfirstlane, dl, MVT::i32);
4579 NewSP = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32, ReadFirstLaneID,
4580 NewSP);
4581 }
4582
4583 Chain = DAG.getCopyToReg(Chain, dl, SPReg, NewSP); // Output chain
4584 SDValue CallSeqEnd = DAG.getCALLSEQ_END(Chain, 0, 0, SDValue(), dl);
4585
4586 return DAG.getMergeValues({BaseAddr, CallSeqEnd}, dl);
4587}
4588
4590 if (Op.getValueType() != MVT::i32)
4591 return Op; // Defer to cannot select error.
4592
4594 SDLoc SL(Op);
4595
4596 SDValue CopyFromSP = DAG.getCopyFromReg(Op->getOperand(0), SL, SP, MVT::i32);
4597
4598 // Convert from wave uniform to swizzled vector address. This should protect
4599 // from any edge cases where the stacksave result isn't directly used with
4600 // stackrestore.
4601 SDValue VectorAddress =
4602 DAG.getNode(AMDGPUISD::WAVE_ADDRESS, SL, MVT::i32, CopyFromSP);
4603 return DAG.getMergeValues({VectorAddress, CopyFromSP.getValue(1)}, SL);
4604}
4605
4607 SelectionDAG &DAG) const {
4608 SDLoc SL(Op);
4609 assert(Op.getValueType() == MVT::i32);
4610
4611 uint32_t BothRoundHwReg =
4613 SDValue GetRoundBothImm = DAG.getTargetConstant(BothRoundHwReg, SL, MVT::i32);
4614
4615 SDValue IntrinID =
4616 DAG.getTargetConstant(Intrinsic::amdgcn_s_getreg, SL, MVT::i32);
4617 SDValue GetReg = DAG.getNode(ISD::INTRINSIC_W_CHAIN, SL, Op->getVTList(),
4618 Op.getOperand(0), IntrinID, GetRoundBothImm);
4619
4620 // There are two rounding modes, one for f32 and one for f64/f16. We only
4621 // report in the standard value range if both are the same.
4622 //
4623 // The raw values also differ from the expected FLT_ROUNDS values. Nearest
4624 // ties away from zero is not supported, and the other values are rotated by
4625 // 1.
4626 //
4627 // If the two rounding modes are not the same, report a target defined value.
4628
4629 // Mode register rounding mode fields:
4630 //
4631 // [1:0] Single-precision round mode.
4632 // [3:2] Double/Half-precision round mode.
4633 //
4634 // 0=nearest even; 1= +infinity; 2= -infinity, 3= toward zero.
4635 //
4636 // Hardware Spec
4637 // Toward-0 3 0
4638 // Nearest Even 0 1
4639 // +Inf 1 2
4640 // -Inf 2 3
4641 // NearestAway0 N/A 4
4642 //
4643 // We have to handle 16 permutations of a 4-bit value, so we create a 64-bit
4644 // table we can index by the raw hardware mode.
4645 //
4646 // (trunc (FltRoundConversionTable >> MODE.fp_round)) & 0xf
4647
4648 SDValue BitTable =
4650
4651 SDValue Two = DAG.getConstant(2, SL, MVT::i32);
4652 SDValue RoundModeTimesNumBits =
4653 DAG.getNode(ISD::SHL, SL, MVT::i32, GetReg, Two);
4654
4655 // TODO: We could possibly avoid a 64-bit shift and use a simpler table if we
4656 // knew only one mode was demanded.
4657 SDValue TableValue =
4658 DAG.getNode(ISD::SRL, SL, MVT::i64, BitTable, RoundModeTimesNumBits);
4659 SDValue TruncTable = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, TableValue);
4660
4661 SDValue EntryMask = DAG.getConstant(0xf, SL, MVT::i32);
4662 SDValue TableEntry =
4663 DAG.getNode(ISD::AND, SL, MVT::i32, TruncTable, EntryMask);
4664
4665 // There's a gap in the 4-bit encoded table and actual enum values, so offset
4666 // if it's an extended value.
4667 SDValue Four = DAG.getConstant(4, SL, MVT::i32);
4668 SDValue IsStandardValue =
4669 DAG.getSetCC(SL, MVT::i1, TableEntry, Four, ISD::SETULT);
4670 SDValue EnumOffset = DAG.getNode(ISD::ADD, SL, MVT::i32, TableEntry, Four);
4671 SDValue Result = DAG.getNode(ISD::SELECT, SL, MVT::i32, IsStandardValue,
4672 TableEntry, EnumOffset);
4673
4674 return DAG.getMergeValues({Result, GetReg.getValue(1)}, SL);
4675}
4676
4678 SelectionDAG &DAG) const {
4679 SDLoc SL(Op);
4680
4681 SDValue NewMode = Op.getOperand(1);
4682 assert(NewMode.getValueType() == MVT::i32);
4683
4684 // Index a table of 4-bit entries mapping from the C FLT_ROUNDS values to the
4685 // hardware MODE.fp_round values.
4686 if (auto *ConstMode = dyn_cast<ConstantSDNode>(NewMode)) {
4687 uint32_t ClampedVal = std::min(
4688 static_cast<uint32_t>(ConstMode->getZExtValue()),
4690 NewMode = DAG.getConstant(
4691 AMDGPU::decodeFltRoundToHWConversionTable(ClampedVal), SL, MVT::i32);
4692 } else {
4693 // If we know the input can only be one of the supported standard modes in
4694 // the range 0-3, we can use a simplified mapping to hardware values.
4695 KnownBits KB = DAG.computeKnownBits(NewMode);
4696 const bool UseReducedTable = KB.countMinLeadingZeros() >= 30;
4697 // The supported standard values are 0-3. The extended values start at 8. We
4698 // need to offset by 4 if the value is in the extended range.
4699
4700 if (UseReducedTable) {
4701 // Truncate to the low 32-bits.
4702 SDValue BitTable = DAG.getConstant(
4703 AMDGPU::FltRoundToHWConversionTable & 0xffff, SL, MVT::i32);
4704
4705 SDValue Two = DAG.getConstant(2, SL, MVT::i32);
4706 SDValue RoundModeTimesNumBits =
4707 DAG.getNode(ISD::SHL, SL, MVT::i32, NewMode, Two);
4708
4709 NewMode =
4710 DAG.getNode(ISD::SRL, SL, MVT::i32, BitTable, RoundModeTimesNumBits);
4711
4712 // TODO: SimplifyDemandedBits on the setreg source here can likely reduce
4713 // the table extracted bits into inline immediates.
4714 } else {
4715 // table_index = umin(value, value - 4)
4716 // MODE.fp_round = (bit_table >> (table_index << 2)) & 0xf
4717 SDValue BitTable =
4719
4720 SDValue Four = DAG.getConstant(4, SL, MVT::i32);
4721 SDValue OffsetEnum = DAG.getNode(ISD::SUB, SL, MVT::i32, NewMode, Four);
4722 SDValue IndexVal =
4723 DAG.getNode(ISD::UMIN, SL, MVT::i32, NewMode, OffsetEnum);
4724
4725 SDValue Two = DAG.getConstant(2, SL, MVT::i32);
4726 SDValue RoundModeTimesNumBits =
4727 DAG.getNode(ISD::SHL, SL, MVT::i32, IndexVal, Two);
4728
4729 SDValue TableValue =
4730 DAG.getNode(ISD::SRL, SL, MVT::i64, BitTable, RoundModeTimesNumBits);
4731 SDValue TruncTable = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, TableValue);
4732
4733 // No need to mask out the high bits since the setreg will ignore them
4734 // anyway.
4735 NewMode = TruncTable;
4736 }
4737
4738 // Insert a readfirstlane in case the value is a VGPR. We could do this
4739 // earlier and keep more operations scalar, but that interferes with
4740 // combining the source.
4741 SDValue ReadFirstLaneID =
4742 DAG.getTargetConstant(Intrinsic::amdgcn_readfirstlane, SL, MVT::i32);
4743 NewMode = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SL, MVT::i32,
4744 ReadFirstLaneID, NewMode);
4745 }
4746
4747 // N.B. The setreg will be later folded into s_round_mode on supported
4748 // targets.
4749 SDValue IntrinID =
4750 DAG.getTargetConstant(Intrinsic::amdgcn_s_setreg, SL, MVT::i32);
4751 uint32_t BothRoundHwReg =
4753 SDValue RoundBothImm = DAG.getTargetConstant(BothRoundHwReg, SL, MVT::i32);
4754
4755 SDValue SetReg =
4756 DAG.getNode(ISD::INTRINSIC_VOID, SL, Op->getVTList(), Op.getOperand(0),
4757 IntrinID, RoundBothImm, NewMode);
4758
4759 return SetReg;
4760}
4761
4763 if (Op->isDivergent() &&
4764 (!Subtarget->hasVmemPrefInsts() || !Op.getConstantOperandVal(4)))
4765 // Cannot do I$ prefetch with divergent pointer.
4766 return SDValue();
4767
4768 switch (cast<MemSDNode>(Op)->getAddressSpace()) {
4772 break;
4774 if (Subtarget->hasSafeSmemPrefetch())
4775 break;
4776 [[fallthrough]];
4777 default:
4778 return SDValue();
4779 }
4780
4781 // I$ prefetch
4782 if (!Subtarget->hasSafeSmemPrefetch() && !Op.getConstantOperandVal(4))
4783 return SDValue();
4784
4785 return Op;
4786}
4787
4788// Work around DAG legality rules only based on the result type.
4790 bool IsStrict = Op.getOpcode() == ISD::STRICT_FP_EXTEND;
4791 SDValue Src = Op.getOperand(IsStrict ? 1 : 0);
4792 EVT SrcVT = Src.getValueType();
4793
4794 if (SrcVT.getScalarType() != MVT::bf16)
4795 return Op;
4796
4797 SDLoc SL(Op);
4798 SDValue BitCast =
4799 DAG.getNode(ISD::BITCAST, SL, SrcVT.changeTypeToInteger(), Src);
4800
4801 EVT DstVT = Op.getValueType();
4802 if (IsStrict)
4803 llvm_unreachable("Need STRICT_BF16_TO_FP");
4804
4805 return DAG.getNode(ISD::BF16_TO_FP, SL, DstVT, BitCast);
4806}
4807
4809 SDLoc SL(Op);
4810 if (Op.getValueType() != MVT::i64)
4811 return Op;
4812
4813 uint32_t ModeHwReg =
4815 SDValue ModeHwRegImm = DAG.getTargetConstant(ModeHwReg, SL, MVT::i32);
4816 uint32_t TrapHwReg =
4818 SDValue TrapHwRegImm = DAG.getTargetConstant(TrapHwReg, SL, MVT::i32);
4819
4820 SDVTList VTList = DAG.getVTList(MVT::i32, MVT::Other);
4821 SDValue IntrinID =
4822 DAG.getTargetConstant(Intrinsic::amdgcn_s_getreg, SL, MVT::i32);
4823 SDValue GetModeReg = DAG.getNode(ISD::INTRINSIC_W_CHAIN, SL, VTList,
4824 Op.getOperand(0), IntrinID, ModeHwRegImm);
4825 SDValue GetTrapReg = DAG.getNode(ISD::INTRINSIC_W_CHAIN, SL, VTList,
4826 Op.getOperand(0), IntrinID, TrapHwRegImm);
4827 SDValue TokenReg =
4828 DAG.getNode(ISD::TokenFactor, SL, MVT::Other, GetModeReg.getValue(1),
4829 GetTrapReg.getValue(1));
4830
4831 SDValue CvtPtr =
4832 DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, GetModeReg, GetTrapReg);
4833 SDValue Result = DAG.getNode(ISD::BITCAST, SL, MVT::i64, CvtPtr);
4834
4835 return DAG.getMergeValues({Result, TokenReg}, SL);
4836}
4837
4839 SDLoc SL(Op);
4840 if (Op.getOperand(1).getValueType() != MVT::i64)
4841 return Op;
4842
4843 SDValue Input = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op.getOperand(1));
4844 SDValue NewModeReg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Input,
4845 DAG.getConstant(0, SL, MVT::i32));
4846 SDValue NewTrapReg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Input,
4847 DAG.getConstant(1, SL, MVT::i32));
4848
4849 SDValue ReadFirstLaneID =
4850 DAG.getTargetConstant(Intrinsic::amdgcn_readfirstlane, SL, MVT::i32);
4851 NewModeReg = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SL, MVT::i32,
4852 ReadFirstLaneID, NewModeReg);
4853 NewTrapReg = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SL, MVT::i32,
4854 ReadFirstLaneID, NewTrapReg);
4855
4856 unsigned ModeHwReg =
4858 SDValue ModeHwRegImm = DAG.getTargetConstant(ModeHwReg, SL, MVT::i32);
4859 unsigned TrapHwReg =
4861 SDValue TrapHwRegImm = DAG.getTargetConstant(TrapHwReg, SL, MVT::i32);
4862
4863 SDValue IntrinID =
4864 DAG.getTargetConstant(Intrinsic::amdgcn_s_setreg, SL, MVT::i32);
4865 SDValue SetModeReg =
4866 DAG.getNode(ISD::INTRINSIC_VOID, SL, MVT::Other, Op.getOperand(0),
4867 IntrinID, ModeHwRegImm, NewModeReg);
4868 SDValue SetTrapReg =
4869 DAG.getNode(ISD::INTRINSIC_VOID, SL, MVT::Other, Op.getOperand(0),
4870 IntrinID, TrapHwRegImm, NewTrapReg);
4871 return DAG.getNode(ISD::TokenFactor, SL, MVT::Other, SetTrapReg, SetModeReg);
4872}
4873
4875 const MachineFunction &MF) const {
4876 const Function &Fn = MF.getFunction();
4877
4879 .Case("m0", AMDGPU::M0)
4880 .Case("exec", AMDGPU::EXEC)
4881 .Case("exec_lo", AMDGPU::EXEC_LO)
4882 .Case("exec_hi", AMDGPU::EXEC_HI)
4883 .Case("flat_scratch", AMDGPU::FLAT_SCR)
4884 .Case("flat_scratch_lo", AMDGPU::FLAT_SCR_LO)
4885 .Case("flat_scratch_hi", AMDGPU::FLAT_SCR_HI)
4886 .Default(Register());
4887 if (!Reg)
4888 return Reg;
4889
4890 if (!Subtarget->hasFlatScrRegister() &&
4891 Subtarget->getRegisterInfo()->regsOverlap(Reg, AMDGPU::FLAT_SCR)) {
4892 Fn.getContext().emitError(Twine("invalid register \"" + StringRef(RegName) +
4893 "\" for subtarget."));
4894 }
4895
4896 switch (Reg) {
4897 case AMDGPU::M0:
4898 case AMDGPU::EXEC_LO:
4899 case AMDGPU::EXEC_HI:
4900 case AMDGPU::FLAT_SCR_LO:
4901 case AMDGPU::FLAT_SCR_HI:
4902 if (VT.getSizeInBits() == 32)
4903 return Reg;
4904 break;
4905 case AMDGPU::EXEC:
4906 case AMDGPU::FLAT_SCR:
4907 if (VT.getSizeInBits() == 64)
4908 return Reg;
4909 break;
4910 default:
4911 llvm_unreachable("missing register type checking");
4912 }
4913
4915 Twine("invalid type for register \"" + StringRef(RegName) + "\"."));
4916}
4917
4918// If kill is not the last instruction, split the block so kill is always a
4919// proper terminator.
4922 MachineBasicBlock *BB) const {
4923 MachineBasicBlock *SplitBB = BB->splitAt(MI, /*UpdateLiveIns=*/true);
4925 MI.setDesc(TII->getKillTerminatorFromPseudo(MI.getOpcode()));
4926 return SplitBB;
4927}
4928
4929// Split block \p MBB at \p MI, as to insert a loop. If \p InstInLoop is true,
4930// \p MI will be the only instruction in the loop body block. Otherwise, it will
4931// be the first instruction in the remainder block.
4932//
4933/// \returns { LoopBody, Remainder }
4934static std::pair<MachineBasicBlock *, MachineBasicBlock *>
4936 MachineFunction *MF = MBB.getParent();
4938
4939 // To insert the loop we need to split the block. Move everything after this
4940 // point to a new block, and insert a new empty block between the two.
4942 MachineBasicBlock *RemainderBB = MF->CreateMachineBasicBlock();
4944 ++MBBI;
4945
4946 MF->insert(MBBI, LoopBB);
4947 MF->insert(MBBI, RemainderBB);
4948
4949 LoopBB->addSuccessor(LoopBB);
4950 LoopBB->addSuccessor(RemainderBB);
4951
4952 // Move the rest of the block into a new block.
4953 RemainderBB->transferSuccessorsAndUpdatePHIs(&MBB);
4954
4955 if (InstInLoop) {
4956 auto Next = std::next(I);
4957
4958 // Move instruction to loop body.
4959 LoopBB->splice(LoopBB->begin(), &MBB, I, Next);
4960
4961 // Move the rest of the block.
4962 RemainderBB->splice(RemainderBB->begin(), &MBB, Next, MBB.end());
4963 } else {
4964 RemainderBB->splice(RemainderBB->begin(), &MBB, I, MBB.end());
4965 }
4966
4967 MBB.addSuccessor(LoopBB);
4968
4969 return std::pair(LoopBB, RemainderBB);
4970}
4971
4972/// Insert \p MI into a BUNDLE with an S_WAITCNT 0 immediately following it.
4974 MachineBasicBlock *MBB = MI.getParent();
4976 auto I = MI.getIterator();
4977 auto E = std::next(I);
4978
4979 // clang-format off
4980 BuildMI(*MBB, E, MI.getDebugLoc(), TII->get(AMDGPU::S_WAITCNT))
4981 .addImm(0);
4982 // clang-format on
4983
4984 MIBundleBuilder Bundler(*MBB, I, E);
4985 finalizeBundle(*MBB, Bundler.begin());
4986}
4987
4990 MachineBasicBlock *BB) const {
4991 const DebugLoc &DL = MI.getDebugLoc();
4992
4994
4996
4997 // Apparently kill flags are only valid if the def is in the same block?
4998 if (MachineOperand *Src = TII->getNamedOperand(MI, AMDGPU::OpName::data0))
4999 Src->setIsKill(false);
5000
5001 auto [LoopBB, RemainderBB] = splitBlockForLoop(MI, *BB, true);
5002
5003 MachineBasicBlock::iterator I = LoopBB->end();
5004
5005 const unsigned EncodedReg = AMDGPU::Hwreg::HwregEncoding::encode(
5007
5008 // Clear TRAP_STS.MEM_VIOL
5009 BuildMI(*LoopBB, LoopBB->begin(), DL, TII->get(AMDGPU::S_SETREG_IMM32_B32))
5010 .addImm(0)
5011 .addImm(EncodedReg);
5012
5014
5015 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
5016
5017 // Load and check TRAP_STS.MEM_VIOL
5018 BuildMI(*LoopBB, I, DL, TII->get(AMDGPU::S_GETREG_B32), Reg)
5019 .addImm(EncodedReg);
5020
5021 // FIXME: Do we need to use an isel pseudo that may clobber scc?
5022 BuildMI(*LoopBB, I, DL, TII->get(AMDGPU::S_CMP_LG_U32))
5023 .addReg(Reg, RegState::Kill)
5024 .addImm(0);
5025 // clang-format off
5026 BuildMI(*LoopBB, I, DL, TII->get(AMDGPU::S_CBRANCH_SCC1))
5027 .addMBB(LoopBB);
5028 // clang-format on
5029
5030 return RemainderBB;
5031}
5032
5033// Do a v_movrels_b32 or v_movreld_b32 for each unique value of \p IdxReg in the
5034// wavefront. If the value is uniform and just happens to be in a VGPR, this
5035// will only do one iteration. In the worst case, this will loop 64 times.
5036//
5037// TODO: Just use v_readlane_b32 if we know the VGPR has a uniform value.
5040 MachineBasicBlock &OrigBB, MachineBasicBlock &LoopBB,
5041 const DebugLoc &DL, const MachineOperand &Idx,
5042 unsigned InitReg, unsigned ResultReg, unsigned PhiReg,
5043 unsigned InitSaveExecReg, int Offset, bool UseGPRIdxMode,
5044 Register &SGPRIdxReg) {
5045
5046 MachineFunction *MF = OrigBB.getParent();
5047 const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>();
5048 const SIRegisterInfo *TRI = ST.getRegisterInfo();
5051
5052 const TargetRegisterClass *BoolRC = TRI->getBoolRC();
5053 Register PhiExec = MRI.createVirtualRegister(BoolRC);
5054 Register NewExec = MRI.createVirtualRegister(BoolRC);
5055 Register CurrentIdxReg =
5056 MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
5057 Register CondReg = MRI.createVirtualRegister(BoolRC);
5058
5059 BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiReg)
5060 .addReg(InitReg)
5061 .addMBB(&OrigBB)
5062 .addReg(ResultReg)
5063 .addMBB(&LoopBB);
5064
5065 BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiExec)
5066 .addReg(InitSaveExecReg)
5067 .addMBB(&OrigBB)
5068 .addReg(NewExec)
5069 .addMBB(&LoopBB);
5070
5071 // Read the next variant <- also loop target.
5072 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32), CurrentIdxReg)
5073 .addReg(Idx.getReg(), getUndefRegState(Idx.isUndef()));
5074
5075 // Compare the just read M0 value to all possible Idx values.
5076 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_CMP_EQ_U32_e64), CondReg)
5077 .addReg(CurrentIdxReg)
5078 .addReg(Idx.getReg(), 0, Idx.getSubReg());
5079
5080 // Update EXEC, save the original EXEC value to VCC.
5081 BuildMI(LoopBB, I, DL, TII->get(LMC.AndSaveExecOpc), NewExec)
5082 .addReg(CondReg, RegState::Kill);
5083
5084 MRI.setSimpleHint(NewExec, CondReg);
5085
5086 if (UseGPRIdxMode) {
5087 if (Offset == 0) {
5088 SGPRIdxReg = CurrentIdxReg;
5089 } else {
5090 SGPRIdxReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
5091 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_ADD_I32), SGPRIdxReg)
5092 .addReg(CurrentIdxReg, RegState::Kill)
5093 .addImm(Offset);
5094 }
5095 } else {
5096 // Move index from VCC into M0
5097 if (Offset == 0) {
5098 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::COPY), AMDGPU::M0)
5099 .addReg(CurrentIdxReg, RegState::Kill);
5100 } else {
5101 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
5102 .addReg(CurrentIdxReg, RegState::Kill)
5103 .addImm(Offset);
5104 }
5105 }
5106
5107 // Update EXEC, switch all done bits to 0 and all todo bits to 1.
5108 MachineInstr *InsertPt =
5109 BuildMI(LoopBB, I, DL, TII->get(LMC.XorTermOpc), LMC.ExecReg)
5110 .addReg(LMC.ExecReg)
5111 .addReg(NewExec);
5112
5113 // XXX - s_xor_b64 sets scc to 1 if the result is nonzero, so can we use
5114 // s_cbranch_scc0?
5115
5116 // Loop back to V_READFIRSTLANE_B32 if there are still variants to cover.
5117 // clang-format off
5118 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
5119 .addMBB(&LoopBB);
5120 // clang-format on
5121
5122 return InsertPt->getIterator();
5123}
5124
5125// This has slightly sub-optimal regalloc when the source vector is killed by
5126// the read. The register allocator does not understand that the kill is
5127// per-workitem, so is kept alive for the whole loop so we end up not re-using a
5128// subregister from it, using 1 more VGPR than necessary. This was saved when
5129// this was expanded after register allocation.
5132 unsigned InitResultReg, unsigned PhiReg, int Offset,
5133 bool UseGPRIdxMode, Register &SGPRIdxReg) {
5134 MachineFunction *MF = MBB.getParent();
5135 const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>();
5136 const SIRegisterInfo *TRI = ST.getRegisterInfo();
5138 const DebugLoc &DL = MI.getDebugLoc();
5140
5141 const auto *BoolXExecRC = TRI->getWaveMaskRegClass();
5142 Register DstReg = MI.getOperand(0).getReg();
5143 Register SaveExec = MRI.createVirtualRegister(BoolXExecRC);
5144 Register TmpExec = MRI.createVirtualRegister(BoolXExecRC);
5146
5147 BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), TmpExec);
5148
5149 // Save the EXEC mask
5150 // clang-format off
5151 BuildMI(MBB, I, DL, TII->get(LMC.MovOpc), SaveExec)
5152 .addReg(LMC.ExecReg);
5153 // clang-format on
5154
5155 auto [LoopBB, RemainderBB] = splitBlockForLoop(MI, MBB, false);
5156
5157 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
5158
5159 auto InsPt = emitLoadM0FromVGPRLoop(TII, MRI, MBB, *LoopBB, DL, *Idx,
5160 InitResultReg, DstReg, PhiReg, TmpExec,
5161 Offset, UseGPRIdxMode, SGPRIdxReg);
5162
5163 MachineBasicBlock *LandingPad = MF->CreateMachineBasicBlock();
5165 ++MBBI;
5166 MF->insert(MBBI, LandingPad);
5167 LoopBB->removeSuccessor(RemainderBB);
5168 LandingPad->addSuccessor(RemainderBB);
5169 LoopBB->addSuccessor(LandingPad);
5170 MachineBasicBlock::iterator First = LandingPad->begin();
5171 // clang-format off
5172 BuildMI(*LandingPad, First, DL, TII->get(LMC.MovOpc), LMC.ExecReg)
5173 .addReg(SaveExec);
5174 // clang-format on
5175
5176 return InsPt;
5177}
5178
5179// Returns subreg index, offset
5180static std::pair<unsigned, int>
5182 const TargetRegisterClass *SuperRC, unsigned VecReg,
5183 int Offset) {
5184 int NumElts = TRI.getRegSizeInBits(*SuperRC) / 32;
5185
5186 // Skip out of bounds offsets, or else we would end up using an undefined
5187 // register.
5188 if (Offset >= NumElts || Offset < 0)
5189 return std::pair(AMDGPU::sub0, Offset);
5190
5191 return std::pair(SIRegisterInfo::getSubRegFromChannel(Offset), 0);
5192}
5193
5196 int Offset) {
5197 MachineBasicBlock *MBB = MI.getParent();
5198 const DebugLoc &DL = MI.getDebugLoc();
5200
5201 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
5202
5203 assert(Idx->getReg() != AMDGPU::NoRegister);
5204
5205 if (Offset == 0) {
5206 // clang-format off
5207 BuildMI(*MBB, I, DL, TII->get(AMDGPU::COPY), AMDGPU::M0)
5208 .add(*Idx);
5209 // clang-format on
5210 } else {
5211 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
5212 .add(*Idx)
5213 .addImm(Offset);
5214 }
5215}
5216
5219 int Offset) {
5220 MachineBasicBlock *MBB = MI.getParent();
5221 const DebugLoc &DL = MI.getDebugLoc();
5223
5224 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
5225
5226 if (Offset == 0)
5227 return Idx->getReg();
5228
5229 Register Tmp = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
5230 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_ADD_I32), Tmp)
5231 .add(*Idx)
5232 .addImm(Offset);
5233 return Tmp;
5234}
5235
5238 const GCNSubtarget &ST) {
5239 const SIInstrInfo *TII = ST.getInstrInfo();
5240 const SIRegisterInfo &TRI = TII->getRegisterInfo();
5241 MachineFunction *MF = MBB.getParent();
5243
5244 Register Dst = MI.getOperand(0).getReg();
5245 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
5246 Register SrcReg = TII->getNamedOperand(MI, AMDGPU::OpName::src)->getReg();
5247 int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm();
5248
5249 const TargetRegisterClass *VecRC = MRI.getRegClass(SrcReg);
5250 const TargetRegisterClass *IdxRC = MRI.getRegClass(Idx->getReg());
5251
5252 unsigned SubReg;
5253 std::tie(SubReg, Offset) =
5254 computeIndirectRegAndOffset(TRI, VecRC, SrcReg, Offset);
5255
5256 const bool UseGPRIdxMode = ST.useVGPRIndexMode();
5257
5258 // Check for a SGPR index.
5259 if (TII->getRegisterInfo().isSGPRClass(IdxRC)) {
5261 const DebugLoc &DL = MI.getDebugLoc();
5262
5263 if (UseGPRIdxMode) {
5264 // TODO: Look at the uses to avoid the copy. This may require rescheduling
5265 // to avoid interfering with other uses, so probably requires a new
5266 // optimization pass.
5268
5269 const MCInstrDesc &GPRIDXDesc =
5270 TII->getIndirectGPRIDXPseudo(TRI.getRegSizeInBits(*VecRC), true);
5271 BuildMI(MBB, I, DL, GPRIDXDesc, Dst)
5272 .addReg(SrcReg)
5273 .addReg(Idx)
5274 .addImm(SubReg);
5275 } else {
5277
5278 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
5279 .addReg(SrcReg, 0, SubReg)
5280 .addReg(SrcReg, RegState::Implicit);
5281 }
5282
5283 MI.eraseFromParent();
5284
5285 return &MBB;
5286 }
5287
5288 // Control flow needs to be inserted if indexing with a VGPR.
5289 const DebugLoc &DL = MI.getDebugLoc();
5291
5292 Register PhiReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
5293 Register InitReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
5294
5295 BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), InitReg);
5296
5297 Register SGPRIdxReg;
5298 auto InsPt = loadM0FromVGPR(TII, MBB, MI, InitReg, PhiReg, Offset,
5299 UseGPRIdxMode, SGPRIdxReg);
5300
5301 MachineBasicBlock *LoopBB = InsPt->getParent();
5302
5303 if (UseGPRIdxMode) {
5304 const MCInstrDesc &GPRIDXDesc =
5305 TII->getIndirectGPRIDXPseudo(TRI.getRegSizeInBits(*VecRC), true);
5306
5307 BuildMI(*LoopBB, InsPt, DL, GPRIDXDesc, Dst)
5308 .addReg(SrcReg)
5309 .addReg(SGPRIdxReg)
5310 .addImm(SubReg);
5311 } else {
5312 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
5313 .addReg(SrcReg, 0, SubReg)
5314 .addReg(SrcReg, RegState::Implicit);
5315 }
5316
5317 MI.eraseFromParent();
5318
5319 return LoopBB;
5320}
5321
5324 const GCNSubtarget &ST) {
5325 const SIInstrInfo *TII = ST.getInstrInfo();
5326 const SIRegisterInfo &TRI = TII->getRegisterInfo();
5327 MachineFunction *MF = MBB.getParent();
5329
5330 Register Dst = MI.getOperand(0).getReg();
5331 const MachineOperand *SrcVec = TII->getNamedOperand(MI, AMDGPU::OpName::src);
5332 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
5333 const MachineOperand *Val = TII->getNamedOperand(MI, AMDGPU::OpName::val);
5334 int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm();
5335 const TargetRegisterClass *VecRC = MRI.getRegClass(SrcVec->getReg());
5336 const TargetRegisterClass *IdxRC = MRI.getRegClass(Idx->getReg());
5337
5338 // This can be an immediate, but will be folded later.
5339 assert(Val->getReg());
5340
5341 unsigned SubReg;
5342 std::tie(SubReg, Offset) =
5343 computeIndirectRegAndOffset(TRI, VecRC, SrcVec->getReg(), Offset);
5344 const bool UseGPRIdxMode = ST.useVGPRIndexMode();
5345
5346 if (Idx->getReg() == AMDGPU::NoRegister) {
5348 const DebugLoc &DL = MI.getDebugLoc();
5349
5350 assert(Offset == 0);
5351
5352 BuildMI(MBB, I, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dst)
5353 .add(*SrcVec)
5354 .add(*Val)
5355 .addImm(SubReg);
5356
5357 MI.eraseFromParent();
5358 return &MBB;
5359 }
5360
5361 // Check for a SGPR index.
5362 if (TII->getRegisterInfo().isSGPRClass(IdxRC)) {
5364 const DebugLoc &DL = MI.getDebugLoc();
5365
5366 if (UseGPRIdxMode) {
5368
5369 const MCInstrDesc &GPRIDXDesc =
5370 TII->getIndirectGPRIDXPseudo(TRI.getRegSizeInBits(*VecRC), false);
5371 BuildMI(MBB, I, DL, GPRIDXDesc, Dst)
5372 .addReg(SrcVec->getReg())
5373 .add(*Val)
5374 .addReg(Idx)
5375 .addImm(SubReg);
5376 } else {
5378
5379 const MCInstrDesc &MovRelDesc = TII->getIndirectRegWriteMovRelPseudo(
5380 TRI.getRegSizeInBits(*VecRC), 32, false);
5381 BuildMI(MBB, I, DL, MovRelDesc, Dst)
5382 .addReg(SrcVec->getReg())
5383 .add(*Val)
5384 .addImm(SubReg);
5385 }
5386 MI.eraseFromParent();
5387 return &MBB;
5388 }
5389
5390 // Control flow needs to be inserted if indexing with a VGPR.
5391 if (Val->isReg())
5392 MRI.clearKillFlags(Val->getReg());
5393
5394 const DebugLoc &DL = MI.getDebugLoc();
5395
5396 Register PhiReg = MRI.createVirtualRegister(VecRC);
5397
5398 Register SGPRIdxReg;
5399 auto InsPt = loadM0FromVGPR(TII, MBB, MI, SrcVec->getReg(), PhiReg, Offset,
5400 UseGPRIdxMode, SGPRIdxReg);
5401 MachineBasicBlock *LoopBB = InsPt->getParent();
5402
5403 if (UseGPRIdxMode) {
5404 const MCInstrDesc &GPRIDXDesc =
5405 TII->getIndirectGPRIDXPseudo(TRI.getRegSizeInBits(*VecRC), false);
5406
5407 BuildMI(*LoopBB, InsPt, DL, GPRIDXDesc, Dst)
5408 .addReg(PhiReg)
5409 .add(*Val)
5410 .addReg(SGPRIdxReg)
5411 .addImm(SubReg);
5412 } else {
5413 const MCInstrDesc &MovRelDesc = TII->getIndirectRegWriteMovRelPseudo(
5414 TRI.getRegSizeInBits(*VecRC), 32, false);
5415 BuildMI(*LoopBB, InsPt, DL, MovRelDesc, Dst)
5416 .addReg(PhiReg)
5417 .add(*Val)
5418 .addImm(SubReg);
5419 }
5420
5421 MI.eraseFromParent();
5422 return LoopBB;
5423}
5424
5426 MachineBasicBlock *BB) {
5427 // For targets older than GFX12, we emit a sequence of 32-bit operations.
5428 // For GFX12, we emit s_add_u64 and s_sub_u64.
5429 MachineFunction *MF = BB->getParent();
5430 const SIInstrInfo *TII = MF->getSubtarget<GCNSubtarget>().getInstrInfo();
5431 const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>();
5433 const DebugLoc &DL = MI.getDebugLoc();
5434 MachineOperand &Dest = MI.getOperand(0);
5435 MachineOperand &Src0 = MI.getOperand(1);
5436 MachineOperand &Src1 = MI.getOperand(2);
5437 bool IsAdd = (MI.getOpcode() == AMDGPU::S_ADD_U64_PSEUDO);
5438 if (ST.hasScalarAddSub64()) {
5439 unsigned Opc = IsAdd ? AMDGPU::S_ADD_U64 : AMDGPU::S_SUB_U64;
5440 // clang-format off
5441 BuildMI(*BB, MI, DL, TII->get(Opc), Dest.getReg())
5442 .add(Src0)
5443 .add(Src1);
5444 // clang-format on
5445 } else {
5446 const SIRegisterInfo *TRI = ST.getRegisterInfo();
5447 const TargetRegisterClass *BoolRC = TRI->getBoolRC();
5448
5449 Register DestSub0 = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
5450 Register DestSub1 = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
5451
5452 MachineOperand Src0Sub0 = TII->buildExtractSubRegOrImm(
5453 MI, MRI, Src0, BoolRC, AMDGPU::sub0, &AMDGPU::SReg_32RegClass);
5454 MachineOperand Src0Sub1 = TII->buildExtractSubRegOrImm(
5455 MI, MRI, Src0, BoolRC, AMDGPU::sub1, &AMDGPU::SReg_32RegClass);
5456
5457 MachineOperand Src1Sub0 = TII->buildExtractSubRegOrImm(
5458 MI, MRI, Src1, BoolRC, AMDGPU::sub0, &AMDGPU::SReg_32RegClass);
5459 MachineOperand Src1Sub1 = TII->buildExtractSubRegOrImm(
5460 MI, MRI, Src1, BoolRC, AMDGPU::sub1, &AMDGPU::SReg_32RegClass);
5461
5462 unsigned LoOpc = IsAdd ? AMDGPU::S_ADD_U32 : AMDGPU::S_SUB_U32;
5463 unsigned HiOpc = IsAdd ? AMDGPU::S_ADDC_U32 : AMDGPU::S_SUBB_U32;
5464 BuildMI(*BB, MI, DL, TII->get(LoOpc), DestSub0).add(Src0Sub0).add(Src1Sub0);
5465 BuildMI(*BB, MI, DL, TII->get(HiOpc), DestSub1).add(Src0Sub1).add(Src1Sub1);
5466 BuildMI(*BB, MI, DL, TII->get(TargetOpcode::REG_SEQUENCE), Dest.getReg())
5467 .addReg(DestSub0)
5468 .addImm(AMDGPU::sub0)
5469 .addReg(DestSub1)
5470 .addImm(AMDGPU::sub1);
5471 }
5472 MI.eraseFromParent();
5473 return BB;
5474}
5475
5477 switch (Opc) {
5478 case AMDGPU::S_MIN_U32:
5479 return std::numeric_limits<uint32_t>::max();
5480 case AMDGPU::S_MIN_I32:
5481 return std::numeric_limits<int32_t>::max();
5482 case AMDGPU::S_MAX_U32:
5483 return std::numeric_limits<uint32_t>::min();
5484 case AMDGPU::S_MAX_I32:
5485 return std::numeric_limits<int32_t>::min();
5486 case AMDGPU::V_ADD_F32_e64: // -0.0
5487 return 0x80000000;
5488 case AMDGPU::V_SUB_F32_e64: // +0.0
5489 return 0x0;
5490 case AMDGPU::S_ADD_I32:
5491 case AMDGPU::S_SUB_I32:
5492 case AMDGPU::S_OR_B32:
5493 case AMDGPU::S_XOR_B32:
5494 return std::numeric_limits<uint32_t>::min();
5495 case AMDGPU::S_AND_B32:
5496 return std::numeric_limits<uint32_t>::max();
5497 case AMDGPU::V_MIN_F32_e64:
5498 case AMDGPU::V_MAX_F32_e64:
5499 return 0x7fc00000; // qNAN
5500 default:
5502 "Unexpected opcode in getIdentityValueFor32BitWaveReduction");
5503 }
5504}
5505
5507 switch (Opc) {
5508 case AMDGPU::V_CMP_LT_U64_e64: // umin.u64
5509 return std::numeric_limits<uint64_t>::max();
5510 case AMDGPU::V_CMP_LT_I64_e64: // min.i64
5511 return std::numeric_limits<int64_t>::max();
5512 case AMDGPU::V_CMP_GT_U64_e64: // umax.u64
5513 return std::numeric_limits<uint64_t>::min();
5514 case AMDGPU::V_CMP_GT_I64_e64: // max.i64
5515 return std::numeric_limits<int64_t>::min();
5516 case AMDGPU::S_ADD_U64_PSEUDO:
5517 case AMDGPU::S_SUB_U64_PSEUDO:
5518 case AMDGPU::S_OR_B64:
5519 case AMDGPU::S_XOR_B64:
5520 return std::numeric_limits<uint64_t>::min();
5521 case AMDGPU::S_AND_B64:
5522 return std::numeric_limits<uint64_t>::max();
5523 default:
5525 "Unexpected opcode in getIdentityValueFor64BitWaveReduction");
5526 }
5527}
5528
5529static bool is32bitWaveReduceOperation(unsigned Opc) {
5530 return Opc == AMDGPU::S_MIN_U32 || Opc == AMDGPU::S_MIN_I32 ||
5531 Opc == AMDGPU::S_MAX_U32 || Opc == AMDGPU::S_MAX_I32 ||
5532 Opc == AMDGPU::S_ADD_I32 || Opc == AMDGPU::S_SUB_I32 ||
5533 Opc == AMDGPU::S_AND_B32 || Opc == AMDGPU::S_OR_B32 ||
5534 Opc == AMDGPU::S_XOR_B32 || Opc == AMDGPU::V_MIN_F32_e64 ||
5535 Opc == AMDGPU::V_MAX_F32_e64 || Opc == AMDGPU::V_ADD_F32_e64 ||
5536 Opc == AMDGPU::V_SUB_F32_e64;
5537}
5538
5540 return Opc == AMDGPU::V_MIN_F32_e64 || Opc == AMDGPU::V_MAX_F32_e64 ||
5541 Opc == AMDGPU::V_ADD_F32_e64 || Opc == AMDGPU::V_SUB_F32_e64;
5542}
5543
5546 const GCNSubtarget &ST,
5547 unsigned Opc) {
5549 const SIRegisterInfo *TRI = ST.getRegisterInfo();
5550 const DebugLoc &DL = MI.getDebugLoc();
5551 const SIInstrInfo *TII = ST.getInstrInfo();
5552
5553 // Reduction operations depend on whether the input operand is SGPR or VGPR.
5554 Register SrcReg = MI.getOperand(1).getReg();
5555 bool isSGPR = TRI->isSGPRClass(MRI.getRegClass(SrcReg));
5556 Register DstReg = MI.getOperand(0).getReg();
5557 MachineBasicBlock *RetBB = nullptr;
5558 if (isSGPR) {
5559 switch (Opc) {
5560 case AMDGPU::S_MIN_U32:
5561 case AMDGPU::S_MIN_I32:
5562 case AMDGPU::V_MIN_F32_e64:
5563 case AMDGPU::S_MAX_U32:
5564 case AMDGPU::S_MAX_I32:
5565 case AMDGPU::V_MAX_F32_e64:
5566 case AMDGPU::S_AND_B32:
5567 case AMDGPU::S_OR_B32: {
5568 // Idempotent operations.
5569 BuildMI(BB, MI, DL, TII->get(AMDGPU::S_MOV_B32), DstReg).addReg(SrcReg);
5570 RetBB = &BB;
5571 break;
5572 }
5573 case AMDGPU::V_CMP_LT_U64_e64: // umin
5574 case AMDGPU::V_CMP_LT_I64_e64: // min
5575 case AMDGPU::V_CMP_GT_U64_e64: // umax
5576 case AMDGPU::V_CMP_GT_I64_e64: // max
5577 case AMDGPU::S_AND_B64:
5578 case AMDGPU::S_OR_B64: {
5579 // Idempotent operations.
5580 BuildMI(BB, MI, DL, TII->get(AMDGPU::S_MOV_B64), DstReg).addReg(SrcReg);
5581 RetBB = &BB;
5582 break;
5583 }
5584 case AMDGPU::S_XOR_B32:
5585 case AMDGPU::S_XOR_B64:
5586 case AMDGPU::S_ADD_I32:
5587 case AMDGPU::S_ADD_U64_PSEUDO:
5588 case AMDGPU::V_ADD_F32_e64:
5589 case AMDGPU::S_SUB_I32:
5590 case AMDGPU::S_SUB_U64_PSEUDO:
5591 case AMDGPU::V_SUB_F32_e64: {
5592 const TargetRegisterClass *WaveMaskRegClass = TRI->getWaveMaskRegClass();
5593 const TargetRegisterClass *DstRegClass = MRI.getRegClass(DstReg);
5594 Register ExecMask = MRI.createVirtualRegister(WaveMaskRegClass);
5595 Register NumActiveLanes =
5596 MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
5597
5598 bool IsWave32 = ST.isWave32();
5599 unsigned MovOpc = IsWave32 ? AMDGPU::S_MOV_B32 : AMDGPU::S_MOV_B64;
5600 MCRegister ExecReg = IsWave32 ? AMDGPU::EXEC_LO : AMDGPU::EXEC;
5601 unsigned BitCountOpc =
5602 IsWave32 ? AMDGPU::S_BCNT1_I32_B32 : AMDGPU::S_BCNT1_I32_B64;
5603
5604 BuildMI(BB, MI, DL, TII->get(MovOpc), ExecMask).addReg(ExecReg);
5605
5606 auto NewAccumulator =
5607 BuildMI(BB, MI, DL, TII->get(BitCountOpc), NumActiveLanes)
5608 .addReg(ExecMask);
5609
5610 switch (Opc) {
5611 case AMDGPU::S_XOR_B32:
5612 case AMDGPU::S_XOR_B64: {
5613 // Performing an XOR operation on a uniform value
5614 // depends on the parity of the number of active lanes.
5615 // For even parity, the result will be 0, for odd
5616 // parity the result will be the same as the input value.
5617 Register ParityRegister =
5618 MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
5619
5620 BuildMI(BB, MI, DL, TII->get(AMDGPU::S_AND_B32), ParityRegister)
5621 .addReg(NewAccumulator->getOperand(0).getReg())
5622 .addImm(1)
5623 .setOperandDead(3); // Dead scc
5624 if (Opc == AMDGPU::S_XOR_B32) {
5625 BuildMI(BB, MI, DL, TII->get(AMDGPU::S_MUL_I32), DstReg)
5626 .addReg(SrcReg)
5627 .addReg(ParityRegister);
5628 } else {
5629 Register DestSub0 =
5630 MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
5631 Register DestSub1 =
5632 MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
5633
5634 const TargetRegisterClass *SrcRC = MRI.getRegClass(SrcReg);
5635 const TargetRegisterClass *SrcSubRC =
5636 TRI->getSubRegisterClass(SrcRC, AMDGPU::sub0);
5637
5638 MachineOperand Op1L = TII->buildExtractSubRegOrImm(
5639 MI, MRI, MI.getOperand(1), SrcRC, AMDGPU::sub0, SrcSubRC);
5640 MachineOperand Op1H = TII->buildExtractSubRegOrImm(
5641 MI, MRI, MI.getOperand(1), SrcRC, AMDGPU::sub1, SrcSubRC);
5642
5643 BuildMI(BB, MI, DL, TII->get(AMDGPU::S_MUL_I32), DestSub0)
5644 .add(Op1L)
5645 .addReg(ParityRegister);
5646
5647 BuildMI(BB, MI, DL, TII->get(AMDGPU::S_MUL_I32), DestSub1)
5648 .add(Op1H)
5649 .addReg(ParityRegister);
5650
5651 BuildMI(BB, MI, DL, TII->get(TargetOpcode::REG_SEQUENCE), DstReg)
5652 .addReg(DestSub0)
5653 .addImm(AMDGPU::sub0)
5654 .addReg(DestSub1)
5655 .addImm(AMDGPU::sub1);
5656 }
5657 break;
5658 }
5659 case AMDGPU::S_SUB_I32: {
5660 Register NegatedVal = MRI.createVirtualRegister(DstRegClass);
5661
5662 // Take the negation of the source operand.
5663 BuildMI(BB, MI, DL, TII->get(AMDGPU::S_SUB_I32), NegatedVal)
5664 .addImm(0)
5665 .addReg(SrcReg);
5666 BuildMI(BB, MI, DL, TII->get(AMDGPU::S_MUL_I32), DstReg)
5667 .addReg(NegatedVal)
5668 .addReg(NewAccumulator->getOperand(0).getReg());
5669 break;
5670 }
5671 case AMDGPU::S_ADD_I32: {
5672 BuildMI(BB, MI, DL, TII->get(AMDGPU::S_MUL_I32), DstReg)
5673 .addReg(SrcReg)
5674 .addReg(NewAccumulator->getOperand(0).getReg());
5675 break;
5676 }
5677 case AMDGPU::S_ADD_U64_PSEUDO:
5678 case AMDGPU::S_SUB_U64_PSEUDO: {
5679 Register DestSub0 = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
5680 Register DestSub1 = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
5681 Register Op1H_Op0L_Reg =
5682 MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
5683 Register Op1L_Op0H_Reg =
5684 MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
5685 Register CarryReg = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
5686 Register AddReg = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
5687 Register NegatedValLo =
5688 MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
5689 Register NegatedValHi =
5690 MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
5691
5692 const TargetRegisterClass *Src1RC = MRI.getRegClass(SrcReg);
5693 const TargetRegisterClass *Src1SubRC =
5694 TRI->getSubRegisterClass(Src1RC, AMDGPU::sub0);
5695
5696 MachineOperand Op1L = TII->buildExtractSubRegOrImm(
5697 MI, MRI, MI.getOperand(1), Src1RC, AMDGPU::sub0, Src1SubRC);
5698 MachineOperand Op1H = TII->buildExtractSubRegOrImm(
5699 MI, MRI, MI.getOperand(1), Src1RC, AMDGPU::sub1, Src1SubRC);
5700
5701 if (Opc == AMDGPU::S_SUB_U64_PSEUDO) {
5702 BuildMI(BB, MI, DL, TII->get(AMDGPU::S_SUB_I32), NegatedValLo)
5703 .addImm(0)
5704 .addReg(NewAccumulator->getOperand(0).getReg())
5705 .setOperandDead(3); // Dead scc
5706 BuildMI(BB, MI, DL, TII->get(AMDGPU::S_ASHR_I32), NegatedValHi)
5707 .addReg(NegatedValLo)
5708 .addImm(31)
5709 .setOperandDead(3); // Dead scc
5710 BuildMI(BB, MI, DL, TII->get(AMDGPU::S_MUL_I32), Op1L_Op0H_Reg)
5711 .add(Op1L)
5712 .addReg(NegatedValHi);
5713 }
5714 Register LowOpcode = Opc == AMDGPU::S_SUB_U64_PSEUDO
5715 ? NegatedValLo
5716 : NewAccumulator->getOperand(0).getReg();
5717 BuildMI(BB, MI, DL, TII->get(AMDGPU::S_MUL_I32), DestSub0)
5718 .add(Op1L)
5719 .addReg(LowOpcode);
5720 BuildMI(BB, MI, DL, TII->get(AMDGPU::S_MUL_HI_U32), CarryReg)
5721 .add(Op1L)
5722 .addReg(LowOpcode);
5723 BuildMI(BB, MI, DL, TII->get(AMDGPU::S_MUL_I32), Op1H_Op0L_Reg)
5724 .add(Op1H)
5725 .addReg(LowOpcode);
5726
5727 Register HiVal = Opc == AMDGPU::S_SUB_U64_PSEUDO ? AddReg : DestSub1;
5728 BuildMI(BB, MI, DL, TII->get(AMDGPU::S_ADD_U32), HiVal)
5729 .addReg(CarryReg)
5730 .addReg(Op1H_Op0L_Reg)
5731 .setOperandDead(3); // Dead scc
5732
5733 if (Opc == AMDGPU::S_SUB_U64_PSEUDO) {
5734 BuildMI(BB, MI, DL, TII->get(AMDGPU::S_ADD_U32), DestSub1)
5735 .addReg(HiVal)
5736 .addReg(Op1L_Op0H_Reg)
5737 .setOperandDead(3); // Dead scc
5738 }
5739 BuildMI(BB, MI, DL, TII->get(TargetOpcode::REG_SEQUENCE), DstReg)
5740 .addReg(DestSub0)
5741 .addImm(AMDGPU::sub0)
5742 .addReg(DestSub1)
5743 .addImm(AMDGPU::sub1);
5744 break;
5745 }
5746 case AMDGPU::V_ADD_F32_e64:
5747 case AMDGPU::V_SUB_F32_e64: {
5748 Register ActiveLanesVreg =
5749 MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
5750 Register DstVreg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
5751 // Get number of active lanes as a float val.
5752 BuildMI(BB, MI, DL, TII->get(AMDGPU::V_CVT_F32_I32_e64),
5753 ActiveLanesVreg)
5754 .addReg(NewAccumulator->getOperand(0).getReg())
5755 .addImm(0) // clamp
5756 .addImm(0); // output-modifier
5757
5758 // Take negation of input for SUB reduction
5759 unsigned srcMod = Opc == AMDGPU::V_SUB_F32_e64 ? 1 : 0;
5760 BuildMI(BB, MI, DL, TII->get(AMDGPU::V_MUL_F32_e64), DstVreg)
5761 .addImm(srcMod) // src0 modifier
5762 .addReg(SrcReg)
5763 .addImm(0) // src1 modifier
5764 .addReg(ActiveLanesVreg)
5765 .addImm(0) // clamp
5766 .addImm(0); // output-mod
5767 BuildMI(BB, MI, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32), DstReg)
5768 .addReg(DstVreg);
5769 }
5770 }
5771 RetBB = &BB;
5772 }
5773 }
5774 } else {
5775 // TODO: Implement DPP Strategy and switch based on immediate strategy
5776 // operand. For now, for all the cases (default, Iterative and DPP we use
5777 // iterative approach by default.)
5778
5779 // To reduce the VGPR using iterative approach, we need to iterate
5780 // over all the active lanes. Lowering consists of ComputeLoop,
5781 // which iterate over only active lanes. We use copy of EXEC register
5782 // as induction variable and every active lane modifies it using bitset0
5783 // so that we will get the next active lane for next iteration.
5785 Register SrcReg = MI.getOperand(1).getReg();
5786 bool is32BitOpc = is32bitWaveReduceOperation(Opc);
5788
5789 // Create Control flow for loop
5790 // Split MI's Machine Basic block into For loop
5791 auto [ComputeLoop, ComputeEnd] = splitBlockForLoop(MI, BB, true);
5792
5793 // Create virtual registers required for lowering.
5794 const TargetRegisterClass *WaveMaskRegClass = TRI->getWaveMaskRegClass();
5795 const TargetRegisterClass *DstRegClass = MRI.getRegClass(DstReg);
5796 Register LoopIterator = MRI.createVirtualRegister(WaveMaskRegClass);
5797 Register IdentityValReg = MRI.createVirtualRegister(DstRegClass);
5798 Register AccumulatorReg = MRI.createVirtualRegister(DstRegClass);
5799 Register ActiveBitsReg = MRI.createVirtualRegister(WaveMaskRegClass);
5800 Register NewActiveBitsReg = MRI.createVirtualRegister(WaveMaskRegClass);
5801 Register FF1Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
5802 Register LaneValueReg = MRI.createVirtualRegister(DstRegClass);
5803
5804 bool IsWave32 = ST.isWave32();
5805 unsigned MovOpcForExec = IsWave32 ? AMDGPU::S_MOV_B32 : AMDGPU::S_MOV_B64;
5806 unsigned ExecReg = IsWave32 ? AMDGPU::EXEC_LO : AMDGPU::EXEC;
5807
5808 // Create initial values of induction variable from Exec, Accumulator and
5809 // insert branch instr to newly created ComputeBlock
5810 BuildMI(BB, I, DL, TII->get(MovOpcForExec), LoopIterator).addReg(ExecReg);
5811 if (is32BitOpc) {
5813 BuildMI(BB, I, DL, TII->get(AMDGPU::S_MOV_B32), IdentityValReg)
5814 .addImm(IdentityValue);
5815 } else {
5817 BuildMI(BB, I, DL, TII->get(AMDGPU::S_MOV_B64_IMM_PSEUDO), IdentityValReg)
5818 .addImm(IdentityValue);
5819 }
5820 // clang-format off
5821 BuildMI(BB, I, DL, TII->get(AMDGPU::S_BRANCH))
5822 .addMBB(ComputeLoop);
5823 // clang-format on
5824
5825 // Start constructing ComputeLoop
5826 I = ComputeLoop->begin();
5827 auto Accumulator =
5828 BuildMI(*ComputeLoop, I, DL, TII->get(AMDGPU::PHI), AccumulatorReg)
5829 .addReg(IdentityValReg)
5830 .addMBB(&BB);
5831 auto ActiveBits =
5832 BuildMI(*ComputeLoop, I, DL, TII->get(AMDGPU::PHI), ActiveBitsReg)
5833 .addReg(LoopIterator)
5834 .addMBB(&BB);
5835
5836 I = ComputeLoop->end();
5837 MachineInstr *NewAccumulator;
5838 // Perform the computations
5839 unsigned SFFOpc = IsWave32 ? AMDGPU::S_FF1_I32_B32 : AMDGPU::S_FF1_I32_B64;
5840 BuildMI(*ComputeLoop, I, DL, TII->get(SFFOpc), FF1Reg)
5841 .addReg(ActiveBitsReg);
5842 if (is32BitOpc) {
5843 BuildMI(*ComputeLoop, I, DL, TII->get(AMDGPU::V_READLANE_B32),
5844 LaneValueReg)
5845 .addReg(SrcReg)
5846 .addReg(FF1Reg);
5847 if (isFPOp) {
5848 Register LaneValVreg =
5849 MRI.createVirtualRegister(MRI.getRegClass(SrcReg));
5850 Register DstVreg = MRI.createVirtualRegister(MRI.getRegClass(SrcReg));
5851 // Get the Lane Value in VGPR to avoid the Constant Bus Restriction
5852 BuildMI(*ComputeLoop, I, DL, TII->get(AMDGPU::V_MOV_B32_e32),
5853 LaneValVreg)
5854 .addReg(LaneValueReg);
5855 BuildMI(*ComputeLoop, I, DL, TII->get(Opc), DstVreg)
5856 .addImm(0) // src0 modifier
5857 .addReg(Accumulator->getOperand(0).getReg())
5858 .addImm(0) // src1 modifier
5859 .addReg(LaneValVreg)
5860 .addImm(0) // clamp
5861 .addImm(0); // omod
5862 NewAccumulator = BuildMI(*ComputeLoop, I, DL,
5863 TII->get(AMDGPU::V_READFIRSTLANE_B32), DstReg)
5864 .addReg(DstVreg);
5865 } else {
5866 NewAccumulator = BuildMI(*ComputeLoop, I, DL, TII->get(Opc), DstReg)
5867 .addReg(Accumulator->getOperand(0).getReg())
5868 .addReg(LaneValueReg);
5869 }
5870 } else {
5871 Register LaneValueLoReg =
5872 MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
5873 Register LaneValueHiReg =
5874 MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
5875 Register LaneValReg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
5876 const TargetRegisterClass *SrcRC = MRI.getRegClass(SrcReg);
5877 const TargetRegisterClass *SrcSubRC =
5878 TRI->getSubRegisterClass(SrcRC, AMDGPU::sub0);
5879 MachineOperand Op1L = TII->buildExtractSubRegOrImm(
5880 MI, MRI, MI.getOperand(1), SrcRC, AMDGPU::sub0, SrcSubRC);
5881 MachineOperand Op1H = TII->buildExtractSubRegOrImm(
5882 MI, MRI, MI.getOperand(1), SrcRC, AMDGPU::sub1, SrcSubRC);
5883 // lane value input should be in an sgpr
5884 BuildMI(*ComputeLoop, I, DL, TII->get(AMDGPU::V_READLANE_B32),
5885 LaneValueLoReg)
5886 .add(Op1L)
5887 .addReg(FF1Reg);
5888 BuildMI(*ComputeLoop, I, DL, TII->get(AMDGPU::V_READLANE_B32),
5889 LaneValueHiReg)
5890 .add(Op1H)
5891 .addReg(FF1Reg);
5892 auto LaneValue = BuildMI(*ComputeLoop, I, DL,
5893 TII->get(TargetOpcode::REG_SEQUENCE), LaneValReg)
5894 .addReg(LaneValueLoReg)
5895 .addImm(AMDGPU::sub0)
5896 .addReg(LaneValueHiReg)
5897 .addImm(AMDGPU::sub1);
5898 switch (Opc) {
5899 case AMDGPU::S_OR_B64:
5900 case AMDGPU::S_AND_B64:
5901 case AMDGPU::S_XOR_B64: {
5902 NewAccumulator = BuildMI(*ComputeLoop, I, DL, TII->get(Opc), DstReg)
5903 .addReg(Accumulator->getOperand(0).getReg())
5904 .addReg(LaneValue->getOperand(0).getReg())
5905 .setOperandDead(3); // Dead scc
5906 break;
5907 }
5908 case AMDGPU::V_CMP_GT_I64_e64:
5909 case AMDGPU::V_CMP_GT_U64_e64:
5910 case AMDGPU::V_CMP_LT_I64_e64:
5911 case AMDGPU::V_CMP_LT_U64_e64: {
5912 Register LaneMaskReg = MRI.createVirtualRegister(WaveMaskRegClass);
5913 Register ComparisonResultReg =
5914 MRI.createVirtualRegister(WaveMaskRegClass);
5915 const TargetRegisterClass *VregClass = TRI->getVGPR64Class();
5916 const TargetRegisterClass *VSubRegClass =
5917 TRI->getSubRegisterClass(VregClass, AMDGPU::sub0);
5918 Register AccumulatorVReg = MRI.createVirtualRegister(VregClass);
5919 MachineOperand SrcReg0Sub0 =
5920 TII->buildExtractSubRegOrImm(MI, MRI, Accumulator->getOperand(0),
5921 VregClass, AMDGPU::sub0, VSubRegClass);
5922 MachineOperand SrcReg0Sub1 =
5923 TII->buildExtractSubRegOrImm(MI, MRI, Accumulator->getOperand(0),
5924 VregClass, AMDGPU::sub1, VSubRegClass);
5925 BuildMI(*ComputeLoop, I, DL, TII->get(TargetOpcode::REG_SEQUENCE),
5926 AccumulatorVReg)
5927 .add(SrcReg0Sub0)
5928 .addImm(AMDGPU::sub0)
5929 .add(SrcReg0Sub1)
5930 .addImm(AMDGPU::sub1);
5931 BuildMI(*ComputeLoop, I, DL, TII->get(Opc), LaneMaskReg)
5932 .addReg(LaneValue->getOperand(0).getReg())
5933 .addReg(AccumulatorVReg);
5934
5935 unsigned AndOpc = IsWave32 ? AMDGPU::S_AND_B32 : AMDGPU::S_AND_B64;
5936 BuildMI(*ComputeLoop, I, DL, TII->get(AndOpc), ComparisonResultReg)
5937 .addReg(LaneMaskReg)
5938 .addReg(ActiveBitsReg);
5939
5940 NewAccumulator = BuildMI(*ComputeLoop, I, DL,
5941 TII->get(AMDGPU::S_CSELECT_B64), DstReg)
5942 .addReg(LaneValue->getOperand(0).getReg())
5943 .addReg(Accumulator->getOperand(0).getReg());
5944 break;
5945 }
5946 case AMDGPU::S_ADD_U64_PSEUDO:
5947 case AMDGPU::S_SUB_U64_PSEUDO: {
5948 NewAccumulator = BuildMI(*ComputeLoop, I, DL, TII->get(Opc), DstReg)
5949 .addReg(Accumulator->getOperand(0).getReg())
5950 .addReg(LaneValue->getOperand(0).getReg());
5951 ComputeLoop = Expand64BitScalarArithmetic(*NewAccumulator, ComputeLoop);
5952 break;
5953 }
5954 }
5955 }
5956 // Manipulate the iterator to get the next active lane
5957 unsigned BITSETOpc =
5958 IsWave32 ? AMDGPU::S_BITSET0_B32 : AMDGPU::S_BITSET0_B64;
5959 BuildMI(*ComputeLoop, I, DL, TII->get(BITSETOpc), NewActiveBitsReg)
5960 .addReg(FF1Reg)
5961 .addReg(ActiveBitsReg);
5962
5963 // Add phi nodes
5964 Accumulator.addReg(DstReg).addMBB(ComputeLoop);
5965 ActiveBits.addReg(NewActiveBitsReg).addMBB(ComputeLoop);
5966
5967 // Creating branching
5968 unsigned CMPOpc = IsWave32 ? AMDGPU::S_CMP_LG_U32 : AMDGPU::S_CMP_LG_U64;
5969 BuildMI(*ComputeLoop, I, DL, TII->get(CMPOpc))
5970 .addReg(NewActiveBitsReg)
5971 .addImm(0);
5972 BuildMI(*ComputeLoop, I, DL, TII->get(AMDGPU::S_CBRANCH_SCC1))
5973 .addMBB(ComputeLoop);
5974
5975 RetBB = ComputeEnd;
5976 }
5977 MI.eraseFromParent();
5978 return RetBB;
5979}
5980
5983 MachineBasicBlock *BB) const {
5984 MachineFunction *MF = BB->getParent();
5986 const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>();
5988 const SIRegisterInfo *TRI = Subtarget->getRegisterInfo();
5990 const DebugLoc &DL = MI.getDebugLoc();
5991
5992 switch (MI.getOpcode()) {
5993 case AMDGPU::WAVE_REDUCE_UMIN_PSEUDO_U32:
5994 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::S_MIN_U32);
5995 case AMDGPU::WAVE_REDUCE_UMIN_PSEUDO_U64:
5996 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::V_CMP_LT_U64_e64);
5997 case AMDGPU::WAVE_REDUCE_MIN_PSEUDO_I32:
5998 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::S_MIN_I32);
5999 case AMDGPU::WAVE_REDUCE_MIN_PSEUDO_I64:
6000 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::V_CMP_LT_I64_e64);
6001 case AMDGPU::WAVE_REDUCE_FMIN_PSEUDO_F32:
6002 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::V_MIN_F32_e64);
6003 case AMDGPU::WAVE_REDUCE_UMAX_PSEUDO_U32:
6004 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::S_MAX_U32);
6005 case AMDGPU::WAVE_REDUCE_UMAX_PSEUDO_U64:
6006 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::V_CMP_GT_U64_e64);
6007 case AMDGPU::WAVE_REDUCE_MAX_PSEUDO_I32:
6008 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::S_MAX_I32);
6009 case AMDGPU::WAVE_REDUCE_MAX_PSEUDO_I64:
6010 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::V_CMP_GT_I64_e64);
6011 case AMDGPU::WAVE_REDUCE_FMAX_PSEUDO_F32:
6012 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::V_MAX_F32_e64);
6013 case AMDGPU::WAVE_REDUCE_ADD_PSEUDO_I32:
6014 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::S_ADD_I32);
6015 case AMDGPU::WAVE_REDUCE_ADD_PSEUDO_U64:
6016 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::S_ADD_U64_PSEUDO);
6017 case AMDGPU::WAVE_REDUCE_FADD_PSEUDO_F32:
6018 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::V_ADD_F32_e64);
6019 case AMDGPU::WAVE_REDUCE_SUB_PSEUDO_I32:
6020 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::S_SUB_I32);
6021 case AMDGPU::WAVE_REDUCE_SUB_PSEUDO_U64:
6022 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::S_SUB_U64_PSEUDO);
6023 case AMDGPU::WAVE_REDUCE_FSUB_PSEUDO_F32:
6024 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::V_SUB_F32_e64);
6025 case AMDGPU::WAVE_REDUCE_AND_PSEUDO_B32:
6026 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::S_AND_B32);
6027 case AMDGPU::WAVE_REDUCE_AND_PSEUDO_B64:
6028 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::S_AND_B64);
6029 case AMDGPU::WAVE_REDUCE_OR_PSEUDO_B32:
6030 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::S_OR_B32);
6031 case AMDGPU::WAVE_REDUCE_OR_PSEUDO_B64:
6032 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::S_OR_B64);
6033 case AMDGPU::WAVE_REDUCE_XOR_PSEUDO_B32:
6034 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::S_XOR_B32);
6035 case AMDGPU::WAVE_REDUCE_XOR_PSEUDO_B64:
6036 return lowerWaveReduce(MI, *BB, *getSubtarget(), AMDGPU::S_XOR_B64);
6037 case AMDGPU::S_UADDO_PSEUDO:
6038 case AMDGPU::S_USUBO_PSEUDO: {
6039 MachineOperand &Dest0 = MI.getOperand(0);
6040 MachineOperand &Dest1 = MI.getOperand(1);
6041 MachineOperand &Src0 = MI.getOperand(2);
6042 MachineOperand &Src1 = MI.getOperand(3);
6043
6044 unsigned Opc = (MI.getOpcode() == AMDGPU::S_UADDO_PSEUDO)
6045 ? AMDGPU::S_ADD_U32
6046 : AMDGPU::S_SUB_U32;
6047 // clang-format off
6048 BuildMI(*BB, MI, DL, TII->get(Opc), Dest0.getReg())
6049 .add(Src0)
6050 .add(Src1);
6051 // clang-format on
6052
6053 unsigned SelOpc =
6054 Subtarget->isWave64() ? AMDGPU::S_CSELECT_B64 : AMDGPU::S_CSELECT_B32;
6055 BuildMI(*BB, MI, DL, TII->get(SelOpc), Dest1.getReg()).addImm(-1).addImm(0);
6056
6057 MI.eraseFromParent();
6058 return BB;
6059 }
6060 case AMDGPU::S_ADD_U64_PSEUDO:
6061 case AMDGPU::S_SUB_U64_PSEUDO: {
6062 return Expand64BitScalarArithmetic(MI, BB);
6063 }
6064 case AMDGPU::V_ADD_U64_PSEUDO:
6065 case AMDGPU::V_SUB_U64_PSEUDO: {
6066 bool IsAdd = (MI.getOpcode() == AMDGPU::V_ADD_U64_PSEUDO);
6067
6068 MachineOperand &Dest = MI.getOperand(0);
6069 MachineOperand &Src0 = MI.getOperand(1);
6070 MachineOperand &Src1 = MI.getOperand(2);
6071
6072 if (ST.hasAddSubU64Insts()) {
6073 auto I = BuildMI(*BB, MI, DL,
6074 TII->get(IsAdd ? AMDGPU::V_ADD_U64_e64
6075 : AMDGPU::V_SUB_U64_e64),
6076 Dest.getReg())
6077 .add(Src0)
6078 .add(Src1)
6079 .addImm(0); // clamp
6080 TII->legalizeOperands(*I);
6081 MI.eraseFromParent();
6082 return BB;
6083 }
6084
6085 if (IsAdd && ST.hasLshlAddU64Inst()) {
6086 auto Add = BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_LSHL_ADD_U64_e64),
6087 Dest.getReg())
6088 .add(Src0)
6089 .addImm(0)
6090 .add(Src1);
6091 TII->legalizeOperands(*Add);
6092 MI.eraseFromParent();
6093 return BB;
6094 }
6095
6096 const auto *CarryRC = TRI->getWaveMaskRegClass();
6097
6098 Register DestSub0 = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
6099 Register DestSub1 = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
6100
6101 Register CarryReg = MRI.createVirtualRegister(CarryRC);
6102 Register DeadCarryReg = MRI.createVirtualRegister(CarryRC);
6103
6104 const TargetRegisterClass *Src0RC = Src0.isReg()
6105 ? MRI.getRegClass(Src0.getReg())
6106 : &AMDGPU::VReg_64RegClass;
6107 const TargetRegisterClass *Src1RC = Src1.isReg()
6108 ? MRI.getRegClass(Src1.getReg())
6109 : &AMDGPU::VReg_64RegClass;
6110
6111 const TargetRegisterClass *Src0SubRC =
6112 TRI->getSubRegisterClass(Src0RC, AMDGPU::sub0);
6113 const TargetRegisterClass *Src1SubRC =
6114 TRI->getSubRegisterClass(Src1RC, AMDGPU::sub1);
6115
6116 MachineOperand SrcReg0Sub0 = TII->buildExtractSubRegOrImm(
6117 MI, MRI, Src0, Src0RC, AMDGPU::sub0, Src0SubRC);
6118 MachineOperand SrcReg1Sub0 = TII->buildExtractSubRegOrImm(
6119 MI, MRI, Src1, Src1RC, AMDGPU::sub0, Src1SubRC);
6120
6121 MachineOperand SrcReg0Sub1 = TII->buildExtractSubRegOrImm(
6122 MI, MRI, Src0, Src0RC, AMDGPU::sub1, Src0SubRC);
6123 MachineOperand SrcReg1Sub1 = TII->buildExtractSubRegOrImm(
6124 MI, MRI, Src1, Src1RC, AMDGPU::sub1, Src1SubRC);
6125
6126 unsigned LoOpc =
6127 IsAdd ? AMDGPU::V_ADD_CO_U32_e64 : AMDGPU::V_SUB_CO_U32_e64;
6128 MachineInstr *LoHalf = BuildMI(*BB, MI, DL, TII->get(LoOpc), DestSub0)
6129 .addReg(CarryReg, RegState::Define)
6130 .add(SrcReg0Sub0)
6131 .add(SrcReg1Sub0)
6132 .addImm(0); // clamp bit
6133
6134 unsigned HiOpc = IsAdd ? AMDGPU::V_ADDC_U32_e64 : AMDGPU::V_SUBB_U32_e64;
6135 MachineInstr *HiHalf =
6136 BuildMI(*BB, MI, DL, TII->get(HiOpc), DestSub1)
6137 .addReg(DeadCarryReg, RegState::Define | RegState::Dead)
6138 .add(SrcReg0Sub1)
6139 .add(SrcReg1Sub1)
6140 .addReg(CarryReg, RegState::Kill)
6141 .addImm(0); // clamp bit
6142
6143 BuildMI(*BB, MI, DL, TII->get(TargetOpcode::REG_SEQUENCE), Dest.getReg())
6144 .addReg(DestSub0)
6145 .addImm(AMDGPU::sub0)
6146 .addReg(DestSub1)
6147 .addImm(AMDGPU::sub1);
6148 TII->legalizeOperands(*LoHalf);
6149 TII->legalizeOperands(*HiHalf);
6150 MI.eraseFromParent();
6151 return BB;
6152 }
6153 case AMDGPU::S_ADD_CO_PSEUDO:
6154 case AMDGPU::S_SUB_CO_PSEUDO: {
6155 // This pseudo has a chance to be selected
6156 // only from uniform add/subcarry node. All the VGPR operands
6157 // therefore assumed to be splat vectors.
6159 MachineOperand &Dest = MI.getOperand(0);
6160 MachineOperand &CarryDest = MI.getOperand(1);
6161 MachineOperand &Src0 = MI.getOperand(2);
6162 MachineOperand &Src1 = MI.getOperand(3);
6163 MachineOperand &Src2 = MI.getOperand(4);
6164 if (Src0.isReg() && TRI->isVectorRegister(MRI, Src0.getReg())) {
6165 Register RegOp0 = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
6166 BuildMI(*BB, MII, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32), RegOp0)
6167 .addReg(Src0.getReg());
6168 Src0.setReg(RegOp0);
6169 }
6170 if (Src1.isReg() && TRI->isVectorRegister(MRI, Src1.getReg())) {
6171 Register RegOp1 = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
6172 BuildMI(*BB, MII, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32), RegOp1)
6173 .addReg(Src1.getReg());
6174 Src1.setReg(RegOp1);
6175 }
6176 Register RegOp2 = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
6177 if (TRI->isVectorRegister(MRI, Src2.getReg())) {
6178 BuildMI(*BB, MII, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32), RegOp2)
6179 .addReg(Src2.getReg());
6180 Src2.setReg(RegOp2);
6181 }
6182
6183 if (ST.isWave64()) {
6184 if (ST.hasScalarCompareEq64()) {
6185 BuildMI(*BB, MII, DL, TII->get(AMDGPU::S_CMP_LG_U64))
6186 .addReg(Src2.getReg())
6187 .addImm(0);
6188 } else {
6189 const TargetRegisterClass *Src2RC = MRI.getRegClass(Src2.getReg());
6190 const TargetRegisterClass *SubRC =
6191 TRI->getSubRegisterClass(Src2RC, AMDGPU::sub0);
6192 MachineOperand Src2Sub0 = TII->buildExtractSubRegOrImm(
6193 MII, MRI, Src2, Src2RC, AMDGPU::sub0, SubRC);
6194 MachineOperand Src2Sub1 = TII->buildExtractSubRegOrImm(
6195 MII, MRI, Src2, Src2RC, AMDGPU::sub1, SubRC);
6196 Register Src2_32 = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
6197
6198 BuildMI(*BB, MII, DL, TII->get(AMDGPU::S_OR_B32), Src2_32)
6199 .add(Src2Sub0)
6200 .add(Src2Sub1);
6201
6202 BuildMI(*BB, MII, DL, TII->get(AMDGPU::S_CMP_LG_U32))
6203 .addReg(Src2_32, RegState::Kill)
6204 .addImm(0);
6205 }
6206 } else {
6207 BuildMI(*BB, MII, DL, TII->get(AMDGPU::S_CMP_LG_U32))
6208 .addReg(Src2.getReg())
6209 .addImm(0);
6210 }
6211
6212 unsigned Opc = MI.getOpcode() == AMDGPU::S_ADD_CO_PSEUDO
6213 ? AMDGPU::S_ADDC_U32
6214 : AMDGPU::S_SUBB_U32;
6215
6216 BuildMI(*BB, MII, DL, TII->get(Opc), Dest.getReg()).add(Src0).add(Src1);
6217
6218 unsigned SelOpc =
6219 ST.isWave64() ? AMDGPU::S_CSELECT_B64 : AMDGPU::S_CSELECT_B32;
6220
6221 BuildMI(*BB, MII, DL, TII->get(SelOpc), CarryDest.getReg())
6222 .addImm(-1)
6223 .addImm(0);
6224
6225 MI.eraseFromParent();
6226 return BB;
6227 }
6228 case AMDGPU::SI_INIT_M0: {
6229 MachineOperand &M0Init = MI.getOperand(0);
6230 BuildMI(*BB, MI.getIterator(), MI.getDebugLoc(),
6231 TII->get(M0Init.isReg() ? AMDGPU::COPY : AMDGPU::S_MOV_B32),
6232 AMDGPU::M0)
6233 .add(M0Init);
6234 MI.eraseFromParent();
6235 return BB;
6236 }
6237 case AMDGPU::S_BARRIER_SIGNAL_ISFIRST_IMM: {
6238 // Set SCC to true, in case the barrier instruction gets converted to a NOP.
6239 BuildMI(*BB, MI.getIterator(), MI.getDebugLoc(),
6240 TII->get(AMDGPU::S_CMP_EQ_U32))
6241 .addImm(0)
6242 .addImm(0);
6243 return BB;
6244 }
6245 case AMDGPU::GET_GROUPSTATICSIZE: {
6246 assert(getTargetMachine().getTargetTriple().getOS() == Triple::AMDHSA ||
6247 getTargetMachine().getTargetTriple().getOS() == Triple::AMDPAL);
6248 BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_MOV_B32))
6249 .add(MI.getOperand(0))
6250 .addImm(MFI->getLDSSize());
6251 MI.eraseFromParent();
6252 return BB;
6253 }
6254 case AMDGPU::GET_SHADERCYCLESHILO: {
6256 // The algorithm is:
6257 //
6258 // hi1 = getreg(SHADER_CYCLES_HI)
6259 // lo1 = getreg(SHADER_CYCLES_LO)
6260 // hi2 = getreg(SHADER_CYCLES_HI)
6261 //
6262 // If hi1 == hi2 then there was no overflow and the result is hi2:lo1.
6263 // Otherwise there was overflow and the result is hi2:0. In both cases the
6264 // result should represent the actual time at some point during the sequence
6265 // of three getregs.
6266 using namespace AMDGPU::Hwreg;
6267 Register RegHi1 = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
6268 BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_GETREG_B32), RegHi1)
6269 .addImm(HwregEncoding::encode(ID_SHADER_CYCLES_HI, 0, 32));
6270 Register RegLo1 = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
6271 BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_GETREG_B32), RegLo1)
6272 .addImm(HwregEncoding::encode(ID_SHADER_CYCLES, 0, 32));
6273 Register RegHi2 = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
6274 BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_GETREG_B32), RegHi2)
6275 .addImm(HwregEncoding::encode(ID_SHADER_CYCLES_HI, 0, 32));
6276 BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_CMP_EQ_U32))
6277 .addReg(RegHi1)
6278 .addReg(RegHi2);
6279 Register RegLo = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
6280 BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_CSELECT_B32), RegLo)
6281 .addReg(RegLo1)
6282 .addImm(0);
6283 BuildMI(*BB, MI, DL, TII->get(AMDGPU::REG_SEQUENCE))
6284 .add(MI.getOperand(0))
6285 .addReg(RegLo)
6286 .addImm(AMDGPU::sub0)
6287 .addReg(RegHi2)
6288 .addImm(AMDGPU::sub1);
6289 MI.eraseFromParent();
6290 return BB;
6291 }
6292 case AMDGPU::SI_INDIRECT_SRC_V1:
6293 case AMDGPU::SI_INDIRECT_SRC_V2:
6294 case AMDGPU::SI_INDIRECT_SRC_V3:
6295 case AMDGPU::SI_INDIRECT_SRC_V4:
6296 case AMDGPU::SI_INDIRECT_SRC_V5:
6297 case AMDGPU::SI_INDIRECT_SRC_V6:
6298 case AMDGPU::SI_INDIRECT_SRC_V7:
6299 case AMDGPU::SI_INDIRECT_SRC_V8:
6300 case AMDGPU::SI_INDIRECT_SRC_V9:
6301 case AMDGPU::SI_INDIRECT_SRC_V10:
6302 case AMDGPU::SI_INDIRECT_SRC_V11:
6303 case AMDGPU::SI_INDIRECT_SRC_V12:
6304 case AMDGPU::SI_INDIRECT_SRC_V16:
6305 case AMDGPU::SI_INDIRECT_SRC_V32:
6306 return emitIndirectSrc(MI, *BB, *getSubtarget());
6307 case AMDGPU::SI_INDIRECT_DST_V1:
6308 case AMDGPU::SI_INDIRECT_DST_V2:
6309 case AMDGPU::SI_INDIRECT_DST_V3:
6310 case AMDGPU::SI_INDIRECT_DST_V4:
6311 case AMDGPU::SI_INDIRECT_DST_V5:
6312 case AMDGPU::SI_INDIRECT_DST_V6:
6313 case AMDGPU::SI_INDIRECT_DST_V7:
6314 case AMDGPU::SI_INDIRECT_DST_V8:
6315 case AMDGPU::SI_INDIRECT_DST_V9:
6316 case AMDGPU::SI_INDIRECT_DST_V10:
6317 case AMDGPU::SI_INDIRECT_DST_V11:
6318 case AMDGPU::SI_INDIRECT_DST_V12:
6319 case AMDGPU::SI_INDIRECT_DST_V16:
6320 case AMDGPU::SI_INDIRECT_DST_V32:
6321 return emitIndirectDst(MI, *BB, *getSubtarget());
6322 case AMDGPU::SI_KILL_F32_COND_IMM_PSEUDO:
6323 case AMDGPU::SI_KILL_I1_PSEUDO:
6324 return splitKillBlock(MI, BB);
6325 case AMDGPU::V_CNDMASK_B64_PSEUDO: {
6326 Register Dst = MI.getOperand(0).getReg();
6327 const MachineOperand &Src0 = MI.getOperand(1);
6328 const MachineOperand &Src1 = MI.getOperand(2);
6329 Register SrcCond = MI.getOperand(3).getReg();
6330
6331 Register DstLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
6332 Register DstHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
6333 const auto *CondRC = TRI->getWaveMaskRegClass();
6334 Register SrcCondCopy = MRI.createVirtualRegister(CondRC);
6335
6336 const TargetRegisterClass *Src0RC = Src0.isReg()
6337 ? MRI.getRegClass(Src0.getReg())
6338 : &AMDGPU::VReg_64RegClass;
6339 const TargetRegisterClass *Src1RC = Src1.isReg()
6340 ? MRI.getRegClass(Src1.getReg())
6341 : &AMDGPU::VReg_64RegClass;
6342
6343 const TargetRegisterClass *Src0SubRC =
6344 TRI->getSubRegisterClass(Src0RC, AMDGPU::sub0);
6345 const TargetRegisterClass *Src1SubRC =
6346 TRI->getSubRegisterClass(Src1RC, AMDGPU::sub1);
6347
6348 MachineOperand Src0Sub0 = TII->buildExtractSubRegOrImm(
6349 MI, MRI, Src0, Src0RC, AMDGPU::sub0, Src0SubRC);
6350 MachineOperand Src1Sub0 = TII->buildExtractSubRegOrImm(
6351 MI, MRI, Src1, Src1RC, AMDGPU::sub0, Src1SubRC);
6352
6353 MachineOperand Src0Sub1 = TII->buildExtractSubRegOrImm(
6354 MI, MRI, Src0, Src0RC, AMDGPU::sub1, Src0SubRC);
6355 MachineOperand Src1Sub1 = TII->buildExtractSubRegOrImm(
6356 MI, MRI, Src1, Src1RC, AMDGPU::sub1, Src1SubRC);
6357
6358 BuildMI(*BB, MI, DL, TII->get(AMDGPU::COPY), SrcCondCopy).addReg(SrcCond);
6359 BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstLo)
6360 .addImm(0)
6361 .add(Src0Sub0)
6362 .addImm(0)
6363 .add(Src1Sub0)
6364 .addReg(SrcCondCopy);
6365 BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstHi)
6366 .addImm(0)
6367 .add(Src0Sub1)
6368 .addImm(0)
6369 .add(Src1Sub1)
6370 .addReg(SrcCondCopy);
6371
6372 BuildMI(*BB, MI, DL, TII->get(AMDGPU::REG_SEQUENCE), Dst)
6373 .addReg(DstLo)
6374 .addImm(AMDGPU::sub0)
6375 .addReg(DstHi)
6376 .addImm(AMDGPU::sub1);
6377 MI.eraseFromParent();
6378 return BB;
6379 }
6380 case AMDGPU::SI_BR_UNDEF: {
6381 MachineInstr *Br = BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_CBRANCH_SCC1))
6382 .add(MI.getOperand(0));
6383 Br->getOperand(1).setIsUndef(); // read undef SCC
6384 MI.eraseFromParent();
6385 return BB;
6386 }
6387 case AMDGPU::ADJCALLSTACKUP:
6388 case AMDGPU::ADJCALLSTACKDOWN: {
6390 MachineInstrBuilder MIB(*MF, &MI);
6391 MIB.addReg(Info->getStackPtrOffsetReg(), RegState::ImplicitDefine)
6392 .addReg(Info->getStackPtrOffsetReg(), RegState::Implicit);
6393 return BB;
6394 }
6395 case AMDGPU::SI_CALL_ISEL: {
6396 unsigned ReturnAddrReg = TII->getRegisterInfo().getReturnAddressReg(*MF);
6397
6399 MIB = BuildMI(*BB, MI, DL, TII->get(AMDGPU::SI_CALL), ReturnAddrReg);
6400
6401 for (const MachineOperand &MO : MI.operands())
6402 MIB.add(MO);
6403
6404 MIB.cloneMemRefs(MI);
6405 MI.eraseFromParent();
6406 return BB;
6407 }
6408 case AMDGPU::V_ADD_CO_U32_e32:
6409 case AMDGPU::V_SUB_CO_U32_e32:
6410 case AMDGPU::V_SUBREV_CO_U32_e32: {
6411 // TODO: Define distinct V_*_I32_Pseudo instructions instead.
6412 unsigned Opc = MI.getOpcode();
6413
6414 bool NeedClampOperand = false;
6415 if (TII->pseudoToMCOpcode(Opc) == -1) {
6417 NeedClampOperand = true;
6418 }
6419
6420 auto I = BuildMI(*BB, MI, DL, TII->get(Opc), MI.getOperand(0).getReg());
6421 if (TII->isVOP3(*I)) {
6422 I.addReg(TRI->getVCC(), RegState::Define);
6423 }
6424 I.add(MI.getOperand(1)).add(MI.getOperand(2));
6425 if (NeedClampOperand)
6426 I.addImm(0); // clamp bit for e64 encoding
6427
6428 TII->legalizeOperands(*I);
6429
6430 MI.eraseFromParent();
6431 return BB;
6432 }
6433 case AMDGPU::V_ADDC_U32_e32:
6434 case AMDGPU::V_SUBB_U32_e32:
6435 case AMDGPU::V_SUBBREV_U32_e32:
6436 // These instructions have an implicit use of vcc which counts towards the
6437 // constant bus limit.
6438 TII->legalizeOperands(MI);
6439 return BB;
6440 case AMDGPU::DS_GWS_INIT:
6441 case AMDGPU::DS_GWS_SEMA_BR:
6442 case AMDGPU::DS_GWS_BARRIER:
6443 case AMDGPU::DS_GWS_SEMA_V:
6444 case AMDGPU::DS_GWS_SEMA_P:
6445 case AMDGPU::DS_GWS_SEMA_RELEASE_ALL:
6446 // A s_waitcnt 0 is required to be the instruction immediately following.
6447 if (getSubtarget()->hasGWSAutoReplay()) {
6449 return BB;
6450 }
6451
6452 return emitGWSMemViolTestLoop(MI, BB);
6453 case AMDGPU::S_SETREG_B32: {
6454 // Try to optimize cases that only set the denormal mode or rounding mode.
6455 //
6456 // If the s_setreg_b32 fully sets all of the bits in the rounding mode or
6457 // denormal mode to a constant, we can use s_round_mode or s_denorm_mode
6458 // instead.
6459 //
6460 // FIXME: This could be predicates on the immediate, but tablegen doesn't
6461 // allow you to have a no side effect instruction in the output of a
6462 // sideeffecting pattern.
6463 auto [ID, Offset, Width] =
6464 AMDGPU::Hwreg::HwregEncoding::decode(MI.getOperand(1).getImm());
6466 return BB;
6467
6468 const unsigned WidthMask = maskTrailingOnes<unsigned>(Width);
6469 const unsigned SetMask = WidthMask << Offset;
6470
6471 if (getSubtarget()->hasDenormModeInst()) {
6472 unsigned SetDenormOp = 0;
6473 unsigned SetRoundOp = 0;
6474
6475 // The dedicated instructions can only set the whole denorm or round mode
6476 // at once, not a subset of bits in either.
6477 if (SetMask ==
6479 // If this fully sets both the round and denorm mode, emit the two
6480 // dedicated instructions for these.
6481 SetRoundOp = AMDGPU::S_ROUND_MODE;
6482 SetDenormOp = AMDGPU::S_DENORM_MODE;
6483 } else if (SetMask == AMDGPU::Hwreg::FP_ROUND_MASK) {
6484 SetRoundOp = AMDGPU::S_ROUND_MODE;
6485 } else if (SetMask == AMDGPU::Hwreg::FP_DENORM_MASK) {
6486 SetDenormOp = AMDGPU::S_DENORM_MODE;
6487 }
6488
6489 if (SetRoundOp || SetDenormOp) {
6490 MachineInstr *Def = MRI.getVRegDef(MI.getOperand(0).getReg());
6491 if (Def && Def->isMoveImmediate() && Def->getOperand(1).isImm()) {
6492 unsigned ImmVal = Def->getOperand(1).getImm();
6493 if (SetRoundOp) {
6494 BuildMI(*BB, MI, MI.getDebugLoc(), TII->get(SetRoundOp))
6495 .addImm(ImmVal & 0xf);
6496
6497 // If we also have the denorm mode, get just the denorm mode bits.
6498 ImmVal >>= 4;
6499 }
6500
6501 if (SetDenormOp) {
6502 BuildMI(*BB, MI, MI.getDebugLoc(), TII->get(SetDenormOp))
6503 .addImm(ImmVal & 0xf);
6504 }
6505
6506 MI.eraseFromParent();
6507 return BB;
6508 }
6509 }
6510 }
6511
6512 // If only FP bits are touched, used the no side effects pseudo.
6513 if ((SetMask & (AMDGPU::Hwreg::FP_ROUND_MASK |
6514 AMDGPU::Hwreg::FP_DENORM_MASK)) == SetMask)
6515 MI.setDesc(TII->get(AMDGPU::S_SETREG_B32_mode));
6516
6517 return BB;
6518 }
6519 case AMDGPU::S_INVERSE_BALLOT_U32:
6520 case AMDGPU::S_INVERSE_BALLOT_U64:
6521 // These opcodes only exist to let SIFixSGPRCopies insert a readfirstlane if
6522 // necessary. After that they are equivalent to a COPY.
6523 MI.setDesc(TII->get(AMDGPU::COPY));
6524 return BB;
6525 case AMDGPU::ENDPGM_TRAP: {
6526 if (BB->succ_empty() && std::next(MI.getIterator()) == BB->end()) {
6527 MI.setDesc(TII->get(AMDGPU::S_ENDPGM));
6528 MI.addOperand(MachineOperand::CreateImm(0));
6529 return BB;
6530 }
6531
6532 // We need a block split to make the real endpgm a terminator. We also don't
6533 // want to break phis in successor blocks, so we can't just delete to the
6534 // end of the block.
6535
6536 MachineBasicBlock *SplitBB = BB->splitAt(MI, false /*UpdateLiveIns*/);
6538 MF->push_back(TrapBB);
6539 // clang-format off
6540 BuildMI(*TrapBB, TrapBB->end(), DL, TII->get(AMDGPU::S_ENDPGM))
6541 .addImm(0);
6542 BuildMI(*BB, &MI, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
6543 .addMBB(TrapBB);
6544 // clang-format on
6545
6546 BB->addSuccessor(TrapBB);
6547 MI.eraseFromParent();
6548 return SplitBB;
6549 }
6550 case AMDGPU::SIMULATED_TRAP: {
6551 assert(Subtarget->hasPrivEnabledTrap2NopBug());
6552 MachineBasicBlock *SplitBB =
6553 TII->insertSimulatedTrap(MRI, *BB, MI, MI.getDebugLoc());
6554 MI.eraseFromParent();
6555 return SplitBB;
6556 }
6557 case AMDGPU::SI_TCRETURN_GFX_WholeWave:
6558 case AMDGPU::SI_WHOLE_WAVE_FUNC_RETURN: {
6560
6561 // During ISel, it's difficult to propagate the original EXEC mask to use as
6562 // an input to SI_WHOLE_WAVE_FUNC_RETURN. Set it up here instead.
6563 MachineInstr *Setup = TII->getWholeWaveFunctionSetup(*BB->getParent());
6564 assert(Setup && "Couldn't find SI_SETUP_WHOLE_WAVE_FUNC");
6565 Register OriginalExec = Setup->getOperand(0).getReg();
6566 MF->getRegInfo().clearKillFlags(OriginalExec);
6567 MI.getOperand(0).setReg(OriginalExec);
6568 return BB;
6569 }
6570 default:
6571 if (TII->isImage(MI) || TII->isMUBUF(MI)) {
6572 if (!MI.mayStore())
6574 return BB;
6575 }
6577 }
6578}
6579
6581 // This currently forces unfolding various combinations of fsub into fma with
6582 // free fneg'd operands. As long as we have fast FMA (controlled by
6583 // isFMAFasterThanFMulAndFAdd), we should perform these.
6584
6585 // When fma is quarter rate, for f64 where add / sub are at best half rate,
6586 // most of these combines appear to be cycle neutral but save on instruction
6587 // count / code size.
6588 return true;
6589}
6590
6592
6594 EVT VT) const {
6595 if (!VT.isVector()) {
6596 return MVT::i1;
6597 }
6598 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements());
6599}
6600
6602 // TODO: Should i16 be used always if legal? For now it would force VALU
6603 // shifts.
6604 return (VT == MVT::i16) ? MVT::i16 : MVT::i32;
6605}
6606
6608 return (Ty.getScalarSizeInBits() <= 16 && Subtarget->has16BitInsts())
6609 ? Ty.changeElementSize(16)
6610 : Ty.changeElementSize(32);
6611}
6612
6613// Answering this is somewhat tricky and depends on the specific device which
6614// have different rates for fma or all f64 operations.
6615//
6616// v_fma_f64 and v_mul_f64 always take the same number of cycles as each other
6617// regardless of which device (although the number of cycles differs between
6618// devices), so it is always profitable for f64.
6619//
6620// v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable
6621// only on full rate devices. Normally, we should prefer selecting v_mad_f32
6622// which we can always do even without fused FP ops since it returns the same
6623// result as the separate operations and since it is always full
6624// rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32
6625// however does not support denormals, so we do report fma as faster if we have
6626// a fast fma device and require denormals.
6627//
6629 EVT VT) const {
6630 VT = VT.getScalarType();
6631
6632 switch (VT.getSimpleVT().SimpleTy) {
6633 case MVT::f32: {
6634 // If mad is not available this depends only on if f32 fma is full rate.
6635 if (!Subtarget->hasMadMacF32Insts())
6636 return Subtarget->hasFastFMAF32();
6637
6638 // Otherwise f32 mad is always full rate and returns the same result as
6639 // the separate operations so should be preferred over fma.
6640 // However does not support denormals.
6642 return Subtarget->hasFastFMAF32() || Subtarget->hasDLInsts();
6643
6644 // If the subtarget has v_fmac_f32, that's just as good as v_mac_f32.
6645 return Subtarget->hasFastFMAF32() && Subtarget->hasDLInsts();
6646 }
6647 case MVT::f64:
6648 return true;
6649 case MVT::f16:
6650 case MVT::bf16:
6651 return Subtarget->has16BitInsts() && !denormalModeIsFlushAllF64F16(MF);
6652 default:
6653 break;
6654 }
6655
6656 return false;
6657}
6658
6660 LLT Ty) const {
6661 switch (Ty.getScalarSizeInBits()) {
6662 case 16:
6663 return isFMAFasterThanFMulAndFAdd(MF, MVT::f16);
6664 case 32:
6665 return isFMAFasterThanFMulAndFAdd(MF, MVT::f32);
6666 case 64:
6667 return isFMAFasterThanFMulAndFAdd(MF, MVT::f64);
6668 default:
6669 break;
6670 }
6671
6672 return false;
6673}
6674
6676 if (!Ty.isScalar())
6677 return false;
6678
6679 if (Ty.getScalarSizeInBits() == 16)
6680 return Subtarget->hasMadF16() && denormalModeIsFlushAllF64F16(*MI.getMF());
6681 if (Ty.getScalarSizeInBits() == 32)
6682 return Subtarget->hasMadMacF32Insts() &&
6683 denormalModeIsFlushAllF32(*MI.getMF());
6684
6685 return false;
6686}
6687
6689 const SDNode *N) const {
6690 // TODO: Check future ftz flag
6691 // v_mad_f32/v_mac_f32 do not support denormals.
6692 EVT VT = N->getValueType(0);
6693 if (VT == MVT::f32)
6694 return Subtarget->hasMadMacF32Insts() &&
6696 if (VT == MVT::f16) {
6697 return Subtarget->hasMadF16() &&
6699 }
6700
6701 return false;
6702}
6703
6704//===----------------------------------------------------------------------===//
6705// Custom DAG Lowering Operations
6706//===----------------------------------------------------------------------===//
6707
6708// Work around LegalizeDAG doing the wrong thing and fully scalarizing if the
6709// wider vector type is legal.
6711 SelectionDAG &DAG) const {
6712 unsigned Opc = Op.getOpcode();
6713 EVT VT = Op.getValueType();
6714 assert(VT == MVT::v4i16 || VT == MVT::v4f16 || VT == MVT::v4bf16 ||
6715 VT == MVT::v4f32 || VT == MVT::v8i16 || VT == MVT::v8f16 ||
6716 VT == MVT::v8bf16 || VT == MVT::v16i16 || VT == MVT::v16f16 ||
6717 VT == MVT::v16bf16 || VT == MVT::v8f32 || VT == MVT::v16f32 ||
6718 VT == MVT::v32f32 || VT == MVT::v32i16 || VT == MVT::v32f16 ||
6719 VT == MVT::v32bf16);
6720
6721 auto [Lo, Hi] = DAG.SplitVectorOperand(Op.getNode(), 0);
6722
6723 SDLoc SL(Op);
6724 SDValue OpLo = DAG.getNode(Opc, SL, Lo.getValueType(), Lo, Op->getFlags());
6725 SDValue OpHi = DAG.getNode(Opc, SL, Hi.getValueType(), Hi, Op->getFlags());
6726
6727 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(Op), VT, OpLo, OpHi);
6728}
6729
6730// Enable lowering of ROTR for vxi32 types. This is a workaround for a
6731// regression whereby extra unnecessary instructions were added to codegen
6732// for rotr operations, casued by legalising v2i32 or. This resulted in extra
6733// instructions to extract the result from the vector.
6735 [[maybe_unused]] EVT VT = Op.getValueType();
6736
6737 assert((VT == MVT::v2i32 || VT == MVT::v4i32 || VT == MVT::v8i32 ||
6738 VT == MVT::v16i32) &&
6739 "Unexpected ValueType.");
6740
6741 return DAG.UnrollVectorOp(Op.getNode());
6742}
6743
6744// Work around LegalizeDAG doing the wrong thing and fully scalarizing if the
6745// wider vector type is legal.
6747 SelectionDAG &DAG) const {
6748 unsigned Opc = Op.getOpcode();
6749 EVT VT = Op.getValueType();
6750 assert(VT == MVT::v4i16 || VT == MVT::v4f16 || VT == MVT::v4bf16 ||
6751 VT == MVT::v4f32 || VT == MVT::v8i16 || VT == MVT::v8f16 ||
6752 VT == MVT::v8bf16 || VT == MVT::v16i16 || VT == MVT::v16f16 ||
6753 VT == MVT::v16bf16 || VT == MVT::v8f32 || VT == MVT::v16f32 ||
6754 VT == MVT::v32f32 || VT == MVT::v32i16 || VT == MVT::v32f16 ||
6755 VT == MVT::v32bf16);
6756
6757 auto [Lo0, Hi0] = DAG.SplitVectorOperand(Op.getNode(), 0);
6758 auto [Lo1, Hi1] = DAG.SplitVectorOperand(Op.getNode(), 1);
6759
6760 SDLoc SL(Op);
6761
6762 SDValue OpLo =
6763 DAG.getNode(Opc, SL, Lo0.getValueType(), Lo0, Lo1, Op->getFlags());
6764 SDValue OpHi =
6765 DAG.getNode(Opc, SL, Hi0.getValueType(), Hi0, Hi1, Op->getFlags());
6766
6767 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(Op), VT, OpLo, OpHi);
6768}
6769
6771 SelectionDAG &DAG) const {
6772 unsigned Opc = Op.getOpcode();
6773 EVT VT = Op.getValueType();
6774 assert(VT == MVT::v4i16 || VT == MVT::v4f16 || VT == MVT::v8i16 ||
6775 VT == MVT::v8f16 || VT == MVT::v4f32 || VT == MVT::v16i16 ||
6776 VT == MVT::v16f16 || VT == MVT::v8f32 || VT == MVT::v16f32 ||
6777 VT == MVT::v32f32 || VT == MVT::v32f16 || VT == MVT::v32i16 ||
6778 VT == MVT::v4bf16 || VT == MVT::v8bf16 || VT == MVT::v16bf16 ||
6779 VT == MVT::v32bf16);
6780
6781 SDValue Op0 = Op.getOperand(0);
6782 auto [Lo0, Hi0] = Op0.getValueType().isVector()
6783 ? DAG.SplitVectorOperand(Op.getNode(), 0)
6784 : std::pair(Op0, Op0);
6785
6786 auto [Lo1, Hi1] = DAG.SplitVectorOperand(Op.getNode(), 1);
6787 auto [Lo2, Hi2] = DAG.SplitVectorOperand(Op.getNode(), 2);
6788
6789 SDLoc SL(Op);
6790 auto ResVT = DAG.GetSplitDestVTs(VT);
6791
6792 SDValue OpLo =
6793 DAG.getNode(Opc, SL, ResVT.first, Lo0, Lo1, Lo2, Op->getFlags());
6794 SDValue OpHi =
6795 DAG.getNode(Opc, SL, ResVT.second, Hi0, Hi1, Hi2, Op->getFlags());
6796
6797 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(Op), VT, OpLo, OpHi);
6798}
6799
6801 switch (Op.getOpcode()) {
6802 default:
6804 case ISD::BRCOND:
6805 return LowerBRCOND(Op, DAG);
6806 case ISD::RETURNADDR:
6807 return LowerRETURNADDR(Op, DAG);
6808 case ISD::LOAD: {
6809 SDValue Result = LowerLOAD(Op, DAG);
6810 assert((!Result.getNode() || Result.getNode()->getNumValues() == 2) &&
6811 "Load should return a value and a chain");
6812 return Result;
6813 }
6814 case ISD::FSQRT: {
6815 EVT VT = Op.getValueType();
6816 if (VT == MVT::f32)
6817 return lowerFSQRTF32(Op, DAG);
6818 if (VT == MVT::f64)
6819 return lowerFSQRTF64(Op, DAG);
6820 return SDValue();
6821 }
6822 case ISD::FSIN:
6823 case ISD::FCOS:
6824 return LowerTrig(Op, DAG);
6825 case ISD::SELECT:
6826 return LowerSELECT(Op, DAG);
6827 case ISD::FDIV:
6828 return LowerFDIV(Op, DAG);
6829 case ISD::FFREXP:
6830 return LowerFFREXP(Op, DAG);
6832 return LowerATOMIC_CMP_SWAP(Op, DAG);
6833 case ISD::STORE:
6834 return LowerSTORE(Op, DAG);
6835 case ISD::GlobalAddress: {
6838 return LowerGlobalAddress(MFI, Op, DAG);
6839 }
6841 return LowerExternalSymbol(Op, DAG);
6843 return LowerINTRINSIC_WO_CHAIN(Op, DAG);
6845 return LowerINTRINSIC_W_CHAIN(Op, DAG);
6847 return LowerINTRINSIC_VOID(Op, DAG);
6848 case ISD::ADDRSPACECAST:
6849 return lowerADDRSPACECAST(Op, DAG);
6851 return lowerINSERT_SUBVECTOR(Op, DAG);
6853 return lowerINSERT_VECTOR_ELT(Op, DAG);
6855 return lowerEXTRACT_VECTOR_ELT(Op, DAG);
6857 return lowerVECTOR_SHUFFLE(Op, DAG);
6859 return lowerSCALAR_TO_VECTOR(Op, DAG);
6860 case ISD::BUILD_VECTOR:
6861 return lowerBUILD_VECTOR(Op, DAG);
6862 case ISD::FP_ROUND:
6864 return lowerFP_ROUND(Op, DAG);
6865 case ISD::TRAP:
6866 return lowerTRAP(Op, DAG);
6867 case ISD::DEBUGTRAP:
6868 return lowerDEBUGTRAP(Op, DAG);
6869 case ISD::ABS:
6870 case ISD::FABS:
6871 case ISD::FNEG:
6872 case ISD::FCANONICALIZE:
6873 case ISD::BSWAP:
6874 return splitUnaryVectorOp(Op, DAG);
6875 case ISD::FMINNUM:
6876 case ISD::FMAXNUM:
6877 return lowerFMINNUM_FMAXNUM(Op, DAG);
6878 case ISD::FMINIMUMNUM:
6879 case ISD::FMAXIMUMNUM:
6880 return lowerFMINIMUMNUM_FMAXIMUMNUM(Op, DAG);
6881 case ISD::FMINIMUM:
6882 case ISD::FMAXIMUM:
6883 return lowerFMINIMUM_FMAXIMUM(Op, DAG);
6884 case ISD::FLDEXP:
6885 case ISD::STRICT_FLDEXP:
6886 return lowerFLDEXP(Op, DAG);
6887 case ISD::FMA:
6888 return splitTernaryVectorOp(Op, DAG);
6889 case ISD::FP_TO_SINT:
6890 case ISD::FP_TO_UINT:
6891 if (Subtarget->getGeneration() >= AMDGPUSubtarget::GFX11 &&
6892 Op.getValueType() == MVT::i16 &&
6893 Op.getOperand(0).getValueType() == MVT::f32) {
6894 // Make f32->i16 legal so we can select V_CVT_PK_[IU]16_F32.
6895 return Op;
6896 }
6897 return LowerFP_TO_INT(Op, DAG);
6898 case ISD::SHL:
6899 case ISD::SRA:
6900 case ISD::SRL:
6901 case ISD::ADD:
6902 case ISD::SUB:
6903 case ISD::SMIN:
6904 case ISD::SMAX:
6905 case ISD::UMIN:
6906 case ISD::UMAX:
6907 case ISD::FADD:
6908 case ISD::FMUL:
6909 case ISD::FMINNUM_IEEE:
6910 case ISD::FMAXNUM_IEEE:
6911 case ISD::UADDSAT:
6912 case ISD::USUBSAT:
6913 case ISD::SADDSAT:
6914 case ISD::SSUBSAT:
6915 return splitBinaryVectorOp(Op, DAG);
6916 case ISD::FCOPYSIGN:
6917 return lowerFCOPYSIGN(Op, DAG);
6918 case ISD::MUL:
6919 return lowerMUL(Op, DAG);
6920 case ISD::SMULO:
6921 case ISD::UMULO:
6922 return lowerXMULO(Op, DAG);
6923 case ISD::SMUL_LOHI:
6924 case ISD::UMUL_LOHI:
6925 return lowerXMUL_LOHI(Op, DAG);
6927 return LowerDYNAMIC_STACKALLOC(Op, DAG);
6928 case ISD::STACKSAVE:
6929 return LowerSTACKSAVE(Op, DAG);
6930 case ISD::GET_ROUNDING:
6931 return lowerGET_ROUNDING(Op, DAG);
6932 case ISD::SET_ROUNDING:
6933 return lowerSET_ROUNDING(Op, DAG);
6934 case ISD::PREFETCH:
6935 return lowerPREFETCH(Op, DAG);
6936 case ISD::FP_EXTEND:
6938 return lowerFP_EXTEND(Op, DAG);
6939 case ISD::GET_FPENV:
6940 return lowerGET_FPENV(Op, DAG);
6941 case ISD::SET_FPENV:
6942 return lowerSET_FPENV(Op, DAG);
6943 case ISD::ROTR:
6944 return lowerROTR(Op, DAG);
6945 }
6946 return SDValue();
6947}
6948
6949// Used for D16: Casts the result of an instruction into the right vector,
6950// packs values if loads return unpacked values.
6952 const SDLoc &DL, SelectionDAG &DAG,
6953 bool Unpacked) {
6954 if (!LoadVT.isVector())
6955 return Result;
6956
6957 // Cast back to the original packed type or to a larger type that is a
6958 // multiple of 32 bit for D16. Widening the return type is a required for
6959 // legalization.
6960 EVT FittingLoadVT = LoadVT;
6961 if ((LoadVT.getVectorNumElements() % 2) == 1) {
6962 FittingLoadVT =
6964 LoadVT.getVectorNumElements() + 1);
6965 }
6966
6967 if (Unpacked) { // From v2i32/v4i32 back to v2f16/v4f16.
6968 // Truncate to v2i16/v4i16.
6969 EVT IntLoadVT = FittingLoadVT.changeTypeToInteger();
6970
6971 // Workaround legalizer not scalarizing truncate after vector op
6972 // legalization but not creating intermediate vector trunc.
6974 DAG.ExtractVectorElements(Result, Elts);
6975 for (SDValue &Elt : Elts)
6976 Elt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i16, Elt);
6977
6978 // Pad illegal v1i16/v3fi6 to v4i16
6979 if ((LoadVT.getVectorNumElements() % 2) == 1)
6980 Elts.push_back(DAG.getPOISON(MVT::i16));
6981
6982 Result = DAG.getBuildVector(IntLoadVT, DL, Elts);
6983
6984 // Bitcast to original type (v2f16/v4f16).
6985 return DAG.getNode(ISD::BITCAST, DL, FittingLoadVT, Result);
6986 }
6987
6988 // Cast back to the original packed type.
6989 return DAG.getNode(ISD::BITCAST, DL, FittingLoadVT, Result);
6990}
6991
6992SDValue SITargetLowering::adjustLoadValueType(unsigned Opcode, MemSDNode *M,
6993 SelectionDAG &DAG,
6995 bool IsIntrinsic) const {
6996 SDLoc DL(M);
6997
6998 bool Unpacked = Subtarget->hasUnpackedD16VMem();
6999 EVT LoadVT = M->getValueType(0);
7000
7001 EVT EquivLoadVT = LoadVT;
7002 if (LoadVT.isVector()) {
7003 if (Unpacked) {
7004 EquivLoadVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32,
7005 LoadVT.getVectorNumElements());
7006 } else if ((LoadVT.getVectorNumElements() % 2) == 1) {
7007 // Widen v3f16 to legal type
7008 EquivLoadVT =
7010 LoadVT.getVectorNumElements() + 1);
7011 }
7012 }
7013
7014 // Change from v4f16/v2f16 to EquivLoadVT.
7015 SDVTList VTList = DAG.getVTList(EquivLoadVT, MVT::Other);
7016
7018 IsIntrinsic ? (unsigned)ISD::INTRINSIC_W_CHAIN : Opcode, DL, VTList, Ops,
7019 M->getMemoryVT(), M->getMemOperand());
7020
7021 SDValue Adjusted = adjustLoadValueTypeImpl(Load, LoadVT, DL, DAG, Unpacked);
7022
7023 return DAG.getMergeValues({Adjusted, Load.getValue(1)}, DL);
7024}
7025
7026SDValue SITargetLowering::lowerIntrinsicLoad(MemSDNode *M, bool IsFormat,
7027 SelectionDAG &DAG,
7028 ArrayRef<SDValue> Ops) const {
7029 SDLoc DL(M);
7030 EVT LoadVT = M->getValueType(0);
7031 EVT EltType = LoadVT.getScalarType();
7032 EVT IntVT = LoadVT.changeTypeToInteger();
7033
7034 bool IsD16 = IsFormat && (EltType.getSizeInBits() == 16);
7035
7036 assert(M->getNumValues() == 2 || M->getNumValues() == 3);
7037 bool IsTFE = M->getNumValues() == 3;
7038
7039 unsigned Opc = IsFormat ? (IsTFE ? AMDGPUISD::BUFFER_LOAD_FORMAT_TFE
7040 : AMDGPUISD::BUFFER_LOAD_FORMAT)
7041 : IsTFE ? AMDGPUISD::BUFFER_LOAD_TFE
7042 : AMDGPUISD::BUFFER_LOAD;
7043
7044 if (IsD16) {
7045 return adjustLoadValueType(AMDGPUISD::BUFFER_LOAD_FORMAT_D16, M, DAG, Ops);
7046 }
7047
7048 // Handle BUFFER_LOAD_BYTE/UBYTE/SHORT/USHORT overloaded intrinsics
7049 if (!IsD16 && !LoadVT.isVector() && EltType.getSizeInBits() < 32)
7050 return handleByteShortBufferLoads(DAG, LoadVT, DL, Ops, M->getMemOperand(),
7051 IsTFE);
7052
7053 if (isTypeLegal(LoadVT)) {
7054 return getMemIntrinsicNode(Opc, DL, M->getVTList(), Ops, IntVT,
7055 M->getMemOperand(), DAG);
7056 }
7057
7058 EVT CastVT = getEquivalentMemType(*DAG.getContext(), LoadVT);
7059 SDVTList VTList = DAG.getVTList(CastVT, MVT::Other);
7060 SDValue MemNode = getMemIntrinsicNode(Opc, DL, VTList, Ops, CastVT,
7061 M->getMemOperand(), DAG);
7062 return DAG.getMergeValues(
7063 {DAG.getNode(ISD::BITCAST, DL, LoadVT, MemNode), MemNode.getValue(1)},
7064 DL);
7065}
7066
7068 SelectionDAG &DAG) {
7069 EVT VT = N->getValueType(0);
7070 unsigned CondCode = N->getConstantOperandVal(3);
7071 if (!ICmpInst::isIntPredicate(static_cast<ICmpInst::Predicate>(CondCode)))
7072 return DAG.getPOISON(VT);
7073
7074 ICmpInst::Predicate IcInput = static_cast<ICmpInst::Predicate>(CondCode);
7075
7076 SDValue LHS = N->getOperand(1);
7077 SDValue RHS = N->getOperand(2);
7078
7079 SDLoc DL(N);
7080
7081 EVT CmpVT = LHS.getValueType();
7082 if (CmpVT == MVT::i16 && !TLI.isTypeLegal(MVT::i16)) {
7083 unsigned PromoteOp =
7085 LHS = DAG.getNode(PromoteOp, DL, MVT::i32, LHS);
7086 RHS = DAG.getNode(PromoteOp, DL, MVT::i32, RHS);
7087 }
7088
7089 ISD::CondCode CCOpcode = getICmpCondCode(IcInput);
7090
7091 unsigned WavefrontSize = TLI.getSubtarget()->getWavefrontSize();
7092 EVT CCVT = EVT::getIntegerVT(*DAG.getContext(), WavefrontSize);
7093
7094 SDValue SetCC = DAG.getNode(AMDGPUISD::SETCC, DL, CCVT, LHS, RHS,
7095 DAG.getCondCode(CCOpcode));
7096 if (VT.bitsEq(CCVT))
7097 return SetCC;
7098 return DAG.getZExtOrTrunc(SetCC, DL, VT);
7099}
7100
7102 SelectionDAG &DAG) {
7103 EVT VT = N->getValueType(0);
7104
7105 unsigned CondCode = N->getConstantOperandVal(3);
7106 if (!FCmpInst::isFPPredicate(static_cast<FCmpInst::Predicate>(CondCode)))
7107 return DAG.getPOISON(VT);
7108
7109 SDValue Src0 = N->getOperand(1);
7110 SDValue Src1 = N->getOperand(2);
7111 EVT CmpVT = Src0.getValueType();
7112 SDLoc SL(N);
7113
7114 if (CmpVT == MVT::f16 && !TLI.isTypeLegal(CmpVT)) {
7115 Src0 = DAG.getNode(ISD::FP_EXTEND, SL, MVT::f32, Src0);
7116 Src1 = DAG.getNode(ISD::FP_EXTEND, SL, MVT::f32, Src1);
7117 }
7118
7119 FCmpInst::Predicate IcInput = static_cast<FCmpInst::Predicate>(CondCode);
7120 ISD::CondCode CCOpcode = getFCmpCondCode(IcInput);
7121 unsigned WavefrontSize = TLI.getSubtarget()->getWavefrontSize();
7122 EVT CCVT = EVT::getIntegerVT(*DAG.getContext(), WavefrontSize);
7123 SDValue SetCC = DAG.getNode(AMDGPUISD::SETCC, SL, CCVT, Src0, Src1,
7124 DAG.getCondCode(CCOpcode));
7125 if (VT.bitsEq(CCVT))
7126 return SetCC;
7127 return DAG.getZExtOrTrunc(SetCC, SL, VT);
7128}
7129
7131 SelectionDAG &DAG) {
7132 EVT VT = N->getValueType(0);
7133 SDValue Src = N->getOperand(1);
7134 SDLoc SL(N);
7135
7136 if (Src.getOpcode() == ISD::SETCC) {
7137 SDValue Op0 = Src.getOperand(0);
7138 SDValue Op1 = Src.getOperand(1);
7139 // Need to expand bfloat to float for comparison (setcc).
7140 if (Op0.getValueType() == MVT::bf16) {
7141 Op0 = DAG.getNode(ISD::FP_EXTEND, SL, MVT::f32, Op0);
7142 Op1 = DAG.getNode(ISD::FP_EXTEND, SL, MVT::f32, Op1);
7143 }
7144 // (ballot (ISD::SETCC ...)) -> (AMDGPUISD::SETCC ...)
7145 return DAG.getNode(AMDGPUISD::SETCC, SL, VT, Op0, Op1, Src.getOperand(2));
7146 }
7147 if (const ConstantSDNode *Arg = dyn_cast<ConstantSDNode>(Src)) {
7148 // (ballot 0) -> 0
7149 if (Arg->isZero())
7150 return DAG.getConstant(0, SL, VT);
7151
7152 // (ballot 1) -> EXEC/EXEC_LO
7153 if (Arg->isOne()) {
7154 Register Exec;
7155 if (VT.getScalarSizeInBits() == 32)
7156 Exec = AMDGPU::EXEC_LO;
7157 else if (VT.getScalarSizeInBits() == 64)
7158 Exec = AMDGPU::EXEC;
7159 else
7160 return SDValue();
7161
7162 return DAG.getCopyFromReg(DAG.getEntryNode(), SL, Exec, VT);
7163 }
7164 }
7165
7166 // (ballot (i1 $src)) -> (AMDGPUISD::SETCC (i32 (zext $src)) (i32 0)
7167 // ISD::SETNE)
7168 return DAG.getNode(
7169 AMDGPUISD::SETCC, SL, VT, DAG.getZExtOrTrunc(Src, SL, MVT::i32),
7170 DAG.getConstant(0, SL, MVT::i32), DAG.getCondCode(ISD::SETNE));
7171}
7172
7174 SelectionDAG &DAG) {
7175 EVT VT = N->getValueType(0);
7176 unsigned ValSize = VT.getSizeInBits();
7177 unsigned IID = N->getConstantOperandVal(0);
7178 bool IsPermLane16 = IID == Intrinsic::amdgcn_permlane16 ||
7179 IID == Intrinsic::amdgcn_permlanex16;
7180 bool IsSetInactive = IID == Intrinsic::amdgcn_set_inactive ||
7181 IID == Intrinsic::amdgcn_set_inactive_chain_arg;
7182 SDLoc SL(N);
7183 MVT IntVT = MVT::getIntegerVT(ValSize);
7184 const GCNSubtarget *ST = TLI.getSubtarget();
7185 unsigned SplitSize = 32;
7186 if (IID == Intrinsic::amdgcn_update_dpp && (ValSize % 64 == 0) &&
7187 ST->hasDPALU_DPP() &&
7188 AMDGPU::isLegalDPALU_DPPControl(*ST, N->getConstantOperandVal(3)))
7189 SplitSize = 64;
7190
7191 auto createLaneOp = [&DAG, &SL, N, IID](SDValue Src0, SDValue Src1,
7192 SDValue Src2, MVT ValT) -> SDValue {
7193 SmallVector<SDValue, 8> Operands;
7194 switch (IID) {
7195 case Intrinsic::amdgcn_permlane16:
7196 case Intrinsic::amdgcn_permlanex16:
7197 case Intrinsic::amdgcn_update_dpp:
7198 Operands.push_back(N->getOperand(6));
7199 Operands.push_back(N->getOperand(5));
7200 Operands.push_back(N->getOperand(4));
7201 [[fallthrough]];
7202 case Intrinsic::amdgcn_writelane:
7203 Operands.push_back(Src2);
7204 [[fallthrough]];
7205 case Intrinsic::amdgcn_readlane:
7206 case Intrinsic::amdgcn_set_inactive:
7207 case Intrinsic::amdgcn_set_inactive_chain_arg:
7208 case Intrinsic::amdgcn_mov_dpp8:
7209 Operands.push_back(Src1);
7210 [[fallthrough]];
7211 case Intrinsic::amdgcn_readfirstlane:
7212 case Intrinsic::amdgcn_permlane64:
7213 Operands.push_back(Src0);
7214 break;
7215 default:
7216 llvm_unreachable("unhandled lane op");
7217 }
7218
7219 Operands.push_back(DAG.getTargetConstant(IID, SL, MVT::i32));
7220 std::reverse(Operands.begin(), Operands.end());
7221
7222 if (SDNode *GL = N->getGluedNode()) {
7223 assert(GL->getOpcode() == ISD::CONVERGENCECTRL_GLUE);
7224 GL = GL->getOperand(0).getNode();
7225 Operands.push_back(DAG.getNode(ISD::CONVERGENCECTRL_GLUE, SL, MVT::Glue,
7226 SDValue(GL, 0)));
7227 }
7228
7229 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SL, ValT, Operands);
7230 };
7231
7232 SDValue Src0 = N->getOperand(1);
7233 SDValue Src1, Src2;
7234 if (IID == Intrinsic::amdgcn_readlane || IID == Intrinsic::amdgcn_writelane ||
7235 IID == Intrinsic::amdgcn_mov_dpp8 ||
7236 IID == Intrinsic::amdgcn_update_dpp || IsSetInactive || IsPermLane16) {
7237 Src1 = N->getOperand(2);
7238 if (IID == Intrinsic::amdgcn_writelane ||
7239 IID == Intrinsic::amdgcn_update_dpp || IsPermLane16)
7240 Src2 = N->getOperand(3);
7241 }
7242
7243 if (ValSize == SplitSize) {
7244 // Already legal
7245 return SDValue();
7246 }
7247
7248 if (ValSize < 32) {
7249 bool IsFloat = VT.isFloatingPoint();
7250 Src0 = DAG.getAnyExtOrTrunc(IsFloat ? DAG.getBitcast(IntVT, Src0) : Src0,
7251 SL, MVT::i32);
7252
7253 if (IID == Intrinsic::amdgcn_update_dpp || IsSetInactive || IsPermLane16) {
7254 Src1 = DAG.getAnyExtOrTrunc(IsFloat ? DAG.getBitcast(IntVT, Src1) : Src1,
7255 SL, MVT::i32);
7256 }
7257
7258 if (IID == Intrinsic::amdgcn_writelane) {
7259 Src2 = DAG.getAnyExtOrTrunc(IsFloat ? DAG.getBitcast(IntVT, Src2) : Src2,
7260 SL, MVT::i32);
7261 }
7262
7263 SDValue LaneOp = createLaneOp(Src0, Src1, Src2, MVT::i32);
7264 SDValue Trunc = DAG.getAnyExtOrTrunc(LaneOp, SL, IntVT);
7265 return IsFloat ? DAG.getBitcast(VT, Trunc) : Trunc;
7266 }
7267
7268 if (ValSize % SplitSize != 0)
7269 return SDValue();
7270
7271 auto unrollLaneOp = [&DAG, &SL](SDNode *N) -> SDValue {
7272 EVT VT = N->getValueType(0);
7273 unsigned NE = VT.getVectorNumElements();
7274 EVT EltVT = VT.getVectorElementType();
7276 unsigned NumOperands = N->getNumOperands();
7277 SmallVector<SDValue, 4> Operands(NumOperands);
7278 SDNode *GL = N->getGluedNode();
7279
7280 // only handle convergencectrl_glue
7282
7283 for (unsigned i = 0; i != NE; ++i) {
7284 for (unsigned j = 0, e = GL ? NumOperands - 1 : NumOperands; j != e;
7285 ++j) {
7286 SDValue Operand = N->getOperand(j);
7287 EVT OperandVT = Operand.getValueType();
7288 if (OperandVT.isVector()) {
7289 // A vector operand; extract a single element.
7290 EVT OperandEltVT = OperandVT.getVectorElementType();
7291 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, OperandEltVT,
7292 Operand, DAG.getVectorIdxConstant(i, SL));
7293 } else {
7294 // A scalar operand; just use it as is.
7295 Operands[j] = Operand;
7296 }
7297 }
7298
7299 if (GL)
7300 Operands[NumOperands - 1] =
7301 DAG.getNode(ISD::CONVERGENCECTRL_GLUE, SL, MVT::Glue,
7302 SDValue(GL->getOperand(0).getNode(), 0));
7303
7304 Scalars.push_back(DAG.getNode(N->getOpcode(), SL, EltVT, Operands));
7305 }
7306
7307 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NE);
7308 return DAG.getBuildVector(VecVT, SL, Scalars);
7309 };
7310
7311 if (VT.isVector()) {
7312 switch (MVT::SimpleValueType EltTy =
7314 case MVT::i32:
7315 case MVT::f32:
7316 if (SplitSize == 32) {
7317 SDValue LaneOp = createLaneOp(Src0, Src1, Src2, VT.getSimpleVT());
7318 return unrollLaneOp(LaneOp.getNode());
7319 }
7320 [[fallthrough]];
7321 case MVT::i16:
7322 case MVT::f16:
7323 case MVT::bf16: {
7324 unsigned SubVecNumElt =
7325 SplitSize / VT.getVectorElementType().getSizeInBits();
7326 MVT SubVecVT = MVT::getVectorVT(EltTy, SubVecNumElt);
7328 SDValue Src0SubVec, Src1SubVec, Src2SubVec;
7329 for (unsigned i = 0, EltIdx = 0; i < ValSize / SplitSize; i++) {
7330 Src0SubVec = DAG.getNode(ISD::EXTRACT_SUBVECTOR, SL, SubVecVT, Src0,
7331 DAG.getConstant(EltIdx, SL, MVT::i32));
7332
7333 if (IID == Intrinsic::amdgcn_update_dpp || IsSetInactive ||
7334 IsPermLane16)
7335 Src1SubVec = DAG.getNode(ISD::EXTRACT_SUBVECTOR, SL, SubVecVT, Src1,
7336 DAG.getConstant(EltIdx, SL, MVT::i32));
7337
7338 if (IID == Intrinsic::amdgcn_writelane)
7339 Src2SubVec = DAG.getNode(ISD::EXTRACT_SUBVECTOR, SL, SubVecVT, Src2,
7340 DAG.getConstant(EltIdx, SL, MVT::i32));
7341
7342 Pieces.push_back(
7343 IID == Intrinsic::amdgcn_update_dpp || IsSetInactive || IsPermLane16
7344 ? createLaneOp(Src0SubVec, Src1SubVec, Src2, SubVecVT)
7345 : createLaneOp(Src0SubVec, Src1, Src2SubVec, SubVecVT));
7346 EltIdx += SubVecNumElt;
7347 }
7348 return DAG.getNode(ISD::CONCAT_VECTORS, SL, VT, Pieces);
7349 }
7350 default:
7351 // Handle all other cases by bitcasting to i32 vectors
7352 break;
7353 }
7354 }
7355
7356 MVT VecVT =
7357 MVT::getVectorVT(MVT::getIntegerVT(SplitSize), ValSize / SplitSize);
7358 Src0 = DAG.getBitcast(VecVT, Src0);
7359
7360 if (IID == Intrinsic::amdgcn_update_dpp || IsSetInactive || IsPermLane16)
7361 Src1 = DAG.getBitcast(VecVT, Src1);
7362
7363 if (IID == Intrinsic::amdgcn_writelane)
7364 Src2 = DAG.getBitcast(VecVT, Src2);
7365
7366 SDValue LaneOp = createLaneOp(Src0, Src1, Src2, VecVT);
7367 SDValue UnrolledLaneOp = unrollLaneOp(LaneOp.getNode());
7368 return DAG.getBitcast(VT, UnrolledLaneOp);
7369}
7370
7372 SelectionDAG &DAG) {
7373 EVT VT = N->getValueType(0);
7374
7375 if (VT.getSizeInBits() != 32)
7376 return SDValue();
7377
7378 SDLoc SL(N);
7379
7380 SDValue Value = N->getOperand(1);
7381 SDValue Index = N->getOperand(2);
7382
7383 // ds_bpermute requires index to be multiplied by 4
7384 SDValue ShiftAmount = DAG.getShiftAmountConstant(2, MVT::i32, SL);
7385 SDValue ShiftedIndex =
7386 DAG.getNode(ISD::SHL, SL, Index.getValueType(), Index, ShiftAmount);
7387
7388 // Intrinsics will require i32 to operate on
7389 SDValue ValueI32 = DAG.getBitcast(MVT::i32, Value);
7390
7391 auto MakeIntrinsic = [&DAG, &SL](unsigned IID, MVT RetVT,
7392 SmallVector<SDValue> IntrinArgs) -> SDValue {
7393 SmallVector<SDValue> Operands(1);
7394 Operands[0] = DAG.getTargetConstant(IID, SL, MVT::i32);
7395 Operands.append(IntrinArgs);
7396 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SL, RetVT, Operands);
7397 };
7398
7399 // If we can bpermute across the whole wave, then just do that
7401 SDValue BPermute = MakeIntrinsic(Intrinsic::amdgcn_ds_bpermute, MVT::i32,
7402 {ShiftedIndex, ValueI32});
7403 return DAG.getBitcast(VT, BPermute);
7404 }
7405
7406 assert(TLI.getSubtarget()->isWave64());
7407
7408 // Otherwise, we need to make use of whole wave mode
7409 SDValue PoisonVal = DAG.getPOISON(ValueI32->getValueType(0));
7410
7411 // Set inactive lanes to poison
7412 SDValue WWMValue = MakeIntrinsic(Intrinsic::amdgcn_set_inactive, MVT::i32,
7413 {ValueI32, PoisonVal});
7414 SDValue WWMIndex = MakeIntrinsic(Intrinsic::amdgcn_set_inactive, MVT::i32,
7415 {ShiftedIndex, PoisonVal});
7416
7417 SDValue Swapped =
7418 MakeIntrinsic(Intrinsic::amdgcn_permlane64, MVT::i32, {WWMValue});
7419
7420 // Get permutation of each half, then we'll select which one to use
7421 SDValue BPermSameHalf = MakeIntrinsic(Intrinsic::amdgcn_ds_bpermute, MVT::i32,
7422 {WWMIndex, WWMValue});
7423 SDValue BPermOtherHalf = MakeIntrinsic(Intrinsic::amdgcn_ds_bpermute,
7424 MVT::i32, {WWMIndex, Swapped});
7425 SDValue BPermOtherHalfWWM =
7426 MakeIntrinsic(Intrinsic::amdgcn_wwm, MVT::i32, {BPermOtherHalf});
7427
7428 // Select which side to take the permute from
7429 SDValue ThreadIDMask = DAG.getAllOnesConstant(SL, MVT::i32);
7430 // We can get away with only using mbcnt_lo here since we're only
7431 // trying to detect which side of 32 each lane is on, and mbcnt_lo
7432 // returns 32 for lanes 32-63.
7433 SDValue ThreadID =
7434 MakeIntrinsic(Intrinsic::amdgcn_mbcnt_lo, MVT::i32,
7435 {ThreadIDMask, DAG.getTargetConstant(0, SL, MVT::i32)});
7436
7437 SDValue SameOrOtherHalf =
7438 DAG.getNode(ISD::AND, SL, MVT::i32,
7439 DAG.getNode(ISD::XOR, SL, MVT::i32, ThreadID, Index),
7440 DAG.getTargetConstant(32, SL, MVT::i32));
7441 SDValue UseSameHalf =
7442 DAG.getSetCC(SL, MVT::i1, SameOrOtherHalf,
7443 DAG.getConstant(0, SL, MVT::i32), ISD::SETEQ);
7444 SDValue Result = DAG.getSelect(SL, MVT::i32, UseSameHalf, BPermSameHalf,
7445 BPermOtherHalfWWM);
7446 return DAG.getBitcast(VT, Result);
7447}
7448
7451 SelectionDAG &DAG) const {
7452 switch (N->getOpcode()) {
7454 if (SDValue Res = lowerINSERT_VECTOR_ELT(SDValue(N, 0), DAG))
7455 Results.push_back(Res);
7456 return;
7457 }
7459 if (SDValue Res = lowerEXTRACT_VECTOR_ELT(SDValue(N, 0), DAG))
7460 Results.push_back(Res);
7461 return;
7462 }
7464 unsigned IID = N->getConstantOperandVal(0);
7465 switch (IID) {
7466 case Intrinsic::amdgcn_make_buffer_rsrc:
7467 Results.push_back(lowerPointerAsRsrcIntrin(N, DAG));
7468 return;
7469 case Intrinsic::amdgcn_cvt_pkrtz: {
7470 SDValue Src0 = N->getOperand(1);
7471 SDValue Src1 = N->getOperand(2);
7472 SDLoc SL(N);
7473 SDValue Cvt =
7474 DAG.getNode(AMDGPUISD::CVT_PKRTZ_F16_F32, SL, MVT::i32, Src0, Src1);
7475 Results.push_back(DAG.getNode(ISD::BITCAST, SL, MVT::v2f16, Cvt));
7476 return;
7477 }
7478 case Intrinsic::amdgcn_cvt_pknorm_i16:
7479 case Intrinsic::amdgcn_cvt_pknorm_u16:
7480 case Intrinsic::amdgcn_cvt_pk_i16:
7481 case Intrinsic::amdgcn_cvt_pk_u16: {
7482 SDValue Src0 = N->getOperand(1);
7483 SDValue Src1 = N->getOperand(2);
7484 SDLoc SL(N);
7485 unsigned Opcode;
7486
7487 if (IID == Intrinsic::amdgcn_cvt_pknorm_i16)
7488 Opcode = AMDGPUISD::CVT_PKNORM_I16_F32;
7489 else if (IID == Intrinsic::amdgcn_cvt_pknorm_u16)
7490 Opcode = AMDGPUISD::CVT_PKNORM_U16_F32;
7491 else if (IID == Intrinsic::amdgcn_cvt_pk_i16)
7492 Opcode = AMDGPUISD::CVT_PK_I16_I32;
7493 else
7494 Opcode = AMDGPUISD::CVT_PK_U16_U32;
7495
7496 EVT VT = N->getValueType(0);
7497 if (isTypeLegal(VT))
7498 Results.push_back(DAG.getNode(Opcode, SL, VT, Src0, Src1));
7499 else {
7500 SDValue Cvt = DAG.getNode(Opcode, SL, MVT::i32, Src0, Src1);
7501 Results.push_back(DAG.getNode(ISD::BITCAST, SL, MVT::v2i16, Cvt));
7502 }
7503 return;
7504 }
7505 case Intrinsic::amdgcn_s_buffer_load: {
7506 // Lower llvm.amdgcn.s.buffer.load.(i8, u8) intrinsics. First, we generate
7507 // s_buffer_load_u8 for signed and unsigned load instructions. Next, DAG
7508 // combiner tries to merge the s_buffer_load_u8 with a sext instruction
7509 // (performSignExtendInRegCombine()) and it replaces s_buffer_load_u8 with
7510 // s_buffer_load_i8.
7511 if (!Subtarget->hasScalarSubwordLoads())
7512 return;
7513 SDValue Op = SDValue(N, 0);
7514 SDValue Rsrc = Op.getOperand(1);
7515 SDValue Offset = Op.getOperand(2);
7516 SDValue CachePolicy = Op.getOperand(3);
7517 EVT VT = Op.getValueType();
7518 assert(VT == MVT::i8 && "Expected 8-bit s_buffer_load intrinsics.\n");
7519 SDLoc DL(Op);
7521 const DataLayout &DataLayout = DAG.getDataLayout();
7522 Align Alignment =
7528 VT.getStoreSize(), Alignment);
7529 SDValue LoadVal;
7530 if (!Offset->isDivergent()) {
7531 SDValue Ops[] = {Rsrc, // source register
7532 Offset, CachePolicy};
7533 SDValue BufferLoad =
7534 DAG.getMemIntrinsicNode(AMDGPUISD::SBUFFER_LOAD_UBYTE, DL,
7535 DAG.getVTList(MVT::i32), Ops, VT, MMO);
7536 LoadVal = DAG.getNode(ISD::TRUNCATE, DL, VT, BufferLoad);
7537 } else {
7538 SDValue Ops[] = {
7539 DAG.getEntryNode(), // Chain
7540 Rsrc, // rsrc
7541 DAG.getConstant(0, DL, MVT::i32), // vindex
7542 {}, // voffset
7543 {}, // soffset
7544 {}, // offset
7545 CachePolicy, // cachepolicy
7546 DAG.getTargetConstant(0, DL, MVT::i1), // idxen
7547 };
7548 setBufferOffsets(Offset, DAG, &Ops[3], Align(4));
7549 LoadVal = handleByteShortBufferLoads(DAG, VT, DL, Ops, MMO);
7550 }
7551 Results.push_back(LoadVal);
7552 return;
7553 }
7554 case Intrinsic::amdgcn_dead: {
7555 for (unsigned I = 0, E = N->getNumValues(); I < E; ++I)
7556 Results.push_back(DAG.getPOISON(N->getValueType(I)));
7557 return;
7558 }
7559 }
7560 break;
7561 }
7563 if (SDValue Res = LowerINTRINSIC_W_CHAIN(SDValue(N, 0), DAG)) {
7564 if (Res.getOpcode() == ISD::MERGE_VALUES) {
7565 // FIXME: Hacky
7566 for (unsigned I = 0; I < Res.getNumOperands(); I++) {
7567 Results.push_back(Res.getOperand(I));
7568 }
7569 } else {
7570 Results.push_back(Res);
7571 Results.push_back(Res.getValue(1));
7572 }
7573 return;
7574 }
7575
7576 break;
7577 }
7578 case ISD::SELECT: {
7579 SDLoc SL(N);
7580 EVT VT = N->getValueType(0);
7581 EVT NewVT = getEquivalentMemType(*DAG.getContext(), VT);
7582 SDValue LHS = DAG.getNode(ISD::BITCAST, SL, NewVT, N->getOperand(1));
7583 SDValue RHS = DAG.getNode(ISD::BITCAST, SL, NewVT, N->getOperand(2));
7584
7585 EVT SelectVT = NewVT;
7586 if (NewVT.bitsLT(MVT::i32)) {
7587 LHS = DAG.getNode(ISD::ANY_EXTEND, SL, MVT::i32, LHS);
7588 RHS = DAG.getNode(ISD::ANY_EXTEND, SL, MVT::i32, RHS);
7589 SelectVT = MVT::i32;
7590 }
7591
7592 SDValue NewSelect =
7593 DAG.getNode(ISD::SELECT, SL, SelectVT, N->getOperand(0), LHS, RHS);
7594
7595 if (NewVT != SelectVT)
7596 NewSelect = DAG.getNode(ISD::TRUNCATE, SL, NewVT, NewSelect);
7597 Results.push_back(DAG.getNode(ISD::BITCAST, SL, VT, NewSelect));
7598 return;
7599 }
7600 case ISD::FNEG: {
7601 if (N->getValueType(0) != MVT::v2f16)
7602 break;
7603
7604 SDLoc SL(N);
7605 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::i32, N->getOperand(0));
7606
7607 SDValue Op = DAG.getNode(ISD::XOR, SL, MVT::i32, BC,
7608 DAG.getConstant(0x80008000, SL, MVT::i32));
7609 Results.push_back(DAG.getNode(ISD::BITCAST, SL, MVT::v2f16, Op));
7610 return;
7611 }
7612 case ISD::FABS: {
7613 if (N->getValueType(0) != MVT::v2f16)
7614 break;
7615
7616 SDLoc SL(N);
7617 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::i32, N->getOperand(0));
7618
7619 SDValue Op = DAG.getNode(ISD::AND, SL, MVT::i32, BC,
7620 DAG.getConstant(0x7fff7fff, SL, MVT::i32));
7621 Results.push_back(DAG.getNode(ISD::BITCAST, SL, MVT::v2f16, Op));
7622 return;
7623 }
7624 case ISD::FSQRT: {
7625 if (N->getValueType(0) != MVT::f16)
7626 break;
7627 Results.push_back(lowerFSQRTF16(SDValue(N, 0), DAG));
7628 break;
7629 }
7630 default:
7632 break;
7633 }
7634}
7635
7636/// Helper function for LowerBRCOND
7637static SDNode *findUser(SDValue Value, unsigned Opcode) {
7638
7639 for (SDUse &U : Value->uses()) {
7640 if (U.get() != Value)
7641 continue;
7642
7643 if (U.getUser()->getOpcode() == Opcode)
7644 return U.getUser();
7645 }
7646 return nullptr;
7647}
7648
7649unsigned SITargetLowering::isCFIntrinsic(const SDNode *Intr) const {
7650 if (Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN) {
7651 switch (Intr->getConstantOperandVal(1)) {
7652 case Intrinsic::amdgcn_if:
7653 return AMDGPUISD::IF;
7654 case Intrinsic::amdgcn_else:
7655 return AMDGPUISD::ELSE;
7656 case Intrinsic::amdgcn_loop:
7657 return AMDGPUISD::LOOP;
7658 case Intrinsic::amdgcn_end_cf:
7659 llvm_unreachable("should not occur");
7660 default:
7661 return 0;
7662 }
7663 }
7664
7665 // break, if_break, else_break are all only used as inputs to loop, not
7666 // directly as branch conditions.
7667 return 0;
7668}
7669
7676
7678 if (Subtarget->isAmdPalOS() || Subtarget->isMesa3DOS())
7679 return false;
7680
7681 // FIXME: Either avoid relying on address space here or change the default
7682 // address space for functions to avoid the explicit check.
7683 return (GV->getValueType()->isFunctionTy() ||
7686}
7687
7689 return !shouldEmitFixup(GV) && !shouldEmitGOTReloc(GV);
7690}
7691
7693 if (!GV->hasExternalLinkage())
7694 return true;
7695
7696 const auto OS = getTargetMachine().getTargetTriple().getOS();
7697 return OS == Triple::AMDHSA || OS == Triple::AMDPAL;
7698}
7699
7700/// This transforms the control flow intrinsics to get the branch destination as
7701/// last parameter, also switches branch target with BR if the need arise
7702SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND, SelectionDAG &DAG) const {
7703 SDLoc DL(BRCOND);
7704
7705 SDNode *Intr = BRCOND.getOperand(1).getNode();
7706 SDValue Target = BRCOND.getOperand(2);
7707 SDNode *BR = nullptr;
7708 SDNode *SetCC = nullptr;
7709
7710 switch (Intr->getOpcode()) {
7711 case ISD::SETCC: {
7712 // As long as we negate the condition everything is fine
7713 SetCC = Intr;
7714 Intr = SetCC->getOperand(0).getNode();
7715 break;
7716 }
7717 case ISD::XOR: {
7718 // Similar to SETCC, if we have (xor c, -1), we will be fine.
7719 SDValue LHS = Intr->getOperand(0);
7720 SDValue RHS = Intr->getOperand(1);
7721 if (auto *C = dyn_cast<ConstantSDNode>(RHS); C && C->getZExtValue()) {
7722 Intr = LHS.getNode();
7723 break;
7724 }
7725 [[fallthrough]];
7726 }
7727 default: {
7728 // Get the target from BR if we don't negate the condition
7729 BR = findUser(BRCOND, ISD::BR);
7730 assert(BR && "brcond missing unconditional branch user");
7731 Target = BR->getOperand(1);
7732 }
7733 }
7734
7735 unsigned CFNode = isCFIntrinsic(Intr);
7736 if (CFNode == 0) {
7737 // This is a uniform branch so we don't need to legalize.
7738 return BRCOND;
7739 }
7740
7741 bool HaveChain = Intr->getOpcode() == ISD::INTRINSIC_VOID ||
7743
7744 assert(!SetCC ||
7745 (SetCC->getConstantOperandVal(1) == 1 &&
7746 cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
7747 ISD::SETNE));
7748
7749 // operands of the new intrinsic call
7751 if (HaveChain)
7752 Ops.push_back(BRCOND.getOperand(0));
7753
7754 Ops.append(Intr->op_begin() + (HaveChain ? 2 : 1), Intr->op_end());
7755 Ops.push_back(Target);
7756
7757 ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end());
7758
7759 // build the new intrinsic call
7760 SDNode *Result = DAG.getNode(CFNode, DL, DAG.getVTList(Res), Ops).getNode();
7761
7762 if (!HaveChain) {
7763 SDValue Ops[] = {SDValue(Result, 0), BRCOND.getOperand(0)};
7764
7766 }
7767
7768 if (BR) {
7769 // Give the branch instruction our target
7770 SDValue Ops[] = {BR->getOperand(0), BRCOND.getOperand(2)};
7771 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops);
7772 DAG.ReplaceAllUsesWith(BR, NewBR.getNode());
7773 }
7774
7775 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
7776
7777 // Copy the intrinsic results to registers
7778 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
7779 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
7780 if (!CopyToReg)
7781 continue;
7782
7783 Chain = DAG.getCopyToReg(Chain, DL, CopyToReg->getOperand(1),
7784 SDValue(Result, i - 1), SDValue());
7785
7786 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
7787 }
7788
7789 // Remove the old intrinsic from the chain
7790 DAG.ReplaceAllUsesOfValueWith(SDValue(Intr, Intr->getNumValues() - 1),
7791 Intr->getOperand(0));
7792
7793 return Chain;
7794}
7795
7796SDValue SITargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const {
7797 MVT VT = Op.getSimpleValueType();
7798 SDLoc DL(Op);
7799 // Checking the depth
7800 if (Op.getConstantOperandVal(0) != 0)
7801 return DAG.getConstant(0, DL, VT);
7802
7803 MachineFunction &MF = DAG.getMachineFunction();
7804 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
7805 // Check for kernel and shader functions
7806 if (Info->isEntryFunction())
7807 return DAG.getConstant(0, DL, VT);
7808
7809 MachineFrameInfo &MFI = MF.getFrameInfo();
7810 // There is a call to @llvm.returnaddress in this function
7811 MFI.setReturnAddressIsTaken(true);
7812
7813 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
7814 // Get the return address reg and mark it as an implicit live-in
7815 Register Reg = MF.addLiveIn(TRI->getReturnAddressReg(MF),
7816 getRegClassFor(VT, Op.getNode()->isDivergent()));
7817
7818 return DAG.getCopyFromReg(DAG.getEntryNode(), DL, Reg, VT);
7819}
7820
7821SDValue SITargetLowering::getFPExtOrFPRound(SelectionDAG &DAG, SDValue Op,
7822 const SDLoc &DL, EVT VT) const {
7823 return Op.getValueType().bitsLE(VT)
7824 ? DAG.getNode(ISD::FP_EXTEND, DL, VT, Op)
7825 : DAG.getNode(ISD::FP_ROUND, DL, VT, Op,
7826 DAG.getTargetConstant(0, DL, MVT::i32));
7827}
7828
7829SDValue SITargetLowering::splitFP_ROUNDVectorOp(SDValue Op,
7830 SelectionDAG &DAG) const {
7831 EVT DstVT = Op.getValueType();
7832 unsigned NumElts = DstVT.getVectorNumElements();
7833 assert(NumElts > 2 && isPowerOf2_32(NumElts));
7834
7835 auto [Lo, Hi] = DAG.SplitVectorOperand(Op.getNode(), 0);
7836
7837 SDLoc DL(Op);
7838 unsigned Opc = Op.getOpcode();
7839 SDValue Flags = Op.getOperand(1);
7840 EVT HalfDstVT =
7841 EVT::getVectorVT(*DAG.getContext(), DstVT.getScalarType(), NumElts / 2);
7842 SDValue OpLo = DAG.getNode(Opc, DL, HalfDstVT, Lo, Flags);
7843 SDValue OpHi = DAG.getNode(Opc, DL, HalfDstVT, Hi, Flags);
7844
7845 return DAG.getNode(ISD::CONCAT_VECTORS, DL, DstVT, OpLo, OpHi);
7846}
7847
7848SDValue SITargetLowering::lowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
7849 SDValue Src = Op.getOperand(0);
7850 EVT SrcVT = Src.getValueType();
7851 EVT DstVT = Op.getValueType();
7852
7853 if (DstVT.isVector() && DstVT.getScalarType() == MVT::f16) {
7854 assert(Subtarget->hasCvtPkF16F32Inst() && "support v_cvt_pk_f16_f32");
7855 if (SrcVT.getScalarType() != MVT::f32)
7856 return SDValue();
7857 return SrcVT == MVT::v2f32 ? Op : splitFP_ROUNDVectorOp(Op, DAG);
7858 }
7859
7860 if (SrcVT.getScalarType() != MVT::f64)
7861 return Op;
7862
7863 SDLoc DL(Op);
7864 if (DstVT == MVT::f16) {
7865 // TODO: Handle strictfp
7866 if (Op.getOpcode() != ISD::FP_ROUND)
7867 return Op;
7868
7869 if (!Subtarget->has16BitInsts()) {
7870 SDValue FpToFp16 = DAG.getNode(ISD::FP_TO_FP16, DL, MVT::i32, Src);
7871 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, MVT::i16, FpToFp16);
7872 return DAG.getNode(ISD::BITCAST, DL, MVT::f16, Trunc);
7873 }
7874 if (Op->getFlags().hasApproximateFuncs()) {
7875 SDValue Flags = Op.getOperand(1);
7876 SDValue Src32 = DAG.getNode(ISD::FP_ROUND, DL, MVT::f32, Src, Flags);
7877 return DAG.getNode(ISD::FP_ROUND, DL, MVT::f16, Src32, Flags);
7878 }
7879 SDValue FpToFp16 = LowerF64ToF16Safe(Src, DL, DAG);
7880 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, MVT::i16, FpToFp16);
7881 return DAG.getNode(ISD::BITCAST, DL, MVT::f16, Trunc);
7882 }
7883
7884 assert(DstVT.getScalarType() == MVT::bf16 &&
7885 "custom lower FP_ROUND for f16 or bf16");
7886 assert(Subtarget->hasBF16ConversionInsts() && "f32 -> bf16 is legal");
7887
7888 // Round-inexact-to-odd f64 to f32, then do the final rounding using the
7889 // hardware f32 -> bf16 instruction.
7890 EVT F32VT = SrcVT.changeElementType(*DAG.getContext(), MVT::f32);
7891 SDValue Rod = expandRoundInexactToOdd(F32VT, Src, DL, DAG);
7892 return DAG.getNode(ISD::FP_ROUND, DL, DstVT, Rod,
7893 DAG.getTargetConstant(0, DL, MVT::i32));
7894}
7895
7896SDValue SITargetLowering::lowerFMINNUM_FMAXNUM(SDValue Op,
7897 SelectionDAG &DAG) const {
7898 EVT VT = Op.getValueType();
7899 const MachineFunction &MF = DAG.getMachineFunction();
7900 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
7901 bool IsIEEEMode = Info->getMode().IEEE;
7902
7903 // FIXME: Assert during selection that this is only selected for
7904 // ieee_mode. Currently a combine can produce the ieee version for non-ieee
7905 // mode functions, but this happens to be OK since it's only done in cases
7906 // where there is known no sNaN.
7907 if (IsIEEEMode)
7908 return expandFMINNUM_FMAXNUM(Op.getNode(), DAG);
7909
7910 if (VT == MVT::v4f16 || VT == MVT::v8f16 || VT == MVT::v16f16 ||
7911 VT == MVT::v16bf16)
7912 return splitBinaryVectorOp(Op, DAG);
7913 return Op;
7914}
7915
7916SDValue
7917SITargetLowering::lowerFMINIMUMNUM_FMAXIMUMNUM(SDValue Op,
7918 SelectionDAG &DAG) const {
7919 EVT VT = Op.getValueType();
7920 const MachineFunction &MF = DAG.getMachineFunction();
7921 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
7922 bool IsIEEEMode = Info->getMode().IEEE;
7923
7924 if (IsIEEEMode)
7925 return expandFMINIMUMNUM_FMAXIMUMNUM(Op.getNode(), DAG);
7926
7927 if (VT == MVT::v4f16 || VT == MVT::v8f16 || VT == MVT::v16f16 ||
7928 VT == MVT::v16bf16)
7929 return splitBinaryVectorOp(Op, DAG);
7930 return Op;
7931}
7932
7933SDValue SITargetLowering::lowerFMINIMUM_FMAXIMUM(SDValue Op,
7934 SelectionDAG &DAG) const {
7935 EVT VT = Op.getValueType();
7936 if (VT.isVector())
7937 return splitBinaryVectorOp(Op, DAG);
7938
7939 assert(!Subtarget->hasIEEEMinimumMaximumInsts() &&
7940 !Subtarget->hasMinimum3Maximum3F16() &&
7941 Subtarget->hasMinimum3Maximum3PKF16() && VT == MVT::f16 &&
7942 "should not need to widen f16 minimum/maximum to v2f16");
7943
7944 // Widen f16 operation to v2f16
7945
7946 // fminimum f16:x, f16:y ->
7947 // extract_vector_elt (fminimum (v2f16 (scalar_to_vector x))
7948 // (v2f16 (scalar_to_vector y))), 0
7949 SDLoc SL(Op);
7950 SDValue WideSrc0 =
7951 DAG.getNode(ISD::SCALAR_TO_VECTOR, SL, MVT::v2f16, Op.getOperand(0));
7952 SDValue WideSrc1 =
7953 DAG.getNode(ISD::SCALAR_TO_VECTOR, SL, MVT::v2f16, Op.getOperand(1));
7954
7955 SDValue Widened =
7956 DAG.getNode(Op.getOpcode(), SL, MVT::v2f16, WideSrc0, WideSrc1);
7957
7958 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::f16, Widened,
7959 DAG.getConstant(0, SL, MVT::i32));
7960}
7961
7962SDValue SITargetLowering::lowerFLDEXP(SDValue Op, SelectionDAG &DAG) const {
7963 bool IsStrict = Op.getOpcode() == ISD::STRICT_FLDEXP;
7964 EVT VT = Op.getValueType();
7965 assert(VT == MVT::f16);
7966
7967 SDValue Exp = Op.getOperand(IsStrict ? 2 : 1);
7968 EVT ExpVT = Exp.getValueType();
7969 if (ExpVT == MVT::i16)
7970 return Op;
7971
7972 SDLoc DL(Op);
7973
7974 // Correct the exponent type for f16 to i16.
7975 // Clamp the range of the exponent to the instruction's range.
7976
7977 // TODO: This should be a generic narrowing legalization, and can easily be
7978 // for GlobalISel.
7979
7980 SDValue MinExp = DAG.getSignedConstant(minIntN(16), DL, ExpVT);
7981 SDValue ClampMin = DAG.getNode(ISD::SMAX, DL, ExpVT, Exp, MinExp);
7982
7983 SDValue MaxExp = DAG.getSignedConstant(maxIntN(16), DL, ExpVT);
7984 SDValue Clamp = DAG.getNode(ISD::SMIN, DL, ExpVT, ClampMin, MaxExp);
7985
7986 SDValue TruncExp = DAG.getNode(ISD::TRUNCATE, DL, MVT::i16, Clamp);
7987
7988 if (IsStrict) {
7989 return DAG.getNode(ISD::STRICT_FLDEXP, DL, {VT, MVT::Other},
7990 {Op.getOperand(0), Op.getOperand(1), TruncExp});
7991 }
7992
7993 return DAG.getNode(ISD::FLDEXP, DL, VT, Op.getOperand(0), TruncExp);
7994}
7995
7997 switch (Op->getOpcode()) {
7998 case ISD::SRA:
7999 case ISD::SMIN:
8000 case ISD::SMAX:
8001 return ISD::SIGN_EXTEND;
8002 case ISD::SRL:
8003 case ISD::UMIN:
8004 case ISD::UMAX:
8005 return ISD::ZERO_EXTEND;
8006 case ISD::ADD:
8007 case ISD::SUB:
8008 case ISD::AND:
8009 case ISD::OR:
8010 case ISD::XOR:
8011 case ISD::SHL:
8012 case ISD::SELECT:
8013 case ISD::MUL:
8014 // operation result won't be influenced by garbage high bits.
8015 // TODO: are all of those cases correct, and are there more?
8016 return ISD::ANY_EXTEND;
8017 case ISD::SETCC: {
8018 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8020 }
8021 default:
8022 llvm_unreachable("unexpected opcode!");
8023 }
8024}
8025
8026SDValue SITargetLowering::promoteUniformOpToI32(SDValue Op,
8027 DAGCombinerInfo &DCI) const {
8028 const unsigned Opc = Op.getOpcode();
8029 assert(Opc == ISD::ADD || Opc == ISD::SUB || Opc == ISD::SHL ||
8030 Opc == ISD::SRL || Opc == ISD::SRA || Opc == ISD::AND ||
8031 Opc == ISD::OR || Opc == ISD::XOR || Opc == ISD::MUL ||
8032 Opc == ISD::SETCC || Opc == ISD::SELECT || Opc == ISD::SMIN ||
8033 Opc == ISD::SMAX || Opc == ISD::UMIN || Opc == ISD::UMAX);
8034
8035 EVT OpTy = (Opc != ISD::SETCC) ? Op.getValueType()
8036 : Op->getOperand(0).getValueType();
8037 auto &DAG = DCI.DAG;
8038 auto ExtTy = OpTy.changeElementType(*DAG.getContext(), MVT::i32);
8039
8040 if (DCI.isBeforeLegalizeOps() ||
8041 isNarrowingProfitable(Op.getNode(), ExtTy, OpTy))
8042 return SDValue();
8043
8044 SDLoc DL(Op);
8045 SDValue LHS;
8046 SDValue RHS;
8047 if (Opc == ISD::SELECT) {
8048 LHS = Op->getOperand(1);
8049 RHS = Op->getOperand(2);
8050 } else {
8051 LHS = Op->getOperand(0);
8052 RHS = Op->getOperand(1);
8053 }
8054
8055 const unsigned ExtOp = getExtOpcodeForPromotedOp(Op);
8056 LHS = DAG.getNode(ExtOp, DL, ExtTy, {LHS});
8057
8058 // Special case: for shifts, the RHS always needs a zext.
8059 if (Opc == ISD::SHL || Opc == ISD::SRL || Opc == ISD::SRA)
8060 RHS = DAG.getNode(ISD::ZERO_EXTEND, DL, ExtTy, {RHS});
8061 else
8062 RHS = DAG.getNode(ExtOp, DL, ExtTy, {RHS});
8063
8064 // setcc always return i1/i1 vec so no need to truncate after.
8065 if (Opc == ISD::SETCC) {
8066 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8067 return DAG.getSetCC(DL, Op.getValueType(), LHS, RHS, CC);
8068 }
8069
8070 // For other ops, we extend the operation's return type as well so we need to
8071 // truncate back to the original type.
8072 SDValue NewVal;
8073 if (Opc == ISD::SELECT)
8074 NewVal = DAG.getNode(ISD::SELECT, DL, ExtTy, {Op->getOperand(0), LHS, RHS});
8075 else
8076 NewVal = DAG.getNode(Opc, DL, ExtTy, {LHS, RHS});
8077
8078 return DAG.getZExtOrTrunc(NewVal, DL, OpTy);
8079}
8080
8081SDValue SITargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
8082 SDValue Mag = Op.getOperand(0);
8083 EVT MagVT = Mag.getValueType();
8084
8085 if (MagVT.getVectorNumElements() > 2)
8086 return splitBinaryVectorOp(Op, DAG);
8087
8088 SDValue Sign = Op.getOperand(1);
8089 EVT SignVT = Sign.getValueType();
8090
8091 if (MagVT == SignVT)
8092 return Op;
8093
8094 // fcopysign v2f16:mag, v2f32:sign ->
8095 // fcopysign v2f16:mag, bitcast (trunc (bitcast sign to v2i32) to v2i16)
8096
8097 SDLoc SL(Op);
8098 SDValue SignAsInt32 = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Sign);
8099 SDValue SignAsInt16 = DAG.getNode(ISD::TRUNCATE, SL, MVT::v2i16, SignAsInt32);
8100
8101 SDValue SignAsHalf16 = DAG.getNode(ISD::BITCAST, SL, MagVT, SignAsInt16);
8102
8103 return DAG.getNode(ISD::FCOPYSIGN, SL, MagVT, Mag, SignAsHalf16);
8104}
8105
8106// Custom lowering for vector multiplications and s_mul_u64.
8107SDValue SITargetLowering::lowerMUL(SDValue Op, SelectionDAG &DAG) const {
8108 EVT VT = Op.getValueType();
8109
8110 // Split vector operands.
8111 if (VT.isVector())
8112 return splitBinaryVectorOp(Op, DAG);
8113
8114 assert(VT == MVT::i64 && "The following code is a special for s_mul_u64");
8115
8116 // There are four ways to lower s_mul_u64:
8117 //
8118 // 1. If all the operands are uniform, then we lower it as it is.
8119 //
8120 // 2. If the operands are divergent, then we have to split s_mul_u64 in 32-bit
8121 // multiplications because there is not a vector equivalent of s_mul_u64.
8122 //
8123 // 3. If the cost model decides that it is more efficient to use vector
8124 // registers, then we have to split s_mul_u64 in 32-bit multiplications.
8125 // This happens in splitScalarSMULU64() in SIInstrInfo.cpp .
8126 //
8127 // 4. If the cost model decides to use vector registers and both of the
8128 // operands are zero-extended/sign-extended from 32-bits, then we split the
8129 // s_mul_u64 in two 32-bit multiplications. The problem is that it is not
8130 // possible to check if the operands are zero-extended or sign-extended in
8131 // SIInstrInfo.cpp. For this reason, here, we replace s_mul_u64 with
8132 // s_mul_u64_u32_pseudo if both operands are zero-extended and we replace
8133 // s_mul_u64 with s_mul_i64_i32_pseudo if both operands are sign-extended.
8134 // If the cost model decides that we have to use vector registers, then
8135 // splitScalarSMulPseudo() (in SIInstrInfo.cpp) split s_mul_u64_u32/
8136 // s_mul_i64_i32_pseudo in two vector multiplications. If the cost model
8137 // decides that we should use scalar registers, then s_mul_u64_u32_pseudo/
8138 // s_mul_i64_i32_pseudo is lowered as s_mul_u64 in expandPostRAPseudo() in
8139 // SIInstrInfo.cpp .
8140
8141 if (Op->isDivergent())
8142 return SDValue();
8143
8144 SDValue Op0 = Op.getOperand(0);
8145 SDValue Op1 = Op.getOperand(1);
8146 // If all the operands are zero-enteted to 32-bits, then we replace s_mul_u64
8147 // with s_mul_u64_u32_pseudo. If all the operands are sign-extended to
8148 // 32-bits, then we replace s_mul_u64 with s_mul_i64_i32_pseudo.
8149 KnownBits Op0KnownBits = DAG.computeKnownBits(Op0);
8150 unsigned Op0LeadingZeros = Op0KnownBits.countMinLeadingZeros();
8151 KnownBits Op1KnownBits = DAG.computeKnownBits(Op1);
8152 unsigned Op1LeadingZeros = Op1KnownBits.countMinLeadingZeros();
8153 SDLoc SL(Op);
8154 if (Op0LeadingZeros >= 32 && Op1LeadingZeros >= 32)
8155 return SDValue(
8156 DAG.getMachineNode(AMDGPU::S_MUL_U64_U32_PSEUDO, SL, VT, Op0, Op1), 0);
8157 unsigned Op0SignBits = DAG.ComputeNumSignBits(Op0);
8158 unsigned Op1SignBits = DAG.ComputeNumSignBits(Op1);
8159 if (Op0SignBits >= 33 && Op1SignBits >= 33)
8160 return SDValue(
8161 DAG.getMachineNode(AMDGPU::S_MUL_I64_I32_PSEUDO, SL, VT, Op0, Op1), 0);
8162 // If all the operands are uniform, then we lower s_mul_u64 as it is.
8163 return Op;
8164}
8165
8166SDValue SITargetLowering::lowerXMULO(SDValue Op, SelectionDAG &DAG) const {
8167 EVT VT = Op.getValueType();
8168 SDLoc SL(Op);
8169 SDValue LHS = Op.getOperand(0);
8170 SDValue RHS = Op.getOperand(1);
8171 bool isSigned = Op.getOpcode() == ISD::SMULO;
8172
8173 if (ConstantSDNode *RHSC = isConstOrConstSplat(RHS)) {
8174 const APInt &C = RHSC->getAPIntValue();
8175 // mulo(X, 1 << S) -> { X << S, (X << S) >> S != X }
8176 if (C.isPowerOf2()) {
8177 // smulo(x, signed_min) is same as umulo(x, signed_min).
8178 bool UseArithShift = isSigned && !C.isMinSignedValue();
8179 SDValue ShiftAmt = DAG.getConstant(C.logBase2(), SL, MVT::i32);
8180 SDValue Result = DAG.getNode(ISD::SHL, SL, VT, LHS, ShiftAmt);
8181 SDValue Overflow =
8182 DAG.getSetCC(SL, MVT::i1,
8183 DAG.getNode(UseArithShift ? ISD::SRA : ISD::SRL, SL, VT,
8184 Result, ShiftAmt),
8185 LHS, ISD::SETNE);
8186 return DAG.getMergeValues({Result, Overflow}, SL);
8187 }
8188 }
8189
8190 SDValue Result = DAG.getNode(ISD::MUL, SL, VT, LHS, RHS);
8191 SDValue Top =
8192 DAG.getNode(isSigned ? ISD::MULHS : ISD::MULHU, SL, VT, LHS, RHS);
8193
8194 SDValue Sign = isSigned
8195 ? DAG.getNode(ISD::SRA, SL, VT, Result,
8196 DAG.getConstant(VT.getScalarSizeInBits() - 1,
8197 SL, MVT::i32))
8198 : DAG.getConstant(0, SL, VT);
8199 SDValue Overflow = DAG.getSetCC(SL, MVT::i1, Top, Sign, ISD::SETNE);
8200
8201 return DAG.getMergeValues({Result, Overflow}, SL);
8202}
8203
8204SDValue SITargetLowering::lowerXMUL_LOHI(SDValue Op, SelectionDAG &DAG) const {
8205 if (Op->isDivergent()) {
8206 // Select to V_MAD_[IU]64_[IU]32.
8207 return Op;
8208 }
8209 if (Subtarget->hasSMulHi()) {
8210 // Expand to S_MUL_I32 + S_MUL_HI_[IU]32.
8211 return SDValue();
8212 }
8213 // The multiply is uniform but we would have to use V_MUL_HI_[IU]32 to
8214 // calculate the high part, so we might as well do the whole thing with
8215 // V_MAD_[IU]64_[IU]32.
8216 return Op;
8217}
8218
8219SDValue SITargetLowering::lowerTRAP(SDValue Op, SelectionDAG &DAG) const {
8220 if (!Subtarget->isTrapHandlerEnabled() ||
8221 Subtarget->getTrapHandlerAbi() != GCNSubtarget::TrapHandlerAbi::AMDHSA)
8222 return lowerTrapEndpgm(Op, DAG);
8223
8224 return Subtarget->supportsGetDoorbellID() ? lowerTrapHsa(Op, DAG)
8225 : lowerTrapHsaQueuePtr(Op, DAG);
8226}
8227
8228SDValue SITargetLowering::lowerTrapEndpgm(SDValue Op, SelectionDAG &DAG) const {
8229 SDLoc SL(Op);
8230 SDValue Chain = Op.getOperand(0);
8231 return DAG.getNode(AMDGPUISD::ENDPGM_TRAP, SL, MVT::Other, Chain);
8232}
8233
8234SDValue
8235SITargetLowering::loadImplicitKernelArgument(SelectionDAG &DAG, MVT VT,
8236 const SDLoc &DL, Align Alignment,
8237 ImplicitParameter Param) const {
8238 MachineFunction &MF = DAG.getMachineFunction();
8239 uint64_t Offset = getImplicitParameterOffset(MF, Param);
8240 SDValue Ptr = lowerKernArgParameterPtr(DAG, DL, DAG.getEntryNode(), Offset);
8241 MachinePointerInfo PtrInfo =
8243 return DAG.getLoad(
8244 VT, DL, DAG.getEntryNode(), Ptr, PtrInfo.getWithOffset(Offset), Alignment,
8246}
8247
8248SDValue SITargetLowering::lowerTrapHsaQueuePtr(SDValue Op,
8249 SelectionDAG &DAG) const {
8250 SDLoc SL(Op);
8251 SDValue Chain = Op.getOperand(0);
8252
8253 SDValue QueuePtr;
8254 // For code object version 5, QueuePtr is passed through implicit kernarg.
8255 const Module *M = DAG.getMachineFunction().getFunction().getParent();
8257 QueuePtr =
8258 loadImplicitKernelArgument(DAG, MVT::i64, SL, Align(8), QUEUE_PTR);
8259 } else {
8260 MachineFunction &MF = DAG.getMachineFunction();
8261 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
8262 Register UserSGPR = Info->getQueuePtrUserSGPR();
8263
8264 if (UserSGPR == AMDGPU::NoRegister) {
8265 // We probably are in a function incorrectly marked with
8266 // amdgpu-no-queue-ptr. This is undefined. We don't want to delete the
8267 // trap, so just use a null pointer.
8268 QueuePtr = DAG.getConstant(0, SL, MVT::i64);
8269 } else {
8270 QueuePtr = CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, UserSGPR,
8271 MVT::i64);
8272 }
8273 }
8274
8275 SDValue SGPR01 = DAG.getRegister(AMDGPU::SGPR0_SGPR1, MVT::i64);
8276 SDValue ToReg = DAG.getCopyToReg(Chain, SL, SGPR01, QueuePtr, SDValue());
8277
8278 uint64_t TrapID = static_cast<uint64_t>(GCNSubtarget::TrapID::LLVMAMDHSATrap);
8279 SDValue Ops[] = {ToReg, DAG.getTargetConstant(TrapID, SL, MVT::i16), SGPR01,
8280 ToReg.getValue(1)};
8281 return DAG.getNode(AMDGPUISD::TRAP, SL, MVT::Other, Ops);
8282}
8283
8284SDValue SITargetLowering::lowerTrapHsa(SDValue Op, SelectionDAG &DAG) const {
8285 SDLoc SL(Op);
8286 SDValue Chain = Op.getOperand(0);
8287
8288 // We need to simulate the 's_trap 2' instruction on targets that run in
8289 // PRIV=1 (where it is treated as a nop).
8290 if (Subtarget->hasPrivEnabledTrap2NopBug())
8291 return DAG.getNode(AMDGPUISD::SIMULATED_TRAP, SL, MVT::Other, Chain);
8292
8293 uint64_t TrapID = static_cast<uint64_t>(GCNSubtarget::TrapID::LLVMAMDHSATrap);
8294 SDValue Ops[] = {Chain, DAG.getTargetConstant(TrapID, SL, MVT::i16)};
8295 return DAG.getNode(AMDGPUISD::TRAP, SL, MVT::Other, Ops);
8296}
8297
8298SDValue SITargetLowering::lowerDEBUGTRAP(SDValue Op, SelectionDAG &DAG) const {
8299 SDLoc SL(Op);
8300 SDValue Chain = Op.getOperand(0);
8301 MachineFunction &MF = DAG.getMachineFunction();
8302
8303 if (!Subtarget->isTrapHandlerEnabled() ||
8304 Subtarget->getTrapHandlerAbi() != GCNSubtarget::TrapHandlerAbi::AMDHSA) {
8305 LLVMContext &Ctx = MF.getFunction().getContext();
8306 Ctx.diagnose(DiagnosticInfoUnsupported(MF.getFunction(),
8307 "debugtrap handler not supported",
8308 Op.getDebugLoc(), DS_Warning));
8309 return Chain;
8310 }
8311
8312 uint64_t TrapID =
8313 static_cast<uint64_t>(GCNSubtarget::TrapID::LLVMAMDHSADebugTrap);
8314 SDValue Ops[] = {Chain, DAG.getTargetConstant(TrapID, SL, MVT::i16)};
8315 return DAG.getNode(AMDGPUISD::TRAP, SL, MVT::Other, Ops);
8316}
8317
8318SDValue SITargetLowering::getSegmentAperture(unsigned AS, const SDLoc &DL,
8319 SelectionDAG &DAG) const {
8320 if (Subtarget->hasApertureRegs()) {
8321 const unsigned ApertureRegNo = (AS == AMDGPUAS::LOCAL_ADDRESS)
8322 ? AMDGPU::SRC_SHARED_BASE
8323 : AMDGPU::SRC_PRIVATE_BASE;
8324 assert((ApertureRegNo != AMDGPU::SRC_PRIVATE_BASE ||
8325 !Subtarget->hasGloballyAddressableScratch()) &&
8326 "Cannot use src_private_base with globally addressable scratch!");
8327 // Note: this feature (register) is broken. When used as a 32-bit operand,
8328 // it returns a wrong value (all zeroes?). The real value is in the upper 32
8329 // bits.
8330 //
8331 // To work around the issue, emit a 64 bit copy from this register
8332 // then extract the high bits. Note that this shouldn't even result in a
8333 // shift being emitted and simply become a pair of registers (e.g.):
8334 // s_mov_b64 s[6:7], src_shared_base
8335 // v_mov_b32_e32 v1, s7
8336 SDValue Copy =
8337 DAG.getCopyFromReg(DAG.getEntryNode(), DL, ApertureRegNo, MVT::v2i32);
8338 return DAG.getExtractVectorElt(DL, MVT::i32, Copy, 1);
8339 }
8340
8341 // For code object version 5, private_base and shared_base are passed through
8342 // implicit kernargs.
8343 const Module *M = DAG.getMachineFunction().getFunction().getParent();
8347 return loadImplicitKernelArgument(DAG, MVT::i32, DL, Align(4), Param);
8348 }
8349
8350 MachineFunction &MF = DAG.getMachineFunction();
8351 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
8352 Register UserSGPR = Info->getQueuePtrUserSGPR();
8353 if (UserSGPR == AMDGPU::NoRegister) {
8354 // We probably are in a function incorrectly marked with
8355 // amdgpu-no-queue-ptr. This is undefined.
8356 return DAG.getPOISON(MVT::i32);
8357 }
8358
8359 SDValue QueuePtr =
8360 CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, UserSGPR, MVT::i64);
8361
8362 // Offset into amd_queue_t for group_segment_aperture_base_hi /
8363 // private_segment_aperture_base_hi.
8364 uint32_t StructOffset = (AS == AMDGPUAS::LOCAL_ADDRESS) ? 0x40 : 0x44;
8365
8366 SDValue Ptr =
8367 DAG.getObjectPtrOffset(DL, QueuePtr, TypeSize::getFixed(StructOffset));
8368
8369 // TODO: Use custom target PseudoSourceValue.
8370 // TODO: We should use the value from the IR intrinsic call, but it might not
8371 // be available and how do we get it?
8372 MachinePointerInfo PtrInfo(AMDGPUAS::CONSTANT_ADDRESS);
8373 return DAG.getLoad(MVT::i32, DL, QueuePtr.getValue(1), Ptr, PtrInfo,
8374 commonAlignment(Align(64), StructOffset),
8377}
8378
8379/// Return true if the value is a known valid address, such that a null check is
8380/// not necessary.
8382 const AMDGPUTargetMachine &TM, unsigned AddrSpace) {
8384 return true;
8385
8386 if (auto *ConstVal = dyn_cast<ConstantSDNode>(Val))
8387 return ConstVal->getSExtValue() != TM.getNullPointerValue(AddrSpace);
8388
8389 // TODO: Search through arithmetic, handle arguments and loads
8390 // marked nonnull.
8391 return false;
8392}
8393
8394SDValue SITargetLowering::lowerADDRSPACECAST(SDValue Op,
8395 SelectionDAG &DAG) const {
8396 SDLoc SL(Op);
8397
8398 const AMDGPUTargetMachine &TM =
8399 static_cast<const AMDGPUTargetMachine &>(getTargetMachine());
8400
8401 unsigned DestAS, SrcAS;
8402 SDValue Src;
8403 bool IsNonNull = false;
8404 if (const auto *ASC = dyn_cast<AddrSpaceCastSDNode>(Op)) {
8405 SrcAS = ASC->getSrcAddressSpace();
8406 Src = ASC->getOperand(0);
8407 DestAS = ASC->getDestAddressSpace();
8408 } else {
8409 assert(Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
8410 Op.getConstantOperandVal(0) ==
8411 Intrinsic::amdgcn_addrspacecast_nonnull);
8412 Src = Op->getOperand(1);
8413 SrcAS = Op->getConstantOperandVal(2);
8414 DestAS = Op->getConstantOperandVal(3);
8415 IsNonNull = true;
8416 }
8417
8418 SDValue FlatNullPtr = DAG.getConstant(0, SL, MVT::i64);
8419
8420 // flat -> local/private
8421 if (SrcAS == AMDGPUAS::FLAT_ADDRESS) {
8422 if (DestAS == AMDGPUAS::LOCAL_ADDRESS ||
8423 DestAS == AMDGPUAS::PRIVATE_ADDRESS) {
8424 SDValue Ptr = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, Src);
8425
8426 if (DestAS == AMDGPUAS::PRIVATE_ADDRESS &&
8427 Subtarget->hasGloballyAddressableScratch()) {
8428 // flat -> private with globally addressable scratch: subtract
8429 // src_flat_scratch_base_lo.
8430 SDValue FlatScratchBaseLo(
8431 DAG.getMachineNode(
8432 AMDGPU::S_MOV_B32, SL, MVT::i32,
8433 DAG.getRegister(AMDGPU::SRC_FLAT_SCRATCH_BASE_LO, MVT::i32)),
8434 0);
8435 Ptr = DAG.getNode(ISD::SUB, SL, MVT::i32, Ptr, FlatScratchBaseLo);
8436 }
8437
8438 if (IsNonNull || isKnownNonNull(Op, DAG, TM, SrcAS))
8439 return Ptr;
8440
8441 unsigned NullVal = TM.getNullPointerValue(DestAS);
8442 SDValue SegmentNullPtr = DAG.getConstant(NullVal, SL, MVT::i32);
8443 SDValue NonNull = DAG.getSetCC(SL, MVT::i1, Src, FlatNullPtr, ISD::SETNE);
8444
8445 return DAG.getNode(ISD::SELECT, SL, MVT::i32, NonNull, Ptr,
8446 SegmentNullPtr);
8447 }
8448 }
8449
8450 // local/private -> flat
8451 if (DestAS == AMDGPUAS::FLAT_ADDRESS) {
8452 if (SrcAS == AMDGPUAS::LOCAL_ADDRESS ||
8453 SrcAS == AMDGPUAS::PRIVATE_ADDRESS) {
8454 SDValue CvtPtr;
8455 if (SrcAS == AMDGPUAS::PRIVATE_ADDRESS &&
8456 Subtarget->hasGloballyAddressableScratch()) {
8457 // For wave32: Addr = (TID[4:0] << 52) + FLAT_SCRATCH_BASE + privateAddr
8458 // For wave64: Addr = (TID[5:0] << 51) + FLAT_SCRATCH_BASE + privateAddr
8459 SDValue AllOnes = DAG.getSignedTargetConstant(-1, SL, MVT::i32);
8460 SDValue ThreadID = DAG.getConstant(0, SL, MVT::i32);
8461 ThreadID = DAG.getNode(
8462 ISD::INTRINSIC_WO_CHAIN, SL, MVT::i32,
8463 DAG.getTargetConstant(Intrinsic::amdgcn_mbcnt_lo, SL, MVT::i32),
8464 AllOnes, ThreadID);
8465 if (Subtarget->isWave64())
8466 ThreadID = DAG.getNode(
8467 ISD::INTRINSIC_WO_CHAIN, SL, MVT::i32,
8468 DAG.getTargetConstant(Intrinsic::amdgcn_mbcnt_hi, SL, MVT::i32),
8469 AllOnes, ThreadID);
8470 SDValue ShAmt = DAG.getShiftAmountConstant(
8471 57 - 32 - Subtarget->getWavefrontSizeLog2(), MVT::i32, SL);
8472 SDValue SrcHi = DAG.getNode(ISD::SHL, SL, MVT::i32, ThreadID, ShAmt);
8473 CvtPtr = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, Src, SrcHi);
8474 CvtPtr = DAG.getNode(ISD::BITCAST, SL, MVT::i64, CvtPtr);
8475 // Accessing src_flat_scratch_base_lo as a 64-bit operand gives the full
8476 // 64-bit hi:lo value.
8477 SDValue FlatScratchBase = {
8478 DAG.getMachineNode(
8479 AMDGPU::S_MOV_B64, SL, MVT::i64,
8480 DAG.getRegister(AMDGPU::SRC_FLAT_SCRATCH_BASE, MVT::i64)),
8481 0};
8482 CvtPtr = DAG.getNode(ISD::ADD, SL, MVT::i64, CvtPtr, FlatScratchBase);
8483 } else {
8484 SDValue Aperture = getSegmentAperture(SrcAS, SL, DAG);
8485 CvtPtr = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, Src, Aperture);
8486 CvtPtr = DAG.getNode(ISD::BITCAST, SL, MVT::i64, CvtPtr);
8487 }
8488
8489 if (IsNonNull || isKnownNonNull(Op, DAG, TM, SrcAS))
8490 return CvtPtr;
8491
8492 unsigned NullVal = TM.getNullPointerValue(SrcAS);
8493 SDValue SegmentNullPtr = DAG.getConstant(NullVal, SL, MVT::i32);
8494
8495 SDValue NonNull =
8496 DAG.getSetCC(SL, MVT::i1, Src, SegmentNullPtr, ISD::SETNE);
8497
8498 return DAG.getNode(ISD::SELECT, SL, MVT::i64, NonNull, CvtPtr,
8499 FlatNullPtr);
8500 }
8501 }
8502
8503 if (SrcAS == AMDGPUAS::CONSTANT_ADDRESS_32BIT &&
8504 Op.getValueType() == MVT::i64) {
8505 const SIMachineFunctionInfo *Info =
8506 DAG.getMachineFunction().getInfo<SIMachineFunctionInfo>();
8507 if (Info->get32BitAddressHighBits() == 0)
8508 return DAG.getNode(ISD::ZERO_EXTEND, SL, MVT::i64, Src);
8509
8510 SDValue Hi = DAG.getConstant(Info->get32BitAddressHighBits(), SL, MVT::i32);
8511 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, Src, Hi);
8512 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
8513 }
8514
8515 if (DestAS == AMDGPUAS::CONSTANT_ADDRESS_32BIT &&
8516 Src.getValueType() == MVT::i64)
8517 return DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, Src);
8518
8519 // global <-> flat are no-ops and never emitted.
8520
8521 // Invalid casts are poison.
8522 return DAG.getPOISON(Op->getValueType(0));
8523}
8524
8525// This lowers an INSERT_SUBVECTOR by extracting the individual elements from
8526// the small vector and inserting them into the big vector. That is better than
8527// the default expansion of doing it via a stack slot. Even though the use of
8528// the stack slot would be optimized away afterwards, the stack slot itself
8529// remains.
8530SDValue SITargetLowering::lowerINSERT_SUBVECTOR(SDValue Op,
8531 SelectionDAG &DAG) const {
8532 SDValue Vec = Op.getOperand(0);
8533 SDValue Ins = Op.getOperand(1);
8534 SDValue Idx = Op.getOperand(2);
8535 EVT VecVT = Vec.getValueType();
8536 EVT InsVT = Ins.getValueType();
8537 EVT EltVT = VecVT.getVectorElementType();
8538 unsigned InsNumElts = InsVT.getVectorNumElements();
8539 unsigned IdxVal = Idx->getAsZExtVal();
8540 SDLoc SL(Op);
8541
8542 if (EltVT.getScalarSizeInBits() == 16 && IdxVal % 2 == 0) {
8543 // Insert 32-bit registers at a time.
8544 assert(InsNumElts % 2 == 0 && "expect legal vector types");
8545
8546 unsigned VecNumElts = VecVT.getVectorNumElements();
8547 EVT NewVecVT =
8548 EVT::getVectorVT(*DAG.getContext(), MVT::i32, VecNumElts / 2);
8549 EVT NewInsVT = InsNumElts == 2 ? MVT::i32
8551 MVT::i32, InsNumElts / 2);
8552
8553 Vec = DAG.getNode(ISD::BITCAST, SL, NewVecVT, Vec);
8554 Ins = DAG.getNode(ISD::BITCAST, SL, NewInsVT, Ins);
8555
8556 for (unsigned I = 0; I != InsNumElts / 2; ++I) {
8557 SDValue Elt;
8558 if (InsNumElts == 2) {
8559 Elt = Ins;
8560 } else {
8561 Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Ins,
8562 DAG.getConstant(I, SL, MVT::i32));
8563 }
8564 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, SL, NewVecVT, Vec, Elt,
8565 DAG.getConstant(IdxVal / 2 + I, SL, MVT::i32));
8566 }
8567
8568 return DAG.getNode(ISD::BITCAST, SL, VecVT, Vec);
8569 }
8570
8571 for (unsigned I = 0; I != InsNumElts; ++I) {
8572 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, Ins,
8573 DAG.getConstant(I, SL, MVT::i32));
8574 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, SL, VecVT, Vec, Elt,
8575 DAG.getConstant(IdxVal + I, SL, MVT::i32));
8576 }
8577 return Vec;
8578}
8579
8580SDValue SITargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op,
8581 SelectionDAG &DAG) const {
8582 SDValue Vec = Op.getOperand(0);
8583 SDValue InsVal = Op.getOperand(1);
8584 SDValue Idx = Op.getOperand(2);
8585 EVT VecVT = Vec.getValueType();
8586 EVT EltVT = VecVT.getVectorElementType();
8587 unsigned VecSize = VecVT.getSizeInBits();
8588 unsigned EltSize = EltVT.getSizeInBits();
8589 SDLoc SL(Op);
8590
8591 // Specially handle the case of v4i16 with static indexing.
8592 unsigned NumElts = VecVT.getVectorNumElements();
8593 auto *KIdx = dyn_cast<ConstantSDNode>(Idx);
8594 if (NumElts == 4 && EltSize == 16 && KIdx) {
8595 SDValue BCVec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Vec);
8596
8597 SDValue LoHalf = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BCVec,
8598 DAG.getConstant(0, SL, MVT::i32));
8599 SDValue HiHalf = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BCVec,
8600 DAG.getConstant(1, SL, MVT::i32));
8601
8602 SDValue LoVec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i16, LoHalf);
8603 SDValue HiVec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i16, HiHalf);
8604
8605 unsigned Idx = KIdx->getZExtValue();
8606 bool InsertLo = Idx < 2;
8607 SDValue InsHalf = DAG.getNode(
8608 ISD::INSERT_VECTOR_ELT, SL, MVT::v2i16, InsertLo ? LoVec : HiVec,
8609 DAG.getNode(ISD::BITCAST, SL, MVT::i16, InsVal),
8610 DAG.getConstant(InsertLo ? Idx : (Idx - 2), SL, MVT::i32));
8611
8612 InsHalf = DAG.getNode(ISD::BITCAST, SL, MVT::i32, InsHalf);
8613
8614 SDValue Concat =
8615 InsertLo ? DAG.getBuildVector(MVT::v2i32, SL, {InsHalf, HiHalf})
8616 : DAG.getBuildVector(MVT::v2i32, SL, {LoHalf, InsHalf});
8617
8618 return DAG.getNode(ISD::BITCAST, SL, VecVT, Concat);
8619 }
8620
8621 // Static indexing does not lower to stack access, and hence there is no need
8622 // for special custom lowering to avoid stack access.
8623 if (isa<ConstantSDNode>(Idx))
8624 return SDValue();
8625
8626 // Avoid stack access for dynamic indexing by custom lowering to
8627 // v_bfi_b32 (v_bfm_b32 16, (shl idx, 16)), val, vec
8628
8629 assert(VecSize <= 64 && "Expected target vector size to be <= 64 bits");
8630
8631 MVT IntVT = MVT::getIntegerVT(VecSize);
8632
8633 // Convert vector index to bit-index and get the required bit mask.
8634 assert(isPowerOf2_32(EltSize));
8635 const auto EltMask = maskTrailingOnes<uint64_t>(EltSize);
8636 SDValue ScaleFactor = DAG.getConstant(Log2_32(EltSize), SL, MVT::i32);
8637 SDValue ScaledIdx = DAG.getNode(ISD::SHL, SL, MVT::i32, Idx, ScaleFactor);
8638 SDValue BFM = DAG.getNode(ISD::SHL, SL, IntVT,
8639 DAG.getConstant(EltMask, SL, IntVT), ScaledIdx);
8640
8641 // 1. Create a congruent vector with the target value in each element.
8642 SDValue ExtVal = DAG.getNode(ISD::BITCAST, SL, IntVT,
8643 DAG.getSplatBuildVector(VecVT, SL, InsVal));
8644
8645 // 2. Mask off all other indices except the required index within (1).
8646 SDValue LHS = DAG.getNode(ISD::AND, SL, IntVT, BFM, ExtVal);
8647
8648 // 3. Mask off the required index within the target vector.
8649 SDValue BCVec = DAG.getNode(ISD::BITCAST, SL, IntVT, Vec);
8650 SDValue RHS =
8651 DAG.getNode(ISD::AND, SL, IntVT, DAG.getNOT(SL, BFM, IntVT), BCVec);
8652
8653 // 4. Get (2) and (3) ORed into the target vector.
8654 SDValue BFI =
8655 DAG.getNode(ISD::OR, SL, IntVT, LHS, RHS, SDNodeFlags::Disjoint);
8656
8657 return DAG.getNode(ISD::BITCAST, SL, VecVT, BFI);
8658}
8659
8660SDValue SITargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op,
8661 SelectionDAG &DAG) const {
8662 SDLoc SL(Op);
8663
8664 EVT ResultVT = Op.getValueType();
8665 SDValue Vec = Op.getOperand(0);
8666 SDValue Idx = Op.getOperand(1);
8667 EVT VecVT = Vec.getValueType();
8668 unsigned VecSize = VecVT.getSizeInBits();
8669 EVT EltVT = VecVT.getVectorElementType();
8670
8671 DAGCombinerInfo DCI(DAG, AfterLegalizeVectorOps, true, nullptr);
8672
8673 // Make sure we do any optimizations that will make it easier to fold
8674 // source modifiers before obscuring it with bit operations.
8675
8676 // XXX - Why doesn't this get called when vector_shuffle is expanded?
8677 if (SDValue Combined = performExtractVectorEltCombine(Op.getNode(), DCI))
8678 return Combined;
8679
8680 if (VecSize == 128 || VecSize == 256 || VecSize == 512) {
8681 SDValue Lo, Hi;
8682 auto [LoVT, HiVT] = DAG.GetSplitDestVTs(VecVT);
8683
8684 if (VecSize == 128) {
8685 SDValue V2 = DAG.getBitcast(MVT::v2i64, Vec);
8686 Lo = DAG.getBitcast(LoVT,
8687 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i64, V2,
8688 DAG.getConstant(0, SL, MVT::i32)));
8689 Hi = DAG.getBitcast(HiVT,
8690 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i64, V2,
8691 DAG.getConstant(1, SL, MVT::i32)));
8692 } else if (VecSize == 256) {
8693 SDValue V2 = DAG.getBitcast(MVT::v4i64, Vec);
8694 SDValue Parts[4];
8695 for (unsigned P = 0; P < 4; ++P) {
8696 Parts[P] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i64, V2,
8697 DAG.getConstant(P, SL, MVT::i32));
8698 }
8699
8700 Lo = DAG.getBitcast(LoVT, DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i64,
8701 Parts[0], Parts[1]));
8702 Hi = DAG.getBitcast(HiVT, DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i64,
8703 Parts[2], Parts[3]));
8704 } else {
8705 assert(VecSize == 512);
8706
8707 SDValue V2 = DAG.getBitcast(MVT::v8i64, Vec);
8708 SDValue Parts[8];
8709 for (unsigned P = 0; P < 8; ++P) {
8710 Parts[P] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i64, V2,
8711 DAG.getConstant(P, SL, MVT::i32));
8712 }
8713
8714 Lo = DAG.getBitcast(LoVT,
8715 DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v4i64,
8716 Parts[0], Parts[1], Parts[2], Parts[3]));
8717 Hi = DAG.getBitcast(HiVT,
8718 DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v4i64,
8719 Parts[4], Parts[5], Parts[6], Parts[7]));
8720 }
8721
8722 EVT IdxVT = Idx.getValueType();
8723 unsigned NElem = VecVT.getVectorNumElements();
8724 assert(isPowerOf2_32(NElem));
8725 SDValue IdxMask = DAG.getConstant(NElem / 2 - 1, SL, IdxVT);
8726 SDValue NewIdx = DAG.getNode(ISD::AND, SL, IdxVT, Idx, IdxMask);
8727 SDValue Half = DAG.getSelectCC(SL, Idx, IdxMask, Hi, Lo, ISD::SETUGT);
8728 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, Half, NewIdx);
8729 }
8730
8731 assert(VecSize <= 64);
8732
8733 MVT IntVT = MVT::getIntegerVT(VecSize);
8734
8735 // If Vec is just a SCALAR_TO_VECTOR, then use the scalar integer directly.
8736 SDValue VecBC = peekThroughBitcasts(Vec);
8737 if (VecBC.getOpcode() == ISD::SCALAR_TO_VECTOR) {
8738 SDValue Src = VecBC.getOperand(0);
8739 Src = DAG.getBitcast(Src.getValueType().changeTypeToInteger(), Src);
8740 Vec = DAG.getAnyExtOrTrunc(Src, SL, IntVT);
8741 }
8742
8743 unsigned EltSize = EltVT.getSizeInBits();
8744 assert(isPowerOf2_32(EltSize));
8745
8746 SDValue ScaleFactor = DAG.getConstant(Log2_32(EltSize), SL, MVT::i32);
8747
8748 // Convert vector index to bit-index (* EltSize)
8749 SDValue ScaledIdx = DAG.getNode(ISD::SHL, SL, MVT::i32, Idx, ScaleFactor);
8750
8751 SDValue BC = DAG.getNode(ISD::BITCAST, SL, IntVT, Vec);
8752 SDValue Elt = DAG.getNode(ISD::SRL, SL, IntVT, BC, ScaledIdx);
8753
8754 if (ResultVT == MVT::f16 || ResultVT == MVT::bf16) {
8755 SDValue Result = DAG.getNode(ISD::TRUNCATE, SL, MVT::i16, Elt);
8756 return DAG.getNode(ISD::BITCAST, SL, ResultVT, Result);
8757 }
8758
8759 return DAG.getAnyExtOrTrunc(Elt, SL, ResultVT);
8760}
8761
8762static bool elementPairIsContiguous(ArrayRef<int> Mask, int Elt) {
8763 assert(Elt % 2 == 0);
8764 return Mask[Elt + 1] == Mask[Elt] + 1 && (Mask[Elt] % 2 == 0);
8765}
8766
8767static bool elementPairIsOddToEven(ArrayRef<int> Mask, int Elt) {
8768 assert(Elt % 2 == 0);
8769 return Mask[Elt] >= 0 && Mask[Elt + 1] >= 0 && (Mask[Elt] & 1) &&
8770 !(Mask[Elt + 1] & 1);
8771}
8772
8773SDValue SITargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
8774 SelectionDAG &DAG) const {
8775 SDLoc SL(Op);
8776 EVT ResultVT = Op.getValueType();
8777 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
8778 MVT EltVT = ResultVT.getVectorElementType().getSimpleVT();
8779 const int NewSrcNumElts = 2;
8780 MVT PackVT = MVT::getVectorVT(EltVT, NewSrcNumElts);
8781 int SrcNumElts = Op.getOperand(0).getValueType().getVectorNumElements();
8782
8783 // Break up the shuffle into registers sized pieces.
8784 //
8785 // We're trying to form sub-shuffles that the register allocation pipeline
8786 // won't be able to figure out, like how to use v_pk_mov_b32 to do a register
8787 // blend or 16-bit op_sel. It should be able to figure out how to reassemble a
8788 // pair of copies into a consecutive register copy, so use the ordinary
8789 // extract_vector_elt lowering unless we can use the shuffle.
8790 //
8791 // TODO: This is a bit of hack, and we should probably always use
8792 // extract_subvector for the largest possible subvector we can (or at least
8793 // use it for PackVT aligned pieces). However we have worse support for
8794 // combines on them don't directly treat extract_subvector / insert_subvector
8795 // as legal. The DAG scheduler also ends up doing a worse job with the
8796 // extract_subvectors.
8797 const bool ShouldUseConsecutiveExtract = EltVT.getSizeInBits() == 16;
8798
8799 // vector_shuffle <0,1,6,7> lhs, rhs
8800 // -> concat_vectors (extract_subvector lhs, 0), (extract_subvector rhs, 2)
8801 //
8802 // vector_shuffle <6,7,2,3> lhs, rhs
8803 // -> concat_vectors (extract_subvector rhs, 2), (extract_subvector lhs, 2)
8804 //
8805 // vector_shuffle <6,7,0,1> lhs, rhs
8806 // -> concat_vectors (extract_subvector rhs, 2), (extract_subvector lhs, 0)
8807
8808 // Avoid scalarizing when both halves are reading from consecutive elements.
8809
8810 // If we're treating 2 element shuffles as legal, also create odd-to-even
8811 // shuffles of neighboring pairs.
8812 //
8813 // vector_shuffle <3,2,7,6> lhs, rhs
8814 // -> concat_vectors vector_shuffle <1, 0> (extract_subvector lhs, 0)
8815 // vector_shuffle <1, 0> (extract_subvector rhs, 2)
8816
8818 for (int I = 0, N = ResultVT.getVectorNumElements(); I != N; I += 2) {
8819 if (ShouldUseConsecutiveExtract &&
8821 const int Idx = SVN->getMaskElt(I);
8822 int VecIdx = Idx < SrcNumElts ? 0 : 1;
8823 int EltIdx = Idx < SrcNumElts ? Idx : Idx - SrcNumElts;
8824 SDValue SubVec = DAG.getNode(ISD::EXTRACT_SUBVECTOR, SL, PackVT,
8825 SVN->getOperand(VecIdx),
8826 DAG.getConstant(EltIdx, SL, MVT::i32));
8827 Pieces.push_back(SubVec);
8828 } else if (elementPairIsOddToEven(SVN->getMask(), I) &&
8830 int Idx0 = SVN->getMaskElt(I);
8831 int Idx1 = SVN->getMaskElt(I + 1);
8832
8833 SDValue SrcOp0 = SVN->getOperand(0);
8834 SDValue SrcOp1 = SrcOp0;
8835 if (Idx0 >= SrcNumElts) {
8836 SrcOp0 = SVN->getOperand(1);
8837 Idx0 -= SrcNumElts;
8838 }
8839
8840 if (Idx1 >= SrcNumElts) {
8841 SrcOp1 = SVN->getOperand(1);
8842 Idx1 -= SrcNumElts;
8843 }
8844
8845 int AlignedIdx0 = Idx0 & ~(NewSrcNumElts - 1);
8846 int AlignedIdx1 = Idx1 & ~(NewSrcNumElts - 1);
8847
8848 // Extract nearest even aligned piece.
8849 SDValue SubVec0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, SL, PackVT, SrcOp0,
8850 DAG.getConstant(AlignedIdx0, SL, MVT::i32));
8851 SDValue SubVec1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, SL, PackVT, SrcOp1,
8852 DAG.getConstant(AlignedIdx1, SL, MVT::i32));
8853
8854 int NewMaskIdx0 = Idx0 - AlignedIdx0;
8855 int NewMaskIdx1 = Idx1 - AlignedIdx1;
8856
8857 SDValue Result0 = SubVec0;
8858 SDValue Result1 = SubVec0;
8859
8860 if (SubVec0 != SubVec1) {
8861 NewMaskIdx1 += NewSrcNumElts;
8862 Result1 = SubVec1;
8863 } else {
8864 Result1 = DAG.getPOISON(PackVT);
8865 }
8866
8867 SDValue Shuf = DAG.getVectorShuffle(PackVT, SL, Result0, Result1,
8868 {NewMaskIdx0, NewMaskIdx1});
8869 Pieces.push_back(Shuf);
8870 } else {
8871 const int Idx0 = SVN->getMaskElt(I);
8872 const int Idx1 = SVN->getMaskElt(I + 1);
8873 int VecIdx0 = Idx0 < SrcNumElts ? 0 : 1;
8874 int VecIdx1 = Idx1 < SrcNumElts ? 0 : 1;
8875 int EltIdx0 = Idx0 < SrcNumElts ? Idx0 : Idx0 - SrcNumElts;
8876 int EltIdx1 = Idx1 < SrcNumElts ? Idx1 : Idx1 - SrcNumElts;
8877
8878 SDValue Vec0 = SVN->getOperand(VecIdx0);
8879 SDValue Elt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, Vec0,
8880 DAG.getSignedConstant(EltIdx0, SL, MVT::i32));
8881
8882 SDValue Vec1 = SVN->getOperand(VecIdx1);
8883 SDValue Elt1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, Vec1,
8884 DAG.getSignedConstant(EltIdx1, SL, MVT::i32));
8885 Pieces.push_back(DAG.getBuildVector(PackVT, SL, {Elt0, Elt1}));
8886 }
8887 }
8888
8889 return DAG.getNode(ISD::CONCAT_VECTORS, SL, ResultVT, Pieces);
8890}
8891
8892SDValue SITargetLowering::lowerSCALAR_TO_VECTOR(SDValue Op,
8893 SelectionDAG &DAG) const {
8894 SDValue SVal = Op.getOperand(0);
8895 EVT ResultVT = Op.getValueType();
8896 EVT SValVT = SVal.getValueType();
8897 SDValue UndefVal = DAG.getPOISON(SValVT);
8898 SDLoc SL(Op);
8899
8901 VElts.push_back(SVal);
8902 for (int I = 1, E = ResultVT.getVectorNumElements(); I < E; ++I)
8903 VElts.push_back(UndefVal);
8904
8905 return DAG.getBuildVector(ResultVT, SL, VElts);
8906}
8907
8908SDValue SITargetLowering::lowerBUILD_VECTOR(SDValue Op,
8909 SelectionDAG &DAG) const {
8910 SDLoc SL(Op);
8911 EVT VT = Op.getValueType();
8912
8913 if (VT == MVT::v2f16 || VT == MVT::v2i16 || VT == MVT::v2bf16) {
8914 assert(!Subtarget->hasVOP3PInsts() && "this should be legal");
8915
8916 SDValue Lo = Op.getOperand(0);
8917 SDValue Hi = Op.getOperand(1);
8918
8919 // Avoid adding defined bits with the zero_extend.
8920 if (Hi.isUndef()) {
8921 Lo = DAG.getNode(ISD::BITCAST, SL, MVT::i16, Lo);
8922 SDValue ExtLo = DAG.getNode(ISD::ANY_EXTEND, SL, MVT::i32, Lo);
8923 return DAG.getNode(ISD::BITCAST, SL, VT, ExtLo);
8924 }
8925
8926 Hi = DAG.getNode(ISD::BITCAST, SL, MVT::i16, Hi);
8927 Hi = DAG.getNode(ISD::ZERO_EXTEND, SL, MVT::i32, Hi);
8928
8929 SDValue ShlHi = DAG.getNode(ISD::SHL, SL, MVT::i32, Hi,
8930 DAG.getConstant(16, SL, MVT::i32));
8931 if (Lo.isUndef())
8932 return DAG.getNode(ISD::BITCAST, SL, VT, ShlHi);
8933
8934 Lo = DAG.getNode(ISD::BITCAST, SL, MVT::i16, Lo);
8935 Lo = DAG.getNode(ISD::ZERO_EXTEND, SL, MVT::i32, Lo);
8936
8937 SDValue Or =
8938 DAG.getNode(ISD::OR, SL, MVT::i32, Lo, ShlHi, SDNodeFlags::Disjoint);
8939 return DAG.getNode(ISD::BITCAST, SL, VT, Or);
8940 }
8941
8942 // Split into 2-element chunks.
8943 const unsigned NumParts = VT.getVectorNumElements() / 2;
8944 EVT PartVT = MVT::getVectorVT(VT.getVectorElementType().getSimpleVT(), 2);
8945 MVT PartIntVT = MVT::getIntegerVT(PartVT.getSizeInBits());
8946
8948 for (unsigned P = 0; P < NumParts; ++P) {
8949 SDValue Vec = DAG.getBuildVector(
8950 PartVT, SL, {Op.getOperand(P * 2), Op.getOperand(P * 2 + 1)});
8951 Casts.push_back(DAG.getNode(ISD::BITCAST, SL, PartIntVT, Vec));
8952 }
8953
8954 SDValue Blend =
8955 DAG.getBuildVector(MVT::getVectorVT(PartIntVT, NumParts), SL, Casts);
8956 return DAG.getNode(ISD::BITCAST, SL, VT, Blend);
8957}
8958
8960 const GlobalAddressSDNode *GA) const {
8961 // OSes that use ELF REL relocations (instead of RELA) can only store a
8962 // 32-bit addend in the instruction, so it is not safe to allow offset folding
8963 // which can create arbitrary 64-bit addends. (This is only a problem for
8964 // R_AMDGPU_*32_HI relocations since other relocation types are unaffected by
8965 // the high 32 bits of the addend.)
8966 //
8967 // This should be kept in sync with how HasRelocationAddend is initialized in
8968 // the constructor of ELFAMDGPUAsmBackend.
8969 if (!Subtarget->isAmdHsaOS())
8970 return false;
8971
8972 // We can fold offsets for anything that doesn't require a GOT relocation.
8973 return (GA->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS ||
8977}
8978
8979static SDValue
8981 const SDLoc &DL, int64_t Offset, EVT PtrVT,
8982 unsigned GAFlags = SIInstrInfo::MO_NONE) {
8983 assert(isInt<32>(Offset + 4) && "32-bit offset is expected!");
8984 // In order to support pc-relative addressing, the PC_ADD_REL_OFFSET SDNode is
8985 // lowered to the following code sequence:
8986 //
8987 // For constant address space:
8988 // s_getpc_b64 s[0:1]
8989 // s_add_u32 s0, s0, $symbol
8990 // s_addc_u32 s1, s1, 0
8991 //
8992 // s_getpc_b64 returns the address of the s_add_u32 instruction and then
8993 // a fixup or relocation is emitted to replace $symbol with a literal
8994 // constant, which is a pc-relative offset from the encoding of the $symbol
8995 // operand to the global variable.
8996 //
8997 // For global address space:
8998 // s_getpc_b64 s[0:1]
8999 // s_add_u32 s0, s0, $symbol@{gotpc}rel32@lo
9000 // s_addc_u32 s1, s1, $symbol@{gotpc}rel32@hi
9001 //
9002 // s_getpc_b64 returns the address of the s_add_u32 instruction and then
9003 // fixups or relocations are emitted to replace $symbol@*@lo and
9004 // $symbol@*@hi with lower 32 bits and higher 32 bits of a literal constant,
9005 // which is a 64-bit pc-relative offset from the encoding of the $symbol
9006 // operand to the global variable.
9007 if (((const GCNSubtarget &)DAG.getSubtarget()).has64BitLiterals()) {
9008 assert(GAFlags != SIInstrInfo::MO_NONE);
9009
9010 SDValue Ptr =
9011 DAG.getTargetGlobalAddress(GV, DL, MVT::i64, Offset, GAFlags + 2);
9012 return DAG.getNode(AMDGPUISD::PC_ADD_REL_OFFSET64, DL, PtrVT, Ptr);
9013 }
9014
9015 SDValue PtrLo = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, Offset, GAFlags);
9016 SDValue PtrHi;
9017 if (GAFlags == SIInstrInfo::MO_NONE)
9018 PtrHi = DAG.getTargetConstant(0, DL, MVT::i32);
9019 else
9020 PtrHi = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, Offset, GAFlags + 1);
9021 return DAG.getNode(AMDGPUISD::PC_ADD_REL_OFFSET, DL, PtrVT, PtrLo, PtrHi);
9022}
9023
9024SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
9025 SDValue Op,
9026 SelectionDAG &DAG) const {
9027 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
9028 SDLoc DL(GSD);
9029 EVT PtrVT = Op.getValueType();
9030
9031 const GlobalValue *GV = GSD->getGlobal();
9037 GV->hasExternalLinkage()) {
9038 Type *Ty = GV->getValueType();
9039 // HIP uses an unsized array `extern __shared__ T s[]` or similar
9040 // zero-sized type in other languages to declare the dynamic shared
9041 // memory which size is not known at the compile time. They will be
9042 // allocated by the runtime and placed directly after the static
9043 // allocated ones. They all share the same offset.
9044 if (DAG.getDataLayout().getTypeAllocSize(Ty).isZero()) {
9045 assert(PtrVT == MVT::i32 && "32-bit pointer is expected.");
9046 // Adjust alignment for that dynamic shared memory array.
9049 MFI->setUsesDynamicLDS(true);
9050 return SDValue(
9051 DAG.getMachineNode(AMDGPU::GET_GROUPSTATICSIZE, DL, PtrVT), 0);
9052 }
9053 }
9055 }
9056
9058 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, GSD->getOffset(),
9060 return DAG.getNode(AMDGPUISD::LDS, DL, MVT::i32, GA);
9061 }
9062
9063 if (Subtarget->isAmdPalOS() || Subtarget->isMesa3DOS()) {
9064 if (Subtarget->has64BitLiterals()) {
9066 GV, DL, MVT::i64, GSD->getOffset(), SIInstrInfo::MO_ABS64);
9067 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B64, DL, MVT::i64, Addr),
9068 0);
9069 }
9070
9071 SDValue AddrLo = DAG.getTargetGlobalAddress(
9072 GV, DL, MVT::i32, GSD->getOffset(), SIInstrInfo::MO_ABS32_LO);
9073 AddrLo = {DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, AddrLo), 0};
9074
9075 SDValue AddrHi = DAG.getTargetGlobalAddress(
9076 GV, DL, MVT::i32, GSD->getOffset(), SIInstrInfo::MO_ABS32_HI);
9077 AddrHi = {DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, AddrHi), 0};
9078
9079 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, AddrLo, AddrHi);
9080 }
9081
9082 if (shouldEmitFixup(GV))
9083 return buildPCRelGlobalAddress(DAG, GV, DL, GSD->getOffset(), PtrVT);
9084
9085 if (shouldEmitPCReloc(GV))
9086 return buildPCRelGlobalAddress(DAG, GV, DL, GSD->getOffset(), PtrVT,
9088
9089 SDValue GOTAddr = buildPCRelGlobalAddress(DAG, GV, DL, 0, PtrVT,
9091 PointerType *PtrTy =
9093 const DataLayout &DataLayout = DAG.getDataLayout();
9094 Align Alignment = DataLayout.getABITypeAlign(PtrTy);
9095 MachinePointerInfo PtrInfo =
9097
9098 return DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), GOTAddr, PtrInfo, Alignment,
9101}
9102
9103SDValue SITargetLowering::LowerExternalSymbol(SDValue Op,
9104 SelectionDAG &DAG) const {
9105 // TODO: Handle this. It should be mostly the same as LowerGlobalAddress.
9106 const Function &Fn = DAG.getMachineFunction().getFunction();
9107 DAG.getContext()->diagnose(DiagnosticInfoUnsupported(
9108 Fn, "unsupported external symbol", Op.getDebugLoc()));
9109 return DAG.getPOISON(Op.getValueType());
9110}
9111
9113 const SDLoc &DL, SDValue V) const {
9114 // We can't use S_MOV_B32 directly, because there is no way to specify m0 as
9115 // the destination register.
9116 //
9117 // We can't use CopyToReg, because MachineCSE won't combine COPY instructions,
9118 // so we will end up with redundant moves to m0.
9119 //
9120 // We use a pseudo to ensure we emit s_mov_b32 with m0 as the direct result.
9121
9122 // A Null SDValue creates a glue result.
9123 SDNode *M0 = DAG.getMachineNode(AMDGPU::SI_INIT_M0, DL, MVT::Other, MVT::Glue,
9124 V, Chain);
9125 return SDValue(M0, 0);
9126}
9127
9128SDValue SITargetLowering::lowerImplicitZextParam(SelectionDAG &DAG, SDValue Op,
9129 MVT VT,
9130 unsigned Offset) const {
9131 SDLoc SL(Op);
9132 SDValue Param = lowerKernargMemParameter(
9133 DAG, MVT::i32, MVT::i32, SL, DAG.getEntryNode(), Offset, Align(4), false);
9134 // The local size values will have the hi 16-bits as zero.
9135 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, Param,
9136 DAG.getValueType(VT));
9137}
9138
9140 EVT VT) {
9143 "non-hsa intrinsic with hsa target", DL.getDebugLoc()));
9144 return DAG.getPOISON(VT);
9145}
9146
9148 EVT VT) {
9151 "intrinsic not supported on subtarget", DL.getDebugLoc()));
9152 return DAG.getPOISON(VT);
9153}
9154
9156 ArrayRef<SDValue> Elts) {
9157 assert(!Elts.empty());
9158 MVT Type;
9159 unsigned NumElts = Elts.size();
9160
9161 if (NumElts <= 12) {
9162 Type = MVT::getVectorVT(MVT::f32, NumElts);
9163 } else {
9164 assert(Elts.size() <= 16);
9165 Type = MVT::v16f32;
9166 NumElts = 16;
9167 }
9168
9169 SmallVector<SDValue, 16> VecElts(NumElts);
9170 for (unsigned i = 0; i < Elts.size(); ++i) {
9171 SDValue Elt = Elts[i];
9172 if (Elt.getValueType() != MVT::f32)
9173 Elt = DAG.getBitcast(MVT::f32, Elt);
9174 VecElts[i] = Elt;
9175 }
9176 for (unsigned i = Elts.size(); i < NumElts; ++i)
9177 VecElts[i] = DAG.getPOISON(MVT::f32);
9178
9179 if (NumElts == 1)
9180 return VecElts[0];
9181 return DAG.getBuildVector(Type, DL, VecElts);
9182}
9183
9184static SDValue padEltsToUndef(SelectionDAG &DAG, const SDLoc &DL, EVT CastVT,
9185 SDValue Src, int ExtraElts) {
9186 EVT SrcVT = Src.getValueType();
9187
9189
9190 if (SrcVT.isVector())
9191 DAG.ExtractVectorElements(Src, Elts);
9192 else
9193 Elts.push_back(Src);
9194
9195 SDValue Undef = DAG.getPOISON(SrcVT.getScalarType());
9196 while (ExtraElts--)
9197 Elts.push_back(Undef);
9198
9199 return DAG.getBuildVector(CastVT, DL, Elts);
9200}
9201
9202// Re-construct the required return value for a image load intrinsic.
9203// This is more complicated due to the optional use TexFailCtrl which means the
9204// required return type is an aggregate
9206 ArrayRef<EVT> ResultTypes, bool IsTexFail,
9207 bool Unpacked, bool IsD16, int DMaskPop,
9208 int NumVDataDwords, bool IsAtomicPacked16Bit,
9209 const SDLoc &DL) {
9210 // Determine the required return type. This is the same regardless of
9211 // IsTexFail flag
9212 EVT ReqRetVT = ResultTypes[0];
9213 int ReqRetNumElts = ReqRetVT.isVector() ? ReqRetVT.getVectorNumElements() : 1;
9214 int NumDataDwords = ((IsD16 && !Unpacked) || IsAtomicPacked16Bit)
9215 ? (ReqRetNumElts + 1) / 2
9216 : ReqRetNumElts;
9217
9218 int MaskPopDwords = (!IsD16 || Unpacked) ? DMaskPop : (DMaskPop + 1) / 2;
9219
9220 MVT DataDwordVT =
9221 NumDataDwords == 1 ? MVT::i32 : MVT::getVectorVT(MVT::i32, NumDataDwords);
9222
9223 MVT MaskPopVT =
9224 MaskPopDwords == 1 ? MVT::i32 : MVT::getVectorVT(MVT::i32, MaskPopDwords);
9225
9226 SDValue Data(Result, 0);
9227 SDValue TexFail;
9228
9229 if (DMaskPop > 0 && Data.getValueType() != MaskPopVT) {
9230 SDValue ZeroIdx = DAG.getConstant(0, DL, MVT::i32);
9231 if (MaskPopVT.isVector()) {
9232 Data = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MaskPopVT,
9233 SDValue(Result, 0), ZeroIdx);
9234 } else {
9235 Data = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MaskPopVT,
9236 SDValue(Result, 0), ZeroIdx);
9237 }
9238 }
9239
9240 if (DataDwordVT.isVector() && !IsAtomicPacked16Bit)
9241 Data = padEltsToUndef(DAG, DL, DataDwordVT, Data,
9242 NumDataDwords - MaskPopDwords);
9243
9244 if (IsD16)
9245 Data = adjustLoadValueTypeImpl(Data, ReqRetVT, DL, DAG, Unpacked);
9246
9247 EVT LegalReqRetVT = ReqRetVT;
9248 if (!ReqRetVT.isVector()) {
9249 if (!Data.getValueType().isInteger())
9250 Data = DAG.getNode(ISD::BITCAST, DL,
9251 Data.getValueType().changeTypeToInteger(), Data);
9252 Data = DAG.getNode(ISD::TRUNCATE, DL, ReqRetVT.changeTypeToInteger(), Data);
9253 } else {
9254 // We need to widen the return vector to a legal type
9255 if ((ReqRetVT.getVectorNumElements() % 2) == 1 &&
9256 ReqRetVT.getVectorElementType().getSizeInBits() == 16) {
9257 LegalReqRetVT =
9259 ReqRetVT.getVectorNumElements() + 1);
9260 }
9261 }
9262 Data = DAG.getNode(ISD::BITCAST, DL, LegalReqRetVT, Data);
9263
9264 if (IsTexFail) {
9265 TexFail =
9266 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, SDValue(Result, 0),
9267 DAG.getConstant(MaskPopDwords, DL, MVT::i32));
9268
9269 return DAG.getMergeValues({Data, TexFail, SDValue(Result, 1)}, DL);
9270 }
9271
9272 if (Result->getNumValues() == 1)
9273 return Data;
9274
9275 return DAG.getMergeValues({Data, SDValue(Result, 1)}, DL);
9276}
9277
9278static bool parseTexFail(SDValue TexFailCtrl, SelectionDAG &DAG, SDValue *TFE,
9279 SDValue *LWE, bool &IsTexFail) {
9280 auto *TexFailCtrlConst = cast<ConstantSDNode>(TexFailCtrl.getNode());
9281
9282 uint64_t Value = TexFailCtrlConst->getZExtValue();
9283 if (Value) {
9284 IsTexFail = true;
9285 }
9286
9287 SDLoc DL(TexFailCtrlConst);
9288 *TFE = DAG.getTargetConstant((Value & 0x1) ? 1 : 0, DL, MVT::i32);
9289 Value &= ~(uint64_t)0x1;
9290 *LWE = DAG.getTargetConstant((Value & 0x2) ? 1 : 0, DL, MVT::i32);
9291 Value &= ~(uint64_t)0x2;
9292
9293 return Value == 0;
9294}
9295
9297 MVT PackVectorVT,
9298 SmallVectorImpl<SDValue> &PackedAddrs,
9299 unsigned DimIdx, unsigned EndIdx,
9300 unsigned NumGradients) {
9301 SDLoc DL(Op);
9302 for (unsigned I = DimIdx; I < EndIdx; I++) {
9303 SDValue Addr = Op.getOperand(I);
9304
9305 // Gradients are packed with undef for each coordinate.
9306 // In <hi 16 bit>,<lo 16 bit> notation, the registers look like this:
9307 // 1D: undef,dx/dh; undef,dx/dv
9308 // 2D: dy/dh,dx/dh; dy/dv,dx/dv
9309 // 3D: dy/dh,dx/dh; undef,dz/dh; dy/dv,dx/dv; undef,dz/dv
9310 if (((I + 1) >= EndIdx) ||
9311 ((NumGradients / 2) % 2 == 1 && (I == DimIdx + (NumGradients / 2) - 1 ||
9312 I == DimIdx + NumGradients - 1))) {
9313 if (Addr.getValueType() != MVT::i16)
9314 Addr = DAG.getBitcast(MVT::i16, Addr);
9315 Addr = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, Addr);
9316 } else {
9317 Addr = DAG.getBuildVector(PackVectorVT, DL, {Addr, Op.getOperand(I + 1)});
9318 I++;
9319 }
9320 Addr = DAG.getBitcast(MVT::f32, Addr);
9321 PackedAddrs.push_back(Addr);
9322 }
9323}
9324
9325SDValue SITargetLowering::lowerImage(SDValue Op,
9327 SelectionDAG &DAG, bool WithChain) const {
9328 SDLoc DL(Op);
9329 MachineFunction &MF = DAG.getMachineFunction();
9330 const GCNSubtarget *ST = &MF.getSubtarget<GCNSubtarget>();
9331 unsigned IntrOpcode = Intr->BaseOpcode;
9332 // For image atomic: use no-return opcode if result is unused.
9333 if (Intr->AtomicNoRetBaseOpcode != Intr->BaseOpcode &&
9334 !Op.getNode()->hasAnyUseOfValue(0))
9335 IntrOpcode = Intr->AtomicNoRetBaseOpcode;
9336 const AMDGPU::MIMGBaseOpcodeInfo *BaseOpcode =
9338 const AMDGPU::MIMGDimInfo *DimInfo = AMDGPU::getMIMGDimInfo(Intr->Dim);
9339 bool IsGFX10Plus = AMDGPU::isGFX10Plus(*Subtarget);
9340 bool IsGFX11Plus = AMDGPU::isGFX11Plus(*Subtarget);
9341 bool IsGFX12Plus = AMDGPU::isGFX12Plus(*Subtarget);
9342
9343 SmallVector<EVT, 3> ResultTypes(Op->values());
9344 SmallVector<EVT, 3> OrigResultTypes(Op->values());
9345 if (BaseOpcode->NoReturn && BaseOpcode->Atomic)
9346 ResultTypes.erase(&ResultTypes[0]);
9347
9348 bool IsD16 = false;
9349 bool IsG16 = false;
9350 bool IsA16 = false;
9351 SDValue VData;
9352 int NumVDataDwords = 0;
9353 bool AdjustRetType = false;
9354 bool IsAtomicPacked16Bit = false;
9355
9356 // Offset of intrinsic arguments
9357 const unsigned ArgOffset = WithChain ? 2 : 1;
9358
9359 unsigned DMask;
9360 unsigned DMaskLanes = 0;
9361
9362 if (BaseOpcode->Atomic) {
9363 VData = Op.getOperand(2);
9364
9365 IsAtomicPacked16Bit =
9366 (IntrOpcode == AMDGPU::IMAGE_ATOMIC_PK_ADD_F16 ||
9367 IntrOpcode == AMDGPU::IMAGE_ATOMIC_PK_ADD_F16_NORTN ||
9368 IntrOpcode == AMDGPU::IMAGE_ATOMIC_PK_ADD_BF16 ||
9369 IntrOpcode == AMDGPU::IMAGE_ATOMIC_PK_ADD_BF16_NORTN);
9370
9371 bool Is64Bit = VData.getValueSizeInBits() == 64;
9372 if (BaseOpcode->AtomicX2) {
9373 SDValue VData2 = Op.getOperand(3);
9374 VData = DAG.getBuildVector(Is64Bit ? MVT::v2i64 : MVT::v2i32, DL,
9375 {VData, VData2});
9376 if (Is64Bit)
9377 VData = DAG.getBitcast(MVT::v4i32, VData);
9378
9379 if (!BaseOpcode->NoReturn)
9380 ResultTypes[0] = Is64Bit ? MVT::v2i64 : MVT::v2i32;
9381
9382 DMask = Is64Bit ? 0xf : 0x3;
9383 NumVDataDwords = Is64Bit ? 4 : 2;
9384 } else {
9385 DMask = Is64Bit ? 0x3 : 0x1;
9386 NumVDataDwords = Is64Bit ? 2 : 1;
9387 }
9388 } else {
9389 DMask = Op->getConstantOperandVal(ArgOffset + Intr->DMaskIndex);
9390 DMaskLanes = BaseOpcode->Gather4 ? 4 : llvm::popcount(DMask);
9391
9392 if (BaseOpcode->Store) {
9393 VData = Op.getOperand(2);
9394
9395 MVT StoreVT = VData.getSimpleValueType();
9396 if (StoreVT.getScalarType() == MVT::f16) {
9397 if (!Subtarget->hasD16Images() || !BaseOpcode->HasD16)
9398 return Op; // D16 is unsupported for this instruction
9399
9400 IsD16 = true;
9401 VData = handleD16VData(VData, DAG, true);
9402 }
9403
9404 NumVDataDwords = (VData.getValueType().getSizeInBits() + 31) / 32;
9405 } else if (!BaseOpcode->NoReturn) {
9406 // Work out the num dwords based on the dmask popcount and underlying type
9407 // and whether packing is supported.
9408 MVT LoadVT = ResultTypes[0].getSimpleVT();
9409 if (LoadVT.getScalarType() == MVT::f16) {
9410 if (!Subtarget->hasD16Images() || !BaseOpcode->HasD16)
9411 return Op; // D16 is unsupported for this instruction
9412
9413 IsD16 = true;
9414 }
9415
9416 // Confirm that the return type is large enough for the dmask specified
9417 if ((LoadVT.isVector() && LoadVT.getVectorNumElements() < DMaskLanes) ||
9418 (!LoadVT.isVector() && DMaskLanes > 1))
9419 return Op;
9420
9421 // The sq block of gfx8 and gfx9 do not estimate register use correctly
9422 // for d16 image_gather4, image_gather4_l, and image_gather4_lz
9423 // instructions.
9424 if (IsD16 && !Subtarget->hasUnpackedD16VMem() &&
9425 !(BaseOpcode->Gather4 && Subtarget->hasImageGather4D16Bug()))
9426 NumVDataDwords = (DMaskLanes + 1) / 2;
9427 else
9428 NumVDataDwords = DMaskLanes;
9429
9430 AdjustRetType = true;
9431 }
9432 }
9433
9434 unsigned VAddrEnd = ArgOffset + Intr->VAddrEnd;
9436
9437 // Check for 16 bit addresses or derivatives and pack if true.
9438 MVT VAddrVT =
9439 Op.getOperand(ArgOffset + Intr->GradientStart).getSimpleValueType();
9440 MVT VAddrScalarVT = VAddrVT.getScalarType();
9441 MVT GradPackVectorVT = VAddrScalarVT == MVT::f16 ? MVT::v2f16 : MVT::v2i16;
9442 IsG16 = VAddrScalarVT == MVT::f16 || VAddrScalarVT == MVT::i16;
9443
9444 VAddrVT = Op.getOperand(ArgOffset + Intr->CoordStart).getSimpleValueType();
9445 VAddrScalarVT = VAddrVT.getScalarType();
9446 MVT AddrPackVectorVT = VAddrScalarVT == MVT::f16 ? MVT::v2f16 : MVT::v2i16;
9447 IsA16 = VAddrScalarVT == MVT::f16 || VAddrScalarVT == MVT::i16;
9448
9449 // Push back extra arguments.
9450 for (unsigned I = Intr->VAddrStart; I < Intr->GradientStart; I++) {
9451 if (IsA16 && (Op.getOperand(ArgOffset + I).getValueType() == MVT::f16)) {
9452 assert(I == Intr->BiasIndex && "Got unexpected 16-bit extra argument");
9453 // Special handling of bias when A16 is on. Bias is of type half but
9454 // occupies full 32-bit.
9455 SDValue Bias = DAG.getBuildVector(
9456 MVT::v2f16, DL,
9457 {Op.getOperand(ArgOffset + I), DAG.getPOISON(MVT::f16)});
9458 VAddrs.push_back(Bias);
9459 } else {
9460 assert((!IsA16 || Intr->NumBiasArgs == 0 || I != Intr->BiasIndex) &&
9461 "Bias needs to be converted to 16 bit in A16 mode");
9462 VAddrs.push_back(Op.getOperand(ArgOffset + I));
9463 }
9464 }
9465
9466 if (BaseOpcode->Gradients && !ST->hasG16() && (IsA16 != IsG16)) {
9467 // 16 bit gradients are supported, but are tied to the A16 control
9468 // so both gradients and addresses must be 16 bit
9469 LLVM_DEBUG(
9470 dbgs() << "Failed to lower image intrinsic: 16 bit addresses "
9471 "require 16 bit args for both gradients and addresses");
9472 return Op;
9473 }
9474
9475 if (IsA16) {
9476 if (!ST->hasA16()) {
9477 LLVM_DEBUG(dbgs() << "Failed to lower image intrinsic: Target does not "
9478 "support 16 bit addresses\n");
9479 return Op;
9480 }
9481 }
9482
9483 // We've dealt with incorrect input so we know that if IsA16, IsG16
9484 // are set then we have to compress/pack operands (either address,
9485 // gradient or both)
9486 // In the case where a16 and gradients are tied (no G16 support) then we
9487 // have already verified that both IsA16 and IsG16 are true
9488 if (BaseOpcode->Gradients && IsG16 && ST->hasG16()) {
9489 // Activate g16
9490 const AMDGPU::MIMGG16MappingInfo *G16MappingInfo =
9492 IntrOpcode = G16MappingInfo->G16; // set new opcode to variant with _g16
9493 }
9494
9495 // Add gradients (packed or unpacked)
9496 if (IsG16) {
9497 // Pack the gradients
9498 // const int PackEndIdx = IsA16 ? VAddrEnd : (ArgOffset + Intr->CoordStart);
9499 packImage16bitOpsToDwords(DAG, Op, GradPackVectorVT, VAddrs,
9500 ArgOffset + Intr->GradientStart,
9501 ArgOffset + Intr->CoordStart, Intr->NumGradients);
9502 } else {
9503 for (unsigned I = ArgOffset + Intr->GradientStart;
9504 I < ArgOffset + Intr->CoordStart; I++)
9505 VAddrs.push_back(Op.getOperand(I));
9506 }
9507
9508 // Add addresses (packed or unpacked)
9509 if (IsA16) {
9510 packImage16bitOpsToDwords(DAG, Op, AddrPackVectorVT, VAddrs,
9511 ArgOffset + Intr->CoordStart, VAddrEnd,
9512 0 /* No gradients */);
9513 } else {
9514 // Add uncompressed address
9515 for (unsigned I = ArgOffset + Intr->CoordStart; I < VAddrEnd; I++)
9516 VAddrs.push_back(Op.getOperand(I));
9517 }
9518
9519 // If the register allocator cannot place the address registers contiguously
9520 // without introducing moves, then using the non-sequential address encoding
9521 // is always preferable, since it saves VALU instructions and is usually a
9522 // wash in terms of code size or even better.
9523 //
9524 // However, we currently have no way of hinting to the register allocator that
9525 // MIMG addresses should be placed contiguously when it is possible to do so,
9526 // so force non-NSA for the common 2-address case as a heuristic.
9527 //
9528 // SIShrinkInstructions will convert NSA encodings to non-NSA after register
9529 // allocation when possible.
9530 //
9531 // Partial NSA is allowed on GFX11+ where the final register is a contiguous
9532 // set of the remaining addresses.
9533 const unsigned NSAMaxSize = ST->getNSAMaxSize(BaseOpcode->Sampler);
9534 const bool HasPartialNSAEncoding = ST->hasPartialNSAEncoding();
9535 const bool UseNSA = ST->hasNSAEncoding() &&
9536 VAddrs.size() >= ST->getNSAThreshold(MF) &&
9537 (VAddrs.size() <= NSAMaxSize || HasPartialNSAEncoding);
9538 const bool UsePartialNSA =
9539 UseNSA && HasPartialNSAEncoding && VAddrs.size() > NSAMaxSize;
9540
9541 SDValue VAddr;
9542 if (UsePartialNSA) {
9543 VAddr = getBuildDwordsVector(DAG, DL,
9544 ArrayRef(VAddrs).drop_front(NSAMaxSize - 1));
9545 } else if (!UseNSA) {
9546 VAddr = getBuildDwordsVector(DAG, DL, VAddrs);
9547 }
9548
9549 SDValue True = DAG.getTargetConstant(1, DL, MVT::i1);
9550 SDValue False = DAG.getTargetConstant(0, DL, MVT::i1);
9551 SDValue Unorm;
9552 if (!BaseOpcode->Sampler) {
9553 Unorm = True;
9554 } else {
9555 uint64_t UnormConst =
9556 Op.getConstantOperandVal(ArgOffset + Intr->UnormIndex);
9557
9558 Unorm = UnormConst ? True : False;
9559 }
9560
9561 SDValue TFE;
9562 SDValue LWE;
9563 SDValue TexFail = Op.getOperand(ArgOffset + Intr->TexFailCtrlIndex);
9564 bool IsTexFail = false;
9565 if (!parseTexFail(TexFail, DAG, &TFE, &LWE, IsTexFail))
9566 return Op;
9567
9568 if (IsTexFail) {
9569 if (!DMaskLanes) {
9570 // Expecting to get an error flag since TFC is on - and dmask is 0
9571 // Force dmask to be at least 1 otherwise the instruction will fail
9572 DMask = 0x1;
9573 DMaskLanes = 1;
9574 NumVDataDwords = 1;
9575 }
9576 NumVDataDwords += 1;
9577 AdjustRetType = true;
9578 }
9579
9580 // Has something earlier tagged that the return type needs adjusting
9581 // This happens if the instruction is a load or has set TexFailCtrl flags
9582 if (AdjustRetType) {
9583 // NumVDataDwords reflects the true number of dwords required in the return
9584 // type
9585 if (DMaskLanes == 0 && !BaseOpcode->Store) {
9586 // This is a no-op load. This can be eliminated
9587 SDValue Undef = DAG.getPOISON(Op.getValueType());
9588 if (isa<MemSDNode>(Op))
9589 return DAG.getMergeValues({Undef, Op.getOperand(0)}, DL);
9590 return Undef;
9591 }
9592
9593 EVT NewVT = NumVDataDwords > 1 ? EVT::getVectorVT(*DAG.getContext(),
9594 MVT::i32, NumVDataDwords)
9595 : MVT::i32;
9596
9597 ResultTypes[0] = NewVT;
9598 if (ResultTypes.size() == 3) {
9599 // Original result was aggregate type used for TexFailCtrl results
9600 // The actual instruction returns as a vector type which has now been
9601 // created. Remove the aggregate result.
9602 ResultTypes.erase(&ResultTypes[1]);
9603 }
9604 }
9605
9606 unsigned CPol = Op.getConstantOperandVal(ArgOffset + Intr->CachePolicyIndex);
9607 // Keep GLC only when the atomic's result is actually used.
9608 if (BaseOpcode->Atomic && !BaseOpcode->NoReturn)
9610 if (CPol & ~((IsGFX12Plus ? AMDGPU::CPol::ALL : AMDGPU::CPol::ALL_pregfx12) |
9612 return Op;
9613
9615 if (BaseOpcode->Store || BaseOpcode->Atomic)
9616 Ops.push_back(VData); // vdata
9617 if (UsePartialNSA) {
9618 append_range(Ops, ArrayRef(VAddrs).take_front(NSAMaxSize - 1));
9619 Ops.push_back(VAddr);
9620 } else if (UseNSA)
9621 append_range(Ops, VAddrs);
9622 else
9623 Ops.push_back(VAddr);
9624 SDValue Rsrc = Op.getOperand(ArgOffset + Intr->RsrcIndex);
9625 EVT RsrcVT = Rsrc.getValueType();
9626 if (RsrcVT != MVT::v4i32 && RsrcVT != MVT::v8i32)
9627 return Op;
9628 Ops.push_back(Rsrc);
9629 if (BaseOpcode->Sampler) {
9630 SDValue Samp = Op.getOperand(ArgOffset + Intr->SampIndex);
9631 if (Samp.getValueType() != MVT::v4i32)
9632 return Op;
9633 Ops.push_back(Samp);
9634 }
9635 Ops.push_back(DAG.getTargetConstant(DMask, DL, MVT::i32));
9636 if (IsGFX10Plus)
9637 Ops.push_back(DAG.getTargetConstant(DimInfo->Encoding, DL, MVT::i32));
9638 if (!IsGFX12Plus || BaseOpcode->Sampler || BaseOpcode->MSAA)
9639 Ops.push_back(Unorm);
9640 Ops.push_back(DAG.getTargetConstant(CPol, DL, MVT::i32));
9641 Ops.push_back(IsA16 && // r128, a16 for gfx9
9642 ST->hasFeature(AMDGPU::FeatureR128A16)
9643 ? True
9644 : False);
9645 if (IsGFX10Plus)
9646 Ops.push_back(IsA16 ? True : False);
9647
9648 if (!Subtarget->hasGFX90AInsts())
9649 Ops.push_back(TFE); // tfe
9650 else if (TFE->getAsZExtVal()) {
9651 DAG.getContext()->diagnose(DiagnosticInfoUnsupported(
9653 "TFE is not supported on this GPU", DL.getDebugLoc()));
9654 }
9655
9656 if (!IsGFX12Plus || BaseOpcode->Sampler || BaseOpcode->MSAA)
9657 Ops.push_back(LWE); // lwe
9658 if (!IsGFX10Plus)
9659 Ops.push_back(DimInfo->DA ? True : False);
9660 if (BaseOpcode->HasD16)
9661 Ops.push_back(IsD16 ? True : False);
9662 if (isa<MemSDNode>(Op))
9663 Ops.push_back(Op.getOperand(0)); // chain
9664
9665 int NumVAddrDwords =
9666 UseNSA ? VAddrs.size() : VAddr.getValueType().getSizeInBits() / 32;
9667 int Opcode = -1;
9668
9669 if (IsGFX12Plus) {
9670 Opcode = AMDGPU::getMIMGOpcode(IntrOpcode, AMDGPU::MIMGEncGfx12,
9671 NumVDataDwords, NumVAddrDwords);
9672 } else if (IsGFX11Plus) {
9673 Opcode = AMDGPU::getMIMGOpcode(IntrOpcode,
9674 UseNSA ? AMDGPU::MIMGEncGfx11NSA
9675 : AMDGPU::MIMGEncGfx11Default,
9676 NumVDataDwords, NumVAddrDwords);
9677 } else if (IsGFX10Plus) {
9678 Opcode = AMDGPU::getMIMGOpcode(IntrOpcode,
9679 UseNSA ? AMDGPU::MIMGEncGfx10NSA
9680 : AMDGPU::MIMGEncGfx10Default,
9681 NumVDataDwords, NumVAddrDwords);
9682 } else {
9683 if (Subtarget->hasGFX90AInsts()) {
9684 Opcode = AMDGPU::getMIMGOpcode(IntrOpcode, AMDGPU::MIMGEncGfx90a,
9685 NumVDataDwords, NumVAddrDwords);
9686 if (Opcode == -1) {
9687 DAG.getContext()->diagnose(DiagnosticInfoUnsupported(
9689 "requested image instruction is not supported on this GPU",
9690 DL.getDebugLoc()));
9691
9692 unsigned Idx = 0;
9693 SmallVector<SDValue, 3> RetValues(OrigResultTypes.size());
9694 for (EVT VT : OrigResultTypes) {
9695 if (VT == MVT::Other)
9696 RetValues[Idx++] = Op.getOperand(0); // Chain
9697 else
9698 RetValues[Idx++] = DAG.getPOISON(VT);
9699 }
9700
9701 return DAG.getMergeValues(RetValues, DL);
9702 }
9703 }
9704 if (Opcode == -1 &&
9705 Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
9706 Opcode = AMDGPU::getMIMGOpcode(IntrOpcode, AMDGPU::MIMGEncGfx8,
9707 NumVDataDwords, NumVAddrDwords);
9708 if (Opcode == -1)
9709 Opcode = AMDGPU::getMIMGOpcode(IntrOpcode, AMDGPU::MIMGEncGfx6,
9710 NumVDataDwords, NumVAddrDwords);
9711 }
9712 if (Opcode == -1)
9713 return Op;
9714
9715 MachineSDNode *NewNode = DAG.getMachineNode(Opcode, DL, ResultTypes, Ops);
9716 if (auto *MemOp = dyn_cast<MemSDNode>(Op)) {
9717 MachineMemOperand *MemRef = MemOp->getMemOperand();
9718 DAG.setNodeMemRefs(NewNode, {MemRef});
9719 }
9720
9721 if (BaseOpcode->NoReturn) {
9722 if (BaseOpcode->Atomic)
9723 return DAG.getMergeValues(
9724 {DAG.getPOISON(OrigResultTypes[0]), SDValue(NewNode, 0)}, DL);
9725
9726 return SDValue(NewNode, 0);
9727 }
9728
9729 if (BaseOpcode->AtomicX2) {
9731 DAG.ExtractVectorElements(SDValue(NewNode, 0), Elt, 0, 1);
9732 return DAG.getMergeValues({Elt[0], SDValue(NewNode, 1)}, DL);
9733 }
9734
9735 return constructRetValue(DAG, NewNode, OrigResultTypes, IsTexFail,
9736 Subtarget->hasUnpackedD16VMem(), IsD16, DMaskLanes,
9737 NumVDataDwords, IsAtomicPacked16Bit, DL);
9738}
9739
9740SDValue SITargetLowering::lowerSBuffer(EVT VT, SDLoc DL, SDValue Rsrc,
9741 SDValue Offset, SDValue CachePolicy,
9742 SelectionDAG &DAG) const {
9743 MachineFunction &MF = DAG.getMachineFunction();
9744
9745 const DataLayout &DataLayout = DAG.getDataLayout();
9746 Align Alignment =
9747 DataLayout.getABITypeAlign(VT.getTypeForEVT(*DAG.getContext()));
9748
9749 MachineMemOperand *MMO = MF.getMachineMemOperand(
9750 MachinePointerInfo(),
9753 VT.getStoreSize(), Alignment);
9754
9755 if (!Offset->isDivergent()) {
9756 SDValue Ops[] = {Rsrc, Offset, CachePolicy};
9757
9758 // Lower llvm.amdgcn.s.buffer.load.{i16, u16} intrinsics. Initially, the
9759 // s_buffer_load_u16 instruction is emitted for both signed and unsigned
9760 // loads. Later, DAG combiner tries to combine s_buffer_load_u16 with sext
9761 // and generates s_buffer_load_i16 (performSignExtendInRegCombine).
9762 if (VT == MVT::i16 && Subtarget->hasScalarSubwordLoads()) {
9763 SDValue BufferLoad =
9764 DAG.getMemIntrinsicNode(AMDGPUISD::SBUFFER_LOAD_USHORT, DL,
9765 DAG.getVTList(MVT::i32), Ops, VT, MMO);
9766 return DAG.getNode(ISD::TRUNCATE, DL, VT, BufferLoad);
9767 }
9768
9769 // Widen vec3 load to vec4.
9770 if (VT.isVector() && VT.getVectorNumElements() == 3 &&
9771 !Subtarget->hasScalarDwordx3Loads()) {
9772 EVT WidenedVT =
9774 auto WidenedOp = DAG.getMemIntrinsicNode(
9775 AMDGPUISD::SBUFFER_LOAD, DL, DAG.getVTList(WidenedVT), Ops, WidenedVT,
9776 MF.getMachineMemOperand(MMO, 0, WidenedVT.getStoreSize()));
9777 auto Subvector = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, WidenedOp,
9778 DAG.getVectorIdxConstant(0, DL));
9779 return Subvector;
9780 }
9781
9782 return DAG.getMemIntrinsicNode(AMDGPUISD::SBUFFER_LOAD, DL,
9783 DAG.getVTList(VT), Ops, VT, MMO);
9784 }
9785
9786 // We have a divergent offset. Emit a MUBUF buffer load instead. We can
9787 // assume that the buffer is unswizzled.
9788 SDValue Ops[] = {
9789 DAG.getEntryNode(), // Chain
9790 Rsrc, // rsrc
9791 DAG.getConstant(0, DL, MVT::i32), // vindex
9792 {}, // voffset
9793 {}, // soffset
9794 {}, // offset
9795 CachePolicy, // cachepolicy
9796 DAG.getTargetConstant(0, DL, MVT::i1), // idxen
9797 };
9798 if (VT == MVT::i16 && Subtarget->hasScalarSubwordLoads()) {
9799 setBufferOffsets(Offset, DAG, &Ops[3], Align(4));
9800 return handleByteShortBufferLoads(DAG, VT, DL, Ops, MMO);
9801 }
9802
9804 unsigned NumLoads = 1;
9805 MVT LoadVT = VT.getSimpleVT();
9806 unsigned NumElts = LoadVT.isVector() ? LoadVT.getVectorNumElements() : 1;
9807 assert((LoadVT.getScalarType() == MVT::i32 ||
9808 LoadVT.getScalarType() == MVT::f32));
9809
9810 if (NumElts == 8 || NumElts == 16) {
9811 NumLoads = NumElts / 4;
9812 LoadVT = MVT::getVectorVT(LoadVT.getScalarType(), 4);
9813 }
9814
9815 SDVTList VTList = DAG.getVTList({LoadVT, MVT::Other});
9816
9817 // Use the alignment to ensure that the required offsets will fit into the
9818 // immediate offsets.
9819 setBufferOffsets(Offset, DAG, &Ops[3],
9820 NumLoads > 1 ? Align(16 * NumLoads) : Align(4));
9821
9822 uint64_t InstOffset = Ops[5]->getAsZExtVal();
9823 for (unsigned i = 0; i < NumLoads; ++i) {
9824 Ops[5] = DAG.getTargetConstant(InstOffset + 16 * i, DL, MVT::i32);
9825 Loads.push_back(getMemIntrinsicNode(AMDGPUISD::BUFFER_LOAD, DL, VTList, Ops,
9826 LoadVT, MMO, DAG));
9827 }
9828
9829 if (NumElts == 8 || NumElts == 16)
9830 return DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, Loads);
9831
9832 return Loads[0];
9833}
9834
9835SDValue SITargetLowering::lowerWaveID(SelectionDAG &DAG, SDValue Op) const {
9836 // With architected SGPRs, waveIDinGroup is in TTMP8[29:25].
9837 if (!Subtarget->hasArchitectedSGPRs())
9838 return {};
9839 SDLoc SL(Op);
9840 MVT VT = MVT::i32;
9841 SDValue TTMP8 = DAG.getCopyFromReg(DAG.getEntryNode(), SL, AMDGPU::TTMP8, VT);
9842 return DAG.getNode(AMDGPUISD::BFE_U32, SL, VT, TTMP8,
9843 DAG.getConstant(25, SL, VT), DAG.getConstant(5, SL, VT));
9844}
9845
9846SDValue SITargetLowering::lowerConstHwRegRead(SelectionDAG &DAG, SDValue Op,
9847 AMDGPU::Hwreg::Id HwReg,
9848 unsigned LowBit,
9849 unsigned Width) const {
9850 SDLoc SL(Op);
9851 using namespace AMDGPU::Hwreg;
9852 return {DAG.getMachineNode(
9853 AMDGPU::S_GETREG_B32_const, SL, MVT::i32,
9854 DAG.getTargetConstant(HwregEncoding::encode(HwReg, LowBit, Width),
9855 SL, MVT::i32)),
9856 0};
9857}
9858
9859SDValue SITargetLowering::lowerWorkitemID(SelectionDAG &DAG, SDValue Op,
9860 unsigned Dim,
9861 const ArgDescriptor &Arg) const {
9862 SDLoc SL(Op);
9863 MachineFunction &MF = DAG.getMachineFunction();
9864 unsigned MaxID = Subtarget->getMaxWorkitemID(MF.getFunction(), Dim);
9865 if (MaxID == 0)
9866 return DAG.getConstant(0, SL, MVT::i32);
9867
9868 // It's undefined behavior if a function marked with the amdgpu-no-*
9869 // attributes uses the corresponding intrinsic.
9870 if (!Arg)
9871 return DAG.getPOISON(Op->getValueType(0));
9872
9873 SDValue Val = loadInputValue(DAG, &AMDGPU::VGPR_32RegClass, MVT::i32,
9874 SDLoc(DAG.getEntryNode()), Arg);
9875
9876 // Don't bother inserting AssertZext for packed IDs since we're emitting the
9877 // masking operations anyway.
9878 //
9879 // TODO: We could assert the top bit is 0 for the source copy.
9880 if (Arg.isMasked())
9881 return Val;
9882
9883 // Preserve the known bits after expansion to a copy.
9884 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), llvm::bit_width(MaxID));
9885 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, Val,
9886 DAG.getValueType(SmallVT));
9887}
9888
9889SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
9890 SelectionDAG &DAG) const {
9891 MachineFunction &MF = DAG.getMachineFunction();
9892 auto *MFI = MF.getInfo<SIMachineFunctionInfo>();
9893
9894 EVT VT = Op.getValueType();
9895 SDLoc DL(Op);
9896 unsigned IntrinsicID = Op.getConstantOperandVal(0);
9897
9898 // TODO: Should this propagate fast-math-flags?
9899
9900 switch (IntrinsicID) {
9901 case Intrinsic::amdgcn_implicit_buffer_ptr: {
9902 if (getSubtarget()->isAmdHsaOrMesa(MF.getFunction()))
9903 return emitNonHSAIntrinsicError(DAG, DL, VT);
9904 return getPreloadedValue(DAG, *MFI, VT,
9906 }
9907 case Intrinsic::amdgcn_dispatch_ptr:
9908 case Intrinsic::amdgcn_queue_ptr: {
9909 if (!Subtarget->isAmdHsaOrMesa(MF.getFunction())) {
9910 DAG.getContext()->diagnose(DiagnosticInfoUnsupported(
9911 MF.getFunction(), "unsupported hsa intrinsic without hsa target",
9912 DL.getDebugLoc()));
9913 return DAG.getPOISON(VT);
9914 }
9915
9916 auto RegID = IntrinsicID == Intrinsic::amdgcn_dispatch_ptr
9919 return getPreloadedValue(DAG, *MFI, VT, RegID);
9920 }
9921 case Intrinsic::amdgcn_implicitarg_ptr: {
9922 if (MFI->isEntryFunction())
9923 return getImplicitArgPtr(DAG, DL);
9924 return getPreloadedValue(DAG, *MFI, VT,
9926 }
9927 case Intrinsic::amdgcn_kernarg_segment_ptr: {
9928 if (!AMDGPU::isKernel(MF.getFunction())) {
9929 // This only makes sense to call in a kernel, so just lower to null.
9930 return DAG.getConstant(0, DL, VT);
9931 }
9932
9933 return getPreloadedValue(DAG, *MFI, VT,
9935 }
9936 case Intrinsic::amdgcn_dispatch_id: {
9937 return getPreloadedValue(DAG, *MFI, VT, AMDGPUFunctionArgInfo::DISPATCH_ID);
9938 }
9939 case Intrinsic::amdgcn_rcp:
9940 return DAG.getNode(AMDGPUISD::RCP, DL, VT, Op.getOperand(1));
9941 case Intrinsic::amdgcn_rsq:
9942 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
9943 case Intrinsic::amdgcn_rsq_legacy:
9944 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
9945 return emitRemovedIntrinsicError(DAG, DL, VT);
9946 return SDValue();
9947 case Intrinsic::amdgcn_rcp_legacy:
9948 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
9949 return emitRemovedIntrinsicError(DAG, DL, VT);
9950 return DAG.getNode(AMDGPUISD::RCP_LEGACY, DL, VT, Op.getOperand(1));
9951 case Intrinsic::amdgcn_rsq_clamp: {
9952 if (Subtarget->getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
9953 return DAG.getNode(AMDGPUISD::RSQ_CLAMP, DL, VT, Op.getOperand(1));
9954
9955 Type *Type = VT.getTypeForEVT(*DAG.getContext());
9956 APFloat Max = APFloat::getLargest(Type->getFltSemantics());
9957 APFloat Min = APFloat::getLargest(Type->getFltSemantics(), true);
9958
9959 SDValue Rsq = DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
9960 SDValue Tmp =
9961 DAG.getNode(ISD::FMINNUM, DL, VT, Rsq, DAG.getConstantFP(Max, DL, VT));
9962 return DAG.getNode(ISD::FMAXNUM, DL, VT, Tmp,
9963 DAG.getConstantFP(Min, DL, VT));
9964 }
9965 case Intrinsic::r600_read_ngroups_x:
9966 if (Subtarget->isAmdHsaOS())
9967 return emitNonHSAIntrinsicError(DAG, DL, VT);
9968
9969 return lowerKernargMemParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
9971 false);
9972 case Intrinsic::r600_read_ngroups_y:
9973 if (Subtarget->isAmdHsaOS())
9974 return emitNonHSAIntrinsicError(DAG, DL, VT);
9975
9976 return lowerKernargMemParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
9978 false);
9979 case Intrinsic::r600_read_ngroups_z:
9980 if (Subtarget->isAmdHsaOS())
9981 return emitNonHSAIntrinsicError(DAG, DL, VT);
9982
9983 return lowerKernargMemParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
9985 false);
9986 case Intrinsic::r600_read_local_size_x:
9987 if (Subtarget->isAmdHsaOS())
9988 return emitNonHSAIntrinsicError(DAG, DL, VT);
9989
9990 return lowerImplicitZextParam(DAG, Op, MVT::i16,
9992 case Intrinsic::r600_read_local_size_y:
9993 if (Subtarget->isAmdHsaOS())
9994 return emitNonHSAIntrinsicError(DAG, DL, VT);
9995
9996 return lowerImplicitZextParam(DAG, Op, MVT::i16,
9998 case Intrinsic::r600_read_local_size_z:
9999 if (Subtarget->isAmdHsaOS())
10000 return emitNonHSAIntrinsicError(DAG, DL, VT);
10001
10002 return lowerImplicitZextParam(DAG, Op, MVT::i16,
10004 case Intrinsic::amdgcn_workgroup_id_x:
10005 return lowerWorkGroupId(DAG, *MFI, VT,
10009 case Intrinsic::amdgcn_workgroup_id_y:
10010 return lowerWorkGroupId(DAG, *MFI, VT,
10014 case Intrinsic::amdgcn_workgroup_id_z:
10015 return lowerWorkGroupId(DAG, *MFI, VT,
10019 case Intrinsic::amdgcn_cluster_id_x:
10020 return Subtarget->hasClusters()
10021 ? getPreloadedValue(DAG, *MFI, VT,
10023 : DAG.getPOISON(VT);
10024 case Intrinsic::amdgcn_cluster_id_y:
10025 return Subtarget->hasClusters()
10026 ? getPreloadedValue(DAG, *MFI, VT,
10028 : DAG.getPOISON(VT);
10029 case Intrinsic::amdgcn_cluster_id_z:
10030 return Subtarget->hasClusters()
10031 ? getPreloadedValue(DAG, *MFI, VT,
10033 : DAG.getPOISON(VT);
10034 case Intrinsic::amdgcn_cluster_workgroup_id_x:
10035 return Subtarget->hasClusters()
10036 ? getPreloadedValue(
10037 DAG, *MFI, VT,
10039 : DAG.getPOISON(VT);
10040 case Intrinsic::amdgcn_cluster_workgroup_id_y:
10041 return Subtarget->hasClusters()
10042 ? getPreloadedValue(
10043 DAG, *MFI, VT,
10045 : DAG.getPOISON(VT);
10046 case Intrinsic::amdgcn_cluster_workgroup_id_z:
10047 return Subtarget->hasClusters()
10048 ? getPreloadedValue(
10049 DAG, *MFI, VT,
10051 : DAG.getPOISON(VT);
10052 case Intrinsic::amdgcn_cluster_workgroup_flat_id:
10053 return Subtarget->hasClusters()
10054 ? lowerConstHwRegRead(DAG, Op, AMDGPU::Hwreg::ID_IB_STS2, 21, 4)
10055 : SDValue();
10056 case Intrinsic::amdgcn_cluster_workgroup_max_id_x:
10057 return Subtarget->hasClusters()
10058 ? getPreloadedValue(
10059 DAG, *MFI, VT,
10061 : DAG.getPOISON(VT);
10062 case Intrinsic::amdgcn_cluster_workgroup_max_id_y:
10063 return Subtarget->hasClusters()
10064 ? getPreloadedValue(
10065 DAG, *MFI, VT,
10067 : DAG.getPOISON(VT);
10068 case Intrinsic::amdgcn_cluster_workgroup_max_id_z:
10069 return Subtarget->hasClusters()
10070 ? getPreloadedValue(
10071 DAG, *MFI, VT,
10073 : DAG.getPOISON(VT);
10074 case Intrinsic::amdgcn_cluster_workgroup_max_flat_id:
10075 return Subtarget->hasClusters()
10076 ? getPreloadedValue(
10077 DAG, *MFI, VT,
10079 : DAG.getPOISON(VT);
10080 case Intrinsic::amdgcn_wave_id:
10081 return lowerWaveID(DAG, Op);
10082 case Intrinsic::amdgcn_lds_kernel_id: {
10083 if (MFI->isEntryFunction())
10084 return getLDSKernelId(DAG, DL);
10085 return getPreloadedValue(DAG, *MFI, VT,
10087 }
10088 case Intrinsic::amdgcn_workitem_id_x:
10089 return lowerWorkitemID(DAG, Op, 0, MFI->getArgInfo().WorkItemIDX);
10090 case Intrinsic::amdgcn_workitem_id_y:
10091 return lowerWorkitemID(DAG, Op, 1, MFI->getArgInfo().WorkItemIDY);
10092 case Intrinsic::amdgcn_workitem_id_z:
10093 return lowerWorkitemID(DAG, Op, 2, MFI->getArgInfo().WorkItemIDZ);
10094 case Intrinsic::amdgcn_wavefrontsize:
10095 return DAG.getConstant(MF.getSubtarget<GCNSubtarget>().getWavefrontSize(),
10096 SDLoc(Op), MVT::i32);
10097 case Intrinsic::amdgcn_s_buffer_load: {
10098 unsigned CPol = Op.getConstantOperandVal(3);
10099 // s_buffer_load, because of how it's optimized, can't be volatile
10100 // so reject ones with the volatile bit set.
10101 if (CPol & ~((Subtarget->getGeneration() >= AMDGPUSubtarget::GFX12)
10104 return Op;
10105 return lowerSBuffer(VT, DL, Op.getOperand(1), Op.getOperand(2),
10106 Op.getOperand(3), DAG);
10107 }
10108 case Intrinsic::amdgcn_fdiv_fast:
10109 return lowerFDIV_FAST(Op, DAG);
10110 case Intrinsic::amdgcn_sin:
10111 return DAG.getNode(AMDGPUISD::SIN_HW, DL, VT, Op.getOperand(1));
10112
10113 case Intrinsic::amdgcn_cos:
10114 return DAG.getNode(AMDGPUISD::COS_HW, DL, VT, Op.getOperand(1));
10115
10116 case Intrinsic::amdgcn_mul_u24:
10117 return DAG.getNode(AMDGPUISD::MUL_U24, DL, VT, Op.getOperand(1),
10118 Op.getOperand(2));
10119 case Intrinsic::amdgcn_mul_i24:
10120 return DAG.getNode(AMDGPUISD::MUL_I24, DL, VT, Op.getOperand(1),
10121 Op.getOperand(2));
10122
10123 case Intrinsic::amdgcn_log_clamp: {
10124 if (Subtarget->getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
10125 return SDValue();
10126
10127 return emitRemovedIntrinsicError(DAG, DL, VT);
10128 }
10129 case Intrinsic::amdgcn_fract:
10130 return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1));
10131
10132 case Intrinsic::amdgcn_class:
10133 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, VT, Op.getOperand(1),
10134 Op.getOperand(2));
10135 case Intrinsic::amdgcn_div_fmas:
10136 return DAG.getNode(AMDGPUISD::DIV_FMAS, DL, VT, Op.getOperand(1),
10137 Op.getOperand(2), Op.getOperand(3), Op.getOperand(4));
10138
10139 case Intrinsic::amdgcn_div_fixup:
10140 return DAG.getNode(AMDGPUISD::DIV_FIXUP, DL, VT, Op.getOperand(1),
10141 Op.getOperand(2), Op.getOperand(3));
10142
10143 case Intrinsic::amdgcn_div_scale: {
10144 const ConstantSDNode *Param = cast<ConstantSDNode>(Op.getOperand(3));
10145
10146 // Translate to the operands expected by the machine instruction. The
10147 // first parameter must be the same as the first instruction.
10148 SDValue Numerator = Op.getOperand(1);
10149 SDValue Denominator = Op.getOperand(2);
10150
10151 // Note this order is opposite of the machine instruction's operations,
10152 // which is s0.f = Quotient, s1.f = Denominator, s2.f = Numerator. The
10153 // intrinsic has the numerator as the first operand to match a normal
10154 // division operation.
10155
10156 SDValue Src0 = Param->isAllOnes() ? Numerator : Denominator;
10157
10158 return DAG.getNode(AMDGPUISD::DIV_SCALE, DL, Op->getVTList(), Src0,
10159 Denominator, Numerator);
10160 }
10161 case Intrinsic::amdgcn_icmp: {
10162 // There is a Pat that handles this variant, so return it as-is.
10163 if (Op.getOperand(1).getValueType() == MVT::i1 &&
10164 Op.getConstantOperandVal(2) == 0 &&
10165 Op.getConstantOperandVal(3) == ICmpInst::Predicate::ICMP_NE)
10166 return Op;
10167 return lowerICMPIntrinsic(*this, Op.getNode(), DAG);
10168 }
10169 case Intrinsic::amdgcn_fcmp: {
10170 return lowerFCMPIntrinsic(*this, Op.getNode(), DAG);
10171 }
10172 case Intrinsic::amdgcn_ballot:
10173 return lowerBALLOTIntrinsic(*this, Op.getNode(), DAG);
10174 case Intrinsic::amdgcn_fmed3:
10175 return DAG.getNode(AMDGPUISD::FMED3, DL, VT, Op.getOperand(1),
10176 Op.getOperand(2), Op.getOperand(3));
10177 case Intrinsic::amdgcn_fdot2:
10178 return DAG.getNode(AMDGPUISD::FDOT2, DL, VT, Op.getOperand(1),
10179 Op.getOperand(2), Op.getOperand(3), Op.getOperand(4));
10180 case Intrinsic::amdgcn_fmul_legacy:
10181 return DAG.getNode(AMDGPUISD::FMUL_LEGACY, DL, VT, Op.getOperand(1),
10182 Op.getOperand(2));
10183 case Intrinsic::amdgcn_sffbh:
10184 return DAG.getNode(AMDGPUISD::FFBH_I32, DL, VT, Op.getOperand(1));
10185 case Intrinsic::amdgcn_sbfe:
10186 return DAG.getNode(AMDGPUISD::BFE_I32, DL, VT, Op.getOperand(1),
10187 Op.getOperand(2), Op.getOperand(3));
10188 case Intrinsic::amdgcn_ubfe:
10189 return DAG.getNode(AMDGPUISD::BFE_U32, DL, VT, Op.getOperand(1),
10190 Op.getOperand(2), Op.getOperand(3));
10191 case Intrinsic::amdgcn_cvt_pkrtz:
10192 case Intrinsic::amdgcn_cvt_pknorm_i16:
10193 case Intrinsic::amdgcn_cvt_pknorm_u16:
10194 case Intrinsic::amdgcn_cvt_pk_i16:
10195 case Intrinsic::amdgcn_cvt_pk_u16: {
10196 // FIXME: Stop adding cast if v2f16/v2i16 are legal.
10197 EVT VT = Op.getValueType();
10198 unsigned Opcode;
10199
10200 if (IntrinsicID == Intrinsic::amdgcn_cvt_pkrtz)
10201 Opcode = AMDGPUISD::CVT_PKRTZ_F16_F32;
10202 else if (IntrinsicID == Intrinsic::amdgcn_cvt_pknorm_i16)
10203 Opcode = AMDGPUISD::CVT_PKNORM_I16_F32;
10204 else if (IntrinsicID == Intrinsic::amdgcn_cvt_pknorm_u16)
10205 Opcode = AMDGPUISD::CVT_PKNORM_U16_F32;
10206 else if (IntrinsicID == Intrinsic::amdgcn_cvt_pk_i16)
10207 Opcode = AMDGPUISD::CVT_PK_I16_I32;
10208 else
10209 Opcode = AMDGPUISD::CVT_PK_U16_U32;
10210
10211 if (isTypeLegal(VT))
10212 return DAG.getNode(Opcode, DL, VT, Op.getOperand(1), Op.getOperand(2));
10213
10214 SDValue Node =
10215 DAG.getNode(Opcode, DL, MVT::i32, Op.getOperand(1), Op.getOperand(2));
10216 return DAG.getNode(ISD::BITCAST, DL, VT, Node);
10217 }
10218 case Intrinsic::amdgcn_fmad_ftz:
10219 return DAG.getNode(AMDGPUISD::FMAD_FTZ, DL, VT, Op.getOperand(1),
10220 Op.getOperand(2), Op.getOperand(3));
10221
10222 case Intrinsic::amdgcn_if_break:
10223 return SDValue(DAG.getMachineNode(AMDGPU::SI_IF_BREAK, DL, VT,
10224 Op->getOperand(1), Op->getOperand(2)),
10225 0);
10226
10227 case Intrinsic::amdgcn_groupstaticsize: {
10229 if (OS == Triple::AMDHSA || OS == Triple::AMDPAL)
10230 return Op;
10231
10232 const Module *M = MF.getFunction().getParent();
10233 const GlobalValue *GV =
10234 Intrinsic::getDeclarationIfExists(M, Intrinsic::amdgcn_groupstaticsize);
10235 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, 0,
10237 return {DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, GA), 0};
10238 }
10239 case Intrinsic::amdgcn_is_shared:
10240 case Intrinsic::amdgcn_is_private: {
10241 SDLoc SL(Op);
10242 SDValue SrcVec =
10243 DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
10244 SDValue SrcHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, SrcVec,
10245 DAG.getConstant(1, SL, MVT::i32));
10246
10247 unsigned AS = (IntrinsicID == Intrinsic::amdgcn_is_shared)
10249 : AMDGPUAS::PRIVATE_ADDRESS;
10250 if (AS == AMDGPUAS::PRIVATE_ADDRESS &&
10251 Subtarget->hasGloballyAddressableScratch()) {
10252 SDValue FlatScratchBaseHi(
10253 DAG.getMachineNode(
10254 AMDGPU::S_MOV_B32, DL, MVT::i32,
10255 DAG.getRegister(AMDGPU::SRC_FLAT_SCRATCH_BASE_HI, MVT::i32)),
10256 0);
10257 // Test bits 63..58 against the aperture address.
10258 return DAG.getSetCC(
10259 SL, MVT::i1,
10260 DAG.getNode(ISD::XOR, SL, MVT::i32, SrcHi, FlatScratchBaseHi),
10261 DAG.getConstant(1u << 26, SL, MVT::i32), ISD::SETULT);
10262 }
10263
10264 SDValue Aperture = getSegmentAperture(AS, SL, DAG);
10265 return DAG.getSetCC(SL, MVT::i1, SrcHi, Aperture, ISD::SETEQ);
10266 }
10267 case Intrinsic::amdgcn_perm:
10268 return DAG.getNode(AMDGPUISD::PERM, DL, MVT::i32, Op.getOperand(1),
10269 Op.getOperand(2), Op.getOperand(3));
10270 case Intrinsic::amdgcn_reloc_constant: {
10271 Module *M = MF.getFunction().getParent();
10272 const MDNode *Metadata = cast<MDNodeSDNode>(Op.getOperand(1))->getMD();
10273 auto SymbolName = cast<MDString>(Metadata->getOperand(0))->getString();
10274 auto *RelocSymbol = cast<GlobalVariable>(
10275 M->getOrInsertGlobal(SymbolName, Type::getInt32Ty(M->getContext())));
10276 SDValue GA = DAG.getTargetGlobalAddress(RelocSymbol, DL, MVT::i32, 0,
10278 return {DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, GA), 0};
10279 }
10280 case Intrinsic::amdgcn_swmmac_f16_16x16x32_f16:
10281 case Intrinsic::amdgcn_swmmac_bf16_16x16x32_bf16:
10282 case Intrinsic::amdgcn_swmmac_f32_16x16x32_bf16:
10283 case Intrinsic::amdgcn_swmmac_f32_16x16x32_f16:
10284 case Intrinsic::amdgcn_swmmac_f32_16x16x32_fp8_fp8:
10285 case Intrinsic::amdgcn_swmmac_f32_16x16x32_fp8_bf8:
10286 case Intrinsic::amdgcn_swmmac_f32_16x16x32_bf8_fp8:
10287 case Intrinsic::amdgcn_swmmac_f32_16x16x32_bf8_bf8: {
10288 if (Op.getOperand(4).getValueType() == MVT::i32)
10289 return SDValue();
10290
10291 SDLoc SL(Op);
10292 auto IndexKeyi32 = DAG.getAnyExtOrTrunc(Op.getOperand(4), SL, MVT::i32);
10293 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SL, Op.getValueType(),
10294 Op.getOperand(0), Op.getOperand(1), Op.getOperand(2),
10295 Op.getOperand(3), IndexKeyi32);
10296 }
10297 case Intrinsic::amdgcn_swmmac_f32_16x16x128_fp8_fp8:
10298 case Intrinsic::amdgcn_swmmac_f32_16x16x128_fp8_bf8:
10299 case Intrinsic::amdgcn_swmmac_f32_16x16x128_bf8_fp8:
10300 case Intrinsic::amdgcn_swmmac_f32_16x16x128_bf8_bf8:
10301 case Intrinsic::amdgcn_swmmac_f16_16x16x128_fp8_fp8:
10302 case Intrinsic::amdgcn_swmmac_f16_16x16x128_fp8_bf8:
10303 case Intrinsic::amdgcn_swmmac_f16_16x16x128_bf8_fp8:
10304 case Intrinsic::amdgcn_swmmac_f16_16x16x128_bf8_bf8: {
10305 if (Op.getOperand(4).getValueType() == MVT::i64)
10306 return SDValue();
10307
10308 SDLoc SL(Op);
10309 auto IndexKeyi64 = DAG.getAnyExtOrTrunc(Op.getOperand(4), SL, MVT::i64);
10310 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SL, Op.getValueType(),
10311 {Op.getOperand(0), Op.getOperand(1), Op.getOperand(2),
10312 Op.getOperand(3), IndexKeyi64, Op.getOperand(5),
10313 Op.getOperand(6)});
10314 }
10315 case Intrinsic::amdgcn_swmmac_f16_16x16x64_f16:
10316 case Intrinsic::amdgcn_swmmac_bf16_16x16x64_bf16:
10317 case Intrinsic::amdgcn_swmmac_f32_16x16x64_bf16:
10318 case Intrinsic::amdgcn_swmmac_bf16f32_16x16x64_bf16:
10319 case Intrinsic::amdgcn_swmmac_f32_16x16x64_f16:
10320 case Intrinsic::amdgcn_swmmac_i32_16x16x128_iu8: {
10321 EVT IndexKeyTy = IntrinsicID == Intrinsic::amdgcn_swmmac_i32_16x16x128_iu8
10322 ? MVT::i64
10323 : MVT::i32;
10324 if (Op.getOperand(6).getValueType() == IndexKeyTy)
10325 return SDValue();
10326
10327 SDLoc SL(Op);
10328 auto IndexKey = DAG.getAnyExtOrTrunc(Op.getOperand(6), SL, IndexKeyTy);
10330 Op.getOperand(0), Op.getOperand(1), Op.getOperand(2),
10331 Op.getOperand(3), Op.getOperand(4), Op.getOperand(5),
10332 IndexKey, Op.getOperand(7), Op.getOperand(8)};
10333 if (IntrinsicID == Intrinsic::amdgcn_swmmac_i32_16x16x128_iu8)
10334 Args.push_back(Op.getOperand(9));
10335 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SL, Op.getValueType(), Args);
10336 }
10337 case Intrinsic::amdgcn_swmmac_i32_16x16x32_iu4:
10338 case Intrinsic::amdgcn_swmmac_i32_16x16x32_iu8:
10339 case Intrinsic::amdgcn_swmmac_i32_16x16x64_iu4: {
10340 if (Op.getOperand(6).getValueType() == MVT::i32)
10341 return SDValue();
10342
10343 SDLoc SL(Op);
10344 auto IndexKeyi32 = DAG.getAnyExtOrTrunc(Op.getOperand(6), SL, MVT::i32);
10345 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SL, Op.getValueType(),
10346 {Op.getOperand(0), Op.getOperand(1), Op.getOperand(2),
10347 Op.getOperand(3), Op.getOperand(4), Op.getOperand(5),
10348 IndexKeyi32, Op.getOperand(7)});
10349 }
10350 case Intrinsic::amdgcn_addrspacecast_nonnull:
10351 return lowerADDRSPACECAST(Op, DAG);
10352 case Intrinsic::amdgcn_readlane:
10353 case Intrinsic::amdgcn_readfirstlane:
10354 case Intrinsic::amdgcn_writelane:
10355 case Intrinsic::amdgcn_permlane16:
10356 case Intrinsic::amdgcn_permlanex16:
10357 case Intrinsic::amdgcn_permlane64:
10358 case Intrinsic::amdgcn_set_inactive:
10359 case Intrinsic::amdgcn_set_inactive_chain_arg:
10360 case Intrinsic::amdgcn_mov_dpp8:
10361 case Intrinsic::amdgcn_update_dpp:
10362 return lowerLaneOp(*this, Op.getNode(), DAG);
10363 case Intrinsic::amdgcn_dead: {
10365 for (const EVT ValTy : Op.getNode()->values())
10366 Poisons.push_back(DAG.getPOISON(ValTy));
10367 return DAG.getMergeValues(Poisons, SDLoc(Op));
10368 }
10369 case Intrinsic::amdgcn_wave_shuffle:
10370 return lowerWaveShuffle(*this, Op.getNode(), DAG);
10371 default:
10372 if (const AMDGPU::ImageDimIntrinsicInfo *ImageDimIntr =
10374 return lowerImage(Op, ImageDimIntr, DAG, false);
10375
10376 return Op;
10377 }
10378}
10379
10380// On targets not supporting constant in soffset field, turn zero to
10381// SGPR_NULL to avoid generating an extra s_mov with zero.
10383 const GCNSubtarget *Subtarget) {
10384 if (Subtarget->hasRestrictedSOffset() && isNullConstant(SOffset))
10385 return DAG.getRegister(AMDGPU::SGPR_NULL, MVT::i32);
10386 return SOffset;
10387}
10388
10389SDValue SITargetLowering::lowerRawBufferAtomicIntrin(SDValue Op,
10390 SelectionDAG &DAG,
10391 unsigned NewOpcode) const {
10392 SDLoc DL(Op);
10393
10394 SDValue VData = Op.getOperand(2);
10395 SDValue Rsrc = bufferRsrcPtrToVector(Op.getOperand(3), DAG);
10396 auto [VOffset, Offset] = splitBufferOffsets(Op.getOperand(4), DAG);
10397 auto SOffset = selectSOffset(Op.getOperand(5), DAG, Subtarget);
10398 SDValue Ops[] = {
10399 Op.getOperand(0), // Chain
10400 VData, // vdata
10401 Rsrc, // rsrc
10402 DAG.getConstant(0, DL, MVT::i32), // vindex
10403 VOffset, // voffset
10404 SOffset, // soffset
10405 Offset, // offset
10406 Op.getOperand(6), // cachepolicy
10407 DAG.getTargetConstant(0, DL, MVT::i1), // idxen
10408 };
10409
10410 auto *M = cast<MemSDNode>(Op);
10411
10412 EVT MemVT = VData.getValueType();
10413 return DAG.getMemIntrinsicNode(NewOpcode, DL, Op->getVTList(), Ops, MemVT,
10414 M->getMemOperand());
10415}
10416
10417SDValue
10418SITargetLowering::lowerStructBufferAtomicIntrin(SDValue Op, SelectionDAG &DAG,
10419 unsigned NewOpcode) const {
10420 SDLoc DL(Op);
10421
10422 SDValue VData = Op.getOperand(2);
10423 SDValue Rsrc = bufferRsrcPtrToVector(Op.getOperand(3), DAG);
10424 auto [VOffset, Offset] = splitBufferOffsets(Op.getOperand(5), DAG);
10425 auto SOffset = selectSOffset(Op.getOperand(6), DAG, Subtarget);
10426 SDValue Ops[] = {
10427 Op.getOperand(0), // Chain
10428 VData, // vdata
10429 Rsrc, // rsrc
10430 Op.getOperand(4), // vindex
10431 VOffset, // voffset
10432 SOffset, // soffset
10433 Offset, // offset
10434 Op.getOperand(7), // cachepolicy
10435 DAG.getTargetConstant(1, DL, MVT::i1), // idxen
10436 };
10437
10438 auto *M = cast<MemSDNode>(Op);
10439
10440 EVT MemVT = VData.getValueType();
10441 return DAG.getMemIntrinsicNode(NewOpcode, DL, Op->getVTList(), Ops, MemVT,
10442 M->getMemOperand());
10443}
10444
10445SDValue SITargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op,
10446 SelectionDAG &DAG) const {
10447 unsigned IntrID = Op.getConstantOperandVal(1);
10448 SDLoc DL(Op);
10449
10450 switch (IntrID) {
10451 case Intrinsic::amdgcn_ds_ordered_add:
10452 case Intrinsic::amdgcn_ds_ordered_swap: {
10453 MemSDNode *M = cast<MemSDNode>(Op);
10454 SDValue Chain = M->getOperand(0);
10455 SDValue M0 = M->getOperand(2);
10456 SDValue Value = M->getOperand(3);
10457 unsigned IndexOperand = M->getConstantOperandVal(7);
10458 unsigned WaveRelease = M->getConstantOperandVal(8);
10459 unsigned WaveDone = M->getConstantOperandVal(9);
10460
10461 unsigned OrderedCountIndex = IndexOperand & 0x3f;
10462 IndexOperand &= ~0x3f;
10463 unsigned CountDw = 0;
10464
10465 if (Subtarget->getGeneration() >= AMDGPUSubtarget::GFX10) {
10466 CountDw = (IndexOperand >> 24) & 0xf;
10467 IndexOperand &= ~(0xf << 24);
10468
10469 if (CountDw < 1 || CountDw > 4) {
10470 const Function &Fn = DAG.getMachineFunction().getFunction();
10471 DAG.getContext()->diagnose(DiagnosticInfoUnsupported(
10472 Fn, "ds_ordered_count: dword count must be between 1 and 4",
10473 DL.getDebugLoc()));
10474 CountDw = 1;
10475 }
10476 }
10477
10478 if (IndexOperand) {
10479 const Function &Fn = DAG.getMachineFunction().getFunction();
10480 DAG.getContext()->diagnose(DiagnosticInfoUnsupported(
10481 Fn, "ds_ordered_count: bad index operand", DL.getDebugLoc()));
10482 }
10483
10484 if (WaveDone && !WaveRelease) {
10485 // TODO: Move this to IR verifier
10486 const Function &Fn = DAG.getMachineFunction().getFunction();
10487 DAG.getContext()->diagnose(DiagnosticInfoUnsupported(
10488 Fn, "ds_ordered_count: wave_done requires wave_release",
10489 DL.getDebugLoc()));
10490 }
10491
10492 unsigned Instruction = IntrID == Intrinsic::amdgcn_ds_ordered_add ? 0 : 1;
10493 unsigned ShaderType =
10495 unsigned Offset0 = OrderedCountIndex << 2;
10496 unsigned Offset1 = WaveRelease | (WaveDone << 1) | (Instruction << 4);
10497
10498 if (Subtarget->getGeneration() >= AMDGPUSubtarget::GFX10)
10499 Offset1 |= (CountDw - 1) << 6;
10500
10501 if (Subtarget->getGeneration() < AMDGPUSubtarget::GFX11)
10502 Offset1 |= ShaderType << 2;
10503
10504 unsigned Offset = Offset0 | (Offset1 << 8);
10505
10506 SDValue Ops[] = {
10507 Chain, Value, DAG.getTargetConstant(Offset, DL, MVT::i16),
10508 copyToM0(DAG, Chain, DL, M0).getValue(1), // Glue
10509 };
10510 return DAG.getMemIntrinsicNode(AMDGPUISD::DS_ORDERED_COUNT, DL,
10511 M->getVTList(), Ops, M->getMemoryVT(),
10512 M->getMemOperand());
10513 }
10514 case Intrinsic::amdgcn_raw_buffer_load:
10515 case Intrinsic::amdgcn_raw_ptr_buffer_load:
10516 case Intrinsic::amdgcn_raw_atomic_buffer_load:
10517 case Intrinsic::amdgcn_raw_ptr_atomic_buffer_load:
10518 case Intrinsic::amdgcn_raw_buffer_load_format:
10519 case Intrinsic::amdgcn_raw_ptr_buffer_load_format: {
10520 const bool IsFormat =
10521 IntrID == Intrinsic::amdgcn_raw_buffer_load_format ||
10522 IntrID == Intrinsic::amdgcn_raw_ptr_buffer_load_format;
10523
10524 SDValue Rsrc = bufferRsrcPtrToVector(Op.getOperand(2), DAG);
10525 auto [VOffset, Offset] = splitBufferOffsets(Op.getOperand(3), DAG);
10526 auto SOffset = selectSOffset(Op.getOperand(4), DAG, Subtarget);
10527 SDValue Ops[] = {
10528 Op.getOperand(0), // Chain
10529 Rsrc, // rsrc
10530 DAG.getConstant(0, DL, MVT::i32), // vindex
10531 VOffset, // voffset
10532 SOffset, // soffset
10533 Offset, // offset
10534 Op.getOperand(5), // cachepolicy, swizzled buffer
10535 DAG.getTargetConstant(0, DL, MVT::i1), // idxen
10536 };
10537
10538 auto *M = cast<MemSDNode>(Op);
10539 return lowerIntrinsicLoad(M, IsFormat, DAG, Ops);
10540 }
10541 case Intrinsic::amdgcn_struct_buffer_load:
10542 case Intrinsic::amdgcn_struct_ptr_buffer_load:
10543 case Intrinsic::amdgcn_struct_buffer_load_format:
10544 case Intrinsic::amdgcn_struct_ptr_buffer_load_format:
10545 case Intrinsic::amdgcn_struct_atomic_buffer_load:
10546 case Intrinsic::amdgcn_struct_ptr_atomic_buffer_load: {
10547 const bool IsFormat =
10548 IntrID == Intrinsic::amdgcn_struct_buffer_load_format ||
10549 IntrID == Intrinsic::amdgcn_struct_ptr_buffer_load_format;
10550
10551 SDValue Rsrc = bufferRsrcPtrToVector(Op.getOperand(2), DAG);
10552 auto [VOffset, Offset] = splitBufferOffsets(Op.getOperand(4), DAG);
10553 auto SOffset = selectSOffset(Op.getOperand(5), DAG, Subtarget);
10554 SDValue Ops[] = {
10555 Op.getOperand(0), // Chain
10556 Rsrc, // rsrc
10557 Op.getOperand(3), // vindex
10558 VOffset, // voffset
10559 SOffset, // soffset
10560 Offset, // offset
10561 Op.getOperand(6), // cachepolicy, swizzled buffer
10562 DAG.getTargetConstant(1, DL, MVT::i1), // idxen
10563 };
10564
10565 return lowerIntrinsicLoad(cast<MemSDNode>(Op), IsFormat, DAG, Ops);
10566 }
10567 case Intrinsic::amdgcn_raw_tbuffer_load:
10568 case Intrinsic::amdgcn_raw_ptr_tbuffer_load: {
10569 MemSDNode *M = cast<MemSDNode>(Op);
10570 EVT LoadVT = Op.getValueType();
10571 SDValue Rsrc = bufferRsrcPtrToVector(Op.getOperand(2), DAG);
10572 auto [VOffset, Offset] = splitBufferOffsets(Op.getOperand(3), DAG);
10573 auto SOffset = selectSOffset(Op.getOperand(4), DAG, Subtarget);
10574
10575 SDValue Ops[] = {
10576 Op.getOperand(0), // Chain
10577 Rsrc, // rsrc
10578 DAG.getConstant(0, DL, MVT::i32), // vindex
10579 VOffset, // voffset
10580 SOffset, // soffset
10581 Offset, // offset
10582 Op.getOperand(5), // format
10583 Op.getOperand(6), // cachepolicy, swizzled buffer
10584 DAG.getTargetConstant(0, DL, MVT::i1), // idxen
10585 };
10586
10587 if (LoadVT.getScalarType() == MVT::f16)
10588 return adjustLoadValueType(AMDGPUISD::TBUFFER_LOAD_FORMAT_D16, M, DAG,
10589 Ops);
10590 return getMemIntrinsicNode(AMDGPUISD::TBUFFER_LOAD_FORMAT, DL,
10591 Op->getVTList(), Ops, LoadVT, M->getMemOperand(),
10592 DAG);
10593 }
10594 case Intrinsic::amdgcn_struct_tbuffer_load:
10595 case Intrinsic::amdgcn_struct_ptr_tbuffer_load: {
10596 MemSDNode *M = cast<MemSDNode>(Op);
10597 EVT LoadVT = Op.getValueType();
10598 SDValue Rsrc = bufferRsrcPtrToVector(Op.getOperand(2), DAG);
10599 auto [VOffset, Offset] = splitBufferOffsets(Op.getOperand(4), DAG);
10600 auto SOffset = selectSOffset(Op.getOperand(5), DAG, Subtarget);
10601
10602 SDValue Ops[] = {
10603 Op.getOperand(0), // Chain
10604 Rsrc, // rsrc
10605 Op.getOperand(3), // vindex
10606 VOffset, // voffset
10607 SOffset, // soffset
10608 Offset, // offset
10609 Op.getOperand(6), // format
10610 Op.getOperand(7), // cachepolicy, swizzled buffer
10611 DAG.getTargetConstant(1, DL, MVT::i1), // idxen
10612 };
10613
10614 if (LoadVT.getScalarType() == MVT::f16)
10615 return adjustLoadValueType(AMDGPUISD::TBUFFER_LOAD_FORMAT_D16, M, DAG,
10616 Ops);
10617 return getMemIntrinsicNode(AMDGPUISD::TBUFFER_LOAD_FORMAT, DL,
10618 Op->getVTList(), Ops, LoadVT, M->getMemOperand(),
10619 DAG);
10620 }
10621 case Intrinsic::amdgcn_raw_buffer_atomic_fadd:
10622 case Intrinsic::amdgcn_raw_ptr_buffer_atomic_fadd:
10623 return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_FADD);
10624 case Intrinsic::amdgcn_struct_buffer_atomic_fadd:
10625 case Intrinsic::amdgcn_struct_ptr_buffer_atomic_fadd:
10626 return lowerStructBufferAtomicIntrin(Op, DAG,
10627 AMDGPUISD::BUFFER_ATOMIC_FADD);
10628 case Intrinsic::amdgcn_raw_buffer_atomic_fmin:
10629 case Intrinsic::amdgcn_raw_ptr_buffer_atomic_fmin:
10630 return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_FMIN);
10631 case Intrinsic::amdgcn_struct_buffer_atomic_fmin:
10632 case Intrinsic::amdgcn_struct_ptr_buffer_atomic_fmin:
10633 return lowerStructBufferAtomicIntrin(Op, DAG,
10634 AMDGPUISD::BUFFER_ATOMIC_FMIN);
10635 case Intrinsic::amdgcn_raw_buffer_atomic_fmax:
10636 case Intrinsic::amdgcn_raw_ptr_buffer_atomic_fmax:
10637 return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_FMAX);
10638 case Intrinsic::amdgcn_struct_buffer_atomic_fmax:
10639 case Intrinsic::amdgcn_struct_ptr_buffer_atomic_fmax:
10640 return lowerStructBufferAtomicIntrin(Op, DAG,
10641 AMDGPUISD::BUFFER_ATOMIC_FMAX);
10642 case Intrinsic::amdgcn_raw_buffer_atomic_swap:
10643 case Intrinsic::amdgcn_raw_ptr_buffer_atomic_swap:
10644 return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_SWAP);
10645 case Intrinsic::amdgcn_raw_buffer_atomic_add:
10646 case Intrinsic::amdgcn_raw_ptr_buffer_atomic_add:
10647 return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_ADD);
10648 case Intrinsic::amdgcn_raw_buffer_atomic_sub:
10649 case Intrinsic::amdgcn_raw_ptr_buffer_atomic_sub:
10650 return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_SUB);
10651 case Intrinsic::amdgcn_raw_buffer_atomic_smin:
10652 case Intrinsic::amdgcn_raw_ptr_buffer_atomic_smin:
10653 return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_SMIN);
10654 case Intrinsic::amdgcn_raw_buffer_atomic_umin:
10655 case Intrinsic::amdgcn_raw_ptr_buffer_atomic_umin:
10656 return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_UMIN);
10657 case Intrinsic::amdgcn_raw_buffer_atomic_smax:
10658 case Intrinsic::amdgcn_raw_ptr_buffer_atomic_smax:
10659 return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_SMAX);
10660 case Intrinsic::amdgcn_raw_buffer_atomic_umax:
10661 case Intrinsic::amdgcn_raw_ptr_buffer_atomic_umax:
10662 return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_UMAX);
10663 case Intrinsic::amdgcn_raw_buffer_atomic_and:
10664 case Intrinsic::amdgcn_raw_ptr_buffer_atomic_and:
10665 return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_AND);
10666 case Intrinsic::amdgcn_raw_buffer_atomic_or:
10667 case Intrinsic::amdgcn_raw_ptr_buffer_atomic_or:
10668 return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_OR);
10669 case Intrinsic::amdgcn_raw_buffer_atomic_xor:
10670 case Intrinsic::amdgcn_raw_ptr_buffer_atomic_xor:
10671 return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_XOR);
10672 case Intrinsic::amdgcn_raw_buffer_atomic_inc:
10673 case Intrinsic::amdgcn_raw_ptr_buffer_atomic_inc:
10674 return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_INC);
10675 case Intrinsic::amdgcn_raw_buffer_atomic_dec:
10676 case Intrinsic::amdgcn_raw_ptr_buffer_atomic_dec:
10677 return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_DEC);
10678 case Intrinsic::amdgcn_struct_buffer_atomic_swap:
10679 case Intrinsic::amdgcn_struct_ptr_buffer_atomic_swap:
10680 return lowerStructBufferAtomicIntrin(Op, DAG,
10681 AMDGPUISD::BUFFER_ATOMIC_SWAP);
10682 case Intrinsic::amdgcn_struct_buffer_atomic_add:
10683 case Intrinsic::amdgcn_struct_ptr_buffer_atomic_add:
10684 return lowerStructBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_ADD);
10685 case Intrinsic::amdgcn_struct_buffer_atomic_sub:
10686 case Intrinsic::amdgcn_struct_ptr_buffer_atomic_sub:
10687 return lowerStructBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_SUB);
10688 case Intrinsic::amdgcn_struct_buffer_atomic_smin:
10689 case Intrinsic::amdgcn_struct_ptr_buffer_atomic_smin:
10690 return lowerStructBufferAtomicIntrin(Op, DAG,
10691 AMDGPUISD::BUFFER_ATOMIC_SMIN);
10692 case Intrinsic::amdgcn_struct_buffer_atomic_umin:
10693 case Intrinsic::amdgcn_struct_ptr_buffer_atomic_umin:
10694 return lowerStructBufferAtomicIntrin(Op, DAG,
10695 AMDGPUISD::BUFFER_ATOMIC_UMIN);
10696 case Intrinsic::amdgcn_struct_buffer_atomic_smax:
10697 case Intrinsic::amdgcn_struct_ptr_buffer_atomic_smax:
10698 return lowerStructBufferAtomicIntrin(Op, DAG,
10699 AMDGPUISD::BUFFER_ATOMIC_SMAX);
10700 case Intrinsic::amdgcn_struct_buffer_atomic_umax:
10701 case Intrinsic::amdgcn_struct_ptr_buffer_atomic_umax:
10702 return lowerStructBufferAtomicIntrin(Op, DAG,
10703 AMDGPUISD::BUFFER_ATOMIC_UMAX);
10704 case Intrinsic::amdgcn_struct_buffer_atomic_and:
10705 case Intrinsic::amdgcn_struct_ptr_buffer_atomic_and:
10706 return lowerStructBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_AND);
10707 case Intrinsic::amdgcn_struct_buffer_atomic_or:
10708 case Intrinsic::amdgcn_struct_ptr_buffer_atomic_or:
10709 return lowerStructBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_OR);
10710 case Intrinsic::amdgcn_struct_buffer_atomic_xor:
10711 case Intrinsic::amdgcn_struct_ptr_buffer_atomic_xor:
10712 return lowerStructBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_XOR);
10713 case Intrinsic::amdgcn_struct_buffer_atomic_inc:
10714 case Intrinsic::amdgcn_struct_ptr_buffer_atomic_inc:
10715 return lowerStructBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_INC);
10716 case Intrinsic::amdgcn_struct_buffer_atomic_dec:
10717 case Intrinsic::amdgcn_struct_ptr_buffer_atomic_dec:
10718 return lowerStructBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_DEC);
10719 case Intrinsic::amdgcn_raw_buffer_atomic_sub_clamp_u32:
10720 case Intrinsic::amdgcn_raw_ptr_buffer_atomic_sub_clamp_u32:
10721 return lowerRawBufferAtomicIntrin(Op, DAG, AMDGPUISD::BUFFER_ATOMIC_CSUB);
10722 case Intrinsic::amdgcn_struct_buffer_atomic_sub_clamp_u32:
10723 case Intrinsic::amdgcn_struct_ptr_buffer_atomic_sub_clamp_u32:
10724 return lowerStructBufferAtomicIntrin(Op, DAG,
10725 AMDGPUISD::BUFFER_ATOMIC_CSUB);
10726 case Intrinsic::amdgcn_raw_buffer_atomic_cond_sub_u32:
10727 case Intrinsic::amdgcn_raw_ptr_buffer_atomic_cond_sub_u32:
10728 return lowerRawBufferAtomicIntrin(Op, DAG,
10729 AMDGPUISD::BUFFER_ATOMIC_COND_SUB_U32);
10730 case Intrinsic::amdgcn_struct_buffer_atomic_cond_sub_u32:
10731 case Intrinsic::amdgcn_struct_ptr_buffer_atomic_cond_sub_u32:
10732 return lowerStructBufferAtomicIntrin(Op, DAG,
10733 AMDGPUISD::BUFFER_ATOMIC_COND_SUB_U32);
10734 case Intrinsic::amdgcn_raw_buffer_atomic_cmpswap:
10735 case Intrinsic::amdgcn_raw_ptr_buffer_atomic_cmpswap: {
10736 SDValue Rsrc = bufferRsrcPtrToVector(Op.getOperand(4), DAG);
10737 auto [VOffset, Offset] = splitBufferOffsets(Op.getOperand(5), DAG);
10738 auto SOffset = selectSOffset(Op.getOperand(6), DAG, Subtarget);
10739 SDValue Ops[] = {
10740 Op.getOperand(0), // Chain
10741 Op.getOperand(2), // src
10742 Op.getOperand(3), // cmp
10743 Rsrc, // rsrc
10744 DAG.getConstant(0, DL, MVT::i32), // vindex
10745 VOffset, // voffset
10746 SOffset, // soffset
10747 Offset, // offset
10748 Op.getOperand(7), // cachepolicy
10749 DAG.getTargetConstant(0, DL, MVT::i1), // idxen
10750 };
10751 EVT VT = Op.getValueType();
10752 auto *M = cast<MemSDNode>(Op);
10753
10754 return DAG.getMemIntrinsicNode(AMDGPUISD::BUFFER_ATOMIC_CMPSWAP, DL,
10755 Op->getVTList(), Ops, VT,
10756 M->getMemOperand());
10757 }
10758 case Intrinsic::amdgcn_struct_buffer_atomic_cmpswap:
10759 case Intrinsic::amdgcn_struct_ptr_buffer_atomic_cmpswap: {
10760 SDValue Rsrc = bufferRsrcPtrToVector(Op->getOperand(4), DAG);
10761 auto [VOffset, Offset] = splitBufferOffsets(Op.getOperand(6), DAG);
10762 auto SOffset = selectSOffset(Op.getOperand(7), DAG, Subtarget);
10763 SDValue Ops[] = {
10764 Op.getOperand(0), // Chain
10765 Op.getOperand(2), // src
10766 Op.getOperand(3), // cmp
10767 Rsrc, // rsrc
10768 Op.getOperand(5), // vindex
10769 VOffset, // voffset
10770 SOffset, // soffset
10771 Offset, // offset
10772 Op.getOperand(8), // cachepolicy
10773 DAG.getTargetConstant(1, DL, MVT::i1), // idxen
10774 };
10775 EVT VT = Op.getValueType();
10776 auto *M = cast<MemSDNode>(Op);
10777
10778 return DAG.getMemIntrinsicNode(AMDGPUISD::BUFFER_ATOMIC_CMPSWAP, DL,
10779 Op->getVTList(), Ops, VT,
10780 M->getMemOperand());
10781 }
10782 case Intrinsic::amdgcn_image_bvh_dual_intersect_ray:
10783 case Intrinsic::amdgcn_image_bvh8_intersect_ray: {
10784 MemSDNode *M = cast<MemSDNode>(Op);
10785 SDValue NodePtr = M->getOperand(2);
10786 SDValue RayExtent = M->getOperand(3);
10787 SDValue InstanceMask = M->getOperand(4);
10788 SDValue RayOrigin = M->getOperand(5);
10789 SDValue RayDir = M->getOperand(6);
10790 SDValue Offsets = M->getOperand(7);
10791 SDValue TDescr = M->getOperand(8);
10792
10793 assert(NodePtr.getValueType() == MVT::i64);
10794 assert(RayDir.getValueType() == MVT::v3f32);
10795
10796 if (!Subtarget->hasBVHDualAndBVH8Insts()) {
10797 emitRemovedIntrinsicError(DAG, DL, Op.getValueType());
10798 return SDValue();
10799 }
10800
10801 bool IsBVH8 = IntrID == Intrinsic::amdgcn_image_bvh8_intersect_ray;
10802 const unsigned NumVDataDwords = 10;
10803 const unsigned NumVAddrDwords = IsBVH8 ? 11 : 12;
10804 int Opcode = AMDGPU::getMIMGOpcode(
10805 IsBVH8 ? AMDGPU::IMAGE_BVH8_INTERSECT_RAY
10806 : AMDGPU::IMAGE_BVH_DUAL_INTERSECT_RAY,
10807 AMDGPU::MIMGEncGfx12, NumVDataDwords, NumVAddrDwords);
10808 assert(Opcode != -1);
10809
10811 Ops.push_back(NodePtr);
10812 Ops.push_back(DAG.getBuildVector(
10813 MVT::v2i32, DL,
10814 {DAG.getBitcast(MVT::i32, RayExtent),
10815 DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, InstanceMask)}));
10816 Ops.push_back(RayOrigin);
10817 Ops.push_back(RayDir);
10818 Ops.push_back(Offsets);
10819 Ops.push_back(TDescr);
10820 Ops.push_back(M->getChain());
10821
10822 auto *NewNode = DAG.getMachineNode(Opcode, DL, M->getVTList(), Ops);
10823 MachineMemOperand *MemRef = M->getMemOperand();
10824 DAG.setNodeMemRefs(NewNode, {MemRef});
10825 return SDValue(NewNode, 0);
10826 }
10827 case Intrinsic::amdgcn_image_bvh_intersect_ray: {
10828 MemSDNode *M = cast<MemSDNode>(Op);
10829 SDValue NodePtr = M->getOperand(2);
10830 SDValue RayExtent = M->getOperand(3);
10831 SDValue RayOrigin = M->getOperand(4);
10832 SDValue RayDir = M->getOperand(5);
10833 SDValue RayInvDir = M->getOperand(6);
10834 SDValue TDescr = M->getOperand(7);
10835
10836 assert(NodePtr.getValueType() == MVT::i32 ||
10837 NodePtr.getValueType() == MVT::i64);
10838 assert(RayDir.getValueType() == MVT::v3f16 ||
10839 RayDir.getValueType() == MVT::v3f32);
10840
10841 if (!Subtarget->hasGFX10_AEncoding()) {
10842 emitRemovedIntrinsicError(DAG, DL, Op.getValueType());
10843 return SDValue();
10844 }
10845
10846 const bool IsGFX11 = AMDGPU::isGFX11(*Subtarget);
10847 const bool IsGFX11Plus = AMDGPU::isGFX11Plus(*Subtarget);
10848 const bool IsGFX12Plus = AMDGPU::isGFX12Plus(*Subtarget);
10849 const bool IsA16 = RayDir.getValueType().getVectorElementType() == MVT::f16;
10850 const bool Is64 = NodePtr.getValueType() == MVT::i64;
10851 const unsigned NumVDataDwords = 4;
10852 const unsigned NumVAddrDwords = IsA16 ? (Is64 ? 9 : 8) : (Is64 ? 12 : 11);
10853 const unsigned NumVAddrs = IsGFX11Plus ? (IsA16 ? 4 : 5) : NumVAddrDwords;
10854 const bool UseNSA = (Subtarget->hasNSAEncoding() &&
10855 NumVAddrs <= Subtarget->getNSAMaxSize()) ||
10856 IsGFX12Plus;
10857 const unsigned BaseOpcodes[2][2] = {
10858 {AMDGPU::IMAGE_BVH_INTERSECT_RAY, AMDGPU::IMAGE_BVH_INTERSECT_RAY_a16},
10859 {AMDGPU::IMAGE_BVH64_INTERSECT_RAY,
10860 AMDGPU::IMAGE_BVH64_INTERSECT_RAY_a16}};
10861 int Opcode;
10862 if (UseNSA) {
10863 Opcode = AMDGPU::getMIMGOpcode(BaseOpcodes[Is64][IsA16],
10864 IsGFX12Plus ? AMDGPU::MIMGEncGfx12
10865 : IsGFX11 ? AMDGPU::MIMGEncGfx11NSA
10866 : AMDGPU::MIMGEncGfx10NSA,
10867 NumVDataDwords, NumVAddrDwords);
10868 } else {
10869 assert(!IsGFX12Plus);
10870 Opcode = AMDGPU::getMIMGOpcode(BaseOpcodes[Is64][IsA16],
10871 IsGFX11 ? AMDGPU::MIMGEncGfx11Default
10872 : AMDGPU::MIMGEncGfx10Default,
10873 NumVDataDwords, NumVAddrDwords);
10874 }
10875 assert(Opcode != -1);
10876
10878
10879 auto packLanes = [&DAG, &Ops, &DL](SDValue Op, bool IsAligned) {
10881 DAG.ExtractVectorElements(Op, Lanes, 0, 3);
10882 if (Lanes[0].getValueSizeInBits() == 32) {
10883 for (unsigned I = 0; I < 3; ++I)
10884 Ops.push_back(DAG.getBitcast(MVT::i32, Lanes[I]));
10885 } else {
10886 if (IsAligned) {
10887 Ops.push_back(DAG.getBitcast(
10888 MVT::i32,
10889 DAG.getBuildVector(MVT::v2f16, DL, {Lanes[0], Lanes[1]})));
10890 Ops.push_back(Lanes[2]);
10891 } else {
10892 SDValue Elt0 = Ops.pop_back_val();
10893 Ops.push_back(DAG.getBitcast(
10894 MVT::i32, DAG.getBuildVector(MVT::v2f16, DL, {Elt0, Lanes[0]})));
10895 Ops.push_back(DAG.getBitcast(
10896 MVT::i32,
10897 DAG.getBuildVector(MVT::v2f16, DL, {Lanes[1], Lanes[2]})));
10898 }
10899 }
10900 };
10901
10902 if (UseNSA && IsGFX11Plus) {
10903 Ops.push_back(NodePtr);
10904 Ops.push_back(DAG.getBitcast(MVT::i32, RayExtent));
10905 Ops.push_back(RayOrigin);
10906 if (IsA16) {
10907 SmallVector<SDValue, 3> DirLanes, InvDirLanes, MergedLanes;
10908 DAG.ExtractVectorElements(RayDir, DirLanes, 0, 3);
10909 DAG.ExtractVectorElements(RayInvDir, InvDirLanes, 0, 3);
10910 for (unsigned I = 0; I < 3; ++I) {
10911 MergedLanes.push_back(DAG.getBitcast(
10912 MVT::i32, DAG.getBuildVector(MVT::v2f16, DL,
10913 {DirLanes[I], InvDirLanes[I]})));
10914 }
10915 Ops.push_back(DAG.getBuildVector(MVT::v3i32, DL, MergedLanes));
10916 } else {
10917 Ops.push_back(RayDir);
10918 Ops.push_back(RayInvDir);
10919 }
10920 } else {
10921 if (Is64)
10922 DAG.ExtractVectorElements(DAG.getBitcast(MVT::v2i32, NodePtr), Ops, 0,
10923 2);
10924 else
10925 Ops.push_back(NodePtr);
10926
10927 Ops.push_back(DAG.getBitcast(MVT::i32, RayExtent));
10928 packLanes(RayOrigin, true);
10929 packLanes(RayDir, true);
10930 packLanes(RayInvDir, false);
10931 }
10932
10933 if (!UseNSA) {
10934 // Build a single vector containing all the operands so far prepared.
10935 if (NumVAddrDwords > 12) {
10936 SDValue Undef = DAG.getPOISON(MVT::i32);
10937 Ops.append(16 - Ops.size(), Undef);
10938 }
10939 assert(Ops.size() >= 8 && Ops.size() <= 12);
10940 SDValue MergedOps =
10941 DAG.getBuildVector(MVT::getVectorVT(MVT::i32, Ops.size()), DL, Ops);
10942 Ops.clear();
10943 Ops.push_back(MergedOps);
10944 }
10945
10946 Ops.push_back(TDescr);
10947 Ops.push_back(DAG.getTargetConstant(IsA16, DL, MVT::i1));
10948 Ops.push_back(M->getChain());
10949
10950 auto *NewNode = DAG.getMachineNode(Opcode, DL, M->getVTList(), Ops);
10951 MachineMemOperand *MemRef = M->getMemOperand();
10952 DAG.setNodeMemRefs(NewNode, {MemRef});
10953 return SDValue(NewNode, 0);
10954 }
10955 case Intrinsic::amdgcn_global_atomic_fmin_num:
10956 case Intrinsic::amdgcn_global_atomic_fmax_num:
10957 case Intrinsic::amdgcn_flat_atomic_fmin_num:
10958 case Intrinsic::amdgcn_flat_atomic_fmax_num: {
10959 MemSDNode *M = cast<MemSDNode>(Op);
10960 SDValue Ops[] = {
10961 M->getOperand(0), // Chain
10962 M->getOperand(2), // Ptr
10963 M->getOperand(3) // Value
10964 };
10965 unsigned Opcode = 0;
10966 switch (IntrID) {
10967 case Intrinsic::amdgcn_global_atomic_fmin_num:
10968 case Intrinsic::amdgcn_flat_atomic_fmin_num: {
10969 Opcode = ISD::ATOMIC_LOAD_FMIN;
10970 break;
10971 }
10972 case Intrinsic::amdgcn_global_atomic_fmax_num:
10973 case Intrinsic::amdgcn_flat_atomic_fmax_num: {
10974 Opcode = ISD::ATOMIC_LOAD_FMAX;
10975 break;
10976 }
10977 default:
10978 llvm_unreachable("unhandled atomic opcode");
10979 }
10980 return DAG.getAtomic(Opcode, SDLoc(Op), M->getMemoryVT(), M->getVTList(),
10981 Ops, M->getMemOperand());
10982 }
10983 case Intrinsic::amdgcn_s_get_barrier_state:
10984 case Intrinsic::amdgcn_s_get_named_barrier_state: {
10985 SDValue Chain = Op->getOperand(0);
10987 unsigned Opc;
10988
10989 if (isa<ConstantSDNode>(Op->getOperand(2))) {
10990 uint64_t BarID = cast<ConstantSDNode>(Op->getOperand(2))->getZExtValue();
10991 if (IntrID == Intrinsic::amdgcn_s_get_named_barrier_state)
10992 BarID = (BarID >> 4) & 0x3F;
10993 Opc = AMDGPU::S_GET_BARRIER_STATE_IMM;
10994 SDValue K = DAG.getTargetConstant(BarID, DL, MVT::i32);
10995 Ops.push_back(K);
10996 Ops.push_back(Chain);
10997 } else {
10998 Opc = AMDGPU::S_GET_BARRIER_STATE_M0;
10999 if (IntrID == Intrinsic::amdgcn_s_get_named_barrier_state) {
11000 SDValue M0Val;
11001 M0Val = DAG.getNode(ISD::SRL, DL, MVT::i32, Op->getOperand(2),
11002 DAG.getShiftAmountConstant(4, MVT::i32, DL));
11003 M0Val = SDValue(
11004 DAG.getMachineNode(AMDGPU::S_AND_B32, DL, MVT::i32, M0Val,
11005 DAG.getTargetConstant(0x3F, DL, MVT::i32)),
11006 0);
11007 Ops.push_back(copyToM0(DAG, Chain, DL, M0Val).getValue(0));
11008 } else
11009 Ops.push_back(copyToM0(DAG, Chain, DL, Op->getOperand(2)).getValue(0));
11010 }
11011
11012 auto *NewMI = DAG.getMachineNode(Opc, DL, Op->getVTList(), Ops);
11013 return SDValue(NewMI, 0);
11014 }
11015 case Intrinsic::amdgcn_cooperative_atomic_load_32x4B:
11016 case Intrinsic::amdgcn_cooperative_atomic_load_16x8B:
11017 case Intrinsic::amdgcn_cooperative_atomic_load_8x16B: {
11018 MemIntrinsicSDNode *MII = cast<MemIntrinsicSDNode>(Op);
11019 SDValue Chain = Op->getOperand(0);
11020 SDValue Ptr = Op->getOperand(2);
11021 EVT VT = Op->getValueType(0);
11022 return DAG.getAtomicLoad(ISD::NON_EXTLOAD, DL, MII->getMemoryVT(), VT,
11023 Chain, Ptr, MII->getMemOperand());
11024 }
11025 default:
11026
11027 if (const AMDGPU::ImageDimIntrinsicInfo *ImageDimIntr =
11029 return lowerImage(Op, ImageDimIntr, DAG, true);
11030
11031 return SDValue();
11032 }
11033}
11034
11035// Call DAG.getMemIntrinsicNode for a load, but first widen a dwordx3 type to
11036// dwordx4 if on SI and handle TFE loads.
11037SDValue SITargetLowering::getMemIntrinsicNode(unsigned Opcode, const SDLoc &DL,
11038 SDVTList VTList,
11039 ArrayRef<SDValue> Ops, EVT MemVT,
11040 MachineMemOperand *MMO,
11041 SelectionDAG &DAG) const {
11042 LLVMContext &C = *DAG.getContext();
11043 MachineFunction &MF = DAG.getMachineFunction();
11044 EVT VT = VTList.VTs[0];
11045
11046 assert(VTList.NumVTs == 2 || VTList.NumVTs == 3);
11047 bool IsTFE = VTList.NumVTs == 3;
11048 if (IsTFE) {
11049 unsigned NumValueDWords = divideCeil(VT.getSizeInBits(), 32);
11050 unsigned NumOpDWords = NumValueDWords + 1;
11051 EVT OpDWordsVT = EVT::getVectorVT(C, MVT::i32, NumOpDWords);
11052 SDVTList OpDWordsVTList = DAG.getVTList(OpDWordsVT, VTList.VTs[2]);
11053 MachineMemOperand *OpDWordsMMO =
11054 MF.getMachineMemOperand(MMO, 0, NumOpDWords * 4);
11055 SDValue Op = getMemIntrinsicNode(Opcode, DL, OpDWordsVTList, Ops,
11056 OpDWordsVT, OpDWordsMMO, DAG);
11057 SDValue Status = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Op,
11058 DAG.getVectorIdxConstant(NumValueDWords, DL));
11059 SDValue ZeroIdx = DAG.getVectorIdxConstant(0, DL);
11060 SDValue ValueDWords =
11061 NumValueDWords == 1
11062 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Op, ZeroIdx)
11064 EVT::getVectorVT(C, MVT::i32, NumValueDWords), Op,
11065 ZeroIdx);
11066 SDValue Value = DAG.getNode(ISD::BITCAST, DL, VT, ValueDWords);
11067 return DAG.getMergeValues({Value, Status, SDValue(Op.getNode(), 1)}, DL);
11068 }
11069
11070 if (!Subtarget->hasDwordx3LoadStores() &&
11071 (VT == MVT::v3i32 || VT == MVT::v3f32)) {
11072 EVT WidenedVT = EVT::getVectorVT(C, VT.getVectorElementType(), 4);
11073 EVT WidenedMemVT = EVT::getVectorVT(C, MemVT.getVectorElementType(), 4);
11074 MachineMemOperand *WidenedMMO = MF.getMachineMemOperand(MMO, 0, 16);
11075 SDVTList WidenedVTList = DAG.getVTList(WidenedVT, VTList.VTs[1]);
11076 SDValue Op = DAG.getMemIntrinsicNode(Opcode, DL, WidenedVTList, Ops,
11077 WidenedMemVT, WidenedMMO);
11079 DAG.getVectorIdxConstant(0, DL));
11080 return DAG.getMergeValues({Value, SDValue(Op.getNode(), 1)}, DL);
11081 }
11082
11083 return DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops, MemVT, MMO);
11084}
11085
11086SDValue SITargetLowering::handleD16VData(SDValue VData, SelectionDAG &DAG,
11087 bool ImageStore) const {
11088 EVT StoreVT = VData.getValueType();
11089
11090 // No change for f16 and legal vector D16 types.
11091 if (!StoreVT.isVector())
11092 return VData;
11093
11094 SDLoc DL(VData);
11095 unsigned NumElements = StoreVT.getVectorNumElements();
11096
11097 if (Subtarget->hasUnpackedD16VMem()) {
11098 // We need to unpack the packed data to store.
11099 EVT IntStoreVT = StoreVT.changeTypeToInteger();
11100 SDValue IntVData = DAG.getNode(ISD::BITCAST, DL, IntStoreVT, VData);
11101
11102 EVT EquivStoreVT =
11103 EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElements);
11104 SDValue ZExt = DAG.getNode(ISD::ZERO_EXTEND, DL, EquivStoreVT, IntVData);
11105 return DAG.UnrollVectorOp(ZExt.getNode());
11106 }
11107
11108 // The sq block of gfx8.1 does not estimate register use correctly for d16
11109 // image store instructions. The data operand is computed as if it were not a
11110 // d16 image instruction.
11111 if (ImageStore && Subtarget->hasImageStoreD16Bug()) {
11112 // Bitcast to i16
11113 EVT IntStoreVT = StoreVT.changeTypeToInteger();
11114 SDValue IntVData = DAG.getNode(ISD::BITCAST, DL, IntStoreVT, VData);
11115
11116 // Decompose into scalars
11118 DAG.ExtractVectorElements(IntVData, Elts);
11119
11120 // Group pairs of i16 into v2i16 and bitcast to i32
11121 SmallVector<SDValue, 4> PackedElts;
11122 for (unsigned I = 0; I < Elts.size() / 2; I += 1) {
11123 SDValue Pair =
11124 DAG.getBuildVector(MVT::v2i16, DL, {Elts[I * 2], Elts[I * 2 + 1]});
11125 SDValue IntPair = DAG.getNode(ISD::BITCAST, DL, MVT::i32, Pair);
11126 PackedElts.push_back(IntPair);
11127 }
11128 if ((NumElements % 2) == 1) {
11129 // Handle v3i16
11130 unsigned I = Elts.size() / 2;
11131 SDValue Pair = DAG.getBuildVector(MVT::v2i16, DL,
11132 {Elts[I * 2], DAG.getPOISON(MVT::i16)});
11133 SDValue IntPair = DAG.getNode(ISD::BITCAST, DL, MVT::i32, Pair);
11134 PackedElts.push_back(IntPair);
11135 }
11136
11137 // Pad using UNDEF
11138 PackedElts.resize(Elts.size(), DAG.getPOISON(MVT::i32));
11139
11140 // Build final vector
11141 EVT VecVT =
11142 EVT::getVectorVT(*DAG.getContext(), MVT::i32, PackedElts.size());
11143 return DAG.getBuildVector(VecVT, DL, PackedElts);
11144 }
11145
11146 if (NumElements == 3) {
11147 EVT IntStoreVT =
11149 SDValue IntVData = DAG.getNode(ISD::BITCAST, DL, IntStoreVT, VData);
11150
11151 EVT WidenedStoreVT = EVT::getVectorVT(
11152 *DAG.getContext(), StoreVT.getVectorElementType(), NumElements + 1);
11153 EVT WidenedIntVT = EVT::getIntegerVT(*DAG.getContext(),
11154 WidenedStoreVT.getStoreSizeInBits());
11155 SDValue ZExt = DAG.getNode(ISD::ZERO_EXTEND, DL, WidenedIntVT, IntVData);
11156 return DAG.getNode(ISD::BITCAST, DL, WidenedStoreVT, ZExt);
11157 }
11158
11159 assert(isTypeLegal(StoreVT));
11160 return VData;
11161}
11162
11163SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op,
11164 SelectionDAG &DAG) const {
11165 SDLoc DL(Op);
11166 SDValue Chain = Op.getOperand(0);
11167 unsigned IntrinsicID = Op.getConstantOperandVal(1);
11168 MachineFunction &MF = DAG.getMachineFunction();
11169
11170 switch (IntrinsicID) {
11171 case Intrinsic::amdgcn_exp_compr: {
11172 if (!Subtarget->hasCompressedExport()) {
11173 DAG.getContext()->diagnose(DiagnosticInfoUnsupported(
11175 "intrinsic not supported on subtarget", DL.getDebugLoc()));
11176 }
11177 SDValue Src0 = Op.getOperand(4);
11178 SDValue Src1 = Op.getOperand(5);
11179 // Hack around illegal type on SI by directly selecting it.
11180 if (isTypeLegal(Src0.getValueType()))
11181 return SDValue();
11182
11183 const ConstantSDNode *Done = cast<ConstantSDNode>(Op.getOperand(6));
11184 SDValue Undef = DAG.getPOISON(MVT::f32);
11185 const SDValue Ops[] = {
11186 Op.getOperand(2), // tgt
11187 DAG.getNode(ISD::BITCAST, DL, MVT::f32, Src0), // src0
11188 DAG.getNode(ISD::BITCAST, DL, MVT::f32, Src1), // src1
11189 Undef, // src2
11190 Undef, // src3
11191 Op.getOperand(7), // vm
11192 DAG.getTargetConstant(1, DL, MVT::i1), // compr
11193 Op.getOperand(3), // en
11194 Op.getOperand(0) // Chain
11195 };
11196
11197 unsigned Opc = Done->isZero() ? AMDGPU::EXP : AMDGPU::EXP_DONE;
11198 return SDValue(DAG.getMachineNode(Opc, DL, Op->getVTList(), Ops), 0);
11199 }
11200
11201 case Intrinsic::amdgcn_struct_tbuffer_store:
11202 case Intrinsic::amdgcn_struct_ptr_tbuffer_store: {
11203 SDValue VData = Op.getOperand(2);
11204 bool IsD16 = (VData.getValueType().getScalarType() == MVT::f16);
11205 if (IsD16)
11206 VData = handleD16VData(VData, DAG);
11207 SDValue Rsrc = bufferRsrcPtrToVector(Op.getOperand(3), DAG);
11208 auto [VOffset, Offset] = splitBufferOffsets(Op.getOperand(5), DAG);
11209 auto SOffset = selectSOffset(Op.getOperand(6), DAG, Subtarget);
11210 SDValue Ops[] = {
11211 Chain,
11212 VData, // vdata
11213 Rsrc, // rsrc
11214 Op.getOperand(4), // vindex
11215 VOffset, // voffset
11216 SOffset, // soffset
11217 Offset, // offset
11218 Op.getOperand(7), // format
11219 Op.getOperand(8), // cachepolicy, swizzled buffer
11220 DAG.getTargetConstant(1, DL, MVT::i1), // idxen
11221 };
11222 unsigned Opc = IsD16 ? AMDGPUISD::TBUFFER_STORE_FORMAT_D16
11223 : AMDGPUISD::TBUFFER_STORE_FORMAT;
11224 MemSDNode *M = cast<MemSDNode>(Op);
11225 return DAG.getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops,
11226 M->getMemoryVT(), M->getMemOperand());
11227 }
11228
11229 case Intrinsic::amdgcn_raw_tbuffer_store:
11230 case Intrinsic::amdgcn_raw_ptr_tbuffer_store: {
11231 SDValue VData = Op.getOperand(2);
11232 bool IsD16 = (VData.getValueType().getScalarType() == MVT::f16);
11233 if (IsD16)
11234 VData = handleD16VData(VData, DAG);
11235 SDValue Rsrc = bufferRsrcPtrToVector(Op.getOperand(3), DAG);
11236 auto [VOffset, Offset] = splitBufferOffsets(Op.getOperand(4), DAG);
11237 auto SOffset = selectSOffset(Op.getOperand(5), DAG, Subtarget);
11238 SDValue Ops[] = {
11239 Chain,
11240 VData, // vdata
11241 Rsrc, // rsrc
11242 DAG.getConstant(0, DL, MVT::i32), // vindex
11243 VOffset, // voffset
11244 SOffset, // soffset
11245 Offset, // offset
11246 Op.getOperand(6), // format
11247 Op.getOperand(7), // cachepolicy, swizzled buffer
11248 DAG.getTargetConstant(0, DL, MVT::i1), // idxen
11249 };
11250 unsigned Opc = IsD16 ? AMDGPUISD::TBUFFER_STORE_FORMAT_D16
11251 : AMDGPUISD::TBUFFER_STORE_FORMAT;
11252 MemSDNode *M = cast<MemSDNode>(Op);
11253 return DAG.getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops,
11254 M->getMemoryVT(), M->getMemOperand());
11255 }
11256
11257 case Intrinsic::amdgcn_raw_buffer_store:
11258 case Intrinsic::amdgcn_raw_ptr_buffer_store:
11259 case Intrinsic::amdgcn_raw_buffer_store_format:
11260 case Intrinsic::amdgcn_raw_ptr_buffer_store_format: {
11261 const bool IsFormat =
11262 IntrinsicID == Intrinsic::amdgcn_raw_buffer_store_format ||
11263 IntrinsicID == Intrinsic::amdgcn_raw_ptr_buffer_store_format;
11264
11265 SDValue VData = Op.getOperand(2);
11266 EVT VDataVT = VData.getValueType();
11267 EVT EltType = VDataVT.getScalarType();
11268 bool IsD16 = IsFormat && (EltType.getSizeInBits() == 16);
11269 if (IsD16) {
11270 VData = handleD16VData(VData, DAG);
11271 VDataVT = VData.getValueType();
11272 }
11273
11274 if (!isTypeLegal(VDataVT)) {
11275 VData =
11276 DAG.getNode(ISD::BITCAST, DL,
11277 getEquivalentMemType(*DAG.getContext(), VDataVT), VData);
11278 }
11279
11280 SDValue Rsrc = bufferRsrcPtrToVector(Op.getOperand(3), DAG);
11281 auto [VOffset, Offset] = splitBufferOffsets(Op.getOperand(4), DAG);
11282 auto SOffset = selectSOffset(Op.getOperand(5), DAG, Subtarget);
11283 SDValue Ops[] = {
11284 Chain,
11285 VData,
11286 Rsrc,
11287 DAG.getConstant(0, DL, MVT::i32), // vindex
11288 VOffset, // voffset
11289 SOffset, // soffset
11290 Offset, // offset
11291 Op.getOperand(6), // cachepolicy, swizzled buffer
11292 DAG.getTargetConstant(0, DL, MVT::i1), // idxen
11293 };
11294 unsigned Opc =
11295 IsFormat ? AMDGPUISD::BUFFER_STORE_FORMAT : AMDGPUISD::BUFFER_STORE;
11296 Opc = IsD16 ? AMDGPUISD::BUFFER_STORE_FORMAT_D16 : Opc;
11297 MemSDNode *M = cast<MemSDNode>(Op);
11298
11299 // Handle BUFFER_STORE_BYTE/SHORT overloaded intrinsics
11300 if (!IsD16 && !VDataVT.isVector() && EltType.getSizeInBits() < 32)
11301 return handleByteShortBufferStores(DAG, VDataVT, DL, Ops, M);
11302
11303 return DAG.getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops,
11304 M->getMemoryVT(), M->getMemOperand());
11305 }
11306
11307 case Intrinsic::amdgcn_struct_buffer_store:
11308 case Intrinsic::amdgcn_struct_ptr_buffer_store:
11309 case Intrinsic::amdgcn_struct_buffer_store_format:
11310 case Intrinsic::amdgcn_struct_ptr_buffer_store_format: {
11311 const bool IsFormat =
11312 IntrinsicID == Intrinsic::amdgcn_struct_buffer_store_format ||
11313 IntrinsicID == Intrinsic::amdgcn_struct_ptr_buffer_store_format;
11314
11315 SDValue VData = Op.getOperand(2);
11316 EVT VDataVT = VData.getValueType();
11317 EVT EltType = VDataVT.getScalarType();
11318 bool IsD16 = IsFormat && (EltType.getSizeInBits() == 16);
11319
11320 if (IsD16) {
11321 VData = handleD16VData(VData, DAG);
11322 VDataVT = VData.getValueType();
11323 }
11324
11325 if (!isTypeLegal(VDataVT)) {
11326 VData =
11327 DAG.getNode(ISD::BITCAST, DL,
11328 getEquivalentMemType(*DAG.getContext(), VDataVT), VData);
11329 }
11330
11331 auto Rsrc = bufferRsrcPtrToVector(Op.getOperand(3), DAG);
11332 auto [VOffset, Offset] = splitBufferOffsets(Op.getOperand(5), DAG);
11333 auto SOffset = selectSOffset(Op.getOperand(6), DAG, Subtarget);
11334 SDValue Ops[] = {
11335 Chain,
11336 VData,
11337 Rsrc,
11338 Op.getOperand(4), // vindex
11339 VOffset, // voffset
11340 SOffset, // soffset
11341 Offset, // offset
11342 Op.getOperand(7), // cachepolicy, swizzled buffer
11343 DAG.getTargetConstant(1, DL, MVT::i1), // idxen
11344 };
11345 unsigned Opc =
11346 !IsFormat ? AMDGPUISD::BUFFER_STORE : AMDGPUISD::BUFFER_STORE_FORMAT;
11347 Opc = IsD16 ? AMDGPUISD::BUFFER_STORE_FORMAT_D16 : Opc;
11348 MemSDNode *M = cast<MemSDNode>(Op);
11349
11350 // Handle BUFFER_STORE_BYTE/SHORT overloaded intrinsics
11351 EVT VDataType = VData.getValueType().getScalarType();
11352 if (!IsD16 && !VDataVT.isVector() && EltType.getSizeInBits() < 32)
11353 return handleByteShortBufferStores(DAG, VDataType, DL, Ops, M);
11354
11355 return DAG.getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops,
11356 M->getMemoryVT(), M->getMemOperand());
11357 }
11358 case Intrinsic::amdgcn_raw_buffer_load_lds:
11359 case Intrinsic::amdgcn_raw_ptr_buffer_load_lds:
11360 case Intrinsic::amdgcn_struct_buffer_load_lds:
11361 case Intrinsic::amdgcn_struct_ptr_buffer_load_lds: {
11362 if (!Subtarget->hasVMemToLDSLoad())
11363 return SDValue();
11364 unsigned Opc;
11365 bool HasVIndex =
11366 IntrinsicID == Intrinsic::amdgcn_struct_buffer_load_lds ||
11367 IntrinsicID == Intrinsic::amdgcn_struct_ptr_buffer_load_lds;
11368 unsigned OpOffset = HasVIndex ? 1 : 0;
11369 SDValue VOffset = Op.getOperand(5 + OpOffset);
11370 bool HasVOffset = !isNullConstant(VOffset);
11371 unsigned Size = Op->getConstantOperandVal(4);
11372
11373 switch (Size) {
11374 default:
11375 return SDValue();
11376 case 1:
11377 Opc = HasVIndex ? HasVOffset ? AMDGPU::BUFFER_LOAD_UBYTE_LDS_BOTHEN
11378 : AMDGPU::BUFFER_LOAD_UBYTE_LDS_IDXEN
11379 : HasVOffset ? AMDGPU::BUFFER_LOAD_UBYTE_LDS_OFFEN
11380 : AMDGPU::BUFFER_LOAD_UBYTE_LDS_OFFSET;
11381 break;
11382 case 2:
11383 Opc = HasVIndex ? HasVOffset ? AMDGPU::BUFFER_LOAD_USHORT_LDS_BOTHEN
11384 : AMDGPU::BUFFER_LOAD_USHORT_LDS_IDXEN
11385 : HasVOffset ? AMDGPU::BUFFER_LOAD_USHORT_LDS_OFFEN
11386 : AMDGPU::BUFFER_LOAD_USHORT_LDS_OFFSET;
11387 break;
11388 case 4:
11389 Opc = HasVIndex ? HasVOffset ? AMDGPU::BUFFER_LOAD_DWORD_LDS_BOTHEN
11390 : AMDGPU::BUFFER_LOAD_DWORD_LDS_IDXEN
11391 : HasVOffset ? AMDGPU::BUFFER_LOAD_DWORD_LDS_OFFEN
11392 : AMDGPU::BUFFER_LOAD_DWORD_LDS_OFFSET;
11393 break;
11394 case 12:
11395 if (!Subtarget->hasLDSLoadB96_B128())
11396 return SDValue();
11397 Opc = HasVIndex ? HasVOffset ? AMDGPU::BUFFER_LOAD_DWORDX3_LDS_BOTHEN
11398 : AMDGPU::BUFFER_LOAD_DWORDX3_LDS_IDXEN
11399 : HasVOffset ? AMDGPU::BUFFER_LOAD_DWORDX3_LDS_OFFEN
11400 : AMDGPU::BUFFER_LOAD_DWORDX3_LDS_OFFSET;
11401 break;
11402 case 16:
11403 if (!Subtarget->hasLDSLoadB96_B128())
11404 return SDValue();
11405 Opc = HasVIndex ? HasVOffset ? AMDGPU::BUFFER_LOAD_DWORDX4_LDS_BOTHEN
11406 : AMDGPU::BUFFER_LOAD_DWORDX4_LDS_IDXEN
11407 : HasVOffset ? AMDGPU::BUFFER_LOAD_DWORDX4_LDS_OFFEN
11408 : AMDGPU::BUFFER_LOAD_DWORDX4_LDS_OFFSET;
11409 break;
11410 }
11411
11412 SDValue M0Val = copyToM0(DAG, Chain, DL, Op.getOperand(3));
11413
11415
11416 if (HasVIndex && HasVOffset)
11417 Ops.push_back(DAG.getBuildVector(MVT::v2i32, DL,
11418 {Op.getOperand(5), // VIndex
11419 VOffset}));
11420 else if (HasVIndex)
11421 Ops.push_back(Op.getOperand(5));
11422 else if (HasVOffset)
11423 Ops.push_back(VOffset);
11424
11425 SDValue Rsrc = bufferRsrcPtrToVector(Op.getOperand(2), DAG);
11426 Ops.push_back(Rsrc);
11427 Ops.push_back(Op.getOperand(6 + OpOffset)); // soffset
11428 Ops.push_back(Op.getOperand(7 + OpOffset)); // imm offset
11429 bool IsGFX12Plus = AMDGPU::isGFX12Plus(*Subtarget);
11430 unsigned Aux = Op.getConstantOperandVal(8 + OpOffset);
11431 Ops.push_back(DAG.getTargetConstant(
11432 Aux & (IsGFX12Plus ? AMDGPU::CPol::ALL : AMDGPU::CPol::ALL_pregfx12),
11433 DL, MVT::i8)); // cpol
11434 Ops.push_back(DAG.getTargetConstant(
11435 Aux & (IsGFX12Plus ? AMDGPU::CPol::SWZ : AMDGPU::CPol::SWZ_pregfx12)
11436 ? 1
11437 : 0,
11438 DL, MVT::i8)); // swz
11439 Ops.push_back(M0Val.getValue(0)); // Chain
11440 Ops.push_back(M0Val.getValue(1)); // Glue
11441
11442 auto *M = cast<MemSDNode>(Op);
11443 MachineMemOperand *LoadMMO = M->getMemOperand();
11444 // Don't set the offset value here because the pointer points to the base of
11445 // the buffer.
11446 MachinePointerInfo LoadPtrI = LoadMMO->getPointerInfo();
11447
11448 MachinePointerInfo StorePtrI = LoadPtrI;
11449 LoadPtrI.V = PoisonValue::get(
11453
11454 auto F = LoadMMO->getFlags() &
11456 LoadMMO =
11458 LoadMMO->getBaseAlign(), LoadMMO->getAAInfo());
11459
11460 MachineMemOperand *StoreMMO = MF.getMachineMemOperand(
11461 StorePtrI, F | MachineMemOperand::MOStore, sizeof(int32_t),
11462 LoadMMO->getBaseAlign(), LoadMMO->getAAInfo());
11463
11464 auto *Load = DAG.getMachineNode(Opc, DL, M->getVTList(), Ops);
11465 DAG.setNodeMemRefs(Load, {LoadMMO, StoreMMO});
11466
11467 return SDValue(Load, 0);
11468 }
11469 // Buffers are handled by LowerBufferFatPointers, and we're going to go
11470 // for "trust me" that the remaining cases are global pointers until
11471 // such time as we can put two mem operands on an intrinsic.
11472 case Intrinsic::amdgcn_load_to_lds:
11473 case Intrinsic::amdgcn_global_load_lds: {
11474 if (!Subtarget->hasVMemToLDSLoad())
11475 return SDValue();
11476
11477 unsigned Opc;
11478 unsigned Size = Op->getConstantOperandVal(4);
11479 switch (Size) {
11480 default:
11481 return SDValue();
11482 case 1:
11483 Opc = AMDGPU::GLOBAL_LOAD_LDS_UBYTE;
11484 break;
11485 case 2:
11486 Opc = AMDGPU::GLOBAL_LOAD_LDS_USHORT;
11487 break;
11488 case 4:
11489 Opc = AMDGPU::GLOBAL_LOAD_LDS_DWORD;
11490 break;
11491 case 12:
11492 if (!Subtarget->hasLDSLoadB96_B128())
11493 return SDValue();
11494 Opc = AMDGPU::GLOBAL_LOAD_LDS_DWORDX3;
11495 break;
11496 case 16:
11497 if (!Subtarget->hasLDSLoadB96_B128())
11498 return SDValue();
11499 Opc = AMDGPU::GLOBAL_LOAD_LDS_DWORDX4;
11500 break;
11501 }
11502
11503 SDValue M0Val = copyToM0(DAG, Chain, DL, Op.getOperand(3));
11504
11506
11507 SDValue Addr = Op.getOperand(2); // Global ptr
11508 SDValue VOffset;
11509 // Try to split SAddr and VOffset. Global and LDS pointers share the same
11510 // immediate offset, so we cannot use a regular SelectGlobalSAddr().
11511 if (Addr->isDivergent() && Addr->isAnyAdd()) {
11512 SDValue LHS = Addr.getOperand(0);
11513 SDValue RHS = Addr.getOperand(1);
11514
11515 if (LHS->isDivergent())
11516 std::swap(LHS, RHS);
11517
11518 if (!LHS->isDivergent() && RHS.getOpcode() == ISD::ZERO_EXTEND &&
11519 RHS.getOperand(0).getValueType() == MVT::i32) {
11520 // add (i64 sgpr), (zero_extend (i32 vgpr))
11521 Addr = LHS;
11522 VOffset = RHS.getOperand(0);
11523 }
11524 }
11525
11526 Ops.push_back(Addr);
11527 if (!Addr->isDivergent()) {
11529 if (!VOffset)
11530 VOffset =
11531 SDValue(DAG.getMachineNode(AMDGPU::V_MOV_B32_e32, DL, MVT::i32,
11532 DAG.getTargetConstant(0, DL, MVT::i32)),
11533 0);
11534 Ops.push_back(VOffset);
11535 }
11536
11537 Ops.push_back(Op.getOperand(5)); // Offset
11538
11539 unsigned Aux = Op.getConstantOperandVal(6);
11540 Ops.push_back(DAG.getTargetConstant(Aux & ~AMDGPU::CPol::VIRTUAL_BITS, DL,
11541 MVT::i32)); // CPol
11542
11543 Ops.push_back(M0Val.getValue(0)); // Chain
11544 Ops.push_back(M0Val.getValue(1)); // Glue
11545
11546 auto *M = cast<MemSDNode>(Op);
11547 MachineMemOperand *LoadMMO = M->getMemOperand();
11548 MachinePointerInfo LoadPtrI = LoadMMO->getPointerInfo();
11549 LoadPtrI.Offset = Op->getConstantOperandVal(5);
11550 MachinePointerInfo StorePtrI = LoadPtrI;
11551 LoadPtrI.V = PoisonValue::get(
11555 auto F = LoadMMO->getFlags() &
11557 LoadMMO =
11559 LoadMMO->getBaseAlign(), LoadMMO->getAAInfo());
11560 MachineMemOperand *StoreMMO = MF.getMachineMemOperand(
11561 StorePtrI, F | MachineMemOperand::MOStore, sizeof(int32_t), Align(4),
11562 LoadMMO->getAAInfo());
11563
11564 auto *Load = DAG.getMachineNode(Opc, DL, Op->getVTList(), Ops);
11565 DAG.setNodeMemRefs(Load, {LoadMMO, StoreMMO});
11566
11567 return SDValue(Load, 0);
11568 }
11569 case Intrinsic::amdgcn_end_cf:
11570 return SDValue(DAG.getMachineNode(AMDGPU::SI_END_CF, DL, MVT::Other,
11571 Op->getOperand(2), Chain),
11572 0);
11573 case Intrinsic::amdgcn_s_barrier_init:
11574 case Intrinsic::amdgcn_s_barrier_signal_var: {
11575 // these two intrinsics have two operands: barrier pointer and member count
11576 SDValue Chain = Op->getOperand(0);
11578 SDValue BarOp = Op->getOperand(2);
11579 SDValue CntOp = Op->getOperand(3);
11580 SDValue M0Val;
11581 unsigned Opc = IntrinsicID == Intrinsic::amdgcn_s_barrier_init
11582 ? AMDGPU::S_BARRIER_INIT_M0
11583 : AMDGPU::S_BARRIER_SIGNAL_M0;
11584 // extract the BarrierID from bits 4-9 of BarOp
11585 SDValue BarID;
11586 BarID = DAG.getNode(ISD::SRL, DL, MVT::i32, BarOp,
11587 DAG.getShiftAmountConstant(4, MVT::i32, DL));
11588 BarID =
11589 SDValue(DAG.getMachineNode(AMDGPU::S_AND_B32, DL, MVT::i32, BarID,
11590 DAG.getTargetConstant(0x3F, DL, MVT::i32)),
11591 0);
11592 // Member count should be put into M0[ShAmt:+6]
11593 // Barrier ID should be put into M0[5:0]
11594 M0Val =
11595 SDValue(DAG.getMachineNode(AMDGPU::S_AND_B32, DL, MVT::i32, CntOp,
11596 DAG.getTargetConstant(0x3F, DL, MVT::i32)),
11597 0);
11598 constexpr unsigned ShAmt = 16;
11599 M0Val = DAG.getNode(ISD::SHL, DL, MVT::i32, CntOp,
11600 DAG.getShiftAmountConstant(ShAmt, MVT::i32, DL));
11601
11602 M0Val = SDValue(
11603 DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, M0Val, BarID), 0);
11604
11605 Ops.push_back(copyToM0(DAG, Chain, DL, M0Val).getValue(0));
11606
11607 auto *NewMI = DAG.getMachineNode(Opc, DL, Op->getVTList(), Ops);
11608 return SDValue(NewMI, 0);
11609 }
11610 case Intrinsic::amdgcn_s_wakeup_barrier: {
11611 if (!Subtarget->hasSWakeupBarrier())
11612 return SDValue();
11613 [[fallthrough]];
11614 }
11615 case Intrinsic::amdgcn_s_barrier_join: {
11616 // these three intrinsics have one operand: barrier pointer
11617 SDValue Chain = Op->getOperand(0);
11619 SDValue BarOp = Op->getOperand(2);
11620 unsigned Opc;
11621
11622 if (isa<ConstantSDNode>(BarOp)) {
11623 uint64_t BarVal = cast<ConstantSDNode>(BarOp)->getZExtValue();
11624 switch (IntrinsicID) {
11625 default:
11626 return SDValue();
11627 case Intrinsic::amdgcn_s_barrier_join:
11628 Opc = AMDGPU::S_BARRIER_JOIN_IMM;
11629 break;
11630 case Intrinsic::amdgcn_s_wakeup_barrier:
11631 Opc = AMDGPU::S_WAKEUP_BARRIER_IMM;
11632 break;
11633 }
11634 // extract the BarrierID from bits 4-9 of the immediate
11635 unsigned BarID = (BarVal >> 4) & 0x3F;
11636 SDValue K = DAG.getTargetConstant(BarID, DL, MVT::i32);
11637 Ops.push_back(K);
11638 Ops.push_back(Chain);
11639 } else {
11640 switch (IntrinsicID) {
11641 default:
11642 return SDValue();
11643 case Intrinsic::amdgcn_s_barrier_join:
11644 Opc = AMDGPU::S_BARRIER_JOIN_M0;
11645 break;
11646 case Intrinsic::amdgcn_s_wakeup_barrier:
11647 Opc = AMDGPU::S_WAKEUP_BARRIER_M0;
11648 break;
11649 }
11650 // extract the BarrierID from bits 4-9 of BarOp, copy to M0[5:0]
11651 SDValue M0Val;
11652 M0Val = DAG.getNode(ISD::SRL, DL, MVT::i32, BarOp,
11653 DAG.getShiftAmountConstant(4, MVT::i32, DL));
11654 M0Val =
11655 SDValue(DAG.getMachineNode(AMDGPU::S_AND_B32, DL, MVT::i32, M0Val,
11656 DAG.getTargetConstant(0x3F, DL, MVT::i32)),
11657 0);
11658 Ops.push_back(copyToM0(DAG, Chain, DL, M0Val).getValue(0));
11659 }
11660
11661 auto *NewMI = DAG.getMachineNode(Opc, DL, Op->getVTList(), Ops);
11662 return SDValue(NewMI, 0);
11663 }
11664 case Intrinsic::amdgcn_s_prefetch_data: {
11665 // For non-global address space preserve the chain and remove the call.
11667 return Op.getOperand(0);
11668 return Op;
11669 }
11670 case Intrinsic::amdgcn_s_buffer_prefetch_data: {
11671 SDValue Ops[] = {
11672 Chain, bufferRsrcPtrToVector(Op.getOperand(2), DAG),
11673 Op.getOperand(3), // offset
11674 Op.getOperand(4), // length
11675 };
11676
11677 MemSDNode *M = cast<MemSDNode>(Op);
11678 return DAG.getMemIntrinsicNode(AMDGPUISD::SBUFFER_PREFETCH_DATA, DL,
11679 Op->getVTList(), Ops, M->getMemoryVT(),
11680 M->getMemOperand());
11681 }
11682 case Intrinsic::amdgcn_cooperative_atomic_store_32x4B:
11683 case Intrinsic::amdgcn_cooperative_atomic_store_16x8B:
11684 case Intrinsic::amdgcn_cooperative_atomic_store_8x16B: {
11685 MemIntrinsicSDNode *MII = cast<MemIntrinsicSDNode>(Op);
11686 SDValue Chain = Op->getOperand(0);
11687 SDValue Ptr = Op->getOperand(2);
11688 SDValue Val = Op->getOperand(3);
11689 return DAG.getAtomic(ISD::ATOMIC_STORE, DL, MII->getMemoryVT(), Chain, Val,
11690 Ptr, MII->getMemOperand());
11691 }
11692 default: {
11693 if (const AMDGPU::ImageDimIntrinsicInfo *ImageDimIntr =
11695 return lowerImage(Op, ImageDimIntr, DAG, true);
11696
11697 return Op;
11698 }
11699 }
11700}
11701
11702// Return whether the operation has NoUnsignedWrap property.
11703static bool isNoUnsignedWrap(SDValue Addr) {
11704 return (Addr.getOpcode() == ISD::ADD &&
11705 Addr->getFlags().hasNoUnsignedWrap()) ||
11706 Addr->getOpcode() == ISD::OR;
11707}
11708
11710 EVT PtrVT) const {
11711 return PtrVT == MVT::i64;
11712}
11713
11715 EVT PtrVT) const {
11716 return true;
11717}
11718
11719// The raw.(t)buffer and struct.(t)buffer intrinsics have two offset args:
11720// offset (the offset that is included in bounds checking and swizzling, to be
11721// split between the instruction's voffset and immoffset fields) and soffset
11722// (the offset that is excluded from bounds checking and swizzling, to go in
11723// the instruction's soffset field). This function takes the first kind of
11724// offset and figures out how to split it between voffset and immoffset.
11725std::pair<SDValue, SDValue>
11726SITargetLowering::splitBufferOffsets(SDValue Offset, SelectionDAG &DAG) const {
11727 SDLoc DL(Offset);
11728 const unsigned MaxImm = SIInstrInfo::getMaxMUBUFImmOffset(*Subtarget);
11729 SDValue N0 = Offset;
11730 ConstantSDNode *C1 = nullptr;
11731
11732 if ((C1 = dyn_cast<ConstantSDNode>(N0)))
11733 N0 = SDValue();
11734 else if (DAG.isBaseWithConstantOffset(N0)) {
11735 // On GFX1250+, voffset and immoffset are zero-extended from 32 bits before
11736 // being added, so we can only safely match a 32-bit addition with no
11737 // unsigned overflow.
11738 bool CheckNUW = AMDGPU::isGFX1250(*Subtarget);
11739 if (!CheckNUW || isNoUnsignedWrap(N0)) {
11740 C1 = cast<ConstantSDNode>(N0.getOperand(1));
11741 N0 = N0.getOperand(0);
11742 }
11743 }
11744
11745 if (C1) {
11746 unsigned ImmOffset = C1->getZExtValue();
11747 // If the immediate value is too big for the immoffset field, put only bits
11748 // that would normally fit in the immoffset field. The remaining value that
11749 // is copied/added for the voffset field is a large power of 2, and it
11750 // stands more chance of being CSEd with the copy/add for another similar
11751 // load/store.
11752 // However, do not do that rounding down if that is a negative
11753 // number, as it appears to be illegal to have a negative offset in the
11754 // vgpr, even if adding the immediate offset makes it positive.
11755 unsigned Overflow = ImmOffset & ~MaxImm;
11756 ImmOffset -= Overflow;
11757 if ((int32_t)Overflow < 0) {
11758 Overflow += ImmOffset;
11759 ImmOffset = 0;
11760 }
11761 C1 = cast<ConstantSDNode>(DAG.getTargetConstant(ImmOffset, DL, MVT::i32));
11762 if (Overflow) {
11763 auto OverflowVal = DAG.getConstant(Overflow, DL, MVT::i32);
11764 if (!N0)
11765 N0 = OverflowVal;
11766 else {
11767 SDValue Ops[] = {N0, OverflowVal};
11768 N0 = DAG.getNode(ISD::ADD, DL, MVT::i32, Ops);
11769 }
11770 }
11771 }
11772 if (!N0)
11773 N0 = DAG.getConstant(0, DL, MVT::i32);
11774 if (!C1)
11775 C1 = cast<ConstantSDNode>(DAG.getTargetConstant(0, DL, MVT::i32));
11776 return {N0, SDValue(C1, 0)};
11777}
11778
11779// Analyze a combined offset from an amdgcn_s_buffer_load intrinsic and store
11780// the three offsets (voffset, soffset and instoffset) into the SDValue[3] array
11781// pointed to by Offsets.
11782void SITargetLowering::setBufferOffsets(SDValue CombinedOffset,
11783 SelectionDAG &DAG, SDValue *Offsets,
11784 Align Alignment) const {
11785 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
11786 SDLoc DL(CombinedOffset);
11787 if (auto *C = dyn_cast<ConstantSDNode>(CombinedOffset)) {
11788 uint32_t Imm = C->getZExtValue();
11789 uint32_t SOffset, ImmOffset;
11790 if (TII->splitMUBUFOffset(Imm, SOffset, ImmOffset, Alignment)) {
11791 Offsets[0] = DAG.getConstant(0, DL, MVT::i32);
11792 Offsets[1] = DAG.getConstant(SOffset, DL, MVT::i32);
11793 Offsets[2] = DAG.getTargetConstant(ImmOffset, DL, MVT::i32);
11794 return;
11795 }
11796 }
11797 if (DAG.isBaseWithConstantOffset(CombinedOffset)) {
11798 SDValue N0 = CombinedOffset.getOperand(0);
11799 SDValue N1 = CombinedOffset.getOperand(1);
11800 uint32_t SOffset, ImmOffset;
11801 int Offset = cast<ConstantSDNode>(N1)->getSExtValue();
11802 if (Offset >= 0 &&
11803 TII->splitMUBUFOffset(Offset, SOffset, ImmOffset, Alignment)) {
11804 Offsets[0] = N0;
11805 Offsets[1] = DAG.getConstant(SOffset, DL, MVT::i32);
11806 Offsets[2] = DAG.getTargetConstant(ImmOffset, DL, MVT::i32);
11807 return;
11808 }
11809 }
11810
11811 SDValue SOffsetZero = Subtarget->hasRestrictedSOffset()
11812 ? DAG.getRegister(AMDGPU::SGPR_NULL, MVT::i32)
11813 : DAG.getConstant(0, DL, MVT::i32);
11814
11815 Offsets[0] = CombinedOffset;
11816 Offsets[1] = SOffsetZero;
11817 Offsets[2] = DAG.getTargetConstant(0, DL, MVT::i32);
11818}
11819
11820SDValue SITargetLowering::bufferRsrcPtrToVector(SDValue MaybePointer,
11821 SelectionDAG &DAG) const {
11822 if (!MaybePointer.getValueType().isScalarInteger())
11823 return MaybePointer;
11824
11825 SDValue Rsrc = DAG.getBitcast(MVT::v4i32, MaybePointer);
11826 return Rsrc;
11827}
11828
11829// Wrap a global or flat pointer into a buffer intrinsic using the flags
11830// specified in the intrinsic.
11831SDValue SITargetLowering::lowerPointerAsRsrcIntrin(SDNode *Op,
11832 SelectionDAG &DAG) const {
11833 SDLoc Loc(Op);
11834
11835 SDValue Pointer = Op->getOperand(1);
11836 SDValue Stride = Op->getOperand(2);
11837 SDValue NumRecords = Op->getOperand(3);
11838 SDValue Flags = Op->getOperand(4);
11839
11840 SDValue ExtStride = DAG.getAnyExtOrTrunc(Stride, Loc, MVT::i32);
11841 SDValue Rsrc;
11842
11843 if (Subtarget->has45BitNumRecordsBufferResource()) {
11844 SDValue Zero = DAG.getConstant(0, Loc, MVT::i32);
11845 // Build the lower 64-bit value, which has a 57-bit base and the lower 7-bit
11846 // num_records.
11847 SDValue ExtPointer = DAG.getAnyExtOrTrunc(Pointer, Loc, MVT::i64);
11848 SDValue NumRecordsLHS =
11849 DAG.getNode(ISD::SHL, Loc, MVT::i64, NumRecords,
11850 DAG.getShiftAmountConstant(57, MVT::i32, Loc));
11851 SDValue LowHalf =
11852 DAG.getNode(ISD::OR, Loc, MVT::i64, ExtPointer, NumRecordsLHS);
11853
11854 // Build the higher 64-bit value, which has the higher 38-bit num_records,
11855 // 6-bit zero (omit), 16-bit stride and scale and 4-bit flag.
11856 SDValue NumRecordsRHS =
11857 DAG.getNode(ISD::SRL, Loc, MVT::i64, NumRecords,
11858 DAG.getShiftAmountConstant(7, MVT::i32, Loc));
11859 SDValue ShiftedStride =
11860 DAG.getNode(ISD::SHL, Loc, MVT::i32, ExtStride,
11861 DAG.getShiftAmountConstant(12, MVT::i32, Loc));
11862 SDValue ExtShiftedStrideVec =
11863 DAG.getNode(ISD::BUILD_VECTOR, Loc, MVT::v2i32, Zero, ShiftedStride);
11864 SDValue ExtShiftedStride =
11865 DAG.getNode(ISD::BITCAST, Loc, MVT::i64, ExtShiftedStrideVec);
11866 SDValue ShiftedFlags =
11867 DAG.getNode(ISD::SHL, Loc, MVT::i32, Flags,
11868 DAG.getShiftAmountConstant(28, MVT::i32, Loc));
11869 SDValue ExtShiftedFlagsVec =
11870 DAG.getNode(ISD::BUILD_VECTOR, Loc, MVT::v2i32, Zero, ShiftedFlags);
11871 SDValue ExtShiftedFlags =
11872 DAG.getNode(ISD::BITCAST, Loc, MVT::i64, ExtShiftedFlagsVec);
11873 SDValue CombinedFields =
11874 DAG.getNode(ISD::OR, Loc, MVT::i64, NumRecordsRHS, ExtShiftedStride);
11875 SDValue HighHalf =
11876 DAG.getNode(ISD::OR, Loc, MVT::i64, CombinedFields, ExtShiftedFlags);
11877
11878 Rsrc = DAG.getNode(ISD::BUILD_VECTOR, Loc, MVT::v2i64, LowHalf, HighHalf);
11879 } else {
11880 NumRecords = DAG.getAnyExtOrTrunc(NumRecords, Loc, MVT::i32);
11881 auto [LowHalf, HighHalf] =
11882 DAG.SplitScalar(Pointer, Loc, MVT::i32, MVT::i32);
11883 SDValue Mask = DAG.getConstant(0x0000ffff, Loc, MVT::i32);
11884 SDValue Masked = DAG.getNode(ISD::AND, Loc, MVT::i32, HighHalf, Mask);
11885 SDValue ShiftedStride =
11886 DAG.getNode(ISD::SHL, Loc, MVT::i32, ExtStride,
11887 DAG.getShiftAmountConstant(16, MVT::i32, Loc));
11888 SDValue NewHighHalf =
11889 DAG.getNode(ISD::OR, Loc, MVT::i32, Masked, ShiftedStride);
11890
11891 Rsrc = DAG.getNode(ISD::BUILD_VECTOR, Loc, MVT::v4i32, LowHalf, NewHighHalf,
11892 NumRecords, Flags);
11893 }
11894
11895 SDValue RsrcPtr = DAG.getNode(ISD::BITCAST, Loc, MVT::i128, Rsrc);
11896 return RsrcPtr;
11897}
11898
11899// Handle 8 bit and 16 bit buffer loads
11900SDValue SITargetLowering::handleByteShortBufferLoads(SelectionDAG &DAG,
11901 EVT LoadVT, SDLoc DL,
11903 MachineMemOperand *MMO,
11904 bool IsTFE) const {
11905 EVT IntVT = LoadVT.changeTypeToInteger();
11906
11907 if (IsTFE) {
11908 unsigned Opc = (LoadVT.getScalarType() == MVT::i8)
11909 ? AMDGPUISD::BUFFER_LOAD_UBYTE_TFE
11910 : AMDGPUISD::BUFFER_LOAD_USHORT_TFE;
11911 MachineFunction &MF = DAG.getMachineFunction();
11912 MachineMemOperand *OpMMO = MF.getMachineMemOperand(MMO, 0, 8);
11913 SDVTList VTs = DAG.getVTList(MVT::v2i32, MVT::Other);
11914 SDValue Op = getMemIntrinsicNode(Opc, DL, VTs, Ops, MVT::v2i32, OpMMO, DAG);
11915 SDValue Status = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Op,
11916 DAG.getConstant(1, DL, MVT::i32));
11917 SDValue Data = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Op,
11918 DAG.getConstant(0, DL, MVT::i32));
11919 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, IntVT, Data);
11920 SDValue Value = DAG.getNode(ISD::BITCAST, DL, LoadVT, Trunc);
11921 return DAG.getMergeValues({Value, Status, SDValue(Op.getNode(), 1)}, DL);
11922 }
11923
11924 unsigned Opc = LoadVT.getScalarType() == MVT::i8
11925 ? AMDGPUISD::BUFFER_LOAD_UBYTE
11926 : AMDGPUISD::BUFFER_LOAD_USHORT;
11927
11928 SDVTList ResList = DAG.getVTList(MVT::i32, MVT::Other);
11929 SDValue BufferLoad =
11930 DAG.getMemIntrinsicNode(Opc, DL, ResList, Ops, IntVT, MMO);
11931 SDValue LoadVal = DAG.getNode(ISD::TRUNCATE, DL, IntVT, BufferLoad);
11932 LoadVal = DAG.getNode(ISD::BITCAST, DL, LoadVT, LoadVal);
11933
11934 return DAG.getMergeValues({LoadVal, BufferLoad.getValue(1)}, DL);
11935}
11936
11937// Handle 8 bit and 16 bit buffer stores
11938SDValue SITargetLowering::handleByteShortBufferStores(SelectionDAG &DAG,
11939 EVT VDataType, SDLoc DL,
11940 SDValue Ops[],
11941 MemSDNode *M) const {
11942 if (VDataType == MVT::f16 || VDataType == MVT::bf16)
11943 Ops[1] = DAG.getNode(ISD::BITCAST, DL, MVT::i16, Ops[1]);
11944
11945 SDValue BufferStoreExt = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, Ops[1]);
11946 Ops[1] = BufferStoreExt;
11947 unsigned Opc = (VDataType == MVT::i8) ? AMDGPUISD::BUFFER_STORE_BYTE
11948 : AMDGPUISD::BUFFER_STORE_SHORT;
11949 ArrayRef<SDValue> OpsRef = ArrayRef(&Ops[0], 9);
11950 return DAG.getMemIntrinsicNode(Opc, DL, M->getVTList(), OpsRef, VDataType,
11951 M->getMemOperand());
11952}
11953
11955 SDValue Op, const SDLoc &SL, EVT VT) {
11956 if (VT.bitsLT(Op.getValueType()))
11957 return DAG.getNode(ISD::TRUNCATE, SL, VT, Op);
11958
11959 switch (ExtType) {
11960 case ISD::SEXTLOAD:
11961 return DAG.getNode(ISD::SIGN_EXTEND, SL, VT, Op);
11962 case ISD::ZEXTLOAD:
11963 return DAG.getNode(ISD::ZERO_EXTEND, SL, VT, Op);
11964 case ISD::EXTLOAD:
11965 return DAG.getNode(ISD::ANY_EXTEND, SL, VT, Op);
11966 case ISD::NON_EXTLOAD:
11967 return Op;
11968 }
11969
11970 llvm_unreachable("invalid ext type");
11971}
11972
11973// Try to turn 8 and 16-bit scalar loads into SMEM eligible 32-bit loads.
11974// TODO: Skip this on GFX12 which does have scalar sub-dword loads.
11975SDValue SITargetLowering::widenLoad(LoadSDNode *Ld,
11976 DAGCombinerInfo &DCI) const {
11977 SelectionDAG &DAG = DCI.DAG;
11978 if (Ld->getAlign() < Align(4) || Ld->isDivergent())
11979 return SDValue();
11980
11981 // FIXME: Constant loads should all be marked invariant.
11982 unsigned AS = Ld->getAddressSpace();
11983 if (AS != AMDGPUAS::CONSTANT_ADDRESS &&
11985 (AS != AMDGPUAS::GLOBAL_ADDRESS || !Ld->isInvariant()))
11986 return SDValue();
11987
11988 // Don't do this early, since it may interfere with adjacent load merging for
11989 // illegal types. We can avoid losing alignment information for exotic types
11990 // pre-legalize.
11991 EVT MemVT = Ld->getMemoryVT();
11992 if ((MemVT.isSimple() && !DCI.isAfterLegalizeDAG()) ||
11993 MemVT.getSizeInBits() >= 32)
11994 return SDValue();
11995
11996 SDLoc SL(Ld);
11997
11998 assert((!MemVT.isVector() || Ld->getExtensionType() == ISD::NON_EXTLOAD) &&
11999 "unexpected vector extload");
12000
12001 // TODO: Drop only high part of range.
12002 SDValue Ptr = Ld->getBasePtr();
12003 SDValue NewLoad = DAG.getLoad(
12004 ISD::UNINDEXED, ISD::NON_EXTLOAD, MVT::i32, SL, Ld->getChain(), Ptr,
12005 Ld->getOffset(), Ld->getPointerInfo(), MVT::i32, Ld->getAlign(),
12006 Ld->getMemOperand()->getFlags(), Ld->getAAInfo(),
12007 nullptr); // Drop ranges
12008
12009 EVT TruncVT = EVT::getIntegerVT(*DAG.getContext(), MemVT.getSizeInBits());
12010 if (MemVT.isFloatingPoint()) {
12012 "unexpected fp extload");
12013 TruncVT = MemVT.changeTypeToInteger();
12014 }
12015
12016 SDValue Cvt = NewLoad;
12017 if (Ld->getExtensionType() == ISD::SEXTLOAD) {
12018 Cvt = DAG.getNode(ISD::SIGN_EXTEND_INREG, SL, MVT::i32, NewLoad,
12019 DAG.getValueType(TruncVT));
12020 } else if (Ld->getExtensionType() == ISD::ZEXTLOAD ||
12022 Cvt = DAG.getZeroExtendInReg(NewLoad, SL, TruncVT);
12023 } else {
12025 }
12026
12027 EVT VT = Ld->getValueType(0);
12028 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
12029
12030 DCI.AddToWorklist(Cvt.getNode());
12031
12032 // We may need to handle exotic cases, such as i16->i64 extloads, so insert
12033 // the appropriate extension from the 32-bit load.
12034 Cvt = getLoadExtOrTrunc(DAG, Ld->getExtensionType(), Cvt, SL, IntVT);
12035 DCI.AddToWorklist(Cvt.getNode());
12036
12037 // Handle conversion back to floating point if necessary.
12038 Cvt = DAG.getNode(ISD::BITCAST, SL, VT, Cvt);
12039
12040 return DAG.getMergeValues({Cvt, NewLoad.getValue(1)}, SL);
12041}
12042
12044 const SIMachineFunctionInfo &Info) {
12045 // TODO: Should check if the address can definitely not access stack.
12046 if (Info.isEntryFunction())
12047 return Info.getUserSGPRInfo().hasFlatScratchInit();
12048 return true;
12049}
12050
12051SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
12052 SDLoc DL(Op);
12053 LoadSDNode *Load = cast<LoadSDNode>(Op);
12054 ISD::LoadExtType ExtType = Load->getExtensionType();
12055 EVT MemVT = Load->getMemoryVT();
12056 MachineMemOperand *MMO = Load->getMemOperand();
12057
12058 if (ExtType == ISD::NON_EXTLOAD && MemVT.getSizeInBits() < 32) {
12059 if (MemVT == MVT::i16 && isTypeLegal(MVT::i16))
12060 return SDValue();
12061
12062 // FIXME: Copied from PPC
12063 // First, load into 32 bits, then truncate to 1 bit.
12064
12065 SDValue Chain = Load->getChain();
12066 SDValue BasePtr = Load->getBasePtr();
12067
12068 EVT RealMemVT = (MemVT == MVT::i1) ? MVT::i8 : MVT::i16;
12069
12070 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain, BasePtr,
12071 RealMemVT, MMO);
12072
12073 if (!MemVT.isVector()) {
12074 SDValue Ops[] = {DAG.getNode(ISD::TRUNCATE, DL, MemVT, NewLD),
12075 NewLD.getValue(1)};
12076
12077 return DAG.getMergeValues(Ops, DL);
12078 }
12079
12081 for (unsigned I = 0, N = MemVT.getVectorNumElements(); I != N; ++I) {
12082 SDValue Elt = DAG.getNode(ISD::SRL, DL, MVT::i32, NewLD,
12083 DAG.getConstant(I, DL, MVT::i32));
12084
12085 Elts.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i1, Elt));
12086 }
12087
12088 SDValue Ops[] = {DAG.getBuildVector(MemVT, DL, Elts), NewLD.getValue(1)};
12089
12090 return DAG.getMergeValues(Ops, DL);
12091 }
12092
12093 if (!MemVT.isVector())
12094 return SDValue();
12095
12096 assert(Op.getValueType().getVectorElementType() == MVT::i32 &&
12097 "Custom lowering for non-i32 vectors hasn't been implemented.");
12098
12099 Align Alignment = Load->getAlign();
12100 unsigned AS = Load->getAddressSpace();
12101 if (Subtarget->hasLDSMisalignedBug() && AS == AMDGPUAS::FLAT_ADDRESS &&
12102 Alignment.value() < MemVT.getStoreSize() && MemVT.getSizeInBits() > 32) {
12103 return SplitVectorLoad(Op, DAG);
12104 }
12105
12106 MachineFunction &MF = DAG.getMachineFunction();
12107 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
12108 // If there is a possibility that flat instruction access scratch memory
12109 // then we need to use the same legalization rules we use for private.
12110 if (AS == AMDGPUAS::FLAT_ADDRESS &&
12111 !Subtarget->hasMultiDwordFlatScratchAddressing())
12112 AS = addressMayBeAccessedAsPrivate(Load->getMemOperand(), *MFI)
12115
12116 unsigned NumElements = MemVT.getVectorNumElements();
12117
12118 if (AS == AMDGPUAS::CONSTANT_ADDRESS ||
12120 (AS == AMDGPUAS::GLOBAL_ADDRESS &&
12121 Subtarget->getScalarizeGlobalBehavior() && Load->isSimple() &&
12122 (Load->isInvariant() || isMemOpHasNoClobberedMemOperand(Load)))) {
12123 if ((!Op->isDivergent() || AMDGPU::isUniformMMO(MMO)) &&
12124 Alignment >= Align(4) && NumElements < 32) {
12125 if (MemVT.isPow2VectorType() ||
12126 (Subtarget->hasScalarDwordx3Loads() && NumElements == 3))
12127 return SDValue();
12128 return WidenOrSplitVectorLoad(Op, DAG);
12129 }
12130 // Non-uniform loads will be selected to MUBUF instructions, so they
12131 // have the same legalization requirements as global and private
12132 // loads.
12133 //
12134 }
12135 if (AS == AMDGPUAS::CONSTANT_ADDRESS ||
12138 if (NumElements > 4)
12139 return SplitVectorLoad(Op, DAG);
12140 // v3 loads not supported on SI.
12141 if (NumElements == 3 && !Subtarget->hasDwordx3LoadStores())
12142 return WidenOrSplitVectorLoad(Op, DAG);
12143
12144 // v3 and v4 loads are supported for private and global memory.
12145 return SDValue();
12146 }
12147 if (AS == AMDGPUAS::PRIVATE_ADDRESS) {
12148 // Depending on the setting of the private_element_size field in the
12149 // resource descriptor, we can only make private accesses up to a certain
12150 // size.
12151 switch (Subtarget->getMaxPrivateElementSize()) {
12152 case 4: {
12153 auto [Op0, Op1] = scalarizeVectorLoad(Load, DAG);
12154 return DAG.getMergeValues({Op0, Op1}, DL);
12155 }
12156 case 8:
12157 if (NumElements > 2)
12158 return SplitVectorLoad(Op, DAG);
12159 return SDValue();
12160 case 16:
12161 // Same as global/flat
12162 if (NumElements > 4)
12163 return SplitVectorLoad(Op, DAG);
12164 // v3 loads not supported on SI.
12165 if (NumElements == 3 && !Subtarget->hasDwordx3LoadStores())
12166 return WidenOrSplitVectorLoad(Op, DAG);
12167
12168 return SDValue();
12169 default:
12170 llvm_unreachable("unsupported private_element_size");
12171 }
12172 } else if (AS == AMDGPUAS::LOCAL_ADDRESS || AS == AMDGPUAS::REGION_ADDRESS) {
12173 unsigned Fast = 0;
12174 auto Flags = Load->getMemOperand()->getFlags();
12176 Load->getAlign(), Flags, &Fast) &&
12177 Fast > 1)
12178 return SDValue();
12179
12180 if (MemVT.isVector())
12181 return SplitVectorLoad(Op, DAG);
12182 }
12183
12185 MemVT, *Load->getMemOperand())) {
12186 auto [Op0, Op1] = expandUnalignedLoad(Load, DAG);
12187 return DAG.getMergeValues({Op0, Op1}, DL);
12188 }
12189
12190 return SDValue();
12191}
12192
12193SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
12194 EVT VT = Op.getValueType();
12195 if (VT.getSizeInBits() == 128 || VT.getSizeInBits() == 256 ||
12196 VT.getSizeInBits() == 512)
12197 return splitTernaryVectorOp(Op, DAG);
12198
12199 assert(VT.getSizeInBits() == 64);
12200
12201 SDLoc DL(Op);
12202 SDValue Cond = DAG.getFreeze(Op.getOperand(0));
12203
12204 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
12205 SDValue One = DAG.getConstant(1, DL, MVT::i32);
12206
12207 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
12208 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
12209
12210 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
12211 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
12212
12213 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
12214
12215 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
12216 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
12217
12218 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
12219
12220 SDValue Res = DAG.getBuildVector(MVT::v2i32, DL, {Lo, Hi});
12221 return DAG.getNode(ISD::BITCAST, DL, VT, Res);
12222}
12223
12224// Catch division cases where we can use shortcuts with rcp and rsq
12225// instructions.
12226SDValue SITargetLowering::lowerFastUnsafeFDIV(SDValue Op,
12227 SelectionDAG &DAG) const {
12228 SDLoc SL(Op);
12229 SDValue LHS = Op.getOperand(0);
12230 SDValue RHS = Op.getOperand(1);
12231 EVT VT = Op.getValueType();
12232 const SDNodeFlags Flags = Op->getFlags();
12233
12234 bool AllowInaccurateRcp = Flags.hasApproximateFuncs();
12235
12236 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
12237 // Without !fpmath accuracy information, we can't do more because we don't
12238 // know exactly whether rcp is accurate enough to meet !fpmath requirement.
12239 // f16 is always accurate enough
12240 if (!AllowInaccurateRcp && VT != MVT::f16 && VT != MVT::bf16)
12241 return SDValue();
12242
12243 if (CLHS->isExactlyValue(1.0)) {
12244 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to
12245 // the CI documentation has a worst case error of 1 ulp.
12246 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to
12247 // use it as long as we aren't trying to use denormals.
12248 //
12249 // v_rcp_f16 and v_rsq_f16 DO support denormals and 0.51ulp.
12250
12251 // 1.0 / sqrt(x) -> rsq(x)
12252
12253 // XXX - Is afn sufficient to do this for f64? The maximum ULP
12254 // error seems really high at 2^29 ULP.
12255 // 1.0 / x -> rcp(x)
12256 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
12257 }
12258
12259 // Same as for 1.0, but expand the sign out of the constant.
12260 if (CLHS->isExactlyValue(-1.0)) {
12261 // -1.0 / x -> rcp (fneg x)
12262 SDValue FNegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
12263 return DAG.getNode(AMDGPUISD::RCP, SL, VT, FNegRHS);
12264 }
12265 }
12266
12267 // For f16 and bf16 require afn or arcp.
12268 // For f32 require afn.
12269 if (!AllowInaccurateRcp &&
12270 ((VT != MVT::f16 && VT != MVT::bf16) || !Flags.hasAllowReciprocal()))
12271 return SDValue();
12272
12273 // Turn into multiply by the reciprocal.
12274 // x / y -> x * (1.0 / y)
12275 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
12276 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip, Flags);
12277}
12278
12279SDValue SITargetLowering::lowerFastUnsafeFDIV64(SDValue Op,
12280 SelectionDAG &DAG) const {
12281 SDLoc SL(Op);
12282 SDValue X = Op.getOperand(0);
12283 SDValue Y = Op.getOperand(1);
12284 EVT VT = Op.getValueType();
12285 const SDNodeFlags Flags = Op->getFlags();
12286
12287 bool AllowInaccurateDiv = Flags.hasApproximateFuncs();
12288 if (!AllowInaccurateDiv)
12289 return SDValue();
12290
12291 SDValue NegY = DAG.getNode(ISD::FNEG, SL, VT, Y);
12292 SDValue One = DAG.getConstantFP(1.0, SL, VT);
12293
12294 SDValue R = DAG.getNode(AMDGPUISD::RCP, SL, VT, Y);
12295 SDValue Tmp0 = DAG.getNode(ISD::FMA, SL, VT, NegY, R, One);
12296
12297 R = DAG.getNode(ISD::FMA, SL, VT, Tmp0, R, R);
12298 SDValue Tmp1 = DAG.getNode(ISD::FMA, SL, VT, NegY, R, One);
12299 R = DAG.getNode(ISD::FMA, SL, VT, Tmp1, R, R);
12300 SDValue Ret = DAG.getNode(ISD::FMUL, SL, VT, X, R);
12301 SDValue Tmp2 = DAG.getNode(ISD::FMA, SL, VT, NegY, Ret, X);
12302 return DAG.getNode(ISD::FMA, SL, VT, Tmp2, R, Ret);
12303}
12304
12305static SDValue getFPBinOp(SelectionDAG &DAG, unsigned Opcode, const SDLoc &SL,
12306 EVT VT, SDValue A, SDValue B, SDValue GlueChain,
12307 SDNodeFlags Flags) {
12308 if (GlueChain->getNumValues() <= 1) {
12309 return DAG.getNode(Opcode, SL, VT, A, B, Flags);
12310 }
12311
12312 assert(GlueChain->getNumValues() == 3);
12313
12314 SDVTList VTList = DAG.getVTList(VT, MVT::Other, MVT::Glue);
12315 switch (Opcode) {
12316 default:
12317 llvm_unreachable("no chain equivalent for opcode");
12318 case ISD::FMUL:
12319 Opcode = AMDGPUISD::FMUL_W_CHAIN;
12320 break;
12321 }
12322
12323 return DAG.getNode(Opcode, SL, VTList,
12324 {GlueChain.getValue(1), A, B, GlueChain.getValue(2)},
12325 Flags);
12326}
12327
12328static SDValue getFPTernOp(SelectionDAG &DAG, unsigned Opcode, const SDLoc &SL,
12329 EVT VT, SDValue A, SDValue B, SDValue C,
12330 SDValue GlueChain, SDNodeFlags Flags) {
12331 if (GlueChain->getNumValues() <= 1) {
12332 return DAG.getNode(Opcode, SL, VT, {A, B, C}, Flags);
12333 }
12334
12335 assert(GlueChain->getNumValues() == 3);
12336
12337 SDVTList VTList = DAG.getVTList(VT, MVT::Other, MVT::Glue);
12338 switch (Opcode) {
12339 default:
12340 llvm_unreachable("no chain equivalent for opcode");
12341 case ISD::FMA:
12342 Opcode = AMDGPUISD::FMA_W_CHAIN;
12343 break;
12344 }
12345
12346 return DAG.getNode(Opcode, SL, VTList,
12347 {GlueChain.getValue(1), A, B, C, GlueChain.getValue(2)},
12348 Flags);
12349}
12350
12351SDValue SITargetLowering::LowerFDIV16(SDValue Op, SelectionDAG &DAG) const {
12352 if (SDValue FastLowered = lowerFastUnsafeFDIV(Op, DAG))
12353 return FastLowered;
12354
12355 SDLoc SL(Op);
12356 EVT VT = Op.getValueType();
12357 SDValue LHS = Op.getOperand(0);
12358 SDValue RHS = Op.getOperand(1);
12359
12360 SDValue LHSExt = DAG.getNode(ISD::FP_EXTEND, SL, MVT::f32, LHS);
12361 SDValue RHSExt = DAG.getNode(ISD::FP_EXTEND, SL, MVT::f32, RHS);
12362
12363 if (VT == MVT::bf16) {
12364 SDValue ExtDiv =
12365 DAG.getNode(ISD::FDIV, SL, MVT::f32, LHSExt, RHSExt, Op->getFlags());
12366 return DAG.getNode(ISD::FP_ROUND, SL, MVT::bf16, ExtDiv,
12367 DAG.getTargetConstant(0, SL, MVT::i32));
12368 }
12369
12370 assert(VT == MVT::f16);
12371
12372 // a32.u = opx(V_CVT_F32_F16, a.u); // CVT to F32
12373 // b32.u = opx(V_CVT_F32_F16, b.u); // CVT to F32
12374 // r32.u = opx(V_RCP_F32, b32.u); // rcp = 1 / d
12375 // q32.u = opx(V_MUL_F32, a32.u, r32.u); // q = n * rcp
12376 // e32.u = opx(V_MAD_F32, (b32.u^_neg32), q32.u, a32.u); // err = -d * q + n
12377 // q32.u = opx(V_MAD_F32, e32.u, r32.u, q32.u); // q = n * rcp
12378 // e32.u = opx(V_MAD_F32, (b32.u^_neg32), q32.u, a32.u); // err = -d * q + n
12379 // tmp.u = opx(V_MUL_F32, e32.u, r32.u);
12380 // tmp.u = opx(V_AND_B32, tmp.u, 0xff800000)
12381 // q32.u = opx(V_ADD_F32, tmp.u, q32.u);
12382 // q16.u = opx(V_CVT_F16_F32, q32.u);
12383 // q16.u = opx(V_DIV_FIXUP_F16, q16.u, b.u, a.u); // q = touchup(q, d, n)
12384
12385 // We will use ISD::FMA on targets that don't support ISD::FMAD.
12386 unsigned FMADOpCode =
12388 SDValue NegRHSExt = DAG.getNode(ISD::FNEG, SL, MVT::f32, RHSExt);
12389 SDValue Rcp =
12390 DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, RHSExt, Op->getFlags());
12391 SDValue Quot =
12392 DAG.getNode(ISD::FMUL, SL, MVT::f32, LHSExt, Rcp, Op->getFlags());
12393 SDValue Err = DAG.getNode(FMADOpCode, SL, MVT::f32, NegRHSExt, Quot, LHSExt,
12394 Op->getFlags());
12395 Quot = DAG.getNode(FMADOpCode, SL, MVT::f32, Err, Rcp, Quot, Op->getFlags());
12396 Err = DAG.getNode(FMADOpCode, SL, MVT::f32, NegRHSExt, Quot, LHSExt,
12397 Op->getFlags());
12398 SDValue Tmp = DAG.getNode(ISD::FMUL, SL, MVT::f32, Err, Rcp, Op->getFlags());
12399 SDValue TmpCast = DAG.getNode(ISD::BITCAST, SL, MVT::i32, Tmp);
12400 TmpCast = DAG.getNode(ISD::AND, SL, MVT::i32, TmpCast,
12401 DAG.getConstant(0xff800000, SL, MVT::i32));
12402 Tmp = DAG.getNode(ISD::BITCAST, SL, MVT::f32, TmpCast);
12403 Quot = DAG.getNode(ISD::FADD, SL, MVT::f32, Tmp, Quot, Op->getFlags());
12404 SDValue RDst = DAG.getNode(ISD::FP_ROUND, SL, MVT::f16, Quot,
12405 DAG.getTargetConstant(0, SL, MVT::i32));
12406 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f16, RDst, RHS, LHS,
12407 Op->getFlags());
12408}
12409
12410// Faster 2.5 ULP division that does not support denormals.
12411SDValue SITargetLowering::lowerFDIV_FAST(SDValue Op, SelectionDAG &DAG) const {
12412 SDNodeFlags Flags = Op->getFlags();
12413 SDLoc SL(Op);
12414 SDValue LHS = Op.getOperand(1);
12415 SDValue RHS = Op.getOperand(2);
12416
12417 // TODO: The combiner should probably handle elimination of redundant fabs.
12419 ? RHS
12420 : DAG.getNode(ISD::FABS, SL, MVT::f32, RHS, Flags);
12421
12422 const APFloat K0Val(0x1p+96f);
12423 const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32);
12424
12425 const APFloat K1Val(0x1p-32f);
12426 const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32);
12427
12428 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
12429
12430 EVT SetCCVT =
12431 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32);
12432
12433 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
12434
12435 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One, Flags);
12436
12437 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3, Flags);
12438
12439 // rcp does not support denormals.
12440 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1, Flags);
12441
12442 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0, Flags);
12443
12444 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul, Flags);
12445}
12446
12447// Returns immediate value for setting the F32 denorm mode when using the
12448// S_DENORM_MODE instruction.
12451 const GCNSubtarget *ST) {
12452 assert(ST->hasDenormModeInst() && "Requires S_DENORM_MODE");
12453 uint32_t DPDenormModeDefault = Info->getMode().fpDenormModeDPValue();
12454 uint32_t Mode = SPDenormMode | (DPDenormModeDefault << 2);
12455 return DAG.getTargetConstant(Mode, SDLoc(), MVT::i32);
12456}
12457
12458SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
12459 if (SDValue FastLowered = lowerFastUnsafeFDIV(Op, DAG))
12460 return FastLowered;
12461
12462 // The selection matcher assumes anything with a chain selecting to a
12463 // mayRaiseFPException machine instruction. Since we're introducing a chain
12464 // here, we need to explicitly report nofpexcept for the regular fdiv
12465 // lowering.
12466 SDNodeFlags Flags = Op->getFlags();
12467 Flags.setNoFPExcept(true);
12468
12469 SDLoc SL(Op);
12470 SDValue LHS = Op.getOperand(0);
12471 SDValue RHS = Op.getOperand(1);
12472
12473 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
12474
12475 SDVTList ScaleVT = DAG.getVTList(MVT::f32, MVT::i1);
12476
12477 SDValue DenominatorScaled =
12478 DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, {RHS, RHS, LHS}, Flags);
12479 SDValue NumeratorScaled =
12480 DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, {LHS, RHS, LHS}, Flags);
12481
12482 // Denominator is scaled to not be denormal, so using rcp is ok.
12483 SDValue ApproxRcp =
12484 DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, DenominatorScaled, Flags);
12485 SDValue NegDivScale0 =
12486 DAG.getNode(ISD::FNEG, SL, MVT::f32, DenominatorScaled, Flags);
12487
12488 using namespace AMDGPU::Hwreg;
12489 const unsigned Denorm32Reg = HwregEncoding::encode(ID_MODE, 4, 2);
12490 const SDValue BitField = DAG.getTargetConstant(Denorm32Reg, SL, MVT::i32);
12491
12492 const MachineFunction &MF = DAG.getMachineFunction();
12493 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
12494 const DenormalMode DenormMode = Info->getMode().FP32Denormals;
12495
12496 const bool PreservesDenormals = DenormMode == DenormalMode::getIEEE();
12497 const bool HasDynamicDenormals =
12498 (DenormMode.Input == DenormalMode::Dynamic) ||
12499 (DenormMode.Output == DenormalMode::Dynamic);
12500
12501 SDValue SavedDenormMode;
12502
12503 if (!PreservesDenormals) {
12504 // Note we can't use the STRICT_FMA/STRICT_FMUL for the non-strict FDIV
12505 // lowering. The chain dependence is insufficient, and we need glue. We do
12506 // not need the glue variants in a strictfp function.
12507
12508 SDVTList BindParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
12509
12510 SDValue Glue = DAG.getEntryNode();
12511 if (HasDynamicDenormals) {
12512 SDNode *GetReg = DAG.getMachineNode(AMDGPU::S_GETREG_B32, SL,
12513 DAG.getVTList(MVT::i32, MVT::Glue),
12514 {BitField, Glue});
12515 SavedDenormMode = SDValue(GetReg, 0);
12516
12517 Glue = DAG.getMergeValues(
12518 {DAG.getEntryNode(), SDValue(GetReg, 0), SDValue(GetReg, 1)}, SL);
12519 }
12520
12521 SDNode *EnableDenorm;
12522 if (Subtarget->hasDenormModeInst()) {
12523 const SDValue EnableDenormValue =
12525
12526 EnableDenorm = DAG.getNode(AMDGPUISD::DENORM_MODE, SL, BindParamVTs, Glue,
12527 EnableDenormValue)
12528 .getNode();
12529 } else {
12530 const SDValue EnableDenormValue =
12531 DAG.getConstant(FP_DENORM_FLUSH_NONE, SL, MVT::i32);
12532 EnableDenorm = DAG.getMachineNode(AMDGPU::S_SETREG_B32, SL, BindParamVTs,
12533 {EnableDenormValue, BitField, Glue});
12534 }
12535
12536 SDValue Ops[3] = {NegDivScale0, SDValue(EnableDenorm, 0),
12537 SDValue(EnableDenorm, 1)};
12538
12539 NegDivScale0 = DAG.getMergeValues(Ops, SL);
12540 }
12541
12542 SDValue Fma0 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, NegDivScale0,
12543 ApproxRcp, One, NegDivScale0, Flags);
12544
12545 SDValue Fma1 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, Fma0, ApproxRcp,
12546 ApproxRcp, Fma0, Flags);
12547
12548 SDValue Mul = getFPBinOp(DAG, ISD::FMUL, SL, MVT::f32, NumeratorScaled, Fma1,
12549 Fma1, Flags);
12550
12551 SDValue Fma2 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, NegDivScale0, Mul,
12552 NumeratorScaled, Mul, Flags);
12553
12554 SDValue Fma3 =
12555 getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, Fma2, Fma1, Mul, Fma2, Flags);
12556
12557 SDValue Fma4 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, NegDivScale0, Fma3,
12558 NumeratorScaled, Fma3, Flags);
12559
12560 if (!PreservesDenormals) {
12561 SDNode *DisableDenorm;
12562 if (!HasDynamicDenormals && Subtarget->hasDenormModeInst()) {
12563 const SDValue DisableDenormValue = getSPDenormModeValue(
12564 FP_DENORM_FLUSH_IN_FLUSH_OUT, DAG, Info, Subtarget);
12565
12566 SDVTList BindParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
12567 DisableDenorm =
12568 DAG.getNode(AMDGPUISD::DENORM_MODE, SL, BindParamVTs,
12569 Fma4.getValue(1), DisableDenormValue, Fma4.getValue(2))
12570 .getNode();
12571 } else {
12572 assert(HasDynamicDenormals == (bool)SavedDenormMode);
12573 const SDValue DisableDenormValue =
12574 HasDynamicDenormals
12575 ? SavedDenormMode
12576 : DAG.getConstant(FP_DENORM_FLUSH_IN_FLUSH_OUT, SL, MVT::i32);
12577
12578 DisableDenorm = DAG.getMachineNode(
12579 AMDGPU::S_SETREG_B32, SL, MVT::Other,
12580 {DisableDenormValue, BitField, Fma4.getValue(1), Fma4.getValue(2)});
12581 }
12582
12583 SDValue OutputChain = DAG.getNode(ISD::TokenFactor, SL, MVT::Other,
12584 SDValue(DisableDenorm, 0), DAG.getRoot());
12585 DAG.setRoot(OutputChain);
12586 }
12587
12588 SDValue Scale = NumeratorScaled.getValue(1);
12589 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f32,
12590 {Fma4, Fma1, Fma3, Scale}, Flags);
12591
12592 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f32, Fmas, RHS, LHS, Flags);
12593}
12594
12595SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
12596 if (SDValue FastLowered = lowerFastUnsafeFDIV64(Op, DAG))
12597 return FastLowered;
12598
12599 SDLoc SL(Op);
12600 SDValue X = Op.getOperand(0);
12601 SDValue Y = Op.getOperand(1);
12602
12603 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
12604
12605 SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1);
12606
12607 SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X);
12608
12609 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0);
12610
12611 SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0);
12612
12613 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One);
12614
12615 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp);
12616
12617 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One);
12618
12619 SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X);
12620
12621 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1);
12622 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3);
12623
12624 SDValue Fma4 =
12625 DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Mul, DivScale1);
12626
12627 SDValue Scale;
12628
12629 if (!Subtarget->hasUsableDivScaleConditionOutput()) {
12630 // Workaround a hardware bug on SI where the condition output from div_scale
12631 // is not usable.
12632
12633 const SDValue Hi = DAG.getConstant(1, SL, MVT::i32);
12634
12635 // Figure out if the scale to use for div_fmas.
12636 SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
12637 SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y);
12638 SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0);
12639 SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1);
12640
12641 SDValue NumHi =
12642 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi);
12643 SDValue DenHi =
12644 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi);
12645
12646 SDValue Scale0Hi =
12647 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi);
12648 SDValue Scale1Hi =
12649 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi);
12650
12651 SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ);
12652 SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ);
12653 Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen);
12654 } else {
12655 Scale = DivScale1.getValue(1);
12656 }
12657
12658 SDValue Fmas =
12659 DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64, Fma4, Fma3, Mul, Scale);
12660
12661 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X);
12662}
12663
12664SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
12665 EVT VT = Op.getValueType();
12666
12667 if (VT == MVT::f32)
12668 return LowerFDIV32(Op, DAG);
12669
12670 if (VT == MVT::f64)
12671 return LowerFDIV64(Op, DAG);
12672
12673 if (VT == MVT::f16 || VT == MVT::bf16)
12674 return LowerFDIV16(Op, DAG);
12675
12676 llvm_unreachable("Unexpected type for fdiv");
12677}
12678
12679SDValue SITargetLowering::LowerFFREXP(SDValue Op, SelectionDAG &DAG) const {
12680 SDLoc dl(Op);
12681 SDValue Val = Op.getOperand(0);
12682 EVT VT = Val.getValueType();
12683 EVT ResultExpVT = Op->getValueType(1);
12684 EVT InstrExpVT = VT == MVT::f16 ? MVT::i16 : MVT::i32;
12685
12686 SDValue Mant = DAG.getNode(
12688 DAG.getTargetConstant(Intrinsic::amdgcn_frexp_mant, dl, MVT::i32), Val);
12689
12690 SDValue Exp = DAG.getNode(
12691 ISD::INTRINSIC_WO_CHAIN, dl, InstrExpVT,
12692 DAG.getTargetConstant(Intrinsic::amdgcn_frexp_exp, dl, MVT::i32), Val);
12693
12694 if (Subtarget->hasFractBug()) {
12695 SDValue Fabs = DAG.getNode(ISD::FABS, dl, VT, Val);
12696 SDValue Inf =
12698
12699 SDValue IsFinite = DAG.getSetCC(dl, MVT::i1, Fabs, Inf, ISD::SETOLT);
12700 SDValue Zero = DAG.getConstant(0, dl, InstrExpVT);
12701 Exp = DAG.getNode(ISD::SELECT, dl, InstrExpVT, IsFinite, Exp, Zero);
12702 Mant = DAG.getNode(ISD::SELECT, dl, VT, IsFinite, Mant, Val);
12703 }
12704
12705 SDValue CastExp = DAG.getSExtOrTrunc(Exp, dl, ResultExpVT);
12706 return DAG.getMergeValues({Mant, CastExp}, dl);
12707}
12708
12709SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
12710 SDLoc DL(Op);
12711 StoreSDNode *Store = cast<StoreSDNode>(Op);
12712 EVT VT = Store->getMemoryVT();
12713
12714 if (VT == MVT::i1) {
12715 return DAG.getTruncStore(
12716 Store->getChain(), DL,
12717 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
12718 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
12719 }
12720
12721 assert(VT.isVector() &&
12722 Store->getValue().getValueType().getScalarType() == MVT::i32);
12723
12724 unsigned AS = Store->getAddressSpace();
12725 if (Subtarget->hasLDSMisalignedBug() && AS == AMDGPUAS::FLAT_ADDRESS &&
12726 Store->getAlign().value() < VT.getStoreSize() &&
12727 VT.getSizeInBits() > 32) {
12728 return SplitVectorStore(Op, DAG);
12729 }
12730
12731 MachineFunction &MF = DAG.getMachineFunction();
12732 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
12733 // If there is a possibility that flat instruction access scratch memory
12734 // then we need to use the same legalization rules we use for private.
12735 if (AS == AMDGPUAS::FLAT_ADDRESS &&
12736 !Subtarget->hasMultiDwordFlatScratchAddressing())
12737 AS = addressMayBeAccessedAsPrivate(Store->getMemOperand(), *MFI)
12740
12741 unsigned NumElements = VT.getVectorNumElements();
12743 if (NumElements > 4)
12744 return SplitVectorStore(Op, DAG);
12745 // v3 stores not supported on SI.
12746 if (NumElements == 3 && !Subtarget->hasDwordx3LoadStores())
12747 return SplitVectorStore(Op, DAG);
12748
12750 VT, *Store->getMemOperand()))
12751 return expandUnalignedStore(Store, DAG);
12752
12753 return SDValue();
12754 }
12755 if (AS == AMDGPUAS::PRIVATE_ADDRESS) {
12756 switch (Subtarget->getMaxPrivateElementSize()) {
12757 case 4:
12758 return scalarizeVectorStore(Store, DAG);
12759 case 8:
12760 if (NumElements > 2)
12761 return SplitVectorStore(Op, DAG);
12762 return SDValue();
12763 case 16:
12764 if (NumElements > 4 ||
12765 (NumElements == 3 && !Subtarget->enableFlatScratch()))
12766 return SplitVectorStore(Op, DAG);
12767 return SDValue();
12768 default:
12769 llvm_unreachable("unsupported private_element_size");
12770 }
12771 } else if (AS == AMDGPUAS::LOCAL_ADDRESS || AS == AMDGPUAS::REGION_ADDRESS) {
12772 unsigned Fast = 0;
12773 auto Flags = Store->getMemOperand()->getFlags();
12775 Store->getAlign(), Flags, &Fast) &&
12776 Fast > 1)
12777 return SDValue();
12778
12779 if (VT.isVector())
12780 return SplitVectorStore(Op, DAG);
12781
12782 return expandUnalignedStore(Store, DAG);
12783 }
12784
12785 // Probably an invalid store. If so we'll end up emitting a selection error.
12786 return SDValue();
12787}
12788
12789// Avoid the full correct expansion for f32 sqrt when promoting from f16.
12790SDValue SITargetLowering::lowerFSQRTF16(SDValue Op, SelectionDAG &DAG) const {
12791 SDLoc SL(Op);
12792 assert(!Subtarget->has16BitInsts());
12793 SDNodeFlags Flags = Op->getFlags();
12794 SDValue Ext =
12795 DAG.getNode(ISD::FP_EXTEND, SL, MVT::f32, Op.getOperand(0), Flags);
12796
12797 SDValue SqrtID = DAG.getTargetConstant(Intrinsic::amdgcn_sqrt, SL, MVT::i32);
12798 SDValue Sqrt =
12799 DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SL, MVT::f32, SqrtID, Ext, Flags);
12800
12801 return DAG.getNode(ISD::FP_ROUND, SL, MVT::f16, Sqrt,
12802 DAG.getTargetConstant(0, SL, MVT::i32), Flags);
12803}
12804
12805SDValue SITargetLowering::lowerFSQRTF32(SDValue Op, SelectionDAG &DAG) const {
12806 SDLoc DL(Op);
12807 SDNodeFlags Flags = Op->getFlags();
12808 MVT VT = Op.getValueType().getSimpleVT();
12809 const SDValue X = Op.getOperand(0);
12810
12811 if (allowApproxFunc(DAG, Flags)) {
12812 // Instruction is 1ulp but ignores denormals.
12813 return DAG.getNode(
12815 DAG.getTargetConstant(Intrinsic::amdgcn_sqrt, DL, MVT::i32), X, Flags);
12816 }
12817
12818 SDValue ScaleThreshold = DAG.getConstantFP(0x1.0p-96f, DL, VT);
12819 SDValue NeedScale = DAG.getSetCC(DL, MVT::i1, X, ScaleThreshold, ISD::SETOLT);
12820
12821 SDValue ScaleUpFactor = DAG.getConstantFP(0x1.0p+32f, DL, VT);
12822
12823 SDValue ScaledX = DAG.getNode(ISD::FMUL, DL, VT, X, ScaleUpFactor, Flags);
12824
12825 SDValue SqrtX =
12826 DAG.getNode(ISD::SELECT, DL, VT, NeedScale, ScaledX, X, Flags);
12827
12828 SDValue SqrtS;
12829 if (needsDenormHandlingF32(DAG, X, Flags)) {
12830 SDValue SqrtID =
12831 DAG.getTargetConstant(Intrinsic::amdgcn_sqrt, DL, MVT::i32);
12832 SqrtS = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT, SqrtID, SqrtX, Flags);
12833
12834 SDValue SqrtSAsInt = DAG.getNode(ISD::BITCAST, DL, MVT::i32, SqrtS);
12835 SDValue SqrtSNextDownInt =
12836 DAG.getNode(ISD::ADD, DL, MVT::i32, SqrtSAsInt,
12837 DAG.getAllOnesConstant(DL, MVT::i32));
12838 SDValue SqrtSNextDown = DAG.getNode(ISD::BITCAST, DL, VT, SqrtSNextDownInt);
12839
12840 SDValue NegSqrtSNextDown =
12841 DAG.getNode(ISD::FNEG, DL, VT, SqrtSNextDown, Flags);
12842
12843 SDValue SqrtVP =
12844 DAG.getNode(ISD::FMA, DL, VT, NegSqrtSNextDown, SqrtS, SqrtX, Flags);
12845
12846 SDValue SqrtSNextUpInt = DAG.getNode(ISD::ADD, DL, MVT::i32, SqrtSAsInt,
12847 DAG.getConstant(1, DL, MVT::i32));
12848 SDValue SqrtSNextUp = DAG.getNode(ISD::BITCAST, DL, VT, SqrtSNextUpInt);
12849
12850 SDValue NegSqrtSNextUp = DAG.getNode(ISD::FNEG, DL, VT, SqrtSNextUp, Flags);
12851 SDValue SqrtVS =
12852 DAG.getNode(ISD::FMA, DL, VT, NegSqrtSNextUp, SqrtS, SqrtX, Flags);
12853
12854 SDValue Zero = DAG.getConstantFP(0.0f, DL, VT);
12855 SDValue SqrtVPLE0 = DAG.getSetCC(DL, MVT::i1, SqrtVP, Zero, ISD::SETOLE);
12856
12857 SqrtS = DAG.getNode(ISD::SELECT, DL, VT, SqrtVPLE0, SqrtSNextDown, SqrtS,
12858 Flags);
12859
12860 SDValue SqrtVPVSGT0 = DAG.getSetCC(DL, MVT::i1, SqrtVS, Zero, ISD::SETOGT);
12861 SqrtS = DAG.getNode(ISD::SELECT, DL, VT, SqrtVPVSGT0, SqrtSNextUp, SqrtS,
12862 Flags);
12863 } else {
12864 SDValue SqrtR = DAG.getNode(AMDGPUISD::RSQ, DL, VT, SqrtX, Flags);
12865
12866 SqrtS = DAG.getNode(ISD::FMUL, DL, VT, SqrtX, SqrtR, Flags);
12867
12868 SDValue Half = DAG.getConstantFP(0.5f, DL, VT);
12869 SDValue SqrtH = DAG.getNode(ISD::FMUL, DL, VT, SqrtR, Half, Flags);
12870 SDValue NegSqrtH = DAG.getNode(ISD::FNEG, DL, VT, SqrtH, Flags);
12871
12872 SDValue SqrtE = DAG.getNode(ISD::FMA, DL, VT, NegSqrtH, SqrtS, Half, Flags);
12873 SqrtH = DAG.getNode(ISD::FMA, DL, VT, SqrtH, SqrtE, SqrtH, Flags);
12874 SqrtS = DAG.getNode(ISD::FMA, DL, VT, SqrtS, SqrtE, SqrtS, Flags);
12875
12876 SDValue NegSqrtS = DAG.getNode(ISD::FNEG, DL, VT, SqrtS, Flags);
12877 SDValue SqrtD =
12878 DAG.getNode(ISD::FMA, DL, VT, NegSqrtS, SqrtS, SqrtX, Flags);
12879 SqrtS = DAG.getNode(ISD::FMA, DL, VT, SqrtD, SqrtH, SqrtS, Flags);
12880 }
12881
12882 SDValue ScaleDownFactor = DAG.getConstantFP(0x1.0p-16f, DL, VT);
12883
12884 SDValue ScaledDown =
12885 DAG.getNode(ISD::FMUL, DL, VT, SqrtS, ScaleDownFactor, Flags);
12886
12887 SqrtS = DAG.getNode(ISD::SELECT, DL, VT, NeedScale, ScaledDown, SqrtS, Flags);
12888 SDValue IsZeroOrInf =
12889 DAG.getNode(ISD::IS_FPCLASS, DL, MVT::i1, SqrtX,
12890 DAG.getTargetConstant(fcZero | fcPosInf, DL, MVT::i32));
12891
12892 return DAG.getNode(ISD::SELECT, DL, VT, IsZeroOrInf, SqrtX, SqrtS, Flags);
12893}
12894
12895SDValue SITargetLowering::lowerFSQRTF64(SDValue Op, SelectionDAG &DAG) const {
12896 // For double type, the SQRT and RSQ instructions don't have required
12897 // precision, we apply Goldschmidt's algorithm to improve the result:
12898 //
12899 // y0 = rsq(x)
12900 // g0 = x * y0
12901 // h0 = 0.5 * y0
12902 //
12903 // r0 = 0.5 - h0 * g0
12904 // g1 = g0 * r0 + g0
12905 // h1 = h0 * r0 + h0
12906 //
12907 // r1 = 0.5 - h1 * g1 => d0 = x - g1 * g1
12908 // g2 = g1 * r1 + g1 g2 = d0 * h1 + g1
12909 // h2 = h1 * r1 + h1
12910 //
12911 // r2 = 0.5 - h2 * g2 => d1 = x - g2 * g2
12912 // g3 = g2 * r2 + g2 g3 = d1 * h1 + g2
12913 //
12914 // sqrt(x) = g3
12915
12916 SDNodeFlags Flags = Op->getFlags();
12917
12918 SDLoc DL(Op);
12919
12920 SDValue X = Op.getOperand(0);
12921 SDValue ScaleConstant = DAG.getConstantFP(0x1.0p-767, DL, MVT::f64);
12922
12923 SDValue Scaling = DAG.getSetCC(DL, MVT::i1, X, ScaleConstant, ISD::SETOLT);
12924
12925 SDValue ZeroInt = DAG.getConstant(0, DL, MVT::i32);
12926
12927 // Scale up input if it is too small.
12928 SDValue ScaleUpFactor = DAG.getConstant(256, DL, MVT::i32);
12929 SDValue ScaleUp =
12930 DAG.getNode(ISD::SELECT, DL, MVT::i32, Scaling, ScaleUpFactor, ZeroInt);
12931 SDValue SqrtX = DAG.getNode(ISD::FLDEXP, DL, MVT::f64, X, ScaleUp, Flags);
12932
12933 SDValue SqrtY = DAG.getNode(AMDGPUISD::RSQ, DL, MVT::f64, SqrtX);
12934
12935 SDValue SqrtS0 = DAG.getNode(ISD::FMUL, DL, MVT::f64, SqrtX, SqrtY);
12936
12937 SDValue Half = DAG.getConstantFP(0.5, DL, MVT::f64);
12938 SDValue SqrtH0 = DAG.getNode(ISD::FMUL, DL, MVT::f64, SqrtY, Half);
12939
12940 SDValue NegSqrtH0 = DAG.getNode(ISD::FNEG, DL, MVT::f64, SqrtH0);
12941 SDValue SqrtR0 = DAG.getNode(ISD::FMA, DL, MVT::f64, NegSqrtH0, SqrtS0, Half);
12942
12943 SDValue SqrtH1 = DAG.getNode(ISD::FMA, DL, MVT::f64, SqrtH0, SqrtR0, SqrtH0);
12944
12945 SDValue SqrtS1 = DAG.getNode(ISD::FMA, DL, MVT::f64, SqrtS0, SqrtR0, SqrtS0);
12946
12947 SDValue NegSqrtS1 = DAG.getNode(ISD::FNEG, DL, MVT::f64, SqrtS1);
12948 SDValue SqrtD0 =
12949 DAG.getNode(ISD::FMA, DL, MVT::f64, NegSqrtS1, SqrtS1, SqrtX);
12950
12951 SDValue SqrtS2 = DAG.getNode(ISD::FMA, DL, MVT::f64, SqrtD0, SqrtH1, SqrtS1);
12952
12953 SDValue NegSqrtS2 = DAG.getNode(ISD::FNEG, DL, MVT::f64, SqrtS2);
12954 SDValue SqrtD1 =
12955 DAG.getNode(ISD::FMA, DL, MVT::f64, NegSqrtS2, SqrtS2, SqrtX);
12956
12957 SDValue SqrtRet = DAG.getNode(ISD::FMA, DL, MVT::f64, SqrtD1, SqrtH1, SqrtS2);
12958
12959 SDValue ScaleDownFactor = DAG.getSignedConstant(-128, DL, MVT::i32);
12960 SDValue ScaleDown =
12961 DAG.getNode(ISD::SELECT, DL, MVT::i32, Scaling, ScaleDownFactor, ZeroInt);
12962 SqrtRet = DAG.getNode(ISD::FLDEXP, DL, MVT::f64, SqrtRet, ScaleDown, Flags);
12963
12964 // TODO: Switch to fcmp oeq 0 for finite only. Can't fully remove this check
12965 // with finite only or nsz because rsq(+/-0) = +/-inf
12966
12967 // TODO: Check for DAZ and expand to subnormals
12968 SDValue IsZeroOrInf =
12969 DAG.getNode(ISD::IS_FPCLASS, DL, MVT::i1, SqrtX,
12970 DAG.getTargetConstant(fcZero | fcPosInf, DL, MVT::i32));
12971
12972 // If x is +INF, +0, or -0, use its original value
12973 return DAG.getNode(ISD::SELECT, DL, MVT::f64, IsZeroOrInf, SqrtX, SqrtRet,
12974 Flags);
12975}
12976
12977SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
12978 SDLoc DL(Op);
12979 EVT VT = Op.getValueType();
12980 SDValue Arg = Op.getOperand(0);
12981 SDValue TrigVal;
12982
12983 // Propagate fast-math flags so that the multiply we introduce can be folded
12984 // if Arg is already the result of a multiply by constant.
12985 auto Flags = Op->getFlags();
12986
12987 SDValue OneOver2Pi = DAG.getConstantFP(0.5 * numbers::inv_pi, DL, VT);
12988
12989 if (Subtarget->hasTrigReducedRange()) {
12990 SDValue MulVal = DAG.getNode(ISD::FMUL, DL, VT, Arg, OneOver2Pi, Flags);
12991 TrigVal = DAG.getNode(AMDGPUISD::FRACT, DL, VT, MulVal, Flags);
12992 } else {
12993 TrigVal = DAG.getNode(ISD::FMUL, DL, VT, Arg, OneOver2Pi, Flags);
12994 }
12995
12996 switch (Op.getOpcode()) {
12997 case ISD::FCOS:
12998 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, TrigVal, Flags);
12999 case ISD::FSIN:
13000 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, TrigVal, Flags);
13001 default:
13002 llvm_unreachable("Wrong trig opcode");
13003 }
13004}
13005
13006SDValue SITargetLowering::LowerATOMIC_CMP_SWAP(SDValue Op,
13007 SelectionDAG &DAG) const {
13008 AtomicSDNode *AtomicNode = cast<AtomicSDNode>(Op);
13009 assert(AtomicNode->isCompareAndSwap());
13010 unsigned AS = AtomicNode->getAddressSpace();
13011
13012 // No custom lowering required for local address space
13014 return Op;
13015
13016 // Non-local address space requires custom lowering for atomic compare
13017 // and swap; cmp and swap should be in a v2i32 or v2i64 in case of _X2
13018 SDLoc DL(Op);
13019 SDValue ChainIn = Op.getOperand(0);
13020 SDValue Addr = Op.getOperand(1);
13021 SDValue Old = Op.getOperand(2);
13022 SDValue New = Op.getOperand(3);
13023 EVT VT = Op.getValueType();
13024 MVT SimpleVT = VT.getSimpleVT();
13025 MVT VecType = MVT::getVectorVT(SimpleVT, 2);
13026
13027 SDValue NewOld = DAG.getBuildVector(VecType, DL, {New, Old});
13028 SDValue Ops[] = {ChainIn, Addr, NewOld};
13029
13030 return DAG.getMemIntrinsicNode(AMDGPUISD::ATOMIC_CMP_SWAP, DL,
13031 Op->getVTList(), Ops, VT,
13032 AtomicNode->getMemOperand());
13033}
13034
13035//===----------------------------------------------------------------------===//
13036// Custom DAG optimizations
13037//===----------------------------------------------------------------------===//
13038
13039SDValue
13040SITargetLowering::performUCharToFloatCombine(SDNode *N,
13041 DAGCombinerInfo &DCI) const {
13042 EVT VT = N->getValueType(0);
13043 EVT ScalarVT = VT.getScalarType();
13044 if (ScalarVT != MVT::f32 && ScalarVT != MVT::f16)
13045 return SDValue();
13046
13047 SelectionDAG &DAG = DCI.DAG;
13048 SDLoc DL(N);
13049
13050 SDValue Src = N->getOperand(0);
13051 EVT SrcVT = Src.getValueType();
13052
13053 // TODO: We could try to match extracting the higher bytes, which would be
13054 // easier if i8 vectors weren't promoted to i32 vectors, particularly after
13055 // types are legalized. v4i8 -> v4f32 is probably the only case to worry
13056 // about in practice.
13057 if (DCI.isAfterLegalizeDAG() && SrcVT == MVT::i32) {
13058 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
13059 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, MVT::f32, Src);
13060 DCI.AddToWorklist(Cvt.getNode());
13061
13062 // For the f16 case, fold to a cast to f32 and then cast back to f16.
13063 if (ScalarVT != MVT::f32) {
13064 Cvt = DAG.getNode(ISD::FP_ROUND, DL, VT, Cvt,
13065 DAG.getTargetConstant(0, DL, MVT::i32));
13066 }
13067 return Cvt;
13068 }
13069 }
13070
13071 return SDValue();
13072}
13073
13074SDValue SITargetLowering::performFCopySignCombine(SDNode *N,
13075 DAGCombinerInfo &DCI) const {
13076 SDValue MagnitudeOp = N->getOperand(0);
13077 SDValue SignOp = N->getOperand(1);
13078
13079 // The generic combine for fcopysign + fp cast is too conservative with
13080 // vectors, and also gets confused by the splitting we will perform here, so
13081 // peek through FP casts.
13082 if (SignOp.getOpcode() == ISD::FP_EXTEND ||
13083 SignOp.getOpcode() == ISD::FP_ROUND)
13084 SignOp = SignOp.getOperand(0);
13085
13086 SelectionDAG &DAG = DCI.DAG;
13087 SDLoc DL(N);
13088 EVT SignVT = SignOp.getValueType();
13089
13090 // f64 fcopysign is really an f32 copysign on the high bits, so replace the
13091 // lower half with a copy.
13092 // fcopysign f64:x, _:y -> x.lo32, (fcopysign (f32 x.hi32), _:y)
13093 EVT MagVT = MagnitudeOp.getValueType();
13094
13095 unsigned NumElts = MagVT.isVector() ? MagVT.getVectorNumElements() : 1;
13096
13097 if (MagVT.getScalarType() == MVT::f64) {
13098 EVT F32VT = MagVT.isVector()
13099 ? EVT::getVectorVT(*DAG.getContext(), MVT::f32, 2 * NumElts)
13100 : MVT::v2f32;
13101
13102 SDValue MagAsVector = DAG.getNode(ISD::BITCAST, DL, F32VT, MagnitudeOp);
13103
13105 for (unsigned I = 0; I != NumElts; ++I) {
13106 SDValue MagLo =
13107 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, MagAsVector,
13108 DAG.getConstant(2 * I, DL, MVT::i32));
13109 SDValue MagHi =
13110 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, MagAsVector,
13111 DAG.getConstant(2 * I + 1, DL, MVT::i32));
13112
13113 SDValue SignOpElt =
13114 MagVT.isVector()
13116 SignOp, DAG.getConstant(I, DL, MVT::i32))
13117 : SignOp;
13118
13119 SDValue HiOp =
13120 DAG.getNode(ISD::FCOPYSIGN, DL, MVT::f32, MagHi, SignOpElt);
13121
13122 SDValue Vector =
13123 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2f32, MagLo, HiOp);
13124
13125 SDValue NewElt = DAG.getNode(ISD::BITCAST, DL, MVT::f64, Vector);
13126 NewElts.push_back(NewElt);
13127 }
13128
13129 if (NewElts.size() == 1)
13130 return NewElts[0];
13131
13132 return DAG.getNode(ISD::BUILD_VECTOR, DL, MagVT, NewElts);
13133 }
13134
13135 if (SignVT.getScalarType() != MVT::f64)
13136 return SDValue();
13137
13138 // Reduce width of sign operand, we only need the highest bit.
13139 //
13140 // fcopysign f64:x, f64:y ->
13141 // fcopysign f64:x, (extract_vector_elt (bitcast f64:y to v2f32), 1)
13142 // TODO: In some cases it might make sense to go all the way to f16.
13143
13144 EVT F32VT = MagVT.isVector()
13145 ? EVT::getVectorVT(*DAG.getContext(), MVT::f32, 2 * NumElts)
13146 : MVT::v2f32;
13147
13148 SDValue SignAsVector = DAG.getNode(ISD::BITCAST, DL, F32VT, SignOp);
13149
13150 SmallVector<SDValue, 8> F32Signs;
13151 for (unsigned I = 0; I != NumElts; ++I) {
13152 // Take sign from odd elements of cast vector
13153 SDValue SignAsF32 =
13154 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, SignAsVector,
13155 DAG.getConstant(2 * I + 1, DL, MVT::i32));
13156 F32Signs.push_back(SignAsF32);
13157 }
13158
13159 SDValue NewSign =
13160 NumElts == 1
13161 ? F32Signs.back()
13163 EVT::getVectorVT(*DAG.getContext(), MVT::f32, NumElts),
13164 F32Signs);
13165
13166 return DAG.getNode(ISD::FCOPYSIGN, DL, N->getValueType(0), N->getOperand(0),
13167 NewSign);
13168}
13169
13170// (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2)
13171// (shl (or x, c1), c2) -> add (shl x, c2), (shl c1, c2) iff x and c1 share no
13172// bits
13173
13174// This is a variant of
13175// (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2),
13176//
13177// The normal DAG combiner will do this, but only if the add has one use since
13178// that would increase the number of instructions.
13179//
13180// This prevents us from seeing a constant offset that can be folded into a
13181// memory instruction's addressing mode. If we know the resulting add offset of
13182// a pointer can be folded into an addressing offset, we can replace the pointer
13183// operand with the add of new constant offset. This eliminates one of the uses,
13184// and may allow the remaining use to also be simplified.
13185//
13186SDValue SITargetLowering::performSHLPtrCombine(SDNode *N, unsigned AddrSpace,
13187 EVT MemVT,
13188 DAGCombinerInfo &DCI) const {
13189 SDValue N0 = N->getOperand(0);
13190 SDValue N1 = N->getOperand(1);
13191
13192 // We only do this to handle cases where it's profitable when there are
13193 // multiple uses of the add, so defer to the standard combine.
13194 if ((!N0->isAnyAdd() && N0.getOpcode() != ISD::OR) || N0->hasOneUse())
13195 return SDValue();
13196
13197 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1);
13198 if (!CN1)
13199 return SDValue();
13200
13201 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1));
13202 if (!CAdd)
13203 return SDValue();
13204
13205 SelectionDAG &DAG = DCI.DAG;
13206
13207 if (N0->getOpcode() == ISD::OR &&
13208 !DAG.haveNoCommonBitsSet(N0.getOperand(0), N0.getOperand(1)))
13209 return SDValue();
13210
13211 // If the resulting offset is too large, we can't fold it into the
13212 // addressing mode offset.
13213 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue();
13214 Type *Ty = MemVT.getTypeForEVT(*DCI.DAG.getContext());
13215
13216 AddrMode AM;
13217 AM.HasBaseReg = true;
13218 AM.BaseOffs = Offset.getSExtValue();
13219 if (!isLegalAddressingMode(DCI.DAG.getDataLayout(), AM, Ty, AddrSpace))
13220 return SDValue();
13221
13222 SDLoc SL(N);
13223 EVT VT = N->getValueType(0);
13224
13225 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1);
13226 SDValue COffset = DAG.getConstant(Offset, SL, VT);
13227
13228 SDNodeFlags Flags;
13229 Flags.setNoUnsignedWrap(
13230 N->getFlags().hasNoUnsignedWrap() &&
13231 (N0.getOpcode() == ISD::OR || N0->getFlags().hasNoUnsignedWrap()));
13232
13233 // Use ISD::ADD even if the original operation was ISD::PTRADD, since we can't
13234 // be sure that the new left operand is a proper base pointer.
13235 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset, Flags);
13236}
13237
13238/// MemSDNode::getBasePtr() does not work for intrinsics, which needs to offset
13239/// by the chain and intrinsic ID. Theoretically we would also need to check the
13240/// specific intrinsic, but they all place the pointer operand first.
13241static unsigned getBasePtrIndex(const MemSDNode *N) {
13242 switch (N->getOpcode()) {
13243 case ISD::STORE:
13246 return 2;
13247 default:
13248 return 1;
13249 }
13250}
13251
13252SDValue SITargetLowering::performMemSDNodeCombine(MemSDNode *N,
13253 DAGCombinerInfo &DCI) const {
13254 SelectionDAG &DAG = DCI.DAG;
13255
13256 unsigned PtrIdx = getBasePtrIndex(N);
13257 SDValue Ptr = N->getOperand(PtrIdx);
13258
13259 // TODO: We could also do this for multiplies.
13260 if (Ptr.getOpcode() == ISD::SHL) {
13261 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), N->getAddressSpace(),
13262 N->getMemoryVT(), DCI);
13263 if (NewPtr) {
13264 SmallVector<SDValue, 8> NewOps(N->ops());
13265
13266 NewOps[PtrIdx] = NewPtr;
13267 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
13268 }
13269 }
13270
13271 return SDValue();
13272}
13273
13274static bool bitOpWithConstantIsReducible(unsigned Opc, uint32_t Val) {
13275 return (Opc == ISD::AND && (Val == 0 || Val == 0xffffffff)) ||
13276 (Opc == ISD::OR && (Val == 0xffffffff || Val == 0)) ||
13277 (Opc == ISD::XOR && Val == 0);
13278}
13279
13280// Break up 64-bit bit operation of a constant into two 32-bit and/or/xor. This
13281// will typically happen anyway for a VALU 64-bit and. This exposes other 32-bit
13282// integer combine opportunities since most 64-bit operations are decomposed
13283// this way. TODO: We won't want this for SALU especially if it is an inline
13284// immediate.
13285SDValue SITargetLowering::splitBinaryBitConstantOp(
13286 DAGCombinerInfo &DCI, const SDLoc &SL, unsigned Opc, SDValue LHS,
13287 const ConstantSDNode *CRHS) const {
13288 uint64_t Val = CRHS->getZExtValue();
13289 uint32_t ValLo = Lo_32(Val);
13290 uint32_t ValHi = Hi_32(Val);
13291 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
13292
13293 if ((bitOpWithConstantIsReducible(Opc, ValLo) ||
13295 (CRHS->hasOneUse() && !TII->isInlineConstant(CRHS->getAPIntValue()))) {
13296 // We have 64-bit scalar and/or/xor, but do not have vector forms.
13297 if (Subtarget->has64BitLiterals() && CRHS->hasOneUse() &&
13298 !CRHS->user_begin()->isDivergent())
13299 return SDValue();
13300
13301 // If we need to materialize a 64-bit immediate, it will be split up later
13302 // anyway. Avoid creating the harder to understand 64-bit immediate
13303 // materialization.
13304 return splitBinaryBitConstantOpImpl(DCI, SL, Opc, LHS, ValLo, ValHi);
13305 }
13306
13307 return SDValue();
13308}
13309
13311 if (V.getValueType() != MVT::i1)
13312 return false;
13313 switch (V.getOpcode()) {
13314 default:
13315 break;
13316 case ISD::SETCC:
13317 case ISD::IS_FPCLASS:
13318 case AMDGPUISD::FP_CLASS:
13319 return true;
13320 case ISD::AND:
13321 case ISD::OR:
13322 case ISD::XOR:
13323 return isBoolSGPR(V.getOperand(0)) && isBoolSGPR(V.getOperand(1));
13324 case ISD::SADDO:
13325 case ISD::UADDO:
13326 case ISD::SSUBO:
13327 case ISD::USUBO:
13328 case ISD::SMULO:
13329 case ISD::UMULO:
13330 return V.getResNo() == 1;
13332 unsigned IntrinsicID = V.getConstantOperandVal(0);
13333 switch (IntrinsicID) {
13334 case Intrinsic::amdgcn_is_shared:
13335 case Intrinsic::amdgcn_is_private:
13336 return true;
13337 default:
13338 return false;
13339 }
13340
13341 return false;
13342 }
13343 }
13344 return false;
13345}
13346
13347// If a constant has all zeroes or all ones within each byte return it.
13348// Otherwise return 0.
13350 // 0xff for any zero byte in the mask
13351 uint32_t ZeroByteMask = 0;
13352 if (!(C & 0x000000ff))
13353 ZeroByteMask |= 0x000000ff;
13354 if (!(C & 0x0000ff00))
13355 ZeroByteMask |= 0x0000ff00;
13356 if (!(C & 0x00ff0000))
13357 ZeroByteMask |= 0x00ff0000;
13358 if (!(C & 0xff000000))
13359 ZeroByteMask |= 0xff000000;
13360 uint32_t NonZeroByteMask = ~ZeroByteMask; // 0xff for any non-zero byte
13361 if ((NonZeroByteMask & C) != NonZeroByteMask)
13362 return 0; // Partial bytes selected.
13363 return C;
13364}
13365
13366// Check if a node selects whole bytes from its operand 0 starting at a byte
13367// boundary while masking the rest. Returns select mask as in the v_perm_b32
13368// or -1 if not succeeded.
13369// Note byte select encoding:
13370// value 0-3 selects corresponding source byte;
13371// value 0xc selects zero;
13372// value 0xff selects 0xff.
13374 assert(V.getValueSizeInBits() == 32);
13375
13376 if (V.getNumOperands() != 2)
13377 return ~0;
13378
13379 ConstantSDNode *N1 = dyn_cast<ConstantSDNode>(V.getOperand(1));
13380 if (!N1)
13381 return ~0;
13382
13383 uint32_t C = N1->getZExtValue();
13384
13385 switch (V.getOpcode()) {
13386 default:
13387 break;
13388 case ISD::AND:
13389 if (uint32_t ConstMask = getConstantPermuteMask(C))
13390 return (0x03020100 & ConstMask) | (0x0c0c0c0c & ~ConstMask);
13391 break;
13392
13393 case ISD::OR:
13394 if (uint32_t ConstMask = getConstantPermuteMask(C))
13395 return (0x03020100 & ~ConstMask) | ConstMask;
13396 break;
13397
13398 case ISD::SHL:
13399 if (C % 8)
13400 return ~0;
13401
13402 return uint32_t((0x030201000c0c0c0cull << C) >> 32);
13403
13404 case ISD::SRL:
13405 if (C % 8)
13406 return ~0;
13407
13408 return uint32_t(0x0c0c0c0c03020100ull >> C);
13409 }
13410
13411 return ~0;
13412}
13413
13414SDValue SITargetLowering::performAndCombine(SDNode *N,
13415 DAGCombinerInfo &DCI) const {
13416 if (DCI.isBeforeLegalize())
13417 return SDValue();
13418
13419 SelectionDAG &DAG = DCI.DAG;
13420 EVT VT = N->getValueType(0);
13421 SDValue LHS = N->getOperand(0);
13422 SDValue RHS = N->getOperand(1);
13423
13424 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS);
13425 if (VT == MVT::i64 && CRHS) {
13426 if (SDValue Split =
13427 splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::AND, LHS, CRHS))
13428 return Split;
13429 }
13430
13431 if (CRHS && VT == MVT::i32) {
13432 // and (srl x, c), mask => shl (bfe x, nb + c, mask >> nb), nb
13433 // nb = number of trailing zeroes in mask
13434 // It can be optimized out using SDWA for GFX8+ in the SDWA peephole pass,
13435 // given that we are selecting 8 or 16 bit fields starting at byte boundary.
13436 uint64_t Mask = CRHS->getZExtValue();
13437 unsigned Bits = llvm::popcount(Mask);
13438 if (getSubtarget()->hasSDWA() && LHS->getOpcode() == ISD::SRL &&
13439 (Bits == 8 || Bits == 16) && isShiftedMask_64(Mask) && !(Mask & 1)) {
13440 if (auto *CShift = dyn_cast<ConstantSDNode>(LHS->getOperand(1))) {
13441 unsigned Shift = CShift->getZExtValue();
13442 unsigned NB = CRHS->getAPIntValue().countr_zero();
13443 unsigned Offset = NB + Shift;
13444 if ((Offset & (Bits - 1)) == 0) { // Starts at a byte or word boundary.
13445 SDLoc SL(N);
13446 SDValue BFE =
13447 DAG.getNode(AMDGPUISD::BFE_U32, SL, MVT::i32, LHS->getOperand(0),
13448 DAG.getConstant(Offset, SL, MVT::i32),
13449 DAG.getConstant(Bits, SL, MVT::i32));
13450 EVT NarrowVT = EVT::getIntegerVT(*DAG.getContext(), Bits);
13451 SDValue Ext = DAG.getNode(ISD::AssertZext, SL, VT, BFE,
13452 DAG.getValueType(NarrowVT));
13453 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(LHS), VT, Ext,
13454 DAG.getConstant(NB, SDLoc(CRHS), MVT::i32));
13455 return Shl;
13456 }
13457 }
13458 }
13459
13460 // and (perm x, y, c1), c2 -> perm x, y, permute_mask(c1, c2)
13461 if (LHS.hasOneUse() && LHS.getOpcode() == AMDGPUISD::PERM &&
13462 isa<ConstantSDNode>(LHS.getOperand(2))) {
13463 uint32_t Sel = getConstantPermuteMask(Mask);
13464 if (!Sel)
13465 return SDValue();
13466
13467 // Select 0xc for all zero bytes
13468 Sel = (LHS.getConstantOperandVal(2) & Sel) | (~Sel & 0x0c0c0c0c);
13469 SDLoc DL(N);
13470 return DAG.getNode(AMDGPUISD::PERM, DL, MVT::i32, LHS.getOperand(0),
13471 LHS.getOperand(1), DAG.getConstant(Sel, DL, MVT::i32));
13472 }
13473 }
13474
13475 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) ->
13476 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity)
13477 if (LHS.getOpcode() == ISD::SETCC && RHS.getOpcode() == ISD::SETCC) {
13478 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
13479 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get();
13480
13481 SDValue X = LHS.getOperand(0);
13482 SDValue Y = RHS.getOperand(0);
13483 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X ||
13484 !isTypeLegal(X.getValueType()))
13485 return SDValue();
13486
13487 if (LCC == ISD::SETO) {
13488 if (X != LHS.getOperand(1))
13489 return SDValue();
13490
13491 if (RCC == ISD::SETUNE) {
13492 const ConstantFPSDNode *C1 =
13493 dyn_cast<ConstantFPSDNode>(RHS.getOperand(1));
13494 if (!C1 || !C1->isInfinity() || C1->isNegative())
13495 return SDValue();
13496
13497 const uint32_t Mask = SIInstrFlags::N_NORMAL |
13501
13502 static_assert(
13505 0x3ff) == Mask,
13506 "mask not equal");
13507
13508 SDLoc DL(N);
13509 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1, X,
13510 DAG.getConstant(Mask, DL, MVT::i32));
13511 }
13512 }
13513 }
13514
13515 if (RHS.getOpcode() == ISD::SETCC && LHS.getOpcode() == AMDGPUISD::FP_CLASS)
13516 std::swap(LHS, RHS);
13517
13518 if (LHS.getOpcode() == ISD::SETCC && RHS.getOpcode() == AMDGPUISD::FP_CLASS &&
13519 RHS.hasOneUse()) {
13520 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
13521 // and (fcmp seto), (fp_class x, mask) -> fp_class x, mask & ~(p_nan |
13522 // n_nan) and (fcmp setuo), (fp_class x, mask) -> fp_class x, mask & (p_nan
13523 // | n_nan)
13524 const ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
13525 if ((LCC == ISD::SETO || LCC == ISD::SETUO) && Mask &&
13526 (RHS.getOperand(0) == LHS.getOperand(0) &&
13527 LHS.getOperand(0) == LHS.getOperand(1))) {
13528 const unsigned OrdMask = SIInstrFlags::S_NAN | SIInstrFlags::Q_NAN;
13529 unsigned NewMask = LCC == ISD::SETO ? Mask->getZExtValue() & ~OrdMask
13530 : Mask->getZExtValue() & OrdMask;
13531
13532 SDLoc DL(N);
13533 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1, RHS.getOperand(0),
13534 DAG.getConstant(NewMask, DL, MVT::i32));
13535 }
13536 }
13537
13538 if (VT == MVT::i32 && (RHS.getOpcode() == ISD::SIGN_EXTEND ||
13539 LHS.getOpcode() == ISD::SIGN_EXTEND)) {
13540 // and x, (sext cc from i1) => select cc, x, 0
13541 if (RHS.getOpcode() != ISD::SIGN_EXTEND)
13542 std::swap(LHS, RHS);
13543 if (isBoolSGPR(RHS.getOperand(0)))
13544 return DAG.getSelect(SDLoc(N), MVT::i32, RHS.getOperand(0), LHS,
13545 DAG.getConstant(0, SDLoc(N), MVT::i32));
13546 }
13547
13548 // and (op x, c1), (op y, c2) -> perm x, y, permute_mask(c1, c2)
13549 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
13550 if (VT == MVT::i32 && LHS.hasOneUse() && RHS.hasOneUse() &&
13551 N->isDivergent() && TII->pseudoToMCOpcode(AMDGPU::V_PERM_B32_e64) != -1) {
13552 uint32_t LHSMask = getPermuteMask(LHS);
13553 uint32_t RHSMask = getPermuteMask(RHS);
13554 if (LHSMask != ~0u && RHSMask != ~0u) {
13555 // Canonicalize the expression in an attempt to have fewer unique masks
13556 // and therefore fewer registers used to hold the masks.
13557 if (LHSMask > RHSMask) {
13558 std::swap(LHSMask, RHSMask);
13559 std::swap(LHS, RHS);
13560 }
13561
13562 // Select 0xc for each lane used from source operand. Zero has 0xc mask
13563 // set, 0xff have 0xff in the mask, actual lanes are in the 0-3 range.
13564 uint32_t LHSUsedLanes = ~(LHSMask & 0x0c0c0c0c) & 0x0c0c0c0c;
13565 uint32_t RHSUsedLanes = ~(RHSMask & 0x0c0c0c0c) & 0x0c0c0c0c;
13566
13567 // Check of we need to combine values from two sources within a byte.
13568 if (!(LHSUsedLanes & RHSUsedLanes) &&
13569 // If we select high and lower word keep it for SDWA.
13570 // TODO: teach SDWA to work with v_perm_b32 and remove the check.
13571 !(LHSUsedLanes == 0x0c0c0000 && RHSUsedLanes == 0x00000c0c)) {
13572 // Each byte in each mask is either selector mask 0-3, or has higher
13573 // bits set in either of masks, which can be 0xff for 0xff or 0x0c for
13574 // zero. If 0x0c is in either mask it shall always be 0x0c. Otherwise
13575 // mask which is not 0xff wins. By anding both masks we have a correct
13576 // result except that 0x0c shall be corrected to give 0x0c only.
13577 uint32_t Mask = LHSMask & RHSMask;
13578 for (unsigned I = 0; I < 32; I += 8) {
13579 uint32_t ByteSel = 0xff << I;
13580 if ((LHSMask & ByteSel) == 0x0c || (RHSMask & ByteSel) == 0x0c)
13581 Mask &= (0x0c << I) & 0xffffffff;
13582 }
13583
13584 // Add 4 to each active LHS lane. It will not affect any existing 0xff
13585 // or 0x0c.
13586 uint32_t Sel = Mask | (LHSUsedLanes & 0x04040404);
13587 SDLoc DL(N);
13588
13589 return DAG.getNode(AMDGPUISD::PERM, DL, MVT::i32, LHS.getOperand(0),
13590 RHS.getOperand(0),
13591 DAG.getConstant(Sel, DL, MVT::i32));
13592 }
13593 }
13594 }
13595
13596 return SDValue();
13597}
13598
13599// A key component of v_perm is a mapping between byte position of the src
13600// operands, and the byte position of the dest. To provide such, we need: 1. the
13601// node that provides x byte of the dest of the OR, and 2. the byte of the node
13602// used to provide that x byte. calculateByteProvider finds which node provides
13603// a certain byte of the dest of the OR, and calculateSrcByte takes that node,
13604// and finds an ultimate src and byte position For example: The supported
13605// LoadCombine pattern for vector loads is as follows
13606// t1
13607// or
13608// / \
13609// t2 t3
13610// zext shl
13611// | | \
13612// t4 t5 16
13613// or anyext
13614// / \ |
13615// t6 t7 t8
13616// srl shl or
13617// / | / \ / \
13618// t9 t10 t11 t12 t13 t14
13619// trunc* 8 trunc* 8 and and
13620// | | / | | \
13621// t15 t16 t17 t18 t19 t20
13622// trunc* 255 srl -256
13623// | / \
13624// t15 t15 16
13625//
13626// *In this example, the truncs are from i32->i16
13627//
13628// calculateByteProvider would find t6, t7, t13, and t14 for bytes 0-3
13629// respectively. calculateSrcByte would find (given node) -> ultimate src &
13630// byteposition: t6 -> t15 & 1, t7 -> t16 & 0, t13 -> t15 & 0, t14 -> t15 & 3.
13631// After finding the mapping, we can combine the tree into vperm t15, t16,
13632// 0x05000407
13633
13634// Find the source and byte position from a node.
13635// \p DestByte is the byte position of the dest of the or that the src
13636// ultimately provides. \p SrcIndex is the byte of the src that maps to this
13637// dest of the or byte. \p Depth tracks how many recursive iterations we have
13638// performed.
13639static const std::optional<ByteProvider<SDValue>>
13640calculateSrcByte(const SDValue Op, uint64_t DestByte, uint64_t SrcIndex = 0,
13641 unsigned Depth = 0) {
13642 // We may need to recursively traverse a series of SRLs
13643 if (Depth >= 6)
13644 return std::nullopt;
13645
13646 if (Op.getValueSizeInBits() < 8)
13647 return std::nullopt;
13648
13649 if (Op.getValueType().isVector())
13650 return ByteProvider<SDValue>::getSrc(Op, DestByte, SrcIndex);
13651
13652 switch (Op->getOpcode()) {
13653 case ISD::TRUNCATE: {
13654 return calculateSrcByte(Op->getOperand(0), DestByte, SrcIndex, Depth + 1);
13655 }
13656
13657 case ISD::SIGN_EXTEND:
13658 case ISD::ZERO_EXTEND:
13660 SDValue NarrowOp = Op->getOperand(0);
13661 auto NarrowVT = NarrowOp.getValueType();
13662 if (Op->getOpcode() == ISD::SIGN_EXTEND_INREG) {
13663 auto *VTSign = cast<VTSDNode>(Op->getOperand(1));
13664 NarrowVT = VTSign->getVT();
13665 }
13666 if (!NarrowVT.isByteSized())
13667 return std::nullopt;
13668 uint64_t NarrowByteWidth = NarrowVT.getStoreSize();
13669
13670 if (SrcIndex >= NarrowByteWidth)
13671 return std::nullopt;
13672 return calculateSrcByte(Op->getOperand(0), DestByte, SrcIndex, Depth + 1);
13673 }
13674
13675 case ISD::SRA:
13676 case ISD::SRL: {
13677 auto *ShiftOp = dyn_cast<ConstantSDNode>(Op->getOperand(1));
13678 if (!ShiftOp)
13679 return std::nullopt;
13680
13681 uint64_t BitShift = ShiftOp->getZExtValue();
13682
13683 if (BitShift % 8 != 0)
13684 return std::nullopt;
13685
13686 SrcIndex += BitShift / 8;
13687
13688 return calculateSrcByte(Op->getOperand(0), DestByte, SrcIndex, Depth + 1);
13689 }
13690
13691 default: {
13692 return ByteProvider<SDValue>::getSrc(Op, DestByte, SrcIndex);
13693 }
13694 }
13695 llvm_unreachable("fully handled switch");
13696}
13697
13698// For a byte position in the result of an Or, traverse the tree and find the
13699// node (and the byte of the node) which ultimately provides this {Or,
13700// BytePosition}. \p Op is the operand we are currently examining. \p Index is
13701// the byte position of the Op that corresponds with the originally requested
13702// byte of the Or \p Depth tracks how many recursive iterations we have
13703// performed. \p StartingIndex is the originally requested byte of the Or
13704static const std::optional<ByteProvider<SDValue>>
13705calculateByteProvider(const SDValue &Op, unsigned Index, unsigned Depth,
13706 unsigned StartingIndex = 0) {
13707 // Finding Src tree of RHS of or typically requires at least 1 additional
13708 // depth
13709 if (Depth > 6)
13710 return std::nullopt;
13711
13712 unsigned BitWidth = Op.getScalarValueSizeInBits();
13713 if (BitWidth % 8 != 0)
13714 return std::nullopt;
13715 if (Index > BitWidth / 8 - 1)
13716 return std::nullopt;
13717
13718 bool IsVec = Op.getValueType().isVector();
13719 switch (Op.getOpcode()) {
13720 case ISD::OR: {
13721 if (IsVec)
13722 return std::nullopt;
13723
13724 auto RHS = calculateByteProvider(Op.getOperand(1), Index, Depth + 1,
13725 StartingIndex);
13726 if (!RHS)
13727 return std::nullopt;
13728 auto LHS = calculateByteProvider(Op.getOperand(0), Index, Depth + 1,
13729 StartingIndex);
13730 if (!LHS)
13731 return std::nullopt;
13732 // A well formed Or will have two ByteProviders for each byte, one of which
13733 // is constant zero
13734 if (!LHS->isConstantZero() && !RHS->isConstantZero())
13735 return std::nullopt;
13736 if (!LHS || LHS->isConstantZero())
13737 return RHS;
13738 if (!RHS || RHS->isConstantZero())
13739 return LHS;
13740 return std::nullopt;
13741 }
13742
13743 case ISD::AND: {
13744 if (IsVec)
13745 return std::nullopt;
13746
13747 auto *BitMaskOp = dyn_cast<ConstantSDNode>(Op->getOperand(1));
13748 if (!BitMaskOp)
13749 return std::nullopt;
13750
13751 uint32_t BitMask = BitMaskOp->getZExtValue();
13752 // Bits we expect for our StartingIndex
13753 uint32_t IndexMask = 0xFF << (Index * 8);
13754
13755 if ((IndexMask & BitMask) != IndexMask) {
13756 // If the result of the and partially provides the byte, then it
13757 // is not well formatted
13758 if (IndexMask & BitMask)
13759 return std::nullopt;
13761 }
13762
13763 return calculateSrcByte(Op->getOperand(0), StartingIndex, Index);
13764 }
13765
13766 case ISD::FSHR: {
13767 if (IsVec)
13768 return std::nullopt;
13769
13770 // fshr(X,Y,Z): (X << (BW - (Z % BW))) | (Y >> (Z % BW))
13771 auto *ShiftOp = dyn_cast<ConstantSDNode>(Op->getOperand(2));
13772 if (!ShiftOp || Op.getValueType().isVector())
13773 return std::nullopt;
13774
13775 uint64_t BitsProvided = Op.getValueSizeInBits();
13776 if (BitsProvided % 8 != 0)
13777 return std::nullopt;
13778
13779 uint64_t BitShift = ShiftOp->getAPIntValue().urem(BitsProvided);
13780 if (BitShift % 8)
13781 return std::nullopt;
13782
13783 uint64_t ConcatSizeInBytes = BitsProvided / 4;
13784 uint64_t ByteShift = BitShift / 8;
13785
13786 uint64_t NewIndex = (Index + ByteShift) % ConcatSizeInBytes;
13787 uint64_t BytesProvided = BitsProvided / 8;
13788 SDValue NextOp = Op.getOperand(NewIndex >= BytesProvided ? 0 : 1);
13789 NewIndex %= BytesProvided;
13790 return calculateByteProvider(NextOp, NewIndex, Depth + 1, StartingIndex);
13791 }
13792
13793 case ISD::SRA:
13794 case ISD::SRL: {
13795 if (IsVec)
13796 return std::nullopt;
13797
13798 auto *ShiftOp = dyn_cast<ConstantSDNode>(Op->getOperand(1));
13799 if (!ShiftOp)
13800 return std::nullopt;
13801
13802 uint64_t BitShift = ShiftOp->getZExtValue();
13803 if (BitShift % 8)
13804 return std::nullopt;
13805
13806 auto BitsProvided = Op.getScalarValueSizeInBits();
13807 if (BitsProvided % 8 != 0)
13808 return std::nullopt;
13809
13810 uint64_t BytesProvided = BitsProvided / 8;
13811 uint64_t ByteShift = BitShift / 8;
13812 // The dest of shift will have good [0 : (BytesProvided - ByteShift)] bytes.
13813 // If the byte we are trying to provide (as tracked by index) falls in this
13814 // range, then the SRL provides the byte. The byte of interest of the src of
13815 // the SRL is Index + ByteShift
13816 return BytesProvided - ByteShift > Index
13817 ? calculateSrcByte(Op->getOperand(0), StartingIndex,
13818 Index + ByteShift)
13820 }
13821
13822 case ISD::SHL: {
13823 if (IsVec)
13824 return std::nullopt;
13825
13826 auto *ShiftOp = dyn_cast<ConstantSDNode>(Op->getOperand(1));
13827 if (!ShiftOp)
13828 return std::nullopt;
13829
13830 uint64_t BitShift = ShiftOp->getZExtValue();
13831 if (BitShift % 8 != 0)
13832 return std::nullopt;
13833 uint64_t ByteShift = BitShift / 8;
13834
13835 // If we are shifting by an amount greater than (or equal to)
13836 // the index we are trying to provide, then it provides 0s. If not,
13837 // then this bytes are not definitively 0s, and the corresponding byte
13838 // of interest is Index - ByteShift of the src
13839 return Index < ByteShift
13841 : calculateByteProvider(Op.getOperand(0), Index - ByteShift,
13842 Depth + 1, StartingIndex);
13843 }
13844 case ISD::ANY_EXTEND:
13845 case ISD::SIGN_EXTEND:
13846 case ISD::ZERO_EXTEND:
13848 case ISD::AssertZext:
13849 case ISD::AssertSext: {
13850 if (IsVec)
13851 return std::nullopt;
13852
13853 SDValue NarrowOp = Op->getOperand(0);
13854 unsigned NarrowBitWidth = NarrowOp.getValueSizeInBits();
13855 if (Op->getOpcode() == ISD::SIGN_EXTEND_INREG ||
13856 Op->getOpcode() == ISD::AssertZext ||
13857 Op->getOpcode() == ISD::AssertSext) {
13858 auto *VTSign = cast<VTSDNode>(Op->getOperand(1));
13859 NarrowBitWidth = VTSign->getVT().getSizeInBits();
13860 }
13861 if (NarrowBitWidth % 8 != 0)
13862 return std::nullopt;
13863 uint64_t NarrowByteWidth = NarrowBitWidth / 8;
13864
13865 if (Index >= NarrowByteWidth)
13866 return Op.getOpcode() == ISD::ZERO_EXTEND
13867 ? std::optional<ByteProvider<SDValue>>(
13869 : std::nullopt;
13870 return calculateByteProvider(NarrowOp, Index, Depth + 1, StartingIndex);
13871 }
13872
13873 case ISD::TRUNCATE: {
13874 if (IsVec)
13875 return std::nullopt;
13876
13877 uint64_t NarrowByteWidth = BitWidth / 8;
13878
13879 if (NarrowByteWidth >= Index) {
13880 return calculateByteProvider(Op.getOperand(0), Index, Depth + 1,
13881 StartingIndex);
13882 }
13883
13884 return std::nullopt;
13885 }
13886
13887 case ISD::CopyFromReg: {
13888 if (BitWidth / 8 > Index)
13889 return calculateSrcByte(Op, StartingIndex, Index);
13890
13891 return std::nullopt;
13892 }
13893
13894 case ISD::LOAD: {
13895 auto *L = cast<LoadSDNode>(Op.getNode());
13896
13897 unsigned NarrowBitWidth = L->getMemoryVT().getSizeInBits();
13898 if (NarrowBitWidth % 8 != 0)
13899 return std::nullopt;
13900 uint64_t NarrowByteWidth = NarrowBitWidth / 8;
13901
13902 // If the width of the load does not reach byte we are trying to provide for
13903 // and it is not a ZEXTLOAD, then the load does not provide for the byte in
13904 // question
13905 if (Index >= NarrowByteWidth) {
13906 return L->getExtensionType() == ISD::ZEXTLOAD
13907 ? std::optional<ByteProvider<SDValue>>(
13909 : std::nullopt;
13910 }
13911
13912 if (NarrowByteWidth > Index) {
13913 return calculateSrcByte(Op, StartingIndex, Index);
13914 }
13915
13916 return std::nullopt;
13917 }
13918
13919 case ISD::BSWAP: {
13920 if (IsVec)
13921 return std::nullopt;
13922
13923 return calculateByteProvider(Op->getOperand(0), BitWidth / 8 - Index - 1,
13924 Depth + 1, StartingIndex);
13925 }
13926
13928 auto *IdxOp = dyn_cast<ConstantSDNode>(Op->getOperand(1));
13929 if (!IdxOp)
13930 return std::nullopt;
13931 auto VecIdx = IdxOp->getZExtValue();
13932 auto ScalarSize = Op.getScalarValueSizeInBits();
13933 if (ScalarSize < 32)
13934 Index = ScalarSize == 8 ? VecIdx : VecIdx * 2 + Index;
13935 return calculateSrcByte(ScalarSize >= 32 ? Op : Op.getOperand(0),
13936 StartingIndex, Index);
13937 }
13938
13939 case AMDGPUISD::PERM: {
13940 if (IsVec)
13941 return std::nullopt;
13942
13943 auto *PermMask = dyn_cast<ConstantSDNode>(Op->getOperand(2));
13944 if (!PermMask)
13945 return std::nullopt;
13946
13947 auto IdxMask =
13948 (PermMask->getZExtValue() & (0xFF << (Index * 8))) >> (Index * 8);
13949 if (IdxMask > 0x07 && IdxMask != 0x0c)
13950 return std::nullopt;
13951
13952 auto NextOp = Op.getOperand(IdxMask > 0x03 ? 0 : 1);
13953 auto NextIndex = IdxMask > 0x03 ? IdxMask % 4 : IdxMask;
13954
13955 return IdxMask != 0x0c ? calculateSrcByte(NextOp, StartingIndex, NextIndex)
13958 }
13959
13960 default: {
13961 return std::nullopt;
13962 }
13963 }
13964
13965 llvm_unreachable("fully handled switch");
13966}
13967
13968// Returns true if the Operand is a scalar and is 16 bits
13969static bool isExtendedFrom16Bits(SDValue &Operand) {
13970
13971 switch (Operand.getOpcode()) {
13972 case ISD::ANY_EXTEND:
13973 case ISD::SIGN_EXTEND:
13974 case ISD::ZERO_EXTEND: {
13975 auto OpVT = Operand.getOperand(0).getValueType();
13976 return !OpVT.isVector() && OpVT.getSizeInBits() == 16;
13977 }
13978 case ISD::LOAD: {
13979 LoadSDNode *L = cast<LoadSDNode>(Operand.getNode());
13980 auto ExtType = cast<LoadSDNode>(L)->getExtensionType();
13981 if (ExtType == ISD::ZEXTLOAD || ExtType == ISD::SEXTLOAD ||
13982 ExtType == ISD::EXTLOAD) {
13983 auto MemVT = L->getMemoryVT();
13984 return !MemVT.isVector() && MemVT.getSizeInBits() == 16;
13985 }
13986 return L->getMemoryVT().getSizeInBits() == 16;
13987 }
13988 default:
13989 return false;
13990 }
13991}
13992
13993// Returns true if the mask matches consecutive bytes, and the first byte
13994// begins at a power of 2 byte offset from 0th byte
13995static bool addresses16Bits(int Mask) {
13996 int Low8 = Mask & 0xff;
13997 int Hi8 = (Mask & 0xff00) >> 8;
13998
13999 assert(Low8 < 8 && Hi8 < 8);
14000 // Are the bytes contiguous in the order of increasing addresses.
14001 bool IsConsecutive = (Hi8 - Low8 == 1);
14002 // Is the first byte at location that is aligned for 16 bit instructions.
14003 // A counter example is taking 2 consecutive bytes starting at the 8th bit.
14004 // In this case, we still need code to extract the 16 bit operand, so it
14005 // is better to use i8 v_perm
14006 bool Is16Aligned = !(Low8 % 2);
14007
14008 return IsConsecutive && Is16Aligned;
14009}
14010
14011// Do not lower into v_perm if the operands are actually 16 bit
14012// and the selected bits (based on PermMask) correspond with two
14013// easily addressable 16 bit operands.
14015 SDValue &OtherOp) {
14016 int Low16 = PermMask & 0xffff;
14017 int Hi16 = (PermMask & 0xffff0000) >> 16;
14018
14019 auto TempOp = peekThroughBitcasts(Op);
14020 auto TempOtherOp = peekThroughBitcasts(OtherOp);
14021
14022 auto OpIs16Bit =
14023 TempOtherOp.getValueSizeInBits() == 16 || isExtendedFrom16Bits(TempOp);
14024 if (!OpIs16Bit)
14025 return true;
14026
14027 auto OtherOpIs16Bit = TempOtherOp.getValueSizeInBits() == 16 ||
14028 isExtendedFrom16Bits(TempOtherOp);
14029 if (!OtherOpIs16Bit)
14030 return true;
14031
14032 // Do we cleanly address both
14033 return !addresses16Bits(Low16) || !addresses16Bits(Hi16);
14034}
14035
14037 unsigned DWordOffset) {
14038 SDValue Ret;
14039
14040 auto TypeSize = Src.getValueSizeInBits().getFixedValue();
14041 // ByteProvider must be at least 8 bits
14042 assert(Src.getValueSizeInBits().isKnownMultipleOf(8));
14043
14044 if (TypeSize <= 32)
14045 return DAG.getBitcastedAnyExtOrTrunc(Src, SL, MVT::i32);
14046
14047 if (Src.getValueType().isVector()) {
14048 auto ScalarTySize = Src.getScalarValueSizeInBits();
14049 auto ScalarTy = Src.getValueType().getScalarType();
14050 if (ScalarTySize == 32) {
14051 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Src,
14052 DAG.getConstant(DWordOffset, SL, MVT::i32));
14053 }
14054 if (ScalarTySize > 32) {
14055 Ret = DAG.getNode(
14056 ISD::EXTRACT_VECTOR_ELT, SL, ScalarTy, Src,
14057 DAG.getConstant(DWordOffset / (ScalarTySize / 32), SL, MVT::i32));
14058 auto ShiftVal = 32 * (DWordOffset % (ScalarTySize / 32));
14059 if (ShiftVal)
14060 Ret = DAG.getNode(ISD::SRL, SL, Ret.getValueType(), Ret,
14061 DAG.getConstant(ShiftVal, SL, MVT::i32));
14062 return DAG.getBitcastedAnyExtOrTrunc(Ret, SL, MVT::i32);
14063 }
14064
14065 assert(ScalarTySize < 32);
14066 auto NumElements = TypeSize / ScalarTySize;
14067 auto Trunc32Elements = (ScalarTySize * NumElements) / 32;
14068 auto NormalizedTrunc = Trunc32Elements * 32 / ScalarTySize;
14069 auto NumElementsIn32 = 32 / ScalarTySize;
14070 auto NumAvailElements = DWordOffset < Trunc32Elements
14071 ? NumElementsIn32
14072 : NumElements - NormalizedTrunc;
14073
14075 DAG.ExtractVectorElements(Src, VecSrcs, DWordOffset * NumElementsIn32,
14076 NumAvailElements);
14077
14078 Ret = DAG.getBuildVector(
14079 MVT::getVectorVT(MVT::getIntegerVT(ScalarTySize), NumAvailElements), SL,
14080 VecSrcs);
14081 return Ret = DAG.getBitcastedAnyExtOrTrunc(Ret, SL, MVT::i32);
14082 }
14083
14084 /// Scalar Type
14085 auto ShiftVal = 32 * DWordOffset;
14086 Ret = DAG.getNode(ISD::SRL, SL, Src.getValueType(), Src,
14087 DAG.getConstant(ShiftVal, SL, MVT::i32));
14088 return DAG.getBitcastedAnyExtOrTrunc(Ret, SL, MVT::i32);
14089}
14090
14092 SelectionDAG &DAG = DCI.DAG;
14093 [[maybe_unused]] EVT VT = N->getValueType(0);
14095
14096 // VT is known to be MVT::i32, so we need to provide 4 bytes.
14097 assert(VT == MVT::i32);
14098 for (int i = 0; i < 4; i++) {
14099 // Find the ByteProvider that provides the ith byte of the result of OR
14100 std::optional<ByteProvider<SDValue>> P =
14101 calculateByteProvider(SDValue(N, 0), i, 0, /*StartingIndex = */ i);
14102 // TODO support constantZero
14103 if (!P || P->isConstantZero())
14104 return SDValue();
14105
14106 PermNodes.push_back(*P);
14107 }
14108 if (PermNodes.size() != 4)
14109 return SDValue();
14110
14111 std::pair<unsigned, unsigned> FirstSrc(0, PermNodes[0].SrcOffset / 4);
14112 std::optional<std::pair<unsigned, unsigned>> SecondSrc;
14113 uint64_t PermMask = 0x00000000;
14114 for (size_t i = 0; i < PermNodes.size(); i++) {
14115 auto PermOp = PermNodes[i];
14116 // Since the mask is applied to Src1:Src2, Src1 bytes must be offset
14117 // by sizeof(Src2) = 4
14118 int SrcByteAdjust = 4;
14119
14120 // If the Src uses a byte from a different DWORD, then it corresponds
14121 // with a difference source
14122 if (!PermOp.hasSameSrc(PermNodes[FirstSrc.first]) ||
14123 ((PermOp.SrcOffset / 4) != FirstSrc.second)) {
14124 if (SecondSrc)
14125 if (!PermOp.hasSameSrc(PermNodes[SecondSrc->first]) ||
14126 ((PermOp.SrcOffset / 4) != SecondSrc->second))
14127 return SDValue();
14128
14129 // Set the index of the second distinct Src node
14130 SecondSrc = {i, PermNodes[i].SrcOffset / 4};
14131 assert(!(PermNodes[SecondSrc->first].Src->getValueSizeInBits() % 8));
14132 SrcByteAdjust = 0;
14133 }
14134 assert((PermOp.SrcOffset % 4) + SrcByteAdjust < 8);
14136 PermMask |= ((PermOp.SrcOffset % 4) + SrcByteAdjust) << (i * 8);
14137 }
14138 SDLoc DL(N);
14139 SDValue Op = *PermNodes[FirstSrc.first].Src;
14140 Op = getDWordFromOffset(DAG, DL, Op, FirstSrc.second);
14141 assert(Op.getValueSizeInBits() == 32);
14142
14143 // Check that we are not just extracting the bytes in order from an op
14144 if (!SecondSrc) {
14145 int Low16 = PermMask & 0xffff;
14146 int Hi16 = (PermMask & 0xffff0000) >> 16;
14147
14148 bool WellFormedLow = (Low16 == 0x0504) || (Low16 == 0x0100);
14149 bool WellFormedHi = (Hi16 == 0x0706) || (Hi16 == 0x0302);
14150
14151 // The perm op would really just produce Op. So combine into Op
14152 if (WellFormedLow && WellFormedHi)
14153 return DAG.getBitcast(MVT::getIntegerVT(32), Op);
14154 }
14155
14156 SDValue OtherOp = SecondSrc ? *PermNodes[SecondSrc->first].Src : Op;
14157
14158 if (SecondSrc) {
14159 OtherOp = getDWordFromOffset(DAG, DL, OtherOp, SecondSrc->second);
14160 assert(OtherOp.getValueSizeInBits() == 32);
14161 }
14162
14163 // Check that we haven't just recreated the same FSHR node.
14164 if (N->getOpcode() == ISD::FSHR &&
14165 (N->getOperand(0) == Op || N->getOperand(0) == OtherOp) &&
14166 (N->getOperand(1) == Op || N->getOperand(1) == OtherOp))
14167 return SDValue();
14168
14169 if (hasNon16BitAccesses(PermMask, Op, OtherOp)) {
14170
14171 assert(Op.getValueType().isByteSized() &&
14172 OtherOp.getValueType().isByteSized());
14173
14174 // If the ultimate src is less than 32 bits, then we will only be
14175 // using bytes 0: Op.getValueSizeInBytes() - 1 in the or.
14176 // CalculateByteProvider would not have returned Op as source if we
14177 // used a byte that is outside its ValueType. Thus, we are free to
14178 // ANY_EXTEND as the extended bits are dont-cares.
14179 Op = DAG.getBitcastedAnyExtOrTrunc(Op, DL, MVT::i32);
14180 OtherOp = DAG.getBitcastedAnyExtOrTrunc(OtherOp, DL, MVT::i32);
14181
14182 return DAG.getNode(AMDGPUISD::PERM, DL, MVT::i32, Op, OtherOp,
14183 DAG.getConstant(PermMask, DL, MVT::i32));
14184 }
14185 return SDValue();
14186}
14187
14188SDValue SITargetLowering::performOrCombine(SDNode *N,
14189 DAGCombinerInfo &DCI) const {
14190 SelectionDAG &DAG = DCI.DAG;
14191 SDValue LHS = N->getOperand(0);
14192 SDValue RHS = N->getOperand(1);
14193
14194 EVT VT = N->getValueType(0);
14195 if (VT == MVT::i1) {
14196 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2)
14197 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS &&
14198 RHS.getOpcode() == AMDGPUISD::FP_CLASS) {
14199 SDValue Src = LHS.getOperand(0);
14200 if (Src != RHS.getOperand(0))
14201 return SDValue();
14202
14203 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
14204 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
14205 if (!CLHS || !CRHS)
14206 return SDValue();
14207
14208 // Only 10 bits are used.
14209 static const uint32_t MaxMask = 0x3ff;
14210
14211 uint32_t NewMask =
14212 (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask;
14213 SDLoc DL(N);
14214 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1, Src,
14215 DAG.getConstant(NewMask, DL, MVT::i32));
14216 }
14217
14218 return SDValue();
14219 }
14220
14221 // or (perm x, y, c1), c2 -> perm x, y, permute_mask(c1, c2)
14223 LHS.getOpcode() == AMDGPUISD::PERM &&
14224 isa<ConstantSDNode>(LHS.getOperand(2))) {
14225 uint32_t Sel = getConstantPermuteMask(N->getConstantOperandVal(1));
14226 if (!Sel)
14227 return SDValue();
14228
14229 Sel |= LHS.getConstantOperandVal(2);
14230 SDLoc DL(N);
14231 return DAG.getNode(AMDGPUISD::PERM, DL, MVT::i32, LHS.getOperand(0),
14232 LHS.getOperand(1), DAG.getConstant(Sel, DL, MVT::i32));
14233 }
14234
14235 // or (op x, c1), (op y, c2) -> perm x, y, permute_mask(c1, c2)
14236 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
14237 if (VT == MVT::i32 && LHS.hasOneUse() && RHS.hasOneUse() &&
14238 N->isDivergent() && TII->pseudoToMCOpcode(AMDGPU::V_PERM_B32_e64) != -1) {
14239
14240 // If all the uses of an or need to extract the individual elements, do not
14241 // attempt to lower into v_perm
14242 auto usesCombinedOperand = [](SDNode *OrUse) {
14243 // If we have any non-vectorized use, then it is a candidate for v_perm
14244 if (OrUse->getOpcode() != ISD::BITCAST ||
14245 !OrUse->getValueType(0).isVector())
14246 return true;
14247
14248 // If we have any non-vectorized use, then it is a candidate for v_perm
14249 for (auto *VUser : OrUse->users()) {
14250 if (!VUser->getValueType(0).isVector())
14251 return true;
14252
14253 // If the use of a vector is a store, then combining via a v_perm
14254 // is beneficial.
14255 // TODO -- whitelist more uses
14256 for (auto VectorwiseOp : {ISD::STORE, ISD::CopyToReg, ISD::CopyFromReg})
14257 if (VUser->getOpcode() == VectorwiseOp)
14258 return true;
14259 }
14260 return false;
14261 };
14262
14263 if (!any_of(N->users(), usesCombinedOperand))
14264 return SDValue();
14265
14266 uint32_t LHSMask = getPermuteMask(LHS);
14267 uint32_t RHSMask = getPermuteMask(RHS);
14268
14269 if (LHSMask != ~0u && RHSMask != ~0u) {
14270 // Canonicalize the expression in an attempt to have fewer unique masks
14271 // and therefore fewer registers used to hold the masks.
14272 if (LHSMask > RHSMask) {
14273 std::swap(LHSMask, RHSMask);
14274 std::swap(LHS, RHS);
14275 }
14276
14277 // Select 0xc for each lane used from source operand. Zero has 0xc mask
14278 // set, 0xff have 0xff in the mask, actual lanes are in the 0-3 range.
14279 uint32_t LHSUsedLanes = ~(LHSMask & 0x0c0c0c0c) & 0x0c0c0c0c;
14280 uint32_t RHSUsedLanes = ~(RHSMask & 0x0c0c0c0c) & 0x0c0c0c0c;
14281
14282 // Check of we need to combine values from two sources within a byte.
14283 if (!(LHSUsedLanes & RHSUsedLanes) &&
14284 // If we select high and lower word keep it for SDWA.
14285 // TODO: teach SDWA to work with v_perm_b32 and remove the check.
14286 !(LHSUsedLanes == 0x0c0c0000 && RHSUsedLanes == 0x00000c0c)) {
14287 // Kill zero bytes selected by other mask. Zero value is 0xc.
14288 LHSMask &= ~RHSUsedLanes;
14289 RHSMask &= ~LHSUsedLanes;
14290 // Add 4 to each active LHS lane
14291 LHSMask |= LHSUsedLanes & 0x04040404;
14292 // Combine masks
14293 uint32_t Sel = LHSMask | RHSMask;
14294 SDLoc DL(N);
14295
14296 return DAG.getNode(AMDGPUISD::PERM, DL, MVT::i32, LHS.getOperand(0),
14297 RHS.getOperand(0),
14298 DAG.getConstant(Sel, DL, MVT::i32));
14299 }
14300 }
14301 if (LHSMask == ~0u || RHSMask == ~0u) {
14302 if (SDValue Perm = matchPERM(N, DCI))
14303 return Perm;
14304 }
14305 }
14306
14307 // Detect identity v2i32 OR and replace with identity source node.
14308 // Specifically an Or that has operands constructed from the same source node
14309 // via extract_vector_elt and build_vector. I.E.
14310 // v2i32 or(
14311 // v2i32 build_vector(
14312 // i32 extract_elt(%IdentitySrc, 0),
14313 // i32 0
14314 // ),
14315 // v2i32 build_vector(
14316 // i32 0,
14317 // i32 extract_elt(%IdentitySrc, 1)
14318 // ) )
14319 // =>
14320 // v2i32 %IdentitySrc
14321
14322 if (VT == MVT::v2i32 && LHS->getOpcode() == ISD::BUILD_VECTOR &&
14323 RHS->getOpcode() == ISD::BUILD_VECTOR) {
14324
14325 ConstantSDNode *LC = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
14326 ConstantSDNode *RC = dyn_cast<ConstantSDNode>(RHS->getOperand(0));
14327
14328 // Test for and normalise build vectors.
14329 if (LC && RC && LC->getZExtValue() == 0 && RC->getZExtValue() == 0) {
14330
14331 // Get the extract_vector_element operands.
14332 SDValue LEVE = LHS->getOperand(0);
14333 SDValue REVE = RHS->getOperand(1);
14334
14335 if (LEVE->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
14337 // Check that different elements from the same vector are
14338 // extracted.
14339 if (LEVE->getOperand(0) == REVE->getOperand(0) &&
14340 LEVE->getOperand(1) != REVE->getOperand(1)) {
14341 SDValue IdentitySrc = LEVE.getOperand(0);
14342 return IdentitySrc;
14343 }
14344 }
14345 }
14346 }
14347
14348 if (VT != MVT::i64 || DCI.isBeforeLegalizeOps())
14349 return SDValue();
14350
14351 // TODO: This could be a generic combine with a predicate for extracting the
14352 // high half of an integer being free.
14353
14354 // (or i64:x, (zero_extend i32:y)) ->
14355 // i64 (bitcast (v2i32 build_vector (or i32:y, lo_32(x)), hi_32(x)))
14356 if (LHS.getOpcode() == ISD::ZERO_EXTEND &&
14357 RHS.getOpcode() != ISD::ZERO_EXTEND)
14358 std::swap(LHS, RHS);
14359
14360 if (RHS.getOpcode() == ISD::ZERO_EXTEND) {
14361 SDValue ExtSrc = RHS.getOperand(0);
14362 EVT SrcVT = ExtSrc.getValueType();
14363 if (SrcVT == MVT::i32) {
14364 SDLoc SL(N);
14365 auto [LowLHS, HiBits] = split64BitValue(LHS, DAG);
14366 SDValue LowOr = DAG.getNode(ISD::OR, SL, MVT::i32, LowLHS, ExtSrc);
14367
14368 DCI.AddToWorklist(LowOr.getNode());
14369 DCI.AddToWorklist(HiBits.getNode());
14370
14371 SDValue Vec =
14372 DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, LowOr, HiBits);
14373 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
14374 }
14375 }
14376
14377 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
14378 if (CRHS) {
14379 if (SDValue Split = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::OR,
14380 N->getOperand(0), CRHS))
14381 return Split;
14382 }
14383
14384 return SDValue();
14385}
14386
14387SDValue SITargetLowering::performXorCombine(SDNode *N,
14388 DAGCombinerInfo &DCI) const {
14389 if (SDValue RV = reassociateScalarOps(N, DCI.DAG))
14390 return RV;
14391
14392 SDValue LHS = N->getOperand(0);
14393 SDValue RHS = N->getOperand(1);
14394
14395 const ConstantSDNode *CRHS = isConstOrConstSplat(RHS);
14396 SelectionDAG &DAG = DCI.DAG;
14397
14398 EVT VT = N->getValueType(0);
14399 if (CRHS && VT == MVT::i64) {
14400 if (SDValue Split =
14401 splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::XOR, LHS, CRHS))
14402 return Split;
14403 }
14404
14405 // v2i32 (xor (vselect cc, x, y), K) ->
14406 // (v2i32 svelect cc, (xor x, K), (xor y, K)) This enables the xor to be
14407 // replaced with source modifiers when the select is lowered to CNDMASK.
14408 unsigned Opc = LHS.getOpcode();
14409 if (((Opc == ISD::VSELECT && VT == MVT::v2i32) ||
14410 (Opc == ISD::SELECT && VT == MVT::i64)) &&
14411 CRHS && CRHS->getAPIntValue().isSignMask()) {
14412 SDValue CC = LHS->getOperand(0);
14413 SDValue TRUE = LHS->getOperand(1);
14414 SDValue FALSE = LHS->getOperand(2);
14415 SDValue XTrue = DAG.getNode(ISD::XOR, SDLoc(N), VT, TRUE, RHS);
14416 SDValue XFalse = DAG.getNode(ISD::XOR, SDLoc(N), VT, FALSE, RHS);
14417 SDValue XSelect =
14418 DAG.getNode(ISD::VSELECT, SDLoc(N), VT, CC, XTrue, XFalse);
14419 return XSelect;
14420 }
14421
14422 // Make sure to apply the 64-bit constant splitting fold before trying to fold
14423 // fneg-like xors into 64-bit select.
14424 if (LHS.getOpcode() == ISD::SELECT && VT == MVT::i32) {
14425 // This looks like an fneg, try to fold as a source modifier.
14426 if (CRHS && CRHS->getAPIntValue().isSignMask() &&
14428 // xor (select c, a, b), 0x80000000 ->
14429 // bitcast (select c, (fneg (bitcast a)), (fneg (bitcast b)))
14430 SDLoc DL(N);
14431 SDValue CastLHS =
14432 DAG.getNode(ISD::BITCAST, DL, MVT::f32, LHS->getOperand(1));
14433 SDValue CastRHS =
14434 DAG.getNode(ISD::BITCAST, DL, MVT::f32, LHS->getOperand(2));
14435 SDValue FNegLHS = DAG.getNode(ISD::FNEG, DL, MVT::f32, CastLHS);
14436 SDValue FNegRHS = DAG.getNode(ISD::FNEG, DL, MVT::f32, CastRHS);
14437 SDValue NewSelect = DAG.getNode(ISD::SELECT, DL, MVT::f32,
14438 LHS->getOperand(0), FNegLHS, FNegRHS);
14439 return DAG.getNode(ISD::BITCAST, DL, VT, NewSelect);
14440 }
14441 }
14442
14443 return SDValue();
14444}
14445
14446SDValue SITargetLowering::performZeroExtendCombine(SDNode *N,
14447 DAGCombinerInfo &DCI) const {
14448 if (!Subtarget->has16BitInsts() ||
14449 DCI.getDAGCombineLevel() < AfterLegalizeDAG)
14450 return SDValue();
14451
14452 EVT VT = N->getValueType(0);
14453 if (VT != MVT::i32)
14454 return SDValue();
14455
14456 SDValue Src = N->getOperand(0);
14457 if (Src.getValueType() != MVT::i16)
14458 return SDValue();
14459
14460 return SDValue();
14461}
14462
14463SDValue
14464SITargetLowering::performSignExtendInRegCombine(SDNode *N,
14465 DAGCombinerInfo &DCI) const {
14466 SDValue Src = N->getOperand(0);
14467 auto *VTSign = cast<VTSDNode>(N->getOperand(1));
14468
14469 // Combine s_buffer_load_u8 or s_buffer_load_u16 with sext and replace them
14470 // with s_buffer_load_i8 and s_buffer_load_i16 respectively.
14471 if (((Src.getOpcode() == AMDGPUISD::SBUFFER_LOAD_UBYTE &&
14472 VTSign->getVT() == MVT::i8) ||
14473 (Src.getOpcode() == AMDGPUISD::SBUFFER_LOAD_USHORT &&
14474 VTSign->getVT() == MVT::i16))) {
14475 assert(Subtarget->hasScalarSubwordLoads() &&
14476 "s_buffer_load_{u8, i8} are supported "
14477 "in GFX12 (or newer) architectures.");
14478 EVT VT = Src.getValueType();
14479 unsigned Opc = (Src.getOpcode() == AMDGPUISD::SBUFFER_LOAD_UBYTE)
14480 ? AMDGPUISD::SBUFFER_LOAD_BYTE
14481 : AMDGPUISD::SBUFFER_LOAD_SHORT;
14482 SDLoc DL(N);
14483 SDVTList ResList = DCI.DAG.getVTList(MVT::i32);
14484 SDValue Ops[] = {
14485 Src.getOperand(0), // source register
14486 Src.getOperand(1), // offset
14487 Src.getOperand(2) // cachePolicy
14488 };
14489 auto *M = cast<MemSDNode>(Src);
14490 SDValue BufferLoad = DCI.DAG.getMemIntrinsicNode(
14491 Opc, DL, ResList, Ops, M->getMemoryVT(), M->getMemOperand());
14492 SDValue LoadVal = DCI.DAG.getNode(ISD::TRUNCATE, DL, VT, BufferLoad);
14493 return LoadVal;
14494 }
14495 if (((Src.getOpcode() == AMDGPUISD::BUFFER_LOAD_UBYTE &&
14496 VTSign->getVT() == MVT::i8) ||
14497 (Src.getOpcode() == AMDGPUISD::BUFFER_LOAD_USHORT &&
14498 VTSign->getVT() == MVT::i16)) &&
14499 Src.hasOneUse()) {
14500 auto *M = cast<MemSDNode>(Src);
14501 SDValue Ops[] = {Src.getOperand(0), // Chain
14502 Src.getOperand(1), // rsrc
14503 Src.getOperand(2), // vindex
14504 Src.getOperand(3), // voffset
14505 Src.getOperand(4), // soffset
14506 Src.getOperand(5), // offset
14507 Src.getOperand(6), Src.getOperand(7)};
14508 // replace with BUFFER_LOAD_BYTE/SHORT
14509 SDVTList ResList =
14510 DCI.DAG.getVTList(MVT::i32, Src.getOperand(0).getValueType());
14511 unsigned Opc = (Src.getOpcode() == AMDGPUISD::BUFFER_LOAD_UBYTE)
14512 ? AMDGPUISD::BUFFER_LOAD_BYTE
14513 : AMDGPUISD::BUFFER_LOAD_SHORT;
14514 SDValue BufferLoadSignExt = DCI.DAG.getMemIntrinsicNode(
14515 Opc, SDLoc(N), ResList, Ops, M->getMemoryVT(), M->getMemOperand());
14516 return DCI.DAG.getMergeValues(
14517 {BufferLoadSignExt, BufferLoadSignExt.getValue(1)}, SDLoc(N));
14518 }
14519 return SDValue();
14520}
14521
14522SDValue SITargetLowering::performClassCombine(SDNode *N,
14523 DAGCombinerInfo &DCI) const {
14524 SelectionDAG &DAG = DCI.DAG;
14525 SDValue Mask = N->getOperand(1);
14526
14527 // fp_class x, 0 -> false
14528 if (isNullConstant(Mask))
14529 return DAG.getConstant(0, SDLoc(N), MVT::i1);
14530
14531 if (N->getOperand(0).isUndef())
14532 return DAG.getUNDEF(MVT::i1);
14533
14534 return SDValue();
14535}
14536
14537SDValue SITargetLowering::performRcpCombine(SDNode *N,
14538 DAGCombinerInfo &DCI) const {
14539 EVT VT = N->getValueType(0);
14540 SDValue N0 = N->getOperand(0);
14541
14542 if (N0.isUndef()) {
14543 return DCI.DAG.getConstantFP(APFloat::getQNaN(VT.getFltSemantics()),
14544 SDLoc(N), VT);
14545 }
14546
14547 if (VT == MVT::f32 && (N0.getOpcode() == ISD::UINT_TO_FP ||
14548 N0.getOpcode() == ISD::SINT_TO_FP)) {
14549 return DCI.DAG.getNode(AMDGPUISD::RCP_IFLAG, SDLoc(N), VT, N0,
14550 N->getFlags());
14551 }
14552
14553 // TODO: Could handle f32 + amdgcn.sqrt but probably never reaches here.
14554 if ((VT == MVT::f16 && N0.getOpcode() == ISD::FSQRT) &&
14555 N->getFlags().hasAllowContract() && N0->getFlags().hasAllowContract()) {
14556 return DCI.DAG.getNode(AMDGPUISD::RSQ, SDLoc(N), VT, N0.getOperand(0),
14557 N->getFlags());
14558 }
14559
14561}
14562
14564 unsigned MaxDepth) const {
14565 unsigned Opcode = Op.getOpcode();
14566 if (Opcode == ISD::FCANONICALIZE)
14567 return true;
14568
14569 if (auto *CFP = dyn_cast<ConstantFPSDNode>(Op)) {
14570 const auto &F = CFP->getValueAPF();
14571 if (F.isNaN() && F.isSignaling())
14572 return false;
14573 if (!F.isDenormal())
14574 return true;
14575
14576 DenormalMode Mode =
14577 DAG.getMachineFunction().getDenormalMode(F.getSemantics());
14578 return Mode == DenormalMode::getIEEE();
14579 }
14580
14581 // If source is a result of another standard FP operation it is already in
14582 // canonical form.
14583 if (MaxDepth == 0)
14584 return false;
14585
14586 switch (Opcode) {
14587 // These will flush denorms if required.
14588 case ISD::FADD:
14589 case ISD::FSUB:
14590 case ISD::FMUL:
14591 case ISD::FCEIL:
14592 case ISD::FFLOOR:
14593 case ISD::FMA:
14594 case ISD::FMAD:
14595 case ISD::FSQRT:
14596 case ISD::FDIV:
14597 case ISD::FREM:
14598 case ISD::FP_ROUND:
14599 case ISD::FP_EXTEND:
14600 case ISD::FP16_TO_FP:
14601 case ISD::FP_TO_FP16:
14602 case ISD::BF16_TO_FP:
14603 case ISD::FP_TO_BF16:
14604 case ISD::FLDEXP:
14605 case AMDGPUISD::FMUL_LEGACY:
14606 case AMDGPUISD::FMAD_FTZ:
14607 case AMDGPUISD::RCP:
14608 case AMDGPUISD::RSQ:
14609 case AMDGPUISD::RSQ_CLAMP:
14610 case AMDGPUISD::RCP_LEGACY:
14611 case AMDGPUISD::RCP_IFLAG:
14612 case AMDGPUISD::LOG:
14613 case AMDGPUISD::EXP:
14614 case AMDGPUISD::DIV_SCALE:
14615 case AMDGPUISD::DIV_FMAS:
14616 case AMDGPUISD::DIV_FIXUP:
14617 case AMDGPUISD::FRACT:
14618 case AMDGPUISD::CVT_PKRTZ_F16_F32:
14619 case AMDGPUISD::CVT_F32_UBYTE0:
14620 case AMDGPUISD::CVT_F32_UBYTE1:
14621 case AMDGPUISD::CVT_F32_UBYTE2:
14622 case AMDGPUISD::CVT_F32_UBYTE3:
14623 case AMDGPUISD::FP_TO_FP16:
14624 case AMDGPUISD::SIN_HW:
14625 case AMDGPUISD::COS_HW:
14626 return true;
14627
14628 // It can/will be lowered or combined as a bit operation.
14629 // Need to check their input recursively to handle.
14630 case ISD::FNEG:
14631 case ISD::FABS:
14632 case ISD::FCOPYSIGN:
14633 return isCanonicalized(DAG, Op.getOperand(0), MaxDepth - 1);
14634
14635 case ISD::AND:
14636 if (Op.getValueType() == MVT::i32) {
14637 // Be careful as we only know it is a bitcast floating point type. It
14638 // could be f32, v2f16, we have no way of knowing. Luckily the constant
14639 // value that we optimize for, which comes up in fp32 to bf16 conversions,
14640 // is valid to optimize for all types.
14641 if (auto *RHS = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
14642 if (RHS->getZExtValue() == 0xffff0000) {
14643 return isCanonicalized(DAG, Op.getOperand(0), MaxDepth - 1);
14644 }
14645 }
14646 }
14647 break;
14648
14649 case ISD::FSIN:
14650 case ISD::FCOS:
14651 case ISD::FSINCOS:
14652 return Op.getValueType().getScalarType() != MVT::f16;
14653
14654 case ISD::FMINNUM:
14655 case ISD::FMAXNUM:
14656 case ISD::FMINNUM_IEEE:
14657 case ISD::FMAXNUM_IEEE:
14658 case ISD::FMINIMUM:
14659 case ISD::FMAXIMUM:
14660 case ISD::FMINIMUMNUM:
14661 case ISD::FMAXIMUMNUM:
14662 case AMDGPUISD::CLAMP:
14663 case AMDGPUISD::FMED3:
14664 case AMDGPUISD::FMAX3:
14665 case AMDGPUISD::FMIN3:
14666 case AMDGPUISD::FMAXIMUM3:
14667 case AMDGPUISD::FMINIMUM3: {
14668 // FIXME: Shouldn't treat the generic operations different based these.
14669 // However, we aren't really required to flush the result from
14670 // minnum/maxnum..
14671
14672 // snans will be quieted, so we only need to worry about denormals.
14673 if (Subtarget->supportsMinMaxDenormModes() ||
14674 // FIXME: denormalsEnabledForType is broken for dynamic
14675 denormalsEnabledForType(DAG, Op.getValueType()))
14676 return true;
14677
14678 // Flushing may be required.
14679 // In pre-GFX9 targets V_MIN_F32 and others do not flush denorms. For such
14680 // targets need to check their input recursively.
14681
14682 // FIXME: Does this apply with clamp? It's implemented with max.
14683 for (unsigned I = 0, E = Op.getNumOperands(); I != E; ++I) {
14684 if (!isCanonicalized(DAG, Op.getOperand(I), MaxDepth - 1))
14685 return false;
14686 }
14687
14688 return true;
14689 }
14690 case ISD::SELECT: {
14691 return isCanonicalized(DAG, Op.getOperand(1), MaxDepth - 1) &&
14692 isCanonicalized(DAG, Op.getOperand(2), MaxDepth - 1);
14693 }
14694 case ISD::BUILD_VECTOR: {
14695 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
14696 SDValue SrcOp = Op.getOperand(i);
14697 if (!isCanonicalized(DAG, SrcOp, MaxDepth - 1))
14698 return false;
14699 }
14700
14701 return true;
14702 }
14705 return isCanonicalized(DAG, Op.getOperand(0), MaxDepth - 1);
14706 }
14708 return isCanonicalized(DAG, Op.getOperand(0), MaxDepth - 1) &&
14709 isCanonicalized(DAG, Op.getOperand(1), MaxDepth - 1);
14710 }
14711 case ISD::UNDEF:
14712 // Could be anything.
14713 return false;
14714
14715 case ISD::BITCAST:
14716 // TODO: This is incorrect as it loses track of the operand's type. We may
14717 // end up effectively bitcasting from f32 to v2f16 or vice versa, and the
14718 // same bits that are canonicalized in one type need not be in the other.
14719 return isCanonicalized(DAG, Op.getOperand(0), MaxDepth - 1);
14720 case ISD::TRUNCATE: {
14721 // Hack round the mess we make when legalizing extract_vector_elt
14722 if (Op.getValueType() == MVT::i16) {
14723 SDValue TruncSrc = Op.getOperand(0);
14724 if (TruncSrc.getValueType() == MVT::i32 &&
14725 TruncSrc.getOpcode() == ISD::BITCAST &&
14726 TruncSrc.getOperand(0).getValueType() == MVT::v2f16) {
14727 return isCanonicalized(DAG, TruncSrc.getOperand(0), MaxDepth - 1);
14728 }
14729 }
14730 return false;
14731 }
14733 unsigned IntrinsicID = Op.getConstantOperandVal(0);
14734 // TODO: Handle more intrinsics
14735 switch (IntrinsicID) {
14736 case Intrinsic::amdgcn_cvt_pkrtz:
14737 case Intrinsic::amdgcn_cubeid:
14738 case Intrinsic::amdgcn_frexp_mant:
14739 case Intrinsic::amdgcn_fdot2:
14740 case Intrinsic::amdgcn_rcp:
14741 case Intrinsic::amdgcn_rsq:
14742 case Intrinsic::amdgcn_rsq_clamp:
14743 case Intrinsic::amdgcn_rcp_legacy:
14744 case Intrinsic::amdgcn_rsq_legacy:
14745 case Intrinsic::amdgcn_trig_preop:
14746 case Intrinsic::amdgcn_tanh:
14747 case Intrinsic::amdgcn_log:
14748 case Intrinsic::amdgcn_exp2:
14749 case Intrinsic::amdgcn_sqrt:
14750 return true;
14751 default:
14752 break;
14753 }
14754
14755 break;
14756 }
14757 default:
14758 break;
14759 }
14760
14761 // FIXME: denormalsEnabledForType is broken for dynamic
14762 return denormalsEnabledForType(DAG, Op.getValueType()) &&
14763 DAG.isKnownNeverSNaN(Op);
14764}
14765
14767 unsigned MaxDepth) const {
14768 const MachineRegisterInfo &MRI = MF.getRegInfo();
14769 MachineInstr *MI = MRI.getVRegDef(Reg);
14770 unsigned Opcode = MI->getOpcode();
14771
14772 if (Opcode == AMDGPU::G_FCANONICALIZE)
14773 return true;
14774
14775 std::optional<FPValueAndVReg> FCR;
14776 // Constant splat (can be padded with undef) or scalar constant.
14778 if (FCR->Value.isSignaling())
14779 return false;
14780 if (!FCR->Value.isDenormal())
14781 return true;
14782
14783 DenormalMode Mode = MF.getDenormalMode(FCR->Value.getSemantics());
14784 return Mode == DenormalMode::getIEEE();
14785 }
14786
14787 if (MaxDepth == 0)
14788 return false;
14789
14790 switch (Opcode) {
14791 case AMDGPU::G_FADD:
14792 case AMDGPU::G_FSUB:
14793 case AMDGPU::G_FMUL:
14794 case AMDGPU::G_FCEIL:
14795 case AMDGPU::G_FFLOOR:
14796 case AMDGPU::G_FRINT:
14797 case AMDGPU::G_FNEARBYINT:
14798 case AMDGPU::G_INTRINSIC_FPTRUNC_ROUND:
14799 case AMDGPU::G_INTRINSIC_TRUNC:
14800 case AMDGPU::G_INTRINSIC_ROUNDEVEN:
14801 case AMDGPU::G_FMA:
14802 case AMDGPU::G_FMAD:
14803 case AMDGPU::G_FSQRT:
14804 case AMDGPU::G_FDIV:
14805 case AMDGPU::G_FREM:
14806 case AMDGPU::G_FPOW:
14807 case AMDGPU::G_FPEXT:
14808 case AMDGPU::G_FLOG:
14809 case AMDGPU::G_FLOG2:
14810 case AMDGPU::G_FLOG10:
14811 case AMDGPU::G_FPTRUNC:
14812 case AMDGPU::G_AMDGPU_RCP_IFLAG:
14813 case AMDGPU::G_AMDGPU_CVT_F32_UBYTE0:
14814 case AMDGPU::G_AMDGPU_CVT_F32_UBYTE1:
14815 case AMDGPU::G_AMDGPU_CVT_F32_UBYTE2:
14816 case AMDGPU::G_AMDGPU_CVT_F32_UBYTE3:
14817 return true;
14818 case AMDGPU::G_FNEG:
14819 case AMDGPU::G_FABS:
14820 case AMDGPU::G_FCOPYSIGN:
14821 return isCanonicalized(MI->getOperand(1).getReg(), MF, MaxDepth - 1);
14822 case AMDGPU::G_FMINNUM:
14823 case AMDGPU::G_FMAXNUM:
14824 case AMDGPU::G_FMINNUM_IEEE:
14825 case AMDGPU::G_FMAXNUM_IEEE:
14826 case AMDGPU::G_FMINIMUM:
14827 case AMDGPU::G_FMAXIMUM:
14828 case AMDGPU::G_FMINIMUMNUM:
14829 case AMDGPU::G_FMAXIMUMNUM: {
14830 if (Subtarget->supportsMinMaxDenormModes() ||
14831 // FIXME: denormalsEnabledForType is broken for dynamic
14832 denormalsEnabledForType(MRI.getType(Reg), MF))
14833 return true;
14834
14835 [[fallthrough]];
14836 }
14837 case AMDGPU::G_BUILD_VECTOR:
14838 for (const MachineOperand &MO : llvm::drop_begin(MI->operands()))
14839 if (!isCanonicalized(MO.getReg(), MF, MaxDepth - 1))
14840 return false;
14841 return true;
14842 case AMDGPU::G_INTRINSIC:
14843 case AMDGPU::G_INTRINSIC_CONVERGENT:
14844 switch (cast<GIntrinsic>(MI)->getIntrinsicID()) {
14845 case Intrinsic::amdgcn_fmul_legacy:
14846 case Intrinsic::amdgcn_fmad_ftz:
14847 case Intrinsic::amdgcn_sqrt:
14848 case Intrinsic::amdgcn_fmed3:
14849 case Intrinsic::amdgcn_sin:
14850 case Intrinsic::amdgcn_cos:
14851 case Intrinsic::amdgcn_log:
14852 case Intrinsic::amdgcn_exp2:
14853 case Intrinsic::amdgcn_log_clamp:
14854 case Intrinsic::amdgcn_rcp:
14855 case Intrinsic::amdgcn_rcp_legacy:
14856 case Intrinsic::amdgcn_rsq:
14857 case Intrinsic::amdgcn_rsq_clamp:
14858 case Intrinsic::amdgcn_rsq_legacy:
14859 case Intrinsic::amdgcn_div_scale:
14860 case Intrinsic::amdgcn_div_fmas:
14861 case Intrinsic::amdgcn_div_fixup:
14862 case Intrinsic::amdgcn_fract:
14863 case Intrinsic::amdgcn_cvt_pkrtz:
14864 case Intrinsic::amdgcn_cubeid:
14865 case Intrinsic::amdgcn_cubema:
14866 case Intrinsic::amdgcn_cubesc:
14867 case Intrinsic::amdgcn_cubetc:
14868 case Intrinsic::amdgcn_frexp_mant:
14869 case Intrinsic::amdgcn_fdot2:
14870 case Intrinsic::amdgcn_trig_preop:
14871 case Intrinsic::amdgcn_tanh:
14872 return true;
14873 default:
14874 break;
14875 }
14876
14877 [[fallthrough]];
14878 default:
14879 return false;
14880 }
14881
14882 llvm_unreachable("invalid operation");
14883}
14884
14885// Constant fold canonicalize.
14886SDValue SITargetLowering::getCanonicalConstantFP(SelectionDAG &DAG,
14887 const SDLoc &SL, EVT VT,
14888 const APFloat &C) const {
14889 // Flush denormals to 0 if not enabled.
14890 if (C.isDenormal()) {
14891 DenormalMode Mode =
14892 DAG.getMachineFunction().getDenormalMode(C.getSemantics());
14893 if (Mode == DenormalMode::getPreserveSign()) {
14894 return DAG.getConstantFP(
14895 APFloat::getZero(C.getSemantics(), C.isNegative()), SL, VT);
14896 }
14897
14898 if (Mode != DenormalMode::getIEEE())
14899 return SDValue();
14900 }
14901
14902 if (C.isNaN()) {
14903 APFloat CanonicalQNaN = APFloat::getQNaN(C.getSemantics());
14904 if (C.isSignaling()) {
14905 // Quiet a signaling NaN.
14906 // FIXME: Is this supposed to preserve payload bits?
14907 return DAG.getConstantFP(CanonicalQNaN, SL, VT);
14908 }
14909
14910 // Make sure it is the canonical NaN bitpattern.
14911 //
14912 // TODO: Can we use -1 as the canonical NaN value since it's an inline
14913 // immediate?
14914 if (C.bitcastToAPInt() != CanonicalQNaN.bitcastToAPInt())
14915 return DAG.getConstantFP(CanonicalQNaN, SL, VT);
14916 }
14917
14918 // Already canonical.
14919 return DAG.getConstantFP(C, SL, VT);
14920}
14921
14923 return Op.isUndef() || isa<ConstantFPSDNode>(Op);
14924}
14925
14926SDValue
14927SITargetLowering::performFCanonicalizeCombine(SDNode *N,
14928 DAGCombinerInfo &DCI) const {
14929 SelectionDAG &DAG = DCI.DAG;
14930 SDValue N0 = N->getOperand(0);
14931 EVT VT = N->getValueType(0);
14932
14933 // fcanonicalize undef -> qnan
14934 if (N0.isUndef()) {
14936 return DAG.getConstantFP(QNaN, SDLoc(N), VT);
14937 }
14938
14939 if (ConstantFPSDNode *CFP = isConstOrConstSplatFP(N0)) {
14940 EVT VT = N->getValueType(0);
14941 return getCanonicalConstantFP(DAG, SDLoc(N), VT, CFP->getValueAPF());
14942 }
14943
14944 // fcanonicalize (build_vector x, k) -> build_vector (fcanonicalize x),
14945 // (fcanonicalize k)
14946 //
14947 // fcanonicalize (build_vector x, undef) -> build_vector (fcanonicalize x), 0
14948
14949 // TODO: This could be better with wider vectors that will be split to v2f16,
14950 // and to consider uses since there aren't that many packed operations.
14951 if (N0.getOpcode() == ISD::BUILD_VECTOR && VT == MVT::v2f16 &&
14952 isTypeLegal(MVT::v2f16)) {
14953 SDLoc SL(N);
14954 SDValue NewElts[2];
14955 SDValue Lo = N0.getOperand(0);
14956 SDValue Hi = N0.getOperand(1);
14957 EVT EltVT = Lo.getValueType();
14958
14960 for (unsigned I = 0; I != 2; ++I) {
14961 SDValue Op = N0.getOperand(I);
14962 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op)) {
14963 NewElts[I] =
14964 getCanonicalConstantFP(DAG, SL, EltVT, CFP->getValueAPF());
14965 } else if (Op.isUndef()) {
14966 // Handled below based on what the other operand is.
14967 NewElts[I] = Op;
14968 } else {
14969 NewElts[I] = DAG.getNode(ISD::FCANONICALIZE, SL, EltVT, Op);
14970 }
14971 }
14972
14973 // If one half is undef, and one is constant, prefer a splat vector rather
14974 // than the normal qNaN. If it's a register, prefer 0.0 since that's
14975 // cheaper to use and may be free with a packed operation.
14976 if (NewElts[0].isUndef()) {
14977 if (isa<ConstantFPSDNode>(NewElts[1]))
14978 NewElts[0] = isa<ConstantFPSDNode>(NewElts[1])
14979 ? NewElts[1]
14980 : DAG.getConstantFP(0.0f, SL, EltVT);
14981 }
14982
14983 if (NewElts[1].isUndef()) {
14984 NewElts[1] = isa<ConstantFPSDNode>(NewElts[0])
14985 ? NewElts[0]
14986 : DAG.getConstantFP(0.0f, SL, EltVT);
14987 }
14988
14989 return DAG.getBuildVector(VT, SL, NewElts);
14990 }
14991 }
14992
14993 return SDValue();
14994}
14995
14996static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) {
14997 switch (Opc) {
14998 case ISD::FMAXNUM:
14999 case ISD::FMAXNUM_IEEE:
15000 case ISD::FMAXIMUMNUM:
15001 return AMDGPUISD::FMAX3;
15002 case ISD::FMAXIMUM:
15003 return AMDGPUISD::FMAXIMUM3;
15004 case ISD::SMAX:
15005 return AMDGPUISD::SMAX3;
15006 case ISD::UMAX:
15007 return AMDGPUISD::UMAX3;
15008 case ISD::FMINNUM:
15009 case ISD::FMINNUM_IEEE:
15010 case ISD::FMINIMUMNUM:
15011 return AMDGPUISD::FMIN3;
15012 case ISD::FMINIMUM:
15013 return AMDGPUISD::FMINIMUM3;
15014 case ISD::SMIN:
15015 return AMDGPUISD::SMIN3;
15016 case ISD::UMIN:
15017 return AMDGPUISD::UMIN3;
15018 default:
15019 llvm_unreachable("Not a min/max opcode");
15020 }
15021}
15022
15023SDValue SITargetLowering::performIntMed3ImmCombine(SelectionDAG &DAG,
15024 const SDLoc &SL, SDValue Src,
15025 SDValue MinVal,
15026 SDValue MaxVal,
15027 bool Signed) const {
15028
15029 // med3 comes from
15030 // min(max(x, K0), K1), K0 < K1
15031 // max(min(x, K0), K1), K1 < K0
15032 //
15033 // "MinVal" and "MaxVal" respectively refer to the rhs of the
15034 // min/max op.
15035 ConstantSDNode *MinK = dyn_cast<ConstantSDNode>(MinVal);
15036 ConstantSDNode *MaxK = dyn_cast<ConstantSDNode>(MaxVal);
15037
15038 if (!MinK || !MaxK)
15039 return SDValue();
15040
15041 if (Signed) {
15042 if (MaxK->getAPIntValue().sge(MinK->getAPIntValue()))
15043 return SDValue();
15044 } else {
15045 if (MaxK->getAPIntValue().uge(MinK->getAPIntValue()))
15046 return SDValue();
15047 }
15048
15049 EVT VT = MinK->getValueType(0);
15050 unsigned Med3Opc = Signed ? AMDGPUISD::SMED3 : AMDGPUISD::UMED3;
15051 if (VT == MVT::i32 || (VT == MVT::i16 && Subtarget->hasMed3_16()))
15052 return DAG.getNode(Med3Opc, SL, VT, Src, MaxVal, MinVal);
15053
15054 // Note: we could also extend to i32 and use i32 med3 if i16 med3 is
15055 // not available, but this is unlikely to be profitable as constants
15056 // will often need to be materialized & extended, especially on
15057 // pre-GFX10 where VOP3 instructions couldn't take literal operands.
15058 return SDValue();
15059}
15060
15063 return C;
15064
15066 if (ConstantFPSDNode *C = BV->getConstantFPSplatNode())
15067 return C;
15068 }
15069
15070 return nullptr;
15071}
15072
15073SDValue SITargetLowering::performFPMed3ImmCombine(SelectionDAG &DAG,
15074 const SDLoc &SL, SDValue Op0,
15075 SDValue Op1) const {
15076 ConstantFPSDNode *K1 = getSplatConstantFP(Op1);
15077 if (!K1)
15078 return SDValue();
15079
15080 ConstantFPSDNode *K0 = getSplatConstantFP(Op0.getOperand(1));
15081 if (!K0)
15082 return SDValue();
15083
15084 // Ordered >= (although NaN inputs should have folded away by now).
15085 if (K0->getValueAPF() > K1->getValueAPF())
15086 return SDValue();
15087
15088 // med3 with a nan input acts like
15089 // v_min_f32(v_min_f32(S0.f32, S1.f32), S2.f32)
15090 //
15091 // So the result depends on whether the IEEE mode bit is enabled or not with a
15092 // signaling nan input.
15093 // ieee=1
15094 // s0 snan: yields s2
15095 // s1 snan: yields s2
15096 // s2 snan: qnan
15097
15098 // s0 qnan: min(s1, s2)
15099 // s1 qnan: min(s0, s2)
15100 // s2 qnan: min(s0, s1)
15101
15102 // ieee=0
15103 // s0 snan: min(s1, s2)
15104 // s1 snan: min(s0, s2)
15105 // s2 snan: qnan
15106
15107 // s0 qnan: min(s1, s2)
15108 // s1 qnan: min(s0, s2)
15109 // s2 qnan: min(s0, s1)
15110 const MachineFunction &MF = DAG.getMachineFunction();
15111 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
15112
15113 // TODO: Check IEEE bit enabled. We can form fmed3 with IEEE=0 regardless of
15114 // whether the input is a signaling nan if op0 is fmaximum or fmaximumnum. We
15115 // can only form if op0 is fmaxnum_ieee if IEEE=1.
15116 EVT VT = Op0.getValueType();
15117 if (Info->getMode().DX10Clamp) {
15118 // If dx10_clamp is enabled, NaNs clamp to 0.0. This is the same as the
15119 // hardware fmed3 behavior converting to a min.
15120 // FIXME: Should this be allowing -0.0?
15121 if (K1->isExactlyValue(1.0) && K0->isExactlyValue(0.0))
15122 return DAG.getNode(AMDGPUISD::CLAMP, SL, VT, Op0.getOperand(0));
15123 }
15124
15125 // med3 for f16 is only available on gfx9+, and not available for v2f16.
15126 if (VT == MVT::f32 || (VT == MVT::f16 && Subtarget->hasMed3_16())) {
15127 // This isn't safe with signaling NaNs because in IEEE mode, min/max on a
15128 // signaling NaN gives a quiet NaN. The quiet NaN input to the min would
15129 // then give the other result, which is different from med3 with a NaN
15130 // input.
15131 SDValue Var = Op0.getOperand(0);
15132 if (!DAG.isKnownNeverSNaN(Var))
15133 return SDValue();
15134
15135 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
15136
15137 if ((!K0->hasOneUse() || TII->isInlineConstant(K0->getValueAPF())) &&
15138 (!K1->hasOneUse() || TII->isInlineConstant(K1->getValueAPF()))) {
15139 return DAG.getNode(AMDGPUISD::FMED3, SL, K0->getValueType(0), Var,
15140 SDValue(K0, 0), SDValue(K1, 0));
15141 }
15142 }
15143
15144 return SDValue();
15145}
15146
15147/// \return true if the subtarget supports minimum3 and maximum3 with the given
15148/// base min/max opcode \p Opc for type \p VT.
15149static bool supportsMin3Max3(const GCNSubtarget &Subtarget, unsigned Opc,
15150 EVT VT) {
15151 switch (Opc) {
15152 case ISD::FMINNUM:
15153 case ISD::FMAXNUM:
15154 case ISD::FMINNUM_IEEE:
15155 case ISD::FMAXNUM_IEEE:
15156 case ISD::FMINIMUMNUM:
15157 case ISD::FMAXIMUMNUM:
15158 case AMDGPUISD::FMIN_LEGACY:
15159 case AMDGPUISD::FMAX_LEGACY:
15160 return (VT == MVT::f32) || (VT == MVT::f16 && Subtarget.hasMin3Max3_16()) ||
15161 (VT == MVT::v2f16 && Subtarget.hasMin3Max3PKF16());
15162 case ISD::FMINIMUM:
15163 case ISD::FMAXIMUM:
15164 return (VT == MVT::f32 && Subtarget.hasMinimum3Maximum3F32()) ||
15165 (VT == MVT::f16 && Subtarget.hasMinimum3Maximum3F16()) ||
15166 (VT == MVT::v2f16 && Subtarget.hasMinimum3Maximum3PKF16());
15167 case ISD::SMAX:
15168 case ISD::SMIN:
15169 case ISD::UMAX:
15170 case ISD::UMIN:
15171 return (VT == MVT::i32) || (VT == MVT::i16 && Subtarget.hasMin3Max3_16());
15172 default:
15173 return false;
15174 }
15175
15176 llvm_unreachable("not a min/max opcode");
15177}
15178
15179SDValue SITargetLowering::performMinMaxCombine(SDNode *N,
15180 DAGCombinerInfo &DCI) const {
15181 SelectionDAG &DAG = DCI.DAG;
15182
15183 EVT VT = N->getValueType(0);
15184 unsigned Opc = N->getOpcode();
15185 SDValue Op0 = N->getOperand(0);
15186 SDValue Op1 = N->getOperand(1);
15187
15188 // Only do this if the inner op has one use since this will just increases
15189 // register pressure for no benefit.
15190
15191 if (supportsMin3Max3(*Subtarget, Opc, VT)) {
15192 // max(max(a, b), c) -> max3(a, b, c)
15193 // min(min(a, b), c) -> min3(a, b, c)
15194 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) {
15195 SDLoc DL(N);
15196 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc), DL, N->getValueType(0),
15197 Op0.getOperand(0), Op0.getOperand(1), Op1);
15198 }
15199
15200 // Try commuted.
15201 // max(a, max(b, c)) -> max3(a, b, c)
15202 // min(a, min(b, c)) -> min3(a, b, c)
15203 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) {
15204 SDLoc DL(N);
15205 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc), DL, N->getValueType(0),
15206 Op0, Op1.getOperand(0), Op1.getOperand(1));
15207 }
15208 }
15209
15210 // min(max(x, K0), K1), K0 < K1 -> med3(x, K0, K1)
15211 // max(min(x, K0), K1), K1 < K0 -> med3(x, K1, K0)
15212 if (Opc == ISD::SMIN && Op0.getOpcode() == ISD::SMAX && Op0.hasOneUse()) {
15213 if (SDValue Med3 = performIntMed3ImmCombine(
15214 DAG, SDLoc(N), Op0->getOperand(0), Op1, Op0->getOperand(1), true))
15215 return Med3;
15216 }
15217 if (Opc == ISD::SMAX && Op0.getOpcode() == ISD::SMIN && Op0.hasOneUse()) {
15218 if (SDValue Med3 = performIntMed3ImmCombine(
15219 DAG, SDLoc(N), Op0->getOperand(0), Op0->getOperand(1), Op1, true))
15220 return Med3;
15221 }
15222
15223 if (Opc == ISD::UMIN && Op0.getOpcode() == ISD::UMAX && Op0.hasOneUse()) {
15224 if (SDValue Med3 = performIntMed3ImmCombine(
15225 DAG, SDLoc(N), Op0->getOperand(0), Op1, Op0->getOperand(1), false))
15226 return Med3;
15227 }
15228 if (Opc == ISD::UMAX && Op0.getOpcode() == ISD::UMIN && Op0.hasOneUse()) {
15229 if (SDValue Med3 = performIntMed3ImmCombine(
15230 DAG, SDLoc(N), Op0->getOperand(0), Op0->getOperand(1), Op1, false))
15231 return Med3;
15232 }
15233
15234 // if !is_snan(x):
15235 // fminnum(fmaxnum(x, K0), K1), K0 < K1 -> fmed3(x, K0, K1)
15236 // fminnum_ieee(fmaxnum_ieee(x, K0), K1), K0 < K1 -> fmed3(x, K0, K1)
15237 // fminnumnum(fmaxnumnum(x, K0), K1), K0 < K1 -> fmed3(x, K0, K1)
15238 // fmin_legacy(fmax_legacy(x, K0), K1), K0 < K1 -> fmed3(x, K0, K1)
15239 if (((Opc == ISD::FMINNUM && Op0.getOpcode() == ISD::FMAXNUM) ||
15242 (Opc == AMDGPUISD::FMIN_LEGACY &&
15243 Op0.getOpcode() == AMDGPUISD::FMAX_LEGACY)) &&
15244 (VT == MVT::f32 || VT == MVT::f64 ||
15245 (VT == MVT::f16 && Subtarget->has16BitInsts()) ||
15246 (VT == MVT::bf16 && Subtarget->hasBF16PackedInsts()) ||
15247 (VT == MVT::v2bf16 && Subtarget->hasBF16PackedInsts()) ||
15248 (VT == MVT::v2f16 && Subtarget->hasVOP3PInsts())) &&
15249 Op0.hasOneUse()) {
15250 if (SDValue Res = performFPMed3ImmCombine(DAG, SDLoc(N), Op0, Op1))
15251 return Res;
15252 }
15253
15254 // Prefer fminnum_ieee over fminimum. For gfx950, minimum/maximum are legal
15255 // for some types, but at a higher cost since it's implemented with a 3
15256 // operand form.
15257 const SDNodeFlags Flags = N->getFlags();
15258 if ((Opc == ISD::FMINIMUM || Opc == ISD::FMAXIMUM) &&
15259 !Subtarget->hasIEEEMinimumMaximumInsts() && Flags.hasNoNaNs()) {
15260 unsigned NewOpc =
15262 return DAG.getNode(NewOpc, SDLoc(N), VT, Op0, Op1, Flags);
15263 }
15264
15265 return SDValue();
15266}
15267
15271 // FIXME: Should this be allowing -0.0?
15272 return (CA->isExactlyValue(0.0) && CB->isExactlyValue(1.0)) ||
15273 (CA->isExactlyValue(1.0) && CB->isExactlyValue(0.0));
15274 }
15275 }
15276
15277 return false;
15278}
15279
15280// FIXME: Should only worry about snans for version with chain.
15281SDValue SITargetLowering::performFMed3Combine(SDNode *N,
15282 DAGCombinerInfo &DCI) const {
15283 EVT VT = N->getValueType(0);
15284 // v_med3_f32 and v_max_f32 behave identically wrt denorms, exceptions and
15285 // NaNs. With a NaN input, the order of the operands may change the result.
15286
15287 SelectionDAG &DAG = DCI.DAG;
15288 SDLoc SL(N);
15289
15290 SDValue Src0 = N->getOperand(0);
15291 SDValue Src1 = N->getOperand(1);
15292 SDValue Src2 = N->getOperand(2);
15293
15294 if (isClampZeroToOne(Src0, Src1)) {
15295 // const_a, const_b, x -> clamp is safe in all cases including signaling
15296 // nans.
15297 // FIXME: Should this be allowing -0.0?
15298 return DAG.getNode(AMDGPUISD::CLAMP, SL, VT, Src2);
15299 }
15300
15301 const MachineFunction &MF = DAG.getMachineFunction();
15302 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
15303
15304 // FIXME: dx10_clamp behavior assumed in instcombine. Should we really bother
15305 // handling no dx10-clamp?
15306 if (Info->getMode().DX10Clamp) {
15307 // If NaNs is clamped to 0, we are free to reorder the inputs.
15308
15309 if (isa<ConstantFPSDNode>(Src0) && !isa<ConstantFPSDNode>(Src1))
15310 std::swap(Src0, Src1);
15311
15312 if (isa<ConstantFPSDNode>(Src1) && !isa<ConstantFPSDNode>(Src2))
15313 std::swap(Src1, Src2);
15314
15315 if (isa<ConstantFPSDNode>(Src0) && !isa<ConstantFPSDNode>(Src1))
15316 std::swap(Src0, Src1);
15317
15318 if (isClampZeroToOne(Src1, Src2))
15319 return DAG.getNode(AMDGPUISD::CLAMP, SL, VT, Src0);
15320 }
15321
15322 return SDValue();
15323}
15324
15325SDValue SITargetLowering::performCvtPkRTZCombine(SDNode *N,
15326 DAGCombinerInfo &DCI) const {
15327 SDValue Src0 = N->getOperand(0);
15328 SDValue Src1 = N->getOperand(1);
15329 if (Src0.isUndef() && Src1.isUndef())
15330 return DCI.DAG.getUNDEF(N->getValueType(0));
15331 return SDValue();
15332}
15333
15334// Check if EXTRACT_VECTOR_ELT/INSERT_VECTOR_ELT (<n x e>, var-idx) should be
15335// expanded into a set of cmp/select instructions.
15337 unsigned NumElem,
15338 bool IsDivergentIdx,
15339 const GCNSubtarget *Subtarget) {
15341 return false;
15342
15343 unsigned VecSize = EltSize * NumElem;
15344
15345 // Sub-dword vectors of size 2 dword or less have better implementation.
15346 if (VecSize <= 64 && EltSize < 32)
15347 return false;
15348
15349 // Always expand the rest of sub-dword instructions, otherwise it will be
15350 // lowered via memory.
15351 if (EltSize < 32)
15352 return true;
15353
15354 // Always do this if var-idx is divergent, otherwise it will become a loop.
15355 if (IsDivergentIdx)
15356 return true;
15357
15358 // Large vectors would yield too many compares and v_cndmask_b32 instructions.
15359 unsigned NumInsts = NumElem /* Number of compares */ +
15360 ((EltSize + 31) / 32) * NumElem /* Number of cndmasks */;
15361
15362 // On some architectures (GFX9) movrel is not available and it's better
15363 // to expand.
15364 if (Subtarget->useVGPRIndexMode())
15365 return NumInsts <= 16;
15366
15367 // If movrel is available, use it instead of expanding for vector of 8
15368 // elements.
15369 if (Subtarget->hasMovrel())
15370 return NumInsts <= 15;
15371
15372 return true;
15373}
15374
15376 SDValue Idx = N->getOperand(N->getNumOperands() - 1);
15377 if (isa<ConstantSDNode>(Idx))
15378 return false;
15379
15380 SDValue Vec = N->getOperand(0);
15381 EVT VecVT = Vec.getValueType();
15382 EVT EltVT = VecVT.getVectorElementType();
15383 unsigned EltSize = EltVT.getSizeInBits();
15384 unsigned NumElem = VecVT.getVectorNumElements();
15385
15387 EltSize, NumElem, Idx->isDivergent(), getSubtarget());
15388}
15389
15390SDValue
15391SITargetLowering::performExtractVectorEltCombine(SDNode *N,
15392 DAGCombinerInfo &DCI) const {
15393 SDValue Vec = N->getOperand(0);
15394 SelectionDAG &DAG = DCI.DAG;
15395
15396 EVT VecVT = Vec.getValueType();
15397 EVT VecEltVT = VecVT.getVectorElementType();
15398 EVT ResVT = N->getValueType(0);
15399
15400 unsigned VecSize = VecVT.getSizeInBits();
15401 unsigned VecEltSize = VecEltVT.getSizeInBits();
15402
15403 if ((Vec.getOpcode() == ISD::FNEG || Vec.getOpcode() == ISD::FABS) &&
15405 SDLoc SL(N);
15406 SDValue Idx = N->getOperand(1);
15407 SDValue Elt =
15408 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, ResVT, Vec.getOperand(0), Idx);
15409 return DAG.getNode(Vec.getOpcode(), SL, ResVT, Elt);
15410 }
15411
15412 // (extract_vector_element (and {y0, y1}, (build_vector 0x1f, 0x1f)), index)
15413 // -> (and (extract_vector_element {y0, y1}, index), 0x1f)
15414 // There are optimisations to transform 64-bit shifts into 32-bit shifts
15415 // depending on the shift operand. See e.g. performSraCombine().
15416 // This combine ensures that the optimisation is compatible with v2i32
15417 // legalised AND.
15418 if (VecVT == MVT::v2i32 && Vec->getOpcode() == ISD::AND &&
15419 Vec->getOperand(1)->getOpcode() == ISD::BUILD_VECTOR) {
15420
15422 if (!C || C->getZExtValue() != 0x1f)
15423 return SDValue();
15424
15425 SDLoc SL(N);
15426 SDValue AndMask = DAG.getConstant(0x1f, SL, MVT::i32);
15427 SDValue EVE = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32,
15428 Vec->getOperand(0), N->getOperand(1));
15429 SDValue A = DAG.getNode(ISD::AND, SL, MVT::i32, EVE, AndMask);
15430 DAG.ReplaceAllUsesWith(N, A.getNode());
15431 }
15432
15433 // ScalarRes = EXTRACT_VECTOR_ELT ((vector-BINOP Vec1, Vec2), Idx)
15434 // =>
15435 // Vec1Elt = EXTRACT_VECTOR_ELT(Vec1, Idx)
15436 // Vec2Elt = EXTRACT_VECTOR_ELT(Vec2, Idx)
15437 // ScalarRes = scalar-BINOP Vec1Elt, Vec2Elt
15438 if (Vec.hasOneUse() && DCI.isBeforeLegalize() && VecEltVT == ResVT) {
15439 SDLoc SL(N);
15440 SDValue Idx = N->getOperand(1);
15441 unsigned Opc = Vec.getOpcode();
15442
15443 switch (Opc) {
15444 default:
15445 break;
15446 // TODO: Support other binary operations.
15447 case ISD::FADD:
15448 case ISD::FSUB:
15449 case ISD::FMUL:
15450 case ISD::ADD:
15451 case ISD::UMIN:
15452 case ISD::UMAX:
15453 case ISD::SMIN:
15454 case ISD::SMAX:
15455 case ISD::FMAXNUM:
15456 case ISD::FMINNUM:
15457 case ISD::FMAXNUM_IEEE:
15458 case ISD::FMINNUM_IEEE:
15459 case ISD::FMAXIMUM:
15460 case ISD::FMINIMUM: {
15461 SDValue Elt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, ResVT,
15462 Vec.getOperand(0), Idx);
15463 SDValue Elt1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, ResVT,
15464 Vec.getOperand(1), Idx);
15465
15466 DCI.AddToWorklist(Elt0.getNode());
15467 DCI.AddToWorklist(Elt1.getNode());
15468 return DAG.getNode(Opc, SL, ResVT, Elt0, Elt1, Vec->getFlags());
15469 }
15470 }
15471 }
15472
15473 // EXTRACT_VECTOR_ELT (<n x e>, var-idx) => n x select (e, const-idx)
15475 SDLoc SL(N);
15476 SDValue Idx = N->getOperand(1);
15477 SDValue V;
15478 for (unsigned I = 0, E = VecVT.getVectorNumElements(); I < E; ++I) {
15479 SDValue IC = DAG.getVectorIdxConstant(I, SL);
15480 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, ResVT, Vec, IC);
15481 if (I == 0)
15482 V = Elt;
15483 else
15484 V = DAG.getSelectCC(SL, Idx, IC, Elt, V, ISD::SETEQ);
15485 }
15486 return V;
15487 }
15488
15489 if (!DCI.isBeforeLegalize())
15490 return SDValue();
15491
15492 // Try to turn sub-dword accesses of vectors into accesses of the same 32-bit
15493 // elements. This exposes more load reduction opportunities by replacing
15494 // multiple small extract_vector_elements with a single 32-bit extract.
15495 auto *Idx = dyn_cast<ConstantSDNode>(N->getOperand(1));
15496 if (isa<MemSDNode>(Vec) && VecEltSize <= 16 && VecEltVT.isByteSized() &&
15497 VecSize > 32 && VecSize % 32 == 0 && Idx) {
15498 EVT NewVT = getEquivalentMemType(*DAG.getContext(), VecVT);
15499
15500 unsigned BitIndex = Idx->getZExtValue() * VecEltSize;
15501 unsigned EltIdx = BitIndex / 32;
15502 unsigned LeftoverBitIdx = BitIndex % 32;
15503 SDLoc SL(N);
15504
15505 SDValue Cast = DAG.getNode(ISD::BITCAST, SL, NewVT, Vec);
15506 DCI.AddToWorklist(Cast.getNode());
15507
15508 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Cast,
15509 DAG.getConstant(EltIdx, SL, MVT::i32));
15510 DCI.AddToWorklist(Elt.getNode());
15511 SDValue Srl = DAG.getNode(ISD::SRL, SL, MVT::i32, Elt,
15512 DAG.getConstant(LeftoverBitIdx, SL, MVT::i32));
15513 DCI.AddToWorklist(Srl.getNode());
15514
15515 EVT VecEltAsIntVT = VecEltVT.changeTypeToInteger();
15516 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SL, VecEltAsIntVT, Srl);
15517 DCI.AddToWorklist(Trunc.getNode());
15518
15519 if (VecEltVT == ResVT) {
15520 return DAG.getNode(ISD::BITCAST, SL, VecEltVT, Trunc);
15521 }
15522
15523 assert(ResVT.isScalarInteger());
15524 return DAG.getAnyExtOrTrunc(Trunc, SL, ResVT);
15525 }
15526
15527 return SDValue();
15528}
15529
15530SDValue
15531SITargetLowering::performInsertVectorEltCombine(SDNode *N,
15532 DAGCombinerInfo &DCI) const {
15533 SDValue Vec = N->getOperand(0);
15534 SDValue Idx = N->getOperand(2);
15535 EVT VecVT = Vec.getValueType();
15536 EVT EltVT = VecVT.getVectorElementType();
15537
15538 // INSERT_VECTOR_ELT (<n x e>, var-idx)
15539 // => BUILD_VECTOR n x select (e, const-idx)
15541 return SDValue();
15542
15543 SelectionDAG &DAG = DCI.DAG;
15544 SDLoc SL(N);
15545 SDValue Ins = N->getOperand(1);
15546 EVT IdxVT = Idx.getValueType();
15547
15549 for (unsigned I = 0, E = VecVT.getVectorNumElements(); I < E; ++I) {
15550 SDValue IC = DAG.getConstant(I, SL, IdxVT);
15551 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, Vec, IC);
15552 SDValue V = DAG.getSelectCC(SL, Idx, IC, Ins, Elt, ISD::SETEQ);
15553 Ops.push_back(V);
15554 }
15555
15556 return DAG.getBuildVector(VecVT, SL, Ops);
15557}
15558
15559/// Return the source of an fp_extend from f16 to f32, or a converted FP
15560/// constant.
15562 if (Src.getOpcode() == ISD::FP_EXTEND &&
15563 Src.getOperand(0).getValueType() == MVT::f16) {
15564 return Src.getOperand(0);
15565 }
15566
15567 if (auto *CFP = dyn_cast<ConstantFPSDNode>(Src)) {
15568 APFloat Val = CFP->getValueAPF();
15569 bool LosesInfo = true;
15571 if (!LosesInfo)
15572 return DAG.getConstantFP(Val, SDLoc(Src), MVT::f16);
15573 }
15574
15575 return SDValue();
15576}
15577
15578SDValue SITargetLowering::performFPRoundCombine(SDNode *N,
15579 DAGCombinerInfo &DCI) const {
15580 assert(Subtarget->has16BitInsts() && !Subtarget->hasMed3_16() &&
15581 "combine only useful on gfx8");
15582
15583 SDValue TruncSrc = N->getOperand(0);
15584 EVT VT = N->getValueType(0);
15585 if (VT != MVT::f16)
15586 return SDValue();
15587
15588 if (TruncSrc.getOpcode() != AMDGPUISD::FMED3 ||
15589 TruncSrc.getValueType() != MVT::f32 || !TruncSrc.hasOneUse())
15590 return SDValue();
15591
15592 SelectionDAG &DAG = DCI.DAG;
15593 SDLoc SL(N);
15594
15595 // Optimize f16 fmed3 pattern performed on f32. On gfx8 there is no f16 fmed3,
15596 // and expanding it with min/max saves 1 instruction vs. casting to f32 and
15597 // casting back.
15598
15599 // fptrunc (f32 (fmed3 (fpext f16:a, fpext f16:b, fpext f16:c))) =>
15600 // fmin(fmax(a, b), fmax(fmin(a, b), c))
15601 SDValue A = strictFPExtFromF16(DAG, TruncSrc.getOperand(0));
15602 if (!A)
15603 return SDValue();
15604
15605 SDValue B = strictFPExtFromF16(DAG, TruncSrc.getOperand(1));
15606 if (!B)
15607 return SDValue();
15608
15609 SDValue C = strictFPExtFromF16(DAG, TruncSrc.getOperand(2));
15610 if (!C)
15611 return SDValue();
15612
15613 // This changes signaling nan behavior. If an input is a signaling nan, it
15614 // would have been quieted by the fpext originally. We don't care because
15615 // these are unconstrained ops. If we needed to insert quieting canonicalizes
15616 // we would be worse off than just doing the promotion.
15617 SDValue A1 = DAG.getNode(ISD::FMINNUM_IEEE, SL, VT, A, B);
15618 SDValue B1 = DAG.getNode(ISD::FMAXNUM_IEEE, SL, VT, A, B);
15619 SDValue C1 = DAG.getNode(ISD::FMAXNUM_IEEE, SL, VT, A1, C);
15620 return DAG.getNode(ISD::FMINNUM_IEEE, SL, VT, B1, C1);
15621}
15622
15623unsigned SITargetLowering::getFusedOpcode(const SelectionDAG &DAG,
15624 const SDNode *N0,
15625 const SDNode *N1) const {
15626 EVT VT = N0->getValueType(0);
15627
15628 // Only do this if we are not trying to support denormals. v_mad_f32 does not
15629 // support denormals ever.
15630 if (((VT == MVT::f32 &&
15632 (VT == MVT::f16 && Subtarget->hasMadF16() &&
15635 return ISD::FMAD;
15636
15637 const TargetOptions &Options = DAG.getTarget().Options;
15638 if ((Options.AllowFPOpFusion == FPOpFusion::Fast ||
15639 (N0->getFlags().hasAllowContract() &&
15640 N1->getFlags().hasAllowContract())) &&
15642 return ISD::FMA;
15643 }
15644
15645 return 0;
15646}
15647
15648// For a reassociatable opcode perform:
15649// op x, (op y, z) -> op (op x, z), y, if x and z are uniform
15650SDValue SITargetLowering::reassociateScalarOps(SDNode *N,
15651 SelectionDAG &DAG) const {
15652 EVT VT = N->getValueType(0);
15653 if (VT != MVT::i32 && VT != MVT::i64)
15654 return SDValue();
15655
15656 if (DAG.isBaseWithConstantOffset(SDValue(N, 0)))
15657 return SDValue();
15658
15659 unsigned Opc = N->getOpcode();
15660 SDValue Op0 = N->getOperand(0);
15661 SDValue Op1 = N->getOperand(1);
15662
15663 if (!(Op0->isDivergent() ^ Op1->isDivergent()))
15664 return SDValue();
15665
15666 if (Op0->isDivergent())
15667 std::swap(Op0, Op1);
15668
15669 if (Op1.getOpcode() != Opc || !Op1.hasOneUse())
15670 return SDValue();
15671
15672 SDValue Op2 = Op1.getOperand(1);
15673 Op1 = Op1.getOperand(0);
15674 if (!(Op1->isDivergent() ^ Op2->isDivergent()))
15675 return SDValue();
15676
15677 if (Op1->isDivergent())
15678 std::swap(Op1, Op2);
15679
15680 SDLoc SL(N);
15681 SDValue Add1 = DAG.getNode(Opc, SL, VT, Op0, Op1);
15682 return DAG.getNode(Opc, SL, VT, Add1, Op2);
15683}
15684
15685static SDValue getMad64_32(SelectionDAG &DAG, const SDLoc &SL, EVT VT,
15686 SDValue N0, SDValue N1, SDValue N2, bool Signed) {
15688 SDVTList VTs = DAG.getVTList(MVT::i64, MVT::i1);
15689 SDValue Mad = DAG.getNode(MadOpc, SL, VTs, N0, N1, N2);
15690 return DAG.getNode(ISD::TRUNCATE, SL, VT, Mad);
15691}
15692
15693// Fold
15694// y = lshr i64 x, 32
15695// res = add (mul i64 y, Const), x where "Const" is a 64-bit constant
15696// with Const.hi == -1
15697// To
15698// res = mad_u64_u32 y.lo ,Const.lo, x.lo
15700 SDValue MulLHS, SDValue MulRHS,
15701 SDValue AddRHS) {
15702 if (MulRHS.getOpcode() == ISD::SRL)
15703 std::swap(MulLHS, MulRHS);
15704
15705 if (MulLHS.getValueType() != MVT::i64 || MulLHS.getOpcode() != ISD::SRL)
15706 return SDValue();
15707
15708 ConstantSDNode *ShiftVal = dyn_cast<ConstantSDNode>(MulLHS.getOperand(1));
15709 if (!ShiftVal || ShiftVal->getAsZExtVal() != 32 ||
15710 MulLHS.getOperand(0) != AddRHS)
15711 return SDValue();
15712
15714 if (!Const || Hi_32(Const->getZExtValue()) != uint32_t(-1))
15715 return SDValue();
15716
15717 SDValue ConstMul =
15718 DAG.getConstant(Lo_32(Const->getZExtValue()), SL, MVT::i32);
15719 return getMad64_32(DAG, SL, MVT::i64,
15720 DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, MulLHS), ConstMul,
15721 DAG.getZeroExtendInReg(AddRHS, SL, MVT::i32), false);
15722}
15723
15724// Fold (add (mul x, y), z) --> (mad_[iu]64_[iu]32 x, y, z) plus high
15725// multiplies, if any.
15726//
15727// Full 64-bit multiplies that feed into an addition are lowered here instead
15728// of using the generic expansion. The generic expansion ends up with
15729// a tree of ADD nodes that prevents us from using the "add" part of the
15730// MAD instruction. The expansion produced here results in a chain of ADDs
15731// instead of a tree.
15732SDValue SITargetLowering::tryFoldToMad64_32(SDNode *N,
15733 DAGCombinerInfo &DCI) const {
15734 assert(N->isAnyAdd());
15735
15736 SelectionDAG &DAG = DCI.DAG;
15737 EVT VT = N->getValueType(0);
15738 SDLoc SL(N);
15739 SDValue LHS = N->getOperand(0);
15740 SDValue RHS = N->getOperand(1);
15741
15742 if (VT.isVector())
15743 return SDValue();
15744
15745 // S_MUL_HI_[IU]32 was added in gfx9, which allows us to keep the overall
15746 // result in scalar registers for uniform values.
15747 if (!N->isDivergent() && Subtarget->hasSMulHi())
15748 return SDValue();
15749
15750 unsigned NumBits = VT.getScalarSizeInBits();
15751 if (NumBits <= 32 || NumBits > 64)
15752 return SDValue();
15753
15754 if (LHS.getOpcode() != ISD::MUL) {
15755 assert(RHS.getOpcode() == ISD::MUL);
15756 std::swap(LHS, RHS);
15757 }
15758
15759 // Avoid the fold if it would unduly increase the number of multiplies due to
15760 // multiple uses, except on hardware with full-rate multiply-add (which is
15761 // part of full-rate 64-bit ops).
15762 if (!Subtarget->hasFullRate64Ops()) {
15763 unsigned NumUsers = 0;
15764 for (SDNode *User : LHS->users()) {
15765 // There is a use that does not feed into addition, so the multiply can't
15766 // be removed. We prefer MUL + ADD + ADDC over MAD + MUL.
15767 if (!User->isAnyAdd())
15768 return SDValue();
15769
15770 // We prefer 2xMAD over MUL + 2xADD + 2xADDC (code density), and prefer
15771 // MUL + 3xADD + 3xADDC over 3xMAD.
15772 ++NumUsers;
15773 if (NumUsers >= 3)
15774 return SDValue();
15775 }
15776 }
15777
15778 SDValue MulLHS = LHS.getOperand(0);
15779 SDValue MulRHS = LHS.getOperand(1);
15780 SDValue AddRHS = RHS;
15781
15782 if (SDValue FoldedMAD = tryFoldMADwithSRL(DAG, SL, MulLHS, MulRHS, AddRHS))
15783 return FoldedMAD;
15784
15785 // Always check whether operands are small unsigned values, since that
15786 // knowledge is useful in more cases. Check for small signed values only if
15787 // doing so can unlock a shorter code sequence.
15788 bool MulLHSUnsigned32 = numBitsUnsigned(MulLHS, DAG) <= 32;
15789 bool MulRHSUnsigned32 = numBitsUnsigned(MulRHS, DAG) <= 32;
15790
15791 bool MulSignedLo = false;
15792 if (!MulLHSUnsigned32 || !MulRHSUnsigned32) {
15793 MulSignedLo =
15794 numBitsSigned(MulLHS, DAG) <= 32 && numBitsSigned(MulRHS, DAG) <= 32;
15795 }
15796
15797 // The operands and final result all have the same number of bits. If
15798 // operands need to be extended, they can be extended with garbage. The
15799 // resulting garbage in the high bits of the mad_[iu]64_[iu]32 result is
15800 // truncated away in the end.
15801 if (VT != MVT::i64) {
15802 MulLHS = DAG.getNode(ISD::ANY_EXTEND, SL, MVT::i64, MulLHS);
15803 MulRHS = DAG.getNode(ISD::ANY_EXTEND, SL, MVT::i64, MulRHS);
15804 AddRHS = DAG.getNode(ISD::ANY_EXTEND, SL, MVT::i64, AddRHS);
15805 }
15806
15807 // The basic code generated is conceptually straightforward. Pseudo code:
15808 //
15809 // accum = mad_64_32 lhs.lo, rhs.lo, accum
15810 // accum.hi = add (mul lhs.hi, rhs.lo), accum.hi
15811 // accum.hi = add (mul lhs.lo, rhs.hi), accum.hi
15812 //
15813 // The second and third lines are optional, depending on whether the factors
15814 // are {sign,zero}-extended or not.
15815 //
15816 // The actual DAG is noisier than the pseudo code, but only due to
15817 // instructions that disassemble values into low and high parts, and
15818 // assemble the final result.
15819 SDValue One = DAG.getConstant(1, SL, MVT::i32);
15820
15821 auto MulLHSLo = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, MulLHS);
15822 auto MulRHSLo = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, MulRHS);
15823 SDValue Accum =
15824 getMad64_32(DAG, SL, MVT::i64, MulLHSLo, MulRHSLo, AddRHS, MulSignedLo);
15825
15826 if (!MulSignedLo && (!MulLHSUnsigned32 || !MulRHSUnsigned32)) {
15827 auto [AccumLo, AccumHi] = DAG.SplitScalar(Accum, SL, MVT::i32, MVT::i32);
15828
15829 if (!MulLHSUnsigned32) {
15830 auto MulLHSHi =
15831 DAG.getNode(ISD::EXTRACT_ELEMENT, SL, MVT::i32, MulLHS, One);
15832 SDValue MulHi = DAG.getNode(ISD::MUL, SL, MVT::i32, MulLHSHi, MulRHSLo);
15833 AccumHi = DAG.getNode(ISD::ADD, SL, MVT::i32, MulHi, AccumHi);
15834 }
15835
15836 if (!MulRHSUnsigned32) {
15837 auto MulRHSHi =
15838 DAG.getNode(ISD::EXTRACT_ELEMENT, SL, MVT::i32, MulRHS, One);
15839 SDValue MulHi = DAG.getNode(ISD::MUL, SL, MVT::i32, MulLHSLo, MulRHSHi);
15840 AccumHi = DAG.getNode(ISD::ADD, SL, MVT::i32, MulHi, AccumHi);
15841 }
15842
15843 Accum = DAG.getBuildVector(MVT::v2i32, SL, {AccumLo, AccumHi});
15844 Accum = DAG.getBitcast(MVT::i64, Accum);
15845 }
15846
15847 if (VT != MVT::i64)
15848 Accum = DAG.getNode(ISD::TRUNCATE, SL, VT, Accum);
15849 return Accum;
15850}
15851
15852SDValue
15853SITargetLowering::foldAddSub64WithZeroLowBitsTo32(SDNode *N,
15854 DAGCombinerInfo &DCI) const {
15855 SDValue RHS = N->getOperand(1);
15856 auto *CRHS = dyn_cast<ConstantSDNode>(RHS);
15857 if (!CRHS)
15858 return SDValue();
15859
15860 // TODO: Worth using computeKnownBits? Maybe expensive since it's so
15861 // common.
15862 uint64_t Val = CRHS->getZExtValue();
15863 if (countr_zero(Val) >= 32) {
15864 SelectionDAG &DAG = DCI.DAG;
15865 SDLoc SL(N);
15866 SDValue LHS = N->getOperand(0);
15867
15868 // Avoid carry machinery if we know the low half of the add does not
15869 // contribute to the final result.
15870 //
15871 // add i64:x, K if computeTrailingZeros(K) >= 32
15872 // => build_pair (add x.hi, K.hi), x.lo
15873
15874 // Breaking the 64-bit add here with this strange constant is unlikely
15875 // to interfere with addressing mode patterns.
15876
15877 SDValue Hi = getHiHalf64(LHS, DAG);
15878 SDValue ConstHi32 = DAG.getConstant(Hi_32(Val), SL, MVT::i32);
15879 unsigned Opcode = N->getOpcode();
15880 if (Opcode == ISD::PTRADD)
15881 Opcode = ISD::ADD;
15882 SDValue AddHi =
15883 DAG.getNode(Opcode, SL, MVT::i32, Hi, ConstHi32, N->getFlags());
15884
15885 SDValue Lo = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, LHS);
15886 return DAG.getNode(ISD::BUILD_PAIR, SL, MVT::i64, Lo, AddHi);
15887 }
15888
15889 return SDValue();
15890}
15891
15892// Collect the ultimate src of each of the mul node's operands, and confirm
15893// each operand is 8 bytes.
15894static std::optional<ByteProvider<SDValue>>
15895handleMulOperand(const SDValue &MulOperand) {
15896 auto Byte0 = calculateByteProvider(MulOperand, 0, 0);
15897 if (!Byte0 || Byte0->isConstantZero()) {
15898 return std::nullopt;
15899 }
15900 auto Byte1 = calculateByteProvider(MulOperand, 1, 0);
15901 if (Byte1 && !Byte1->isConstantZero()) {
15902 return std::nullopt;
15903 }
15904 return Byte0;
15905}
15906
15907static unsigned addPermMasks(unsigned First, unsigned Second) {
15908 unsigned FirstCs = First & 0x0c0c0c0c;
15909 unsigned SecondCs = Second & 0x0c0c0c0c;
15910 unsigned FirstNoCs = First & ~0x0c0c0c0c;
15911 unsigned SecondNoCs = Second & ~0x0c0c0c0c;
15912
15913 assert((FirstCs & 0xFF) | (SecondCs & 0xFF));
15914 assert((FirstCs & 0xFF00) | (SecondCs & 0xFF00));
15915 assert((FirstCs & 0xFF0000) | (SecondCs & 0xFF0000));
15916 assert((FirstCs & 0xFF000000) | (SecondCs & 0xFF000000));
15917
15918 return (FirstNoCs | SecondNoCs) | (FirstCs & SecondCs);
15919}
15920
15921struct DotSrc {
15923 int64_t PermMask;
15925};
15926
15930 SmallVectorImpl<DotSrc> &Src1s, int Step) {
15931
15932 assert(Src0.Src.has_value() && Src1.Src.has_value());
15933 // Src0s and Src1s are empty, just place arbitrarily.
15934 if (Step == 0) {
15935 Src0s.push_back({*Src0.Src, ((Src0.SrcOffset % 4) << 24) + 0x0c0c0c,
15936 Src0.SrcOffset / 4});
15937 Src1s.push_back({*Src1.Src, ((Src1.SrcOffset % 4) << 24) + 0x0c0c0c,
15938 Src1.SrcOffset / 4});
15939 return;
15940 }
15941
15942 for (int BPI = 0; BPI < 2; BPI++) {
15943 std::pair<ByteProvider<SDValue>, ByteProvider<SDValue>> BPP = {Src0, Src1};
15944 if (BPI == 1) {
15945 BPP = {Src1, Src0};
15946 }
15947 unsigned ZeroMask = 0x0c0c0c0c;
15948 unsigned FMask = 0xFF << (8 * (3 - Step));
15949
15950 unsigned FirstMask =
15951 (BPP.first.SrcOffset % 4) << (8 * (3 - Step)) | (ZeroMask & ~FMask);
15952 unsigned SecondMask =
15953 (BPP.second.SrcOffset % 4) << (8 * (3 - Step)) | (ZeroMask & ~FMask);
15954 // Attempt to find Src vector which contains our SDValue, if so, add our
15955 // perm mask to the existing one. If we are unable to find a match for the
15956 // first SDValue, attempt to find match for the second.
15957 int FirstGroup = -1;
15958 for (int I = 0; I < 2; I++) {
15959 SmallVectorImpl<DotSrc> &Srcs = I == 0 ? Src0s : Src1s;
15960 auto MatchesFirst = [&BPP](DotSrc &IterElt) {
15961 return IterElt.SrcOp == *BPP.first.Src &&
15962 (IterElt.DWordOffset == (BPP.first.SrcOffset / 4));
15963 };
15964
15965 auto *Match = llvm::find_if(Srcs, MatchesFirst);
15966 if (Match != Srcs.end()) {
15967 Match->PermMask = addPermMasks(FirstMask, Match->PermMask);
15968 FirstGroup = I;
15969 break;
15970 }
15971 }
15972 if (FirstGroup != -1) {
15973 SmallVectorImpl<DotSrc> &Srcs = FirstGroup == 1 ? Src0s : Src1s;
15974 auto MatchesSecond = [&BPP](DotSrc &IterElt) {
15975 return IterElt.SrcOp == *BPP.second.Src &&
15976 (IterElt.DWordOffset == (BPP.second.SrcOffset / 4));
15977 };
15978 auto *Match = llvm::find_if(Srcs, MatchesSecond);
15979 if (Match != Srcs.end()) {
15980 Match->PermMask = addPermMasks(SecondMask, Match->PermMask);
15981 } else
15982 Srcs.push_back({*BPP.second.Src, SecondMask, BPP.second.SrcOffset / 4});
15983 return;
15984 }
15985 }
15986
15987 // If we have made it here, then we could not find a match in Src0s or Src1s
15988 // for either Src0 or Src1, so just place them arbitrarily.
15989
15990 unsigned ZeroMask = 0x0c0c0c0c;
15991 unsigned FMask = 0xFF << (8 * (3 - Step));
15992
15993 Src0s.push_back(
15994 {*Src0.Src,
15995 ((Src0.SrcOffset % 4) << (8 * (3 - Step)) | (ZeroMask & ~FMask)),
15996 Src0.SrcOffset / 4});
15997 Src1s.push_back(
15998 {*Src1.Src,
15999 ((Src1.SrcOffset % 4) << (8 * (3 - Step)) | (ZeroMask & ~FMask)),
16000 Src1.SrcOffset / 4});
16001}
16002
16004 SmallVectorImpl<DotSrc> &Srcs, bool IsSigned,
16005 bool IsAny) {
16006
16007 // If we just have one source, just permute it accordingly.
16008 if (Srcs.size() == 1) {
16009 auto *Elt = Srcs.begin();
16010 auto EltOp = getDWordFromOffset(DAG, SL, Elt->SrcOp, Elt->DWordOffset);
16011
16012 // v_perm will produce the original value
16013 if (Elt->PermMask == 0x3020100)
16014 return EltOp;
16015
16016 return DAG.getNode(AMDGPUISD::PERM, SL, MVT::i32, EltOp, EltOp,
16017 DAG.getConstant(Elt->PermMask, SL, MVT::i32));
16018 }
16019
16020 auto *FirstElt = Srcs.begin();
16021 auto *SecondElt = std::next(FirstElt);
16022
16024
16025 // If we have multiple sources in the chain, combine them via perms (using
16026 // calculated perm mask) and Ors.
16027 while (true) {
16028 auto FirstMask = FirstElt->PermMask;
16029 auto SecondMask = SecondElt->PermMask;
16030
16031 unsigned FirstCs = FirstMask & 0x0c0c0c0c;
16032 unsigned FirstPlusFour = FirstMask | 0x04040404;
16033 // 0x0c + 0x04 = 0x10, so anding with 0x0F will produced 0x00 for any
16034 // original 0x0C.
16035 FirstMask = (FirstPlusFour & 0x0F0F0F0F) | FirstCs;
16036
16037 auto PermMask = addPermMasks(FirstMask, SecondMask);
16038 auto FirstVal =
16039 getDWordFromOffset(DAG, SL, FirstElt->SrcOp, FirstElt->DWordOffset);
16040 auto SecondVal =
16041 getDWordFromOffset(DAG, SL, SecondElt->SrcOp, SecondElt->DWordOffset);
16042
16043 Perms.push_back(DAG.getNode(AMDGPUISD::PERM, SL, MVT::i32, FirstVal,
16044 SecondVal,
16045 DAG.getConstant(PermMask, SL, MVT::i32)));
16046
16047 FirstElt = std::next(SecondElt);
16048 if (FirstElt == Srcs.end())
16049 break;
16050
16051 SecondElt = std::next(FirstElt);
16052 // If we only have a FirstElt, then just combine that into the cumulative
16053 // source node.
16054 if (SecondElt == Srcs.end()) {
16055 auto EltOp =
16056 getDWordFromOffset(DAG, SL, FirstElt->SrcOp, FirstElt->DWordOffset);
16057
16058 Perms.push_back(
16059 DAG.getNode(AMDGPUISD::PERM, SL, MVT::i32, EltOp, EltOp,
16060 DAG.getConstant(FirstElt->PermMask, SL, MVT::i32)));
16061 break;
16062 }
16063 }
16064
16065 assert(Perms.size() == 1 || Perms.size() == 2);
16066 return Perms.size() == 2
16067 ? DAG.getNode(ISD::OR, SL, MVT::i32, Perms[0], Perms[1])
16068 : Perms[0];
16069}
16070
16071static void fixMasks(SmallVectorImpl<DotSrc> &Srcs, unsigned ChainLength) {
16072 for (auto &[EntryVal, EntryMask, EntryOffset] : Srcs) {
16073 EntryMask = EntryMask >> ((4 - ChainLength) * 8);
16074 auto ZeroMask = ChainLength == 2 ? 0x0c0c0000 : 0x0c000000;
16075 EntryMask += ZeroMask;
16076 }
16077}
16078
16079static bool isMul(const SDValue Op) {
16080 auto Opcode = Op.getOpcode();
16081
16082 return (Opcode == ISD::MUL || Opcode == AMDGPUISD::MUL_U24 ||
16083 Opcode == AMDGPUISD::MUL_I24);
16084}
16085
16086static std::optional<bool>
16088 ByteProvider<SDValue> &Src1, const SDValue &S0Op,
16089 const SDValue &S1Op, const SelectionDAG &DAG) {
16090 // If we both ops are i8s (pre legalize-dag), then the signedness semantics
16091 // of the dot4 is irrelevant.
16092 if (S0Op.getValueSizeInBits() == 8 && S1Op.getValueSizeInBits() == 8)
16093 return false;
16094
16095 auto Known0 = DAG.computeKnownBits(S0Op, 0);
16096 bool S0IsUnsigned = Known0.countMinLeadingZeros() > 0;
16097 bool S0IsSigned = Known0.countMinLeadingOnes() > 0;
16098 auto Known1 = DAG.computeKnownBits(S1Op, 0);
16099 bool S1IsUnsigned = Known1.countMinLeadingZeros() > 0;
16100 bool S1IsSigned = Known1.countMinLeadingOnes() > 0;
16101
16102 assert(!(S0IsUnsigned && S0IsSigned));
16103 assert(!(S1IsUnsigned && S1IsSigned));
16104
16105 // There are 9 possible permutations of
16106 // {S0IsUnsigned, S0IsSigned, S1IsUnsigned, S1IsSigned}
16107
16108 // In two permutations, the sign bits are known to be the same for both Ops,
16109 // so simply return Signed / Unsigned corresponding to the MSB
16110
16111 if ((S0IsUnsigned && S1IsUnsigned) || (S0IsSigned && S1IsSigned))
16112 return S0IsSigned;
16113
16114 // In another two permutations, the sign bits are known to be opposite. In
16115 // this case return std::nullopt to indicate a bad match.
16116
16117 if ((S0IsUnsigned && S1IsSigned) || (S0IsSigned && S1IsUnsigned))
16118 return std::nullopt;
16119
16120 // In the remaining five permutations, we don't know the value of the sign
16121 // bit for at least one Op. Since we have a valid ByteProvider, we know that
16122 // the upper bits must be extension bits. Thus, the only ways for the sign
16123 // bit to be unknown is if it was sign extended from unknown value, or if it
16124 // was any extended. In either case, it is correct to use the signed
16125 // version of the signedness semantics of dot4
16126
16127 // In two of such permutations, we known the sign bit is set for
16128 // one op, and the other is unknown. It is okay to used signed version of
16129 // dot4.
16130 if ((S0IsSigned && !(S1IsSigned || S1IsUnsigned)) ||
16131 ((S1IsSigned && !(S0IsSigned || S0IsUnsigned))))
16132 return true;
16133
16134 // In one such permutation, we don't know either of the sign bits. It is okay
16135 // to used the signed version of dot4.
16136 if ((!(S1IsSigned || S1IsUnsigned) && !(S0IsSigned || S0IsUnsigned)))
16137 return true;
16138
16139 // In two of such permutations, we known the sign bit is unset for
16140 // one op, and the other is unknown. Return std::nullopt to indicate a
16141 // bad match.
16142 if ((S0IsUnsigned && !(S1IsSigned || S1IsUnsigned)) ||
16143 ((S1IsUnsigned && !(S0IsSigned || S0IsUnsigned))))
16144 return std::nullopt;
16145
16146 llvm_unreachable("Fully covered condition");
16147}
16148
16149SDValue SITargetLowering::performAddCombine(SDNode *N,
16150 DAGCombinerInfo &DCI) const {
16151 SelectionDAG &DAG = DCI.DAG;
16152 EVT VT = N->getValueType(0);
16153 SDLoc SL(N);
16154 SDValue LHS = N->getOperand(0);
16155 SDValue RHS = N->getOperand(1);
16156
16157 if (LHS.getOpcode() == ISD::MUL || RHS.getOpcode() == ISD::MUL) {
16158 if (Subtarget->hasMad64_32()) {
16159 if (SDValue Folded = tryFoldToMad64_32(N, DCI))
16160 return Folded;
16161 }
16162 }
16163
16164 if (SDValue V = reassociateScalarOps(N, DAG)) {
16165 return V;
16166 }
16167
16168 if (VT == MVT::i64) {
16169 if (SDValue Folded = foldAddSub64WithZeroLowBitsTo32(N, DCI))
16170 return Folded;
16171 }
16172
16173 if ((isMul(LHS) || isMul(RHS)) && Subtarget->hasDot7Insts() &&
16174 (Subtarget->hasDot1Insts() || Subtarget->hasDot8Insts())) {
16175 SDValue TempNode(N, 0);
16176 std::optional<bool> IsSigned;
16180
16181 // Match the v_dot4 tree, while collecting src nodes.
16182 int ChainLength = 0;
16183 for (int I = 0; I < 4; I++) {
16184 auto MulIdx = isMul(LHS) ? 0 : isMul(RHS) ? 1 : -1;
16185 if (MulIdx == -1)
16186 break;
16187 auto Src0 = handleMulOperand(TempNode->getOperand(MulIdx)->getOperand(0));
16188 if (!Src0)
16189 break;
16190 auto Src1 = handleMulOperand(TempNode->getOperand(MulIdx)->getOperand(1));
16191 if (!Src1)
16192 break;
16193
16194 auto IterIsSigned = checkDot4MulSignedness(
16195 TempNode->getOperand(MulIdx), *Src0, *Src1,
16196 TempNode->getOperand(MulIdx)->getOperand(0),
16197 TempNode->getOperand(MulIdx)->getOperand(1), DAG);
16198 if (!IterIsSigned)
16199 break;
16200 if (!IsSigned)
16201 IsSigned = *IterIsSigned;
16202 if (*IterIsSigned != *IsSigned)
16203 break;
16204 placeSources(*Src0, *Src1, Src0s, Src1s, I);
16205 auto AddIdx = 1 - MulIdx;
16206 // Allow the special case where add (add (mul24, 0), mul24) became ->
16207 // add (mul24, mul24).
16208 if (I == 2 && isMul(TempNode->getOperand(AddIdx))) {
16209 Src2s.push_back(TempNode->getOperand(AddIdx));
16210 auto Src0 =
16211 handleMulOperand(TempNode->getOperand(AddIdx)->getOperand(0));
16212 if (!Src0)
16213 break;
16214 auto Src1 =
16215 handleMulOperand(TempNode->getOperand(AddIdx)->getOperand(1));
16216 if (!Src1)
16217 break;
16218 auto IterIsSigned = checkDot4MulSignedness(
16219 TempNode->getOperand(AddIdx), *Src0, *Src1,
16220 TempNode->getOperand(AddIdx)->getOperand(0),
16221 TempNode->getOperand(AddIdx)->getOperand(1), DAG);
16222 if (!IterIsSigned)
16223 break;
16224 assert(IsSigned);
16225 if (*IterIsSigned != *IsSigned)
16226 break;
16227 placeSources(*Src0, *Src1, Src0s, Src1s, I + 1);
16228 Src2s.push_back(DAG.getConstant(0, SL, MVT::i32));
16229 ChainLength = I + 2;
16230 break;
16231 }
16232
16233 TempNode = TempNode->getOperand(AddIdx);
16234 Src2s.push_back(TempNode);
16235 ChainLength = I + 1;
16236 if (TempNode->getNumOperands() < 2)
16237 break;
16238 LHS = TempNode->getOperand(0);
16239 RHS = TempNode->getOperand(1);
16240 }
16241
16242 if (ChainLength < 2)
16243 return SDValue();
16244
16245 // Masks were constructed with assumption that we would find a chain of
16246 // length 4. If not, then we need to 0 out the MSB bits (via perm mask of
16247 // 0x0c) so they do not affect dot calculation.
16248 if (ChainLength < 4) {
16249 fixMasks(Src0s, ChainLength);
16250 fixMasks(Src1s, ChainLength);
16251 }
16252
16253 SDValue Src0, Src1;
16254
16255 // If we are just using a single source for both, and have permuted the
16256 // bytes consistently, we can just use the sources without permuting
16257 // (commutation).
16258 bool UseOriginalSrc = false;
16259 if (ChainLength == 4 && Src0s.size() == 1 && Src1s.size() == 1 &&
16260 Src0s.begin()->PermMask == Src1s.begin()->PermMask &&
16261 Src0s.begin()->SrcOp.getValueSizeInBits() >= 32 &&
16262 Src1s.begin()->SrcOp.getValueSizeInBits() >= 32) {
16263 SmallVector<unsigned, 4> SrcBytes;
16264 auto Src0Mask = Src0s.begin()->PermMask;
16265 SrcBytes.push_back(Src0Mask & 0xFF000000);
16266 bool UniqueEntries = true;
16267 for (auto I = 1; I < 4; I++) {
16268 auto NextByte = Src0Mask & (0xFF << ((3 - I) * 8));
16269
16270 if (is_contained(SrcBytes, NextByte)) {
16271 UniqueEntries = false;
16272 break;
16273 }
16274 SrcBytes.push_back(NextByte);
16275 }
16276
16277 if (UniqueEntries) {
16278 UseOriginalSrc = true;
16279
16280 auto *FirstElt = Src0s.begin();
16281 auto FirstEltOp =
16282 getDWordFromOffset(DAG, SL, FirstElt->SrcOp, FirstElt->DWordOffset);
16283
16284 auto *SecondElt = Src1s.begin();
16285 auto SecondEltOp = getDWordFromOffset(DAG, SL, SecondElt->SrcOp,
16286 SecondElt->DWordOffset);
16287
16288 Src0 = DAG.getBitcastedAnyExtOrTrunc(FirstEltOp, SL,
16289 MVT::getIntegerVT(32));
16290 Src1 = DAG.getBitcastedAnyExtOrTrunc(SecondEltOp, SL,
16291 MVT::getIntegerVT(32));
16292 }
16293 }
16294
16295 if (!UseOriginalSrc) {
16296 Src0 = resolveSources(DAG, SL, Src0s, false, true);
16297 Src1 = resolveSources(DAG, SL, Src1s, false, true);
16298 }
16299
16300 assert(IsSigned);
16301 SDValue Src2 =
16302 DAG.getExtOrTrunc(*IsSigned, Src2s[ChainLength - 1], SL, MVT::i32);
16303
16304 SDValue IID = DAG.getTargetConstant(*IsSigned ? Intrinsic::amdgcn_sdot4
16305 : Intrinsic::amdgcn_udot4,
16306 SL, MVT::i64);
16307
16308 assert(!VT.isVector());
16309 auto Dot = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SL, MVT::i32, IID, Src0,
16310 Src1, Src2, DAG.getTargetConstant(0, SL, MVT::i1));
16311
16312 return DAG.getExtOrTrunc(*IsSigned, Dot, SL, VT);
16313 }
16314
16315 if (VT != MVT::i32 || !DCI.isAfterLegalizeDAG())
16316 return SDValue();
16317
16318 // add x, zext (setcc) => uaddo_carry x, 0, setcc
16319 // add x, sext (setcc) => usubo_carry x, 0, setcc
16320 unsigned Opc = LHS.getOpcode();
16323 std::swap(RHS, LHS);
16324
16325 Opc = RHS.getOpcode();
16326 switch (Opc) {
16327 default:
16328 break;
16329 case ISD::ZERO_EXTEND:
16330 case ISD::SIGN_EXTEND:
16331 case ISD::ANY_EXTEND: {
16332 auto Cond = RHS.getOperand(0);
16333 // If this won't be a real VOPC output, we would still need to insert an
16334 // extra instruction anyway.
16335 if (!isBoolSGPR(Cond))
16336 break;
16337 SDVTList VTList = DAG.getVTList(MVT::i32, MVT::i1);
16338 SDValue Args[] = {LHS, DAG.getConstant(0, SL, MVT::i32), Cond};
16340 return DAG.getNode(Opc, SL, VTList, Args);
16341 }
16342 case ISD::UADDO_CARRY: {
16343 // add x, (uaddo_carry y, 0, cc) => uaddo_carry x, y, cc
16344 if (!isNullConstant(RHS.getOperand(1)))
16345 break;
16346 SDValue Args[] = {LHS, RHS.getOperand(0), RHS.getOperand(2)};
16347 return DAG.getNode(ISD::UADDO_CARRY, SDLoc(N), RHS->getVTList(), Args);
16348 }
16349 }
16350 return SDValue();
16351}
16352
16353SDValue SITargetLowering::performPtrAddCombine(SDNode *N,
16354 DAGCombinerInfo &DCI) const {
16355 SelectionDAG &DAG = DCI.DAG;
16356 SDLoc DL(N);
16357 EVT VT = N->getValueType(0);
16358 SDValue N0 = N->getOperand(0);
16359 SDValue N1 = N->getOperand(1);
16360
16361 // The following folds transform PTRADDs into regular arithmetic in cases
16362 // where the PTRADD wouldn't be folded as an immediate offset into memory
16363 // instructions anyway. They are target-specific in that other targets might
16364 // prefer to not lose information about the pointer arithmetic.
16365
16366 // Fold (ptradd x, shl(0 - v, k)) -> sub(x, shl(v, k)).
16367 // Adapted from DAGCombiner::visitADDLikeCommutative.
16368 SDValue V, K;
16369 if (sd_match(N1, m_Shl(m_Neg(m_Value(V)), m_Value(K)))) {
16370 SDNodeFlags ShlFlags = N1->getFlags();
16371 // If the original shl is NUW and NSW, the first k+1 bits of 0-v are all 0,
16372 // so v is either 0 or the first k+1 bits of v are all 1 -> NSW can be
16373 // preserved.
16374 SDNodeFlags NewShlFlags =
16375 ShlFlags.hasNoUnsignedWrap() && ShlFlags.hasNoSignedWrap()
16377 : SDNodeFlags();
16378 SDValue Inner = DAG.getNode(ISD::SHL, DL, VT, V, K, NewShlFlags);
16379 DCI.AddToWorklist(Inner.getNode());
16380 return DAG.getNode(ISD::SUB, DL, VT, N0, Inner);
16381 }
16382
16383 // Fold into Mad64 if the right-hand side is a MUL. Analogous to a fold in
16384 // performAddCombine.
16385 if (N1.getOpcode() == ISD::MUL) {
16386 if (Subtarget->hasMad64_32()) {
16387 if (SDValue Folded = tryFoldToMad64_32(N, DCI))
16388 return Folded;
16389 }
16390 }
16391
16392 // If the 32 low bits of the constant are all zero, there is nothing to fold
16393 // into an immediate offset, so it's better to eliminate the unnecessary
16394 // addition for the lower 32 bits than to preserve the PTRADD.
16395 // Analogous to a fold in performAddCombine.
16396 if (VT == MVT::i64) {
16397 if (SDValue Folded = foldAddSub64WithZeroLowBitsTo32(N, DCI))
16398 return Folded;
16399 }
16400
16401 if (N1.getOpcode() != ISD::ADD || !N1.hasOneUse())
16402 return SDValue();
16403
16404 SDValue X = N0;
16405 SDValue Y = N1.getOperand(0);
16406 SDValue Z = N1.getOperand(1);
16407 bool YIsConstant = DAG.isConstantIntBuildVectorOrConstantInt(Y);
16408 bool ZIsConstant = DAG.isConstantIntBuildVectorOrConstantInt(Z);
16409
16410 if (!YIsConstant && !ZIsConstant && !X->isDivergent() &&
16411 Y->isDivergent() != Z->isDivergent()) {
16412 // Reassociate (ptradd x, (add y, z)) -> (ptradd (ptradd x, y), z) if x and
16413 // y are uniform and z isn't.
16414 // Reassociate (ptradd x, (add y, z)) -> (ptradd (ptradd x, z), y) if x and
16415 // z are uniform and y isn't.
16416 // The goal is to push uniform operands up in the computation, so that they
16417 // can be handled with scalar operations. We can't use reassociateScalarOps
16418 // for this since it requires two identical commutative operations to
16419 // reassociate.
16420 if (Y->isDivergent())
16421 std::swap(Y, Z);
16422 // If both additions in the original were NUW, reassociation preserves that.
16423 SDNodeFlags ReassocFlags =
16424 (N->getFlags() & N1->getFlags()) & SDNodeFlags::NoUnsignedWrap;
16425 SDValue UniformInner = DAG.getMemBasePlusOffset(X, Y, DL, ReassocFlags);
16426 DCI.AddToWorklist(UniformInner.getNode());
16427 return DAG.getMemBasePlusOffset(UniformInner, Z, DL, ReassocFlags);
16428 }
16429
16430 return SDValue();
16431}
16432
16433SDValue SITargetLowering::performSubCombine(SDNode *N,
16434 DAGCombinerInfo &DCI) const {
16435 SelectionDAG &DAG = DCI.DAG;
16436 EVT VT = N->getValueType(0);
16437
16438 if (VT == MVT::i64) {
16439 if (SDValue Folded = foldAddSub64WithZeroLowBitsTo32(N, DCI))
16440 return Folded;
16441 }
16442
16443 if (VT != MVT::i32)
16444 return SDValue();
16445
16446 SDLoc SL(N);
16447 SDValue LHS = N->getOperand(0);
16448 SDValue RHS = N->getOperand(1);
16449
16450 // sub x, zext (setcc) => usubo_carry x, 0, setcc
16451 // sub x, sext (setcc) => uaddo_carry x, 0, setcc
16452 unsigned Opc = RHS.getOpcode();
16453 switch (Opc) {
16454 default:
16455 break;
16456 case ISD::ZERO_EXTEND:
16457 case ISD::SIGN_EXTEND:
16458 case ISD::ANY_EXTEND: {
16459 auto Cond = RHS.getOperand(0);
16460 // If this won't be a real VOPC output, we would still need to insert an
16461 // extra instruction anyway.
16462 if (!isBoolSGPR(Cond))
16463 break;
16464 SDVTList VTList = DAG.getVTList(MVT::i32, MVT::i1);
16465 SDValue Args[] = {LHS, DAG.getConstant(0, SL, MVT::i32), Cond};
16467 return DAG.getNode(Opc, SL, VTList, Args);
16468 }
16469 }
16470
16471 if (LHS.getOpcode() == ISD::USUBO_CARRY) {
16472 // sub (usubo_carry x, 0, cc), y => usubo_carry x, y, cc
16473 if (!isNullConstant(LHS.getOperand(1)))
16474 return SDValue();
16475 SDValue Args[] = {LHS.getOperand(0), RHS, LHS.getOperand(2)};
16476 return DAG.getNode(ISD::USUBO_CARRY, SDLoc(N), LHS->getVTList(), Args);
16477 }
16478 return SDValue();
16479}
16480
16481SDValue
16482SITargetLowering::performAddCarrySubCarryCombine(SDNode *N,
16483 DAGCombinerInfo &DCI) const {
16484
16485 if (N->getValueType(0) != MVT::i32)
16486 return SDValue();
16487
16488 if (!isNullConstant(N->getOperand(1)))
16489 return SDValue();
16490
16491 SelectionDAG &DAG = DCI.DAG;
16492 SDValue LHS = N->getOperand(0);
16493
16494 // uaddo_carry (add x, y), 0, cc => uaddo_carry x, y, cc
16495 // usubo_carry (sub x, y), 0, cc => usubo_carry x, y, cc
16496 unsigned LHSOpc = LHS.getOpcode();
16497 unsigned Opc = N->getOpcode();
16498 if ((LHSOpc == ISD::ADD && Opc == ISD::UADDO_CARRY) ||
16499 (LHSOpc == ISD::SUB && Opc == ISD::USUBO_CARRY)) {
16500 SDValue Args[] = {LHS.getOperand(0), LHS.getOperand(1), N->getOperand(2)};
16501 return DAG.getNode(Opc, SDLoc(N), N->getVTList(), Args);
16502 }
16503 return SDValue();
16504}
16505
16506SDValue SITargetLowering::performFAddCombine(SDNode *N,
16507 DAGCombinerInfo &DCI) const {
16508 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
16509 return SDValue();
16510
16511 SelectionDAG &DAG = DCI.DAG;
16512 EVT VT = N->getValueType(0);
16513
16514 SDLoc SL(N);
16515 SDValue LHS = N->getOperand(0);
16516 SDValue RHS = N->getOperand(1);
16517
16518 // These should really be instruction patterns, but writing patterns with
16519 // source modifiers is a pain.
16520
16521 // fadd (fadd (a, a), b) -> mad 2.0, a, b
16522 if (LHS.getOpcode() == ISD::FADD) {
16523 SDValue A = LHS.getOperand(0);
16524 if (A == LHS.getOperand(1)) {
16525 unsigned FusedOp = getFusedOpcode(DAG, N, LHS.getNode());
16526 if (FusedOp != 0) {
16527 const SDValue Two = DAG.getConstantFP(2.0, SL, VT);
16528 return DAG.getNode(FusedOp, SL, VT, A, Two, RHS);
16529 }
16530 }
16531 }
16532
16533 // fadd (b, fadd (a, a)) -> mad 2.0, a, b
16534 if (RHS.getOpcode() == ISD::FADD) {
16535 SDValue A = RHS.getOperand(0);
16536 if (A == RHS.getOperand(1)) {
16537 unsigned FusedOp = getFusedOpcode(DAG, N, RHS.getNode());
16538 if (FusedOp != 0) {
16539 const SDValue Two = DAG.getConstantFP(2.0, SL, VT);
16540 return DAG.getNode(FusedOp, SL, VT, A, Two, LHS);
16541 }
16542 }
16543 }
16544
16545 return SDValue();
16546}
16547
16548SDValue SITargetLowering::performFSubCombine(SDNode *N,
16549 DAGCombinerInfo &DCI) const {
16550 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
16551 return SDValue();
16552
16553 SelectionDAG &DAG = DCI.DAG;
16554 SDLoc SL(N);
16555 EVT VT = N->getValueType(0);
16556 assert(!VT.isVector());
16557
16558 // Try to get the fneg to fold into the source modifier. This undoes generic
16559 // DAG combines and folds them into the mad.
16560 //
16561 // Only do this if we are not trying to support denormals. v_mad_f32 does
16562 // not support denormals ever.
16563 SDValue LHS = N->getOperand(0);
16564 SDValue RHS = N->getOperand(1);
16565 if (LHS.getOpcode() == ISD::FADD) {
16566 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c)
16567 SDValue A = LHS.getOperand(0);
16568 if (A == LHS.getOperand(1)) {
16569 unsigned FusedOp = getFusedOpcode(DAG, N, LHS.getNode());
16570 if (FusedOp != 0) {
16571 const SDValue Two = DAG.getConstantFP(2.0, SL, VT);
16572 SDValue NegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
16573
16574 return DAG.getNode(FusedOp, SL, VT, A, Two, NegRHS);
16575 }
16576 }
16577 }
16578
16579 if (RHS.getOpcode() == ISD::FADD) {
16580 // (fsub c, (fadd a, a)) -> mad -2.0, a, c
16581
16582 SDValue A = RHS.getOperand(0);
16583 if (A == RHS.getOperand(1)) {
16584 unsigned FusedOp = getFusedOpcode(DAG, N, RHS.getNode());
16585 if (FusedOp != 0) {
16586 const SDValue NegTwo = DAG.getConstantFP(-2.0, SL, VT);
16587 return DAG.getNode(FusedOp, SL, VT, A, NegTwo, LHS);
16588 }
16589 }
16590 }
16591
16592 return SDValue();
16593}
16594
16595SDValue SITargetLowering::performFDivCombine(SDNode *N,
16596 DAGCombinerInfo &DCI) const {
16597 SelectionDAG &DAG = DCI.DAG;
16598 SDLoc SL(N);
16599 EVT VT = N->getValueType(0);
16600
16601 // fsqrt legality correlates to rsq availability.
16602 if ((VT != MVT::f16 && VT != MVT::bf16) || !isOperationLegal(ISD::FSQRT, VT))
16603 return SDValue();
16604
16605 SDValue LHS = N->getOperand(0);
16606 SDValue RHS = N->getOperand(1);
16607
16608 SDNodeFlags Flags = N->getFlags();
16609 SDNodeFlags RHSFlags = RHS->getFlags();
16610 if (!Flags.hasAllowContract() || !RHSFlags.hasAllowContract() ||
16611 !RHS->hasOneUse())
16612 return SDValue();
16613
16614 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
16615 bool IsNegative = false;
16616 if (CLHS->isExactlyValue(1.0) ||
16617 (IsNegative = CLHS->isExactlyValue(-1.0))) {
16618 // fdiv contract 1.0, (sqrt contract x) -> rsq for f16
16619 // fdiv contract -1.0, (sqrt contract x) -> fneg(rsq) for f16
16620 if (RHS.getOpcode() == ISD::FSQRT) {
16621 // TODO: Or in RHS flags, somehow missing from SDNodeFlags
16622 SDValue Rsq =
16623 DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0), Flags);
16624 return IsNegative ? DAG.getNode(ISD::FNEG, SL, VT, Rsq, Flags) : Rsq;
16625 }
16626 }
16627 }
16628
16629 return SDValue();
16630}
16631
16632SDValue SITargetLowering::performFMulCombine(SDNode *N,
16633 DAGCombinerInfo &DCI) const {
16634 SelectionDAG &DAG = DCI.DAG;
16635 EVT VT = N->getValueType(0);
16636 EVT ScalarVT = VT.getScalarType();
16637 EVT IntVT = VT.changeElementType(*DAG.getContext(), MVT::i32);
16638
16639 if (!N->isDivergent() && getSubtarget()->hasSALUFloatInsts() &&
16640 (ScalarVT == MVT::f32 || ScalarVT == MVT::f16)) {
16641 // Prefer to use s_mul_f16/f32 instead of v_ldexp_f16/f32.
16642 return SDValue();
16643 }
16644
16645 SDValue LHS = N->getOperand(0);
16646 SDValue RHS = N->getOperand(1);
16647
16648 // It is cheaper to realize i32 inline constants as compared against
16649 // materializing f16 or f64 (or even non-inline f32) values,
16650 // possible via ldexp usage, as shown below :
16651 //
16652 // Given : A = 2^a & B = 2^b ; where a and b are integers.
16653 // fmul x, (select y, A, B) -> ldexp( x, (select i32 y, a, b) )
16654 // fmul x, (select y, -A, -B) -> ldexp( (fneg x), (select i32 y, a, b) )
16655 if ((ScalarVT == MVT::f64 || ScalarVT == MVT::f32 || ScalarVT == MVT::f16) &&
16656 (RHS.hasOneUse() && RHS.getOpcode() == ISD::SELECT)) {
16657 const ConstantFPSDNode *TrueNode = isConstOrConstSplatFP(RHS.getOperand(1));
16658 if (!TrueNode)
16659 return SDValue();
16660 const ConstantFPSDNode *FalseNode =
16661 isConstOrConstSplatFP(RHS.getOperand(2));
16662 if (!FalseNode)
16663 return SDValue();
16664
16665 if (TrueNode->isNegative() != FalseNode->isNegative())
16666 return SDValue();
16667
16668 // For f32, only non-inline constants should be transformed.
16669 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
16670 if (ScalarVT == MVT::f32 &&
16671 TII->isInlineConstant(TrueNode->getValueAPF()) &&
16672 TII->isInlineConstant(FalseNode->getValueAPF()))
16673 return SDValue();
16674
16675 int TrueNodeExpVal = TrueNode->getValueAPF().getExactLog2Abs();
16676 if (TrueNodeExpVal == INT_MIN)
16677 return SDValue();
16678 int FalseNodeExpVal = FalseNode->getValueAPF().getExactLog2Abs();
16679 if (FalseNodeExpVal == INT_MIN)
16680 return SDValue();
16681
16682 SDLoc SL(N);
16683 SDValue SelectNode =
16684 DAG.getNode(ISD::SELECT, SL, IntVT, RHS.getOperand(0),
16685 DAG.getSignedConstant(TrueNodeExpVal, SL, IntVT),
16686 DAG.getSignedConstant(FalseNodeExpVal, SL, IntVT));
16687
16688 LHS = TrueNode->isNegative()
16689 ? DAG.getNode(ISD::FNEG, SL, VT, LHS, LHS->getFlags())
16690 : LHS;
16691
16692 return DAG.getNode(ISD::FLDEXP, SL, VT, LHS, SelectNode, N->getFlags());
16693 }
16694
16695 return SDValue();
16696}
16697
16698SDValue SITargetLowering::performFMACombine(SDNode *N,
16699 DAGCombinerInfo &DCI) const {
16700 SelectionDAG &DAG = DCI.DAG;
16701 EVT VT = N->getValueType(0);
16702 SDLoc SL(N);
16703
16704 if (!Subtarget->hasDot10Insts() || VT != MVT::f32)
16705 return SDValue();
16706
16707 // FMA((F32)S0.x, (F32)S1. x, FMA((F32)S0.y, (F32)S1.y, (F32)z)) ->
16708 // FDOT2((V2F16)S0, (V2F16)S1, (F32)z))
16709 SDValue Op1 = N->getOperand(0);
16710 SDValue Op2 = N->getOperand(1);
16711 SDValue FMA = N->getOperand(2);
16712
16713 if (FMA.getOpcode() != ISD::FMA || Op1.getOpcode() != ISD::FP_EXTEND ||
16714 Op2.getOpcode() != ISD::FP_EXTEND)
16715 return SDValue();
16716
16717 // fdot2_f32_f16 always flushes fp32 denormal operand and output to zero,
16718 // regardless of the denorm mode setting. Therefore,
16719 // fp-contract is sufficient to allow generating fdot2.
16720 const TargetOptions &Options = DAG.getTarget().Options;
16721 if (Options.AllowFPOpFusion == FPOpFusion::Fast ||
16722 (N->getFlags().hasAllowContract() &&
16723 FMA->getFlags().hasAllowContract())) {
16724 Op1 = Op1.getOperand(0);
16725 Op2 = Op2.getOperand(0);
16726 if (Op1.getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
16728 return SDValue();
16729
16730 SDValue Vec1 = Op1.getOperand(0);
16731 SDValue Idx1 = Op1.getOperand(1);
16732 SDValue Vec2 = Op2.getOperand(0);
16733
16734 SDValue FMAOp1 = FMA.getOperand(0);
16735 SDValue FMAOp2 = FMA.getOperand(1);
16736 SDValue FMAAcc = FMA.getOperand(2);
16737
16738 if (FMAOp1.getOpcode() != ISD::FP_EXTEND ||
16739 FMAOp2.getOpcode() != ISD::FP_EXTEND)
16740 return SDValue();
16741
16742 FMAOp1 = FMAOp1.getOperand(0);
16743 FMAOp2 = FMAOp2.getOperand(0);
16744 if (FMAOp1.getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
16746 return SDValue();
16747
16748 SDValue Vec3 = FMAOp1.getOperand(0);
16749 SDValue Vec4 = FMAOp2.getOperand(0);
16750 SDValue Idx2 = FMAOp1.getOperand(1);
16751
16752 if (Idx1 != Op2.getOperand(1) || Idx2 != FMAOp2.getOperand(1) ||
16753 // Idx1 and Idx2 cannot be the same.
16754 Idx1 == Idx2)
16755 return SDValue();
16756
16757 if (Vec1 == Vec2 || Vec3 == Vec4)
16758 return SDValue();
16759
16760 if (Vec1.getValueType() != MVT::v2f16 || Vec2.getValueType() != MVT::v2f16)
16761 return SDValue();
16762
16763 if ((Vec1 == Vec3 && Vec2 == Vec4) || (Vec1 == Vec4 && Vec2 == Vec3)) {
16764 return DAG.getNode(AMDGPUISD::FDOT2, SL, MVT::f32, Vec1, Vec2, FMAAcc,
16765 DAG.getTargetConstant(0, SL, MVT::i1));
16766 }
16767 }
16768 return SDValue();
16769}
16770
16771SDValue SITargetLowering::performSetCCCombine(SDNode *N,
16772 DAGCombinerInfo &DCI) const {
16773 SelectionDAG &DAG = DCI.DAG;
16774 SDLoc SL(N);
16775
16776 SDValue LHS = N->getOperand(0);
16777 SDValue RHS = N->getOperand(1);
16778 EVT VT = LHS.getValueType();
16779 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
16780
16781 auto *CRHS = dyn_cast<ConstantSDNode>(RHS);
16782 if (!CRHS) {
16784 if (CRHS) {
16785 std::swap(LHS, RHS);
16786 CC = getSetCCSwappedOperands(CC);
16787 }
16788 }
16789
16790 if (CRHS) {
16791 if (VT == MVT::i32 && LHS.getOpcode() == ISD::SIGN_EXTEND &&
16792 isBoolSGPR(LHS.getOperand(0))) {
16793 // setcc (sext from i1 cc), -1, ne|sgt|ult) => not cc => xor cc, -1
16794 // setcc (sext from i1 cc), -1, eq|sle|uge) => cc
16795 // setcc (sext from i1 cc), 0, eq|sge|ule) => not cc => xor cc, -1
16796 // setcc (sext from i1 cc), 0, ne|ugt|slt) => cc
16797 if ((CRHS->isAllOnes() &&
16798 (CC == ISD::SETNE || CC == ISD::SETGT || CC == ISD::SETULT)) ||
16799 (CRHS->isZero() &&
16800 (CC == ISD::SETEQ || CC == ISD::SETGE || CC == ISD::SETULE)))
16801 return DAG.getNode(ISD::XOR, SL, MVT::i1, LHS.getOperand(0),
16802 DAG.getAllOnesConstant(SL, MVT::i1));
16803 if ((CRHS->isAllOnes() &&
16804 (CC == ISD::SETEQ || CC == ISD::SETLE || CC == ISD::SETUGE)) ||
16805 (CRHS->isZero() &&
16806 (CC == ISD::SETNE || CC == ISD::SETUGT || CC == ISD::SETLT)))
16807 return LHS.getOperand(0);
16808 }
16809
16810 const APInt &CRHSVal = CRHS->getAPIntValue();
16811 if ((CC == ISD::SETEQ || CC == ISD::SETNE) &&
16812 LHS.getOpcode() == ISD::SELECT &&
16813 isa<ConstantSDNode>(LHS.getOperand(1)) &&
16814 isa<ConstantSDNode>(LHS.getOperand(2)) &&
16815 LHS.getConstantOperandVal(1) != LHS.getConstantOperandVal(2) &&
16816 isBoolSGPR(LHS.getOperand(0))) {
16817 // Given CT != FT:
16818 // setcc (select cc, CT, CF), CF, eq => xor cc, -1
16819 // setcc (select cc, CT, CF), CF, ne => cc
16820 // setcc (select cc, CT, CF), CT, ne => xor cc, -1
16821 // setcc (select cc, CT, CF), CT, eq => cc
16822 const APInt &CT = LHS.getConstantOperandAPInt(1);
16823 const APInt &CF = LHS.getConstantOperandAPInt(2);
16824
16825 if ((CF == CRHSVal && CC == ISD::SETEQ) ||
16826 (CT == CRHSVal && CC == ISD::SETNE))
16827 return DAG.getNode(ISD::XOR, SL, MVT::i1, LHS.getOperand(0),
16828 DAG.getAllOnesConstant(SL, MVT::i1));
16829 if ((CF == CRHSVal && CC == ISD::SETNE) ||
16830 (CT == CRHSVal && CC == ISD::SETEQ))
16831 return LHS.getOperand(0);
16832 }
16833 }
16834
16835 // Eliminate setcc by using carryout from add/sub instruction
16836
16837 // LHS = ADD i64 RHS, Z LHSlo = UADDO i32 RHSlo, Zlo
16838 // setcc LHS ult RHS -> LHSHi = UADDO_CARRY i32 RHShi, Zhi
16839 // similarly for subtraction
16840
16841 // LHS = ADD i64 Y, 1 LHSlo = UADDO i32 Ylo, 1
16842 // setcc LHS eq 0 -> LHSHi = UADDO_CARRY i32 Yhi, 0
16843
16844 if (VT == MVT::i64 && ((CC == ISD::SETULT &&
16846 (CC == ISD::SETUGT &&
16848 (CC == ISD::SETEQ && CRHS && CRHS->isZero() &&
16849 sd_match(LHS, m_Add(m_Value(), m_One()))))) {
16850 bool IsAdd = LHS.getOpcode() == ISD::ADD;
16851
16852 SDValue Op0 = LHS.getOperand(0);
16853 SDValue Op1 = LHS.getOperand(1);
16854
16855 SDValue Op0Lo = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, Op0);
16856 SDValue Op1Lo = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, Op1);
16857
16858 SDValue Op0Hi = getHiHalf64(Op0, DAG);
16859 SDValue Op1Hi = getHiHalf64(Op1, DAG);
16860
16861 SDValue NodeLo =
16862 DAG.getNode(IsAdd ? ISD::UADDO : ISD::USUBO, SL,
16863 DAG.getVTList(MVT::i32, MVT::i1), {Op0Lo, Op1Lo});
16864
16865 SDValue CarryInHi = NodeLo.getValue(1);
16866 SDValue NodeHi = DAG.getNode(IsAdd ? ISD::UADDO_CARRY : ISD::USUBO_CARRY,
16867 SL, DAG.getVTList(MVT::i32, MVT::i1),
16868 {Op0Hi, Op1Hi, CarryInHi});
16869
16870 SDValue ResultLo = NodeLo.getValue(0);
16871 SDValue ResultHi = NodeHi.getValue(0);
16872
16873 SDValue JoinedResult =
16874 DAG.getBuildVector(MVT::v2i32, SL, {ResultLo, ResultHi});
16875
16876 SDValue Result = DAG.getNode(ISD::BITCAST, SL, VT, JoinedResult);
16877 SDValue Overflow = NodeHi.getValue(1);
16878 DCI.CombineTo(LHS.getNode(), Result);
16879 return Overflow;
16880 }
16881
16882 if (VT != MVT::f32 && VT != MVT::f64 &&
16883 (!Subtarget->has16BitInsts() || VT != MVT::f16))
16884 return SDValue();
16885
16886 // Match isinf/isfinite pattern
16887 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity))
16888 // (fcmp one (fabs x), inf) -> (fp_class x,
16889 // (p_normal | n_normal | p_subnormal | n_subnormal | p_zero | n_zero)
16890 if ((CC == ISD::SETOEQ || CC == ISD::SETONE) &&
16891 LHS.getOpcode() == ISD::FABS) {
16892 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
16893 if (!CRHS)
16894 return SDValue();
16895
16896 const APFloat &APF = CRHS->getValueAPF();
16897 if (APF.isInfinity() && !APF.isNegative()) {
16898 const unsigned IsInfMask =
16900 const unsigned IsFiniteMask =
16904 unsigned Mask = CC == ISD::SETOEQ ? IsInfMask : IsFiniteMask;
16905 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0),
16906 DAG.getConstant(Mask, SL, MVT::i32));
16907 }
16908 }
16909
16910 return SDValue();
16911}
16912
16913SDValue
16914SITargetLowering::performCvtF32UByteNCombine(SDNode *N,
16915 DAGCombinerInfo &DCI) const {
16916 SelectionDAG &DAG = DCI.DAG;
16917 SDLoc SL(N);
16918 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
16919
16920 SDValue Src = N->getOperand(0);
16921 SDValue Shift = N->getOperand(0);
16922
16923 // TODO: Extend type shouldn't matter (assuming legal types).
16924 if (Shift.getOpcode() == ISD::ZERO_EXTEND)
16925 Shift = Shift.getOperand(0);
16926
16927 if (Shift.getOpcode() == ISD::SRL || Shift.getOpcode() == ISD::SHL) {
16928 // cvt_f32_ubyte1 (shl x, 8) -> cvt_f32_ubyte0 x
16929 // cvt_f32_ubyte3 (shl x, 16) -> cvt_f32_ubyte1 x
16930 // cvt_f32_ubyte0 (srl x, 16) -> cvt_f32_ubyte2 x
16931 // cvt_f32_ubyte1 (srl x, 16) -> cvt_f32_ubyte3 x
16932 // cvt_f32_ubyte0 (srl x, 8) -> cvt_f32_ubyte1 x
16933 if (auto *C = dyn_cast<ConstantSDNode>(Shift.getOperand(1))) {
16934 SDValue Shifted = DAG.getZExtOrTrunc(
16935 Shift.getOperand(0), SDLoc(Shift.getOperand(0)), MVT::i32);
16936
16937 unsigned ShiftOffset = 8 * Offset;
16938 if (Shift.getOpcode() == ISD::SHL)
16939 ShiftOffset -= C->getZExtValue();
16940 else
16941 ShiftOffset += C->getZExtValue();
16942
16943 if (ShiftOffset < 32 && (ShiftOffset % 8) == 0) {
16944 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0 + ShiftOffset / 8, SL,
16945 MVT::f32, Shifted);
16946 }
16947 }
16948 }
16949
16950 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16951 APInt DemandedBits = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
16952 if (TLI.SimplifyDemandedBits(Src, DemandedBits, DCI)) {
16953 // We simplified Src. If this node is not dead, visit it again so it is
16954 // folded properly.
16955 if (N->getOpcode() != ISD::DELETED_NODE)
16956 DCI.AddToWorklist(N);
16957 return SDValue(N, 0);
16958 }
16959
16960 // Handle (or x, (srl y, 8)) pattern when known bits are zero.
16961 if (SDValue DemandedSrc =
16962 TLI.SimplifyMultipleUseDemandedBits(Src, DemandedBits, DAG))
16963 return DAG.getNode(N->getOpcode(), SL, MVT::f32, DemandedSrc);
16964
16965 return SDValue();
16966}
16967
16968SDValue SITargetLowering::performClampCombine(SDNode *N,
16969 DAGCombinerInfo &DCI) const {
16970 ConstantFPSDNode *CSrc = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
16971 if (!CSrc)
16972 return SDValue();
16973
16974 const MachineFunction &MF = DCI.DAG.getMachineFunction();
16975 const APFloat &F = CSrc->getValueAPF();
16976 APFloat Zero = APFloat::getZero(F.getSemantics());
16977 if (F < Zero ||
16978 (F.isNaN() && MF.getInfo<SIMachineFunctionInfo>()->getMode().DX10Clamp)) {
16979 return DCI.DAG.getConstantFP(Zero, SDLoc(N), N->getValueType(0));
16980 }
16981
16982 APFloat One(F.getSemantics(), "1.0");
16983 if (F > One)
16984 return DCI.DAG.getConstantFP(One, SDLoc(N), N->getValueType(0));
16985
16986 return SDValue(CSrc, 0);
16987}
16988
16989SDValue SITargetLowering::performSelectCombine(SDNode *N,
16990 DAGCombinerInfo &DCI) const {
16991
16992 // Try to fold CMP + SELECT patterns with shared constants (both FP and
16993 // integer).
16994 // Detect when CMP and SELECT use the same constant and fold them to avoid
16995 // loading the constant twice. Specifically handles patterns like:
16996 // %cmp = icmp eq i32 %val, 4242
16997 // %sel = select i1 %cmp, i32 4242, i32 %other
16998 // It can be optimized to reuse %val instead of 4242 in select.
16999 SDValue Cond = N->getOperand(0);
17000 SDValue TrueVal = N->getOperand(1);
17001 SDValue FalseVal = N->getOperand(2);
17002
17003 // Check if condition is a comparison.
17004 if (Cond.getOpcode() != ISD::SETCC)
17005 return SDValue();
17006
17007 SDValue LHS = Cond.getOperand(0);
17008 SDValue RHS = Cond.getOperand(1);
17009 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
17010
17011 bool isFloatingPoint = LHS.getValueType().isFloatingPoint();
17012 bool isInteger = LHS.getValueType().isInteger();
17013
17014 // Handle simple floating-point and integer types only.
17015 if (!isFloatingPoint && !isInteger)
17016 return SDValue();
17017
17018 bool isEquality = CC == (isFloatingPoint ? ISD::SETOEQ : ISD::SETEQ);
17019 bool isNonEquality = CC == (isFloatingPoint ? ISD::SETONE : ISD::SETNE);
17020 if (!isEquality && !isNonEquality)
17021 return SDValue();
17022
17023 SDValue ArgVal, ConstVal;
17024 if ((isFloatingPoint && isa<ConstantFPSDNode>(RHS)) ||
17025 (isInteger && isa<ConstantSDNode>(RHS))) {
17026 ConstVal = RHS;
17027 ArgVal = LHS;
17028 } else if ((isFloatingPoint && isa<ConstantFPSDNode>(LHS)) ||
17029 (isInteger && isa<ConstantSDNode>(LHS))) {
17030 ConstVal = LHS;
17031 ArgVal = RHS;
17032 } else {
17033 return SDValue();
17034 }
17035
17036 // Skip optimization for inlinable immediates.
17037 if (isFloatingPoint) {
17038 const APFloat &Val = cast<ConstantFPSDNode>(ConstVal)->getValueAPF();
17039 if (!Val.isNormal() || Subtarget->getInstrInfo()->isInlineConstant(Val))
17040 return SDValue();
17041 } else {
17043 cast<ConstantSDNode>(ConstVal)->getSExtValue()))
17044 return SDValue();
17045 }
17046
17047 // For equality and non-equality comparisons, patterns:
17048 // select (setcc x, const), const, y -> select (setcc x, const), x, y
17049 // select (setccinv x, const), y, const -> select (setccinv x, const), y, x
17050 if (!(isEquality && TrueVal == ConstVal) &&
17051 !(isNonEquality && FalseVal == ConstVal))
17052 return SDValue();
17053
17054 SDValue SelectLHS = (isEquality && TrueVal == ConstVal) ? ArgVal : TrueVal;
17055 SDValue SelectRHS =
17056 (isNonEquality && FalseVal == ConstVal) ? ArgVal : FalseVal;
17057 return DCI.DAG.getNode(ISD::SELECT, SDLoc(N), N->getValueType(0), Cond,
17058 SelectLHS, SelectRHS);
17059}
17060
17062 DAGCombinerInfo &DCI) const {
17063 switch (N->getOpcode()) {
17064 case ISD::ADD:
17065 case ISD::SUB:
17066 case ISD::SHL:
17067 case ISD::SRL:
17068 case ISD::SRA:
17069 case ISD::AND:
17070 case ISD::OR:
17071 case ISD::XOR:
17072 case ISD::MUL:
17073 case ISD::SETCC:
17074 case ISD::SELECT:
17075 case ISD::SMIN:
17076 case ISD::SMAX:
17077 case ISD::UMIN:
17078 case ISD::UMAX:
17079 if (auto Res = promoteUniformOpToI32(SDValue(N, 0), DCI))
17080 return Res;
17081 break;
17082 default:
17083 break;
17084 }
17085
17086 if (getTargetMachine().getOptLevel() == CodeGenOptLevel::None)
17087 return SDValue();
17088
17089 switch (N->getOpcode()) {
17090 case ISD::ADD:
17091 return performAddCombine(N, DCI);
17092 case ISD::PTRADD:
17093 return performPtrAddCombine(N, DCI);
17094 case ISD::SUB:
17095 return performSubCombine(N, DCI);
17096 case ISD::UADDO_CARRY:
17097 case ISD::USUBO_CARRY:
17098 return performAddCarrySubCarryCombine(N, DCI);
17099 case ISD::FADD:
17100 return performFAddCombine(N, DCI);
17101 case ISD::FSUB:
17102 return performFSubCombine(N, DCI);
17103 case ISD::FDIV:
17104 return performFDivCombine(N, DCI);
17105 case ISD::FMUL:
17106 return performFMulCombine(N, DCI);
17107 case ISD::SETCC:
17108 return performSetCCCombine(N, DCI);
17109 case ISD::SELECT:
17110 if (auto Res = performSelectCombine(N, DCI))
17111 return Res;
17112 break;
17113 case ISD::FMAXNUM:
17114 case ISD::FMINNUM:
17115 case ISD::FMAXNUM_IEEE:
17116 case ISD::FMINNUM_IEEE:
17117 case ISD::FMAXIMUM:
17118 case ISD::FMINIMUM:
17119 case ISD::FMAXIMUMNUM:
17120 case ISD::FMINIMUMNUM:
17121 case ISD::SMAX:
17122 case ISD::SMIN:
17123 case ISD::UMAX:
17124 case ISD::UMIN:
17125 case AMDGPUISD::FMIN_LEGACY:
17126 case AMDGPUISD::FMAX_LEGACY:
17127 return performMinMaxCombine(N, DCI);
17128 case ISD::FMA:
17129 return performFMACombine(N, DCI);
17130 case ISD::AND:
17131 return performAndCombine(N, DCI);
17132 case ISD::OR:
17133 return performOrCombine(N, DCI);
17134 case ISD::FSHR: {
17136 if (N->getValueType(0) == MVT::i32 && N->isDivergent() &&
17137 TII->pseudoToMCOpcode(AMDGPU::V_PERM_B32_e64) != -1) {
17138 return matchPERM(N, DCI);
17139 }
17140 break;
17141 }
17142 case ISD::XOR:
17143 return performXorCombine(N, DCI);
17144 case ISD::ZERO_EXTEND:
17145 return performZeroExtendCombine(N, DCI);
17147 return performSignExtendInRegCombine(N, DCI);
17148 case AMDGPUISD::FP_CLASS:
17149 return performClassCombine(N, DCI);
17150 case ISD::FCANONICALIZE:
17151 return performFCanonicalizeCombine(N, DCI);
17152 case AMDGPUISD::RCP:
17153 return performRcpCombine(N, DCI);
17154 case ISD::FLDEXP:
17155 case AMDGPUISD::FRACT:
17156 case AMDGPUISD::RSQ:
17157 case AMDGPUISD::RCP_LEGACY:
17158 case AMDGPUISD::RCP_IFLAG:
17159 case AMDGPUISD::RSQ_CLAMP: {
17160 // FIXME: This is probably wrong. If src is an sNaN, it won't be quieted
17161 SDValue Src = N->getOperand(0);
17162 if (Src.isUndef())
17163 return Src;
17164 break;
17165 }
17166 case ISD::SINT_TO_FP:
17167 case ISD::UINT_TO_FP:
17168 return performUCharToFloatCombine(N, DCI);
17169 case ISD::FCOPYSIGN:
17170 return performFCopySignCombine(N, DCI);
17171 case AMDGPUISD::CVT_F32_UBYTE0:
17172 case AMDGPUISD::CVT_F32_UBYTE1:
17173 case AMDGPUISD::CVT_F32_UBYTE2:
17174 case AMDGPUISD::CVT_F32_UBYTE3:
17175 return performCvtF32UByteNCombine(N, DCI);
17176 case AMDGPUISD::FMED3:
17177 return performFMed3Combine(N, DCI);
17178 case AMDGPUISD::CVT_PKRTZ_F16_F32:
17179 return performCvtPkRTZCombine(N, DCI);
17180 case AMDGPUISD::CLAMP:
17181 return performClampCombine(N, DCI);
17182 case ISD::SCALAR_TO_VECTOR: {
17183 SelectionDAG &DAG = DCI.DAG;
17184 EVT VT = N->getValueType(0);
17185
17186 // v2i16 (scalar_to_vector i16:x) -> v2i16 (bitcast (any_extend i16:x))
17187 if (VT == MVT::v2i16 || VT == MVT::v2f16 || VT == MVT::v2bf16) {
17188 SDLoc SL(N);
17189 SDValue Src = N->getOperand(0);
17190 EVT EltVT = Src.getValueType();
17191 if (EltVT != MVT::i16)
17192 Src = DAG.getNode(ISD::BITCAST, SL, MVT::i16, Src);
17193
17194 SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SL, MVT::i32, Src);
17195 return DAG.getNode(ISD::BITCAST, SL, VT, Ext);
17196 }
17197
17198 break;
17199 }
17201 return performExtractVectorEltCombine(N, DCI);
17203 return performInsertVectorEltCombine(N, DCI);
17204 case ISD::FP_ROUND:
17205 return performFPRoundCombine(N, DCI);
17206 case ISD::LOAD: {
17207 if (SDValue Widened = widenLoad(cast<LoadSDNode>(N), DCI))
17208 return Widened;
17209 [[fallthrough]];
17210 }
17211 default: {
17212 if (!DCI.isBeforeLegalize()) {
17213 if (MemSDNode *MemNode = dyn_cast<MemSDNode>(N))
17214 return performMemSDNodeCombine(MemNode, DCI);
17215 }
17216
17217 break;
17218 }
17219 }
17220
17222}
17223
17224/// Helper function for adjustWritemask
17225static unsigned SubIdx2Lane(unsigned Idx) {
17226 switch (Idx) {
17227 default:
17228 return ~0u;
17229 case AMDGPU::sub0:
17230 return 0;
17231 case AMDGPU::sub1:
17232 return 1;
17233 case AMDGPU::sub2:
17234 return 2;
17235 case AMDGPU::sub3:
17236 return 3;
17237 case AMDGPU::sub4:
17238 return 4; // Possible with TFE/LWE
17239 }
17240}
17241
17242/// Adjust the writemask of MIMG, VIMAGE or VSAMPLE instructions
17243SDNode *SITargetLowering::adjustWritemask(MachineSDNode *&Node,
17244 SelectionDAG &DAG) const {
17245 unsigned Opcode = Node->getMachineOpcode();
17246
17247 // Subtract 1 because the vdata output is not a MachineSDNode operand.
17248 int D16Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::d16) - 1;
17249 if (D16Idx >= 0 && Node->getConstantOperandVal(D16Idx))
17250 return Node; // not implemented for D16
17251
17252 SDNode *Users[5] = {nullptr};
17253 unsigned Lane = 0;
17254 unsigned DmaskIdx =
17255 AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::dmask) - 1;
17256 unsigned OldDmask = Node->getConstantOperandVal(DmaskIdx);
17257 unsigned NewDmask = 0;
17258 unsigned TFEIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::tfe) - 1;
17259 unsigned LWEIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::lwe) - 1;
17260 bool UsesTFC = (int(TFEIdx) >= 0 && Node->getConstantOperandVal(TFEIdx)) ||
17261 (int(LWEIdx) >= 0 && Node->getConstantOperandVal(LWEIdx));
17262 unsigned TFCLane = 0;
17263 bool HasChain = Node->getNumValues() > 1;
17264
17265 if (OldDmask == 0) {
17266 // These are folded out, but on the chance it happens don't assert.
17267 return Node;
17268 }
17269
17270 unsigned OldBitsSet = llvm::popcount(OldDmask);
17271 // Work out which is the TFE/LWE lane if that is enabled.
17272 if (UsesTFC) {
17273 TFCLane = OldBitsSet;
17274 }
17275
17276 // Try to figure out the used register components
17277 for (SDUse &Use : Node->uses()) {
17278
17279 // Don't look at users of the chain.
17280 if (Use.getResNo() != 0)
17281 continue;
17282
17283 SDNode *User = Use.getUser();
17284
17285 // Abort if we can't understand the usage
17286 if (!User->isMachineOpcode() ||
17287 User->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
17288 return Node;
17289
17290 // Lane means which subreg of %vgpra_vgprb_vgprc_vgprd is used.
17291 // Note that subregs are packed, i.e. Lane==0 is the first bit set
17292 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
17293 // set, etc.
17294 Lane = SubIdx2Lane(User->getConstantOperandVal(1));
17295 if (Lane == ~0u)
17296 return Node;
17297
17298 // Check if the use is for the TFE/LWE generated result at VGPRn+1.
17299 if (UsesTFC && Lane == TFCLane) {
17300 Users[Lane] = User;
17301 } else {
17302 // Set which texture component corresponds to the lane.
17303 unsigned Comp;
17304 for (unsigned i = 0, Dmask = OldDmask; (i <= Lane) && (Dmask != 0); i++) {
17305 Comp = llvm::countr_zero(Dmask);
17306 Dmask &= ~(1 << Comp);
17307 }
17308
17309 // Abort if we have more than one user per component.
17310 if (Users[Lane])
17311 return Node;
17312
17313 Users[Lane] = User;
17314 NewDmask |= 1 << Comp;
17315 }
17316 }
17317
17318 // Don't allow 0 dmask, as hardware assumes one channel enabled.
17319 bool NoChannels = !NewDmask;
17320 if (NoChannels) {
17321 if (!UsesTFC) {
17322 // No uses of the result and not using TFC. Then do nothing.
17323 return Node;
17324 }
17325 // If the original dmask has one channel - then nothing to do
17326 if (OldBitsSet == 1)
17327 return Node;
17328 // Use an arbitrary dmask - required for the instruction to work
17329 NewDmask = 1;
17330 }
17331 // Abort if there's no change
17332 if (NewDmask == OldDmask)
17333 return Node;
17334
17335 unsigned BitsSet = llvm::popcount(NewDmask);
17336
17337 // Check for TFE or LWE - increase the number of channels by one to account
17338 // for the extra return value
17339 // This will need adjustment for D16 if this is also included in
17340 // adjustWriteMask (this function) but at present D16 are excluded.
17341 unsigned NewChannels = BitsSet + UsesTFC;
17342
17343 int NewOpcode =
17344 AMDGPU::getMaskedMIMGOp(Node->getMachineOpcode(), NewChannels);
17345 assert(NewOpcode != -1 &&
17346 NewOpcode != static_cast<int>(Node->getMachineOpcode()) &&
17347 "failed to find equivalent MIMG op");
17348
17349 // Adjust the writemask in the node
17351 llvm::append_range(Ops, Node->ops().take_front(DmaskIdx));
17352 Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32));
17353 llvm::append_range(Ops, Node->ops().drop_front(DmaskIdx + 1));
17354
17355 MVT SVT = Node->getValueType(0).getVectorElementType().getSimpleVT();
17356
17357 MVT ResultVT = NewChannels == 1
17358 ? SVT
17359 : MVT::getVectorVT(SVT, NewChannels == 3 ? 4
17360 : NewChannels == 5 ? 8
17361 : NewChannels);
17362 SDVTList NewVTList =
17363 HasChain ? DAG.getVTList(ResultVT, MVT::Other) : DAG.getVTList(ResultVT);
17364
17365 MachineSDNode *NewNode =
17366 DAG.getMachineNode(NewOpcode, SDLoc(Node), NewVTList, Ops);
17367
17368 if (HasChain) {
17369 // Update chain.
17370 DAG.setNodeMemRefs(NewNode, Node->memoperands());
17371 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(NewNode, 1));
17372 }
17373
17374 if (NewChannels == 1) {
17375 assert(Node->hasNUsesOfValue(1, 0));
17376 SDNode *Copy =
17377 DAG.getMachineNode(TargetOpcode::COPY, SDLoc(Node),
17378 Users[Lane]->getValueType(0), SDValue(NewNode, 0));
17379 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
17380 return nullptr;
17381 }
17382
17383 // Update the users of the node with the new indices
17384 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 5; ++i) {
17385 SDNode *User = Users[i];
17386 if (!User) {
17387 // Handle the special case of NoChannels. We set NewDmask to 1 above, but
17388 // Users[0] is still nullptr because channel 0 doesn't really have a use.
17389 if (i || !NoChannels)
17390 continue;
17391 } else {
17392 SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32);
17393 SDNode *NewUser = DAG.UpdateNodeOperands(User, SDValue(NewNode, 0), Op);
17394 if (NewUser != User) {
17395 DAG.ReplaceAllUsesWith(SDValue(User, 0), SDValue(NewUser, 0));
17396 DAG.RemoveDeadNode(User);
17397 }
17398 }
17399
17400 switch (Idx) {
17401 default:
17402 break;
17403 case AMDGPU::sub0:
17404 Idx = AMDGPU::sub1;
17405 break;
17406 case AMDGPU::sub1:
17407 Idx = AMDGPU::sub2;
17408 break;
17409 case AMDGPU::sub2:
17410 Idx = AMDGPU::sub3;
17411 break;
17412 case AMDGPU::sub3:
17413 Idx = AMDGPU::sub4;
17414 break;
17415 }
17416 }
17417
17418 DAG.RemoveDeadNode(Node);
17419 return nullptr;
17420}
17421
17423 if (Op.getOpcode() == ISD::AssertZext)
17424 Op = Op.getOperand(0);
17425
17426 return isa<FrameIndexSDNode>(Op);
17427}
17428
17429/// Legalize target independent instructions (e.g. INSERT_SUBREG)
17430/// with frame index operands.
17431/// LLVM assumes that inputs are to these instructions are registers.
17432SDNode *
17434 SelectionDAG &DAG) const {
17435 if (Node->getOpcode() == ISD::CopyToReg) {
17436 RegisterSDNode *DestReg = cast<RegisterSDNode>(Node->getOperand(1));
17437 SDValue SrcVal = Node->getOperand(2);
17438
17439 // Insert a copy to a VReg_1 virtual register so LowerI1Copies doesn't have
17440 // to try understanding copies to physical registers.
17441 if (SrcVal.getValueType() == MVT::i1 && DestReg->getReg().isPhysical()) {
17442 SDLoc SL(Node);
17444 SDValue VReg = DAG.getRegister(
17445 MRI.createVirtualRegister(&AMDGPU::VReg_1RegClass), MVT::i1);
17446
17447 SDNode *Glued = Node->getGluedNode();
17448 SDValue ToVReg = DAG.getCopyToReg(
17449 Node->getOperand(0), SL, VReg, SrcVal,
17450 SDValue(Glued, Glued ? Glued->getNumValues() - 1 : 0));
17451 SDValue ToResultReg = DAG.getCopyToReg(ToVReg, SL, SDValue(DestReg, 0),
17452 VReg, ToVReg.getValue(1));
17453 DAG.ReplaceAllUsesWith(Node, ToResultReg.getNode());
17454 DAG.RemoveDeadNode(Node);
17455 return ToResultReg.getNode();
17456 }
17457 }
17458
17460 for (unsigned i = 0; i < Node->getNumOperands(); ++i) {
17461 if (!isFrameIndexOp(Node->getOperand(i))) {
17462 Ops.push_back(Node->getOperand(i));
17463 continue;
17464 }
17465
17466 SDLoc DL(Node);
17467 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL,
17468 Node->getOperand(i).getValueType(),
17469 Node->getOperand(i)),
17470 0));
17471 }
17472
17473 return DAG.UpdateNodeOperands(Node, Ops);
17474}
17475
17476/// Fold the instructions after selecting them.
17477/// Returns null if users were already updated.
17479 SelectionDAG &DAG) const {
17481 unsigned Opcode = Node->getMachineOpcode();
17482
17483 if (TII->isImage(Opcode) && !TII->get(Opcode).mayStore() &&
17484 !TII->isGather4(Opcode) &&
17485 AMDGPU::hasNamedOperand(Opcode, AMDGPU::OpName::dmask)) {
17486 return adjustWritemask(Node, DAG);
17487 }
17488
17489 if (Opcode == AMDGPU::INSERT_SUBREG || Opcode == AMDGPU::REG_SEQUENCE) {
17491 return Node;
17492 }
17493
17494 switch (Opcode) {
17495 case AMDGPU::V_DIV_SCALE_F32_e64:
17496 case AMDGPU::V_DIV_SCALE_F64_e64: {
17497 // Satisfy the operand register constraint when one of the inputs is
17498 // undefined. Ordinarily each undef value will have its own implicit_def of
17499 // a vreg, so force these to use a single register.
17500 SDValue Src0 = Node->getOperand(1);
17501 SDValue Src1 = Node->getOperand(3);
17502 SDValue Src2 = Node->getOperand(5);
17503
17504 if ((Src0.isMachineOpcode() &&
17505 Src0.getMachineOpcode() != AMDGPU::IMPLICIT_DEF) &&
17506 (Src0 == Src1 || Src0 == Src2))
17507 break;
17508
17509 MVT VT = Src0.getValueType().getSimpleVT();
17510 const TargetRegisterClass *RC =
17511 getRegClassFor(VT, Src0.getNode()->isDivergent());
17512
17514 SDValue UndefReg = DAG.getRegister(MRI.createVirtualRegister(RC), VT);
17515
17516 SDValue ImpDef = DAG.getCopyToReg(DAG.getEntryNode(), SDLoc(Node), UndefReg,
17517 Src0, SDValue());
17518
17519 // src0 must be the same register as src1 or src2, even if the value is
17520 // undefined, so make sure we don't violate this constraint.
17521 if (Src0.isMachineOpcode() &&
17522 Src0.getMachineOpcode() == AMDGPU::IMPLICIT_DEF) {
17523 if (Src1.isMachineOpcode() &&
17524 Src1.getMachineOpcode() != AMDGPU::IMPLICIT_DEF)
17525 Src0 = Src1;
17526 else if (Src2.isMachineOpcode() &&
17527 Src2.getMachineOpcode() != AMDGPU::IMPLICIT_DEF)
17528 Src0 = Src2;
17529 else {
17530 assert(Src1.getMachineOpcode() == AMDGPU::IMPLICIT_DEF);
17531 Src0 = UndefReg;
17532 Src1 = UndefReg;
17533 }
17534 } else
17535 break;
17536
17538 Ops[1] = Src0;
17539 Ops[3] = Src1;
17540 Ops[5] = Src2;
17541 Ops.push_back(ImpDef.getValue(1));
17542 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
17543 }
17544 default:
17545 break;
17546 }
17547
17548 return Node;
17549}
17550
17551// Any MIMG instructions that use tfe or lwe require an initialization of the
17552// result register that will be written in the case of a memory access failure.
17553// The required code is also added to tie this init code to the result of the
17554// img instruction.
17557 const SIRegisterInfo &TRI = TII->getRegisterInfo();
17558 MachineRegisterInfo &MRI = MI.getMF()->getRegInfo();
17559 MachineBasicBlock &MBB = *MI.getParent();
17560
17561 int DstIdx =
17562 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::vdata);
17563 unsigned InitIdx = 0;
17564
17565 if (TII->isImage(MI)) {
17566 MachineOperand *TFE = TII->getNamedOperand(MI, AMDGPU::OpName::tfe);
17567 MachineOperand *LWE = TII->getNamedOperand(MI, AMDGPU::OpName::lwe);
17568 MachineOperand *D16 = TII->getNamedOperand(MI, AMDGPU::OpName::d16);
17569
17570 if (!TFE && !LWE) // intersect_ray
17571 return;
17572
17573 unsigned TFEVal = TFE ? TFE->getImm() : 0;
17574 unsigned LWEVal = LWE ? LWE->getImm() : 0;
17575 unsigned D16Val = D16 ? D16->getImm() : 0;
17576
17577 if (!TFEVal && !LWEVal)
17578 return;
17579
17580 // At least one of TFE or LWE are non-zero
17581 // We have to insert a suitable initialization of the result value and
17582 // tie this to the dest of the image instruction.
17583
17584 // Calculate which dword we have to initialize to 0.
17585 MachineOperand *MO_Dmask = TII->getNamedOperand(MI, AMDGPU::OpName::dmask);
17586
17587 // check that dmask operand is found.
17588 assert(MO_Dmask && "Expected dmask operand in instruction");
17589
17590 unsigned dmask = MO_Dmask->getImm();
17591 // Determine the number of active lanes taking into account the
17592 // Gather4 special case
17593 unsigned ActiveLanes = TII->isGather4(MI) ? 4 : llvm::popcount(dmask);
17594
17595 bool Packed = !Subtarget->hasUnpackedD16VMem();
17596
17597 InitIdx = D16Val && Packed ? ((ActiveLanes + 1) >> 1) + 1 : ActiveLanes + 1;
17598
17599 // Abandon attempt if the dst size isn't large enough
17600 // - this is in fact an error but this is picked up elsewhere and
17601 // reported correctly.
17602 const TargetRegisterClass *DstRC = TII->getRegClass(MI.getDesc(), DstIdx);
17603
17604 uint32_t DstSize = TRI.getRegSizeInBits(*DstRC) / 32;
17605 if (DstSize < InitIdx)
17606 return;
17607 } else if (TII->isMUBUF(MI) && AMDGPU::getMUBUFTfe(MI.getOpcode())) {
17608 const TargetRegisterClass *DstRC = TII->getRegClass(MI.getDesc(), DstIdx);
17609 InitIdx = TRI.getRegSizeInBits(*DstRC) / 32;
17610 } else {
17611 return;
17612 }
17613
17614 const DebugLoc &DL = MI.getDebugLoc();
17615
17616 // Create a register for the initialization value.
17617 Register PrevDst = MRI.cloneVirtualRegister(MI.getOperand(DstIdx).getReg());
17618 unsigned NewDst = 0; // Final initialized value will be in here
17619
17620 // If PRTStrictNull feature is enabled (the default) then initialize
17621 // all the result registers to 0, otherwise just the error indication
17622 // register (VGPRn+1)
17623 unsigned SizeLeft = Subtarget->usePRTStrictNull() ? InitIdx : 1;
17624 unsigned CurrIdx = Subtarget->usePRTStrictNull() ? 0 : (InitIdx - 1);
17625
17626 BuildMI(MBB, MI, DL, TII->get(AMDGPU::IMPLICIT_DEF), PrevDst);
17627 for (; SizeLeft; SizeLeft--, CurrIdx++) {
17628 NewDst = MRI.createVirtualRegister(TII->getOpRegClass(MI, DstIdx));
17629 // Initialize dword
17630 Register SubReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
17631 // clang-format off
17632 BuildMI(MBB, MI, DL, TII->get(AMDGPU::V_MOV_B32_e32), SubReg)
17633 .addImm(0);
17634 // clang-format on
17635 // Insert into the super-reg
17636 BuildMI(MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewDst)
17637 .addReg(PrevDst)
17638 .addReg(SubReg)
17640
17641 PrevDst = NewDst;
17642 }
17643
17644 // Add as an implicit operand
17645 MI.addOperand(MachineOperand::CreateReg(NewDst, false, true));
17646
17647 // Tie the just added implicit operand to the dst
17648 MI.tieOperands(DstIdx, MI.getNumOperands() - 1);
17649}
17650
17651/// Assign the register class depending on the number of
17652/// bits set in the writemask
17654 SDNode *Node) const {
17656
17657 MachineFunction *MF = MI.getMF();
17659
17660 if (TII->isVOP3(MI.getOpcode())) {
17661 // Make sure constant bus requirements are respected.
17662 TII->legalizeOperandsVOP3(MRI, MI);
17663
17664 if (TII->isMAI(MI)) {
17665 // The ordinary src0, src1, src2 were legalized above.
17666 //
17667 // We have to also legalize the appended v_mfma_ld_scale_b32 operands,
17668 // as a separate instruction.
17669 int Src0Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
17670 AMDGPU::OpName::scale_src0);
17671 if (Src0Idx != -1) {
17672 int Src1Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
17673 AMDGPU::OpName::scale_src1);
17674 if (TII->usesConstantBus(MRI, MI, Src0Idx) &&
17675 TII->usesConstantBus(MRI, MI, Src1Idx))
17676 TII->legalizeOpWithMove(MI, Src1Idx);
17677 }
17678 }
17679
17680 return;
17681 }
17682
17683 if (TII->isImage(MI))
17684 TII->enforceOperandRCAlignment(MI, AMDGPU::OpName::vaddr);
17685}
17686
17688 uint64_t Val) {
17689 SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32);
17690 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0);
17691}
17692
17694 const SDLoc &DL,
17695 SDValue Ptr) const {
17697
17698 // Build the half of the subregister with the constants before building the
17699 // full 128-bit register. If we are building multiple resource descriptors,
17700 // this will allow CSEing of the 2-component register.
17701 const SDValue Ops0[] = {
17702 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32),
17703 buildSMovImm32(DAG, DL, 0),
17704 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
17705 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32),
17706 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32)};
17707
17708 SDValue SubRegHi = SDValue(
17709 DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v2i32, Ops0), 0);
17710
17711 // Combine the constants and the pointer.
17712 const SDValue Ops1[] = {
17713 DAG.getTargetConstant(AMDGPU::SGPR_128RegClassID, DL, MVT::i32), Ptr,
17714 DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32), SubRegHi,
17715 DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32)};
17716
17717 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1);
17718}
17719
17720/// Return a resource descriptor with the 'Add TID' bit enabled
17721/// The TID (Thread ID) is multiplied by the stride value (bits [61:48]
17722/// of the resource descriptor) to create an offset, which is added to
17723/// the resource pointer.
17725 SDValue Ptr, uint32_t RsrcDword1,
17726 uint64_t RsrcDword2And3) const {
17727 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
17728 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
17729 if (RsrcDword1) {
17730 PtrHi =
17731 SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi,
17732 DAG.getConstant(RsrcDword1, DL, MVT::i32)),
17733 0);
17734 }
17735
17736 SDValue DataLo =
17737 buildSMovImm32(DAG, DL, RsrcDword2And3 & UINT64_C(0xFFFFFFFF));
17738 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32);
17739
17740 const SDValue Ops[] = {
17741 DAG.getTargetConstant(AMDGPU::SGPR_128RegClassID, DL, MVT::i32),
17742 PtrLo,
17743 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
17744 PtrHi,
17745 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32),
17746 DataLo,
17747 DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32),
17748 DataHi,
17749 DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32)};
17750
17751 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
17752}
17753
17754//===----------------------------------------------------------------------===//
17755// SI Inline Assembly Support
17756//===----------------------------------------------------------------------===//
17757
17758std::pair<unsigned, const TargetRegisterClass *>
17760 StringRef Constraint,
17761 MVT VT) const {
17762 const SIRegisterInfo *TRI = static_cast<const SIRegisterInfo *>(TRI_);
17763
17764 const TargetRegisterClass *RC = nullptr;
17765 if (Constraint.size() == 1) {
17766 // Check if we cannot determine the bit size of the given value type. This
17767 // can happen, for example, in this situation where we have an empty struct
17768 // (size 0): `call void asm "", "v"({} poison)`-
17769 if (VT == MVT::Other)
17770 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
17771 const unsigned BitWidth = VT.getSizeInBits();
17772 switch (Constraint[0]) {
17773 default:
17774 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
17775 case 's':
17776 case 'r':
17777 switch (BitWidth) {
17778 case 16:
17779 RC = &AMDGPU::SReg_32RegClass;
17780 break;
17781 case 64:
17782 RC = &AMDGPU::SGPR_64RegClass;
17783 break;
17784 default:
17786 if (!RC)
17787 return std::pair(0U, nullptr);
17788 break;
17789 }
17790 break;
17791 case 'v':
17792 switch (BitWidth) {
17793 case 1:
17794 return std::pair(0U, nullptr);
17795 case 16:
17796 RC = Subtarget->useRealTrue16Insts() ? &AMDGPU::VGPR_16RegClass
17797 : &AMDGPU::VGPR_32_Lo256RegClass;
17798 break;
17799 default:
17800 RC = Subtarget->has1024AddressableVGPRs()
17801 ? TRI->getAlignedLo256VGPRClassForBitWidth(BitWidth)
17802 : TRI->getVGPRClassForBitWidth(BitWidth);
17803 if (!RC)
17804 return std::pair(0U, nullptr);
17805 break;
17806 }
17807 break;
17808 case 'a':
17809 if (!Subtarget->hasMAIInsts())
17810 break;
17811 switch (BitWidth) {
17812 case 1:
17813 return std::pair(0U, nullptr);
17814 case 16:
17815 RC = &AMDGPU::AGPR_32RegClass;
17816 break;
17817 default:
17818 RC = TRI->getAGPRClassForBitWidth(BitWidth);
17819 if (!RC)
17820 return std::pair(0U, nullptr);
17821 break;
17822 }
17823 break;
17824 }
17825 } else if (Constraint == "VA" && Subtarget->hasGFX90AInsts()) {
17826 const unsigned BitWidth = VT.getSizeInBits();
17827 switch (BitWidth) {
17828 case 16:
17829 RC = &AMDGPU::AV_32RegClass;
17830 break;
17831 default:
17832 RC = TRI->getVectorSuperClassForBitWidth(BitWidth);
17833 if (!RC)
17834 return std::pair(0U, nullptr);
17835 break;
17836 }
17837 }
17838
17839 // We actually support i128, i16 and f16 as inline parameters
17840 // even if they are not reported as legal
17841 if (RC && (isTypeLegal(VT) || VT.SimpleTy == MVT::i128 ||
17842 VT.SimpleTy == MVT::i16 || VT.SimpleTy == MVT::f16))
17843 return std::pair(0U, RC);
17844
17845 auto [Kind, Idx, NumRegs] = AMDGPU::parseAsmConstraintPhysReg(Constraint);
17846 if (Kind != '\0') {
17847 if (Kind == 'v') {
17848 RC = &AMDGPU::VGPR_32_Lo256RegClass;
17849 } else if (Kind == 's') {
17850 RC = &AMDGPU::SGPR_32RegClass;
17851 } else if (Kind == 'a') {
17852 RC = &AMDGPU::AGPR_32RegClass;
17853 }
17854
17855 if (RC) {
17856 if (NumRegs > 1) {
17857 if (Idx >= RC->getNumRegs() || Idx + NumRegs - 1 >= RC->getNumRegs())
17858 return std::pair(0U, nullptr);
17859
17860 uint32_t Width = NumRegs * 32;
17861 // Prohibit constraints for register ranges with a width that does not
17862 // match the required type.
17863 if (VT.SimpleTy != MVT::Other && Width != VT.getSizeInBits())
17864 return std::pair(0U, nullptr);
17865
17866 MCRegister Reg = RC->getRegister(Idx);
17868 RC = TRI->getVGPRClassForBitWidth(Width);
17869 else if (SIRegisterInfo::isSGPRClass(RC))
17870 RC = TRI->getSGPRClassForBitWidth(Width);
17871 else if (SIRegisterInfo::isAGPRClass(RC))
17872 RC = TRI->getAGPRClassForBitWidth(Width);
17873 if (RC) {
17874 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0, RC);
17875 if (!Reg) {
17876 // The register class does not contain the requested register,
17877 // e.g., because it is an SGPR pair that would violate alignment
17878 // requirements.
17879 return std::pair(0U, nullptr);
17880 }
17881 return std::pair(Reg, RC);
17882 }
17883 }
17884
17885 // Check for lossy scalar/vector conversions.
17886 if (VT.isVector() && VT.getSizeInBits() != 32)
17887 return std::pair(0U, nullptr);
17888 if (Idx < RC->getNumRegs())
17889 return std::pair(RC->getRegister(Idx), RC);
17890 return std::pair(0U, nullptr);
17891 }
17892 }
17893
17894 auto Ret = TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
17895 if (Ret.first)
17896 Ret.second = TRI->getPhysRegBaseClass(Ret.first);
17897
17898 return Ret;
17899}
17900
17901static bool isImmConstraint(StringRef Constraint) {
17902 if (Constraint.size() == 1) {
17903 switch (Constraint[0]) {
17904 default:
17905 break;
17906 case 'I':
17907 case 'J':
17908 case 'A':
17909 case 'B':
17910 case 'C':
17911 return true;
17912 }
17913 } else if (Constraint == "DA" || Constraint == "DB") {
17914 return true;
17915 }
17916 return false;
17917}
17918
17921 if (Constraint.size() == 1) {
17922 switch (Constraint[0]) {
17923 default:
17924 break;
17925 case 's':
17926 case 'v':
17927 case 'a':
17928 return C_RegisterClass;
17929 }
17930 } else if (Constraint.size() == 2) {
17931 if (Constraint == "VA")
17932 return C_RegisterClass;
17933 }
17934 if (isImmConstraint(Constraint)) {
17935 return C_Other;
17936 }
17937 return TargetLowering::getConstraintType(Constraint);
17938}
17939
17940static uint64_t clearUnusedBits(uint64_t Val, unsigned Size) {
17942 Val = Val & maskTrailingOnes<uint64_t>(Size);
17943 }
17944 return Val;
17945}
17946
17948 StringRef Constraint,
17949 std::vector<SDValue> &Ops,
17950 SelectionDAG &DAG) const {
17951 if (isImmConstraint(Constraint)) {
17952 uint64_t Val;
17953 if (getAsmOperandConstVal(Op, Val) &&
17954 checkAsmConstraintVal(Op, Constraint, Val)) {
17955 Val = clearUnusedBits(Val, Op.getScalarValueSizeInBits());
17956 Ops.push_back(DAG.getTargetConstant(Val, SDLoc(Op), MVT::i64));
17957 }
17958 } else {
17960 }
17961}
17962
17964 unsigned Size = Op.getScalarValueSizeInBits();
17965 if (Size > 64)
17966 return false;
17967
17968 if (Size == 16 && !Subtarget->has16BitInsts())
17969 return false;
17970
17972 Val = C->getSExtValue();
17973 return true;
17974 }
17976 Val = C->getValueAPF().bitcastToAPInt().getSExtValue();
17977 return true;
17978 }
17980 if (Size != 16 || Op.getNumOperands() != 2)
17981 return false;
17982 if (Op.getOperand(0).isUndef() || Op.getOperand(1).isUndef())
17983 return false;
17984 if (ConstantSDNode *C = V->getConstantSplatNode()) {
17985 Val = C->getSExtValue();
17986 return true;
17987 }
17988 if (ConstantFPSDNode *C = V->getConstantFPSplatNode()) {
17989 Val = C->getValueAPF().bitcastToAPInt().getSExtValue();
17990 return true;
17991 }
17992 }
17993
17994 return false;
17995}
17996
17998 uint64_t Val) const {
17999 if (Constraint.size() == 1) {
18000 switch (Constraint[0]) {
18001 case 'I':
18003 case 'J':
18004 return isInt<16>(Val);
18005 case 'A':
18006 return checkAsmConstraintValA(Op, Val);
18007 case 'B':
18008 return isInt<32>(Val);
18009 case 'C':
18010 return isUInt<32>(clearUnusedBits(Val, Op.getScalarValueSizeInBits())) ||
18012 default:
18013 break;
18014 }
18015 } else if (Constraint.size() == 2) {
18016 if (Constraint == "DA") {
18017 int64_t HiBits = static_cast<int32_t>(Val >> 32);
18018 int64_t LoBits = static_cast<int32_t>(Val);
18019 return checkAsmConstraintValA(Op, HiBits, 32) &&
18020 checkAsmConstraintValA(Op, LoBits, 32);
18021 }
18022 if (Constraint == "DB") {
18023 return true;
18024 }
18025 }
18026 llvm_unreachable("Invalid asm constraint");
18027}
18028
18030 unsigned MaxSize) const {
18031 unsigned Size = std::min<unsigned>(Op.getScalarValueSizeInBits(), MaxSize);
18032 bool HasInv2Pi = Subtarget->hasInv2PiInlineImm();
18033 if (Size == 16) {
18034 MVT VT = Op.getSimpleValueType();
18035 switch (VT.SimpleTy) {
18036 default:
18037 return false;
18038 case MVT::i16:
18039 return AMDGPU::isInlinableLiteralI16(Val, HasInv2Pi);
18040 case MVT::f16:
18041 return AMDGPU::isInlinableLiteralFP16(Val, HasInv2Pi);
18042 case MVT::bf16:
18043 return AMDGPU::isInlinableLiteralBF16(Val, HasInv2Pi);
18044 case MVT::v2i16:
18045 return AMDGPU::getInlineEncodingV2I16(Val).has_value();
18046 case MVT::v2f16:
18047 return AMDGPU::getInlineEncodingV2F16(Val).has_value();
18048 case MVT::v2bf16:
18049 return AMDGPU::getInlineEncodingV2BF16(Val).has_value();
18050 }
18051 }
18052 if ((Size == 32 && AMDGPU::isInlinableLiteral32(Val, HasInv2Pi)) ||
18053 (Size == 64 && AMDGPU::isInlinableLiteral64(Val, HasInv2Pi)))
18054 return true;
18055 return false;
18056}
18057
18058static int getAlignedAGPRClassID(unsigned UnalignedClassID) {
18059 switch (UnalignedClassID) {
18060 case AMDGPU::VReg_64RegClassID:
18061 return AMDGPU::VReg_64_Align2RegClassID;
18062 case AMDGPU::VReg_96RegClassID:
18063 return AMDGPU::VReg_96_Align2RegClassID;
18064 case AMDGPU::VReg_128RegClassID:
18065 return AMDGPU::VReg_128_Align2RegClassID;
18066 case AMDGPU::VReg_160RegClassID:
18067 return AMDGPU::VReg_160_Align2RegClassID;
18068 case AMDGPU::VReg_192RegClassID:
18069 return AMDGPU::VReg_192_Align2RegClassID;
18070 case AMDGPU::VReg_224RegClassID:
18071 return AMDGPU::VReg_224_Align2RegClassID;
18072 case AMDGPU::VReg_256RegClassID:
18073 return AMDGPU::VReg_256_Align2RegClassID;
18074 case AMDGPU::VReg_288RegClassID:
18075 return AMDGPU::VReg_288_Align2RegClassID;
18076 case AMDGPU::VReg_320RegClassID:
18077 return AMDGPU::VReg_320_Align2RegClassID;
18078 case AMDGPU::VReg_352RegClassID:
18079 return AMDGPU::VReg_352_Align2RegClassID;
18080 case AMDGPU::VReg_384RegClassID:
18081 return AMDGPU::VReg_384_Align2RegClassID;
18082 case AMDGPU::VReg_512RegClassID:
18083 return AMDGPU::VReg_512_Align2RegClassID;
18084 case AMDGPU::VReg_1024RegClassID:
18085 return AMDGPU::VReg_1024_Align2RegClassID;
18086 case AMDGPU::AReg_64RegClassID:
18087 return AMDGPU::AReg_64_Align2RegClassID;
18088 case AMDGPU::AReg_96RegClassID:
18089 return AMDGPU::AReg_96_Align2RegClassID;
18090 case AMDGPU::AReg_128RegClassID:
18091 return AMDGPU::AReg_128_Align2RegClassID;
18092 case AMDGPU::AReg_160RegClassID:
18093 return AMDGPU::AReg_160_Align2RegClassID;
18094 case AMDGPU::AReg_192RegClassID:
18095 return AMDGPU::AReg_192_Align2RegClassID;
18096 case AMDGPU::AReg_256RegClassID:
18097 return AMDGPU::AReg_256_Align2RegClassID;
18098 case AMDGPU::AReg_512RegClassID:
18099 return AMDGPU::AReg_512_Align2RegClassID;
18100 case AMDGPU::AReg_1024RegClassID:
18101 return AMDGPU::AReg_1024_Align2RegClassID;
18102 default:
18103 return -1;
18104 }
18105}
18106
18107// Figure out which registers should be reserved for stack access. Only after
18108// the function is legalized do we know all of the non-spill stack objects or if
18109// calls are present.
18113 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
18114 const SIRegisterInfo *TRI = Subtarget->getRegisterInfo();
18115 const SIInstrInfo *TII = ST.getInstrInfo();
18116
18117 if (Info->isEntryFunction()) {
18118 // Callable functions have fixed registers used for stack access.
18120 }
18121
18122 // TODO: Move this logic to getReservedRegs()
18123 // Reserve the SGPR(s) to save/restore EXEC for WWM spill/copy handling.
18124 unsigned MaxNumSGPRs = ST.getMaxNumSGPRs(MF);
18125 Register SReg = ST.isWave32()
18126 ? AMDGPU::SGPR_32RegClass.getRegister(MaxNumSGPRs - 1)
18127 : TRI->getAlignedHighSGPRForRC(MF, /*Align=*/2,
18128 &AMDGPU::SGPR_64RegClass);
18129 Info->setSGPRForEXECCopy(SReg);
18130
18131 assert(!TRI->isSubRegister(Info->getScratchRSrcReg(),
18132 Info->getStackPtrOffsetReg()));
18133 if (Info->getStackPtrOffsetReg() != AMDGPU::SP_REG)
18134 MRI.replaceRegWith(AMDGPU::SP_REG, Info->getStackPtrOffsetReg());
18135
18136 // We need to worry about replacing the default register with itself in case
18137 // of MIR testcases missing the MFI.
18138 if (Info->getScratchRSrcReg() != AMDGPU::PRIVATE_RSRC_REG)
18139 MRI.replaceRegWith(AMDGPU::PRIVATE_RSRC_REG, Info->getScratchRSrcReg());
18140
18141 if (Info->getFrameOffsetReg() != AMDGPU::FP_REG)
18142 MRI.replaceRegWith(AMDGPU::FP_REG, Info->getFrameOffsetReg());
18143
18144 Info->limitOccupancy(MF);
18145
18146 if (ST.isWave32() && !MF.empty()) {
18147 for (auto &MBB : MF) {
18148 for (auto &MI : MBB) {
18149 TII->fixImplicitOperands(MI);
18150 }
18151 }
18152 }
18153
18154 // FIXME: This is a hack to fixup AGPR classes to use the properly aligned
18155 // classes if required. Ideally the register class constraints would differ
18156 // per-subtarget, but there's no easy way to achieve that right now. This is
18157 // not a problem for VGPRs because the correctly aligned VGPR class is implied
18158 // from using them as the register class for legal types.
18159 if (ST.needsAlignedVGPRs()) {
18160 for (unsigned I = 0, E = MRI.getNumVirtRegs(); I != E; ++I) {
18161 const Register Reg = Register::index2VirtReg(I);
18162 const TargetRegisterClass *RC = MRI.getRegClassOrNull(Reg);
18163 if (!RC)
18164 continue;
18165 int NewClassID = getAlignedAGPRClassID(RC->getID());
18166 if (NewClassID != -1)
18167 MRI.setRegClass(Reg, TRI->getRegClass(NewClassID));
18168 }
18169 }
18170
18172}
18173
18175 KnownBits &Known,
18176 const APInt &DemandedElts,
18177 const SelectionDAG &DAG,
18178 unsigned Depth) const {
18179 Known.resetAll();
18180 unsigned Opc = Op.getOpcode();
18181 switch (Opc) {
18183 unsigned IID = Op.getConstantOperandVal(0);
18184 switch (IID) {
18185 case Intrinsic::amdgcn_mbcnt_lo:
18186 case Intrinsic::amdgcn_mbcnt_hi: {
18187 const GCNSubtarget &ST =
18189 // Wave64 mbcnt_lo returns at most 32 + src1. Otherwise these return at
18190 // most 31 + src1.
18191 Known.Zero.setBitsFrom(
18192 IID == Intrinsic::amdgcn_mbcnt_lo ? ST.getWavefrontSizeLog2() : 5);
18193 KnownBits Known2 = DAG.computeKnownBits(Op.getOperand(2), Depth + 1);
18194 Known = KnownBits::add(Known, Known2);
18195 return;
18196 }
18197 }
18198 break;
18199 }
18200 }
18202 Op, Known, DemandedElts, DAG, Depth);
18203}
18204
18206 const int FI, KnownBits &Known, const MachineFunction &MF) const {
18208
18209 // Set the high bits to zero based on the maximum allowed scratch size per
18210 // wave. We can't use vaddr in MUBUF instructions if we don't know the address
18211 // calculation won't overflow, so assume the sign bit is never set.
18212 Known.Zero.setHighBits(getSubtarget()->getKnownHighZeroBitsForFrameIndex());
18213}
18214
18216 GISelValueTracking &VT, KnownBits &Known,
18217 unsigned Dim) {
18218 unsigned MaxValue =
18219 ST.getMaxWorkitemID(VT.getMachineFunction().getFunction(), Dim);
18220 Known.Zero.setHighBits(llvm::countl_zero(MaxValue));
18221}
18222
18224 KnownBits &Known, const APInt &DemandedElts,
18225 unsigned BFEWidth, bool SExt, unsigned Depth) {
18227 const MachineOperand &Src1 = MI.getOperand(2);
18228
18229 unsigned Src1Cst = 0;
18230 if (Src1.isImm()) {
18231 Src1Cst = Src1.getImm();
18232 } else if (Src1.isReg()) {
18233 auto Cst = getIConstantVRegValWithLookThrough(Src1.getReg(), MRI);
18234 if (!Cst)
18235 return;
18236 Src1Cst = Cst->Value.getZExtValue();
18237 } else {
18238 return;
18239 }
18240
18241 // Offset is at bits [4:0] for 32 bit, [5:0] for 64 bit.
18242 // Width is always [22:16].
18243 const unsigned Offset =
18244 Src1Cst & maskTrailingOnes<unsigned>((BFEWidth == 32) ? 5 : 6);
18245 const unsigned Width = (Src1Cst >> 16) & maskTrailingOnes<unsigned>(6);
18246
18247 if (Width >= BFEWidth) // Ill-formed.
18248 return;
18249
18250 VT.computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
18251 Depth + 1);
18252
18253 Known = Known.extractBits(Width, Offset);
18254
18255 if (SExt)
18256 Known = Known.sext(BFEWidth);
18257 else
18258 Known = Known.zext(BFEWidth);
18259}
18260
18262 GISelValueTracking &VT, Register R, KnownBits &Known,
18263 const APInt &DemandedElts, const MachineRegisterInfo &MRI,
18264 unsigned Depth) const {
18265 Known.resetAll();
18266 const MachineInstr *MI = MRI.getVRegDef(R);
18267 switch (MI->getOpcode()) {
18268 case AMDGPU::S_BFE_I32:
18269 return knownBitsForSBFE(*MI, VT, Known, DemandedElts, /*Width=*/32,
18270 /*SExt=*/true, Depth);
18271 case AMDGPU::S_BFE_U32:
18272 return knownBitsForSBFE(*MI, VT, Known, DemandedElts, /*Width=*/32,
18273 /*SExt=*/false, Depth);
18274 case AMDGPU::S_BFE_I64:
18275 return knownBitsForSBFE(*MI, VT, Known, DemandedElts, /*Width=*/64,
18276 /*SExt=*/true, Depth);
18277 case AMDGPU::S_BFE_U64:
18278 return knownBitsForSBFE(*MI, VT, Known, DemandedElts, /*Width=*/64,
18279 /*SExt=*/false, Depth);
18280 case AMDGPU::G_INTRINSIC:
18281 case AMDGPU::G_INTRINSIC_CONVERGENT: {
18282 Intrinsic::ID IID = cast<GIntrinsic>(MI)->getIntrinsicID();
18283 switch (IID) {
18284 case Intrinsic::amdgcn_workitem_id_x:
18285 knownBitsForWorkitemID(*getSubtarget(), VT, Known, 0);
18286 break;
18287 case Intrinsic::amdgcn_workitem_id_y:
18288 knownBitsForWorkitemID(*getSubtarget(), VT, Known, 1);
18289 break;
18290 case Intrinsic::amdgcn_workitem_id_z:
18291 knownBitsForWorkitemID(*getSubtarget(), VT, Known, 2);
18292 break;
18293 case Intrinsic::amdgcn_mbcnt_lo:
18294 case Intrinsic::amdgcn_mbcnt_hi: {
18295 // Wave64 mbcnt_lo returns at most 32 + src1. Otherwise these return at
18296 // most 31 + src1.
18297 Known.Zero.setBitsFrom(IID == Intrinsic::amdgcn_mbcnt_lo
18298 ? getSubtarget()->getWavefrontSizeLog2()
18299 : 5);
18300 KnownBits Known2;
18301 VT.computeKnownBitsImpl(MI->getOperand(3).getReg(), Known2, DemandedElts,
18302 Depth + 1);
18303 Known = KnownBits::add(Known, Known2);
18304 break;
18305 }
18306 case Intrinsic::amdgcn_groupstaticsize: {
18307 // We can report everything over the maximum size as 0. We can't report
18308 // based on the actual size because we don't know if it's accurate or not
18309 // at any given point.
18310 Known.Zero.setHighBits(
18311 llvm::countl_zero(getSubtarget()->getAddressableLocalMemorySize()));
18312 break;
18313 }
18314 }
18315 break;
18316 }
18317 case AMDGPU::G_AMDGPU_BUFFER_LOAD_UBYTE:
18318 Known.Zero.setHighBits(24);
18319 break;
18320 case AMDGPU::G_AMDGPU_BUFFER_LOAD_USHORT:
18321 Known.Zero.setHighBits(16);
18322 break;
18323 case AMDGPU::G_AMDGPU_SMED3:
18324 case AMDGPU::G_AMDGPU_UMED3: {
18325 auto [Dst, Src0, Src1, Src2] = MI->getFirst4Regs();
18326
18327 KnownBits Known2;
18328 VT.computeKnownBitsImpl(Src2, Known2, DemandedElts, Depth + 1);
18329 if (Known2.isUnknown())
18330 break;
18331
18332 KnownBits Known1;
18333 VT.computeKnownBitsImpl(Src1, Known1, DemandedElts, Depth + 1);
18334 if (Known1.isUnknown())
18335 break;
18336
18337 KnownBits Known0;
18338 VT.computeKnownBitsImpl(Src0, Known0, DemandedElts, Depth + 1);
18339 if (Known0.isUnknown())
18340 break;
18341
18342 // TODO: Handle LeadZero/LeadOne from UMIN/UMAX handling.
18343 Known.Zero = Known0.Zero & Known1.Zero & Known2.Zero;
18344 Known.One = Known0.One & Known1.One & Known2.One;
18345 break;
18346 }
18347 }
18348}
18349
18352 unsigned Depth) const {
18353 const MachineInstr *MI = MRI.getVRegDef(R);
18354 if (auto *GI = dyn_cast<GIntrinsic>(MI)) {
18355 // FIXME: Can this move to generic code? What about the case where the call
18356 // site specifies a lower alignment?
18357 Intrinsic::ID IID = GI->getIntrinsicID();
18359 AttributeList Attrs =
18360 Intrinsic::getAttributes(Ctx, IID, Intrinsic::getType(Ctx, IID));
18361 if (MaybeAlign RetAlign = Attrs.getRetAlignment())
18362 return *RetAlign;
18363 }
18364 return Align(1);
18365}
18366
18369 const Align CacheLineAlign = Align(64);
18370
18371 // Pre-GFX10 target did not benefit from loop alignment
18372 if (!ML || DisableLoopAlignment || !getSubtarget()->hasInstPrefetch() ||
18373 getSubtarget()->hasInstFwdPrefetchBug())
18374 return PrefAlign;
18375
18376 // On GFX10 I$ is 4 x 64 bytes cache lines.
18377 // By default prefetcher keeps one cache line behind and reads two ahead.
18378 // We can modify it with S_INST_PREFETCH for larger loops to have two lines
18379 // behind and one ahead.
18380 // Therefor we can benefit from aligning loop headers if loop fits 192 bytes.
18381 // If loop fits 64 bytes it always spans no more than two cache lines and
18382 // does not need an alignment.
18383 // Else if loop is less or equal 128 bytes we do not need to modify prefetch,
18384 // Else if loop is less or equal 192 bytes we need two lines behind.
18385
18387 const MachineBasicBlock *Header = ML->getHeader();
18388 if (Header->getAlignment() != PrefAlign)
18389 return Header->getAlignment(); // Already processed.
18390
18391 unsigned LoopSize = 0;
18392 for (const MachineBasicBlock *MBB : ML->blocks()) {
18393 // If inner loop block is aligned assume in average half of the alignment
18394 // size to be added as nops.
18395 if (MBB != Header)
18396 LoopSize += MBB->getAlignment().value() / 2;
18397
18398 for (const MachineInstr &MI : *MBB) {
18399 LoopSize += TII->getInstSizeInBytes(MI);
18400 if (LoopSize > 192)
18401 return PrefAlign;
18402 }
18403 }
18404
18405 if (LoopSize <= 64)
18406 return PrefAlign;
18407
18408 if (LoopSize <= 128)
18409 return CacheLineAlign;
18410
18411 // If any of parent loops is surrounded by prefetch instructions do not
18412 // insert new for inner loop, which would reset parent's settings.
18413 for (MachineLoop *P = ML->getParentLoop(); P; P = P->getParentLoop()) {
18414 if (MachineBasicBlock *Exit = P->getExitBlock()) {
18415 auto I = Exit->getFirstNonDebugInstr();
18416 if (I != Exit->end() && I->getOpcode() == AMDGPU::S_INST_PREFETCH)
18417 return CacheLineAlign;
18418 }
18419 }
18420
18421 MachineBasicBlock *Pre = ML->getLoopPreheader();
18422 MachineBasicBlock *Exit = ML->getExitBlock();
18423
18424 if (Pre && Exit) {
18425 auto PreTerm = Pre->getFirstTerminator();
18426 if (PreTerm == Pre->begin() ||
18427 std::prev(PreTerm)->getOpcode() != AMDGPU::S_INST_PREFETCH)
18428 BuildMI(*Pre, PreTerm, DebugLoc(), TII->get(AMDGPU::S_INST_PREFETCH))
18429 .addImm(1); // prefetch 2 lines behind PC
18430
18431 auto ExitHead = Exit->getFirstNonDebugInstr();
18432 if (ExitHead == Exit->end() ||
18433 ExitHead->getOpcode() != AMDGPU::S_INST_PREFETCH)
18434 BuildMI(*Exit, ExitHead, DebugLoc(), TII->get(AMDGPU::S_INST_PREFETCH))
18435 .addImm(2); // prefetch 1 line behind PC
18436 }
18437
18438 return CacheLineAlign;
18439}
18440
18441[[maybe_unused]]
18442static bool isCopyFromRegOfInlineAsm(const SDNode *N) {
18443 assert(N->getOpcode() == ISD::CopyFromReg);
18444 do {
18445 // Follow the chain until we find an INLINEASM node.
18446 N = N->getOperand(0).getNode();
18447 if (N->getOpcode() == ISD::INLINEASM || N->getOpcode() == ISD::INLINEASM_BR)
18448 return true;
18449 } while (N->getOpcode() == ISD::CopyFromReg);
18450 return false;
18451}
18452
18455 UniformityInfo *UA) const {
18456 switch (N->getOpcode()) {
18457 case ISD::CopyFromReg: {
18458 const RegisterSDNode *R = cast<RegisterSDNode>(N->getOperand(1));
18459 const MachineRegisterInfo &MRI = FLI->MF->getRegInfo();
18460 const SIRegisterInfo *TRI = Subtarget->getRegisterInfo();
18461 Register Reg = R->getReg();
18462
18463 // FIXME: Why does this need to consider isLiveIn?
18464 if (Reg.isPhysical() || MRI.isLiveIn(Reg))
18465 return !TRI->isSGPRReg(MRI, Reg);
18466
18467 if (const Value *V = FLI->getValueFromVirtualReg(R->getReg()))
18468 return UA->isDivergent(V);
18469
18471 return !TRI->isSGPRReg(MRI, Reg);
18472 }
18473 case ISD::LOAD: {
18474 const LoadSDNode *L = cast<LoadSDNode>(N);
18475 unsigned AS = L->getAddressSpace();
18476 // A flat load may access private memory.
18478 }
18479 case ISD::CALLSEQ_END:
18480 return true;
18482 return AMDGPU::isIntrinsicSourceOfDivergence(N->getConstantOperandVal(0));
18484 return AMDGPU::isIntrinsicSourceOfDivergence(N->getConstantOperandVal(1));
18485 case AMDGPUISD::ATOMIC_CMP_SWAP:
18486 case AMDGPUISD::BUFFER_ATOMIC_SWAP:
18487 case AMDGPUISD::BUFFER_ATOMIC_ADD:
18488 case AMDGPUISD::BUFFER_ATOMIC_SUB:
18489 case AMDGPUISD::BUFFER_ATOMIC_SMIN:
18490 case AMDGPUISD::BUFFER_ATOMIC_UMIN:
18491 case AMDGPUISD::BUFFER_ATOMIC_SMAX:
18492 case AMDGPUISD::BUFFER_ATOMIC_UMAX:
18493 case AMDGPUISD::BUFFER_ATOMIC_AND:
18494 case AMDGPUISD::BUFFER_ATOMIC_OR:
18495 case AMDGPUISD::BUFFER_ATOMIC_XOR:
18496 case AMDGPUISD::BUFFER_ATOMIC_INC:
18497 case AMDGPUISD::BUFFER_ATOMIC_DEC:
18498 case AMDGPUISD::BUFFER_ATOMIC_CMPSWAP:
18499 case AMDGPUISD::BUFFER_ATOMIC_FADD:
18500 case AMDGPUISD::BUFFER_ATOMIC_FMIN:
18501 case AMDGPUISD::BUFFER_ATOMIC_FMAX:
18502 // Target-specific read-modify-write atomics are sources of divergence.
18503 return true;
18504 default:
18505 if (auto *A = dyn_cast<AtomicSDNode>(N)) {
18506 // Generic read-modify-write atomics are sources of divergence.
18507 return A->readMem() && A->writeMem();
18508 }
18509 return false;
18510 }
18511}
18512
18514 EVT VT) const {
18515 switch (VT.getScalarType().getSimpleVT().SimpleTy) {
18516 case MVT::f32:
18518 case MVT::f64:
18519 case MVT::f16:
18521 default:
18522 return false;
18523 }
18524}
18525
18527 LLT Ty, const MachineFunction &MF) const {
18528 switch (Ty.getScalarSizeInBits()) {
18529 case 32:
18530 return !denormalModeIsFlushAllF32(MF);
18531 case 64:
18532 case 16:
18533 return !denormalModeIsFlushAllF64F16(MF);
18534 default:
18535 return false;
18536 }
18537}
18538
18540 const APInt &DemandedElts,
18541 const SelectionDAG &DAG,
18542 bool SNaN,
18543 unsigned Depth) const {
18544 if (Op.getOpcode() == AMDGPUISD::CLAMP) {
18545 const MachineFunction &MF = DAG.getMachineFunction();
18547
18548 if (Info->getMode().DX10Clamp)
18549 return true; // Clamped to 0.
18550 return DAG.isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
18551 }
18552
18554 DAG, SNaN, Depth);
18555}
18556
18557// On older subtargets, global FP atomic instructions have a hardcoded FP mode
18558// and do not support FP32 denormals, and only support v2f16/f64 denormals.
18560 if (RMW->hasMetadata("amdgpu.ignore.denormal.mode"))
18561 return true;
18562
18563 const fltSemantics &Flt = RMW->getType()->getScalarType()->getFltSemantics();
18564 auto DenormMode = RMW->getFunction()->getDenormalMode(Flt);
18565 if (DenormMode == DenormalMode::getPreserveSign())
18566 return true;
18567
18568 // TODO: Remove this.
18569 return RMW->getFunction()
18570 ->getFnAttribute("amdgpu-unsafe-fp-atomics")
18571 .getValueAsBool();
18572}
18573
18575 LLVMContext &Ctx = RMW->getContext();
18576 StringRef MemScope =
18577 Ctx.getSyncScopeName(RMW->getSyncScopeID()).value_or("system");
18578
18579 return OptimizationRemark(DEBUG_TYPE, "Passed", RMW)
18580 << "Hardware instruction generated for atomic "
18581 << RMW->getOperationName(RMW->getOperation())
18582 << " operation at memory scope " << MemScope;
18583}
18584
18585static bool isV2F16OrV2BF16(Type *Ty) {
18586 if (auto *VT = dyn_cast<FixedVectorType>(Ty)) {
18587 Type *EltTy = VT->getElementType();
18588 return VT->getNumElements() == 2 &&
18589 (EltTy->isHalfTy() || EltTy->isBFloatTy());
18590 }
18591
18592 return false;
18593}
18594
18595static bool isV2F16(Type *Ty) {
18597 return VT && VT->getNumElements() == 2 && VT->getElementType()->isHalfTy();
18598}
18599
18600static bool isV2BF16(Type *Ty) {
18602 return VT && VT->getNumElements() == 2 && VT->getElementType()->isBFloatTy();
18603}
18604
18605/// \return true if atomicrmw integer ops work for the type.
18606static bool isAtomicRMWLegalIntTy(Type *Ty) {
18607 if (auto *IT = dyn_cast<IntegerType>(Ty)) {
18608 unsigned BW = IT->getBitWidth();
18609 return BW == 32 || BW == 64;
18610 }
18611
18612 return false;
18613}
18614
18615/// \return true if this atomicrmw xchg type can be selected.
18616static bool isAtomicRMWLegalXChgTy(const AtomicRMWInst *RMW) {
18617 Type *Ty = RMW->getType();
18618 if (isAtomicRMWLegalIntTy(Ty))
18619 return true;
18620
18621 if (PointerType *PT = dyn_cast<PointerType>(Ty)) {
18622 const DataLayout &DL = RMW->getFunction()->getParent()->getDataLayout();
18623 unsigned BW = DL.getPointerSizeInBits(PT->getAddressSpace());
18624 return BW == 32 || BW == 64;
18625 }
18626
18627 if (Ty->isFloatTy() || Ty->isDoubleTy())
18628 return true;
18629
18631 return VT->getNumElements() == 2 &&
18632 VT->getElementType()->getPrimitiveSizeInBits() == 16;
18633 }
18634
18635 return false;
18636}
18637
18638/// \returns true if it's valid to emit a native instruction for \p RMW, based
18639/// on the properties of the target memory.
18640static bool globalMemoryFPAtomicIsLegal(const GCNSubtarget &Subtarget,
18641 const AtomicRMWInst *RMW,
18642 bool HasSystemScope) {
18643 // The remote/fine-grained access logic is different from the integer
18644 // atomics. Without AgentScopeFineGrainedRemoteMemoryAtomics support,
18645 // fine-grained access does not work, even for a device local allocation.
18646 //
18647 // With AgentScopeFineGrainedRemoteMemoryAtomics, system scoped device local
18648 // allocations work.
18649 if (HasSystemScope) {
18651 RMW->hasMetadata("amdgpu.no.remote.memory"))
18652 return true;
18653 if (Subtarget.hasEmulatedSystemScopeAtomics())
18654 return true;
18656 return true;
18657
18658 return RMW->hasMetadata("amdgpu.no.fine.grained.memory");
18659}
18660
18661/// \return Action to perform on AtomicRMWInsts for integer operations.
18668
18669/// Return if a flat address space atomicrmw can access private memory.
18671 const MDNode *MD = I->getMetadata(LLVMContext::MD_noalias_addrspace);
18672 return !MD ||
18674}
18675
18683
18686 unsigned AS = RMW->getPointerAddressSpace();
18687 if (AS == AMDGPUAS::PRIVATE_ADDRESS)
18689
18690 // 64-bit flat atomics that dynamically reside in private memory will silently
18691 // be dropped.
18692 //
18693 // Note that we will emit a new copy of the original atomic in the expansion,
18694 // which will be incrementally relegalized.
18695 const DataLayout &DL = RMW->getFunction()->getDataLayout();
18696 if (AS == AMDGPUAS::FLAT_ADDRESS &&
18697 DL.getTypeSizeInBits(RMW->getType()) == 64 &&
18700
18701 auto ReportUnsafeHWInst = [=](TargetLowering::AtomicExpansionKind Kind) {
18703 ORE.emit([=]() {
18704 return emitAtomicRMWLegalRemark(RMW) << " due to an unsafe request.";
18705 });
18706 return Kind;
18707 };
18708
18709 auto SSID = RMW->getSyncScopeID();
18710 bool HasSystemScope =
18711 SSID == SyncScope::System ||
18712 SSID == RMW->getContext().getOrInsertSyncScopeID("one-as");
18713
18714 auto Op = RMW->getOperation();
18715 switch (Op) {
18717 // PCIe supports add and xchg for system atomics.
18718 return isAtomicRMWLegalXChgTy(RMW)
18721 case AtomicRMWInst::Add:
18722 // PCIe supports add and xchg for system atomics.
18724 case AtomicRMWInst::Sub:
18725 case AtomicRMWInst::And:
18726 case AtomicRMWInst::Or:
18727 case AtomicRMWInst::Xor:
18728 case AtomicRMWInst::Max:
18729 case AtomicRMWInst::Min:
18736 if (Op == AtomicRMWInst::USubCond && !Subtarget->hasCondSubInsts())
18738 if (Op == AtomicRMWInst::USubSat && !Subtarget->hasSubClampInsts())
18741 auto *IT = dyn_cast<IntegerType>(RMW->getType());
18742 if (!IT || IT->getBitWidth() != 32)
18744 }
18745
18748 if (Subtarget->hasEmulatedSystemScopeAtomics())
18750
18751 // On most subtargets, for atomicrmw operations other than add/xchg,
18752 // whether or not the instructions will behave correctly depends on where
18753 // the address physically resides and what interconnect is used in the
18754 // system configuration. On some some targets the instruction will nop,
18755 // and in others synchronization will only occur at degraded device scope.
18756 //
18757 // If the allocation is known local to the device, the instructions should
18758 // work correctly.
18759 if (RMW->hasMetadata("amdgpu.no.remote.memory"))
18761
18762 // If fine-grained remote memory works at device scope, we don't need to
18763 // do anything.
18764 if (!HasSystemScope &&
18765 Subtarget->supportsAgentScopeFineGrainedRemoteMemoryAtomics())
18767
18768 // If we are targeting a remote allocated address, it depends what kind of
18769 // allocation the address belongs to.
18770 //
18771 // If the allocation is fine-grained (in host memory, or in PCIe peer
18772 // device memory), the operation will fail depending on the target.
18773 //
18774 // Note fine-grained host memory access does work on APUs or if XGMI is
18775 // used, but we do not know if we are targeting an APU or the system
18776 // configuration from the ISA version/target-cpu.
18777 if (RMW->hasMetadata("amdgpu.no.fine.grained.memory"))
18779
18782 // Atomic sub/or/xor do not work over PCI express, but atomic add
18783 // does. InstCombine transforms these with 0 to or, so undo that.
18784 if (Constant *ConstVal = dyn_cast<Constant>(RMW->getValOperand());
18785 ConstVal && ConstVal->isNullValue())
18787 }
18788
18789 // If the allocation could be in remote, fine-grained memory, the rmw
18790 // instructions may fail. cmpxchg should work, so emit that. On some
18791 // system configurations, PCIe atomics aren't supported so cmpxchg won't
18792 // even work, so you're out of luck anyway.
18793
18794 // In summary:
18795 //
18796 // Cases that may fail:
18797 // - fine-grained pinned host memory
18798 // - fine-grained migratable host memory
18799 // - fine-grained PCIe peer device
18800 //
18801 // Cases that should work, but may be treated overly conservatively.
18802 // - fine-grained host memory on an APU
18803 // - fine-grained XGMI peer device
18805 }
18806
18808 }
18809 case AtomicRMWInst::FAdd: {
18810 Type *Ty = RMW->getType();
18811
18812 // TODO: Handle REGION_ADDRESS
18813 if (AS == AMDGPUAS::LOCAL_ADDRESS) {
18814 // DS F32 FP atomics do respect the denormal mode, but the rounding mode
18815 // is fixed to round-to-nearest-even.
18816 //
18817 // F64 / PK_F16 / PK_BF16 never flush and are also fixed to
18818 // round-to-nearest-even.
18819 //
18820 // We ignore the rounding mode problem, even in strictfp. The C++ standard
18821 // suggests it is OK if the floating-point mode may not match the calling
18822 // thread.
18823 if (Ty->isFloatTy()) {
18824 return Subtarget->hasLDSFPAtomicAddF32() ? AtomicExpansionKind::None
18826 }
18827
18828 if (Ty->isDoubleTy()) {
18829 // Ignores denormal mode, but we don't consider flushing mandatory.
18830 return Subtarget->hasLDSFPAtomicAddF64() ? AtomicExpansionKind::None
18832 }
18833
18834 if (Subtarget->hasAtomicDsPkAdd16Insts() && isV2F16OrV2BF16(Ty))
18836
18838 }
18839
18840 // LDS atomics respect the denormal mode from the mode register.
18841 //
18842 // Traditionally f32 global/buffer memory atomics would unconditionally
18843 // flush denormals, but newer targets do not flush. f64/f16/bf16 cases never
18844 // flush.
18845 //
18846 // On targets with flat atomic fadd, denormals would flush depending on
18847 // whether the target address resides in LDS or global memory. We consider
18848 // this flat-maybe-flush as will-flush.
18849 if (Ty->isFloatTy() &&
18850 !Subtarget->hasMemoryAtomicFaddF32DenormalSupport() &&
18853
18854 // FIXME: These ReportUnsafeHWInsts are imprecise. Some of these cases are
18855 // safe. The message phrasing also should be better.
18856 if (globalMemoryFPAtomicIsLegal(*Subtarget, RMW, HasSystemScope)) {
18857 if (AS == AMDGPUAS::FLAT_ADDRESS) {
18858 // gfx942, gfx12
18859 if (Subtarget->hasAtomicFlatPkAdd16Insts() && isV2F16OrV2BF16(Ty))
18860 return ReportUnsafeHWInst(AtomicExpansionKind::None);
18861 } else if (AMDGPU::isExtendedGlobalAddrSpace(AS)) {
18862 // gfx90a, gfx942, gfx12
18863 if (Subtarget->hasAtomicBufferGlobalPkAddF16Insts() && isV2F16(Ty))
18864 return ReportUnsafeHWInst(AtomicExpansionKind::None);
18865
18866 // gfx942, gfx12
18867 if (Subtarget->hasAtomicGlobalPkAddBF16Inst() && isV2BF16(Ty))
18868 return ReportUnsafeHWInst(AtomicExpansionKind::None);
18869 } else if (AS == AMDGPUAS::BUFFER_FAT_POINTER) {
18870 // gfx90a, gfx942, gfx12
18871 if (Subtarget->hasAtomicBufferGlobalPkAddF16Insts() && isV2F16(Ty))
18872 return ReportUnsafeHWInst(AtomicExpansionKind::None);
18873
18874 // While gfx90a/gfx942 supports v2bf16 for global/flat, it does not for
18875 // buffer. gfx12 does have the buffer version.
18876 if (Subtarget->hasAtomicBufferPkAddBF16Inst() && isV2BF16(Ty))
18877 return ReportUnsafeHWInst(AtomicExpansionKind::None);
18878 }
18879
18880 // global and flat atomic fadd f64: gfx90a, gfx942.
18881 if (Subtarget->hasFlatBufferGlobalAtomicFaddF64Inst() && Ty->isDoubleTy())
18882 return ReportUnsafeHWInst(AtomicExpansionKind::None);
18883
18884 if (AS != AMDGPUAS::FLAT_ADDRESS) {
18885 if (Ty->isFloatTy()) {
18886 // global/buffer atomic fadd f32 no-rtn: gfx908, gfx90a, gfx942,
18887 // gfx11+.
18888 if (RMW->use_empty() && Subtarget->hasAtomicFaddNoRtnInsts())
18889 return ReportUnsafeHWInst(AtomicExpansionKind::None);
18890 // global/buffer atomic fadd f32 rtn: gfx90a, gfx942, gfx11+.
18891 if (!RMW->use_empty() && Subtarget->hasAtomicFaddRtnInsts())
18892 return ReportUnsafeHWInst(AtomicExpansionKind::None);
18893 } else {
18894 // gfx908
18895 if (RMW->use_empty() &&
18896 Subtarget->hasAtomicBufferGlobalPkAddF16NoRtnInsts() &&
18897 isV2F16(Ty))
18898 return ReportUnsafeHWInst(AtomicExpansionKind::None);
18899 }
18900 }
18901
18902 // flat atomic fadd f32: gfx942, gfx11+.
18903 if (AS == AMDGPUAS::FLAT_ADDRESS && Ty->isFloatTy()) {
18904 if (Subtarget->hasFlatAtomicFaddF32Inst())
18905 return ReportUnsafeHWInst(AtomicExpansionKind::None);
18906
18907 // If it is in flat address space, and the type is float, we will try to
18908 // expand it, if the target supports global and lds atomic fadd. The
18909 // reason we need that is, in the expansion, we emit the check of
18910 // address space. If it is in global address space, we emit the global
18911 // atomic fadd; if it is in shared address space, we emit the LDS atomic
18912 // fadd.
18913 if (Subtarget->hasLDSFPAtomicAddF32()) {
18914 if (RMW->use_empty() && Subtarget->hasAtomicFaddNoRtnInsts())
18916 if (!RMW->use_empty() && Subtarget->hasAtomicFaddRtnInsts())
18918 }
18919 }
18920 }
18921
18923 }
18925 case AtomicRMWInst::FMax: {
18926 Type *Ty = RMW->getType();
18927
18928 // LDS float and double fmin/fmax were always supported.
18929 if (AS == AMDGPUAS::LOCAL_ADDRESS) {
18930 return Ty->isFloatTy() || Ty->isDoubleTy() ? AtomicExpansionKind::None
18932 }
18933
18934 if (globalMemoryFPAtomicIsLegal(*Subtarget, RMW, HasSystemScope)) {
18935 // For flat and global cases:
18936 // float, double in gfx7. Manual claims denormal support.
18937 // Removed in gfx8.
18938 // float, double restored in gfx10.
18939 // double removed again in gfx11, so only f32 for gfx11/gfx12.
18940 //
18941 // For gfx9, gfx90a and gfx942 support f64 for global (same as fadd), but
18942 // no f32.
18943 if (AS == AMDGPUAS::FLAT_ADDRESS) {
18944 if (Subtarget->hasAtomicFMinFMaxF32FlatInsts() && Ty->isFloatTy())
18945 return ReportUnsafeHWInst(AtomicExpansionKind::None);
18946 if (Subtarget->hasAtomicFMinFMaxF64FlatInsts() && Ty->isDoubleTy())
18947 return ReportUnsafeHWInst(AtomicExpansionKind::None);
18948 } else if (AMDGPU::isExtendedGlobalAddrSpace(AS) ||
18950 if (Subtarget->hasAtomicFMinFMaxF32GlobalInsts() && Ty->isFloatTy())
18951 return ReportUnsafeHWInst(AtomicExpansionKind::None);
18952 if (Subtarget->hasAtomicFMinFMaxF64GlobalInsts() && Ty->isDoubleTy())
18953 return ReportUnsafeHWInst(AtomicExpansionKind::None);
18954 }
18955 }
18956
18958 }
18961 default:
18963 }
18964
18965 llvm_unreachable("covered atomicrmw op switch");
18966}
18967
18974
18981
18984 unsigned AddrSpace = CmpX->getPointerAddressSpace();
18985 if (AddrSpace == AMDGPUAS::PRIVATE_ADDRESS)
18987
18988 if (AddrSpace != AMDGPUAS::FLAT_ADDRESS || !flatInstrMayAccessPrivate(CmpX))
18990
18991 const DataLayout &DL = CmpX->getDataLayout();
18992
18993 Type *ValTy = CmpX->getNewValOperand()->getType();
18994
18995 // If a 64-bit flat atomic may alias private, we need to avoid using the
18996 // atomic in the private case.
18997 return DL.getTypeSizeInBits(ValTy) == 64 ? AtomicExpansionKind::CustomExpand
18999}
19000
19001const TargetRegisterClass *
19002SITargetLowering::getRegClassFor(MVT VT, bool isDivergent) const {
19004 const SIRegisterInfo *TRI = Subtarget->getRegisterInfo();
19005 if (RC == &AMDGPU::VReg_1RegClass && !isDivergent)
19006 return Subtarget->isWave64() ? &AMDGPU::SReg_64RegClass
19007 : &AMDGPU::SReg_32RegClass;
19008 if (!TRI->isSGPRClass(RC) && !isDivergent)
19009 return TRI->getEquivalentSGPRClass(RC);
19010 if (TRI->isSGPRClass(RC) && isDivergent) {
19011 if (Subtarget->hasGFX90AInsts())
19012 return TRI->getEquivalentAVClass(RC);
19013 return TRI->getEquivalentVGPRClass(RC);
19014 }
19015
19016 return RC;
19017}
19018
19019// FIXME: This is a workaround for DivergenceAnalysis not understanding always
19020// uniform values (as produced by the mask results of control flow intrinsics)
19021// used outside of divergent blocks. The phi users need to also be treated as
19022// always uniform.
19023//
19024// FIXME: DA is no longer in-use. Does this still apply to UniformityAnalysis?
19025static bool hasCFUser(const Value *V, SmallPtrSet<const Value *, 16> &Visited,
19026 unsigned WaveSize) {
19027 // FIXME: We assume we never cast the mask results of a control flow
19028 // intrinsic.
19029 // Early exit if the type won't be consistent as a compile time hack.
19030 IntegerType *IT = dyn_cast<IntegerType>(V->getType());
19031 if (!IT || IT->getBitWidth() != WaveSize)
19032 return false;
19033
19034 if (!isa<Instruction>(V))
19035 return false;
19036 if (!Visited.insert(V).second)
19037 return false;
19038 bool Result = false;
19039 for (const auto *U : V->users()) {
19041 if (V == U->getOperand(1)) {
19042 switch (Intrinsic->getIntrinsicID()) {
19043 default:
19044 Result = false;
19045 break;
19046 case Intrinsic::amdgcn_if_break:
19047 case Intrinsic::amdgcn_if:
19048 case Intrinsic::amdgcn_else:
19049 Result = true;
19050 break;
19051 }
19052 }
19053 if (V == U->getOperand(0)) {
19054 switch (Intrinsic->getIntrinsicID()) {
19055 default:
19056 Result = false;
19057 break;
19058 case Intrinsic::amdgcn_end_cf:
19059 case Intrinsic::amdgcn_loop:
19060 Result = true;
19061 break;
19062 }
19063 }
19064 } else {
19065 Result = hasCFUser(U, Visited, WaveSize);
19066 }
19067 if (Result)
19068 break;
19069 }
19070 return Result;
19071}
19072
19074 const Value *V) const {
19075 if (const CallInst *CI = dyn_cast<CallInst>(V)) {
19076 if (CI->isInlineAsm()) {
19077 // FIXME: This cannot give a correct answer. This should only trigger in
19078 // the case where inline asm returns mixed SGPR and VGPR results, used
19079 // outside the defining block. We don't have a specific result to
19080 // consider, so this assumes if any value is SGPR, the overall register
19081 // also needs to be SGPR.
19082 const SIRegisterInfo *SIRI = Subtarget->getRegisterInfo();
19084 MF.getDataLayout(), Subtarget->getRegisterInfo(), *CI);
19085 for (auto &TC : TargetConstraints) {
19086 if (TC.Type == InlineAsm::isOutput) {
19088 const TargetRegisterClass *RC =
19089 getRegForInlineAsmConstraint(SIRI, TC.ConstraintCode,
19090 TC.ConstraintVT)
19091 .second;
19092 if (RC && SIRI->isSGPRClass(RC))
19093 return true;
19094 }
19095 }
19096 }
19097 }
19099 return hasCFUser(V, Visited, Subtarget->getWavefrontSize());
19100}
19101
19103 for (SDUse &Use : N->uses()) {
19105 if (getBasePtrIndex(M) == Use.getOperandNo())
19106 return true;
19107 }
19108 }
19109 return false;
19110}
19111
19113 SDValue N1) const {
19114 if (!N0.hasOneUse())
19115 return false;
19116 // Take care of the opportunity to keep N0 uniform
19117 if (N0->isDivergent() || !N1->isDivergent())
19118 return true;
19119 // Check if we have a good chance to form the memory access pattern with the
19120 // base and offset
19121 return (DAG.isBaseWithConstantOffset(N0) &&
19123}
19124
19126 Register N0, Register N1) const {
19127 return MRI.hasOneNonDBGUse(N0); // FIXME: handle regbanks
19128}
19129
19132 // Propagate metadata set by AMDGPUAnnotateUniformValues to the MMO of a load.
19134 if (I.getMetadata("amdgpu.noclobber"))
19135 Flags |= MONoClobber;
19136 if (I.getMetadata("amdgpu.last.use"))
19137 Flags |= MOLastUse;
19138 return Flags;
19139}
19140
19142 Instruction *AI) const {
19143 // Given: atomicrmw fadd ptr %addr, float %val ordering
19144 //
19145 // With this expansion we produce the following code:
19146 // [...]
19147 // %is.shared = call i1 @llvm.amdgcn.is.shared(ptr %addr)
19148 // br i1 %is.shared, label %atomicrmw.shared, label %atomicrmw.check.private
19149 //
19150 // atomicrmw.shared:
19151 // %cast.shared = addrspacecast ptr %addr to ptr addrspace(3)
19152 // %loaded.shared = atomicrmw fadd ptr addrspace(3) %cast.shared,
19153 // float %val ordering
19154 // br label %atomicrmw.phi
19155 //
19156 // atomicrmw.check.private:
19157 // %is.private = call i1 @llvm.amdgcn.is.private(ptr %int8ptr)
19158 // br i1 %is.private, label %atomicrmw.private, label %atomicrmw.global
19159 //
19160 // atomicrmw.private:
19161 // %cast.private = addrspacecast ptr %addr to ptr addrspace(5)
19162 // %loaded.private = load float, ptr addrspace(5) %cast.private
19163 // %val.new = fadd float %loaded.private, %val
19164 // store float %val.new, ptr addrspace(5) %cast.private
19165 // br label %atomicrmw.phi
19166 //
19167 // atomicrmw.global:
19168 // %cast.global = addrspacecast ptr %addr to ptr addrspace(1)
19169 // %loaded.global = atomicrmw fadd ptr addrspace(1) %cast.global,
19170 // float %val ordering
19171 // br label %atomicrmw.phi
19172 //
19173 // atomicrmw.phi:
19174 // %loaded.phi = phi float [ %loaded.shared, %atomicrmw.shared ],
19175 // [ %loaded.private, %atomicrmw.private ],
19176 // [ %loaded.global, %atomicrmw.global ]
19177 // br label %atomicrmw.end
19178 //
19179 // atomicrmw.end:
19180 // [...]
19181 //
19182 //
19183 // For 64-bit atomics which may reside in private memory, we perform a simpler
19184 // version that only inserts the private check, and uses the flat operation.
19185
19186 IRBuilder<> Builder(AI);
19187 LLVMContext &Ctx = Builder.getContext();
19188
19189 auto *RMW = dyn_cast<AtomicRMWInst>(AI);
19190 const unsigned PtrOpIdx = RMW ? AtomicRMWInst::getPointerOperandIndex()
19192 Value *Addr = AI->getOperand(PtrOpIdx);
19193
19194 /// TODO: Only need to check private, then emit flat-known-not private (no
19195 /// need for shared block, or cast to global).
19197
19198 Align Alignment;
19199 if (RMW)
19200 Alignment = RMW->getAlign();
19201 else if (CX)
19202 Alignment = CX->getAlign();
19203 else
19204 llvm_unreachable("unhandled atomic operation");
19205
19206 // FullFlatEmulation is true if we need to issue the private, shared, and
19207 // global cases.
19208 //
19209 // If this is false, we are only dealing with the flat-targeting-private case,
19210 // where we only insert a check for private and still use the flat instruction
19211 // for global and shared.
19212
19213 bool FullFlatEmulation =
19214 RMW && RMW->getOperation() == AtomicRMWInst::FAdd &&
19215 ((Subtarget->hasAtomicFaddInsts() && RMW->getType()->isFloatTy()) ||
19216 (Subtarget->hasFlatBufferGlobalAtomicFaddF64Inst() &&
19217 RMW->getType()->isDoubleTy()));
19218
19219 // If the return value isn't used, do not introduce a false use in the phi.
19220 bool ReturnValueIsUsed = !AI->use_empty();
19221
19222 BasicBlock *BB = Builder.GetInsertBlock();
19223 Function *F = BB->getParent();
19224 BasicBlock *ExitBB =
19225 BB->splitBasicBlock(Builder.GetInsertPoint(), "atomicrmw.end");
19226 BasicBlock *SharedBB = nullptr;
19227
19228 BasicBlock *CheckPrivateBB = BB;
19229 if (FullFlatEmulation) {
19230 SharedBB = BasicBlock::Create(Ctx, "atomicrmw.shared", F, ExitBB);
19231 CheckPrivateBB =
19232 BasicBlock::Create(Ctx, "atomicrmw.check.private", F, ExitBB);
19233 }
19234
19235 BasicBlock *PrivateBB =
19236 BasicBlock::Create(Ctx, "atomicrmw.private", F, ExitBB);
19237 BasicBlock *GlobalBB = BasicBlock::Create(Ctx, "atomicrmw.global", F, ExitBB);
19238 BasicBlock *PhiBB = BasicBlock::Create(Ctx, "atomicrmw.phi", F, ExitBB);
19239
19240 std::prev(BB->end())->eraseFromParent();
19241 Builder.SetInsertPoint(BB);
19242
19243 Value *LoadedShared = nullptr;
19244 if (FullFlatEmulation) {
19245 CallInst *IsShared = Builder.CreateIntrinsic(Intrinsic::amdgcn_is_shared,
19246 {Addr}, nullptr, "is.shared");
19247 Builder.CreateCondBr(IsShared, SharedBB, CheckPrivateBB);
19248 Builder.SetInsertPoint(SharedBB);
19249 Value *CastToLocal = Builder.CreateAddrSpaceCast(
19251
19252 Instruction *Clone = AI->clone();
19253 Clone->insertInto(SharedBB, SharedBB->end());
19254 Clone->getOperandUse(PtrOpIdx).set(CastToLocal);
19255 LoadedShared = Clone;
19256
19257 Builder.CreateBr(PhiBB);
19258 Builder.SetInsertPoint(CheckPrivateBB);
19259 }
19260
19261 CallInst *IsPrivate = Builder.CreateIntrinsic(Intrinsic::amdgcn_is_private,
19262 {Addr}, nullptr, "is.private");
19263 Builder.CreateCondBr(IsPrivate, PrivateBB, GlobalBB);
19264
19265 Builder.SetInsertPoint(PrivateBB);
19266
19267 Value *CastToPrivate = Builder.CreateAddrSpaceCast(
19269
19270 Value *LoadedPrivate;
19271 if (RMW) {
19272 LoadedPrivate = Builder.CreateAlignedLoad(
19273 RMW->getType(), CastToPrivate, RMW->getAlign(), "loaded.private");
19274
19275 Value *NewVal = buildAtomicRMWValue(RMW->getOperation(), Builder,
19276 LoadedPrivate, RMW->getValOperand());
19277
19278 Builder.CreateAlignedStore(NewVal, CastToPrivate, RMW->getAlign());
19279 } else {
19280 auto [ResultLoad, Equal] =
19281 buildCmpXchgValue(Builder, CastToPrivate, CX->getCompareOperand(),
19282 CX->getNewValOperand(), CX->getAlign());
19283
19284 Value *Insert = Builder.CreateInsertValue(PoisonValue::get(CX->getType()),
19285 ResultLoad, 0);
19286 LoadedPrivate = Builder.CreateInsertValue(Insert, Equal, 1);
19287 }
19288
19289 Builder.CreateBr(PhiBB);
19290
19291 Builder.SetInsertPoint(GlobalBB);
19292
19293 // Continue using a flat instruction if we only emitted the check for private.
19294 Instruction *LoadedGlobal = AI;
19295 if (FullFlatEmulation) {
19296 Value *CastToGlobal = Builder.CreateAddrSpaceCast(
19298 AI->getOperandUse(PtrOpIdx).set(CastToGlobal);
19299 }
19300
19301 AI->removeFromParent();
19302 AI->insertInto(GlobalBB, GlobalBB->end());
19303
19304 // The new atomicrmw may go through another round of legalization later.
19305 if (!FullFlatEmulation) {
19306 // We inserted the runtime check already, make sure we do not try to
19307 // re-expand this.
19308 // TODO: Should union with any existing metadata.
19309 MDBuilder MDB(F->getContext());
19310 MDNode *RangeNotPrivate =
19313 LoadedGlobal->setMetadata(LLVMContext::MD_noalias_addrspace,
19314 RangeNotPrivate);
19315 }
19316
19317 Builder.CreateBr(PhiBB);
19318
19319 Builder.SetInsertPoint(PhiBB);
19320
19321 if (ReturnValueIsUsed) {
19322 PHINode *Loaded = Builder.CreatePHI(AI->getType(), 3);
19323 AI->replaceAllUsesWith(Loaded);
19324 if (FullFlatEmulation)
19325 Loaded->addIncoming(LoadedShared, SharedBB);
19326 Loaded->addIncoming(LoadedPrivate, PrivateBB);
19327 Loaded->addIncoming(LoadedGlobal, GlobalBB);
19328 Loaded->takeName(AI);
19329 }
19330
19331 Builder.CreateBr(ExitBB);
19332}
19333
19335 unsigned PtrOpIdx) {
19336 Value *PtrOp = I->getOperand(PtrOpIdx);
19339
19340 Type *FlatPtr = PointerType::get(I->getContext(), AMDGPUAS::FLAT_ADDRESS);
19341 Value *ASCast = CastInst::CreatePointerCast(PtrOp, FlatPtr, "scratch.ascast",
19342 I->getIterator());
19343 I->setOperand(PtrOpIdx, ASCast);
19344}
19345
19348
19351
19354 if (const auto *ConstVal = dyn_cast<Constant>(AI->getValOperand());
19355 ConstVal && ConstVal->isNullValue()) {
19356 // atomicrmw or %ptr, 0 -> atomicrmw add %ptr, 0
19358
19359 // We may still need the private-alias-flat handling below.
19360
19361 // TODO: Skip this for cases where we cannot access remote memory.
19362 }
19363 }
19364
19365 // The non-flat expansions should only perform the de-canonicalization of
19366 // identity values.
19368 return;
19369
19371}
19372
19379
19383
19385 "Expand Atomic Load only handles SCRATCH -> FLAT conversion");
19386}
19387
19389 if (SI->getPointerAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS)
19390 return convertScratchAtomicToFlatAtomic(SI, SI->getPointerOperandIndex());
19391
19393 "Expand Atomic Store only handles SCRATCH -> FLAT conversion");
19394}
19395
19396LoadInst *
19398 IRBuilder<> Builder(AI);
19399 auto Order = AI->getOrdering();
19400
19401 // The optimization removes store aspect of the atomicrmw. Therefore, cache
19402 // must be flushed if the atomic ordering had a release semantics. This is
19403 // not necessary a fence, a release fence just coincides to do that flush.
19404 // Avoid replacing of an atomicrmw with a release semantics.
19405 if (isReleaseOrStronger(Order))
19406 return nullptr;
19407
19408 LoadInst *LI = Builder.CreateAlignedLoad(
19409 AI->getType(), AI->getPointerOperand(), AI->getAlign());
19410 LI->setAtomic(Order, AI->getSyncScopeID());
19411 LI->copyMetadata(*AI);
19412 LI->takeName(AI);
19413 AI->replaceAllUsesWith(LI);
19414 AI->eraseFromParent();
19415 return LI;
19416}
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
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:54
#define I(x, y, z)
Definition MD5.cpp:57
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:1258
#define FP_DENORM_FLUSH_IN_FLUSH_OUT
Definition SIDefines.h:1255
static void reservePrivateMemoryRegs(const TargetMachine &TM, MachineFunction &MF, const SIRegisterInfo &TRI, SIMachineFunctionInfo &Info)
static void getCoopAtomicOperandsInfo(const CallBase &CI, bool IsLoad, TargetLoweringBase::IntrinsicInfo &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 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 isFloatingPointWaveReduceOperation(unsigned Opc)
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 SDValue lowerWaveShuffle(const SITargetLowering &TLI, SDNode *N, SelectionDAG &DAG)
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:487
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
AMDGPUTargetLowering(const TargetMachine &TM, const TargetSubtargetInfo &STI, const AMDGPUSubtarget &AMDGPUSTI)
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.
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
static int64_t getNullPointerValue(unsigned AddrSpace)
Get the integer value of a null pointer in the given address space.
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:6053
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:1392
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition APInt.h:1386
static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit)
Get a value with a block of bits set.
Definition APInt.h:259
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:381
bool isSignMask() const
Check if the APInt's value is returned by getSignMask.
Definition APInt.h:467
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition APInt.h:1640
bool isOneBitSet(unsigned BitNo) const
Determine if this APInt Value only has the specified bit set.
Definition APInt.h:367
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition APInt.h:297
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition APInt.h:1238
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1222
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:40
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
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
@ USubCond
Subtract only if no unsigned overflow.
@ Min
*p = old <signed v ? old : v
@ Sub
*p = old - v
@ And
*p = old & v
@ Xor
*p = old ^ v
@ USubSat
*p = usub.sat(old, v) usub.sat matches the behavior of llvm.usub.sat.
@ 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.
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
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
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:219
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:64
LLVM_ABI Align getABITypeAlign(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
bool isBigEndian() const
Definition DataLayout.h:215
LLVM_ABI 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:123
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:765
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:806
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
bool supportsWaveWideBPermute() const
unsigned getMaxPrivateElementSize(bool ForBufferRSrc=false) const
bool isWave64() 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:2794
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:41
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.
PseudoSourceValueManager & getPSVManager() const
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:226
bool doesNotAccessMemory() const
Whether this function accesses no memory.
Definition ModRef.h:220
bool onlyReadsMemory() const
Whether this function only (at most) reads memory.
Definition ModRef.h:223
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
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.
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.
LLVM_ABI const PseudoSourceValue * getConstantPool()
Return a pseudo source value referencing the constant pool.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
static Register index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
Definition Register.h:72
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:83
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.
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,.
bool getTgtMemIntrinsic(IntrinsicInfo &, const CallBase &, MachineFunction &MF, unsigned IntrinsicID) const override
Given an intrinsic, checks if on the target the intrinsic will need to map to a MemIntrinsicNode (tou...
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
SDValue splitBinaryVectorOp(SDValue Op, SelectionDAG &DAG) const
MachinePointerInfo getKernargSegmentPtrInfo(MachineFunction &MF) 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 bool SignBitIsZeroFP(SDValue Op, unsigned Depth=0) const
Return true if the sign bit of Op is known to be zero, for a floating-point value.
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...
MachineFunctionAnalysisManager * getMFAM()
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.
CodeGenOptLevel getOptLevel() const
Returns the optimization level: None, Less, Default, or Aggressive.
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:423
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:343
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:296
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:106
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 unsigned getOperandNo() const
Return the operand # of this use in its User.
Definition Use.cpp: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
const Use & getOperandUse(unsigned i) const
Definition User.h:246
Value * getOperand(unsigned i) const
Definition User.h:233
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:553
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:1106
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:403
Type * getElementType() const
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
constexpr bool isZero() const
Definition TypeSize.h:153
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.
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:813
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition ISDOpcodes.h:256
@ STACKSAVE
STACKSAVE - STACKSAVE has one operand, an input chain.
@ CTLZ_ZERO_UNDEF
Definition ISDOpcodes.h:782
@ PTRADD
PTRADD represents pointer arithmetic semantics, for targets that opt in using shouldPreservePtrArith(...
@ DELETED_NODE
DELETED_NODE - This is an illegal value that is used to catch errors.
Definition ISDOpcodes.h:45
@ SET_FPENV
Sets the current floating-point environment.
@ 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:595
@ BSWAP
Byte Swap and Counting operators.
Definition ISDOpcodes.h:773
@ ATOMIC_STORE
OUTCHAIN = ATOMIC_STORE(INCHAIN, val, ptr) This corresponds to "store atomic" instruction.
@ FMAD
FMAD - Perform a * b + c, while getting the same result as the separately rounded operations.
Definition ISDOpcodes.h:517
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:259
@ LOAD
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition ISDOpcodes.h:847
@ ATOMIC_LOAD_USUB_COND
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition ISDOpcodes.h:513
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition ISDOpcodes.h:215
@ GlobalAddress
Definition ISDOpcodes.h:88
@ ATOMIC_CMP_SWAP_WITH_SUCCESS
Val, Success, OUTCHAIN = ATOMIC_CMP_SWAP_WITH_SUCCESS(INCHAIN, ptr, cmp, swap) N.b.
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition ISDOpcodes.h:874
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition ISDOpcodes.h:579
@ FADD
Simple binary floating point operators.
Definition ISDOpcodes.h:412
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition ISDOpcodes.h:741
@ FP16_TO_FP
FP16_TO_FP, FP_TO_FP16 - These operators are used to perform promotions and truncation for half-preci...
Definition ISDOpcodes.h:997
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
Definition ISDOpcodes.h:987
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition ISDOpcodes.h:249
@ FLDEXP
FLDEXP - ldexp, inspired by libm (op0 * 2**op1).
@ BUILTIN_OP_END
BUILTIN_OP_END - This must be the last enum value in this list.
@ ATOMIC_LOAD_USUB_SAT
@ SET_ROUNDING
Set rounding mode.
Definition ISDOpcodes.h:969
@ CONVERGENCECTRL_GLUE
This does not correspond to any convergence control intrinsic.
@ SIGN_EXTEND
Conversion operators.
Definition ISDOpcodes.h:838
@ SCALAR_TO_VECTOR
SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a scalar value into element 0 of the...
Definition ISDOpcodes.h:659
@ READSTEADYCOUNTER
READSTEADYCOUNTER - This corresponds to the readfixedcounter intrinsic.
@ BR
Control flow instructions. These all have token chains.
@ CTTZ_ZERO_UNDEF
Bit counting operators with an undefined result for zero inputs.
Definition ISDOpcodes.h:781
@ PREFETCH
PREFETCH - This corresponds to a prefetch intrinsic.
@ FSINCOS
FSINCOS - Compute both fsin and fcos as a single operation.
@ FNEG
Perform various unary floating-point operations inspired by libm.
@ BR_CC
BR_CC - Conditional branch.
@ SSUBO
Same for subtraction.
Definition ISDOpcodes.h:347
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition ISDOpcodes.h:536
@ IS_FPCLASS
Performs a check of floating point class property, defined by IEEE-754.
Definition ISDOpcodes.h:543
@ 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:790
@ ATOMIC_LOAD
Val, OUTCHAIN = ATOMIC_LOAD(INCHAIN, ptr) This corresponds to "load atomic" instruction.
@ 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:964
@ MULHU
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition ISDOpcodes.h:698
@ GET_FPMODE
Reads the current dynamic floating-point control modes.
@ GET_FPENV
Gets the current floating-point environment.
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:759
@ VECTOR_SHUFFLE
VECTOR_SHUFFLE(VEC1, VEC2) - Returns a vector, of the same type as VEC1/VEC2.
Definition ISDOpcodes.h:644
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition ISDOpcodes.h:609
@ FMINNUM_IEEE
FMINNUM_IEEE/FMAXNUM_IEEE - Perform floating-point minimumNumber or maximumNumber on two values,...
@ EXTRACT_VECTOR_ELT
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition ISDOpcodes.h:571
@ 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:844
@ DEBUGTRAP
DEBUGTRAP - Trap intended to get the attention of a debugger.
@ SELECT_CC
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition ISDOpcodes.h:805
@ ATOMIC_CMP_SWAP
Val, OUTCHAIN = ATOMIC_CMP_SWAP(INCHAIN, ptr, cmp, swap) For double-word atomic operations: ValLo,...
@ FMINNUM
FMINNUM/FMAXNUM - Perform floating-point minimum maximum on two values, following IEEE-754 definition...
@ SMULO
Same for multiplication.
Definition ISDOpcodes.h:351
@ DYNAMIC_STACKALLOC
DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned to a specified boundary.
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition ISDOpcodes.h:882
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition ISDOpcodes.h:721
@ FP_EXTEND
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition ISDOpcodes.h:972
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition ISDOpcodes.h:799
@ UADDO_CARRY
Carry-using nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:323
@ INLINEASM_BR
INLINEASM_BR - Branching version of inline asm. Used by asm-goto.
@ BF16_TO_FP
BF16_TO_FP, FP_TO_BF16 - These operators are used to perform promotions and truncation for bfloat16.
@ ATOMIC_LOAD_UDEC_WRAP
@ STRICT_FP_ROUND
X = STRICT_FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision ...
Definition ISDOpcodes.h:495
@ FMINIMUM
FMINIMUM/FMAXIMUM - NaN-propagating minimum/maximum that also treat -0.0 as less than 0....
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:920
@ READCYCLECOUNTER
READCYCLECOUNTER - This corresponds to the readcyclecounter intrinsic.
@ STRICT_FP_EXTEND
X = STRICT_FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition ISDOpcodes.h:500
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:733
@ TRAP
TRAP - Trapping instruction.
@ 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:560
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition ISDOpcodes.h:53
@ ATOMIC_SWAP
Val, OUTCHAIN = ATOMIC_SWAP(INCHAIN, ptr, amt) Val, OUTCHAIN = ATOMIC_LOAD_[OpName](INCHAIN,...
@ ExternalSymbol
Definition ISDOpcodes.h:93
@ FFREXP
FFREXP - frexp, extract fractional and exponent component of a floating-point value.
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition ISDOpcodes.h:953
@ ADDRSPACECAST
ADDRSPACECAST - This operator converts between pointers of different address spaces.
Definition ISDOpcodes.h:991
@ INLINEASM
INLINEASM - Represents an inline asm block.
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:850
@ BRCOND
BRCOND - Conditional branch.
@ SHL_PARTS
SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded integer shift operations.
Definition ISDOpcodes.h:827
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition ISDOpcodes.h:62
@ ATOMIC_LOAD_UINC_WRAP
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition ISDOpcodes.h:529
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition ISDOpcodes.h:360
@ FMINIMUMNUM
FMINIMUMNUM/FMAXIMUMNUM - minimumnum/maximumnum that is same with FMINNUM_IEEE and FMAXNUM_IEEE besid...
@ 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:551
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.
Definition Types.h:26
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:532
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:237
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:1737
OuterAnalysisManagerProxy< ModuleAnalysisManager, MachineFunction > ModuleAnalysisManagerMachineFunctionProxy
Provide the ModuleAnalysisManager to Function proxy.
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:2184
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:301
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:1744
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:203
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:74
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:434
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:1770
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:1945
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:872
#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
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
EVT changeElementType(LLVMContext &Context, 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
bool isScalarInteger() const
Return true if this is an integer, but not a vector.
Definition ValueTypes.h:157
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.
MachinePointerInfo getWithOffset(int64_t O) const
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