LLVM 24.0.0git
NVPTXISelLowering.cpp
Go to the documentation of this file.
1//===-- NVPTXISelLowering.cpp - NVPTX 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// This file defines the interfaces that NVPTX uses to lower LLVM code into a
10// selection DAG.
11//
12//===----------------------------------------------------------------------===//
13
14#include "NVPTXISelLowering.h"
16#include "NVPTX.h"
19#include "NVPTXSubtarget.h"
20#include "NVPTXTargetMachine.h"
22#include "NVPTXUtilities.h"
23#include "NVVMProperties.h"
24#include "llvm/ADT/APFloat.h"
25#include "llvm/ADT/APInt.h"
26#include "llvm/ADT/STLExtras.h"
28#include "llvm/ADT/StringRef.h"
42#include "llvm/IR/Argument.h"
43#include "llvm/IR/Attributes.h"
44#include "llvm/IR/Constants.h"
45#include "llvm/IR/DataLayout.h"
48#include "llvm/IR/FPEnv.h"
49#include "llvm/IR/Function.h"
50#include "llvm/IR/GlobalValue.h"
51#include "llvm/IR/IRBuilder.h"
52#include "llvm/IR/Instruction.h"
54#include "llvm/IR/IntrinsicsNVPTX.h"
55#include "llvm/IR/Module.h"
57#include "llvm/IR/Type.h"
58#include "llvm/IR/Value.h"
59#include "llvm/MC/MCContext.h"
60#include "llvm/MC/MCSymbol.h"
71#include <algorithm>
72#include <cassert>
73#include <cmath>
74#include <cstdint>
75#include <iterator>
76#include <optional>
77#include <tuple>
78#include <utility>
79#include <vector>
80
81#define DEBUG_TYPE "nvptx-lower"
82
83using namespace llvm;
84
86 "nvptx-sched4reg",
87 cl::desc("NVPTX Specific: schedule for register pressue"), cl::init(false));
88
90 "nvptx-fma-level", cl::Hidden,
91 cl::desc("NVPTX Specific: FMA contraction (0: don't do it"
92 " 1: do it 2: do it aggressively"),
93 cl::init(2));
94
96 "nvptx-prec-divf32", cl::Hidden,
98 "NVPTX Specific: Override the precision of the lowering for f32 fdiv"),
100 clEnumValN(NVPTX::DivPrecisionLevel::Approx, "0", "Use div.approx"),
101 clEnumValN(NVPTX::DivPrecisionLevel::Full, "1", "Use div.full"),
103 "Use IEEE Compliant F32 div.rnd if available (default)"),
105 "Use IEEE Compliant F32 div.rnd if available, no FTZ")),
107
109 "nvptx-prec-sqrtf32", cl::Hidden,
110 cl::desc("NVPTX Specific: 0 use sqrt.approx, 1 use sqrt.rn."),
111 cl::init(true));
112
113// PTX atom.add.f32 has fixed FTZ behavior that may not match the function's
114// (see shouldExpandAtomicRMWInIR), so we'd normally fall back to a CAS loop
115// when they disagree. This option (enabled by default) allows using atom.add
116// anyway, trading correct denormal handling for the speed of the native
117// instruction.
119 "nvptx-allow-ftz-atomics", cl::Hidden,
120 cl::desc("NVPTX Specific: Lower atomicrmw fadd to atom.add even when its "
121 "FTZ behavior does not match the function's denormal mode."),
122 cl::init(true));
123
124/// Whereas CUDA's implementation (see libdevice) uses ex2.approx for exp2(), it
125/// does NOT use lg2.approx for log2, so this is disabled by default.
127 "nvptx-approx-log2f32",
128 cl::desc("NVPTX Specific: whether to use lg2.approx for log2"),
129 cl::init(false));
130
133 const SDNode &N) const {
134 // If nvptx-prec-div32=N is used on the command-line, always honor it
135 if (UsePrecDivF32.getNumOccurrences() > 0)
136 return UsePrecDivF32;
137
138 const SDNodeFlags Flags = N.getFlags();
139 if (Flags.hasApproximateFuncs())
141
143}
144
146 // If nvptx-prec-sqrtf32 is used on the command-line, always honor it
147 if (UsePrecSqrtF32.getNumOccurrences() > 0)
148 return UsePrecSqrtF32;
149
150 if (N) {
151 const SDNodeFlags Flags = N->getFlags();
152 if (Flags.hasApproximateFuncs())
153 return false;
154 }
155
156 return true;
157}
158
163
164static bool IsPTXVectorType(MVT VT) {
165 switch (VT.SimpleTy) {
166 default:
167 return false;
168 case MVT::v2i1:
169 case MVT::v4i1:
170 case MVT::v2i8:
171 case MVT::v4i8:
172 case MVT::v8i8: // <2 x i8x4>
173 case MVT::v16i8: // <4 x i8x4>
174 case MVT::v2i16:
175 case MVT::v4i16:
176 case MVT::v8i16: // <4 x i16x2>
177 case MVT::v2i32:
178 case MVT::v4i32:
179 case MVT::v2i64:
180 case MVT::v2f16:
181 case MVT::v4f16:
182 case MVT::v8f16: // <4 x f16x2>
183 case MVT::v2bf16:
184 case MVT::v4bf16:
185 case MVT::v8bf16: // <4 x bf16x2>
186 case MVT::v2f32:
187 case MVT::v4f32:
188 case MVT::v2f64:
189 case MVT::v4i64:
190 case MVT::v4f64:
191 case MVT::v8i32:
192 case MVT::v8f32:
193 case MVT::v16f16: // <8 x f16x2>
194 case MVT::v16bf16: // <8 x bf16x2>
195 case MVT::v16i16: // <8 x i16x2>
196 case MVT::v32i8: // <8 x i8x4>
197 return true;
198 }
199}
200
201// When legalizing vector loads/stores, this function is called, which does two
202// things:
203// 1. Determines Whether the vector is something we want to custom lower,
204// std::nullopt is returned if we do not want to custom lower it.
205// 2. If we do want to handle it, returns two parameters:
206// - unsigned int NumElts - The number of elements in the final vector
207// - EVT EltVT - The type of the elements in the final vector
208static std::optional<std::pair<unsigned int, MVT>>
210 unsigned AddressSpace) {
211 const bool CanLowerTo256Bit = STI.has256BitVectorLoadStore(AddressSpace);
212
213 if (CanLowerTo256Bit && VectorEVT.isScalarInteger() &&
214 VectorEVT.getSizeInBits() == 256)
215 return {{4, MVT::i64}};
216
217 if (!VectorEVT.isSimple())
218 return std::nullopt;
219 const MVT VectorVT = VectorEVT.getSimpleVT();
220
221 if (!VectorVT.isVector()) {
222 if (VectorVT == MVT::i128 || VectorVT == MVT::f128)
223 return {{2, MVT::i64}};
224 return std::nullopt;
225 }
226
227 const MVT EltVT = VectorVT.getVectorElementType();
228 const unsigned NumElts = VectorVT.getVectorNumElements();
229
230 // The size of the PTX virtual register that holds a packed type.
231 unsigned PackRegSize;
232
233 // We only handle "native" vector sizes for now, e.g. <4 x double> is not
234 // legal. We can (and should) split that into 2 stores of <2 x double> here
235 // but I'm leaving that as a TODO for now.
236 switch (VectorVT.SimpleTy) {
237 default:
238 return std::nullopt;
239
240 case MVT::v4i64:
241 case MVT::v4f64:
242 // This is a "native" vector type iff the address space is global and the
243 // target supports 256-bit loads/stores
244 if (!CanLowerTo256Bit)
245 return std::nullopt;
246 [[fallthrough]];
247 case MVT::v2i8:
248 case MVT::v2i64:
249 case MVT::v2f64:
250 // This is a "native" vector type
251 return std::pair(NumElts, EltVT);
252
253 case MVT::v16f16: // <8 x f16x2>
254 case MVT::v16bf16: // <8 x bf16x2>
255 case MVT::v16i16: // <8 x i16x2>
256 case MVT::v32i8: // <8 x i8x4>
257 // This can be upsized into a "native" vector type iff the address space is
258 // global and the target supports 256-bit loads/stores.
259 if (!CanLowerTo256Bit)
260 return std::nullopt;
261 [[fallthrough]];
262 case MVT::v2i16: // <1 x i16x2>
263 case MVT::v2f16: // <1 x f16x2>
264 case MVT::v2bf16: // <1 x bf16x2>
265 case MVT::v4i8: // <1 x i8x4>
266 case MVT::v4i16: // <2 x i16x2>
267 case MVT::v4f16: // <2 x f16x2>
268 case MVT::v4bf16: // <2 x bf16x2>
269 case MVT::v8i8: // <2 x i8x4>
270 case MVT::v8f16: // <4 x f16x2>
271 case MVT::v8bf16: // <4 x bf16x2>
272 case MVT::v8i16: // <4 x i16x2>
273 case MVT::v16i8: // <4 x i8x4>
274 PackRegSize = 32;
275 break;
276
277 case MVT::v8f32: // <4 x f32x2>
278 case MVT::v8i32: // <4 x i32x2>
279 // This is a "native" vector type iff the address space is global and the
280 // target supports 256-bit loads/stores
281 if (!CanLowerTo256Bit)
282 return std::nullopt;
283 [[fallthrough]];
284 case MVT::v2f32: // <1 x f32x2>
285 case MVT::v4f32: // <2 x f32x2>
286 case MVT::v2i32: // <1 x i32x2>
287 case MVT::v4i32: // <2 x i32x2>
288 if (!STI.hasF32x2Instructions())
289 return std::pair(NumElts, EltVT);
290 PackRegSize = 64;
291 break;
292 }
293
294 // If we reach here, then we can pack 2 or more elements into a single 32-bit
295 // or 64-bit PTX register and treat the vector as a new vector containing
296 // packed elements.
297
298 // Number of elements to pack in one word.
299 const unsigned NPerReg = PackRegSize / EltVT.getSizeInBits();
300
301 return std::pair(NumElts / NPerReg, MVT::getVectorVT(EltVT, NPerReg));
302}
303
304/// ComputePTXValueVTs - For the given Type \p Ty, returns the set of primitive
305/// legal-ish MVTs that compose it. Unlike ComputeValueVTs, this will legalize
306/// the types as required by the calling convention (with special handling for
307/// i8s).
308/// NOTE: This is a band-aid for code that expects ComputeValueVTs to return the
309/// same number of types as the Ins/Outs arrays in LowerFormalArguments,
310/// LowerCall, and LowerReturn.
311static void ComputePTXValueVTs(const TargetLowering &TLI, const DataLayout &DL,
312 LLVMContext &Ctx, CallingConv::ID CallConv,
313 Type *Ty, SmallVectorImpl<EVT> &ValueVTs,
315 uint64_t StartingOffset = 0) {
316 SmallVector<EVT, 16> TempVTs;
317 SmallVector<uint64_t, 16> TempOffsets;
318 ComputeValueVTs(TLI, DL, Ty, TempVTs, /*MemVTs=*/nullptr, &TempOffsets,
319 StartingOffset);
320
321 for (const auto [VT, Off] : zip(TempVTs, TempOffsets)) {
322 MVT RegisterVT = TLI.getRegisterTypeForCallingConv(Ctx, CallConv, VT);
323 unsigned NumRegs = TLI.getNumRegistersForCallingConv(Ctx, CallConv, VT);
324
325 // Since we actually can load/store b8, we need to ensure that we'll use
326 // the original sized type for any i8s or i8 vectors.
327 if (VT.getScalarType() == MVT::i8) {
328 if (RegisterVT == MVT::i16)
329 RegisterVT = MVT::i8;
330 else if (RegisterVT == MVT::v2i16)
331 RegisterVT = MVT::v2i8;
332 else
333 assert(RegisterVT == MVT::v4i8 &&
334 "Expected v4i8, v2i16, or i16 for i8 RegisterVT");
335 }
336
337 // TODO: This is horribly incorrect for cases where the vector elements are
338 // not a multiple of bytes (ex i1) and legal or i8. However, this problem
339 // has existed for as long as NVPTX has and no one has complained, so we'll
340 // leave it for now.
341 for (unsigned I : seq(NumRegs)) {
342 ValueVTs.push_back(RegisterVT);
343 Offsets.push_back(Off + I * RegisterVT.getStoreSize());
344 }
345 }
346}
347
348// We return an EVT that can hold N VTs
349// If the VT is a vector, the resulting EVT is a flat vector with the same
350// element type as VT's element type.
351static EVT getVectorizedVT(EVT VT, unsigned N, LLVMContext &C) {
352 if (N == 1)
353 return VT;
354
355 return VT.isVector() ? EVT::getVectorVT(C, VT.getScalarType(),
356 VT.getVectorNumElements() * N)
357 : EVT::getVectorVT(C, VT, N);
358}
359
361 const SDLoc &dl, SelectionDAG &DAG) {
362 if (V.getValueType() == VT) {
363 assert(I == 0 && "Index must be 0 for scalar value");
364 return V;
365 }
366
367 if (!VT.isVector())
368 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, V,
369 DAG.getVectorIdxConstant(I, dl));
370
371 return DAG.getNode(
372 ISD::EXTRACT_SUBVECTOR, dl, VT, V,
374}
375
376template <typename T>
377static inline SDValue getBuildVectorizedValue(unsigned N, const SDLoc &dl,
378 SelectionDAG &DAG, T GetElement) {
379 if (N == 1)
380 return GetElement(0);
381
383 for (const unsigned I : llvm::seq(N)) {
384 SDValue Val = GetElement(I);
385 if (Val.getValueType().isVector())
387 else
388 Values.push_back(Val);
389 }
390
391 EVT VT = EVT::getVectorVT(*DAG.getContext(), Values[0].getValueType(),
392 Values.size());
393 return DAG.getBuildVector(VT, dl, Values);
394}
395
396/// PromoteScalarIntegerPTX
397/// Used to make sure the arguments/returns are suitable for passing
398/// and promote them to a larger size if they're not.
399///
400/// The promoted type is placed in \p PromoteVT if the function returns true.
402 if (VT.isScalarInteger()) {
403 switch (PowerOf2Ceil(VT.getFixedSizeInBits())) {
404 default:
406 "Promotion is not suitable for scalars of size larger than 64-bits");
407 case 1:
408 return MVT::i1;
409 case 2:
410 case 4:
411 case 8:
412 return MVT::i8;
413 case 16:
414 return MVT::i16;
415 case 32:
416 return MVT::i32;
417 case 64:
418 return MVT::i64;
419 }
420 }
421 return VT;
422}
423
424// Check whether we can merge loads/stores of some of the pieces of a
425// flattened function parameter or return value into a single vector
426// load/store.
427//
428// The flattened parameter is represented as a list of EVTs and
429// offsets, and the whole structure is aligned to ParamAlignment. This
430// function determines whether we can load/store pieces of the
431// parameter starting at index Idx using a single vectorized op of
432// size AccessSize. If so, it returns the number of param pieces
433// covered by the vector op. Otherwise, it returns 1.
434template <typename T>
436 unsigned Idx, uint32_t AccessSize, const SmallVectorImpl<EVT> &ValueVTs,
437 const SmallVectorImpl<T> &Offsets, Align ParamAlignment) {
438
439 // Can't vectorize if param alignment is not sufficient.
440 if (ParamAlignment < AccessSize)
441 return 1;
442 // Can't vectorize if offset is not aligned.
443 if (Offsets[Idx] & (AccessSize - 1))
444 return 1;
445
446 EVT EltVT = ValueVTs[Idx];
447 unsigned EltSize = EltVT.getStoreSize();
448
449 // Element is too large to vectorize.
450 if (EltSize >= AccessSize)
451 return 1;
452
453 unsigned NumElts = AccessSize / EltSize;
454 // Can't vectorize if AccessBytes if not a multiple of EltSize.
455 if (AccessSize != EltSize * NumElts)
456 return 1;
457
458 // We don't have enough elements to vectorize.
459 if (Idx + NumElts > ValueVTs.size())
460 return 1;
461
462 // PTX ISA can only deal with 2- and 4-element vector ops.
463 if (NumElts != 4 && NumElts != 2)
464 return 1;
465
466 for (unsigned j = Idx + 1; j < Idx + NumElts; ++j) {
467 // Types do not match.
468 if (ValueVTs[j] != EltVT)
469 return 1;
470
471 // Elements are not contiguous.
472 if (Offsets[j] - Offsets[j - 1] != EltSize)
473 return 1;
474 }
475 // OK. We can vectorize ValueVTs[i..i+NumElts)
476 return NumElts;
477}
478
479// Computes whether and how we can vectorize the loads/stores of a
480// flattened function parameter or return value.
481//
482// The flattened parameter is represented as the list of ValueVTs and
483// Offsets, and is aligned to ParamAlignment bytes. We return a vector
484// of the same size as ValueVTs indicating how each piece should be
485// loaded/stored (i.e. as a scalar, or as part of a vector
486// load/store).
487template <typename T>
490 const SmallVectorImpl<T> &Offsets, Align ParamAlignment,
491 bool IsVAArg = false) {
492 // Set vector size to match ValueVTs and mark all elements as
493 // scalars by default.
494
495 if (IsVAArg)
496 return SmallVector<unsigned>(ValueVTs.size(), 1);
497
498 SmallVector<unsigned, 16> VectorInfo;
499
500 const auto GetNumElts = [&](unsigned I) -> unsigned {
501 for (const unsigned AccessSize : {16, 8, 4, 2}) {
502 const unsigned NumElts = canMergeParamLoadStoresStartingAt(
503 I, AccessSize, ValueVTs, Offsets, ParamAlignment);
504 assert((NumElts == 1 || NumElts == 2 || NumElts == 4) &&
505 "Unexpected vectorization size");
506 if (NumElts != 1)
507 return NumElts;
508 }
509 return 1;
510 };
511
512 // Check what we can vectorize using 128/64/32-bit accesses.
513 for (unsigned I = 0, E = ValueVTs.size(); I != E;) {
514 const unsigned NumElts = GetNumElts(I);
515 VectorInfo.push_back(NumElts);
516 I += NumElts;
517 }
518 assert(std::accumulate(VectorInfo.begin(), VectorInfo.end(), 0u) ==
519 ValueVTs.size());
520 return VectorInfo;
521}
522
523// NVPTXTargetLowering Constructor.
525 const NVPTXSubtarget &STI)
526 : TargetLowering(TM, STI), STI(STI), GlobalUniqueCallSite(0) {
527 // always lower memset, memcpy, and memmove intrinsics to load/store
528 // instructions, rather
529 // then generating calls to memset, mempcy or memmove.
533
536
537 // Jump is Expensive. Don't create extra control flow for 'and', 'or'
538 // condition branches.
539 setJumpIsExpensive(true);
540
541 // Wide divides are _very_ slow. Try to reduce the width of the divide if
542 // possible.
543 addBypassSlowDiv(64, 32);
544
545 // By default, use the Source scheduling
546 if (sched4reg)
548 else
550
551 auto setFP16OperationAction = [&](unsigned Op, MVT VT, LegalizeAction Action,
552 LegalizeAction NoF16Action) {
553 bool IsOpSupported = STI.allowFP16Math();
554 switch (Op) {
555 // Several FP16 instructions are available on sm_80 only.
556 case ISD::FMINNUM:
557 case ISD::FMAXNUM:
560 case ISD::FMAXIMUM:
561 case ISD::FMINIMUM:
562 case ISD::FMAXIMUMNUM:
563 case ISD::FMINIMUMNUM:
564 IsOpSupported &= STI.hasFeature(NVPTX::SM80);
565 break;
566 case ISD::FEXP2:
567 case ISD::FTANH:
568 IsOpSupported &=
569 STI.hasFeature(NVPTX::SM75) && STI.hasFeature(NVPTX::PTX70);
570 break;
571 }
572 setOperationAction(Op, VT, IsOpSupported ? Action : NoF16Action);
573 };
574
575 auto setBF16OperationAction = [&](unsigned Op, MVT VT, LegalizeAction Action,
576 LegalizeAction NoBF16Action) {
577 bool IsOpSupported = STI.hasNativeBF16Support(Op);
579 Op, VT, IsOpSupported ? Action : NoBF16Action);
580 };
581
582 auto setI16x2OperationAction = [&](unsigned Op, MVT VT, LegalizeAction Action,
583 LegalizeAction NoI16x2Action) {
584 bool IsOpSupported = false;
585 // instructions are available on sm_90 only
586 switch (Op) {
587 case ISD::ADD:
588 case ISD::SMAX:
589 case ISD::SMIN:
590 case ISD::UMIN:
591 case ISD::UMAX:
592 IsOpSupported =
593 STI.hasFeature(NVPTX::SM90) && STI.hasFeature(NVPTX::PTX80);
594 break;
595 }
596 setOperationAction(Op, VT, IsOpSupported ? Action : NoI16x2Action);
597 };
598
599 addRegisterClass(MVT::i1, &NVPTX::B1RegClass);
600 addRegisterClass(MVT::i16, &NVPTX::B16RegClass);
601 addRegisterClass(MVT::v2i16, &NVPTX::B32RegClass);
602 addRegisterClass(MVT::v4i8, &NVPTX::B32RegClass);
603 addRegisterClass(MVT::i32, &NVPTX::B32RegClass);
604 addRegisterClass(MVT::i64, &NVPTX::B64RegClass);
605 addRegisterClass(MVT::f32, &NVPTX::B32RegClass);
606 addRegisterClass(MVT::f64, &NVPTX::B64RegClass);
607 addRegisterClass(MVT::f16, &NVPTX::B16RegClass);
608 addRegisterClass(MVT::v2f16, &NVPTX::B32RegClass);
609 addRegisterClass(MVT::bf16, &NVPTX::B16RegClass);
610 addRegisterClass(MVT::v2bf16, &NVPTX::B32RegClass);
611
612 if (STI.hasF32x2Instructions()) {
613 addRegisterClass(MVT::v2f32, &NVPTX::B64RegClass);
614 addRegisterClass(MVT::v2i32, &NVPTX::B64RegClass);
615 }
616
617 // Conversion to/from FP16/FP16x2 is always legal.
622
624 if (STI.hasFeature(NVPTX::SM30))
626
627 setFP16OperationAction(ISD::SETCC, MVT::f16, Legal, Promote);
628 setFP16OperationAction(ISD::SETCC, MVT::v2f16, Legal, Expand);
629
630 // Conversion to/from BFP16/BFP16x2 is always legal.
635
636 setBF16OperationAction(ISD::SETCC, MVT::v2bf16, Legal, Expand);
637 setBF16OperationAction(ISD::SETCC, MVT::bf16, Legal, Promote);
638 if (getOperationAction(ISD::SETCC, MVT::bf16) == Promote)
639 AddPromotedToType(ISD::SETCC, MVT::bf16, MVT::f32);
640
641 // Conversion to/from i16/i16x2 is always legal.
646
651
652 // No support for these operations with v2f32/v2i32
653 setOperationAction(ISD::INSERT_VECTOR_ELT, {MVT::v2f32, MVT::v2i32}, Expand);
654 setOperationAction(ISD::VECTOR_SHUFFLE, {MVT::v2f32, MVT::v2i32}, Expand);
655
658 MVT::v2i32, Expand);
659
660 // Need custom lowering in case the index is dynamic.
661 if (STI.hasF32x2Instructions())
662 setOperationAction(ISD::EXTRACT_VECTOR_ELT, {MVT::v2f32, MVT::v2i32},
663 Custom);
664
665 // Custom conversions to/from v2i8.
667
668 // Only logical ops can be done on v4i8/v2i32 directly, others must be done
669 // elementwise.
686 {MVT::v4i8, MVT::v2i32}, Expand);
687
688 // Operations not directly supported by NVPTX.
689 for (MVT VT : {MVT::bf16, MVT::f16, MVT::v2bf16, MVT::v2f16, MVT::f32,
690 MVT::v2f32, MVT::f64, MVT::i1, MVT::i8, MVT::i16, MVT::v2i16,
691 MVT::v4i8, MVT::i32, MVT::v2i32, MVT::i64}) {
694 }
695
696 setOperationAction(ISD::SDIVREM, {MVT::i32, MVT::i64}, Expand);
697 setOperationAction(ISD::UDIVREM, {MVT::i32, MVT::i64}, Expand);
698
699 // We don't want ops like FMINIMUM or UMAX to be lowered to SETCC+VSELECT.
700 setOperationAction(ISD::VSELECT, {MVT::v2f32, MVT::v2i32}, Expand);
701
702 // Some SIGN_EXTEND_INREG can be done using cvt instruction.
703 // For others we will expand to a SHL/SRA pair.
709 setOperationAction(ISD::SIGN_EXTEND_INREG, {MVT::v2i16, MVT::v2i32}, Expand);
710
717
718 if (STI.hasCLMAD())
722
724 {MVT::i8, MVT::i16, MVT::v2i16, MVT::i32, MVT::i64},
725 Expand);
726
727 if (STI.hasHWROT32()) {
730 Custom);
731 }
732
733 setOperationAction(ISD::BR_JT, MVT::Other, STI.hasBrx() ? Legal : Expand);
735
736 // We want to legalize constant related memmove and memcopy
737 // intrinsics.
739
740 // FP extload/truncstore is not legal in PTX. We need to expand all these.
741 for (auto FloatVTs :
743 for (MVT ValVT : FloatVTs) {
744 for (MVT MemVT : FloatVTs) {
745 setLoadExtAction(ISD::EXTLOAD, ValVT, MemVT, Expand);
746 setTruncStoreAction(ValVT, MemVT, Expand);
747 }
748 }
749 }
750
751 // To improve CodeGen we'll legalize any-extend loads to zext loads. This is
752 // how they'll be lowered in ISel anyway, and by doing this a little earlier
753 // we allow for more DAG combine opportunities.
754 for (auto IntVTs :
756 for (MVT ValVT : IntVTs)
757 for (MVT MemVT : IntVTs)
758 if (isTypeLegal(ValVT))
759 setLoadExtAction(ISD::EXTLOAD, ValVT, MemVT, Custom);
760
761 // PTX does not support load / store predicate registers
763 for (MVT VT : MVT::integer_valuetypes()) {
765 Promote);
766 setTruncStoreAction(VT, MVT::i1, Expand);
767 }
768
769 // Disable generations of extload/truncstore for v2i32/v2i16/v2i8. The generic
770 // expansion for these nodes when they are unaligned is incorrect if the
771 // type is a vector.
772 //
773 // TODO: Fix the generic expansion for these nodes found in
774 // TargetLowering::expandUnalignedLoad/Store.
776 MVT::v2i8, Expand);
778 {MVT::v2i8, MVT::v2i16}, Expand);
779 setTruncStoreAction(MVT::v2i16, MVT::v2i8, Expand);
780 setTruncStoreAction(MVT::v2i32, MVT::v2i16, Expand);
781 setTruncStoreAction(MVT::v2i32, MVT::v2i8, Expand);
782
783 // Register custom handling for illegal type loads/stores. We'll try to custom
784 // lower almost all illegal types and logic in the lowering will discard cases
785 // we can't handle.
786 setOperationAction({ISD::LOAD, ISD::STORE}, {MVT::i128, MVT::i256, MVT::f128},
787 Custom);
789 if (!isTypeLegal(VT) && VT.getStoreSizeInBits() <= 256)
791 Custom);
792
793 // Custom legalization for LDU intrinsics.
794 // TODO: The logic to lower these is not very robust and we should rewrite it.
795 // Perhaps LDU should not be represented as an intrinsic at all.
798 if (IsPTXVectorType(VT))
800
804 MVT::i1, Expand);
805
806 // This is legal in NVPTX
811
812 setOperationAction(ISD::DYNAMIC_STACKALLOC, {MVT::i32, MVT::i64}, Custom);
814
815 // TRAP can be lowered to PTX trap
816 setOperationAction(ISD::TRAP, MVT::Other, Legal);
817 // DEBUGTRAP can be lowered to PTX brkpt
819
820 // Support varargs.
825
827 {MVT::i16, MVT::i32, MVT::i64}, Legal);
828 // PTX abs.s is undefined for INT_MIN, so ISD::ABS (which requires
829 // abs(INT_MIN) == INT_MIN) must be expanded. ABS_MIN_POISON matches
830 // PTX abs semantics since INT_MIN input is poison/undefined.
831 setOperationAction(ISD::ABS, {MVT::i16, MVT::i32, MVT::i64}, Expand);
832 setOperationAction(ISD::ABS_MIN_POISON, {MVT::i16, MVT::i32, MVT::i64},
833 Legal);
834
836 Promote);
839
840 setI16x2OperationAction(ISD::ABS_MIN_POISON, MVT::v2i16, Legal, Custom);
841 setI16x2OperationAction(ISD::SMIN, MVT::v2i16, Legal, Custom);
842 setI16x2OperationAction(ISD::SMAX, MVT::v2i16, Legal, Custom);
843 setI16x2OperationAction(ISD::UMIN, MVT::v2i16, Legal, Custom);
844 setI16x2OperationAction(ISD::UMAX, MVT::v2i16, Legal, Custom);
845 setI16x2OperationAction(ISD::CTPOP, MVT::v2i16, Legal, Expand);
846 setI16x2OperationAction(ISD::CTLZ, MVT::v2i16, Legal, Expand);
847
848 setI16x2OperationAction(ISD::ADD, MVT::v2i16, Legal, Custom);
849 setI16x2OperationAction(ISD::SUB, MVT::v2i16, Legal, Custom);
850 setI16x2OperationAction(ISD::MUL, MVT::v2i16, Legal, Custom);
851 setI16x2OperationAction(ISD::SHL, MVT::v2i16, Legal, Custom);
852 setI16x2OperationAction(ISD::SREM, MVT::v2i16, Legal, Custom);
853 setI16x2OperationAction(ISD::UREM, MVT::v2i16, Legal, Custom);
854
855 // Other arithmetic and logic ops are unsupported.
859 {MVT::v2i16, MVT::v2i32}, Expand);
860
861 // v2i32 is not supported for any arithmetic operations
866 MVT::v2i32, Expand);
867
872 if (STI.hasFeature(NVPTX::PTX43)) {
877 }
878
880 setOperationAction(ISD::CTTZ, {MVT::v2i16, MVT::v2i32}, Expand);
883
884 // PTX does not directly support SELP of i1, so promote to i32 first
886
887 // PTX cannot multiply two i64s in a single instruction.
890
891 // We have some custom DAG combine patterns for these nodes
893 ISD::AND,
895 ISD::FADD,
902 ISD::MUL,
904 ISD::SHL,
905 ISD::SREM,
906 ISD::UREM,
910 ISD::LOAD,
915
916 // If the vector operands require register coalescing, scalarize instead
917 if (STI.hasF32x2Instructions())
919
920 // setcc for f16x2 and bf16x2 needs special handling to prevent
921 // legalizer's attempt to scalarize it due to v2i1 not being legal.
922 if (STI.allowFP16Math() || STI.hasBF16Math())
924
925 // Vector reduction operations. These may be turned into shuffle or tree
926 // reductions depending on what instructions are available for each type.
928 MVT EltVT = VT.getVectorElementType();
929 if (EltVT == MVT::f32 || EltVT == MVT::f64) {
932 VT, Custom);
933 }
934 }
935
936 // Promote fp16 arithmetic if fp16 hardware isn't available or the
937 // user passed --nvptx-no-fp16-math. The flag is useful because,
938 // although sm_53+ GPUs have some sort of FP16 support in
939 // hardware, only sm_53 and sm_60 have full implementation. Others
940 // only have token amount of hardware and are likely to run faster
941 // by using fp32 units instead.
942 for (const auto &Op : {ISD::FADD, ISD::FMUL, ISD::FSUB, ISD::FMA}) {
943 setFP16OperationAction(Op, MVT::f16, Legal, Promote);
944 setFP16OperationAction(Op, MVT::v2f16, Legal, Expand);
945 setBF16OperationAction(Op, MVT::v2bf16, Legal, Expand);
946 // bf16 must be promoted to f32.
947 setBF16OperationAction(Op, MVT::bf16, Legal, Promote);
948 if (getOperationAction(Op, MVT::bf16) == Promote)
949 AddPromotedToType(Op, MVT::bf16, MVT::f32);
950 setOperationAction(Op, MVT::v2f32,
951 STI.hasF32x2Instructions() ? Legal : Expand);
952 }
953
954 // On SM80, we select add/mul/sub as fma to avoid promotion to float
955 for (const auto &Op : {ISD::FADD, ISD::FMUL, ISD::FSUB}) {
956 for (const auto &VT : {MVT::bf16, MVT::v2bf16}) {
957 if (!STI.hasNativeBF16Support(Op) && STI.hasNativeBF16Support(ISD::FMA)) {
959 }
960 }
961 }
962
963 // f16/f16x2 neg was introduced in PTX 60, SM_53.
964 const bool IsFP16FP16x2NegAvailable = STI.hasFeature(NVPTX::SM53) &&
965 STI.hasFeature(NVPTX::PTX60) &&
966 STI.allowFP16Math();
967 for (const auto &VT : {MVT::f16, MVT::v2f16})
969 IsFP16FP16x2NegAvailable ? Legal : Expand);
970
971 setBF16OperationAction(ISD::FNEG, MVT::bf16, Legal, Expand);
972 setBF16OperationAction(ISD::FNEG, MVT::v2bf16, Legal, Expand);
973 setOperationAction(ISD::FNEG, MVT::v2f32, Expand);
974 // (would be) Library functions.
975
976 // These map to conversion instructions for scalar FP types.
977 for (const auto &Op : {ISD::FCEIL, ISD::FFLOOR, ISD::FNEARBYINT, ISD::FRINT,
979 setOperationAction(Op, MVT::f16, Legal);
980 setOperationAction(Op, MVT::f32, Legal);
981 setOperationAction(Op, MVT::f64, Legal);
982 setOperationAction(Op, MVT::v2f16, Expand);
983 setOperationAction(Op, MVT::v2bf16, Expand);
984 setOperationAction(Op, MVT::v2f32, Expand);
985 setBF16OperationAction(Op, MVT::bf16, Legal, Promote);
986 if (getOperationAction(Op, MVT::bf16) == Promote)
987 AddPromotedToType(Op, MVT::bf16, MVT::f32);
988 }
989
990 if (!STI.hasFeature(NVPTX::SM80) || !STI.hasFeature(NVPTX::PTX71)) {
992 }
993 if (!STI.hasFeature(NVPTX::SM90)) {
994 for (MVT VT : {MVT::bf16, MVT::f32, MVT::f64}) {
997 }
998 }
999
1000 // Expand v2f32 = fp_extend
1002 // Expand v2[b]f16 = fp_round v2f32
1003 setOperationAction(ISD::FP_ROUND, {MVT::v2bf16, MVT::v2f16}, Expand);
1004
1005 // sm_80 only has conversions between f32 and bf16. Custom lower all other
1006 // bf16 conversions.
1007 if (!STI.hasFeature(NVPTX::SM90)) {
1008 for (MVT VT : {MVT::i1, MVT::i16, MVT::i32, MVT::i64}) {
1011 VT, Custom);
1012 }
1015 MVT::bf16, Custom);
1016 }
1017
1021 setOperationAction(ISD::FROUND, MVT::v2bf16, Expand);
1025 AddPromotedToType(ISD::FROUND, MVT::bf16, MVT::f32);
1026
1027 setOperationAction({ISD::LROUND, ISD::LLROUND}, {MVT::f32, MVT::f64}, Expand);
1028
1029 // 'Expand' implements FCOPYSIGN without calling an external library.
1036
1037 // These map to corresponding instructions for f32/f64. f16 must be
1038 // promoted to f32. v2f16 is expanded to f16, which is then promoted
1039 // to f32.
1040 for (const auto &Op :
1042 setOperationAction(Op, MVT::f16, Promote);
1043 setOperationAction(Op, MVT::f32, Legal);
1044 // only div/rem/sqrt are legal for f64
1045 if (Op == ISD::FDIV || Op == ISD::FREM || Op == ISD::FSQRT) {
1046 setOperationAction(Op, MVT::f64, Legal);
1047 }
1048 setOperationAction(Op, {MVT::v2f16, MVT::v2bf16, MVT::v2f32}, Expand);
1049 setOperationAction(Op, MVT::bf16, Promote);
1050 AddPromotedToType(Op, MVT::bf16, MVT::f32);
1051 }
1052 setOperationAction(ISD::FREM, {MVT::f32, MVT::f64}, Custom);
1053
1054 // FTANH support:
1055 // - f32 (sm_75+, PTX 7.0+)
1056 // - f16/f16x2 (sm_75+, PTX 7.0+)
1057 // - bf16/bf16x2 (sm_90+, PTX 7.8+)
1058 // When f16/bf16 types aren't supported, they are promoted/expanded to f32.
1059 if (STI.hasFeature(NVPTX::SM75) && STI.hasFeature(NVPTX::PTX70))
1061 setOperationAction(ISD::FTANH, MVT::v2f32, Expand);
1062
1063 // Scalar f16/bf16: promote to f32 when not natively supported.
1064 setFP16OperationAction(ISD::FTANH, MVT::f16, Legal, Promote);
1065 setBF16OperationAction(ISD::FTANH, MVT::bf16, Legal, Promote);
1066 if (getOperationAction(ISD::FTANH, MVT::bf16) == Promote)
1067 AddPromotedToType(ISD::FTANH, MVT::bf16, MVT::f32);
1068
1069 // Vector v2f16/v2bf16: expand when not natively supported.
1070 setFP16OperationAction(ISD::FTANH, MVT::v2f16, Legal, Expand);
1071 setBF16OperationAction(ISD::FTANH, MVT::v2bf16, Legal, Expand);
1072
1073 setOperationAction(ISD::FABS, {MVT::f32, MVT::f64}, Legal);
1074 setOperationAction(ISD::FABS, MVT::v2f32, Expand);
1075 if (STI.hasFeature(NVPTX::PTX65)) {
1076 setFP16OperationAction(ISD::FABS, MVT::f16, Legal, Promote);
1077 setFP16OperationAction(ISD::FABS, MVT::v2f16, Legal, Expand);
1078 } else {
1080 setOperationAction(ISD::FABS, MVT::v2f16, Expand);
1081 }
1082 setBF16OperationAction(ISD::FABS, MVT::v2bf16, Legal, Expand);
1083 setBF16OperationAction(ISD::FABS, MVT::bf16, Legal, Promote);
1084 if (getOperationAction(ISD::FABS, MVT::bf16) == Promote)
1085 AddPromotedToType(ISD::FABS, MVT::bf16, MVT::f32);
1086
1087 for (const auto &Op :
1089 setOperationAction(Op, MVT::f32, Legal);
1090 setOperationAction(Op, MVT::f64, Legal);
1091 setFP16OperationAction(Op, MVT::f16, Legal, Promote);
1092 setFP16OperationAction(Op, MVT::v2f16, Legal, Expand);
1093 setBF16OperationAction(Op, MVT::v2bf16, Legal, Expand);
1094 setBF16OperationAction(Op, MVT::bf16, Legal, Promote);
1095 if (getOperationAction(Op, MVT::bf16) == Promote)
1096 AddPromotedToType(Op, MVT::bf16, MVT::f32);
1097 setOperationAction(Op, MVT::v2f32, Expand);
1098 }
1099 bool SupportsF32MinMaxNaN = STI.hasFeature(NVPTX::SM80);
1100 for (const auto &Op : {ISD::FMINIMUM, ISD::FMAXIMUM}) {
1101 setOperationAction(Op, MVT::f32, SupportsF32MinMaxNaN ? Legal : Expand);
1102 setFP16OperationAction(Op, MVT::f16, Legal, Expand);
1103 setFP16OperationAction(Op, MVT::v2f16, Legal, Expand);
1104 setBF16OperationAction(Op, MVT::bf16, Legal, Expand);
1105 setBF16OperationAction(Op, MVT::v2bf16, Legal, Expand);
1106 setOperationAction(Op, MVT::v2f32, Expand);
1107 }
1108
1109 // Custom lowering for inline asm with 128-bit operands
1112
1113 // FEXP2 support:
1114 // - f32
1115 // - f16/f16x2 (sm_70+, PTX 7.0+)
1116 // - bf16/bf16x2 (sm_90+, PTX 7.8+)
1117 // When f16/bf16 types aren't supported, they are promoted/expanded to f32.
1119 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand);
1120 setFP16OperationAction(ISD::FEXP2, MVT::f16, Legal, Promote);
1121 setFP16OperationAction(ISD::FEXP2, MVT::v2f16, Legal, Expand);
1122 setBF16OperationAction(ISD::FEXP2, MVT::bf16, Legal, Promote);
1123 setBF16OperationAction(ISD::FEXP2, MVT::v2bf16, Legal, Expand);
1124
1125 // FLOG2 supports f32 only
1126 // f16/bf16 types aren't supported, but they are promoted/expanded to f32.
1127 if (UseApproxLog2F32) {
1129 setOperationPromotedToType(ISD::FLOG2, MVT::f16, MVT::f32);
1130 setOperationPromotedToType(ISD::FLOG2, MVT::bf16, MVT::f32);
1131 setOperationAction(ISD::FLOG2, {MVT::v2f16, MVT::v2bf16, MVT::v2f32},
1132 Expand);
1133 }
1134
1135 setOperationAction(ISD::ADDRSPACECAST, {MVT::i32, MVT::i64}, Custom);
1136
1137 setOperationAction(ISD::ATOMIC_LOAD_SUB, {MVT::i32, MVT::i64}, Expand);
1138
1139 // atom.b128 is legal in PTX but since we don't represent i128 as a legal
1140 // type, we need to custom lower it.
1142 Custom);
1143
1144 // Now deduce the information based on the above mentioned
1145 // actions
1146 computeRegisterProperties(STI.getRegisterInfo());
1147
1148 // PTX support for 16-bit CAS is emulated. Only use 32+
1149 setMinCmpXchgSizeInBits(STI.getMinCmpXchgSizeInBits());
1150 setMaxAtomicSizeInBitsSupported(STI.hasAtomSwap128() ? 128 : 64);
1153
1154 // Custom lowering for tcgen05.ld vector operands
1156 {MVT::v1i32, MVT::v2i32, MVT::v4i32, MVT::v8i32,
1157 MVT::v16i32, MVT::v32i32, MVT::v64i32, MVT::v128i32,
1158 MVT::v2f32, MVT::v4f32, MVT::v8f32, MVT::v16f32,
1159 MVT::v32f32, MVT::v64f32, MVT::v128f32},
1160 Custom);
1161
1162 // Custom lowering for tcgen05.st vector operands and the st.async
1163 // i128 (.b128) operand. MVT::i8 is needed for the st.async.{sys,gpu} b8
1164 // variant.
1166 {MVT::i8, MVT::v1i32, MVT::v2i32, MVT::v4i32, MVT::v8i32,
1167 MVT::v16i32, MVT::v32i32, MVT::v64i32, MVT::v128i32,
1168 MVT::i128, MVT::Other},
1169 Custom);
1170
1171 // Enable custom lowering for the following:
1172 // * MVT::i128 - clusterlaunchcontrol
1173 // * MVT::i32 - prmt
1174 // * MVT::v4f32 - cvt_rs fp{4/6/8}x4 intrinsics
1175 // * MVT::Other - internal.addrspace.wrap
1177 {MVT::i32, MVT::i128, MVT::v4f32, MVT::Other}, Custom);
1178
1179 // Custom lowering for bswap
1180 setOperationAction(ISD::BSWAP, {MVT::i16, MVT::i32, MVT::i64, MVT::v2i16},
1181 Custom);
1182}
1183
1186 if (!VT.isScalableVector() && VT.getVectorNumElements() != 1 &&
1187 VT.getScalarType() == MVT::i1)
1188 return TypeSplitVector;
1190}
1191
1193 int Enabled, int &ExtraSteps,
1194 bool &UseOneConst,
1195 bool Reciprocal) const {
1198 return SDValue();
1199
1200 if (ExtraSteps == ReciprocalEstimate::Unspecified)
1201 ExtraSteps = 0;
1202
1203 SDLoc DL(Operand);
1204 EVT VT = Operand.getValueType();
1205 bool Ftz = useF32FTZ(DAG.getMachineFunction());
1206
1207 auto MakeIntrinsicCall = [&](Intrinsic::ID IID) {
1208 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
1209 DAG.getConstant(IID, DL, MVT::i32), Operand);
1210 };
1211
1212 // The sqrt and rsqrt refinement processes assume we always start out with an
1213 // approximation of the rsqrt. Therefore, if we're going to do any refinement
1214 // (i.e. ExtraSteps > 0), we must return an rsqrt. But if we're *not* doing
1215 // any refinement, we must return a regular sqrt.
1216 if (Reciprocal || ExtraSteps > 0) {
1217 if (VT == MVT::f32)
1218 return MakeIntrinsicCall(Ftz ? Intrinsic::nvvm_rsqrt_approx_ftz_f
1219 : Intrinsic::nvvm_rsqrt_approx_f);
1220 else if (VT == MVT::f64)
1221 return MakeIntrinsicCall(Intrinsic::nvvm_rsqrt_approx_d);
1222 else
1223 return SDValue();
1224 } else {
1225 if (VT == MVT::f32)
1226 return MakeIntrinsicCall(Ftz ? Intrinsic::nvvm_sqrt_approx_ftz_f
1227 : Intrinsic::nvvm_sqrt_approx_f);
1228 else {
1229 // There's no sqrt.approx.f64 instruction, so we emit
1230 // reciprocal(rsqrt(x)). This is faster than
1231 // select(x == 0, 0, x * rsqrt(x)). (In fact, it's faster than plain
1232 // x * rsqrt(x).)
1233 return DAG.getNode(
1235 DAG.getConstant(Intrinsic::nvvm_rcp_approx_ftz_d, DL, MVT::i32),
1236 MakeIntrinsicCall(Intrinsic::nvvm_rsqrt_approx_d));
1237 }
1238 }
1239}
1240
1242 // Load directly from the source address space of a cast to generic.
1243 unsigned SrcAS = ADDRESS_SPACE_GENERIC;
1244 if (Ptr->getOpcode() == ISD::ADDRSPACECAST) {
1245 const auto *ASC = cast<AddrSpaceCastSDNode>(Ptr);
1246 if (ASC->getDestAddressSpace() == ADDRESS_SPACE_GENERIC) {
1247 Ptr = ASC->getOperand(0);
1248 SrcAS = ASC->getSrcAddressSpace();
1249 }
1250 }
1251
1252 // Preserve the alloca's address space through frame-index inference.
1253 if (const auto *FIN = dyn_cast<FrameIndexSDNode>(Ptr))
1254 if (const AllocaInst *AI =
1256 FIN->getIndex()))
1257 return MachinePointerInfo(AI);
1258
1259 return MachinePointerInfo(SrcAS);
1260}
1261
1263 if (Flags.isSExt())
1264 return ISD::SIGN_EXTEND;
1265 if (Flags.isZExt())
1266 return ISD::ZERO_EXTEND;
1267 return ISD::ANY_EXTEND;
1268}
1269
1271 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1272 SDLoc dl) {
1273 const EVT ActualVT = V.getValueType();
1274 assert((ActualVT == ExpectedVT ||
1275 (ExpectedVT.isInteger() && ActualVT.isInteger())) &&
1276 "Non-integer argument type size mismatch");
1277 if (ExpectedVT.bitsGT(ActualVT))
1278 return DAG.getNode(getExtOpcode(Flags), dl, ExpectedVT, V);
1279 if (ExpectedVT.bitsLT(ActualVT))
1280 return DAG.getNode(ISD::TRUNCATE, dl, ExpectedVT, V);
1281
1282 return V;
1283}
1284
1286 return DAG.getNode(NVPTXISD::Symbol, SDLoc(), T, DAG.getMCSymbol(Sym, T));
1287}
1288
1289static SDValue getSymbolNode(SelectionDAG &DAG, const Twine &Name, EVT T) {
1291 return getSymbolNode(DAG, Ctx.getOrCreateSymbol(Name), T);
1292}
1293
1295 SmallVectorImpl<SDValue> &InVals) const {
1296
1297 if (CLI.IsVarArg &&
1298 (!STI.hasFeature(NVPTX::PTX60) || !STI.hasFeature(NVPTX::SM30)))
1300 "Support for variadic functions (unsized array parameter) introduced "
1301 "in PTX ISA version 6.0 and requires target sm_30.");
1302
1303 SelectionDAG &DAG = CLI.DAG;
1304 SDLoc dl = CLI.DL;
1305 const SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
1306 SDValue Callee = CLI.Callee;
1307 ArgListTy &Args = CLI.getArgs();
1308 Type *RetTy = CLI.RetTy;
1309 const CallBase *CB = CLI.CB;
1310 const DataLayout &DL = DAG.getDataLayout();
1311 LLVMContext &Ctx = *DAG.getContext();
1312
1313 const auto GetI32 = [&](const unsigned I) {
1314 return DAG.getConstant(I, dl, MVT::i32);
1315 };
1316
1317 const unsigned UniqueCallSite = GlobalUniqueCallSite++;
1318 const SDValue CallChain = CLI.Chain;
1319 const SDValue StartChain =
1320 DAG.getCALLSEQ_START(CallChain, UniqueCallSite, 0, dl);
1321 SDValue DeclareGlue = StartChain.getValue(1);
1322
1323 SmallVector<SDValue, 16> CallPrereqs{StartChain};
1324
1325 const auto MakeDeclareScalarParam = [&](SDValue Symbol, unsigned Size) {
1326 // PTX ABI requires integral types to be at least 32 bits in size. FP16 is
1327 // loaded/stored using i16, so it's handled here as well.
1328 const unsigned SizeBits = promoteScalarArgumentSize(Size * 8);
1329 SDValue Declare =
1330 DAG.getNode(NVPTXISD::DeclareScalarParam, dl, {MVT::Other, MVT::Glue},
1331 {StartChain, Symbol, GetI32(SizeBits), DeclareGlue});
1332 CallPrereqs.push_back(Declare);
1333 DeclareGlue = Declare.getValue(1);
1334 return Declare;
1335 };
1336
1337 const auto MakeDeclareArrayParam = [&](SDValue Symbol, Align Align,
1338 unsigned Size) {
1339 SDValue Declare = DAG.getNode(
1340 NVPTXISD::DeclareArrayParam, dl, {MVT::Other, MVT::Glue},
1341 {StartChain, Symbol, GetI32(Align.value()), GetI32(Size), DeclareGlue});
1342 CallPrereqs.push_back(Declare);
1343 DeclareGlue = Declare.getValue(1);
1344 return Declare;
1345 };
1346
1347 // Variadic arguments.
1348 //
1349 // Normally, for each argument, we declare a param scalar or a param
1350 // byte array in the .param space, and store the argument value to that
1351 // param scalar or array starting at offset 0.
1352 //
1353 // In the case of the first variadic argument, we declare a vararg byte array
1354 // with size 0. The exact size of this array isn't known at this point, so
1355 // it'll be patched later. All the variadic arguments will be stored to this
1356 // array at a certain offset (which gets tracked by 'VAOffset'). The offset is
1357 // initially set to 0, so it can be used for non-variadic arguments (which use
1358 // 0 offset) to simplify the code.
1359 //
1360 // After all vararg is processed, 'VAOffset' holds the size of the
1361 // vararg byte array.
1362 assert((CLI.IsVarArg || CLI.Args.size() <= CLI.NumFixedArgs) &&
1363 "Non-VarArg function with extra arguments");
1364
1365 const unsigned FirstVAArg = CLI.NumFixedArgs; // position of first variadic
1366 unsigned VAOffset = 0; // current offset in the param array
1367
1368 const SDValue VADeclareParam =
1369 CLI.Args.size() > FirstVAArg
1370 ? MakeDeclareArrayParam(
1371 getCallParamSymbolNode(DAG, FirstVAArg, MVT::i32),
1372 Align(STI.getMaxRequiredAlignment()), 0)
1373 : SDValue();
1374
1375 // Args.size() and Outs.size() need not match.
1376 // Outs.size() will be larger
1377 // * if there is an aggregate argument with multiple fields (each field
1378 // showing up separately in Outs)
1379 // * if there is a vector argument with more than typical vector-length
1380 // elements (generally if more than 4) where each vector element is
1381 // individually present in Outs.
1382 // So a different index should be used for indexing into Outs/OutVals.
1383 // See similar issue in LowerFormalArguments.
1384 auto AllOuts = ArrayRef(CLI.Outs);
1385 auto AllOutVals = ArrayRef(CLI.OutVals);
1386 assert(AllOuts.size() == AllOutVals.size() &&
1387 "Outs and OutVals must be the same size");
1388 // Declare the .params or .reg need to pass values
1389 // to the function
1390 for (const auto E : llvm::enumerate(Args)) {
1391 const auto ArgI = E.index();
1392 const auto Arg = E.value();
1393 const auto ArgOuts =
1394 AllOuts.take_while([&](auto O) { return O.OrigArgIndex == ArgI; });
1395 const auto ArgOutVals = AllOutVals.take_front(ArgOuts.size());
1396 AllOuts = AllOuts.drop_front(ArgOuts.size());
1397 AllOutVals = AllOutVals.drop_front(ArgOuts.size());
1398
1399 const bool IsVAArg = (ArgI >= FirstVAArg);
1400 const bool IsByVal = Arg.IsByVal;
1401
1402 const SDValue ParamSymbol =
1403 getCallParamSymbolNode(DAG, IsVAArg ? FirstVAArg : ArgI, MVT::i32);
1404
1405 assert((!IsByVal || Arg.IndirectType) &&
1406 "byval arg must have indirect type");
1407 Type *ETy = (IsByVal ? Arg.IndirectType : Arg.Ty);
1408
1409 const Align ArgAlign = [&]() {
1410 const unsigned ParamIdx = ArgI + AttributeList::FirstArgIndex;
1411 if (IsByVal)
1412 return getDeviceByValParamAlign(CB, ETy, ParamIdx, DL);
1413 return getPTXParamAlign(CB, Arg.Ty, ParamIdx, DL);
1414 }();
1415
1416 const unsigned TySize = DL.getTypeAllocSize(ETy);
1417 assert((!IsByVal || TySize == ArgOuts[0].Flags.getByValSize()) &&
1418 "type size mismatch");
1419
1420 const SDValue ArgDeclare = [&]() {
1421 if (IsVAArg)
1422 return VADeclareParam;
1423
1424 if (IsByVal || shouldPassAsArray(Arg.Ty))
1425 return MakeDeclareArrayParam(ParamSymbol, ArgAlign, TySize);
1426
1427 assert(ArgOuts.size() == 1 && "We must pass only one value as non-array");
1428 assert((ArgOuts[0].VT.isInteger() || ArgOuts[0].VT.isFloatingPoint()) &&
1429 "Only int and float types are supported as non-array arguments");
1430
1431 return MakeDeclareScalarParam(ParamSymbol, TySize);
1432 }();
1433
1434 if (IsByVal) {
1435 assert(ArgOutVals.size() == 1 && "We must pass only one value as byval");
1436 SDValue SrcPtr = ArgOutVals[0];
1437 const MachinePointerInfo SrcPtrInfo = refinePtrAS(SrcPtr, DAG);
1438 // Don't use Flags.getNonZeroByValAlign as this includes the stackalign,
1439 // which does not apply to the source pointer.
1440 const Align BaseSrcAlign = [&]() {
1441 // The align attribute on a byval argument indicates the known alignment
1442 // of the pointer passed to the function.
1443 if (CB)
1444 if (const MaybeAlign A = CB->getParamAlign(ArgI))
1445 return *A;
1446 // Fall back to the default alignment for the type.
1447 // TODO: This might be too aggressive but we haven't had a problem with
1448 // it yet.
1449 return getPTXParamTypeAlign(ETy, DL);
1450 }();
1451
1452 if (IsVAArg)
1453 VAOffset = alignTo(VAOffset, ArgAlign);
1454
1455 SmallVector<EVT, 4> ValueVTs, MemVTs;
1457 ComputeValueVTs(*this, DL, ETy, ValueVTs, &MemVTs, &Offsets);
1458
1459 unsigned J = 0;
1460 const auto VI = VectorizePTXValueVTs(MemVTs, Offsets, ArgAlign, IsVAArg);
1461 for (const unsigned NumElts : VI) {
1462 EVT LoadVT = getVectorizedVT(MemVTs[J], NumElts, Ctx);
1463 Align SrcAlign = commonAlignment(BaseSrcAlign, Offsets[J]);
1464 SDValue SrcAddr = DAG.getObjectPtrOffset(dl, SrcPtr, Offsets[J]);
1465 SDValue SrcLoad =
1466 DAG.getLoad(LoadVT, dl, CallChain, SrcAddr,
1467 SrcPtrInfo.getWithOffset(Offsets[J]), SrcAlign);
1468
1469 TypeSize ParamOffset = Offsets[J].getWithIncrement(VAOffset);
1470 Align ParamAlign = commonAlignment(ArgAlign, ParamOffset);
1471 SDValue ParamAddr =
1472 DAG.getObjectPtrOffset(dl, ParamSymbol, ParamOffset);
1473 SDValue StoreParam = DAG.getStore(
1474 ArgDeclare, dl, SrcLoad, ParamAddr,
1476 CallPrereqs.push_back(StoreParam);
1477
1478 J += NumElts;
1479 }
1480 if (IsVAArg)
1481 VAOffset += TySize;
1482 } else {
1485 ComputePTXValueVTs(*this, DL, Ctx, CLI.CallConv, Arg.Ty, VTs, Offsets,
1486 VAOffset);
1487 assert(VTs.size() == Offsets.size() && "Size mismatch");
1488 assert(VTs.size() == ArgOuts.size() && "Size mismatch");
1489
1490 // PTX Interoperability Guide 3.3(A): [Integer] Values shorter
1491 // than 32-bits are sign extended or zero extended, depending on
1492 // whether they are signed or unsigned types. This case applies
1493 // only to scalar parameters and not to aggregate values.
1494 const bool ExtendIntegerParam =
1495 Arg.Ty->isIntegerTy() && DL.getTypeAllocSizeInBits(Arg.Ty) < 32;
1496
1497 const auto GetStoredValue = [&](const unsigned I) {
1498 SDValue StVal = ArgOutVals[I];
1500 StVal.getValueType() &&
1501 "OutVal type should always be legal");
1502
1503 const EVT VTI = promoteScalarIntegerPTX(VTs[I]);
1504 const EVT StoreVT =
1505 ExtendIntegerParam ? MVT::i32 : (VTI == MVT::i1 ? MVT::i8 : VTI);
1506
1507 return correctParamType(StVal, StoreVT, ArgOuts[I].Flags, DAG, dl);
1508 };
1509
1510 unsigned J = 0;
1511 const auto VI = VectorizePTXValueVTs(VTs, Offsets, ArgAlign, IsVAArg);
1512 for (const unsigned NumElts : VI) {
1513 const EVT EltVT = promoteScalarIntegerPTX(VTs[J]);
1514
1515 unsigned Offset;
1516 if (IsVAArg) {
1517 // TODO: We may need to support vector types that can be passed
1518 // as scalars in variadic arguments.
1519 assert(NumElts == 1 &&
1520 "Vectorization should be disabled for vaargs.");
1521
1522 // Align each part of the variadic argument to their type.
1523 VAOffset = alignTo(VAOffset, DAG.getEVTAlign(EltVT));
1524 Offset = VAOffset;
1525
1526 const EVT TheStoreType = ExtendIntegerParam ? MVT::i32 : EltVT;
1527 VAOffset += DL.getTypeAllocSize(TheStoreType.getTypeForEVT(Ctx));
1528 } else {
1529 assert(VAOffset == 0 && "VAOffset must be 0 for non-VA args");
1530 Offset = Offsets[J];
1531 }
1532
1533 SDValue Ptr =
1534 DAG.getObjectPtrOffset(dl, ParamSymbol, TypeSize::getFixed(Offset));
1535
1536 const MaybeAlign CurrentAlign = ExtendIntegerParam
1537 ? MaybeAlign(std::nullopt)
1538 : commonAlignment(ArgAlign, Offset);
1539
1540 SDValue Val =
1541 getBuildVectorizedValue(NumElts, dl, DAG, [&](unsigned K) {
1542 return GetStoredValue(J + K);
1543 });
1544
1545 SDValue StoreParam = DAG.getStore(
1546 ArgDeclare, dl, Val, Ptr,
1548 CallPrereqs.push_back(StoreParam);
1549
1550 J += NumElts;
1551 }
1552 }
1553 }
1554
1555 // Handle Result
1556 if (!Ins.empty()) {
1557 const SDValue RetSymbol = getSymbolNode(DAG, "retval0", MVT::i32);
1558 const unsigned ResultSize = DL.getTypeAllocSize(RetTy);
1559 if (shouldPassAsArray(RetTy)) {
1560 const Align RetAlign =
1561 getPTXParamAlign(CB, RetTy, AttributeList::ReturnIndex, DL);
1562 MakeDeclareArrayParam(RetSymbol, RetAlign, ResultSize);
1563 } else {
1564 MakeDeclareScalarParam(RetSymbol, ResultSize);
1565 }
1566 }
1567
1568 // Set the size of the vararg param byte array if the callee is a variadic
1569 // function and the variadic part is not empty.
1570 if (VADeclareParam) {
1571 SDValue DeclareParamOps[] = {VADeclareParam.getOperand(0),
1572 VADeclareParam.getOperand(1),
1573 VADeclareParam.getOperand(2), GetI32(VAOffset),
1574 VADeclareParam.getOperand(4)};
1575 DAG.MorphNodeTo(VADeclareParam.getNode(), VADeclareParam.getOpcode(),
1576 VADeclareParam->getVTList(), DeclareParamOps);
1577 }
1578
1579 const auto *Func = dyn_cast<GlobalAddressSDNode>(Callee.getNode());
1580 const auto *CalleeF = Func ? dyn_cast<Function>(Func->getGlobal()) : nullptr;
1581
1582 // If the type of the callsite does not match that of the function, convert
1583 // the callsite to an indirect call.
1584 const bool ConvertToIndirectCall =
1585 CalleeF && CB->getFunctionType() != CalleeF->getFunctionType();
1586
1587 // Both indirect calls and libcalls have nullptr Func. In order to distinguish
1588 // between them we must rely on the call site value which is valid for
1589 // indirect calls but is always null for libcalls.
1590 const bool IsIndirectCall = (!Func && CB) || ConvertToIndirectCall;
1591
1592 if (isa<ExternalSymbolSDNode>(Callee)) {
1593 Function* CalleeFunc = nullptr;
1594
1595 // Try to find the callee in the current module.
1596 Callee = DAG.getSymbolFunctionGlobalAddress(Callee, &CalleeFunc);
1597 assert(CalleeFunc != nullptr && "Libcall callee must be set.");
1598
1599 // Set the "libcall callee" attribute to indicate that the function
1600 // must always have a declaration.
1601 CalleeFunc->addFnAttr("nvptx-libcall-callee", "true");
1602 }
1603
1604 // In the indirect function call case, PTX requires a prototype of the form:
1605 // proto_0 : .callprototype(.param .b32 _) _ (.param .b32 _);
1606 // Where the label is to be used as the last arg of the call instruction.
1607 // We record the call site here and emit all prototypes at the
1608 // start of the function in the AsmPrinter.
1609 if (IsIndirectCall)
1610 DAG.getMachineFunction()
1612 ->addCallPrototype(UniqueCallSite, CB);
1613
1614 const bool IsUnknownIntrinsic =
1615 CalleeF && CalleeF->isIntrinsic() &&
1616 CalleeF->getIntrinsicID() == Intrinsic::not_intrinsic;
1617 if (IsUnknownIntrinsic) {
1620 "call to unknown intrinsic '" + CalleeF->getName() +
1621 "' cannot be lowered by the NVPTX backend",
1622 dl.getDebugLoc()));
1623 }
1624
1625 const unsigned Proto = IsIndirectCall ? UniqueCallSite : 0;
1626 const unsigned NumArgs =
1627 std::min<unsigned>(CLI.NumFixedArgs + 1, Args.size());
1628 /// CALL(Chain, IsConvergent, IsIndirectCall/IsUniform, NumReturns,
1629 /// NumParams, Callee, Proto)
1630 const SDValue CallToken = DAG.getTokenFactor(dl, CallPrereqs);
1631 const SDValue Call = DAG.getNode(
1632 NVPTXISD::CALL, dl, MVT::Other,
1633 {CallToken, GetI32(CLI.IsConvergent), GetI32(IsIndirectCall),
1634 GetI32(Ins.empty() ? 0 : 1), GetI32(NumArgs), Callee, GetI32(Proto)});
1635
1636 SmallVector<SDValue, 16> LoadChains{Call};
1637 SmallVector<SDValue, 16> ProxyRegOps;
1638 if (!Ins.empty()) {
1641 ComputePTXValueVTs(*this, DL, Ctx, CLI.CallConv, RetTy, VTs, Offsets);
1642 assert(VTs.size() == Ins.size() && "Bad value decomposition");
1643
1644 const Align RetAlign =
1645 getPTXParamAlign(CB, RetTy, AttributeList::ReturnIndex, DL);
1646 const SDValue RetSymbol = getSymbolNode(DAG, "retval0", MVT::i32);
1647
1648 // PTX Interoperability Guide 3.3(A): [Integer] Values shorter than
1649 // 32-bits are sign extended or zero extended, depending on whether
1650 // they are signed or unsigned types.
1651 const bool ExtendIntegerRetVal =
1652 RetTy->isIntegerTy() && DL.getTypeAllocSizeInBits(RetTy) < 32;
1653
1654 unsigned I = 0;
1655 const auto VI = VectorizePTXValueVTs(VTs, Offsets, RetAlign);
1656 for (const unsigned NumElts : VI) {
1657 const MaybeAlign CurrentAlign =
1658 ExtendIntegerRetVal ? MaybeAlign(std::nullopt)
1659 : commonAlignment(RetAlign, Offsets[I]);
1660
1661 const EVT VTI = promoteScalarIntegerPTX(VTs[I]);
1662 const EVT LoadVT =
1663 ExtendIntegerRetVal ? MVT::i32 : (VTI == MVT::i1 ? MVT::i8 : VTI);
1664 const EVT VecVT = getVectorizedVT(LoadVT, NumElts, Ctx);
1665 SDValue Ptr =
1666 DAG.getObjectPtrOffset(dl, RetSymbol, TypeSize::getFixed(Offsets[I]));
1667
1668 SDValue R = DAG.getLoad(
1669 VecVT, dl, Call, Ptr,
1671
1672 LoadChains.push_back(R.getValue(1));
1673 for (const unsigned J : llvm::seq(NumElts))
1674 ProxyRegOps.push_back(getExtractVectorizedValue(R, J, LoadVT, dl, DAG));
1675 I += NumElts;
1676 }
1677 }
1678
1679 const SDValue EndToken = DAG.getTokenFactor(dl, LoadChains);
1680 const SDValue CallEnd = DAG.getCALLSEQ_END(EndToken, UniqueCallSite,
1681 UniqueCallSite + 1, SDValue(), dl);
1682
1683 // Append ProxyReg instructions to the chain to make sure that `callseq_end`
1684 // will not get lost. Otherwise, during libcalls expansion, the nodes can become
1685 // dangling.
1686 for (const auto [I, Reg] : llvm::enumerate(ProxyRegOps)) {
1687 SDValue Proxy =
1688 DAG.getNode(NVPTXISD::ProxyReg, dl, Reg.getValueType(), {CallEnd, Reg});
1689 SDValue Ret = correctParamType(Proxy, Ins[I].VT, Ins[I].Flags, DAG, dl);
1690 InVals.push_back(Ret);
1691 }
1692
1693 // set IsTailCall to false for now, until we figure out how to express
1694 // tail call optimization in PTX
1695 CLI.IsTailCall = false;
1696 return CallEnd;
1697}
1698
1700 SelectionDAG &DAG) const {
1701
1702 if (!STI.hasFeature(NVPTX::PTX73) || !STI.hasFeature(NVPTX::SM52)) {
1703 const Function &Fn = DAG.getMachineFunction().getFunction();
1704
1706 Fn,
1707 "Support for dynamic alloca introduced in PTX ISA version 7.3 and "
1708 "requires target sm_52.",
1709 SDLoc(Op).getDebugLoc()));
1710 auto Ops = {DAG.getConstant(0, SDLoc(), Op.getValueType()),
1711 Op.getOperand(0)};
1712 return DAG.getMergeValues(Ops, SDLoc());
1713 }
1714
1715 SDLoc DL(Op.getNode());
1716 SDValue Chain = Op.getOperand(0);
1717 SDValue Size = Op.getOperand(1);
1718 uint64_t Align = Op.getConstantOperandVal(2);
1719
1720 // The alignment on a ISD::DYNAMIC_STACKALLOC node may be 0 to indicate that
1721 // the default stack alignment should be used.
1722 if (Align == 0)
1724
1725 // The size for ptx alloca instruction is 64-bit for m64 and 32-bit for m32.
1726 const MVT LocalVT = getPointerTy(DAG.getDataLayout(), ADDRESS_SPACE_LOCAL);
1727
1728 SDValue Alloc =
1729 DAG.getNode(NVPTXISD::DYNAMIC_STACKALLOC, DL, {LocalVT, MVT::Other},
1730 {Chain, DAG.getZExtOrTrunc(Size, DL, LocalVT),
1731 DAG.getTargetConstant(Align, DL, MVT::i32)});
1732
1733 // NVPTXLowerAlloca puts allocas in the local address space, so a local
1734 // pointer is requested here; escapes are explicit addrspacecasts in the IR.
1735 assert(Op.getValueType() == LocalVT && "Unexpected alloca pointer size");
1736
1737 return DAG.getMergeValues({Alloc, SDValue(Alloc.getNode(), 1)}, DL);
1738}
1739
1741 SelectionDAG &DAG) const {
1742 SDLoc DL(Op.getNode());
1743 if (!STI.hasFeature(NVPTX::PTX73) || !STI.hasFeature(NVPTX::SM52)) {
1744 const Function &Fn = DAG.getMachineFunction().getFunction();
1745
1747 Fn,
1748 "Support for stackrestore requires PTX ISA version >= 7.3 and target "
1749 ">= sm_52.",
1750 DL.getDebugLoc()));
1751 return Op.getOperand(0);
1752 }
1753
1754 const MVT LocalVT = getPointerTy(DAG.getDataLayout(), ADDRESS_SPACE_LOCAL);
1755 SDValue Chain = Op.getOperand(0);
1756 SDValue Ptr = Op.getOperand(1);
1757 SDValue ASC = DAG.getAddrSpaceCast(DL, LocalVT, Ptr, ADDRESS_SPACE_GENERIC,
1759 return DAG.getNode(NVPTXISD::STACKRESTORE, DL, MVT::Other, {Chain, ASC});
1760}
1761
1763 SelectionDAG &DAG) const {
1764 SDLoc DL(Op.getNode());
1765 if (!STI.hasFeature(NVPTX::PTX73) || !STI.hasFeature(NVPTX::SM52)) {
1766 const Function &Fn = DAG.getMachineFunction().getFunction();
1767
1769 Fn,
1770 "Support for stacksave requires PTX ISA version >= 7.3 and target >= "
1771 "sm_52.",
1772 DL.getDebugLoc()));
1773 auto Ops = {DAG.getConstant(0, DL, Op.getValueType()), Op.getOperand(0)};
1774 return DAG.getMergeValues(Ops, DL);
1775 }
1776
1777 const MVT LocalVT = getPointerTy(DAG.getDataLayout(), ADDRESS_SPACE_LOCAL);
1778 SDValue Chain = Op.getOperand(0);
1779 SDValue SS =
1780 DAG.getNode(NVPTXISD::STACKSAVE, DL, {LocalVT, MVT::Other}, Chain);
1781 SDValue ASC = DAG.getAddrSpaceCast(
1782 DL, Op.getValueType(), SS, ADDRESS_SPACE_LOCAL, ADDRESS_SPACE_GENERIC);
1783 return DAG.getMergeValues({ASC, SDValue(SS.getNode(), 1)}, DL);
1784}
1785
1786// By default CONCAT_VECTORS is lowered by ExpandVectorBuildThroughStack()
1787// (see LegalizeDAG.cpp). This is slow and uses local memory.
1788// We use extract/insert/build vector just as what LegalizeOp() does in llvm 2.5
1789SDValue
1790NVPTXTargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
1791 SDNode *Node = Op.getNode();
1792 SDLoc dl(Node);
1794 unsigned NumOperands = Node->getNumOperands();
1795 for (unsigned i = 0; i < NumOperands; ++i) {
1796 SDValue SubOp = Node->getOperand(i);
1797 EVT VVT = SubOp.getNode()->getValueType(0);
1798 EVT EltVT = VVT.getVectorElementType();
1799 unsigned NumSubElem = VVT.getVectorNumElements();
1800 for (unsigned j = 0; j < NumSubElem; ++j) {
1801 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, SubOp,
1802 DAG.getIntPtrConstant(j, dl)));
1803 }
1804 }
1805 return DAG.getBuildVector(Node->getValueType(0), dl, Ops);
1806}
1807
1809 SelectionDAG &DAG,
1810 unsigned Mode = NVPTX::PTXPrmtMode::NONE) {
1811 assert(A.getValueType() == MVT::i32 && B.getValueType() == MVT::i32 &&
1812 Selector.getValueType() == MVT::i32 && "PRMT must have i32 operands");
1813 return DAG.getNode(NVPTXISD::PRMT, DL, MVT::i32,
1814 {A, B, Selector, DAG.getConstant(Mode, DL, MVT::i32)});
1815}
1816
1818 SelectionDAG &DAG,
1819 unsigned Mode = NVPTX::PTXPrmtMode::NONE) {
1820 return getPRMT(A, B, DAG.getConstant(Selector, DL, MVT::i32), DL, DAG, Mode);
1821}
1822
1823/// Reduces the elements using the scalar operations provided. The operations
1824/// are sorted descending in number of inputs they take. The flags on the
1825/// original reduction operation will be propagated to each scalar operation.
1826/// Nearby elements are grouped in tree reduction, unlike the shuffle reduction
1827/// used in ExpandReductions and SelectionDAG.
1829 const SmallVector<SDValue> &Elements, EVT EltTy,
1830 ArrayRef<std::pair<unsigned /*NodeType*/, unsigned /*NumInputs*/>> Ops,
1831 const SDLoc &DL, const SDNodeFlags Flags, SelectionDAG &DAG) {
1832 // Build the reduction tree at each level, starting with all the elements.
1833 SmallVector<SDValue> Level = Elements;
1834
1835 unsigned OpIdx = 0;
1836 while (Level.size() > 1) {
1837 // Try to reduce this level using the current operator.
1838 const auto [Op, NumInputs] = Ops[OpIdx];
1839
1840 // Build the next level by partially reducing all elements.
1841 SmallVector<SDValue> ReducedLevel;
1842 unsigned I = 0, E = Level.size();
1843 for (; I + NumInputs <= E; I += NumInputs) {
1844 // Reduce elements in groups of [NumInputs], as much as possible.
1845 ReducedLevel.push_back(DAG.getNode(
1846 Op, DL, EltTy, ArrayRef<SDValue>(Level).slice(I, NumInputs), Flags));
1847 }
1848
1849 if (I < E) {
1850 // Handle leftover elements.
1851
1852 if (ReducedLevel.empty()) {
1853 // We didn't reduce anything at this level. We need to pick a smaller
1854 // operator.
1855 ++OpIdx;
1856 assert(OpIdx < Ops.size() && "no smaller operators for reduction");
1857 continue;
1858 }
1859
1860 // We reduced some things but there's still more left, meaning the
1861 // operator's number of inputs doesn't evenly divide this level size. Move
1862 // these elements to the next level.
1863 for (; I < E; ++I)
1864 ReducedLevel.push_back(Level[I]);
1865 }
1866
1867 // Process the next level.
1868 Level = ReducedLevel;
1869 }
1870
1871 return *Level.begin();
1872}
1873
1874// Get scalar reduction opcode
1875static ISD::NodeType getScalarOpcodeForReduction(unsigned ReductionOpcode) {
1876 switch (ReductionOpcode) {
1878 return ISD::FMAXNUM;
1880 return ISD::FMINNUM;
1882 return ISD::FMAXIMUM;
1884 return ISD::FMINIMUM;
1885 default:
1886 llvm_unreachable("unhandled reduction opcode");
1887 }
1888}
1889
1890/// Get 3-input scalar reduction opcode
1891static std::optional<unsigned>
1892getScalar3OpcodeForReduction(unsigned ReductionOpcode) {
1893 switch (ReductionOpcode) {
1895 return NVPTXISD::FMAXNUM3;
1897 return NVPTXISD::FMINNUM3;
1899 return NVPTXISD::FMAXIMUM3;
1901 return NVPTXISD::FMINIMUM3;
1902 default:
1903 return std::nullopt;
1904 }
1905}
1906
1907/// Lower reductions to either a sequence of operations or a tree if
1908/// reassociations are allowed. This method will use larger operations like
1909/// max3/min3 when the target supports them.
1910SDValue NVPTXTargetLowering::LowerVECREDUCE(SDValue Op,
1911 SelectionDAG &DAG) const {
1912 SDLoc DL(Op);
1913 const SDNodeFlags Flags = Op->getFlags();
1914 SDValue Vector = Op.getOperand(0);
1915
1916 const unsigned Opcode = Op->getOpcode();
1917 const EVT EltTy = Vector.getValueType().getVectorElementType();
1918
1919 // Whether we can use 3-input min/max when expanding the reduction.
1920 const bool CanUseMinMax3 =
1921 EltTy == MVT::f32 && STI.hasFeature(NVPTX::SM100) &&
1922 STI.hasFeature(NVPTX::PTX88) &&
1923 (Opcode == ISD::VECREDUCE_FMAX || Opcode == ISD::VECREDUCE_FMIN ||
1924 Opcode == ISD::VECREDUCE_FMAXIMUM || Opcode == ISD::VECREDUCE_FMINIMUM);
1925
1926 // A list of SDNode opcodes with equivalent semantics, sorted descending by
1927 // number of inputs they take.
1928 SmallVector<std::pair<unsigned /*Op*/, unsigned /*NumIn*/>, 2> ScalarOps;
1929
1930 if (auto Opcode3Elem = getScalar3OpcodeForReduction(Opcode);
1931 CanUseMinMax3 && Opcode3Elem)
1932 ScalarOps.push_back({*Opcode3Elem, 3});
1933 ScalarOps.push_back({getScalarOpcodeForReduction(Opcode), 2});
1934
1936 DAG.ExtractVectorElements(Vector, Elements);
1937
1938 return buildTreeReduction(Elements, EltTy, ScalarOps, DL, Flags, DAG);
1939}
1940
1941SDValue NVPTXTargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
1942 // Handle bitcasting from v2i8 without hitting the default promotion
1943 // strategy which goes through stack memory.
1944 EVT FromVT = Op->getOperand(0)->getValueType(0);
1945 if (FromVT != MVT::v2i8) {
1946 return Op;
1947 }
1948
1949 // Pack vector elements into i16 and bitcast to final type
1950 SDLoc DL(Op);
1951 SDValue Vec0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i8,
1952 Op->getOperand(0), DAG.getIntPtrConstant(0, DL));
1953 SDValue Vec1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i8,
1954 Op->getOperand(0), DAG.getIntPtrConstant(1, DL));
1955 SDValue Extend0 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i16, Vec0);
1956 SDValue Extend1 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i16, Vec1);
1957 SDValue Const8 = DAG.getConstant(8, DL, MVT::i16);
1958 SDValue AsInt = DAG.getNode(
1959 ISD::OR, DL, MVT::i16,
1960 {Extend0, DAG.getNode(ISD::SHL, DL, MVT::i16, {Extend1, Const8})});
1961 EVT ToVT = Op->getValueType(0);
1962 return DAG.getBitcast(ToVT, AsInt);
1963}
1964
1965// We can init constant f16x2/v2i16/v4i8 with a single .b32 move. Normally it
1966// would get lowered as two constant loads and vector-packing move.
1967// Instead we want just a constant move:
1968// mov.b32 %r2, 0x40003C00
1969SDValue NVPTXTargetLowering::LowerBUILD_VECTOR(SDValue Op,
1970 SelectionDAG &DAG) const {
1971 EVT VT = Op->getValueType(0);
1972 if (!(NVPTX::isPackedVectorTy(VT) && VT.is32BitVector()))
1973 return Op;
1974 SDLoc DL(Op);
1975
1976 if (!llvm::all_of(Op->ops(), [](SDValue Operand) {
1977 return Operand->isUndef() || isa<ConstantSDNode>(Operand) ||
1978 isa<ConstantFPSDNode>(Operand);
1979 })) {
1980 if (VT != MVT::v4i8)
1981 return Op;
1982 // Lower non-const v4i8 vector as byte-wise constructed i32, which allows us
1983 // to optimize calculation of constant parts.
1984 auto GetPRMT = [&](const SDValue Left, const SDValue Right, bool Cast,
1985 uint64_t SelectionValue) -> SDValue {
1986 SDValue L = Left;
1987 SDValue R = Right;
1988 if (Cast) {
1989 L = DAG.getAnyExtOrTrunc(L, DL, MVT::i32);
1990 R = DAG.getAnyExtOrTrunc(R, DL, MVT::i32);
1991 }
1992 return getPRMT(L, R, SelectionValue, DL, DAG);
1993 };
1994 auto PRMT__10 = GetPRMT(Op->getOperand(0), Op->getOperand(1), true, 0x3340);
1995 auto PRMT__32 = GetPRMT(Op->getOperand(2), Op->getOperand(3), true, 0x3340);
1996 auto PRMT3210 = GetPRMT(PRMT__10, PRMT__32, false, 0x5410);
1997 return DAG.getBitcast(VT, PRMT3210);
1998 }
1999
2000 // Get value or the Nth operand as an APInt(32). Undef values treated as 0.
2001 auto GetOperand = [](SDValue Op, int N) -> APInt {
2002 const SDValue &Operand = Op->getOperand(N);
2003 EVT VT = Op->getValueType(0);
2004 if (Operand->isUndef())
2005 return APInt(32, 0);
2006 APInt Value;
2007 if (VT == MVT::v2f16 || VT == MVT::v2bf16)
2008 Value = cast<ConstantFPSDNode>(Operand)->getValueAPF().bitcastToAPInt();
2009 else if (VT == MVT::v2i16 || VT == MVT::v4i8)
2010 Value = Operand->getAsAPIntVal();
2011 else
2012 llvm_unreachable("Unsupported type");
2013 // i8 values are carried around as i16, so we need to zero out upper bits,
2014 // so they do not get in the way of combining individual byte values
2015 if (VT == MVT::v4i8)
2016 Value = Value.trunc(8);
2017 return Value.zext(32);
2018 };
2019
2020 // Construct a 32-bit constant by shifting into place smaller values
2021 // (elements of the vector type VT).
2022 // For example, if VT has 2 elements, then N == 2:
2023 // ShiftAmount = 32 / N = 16
2024 // Value |= Op0 (b16) << 0
2025 // Value |= Op1 (b16) << 16
2026 // If N == 4:
2027 // ShiftAmount = 32 / N = 8
2028 // Value |= Op0 (b8) << 0
2029 // Value |= Op1 (b8) << 8
2030 // Value |= Op2 (b8) << 16
2031 // Value |= Op3 (b8) << 24
2032 // ...etc
2033 APInt Value(32, 0);
2034 const unsigned NumElements = VT.getVectorNumElements();
2035 assert(32 % NumElements == 0 && "must evenly divide bit length");
2036 const unsigned ShiftAmount = 32 / NumElements;
2037 for (unsigned ElementNo : seq(NumElements))
2038 Value |= GetOperand(Op, ElementNo).shl(ElementNo * ShiftAmount);
2039 SDValue Const = DAG.getConstant(Value, DL, MVT::i32);
2040 return DAG.getNode(ISD::BITCAST, DL, Op->getValueType(0), Const);
2041}
2042
2043SDValue NVPTXTargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
2044 SelectionDAG &DAG) const {
2045 SDValue Index = Op->getOperand(1);
2046 SDValue Vector = Op->getOperand(0);
2047 SDLoc DL(Op);
2048 EVT VectorVT = Vector.getValueType();
2049
2050 if (VectorVT == MVT::v4i8) {
2051 SDValue Selector = DAG.getNode(ISD::OR, DL, MVT::i32,
2052 DAG.getZExtOrTrunc(Index, DL, MVT::i32),
2053 DAG.getConstant(0x7770, DL, MVT::i32));
2054 SDValue PRMT = getPRMT(DAG.getBitcast(MVT::i32, Vector),
2055 DAG.getConstant(0, DL, MVT::i32), Selector, DL, DAG);
2056 SDValue Ext = DAG.getAnyExtOrTrunc(PRMT, DL, Op->getValueType(0));
2057 SDNodeFlags Flags;
2058 Flags.setNoSignedWrap(Ext.getScalarValueSizeInBits() > 8);
2059 Flags.setNoUnsignedWrap(Ext.getScalarValueSizeInBits() >= 8);
2060 Ext->setFlags(Flags);
2061 return Ext;
2062 }
2063
2064 // Constant index will be matched by tablegen.
2065 if (isa<ConstantSDNode>(Index.getNode()))
2066 return Op;
2067
2068 // Extract individual elements and select one of them.
2069 assert(NVPTX::isPackedVectorTy(VectorVT) &&
2070 VectorVT.getVectorNumElements() == 2 && "Unexpected vector type.");
2071 EVT EltVT = VectorVT.getVectorElementType();
2072
2073 SDLoc dl(Op.getNode());
2074 SDValue E0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Vector,
2075 DAG.getIntPtrConstant(0, dl));
2076 SDValue E1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Vector,
2077 DAG.getIntPtrConstant(1, dl));
2078 return DAG.getSelectCC(dl, Index, DAG.getIntPtrConstant(0, dl), E0, E1,
2080}
2081
2082SDValue NVPTXTargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op,
2083 SelectionDAG &DAG) const {
2084 SDValue Vector = Op->getOperand(0);
2085 EVT VectorVT = Vector.getValueType();
2086
2087 if (VectorVT != MVT::v4i8)
2088 return Op;
2089 SDLoc DL(Op);
2090 SDValue Value = Op->getOperand(1);
2091 if (Value->isUndef())
2092 return Vector;
2093
2094 SDValue Index = Op->getOperand(2);
2095
2096 SDValue BFI =
2097 DAG.getNode(NVPTXISD::BFI, DL, MVT::i32,
2098 {DAG.getZExtOrTrunc(Value, DL, MVT::i32), Vector,
2099 DAG.getNode(ISD::MUL, DL, MVT::i32,
2100 DAG.getZExtOrTrunc(Index, DL, MVT::i32),
2101 DAG.getConstant(8, DL, MVT::i32)),
2102 DAG.getConstant(8, DL, MVT::i32)});
2103 return DAG.getNode(ISD::BITCAST, DL, Op->getValueType(0), BFI);
2104}
2105
2106SDValue NVPTXTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
2107 SelectionDAG &DAG) const {
2108 SDValue V1 = Op.getOperand(0);
2109 EVT VectorVT = V1.getValueType();
2110 if (VectorVT != MVT::v4i8 || Op.getValueType() != MVT::v4i8)
2111 return Op;
2112
2113 // Lower shuffle to PRMT instruction.
2114 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
2115 SDValue V2 = Op.getOperand(1);
2116 uint32_t Selector = 0;
2117 for (auto I : llvm::enumerate(SVN->getMask())) {
2118 if (I.value() != -1) // -1 is a placeholder for undef.
2119 Selector |= (I.value() << (I.index() * 4));
2120 }
2121
2122 SDLoc DL(Op);
2123 SDValue PRMT = getPRMT(DAG.getBitcast(MVT::i32, V1),
2124 DAG.getBitcast(MVT::i32, V2), Selector, DL, DAG);
2125 return DAG.getBitcast(Op.getValueType(), PRMT);
2126}
2127/// LowerShiftRightParts - Lower SRL_PARTS, SRA_PARTS, which
2128/// 1) returns two i32 values and take a 2 x i32 value to shift plus a shift
2129/// amount, or
2130/// 2) returns two i64 values and take a 2 x i64 value to shift plus a shift
2131/// amount.
2132SDValue NVPTXTargetLowering::LowerShiftRightParts(SDValue Op,
2133 SelectionDAG &DAG) const {
2134 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2135 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
2136
2137 EVT VT = Op.getValueType();
2138 unsigned VTBits = VT.getSizeInBits();
2139 SDLoc dl(Op);
2140 SDValue ShOpLo = Op.getOperand(0);
2141 SDValue ShOpHi = Op.getOperand(1);
2142 SDValue ShAmt = Op.getOperand(2);
2143 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
2144
2145 if (VTBits == 32 && STI.hasFeature(NVPTX::SM35)) {
2146 // For 32bit and sm35, we can use the funnel shift 'shf' instruction.
2147 // {dHi, dLo} = {aHi, aLo} >> Amt
2148 // dHi = aHi >> Amt
2149 // dLo = shf.r.clamp aLo, aHi, Amt
2150
2151 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
2152 SDValue Lo =
2153 DAG.getNode(NVPTXISD::FSHR_CLAMP, dl, VT, ShOpHi, ShOpLo, ShAmt);
2154
2155 SDValue Ops[2] = { Lo, Hi };
2156 return DAG.getMergeValues(Ops, dl);
2157 } else {
2158 // {dHi, dLo} = {aHi, aLo} >> Amt
2159 // - if (Amt>=size) then
2160 // dLo = aHi >> (Amt-size)
2161 // dHi = aHi >> Amt (this is either all 0 or all 1)
2162 // else
2163 // dLo = (aLo >>logic Amt) | (aHi << (size-Amt))
2164 // dHi = aHi >> Amt
2165
2166 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2167 DAG.getConstant(VTBits, dl, MVT::i32),
2168 ShAmt);
2169 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
2170 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2171 DAG.getConstant(VTBits, dl, MVT::i32));
2172 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
2173 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2174 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
2175
2176 SDValue Cmp = DAG.getSetCC(dl, MVT::i1, ShAmt,
2177 DAG.getConstant(VTBits, dl, MVT::i32),
2178 ISD::SETGE);
2179 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
2180 SDValue Lo = DAG.getNode(ISD::SELECT, dl, VT, Cmp, TrueVal, FalseVal);
2181
2182 SDValue Ops[2] = { Lo, Hi };
2183 return DAG.getMergeValues(Ops, dl);
2184 }
2185}
2186
2187/// LowerShiftLeftParts - Lower SHL_PARTS, which
2188/// 1) returns two i32 values and take a 2 x i32 value to shift plus a shift
2189/// amount, or
2190/// 2) returns two i64 values and take a 2 x i64 value to shift plus a shift
2191/// amount.
2192SDValue NVPTXTargetLowering::LowerShiftLeftParts(SDValue Op,
2193 SelectionDAG &DAG) const {
2194 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
2195 assert(Op.getOpcode() == ISD::SHL_PARTS);
2196
2197 EVT VT = Op.getValueType();
2198 unsigned VTBits = VT.getSizeInBits();
2199 SDLoc dl(Op);
2200 SDValue ShOpLo = Op.getOperand(0);
2201 SDValue ShOpHi = Op.getOperand(1);
2202 SDValue ShAmt = Op.getOperand(2);
2203
2204 if (VTBits == 32 && STI.hasFeature(NVPTX::SM35)) {
2205 // For 32bit and sm35, we can use the funnel shift 'shf' instruction.
2206 // {dHi, dLo} = {aHi, aLo} << Amt
2207 // dHi = shf.l.clamp aLo, aHi, Amt
2208 // dLo = aLo << Amt
2209
2210 SDValue Hi =
2211 DAG.getNode(NVPTXISD::FSHL_CLAMP, dl, VT, ShOpHi, ShOpLo, ShAmt);
2212 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
2213
2214 SDValue Ops[2] = { Lo, Hi };
2215 return DAG.getMergeValues(Ops, dl);
2216 } else {
2217 // {dHi, dLo} = {aHi, aLo} << Amt
2218 // - if (Amt>=size) then
2219 // dLo = aLo << Amt (all 0)
2220 // dLo = aLo << (Amt-size)
2221 // else
2222 // dLo = aLo << Amt
2223 // dHi = (aHi << Amt) | (aLo >> (size-Amt))
2224
2225 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
2226 DAG.getConstant(VTBits, dl, MVT::i32),
2227 ShAmt);
2228 SDValue Tmp1 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
2229 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
2230 DAG.getConstant(VTBits, dl, MVT::i32));
2231 SDValue Tmp2 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
2232 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
2233 SDValue TrueVal = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
2234
2235 SDValue Cmp = DAG.getSetCC(dl, MVT::i1, ShAmt,
2236 DAG.getConstant(VTBits, dl, MVT::i32),
2237 ISD::SETGE);
2238 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
2239 SDValue Hi = DAG.getNode(ISD::SELECT, dl, VT, Cmp, TrueVal, FalseVal);
2240
2241 SDValue Ops[2] = { Lo, Hi };
2242 return DAG.getMergeValues(Ops, dl);
2243 }
2244}
2245
2246/// If the types match, convert the generic copysign to the NVPTXISD version,
2247/// otherwise bail ensuring that mismatched cases are properly expaned.
2248SDValue NVPTXTargetLowering::LowerFCOPYSIGN(SDValue Op,
2249 SelectionDAG &DAG) const {
2250 EVT VT = Op.getValueType();
2251 SDLoc DL(Op);
2252
2253 SDValue In1 = Op.getOperand(0);
2254 SDValue In2 = Op.getOperand(1);
2255 EVT SrcVT = In2.getValueType();
2256
2257 if (!SrcVT.bitsEq(VT))
2258 return SDValue();
2259
2260 return DAG.getNode(NVPTXISD::FCOPYSIGN, DL, VT, In1, In2);
2261}
2262
2263SDValue NVPTXTargetLowering::LowerFROUND(SDValue Op, SelectionDAG &DAG) const {
2264 EVT VT = Op.getValueType();
2265
2266 if (VT == MVT::f32)
2267 return LowerFROUND32(Op, DAG);
2268
2269 if (VT == MVT::f64)
2270 return LowerFROUND64(Op, DAG);
2271
2272 llvm_unreachable("unhandled type");
2273}
2274
2275// This is the the rounding method used in CUDA libdevice in C like code:
2276// float roundf(float A)
2277// {
2278// float RoundedA = (float) (int) ( A > 0 ? (A + 0.5f) : (A - 0.5f));
2279// RoundedA = abs(A) > 0x1.0p23 ? A : RoundedA;
2280// return abs(A) < 0.5 ? (float)(int)A : RoundedA;
2281// }
2282SDValue NVPTXTargetLowering::LowerFROUND32(SDValue Op,
2283 SelectionDAG &DAG) const {
2284 SDLoc SL(Op);
2285 SDValue A = Op.getOperand(0);
2286 EVT VT = Op.getValueType();
2287
2288 SDValue AbsA = DAG.getNode(ISD::FABS, SL, VT, A);
2289
2290 // RoundedA = (float) (int) ( A > 0 ? (A + 0.5f) : (A - 0.5f))
2291 SDValue Bitcast = DAG.getNode(ISD::BITCAST, SL, MVT::i32, A);
2292 const unsigned SignBitMask = 0x80000000;
2293 SDValue Sign = DAG.getNode(ISD::AND, SL, MVT::i32, Bitcast,
2294 DAG.getConstant(SignBitMask, SL, MVT::i32));
2295 const unsigned PointFiveInBits = 0x3F000000;
2296 SDValue PointFiveWithSignRaw =
2297 DAG.getNode(ISD::OR, SL, MVT::i32, Sign,
2298 DAG.getConstant(PointFiveInBits, SL, MVT::i32));
2299 SDValue PointFiveWithSign =
2300 DAG.getNode(ISD::BITCAST, SL, VT, PointFiveWithSignRaw);
2301 SDValue AdjustedA = DAG.getNode(ISD::FADD, SL, VT, A, PointFiveWithSign);
2302 SDValue RoundedA = DAG.getNode(ISD::FTRUNC, SL, VT, AdjustedA);
2303
2304 // RoundedA = abs(A) > 0x1.0p23 ? A : RoundedA;
2305 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
2306 SDValue IsLarge =
2307 DAG.getSetCC(SL, SetCCVT, AbsA, DAG.getConstantFP(pow(2.0, 23.0), SL, VT),
2308 ISD::SETOGT);
2309 RoundedA = DAG.getNode(ISD::SELECT, SL, VT, IsLarge, A, RoundedA);
2310
2311 // return abs(A) < 0.5 ? (float)(int)A : RoundedA;
2312 SDValue IsSmall =DAG.getSetCC(SL, SetCCVT, AbsA,
2313 DAG.getConstantFP(0.5, SL, VT), ISD::SETOLT);
2314 SDValue RoundedAForSmallA = DAG.getNode(ISD::FTRUNC, SL, VT, A);
2315 return DAG.getNode(ISD::SELECT, SL, VT, IsSmall, RoundedAForSmallA, RoundedA);
2316}
2317
2318// The implementation of round(double) is similar to that of round(float) in
2319// that they both separate the value range into three regions and use a method
2320// specific to the region to round the values. However, round(double) first
2321// calculates the round of the absolute value and then adds the sign back while
2322// round(float) directly rounds the value with sign.
2323SDValue NVPTXTargetLowering::LowerFROUND64(SDValue Op,
2324 SelectionDAG &DAG) const {
2325 SDLoc SL(Op);
2326 SDValue A = Op.getOperand(0);
2327 EVT VT = Op.getValueType();
2328
2329 SDValue AbsA = DAG.getNode(ISD::FABS, SL, VT, A);
2330
2331 // double RoundedA = (double) (int) (abs(A) + 0.5f);
2332 SDValue AdjustedA = DAG.getNode(ISD::FADD, SL, VT, AbsA,
2333 DAG.getConstantFP(0.5, SL, VT));
2334 SDValue RoundedA = DAG.getNode(ISD::FTRUNC, SL, VT, AdjustedA);
2335
2336 // RoundedA = abs(A) < 0.5 ? (double)0 : RoundedA;
2337 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
2338 SDValue IsSmall =DAG.getSetCC(SL, SetCCVT, AbsA,
2339 DAG.getConstantFP(0.5, SL, VT), ISD::SETOLT);
2340 RoundedA = DAG.getNode(ISD::SELECT, SL, VT, IsSmall,
2341 DAG.getConstantFP(0, SL, VT),
2342 RoundedA);
2343
2344 // Add sign to rounded_A
2345 RoundedA = DAG.getNode(ISD::FCOPYSIGN, SL, VT, RoundedA, A);
2346 DAG.getNode(ISD::FTRUNC, SL, VT, A);
2347
2348 // RoundedA = abs(A) > 0x1.0p52 ? A : RoundedA;
2349 SDValue IsLarge =
2350 DAG.getSetCC(SL, SetCCVT, AbsA, DAG.getConstantFP(pow(2.0, 52.0), SL, VT),
2351 ISD::SETOGT);
2352 return DAG.getNode(ISD::SELECT, SL, VT, IsLarge, A, RoundedA);
2353}
2354
2356 EVT VT = N->getValueType(0);
2357 EVT NVT = MVT::f32;
2358 if (VT.isVector()) {
2359 NVT = EVT::getVectorVT(*DAG.getContext(), NVT, VT.getVectorElementCount());
2360 }
2361 SDLoc DL(N);
2362 SDValue Tmp0 = DAG.getFPExtendOrRound(N->getOperand(0), DL, NVT);
2363 SDValue Tmp1 = DAG.getFPExtendOrRound(N->getOperand(1), DL, NVT);
2364 SDValue Res = DAG.getNode(N->getOpcode(), DL, NVT, Tmp0, Tmp1, N->getFlags());
2365 return DAG.getFPExtendOrRound(Res, DL, VT);
2366}
2367
2368SDValue NVPTXTargetLowering::PromoteBinOpIfF32FTZ(SDValue Op,
2369 SelectionDAG &DAG) const {
2370 if (useF32FTZ(DAG.getMachineFunction())) {
2371 return PromoteBinOpToF32(Op.getNode(), DAG);
2372 }
2373 return Op;
2374}
2375
2376SDValue NVPTXTargetLowering::LowerINT_TO_FP(SDValue Op,
2377 SelectionDAG &DAG) const {
2378 assert(!STI.hasFeature(NVPTX::SM90));
2379
2380 if (Op.getValueType() == MVT::bf16) {
2381 SDLoc Loc(Op);
2382 return DAG.getNode(
2383 ISD::FP_ROUND, Loc, MVT::bf16,
2384 DAG.getNode(Op.getOpcode(), Loc, MVT::f32, Op.getOperand(0)),
2385 DAG.getIntPtrConstant(0, Loc, /*isTarget=*/true));
2386 }
2387
2388 // Everything else is considered legal.
2389 return Op;
2390}
2391
2392SDValue NVPTXTargetLowering::LowerFP_TO_INT(SDValue Op,
2393 SelectionDAG &DAG) const {
2394 assert(!STI.hasFeature(NVPTX::SM90));
2395
2396 if (Op.getOperand(0).getValueType() == MVT::bf16) {
2397 SDLoc Loc(Op);
2398 return DAG.getNode(
2399 Op.getOpcode(), Loc, Op.getValueType(),
2400 DAG.getNode(ISD::FP_EXTEND, Loc, MVT::f32, Op.getOperand(0)));
2401 }
2402
2403 // Everything else is considered legal.
2404 return Op;
2405}
2406
2407SDValue NVPTXTargetLowering::LowerFP_ROUND(SDValue Op,
2408 SelectionDAG &DAG) const {
2409 EVT NarrowVT = Op.getValueType();
2410 SDValue Wide = Op.getOperand(0);
2411 EVT WideVT = Wide.getValueType();
2412 if (NarrowVT.getScalarType() == MVT::bf16) {
2413 const TargetLowering *TLI = STI.getTargetLowering();
2414 if (!STI.hasFeature(NVPTX::SM80)) {
2415 return TLI->expandFP_ROUND(Op.getNode(), DAG);
2416 }
2417 if (!STI.hasFeature(NVPTX::SM90)) {
2418 // sm_80 was the first architecture to support f32 -> bf16.
2419 if (WideVT.getScalarType() == MVT::f32) {
2420 return Op;
2421 }
2422 if (WideVT.getScalarType() == MVT::f64) {
2423 SDLoc Loc(Op);
2424 // Round-inexact-to-odd f64 to f32, then do the final rounding using
2425 // the hardware f32 -> bf16 instruction.
2426 SDValue rod = TLI->expandRoundInexactToOdd(
2427 WideVT.changeElementType(*DAG.getContext(), MVT::f32), Wide, Loc,
2428 DAG);
2429 return DAG.getFPExtendOrRound(rod, Loc, NarrowVT);
2430 }
2431 return TLI->expandFP_ROUND(Op.getNode(), DAG);
2432 }
2433 }
2434
2435 // Everything else is considered legal.
2436 return Op;
2437}
2438
2439SDValue NVPTXTargetLowering::LowerFP_EXTEND(SDValue Op,
2440 SelectionDAG &DAG) const {
2441 SDValue Narrow = Op.getOperand(0);
2442 EVT NarrowVT = Narrow.getValueType();
2443 EVT WideVT = Op.getValueType();
2444 if (NarrowVT.getScalarType() == MVT::bf16) {
2445 if (WideVT.getScalarType() == MVT::f32 &&
2446 (!STI.hasFeature(NVPTX::SM80) || !STI.hasFeature(NVPTX::PTX71))) {
2447 SDLoc Loc(Op);
2448 return DAG.getNode(ISD::BF16_TO_FP, Loc, WideVT, Narrow);
2449 }
2450 if (WideVT.getScalarType() == MVT::f64 && !STI.hasFeature(NVPTX::SM90)) {
2451 EVT F32 = NarrowVT.changeElementType(*DAG.getContext(), MVT::f32);
2452 SDLoc Loc(Op);
2453 if (STI.hasFeature(NVPTX::SM80) && STI.hasFeature(NVPTX::PTX71)) {
2454 Op = DAG.getNode(ISD::FP_EXTEND, Loc, F32, Narrow);
2455 } else {
2456 Op = DAG.getNode(ISD::BF16_TO_FP, Loc, F32, Narrow);
2457 }
2458 return DAG.getNode(ISD::FP_EXTEND, Loc, WideVT, Op);
2459 }
2460 }
2461
2462 // Everything else is considered legal.
2463 return Op;
2464}
2465
2467 SDLoc DL(Op);
2468 if (Op.getValueType() != MVT::v2i16)
2469 return Op;
2470 EVT EltVT = Op.getValueType().getVectorElementType();
2471 SmallVector<SDValue> VecElements;
2472 for (int I = 0, E = Op.getValueType().getVectorNumElements(); I < E; I++) {
2473 SmallVector<SDValue> ScalarArgs;
2474 llvm::transform(Op->ops(), std::back_inserter(ScalarArgs),
2475 [&](const SDUse &O) {
2476 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT,
2477 O.get(), DAG.getIntPtrConstant(I, DL));
2478 });
2479 VecElements.push_back(DAG.getNode(Op.getOpcode(), DL, EltVT, ScalarArgs));
2480 }
2481 SDValue V =
2482 DAG.getNode(ISD::BUILD_VECTOR, DL, Op.getValueType(), VecElements);
2483 return V;
2484}
2485
2487 bool hasOffset = false) {
2488 // skip lowering if the vector operand is already legalized
2489 if (!Op->getOperand(hasOffset ? 4 : 3).getValueType().isVector())
2490 return Op;
2491
2492 SDNode *N = Op.getNode();
2493 SDLoc DL(N);
2495
2496 // split the vector argument
2497 for (size_t I = 0; I < N->getNumOperands(); I++) {
2498 SDValue Val = N->getOperand(I);
2499 EVT ValVT = Val.getValueType();
2500 if (ValVT.isVector()) {
2501 EVT EltVT = ValVT.getVectorElementType();
2502 for (unsigned J = 0, NElts = ValVT.getVectorNumElements(); J < NElts; J++)
2503 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Val,
2504 DAG.getIntPtrConstant(J, DL)));
2505 } else
2506 Ops.push_back(Val);
2507 }
2508
2510 SDValue Tcgen05StNode =
2511 DAG.getMemIntrinsicNode(ISD::INTRINSIC_VOID, DL, N->getVTList(), Ops,
2512 MemSD->getMemoryVT(), MemSD->getMemOperand());
2513
2514 return Tcgen05StNode;
2515}
2516
2518 SDLoc DL(Op);
2519 SDValue Src = Op.getOperand(0);
2520 EVT VT = Op.getValueType();
2521
2522 switch (VT.getSimpleVT().SimpleTy) {
2523 case MVT::i16: {
2524 SDValue Extended = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, Src);
2525 SDValue Swapped =
2526 getPRMT(Extended, DAG.getConstant(0, DL, MVT::i32), 0x7701, DL, DAG);
2527 return DAG.getNode(ISD::TRUNCATE, DL, MVT::i16, Swapped);
2528 }
2529 case MVT::i32: {
2530 return getPRMT(Src, DAG.getConstant(0, DL, MVT::i32), 0x0123, DL, DAG);
2531 }
2532 case MVT::v2i16: {
2533 SDValue Converted = DAG.getBitcast(MVT::i32, Src);
2534 SDValue Swapped =
2535 getPRMT(Converted, DAG.getConstant(0, DL, MVT::i32), 0x2301, DL, DAG);
2536 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i16, Swapped);
2537 }
2538 case MVT::i64: {
2539 SDValue UnpackSrc =
2540 DAG.getNode(NVPTXISD::UNPACK_VECTOR, DL, {MVT::i32, MVT::i32}, Src);
2541 SDValue SwappedLow =
2542 getPRMT(UnpackSrc.getValue(0), DAG.getConstant(0, DL, MVT::i32), 0x0123,
2543 DL, DAG);
2544 SDValue SwappedHigh =
2545 getPRMT(UnpackSrc.getValue(1), DAG.getConstant(0, DL, MVT::i32), 0x0123,
2546 DL, DAG);
2547 return DAG.getNode(NVPTXISD::BUILD_VECTOR, DL, MVT::i64,
2548 {SwappedHigh, SwappedLow});
2549 }
2550 default:
2551 llvm_unreachable("unsupported type for bswap");
2552 }
2553}
2554
2556 const Function &Fn = DAG.getMachineFunction().getFunction();
2557 SDNode *N = Op.getNode();
2558 SDLoc DL(N);
2559 Intrinsic::ID IntrinsicID = N->getConstantOperandVal(1);
2560 SDValue DestAddr = N->getOperand(2);
2561 SDValue Value = N->getOperand(3);
2562 SDValue MbarAddr = N->getOperand(4);
2563
2564 MVT ValueVT = Value.getSimpleValueType();
2565
2566 if (ValueVT == MVT::i32 || ValueVT == MVT::i64)
2567 return Op;
2568
2569 if (ValueVT == MVT::i128) {
2570 SDValue Cast = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Value);
2571 SDValue ValueLo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i64, Cast,
2572 DAG.getIntPtrConstant(0, DL));
2573 SDValue ValueHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i64, Cast,
2574 DAG.getIntPtrConstant(1, DL));
2575 SDValue Ops[] = {N->getOperand(0), DestAddr, ValueLo, ValueHi, MbarAddr};
2576 return DAG.getNode(NVPTXISD::ST_ASYNC_MBARRIER_B128, DL, MVT::Other, Ops);
2577 }
2578
2580 Fn,
2581 Twine("unsupported argument type ") + llvm::EVT(ValueVT).getEVTString() +
2582 " for " + llvm::Intrinsic::getName(IntrinsicID) + " intrinsic",
2583 DiagnosticLocation(DL.getDebugLoc())));
2584 return Op.getOperand(0); // Return only the chain
2585}
2586
2588 const Function &Fn = DAG.getMachineFunction().getFunction();
2589 SDNode *N = Op.getNode();
2590 SDLoc DL(N);
2591 Intrinsic::ID IntrinsicID = N->getConstantOperandVal(1);
2592 SDValue DestAddr = N->getOperand(2);
2593 SDValue Value = N->getOperand(3);
2594
2595 MVT ValueVT = Value.getSimpleValueType();
2596
2597 if (ValueVT == MVT::i16 || ValueVT == MVT::i32 || ValueVT == MVT::i64)
2598 return Op;
2599
2600 if (ValueVT == MVT::i8) {
2601 unsigned OpCode;
2602 switch (IntrinsicID) {
2603 case Intrinsic::nvvm_st_async_sys:
2604 OpCode = NVPTXISD::ST_ASYNC_SYS_B8;
2605 break;
2606 case Intrinsic::nvvm_st_async_gpu:
2607 OpCode = NVPTXISD::ST_ASYNC_GPU_B8;
2608 break;
2609 case Intrinsic::nvvm_st_async_mmio_sys:
2610 OpCode = NVPTXISD::ST_ASYNC_MMIO_SYS_B8;
2611 break;
2612 default:
2613 llvm_unreachable("unexpected intrinsic ID for st.async.release");
2614 }
2615
2616 Value = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i16, Value);
2617
2618 // The `.mmio` variant has no multimem form and therefore no `isMultimem`
2619 // operand.
2620 if (IntrinsicID == Intrinsic::nvvm_st_async_mmio_sys) {
2621 SDValue Ops[] = {N->getOperand(0), DestAddr, Value};
2622 return DAG.getNode(OpCode, DL, MVT::Other, Ops);
2623 }
2624
2625 SDValue IsMultimem =
2626 DAG.getTargetConstant(N->getConstantOperandVal(4), DL, MVT::i1);
2627 SDValue Ops[] = {N->getOperand(0), DestAddr, Value, IsMultimem};
2628 return DAG.getNode(OpCode, DL, MVT::Other, Ops);
2629 }
2630
2632 Fn,
2633 Twine("unsupported argument type ") + llvm::EVT(ValueVT).getEVTString() +
2634 " for " + llvm::Intrinsic::getName(IntrinsicID) + " intrinsic",
2635 DiagnosticLocation(DL.getDebugLoc())));
2636 return Op.getOperand(0); // Return only the chain
2637}
2638
2639static unsigned getTcgen05MMADisableOutputLane(unsigned IID) {
2640 switch (IID) {
2641 case Intrinsic::nvvm_tcgen05_mma_shared_disable_output_lane_cg1:
2642 return NVPTXISD::TCGEN05_MMA_SHARED_DISABLE_OUTPUT_LANE_CG1;
2643 case Intrinsic::nvvm_tcgen05_mma_shared_disable_output_lane_cg2:
2644 return NVPTXISD::TCGEN05_MMA_SHARED_DISABLE_OUTPUT_LANE_CG2;
2645 case Intrinsic::nvvm_tcgen05_mma_shared_scale_d_disable_output_lane_cg1:
2646 return NVPTXISD::TCGEN05_MMA_SHARED_SCALE_D_DISABLE_OUTPUT_LANE_CG1;
2647 case Intrinsic::nvvm_tcgen05_mma_shared_scale_d_disable_output_lane_cg2:
2648 return NVPTXISD::TCGEN05_MMA_SHARED_SCALE_D_DISABLE_OUTPUT_LANE_CG2;
2649 case Intrinsic::nvvm_tcgen05_mma_tensor_disable_output_lane_cg1:
2650 return NVPTXISD::TCGEN05_MMA_TENSOR_DISABLE_OUTPUT_LANE_CG1;
2651 case Intrinsic::nvvm_tcgen05_mma_tensor_disable_output_lane_cg2:
2652 return NVPTXISD::TCGEN05_MMA_TENSOR_DISABLE_OUTPUT_LANE_CG2;
2653 case Intrinsic::nvvm_tcgen05_mma_tensor_scale_d_disable_output_lane_cg1:
2654 return NVPTXISD::TCGEN05_MMA_TENSOR_SCALE_D_DISABLE_OUTPUT_LANE_CG1;
2655 case Intrinsic::nvvm_tcgen05_mma_tensor_scale_d_disable_output_lane_cg2:
2656 return NVPTXISD::TCGEN05_MMA_TENSOR_SCALE_D_DISABLE_OUTPUT_LANE_CG2;
2657 case Intrinsic::nvvm_tcgen05_mma_tensor_disable_output_lane_cg1_ashift:
2658 return NVPTXISD::TCGEN05_MMA_TENSOR_DISABLE_OUTPUT_LANE_CG1_ASHIFT;
2659 case Intrinsic::nvvm_tcgen05_mma_tensor_disable_output_lane_cg2_ashift:
2660 return NVPTXISD::TCGEN05_MMA_TENSOR_DISABLE_OUTPUT_LANE_CG2_ASHIFT;
2661 case Intrinsic::
2662 nvvm_tcgen05_mma_tensor_scale_d_disable_output_lane_cg1_ashift:
2663 return NVPTXISD::TCGEN05_MMA_TENSOR_SCALE_D_DISABLE_OUTPUT_LANE_CG1_ASHIFT;
2664 case Intrinsic::
2665 nvvm_tcgen05_mma_tensor_scale_d_disable_output_lane_cg2_ashift:
2666 return NVPTXISD::TCGEN05_MMA_TENSOR_SCALE_D_DISABLE_OUTPUT_LANE_CG2_ASHIFT;
2667 case Intrinsic::nvvm_tcgen05_mma_sp_shared_disable_output_lane_cg1:
2668 return NVPTXISD::TCGEN05_MMA_SP_SHARED_DISABLE_OUTPUT_LANE_CG1;
2669 case Intrinsic::nvvm_tcgen05_mma_sp_shared_disable_output_lane_cg2:
2670 return NVPTXISD::TCGEN05_MMA_SP_SHARED_DISABLE_OUTPUT_LANE_CG2;
2671 case Intrinsic::nvvm_tcgen05_mma_sp_shared_scale_d_disable_output_lane_cg1:
2672 return NVPTXISD::TCGEN05_MMA_SP_SHARED_SCALE_D_DISABLE_OUTPUT_LANE_CG1;
2673 case Intrinsic::nvvm_tcgen05_mma_sp_shared_scale_d_disable_output_lane_cg2:
2674 return NVPTXISD::TCGEN05_MMA_SP_SHARED_SCALE_D_DISABLE_OUTPUT_LANE_CG2;
2675 case Intrinsic::nvvm_tcgen05_mma_sp_tensor_disable_output_lane_cg1:
2676 return NVPTXISD::TCGEN05_MMA_SP_TENSOR_DISABLE_OUTPUT_LANE_CG1;
2677 case Intrinsic::nvvm_tcgen05_mma_sp_tensor_disable_output_lane_cg2:
2678 return NVPTXISD::TCGEN05_MMA_SP_TENSOR_DISABLE_OUTPUT_LANE_CG2;
2679 case Intrinsic::nvvm_tcgen05_mma_sp_tensor_disable_output_lane_cg1_ashift:
2680 return NVPTXISD::TCGEN05_MMA_SP_TENSOR_DISABLE_OUTPUT_LANE_CG1_ASHIFT;
2681 case Intrinsic::nvvm_tcgen05_mma_sp_tensor_disable_output_lane_cg2_ashift:
2682 return NVPTXISD::TCGEN05_MMA_SP_TENSOR_DISABLE_OUTPUT_LANE_CG2_ASHIFT;
2683 case Intrinsic::nvvm_tcgen05_mma_sp_tensor_scale_d_disable_output_lane_cg1:
2684 return NVPTXISD::TCGEN05_MMA_SP_TENSOR_SCALE_D_DISABLE_OUTPUT_LANE_CG1;
2685 case Intrinsic::nvvm_tcgen05_mma_sp_tensor_scale_d_disable_output_lane_cg2:
2686 return NVPTXISD::TCGEN05_MMA_SP_TENSOR_SCALE_D_DISABLE_OUTPUT_LANE_CG2;
2687 case Intrinsic::
2688 nvvm_tcgen05_mma_sp_tensor_scale_d_disable_output_lane_cg1_ashift:
2689 return NVPTXISD::
2690 TCGEN05_MMA_SP_TENSOR_SCALE_D_DISABLE_OUTPUT_LANE_CG1_ASHIFT;
2691 case Intrinsic::
2692 nvvm_tcgen05_mma_sp_tensor_scale_d_disable_output_lane_cg2_ashift:
2693 return NVPTXISD::
2694 TCGEN05_MMA_SP_TENSOR_SCALE_D_DISABLE_OUTPUT_LANE_CG2_ASHIFT;
2695 case Intrinsic::
2696 nvvm_tcgen05_mma_shared_f8f6f4_disable_output_lane_cg1_decompress_b:
2697 return NVPTXISD::TCGEN05_MMA_SHARED_DISABLE_OUTPUT_LANE_CG1_DECOMPRESS_B;
2698 case Intrinsic::
2699 nvvm_tcgen05_mma_shared_f8f6f4_disable_output_lane_cg2_decompress_b:
2700 return NVPTXISD::TCGEN05_MMA_SHARED_DISABLE_OUTPUT_LANE_CG2_DECOMPRESS_B;
2701 case Intrinsic::
2702 nvvm_tcgen05_mma_tensor_f8f6f4_disable_output_lane_cg1_decompress_b:
2703 return NVPTXISD::TCGEN05_MMA_TENSOR_DISABLE_OUTPUT_LANE_CG1_DECOMPRESS_B;
2704 case Intrinsic::
2705 nvvm_tcgen05_mma_tensor_f8f6f4_disable_output_lane_cg2_decompress_b:
2706 return NVPTXISD::TCGEN05_MMA_TENSOR_DISABLE_OUTPUT_LANE_CG2_DECOMPRESS_B;
2707 };
2708 llvm_unreachable("unhandled tcgen05.mma.disable_output_lane intrinsic");
2709}
2710
2712 SDNode *N = Op.getNode();
2713 SDLoc DL(N);
2714 unsigned IID = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
2715
2717 // split the vector argument
2718 for (size_t I = 0; I < N->getNumOperands(); I++) {
2719 if (I == 1)
2720 continue; // skip IID
2721 SDValue Val = N->getOperand(I);
2722 EVT ValVT = Val.getValueType();
2723 if (ValVT.isVector()) {
2724 EVT EltVT = ValVT.getVectorElementType();
2725 for (unsigned J = 0, NElts = ValVT.getVectorNumElements(); J < NElts; J++)
2726 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Val,
2727 DAG.getIntPtrConstant(J, DL)));
2728 } else
2729 Ops.push_back(Val);
2730 }
2731
2733 SDValue Tcgen05MMANode = DAG.getMemIntrinsicNode(
2734 getTcgen05MMADisableOutputLane(IID), DL, N->getVTList(), Ops,
2735 MemSD->getMemoryVT(), MemSD->getMemOperand());
2736
2737 return Tcgen05MMANode;
2738}
2739
2740// Lower vector return type of tcgen05.ld intrinsics
2741static std::optional<std::pair<SDValue, SDValue>>
2742lowerTcgen05Ld(SDNode *N, SelectionDAG &DAG, bool HasOffset = false) {
2743 SDLoc DL(N);
2744 EVT ResVT = N->getValueType(0);
2745 if (!ResVT.isVector())
2746 return {}; // already legalized.
2747
2748 const unsigned NumElts = ResVT.getVectorNumElements();
2749
2750 // Create the return type of the instructions
2751 SmallVector<EVT, 5> ListVTs;
2752 for (unsigned i = 0; i < NumElts; ++i)
2753 ListVTs.push_back(MVT::i32);
2754
2755 ListVTs.push_back(N->getValueType(1)); // Chain
2756
2757 SDVTList ResVTs = DAG.getVTList(ListVTs);
2758
2759 SmallVector<SDValue, 8> Ops{N->getOperand(0), N->getOperand(1),
2760 N->getOperand(2)};
2761
2762 if (HasOffset) {
2763 Ops.push_back(N->getOperand(3)); // offset
2764 Ops.push_back(N->getOperand(4)); // Pack flag
2765 } else
2766 Ops.push_back(N->getOperand(3)); // Pack flag
2767
2769 SDValue NewNode =
2771 MemSD->getMemoryVT(), MemSD->getMemOperand());
2772
2773 // split the vector result
2774 SmallVector<SDValue, 4> ScalarRes;
2775 for (unsigned i = 0; i < NumElts; ++i) {
2776 SDValue Res = NewNode.getValue(i);
2777 ScalarRes.push_back(Res);
2778 }
2779
2780 SDValue Chain = NewNode.getValue(NumElts);
2781 SDValue BuildVector = DAG.getNode(ISD::BUILD_VECTOR, DL, ResVT, ScalarRes);
2782 return {{BuildVector, Chain}};
2783}
2784
2786 unsigned Val) {
2787 SDNode *N = Op.getNode();
2788 SDLoc DL(N);
2789
2790 const Function &Fn = DAG.getMachineFunction().getFunction();
2791
2792 unsigned AS = 0;
2793 if (auto *MemN = dyn_cast<MemIntrinsicSDNode>(N))
2794 AS = MemN->getAddressSpace();
2795 Type *PtrTy = PointerType::get(*DAG.getContext(), AS);
2797
2799 Fn,
2800 "Intrinsic " +
2801 Intrinsic::getName(N->getConstantOperandVal(1), {PtrTy}, M) +
2802 " with value " + Twine(Val) +
2803 " is not supported on the given target.",
2804 DL.getDebugLoc()));
2805 return Op.getOperand(0);
2806}
2807
2809 SDNode *N = Op.getNode();
2810 SDLoc DL(N);
2811
2812 // immediate argument representing elemtype
2813 unsigned Val = N->getConstantOperandVal(3);
2814
2816 Val))
2817 return reportInvalidTensormapReplaceUsage(Op, DAG, Val);
2818
2819 return Op;
2820}
2821
2823 SDNode *N = Op.getNode();
2824 SDLoc DL(N);
2825
2826 // immediate argument representing swizzle mode
2827 unsigned Val = N->getConstantOperandVal(3);
2828
2830 Val))
2831 return reportInvalidTensormapReplaceUsage(Op, DAG, Val);
2832
2833 return Op;
2834}
2835
2837 SDNode *N = Op.getNode();
2838 SDValue Intrin = N->getOperand(1);
2839
2840 // Get the intrinsic ID
2841 unsigned IntrinNo = cast<ConstantSDNode>(Intrin.getNode())->getZExtValue();
2842 switch (IntrinNo) {
2843 default:
2844 break;
2845 case Intrinsic::nvvm_st_async:
2846 return lowerStAsyncWithMbarrier(Op, DAG);
2847 case Intrinsic::nvvm_st_async_sys:
2848 case Intrinsic::nvvm_st_async_gpu:
2849 case Intrinsic::nvvm_st_async_mmio_sys:
2850 return lowerStAsyncRelease(Op, DAG);
2851
2852 case Intrinsic::nvvm_tcgen05_st_16x64b_x1:
2853 case Intrinsic::nvvm_tcgen05_st_16x64b_x2:
2854 case Intrinsic::nvvm_tcgen05_st_16x64b_x4:
2855 case Intrinsic::nvvm_tcgen05_st_16x64b_x8:
2856 case Intrinsic::nvvm_tcgen05_st_16x64b_x16:
2857 case Intrinsic::nvvm_tcgen05_st_16x64b_x32:
2858 case Intrinsic::nvvm_tcgen05_st_16x64b_x128:
2859 case Intrinsic::nvvm_tcgen05_st_16x128b_x1:
2860 case Intrinsic::nvvm_tcgen05_st_16x128b_x2:
2861 case Intrinsic::nvvm_tcgen05_st_16x128b_x4:
2862 case Intrinsic::nvvm_tcgen05_st_16x128b_x8:
2863 case Intrinsic::nvvm_tcgen05_st_16x128b_x16:
2864 case Intrinsic::nvvm_tcgen05_st_16x128b_x32:
2865 case Intrinsic::nvvm_tcgen05_st_16x128b_x64:
2866 case Intrinsic::nvvm_tcgen05_st_16x256b_x1:
2867 case Intrinsic::nvvm_tcgen05_st_16x256b_x2:
2868 case Intrinsic::nvvm_tcgen05_st_16x256b_x4:
2869 case Intrinsic::nvvm_tcgen05_st_16x256b_x8:
2870 case Intrinsic::nvvm_tcgen05_st_16x256b_x16:
2871 case Intrinsic::nvvm_tcgen05_st_16x256b_x32:
2872 case Intrinsic::nvvm_tcgen05_st_32x32b_x1:
2873 case Intrinsic::nvvm_tcgen05_st_32x32b_x2:
2874 case Intrinsic::nvvm_tcgen05_st_32x32b_x4:
2875 case Intrinsic::nvvm_tcgen05_st_32x32b_x8:
2876 case Intrinsic::nvvm_tcgen05_st_32x32b_x16:
2877 case Intrinsic::nvvm_tcgen05_st_32x32b_x32:
2878 case Intrinsic::nvvm_tcgen05_st_16x64b_x64:
2879 case Intrinsic::nvvm_tcgen05_st_32x32b_x64:
2880 case Intrinsic::nvvm_tcgen05_st_32x32b_x128:
2881 return lowerTcgen05St(Op, DAG);
2882 case Intrinsic::nvvm_tcgen05_st_16x32bx2_x1:
2883 case Intrinsic::nvvm_tcgen05_st_16x32bx2_x2:
2884 case Intrinsic::nvvm_tcgen05_st_16x32bx2_x4:
2885 case Intrinsic::nvvm_tcgen05_st_16x32bx2_x8:
2886 case Intrinsic::nvvm_tcgen05_st_16x32bx2_x16:
2887 case Intrinsic::nvvm_tcgen05_st_16x32bx2_x32:
2888 case Intrinsic::nvvm_tcgen05_st_16x32bx2_x64:
2889 case Intrinsic::nvvm_tcgen05_st_16x32bx2_x128:
2890 return lowerTcgen05St(Op, DAG, /* hasOffset */ true);
2891 case Intrinsic::nvvm_tcgen05_mma_shared_disable_output_lane_cg1:
2892 case Intrinsic::nvvm_tcgen05_mma_shared_disable_output_lane_cg2:
2893 case Intrinsic::nvvm_tcgen05_mma_shared_scale_d_disable_output_lane_cg1:
2894 case Intrinsic::nvvm_tcgen05_mma_shared_scale_d_disable_output_lane_cg2:
2895 case Intrinsic::nvvm_tcgen05_mma_sp_shared_disable_output_lane_cg1:
2896 case Intrinsic::nvvm_tcgen05_mma_sp_shared_disable_output_lane_cg2:
2897 case Intrinsic::nvvm_tcgen05_mma_sp_shared_scale_d_disable_output_lane_cg1:
2898 case Intrinsic::nvvm_tcgen05_mma_sp_shared_scale_d_disable_output_lane_cg2:
2899 case Intrinsic::nvvm_tcgen05_mma_tensor_disable_output_lane_cg1:
2900 case Intrinsic::nvvm_tcgen05_mma_tensor_disable_output_lane_cg2:
2901 case Intrinsic::nvvm_tcgen05_mma_tensor_scale_d_disable_output_lane_cg1:
2902 case Intrinsic::nvvm_tcgen05_mma_tensor_scale_d_disable_output_lane_cg2:
2903 case Intrinsic::nvvm_tcgen05_mma_sp_tensor_disable_output_lane_cg1:
2904 case Intrinsic::nvvm_tcgen05_mma_sp_tensor_disable_output_lane_cg2:
2905 case Intrinsic::nvvm_tcgen05_mma_sp_tensor_scale_d_disable_output_lane_cg1:
2906 case Intrinsic::nvvm_tcgen05_mma_sp_tensor_scale_d_disable_output_lane_cg2:
2907 case Intrinsic::nvvm_tcgen05_mma_tensor_disable_output_lane_cg1_ashift:
2908 case Intrinsic::nvvm_tcgen05_mma_tensor_disable_output_lane_cg2_ashift:
2909 case Intrinsic::
2910 nvvm_tcgen05_mma_tensor_scale_d_disable_output_lane_cg1_ashift:
2911 case Intrinsic::
2912 nvvm_tcgen05_mma_tensor_scale_d_disable_output_lane_cg2_ashift:
2913 case Intrinsic::nvvm_tcgen05_mma_sp_tensor_disable_output_lane_cg1_ashift:
2914 case Intrinsic::nvvm_tcgen05_mma_sp_tensor_disable_output_lane_cg2_ashift:
2915 case Intrinsic::
2916 nvvm_tcgen05_mma_sp_tensor_scale_d_disable_output_lane_cg1_ashift:
2917 case Intrinsic::
2918 nvvm_tcgen05_mma_sp_tensor_scale_d_disable_output_lane_cg2_ashift:
2919 case Intrinsic::
2920 nvvm_tcgen05_mma_shared_f8f6f4_disable_output_lane_cg1_decompress_b:
2921 case Intrinsic::
2922 nvvm_tcgen05_mma_shared_f8f6f4_disable_output_lane_cg2_decompress_b:
2923 case Intrinsic::
2924 nvvm_tcgen05_mma_tensor_f8f6f4_disable_output_lane_cg1_decompress_b:
2925 case Intrinsic::
2926 nvvm_tcgen05_mma_tensor_f8f6f4_disable_output_lane_cg2_decompress_b:
2928 case Intrinsic::nvvm_tensormap_replace_elemtype:
2929 return lowerTensormapReplaceElemtype(Op, DAG);
2930 case Intrinsic::nvvm_tensormap_replace_swizzle_mode:
2932 }
2933 return Op;
2934}
2935
2937 SelectionDAG &DAG) {
2938
2939 SDNode *N = Op.getNode();
2940 if (N->getOperand(1).getValueType() != MVT::i128) {
2941 // return, if the operand is already lowered
2942 return SDValue();
2943 }
2944
2945 unsigned IID =
2946 cast<ConstantSDNode>(N->getOperand(0).getNode())->getZExtValue();
2947 auto Opcode = [&]() {
2948 switch (IID) {
2949 case Intrinsic::nvvm_clusterlaunchcontrol_query_cancel_is_canceled:
2950 return NVPTXISD::CLUSTERLAUNCHCONTROL_QUERY_CANCEL_IS_CANCELED;
2951 case Intrinsic::nvvm_clusterlaunchcontrol_query_cancel_get_first_ctaid_x:
2952 return NVPTXISD::CLUSTERLAUNCHCONTROL_QUERY_CANCEL_GET_FIRST_CTAID_X;
2953 case Intrinsic::nvvm_clusterlaunchcontrol_query_cancel_get_first_ctaid_y:
2954 return NVPTXISD::CLUSTERLAUNCHCONTROL_QUERY_CANCEL_GET_FIRST_CTAID_Y;
2955 case Intrinsic::nvvm_clusterlaunchcontrol_query_cancel_get_first_ctaid_z:
2956 return NVPTXISD::CLUSTERLAUNCHCONTROL_QUERY_CANCEL_GET_FIRST_CTAID_Z;
2957 default:
2958 llvm_unreachable("unsupported/unhandled intrinsic");
2959 }
2960 }();
2961
2962 SDLoc DL(N);
2963 SDValue TryCancelResponse = N->getOperand(1);
2964 SDValue Cast = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, TryCancelResponse);
2965 SDValue TryCancelResponse0 =
2966 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i64, Cast,
2967 DAG.getIntPtrConstant(0, DL));
2968 SDValue TryCancelResponse1 =
2969 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i64, Cast,
2970 DAG.getIntPtrConstant(1, DL));
2971
2972 return DAG.getNode(Opcode, DL, N->getVTList(),
2973 {TryCancelResponse0, TryCancelResponse1});
2974}
2975
2977 SDNode *N = Op.getNode();
2978 SDLoc DL(N);
2979 SDValue F32Vec = N->getOperand(1);
2980 SDValue RBits = N->getOperand(2);
2981
2982 unsigned IntrinsicID = N->getConstantOperandVal(0);
2983
2984 // Extract the 4 float elements from the vector
2986 for (unsigned i = 0; i < 4; ++i)
2987 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, F32Vec,
2988 DAG.getIntPtrConstant(i, DL)));
2989
2991
2992 auto [OpCode, RetTy, CvtModeFlag] =
2993 [&]() -> std::tuple<unsigned, MVT::SimpleValueType, uint32_t> {
2994 switch (IntrinsicID) {
2995 case Intrinsic::nvvm_f32x4_to_e4m3x4_rs_relu_satfinite:
2996 return {NVPTXISD::CVT_E4M3X4_F32X4_RS_SF, MVT::v4i8,
2997 CvtMode::RS | CvtMode::RELU_FLAG};
2998 case Intrinsic::nvvm_f32x4_to_e4m3x4_rs_satfinite:
2999 return {NVPTXISD::CVT_E4M3X4_F32X4_RS_SF, MVT::v4i8, CvtMode::RS};
3000 case Intrinsic::nvvm_f32x4_to_e5m2x4_rs_relu_satfinite:
3001 return {NVPTXISD::CVT_E5M2X4_F32X4_RS_SF, MVT::v4i8,
3002 CvtMode::RS | CvtMode::RELU_FLAG};
3003 case Intrinsic::nvvm_f32x4_to_e5m2x4_rs_satfinite:
3004 return {NVPTXISD::CVT_E5M2X4_F32X4_RS_SF, MVT::v4i8, CvtMode::RS};
3005 case Intrinsic::nvvm_f32x4_to_e2m3x4_rs_relu_satfinite:
3006 return {NVPTXISD::CVT_E2M3X4_F32X4_RS_SF, MVT::v4i8,
3007 CvtMode::RS | CvtMode::RELU_FLAG};
3008 case Intrinsic::nvvm_f32x4_to_e2m3x4_rs_satfinite:
3009 return {NVPTXISD::CVT_E2M3X4_F32X4_RS_SF, MVT::v4i8, CvtMode::RS};
3010 case Intrinsic::nvvm_f32x4_to_e3m2x4_rs_relu_satfinite:
3011 return {NVPTXISD::CVT_E3M2X4_F32X4_RS_SF, MVT::v4i8,
3012 CvtMode::RS | CvtMode::RELU_FLAG};
3013 case Intrinsic::nvvm_f32x4_to_e3m2x4_rs_satfinite:
3014 return {NVPTXISD::CVT_E3M2X4_F32X4_RS_SF, MVT::v4i8, CvtMode::RS};
3015 case Intrinsic::nvvm_f32x4_to_e2m1x4_rs_relu_satfinite:
3016 return {NVPTXISD::CVT_E2M1X4_F32X4_RS_SF, MVT::i16,
3017 CvtMode::RS | CvtMode::RELU_FLAG};
3018 case Intrinsic::nvvm_f32x4_to_e2m1x4_rs_satfinite:
3019 return {NVPTXISD::CVT_E2M1X4_F32X4_RS_SF, MVT::i16, CvtMode::RS};
3020 default:
3021 llvm_unreachable("unsupported/unhandled intrinsic");
3022 }
3023 }();
3024
3025 Ops.push_back(RBits);
3026 Ops.push_back(DAG.getConstant(CvtModeFlag, DL, MVT::i32));
3027
3028 return DAG.getNode(OpCode, DL, RetTy, Ops);
3029}
3030
3032 const unsigned Mode = [&]() {
3033 switch (Op->getConstantOperandVal(0)) {
3034 case Intrinsic::nvvm_prmt:
3036 case Intrinsic::nvvm_prmt_b4e:
3038 case Intrinsic::nvvm_prmt_ecl:
3040 case Intrinsic::nvvm_prmt_ecr:
3042 case Intrinsic::nvvm_prmt_f4e:
3044 case Intrinsic::nvvm_prmt_rc16:
3046 case Intrinsic::nvvm_prmt_rc8:
3048 default:
3049 llvm_unreachable("unsupported/unhandled intrinsic");
3050 }
3051 }();
3052 SDLoc DL(Op);
3053 SDValue A = Op->getOperand(1);
3054 SDValue B = Op.getNumOperands() == 4 ? Op.getOperand(2)
3055 : DAG.getConstant(0, DL, MVT::i32);
3056 SDValue Selector = (Op->op_end() - 1)->get();
3057 return getPRMT(A, B, Selector, DL, DAG, Mode);
3058}
3059
3060#define TCGEN05_LD_RED_INTR(SHAPE, NUM, TYPE) \
3061 Intrinsic::nvvm_tcgen05_ld_red_##SHAPE##_x##NUM##_##TYPE
3062
3063#define TCGEN05_LD_RED_INST(SHAPE, NUM, TYPE) \
3064 NVPTXISD::TCGEN05_LD_RED_##SHAPE##_X##NUM##_##TYPE
3065
3066static unsigned getTcgen05LdRedID(Intrinsic::ID IID) {
3067 switch (IID) {
3068 case TCGEN05_LD_RED_INTR(32x32b, 2, f32):
3069 return TCGEN05_LD_RED_INST(32x32b, 2, F32);
3070 case TCGEN05_LD_RED_INTR(32x32b, 4, f32):
3071 return TCGEN05_LD_RED_INST(32x32b, 4, F32);
3072 case TCGEN05_LD_RED_INTR(32x32b, 8, f32):
3073 return TCGEN05_LD_RED_INST(32x32b, 8, F32);
3074 case TCGEN05_LD_RED_INTR(32x32b, 16, f32):
3075 return TCGEN05_LD_RED_INST(32x32b, 16, F32);
3076 case TCGEN05_LD_RED_INTR(32x32b, 32, f32):
3077 return TCGEN05_LD_RED_INST(32x32b, 32, F32);
3078 case TCGEN05_LD_RED_INTR(32x32b, 64, f32):
3079 return TCGEN05_LD_RED_INST(32x32b, 64, F32);
3080 case TCGEN05_LD_RED_INTR(32x32b, 128, f32):
3081 return TCGEN05_LD_RED_INST(32x32b, 128, F32);
3082 case TCGEN05_LD_RED_INTR(16x32bx2, 2, f32):
3083 return TCGEN05_LD_RED_INST(16x32bx2, 2, F32);
3084 case TCGEN05_LD_RED_INTR(16x32bx2, 4, f32):
3085 return TCGEN05_LD_RED_INST(16x32bx2, 4, F32);
3086 case TCGEN05_LD_RED_INTR(16x32bx2, 8, f32):
3087 return TCGEN05_LD_RED_INST(16x32bx2, 8, F32);
3088 case TCGEN05_LD_RED_INTR(16x32bx2, 16, f32):
3089 return TCGEN05_LD_RED_INST(16x32bx2, 16, F32);
3090 case TCGEN05_LD_RED_INTR(16x32bx2, 32, f32):
3091 return TCGEN05_LD_RED_INST(16x32bx2, 32, F32);
3092 case TCGEN05_LD_RED_INTR(16x32bx2, 64, f32):
3093 return TCGEN05_LD_RED_INST(16x32bx2, 64, F32);
3094 case TCGEN05_LD_RED_INTR(16x32bx2, 128, f32):
3095 return TCGEN05_LD_RED_INST(16x32bx2, 128, F32);
3096 case TCGEN05_LD_RED_INTR(32x32b, 2, i32):
3097 return TCGEN05_LD_RED_INST(32x32b, 2, I32);
3098 case TCGEN05_LD_RED_INTR(32x32b, 4, i32):
3099 return TCGEN05_LD_RED_INST(32x32b, 4, I32);
3100 case TCGEN05_LD_RED_INTR(32x32b, 8, i32):
3101 return TCGEN05_LD_RED_INST(32x32b, 8, I32);
3102 case TCGEN05_LD_RED_INTR(32x32b, 16, i32):
3103 return TCGEN05_LD_RED_INST(32x32b, 16, I32);
3104 case TCGEN05_LD_RED_INTR(32x32b, 32, i32):
3105 return TCGEN05_LD_RED_INST(32x32b, 32, I32);
3106 case TCGEN05_LD_RED_INTR(32x32b, 64, i32):
3107 return TCGEN05_LD_RED_INST(32x32b, 64, I32);
3108 case TCGEN05_LD_RED_INTR(32x32b, 128, i32):
3109 return TCGEN05_LD_RED_INST(32x32b, 128, I32);
3110 case TCGEN05_LD_RED_INTR(16x32bx2, 2, i32):
3111 return TCGEN05_LD_RED_INST(16x32bx2, 2, I32);
3112 case TCGEN05_LD_RED_INTR(16x32bx2, 4, i32):
3113 return TCGEN05_LD_RED_INST(16x32bx2, 4, I32);
3114 case TCGEN05_LD_RED_INTR(16x32bx2, 8, i32):
3115 return TCGEN05_LD_RED_INST(16x32bx2, 8, I32);
3116 case TCGEN05_LD_RED_INTR(16x32bx2, 16, i32):
3117 return TCGEN05_LD_RED_INST(16x32bx2, 16, I32);
3118 case TCGEN05_LD_RED_INTR(16x32bx2, 32, i32):
3119 return TCGEN05_LD_RED_INST(16x32bx2, 32, I32);
3120 case TCGEN05_LD_RED_INTR(16x32bx2, 64, i32):
3121 return TCGEN05_LD_RED_INST(16x32bx2, 64, I32);
3122 case TCGEN05_LD_RED_INTR(16x32bx2, 128, i32):
3123 return TCGEN05_LD_RED_INST(16x32bx2, 128, I32);
3124 default:
3125 llvm_unreachable("Invalid tcgen05.ld.red intrinsic ID");
3126 }
3127}
3128
3129// Lower vector return type of tcgen05.ld intrinsics
3130static std::optional<std::tuple<SDValue, SDValue, SDValue>>
3132 SDLoc DL(N);
3133 EVT ResVT = N->getValueType(0);
3134 if (!ResVT.isVector())
3135 return {}; // already legalized.
3136
3137 const unsigned NumElts = ResVT.getVectorNumElements();
3138
3139 // Create the return type of the instructions
3140 // +1 represents the reduction value
3141 SmallVector<EVT, 132> ListVTs{
3142 NumElts + 1,
3143 ResVT.getVectorElementType().isFloatingPoint() ? MVT::f32 : MVT::i32};
3144
3145 ListVTs.push_back(MVT::Other); // Chain
3146
3147 SDVTList ResVTs = DAG.getVTList(ListVTs);
3148
3149 // Prepare the Operands
3150 SmallVector<SDValue, 8> Ops{N->getOperand(0)}; // Chain
3151
3152 // skip IID at index 1
3153 for (unsigned i = 2; i < N->getNumOperands(); i++)
3154 Ops.push_back(N->getOperand(i));
3155
3156 unsigned IID = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
3158 SDValue NewNode =
3159 DAG.getMemIntrinsicNode(getTcgen05LdRedID(IID), DL, ResVTs, Ops,
3160 MemSD->getMemoryVT(), MemSD->getMemOperand());
3161
3162 // Split vector result
3163 SmallVector<SDValue, 132> ScalarRes;
3164 for (unsigned i = 0; i < NumElts; ++i) {
3165 SDValue Res = NewNode.getValue(i);
3166 ScalarRes.push_back(Res);
3167 }
3168
3169 SDValue BuildVector = DAG.getNode(ISD::BUILD_VECTOR, DL, ResVT, ScalarRes);
3170 SDValue RedResult = NewNode.getValue(NumElts);
3171 SDValue Chain = NewNode.getValue(NumElts + 1);
3172 return {{BuildVector, RedResult, Chain}};
3173}
3174
3176 switch (Op->getConstantOperandVal(1)) {
3177 default:
3178 return Op;
3179
3180 // These tcgen05 intrinsics return a v2i32, which is legal, so we have to
3181 // lower them through LowerOperation() instead of ReplaceNodeResults().
3182 case Intrinsic::nvvm_tcgen05_ld_16x64b_x2:
3183 case Intrinsic::nvvm_tcgen05_ld_16x128b_x1:
3184 case Intrinsic::nvvm_tcgen05_ld_32x32b_x2:
3185 if (auto Res = lowerTcgen05Ld(Op.getNode(), DAG))
3186 return DAG.getMergeValues({Res->first, Res->second}, SDLoc(Op));
3187 return SDValue();
3188
3189 case Intrinsic::nvvm_tcgen05_ld_16x32bx2_x2:
3190 if (auto Res = lowerTcgen05Ld(Op.getNode(), DAG, /*HasOffset=*/true))
3191 return DAG.getMergeValues({Res->first, Res->second}, SDLoc(Op));
3192 return SDValue();
3193
3194 case Intrinsic::nvvm_tcgen05_ld_red_32x32b_x2_f32:
3195 case Intrinsic::nvvm_tcgen05_ld_red_32x32b_x2_i32:
3196 case Intrinsic::nvvm_tcgen05_ld_red_16x32bx2_x2_f32:
3197 case Intrinsic::nvvm_tcgen05_ld_red_16x32bx2_x2_i32:
3198 if (auto Res = lowerTcgen05LdRed(Op.getNode(), DAG))
3199 return DAG.getMergeValues(
3200 {std::get<0>(*Res), std::get<1>(*Res), std::get<2>(*Res)}, SDLoc(Op));
3201 return SDValue();
3202 }
3203}
3204
3206 switch (Op->getConstantOperandVal(0)) {
3207 default:
3208 return Op;
3209 case Intrinsic::nvvm_prmt:
3210 case Intrinsic::nvvm_prmt_b4e:
3211 case Intrinsic::nvvm_prmt_ecl:
3212 case Intrinsic::nvvm_prmt_ecr:
3213 case Intrinsic::nvvm_prmt_f4e:
3214 case Intrinsic::nvvm_prmt_rc16:
3215 case Intrinsic::nvvm_prmt_rc8:
3216 return lowerPrmtIntrinsic(Op, DAG);
3217 case Intrinsic::nvvm_clusterlaunchcontrol_query_cancel_is_canceled:
3218 case Intrinsic::nvvm_clusterlaunchcontrol_query_cancel_get_first_ctaid_x:
3219 case Intrinsic::nvvm_clusterlaunchcontrol_query_cancel_get_first_ctaid_y:
3220 case Intrinsic::nvvm_clusterlaunchcontrol_query_cancel_get_first_ctaid_z:
3222 case Intrinsic::nvvm_f32x4_to_e4m3x4_rs_satfinite:
3223 case Intrinsic::nvvm_f32x4_to_e4m3x4_rs_relu_satfinite:
3224 case Intrinsic::nvvm_f32x4_to_e5m2x4_rs_satfinite:
3225 case Intrinsic::nvvm_f32x4_to_e5m2x4_rs_relu_satfinite:
3226 case Intrinsic::nvvm_f32x4_to_e2m3x4_rs_satfinite:
3227 case Intrinsic::nvvm_f32x4_to_e2m3x4_rs_relu_satfinite:
3228 case Intrinsic::nvvm_f32x4_to_e3m2x4_rs_satfinite:
3229 case Intrinsic::nvvm_f32x4_to_e3m2x4_rs_relu_satfinite:
3230 case Intrinsic::nvvm_f32x4_to_e2m1x4_rs_satfinite:
3231 case Intrinsic::nvvm_f32x4_to_e2m1x4_rs_relu_satfinite:
3232 return lowerCvtRSIntrinsics(Op, DAG);
3233 }
3234}
3235
3236// In PTX 64-bit CTLZ and CTPOP are supported, but they return a 32-bit value.
3237// Lower these into a node returning the correct type which is zero-extended
3238// back to the correct size.
3240 SDValue V = Op->getOperand(0);
3241 assert(V.getValueType() == MVT::i64 &&
3242 "Unexpected CTLZ/CTPOP type to legalize");
3243
3244 SDLoc DL(Op);
3245 SDValue CT = DAG.getNode(Op->getOpcode(), DL, MVT::i32, V);
3246 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, CT, SDNodeFlags::NonNeg);
3247}
3248
3250 unsigned Opcode, SelectionDAG &DAG) {
3251 assert(A.getValueType() == MVT::i64 && B.getValueType() == MVT::i64);
3252
3253 const auto *AmtConst = dyn_cast<ConstantSDNode>(ShiftAmount);
3254 if (!AmtConst)
3255 return SDValue();
3256 const auto Amt = AmtConst->getZExtValue() & 63;
3257
3258 SDValue UnpackA =
3259 DAG.getNode(NVPTXISD::UNPACK_VECTOR, DL, {MVT::i32, MVT::i32}, A);
3260 SDValue UnpackB =
3261 DAG.getNode(NVPTXISD::UNPACK_VECTOR, DL, {MVT::i32, MVT::i32}, B);
3262
3263 // Arch is Little endiain: 0 = low bits, 1 = high bits
3264 SDValue ALo = UnpackA.getValue(0);
3265 SDValue AHi = UnpackA.getValue(1);
3266 SDValue BLo = UnpackB.getValue(0);
3267 SDValue BHi = UnpackB.getValue(1);
3268
3269 // The bitfeild consists of { AHi : ALo : BHi : BLo }
3270 //
3271 // * FSHL, Amt < 32 - The window will contain { AHi : ALo : BHi }
3272 // * FSHL, Amt >= 32 - The window will contain { ALo : BHi : BLo }
3273 // * FSHR, Amt < 32 - The window will contain { ALo : BHi : BLo }
3274 // * FSHR, Amt >= 32 - The window will contain { AHi : ALo : BHi }
3275 //
3276 // Note that Amt = 0 and Amt = 32 are special cases where 32-bit funnel shifts
3277 // are not needed at all. Amt = 0 is a no-op producing either A or B depending
3278 // on the direction. Amt = 32 can be implemented by a packing and unpacking
3279 // move to select and arrange the 32bit values. For simplicity, these cases
3280 // are not handled here explicitly and instead we rely on DAGCombiner to
3281 // remove the no-op funnel shifts we insert.
3282 auto [High, Mid, Low] = ((Opcode == ISD::FSHL) == (Amt < 32))
3283 ? std::make_tuple(AHi, ALo, BHi)
3284 : std::make_tuple(ALo, BHi, BLo);
3285
3286 SDValue NewAmt = DAG.getConstant(Amt & 31, DL, MVT::i32);
3287 SDValue RHi = DAG.getNode(Opcode, DL, MVT::i32, {High, Mid, NewAmt});
3288 SDValue RLo = DAG.getNode(Opcode, DL, MVT::i32, {Mid, Low, NewAmt});
3289
3290 return DAG.getNode(NVPTXISD::BUILD_VECTOR, DL, MVT::i64, {RLo, RHi});
3291}
3292
3294 return expandFSH64(Op->getOperand(0), Op->getOperand(1), Op->getOperand(2),
3295 SDLoc(Op), Op->getOpcode(), DAG);
3296}
3297
3299 unsigned Opcode = Op->getOpcode() == ISD::ROTL ? ISD::FSHL : ISD::FSHR;
3300 return expandFSH64(Op->getOperand(0), Op->getOperand(0), Op->getOperand(1),
3301 SDLoc(Op), Opcode, DAG);
3302}
3303
3305 // Lower (frem x, y) into (sub x, (mul (ftrunc (div x, y)) y)),
3306 // i.e. "poor man's fmod()". When y is infinite, x is returned. This matches
3307 // the semantics of LLVM's frem.
3308 SDLoc DL(Op);
3309 SDValue X = Op->getOperand(0);
3310 SDValue Y = Op->getOperand(1);
3311 EVT Ty = Op.getValueType();
3312 SDNodeFlags Flags = Op->getFlags();
3313
3314 SDValue Div = DAG.getNode(ISD::FDIV, DL, Ty, X, Y, Flags);
3315 SDValue Trunc = DAG.getNode(ISD::FTRUNC, DL, Ty, Div, Flags);
3316 SDValue Mul = DAG.getNode(ISD::FMUL, DL, Ty, Trunc, Y,
3318 SDValue Sub = DAG.getNode(ISD::FSUB, DL, Ty, X, Mul,
3320
3321 if (Flags.hasNoInfs())
3322 return Sub;
3323
3324 // If Y is infinite, return X
3325 SDValue AbsY = DAG.getNode(ISD::FABS, DL, Ty, Y);
3326 SDValue Inf =
3327 DAG.getConstantFP(APFloat::getInf(Ty.getFltSemantics()), DL, Ty);
3328 SDValue IsInf = DAG.getSetCC(DL, MVT::i1, AbsY, Inf, ISD::SETEQ);
3329 return DAG.getSelect(DL, Ty, IsInf, X, Sub);
3330}
3331
3333 assert(Op.getValueType() == MVT::i1 && "Custom lowering enabled only for i1");
3334
3335 SDValue Cond = Op->getOperand(0);
3336 SDValue TrueVal = Op->getOperand(1);
3337 SDValue FalseVal = Op->getOperand(2);
3338 SDLoc DL(Op);
3339
3340 // If both operands are truncated, we push the select through the truncates.
3341 if (TrueVal.getOpcode() == ISD::TRUNCATE &&
3342 FalseVal.getOpcode() == ISD::TRUNCATE) {
3343 TrueVal = TrueVal.getOperand(0);
3344 FalseVal = FalseVal.getOperand(0);
3345
3346 EVT VT = TrueVal.getSimpleValueType().bitsLE(FalseVal.getSimpleValueType())
3347 ? TrueVal.getValueType()
3348 : FalseVal.getValueType();
3349 TrueVal = DAG.getAnyExtOrTrunc(TrueVal, DL, VT);
3350 FalseVal = DAG.getAnyExtOrTrunc(FalseVal, DL, VT);
3351 SDValue Select = DAG.getSelect(DL, VT, Cond, TrueVal, FalseVal);
3352 return DAG.getNode(ISD::TRUNCATE, DL, MVT::i1, Select);
3353 }
3354
3355 // Otherwise, expand the select into a series of logical operations. These
3356 // often can be folded into other operations either by us or ptxas.
3357 TrueVal = DAG.getFreeze(TrueVal);
3358 FalseVal = DAG.getFreeze(FalseVal);
3359 SDValue And1 = DAG.getNode(ISD::AND, DL, MVT::i1, Cond, TrueVal);
3360 SDValue NotCond = DAG.getNOT(DL, Cond, MVT::i1);
3361 SDValue And2 = DAG.getNode(ISD::AND, DL, MVT::i1, NotCond, FalseVal);
3362 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i1, And1, And2);
3363 return Or;
3364}
3365
3367 SDNode *N = Op.getNode();
3368
3369 SDValue Chain = N->getOperand(0);
3370 SDValue Val = N->getOperand(1);
3371 SDValue BasePtr = N->getOperand(2);
3372 SDValue Offset = N->getOperand(3);
3373 SDValue Mask = N->getOperand(4);
3374
3375 SDLoc DL(N);
3376 EVT ValVT = Val.getValueType();
3377 MemSDNode *MemSD = cast<MemSDNode>(N);
3378 assert(ValVT.isVector() && "Masked vector store must have vector type");
3379 assert(MemSD->getAlign() >= DAG.getEVTAlign(ValVT) &&
3380 "Unexpected alignment for masked store");
3381
3382 unsigned Opcode = 0;
3383 switch (ValVT.getSimpleVT().SimpleTy) {
3384 default:
3385 llvm_unreachable("Unexpected masked vector store type");
3386 case MVT::v4i64:
3387 case MVT::v4f64: {
3388 Opcode = NVPTXISD::StoreV4;
3389 break;
3390 }
3391 case MVT::v8i32:
3392 case MVT::v8f32: {
3393 Opcode = NVPTXISD::StoreV8;
3394 break;
3395 }
3396 }
3397
3399
3400 // Construct the new SDNode. First operand is the chain.
3401 Ops.push_back(Chain);
3402
3403 // The next N operands are the values to store. Encode the mask into the
3404 // values using the sentinel register 0 to represent a masked-off element.
3405 assert(Mask.getValueType().isVector() &&
3406 Mask.getValueType().getVectorElementType() == MVT::i1 &&
3407 "Mask must be a vector of i1");
3408 assert(Mask.getOpcode() == ISD::BUILD_VECTOR &&
3409 "Mask expected to be a BUILD_VECTOR");
3410 assert(Mask.getValueType().getVectorNumElements() ==
3411 ValVT.getVectorNumElements() &&
3412 "Mask size must be the same as the vector size");
3413 for (auto [I, Op] : enumerate(Mask->ops())) {
3414 // Mask elements must be constants.
3415 if (Op.getNode()->getAsZExtVal() == 0) {
3416 // Append a sentinel register 0 to the Ops vector to represent a masked
3417 // off element, this will be handled in tablegen
3419 ValVT.getVectorElementType()));
3420 } else {
3421 // Extract the element from the vector to store
3422 SDValue ExtVal =
3424 Val, DAG.getIntPtrConstant(I, DL));
3425 Ops.push_back(ExtVal);
3426 }
3427 }
3428
3429 // Next, the pointer operand.
3430 Ops.push_back(BasePtr);
3431
3432 // Finally, the offset operand. We expect this to always be undef, and it will
3433 // be ignored in lowering, but to mirror the handling of the other vector
3434 // store instructions we include it in the new SDNode.
3435 assert(Offset.isUndef() && "Offset operand expected to be undef or poison");
3436 Ops.push_back(Offset);
3437
3438 SDValue NewSt =
3439 DAG.getMemIntrinsicNode(Opcode, DL, DAG.getVTList(MVT::Other), Ops,
3440 MemSD->getMemoryVT(), MemSD->getMemOperand());
3441
3442 return NewSt;
3443}
3444
3445SDValue
3447 switch (Op.getOpcode()) {
3448 case ISD::RETURNADDR:
3449 return SDValue();
3450 case ISD::FRAMEADDR:
3451 return SDValue();
3452 case ISD::ADDRSPACECAST:
3453 return LowerADDRSPACECAST(Op, DAG);
3455 return lowerIntrinsicWChain(Op, DAG);
3457 return lowerIntrinsicWOChain(Op, DAG);
3459 return lowerIntrinsicVoid(Op, DAG);
3460 case ISD::BUILD_VECTOR:
3461 return LowerBUILD_VECTOR(Op, DAG);
3462 case ISD::BITCAST:
3463 return LowerBITCAST(Op, DAG);
3465 return Op;
3467 return LowerEXTRACT_VECTOR_ELT(Op, DAG);
3469 return LowerINSERT_VECTOR_ELT(Op, DAG);
3471 return LowerVECTOR_SHUFFLE(Op, DAG);
3473 return LowerCONCAT_VECTORS(Op, DAG);
3478 return LowerVECREDUCE(Op, DAG);
3479 case ISD::STORE:
3480 return LowerSTORE(Op, DAG);
3481 case ISD::MSTORE: {
3482 assert(STI.has256BitVectorLoadStore(
3483 cast<MemSDNode>(Op.getNode())->getAddressSpace()) &&
3484 "Masked store vector not supported on subtarget.");
3485 return lowerMSTORE(Op, DAG);
3486 }
3487 case ISD::LOAD:
3488 return LowerLOAD(Op, DAG);
3489 case ISD::MLOAD:
3490 return LowerMLOAD(Op, DAG);
3491 case ISD::SHL_PARTS:
3492 return LowerShiftLeftParts(Op, DAG);
3493 case ISD::SRA_PARTS:
3494 case ISD::SRL_PARTS:
3495 return LowerShiftRightParts(Op, DAG);
3496 case ISD::SELECT:
3497 return lowerSELECT(Op, DAG);
3498 case ISD::FROUND:
3499 return LowerFROUND(Op, DAG);
3500 case ISD::FCOPYSIGN:
3501 return LowerFCOPYSIGN(Op, DAG);
3502 case ISD::SINT_TO_FP:
3503 case ISD::UINT_TO_FP:
3504 return LowerINT_TO_FP(Op, DAG);
3505 case ISD::FP_TO_SINT:
3506 case ISD::FP_TO_UINT:
3507 // fptosi/fptoui to i1 truncate toward zero, so the only defined results
3508 // are {0,-1} (signed) and {0,1} (unsigned); every other input results in
3509 // poison. Thus we can simply lower to `x <= -1.0` or `x >= 1.0`.
3510 if (Op.getValueType() == MVT::i1) {
3511 SDLoc DL(Op);
3512 SDValue X = Op.getOperand(0);
3513 bool IsSigned = Op.getOpcode() == ISD::FP_TO_SINT;
3514 return DAG.getSetCC(
3515 DL, MVT::i1, X,
3516 DAG.getConstantFP(IsSigned ? -1.0 : 1.0, DL, X.getValueType()),
3517 IsSigned ? ISD::SETOLE : ISD::SETOGE);
3518 }
3519 return LowerFP_TO_INT(Op, DAG);
3520 case ISD::FP_ROUND:
3521 return LowerFP_ROUND(Op, DAG);
3522 case ISD::FP_EXTEND:
3523 return LowerFP_EXTEND(Op, DAG);
3524 case ISD::VAARG:
3525 return LowerVAARG(Op, DAG);
3526 case ISD::VASTART:
3527 return LowerVASTART(Op, DAG);
3528 case ISD::FSHL:
3529 case ISD::FSHR:
3530 return lowerFSH(Op, DAG);
3531 case ISD::ROTL:
3532 case ISD::ROTR:
3533 return lowerROT(Op, DAG);
3534 case ISD::ABS:
3536 case ISD::SMIN:
3537 case ISD::SMAX:
3538 case ISD::UMIN:
3539 case ISD::UMAX:
3540 case ISD::ADD:
3541 case ISD::SUB:
3542 case ISD::MUL:
3543 case ISD::SHL:
3544 case ISD::SREM:
3545 case ISD::UREM:
3546 return LowerVectorArith(Op, DAG);
3548 return LowerDYNAMIC_STACKALLOC(Op, DAG);
3549 case ISD::STACKRESTORE:
3550 return LowerSTACKRESTORE(Op, DAG);
3551 case ISD::STACKSAVE:
3552 return LowerSTACKSAVE(Op, DAG);
3553 case ISD::CopyToReg:
3554 return LowerCopyToReg_128(Op, DAG);
3555 case ISD::FADD:
3556 case ISD::FSUB:
3557 case ISD::FMUL:
3558 // Used only for bf16 on SM80, where we select fma for non-ftz operation
3559 return PromoteBinOpIfF32FTZ(Op, DAG);
3560 case ISD::CTPOP:
3561 case ISD::CTLZ:
3562 return lowerCTLZCTPOP(Op, DAG);
3563 case ISD::FREM:
3564 return lowerFREM(Op, DAG);
3565 case ISD::BSWAP:
3566 return lowerBSWAP(Op, DAG);
3567 default:
3568 llvm_unreachable("Custom lowering not defined for operation");
3569 }
3570}
3571
3572// This will prevent AsmPrinter from trying to print the jump tables itself.
3576
3577SDValue NVPTXTargetLowering::LowerADDRSPACECAST(SDValue Op,
3578 SelectionDAG &DAG) const {
3580 unsigned SrcAS = N->getSrcAddressSpace();
3581 unsigned DestAS = N->getDestAddressSpace();
3582 if (SrcAS != llvm::ADDRESS_SPACE_GENERIC &&
3583 DestAS != llvm::ADDRESS_SPACE_GENERIC) {
3584 // Shared and SharedCluster can be converted to each other through generic
3585 // space
3586 if ((SrcAS == llvm::ADDRESS_SPACE_SHARED &&
3589 DestAS == llvm::ADDRESS_SPACE_SHARED)) {
3590 SDLoc DL(Op.getNode());
3591 const MVT GenerictVT =
3593 SDValue GenericConversion = DAG.getAddrSpaceCast(
3594 DL, GenerictVT, Op.getOperand(0), SrcAS, ADDRESS_SPACE_GENERIC);
3595 SDValue SharedClusterConversion =
3596 DAG.getAddrSpaceCast(DL, Op.getValueType(), GenericConversion,
3597 ADDRESS_SPACE_GENERIC, DestAS);
3598 return SharedClusterConversion;
3599 }
3600
3601 return DAG.getUNDEF(Op.getValueType());
3602 }
3603
3604 return Op;
3605}
3606
3607// This function is almost a copy of SelectionDAG::expandVAArg().
3608// The only diff is that this one produces loads from local address space.
3609SDValue NVPTXTargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
3610 const TargetLowering *TLI = STI.getTargetLowering();
3611 SDLoc DL(Op);
3612
3613 SDNode *Node = Op.getNode();
3614 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
3615 EVT VT = Node->getValueType(0);
3616 auto *Ty = VT.getTypeForEVT(*DAG.getContext());
3617 SDValue Tmp1 = Node->getOperand(0);
3618 SDValue Tmp2 = Node->getOperand(1);
3619 const MaybeAlign MA(Node->getConstantOperandVal(3));
3620
3621 SDValue VAListLoad = DAG.getLoad(TLI->getPointerTy(DAG.getDataLayout()), DL,
3622 Tmp1, Tmp2, MachinePointerInfo(V));
3623 SDValue VAList = VAListLoad;
3624
3625 if (MA && *MA > TLI->getMinStackArgumentAlignment()) {
3626 VAList = DAG.getNode(
3627 ISD::ADD, DL, VAList.getValueType(), VAList,
3628 DAG.getConstant(MA->value() - 1, DL, VAList.getValueType()));
3629
3630 VAList = DAG.getNode(ISD::AND, DL, VAList.getValueType(), VAList,
3631 DAG.getSignedConstant(-(int64_t)MA->value(), DL,
3632 VAList.getValueType()));
3633 }
3634
3635 // Increment the pointer, VAList, to the next vaarg
3636 Tmp1 = DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
3638 DL, VAList.getValueType()));
3639
3640 // Store the incremented VAList to the legalized pointer
3641 Tmp1 = DAG.getStore(VAListLoad.getValue(1), DL, Tmp1, Tmp2,
3642 MachinePointerInfo(V));
3643
3644 const Value *SrcV = Constant::getNullValue(
3646
3647 // Load the actual argument out of the pointer VAList
3648 return DAG.getLoad(VT, DL, Tmp1, VAList, MachinePointerInfo(SrcV));
3649}
3650
3651SDValue NVPTXTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
3652 const TargetLowering *TLI = STI.getTargetLowering();
3653 SDLoc DL(Op);
3654 EVT PtrVT = TLI->getPointerTy(DAG.getDataLayout());
3655
3656 // Store the address of unsized array <function>_vararg[] in the ap object.
3657 SDValue VAReg = getParamSymbolNode(DAG, /* vararg */ -1, PtrVT);
3658
3659 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
3660 return DAG.getStore(Op.getOperand(0), DL, VAReg, Op.getOperand(1),
3661 MachinePointerInfo(SV));
3662}
3663
3664static std::pair<MemSDNode *, uint32_t>
3666 const NVPTXSubtarget &STI) {
3667 SDValue Chain = N->getOperand(0);
3668 SDValue BasePtr = N->getOperand(1);
3669 SDValue Mask = N->getOperand(3);
3670 [[maybe_unused]] SDValue Passthru = N->getOperand(4);
3671
3672 SDLoc DL(N);
3673 EVT ResVT = N->getValueType(0);
3674 assert(ResVT.isVector() && "Masked vector load must have vector type");
3675 // While we only expect poison passthru vectors as an input to the backend,
3676 // when the legalization framework splits a poison vector in half, it creates
3677 // two undef vectors, so we can technically expect those too.
3678 assert((Passthru.getOpcode() == ISD::POISON ||
3679 Passthru.getOpcode() == ISD::UNDEF) &&
3680 "Passthru operand expected to be poison or undef");
3681
3682 // Extract the mask and convert it to a uint32_t representing the used bytes
3683 // of the entire vector load
3684 uint32_t UsedBytesMask = 0;
3685 uint32_t ElementSizeInBits = ResVT.getVectorElementType().getSizeInBits();
3686 assert(ElementSizeInBits % 8 == 0 && "Unexpected element size");
3687 uint32_t ElementSizeInBytes = ElementSizeInBits / 8;
3688 uint32_t ElementMask = (1u << ElementSizeInBytes) - 1u;
3689
3690 for (SDValue Op : reverse(Mask->ops())) {
3691 // We technically only want to do this shift for every
3692 // iteration *but* the first, but in the first iteration UsedBytesMask is 0,
3693 // so this shift is a no-op.
3694 UsedBytesMask <<= ElementSizeInBytes;
3695
3696 // Mask elements must be constants.
3697 if (Op->getAsZExtVal() != 0)
3698 UsedBytesMask |= ElementMask;
3699 }
3700
3701 assert(UsedBytesMask != 0 && UsedBytesMask != UINT32_MAX &&
3702 "Unexpected masked load with elements masked all on or all off");
3703
3704 // Create a new load sd node to be handled normally by ReplaceLoadVector.
3705 MemSDNode *NewLD = cast<MemSDNode>(
3706 DAG.getLoad(ResVT, DL, Chain, BasePtr, N->getMemOperand()).getNode());
3707
3708 // If our subtarget does not support the used bytes mask pragma, "drop" the
3709 // mask by setting it to UINT32_MAX
3710 if (!STI.hasUsedBytesMaskPragma())
3711 UsedBytesMask = UINT32_MAX;
3712
3713 return {NewLD, UsedBytesMask};
3714}
3715
3716/// replaceLoadVector - Convert vector loads into multi-output scalar loads.
3717static std::optional<std::pair<SDValue, SDValue>>
3720 const EVT ResVT = LD->getValueType(0);
3721 const EVT MemVT = LD->getMemoryVT();
3722
3723 // If we're doing sign/zero extension as part of the load, avoid lowering to
3724 // a LoadV node. TODO: consider relaxing this restriction.
3725 if (ResVT != MemVT)
3726 return std::nullopt;
3727
3728 const auto NumEltsAndEltVT =
3729 getVectorLoweringShape(ResVT, STI, LD->getAddressSpace());
3730 if (!NumEltsAndEltVT)
3731 return std::nullopt;
3732 const auto [NumElts, EltVT] = NumEltsAndEltVT.value();
3733
3734 Align Alignment = LD->getAlign();
3735 const auto &TD = DAG.getDataLayout();
3736 Align PrefAlign = TD.getPrefTypeAlign(MemVT.getTypeForEVT(*DAG.getContext()));
3737 if (Alignment < PrefAlign) {
3738 // This load is not sufficiently aligned, so bail out and let this vector
3739 // load be scalarized. Note that we may still be able to emit smaller
3740 // vector loads. For example, if we are loading a <4 x float> with an
3741 // alignment of 8, this check will fail but the legalizer will try again
3742 // with 2 x <2 x float>, which will succeed with an alignment of 8.
3743 return std::nullopt;
3744 }
3745
3746 // If we have a masked load, convert it to a normal load now
3747 std::optional<uint32_t> UsedBytesMask = std::nullopt;
3748 if (LD->getOpcode() == ISD::MLOAD)
3749 std::tie(LD, UsedBytesMask) =
3751
3752 // Since LoadV2 is a target node, we cannot rely on DAG type legalization.
3753 // Therefore, we must ensure the type is legal. For i1 and i8, we set the
3754 // loaded type to i16 and propagate the "real" type as the memory type.
3755 const MVT LoadEltVT = (EltVT.getSizeInBits() < 16) ? MVT::i16 : EltVT;
3756
3757 unsigned Opcode;
3758 switch (NumElts) {
3759 default:
3760 return std::nullopt;
3761 case 2:
3762 Opcode = NVPTXISD::LoadV2;
3763 break;
3764 case 4:
3765 Opcode = NVPTXISD::LoadV4;
3766 break;
3767 case 8:
3768 Opcode = NVPTXISD::LoadV8;
3769 break;
3770 }
3771 auto ListVTs = SmallVector<EVT, 9>(NumElts, LoadEltVT);
3772 ListVTs.push_back(MVT::Other);
3773 SDVTList LdResVTs = DAG.getVTList(ListVTs);
3774
3775 SDLoc DL(LD);
3776
3777 // Copy regular operands
3778 SmallVector<SDValue, 8> OtherOps(LD->ops());
3779
3780 OtherOps.push_back(
3781 DAG.getConstant(UsedBytesMask.value_or(UINT32_MAX), DL, MVT::i32));
3782
3783 // The select routine does not have access to the LoadSDNode instance, so
3784 // pass along the extension information
3785 OtherOps.push_back(
3786 DAG.getIntPtrConstant(cast<LoadSDNode>(LD)->getExtensionType(), DL));
3787
3788 SDValue NewLD = DAG.getMemIntrinsicNode(Opcode, DL, LdResVTs, OtherOps, MemVT,
3789 LD->getMemOperand());
3790
3791 SmallVector<SDValue> ScalarRes;
3792 if (EltVT.isVector()) {
3794 assert(NumElts * EltVT.getVectorNumElements() ==
3795 ResVT.getVectorNumElements());
3796 // Generate EXTRACT_VECTOR_ELTs to split v2[i,f,bf]16/v4i8 subvectors back
3797 // into individual elements.
3798 for (const unsigned I : llvm::seq(NumElts)) {
3799 SDValue SubVector = NewLD.getValue(I);
3800 DAG.ExtractVectorElements(SubVector, ScalarRes);
3801 }
3802 } else {
3803 for (const unsigned I : llvm::seq(NumElts)) {
3804 SDValue Res = NewLD.getValue(I);
3805 if (LoadEltVT != EltVT)
3806 Res = DAG.getNode(ISD::TRUNCATE, DL, EltVT, Res);
3807 ScalarRes.push_back(Res);
3808 }
3809 }
3810
3811 SDValue LoadChain = NewLD.getValue(NumElts);
3812
3813 const MVT BuildVecVT =
3814 MVT::getVectorVT(EltVT.getScalarType(), ScalarRes.size());
3815 SDValue BuildVec = DAG.getBuildVector(BuildVecVT, DL, ScalarRes);
3816 SDValue LoadValue = DAG.getBitcast(ResVT, BuildVec);
3817
3818 return {{LoadValue, LoadChain}};
3819}
3820
3823 const NVPTXSubtarget &STI) {
3824 if (auto Res = replaceLoadVector(N, DAG, STI))
3825 Results.append({Res->first, Res->second});
3826}
3827
3829 const NVPTXSubtarget &STI) {
3830 if (auto Res = replaceLoadVector(N, DAG, STI))
3831 return DAG.getMergeValues({Res->first, Res->second}, SDLoc(N));
3832 return SDValue();
3833}
3834
3835// v = ld i1* addr
3836// =>
3837// v1 = ld i8* addr (-> i16)
3838// v = trunc i16 to i1
3840 SDLoc dl(LD);
3841 assert(LD->getExtensionType() == ISD::NON_EXTLOAD);
3842 assert(LD->getValueType(0) == MVT::i1 && "Custom lowering for i1 load only");
3843 SDValue newLD = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i16, LD->getChain(),
3844 LD->getBasePtr(), LD->getPointerInfo(),
3845 MVT::i8, LD->getAlign(),
3846 LD->getMemOperand()->getFlags());
3847 SDValue result = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, newLD);
3848 // The legalizer (the caller) is expecting two values from the legalized
3849 // load, so we build a MergeValues node for it. See ExpandUnalignedLoad()
3850 // in LegalizeDAG.cpp which also uses MergeValues.
3851 return DAG.getMergeValues({result, LD->getChain()}, dl);
3852}
3853
3854SDValue NVPTXTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
3855 LoadSDNode *LD = cast<LoadSDNode>(Op);
3856
3857 if (Op.getValueType() == MVT::i1)
3858 return lowerLOADi1(LD, DAG);
3859
3860 // To improve CodeGen we'll legalize any-extend loads to zext loads. This is
3861 // how they'll be lowered in ISel anyway, and by doing this a little earlier
3862 // we allow for more DAG combine opportunities.
3863 if (LD->getExtensionType() == ISD::EXTLOAD) {
3864 assert(LD->getValueType(0).isInteger() && LD->getMemoryVT().isInteger() &&
3865 "Unexpected fpext-load");
3866 return DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(Op), Op.getValueType(),
3867 LD->getChain(), LD->getBasePtr(), LD->getMemoryVT(),
3868 LD->getMemOperand());
3869 }
3870
3871 llvm_unreachable("Unexpected custom lowering for load");
3872}
3873
3874SDValue NVPTXTargetLowering::LowerMLOAD(SDValue Op, SelectionDAG &DAG) const {
3875 // v2f16/v2bf16/v2i16/v4i8 are legal, so we can't rely on legalizer to handle
3876 // masked loads of these types and have to handle them here.
3877 // v2f32 also needs to be handled here if the subtarget has f32x2
3878 // instructions, making it legal.
3879 //
3880 // Note: misaligned masked loads should never reach this point
3881 // because the override of isLegalMaskedLoad in NVPTXTargetTransformInfo.cpp
3882 // will validate alignment. Therefore, we do not need to special case handle
3883 // them here.
3884 EVT VT = Op.getValueType();
3885 if (NVPTX::isPackedVectorTy(VT)) {
3887 cast<MemSDNode>(Op.getNode()), DAG, STI);
3888 MemSDNode *LD = std::get<0>(Result);
3889 uint32_t UsedBytesMask = std::get<1>(Result);
3890
3891 SDLoc DL(LD);
3892
3893 // Copy regular operands
3894 SmallVector<SDValue, 8> OtherOps(LD->ops());
3895
3896 OtherOps.push_back(DAG.getConstant(UsedBytesMask, DL, MVT::i32));
3897
3898 // We currently are not lowering extending loads, but pass the extension
3899 // type anyway as later handling expects it.
3900 OtherOps.push_back(
3901 DAG.getIntPtrConstant(cast<LoadSDNode>(LD)->getExtensionType(), DL));
3902 SDValue NewLD =
3903 DAG.getMemIntrinsicNode(NVPTXISD::MLoad, DL, LD->getVTList(), OtherOps,
3904 LD->getMemoryVT(), LD->getMemOperand());
3905 return NewLD;
3906 }
3907 return SDValue();
3908}
3909
3911 const NVPTXSubtarget &STI) {
3912 MemSDNode *N = cast<MemSDNode>(Op.getNode());
3913 SDValue Val = N->getOperand(1);
3914 SDLoc DL(N);
3915 const EVT ValVT = Val.getValueType();
3916 const EVT MemVT = N->getMemoryVT();
3917
3918 // If we're truncating as part of the store, avoid lowering to a StoreV node.
3919 // TODO: consider relaxing this restriction.
3920 if (ValVT != MemVT)
3921 return SDValue();
3922
3923 const auto NumEltsAndEltVT =
3924 getVectorLoweringShape(ValVT, STI, N->getAddressSpace());
3925 if (!NumEltsAndEltVT)
3926 return SDValue();
3927 const auto [NumElts, EltVT] = NumEltsAndEltVT.value();
3928
3929 const DataLayout &TD = DAG.getDataLayout();
3930
3931 Align Alignment = N->getAlign();
3932 Align PrefAlign = TD.getPrefTypeAlign(ValVT.getTypeForEVT(*DAG.getContext()));
3933 if (Alignment < PrefAlign) {
3934 // This store is not sufficiently aligned, so bail out and let this vector
3935 // store be scalarized. Note that we may still be able to emit smaller
3936 // vector stores. For example, if we are storing a <4 x float> with an
3937 // alignment of 8, this check will fail but the legalizer will try again
3938 // with 2 x <2 x float>, which will succeed with an alignment of 8.
3939 return SDValue();
3940 }
3941
3942 unsigned Opcode;
3943 switch (NumElts) {
3944 default:
3945 return SDValue();
3946 case 2:
3947 Opcode = NVPTXISD::StoreV2;
3948 break;
3949 case 4:
3950 Opcode = NVPTXISD::StoreV4;
3951 break;
3952 case 8:
3953 Opcode = NVPTXISD::StoreV8;
3954 break;
3955 }
3956
3958
3959 // First is the chain
3960 Ops.push_back(N->getOperand(0));
3961
3962 // Then the split values
3963 if (EltVT.isVector()) {
3965 assert(NumElts * EltVT.getVectorNumElements() ==
3966 ValVT.getVectorNumElements());
3967 // Combine individual elements into v2[i,f,bf]16/v4i8 subvectors to be
3968 // stored as b32s
3969 const unsigned NumEltsPerSubVector = EltVT.getVectorNumElements();
3970 for (const unsigned I : llvm::seq(NumElts)) {
3971 SmallVector<SDValue, 4> SubVectorElts;
3972 DAG.ExtractVectorElements(Val, SubVectorElts, I * NumEltsPerSubVector,
3973 NumEltsPerSubVector);
3974 Ops.push_back(DAG.getBuildVector(EltVT, DL, SubVectorElts));
3975 }
3976 } else {
3977 SDValue V = DAG.getBitcast(MVT::getVectorVT(EltVT, NumElts), Val);
3978 for (const unsigned I : llvm::seq(NumElts)) {
3979 SDValue ExtVal = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, V,
3980 DAG.getIntPtrConstant(I, DL));
3981
3982 // Since StoreV2 is a target node, we cannot rely on DAG type
3983 // legalization. Therefore, we must ensure the type is legal. For i1 and
3984 // i8, we set the stored type to i16 and propagate the "real" type as the
3985 // memory type.
3986 if (EltVT.getSizeInBits() < 16)
3987 ExtVal = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i16, ExtVal);
3988 Ops.push_back(ExtVal);
3989 }
3990 }
3991
3992 // Then any remaining arguments
3993 Ops.append(N->op_begin() + 2, N->op_end());
3994
3995 SDValue NewSt =
3996 DAG.getMemIntrinsicNode(Opcode, DL, DAG.getVTList(MVT::Other), Ops,
3997 N->getMemoryVT(), N->getMemOperand());
3998
3999 // return DCI.CombineTo(N, NewSt, true);
4000 return NewSt;
4001}
4002
4003SDValue NVPTXTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
4004 StoreSDNode *Store = cast<StoreSDNode>(Op);
4005 EVT VT = Store->getMemoryVT();
4006
4007 if (VT == MVT::i1)
4008 return LowerSTOREi1(Op, DAG);
4009
4010 // Lower store of any other vector type, including v2f32 as we want to break
4011 // it apart since this is not a widely-supported type.
4012 return lowerSTOREVector(Op, DAG, STI);
4013}
4014
4015// st i1 v, addr
4016// =>
4017// v1 = zxt v to i16
4018// st.u8 i16, addr
4019SDValue NVPTXTargetLowering::LowerSTOREi1(SDValue Op, SelectionDAG &DAG) const {
4020 SDNode *Node = Op.getNode();
4021 SDLoc dl(Node);
4022 StoreSDNode *ST = cast<StoreSDNode>(Node);
4023 SDValue Tmp1 = ST->getChain();
4024 SDValue Tmp2 = ST->getBasePtr();
4025 SDValue Tmp3 = ST->getValue();
4026 assert(Tmp3.getValueType() == MVT::i1 && "Custom lowering for i1 store only");
4027 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Tmp3);
4028 SDValue Result =
4029 DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(), MVT::i8,
4030 ST->getAlign(), ST->getMemOperand()->getFlags());
4031 return Result;
4032}
4033
4034SDValue NVPTXTargetLowering::LowerCopyToReg_128(SDValue Op,
4035 SelectionDAG &DAG) const {
4036 // Change the CopyToReg to take in two 64-bit operands instead of a 128-bit
4037 // operand so that it can pass the legalization.
4038
4039 assert(Op.getOperand(1).getValueType() == MVT::i128 &&
4040 "Custom lowering for 128-bit CopyToReg only");
4041
4042 SDNode *Node = Op.getNode();
4043 SDLoc DL(Node);
4044
4045 SDValue Cast = DAG.getBitcast(MVT::v2i64, Op->getOperand(2));
4046 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i64, Cast,
4047 DAG.getIntPtrConstant(0, DL));
4048 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i64, Cast,
4049 DAG.getIntPtrConstant(1, DL));
4050
4052 SmallVector<EVT, 3> ResultsType(Node->values());
4053
4054 NewOps[0] = Op->getOperand(0); // Chain
4055 NewOps[1] = Op->getOperand(1); // Dst Reg
4056 NewOps[2] = Lo; // Lower 64-bit
4057 NewOps[3] = Hi; // Higher 64-bit
4058 if (Op.getNumOperands() == 4)
4059 NewOps[4] = Op->getOperand(3); // Glue if exists
4060
4061 return DAG.getNode(ISD::CopyToReg, DL, ResultsType, NewOps);
4062}
4063
4064unsigned NVPTXTargetLowering::getNumRegisters(
4065 LLVMContext &Context, EVT VT,
4066 std::optional<MVT> RegisterVT = std::nullopt) const {
4067 if (VT == MVT::i128 && RegisterVT == MVT::i128)
4068 return 1;
4069 return TargetLoweringBase::getNumRegisters(Context, VT, RegisterVT);
4070}
4071
4072bool NVPTXTargetLowering::splitValueIntoRegisterParts(
4073 SelectionDAG &DAG, const SDLoc &DL, SDValue Val, SDValue *Parts,
4074 unsigned NumParts, MVT PartVT, std::optional<CallingConv::ID> CC) const {
4075 if (Val.getValueType() == MVT::i128 && NumParts == 1) {
4076 Parts[0] = Val;
4077 return true;
4078 }
4079 return false;
4080}
4081
4082SDValue NVPTXTargetLowering::getParamSymbolNode(SelectionDAG &DAG, int I,
4083 EVT T) const {
4084 const MachineFunction &MF = DAG.getMachineFunction();
4085 return getSymbolNode(
4086 DAG, getParamSymbol(MF.getContext(), &MF.getFunction(), I), T);
4087}
4088
4089SDValue NVPTXTargetLowering::getCallParamSymbolNode(SelectionDAG &DAG, int I,
4090 EVT T) const {
4091 return getSymbolNode(DAG, "param" + Twine(I), T);
4092}
4093
4095 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
4096 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
4097 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
4098 const DataLayout &DL = DAG.getDataLayout();
4099 LLVMContext &Ctx = *DAG.getContext();
4100
4101 const Function &F = DAG.getMachineFunction().getFunction();
4102 const bool IsKernel = isKernelFunction(F);
4103
4104 const MVT PtrVT = getPointerTy(DL, IsKernel ? ADDRESS_SPACE_ENTRY_PARAM
4106
4107 SDValue Root = DAG.getRoot();
4108 SmallVector<SDValue, 16> OutChains;
4109
4110 // argTypes.size() (or theArgs.size()) and Ins.size() need not match.
4111 // Ins.size() will be larger
4112 // * if there is an aggregate argument with multiple fields (each field
4113 // showing up separately in Ins)
4114 // * if there is a vector argument with more than typical vector-length
4115 // elements (generally if more than 4) where each vector element is
4116 // individually present in Ins.
4117 // So a different index should be used for indexing into Ins.
4118 // See similar issue in LowerCall.
4119
4120 auto AllIns = ArrayRef(Ins);
4121 const auto NonEmptyArgs = make_filter_range(
4122 F.args(), [](const Argument &A) { return !A.getType()->isEmptyTy(); });
4123 for (const auto &[ParamI, Arg] : enumerate(NonEmptyArgs)) {
4124 const unsigned ArgNo = Arg.getArgNo();
4125 const auto ArgIns =
4126 AllIns.take_while([&](auto I) { return I.OrigArgIndex == ArgNo; });
4127 AllIns = AllIns.drop_front(ArgIns.size());
4128
4129 Type *Ty = Arg.getType();
4130 assert(!ArgIns.empty() &&
4131 "Non-empty argument produced no parameter values");
4132
4133 if (Arg.use_empty()) {
4134 // argument is dead
4135 for (const auto &In : ArgIns) {
4136 assert(!In.Used && "Arg.use_empty() is true but Arg is used?");
4137 InVals.push_back(DAG.getUNDEF(In.VT));
4138 }
4139 continue;
4140 }
4141
4142 SDValue ArgSymbol = getParamSymbolNode(DAG, ParamI, PtrVT);
4143
4144 // In the following cases, assign a node order of "i+1"
4145 // to newly created nodes. The SDNodes for params have to
4146 // appear in the same order as their order of appearance
4147 // in the original function. "i+1" holds that order.
4148 if (Arg.hasByValAttr()) {
4149 // Param has ByVal attribute
4150 // Return MoveParam(param symbol).
4151 // Ideally, the param symbol can be returned directly,
4152 // but when SDNode builder decides to use it in a CopyToReg(),
4153 // machine instruction fails because TargetExternalSymbol
4154 // (not lowered) is target dependent, and CopyToReg assumes
4155 // the source is lowered.
4156 assert(ArgIns.size() == 1 && "ByVal argument must be a pointer");
4157 const auto &ByvalIn = ArgIns[0];
4158 assert(getValueType(DL, Ty) == ByvalIn.VT &&
4159 "Ins type did not match function type");
4160
4161 SDValue P;
4162 if (IsKernel) {
4163 assert(Ty->getPointerAddressSpace() == ADDRESS_SPACE_ENTRY_PARAM &&
4164 "Kernel ByVal argument must be lowered to the param address "
4165 "space by NVPTXLowerArgs");
4166 P = ArgSymbol;
4167 P.getNode()->setIROrder(Arg.getArgNo() + 1);
4168 } else {
4169 P = DAG.getNode(NVPTXISD::MoveParam, dl, ArgSymbol.getValueType(),
4170 ArgSymbol);
4171 P.getNode()->setIROrder(Arg.getArgNo() + 1);
4172 P = DAG.getAddrSpaceCast(dl, ByvalIn.VT, P, ADDRESS_SPACE_LOCAL,
4174 }
4175 InVals.push_back(P);
4176 } else {
4179 ComputePTXValueVTs(*this, DL, Ctx, CallConv, Ty, VTs, Offsets);
4180 assert(VTs.size() == ArgIns.size() && "Size mismatch");
4181 assert(VTs.size() == Offsets.size() && "Size mismatch");
4182
4183 const Align ArgAlign = getPTXParamAlign(
4184 &F, Ty, Arg.getArgNo() + AttributeList::FirstArgIndex, DL);
4185
4186 unsigned I = 0;
4187 const auto VI = VectorizePTXValueVTs(VTs, Offsets, ArgAlign);
4188 for (const unsigned NumElts : VI) {
4189 // i1 is loaded/stored as i8
4190 const EVT LoadVT = VTs[I] == MVT::i1 ? MVT::i8 : VTs[I];
4191 const EVT VecVT = getVectorizedVT(LoadVT, NumElts, Ctx);
4192
4193 SDValue VecAddr = DAG.getObjectPtrOffset(
4194 dl, ArgSymbol, TypeSize::getFixed(Offsets[I]));
4195
4196 const Align PartAlign = commonAlignment(ArgAlign, Offsets[I]);
4197 const unsigned AS = IsKernel ? NVPTX::AddressSpace::EntryParam
4199 SDValue P = DAG.getLoad(VecVT, dl, Root, VecAddr,
4200 MachinePointerInfo(AS), PartAlign,
4203 P.getNode()->setIROrder(Arg.getArgNo() + 1);
4204 for (const unsigned J : llvm::seq(NumElts)) {
4205 SDValue Elt = getExtractVectorizedValue(P, J, LoadVT, dl, DAG);
4206
4207 Elt = correctParamType(Elt, ArgIns[I + J].VT, ArgIns[I + J].Flags,
4208 DAG, dl);
4209 InVals.push_back(Elt);
4210 }
4211 I += NumElts;
4212 }
4213 }
4214 }
4215
4216 if (!OutChains.empty())
4217 DAG.setRoot(DAG.getTokenFactor(dl, OutChains));
4218
4219 return Chain;
4220}
4221
4222SDValue
4224 bool isVarArg,
4226 const SmallVectorImpl<SDValue> &OutVals,
4227 const SDLoc &dl, SelectionDAG &DAG) const {
4228 const Function &F = DAG.getMachineFunction().getFunction();
4229 Type *RetTy = F.getReturnType();
4230
4231 if (RetTy->isVoidTy()) {
4232 assert(OutVals.empty() && Outs.empty() && "Return value expected for void");
4233 return DAG.getNode(NVPTXISD::RET_GLUE, dl, MVT::Other, Chain);
4234 }
4235
4236 const DataLayout &DL = DAG.getDataLayout();
4237 LLVMContext &Ctx = *DAG.getContext();
4238
4239 const SDValue RetSymbol = getSymbolNode(DAG, "func_retval0", MVT::i32);
4240 const auto RetAlign =
4241 getPTXParamAlign(&F, RetTy, AttributeList::ReturnIndex, DL);
4242
4243 // PTX Interoperability Guide 3.3(A): [Integer] Values shorter than
4244 // 32-bits are sign extended or zero extended, depending on whether
4245 // they are signed or unsigned types.
4246 const bool ExtendIntegerRetVal =
4247 RetTy->isIntegerTy() && DL.getTypeAllocSizeInBits(RetTy) < 32;
4248
4251 ComputePTXValueVTs(*this, DL, Ctx, CallConv, RetTy, VTs, Offsets);
4252 assert(VTs.size() == OutVals.size() && "Bad return value decomposition");
4253
4254 const auto GetRetVal = [&](unsigned I) -> SDValue {
4255 SDValue RetVal = OutVals[I];
4257 RetVal.getValueType() &&
4258 "OutVal type should always be legal");
4259
4260 const EVT VTI = promoteScalarIntegerPTX(VTs[I]);
4261 const EVT StoreVT =
4262 ExtendIntegerRetVal ? MVT::i32 : (VTI == MVT::i1 ? MVT::i8 : VTI);
4263 return correctParamType(RetVal, StoreVT, Outs[I].Flags, DAG, dl);
4264 };
4265
4266 unsigned I = 0;
4267 const auto VI = VectorizePTXValueVTs(VTs, Offsets, RetAlign);
4268 for (const unsigned NumElts : VI) {
4269 const MaybeAlign CurrentAlign = ExtendIntegerRetVal
4270 ? MaybeAlign(std::nullopt)
4271 : commonAlignment(RetAlign, Offsets[I]);
4272
4274 NumElts, dl, DAG, [&](unsigned K) { return GetRetVal(I + K); });
4275
4276 SDValue Ptr =
4277 DAG.getObjectPtrOffset(dl, RetSymbol, TypeSize::getFixed(Offsets[I]));
4278
4279 Chain = DAG.getStore(Chain, dl, Val, Ptr,
4281 CurrentAlign);
4282
4283 I += NumElts;
4284 }
4285
4286 return DAG.getNode(NVPTXISD::RET_GLUE, dl, MVT::Other, Chain);
4287}
4288
4290 SDValue Op, StringRef Constraint, std::vector<SDValue> &Ops,
4291 SelectionDAG &DAG) const {
4292 if (Constraint.size() > 1)
4293 return;
4295}
4296
4297// llvm.ptx.memcpy.const and llvm.ptx.memmove.const need to be modeled as
4298// TgtMemIntrinsic
4299// because we need the information that is only available in the "Value" type
4300// of destination
4301// pointer. In particular, the address space information.
4304 MachineFunction &MF, unsigned Intrinsic) const {
4305 IntrinsicInfo Info;
4306 switch (Intrinsic) {
4307 default:
4308 return;
4309 case Intrinsic::nvvm_match_all_sync_i32p:
4310 case Intrinsic::nvvm_match_all_sync_i64p:
4311 Info.opc = ISD::INTRINSIC_W_CHAIN;
4312 // memVT is bogus. These intrinsics have IntrInaccessibleMemOnly attribute
4313 // in order to model data exchange with other threads, but perform no real
4314 // memory accesses.
4315 Info.memVT = MVT::i1;
4316
4317 // Our result depends on both our and other thread's arguments.
4319 Infos.push_back(Info);
4320 return;
4321 case Intrinsic::nvvm_wmma_m16n16k16_load_a_f16_col:
4322 case Intrinsic::nvvm_wmma_m16n16k16_load_a_f16_row:
4323 case Intrinsic::nvvm_wmma_m16n16k16_load_a_f16_col_stride:
4324 case Intrinsic::nvvm_wmma_m16n16k16_load_a_f16_row_stride:
4325 case Intrinsic::nvvm_wmma_m16n16k16_load_b_f16_col:
4326 case Intrinsic::nvvm_wmma_m16n16k16_load_b_f16_row:
4327 case Intrinsic::nvvm_wmma_m16n16k16_load_b_f16_col_stride:
4328 case Intrinsic::nvvm_wmma_m16n16k16_load_b_f16_row_stride:
4329 case Intrinsic::nvvm_wmma_m32n8k16_load_a_f16_col:
4330 case Intrinsic::nvvm_wmma_m32n8k16_load_a_f16_row:
4331 case Intrinsic::nvvm_wmma_m32n8k16_load_a_f16_col_stride:
4332 case Intrinsic::nvvm_wmma_m32n8k16_load_a_f16_row_stride:
4333 case Intrinsic::nvvm_wmma_m32n8k16_load_b_f16_col:
4334 case Intrinsic::nvvm_wmma_m32n8k16_load_b_f16_row:
4335 case Intrinsic::nvvm_wmma_m32n8k16_load_b_f16_col_stride:
4336 case Intrinsic::nvvm_wmma_m32n8k16_load_b_f16_row_stride:
4337 case Intrinsic::nvvm_wmma_m8n32k16_load_a_f16_col:
4338 case Intrinsic::nvvm_wmma_m8n32k16_load_a_f16_row:
4339 case Intrinsic::nvvm_wmma_m8n32k16_load_a_f16_col_stride:
4340 case Intrinsic::nvvm_wmma_m8n32k16_load_a_f16_row_stride:
4341 case Intrinsic::nvvm_wmma_m8n32k16_load_b_f16_col:
4342 case Intrinsic::nvvm_wmma_m8n32k16_load_b_f16_row:
4343 case Intrinsic::nvvm_wmma_m8n32k16_load_b_f16_col_stride:
4344 case Intrinsic::nvvm_wmma_m8n32k16_load_b_f16_row_stride: {
4345 Info.opc = ISD::INTRINSIC_W_CHAIN;
4346 Info.memVT = MVT::v8f16;
4347 Info.ptrVal = I.getArgOperand(0);
4348 Info.offset = 0;
4349 Info.flags = MachineMemOperand::MOLoad;
4350 Info.align = Align(16);
4351 Infos.push_back(Info);
4352 return;
4353 }
4354 case Intrinsic::nvvm_wmma_m16n16k16_load_a_s8_col:
4355 case Intrinsic::nvvm_wmma_m16n16k16_load_a_s8_col_stride:
4356 case Intrinsic::nvvm_wmma_m16n16k16_load_a_u8_col_stride:
4357 case Intrinsic::nvvm_wmma_m16n16k16_load_a_u8_col:
4358 case Intrinsic::nvvm_wmma_m16n16k16_load_a_s8_row:
4359 case Intrinsic::nvvm_wmma_m16n16k16_load_a_s8_row_stride:
4360 case Intrinsic::nvvm_wmma_m16n16k16_load_a_u8_row_stride:
4361 case Intrinsic::nvvm_wmma_m16n16k16_load_a_u8_row:
4362 case Intrinsic::nvvm_wmma_m8n32k16_load_a_bf16_col:
4363 case Intrinsic::nvvm_wmma_m8n32k16_load_a_bf16_col_stride:
4364 case Intrinsic::nvvm_wmma_m8n32k16_load_a_bf16_row:
4365 case Intrinsic::nvvm_wmma_m8n32k16_load_a_bf16_row_stride:
4366 case Intrinsic::nvvm_wmma_m16n16k16_load_b_s8_col:
4367 case Intrinsic::nvvm_wmma_m16n16k16_load_b_s8_col_stride:
4368 case Intrinsic::nvvm_wmma_m16n16k16_load_b_u8_col_stride:
4369 case Intrinsic::nvvm_wmma_m16n16k16_load_b_u8_col:
4370 case Intrinsic::nvvm_wmma_m16n16k16_load_b_s8_row:
4371 case Intrinsic::nvvm_wmma_m16n16k16_load_b_s8_row_stride:
4372 case Intrinsic::nvvm_wmma_m16n16k16_load_b_u8_row_stride:
4373 case Intrinsic::nvvm_wmma_m16n16k16_load_b_u8_row:
4374 case Intrinsic::nvvm_wmma_m32n8k16_load_b_bf16_col:
4375 case Intrinsic::nvvm_wmma_m32n8k16_load_b_bf16_col_stride:
4376 case Intrinsic::nvvm_wmma_m32n8k16_load_b_bf16_row:
4377 case Intrinsic::nvvm_wmma_m32n8k16_load_b_bf16_row_stride: {
4378 Info.opc = ISD::INTRINSIC_W_CHAIN;
4379 Info.memVT = MVT::v2i32;
4380 Info.ptrVal = I.getArgOperand(0);
4381 Info.offset = 0;
4382 Info.flags = MachineMemOperand::MOLoad;
4383 Info.align = Align(8);
4384 Infos.push_back(Info);
4385 return;
4386 }
4387
4388 case Intrinsic::nvvm_wmma_m32n8k16_load_a_s8_col:
4389 case Intrinsic::nvvm_wmma_m32n8k16_load_a_s8_col_stride:
4390 case Intrinsic::nvvm_wmma_m32n8k16_load_a_u8_col_stride:
4391 case Intrinsic::nvvm_wmma_m32n8k16_load_a_u8_col:
4392 case Intrinsic::nvvm_wmma_m32n8k16_load_a_s8_row:
4393 case Intrinsic::nvvm_wmma_m32n8k16_load_a_s8_row_stride:
4394 case Intrinsic::nvvm_wmma_m32n8k16_load_a_u8_row_stride:
4395 case Intrinsic::nvvm_wmma_m32n8k16_load_a_u8_row:
4396 case Intrinsic::nvvm_wmma_m16n16k16_load_a_bf16_col:
4397 case Intrinsic::nvvm_wmma_m16n16k16_load_a_bf16_col_stride:
4398 case Intrinsic::nvvm_wmma_m16n16k16_load_a_bf16_row:
4399 case Intrinsic::nvvm_wmma_m16n16k16_load_a_bf16_row_stride:
4400 case Intrinsic::nvvm_wmma_m16n16k8_load_a_tf32_col:
4401 case Intrinsic::nvvm_wmma_m16n16k8_load_a_tf32_col_stride:
4402 case Intrinsic::nvvm_wmma_m16n16k8_load_a_tf32_row:
4403 case Intrinsic::nvvm_wmma_m16n16k8_load_a_tf32_row_stride:
4404
4405 case Intrinsic::nvvm_wmma_m8n32k16_load_b_s8_col:
4406 case Intrinsic::nvvm_wmma_m8n32k16_load_b_s8_col_stride:
4407 case Intrinsic::nvvm_wmma_m8n32k16_load_b_u8_col_stride:
4408 case Intrinsic::nvvm_wmma_m8n32k16_load_b_u8_col:
4409 case Intrinsic::nvvm_wmma_m8n32k16_load_b_s8_row:
4410 case Intrinsic::nvvm_wmma_m8n32k16_load_b_s8_row_stride:
4411 case Intrinsic::nvvm_wmma_m8n32k16_load_b_u8_row_stride:
4412 case Intrinsic::nvvm_wmma_m8n32k16_load_b_u8_row:
4413 case Intrinsic::nvvm_wmma_m16n16k16_load_b_bf16_col:
4414 case Intrinsic::nvvm_wmma_m16n16k16_load_b_bf16_col_stride:
4415 case Intrinsic::nvvm_wmma_m16n16k16_load_b_bf16_row:
4416 case Intrinsic::nvvm_wmma_m16n16k16_load_b_bf16_row_stride:
4417 case Intrinsic::nvvm_wmma_m16n16k8_load_b_tf32_col:
4418 case Intrinsic::nvvm_wmma_m16n16k8_load_b_tf32_col_stride:
4419 case Intrinsic::nvvm_wmma_m16n16k8_load_b_tf32_row:
4420 case Intrinsic::nvvm_wmma_m16n16k8_load_b_tf32_row_stride:
4421 case Intrinsic::nvvm_ldmatrix_sync_aligned_m8n8_x4_b16:
4422 case Intrinsic::nvvm_ldmatrix_sync_aligned_m8n8_x4_trans_b16:
4423 case Intrinsic::nvvm_ldmatrix_sync_aligned_m16n16_x2_trans_b8:
4424 case Intrinsic::nvvm_ldmatrix_sync_aligned_m16n16_x2_trans_b8x16_b4x16_p64:
4425 case Intrinsic::nvvm_ldmatrix_sync_aligned_m16n16_x2_trans_b8x16_b6x16_p32:
4426 case Intrinsic::nvvm_ldmatrix_sync_aligned_m8n16_x4_b8x16_b4x16_p64:
4427 case Intrinsic::nvvm_ldmatrix_sync_aligned_m8n16_x4_b8x16_b6x16_p32:
4428 case Intrinsic::nvvm_ldmatrix_sync_aligned_m8n16_x4_s8_s4: {
4429 Info.opc = ISD::INTRINSIC_W_CHAIN;
4430 Info.memVT = MVT::v4i32;
4431 Info.ptrVal = I.getArgOperand(0);
4432 Info.offset = 0;
4433 Info.flags = MachineMemOperand::MOLoad;
4434 Info.align = Align(16);
4435 Infos.push_back(Info);
4436 return;
4437 }
4438
4439 case Intrinsic::nvvm_wmma_m32n8k16_load_b_s8_col:
4440 case Intrinsic::nvvm_wmma_m32n8k16_load_b_s8_col_stride:
4441 case Intrinsic::nvvm_wmma_m32n8k16_load_b_u8_col_stride:
4442 case Intrinsic::nvvm_wmma_m32n8k16_load_b_u8_col:
4443 case Intrinsic::nvvm_wmma_m32n8k16_load_b_s8_row:
4444 case Intrinsic::nvvm_wmma_m32n8k16_load_b_s8_row_stride:
4445 case Intrinsic::nvvm_wmma_m32n8k16_load_b_u8_row_stride:
4446 case Intrinsic::nvvm_wmma_m32n8k16_load_b_u8_row:
4447
4448 case Intrinsic::nvvm_wmma_m8n32k16_load_a_s8_col:
4449 case Intrinsic::nvvm_wmma_m8n32k16_load_a_s8_col_stride:
4450 case Intrinsic::nvvm_wmma_m8n32k16_load_a_u8_col_stride:
4451 case Intrinsic::nvvm_wmma_m8n32k16_load_a_u8_col:
4452 case Intrinsic::nvvm_wmma_m8n32k16_load_a_s8_row:
4453 case Intrinsic::nvvm_wmma_m8n32k16_load_a_s8_row_stride:
4454 case Intrinsic::nvvm_wmma_m8n32k16_load_a_u8_row_stride:
4455 case Intrinsic::nvvm_wmma_m8n32k16_load_a_u8_row:
4456 case Intrinsic::nvvm_wmma_m8n8k128_load_a_b1_row:
4457 case Intrinsic::nvvm_wmma_m8n8k128_load_a_b1_row_stride:
4458 case Intrinsic::nvvm_wmma_m8n8k128_load_b_b1_col:
4459 case Intrinsic::nvvm_wmma_m8n8k128_load_b_b1_col_stride:
4460 case Intrinsic::nvvm_wmma_m8n8k32_load_a_s4_row:
4461 case Intrinsic::nvvm_wmma_m8n8k32_load_a_s4_row_stride:
4462 case Intrinsic::nvvm_wmma_m8n8k32_load_a_u4_row_stride:
4463 case Intrinsic::nvvm_wmma_m8n8k32_load_a_u4_row:
4464 case Intrinsic::nvvm_wmma_m8n8k32_load_b_s4_col:
4465 case Intrinsic::nvvm_wmma_m8n8k32_load_b_s4_col_stride:
4466 case Intrinsic::nvvm_wmma_m8n8k32_load_b_u4_col_stride:
4467 case Intrinsic::nvvm_wmma_m8n8k32_load_b_u4_col:
4468 case Intrinsic::nvvm_ldmatrix_sync_aligned_m8n8_x1_b16:
4469 case Intrinsic::nvvm_ldmatrix_sync_aligned_m8n8_x1_trans_b16:
4470 case Intrinsic::nvvm_ldmatrix_sync_aligned_m8n16_x1_b8x16_b4x16_p64:
4471 case Intrinsic::nvvm_ldmatrix_sync_aligned_m8n16_x1_b8x16_b6x16_p32:
4472 case Intrinsic::nvvm_ldmatrix_sync_aligned_m8n16_x1_s8_s4: {
4473 Info.opc = ISD::INTRINSIC_W_CHAIN;
4474 Info.memVT = MVT::i32;
4475 Info.ptrVal = I.getArgOperand(0);
4476 Info.offset = 0;
4477 Info.flags = MachineMemOperand::MOLoad;
4478 Info.align = Align(4);
4479 Infos.push_back(Info);
4480 return;
4481 }
4482
4483 case Intrinsic::nvvm_wmma_m16n16k16_load_c_f16_col:
4484 case Intrinsic::nvvm_wmma_m16n16k16_load_c_f16_row:
4485 case Intrinsic::nvvm_wmma_m16n16k16_load_c_f16_col_stride:
4486 case Intrinsic::nvvm_wmma_m16n16k16_load_c_f16_row_stride:
4487 case Intrinsic::nvvm_wmma_m32n8k16_load_c_f16_col:
4488 case Intrinsic::nvvm_wmma_m32n8k16_load_c_f16_row:
4489 case Intrinsic::nvvm_wmma_m32n8k16_load_c_f16_col_stride:
4490 case Intrinsic::nvvm_wmma_m32n8k16_load_c_f16_row_stride:
4491 case Intrinsic::nvvm_wmma_m8n32k16_load_c_f16_col:
4492 case Intrinsic::nvvm_wmma_m8n32k16_load_c_f16_row:
4493 case Intrinsic::nvvm_wmma_m8n32k16_load_c_f16_col_stride:
4494 case Intrinsic::nvvm_wmma_m8n32k16_load_c_f16_row_stride: {
4495 Info.opc = ISD::INTRINSIC_W_CHAIN;
4496 Info.memVT = MVT::v4f16;
4497 Info.ptrVal = I.getArgOperand(0);
4498 Info.offset = 0;
4499 Info.flags = MachineMemOperand::MOLoad;
4500 Info.align = Align(16);
4501 Infos.push_back(Info);
4502 return;
4503 }
4504
4505 case Intrinsic::nvvm_wmma_m16n16k16_load_c_f32_col:
4506 case Intrinsic::nvvm_wmma_m16n16k16_load_c_f32_row:
4507 case Intrinsic::nvvm_wmma_m16n16k16_load_c_f32_col_stride:
4508 case Intrinsic::nvvm_wmma_m16n16k16_load_c_f32_row_stride:
4509 case Intrinsic::nvvm_wmma_m32n8k16_load_c_f32_col:
4510 case Intrinsic::nvvm_wmma_m32n8k16_load_c_f32_row:
4511 case Intrinsic::nvvm_wmma_m32n8k16_load_c_f32_col_stride:
4512 case Intrinsic::nvvm_wmma_m32n8k16_load_c_f32_row_stride:
4513 case Intrinsic::nvvm_wmma_m8n32k16_load_c_f32_col:
4514 case Intrinsic::nvvm_wmma_m8n32k16_load_c_f32_row:
4515 case Intrinsic::nvvm_wmma_m8n32k16_load_c_f32_col_stride:
4516 case Intrinsic::nvvm_wmma_m8n32k16_load_c_f32_row_stride:
4517 case Intrinsic::nvvm_wmma_m16n16k8_load_c_f32_col:
4518 case Intrinsic::nvvm_wmma_m16n16k8_load_c_f32_row:
4519 case Intrinsic::nvvm_wmma_m16n16k8_load_c_f32_col_stride:
4520 case Intrinsic::nvvm_wmma_m16n16k8_load_c_f32_row_stride: {
4521 Info.opc = ISD::INTRINSIC_W_CHAIN;
4522 Info.memVT = MVT::v8f32;
4523 Info.ptrVal = I.getArgOperand(0);
4524 Info.offset = 0;
4525 Info.flags = MachineMemOperand::MOLoad;
4526 Info.align = Align(16);
4527 Infos.push_back(Info);
4528 return;
4529 }
4530
4531 case Intrinsic::nvvm_wmma_m32n8k16_load_a_bf16_col:
4532 case Intrinsic::nvvm_wmma_m32n8k16_load_a_bf16_col_stride:
4533 case Intrinsic::nvvm_wmma_m32n8k16_load_a_bf16_row:
4534 case Intrinsic::nvvm_wmma_m32n8k16_load_a_bf16_row_stride:
4535
4536 case Intrinsic::nvvm_wmma_m8n32k16_load_b_bf16_col:
4537 case Intrinsic::nvvm_wmma_m8n32k16_load_b_bf16_col_stride:
4538 case Intrinsic::nvvm_wmma_m8n32k16_load_b_bf16_row:
4539 case Intrinsic::nvvm_wmma_m8n32k16_load_b_bf16_row_stride:
4540
4541 case Intrinsic::nvvm_wmma_m16n16k16_load_c_s32_col:
4542 case Intrinsic::nvvm_wmma_m16n16k16_load_c_s32_col_stride:
4543 case Intrinsic::nvvm_wmma_m16n16k16_load_c_s32_row:
4544 case Intrinsic::nvvm_wmma_m16n16k16_load_c_s32_row_stride:
4545 case Intrinsic::nvvm_wmma_m32n8k16_load_c_s32_col:
4546 case Intrinsic::nvvm_wmma_m32n8k16_load_c_s32_col_stride:
4547 case Intrinsic::nvvm_wmma_m32n8k16_load_c_s32_row:
4548 case Intrinsic::nvvm_wmma_m32n8k16_load_c_s32_row_stride:
4549 case Intrinsic::nvvm_wmma_m8n32k16_load_c_s32_col:
4550 case Intrinsic::nvvm_wmma_m8n32k16_load_c_s32_col_stride:
4551 case Intrinsic::nvvm_wmma_m8n32k16_load_c_s32_row:
4552 case Intrinsic::nvvm_wmma_m8n32k16_load_c_s32_row_stride: {
4553 Info.opc = ISD::INTRINSIC_W_CHAIN;
4554 Info.memVT = MVT::v8i32;
4555 Info.ptrVal = I.getArgOperand(0);
4556 Info.offset = 0;
4557 Info.flags = MachineMemOperand::MOLoad;
4558 Info.align = Align(16);
4559 Infos.push_back(Info);
4560 return;
4561 }
4562
4563 case Intrinsic::nvvm_wmma_m8n8k128_load_c_s32_col:
4564 case Intrinsic::nvvm_wmma_m8n8k128_load_c_s32_col_stride:
4565 case Intrinsic::nvvm_wmma_m8n8k128_load_c_s32_row:
4566 case Intrinsic::nvvm_wmma_m8n8k128_load_c_s32_row_stride:
4567 case Intrinsic::nvvm_wmma_m8n8k32_load_c_s32_col:
4568 case Intrinsic::nvvm_wmma_m8n8k32_load_c_s32_col_stride:
4569 case Intrinsic::nvvm_wmma_m8n8k32_load_c_s32_row:
4570 case Intrinsic::nvvm_wmma_m8n8k32_load_c_s32_row_stride:
4571 case Intrinsic::nvvm_ldmatrix_sync_aligned_m8n8_x2_b16:
4572 case Intrinsic::nvvm_ldmatrix_sync_aligned_m8n8_x2_trans_b16:
4573 case Intrinsic::nvvm_ldmatrix_sync_aligned_m16n16_x1_trans_b8:
4574 case Intrinsic::nvvm_ldmatrix_sync_aligned_m16n16_x1_trans_b8x16_b4x16_p64:
4575 case Intrinsic::nvvm_ldmatrix_sync_aligned_m16n16_x1_trans_b8x16_b6x16_p32:
4576 case Intrinsic::nvvm_ldmatrix_sync_aligned_m8n16_x2_b8x16_b4x16_p64:
4577 case Intrinsic::nvvm_ldmatrix_sync_aligned_m8n16_x2_b8x16_b6x16_p32:
4578 case Intrinsic::nvvm_ldmatrix_sync_aligned_m8n16_x2_s8_s4: {
4579 Info.opc = ISD::INTRINSIC_W_CHAIN;
4580 Info.memVT = MVT::v2i32;
4581 Info.ptrVal = I.getArgOperand(0);
4582 Info.offset = 0;
4583 Info.flags = MachineMemOperand::MOLoad;
4584 Info.align = Align(8);
4585 Infos.push_back(Info);
4586 return;
4587 }
4588
4589 case Intrinsic::nvvm_wmma_m8n8k4_load_a_f64_col:
4590 case Intrinsic::nvvm_wmma_m8n8k4_load_a_f64_col_stride:
4591 case Intrinsic::nvvm_wmma_m8n8k4_load_a_f64_row:
4592 case Intrinsic::nvvm_wmma_m8n8k4_load_a_f64_row_stride:
4593
4594 case Intrinsic::nvvm_wmma_m8n8k4_load_b_f64_col:
4595 case Intrinsic::nvvm_wmma_m8n8k4_load_b_f64_col_stride:
4596 case Intrinsic::nvvm_wmma_m8n8k4_load_b_f64_row:
4597 case Intrinsic::nvvm_wmma_m8n8k4_load_b_f64_row_stride: {
4598 Info.opc = ISD::INTRINSIC_W_CHAIN;
4599 Info.memVT = MVT::f64;
4600 Info.ptrVal = I.getArgOperand(0);
4601 Info.offset = 0;
4602 Info.flags = MachineMemOperand::MOLoad;
4603 Info.align = Align(8);
4604 Infos.push_back(Info);
4605 return;
4606 }
4607
4608 case Intrinsic::nvvm_wmma_m8n8k4_load_c_f64_col:
4609 case Intrinsic::nvvm_wmma_m8n8k4_load_c_f64_col_stride:
4610 case Intrinsic::nvvm_wmma_m8n8k4_load_c_f64_row:
4611 case Intrinsic::nvvm_wmma_m8n8k4_load_c_f64_row_stride: {
4612 Info.opc = ISD::INTRINSIC_W_CHAIN;
4613 Info.memVT = MVT::v2f64;
4614 Info.ptrVal = I.getArgOperand(0);
4615 Info.offset = 0;
4616 Info.flags = MachineMemOperand::MOLoad;
4617 Info.align = Align(16);
4618 Infos.push_back(Info);
4619 return;
4620 }
4621
4622 case Intrinsic::nvvm_wmma_m16n16k16_store_d_f16_col:
4623 case Intrinsic::nvvm_wmma_m16n16k16_store_d_f16_row:
4624 case Intrinsic::nvvm_wmma_m16n16k16_store_d_f16_col_stride:
4625 case Intrinsic::nvvm_wmma_m16n16k16_store_d_f16_row_stride:
4626 case Intrinsic::nvvm_wmma_m32n8k16_store_d_f16_col:
4627 case Intrinsic::nvvm_wmma_m32n8k16_store_d_f16_row:
4628 case Intrinsic::nvvm_wmma_m32n8k16_store_d_f16_col_stride:
4629 case Intrinsic::nvvm_wmma_m32n8k16_store_d_f16_row_stride:
4630 case Intrinsic::nvvm_wmma_m8n32k16_store_d_f16_col:
4631 case Intrinsic::nvvm_wmma_m8n32k16_store_d_f16_row:
4632 case Intrinsic::nvvm_wmma_m8n32k16_store_d_f16_col_stride:
4633 case Intrinsic::nvvm_wmma_m8n32k16_store_d_f16_row_stride: {
4634 Info.opc = ISD::INTRINSIC_VOID;
4635 Info.memVT = MVT::v4f16;
4636 Info.ptrVal = I.getArgOperand(0);
4637 Info.offset = 0;
4638 Info.flags = MachineMemOperand::MOStore;
4639 Info.align = Align(16);
4640 Infos.push_back(Info);
4641 return;
4642 }
4643
4644 case Intrinsic::nvvm_wmma_m16n16k16_store_d_f32_col:
4645 case Intrinsic::nvvm_wmma_m16n16k16_store_d_f32_row:
4646 case Intrinsic::nvvm_wmma_m16n16k16_store_d_f32_col_stride:
4647 case Intrinsic::nvvm_wmma_m16n16k16_store_d_f32_row_stride:
4648 case Intrinsic::nvvm_wmma_m32n8k16_store_d_f32_col:
4649 case Intrinsic::nvvm_wmma_m32n8k16_store_d_f32_row:
4650 case Intrinsic::nvvm_wmma_m32n8k16_store_d_f32_col_stride:
4651 case Intrinsic::nvvm_wmma_m32n8k16_store_d_f32_row_stride:
4652 case Intrinsic::nvvm_wmma_m8n32k16_store_d_f32_col:
4653 case Intrinsic::nvvm_wmma_m8n32k16_store_d_f32_row:
4654 case Intrinsic::nvvm_wmma_m8n32k16_store_d_f32_col_stride:
4655 case Intrinsic::nvvm_wmma_m8n32k16_store_d_f32_row_stride:
4656 case Intrinsic::nvvm_wmma_m16n16k8_store_d_f32_col:
4657 case Intrinsic::nvvm_wmma_m16n16k8_store_d_f32_row:
4658 case Intrinsic::nvvm_wmma_m16n16k8_store_d_f32_col_stride:
4659 case Intrinsic::nvvm_wmma_m16n16k8_store_d_f32_row_stride: {
4660 Info.opc = ISD::INTRINSIC_VOID;
4661 Info.memVT = MVT::v8f32;
4662 Info.ptrVal = I.getArgOperand(0);
4663 Info.offset = 0;
4664 Info.flags = MachineMemOperand::MOStore;
4665 Info.align = Align(16);
4666 Infos.push_back(Info);
4667 return;
4668 }
4669
4670 case Intrinsic::nvvm_wmma_m16n16k16_store_d_s32_col:
4671 case Intrinsic::nvvm_wmma_m16n16k16_store_d_s32_col_stride:
4672 case Intrinsic::nvvm_wmma_m16n16k16_store_d_s32_row:
4673 case Intrinsic::nvvm_wmma_m16n16k16_store_d_s32_row_stride:
4674 case Intrinsic::nvvm_wmma_m32n8k16_store_d_s32_col:
4675 case Intrinsic::nvvm_wmma_m32n8k16_store_d_s32_col_stride:
4676 case Intrinsic::nvvm_wmma_m32n8k16_store_d_s32_row:
4677 case Intrinsic::nvvm_wmma_m32n8k16_store_d_s32_row_stride:
4678 case Intrinsic::nvvm_wmma_m8n32k16_store_d_s32_col:
4679 case Intrinsic::nvvm_wmma_m8n32k16_store_d_s32_col_stride:
4680 case Intrinsic::nvvm_wmma_m8n32k16_store_d_s32_row:
4681 case Intrinsic::nvvm_wmma_m8n32k16_store_d_s32_row_stride: {
4682 Info.opc = ISD::INTRINSIC_VOID;
4683 Info.memVT = MVT::v8i32;
4684 Info.ptrVal = I.getArgOperand(0);
4685 Info.offset = 0;
4686 Info.flags = MachineMemOperand::MOStore;
4687 Info.align = Align(16);
4688 Infos.push_back(Info);
4689 return;
4690 }
4691
4692 case Intrinsic::nvvm_wmma_m8n8k128_store_d_s32_col:
4693 case Intrinsic::nvvm_wmma_m8n8k128_store_d_s32_col_stride:
4694 case Intrinsic::nvvm_wmma_m8n8k128_store_d_s32_row:
4695 case Intrinsic::nvvm_wmma_m8n8k128_store_d_s32_row_stride:
4696 case Intrinsic::nvvm_wmma_m8n8k32_store_d_s32_col:
4697 case Intrinsic::nvvm_wmma_m8n8k32_store_d_s32_col_stride:
4698 case Intrinsic::nvvm_wmma_m8n8k32_store_d_s32_row:
4699 case Intrinsic::nvvm_wmma_m8n8k32_store_d_s32_row_stride:
4700 case Intrinsic::nvvm_stmatrix_sync_aligned_m8n8_x2_b16:
4701 case Intrinsic::nvvm_stmatrix_sync_aligned_m8n8_x2_trans_b16:
4702 case Intrinsic::nvvm_stmatrix_sync_aligned_m16n8_x2_trans_b8: {
4703 Info.opc = ISD::INTRINSIC_VOID;
4704 Info.memVT = MVT::v2i32;
4705 Info.ptrVal = I.getArgOperand(0);
4706 Info.offset = 0;
4707 Info.flags = MachineMemOperand::MOStore;
4708 Info.align = Align(8);
4709 Infos.push_back(Info);
4710 return;
4711 }
4712
4713 case Intrinsic::nvvm_wmma_m8n8k4_store_d_f64_col:
4714 case Intrinsic::nvvm_wmma_m8n8k4_store_d_f64_col_stride:
4715 case Intrinsic::nvvm_wmma_m8n8k4_store_d_f64_row:
4716 case Intrinsic::nvvm_wmma_m8n8k4_store_d_f64_row_stride: {
4717 Info.opc = ISD::INTRINSIC_VOID;
4718 Info.memVT = MVT::v2f64;
4719 Info.ptrVal = I.getArgOperand(0);
4720 Info.offset = 0;
4721 Info.flags = MachineMemOperand::MOStore;
4722 Info.align = Align(16);
4723 Infos.push_back(Info);
4724 return;
4725 }
4726
4727 case Intrinsic::nvvm_stmatrix_sync_aligned_m8n8_x1_b16:
4728 case Intrinsic::nvvm_stmatrix_sync_aligned_m8n8_x1_trans_b16:
4729 case Intrinsic::nvvm_stmatrix_sync_aligned_m16n8_x1_trans_b8: {
4730 Info.opc = ISD::INTRINSIC_VOID;
4731 Info.memVT = MVT::i32;
4732 Info.ptrVal = I.getArgOperand(0);
4733 Info.offset = 0;
4734 Info.flags = MachineMemOperand::MOStore;
4735 Info.align = Align(4);
4736 Infos.push_back(Info);
4737 return;
4738 }
4739
4740 case Intrinsic::nvvm_stmatrix_sync_aligned_m8n8_x4_b16:
4741 case Intrinsic::nvvm_stmatrix_sync_aligned_m8n8_x4_trans_b16:
4742 case Intrinsic::nvvm_stmatrix_sync_aligned_m16n8_x4_trans_b8: {
4743 Info.opc = ISD::INTRINSIC_VOID;
4744 Info.memVT = MVT::v4i32;
4745 Info.ptrVal = I.getArgOperand(0);
4746 Info.offset = 0;
4747 Info.flags = MachineMemOperand::MOStore;
4748 Info.align = Align(16);
4749 Infos.push_back(Info);
4750 return;
4751 }
4752
4753 case Intrinsic::nvvm_st_bulk: {
4754 Value *Dst = I.getArgOperand(0);
4755 Value *Val = I.getArgOperand(2);
4756 Info.opc = ISD::INTRINSIC_VOID;
4757 Info.memVT = MVT::getVT(Val->getType());
4758 Info.ptrVal = Dst;
4759 Info.offset = 0;
4760 Info.flags = MachineMemOperand::MOStore;
4761 Info.align.reset();
4762 Info.size = MemoryLocation::UnknownSize;
4763 Infos.push_back(Info);
4764 return;
4765 }
4766
4767 case Intrinsic::nvvm_prefetch_tensormap: {
4768 auto &DL = I.getDataLayout();
4769 Info.opc = ISD::INTRINSIC_VOID;
4770 Info.memVT = getPointerTy(DL);
4771 Info.ptrVal = I.getArgOperand(0);
4772 Info.offset = 0;
4773 Info.flags =
4775 Info.align.reset();
4776 Infos.push_back(Info);
4777 return;
4778 }
4779
4780 case Intrinsic::nvvm_mbarrier_init: {
4781 Info.opc = ISD::INTRINSIC_VOID;
4782 Info.memVT = MVT::i64;
4783 Info.ptrVal = I.getArgOperand(0);
4784 Info.offset = 0;
4785 Info.flags = MachineMemOperand::MOStore;
4786 Info.align = Align(8);
4787 Infos.push_back(Info);
4788 return;
4789 }
4790
4791 case Intrinsic::nvvm_mbarrier_check_layout: {
4792 Info.opc = ISD::INTRINSIC_W_CHAIN;
4793 Info.memVT = MVT::i64;
4794 Info.ptrVal = I.getArgOperand(0);
4795 Info.offset = 0;
4796 Info.flags = MachineMemOperand::MOLoad;
4797 Info.align = Align(8);
4798 Infos.push_back(Info);
4799 return;
4800 }
4801
4802 case Intrinsic::nvvm_tensormap_replace_global_address:
4803 case Intrinsic::nvvm_tensormap_replace_global_stride: {
4804 Info.opc = ISD::INTRINSIC_VOID;
4805 Info.memVT = MVT::i64;
4806 Info.ptrVal = I.getArgOperand(0);
4807 Info.offset = 0;
4808 Info.flags = MachineMemOperand::MOStore;
4809 Info.align.reset();
4810 Infos.push_back(Info);
4811 return;
4812 }
4813
4814 case Intrinsic::nvvm_tensormap_replace_rank:
4815 case Intrinsic::nvvm_tensormap_replace_box_dim:
4816 case Intrinsic::nvvm_tensormap_replace_global_dim:
4817 case Intrinsic::nvvm_tensormap_replace_element_stride:
4818 case Intrinsic::nvvm_tensormap_replace_elemtype:
4819 case Intrinsic::nvvm_tensormap_replace_interleave_layout:
4820 case Intrinsic::nvvm_tensormap_replace_swizzle_mode:
4821 case Intrinsic::nvvm_tensormap_replace_swizzle_atomicity:
4822 case Intrinsic::nvvm_tensormap_replace_fill_mode: {
4823 Info.opc = ISD::INTRINSIC_VOID;
4824 Info.memVT = MVT::i32;
4825 Info.ptrVal = I.getArgOperand(0);
4826 Info.offset = 0;
4827 Info.flags = MachineMemOperand::MOStore;
4828 Info.align.reset();
4829 Infos.push_back(Info);
4830 return;
4831 }
4832
4833 case Intrinsic::nvvm_ldu_global_i:
4834 case Intrinsic::nvvm_ldu_global_f:
4835 case Intrinsic::nvvm_ldu_global_p: {
4836 Info.opc = ISD::INTRINSIC_W_CHAIN;
4837 Info.memVT = getValueType(I.getDataLayout(), I.getType());
4838 Info.ptrVal = I.getArgOperand(0);
4839 Info.offset = 0;
4840 Info.flags = MachineMemOperand::MOLoad;
4841 Info.align = cast<ConstantInt>(I.getArgOperand(1))->getMaybeAlignValue();
4842
4843 Infos.push_back(Info);
4844 return;
4845 }
4846 case Intrinsic::nvvm_tex_1d_v4f32_s32:
4847 case Intrinsic::nvvm_tex_1d_v4f32_f32:
4848 case Intrinsic::nvvm_tex_1d_level_v4f32_f32:
4849 case Intrinsic::nvvm_tex_1d_grad_v4f32_f32:
4850 case Intrinsic::nvvm_tex_1d_array_v4f32_s32:
4851 case Intrinsic::nvvm_tex_1d_array_v4f32_f32:
4852 case Intrinsic::nvvm_tex_1d_array_level_v4f32_f32:
4853 case Intrinsic::nvvm_tex_1d_array_grad_v4f32_f32:
4854 case Intrinsic::nvvm_tex_2d_v4f32_s32:
4855 case Intrinsic::nvvm_tex_2d_v4f32_f32:
4856 case Intrinsic::nvvm_tex_2d_level_v4f32_f32:
4857 case Intrinsic::nvvm_tex_2d_grad_v4f32_f32:
4858 case Intrinsic::nvvm_tex_2d_array_v4f32_s32:
4859 case Intrinsic::nvvm_tex_2d_array_v4f32_f32:
4860 case Intrinsic::nvvm_tex_2d_array_level_v4f32_f32:
4861 case Intrinsic::nvvm_tex_2d_array_grad_v4f32_f32:
4862 case Intrinsic::nvvm_tex_3d_v4f32_s32:
4863 case Intrinsic::nvvm_tex_3d_v4f32_f32:
4864 case Intrinsic::nvvm_tex_3d_level_v4f32_f32:
4865 case Intrinsic::nvvm_tex_3d_grad_v4f32_f32:
4866 case Intrinsic::nvvm_tex_cube_v4f32_f32:
4867 case Intrinsic::nvvm_tex_cube_level_v4f32_f32:
4868 case Intrinsic::nvvm_tex_cube_array_v4f32_f32:
4869 case Intrinsic::nvvm_tex_cube_array_level_v4f32_f32:
4870 case Intrinsic::nvvm_tld4_r_2d_v4f32_f32:
4871 case Intrinsic::nvvm_tld4_g_2d_v4f32_f32:
4872 case Intrinsic::nvvm_tld4_b_2d_v4f32_f32:
4873 case Intrinsic::nvvm_tld4_a_2d_v4f32_f32:
4874 case Intrinsic::nvvm_tex_unified_1d_v4f32_s32:
4875 case Intrinsic::nvvm_tex_unified_1d_v4f32_f32:
4876 case Intrinsic::nvvm_tex_unified_1d_level_v4f32_f32:
4877 case Intrinsic::nvvm_tex_unified_1d_grad_v4f32_f32:
4878 case Intrinsic::nvvm_tex_unified_1d_array_v4f32_s32:
4879 case Intrinsic::nvvm_tex_unified_1d_array_v4f32_f32:
4880 case Intrinsic::nvvm_tex_unified_1d_array_level_v4f32_f32:
4881 case Intrinsic::nvvm_tex_unified_1d_array_grad_v4f32_f32:
4882 case Intrinsic::nvvm_tex_unified_2d_v4f32_s32:
4883 case Intrinsic::nvvm_tex_unified_2d_v4f32_f32:
4884 case Intrinsic::nvvm_tex_unified_2d_level_v4f32_f32:
4885 case Intrinsic::nvvm_tex_unified_2d_grad_v4f32_f32:
4886 case Intrinsic::nvvm_tex_unified_2d_array_v4f32_s32:
4887 case Intrinsic::nvvm_tex_unified_2d_array_v4f32_f32:
4888 case Intrinsic::nvvm_tex_unified_2d_array_level_v4f32_f32:
4889 case Intrinsic::nvvm_tex_unified_2d_array_grad_v4f32_f32:
4890 case Intrinsic::nvvm_tex_unified_3d_v4f32_s32:
4891 case Intrinsic::nvvm_tex_unified_3d_v4f32_f32:
4892 case Intrinsic::nvvm_tex_unified_3d_level_v4f32_f32:
4893 case Intrinsic::nvvm_tex_unified_3d_grad_v4f32_f32:
4894 case Intrinsic::nvvm_tex_unified_cube_v4f32_f32:
4895 case Intrinsic::nvvm_tex_unified_cube_level_v4f32_f32:
4896 case Intrinsic::nvvm_tex_unified_cube_array_v4f32_f32:
4897 case Intrinsic::nvvm_tex_unified_cube_array_level_v4f32_f32:
4898 case Intrinsic::nvvm_tex_unified_cube_grad_v4f32_f32:
4899 case Intrinsic::nvvm_tex_unified_cube_array_grad_v4f32_f32:
4900 case Intrinsic::nvvm_tld4_unified_r_2d_v4f32_f32:
4901 case Intrinsic::nvvm_tld4_unified_g_2d_v4f32_f32:
4902 case Intrinsic::nvvm_tld4_unified_b_2d_v4f32_f32:
4903 case Intrinsic::nvvm_tld4_unified_a_2d_v4f32_f32:
4904 Info.opc = ISD::INTRINSIC_W_CHAIN;
4905 Info.memVT = MVT::v4f32;
4906 Info.ptrVal = nullptr;
4907 Info.offset = 0;
4908 Info.flags = MachineMemOperand::MOLoad;
4909 Info.align = Align(16);
4910 Infos.push_back(Info);
4911 return;
4912
4913 case Intrinsic::nvvm_tex_1d_v4s32_s32:
4914 case Intrinsic::nvvm_tex_1d_v4s32_f32:
4915 case Intrinsic::nvvm_tex_1d_level_v4s32_f32:
4916 case Intrinsic::nvvm_tex_1d_grad_v4s32_f32:
4917 case Intrinsic::nvvm_tex_1d_array_v4s32_s32:
4918 case Intrinsic::nvvm_tex_1d_array_v4s32_f32:
4919 case Intrinsic::nvvm_tex_1d_array_level_v4s32_f32:
4920 case Intrinsic::nvvm_tex_1d_array_grad_v4s32_f32:
4921 case Intrinsic::nvvm_tex_2d_v4s32_s32:
4922 case Intrinsic::nvvm_tex_2d_v4s32_f32:
4923 case Intrinsic::nvvm_tex_2d_level_v4s32_f32:
4924 case Intrinsic::nvvm_tex_2d_grad_v4s32_f32:
4925 case Intrinsic::nvvm_tex_2d_array_v4s32_s32:
4926 case Intrinsic::nvvm_tex_2d_array_v4s32_f32:
4927 case Intrinsic::nvvm_tex_2d_array_level_v4s32_f32:
4928 case Intrinsic::nvvm_tex_2d_array_grad_v4s32_f32:
4929 case Intrinsic::nvvm_tex_3d_v4s32_s32:
4930 case Intrinsic::nvvm_tex_3d_v4s32_f32:
4931 case Intrinsic::nvvm_tex_3d_level_v4s32_f32:
4932 case Intrinsic::nvvm_tex_3d_grad_v4s32_f32:
4933 case Intrinsic::nvvm_tex_cube_v4s32_f32:
4934 case Intrinsic::nvvm_tex_cube_level_v4s32_f32:
4935 case Intrinsic::nvvm_tex_cube_array_v4s32_f32:
4936 case Intrinsic::nvvm_tex_cube_array_level_v4s32_f32:
4937 case Intrinsic::nvvm_tex_cube_v4u32_f32:
4938 case Intrinsic::nvvm_tex_cube_level_v4u32_f32:
4939 case Intrinsic::nvvm_tex_cube_array_v4u32_f32:
4940 case Intrinsic::nvvm_tex_cube_array_level_v4u32_f32:
4941 case Intrinsic::nvvm_tex_1d_v4u32_s32:
4942 case Intrinsic::nvvm_tex_1d_v4u32_f32:
4943 case Intrinsic::nvvm_tex_1d_level_v4u32_f32:
4944 case Intrinsic::nvvm_tex_1d_grad_v4u32_f32:
4945 case Intrinsic::nvvm_tex_1d_array_v4u32_s32:
4946 case Intrinsic::nvvm_tex_1d_array_v4u32_f32:
4947 case Intrinsic::nvvm_tex_1d_array_level_v4u32_f32:
4948 case Intrinsic::nvvm_tex_1d_array_grad_v4u32_f32:
4949 case Intrinsic::nvvm_tex_2d_v4u32_s32:
4950 case Intrinsic::nvvm_tex_2d_v4u32_f32:
4951 case Intrinsic::nvvm_tex_2d_level_v4u32_f32:
4952 case Intrinsic::nvvm_tex_2d_grad_v4u32_f32:
4953 case Intrinsic::nvvm_tex_2d_array_v4u32_s32:
4954 case Intrinsic::nvvm_tex_2d_array_v4u32_f32:
4955 case Intrinsic::nvvm_tex_2d_array_level_v4u32_f32:
4956 case Intrinsic::nvvm_tex_2d_array_grad_v4u32_f32:
4957 case Intrinsic::nvvm_tex_3d_v4u32_s32:
4958 case Intrinsic::nvvm_tex_3d_v4u32_f32:
4959 case Intrinsic::nvvm_tex_3d_level_v4u32_f32:
4960 case Intrinsic::nvvm_tex_3d_grad_v4u32_f32:
4961 case Intrinsic::nvvm_tld4_r_2d_v4s32_f32:
4962 case Intrinsic::nvvm_tld4_g_2d_v4s32_f32:
4963 case Intrinsic::nvvm_tld4_b_2d_v4s32_f32:
4964 case Intrinsic::nvvm_tld4_a_2d_v4s32_f32:
4965 case Intrinsic::nvvm_tld4_r_2d_v4u32_f32:
4966 case Intrinsic::nvvm_tld4_g_2d_v4u32_f32:
4967 case Intrinsic::nvvm_tld4_b_2d_v4u32_f32:
4968 case Intrinsic::nvvm_tld4_a_2d_v4u32_f32:
4969 case Intrinsic::nvvm_tex_unified_1d_v4s32_s32:
4970 case Intrinsic::nvvm_tex_unified_1d_v4s32_f32:
4971 case Intrinsic::nvvm_tex_unified_1d_level_v4s32_f32:
4972 case Intrinsic::nvvm_tex_unified_1d_grad_v4s32_f32:
4973 case Intrinsic::nvvm_tex_unified_1d_array_v4s32_s32:
4974 case Intrinsic::nvvm_tex_unified_1d_array_v4s32_f32:
4975 case Intrinsic::nvvm_tex_unified_1d_array_level_v4s32_f32:
4976 case Intrinsic::nvvm_tex_unified_1d_array_grad_v4s32_f32:
4977 case Intrinsic::nvvm_tex_unified_2d_v4s32_s32:
4978 case Intrinsic::nvvm_tex_unified_2d_v4s32_f32:
4979 case Intrinsic::nvvm_tex_unified_2d_level_v4s32_f32:
4980 case Intrinsic::nvvm_tex_unified_2d_grad_v4s32_f32:
4981 case Intrinsic::nvvm_tex_unified_2d_array_v4s32_s32:
4982 case Intrinsic::nvvm_tex_unified_2d_array_v4s32_f32:
4983 case Intrinsic::nvvm_tex_unified_2d_array_level_v4s32_f32:
4984 case Intrinsic::nvvm_tex_unified_2d_array_grad_v4s32_f32:
4985 case Intrinsic::nvvm_tex_unified_3d_v4s32_s32:
4986 case Intrinsic::nvvm_tex_unified_3d_v4s32_f32:
4987 case Intrinsic::nvvm_tex_unified_3d_level_v4s32_f32:
4988 case Intrinsic::nvvm_tex_unified_3d_grad_v4s32_f32:
4989 case Intrinsic::nvvm_tex_unified_1d_v4u32_s32:
4990 case Intrinsic::nvvm_tex_unified_1d_v4u32_f32:
4991 case Intrinsic::nvvm_tex_unified_1d_level_v4u32_f32:
4992 case Intrinsic::nvvm_tex_unified_1d_grad_v4u32_f32:
4993 case Intrinsic::nvvm_tex_unified_1d_array_v4u32_s32:
4994 case Intrinsic::nvvm_tex_unified_1d_array_v4u32_f32:
4995 case Intrinsic::nvvm_tex_unified_1d_array_level_v4u32_f32:
4996 case Intrinsic::nvvm_tex_unified_1d_array_grad_v4u32_f32:
4997 case Intrinsic::nvvm_tex_unified_2d_v4u32_s32:
4998 case Intrinsic::nvvm_tex_unified_2d_v4u32_f32:
4999 case Intrinsic::nvvm_tex_unified_2d_level_v4u32_f32:
5000 case Intrinsic::nvvm_tex_unified_2d_grad_v4u32_f32:
5001 case Intrinsic::nvvm_tex_unified_2d_array_v4u32_s32:
5002 case Intrinsic::nvvm_tex_unified_2d_array_v4u32_f32:
5003 case Intrinsic::nvvm_tex_unified_2d_array_level_v4u32_f32:
5004 case Intrinsic::nvvm_tex_unified_2d_array_grad_v4u32_f32:
5005 case Intrinsic::nvvm_tex_unified_3d_v4u32_s32:
5006 case Intrinsic::nvvm_tex_unified_3d_v4u32_f32:
5007 case Intrinsic::nvvm_tex_unified_3d_level_v4u32_f32:
5008 case Intrinsic::nvvm_tex_unified_3d_grad_v4u32_f32:
5009 case Intrinsic::nvvm_tex_unified_cube_v4s32_f32:
5010 case Intrinsic::nvvm_tex_unified_cube_level_v4s32_f32:
5011 case Intrinsic::nvvm_tex_unified_cube_array_v4s32_f32:
5012 case Intrinsic::nvvm_tex_unified_cube_array_level_v4s32_f32:
5013 case Intrinsic::nvvm_tex_unified_cube_v4u32_f32:
5014 case Intrinsic::nvvm_tex_unified_cube_level_v4u32_f32:
5015 case Intrinsic::nvvm_tex_unified_cube_array_v4u32_f32:
5016 case Intrinsic::nvvm_tex_unified_cube_array_level_v4u32_f32:
5017 case Intrinsic::nvvm_tex_unified_cube_grad_v4s32_f32:
5018 case Intrinsic::nvvm_tex_unified_cube_grad_v4u32_f32:
5019 case Intrinsic::nvvm_tex_unified_cube_array_grad_v4s32_f32:
5020 case Intrinsic::nvvm_tex_unified_cube_array_grad_v4u32_f32:
5021 case Intrinsic::nvvm_tld4_unified_r_2d_v4s32_f32:
5022 case Intrinsic::nvvm_tld4_unified_g_2d_v4s32_f32:
5023 case Intrinsic::nvvm_tld4_unified_b_2d_v4s32_f32:
5024 case Intrinsic::nvvm_tld4_unified_a_2d_v4s32_f32:
5025 case Intrinsic::nvvm_tld4_unified_r_2d_v4u32_f32:
5026 case Intrinsic::nvvm_tld4_unified_g_2d_v4u32_f32:
5027 case Intrinsic::nvvm_tld4_unified_b_2d_v4u32_f32:
5028 case Intrinsic::nvvm_tld4_unified_a_2d_v4u32_f32:
5029 Info.opc = ISD::INTRINSIC_W_CHAIN;
5030 Info.memVT = MVT::v4i32;
5031 Info.ptrVal = nullptr;
5032 Info.offset = 0;
5033 Info.flags = MachineMemOperand::MOLoad;
5034 Info.align = Align(16);
5035 Infos.push_back(Info);
5036 return;
5037
5038 case Intrinsic::nvvm_suld_1d_i8_clamp:
5039 case Intrinsic::nvvm_suld_1d_v2i8_clamp:
5040 case Intrinsic::nvvm_suld_1d_v4i8_clamp:
5041 case Intrinsic::nvvm_suld_1d_array_i8_clamp:
5042 case Intrinsic::nvvm_suld_1d_array_v2i8_clamp:
5043 case Intrinsic::nvvm_suld_1d_array_v4i8_clamp:
5044 case Intrinsic::nvvm_suld_2d_i8_clamp:
5045 case Intrinsic::nvvm_suld_2d_v2i8_clamp:
5046 case Intrinsic::nvvm_suld_2d_v4i8_clamp:
5047 case Intrinsic::nvvm_suld_2d_array_i8_clamp:
5048 case Intrinsic::nvvm_suld_2d_array_v2i8_clamp:
5049 case Intrinsic::nvvm_suld_2d_array_v4i8_clamp:
5050 case Intrinsic::nvvm_suld_3d_i8_clamp:
5051 case Intrinsic::nvvm_suld_3d_v2i8_clamp:
5052 case Intrinsic::nvvm_suld_3d_v4i8_clamp:
5053 case Intrinsic::nvvm_suld_1d_i8_trap:
5054 case Intrinsic::nvvm_suld_1d_v2i8_trap:
5055 case Intrinsic::nvvm_suld_1d_v4i8_trap:
5056 case Intrinsic::nvvm_suld_1d_array_i8_trap:
5057 case Intrinsic::nvvm_suld_1d_array_v2i8_trap:
5058 case Intrinsic::nvvm_suld_1d_array_v4i8_trap:
5059 case Intrinsic::nvvm_suld_2d_i8_trap:
5060 case Intrinsic::nvvm_suld_2d_v2i8_trap:
5061 case Intrinsic::nvvm_suld_2d_v4i8_trap:
5062 case Intrinsic::nvvm_suld_2d_array_i8_trap:
5063 case Intrinsic::nvvm_suld_2d_array_v2i8_trap:
5064 case Intrinsic::nvvm_suld_2d_array_v4i8_trap:
5065 case Intrinsic::nvvm_suld_3d_i8_trap:
5066 case Intrinsic::nvvm_suld_3d_v2i8_trap:
5067 case Intrinsic::nvvm_suld_3d_v4i8_trap:
5068 case Intrinsic::nvvm_suld_1d_i8_zero:
5069 case Intrinsic::nvvm_suld_1d_v2i8_zero:
5070 case Intrinsic::nvvm_suld_1d_v4i8_zero:
5071 case Intrinsic::nvvm_suld_1d_array_i8_zero:
5072 case Intrinsic::nvvm_suld_1d_array_v2i8_zero:
5073 case Intrinsic::nvvm_suld_1d_array_v4i8_zero:
5074 case Intrinsic::nvvm_suld_2d_i8_zero:
5075 case Intrinsic::nvvm_suld_2d_v2i8_zero:
5076 case Intrinsic::nvvm_suld_2d_v4i8_zero:
5077 case Intrinsic::nvvm_suld_2d_array_i8_zero:
5078 case Intrinsic::nvvm_suld_2d_array_v2i8_zero:
5079 case Intrinsic::nvvm_suld_2d_array_v4i8_zero:
5080 case Intrinsic::nvvm_suld_3d_i8_zero:
5081 case Intrinsic::nvvm_suld_3d_v2i8_zero:
5082 case Intrinsic::nvvm_suld_3d_v4i8_zero:
5083 Info.opc = ISD::INTRINSIC_W_CHAIN;
5084 Info.memVT = MVT::i8;
5085 Info.ptrVal = nullptr;
5086 Info.offset = 0;
5087 Info.flags = MachineMemOperand::MOLoad;
5088 Info.align = Align(16);
5089 Infos.push_back(Info);
5090 return;
5091
5092 case Intrinsic::nvvm_suld_1d_i16_clamp:
5093 case Intrinsic::nvvm_suld_1d_v2i16_clamp:
5094 case Intrinsic::nvvm_suld_1d_v4i16_clamp:
5095 case Intrinsic::nvvm_suld_1d_array_i16_clamp:
5096 case Intrinsic::nvvm_suld_1d_array_v2i16_clamp:
5097 case Intrinsic::nvvm_suld_1d_array_v4i16_clamp:
5098 case Intrinsic::nvvm_suld_2d_i16_clamp:
5099 case Intrinsic::nvvm_suld_2d_v2i16_clamp:
5100 case Intrinsic::nvvm_suld_2d_v4i16_clamp:
5101 case Intrinsic::nvvm_suld_2d_array_i16_clamp:
5102 case Intrinsic::nvvm_suld_2d_array_v2i16_clamp:
5103 case Intrinsic::nvvm_suld_2d_array_v4i16_clamp:
5104 case Intrinsic::nvvm_suld_3d_i16_clamp:
5105 case Intrinsic::nvvm_suld_3d_v2i16_clamp:
5106 case Intrinsic::nvvm_suld_3d_v4i16_clamp:
5107 case Intrinsic::nvvm_suld_1d_i16_trap:
5108 case Intrinsic::nvvm_suld_1d_v2i16_trap:
5109 case Intrinsic::nvvm_suld_1d_v4i16_trap:
5110 case Intrinsic::nvvm_suld_1d_array_i16_trap:
5111 case Intrinsic::nvvm_suld_1d_array_v2i16_trap:
5112 case Intrinsic::nvvm_suld_1d_array_v4i16_trap:
5113 case Intrinsic::nvvm_suld_2d_i16_trap:
5114 case Intrinsic::nvvm_suld_2d_v2i16_trap:
5115 case Intrinsic::nvvm_suld_2d_v4i16_trap:
5116 case Intrinsic::nvvm_suld_2d_array_i16_trap:
5117 case Intrinsic::nvvm_suld_2d_array_v2i16_trap:
5118 case Intrinsic::nvvm_suld_2d_array_v4i16_trap:
5119 case Intrinsic::nvvm_suld_3d_i16_trap:
5120 case Intrinsic::nvvm_suld_3d_v2i16_trap:
5121 case Intrinsic::nvvm_suld_3d_v4i16_trap:
5122 case Intrinsic::nvvm_suld_1d_i16_zero:
5123 case Intrinsic::nvvm_suld_1d_v2i16_zero:
5124 case Intrinsic::nvvm_suld_1d_v4i16_zero:
5125 case Intrinsic::nvvm_suld_1d_array_i16_zero:
5126 case Intrinsic::nvvm_suld_1d_array_v2i16_zero:
5127 case Intrinsic::nvvm_suld_1d_array_v4i16_zero:
5128 case Intrinsic::nvvm_suld_2d_i16_zero:
5129 case Intrinsic::nvvm_suld_2d_v2i16_zero:
5130 case Intrinsic::nvvm_suld_2d_v4i16_zero:
5131 case Intrinsic::nvvm_suld_2d_array_i16_zero:
5132 case Intrinsic::nvvm_suld_2d_array_v2i16_zero:
5133 case Intrinsic::nvvm_suld_2d_array_v4i16_zero:
5134 case Intrinsic::nvvm_suld_3d_i16_zero:
5135 case Intrinsic::nvvm_suld_3d_v2i16_zero:
5136 case Intrinsic::nvvm_suld_3d_v4i16_zero:
5137 Info.opc = ISD::INTRINSIC_W_CHAIN;
5138 Info.memVT = MVT::i16;
5139 Info.ptrVal = nullptr;
5140 Info.offset = 0;
5141 Info.flags = MachineMemOperand::MOLoad;
5142 Info.align = Align(16);
5143 Infos.push_back(Info);
5144 return;
5145
5146 case Intrinsic::nvvm_suld_1d_i32_clamp:
5147 case Intrinsic::nvvm_suld_1d_v2i32_clamp:
5148 case Intrinsic::nvvm_suld_1d_v4i32_clamp:
5149 case Intrinsic::nvvm_suld_1d_array_i32_clamp:
5150 case Intrinsic::nvvm_suld_1d_array_v2i32_clamp:
5151 case Intrinsic::nvvm_suld_1d_array_v4i32_clamp:
5152 case Intrinsic::nvvm_suld_2d_i32_clamp:
5153 case Intrinsic::nvvm_suld_2d_v2i32_clamp:
5154 case Intrinsic::nvvm_suld_2d_v4i32_clamp:
5155 case Intrinsic::nvvm_suld_2d_array_i32_clamp:
5156 case Intrinsic::nvvm_suld_2d_array_v2i32_clamp:
5157 case Intrinsic::nvvm_suld_2d_array_v4i32_clamp:
5158 case Intrinsic::nvvm_suld_3d_i32_clamp:
5159 case Intrinsic::nvvm_suld_3d_v2i32_clamp:
5160 case Intrinsic::nvvm_suld_3d_v4i32_clamp:
5161 case Intrinsic::nvvm_suld_1d_i32_trap:
5162 case Intrinsic::nvvm_suld_1d_v2i32_trap:
5163 case Intrinsic::nvvm_suld_1d_v4i32_trap:
5164 case Intrinsic::nvvm_suld_1d_array_i32_trap:
5165 case Intrinsic::nvvm_suld_1d_array_v2i32_trap:
5166 case Intrinsic::nvvm_suld_1d_array_v4i32_trap:
5167 case Intrinsic::nvvm_suld_2d_i32_trap:
5168 case Intrinsic::nvvm_suld_2d_v2i32_trap:
5169 case Intrinsic::nvvm_suld_2d_v4i32_trap:
5170 case Intrinsic::nvvm_suld_2d_array_i32_trap:
5171 case Intrinsic::nvvm_suld_2d_array_v2i32_trap:
5172 case Intrinsic::nvvm_suld_2d_array_v4i32_trap:
5173 case Intrinsic::nvvm_suld_3d_i32_trap:
5174 case Intrinsic::nvvm_suld_3d_v2i32_trap:
5175 case Intrinsic::nvvm_suld_3d_v4i32_trap:
5176 case Intrinsic::nvvm_suld_1d_i32_zero:
5177 case Intrinsic::nvvm_suld_1d_v2i32_zero:
5178 case Intrinsic::nvvm_suld_1d_v4i32_zero:
5179 case Intrinsic::nvvm_suld_1d_array_i32_zero:
5180 case Intrinsic::nvvm_suld_1d_array_v2i32_zero:
5181 case Intrinsic::nvvm_suld_1d_array_v4i32_zero:
5182 case Intrinsic::nvvm_suld_2d_i32_zero:
5183 case Intrinsic::nvvm_suld_2d_v2i32_zero:
5184 case Intrinsic::nvvm_suld_2d_v4i32_zero:
5185 case Intrinsic::nvvm_suld_2d_array_i32_zero:
5186 case Intrinsic::nvvm_suld_2d_array_v2i32_zero:
5187 case Intrinsic::nvvm_suld_2d_array_v4i32_zero:
5188 case Intrinsic::nvvm_suld_3d_i32_zero:
5189 case Intrinsic::nvvm_suld_3d_v2i32_zero:
5190 case Intrinsic::nvvm_suld_3d_v4i32_zero:
5191 Info.opc = ISD::INTRINSIC_W_CHAIN;
5192 Info.memVT = MVT::i32;
5193 Info.ptrVal = nullptr;
5194 Info.offset = 0;
5195 Info.flags = MachineMemOperand::MOLoad;
5196 Info.align = Align(16);
5197 Infos.push_back(Info);
5198 return;
5199
5200 case Intrinsic::nvvm_suld_1d_i64_clamp:
5201 case Intrinsic::nvvm_suld_1d_v2i64_clamp:
5202 case Intrinsic::nvvm_suld_1d_array_i64_clamp:
5203 case Intrinsic::nvvm_suld_1d_array_v2i64_clamp:
5204 case Intrinsic::nvvm_suld_2d_i64_clamp:
5205 case Intrinsic::nvvm_suld_2d_v2i64_clamp:
5206 case Intrinsic::nvvm_suld_2d_array_i64_clamp:
5207 case Intrinsic::nvvm_suld_2d_array_v2i64_clamp:
5208 case Intrinsic::nvvm_suld_3d_i64_clamp:
5209 case Intrinsic::nvvm_suld_3d_v2i64_clamp:
5210 case Intrinsic::nvvm_suld_1d_i64_trap:
5211 case Intrinsic::nvvm_suld_1d_v2i64_trap:
5212 case Intrinsic::nvvm_suld_1d_array_i64_trap:
5213 case Intrinsic::nvvm_suld_1d_array_v2i64_trap:
5214 case Intrinsic::nvvm_suld_2d_i64_trap:
5215 case Intrinsic::nvvm_suld_2d_v2i64_trap:
5216 case Intrinsic::nvvm_suld_2d_array_i64_trap:
5217 case Intrinsic::nvvm_suld_2d_array_v2i64_trap:
5218 case Intrinsic::nvvm_suld_3d_i64_trap:
5219 case Intrinsic::nvvm_suld_3d_v2i64_trap:
5220 case Intrinsic::nvvm_suld_1d_i64_zero:
5221 case Intrinsic::nvvm_suld_1d_v2i64_zero:
5222 case Intrinsic::nvvm_suld_1d_array_i64_zero:
5223 case Intrinsic::nvvm_suld_1d_array_v2i64_zero:
5224 case Intrinsic::nvvm_suld_2d_i64_zero:
5225 case Intrinsic::nvvm_suld_2d_v2i64_zero:
5226 case Intrinsic::nvvm_suld_2d_array_i64_zero:
5227 case Intrinsic::nvvm_suld_2d_array_v2i64_zero:
5228 case Intrinsic::nvvm_suld_3d_i64_zero:
5229 case Intrinsic::nvvm_suld_3d_v2i64_zero:
5230 Info.opc = ISD::INTRINSIC_W_CHAIN;
5231 Info.memVT = MVT::i64;
5232 Info.ptrVal = nullptr;
5233 Info.offset = 0;
5234 Info.flags = MachineMemOperand::MOLoad;
5235 Info.align = Align(16);
5236 Infos.push_back(Info);
5237 return;
5238
5239 case Intrinsic::nvvm_tcgen05_ld_16x64b_x1:
5240 case Intrinsic::nvvm_tcgen05_ld_32x32b_x1:
5241 case Intrinsic::nvvm_tcgen05_ld_16x32bx2_x1: {
5242 Info.opc = ISD::INTRINSIC_W_CHAIN;
5243 Info.memVT = MVT::v1i32;
5244 Info.ptrVal = I.getArgOperand(0);
5245 Info.offset = 0;
5246 Info.flags = MachineMemOperand::MOLoad;
5247 Info.align.reset();
5248 Infos.push_back(Info);
5249 return;
5250 }
5251
5252 case Intrinsic::nvvm_tcgen05_ld_16x64b_x2:
5253 case Intrinsic::nvvm_tcgen05_ld_16x128b_x1:
5254 case Intrinsic::nvvm_tcgen05_ld_32x32b_x2:
5255 case Intrinsic::nvvm_tcgen05_ld_16x32bx2_x2:
5256 case Intrinsic::nvvm_tcgen05_ld_red_32x32b_x2_i32:
5257 case Intrinsic::nvvm_tcgen05_ld_red_16x32bx2_x2_i32: {
5258 Info.opc = ISD::INTRINSIC_W_CHAIN;
5259 Info.memVT = MVT::v2i32;
5260 Info.ptrVal = I.getArgOperand(0);
5261 Info.offset = 0;
5262 Info.flags = MachineMemOperand::MOLoad;
5263 Info.align.reset();
5264 Infos.push_back(Info);
5265 return;
5266 }
5267
5268 case Intrinsic::nvvm_tcgen05_ld_red_32x32b_x2_f32:
5269 case Intrinsic::nvvm_tcgen05_ld_red_16x32bx2_x2_f32: {
5270 Info.opc = ISD::INTRINSIC_W_CHAIN;
5271 Info.memVT = MVT::v2f32;
5272 Info.ptrVal = I.getArgOperand(0);
5273 Info.offset = 0;
5274 Info.flags = MachineMemOperand::MOLoad;
5275 Info.align.reset();
5276 Infos.push_back(Info);
5277 return;
5278 }
5279
5280 case Intrinsic::nvvm_tcgen05_ld_16x64b_x4:
5281 case Intrinsic::nvvm_tcgen05_ld_16x128b_x2:
5282 case Intrinsic::nvvm_tcgen05_ld_32x32b_x4:
5283 case Intrinsic::nvvm_tcgen05_ld_16x256b_x1:
5284 case Intrinsic::nvvm_tcgen05_ld_16x32bx2_x4:
5285 case Intrinsic::nvvm_tcgen05_ld_red_32x32b_x4_i32:
5286 case Intrinsic::nvvm_tcgen05_ld_red_16x32bx2_x4_i32: {
5287 Info.opc = ISD::INTRINSIC_W_CHAIN;
5288 Info.memVT = MVT::v4i32;
5289 Info.ptrVal = I.getArgOperand(0);
5290 Info.offset = 0;
5291 Info.flags = MachineMemOperand::MOLoad;
5292 Info.align.reset();
5293 Infos.push_back(Info);
5294 return;
5295 }
5296
5297 case Intrinsic::nvvm_tcgen05_ld_red_32x32b_x4_f32:
5298 case Intrinsic::nvvm_tcgen05_ld_red_16x32bx2_x4_f32: {
5299 Info.opc = ISD::INTRINSIC_W_CHAIN;
5300 Info.memVT = MVT::v4f32;
5301 Info.ptrVal = I.getArgOperand(0);
5302 Info.offset = 0;
5303 Info.flags = MachineMemOperand::MOLoad;
5304 Info.align.reset();
5305 Infos.push_back(Info);
5306 return;
5307 }
5308
5309 case Intrinsic::nvvm_tcgen05_ld_16x64b_x8:
5310 case Intrinsic::nvvm_tcgen05_ld_16x128b_x4:
5311 case Intrinsic::nvvm_tcgen05_ld_16x256b_x2:
5312 case Intrinsic::nvvm_tcgen05_ld_32x32b_x8:
5313 case Intrinsic::nvvm_tcgen05_ld_16x32bx2_x8:
5314 case Intrinsic::nvvm_tcgen05_ld_red_32x32b_x8_i32:
5315 case Intrinsic::nvvm_tcgen05_ld_red_16x32bx2_x8_i32: {
5316 Info.opc = ISD::INTRINSIC_W_CHAIN;
5317 Info.memVT = MVT::v8i32;
5318 Info.ptrVal = I.getArgOperand(0);
5319 Info.offset = 0;
5320 Info.flags = MachineMemOperand::MOLoad;
5321 Info.align.reset();
5322 Infos.push_back(Info);
5323 return;
5324 }
5325
5326 case Intrinsic::nvvm_tcgen05_ld_red_32x32b_x8_f32:
5327 case Intrinsic::nvvm_tcgen05_ld_red_16x32bx2_x8_f32: {
5328 Info.opc = ISD::INTRINSIC_W_CHAIN;
5329 Info.memVT = MVT::v8f32;
5330 Info.ptrVal = I.getArgOperand(0);
5331 Info.offset = 0;
5332 Info.flags = MachineMemOperand::MOLoad;
5333 Info.align.reset();
5334 Infos.push_back(Info);
5335 return;
5336 }
5337
5338 case Intrinsic::nvvm_tcgen05_ld_16x64b_x16:
5339 case Intrinsic::nvvm_tcgen05_ld_16x128b_x8:
5340 case Intrinsic::nvvm_tcgen05_ld_16x256b_x4:
5341 case Intrinsic::nvvm_tcgen05_ld_32x32b_x16:
5342 case Intrinsic::nvvm_tcgen05_ld_16x32bx2_x16:
5343 case Intrinsic::nvvm_tcgen05_ld_red_32x32b_x16_i32:
5344 case Intrinsic::nvvm_tcgen05_ld_red_16x32bx2_x16_i32: {
5345 Info.opc = ISD::INTRINSIC_W_CHAIN;
5346 Info.memVT = MVT::v16i32;
5347 Info.ptrVal = I.getArgOperand(0);
5348 Info.offset = 0;
5349 Info.flags = MachineMemOperand::MOLoad;
5350 Info.align.reset();
5351 Infos.push_back(Info);
5352 return;
5353 }
5354
5355 case Intrinsic::nvvm_tcgen05_ld_red_32x32b_x16_f32:
5356 case Intrinsic::nvvm_tcgen05_ld_red_16x32bx2_x16_f32: {
5357 Info.opc = ISD::INTRINSIC_W_CHAIN;
5358 Info.memVT = MVT::v16f32;
5359 Info.ptrVal = I.getArgOperand(0);
5360 Info.offset = 0;
5361 Info.flags = MachineMemOperand::MOLoad;
5362 Info.align.reset();
5363 Infos.push_back(Info);
5364 return;
5365 }
5366
5367 case Intrinsic::nvvm_tcgen05_ld_16x64b_x32:
5368 case Intrinsic::nvvm_tcgen05_ld_16x128b_x16:
5369 case Intrinsic::nvvm_tcgen05_ld_16x256b_x8:
5370 case Intrinsic::nvvm_tcgen05_ld_32x32b_x32:
5371 case Intrinsic::nvvm_tcgen05_ld_16x32bx2_x32:
5372 case Intrinsic::nvvm_tcgen05_ld_red_32x32b_x32_i32:
5373 case Intrinsic::nvvm_tcgen05_ld_red_16x32bx2_x32_i32: {
5374 Info.opc = ISD::INTRINSIC_W_CHAIN;
5375 Info.memVT = MVT::v32i32;
5376 Info.ptrVal = I.getArgOperand(0);
5377 Info.offset = 0;
5378 Info.flags = MachineMemOperand::MOLoad;
5379 Info.align.reset();
5380 Infos.push_back(Info);
5381 return;
5382 }
5383
5384 case Intrinsic::nvvm_tcgen05_ld_red_32x32b_x32_f32:
5385 case Intrinsic::nvvm_tcgen05_ld_red_16x32bx2_x32_f32: {
5386 Info.opc = ISD::INTRINSIC_W_CHAIN;
5387 Info.memVT = MVT::v32f32;
5388 Info.ptrVal = I.getArgOperand(0);
5389 Info.offset = 0;
5390 Info.flags = MachineMemOperand::MOLoad;
5391 Info.align.reset();
5392 Infos.push_back(Info);
5393 return;
5394 }
5395
5396 case Intrinsic::nvvm_tcgen05_ld_16x64b_x64:
5397 case Intrinsic::nvvm_tcgen05_ld_16x128b_x32:
5398 case Intrinsic::nvvm_tcgen05_ld_16x256b_x16:
5399 case Intrinsic::nvvm_tcgen05_ld_32x32b_x64:
5400 case Intrinsic::nvvm_tcgen05_ld_16x32bx2_x64:
5401 case Intrinsic::nvvm_tcgen05_ld_red_32x32b_x64_i32:
5402 case Intrinsic::nvvm_tcgen05_ld_red_16x32bx2_x64_i32: {
5403 Info.opc = ISD::INTRINSIC_W_CHAIN;
5404 Info.memVT = MVT::v64i32;
5405 Info.ptrVal = I.getArgOperand(0);
5406 Info.offset = 0;
5407 Info.flags = MachineMemOperand::MOLoad;
5408 Info.align.reset();
5409 Infos.push_back(Info);
5410 return;
5411 }
5412
5413 case Intrinsic::nvvm_tcgen05_ld_red_32x32b_x64_f32:
5414 case Intrinsic::nvvm_tcgen05_ld_red_16x32bx2_x64_f32: {
5415 Info.opc = ISD::INTRINSIC_W_CHAIN;
5416 Info.memVT = MVT::v64f32;
5417 Info.ptrVal = I.getArgOperand(0);
5418 Info.offset = 0;
5419 Info.flags = MachineMemOperand::MOLoad;
5420 Info.align.reset();
5421 Infos.push_back(Info);
5422 return;
5423 }
5424
5425 case Intrinsic::nvvm_tcgen05_ld_16x64b_x128:
5426 case Intrinsic::nvvm_tcgen05_ld_16x128b_x64:
5427 case Intrinsic::nvvm_tcgen05_ld_16x256b_x32:
5428 case Intrinsic::nvvm_tcgen05_ld_32x32b_x128:
5429 case Intrinsic::nvvm_tcgen05_ld_16x32bx2_x128:
5430 case Intrinsic::nvvm_tcgen05_ld_red_32x32b_x128_i32:
5431 case Intrinsic::nvvm_tcgen05_ld_red_16x32bx2_x128_i32: {
5432 Info.opc = ISD::INTRINSIC_W_CHAIN;
5433 Info.memVT = MVT::v128i32;
5434 Info.ptrVal = I.getArgOperand(0);
5435 Info.offset = 0;
5436 Info.flags = MachineMemOperand::MOLoad;
5437 Info.align.reset();
5438 Infos.push_back(Info);
5439 return;
5440 }
5441
5442 case Intrinsic::nvvm_tcgen05_ld_red_32x32b_x128_f32:
5443 case Intrinsic::nvvm_tcgen05_ld_red_16x32bx2_x128_f32: {
5444 Info.opc = ISD::INTRINSIC_W_CHAIN;
5445 Info.memVT = MVT::v128f32;
5446 Info.ptrVal = I.getArgOperand(0);
5447 Info.offset = 0;
5448 Info.flags = MachineMemOperand::MOLoad;
5449 Info.align.reset();
5450 Infos.push_back(Info);
5451 return;
5452 }
5453
5454 case Intrinsic::nvvm_tcgen05_st_16x64b_x1:
5455 case Intrinsic::nvvm_tcgen05_st_32x32b_x1:
5456 case Intrinsic::nvvm_tcgen05_st_16x32bx2_x1: {
5457 Info.opc = ISD::INTRINSIC_VOID;
5458 Info.memVT = MVT::v1i32;
5459 Info.ptrVal = I.getArgOperand(0);
5460 Info.offset = 0;
5461 Info.flags = MachineMemOperand::MOStore;
5462 Info.align.reset();
5463 Infos.push_back(Info);
5464 return;
5465 }
5466
5467 case Intrinsic::nvvm_tcgen05_st_16x64b_x2:
5468 case Intrinsic::nvvm_tcgen05_st_16x128b_x1:
5469 case Intrinsic::nvvm_tcgen05_st_32x32b_x2:
5470 case Intrinsic::nvvm_tcgen05_st_16x32bx2_x2: {
5471 Info.opc = ISD::INTRINSIC_VOID;
5472 Info.memVT = MVT::v2i32;
5473 Info.ptrVal = I.getArgOperand(0);
5474 Info.offset = 0;
5475 Info.flags = MachineMemOperand::MOStore;
5476 Info.align.reset();
5477 Infos.push_back(Info);
5478 return;
5479 }
5480
5481 case Intrinsic::nvvm_tcgen05_st_16x64b_x4:
5482 case Intrinsic::nvvm_tcgen05_st_16x128b_x2:
5483 case Intrinsic::nvvm_tcgen05_st_16x256b_x1:
5484 case Intrinsic::nvvm_tcgen05_st_32x32b_x4:
5485 case Intrinsic::nvvm_tcgen05_st_16x32bx2_x4: {
5486 Info.opc = ISD::INTRINSIC_VOID;
5487 Info.memVT = MVT::v4i32;
5488 Info.ptrVal = I.getArgOperand(0);
5489 Info.offset = 0;
5490 Info.flags = MachineMemOperand::MOStore;
5491 Info.align.reset();
5492 Infos.push_back(Info);
5493 return;
5494 }
5495
5496 case Intrinsic::nvvm_tcgen05_st_16x64b_x8:
5497 case Intrinsic::nvvm_tcgen05_st_16x128b_x4:
5498 case Intrinsic::nvvm_tcgen05_st_16x256b_x2:
5499 case Intrinsic::nvvm_tcgen05_st_32x32b_x8:
5500 case Intrinsic::nvvm_tcgen05_st_16x32bx2_x8: {
5501 Info.opc = ISD::INTRINSIC_VOID;
5502 Info.memVT = MVT::v8i32;
5503 Info.ptrVal = I.getArgOperand(0);
5504 Info.offset = 0;
5505 Info.flags = MachineMemOperand::MOStore;
5506 Info.align.reset();
5507 Infos.push_back(Info);
5508 return;
5509 }
5510
5511 case Intrinsic::nvvm_tcgen05_st_16x64b_x16:
5512 case Intrinsic::nvvm_tcgen05_st_16x128b_x8:
5513 case Intrinsic::nvvm_tcgen05_st_16x256b_x4:
5514 case Intrinsic::nvvm_tcgen05_st_32x32b_x16:
5515 case Intrinsic::nvvm_tcgen05_st_16x32bx2_x16: {
5516 Info.opc = ISD::INTRINSIC_VOID;
5517 Info.memVT = MVT::v16i32;
5518 Info.ptrVal = I.getArgOperand(0);
5519 Info.offset = 0;
5520 Info.flags = MachineMemOperand::MOStore;
5521 Info.align.reset();
5522 Infos.push_back(Info);
5523 return;
5524 }
5525
5526 case Intrinsic::nvvm_tcgen05_st_16x64b_x32:
5527 case Intrinsic::nvvm_tcgen05_st_16x128b_x16:
5528 case Intrinsic::nvvm_tcgen05_st_16x256b_x8:
5529 case Intrinsic::nvvm_tcgen05_st_32x32b_x32:
5530 case Intrinsic::nvvm_tcgen05_st_16x32bx2_x32: {
5531 Info.opc = ISD::INTRINSIC_VOID;
5532 Info.memVT = MVT::v32i32;
5533 Info.ptrVal = I.getArgOperand(0);
5534 Info.offset = 0;
5535 Info.flags = MachineMemOperand::MOStore;
5536 Info.align.reset();
5537 Infos.push_back(Info);
5538 return;
5539 }
5540
5541 case Intrinsic::nvvm_tcgen05_st_16x64b_x64:
5542 case Intrinsic::nvvm_tcgen05_st_16x128b_x32:
5543 case Intrinsic::nvvm_tcgen05_st_16x256b_x16:
5544 case Intrinsic::nvvm_tcgen05_st_32x32b_x64:
5545 case Intrinsic::nvvm_tcgen05_st_16x32bx2_x64: {
5546 Info.opc = ISD::INTRINSIC_VOID;
5547 Info.memVT = MVT::v64i32;
5548 Info.ptrVal = I.getArgOperand(0);
5549 Info.offset = 0;
5550 Info.flags = MachineMemOperand::MOStore;
5551 Info.align.reset();
5552 Infos.push_back(Info);
5553 return;
5554 }
5555
5556 case Intrinsic::nvvm_tcgen05_st_16x64b_x128:
5557 case Intrinsic::nvvm_tcgen05_st_16x128b_x64:
5558 case Intrinsic::nvvm_tcgen05_st_16x256b_x32:
5559 case Intrinsic::nvvm_tcgen05_st_32x32b_x128:
5560 case Intrinsic::nvvm_tcgen05_st_16x32bx2_x128: {
5561 Info.opc = ISD::INTRINSIC_VOID;
5562 Info.memVT = MVT::v128i32;
5563 Info.ptrVal = I.getArgOperand(0);
5564 Info.offset = 0;
5565 Info.flags = MachineMemOperand::MOStore;
5566 Info.align.reset();
5567 Infos.push_back(Info);
5568 return;
5569 }
5570 case Intrinsic::
5571 nvvm_tcgen05_mma_shared_f8f6f4_disable_output_lane_cg1_decompress_b:
5572 case Intrinsic::
5573 nvvm_tcgen05_mma_tensor_f8f6f4_disable_output_lane_cg1_decompress_b:
5574 case Intrinsic::nvvm_tcgen05_mma_shared_disable_output_lane_cg1:
5575 case Intrinsic::nvvm_tcgen05_mma_shared_scale_d_disable_output_lane_cg1:
5576 case Intrinsic::nvvm_tcgen05_mma_sp_shared_disable_output_lane_cg1:
5577 case Intrinsic::nvvm_tcgen05_mma_sp_shared_scale_d_disable_output_lane_cg1:
5578 case Intrinsic::nvvm_tcgen05_mma_tensor_disable_output_lane_cg1:
5579 case Intrinsic::nvvm_tcgen05_mma_tensor_scale_d_disable_output_lane_cg1:
5580 case Intrinsic::nvvm_tcgen05_mma_tensor_disable_output_lane_cg1_ashift:
5581 case Intrinsic::
5582 nvvm_tcgen05_mma_tensor_scale_d_disable_output_lane_cg1_ashift:
5583 case Intrinsic::nvvm_tcgen05_mma_sp_tensor_disable_output_lane_cg1:
5584 case Intrinsic::nvvm_tcgen05_mma_sp_tensor_scale_d_disable_output_lane_cg1:
5585 case Intrinsic::nvvm_tcgen05_mma_sp_tensor_disable_output_lane_cg1_ashift:
5586 case Intrinsic::
5587 nvvm_tcgen05_mma_sp_tensor_scale_d_disable_output_lane_cg1_ashift: {
5588 // We are reading and writing back to TMem
5589 Info.opc = ISD::INTRINSIC_VOID;
5590 Info.memVT = MVT::v4i32;
5591 Info.ptrVal = I.getArgOperand(0);
5592 Info.offset = 0;
5594 Info.align = Align(16);
5595 Infos.push_back(Info);
5596 return;
5597 }
5598
5599 case Intrinsic::
5600 nvvm_tcgen05_mma_shared_f8f6f4_disable_output_lane_cg2_decompress_b:
5601 case Intrinsic::
5602 nvvm_tcgen05_mma_tensor_f8f6f4_disable_output_lane_cg2_decompress_b:
5603 case Intrinsic::nvvm_tcgen05_mma_shared_disable_output_lane_cg2:
5604 case Intrinsic::nvvm_tcgen05_mma_shared_scale_d_disable_output_lane_cg2:
5605 case Intrinsic::nvvm_tcgen05_mma_sp_shared_disable_output_lane_cg2:
5606 case Intrinsic::nvvm_tcgen05_mma_sp_shared_scale_d_disable_output_lane_cg2:
5607 case Intrinsic::nvvm_tcgen05_mma_tensor_disable_output_lane_cg2:
5608 case Intrinsic::nvvm_tcgen05_mma_tensor_scale_d_disable_output_lane_cg2:
5609 case Intrinsic::nvvm_tcgen05_mma_sp_tensor_disable_output_lane_cg2:
5610 case Intrinsic::nvvm_tcgen05_mma_sp_tensor_scale_d_disable_output_lane_cg2:
5611 case Intrinsic::nvvm_tcgen05_mma_tensor_disable_output_lane_cg2_ashift:
5612 case Intrinsic::
5613 nvvm_tcgen05_mma_tensor_scale_d_disable_output_lane_cg2_ashift:
5614 case Intrinsic::nvvm_tcgen05_mma_sp_tensor_disable_output_lane_cg2_ashift:
5615 case Intrinsic::
5616 nvvm_tcgen05_mma_sp_tensor_scale_d_disable_output_lane_cg2_ashift: {
5617 // We are reading and writing back to TMem
5618 Info.opc = ISD::INTRINSIC_VOID;
5619 Info.memVT = MVT::v8i32;
5620 Info.ptrVal = I.getArgOperand(0);
5621 Info.offset = 0;
5623 Info.align = Align(16);
5624 Infos.push_back(Info);
5625 return;
5626 }
5627 case Intrinsic::nvvm_tcgen05_alloc_cg1:
5628 case Intrinsic::nvvm_tcgen05_alloc_cg2:
5629 Info.opc = ISD::INTRINSIC_VOID;
5630 Info.memVT = MVT::i32;
5631 Info.ptrVal = I.getArgOperand(0);
5632 Info.offset = 0;
5633 Info.flags = MachineMemOperand::MOStore;
5634 Info.align = Align(4);
5635 Infos.push_back(Info);
5636 return;
5637 }
5638}
5639
5640// Helper for getting a function parameter symbol. Its name is composed from
5641// the function name and the parameter index. Negative index corresponds to the
5642// special parameter (unsized array) used for passing variable arguments.
5644 int Idx) const {
5645 const StringRef FuncName = getTargetMachine().getSymbol(F)->getName();
5646 if (Idx < 0)
5647 return Ctx.getOrCreateSymbol(FuncName + "_vararg");
5648 return Ctx.getOrCreateSymbol(FuncName + "_param_" + Twine(Idx));
5649}
5650
5651/// isLegalAddressingMode - Return true if the addressing mode represented
5652/// by AM is legal for this target, for a load/store of the specified type.
5653/// Used to guide target specific optimizations, like loop strength reduction
5654/// (LoopStrengthReduce.cpp) and memory optimization for address mode
5655/// (CodeGenPrepare.cpp)
5657 const AddrMode &AM, Type *Ty,
5658 unsigned AS, Instruction *I) const {
5659 // AddrMode - This represents an addressing mode of:
5660 // BaseGV + BaseOffs + BaseReg + Scale*ScaleReg
5661 //
5662 // The legal address modes are
5663 // - [avar]
5664 // - [areg]
5665 // - [areg+immoff]
5666 // - [immAddr]
5667
5668 // immoff must fit in a signed 32-bit int
5669 if (!APInt(64, AM.BaseOffs).isSignedIntN(32))
5670 return false;
5671
5672 if (AM.BaseGV)
5673 return !AM.BaseOffs && !AM.HasBaseReg && !AM.Scale;
5674
5675 switch (AM.Scale) {
5676 case 0: // "r", "r+i" or "i" is allowed
5677 break;
5678 case 1:
5679 if (AM.HasBaseReg) // "r+r+i" or "r+r" is not allowed.
5680 return false;
5681 // Otherwise we have r+i.
5682 break;
5683 default:
5684 // No scale > 1 is allowed
5685 return false;
5686 }
5687 return true;
5688}
5689
5690//===----------------------------------------------------------------------===//
5691// NVPTX Inline Assembly Support
5692//===----------------------------------------------------------------------===//
5693
5694/// getConstraintType - Given a constraint letter, return the type of
5695/// constraint it is for this target.
5698 if (Constraint.size() == 1) {
5699 switch (Constraint[0]) {
5700 default:
5701 break;
5702 case 'b':
5703 case 'r':
5704 case 'h':
5705 case 'c':
5706 case 'l':
5707 case 'f':
5708 case 'd':
5709 case 'q':
5710 case '0':
5711 case 'N':
5712 return C_RegisterClass;
5713 }
5714 }
5715 return TargetLowering::getConstraintType(Constraint);
5716}
5717
5718std::pair<unsigned, const TargetRegisterClass *>
5720 StringRef Constraint,
5721 MVT VT) const {
5722 if (Constraint.size() == 1) {
5723 switch (Constraint[0]) {
5724 case 'b':
5725 return std::make_pair(0U, &NVPTX::B1RegClass);
5726 case 'c':
5727 case 'h':
5728 return std::make_pair(0U, &NVPTX::B16RegClass);
5729 case 'r':
5730 case 'f':
5731 return std::make_pair(0U, &NVPTX::B32RegClass);
5732 case 'l':
5733 case 'N':
5734 case 'd':
5735 return std::make_pair(0U, &NVPTX::B64RegClass);
5736 case 'q': {
5737 if (!STI.hasFeature(NVPTX::SM70))
5738 report_fatal_error("Inline asm with 128 bit operands is only "
5739 "supported for sm_70 and higher!");
5740 return std::make_pair(0U, &NVPTX::B128RegClass);
5741 }
5742 }
5743 }
5744 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
5745}
5746
5747//===----------------------------------------------------------------------===//
5748// NVPTX DAG Combining
5749//===----------------------------------------------------------------------===//
5750
5752 CodeGenOptLevel OptLevel) const {
5753 // Always honor command-line argument
5754 if (FMAContractLevelOpt.getNumOccurrences() > 0)
5755 return FMAContractLevelOpt > 0;
5756
5757 // Do not contract if we're not optimizing the code.
5758 if (OptLevel == CodeGenOptLevel::None)
5759 return false;
5760
5761 return false;
5762}
5763
5764static bool isConstZero(const SDValue &Operand) {
5765 const auto *Const = dyn_cast<ConstantSDNode>(Operand);
5766 return Const && Const->getZExtValue() == 0;
5767}
5768
5769/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
5770/// operands N0 and N1. This is a helper for PerformADDCombine that is
5771/// called with the default operands, and if that fails, with commuted
5772/// operands.
5773static SDValue
5776 EVT VT = N0.getValueType();
5777
5778 // Since integer multiply-add costs the same as integer multiply
5779 // but is more costly than integer add, do the fusion only when
5780 // the mul is only used in the add.
5781 // TODO: this may not be true for later architectures, consider relaxing this
5782 if (!N0.getNode()->hasOneUse())
5783 return SDValue();
5784
5785 // fold (add (select cond, 0, (mul a, b)), c)
5786 // -> (select cond, c, (add (mul a, b), c))
5787 //
5788 if (N0.getOpcode() == ISD::SELECT) {
5789 unsigned ZeroOpNum;
5790 if (isConstZero(N0->getOperand(1)))
5791 ZeroOpNum = 1;
5792 else if (isConstZero(N0->getOperand(2)))
5793 ZeroOpNum = 2;
5794 else
5795 return SDValue();
5796
5797 SDValue M = N0->getOperand((ZeroOpNum == 1) ? 2 : 1);
5798 if (M->getOpcode() != ISD::MUL || !M.getNode()->hasOneUse())
5799 return SDValue();
5800
5801 SDLoc DL(N);
5802 SDValue Mul =
5803 DCI.DAG.getNode(ISD::MUL, DL, VT, M->getOperand(0), M->getOperand(1));
5804 SDValue MAD = DCI.DAG.getNode(ISD::ADD, DL, VT, Mul, N1);
5805 return DCI.DAG.getSelect(SDLoc(N), VT, N0->getOperand(0),
5806 ((ZeroOpNum == 1) ? N1 : MAD),
5807 ((ZeroOpNum == 1) ? MAD : N1));
5808 }
5809
5810 return SDValue();
5811}
5812
5813SDValue NVPTXTargetLowering::performFADDCombineWithOperands(
5815 CodeGenOptLevel OptLevel) const {
5816 EVT VT = N0.getValueType();
5817 if (N0.getOpcode() == ISD::FMUL) {
5818 if (!(allowFMA(DCI.DAG.getMachineFunction(), OptLevel) ||
5819 (N->getFlags().hasAllowContract() &&
5820 N0->getFlags().hasAllowContract())))
5821 return SDValue();
5822
5823 // For floating point:
5824 // Do the fusion only when the mul has less than 5 uses and all
5825 // are add.
5826 // The heuristic is that if a use is not an add, then that use
5827 // cannot be fused into fma, therefore mul is still needed anyway.
5828 // If there are more than 4 uses, even if they are all add, fusing
5829 // them will increase register pressue.
5830 //
5831 int numUses = 0;
5832 int nonAddCount = 0;
5833 for (const SDNode *User : N0.getNode()->users()) {
5834 numUses++;
5835 if (User->getOpcode() != ISD::FADD)
5836 ++nonAddCount;
5837 if (numUses >= 5)
5838 return SDValue();
5839 }
5840 if (nonAddCount) {
5841 int orderNo = N->getIROrder();
5842 int orderNo2 = N0.getNode()->getIROrder();
5843 // simple heuristics here for considering potential register
5844 // pressure, the logics here is that the differnce are used
5845 // to measure the distance between def and use, the longer distance
5846 // more likely cause register pressure.
5847 if (orderNo - orderNo2 < 500)
5848 return SDValue();
5849
5850 // Now, check if at least one of the FMUL's operands is live beyond the
5851 // node N, which guarantees that the FMA will not increase register
5852 // pressure at node N.
5853 bool opIsLive = false;
5854 const SDNode *left = N0.getOperand(0).getNode();
5855 const SDNode *right = N0.getOperand(1).getNode();
5856
5857 if (isa<ConstantSDNode>(left) || isa<ConstantSDNode>(right))
5858 opIsLive = true;
5859
5860 if (!opIsLive)
5861 for (const SDNode *User : left->users()) {
5862 int orderNo3 = User->getIROrder();
5863 if (orderNo3 > orderNo) {
5864 opIsLive = true;
5865 break;
5866 }
5867 }
5868
5869 if (!opIsLive)
5870 for (const SDNode *User : right->users()) {
5871 int orderNo3 = User->getIROrder();
5872 if (orderNo3 > orderNo) {
5873 opIsLive = true;
5874 break;
5875 }
5876 }
5877
5878 if (!opIsLive)
5879 return SDValue();
5880 }
5881
5882 return DCI.DAG.getNode(ISD::FMA, SDLoc(N), VT, N0.getOperand(0),
5883 N0.getOperand(1), N1);
5884 }
5885
5886 return SDValue();
5887}
5888
5889/// Fold unpacking movs into a load by increasing the number of return values.
5890///
5891/// ex:
5892/// L: v2f16,ch = load <p>
5893/// a: f16 = extractelt L:0, 0
5894/// b: f16 = extractelt L:0, 1
5895/// use(a, b)
5896///
5897/// ...is turned into...
5898///
5899/// L: f16,f16,ch = LoadV2 <p>
5900/// use(L:0, L:1)
5901static SDValue
5903 // Don't run this optimization before the legalizer
5904 if (!DCI.isAfterLegalizeDAG())
5905 return SDValue();
5906
5907 EVT ElementVT = N->getValueType(0);
5908 // Avoid non-packed types and v4i8
5909 if (!NVPTX::isPackedVectorTy(ElementVT) || ElementVT == MVT::v4i8)
5910 return SDValue();
5911
5912 // Check whether all outputs are either used by an extractelt or are
5913 // glue/chain nodes
5914 if (!all_of(N->uses(), [&](SDUse &U) {
5915 // Skip glue, chain nodes
5916 if (U.getValueType() == MVT::Glue || U.getValueType() == MVT::Other)
5917 return true;
5918 if (U.getUser()->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
5919 if (N->getOpcode() != ISD::LOAD)
5920 return true;
5921 // Since this is an ISD::LOAD, check all extractelts are used. If
5922 // any are not used, we don't want to defeat another optimization that
5923 // will narrow the load.
5924 //
5925 // For example:
5926 //
5927 // L: v2f16,ch = load <p>
5928 // e0: f16 = extractelt L:0, 0
5929 // e1: f16 = extractelt L:0, 1 <-- unused
5930 // store e0
5931 //
5932 // Can be optimized by DAGCombiner to:
5933 //
5934 // L: f16,ch = load <p>
5935 // store L:0
5936 return !U.getUser()->use_empty();
5937 }
5938
5939 // Otherwise, this use prevents us from splitting a value.
5940 return false;
5941 }))
5942 return SDValue();
5943
5944 auto *LD = cast<MemSDNode>(N);
5945 SDLoc DL(LD);
5946
5947 // the new opcode after we double the number of operands
5948 unsigned Opcode;
5950 unsigned OldNumOutputs; // non-glue, non-chain outputs
5951 switch (LD->getOpcode()) {
5952 case ISD::LOAD:
5953 OldNumOutputs = 1;
5954 // Any packed type is legal, so the legalizer will not have lowered
5955 // ISD::LOAD -> NVPTXISD::Load (unless it's under-aligned). We have to do it
5956 // here.
5957 Opcode = NVPTXISD::LoadV2;
5958 // append a "full" used bytes mask operand right before the extension type
5959 // operand, signifying that all bytes are used.
5960 Operands.push_back(DCI.DAG.getConstant(UINT32_MAX, DL, MVT::i32));
5961 Operands.push_back(DCI.DAG.getIntPtrConstant(
5962 cast<LoadSDNode>(LD)->getExtensionType(), DL));
5963 break;
5964 case NVPTXISD::LoadV2:
5965 OldNumOutputs = 2;
5966 Opcode = NVPTXISD::LoadV4;
5967 break;
5968 case NVPTXISD::LoadV4:
5969 // V8 is only supported for f32/i32. Don't forget, we're not changing the
5970 // load size here. This is already a 256-bit load.
5971 if (ElementVT != MVT::v2f32 && ElementVT != MVT::v2i32)
5972 return SDValue();
5973 OldNumOutputs = 4;
5974 Opcode = NVPTXISD::LoadV8;
5975 break;
5976 case NVPTXISD::LoadV8:
5977 // PTX doesn't support the next doubling of outputs
5978 return SDValue();
5979 }
5980
5981 // the non-glue, non-chain outputs in the new load
5982 const unsigned NewNumOutputs = OldNumOutputs * 2;
5983 SmallVector<EVT> NewVTs(NewNumOutputs, ElementVT.getVectorElementType());
5984 // add remaining chain and glue values
5985 NewVTs.append(LD->value_begin() + OldNumOutputs, LD->value_end());
5986
5987 // Create the new load
5988 SDValue NewLoad = DCI.DAG.getMemIntrinsicNode(
5989 Opcode, DL, DCI.DAG.getVTList(NewVTs), Operands, LD->getMemoryVT(),
5990 LD->getMemOperand());
5991
5992 // Now we use a combination of BUILD_VECTORs and a MERGE_VALUES node to keep
5993 // the outputs the same. These nodes will be optimized away in later
5994 // DAGCombiner iterations.
5996 for (unsigned I : seq(OldNumOutputs))
5997 Results.push_back(DCI.DAG.getBuildVector(
5998 ElementVT, DL, {NewLoad.getValue(I * 2), NewLoad.getValue(I * 2 + 1)}));
5999 // Add remaining chain and glue nodes
6000 for (unsigned I : seq(NewLoad->getNumValues() - NewNumOutputs))
6001 Results.push_back(NewLoad.getValue(NewNumOutputs + I));
6002
6003 return DCI.DAG.getMergeValues(Results, DL);
6004}
6005
6006/// Fold packing movs into a store.
6007///
6008/// ex:
6009/// v1: v2f16 = BUILD_VECTOR a:f16, b:f16
6010/// v2: v2f16 = BUILD_VECTOR c:f16, d:f16
6011/// StoreV2 v1, v2
6012///
6013/// ...is turned into...
6014///
6015/// StoreV4 a, b, c, d
6018 unsigned Front, unsigned Back) {
6019 // We want to run this as late as possible since other optimizations may
6020 // eliminate the BUILD_VECTORs.
6021 if (!DCI.isAfterLegalizeDAG())
6022 return SDValue();
6023
6024 // Get the type of the operands being stored.
6025 EVT ElementVT = N->getOperand(Front).getValueType();
6026
6027 // Avoid non-packed types and v4i8
6028 if (!NVPTX::isPackedVectorTy(ElementVT) || ElementVT == MVT::v4i8)
6029 return SDValue();
6030
6031 auto *ST = cast<MemSDNode>(N);
6032
6033 // The new opcode after we double the number of operands.
6034 unsigned Opcode;
6035 switch (N->getOpcode()) {
6036 case ISD::STORE:
6037 // Any packed type is legal, so the legalizer will not have lowered
6038 // ISD::STORE -> NVPTXISD::Store (unless it's under-aligned). We have to do
6039 // it here.
6040 Opcode = NVPTXISD::StoreV2;
6041 break;
6042 case NVPTXISD::StoreV2:
6043 Opcode = NVPTXISD::StoreV4;
6044 break;
6045 case NVPTXISD::StoreV4:
6046 // V8 is only supported for f32/i32. Don't forget, we're not changing the
6047 // store size here. This is already a 256-bit store.
6048 if (ElementVT != MVT::v2f32 && ElementVT != MVT::v2i32)
6049 return SDValue();
6050 Opcode = NVPTXISD::StoreV8;
6051 break;
6052 case NVPTXISD::StoreV8:
6053 // PTX doesn't support the next doubling of operands
6054 return SDValue();
6055 default:
6056 llvm_unreachable("Unhandled store opcode");
6057 }
6058
6059 // Scan the operands and if they're all BUILD_VECTORs, we'll have gathered
6060 // their elements.
6061 SmallVector<SDValue, 4> Operands(N->ops().take_front(Front));
6062 for (SDValue BV : N->ops().drop_front(Front).drop_back(Back)) {
6063 if (BV.getOpcode() != ISD::BUILD_VECTOR)
6064 return SDValue();
6065
6066 // If the operand has multiple uses, this optimization can increase register
6067 // pressure.
6068 if (!BV.hasOneUse())
6069 return SDValue();
6070
6071 // DAGCombiner visits nodes bottom-up. Check the BUILD_VECTOR operands for
6072 // any signs they may be folded by some other pattern or rule.
6073 for (SDValue Op : BV->ops()) {
6074 // Peek through bitcasts
6075 if (Op.getOpcode() == ISD::BITCAST)
6076 Op = Op.getOperand(0);
6077
6078 // This may be folded into a PRMT.
6079 if (Op.getValueType() == MVT::i16 && Op.getOpcode() == ISD::TRUNCATE &&
6080 Op->getOperand(0).getValueType() == MVT::i32)
6081 return SDValue();
6082
6083 // This may be folded into cvt.bf16x2
6084 if (Op.getOpcode() == ISD::FP_ROUND)
6085 return SDValue();
6086 }
6087 Operands.append({BV.getOperand(0), BV.getOperand(1)});
6088 }
6089 Operands.append(N->op_end() - Back, N->op_end());
6090
6091 // Now we replace the store
6092 return DCI.DAG.getMemIntrinsicNode(Opcode, SDLoc(N), N->getVTList(), Operands,
6093 ST->getMemoryVT(), ST->getMemOperand());
6094}
6095
6097 const NVPTXSubtarget &STI) {
6098
6099 if (DCI.isBeforeLegalize() && N->getOpcode() == ISD::STORE) {
6100 // Here is our chance to custom lower a store with a non-simple type.
6101 // Unfortunately, we can't do this in the legalizer because there is no
6102 // way to setOperationAction for an non-simple type.
6104 if (!ST->getValue().getValueType().isSimple())
6105 return lowerSTOREVector(SDValue(ST, 0), DCI.DAG, STI);
6106 }
6107
6108 return combinePackingMovIntoStore(N, DCI, 1, 2);
6109}
6110
6112 const NVPTXSubtarget &STI) {
6113 if (DCI.isBeforeLegalize() && N->getOpcode() == ISD::LOAD) {
6114 // Here is our chance to custom lower a load with a non-simple type.
6115 // Unfortunately, we can't do this in the legalizer because there is no
6116 // way to setOperationAction for an non-simple type.
6117 if (!N->getValueType(0).isSimple())
6118 return lowerLoadVector(N, DCI.DAG, STI);
6119 }
6120
6121 return combineUnpackingMovIntoLoad(N, DCI);
6122}
6123
6124/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
6125///
6128 CodeGenOptLevel OptLevel) {
6129 if (OptLevel == CodeGenOptLevel::None)
6130 return SDValue();
6131
6132 SDValue N0 = N->getOperand(0);
6133 SDValue N1 = N->getOperand(1);
6134
6135 // Skip non-integer, non-scalar case
6136 EVT VT = N0.getValueType();
6137 if (VT.isVector() || VT != MVT::i32)
6138 return SDValue();
6139
6140 // First try with the default operand order.
6141 if (SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI))
6142 return Result;
6143
6144 // If that didn't work, try again with the operands commuted.
6145 return PerformADDCombineWithOperands(N, N1, N0, DCI);
6146}
6147
6148/// Check if a v2f32 BUILD_VECTOR provably packs values from non-adjacent
6149/// register pairs (non-coalescable).
6150static bool isNonCoalescableBuildVector(const SDValue &BV) {
6151 if (BV.getOpcode() != ISD::BUILD_VECTOR || BV.getValueType() != MVT::v2f32)
6152 return false;
6153
6154 SDValue Elt0 = BV.getOperand(0);
6155 SDValue Elt1 = BV.getOperand(1);
6156
6157 bool IsExt0 = Elt0.getOpcode() == ISD::EXTRACT_VECTOR_ELT;
6158 bool IsExt1 = Elt1.getOpcode() == ISD::EXTRACT_VECTOR_ELT;
6159
6160 // If neither element is an EXTRACT_VECTOR_ELT they are free-standing
6161 // scalars and the register allocator can still place them side-by-side.
6162 if (!IsExt0 && !IsExt1)
6163 return false;
6164
6165 // If exactly one element is an EXTRACT_VECTOR_ELT, the other is a scalar
6166 // that cannot generally occupy the adjacent register slot.
6167 if (IsExt0 != IsExt1)
6168 return true;
6169
6170 // At this point both sources are extracting from vectors. If they are from
6171 // different vectors, then the BUILD_VECTOR is non-coalescable.
6172 SDValue Src0 = Elt0.getOperand(0);
6173 SDValue Src1 = Elt1.getOperand(0);
6174 if (Src0 != Src1)
6175 return true;
6176
6177 auto *Idx0 = dyn_cast<ConstantSDNode>(Elt0.getOperand(1));
6178 auto *Idx1 = dyn_cast<ConstantSDNode>(Elt1.getOperand(1));
6179 // If both indices are dynamic they will be lowered to
6180 // loads and the vector will be spilled to local memory. The register
6181 // allocator can easily place the results in adjacent registers.
6182 if (!Idx0 && !Idx1)
6183 return false;
6184
6185 // If one index is dynamic and the other is constant, the value from the
6186 // constant load will result in an additional register to pair with the result
6187 // from the dynamic load. We consider this non-coalescable.
6188 if ((Idx0 && !Idx1) || (!Idx0 && Idx1))
6189 return true;
6190
6191 // Both are constant, adjacent pairs are coalescable
6192 return std::abs(Idx0->getSExtValue() - Idx1->getSExtValue()) != 1;
6193}
6194
6195/// Return true if FMUL v2f32 node \p N may be scalarized to fold each lane's
6196/// product into a scalar FMA.
6197bool NVPTXTargetLowering::mayFoldFMULIntoFMA(SDNode *N, MachineFunction &MF,
6198 CodeGenOptLevel OptLevel) const {
6199 if (N->getOpcode() != ISD::FMUL || N->getValueType(0) != MVT::v2f32)
6200 return false;
6201 const bool GlobalFMA = allowFMA(MF, OptLevel);
6202 if (!N->getFlags().hasAllowContract() && !GlobalFMA)
6203 return false;
6204
6205 const SDNode *FirstFAdd = nullptr;
6206 unsigned NumScalarFAdd = 0;
6207
6208 // Both lanes must feed unique FADDs
6209 for (SDNode *EE : N->users()) {
6210 if (NumScalarFAdd == 2)
6211 return false;
6212
6213 if (EE->getOpcode() != ISD::EXTRACT_VECTOR_ELT || !EE->hasOneUse() ||
6214 !isa<ConstantSDNode>(EE->getOperand(1)))
6215 return false;
6216
6217 const SDNode *const FAdd = *EE->users().begin();
6218 if (FAdd->getOpcode() != ISD::FADD ||
6219 (!GlobalFMA && !FAdd->getFlags().hasAllowContract()))
6220 return false;
6221
6222 if (!FirstFAdd)
6223 FirstFAdd = FAdd;
6224 else if (FAdd == FirstFAdd)
6225 return false;
6226
6227 NumScalarFAdd++;
6228 }
6229
6230 return NumScalarFAdd == 2;
6231}
6232
6233/// Scalarize a v2f32 arithmetic node (FADD, FMUL, FSUB, FMA) when at least
6234/// one operand is a BUILD_VECTOR that repacks values from non-adjacent register
6235/// pairs. Without this combine the BUILD_VECTOR forces allocation of a
6236/// temporary 64-bit register, increasing register pressure.
6237///
6238/// Example - before:
6239/// t0: v2f32,v2f32,ch = LoadV2 ...
6240/// t1: f32 = extract_vector_elt t0, 0
6241/// t2: f32 = extract_vector_elt t0:1, 0
6242/// t3: v2f32 = BUILD_VECTOR t1, t2 ;; non-coalescable repack
6243/// t4: v2f32 = fma t_a, t3, t_c
6244///
6245/// After:
6246/// t0: v2f32,v2f32,ch = LoadV2 ...
6247/// t1: f32 = extract_vector_elt t0, 0
6248/// t2: f32 = extract_vector_elt t0:1, 0
6249/// a0: f32 = extract_vector_elt t_a, 0
6250/// a1: f32 = extract_vector_elt t_a, 1
6251/// c0: f32 = extract_vector_elt t_c, 0
6252/// c1: f32 = extract_vector_elt t_c, 1
6253/// r0: f32 = fma a0, t1, c0
6254/// r1: f32 = fma a1, t2, c1
6255/// t4: v2f32 = BUILD_VECTOR r0, r1
6256///
6257/// Also scalarizes an FMUL when all output lanes feed into scalar FADDs
6258/// to enable scalar FMA combining.
6259SDValue NVPTXTargetLowering::performScalarizeV2F32Op(
6261 CodeGenOptLevel OptLevel) const {
6262 EVT VT = N->getValueType(0);
6263 if (VT != MVT::v2f32)
6264 return SDValue();
6265
6266 if (none_of(N->ops(), isNonCoalescableBuildVector) &&
6267 !mayFoldFMULIntoFMA(N, DCI.DAG.getMachineFunction(), OptLevel))
6268 return SDValue();
6269
6270 SelectionDAG &DAG = DCI.DAG;
6271 SDLoc DL(N);
6272 EVT EltVT = VT.getVectorElementType();
6273 unsigned Opc = N->getOpcode();
6274
6275 // For each operand, get the scalar element at the given index: if the operand
6276 // is a BUILD_VECTOR, grab the element directly; otherwise, emit an
6277 // EXTRACT_VECTOR_ELT.
6278 auto GetElement = [&](SDValue Op, unsigned Index) -> SDValue {
6279 if (Op.getOpcode() == ISD::BUILD_VECTOR)
6280 return Op.getOperand(Index);
6281 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Op,
6282 DAG.getVectorIdxConstant(Index, DL));
6283 };
6284
6285 // Build scalar operand lists for element 0 and element 1.
6286 SmallVector<SDValue, 3> Ops0, Ops1;
6287 for (const SDValue &Op : N->ops()) {
6288 Ops0.push_back(GetElement(Op, 0));
6289 Ops1.push_back(GetElement(Op, 1));
6290 }
6291
6292 SDValue Res0 = DAG.getNode(Opc, DL, EltVT, Ops0, N->getFlags());
6293 SDValue Res1 = DAG.getNode(Opc, DL, EltVT, Ops1, N->getFlags());
6294
6295 return DAG.getNode(ISD::BUILD_VECTOR, DL, VT, Res0, Res1);
6296}
6297
6298/// Target-specific dag combine xforms for ISD::FADD.
6299SDValue
6300NVPTXTargetLowering::performFADDCombine(SDNode *N,
6302 CodeGenOptLevel OptLevel) const {
6303 if (SDValue Result = performScalarizeV2F32Op(N, DCI, OptLevel))
6304 return Result;
6305
6306 SDValue N0 = N->getOperand(0);
6307 SDValue N1 = N->getOperand(1);
6308
6309 EVT VT = N0.getValueType();
6310 if (VT.isVector() || !(VT == MVT::f32 || VT == MVT::f64))
6311 return SDValue();
6312
6313 // First try with the default operand order.
6314 if (SDValue Result = performFADDCombineWithOperands(N, N0, N1, DCI, OptLevel))
6315 return Result;
6316
6317 // If that didn't work, try again with the operands commuted.
6318 return performFADDCombineWithOperands(N, N1, N0, DCI, OptLevel);
6319}
6320
6321/// Get 3-input version of a 2-input min/max opcode
6322static unsigned getMinMax3Opcode(unsigned MinMax2Opcode) {
6323 switch (MinMax2Opcode) {
6324 case ISD::FMAXNUM:
6325 case ISD::FMAXIMUMNUM:
6326 return NVPTXISD::FMAXNUM3;
6327 case ISD::FMINNUM:
6328 case ISD::FMINIMUMNUM:
6329 return NVPTXISD::FMINNUM3;
6330 case ISD::FMAXIMUM:
6331 return NVPTXISD::FMAXIMUM3;
6332 case ISD::FMINIMUM:
6333 return NVPTXISD::FMINIMUM3;
6334 default:
6335 llvm_unreachable("Invalid 2-input min/max opcode");
6336 }
6337}
6338
6339/// PerformFMinMaxCombine - Combine (fmaxnum (fmaxnum a, b), c) into
6340/// (fmaxnum3 a, b, c). Also covers other llvm min/max intrinsics.
6343 const NVPTXSubtarget &STI) {
6344
6345 // 3-input min/max requires PTX 8.8+ and SM_100+, and only supports f32s
6346 EVT VT = N->getValueType(0);
6347 if (VT != MVT::f32 || !STI.hasFeature(NVPTX::PTX88) ||
6348 !STI.hasFeature(NVPTX::SM100))
6349 return SDValue();
6350
6351 SDValue Op0 = N->getOperand(0);
6352 SDValue Op1 = N->getOperand(1);
6353 unsigned MinMaxOp2 = N->getOpcode();
6354 unsigned MinMaxOp3 = getMinMax3Opcode(MinMaxOp2);
6355
6356 if (Op0.getOpcode() == MinMaxOp2 && Op0.hasOneUse()) {
6357 // (maxnum (maxnum a, b), c) -> (maxnum3 a, b, c)
6358 SDValue A = Op0.getOperand(0);
6359 SDValue B = Op0.getOperand(1);
6360 SDValue C = Op1;
6361 return DCI.DAG.getNode(MinMaxOp3, SDLoc(N), VT, A, B, C, N->getFlags());
6362 } else if (Op1.getOpcode() == MinMaxOp2 && Op1.hasOneUse()) {
6363 // (maxnum a, (maxnum b, c)) -> (maxnum3 a, b, c)
6364 SDValue A = Op0;
6365 SDValue B = Op1.getOperand(0);
6366 SDValue C = Op1.getOperand(1);
6367 return DCI.DAG.getNode(MinMaxOp3, SDLoc(N), VT, A, B, C, N->getFlags());
6368 }
6369 return SDValue();
6370}
6371
6372// sext (mul.iN nsw x, y) => mul.wide.sN x, y
6373// zext (mul.iN nuw x, y) => mul.wide.uN x, y
6374// sext (shl.iN nsw x, const) => mul.wide.sN x, (1 << const)
6375// zext (shl.iN nuw x, const) => mul.wide.uN x, (1 << const)
6378 CodeGenOptLevel OptLevel) {
6379 assert(N->getOpcode() == ISD::SIGN_EXTEND ||
6380 N->getOpcode() == ISD::ZERO_EXTEND);
6381
6382 if (OptLevel == CodeGenOptLevel::None)
6383 return SDValue();
6384
6385 SDValue Op = N->getOperand(0);
6386 if (!Op.hasOneUse())
6387 return SDValue();
6388
6389 EVT ToVT = N->getValueType(0);
6390 EVT FromVT = Op.getValueType();
6391 if (!((ToVT == MVT::i32 && FromVT == MVT::i16) ||
6392 (ToVT == MVT::i64 && FromVT == MVT::i32)))
6393 return SDValue();
6394
6395 bool IsSigned = N->getOpcode() == ISD::SIGN_EXTEND;
6396 if ((IsSigned && !Op->getFlags().hasNoSignedWrap()) ||
6397 (!IsSigned && !Op->getFlags().hasNoUnsignedWrap()))
6398 return SDValue();
6399
6400 SDLoc DL(N);
6401 SDValue LHS = Op.getOperand(0);
6402 SDValue RHS = Op.getOperand(1);
6403 unsigned MulWideOpcode =
6404 IsSigned ? NVPTXISD::MUL_WIDE_SIGNED : NVPTXISD::MUL_WIDE_UNSIGNED;
6405 if (Op.getOpcode() == ISD::MUL) {
6406 return DCI.DAG.getNode(MulWideOpcode, DL, ToVT, LHS, RHS);
6407 } else if (Op.getOpcode() == ISD::SHL && isa<ConstantSDNode>(RHS)) {
6408 const auto ShiftAmt = Op.getConstantOperandVal(1);
6409 const auto MulVal = APInt(FromVT.getSizeInBits(), 1) << ShiftAmt;
6410
6411 // Note that the sext (shl nsw ...) case doesn't work if 1 << const
6412 // overflows to a negative value! The only valid input values in this
6413 // case are 0 and -1 (all other values yield poison because of the nsw),
6414 // and mul.wide.sN would give us the wrong sign for -1. We could use
6415 // mul.wide.uN, but since this is a weird case anyway, we might as well not
6416 // apply this transformation at all.
6417 if (IsSigned && MulVal.isNegative())
6418 return SDValue();
6419
6420 RHS = DCI.DAG.getConstant(MulVal, DL, FromVT);
6421 return DCI.DAG.getNode(MulWideOpcode, DL, ToVT, LHS, RHS);
6422 }
6423
6424 return SDValue();
6425}
6426
6432
6433/// IsMulWideOperandDemotable - Checks if the provided DAG node is an operand
6434/// that can be demoted to \p OptSize bits without loss of information. The
6435/// signedness of the operand, if determinable, is placed in \p S.
6437 unsigned OptSize,
6438 OperandSignedness &S) {
6439 S = Unknown;
6440
6441 if (Op.getOpcode() == ISD::SIGN_EXTEND ||
6442 Op.getOpcode() == ISD::SIGN_EXTEND_INREG) {
6443 EVT OrigVT = Op.getOperand(0).getValueType();
6444 if (OrigVT.getFixedSizeInBits() <= OptSize) {
6445 S = Signed;
6446 return true;
6447 }
6448 } else if (Op.getOpcode() == ISD::ZERO_EXTEND) {
6449 EVT OrigVT = Op.getOperand(0).getValueType();
6450 if (OrigVT.getFixedSizeInBits() <= OptSize) {
6451 S = Unsigned;
6452 return true;
6453 }
6454 }
6455
6456 return false;
6457}
6458
6459/// AreMulWideOperandsDemotable - Checks if the given LHS and RHS operands can
6460/// be demoted to \p OptSize bits without loss of information. If the operands
6461/// contain a constant, it should appear as the RHS operand. The signedness of
6462/// the operands is placed in \p IsSigned.
6464 unsigned OptSize,
6465 bool &IsSigned) {
6466 OperandSignedness LHSSign;
6467
6468 // The LHS operand must be a demotable op
6469 if (!IsMulWideOperandDemotable(LHS, OptSize, LHSSign))
6470 return false;
6471
6472 // We should have been able to determine the signedness from the LHS
6473 if (LHSSign == Unknown)
6474 return false;
6475
6476 IsSigned = (LHSSign == Signed);
6477
6478 // The RHS can be a demotable op or a constant
6480 const APInt &Val = CI->getAPIntValue();
6481 if (LHSSign == Unsigned) {
6482 return Val.isIntN(OptSize);
6483 } else {
6484 return Val.isSignedIntN(OptSize);
6485 }
6486 } else {
6487 OperandSignedness RHSSign;
6488 if (!IsMulWideOperandDemotable(RHS, OptSize, RHSSign))
6489 return false;
6490
6491 return LHSSign == RHSSign;
6492 }
6493}
6494
6495/// TryMULWIDECombine - Attempt to replace a multiply of M bits with a multiply
6496/// of M/2 bits that produces an M-bit result (i.e. mul.wide). This transform
6497/// works on both multiply DAG nodes and SHL DAG nodes with a constant shift
6498/// amount.
6501 EVT MulType = N->getValueType(0);
6502 if (MulType != MVT::i32 && MulType != MVT::i64) {
6503 return SDValue();
6504 }
6505
6506 SDLoc DL(N);
6507 unsigned OptSize = MulType.getSizeInBits() >> 1;
6508 SDValue LHS = N->getOperand(0);
6509 SDValue RHS = N->getOperand(1);
6510
6511 // Canonicalize the multiply so the constant (if any) is on the right
6512 if (N->getOpcode() == ISD::MUL) {
6513 if (isa<ConstantSDNode>(LHS)) {
6514 std::swap(LHS, RHS);
6515 }
6516 }
6517
6518 // If we have a SHL, determine the actual multiply amount
6519 if (N->getOpcode() == ISD::SHL) {
6521 if (!ShlRHS) {
6522 return SDValue();
6523 }
6524
6525 APInt ShiftAmt = ShlRHS->getAPIntValue();
6526 unsigned BitWidth = MulType.getSizeInBits();
6527 if (ShiftAmt.sge(0) && ShiftAmt.slt(BitWidth)) {
6528 APInt MulVal = APInt(BitWidth, 1) << ShiftAmt;
6529 RHS = DCI.DAG.getConstant(MulVal, DL, MulType);
6530 } else {
6531 return SDValue();
6532 }
6533 }
6534
6535 bool Signed;
6536 // Verify that our operands are demotable
6537 if (!AreMulWideOperandsDemotable(LHS, RHS, OptSize, Signed)) {
6538 return SDValue();
6539 }
6540
6541 EVT DemotedVT;
6542 if (MulType == MVT::i32) {
6543 DemotedVT = MVT::i16;
6544 } else {
6545 DemotedVT = MVT::i32;
6546 }
6547
6548 // Truncate the operands to the correct size. Note that these are just for
6549 // type consistency and will (likely) be eliminated in later phases.
6550 SDValue TruncLHS =
6551 DCI.DAG.getNode(ISD::TRUNCATE, DL, DemotedVT, LHS);
6552 SDValue TruncRHS =
6553 DCI.DAG.getNode(ISD::TRUNCATE, DL, DemotedVT, RHS);
6554
6555 unsigned Opc;
6556 if (Signed) {
6557 Opc = NVPTXISD::MUL_WIDE_SIGNED;
6558 } else {
6559 Opc = NVPTXISD::MUL_WIDE_UNSIGNED;
6560 }
6561
6562 return DCI.DAG.getNode(Opc, DL, MulType, TruncLHS, TruncRHS);
6563}
6564
6565static bool isConstOne(const SDValue &Operand) {
6566 const auto *Const = dyn_cast<ConstantSDNode>(Operand);
6567 return Const && Const->getZExtValue() == 1;
6568}
6569
6571 if (Add->getOpcode() != ISD::ADD)
6572 return SDValue();
6573
6574 if (isConstOne(Add->getOperand(0)))
6575 return Add->getOperand(1);
6576
6577 if (isConstOne(Add->getOperand(1)))
6578 return Add->getOperand(0);
6579
6580 return SDValue();
6581}
6582
6585
6587 SDValue Mul = DCI.DAG.getNode(ISD::MUL, DL, VT, X, Y);
6588 return DCI.DAG.getNode(ISD::ADD, DL, VT, Mul, X);
6589 }
6590
6591 return SDValue();
6592}
6593
6595 SDLoc DL,
6597 if (Select->getOpcode() != ISD::SELECT)
6598 return SDValue();
6599
6600 SDValue Cond = Select->getOperand(0);
6601
6602 unsigned ConstOpNo;
6603 if (isConstOne(Select->getOperand(1)))
6604 ConstOpNo = 1;
6605 else if (isConstOne(Select->getOperand(2)))
6606 ConstOpNo = 2;
6607 else
6608 return SDValue();
6609
6610 SDValue Y = Select->getOperand((ConstOpNo == 1) ? 2 : 1);
6611
6612 // Do not combine if the resulting sequence is not obviously profitable.
6614 return SDValue();
6615
6616 SDValue NewMul = DCI.DAG.getNode(ISD::MUL, DL, VT, X, Y);
6617
6618 return DCI.DAG.getNode(ISD::SELECT, DL, VT, Cond,
6619 (ConstOpNo == 1) ? X : NewMul,
6620 (ConstOpNo == 1) ? NewMul : X);
6621}
6622
6623static SDValue
6626
6627 EVT VT = N0.getValueType();
6628 if (VT.isVector())
6629 return SDValue();
6630
6631 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
6632 return SDValue();
6633
6634 SDLoc DL(N);
6635
6636 // (mul x, (add y, 1)) -> (add (mul x, y), x)
6637 if (SDValue Res = combineMADConstOne(N0, N1, VT, DL, DCI))
6638 return Res;
6639 if (SDValue Res = combineMADConstOne(N1, N0, VT, DL, DCI))
6640 return Res;
6641
6642 // (mul x, (select y, 1)) -> (select (mul x, y), x)
6643 if (SDValue Res = combineMulSelectConstOne(N0, N1, VT, DL, DCI))
6644 return Res;
6645 if (SDValue Res = combineMulSelectConstOne(N1, N0, VT, DL, DCI))
6646 return Res;
6647
6648 return SDValue();
6649}
6650
6651/// PerformMULCombine - Runs PTX-specific DAG combine patterns on MUL nodes.
6654 CodeGenOptLevel OptLevel) {
6655 if (OptLevel == CodeGenOptLevel::None)
6656 return SDValue();
6657
6658 if (SDValue Ret = TryMULWIDECombine(N, DCI))
6659 return Ret;
6660
6661 SDValue N0 = N->getOperand(0);
6662 SDValue N1 = N->getOperand(1);
6663 return PerformMULCombineWithOperands(N, N0, N1, DCI);
6664}
6665
6666/// Commute SHL with a bitwise logic operation when doing so exposes a common
6667/// shifted operand. For example:
6668///
6669/// Before:
6670/// N = shl (zext (LogicOp X, C)), ShiftAmount
6671/// OtherShift = shl (zext (OtherLogicOp X, OtherC)), ShiftAmount
6672///
6673/// After:
6674/// ShiftedX = shl (zext X), ShiftAmount
6675/// N = LogicOp ShiftedX, ShiftedC
6676/// OtherShift = OtherLogicOp ShiftedX, ShiftedOtherC
6677///
6678/// ShiftedC = (zext C) << ShiftAmount and ShiftedOtherC =
6679/// (zext OtherC) << ShiftAmount are folded constants. This replaces two
6680/// variable shifts with the single shared ShiftedX. Requiring another matching
6681/// shift avoids disrupting isolated address calculations where a shift may be
6682/// folded into the addressing mode.
6685 using namespace SDPatternMatch;
6686
6687 struct ShiftOfLogicOp {
6688 SDNode *Shift;
6689 SDValue LogicOp;
6690 SDValue X;
6692 unsigned ExtendOpcode;
6693 };
6694
6695 // Match a logic operation, with an optional extension, inside a SHL.
6696 auto matchShiftOfLogicOp =
6697 [&](SDNode *Shift) -> std::optional<ShiftOfLogicOp> {
6698 if (Shift->getOpcode() != ISD::SHL || !Shift->getOperand(0).hasOneUse())
6699 return std::nullopt;
6700 ShiftOfLogicOp Match;
6701 Match.Shift = Shift;
6702 Match.LogicOp = Shift->getOperand(0);
6703 Match.ExtendOpcode = 0;
6704 if (ISD::isExtOpcode(Match.LogicOp.getOpcode())) {
6705 Match.ExtendOpcode = Match.LogicOp.getOpcode();
6706 Match.LogicOp = Match.LogicOp.getOperand(0);
6707 }
6708
6709 if (!sd_match(Match.LogicOp, m_OneUse(m_BitwiseLogic(
6710 m_Value(Match.X),
6711 m_Value(Match.Constant, m_ConstInt())))))
6712 return std::nullopt;
6713
6714 return Match;
6715 };
6716
6717 // Match N as the root shift-of-logic; bail if it does not fit the pattern.
6718 const std::optional<ShiftOfLogicOp> Root = matchShiftOfLogicOp(N);
6719 if (!Root)
6720 return SDValue();
6721
6722 // Only profitable for a constant shift amount: the per-op constant shift then
6723 // folds away instead of becoming an extra variable shift.
6724 if (!isConstOrConstSplat(N->getOperand(1)))
6725 return SDValue();
6726
6727 // Collect candidate shifts that share X. Reached through another user of X,
6728 // the logic result feeds the shift directly or through an optional extend.
6729 SmallVector<SDNode *, 4> CandidateShifts;
6730 for (const SDNode *CandidateLogicOp : Root->X->users()) {
6731 if (CandidateLogicOp == Root->LogicOp.getNode())
6732 continue;
6733 for (SDNode *LogicUser : CandidateLogicOp->users()) {
6734 if (ISD::isExtOpcode(LogicUser->getOpcode())) {
6735 // shl (ext (logic X, C)): step through the extend to find the shift.
6736 for (SDNode *ExtendUser : LogicUser->users())
6737 if (ExtendUser->getOpcode() == ISD::SHL)
6738 CandidateShifts.push_back(ExtendUser);
6739 } else if (LogicUser->getOpcode() == ISD::SHL) {
6740 // shl (logic X, C): the user is already the shift.
6741 CandidateShifts.push_back(LogicUser);
6742 }
6743 }
6744 }
6745
6746 // Verify each candidate against the root's pattern: the same X, extension,
6747 // type, and shift amount.
6748 const EVT VT = N->getValueType(0);
6749 const SDValue ShiftAmount = N->getOperand(1);
6751 for (SDNode *CandidateShift : CandidateShifts) {
6752 const std::optional<ShiftOfLogicOp> Candidate =
6753 matchShiftOfLogicOp(CandidateShift);
6754 if (Candidate && Candidate->X == Root->X &&
6755 Candidate->ExtendOpcode == Root->ExtendOpcode &&
6756 CandidateShift->getValueType(0) == VT &&
6757 CandidateShift->getOperand(1) == ShiftAmount)
6758 Matches.push_back(*Candidate);
6759 }
6760 if (Matches.empty())
6761 return SDValue();
6762
6763 // Build the shared shifted X once, then rewrite the root and every match
6764 // into a logic op over it so the shift is CSE'd.
6765 SelectionDAG &DAG = DCI.DAG;
6766 const SDValue ShiftedX =
6767 DAG.getNode(ISD::SHL, SDLoc(N), VT,
6768 Root->ExtendOpcode
6769 ? DAG.getNode(Root->ExtendOpcode, SDLoc(N), VT, Root->X)
6770 : Root->X,
6771 ShiftAmount);
6772
6773 // Rebuild the logic op from shared ShiftedX and a folded constant shift.
6774 auto buildCommutedLogicOp = [&](const SDValue LogicOp, SDValue C,
6775 const SDLoc &DL) {
6776 if (Root->ExtendOpcode)
6777 C = DAG.getNode(Root->ExtendOpcode, DL, VT, C);
6778 const SDValue ShiftedC = DAG.getNode(ISD::SHL, DL, VT, C, ShiftAmount);
6779 return DAG.getNode(LogicOp.getOpcode(), DL, VT, ShiftedX, ShiftedC,
6780 LogicOp->getFlags());
6781 };
6782
6783 for (const ShiftOfLogicOp &Match : Matches)
6784 DCI.CombineTo(Match.Shift,
6785 buildCommutedLogicOp(Match.LogicOp, Match.Constant,
6786 SDLoc(Match.Shift)));
6787 return buildCommutedLogicOp(Root->LogicOp, Root->Constant, SDLoc(N));
6788}
6789
6790/// PerformSHLCombine - Runs PTX-specific DAG combine patterns on SHL nodes.
6793 CodeGenOptLevel OptLevel) {
6794 if (OptLevel > CodeGenOptLevel::None) {
6795 // Expose a shared shifted operand for CSE before mul.wide folding, which
6796 // would otherwise consume the shift.
6797 if (SDValue Ret = combineShiftOfLogicOp(N, DCI))
6798 return Ret;
6799
6800 // Try mul.wide combining at OptLevel > 0
6801 if (SDValue Ret = TryMULWIDECombine(N, DCI))
6802 return Ret;
6803 }
6804
6805 return SDValue();
6806}
6807
6810 const NVPTXSubtarget &STI) {
6811 EVT CCType = N->getValueType(0);
6812 SDValue A = N->getOperand(0);
6813 SDValue B = N->getOperand(1);
6814
6815 EVT AType = A.getValueType();
6816 if (!(CCType == MVT::v2i1 && (AType == MVT::v2f16 || AType == MVT::v2bf16)))
6817 return SDValue();
6818
6819 if (A.getValueType() == MVT::v2bf16 && !STI.hasFeature(NVPTX::SM90))
6820 return SDValue();
6821
6822 SDLoc DL(N);
6823 // setp.f16x2 returns two scalar predicates, which we need to
6824 // convert back to v2i1. The returned result will be scalarized by
6825 // the legalizer, but the comparison will remain a single vector
6826 // instruction.
6827 SDValue CCNode = DCI.DAG.getNode(
6828 A.getValueType() == MVT::v2f16 ? NVPTXISD::SETP_F16X2
6830 DL, DCI.DAG.getVTList(MVT::i1, MVT::i1), {A, B, N->getOperand(2)});
6831 return DCI.DAG.getNode(ISD::BUILD_VECTOR, DL, CCType, CCNode.getValue(0),
6832 CCNode.getValue(1));
6833}
6834
6837 SDValue Vector = peekThroughFreeze(N->getOperand(0));
6838 SDLoc DL(N);
6839 EVT VectorVT = Vector.getValueType();
6840 if (Vector->getOpcode() == ISD::LOAD && VectorVT.isSimple() &&
6841 IsPTXVectorType(VectorVT.getSimpleVT()))
6842 return SDValue(); // Native vector loads already combine nicely w/
6843 // extract_vector_elt.
6844 // Don't mess with singletons or packed types (v2*32, v2*16, v4i8 and v8i8),
6845 // we already handle them OK.
6846 if (VectorVT.getVectorNumElements() == 1 ||
6847 NVPTX::isPackedVectorTy(VectorVT) || VectorVT == MVT::v8i8)
6848 return SDValue();
6849
6850 // Don't mess with undef values as sra may be simplified to 0, not undef.
6851 if (Vector->isUndef() || ISD::allOperandsUndef(Vector.getNode()))
6852 return SDValue();
6853
6854 uint64_t VectorBits = VectorVT.getSizeInBits();
6855 // We only handle the types we can extract in-register.
6856 if (!(VectorBits == 16 || VectorBits == 32 || VectorBits == 64))
6857 return SDValue();
6858
6859 ConstantSDNode *Index = dyn_cast<ConstantSDNode>(N->getOperand(1));
6860 // Index == 0 is handled by generic DAG combiner.
6861 if (!Index || Index->getZExtValue() == 0)
6862 return SDValue();
6863
6864 MVT IVT = MVT::getIntegerVT(VectorBits);
6865 EVT EltVT = VectorVT.getVectorElementType();
6866 EVT EltIVT = EltVT.changeTypeToInteger();
6867 uint64_t EltBits = EltVT.getScalarSizeInBits();
6868
6869 SDValue Result = DCI.DAG.getNode(
6870 ISD::TRUNCATE, DL, EltIVT,
6871 DCI.DAG.getNode(
6872 ISD::SRA, DL, IVT, DCI.DAG.getNode(ISD::BITCAST, DL, IVT, Vector),
6873 DCI.DAG.getConstant(Index->getZExtValue() * EltBits, DL, IVT)));
6874
6875 // If element has non-integer type, bitcast it back to the expected type.
6876 if (EltVT != EltIVT)
6877 Result = DCI.DAG.getNode(ISD::BITCAST, DL, EltVT, Result);
6878 // Past legalizer, we may need to extent i8 -> i16 to match the register type.
6879 if (EltVT != N->getValueType(0))
6880 Result = DCI.DAG.getNode(ISD::ANY_EXTEND, DL, N->getValueType(0), Result);
6881
6882 return Result;
6883}
6884
6885/// Transform patterns like:
6886/// (select (ugt shift_amt, BitWidth-1), 0, (srl/shl x, shift_amt))
6887/// (select (ult shift_amt, BitWidth), (srl/shl x, shift_amt), 0)
6888/// Into:
6889/// (NVPTXISD::SRL_CLAMP x, shift_amt) or (NVPTXISD::SHL_CLAMP x, shift_amt)
6890///
6891/// These patterns arise from code like `s >= 32 ? 0 : x >> s`. In LLVM,
6892/// over-shifting a value results in poison, but PTX shr/shl instructions clamp
6893/// the shift amount to BitWidth, making the guard redundant.
6894///
6895/// Note: We only handle SRL and SHL, not SRA, because arithmetic right shifts
6896/// can produce 0 or -1 when shift >= BitWidth.
6897/// Note: We don't handle uge or ule. These don't appear because of
6898/// canonicalization.
6901 if (!DCI.isAfterLegalizeDAG())
6902 return SDValue();
6903
6904 using namespace SDPatternMatch;
6905 unsigned BitWidth = N->getValueType(0).getSizeInBits();
6906 SDValue ShiftAmt, ShiftOp;
6907
6908 // Match logical shifts where the shift amount in the guard matches the shift
6909 // amount in the operation.
6910 auto LogicalShift =
6911 m_AllOf(m_Value(ShiftOp),
6912 m_AnyOf(m_Srl(m_Value(), m_TruncOrSelf(m_Deferred(ShiftAmt))),
6913 m_Shl(m_Value(), m_TruncOrSelf(m_Deferred(ShiftAmt)))));
6914
6915 // shift_amt > BitWidth-1 ? 0 : shift_op
6916 bool MatchedUGT =
6917 sd_match(N, m_Select(m_SetCC(m_Value(ShiftAmt),
6919 m_SpecificCondCode(ISD::SETUGT)),
6920 m_Zero(), LogicalShift));
6921 // shift_amt < BitWidth ? shift_op : 0
6922 bool MatchedULT =
6923 !MatchedUGT &&
6924 sd_match(N, m_Select(m_SetCC(m_Value(ShiftAmt),
6926 m_SpecificCondCode(ISD::SETULT)),
6927 LogicalShift, m_Zero()));
6928
6929 if (!MatchedUGT && !MatchedULT)
6930 return SDValue();
6931
6932 // In LLVM IR, the shift amount and the value-to-be-shifted are the same
6933 // type, whereas in PTX the shift amount is always i32. Therefore when
6934 // shifting types larger than i32, we can only do this transformation if we
6935 // know that the upper bits of the shift amount are known zero.
6936 SDValue ClampAmt = ShiftOp.getOperand(1);
6937 unsigned ClampAmtBits = ClampAmt.getValueSizeInBits();
6938 if (ShiftAmt.getValueSizeInBits() > ClampAmtBits &&
6939 DCI.DAG.computeKnownBits(ShiftAmt).countMaxActiveBits() > ClampAmtBits)
6940 return SDValue();
6941
6942 // Return a clamp shift operation, which has the same semantics as PTX shift.
6943 unsigned ClampOpc = ShiftOp.getOpcode() == ISD::SRL ? NVPTXISD::SRL_CLAMP
6944 : NVPTXISD::SHL_CLAMP;
6945 return DCI.DAG.getNode(ClampOpc, SDLoc(N), ShiftOp.getValueType(),
6946 ShiftOp.getOperand(0), ClampAmt);
6947}
6948
6951 SDValue VA = N->getOperand(1);
6952 EVT VectorVT = VA.getValueType();
6953 if (VectorVT != MVT::v4i8)
6954 return SDValue();
6955
6956 // We need to split vselect into individual per-element operations Because we
6957 // use BFE/BFI instruction for byte extraction/insertion, we do end up with
6958 // 32-bit values, so we may as well do comparison as i32 to avoid conversions
6959 // to/from i16 normally used for i8 values.
6961 SDLoc DL(N);
6962 SDValue VCond = N->getOperand(0);
6963 SDValue VB = N->getOperand(2);
6964 for (int I = 0; I < 4; ++I) {
6965 SDValue C = DCI.DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i1, VCond,
6966 DCI.DAG.getConstant(I, DL, MVT::i32));
6967 SDValue EA = DCI.DAG.getAnyExtOrTrunc(
6968 DCI.DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i8, VA,
6969 DCI.DAG.getConstant(I, DL, MVT::i32)),
6970 DL, MVT::i32);
6971 SDValue EB = DCI.DAG.getAnyExtOrTrunc(
6972 DCI.DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i8, VB,
6973 DCI.DAG.getConstant(I, DL, MVT::i32)),
6974 DL, MVT::i32);
6975 E.push_back(DCI.DAG.getAnyExtOrTrunc(
6976 DCI.DAG.getNode(ISD::SELECT, DL, MVT::i32, C, EA, EB), DL, MVT::i8));
6977 }
6978 return DCI.DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v4i8, E);
6979}
6980
6981static SDValue
6983 auto VT = N->getValueType(0);
6984 if (!DCI.isAfterLegalizeDAG() ||
6985 // only process v2*16 types
6986 !(NVPTX::isPackedVectorTy(VT) && VT.is32BitVector() &&
6987 VT.getVectorNumElements() == 2))
6988 return SDValue();
6989
6990 auto Op0 = N->getOperand(0);
6991 auto Op1 = N->getOperand(1);
6992
6993 // Start out by assuming we want to take the lower 2 bytes of each i32
6994 // operand.
6995 uint64_t Op0Bytes = 0x10;
6996 uint64_t Op1Bytes = 0x54;
6997
6998 std::pair<SDValue *, uint64_t *> OpData[2] = {{&Op0, &Op0Bytes},
6999 {&Op1, &Op1Bytes}};
7000
7001 // Check that each operand is an i16, truncated from an i32 operand. We'll
7002 // select individual bytes from those original operands. Optionally, fold in a
7003 // shift right of that original operand.
7004 for (auto &[Op, OpBytes] : OpData) {
7005 // Eat up any bitcast
7006 if (Op->getOpcode() == ISD::BITCAST)
7007 *Op = Op->getOperand(0);
7008
7009 if (!(Op->getValueType() == MVT::i16 && Op->getOpcode() == ISD::TRUNCATE &&
7010 Op->getOperand(0).getValueType() == MVT::i32))
7011 return SDValue();
7012
7013 // If the truncate has multiple uses, this optimization can increase
7014 // register pressure
7015 if (!Op->hasOneUse())
7016 return SDValue();
7017
7018 *Op = Op->getOperand(0);
7019
7020 // Optionally, fold in a shift-right of the original operand and let permute
7021 // pick the two higher bytes of the original value directly.
7022 if (Op->getOpcode() == ISD::SRL && isa<ConstantSDNode>(Op->getOperand(1))) {
7023 if (cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue() == 16) {
7024 // Shift the PRMT byte selector to pick upper bytes from each respective
7025 // value, instead of the lower ones: 0x10 -> 0x32, 0x54 -> 0x76
7026 assert((*OpBytes == 0x10 || *OpBytes == 0x54) &&
7027 "PRMT selector values out of range");
7028 *OpBytes += 0x22;
7029 *Op = Op->getOperand(0);
7030 }
7031 }
7032 }
7033
7034 SDLoc DL(N);
7035 auto &DAG = DCI.DAG;
7036
7037 auto PRMT =
7038 getPRMT(DAG.getBitcast(MVT::i32, Op0), DAG.getBitcast(MVT::i32, Op1),
7039 (Op1Bytes << 8) | Op0Bytes, DL, DAG);
7040 return DAG.getBitcast(VT, PRMT);
7041}
7042
7045 auto *ASCN1 = cast<AddrSpaceCastSDNode>(N);
7046
7047 if (auto *ASCN2 = dyn_cast<AddrSpaceCastSDNode>(ASCN1->getOperand(0))) {
7048 assert(ASCN2->getDestAddressSpace() == ASCN1->getSrcAddressSpace());
7049
7050 // Fold asc[B -> A](asc[A -> B](x)) -> x
7051 if (ASCN1->getDestAddressSpace() == ASCN2->getSrcAddressSpace())
7052 return ASCN2->getOperand(0);
7053 }
7054
7055 return SDValue();
7056}
7057
7058// Given a constant selector value and a prmt mode, return the selector value
7059// normalized to the generic prmt mode. See the PTX ISA documentation for more
7060// details:
7061// https://docs.nvidia.com/cuda/parallel-thread-execution/index.html#data-movement-and-conversion-instructions-prmt
7062static APInt getPRMTSelector(const APInt &Selector, unsigned Mode) {
7063 assert(Selector.getBitWidth() == 32 && "PRMT must have i32 operands");
7064
7066 return Selector;
7067
7068 const unsigned V = Selector.trunc(2).getZExtValue();
7069
7070 const auto GetSelector = [](unsigned S0, unsigned S1, unsigned S2,
7071 unsigned S3) {
7072 return APInt(32, S0 | (S1 << 4) | (S2 << 8) | (S3 << 12));
7073 };
7074
7075 switch (Mode) {
7077 return GetSelector(V, V + 1, V + 2, V + 3);
7079 return GetSelector(V, (V - 1) & 7, (V - 2) & 7, (V - 3) & 7);
7081 return GetSelector(V, V, V, V);
7083 return GetSelector(V, std::max(V, 1U), std::max(V, 2U), 3U);
7085 return GetSelector(0, std::min(V, 1U), std::min(V, 2U), V);
7087 unsigned V1 = (V & 1) << 1;
7088 return GetSelector(V1, V1 + 1, V1, V1 + 1);
7089 }
7090 default:
7091 llvm_unreachable("Invalid PRMT mode");
7092 }
7093}
7094
7095static APInt computePRMT(APInt A, APInt B, APInt Selector, unsigned Mode) {
7096 assert(A.getBitWidth() == 32 && B.getBitWidth() == 32 &&
7097 Selector.getBitWidth() == 32 && "PRMT must have i32 operands");
7098 // {b, a} = {{b7, b6, b5, b4}, {b3, b2, b1, b0}}
7099 APInt BitField = B.concat(A);
7100 APInt SelectorVal = getPRMTSelector(Selector, Mode);
7101 APInt Result(32, 0);
7102 for (unsigned I : llvm::seq(4U)) {
7103 APInt Sel = SelectorVal.extractBits(4, I * 4);
7104 unsigned Idx = Sel.getLoBits(3).getZExtValue();
7105 unsigned Sign = Sel.getHiBits(1).getZExtValue();
7106 APInt Byte = BitField.extractBits(8, Idx * 8);
7107 if (Sign)
7108 Byte = Byte.ashr(8);
7109 Result.insertBits(Byte, I * 8);
7110 }
7111 return Result;
7112}
7113
7115 CodeGenOptLevel OptLevel) {
7116 if (OptLevel == CodeGenOptLevel::None)
7117 return SDValue();
7118
7119 // Constant fold PRMT
7120 if (isa<ConstantSDNode>(N->getOperand(0)) &&
7121 isa<ConstantSDNode>(N->getOperand(1)) &&
7122 isa<ConstantSDNode>(N->getOperand(2)))
7123 return DCI.DAG.getConstant(computePRMT(N->getConstantOperandAPInt(0),
7124 N->getConstantOperandAPInt(1),
7125 N->getConstantOperandAPInt(2),
7126 N->getConstantOperandVal(3)),
7127 SDLoc(N), N->getValueType(0));
7128 return SDValue();
7129}
7130
7131// During call lowering we wrap the return values in a ProxyReg node which
7132// depend on the chain value produced by the completed call. This ensures that
7133// the full call is emitted in cases where libcalls are used to legalize
7134// operations. To improve the functioning of other DAG combines we pull all
7135// operations we can through one of these nodes, ensuring that the ProxyReg
7136// directly wraps a load. That is:
7137//
7138// (ProxyReg (zext (load retval0))) => (zext (ProxyReg (load retval0)))
7139//
7142 switch (R.getOpcode()) {
7143 case ISD::TRUNCATE:
7144 case ISD::ANY_EXTEND:
7145 case ISD::SIGN_EXTEND:
7146 case ISD::ZERO_EXTEND:
7147 case ISD::BITCAST: {
7148 if (SDValue V = sinkProxyReg(R.getOperand(0), Chain, DCI))
7149 return DCI.DAG.getNode(R.getOpcode(), SDLoc(R), R.getValueType(), V);
7150 return SDValue();
7151 }
7152 case ISD::SHL:
7153 case ISD::SRL:
7154 case ISD::SRA:
7155 case ISD::OR: {
7156 if (SDValue A = sinkProxyReg(R.getOperand(0), Chain, DCI))
7157 if (SDValue B = sinkProxyReg(R.getOperand(1), Chain, DCI))
7158 return DCI.DAG.getNode(R.getOpcode(), SDLoc(R), R.getValueType(), A, B);
7159 return SDValue();
7160 }
7161 case ISD::Constant:
7162 return R;
7163 case ISD::LOAD:
7164 case NVPTXISD::LoadV2:
7165 case NVPTXISD::LoadV4: {
7166 return DCI.DAG.getNode(NVPTXISD::ProxyReg, SDLoc(R), R.getValueType(),
7167 {Chain, R});
7168 }
7169 case ISD::BUILD_VECTOR: {
7170 if (DCI.isBeforeLegalize())
7171 return SDValue();
7172
7174 for (auto &Op : R->ops()) {
7175 SDValue V = sinkProxyReg(Op, Chain, DCI);
7176 if (!V)
7177 return SDValue();
7178 Ops.push_back(V);
7179 }
7180 return DCI.DAG.getNode(ISD::BUILD_VECTOR, SDLoc(R), R.getValueType(), Ops);
7181 }
7183 if (DCI.isBeforeLegalize())
7184 return SDValue();
7185
7186 if (SDValue V = sinkProxyReg(R.getOperand(0), Chain, DCI))
7188 R.getValueType(), V, R.getOperand(1));
7189 return SDValue();
7190 }
7191 default:
7192 return SDValue();
7193 }
7194}
7195
7198 const bool IsFTZ =
7199 IID == Intrinsic::nvvm_fadd_ftz || IID == Intrinsic::nvvm_fadd_ftz_sat;
7200 const bool IsSat =
7201 IID == Intrinsic::nvvm_fadd_sat || IID == Intrinsic::nvvm_fadd_ftz_sat;
7202 switch (VT.getScalarType().getSimpleVT().SimpleTy) {
7203 case MVT::f16: {
7204 static constexpr unsigned SubRNOpcodes[2][2] = {
7205 {NVPTXISD::SUB_RN, NVPTXISD::SUB_RN_SAT},
7206 {NVPTXISD::SUB_RN_FTZ, NVPTXISD::SUB_RN_FTZ_SAT}};
7207 return SubRNOpcodes[IsFTZ][IsSat];
7208 }
7209 case MVT::bf16:
7210 return NVPTXISD::SUB_RN;
7211 case MVT::f32: {
7212 // for f32x2 inputs
7213 if (!VT.isVector() || IsSat)
7214 return 0;
7215 static constexpr unsigned SubF32x2Opcodes[4][2] = {
7216 {NVPTXISD::SUB_RZ, NVPTXISD::SUB_RZ_FTZ}, // RZ
7217 {NVPTXISD::SUB_RN, NVPTXISD::SUB_RN_FTZ}, // RN
7218 {NVPTXISD::SUB_RP, NVPTXISD::SUB_RP_FTZ}, // RP
7219 {NVPTXISD::SUB_RM, NVPTXISD::SUB_RM_FTZ}}; // RM
7220 return SubF32x2Opcodes[static_cast<unsigned>(RoundingMode)][IsFTZ];
7221 }
7222 default:
7223 return 0;
7224 }
7225}
7226
7228 Intrinsic::ID AddIntrinsicID,
7230 const EVT VT = N->getValueType(0);
7231 const unsigned Opc = getFAddWithNegOpcode(VT, AddIntrinsicID, RoundingMode);
7232 if (!Opc)
7233 return SDValue();
7234
7235 SDValue Op1 = N->getOperand(1);
7236 SDValue Op2 = N->getOperand(2);
7237
7238 SDValue SubOp1, SubOp2;
7239
7240 if (Op1.getOpcode() == ISD::FNEG) {
7241 SubOp1 = Op2;
7242 SubOp2 = Op1.getOperand(0);
7243 } else if (Op2.getOpcode() == ISD::FNEG) {
7244 SubOp1 = Op1;
7245 SubOp2 = Op2.getOperand(0);
7246 } else {
7247 return SDValue();
7248 }
7249
7250 return DAG.getNode(Opc, SDLoc(N), VT, SubOp1, SubOp2);
7251}
7252
7253// TODO: Remove the type-legality checks here once
7254// https://github.com/llvm/llvm-project/pull/172442 lands, adding support for
7255// explicit type constraints for overloaded intrinsics in tablegen.
7256static bool isSupportedFAdd(EVT VT, const NVPTXSubtarget &STI,
7257 Intrinsic::ID IID,
7260 return false;
7261
7262 const bool IsRN = RoundingMode == APFloat::rmNearestTiesToEven;
7263 const bool IsFTZ =
7264 IID == Intrinsic::nvvm_fadd_ftz || IID == Intrinsic::nvvm_fadd_ftz_sat;
7265 const bool IsSat =
7266 IID == Intrinsic::nvvm_fadd_sat || IID == Intrinsic::nvvm_fadd_ftz_sat;
7267 switch (VT.getScalarType().getSimpleVT().SimpleTy) {
7268 case MVT::f16:
7269 return IsRN;
7270 case MVT::bf16:
7271 return IsRN && !IsSat && !IsFTZ && STI.hasNativeBF16Support(ISD::FADD);
7272 case MVT::f32:
7273 return !VT.isVector() || (!IsSat && STI.hasF32x2Instructions());
7274 case MVT::f64:
7275 return !VT.isVector() && !IsSat && !IsFTZ;
7276 default:
7277 return false;
7278 }
7279}
7280
7282 Intrinsic::ID IID,
7284 const EVT VT = N->getValueType(0);
7287 Twine(Intrinsic::getBaseName(IID)) + " with rounding mode " +
7288 nvvm::GetRoundingModeName(RoundingMode) + " and operand type " +
7289 VT.getEVTString() + " is not supported on this target",
7290 SDLoc(N).getDebugLoc()));
7291 return DAG.getPOISON(VT);
7292}
7293
7296 const NVPTXSubtarget &STI) {
7297 const Intrinsic::ID IID =
7298 static_cast<Intrinsic::ID>(N->getConstantOperandVal(0));
7299
7300 switch (IID) {
7301 default:
7302 break;
7303 case Intrinsic::nvvm_fadd:
7304 case Intrinsic::nvvm_fadd_ftz:
7305 case Intrinsic::nvvm_fadd_sat:
7306 case Intrinsic::nvvm_fadd_ftz_sat: {
7307 const auto RoundingMode = static_cast<APFloat::roundingMode>(
7308 N->getConstantOperandAPInt(3).getSExtValue());
7309 if (!isSupportedFAdd(N->getValueType(0), STI, IID, RoundingMode))
7310 return diagnoseUnsupportedFAdd(N, DCI.DAG, IID, RoundingMode);
7311 return combineFAddWithNeg(N, DCI.DAG, IID, RoundingMode);
7312 }
7313 }
7314 return SDValue();
7315}
7316
7319
7320 SDValue Chain = N->getOperand(0);
7321 SDValue Reg = N->getOperand(1);
7322
7323 // If the ProxyReg is not wrapping a load, try to pull the operations through
7324 // the ProxyReg.
7325 if (Reg.getOpcode() != ISD::LOAD) {
7326 if (SDValue V = sinkProxyReg(Reg, Chain, DCI))
7327 return V;
7328 }
7329
7330 return SDValue();
7331}
7332
7333SDValue NVPTXTargetLowering::PerformDAGCombine(SDNode *N,
7334 DAGCombinerInfo &DCI) const {
7336 switch (N->getOpcode()) {
7337 default:
7338 break;
7339 case ISD::ADD:
7340 return PerformADDCombine(N, DCI, OptLevel);
7341 case ISD::ADDRSPACECAST:
7342 return combineADDRSPACECAST(N, DCI);
7343 case ISD::SIGN_EXTEND:
7344 case ISD::ZERO_EXTEND:
7345 return combineSZExtToMulWide(N, DCI, OptLevel);
7346 case ISD::BUILD_VECTOR:
7347 return PerformBUILD_VECTORCombine(N, DCI);
7349 return PerformEXTRACTCombine(N, DCI);
7350 case ISD::FADD:
7351 return performFADDCombine(N, DCI, OptLevel);
7352 case ISD::FMA:
7353 case ISD::FMUL:
7354 case ISD::FSUB:
7355 return performScalarizeV2F32Op(N, DCI, OptLevel);
7356 case ISD::FMAXNUM:
7357 case ISD::FMINNUM:
7358 case ISD::FMAXIMUM:
7359 case ISD::FMINIMUM:
7360 case ISD::FMAXIMUMNUM:
7361 case ISD::FMINIMUMNUM:
7362 return PerformFMinMaxCombine(N, DCI, STI);
7363 case ISD::LOAD:
7364 case NVPTXISD::LoadV2:
7365 case NVPTXISD::LoadV4:
7366 return combineLOAD(N, DCI, STI);
7367 case ISD::MUL:
7368 return PerformMULCombine(N, DCI, OptLevel);
7369 case NVPTXISD::PRMT:
7370 return combinePRMT(N, DCI, OptLevel);
7371 case NVPTXISD::ProxyReg:
7372 return combineProxyReg(N, DCI);
7373 case ISD::SETCC:
7374 return PerformSETCCCombine(N, DCI, STI);
7375 case ISD::SHL:
7376 return PerformSHLCombine(N, DCI, OptLevel);
7377 case ISD::STORE:
7378 case NVPTXISD::StoreV2:
7379 case NVPTXISD::StoreV4:
7380 return combineSTORE(N, DCI, STI);
7381 case ISD::SELECT:
7382 return PerformSELECTShiftCombine(N, DCI);
7383 case ISD::VSELECT:
7384 return PerformVSELECTCombine(N, DCI);
7386 return combineIntrinsicWOChain(N, DCI, STI);
7387 }
7388 return SDValue();
7389}
7390
7393 // Handle bitcasting to v2i8 without hitting the default promotion
7394 // strategy which goes through stack memory.
7395 SDValue Op(Node, 0);
7396 EVT ToVT = Op->getValueType(0);
7397 if (ToVT != MVT::v2i8) {
7398 return;
7399 }
7400
7401 // Bitcast to i16 and unpack elements into a vector
7402 SDLoc DL(Node);
7403 SDValue AsInt = DAG.getBitcast(MVT::i16, Op->getOperand(0));
7404 SDValue Vec0 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, AsInt);
7405 SDValue Const8 = DAG.getConstant(8, DL, MVT::i16);
7406 SDValue Vec1 =
7407 DAG.getNode(ISD::TRUNCATE, DL, MVT::i8,
7408 DAG.getNode(ISD::SRL, DL, MVT::i16, {AsInt, Const8}));
7409 Results.push_back(
7410 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i8, {Vec0, Vec1}));
7411}
7412
7415 SDValue Chain = N->getOperand(0);
7416 SDValue Intrin = N->getOperand(1);
7417 SDLoc DL(N);
7418
7419 // Get the intrinsic ID
7420 unsigned IntrinNo = Intrin.getNode()->getAsZExtVal();
7421 switch (IntrinNo) {
7422 default:
7423 return;
7424 case Intrinsic::nvvm_ldu_global_i:
7425 case Intrinsic::nvvm_ldu_global_f:
7426 case Intrinsic::nvvm_ldu_global_p: {
7427 EVT ResVT = N->getValueType(0);
7428
7429 if (ResVT.isVector()) {
7430 // Vector LDG/LDU
7431
7432 unsigned NumElts = ResVT.getVectorNumElements();
7433 EVT EltVT = ResVT.getVectorElementType();
7434
7435 // Since LDU/LDG are target nodes, we cannot rely on DAG type
7436 // legalization.
7437 // Therefore, we must ensure the type is legal. For i1 and i8, we set the
7438 // loaded type to i16 and propagate the "real" type as the memory type.
7439 bool NeedTrunc = false;
7440 if (EltVT.getSizeInBits() < 16) {
7441 EltVT = MVT::i16;
7442 NeedTrunc = true;
7443 }
7444
7445 unsigned Opcode = 0;
7446 SDVTList LdResVTs;
7447
7448 switch (NumElts) {
7449 default:
7450 return;
7451 case 2:
7452 Opcode = NVPTXISD::LDUV2;
7453 LdResVTs = DAG.getVTList(EltVT, EltVT, MVT::Other);
7454 break;
7455 case 4: {
7456 Opcode = NVPTXISD::LDUV4;
7457 EVT ListVTs[] = { EltVT, EltVT, EltVT, EltVT, MVT::Other };
7458 LdResVTs = DAG.getVTList(ListVTs);
7459 break;
7460 }
7461 }
7462
7463 SmallVector<SDValue, 8> OtherOps;
7464
7465 // Copy regular operands
7466
7467 OtherOps.push_back(Chain); // Chain
7468 // Skip operand 1 (intrinsic ID)
7469 // Others
7470 OtherOps.append(N->op_begin() + 2, N->op_end());
7471
7473
7474 SDValue NewLD = DAG.getMemIntrinsicNode(Opcode, DL, LdResVTs, OtherOps,
7475 MemSD->getMemoryVT(),
7476 MemSD->getMemOperand());
7477
7478 SmallVector<SDValue, 4> ScalarRes;
7479
7480 for (unsigned i = 0; i < NumElts; ++i) {
7481 SDValue Res = NewLD.getValue(i);
7482 if (NeedTrunc)
7483 Res =
7484 DAG.getNode(ISD::TRUNCATE, DL, ResVT.getVectorElementType(), Res);
7485 ScalarRes.push_back(Res);
7486 }
7487
7488 SDValue LoadChain = NewLD.getValue(NumElts);
7489
7490 SDValue BuildVec =
7491 DAG.getBuildVector(ResVT, DL, ScalarRes);
7492
7493 Results.push_back(BuildVec);
7494 Results.push_back(LoadChain);
7495 } else {
7496 // i8 LDG/LDU
7497 assert(ResVT.isSimple() && ResVT.getSimpleVT().SimpleTy == MVT::i8 &&
7498 "Custom handling of non-i8 ldu/ldg?");
7499
7500 // Just copy all operands as-is
7502
7503 // Force output to i16
7504 SDVTList LdResVTs = DAG.getVTList(MVT::i16, MVT::Other);
7505
7507
7508 // We make sure the memory type is i8, which will be used during isel
7509 // to select the proper instruction.
7510 SDValue NewLD =
7512 MVT::i8, MemSD->getMemOperand());
7513
7514 Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i8,
7515 NewLD.getValue(0)));
7516 Results.push_back(NewLD.getValue(1));
7517 }
7518 return;
7519 }
7520
7521 case Intrinsic::nvvm_tcgen05_ld_16x64b_x1:
7522 case Intrinsic::nvvm_tcgen05_ld_16x64b_x4:
7523 case Intrinsic::nvvm_tcgen05_ld_16x64b_x8:
7524 case Intrinsic::nvvm_tcgen05_ld_16x64b_x16:
7525 case Intrinsic::nvvm_tcgen05_ld_16x64b_x32:
7526 case Intrinsic::nvvm_tcgen05_ld_16x64b_x64:
7527 case Intrinsic::nvvm_tcgen05_ld_16x64b_x128:
7528 case Intrinsic::nvvm_tcgen05_ld_32x32b_x1:
7529 case Intrinsic::nvvm_tcgen05_ld_32x32b_x4:
7530 case Intrinsic::nvvm_tcgen05_ld_32x32b_x8:
7531 case Intrinsic::nvvm_tcgen05_ld_32x32b_x16:
7532 case Intrinsic::nvvm_tcgen05_ld_32x32b_x32:
7533 case Intrinsic::nvvm_tcgen05_ld_32x32b_x64:
7534 case Intrinsic::nvvm_tcgen05_ld_32x32b_x128:
7535 case Intrinsic::nvvm_tcgen05_ld_16x128b_x2:
7536 case Intrinsic::nvvm_tcgen05_ld_16x128b_x4:
7537 case Intrinsic::nvvm_tcgen05_ld_16x128b_x8:
7538 case Intrinsic::nvvm_tcgen05_ld_16x128b_x16:
7539 case Intrinsic::nvvm_tcgen05_ld_16x128b_x32:
7540 case Intrinsic::nvvm_tcgen05_ld_16x128b_x64:
7541 case Intrinsic::nvvm_tcgen05_ld_16x256b_x1:
7542 case Intrinsic::nvvm_tcgen05_ld_16x256b_x2:
7543 case Intrinsic::nvvm_tcgen05_ld_16x256b_x4:
7544 case Intrinsic::nvvm_tcgen05_ld_16x256b_x8:
7545 case Intrinsic::nvvm_tcgen05_ld_16x256b_x16:
7546 case Intrinsic::nvvm_tcgen05_ld_16x256b_x32:
7547 if (auto Res = lowerTcgen05Ld(N, DAG)) {
7548 Results.push_back(Res->first);
7549 Results.push_back(Res->second);
7550 }
7551 return;
7552
7553 case Intrinsic::nvvm_tcgen05_ld_16x32bx2_x1:
7554 case Intrinsic::nvvm_tcgen05_ld_16x32bx2_x4:
7555 case Intrinsic::nvvm_tcgen05_ld_16x32bx2_x8:
7556 case Intrinsic::nvvm_tcgen05_ld_16x32bx2_x16:
7557 case Intrinsic::nvvm_tcgen05_ld_16x32bx2_x32:
7558 case Intrinsic::nvvm_tcgen05_ld_16x32bx2_x64:
7559 case Intrinsic::nvvm_tcgen05_ld_16x32bx2_x128:
7560 if (auto Res = lowerTcgen05Ld(N, DAG, /*HasOffset=*/true)) {
7561 Results.push_back(Res->first);
7562 Results.push_back(Res->second);
7563 }
7564 return;
7565
7566 case Intrinsic::nvvm_tcgen05_ld_red_32x32b_x8_i32:
7567 case Intrinsic::nvvm_tcgen05_ld_red_32x32b_x8_f32:
7568 case Intrinsic::nvvm_tcgen05_ld_red_32x32b_x64_i32:
7569 case Intrinsic::nvvm_tcgen05_ld_red_32x32b_x64_f32:
7570 case Intrinsic::nvvm_tcgen05_ld_red_32x32b_x4_i32:
7571 case Intrinsic::nvvm_tcgen05_ld_red_32x32b_x4_f32:
7572 case Intrinsic::nvvm_tcgen05_ld_red_32x32b_x32_i32:
7573 case Intrinsic::nvvm_tcgen05_ld_red_32x32b_x32_f32:
7574 case Intrinsic::nvvm_tcgen05_ld_red_32x32b_x16_i32:
7575 case Intrinsic::nvvm_tcgen05_ld_red_32x32b_x16_f32:
7576 case Intrinsic::nvvm_tcgen05_ld_red_32x32b_x128_i32:
7577 case Intrinsic::nvvm_tcgen05_ld_red_32x32b_x128_f32:
7578 case Intrinsic::nvvm_tcgen05_ld_red_16x32bx2_x8_i32:
7579 case Intrinsic::nvvm_tcgen05_ld_red_16x32bx2_x8_f32:
7580 case Intrinsic::nvvm_tcgen05_ld_red_16x32bx2_x64_i32:
7581 case Intrinsic::nvvm_tcgen05_ld_red_16x32bx2_x64_f32:
7582 case Intrinsic::nvvm_tcgen05_ld_red_16x32bx2_x4_i32:
7583 case Intrinsic::nvvm_tcgen05_ld_red_16x32bx2_x4_f32:
7584 case Intrinsic::nvvm_tcgen05_ld_red_16x32bx2_x32_i32:
7585 case Intrinsic::nvvm_tcgen05_ld_red_16x32bx2_x32_f32:
7586 case Intrinsic::nvvm_tcgen05_ld_red_16x32bx2_x16_i32:
7587 case Intrinsic::nvvm_tcgen05_ld_red_16x32bx2_x16_f32:
7588 case Intrinsic::nvvm_tcgen05_ld_red_16x32bx2_x128_i32:
7589 case Intrinsic::nvvm_tcgen05_ld_red_16x32bx2_x128_f32:
7590 if (auto Res = lowerTcgen05LdRed(N, DAG)) {
7591 Results.push_back(std::get<0>(*Res));
7592 Results.push_back(std::get<1>(*Res));
7593 Results.push_back(std::get<2>(*Res));
7594 }
7595 return;
7596 }
7597}
7598
7601 // Change the CopyFromReg to output 2 64-bit results instead of a 128-bit
7602 // result so that it can pass the legalization
7603 SDLoc DL(N);
7604 SDValue Chain = N->getOperand(0);
7605 SDValue Reg = N->getOperand(1);
7606 SDValue Glue = N->getOperand(2);
7607
7608 assert(Reg.getValueType() == MVT::i128 &&
7609 "Custom lowering for CopyFromReg with 128-bit reg only");
7610 SmallVector<EVT, 4> ResultsType = {MVT::i64, MVT::i64, N->getValueType(1),
7611 N->getValueType(2)};
7612 SmallVector<SDValue, 3> NewOps = {Chain, Reg, Glue};
7613
7614 SDValue NewValue = DAG.getNode(ISD::CopyFromReg, DL, ResultsType, NewOps);
7615 SDValue Pair = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i128,
7616 {NewValue.getValue(0), NewValue.getValue(1)});
7617
7618 Results.push_back(Pair);
7619 Results.push_back(NewValue.getValue(2));
7620 Results.push_back(NewValue.getValue(3));
7621}
7622
7624 const TargetLowering &TLI,
7626 SDValue Chain = N->getOperand(0);
7627 SDValue Reg = N->getOperand(1);
7628
7629 MVT VT = TLI.getRegisterType(*DAG.getContext(), Reg.getValueType());
7630
7631 SDValue NewReg = DAG.getAnyExtOrTrunc(Reg, SDLoc(N), VT);
7632 SDValue NewProxy =
7633 DAG.getNode(NVPTXISD::ProxyReg, SDLoc(N), VT, {Chain, NewReg});
7634 SDValue Res = DAG.getAnyExtOrTrunc(NewProxy, SDLoc(N), N->getValueType(0));
7635
7636 Results.push_back(Res);
7637}
7638
7640 const NVPTXSubtarget &STI,
7642 assert(N->getValueType(0) == MVT::i128 &&
7643 "Custom lowering for atomic128 only supports i128");
7644
7646 SDLoc dl(N);
7647
7648 if (!STI.hasAtomSwap128()) {
7651 "Support for b128 atomics introduced in PTX ISA version 8.3 and "
7652 "requires target sm_90.",
7653 dl.getDebugLoc()));
7654
7655 Results.push_back(DAG.getUNDEF(MVT::i128));
7656 Results.push_back(AN->getOperand(0)); // Chain
7657 return;
7658 }
7659
7661 Ops.push_back(AN->getOperand(0)); // Chain
7662 Ops.push_back(AN->getOperand(1)); // Ptr
7663 for (const auto &Op : AN->ops().drop_front(2)) {
7664 // Low part
7665 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i64, Op,
7666 DAG.getIntPtrConstant(0, dl)));
7667 // High part
7668 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i64, Op,
7669 DAG.getIntPtrConstant(1, dl)));
7670 }
7671 unsigned Opcode = N->getOpcode() == ISD::ATOMIC_SWAP
7674 SDVTList Tys = DAG.getVTList(MVT::i64, MVT::i64, MVT::Other);
7675 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys, Ops, MVT::i128,
7676 AN->getMemOperand());
7677 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i128,
7678 {Result.getValue(0), Result.getValue(1)}));
7679 Results.push_back(Result.getValue(2));
7680}
7681
7682void NVPTXTargetLowering::ReplaceNodeResults(
7684 switch (N->getOpcode()) {
7685 default:
7686 report_fatal_error("Unhandled custom legalization");
7687 case ISD::BITCAST:
7688 ReplaceBITCAST(N, DAG, Results);
7689 return;
7690 case ISD::LOAD:
7691 case ISD::MLOAD:
7692 replaceLoadVector(N, DAG, Results, STI);
7693 return;
7696 return;
7697 case ISD::CopyFromReg:
7699 return;
7700 case NVPTXISD::ProxyReg:
7701 replaceProxyReg(N, DAG, *this, Results);
7702 return;
7704 case ISD::ATOMIC_SWAP:
7705 replaceAtomicSwap128(N, DAG, STI, Results);
7706 return;
7707 }
7708}
7709
7712 Type *Ty = AI->getValOperand()->getType();
7713
7714 // Try to lower LLVM atomicrmw fadd to PTX atomic.add. This is complicated
7715 // by the weird FTZ behavior PTX atom.add has:
7716 // - atom.add.f32 on global memory flushes denormals
7717 // - atom.add.f32 on shared memory does not flush denormals
7718 // - atom.add.f16 and atomic.add.bf16 never flush denormals
7719 //
7720 // We lower to atom.add only if the function's FTZ behavior matches that of
7721 // atom.add; otherwise, we lower to a CAS loop. But we always allow
7722 // atomic.add.bf16; even though it never flushes denormals, we never flush
7723 // bf16 denormals when doing regular arithmetic, even when FTZ is enabled.
7724 if (AI->isFloatingPointOperation() &&
7726 const Function *F = AI->getFunction();
7727
7728 // AllowFTZAtomics forces atom.add regardless of the FTZ mismatch.
7729 if (Ty->isFloatTy()) {
7730 const bool FTZ = F->getDenormalMode(APFloat::IEEEsingle()).Output ==
7733 switch (AI->getPointerAddressSpace()) {
7735 UseNative |= FTZ;
7736 break;
7739 UseNative |= !FTZ;
7740 break;
7741 }
7742 if (UseNative)
7744 }
7745
7746 if (Ty->isHalfTy()) {
7747 // atom.add.f16 never flushes denormals, so it only agrees with a
7748 // function that is not in FTZ mode for f16.
7749 const bool FTZ = F->getDenormalMode(APFloat::IEEEhalf()).Output ==
7751 if ((!FTZ || AllowFTZAtomics) && STI.hasFeature(NVPTX::SM70) &&
7752 STI.hasFeature(NVPTX::PTX63))
7754 }
7755
7756 if (Ty->isBFloatTy() && STI.hasFeature(NVPTX::SM90))
7758
7759 if (Ty->isDoubleTy() && STI.hasAtomAddF64())
7761 }
7762
7763 // PTX's only atomic fp op is `add`; all other ops expand to a CAS loop.
7764 if (AI->isFloatingPointOperation())
7766
7767 if (Ty->isVectorTy())
7769
7770 assert(Ty->isIntegerTy() && "Ty should be integer at this point");
7771 const unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
7772
7773 switch (AI->getOperation()) {
7774 default:
7777 if (BitWidth == 128)
7779 [[fallthrough]];
7783 switch (BitWidth) {
7784 case 8:
7785 case 16:
7787 case 32:
7789 case 64:
7790 if (STI.hasAtomBitwise64())
7793 case 128:
7795 default:
7796 llvm_unreachable("unsupported width encountered");
7797 }
7804 switch (BitWidth) {
7805 case 8:
7806 case 16:
7808 case 32:
7810 case 64:
7811 if (STI.hasAtomMinMax64())
7814 case 128:
7816 default:
7817 llvm_unreachable("unsupported width encountered");
7818 }
7821 switch (BitWidth) {
7822 case 32:
7824 case 8:
7825 case 16:
7826 case 64:
7827 case 128:
7829 default:
7830 llvm_unreachable("unsupported width encountered");
7831 }
7832 }
7833
7835}
7836
7838 const Instruction *I) const {
7839 // This function returns true iff the operation is emulated using a CAS-loop,
7840 // or if it has the memory order seq_cst (which is not natively supported in
7841 // the PTX `atom` instruction).
7842 //
7843 // atomicrmw and cmpxchg instructions not efficiently supported by PTX
7844 // are lowered to CAS emulation loops that preserve their memory order,
7845 // syncscope, and volatile semantics. For PTX, it is more efficient to use
7846 // atom.cas.relaxed.sco instructions within the loop, and fences before and
7847 // after the loop to restore order.
7848 //
7849 // Atomic instructions efficiently supported by PTX are lowered to
7850 // `atom.<op>.<sem>.<scope` instruction with their corresponding memory order
7851 // and scope. Since PTX does not support seq_cst, we emulate it by lowering to
7852 // a fence.sc followed by an atom according to the PTX atomics ABI
7853 // https://docs.nvidia.com/cuda/ptx-writers-guide-to-interoperability/atomic-abi.html
7854 if (auto *CI = dyn_cast<AtomicCmpXchgInst>(I))
7855 return (cast<IntegerType>(CI->getCompareOperand()->getType())
7856 ->getBitWidth() < STI.getMinCmpXchgSizeInBits()) ||
7857 CI->getMergedOrdering() == AtomicOrdering::SequentiallyConsistent;
7858 if (auto *RI = dyn_cast<AtomicRMWInst>(I))
7860 RI->getOrdering() == AtomicOrdering::SequentiallyConsistent;
7861 return false;
7862}
7863
7865 const Instruction *I) const {
7866 // If the operation is emulated by a CAS-loop, we lower the instruction to
7867 // atom.<op>.relaxed, since AtomicExpandPass will insert fences for enforcing
7868 // the correct memory ordering around the CAS loop.
7869 //
7870 // When the operation is not emulated, but the memory order is seq_cst,
7871 // we must lower to "fence.sc.<scope>; atom.<op>.acquire.<scope>;" to conform
7872 // to the PTX atomics ABI.
7873 // https://docs.nvidia.com/cuda/ptx-writers-guide-to-interoperability/atomic-abi.html
7874 // For such cases, emitLeadingFence() will separately insert the leading
7875 // "fence.sc.<scope>;". Here, we only set the memory order to acquire.
7876 //
7877 // Otherwise, the operation is not emulated, and the memory order is not
7878 // seq_cst. In this case, the LLVM memory order is natively supported by the
7879 // PTX `atom` instruction, and we just lower to the corresponding
7880 // `atom.<op>.relaxed|acquire|release|acq_rel". For such cases, this function
7881 // will NOT be called.
7882 // prerequisite: shouldInsertFencesForAtomic() should have returned `true` for
7883 // I before its memory order was modified.
7884 if (auto *CI = dyn_cast<AtomicCmpXchgInst>(I);
7885 CI && CI->getMergedOrdering() == AtomicOrdering::SequentiallyConsistent &&
7886 cast<IntegerType>(CI->getCompareOperand()->getType())->getBitWidth() >=
7887 STI.getMinCmpXchgSizeInBits())
7889 else if (auto *RI = dyn_cast<AtomicRMWInst>(I);
7890 RI && RI->getOrdering() == AtomicOrdering::SequentiallyConsistent &&
7893
7895}
7896
7898 Instruction *Inst,
7899 AtomicOrdering Ord) const {
7900 // prerequisite: shouldInsertFencesForAtomic() should have returned `true` for
7901 // `Inst` before its memory order was modified. We cannot enforce this with an
7902 // assert, because AtomicExpandPass will have modified the memory order
7903 // between the initial call to shouldInsertFencesForAtomic() and the call to
7904 // this function.
7905 if (!isa<AtomicCmpXchgInst>(Inst) && !isa<AtomicRMWInst>(Inst))
7906 return TargetLoweringBase::emitLeadingFence(Builder, Inst, Ord);
7907
7908 // Specialize for cmpxchg and atomicrmw
7909 auto SSID = getAtomicSyncScopeID(Inst);
7910 assert(SSID.has_value() && "Expected an atomic operation");
7911
7912 if (isReleaseOrStronger(Ord))
7913 return Builder.CreateFence(Ord == AtomicOrdering::SequentiallyConsistent
7916 SSID.value());
7917
7918 return nullptr;
7919}
7920
7922 Instruction *Inst,
7923 AtomicOrdering Ord) const {
7924 // prerequisite: shouldInsertFencesForAtomic() should have returned `true` for
7925 // `Inst` before its memory order was modified. See `emitLeadingFence` for why
7926 // this cannot be enforced with an assert. Specialize for cmpxchg and
7927 // atomicrmw
7928 auto *CI = dyn_cast<AtomicCmpXchgInst>(Inst);
7929 auto *RI = dyn_cast<AtomicRMWInst>(Inst);
7930 if (!CI && !RI)
7931 return TargetLoweringBase::emitTrailingFence(Builder, Inst, Ord);
7932
7933 auto SSID = getAtomicSyncScopeID(Inst);
7934 assert(SSID.has_value() && "Expected an atomic operation");
7935
7936 bool IsEmulated =
7937 CI ? cast<IntegerType>(CI->getCompareOperand()->getType())
7938 ->getBitWidth() < STI.getMinCmpXchgSizeInBits()
7940
7941 if (isAcquireOrStronger(Ord) && IsEmulated)
7942 return Builder.CreateFence(AtomicOrdering::Acquire, SSID.value());
7943
7944 return nullptr;
7945}
7946
7947// Rather than default to SINT when both UINT and SINT are custom, we only
7948// change the opcode when UINT is not legal and SINT is. UINT is preferred when
7949// both are custom since unsigned CVT instructions can lead to slightly better
7950// SASS code with fewer instructions.
7952 EVT ToVT) const {
7953 if (isOperationLegal(Op, ToVT))
7954 return Op;
7955 switch (Op) {
7956 case ISD::FP_TO_UINT:
7958 return ISD::FP_TO_SINT;
7959 break;
7963 break;
7964 default:
7965 break;
7966 }
7967 return Op;
7968}
7969
7970// Pin NVPTXTargetObjectFile's vtables to this file.
7972
7977
7979 const SelectionDAG &DAG, unsigned Depth) {
7980 SDValue A = Op.getOperand(0);
7981 SDValue B = Op.getOperand(1);
7982 ConstantSDNode *Selector = dyn_cast<ConstantSDNode>(Op.getOperand(2));
7983 unsigned Mode = Op.getConstantOperandVal(3);
7984
7985 if (!Selector)
7986 return;
7987
7988 KnownBits AKnown = DAG.computeKnownBits(A, Depth);
7989 KnownBits BKnown = DAG.computeKnownBits(B, Depth);
7990
7991 // {b, a} = {{b7, b6, b5, b4}, {b3, b2, b1, b0}}
7992 assert(AKnown.getBitWidth() == 32 && BKnown.getBitWidth() == 32 &&
7993 "PRMT must have i32 operands");
7994 assert(Known.getBitWidth() == 32 && "PRMT must have i32 result");
7995 KnownBits BitField = BKnown.concat(AKnown);
7996
7997 APInt SelectorVal = getPRMTSelector(Selector->getAPIntValue(), Mode);
7998 for (unsigned I : llvm::seq(4)) {
7999 APInt Sel = SelectorVal.extractBits(4, I * 4);
8000 unsigned Idx = Sel.getLoBits(3).getZExtValue();
8001 unsigned Sign = Sel.getHiBits(1).getZExtValue();
8002 KnownBits Byte = BitField.extractBits(8, Idx * 8);
8003 if (Sign)
8004 Byte = KnownBits::ashr(Byte, KnownBits::makeConstant(APInt(8, 7)));
8005 Known.insertBits(Byte, I * 8);
8006 }
8007}
8008
8011
8012 // We can't do anything without knowing the sign bit.
8013 auto ExtType = LD->getConstantOperandVal(LD->getNumOperands() - 1);
8014 if (ExtType == ISD::SEXTLOAD)
8015 return;
8016
8017 // ExtLoading to vector types is weird and may not work well with known bits.
8018 auto DestVT = LD->getValueType(0);
8019 if (DestVT.isVector())
8020 return;
8021
8022 assert(Known.getBitWidth() == DestVT.getSizeInBits());
8023 auto ElementBitWidth = getFromTypeWidthForLoad(LD);
8024 Known.Zero.setHighBits(Known.getBitWidth() - ElementBitWidth);
8025}
8026
8028 const SDValue Op, KnownBits &Known, const APInt &DemandedElts,
8029 const SelectionDAG &DAG, unsigned Depth) const {
8030 Known.resetAll();
8031
8032 switch (Op.getOpcode()) {
8033 case NVPTXISD::PRMT:
8035 break;
8036 case NVPTXISD::LoadV2:
8037 case NVPTXISD::LoadV4:
8038 case NVPTXISD::LoadV8:
8040 break;
8041 default:
8042 break;
8043 }
8044}
8045
8046static std::pair<APInt, APInt> getPRMTDemandedBits(const APInt &SelectorVal,
8047 const APInt &DemandedBits) {
8048 APInt DemandedLHS = APInt(32, 0);
8049 APInt DemandedRHS = APInt(32, 0);
8050
8051 for (unsigned I : llvm::seq(4)) {
8052 if (DemandedBits.extractBits(8, I * 8).isZero())
8053 continue;
8054
8055 APInt Sel = SelectorVal.extractBits(4, I * 4);
8056 unsigned Idx = Sel.getLoBits(3).getZExtValue();
8057 unsigned Sign = Sel.getHiBits(1).getZExtValue();
8058
8059 APInt &Src = Idx < 4 ? DemandedLHS : DemandedRHS;
8060 unsigned ByteStart = (Idx % 4) * 8;
8061 if (Sign)
8062 Src.setBit(ByteStart + 7);
8063 else
8064 Src.setBits(ByteStart, ByteStart + 8);
8065 }
8066
8067 return {DemandedLHS, DemandedRHS};
8068}
8069
8070// Replace undef with 0 as this is easier for other optimizations such as
8071// known bits.
8073 if (!Op)
8074 return SDValue();
8075 if (Op.isUndef())
8076 return DAG.getConstant(0, SDLoc(), MVT::i32);
8077 return Op;
8078}
8079
8081 const APInt &DemandedBits,
8082 SelectionDAG &DAG,
8083 const TargetLowering &TLI,
8084 unsigned Depth) {
8085 assert(PRMT.getOpcode() == NVPTXISD::PRMT);
8086 SDValue Op0 = PRMT.getOperand(0);
8087 SDValue Op1 = PRMT.getOperand(1);
8088 auto *SelectorConst = dyn_cast<ConstantSDNode>(PRMT.getOperand(2));
8089 if (!SelectorConst)
8090 return SDValue();
8091
8092 unsigned Mode = PRMT.getConstantOperandVal(3);
8093 const APInt Selector = getPRMTSelector(SelectorConst->getAPIntValue(), Mode);
8094
8095 // Try to simplify the PRMT to one of the inputs if the used bytes are all
8096 // from the same input in the correct order.
8097 const unsigned LeadingBytes = DemandedBits.countLeadingZeros() / 8;
8098 const unsigned SelBits = (4 - LeadingBytes) * 4;
8099 if (Selector.getLoBits(SelBits) == APInt(32, 0x3210).getLoBits(SelBits))
8100 return Op0;
8101 if (Selector.getLoBits(SelBits) == APInt(32, 0x7654).getLoBits(SelBits))
8102 return Op1;
8103
8104 auto [DemandedLHS, DemandedRHS] = getPRMTDemandedBits(Selector, DemandedBits);
8105
8106 // Attempt to avoid multi-use ops if we don't need anything from them.
8107 SDValue DemandedOp0 =
8108 TLI.SimplifyMultipleUseDemandedBits(Op0, DemandedLHS, DAG, Depth + 1);
8109 SDValue DemandedOp1 =
8110 TLI.SimplifyMultipleUseDemandedBits(Op1, DemandedRHS, DAG, Depth + 1);
8111
8112 DemandedOp0 = canonicalizePRMTInput(DemandedOp0, DAG);
8113 DemandedOp1 = canonicalizePRMTInput(DemandedOp1, DAG);
8114 if ((DemandedOp0 && DemandedOp0 != Op0) ||
8115 (DemandedOp1 && DemandedOp1 != Op1)) {
8116 Op0 = DemandedOp0 ? DemandedOp0 : Op0;
8117 Op1 = DemandedOp1 ? DemandedOp1 : Op1;
8118 return getPRMT(Op0, Op1, Selector.getZExtValue(), SDLoc(PRMT), DAG);
8119 }
8120
8121 return SDValue();
8122}
8123
8125 SDValue Op, const APInt &DemandedBits, const APInt &DemandedElts,
8126 KnownBits &Known, TargetLoweringOpt &TLO, unsigned Depth) const {
8127 Known.resetAll();
8128
8129 switch (Op.getOpcode()) {
8130 case NVPTXISD::PRMT:
8132 *this, Depth)) {
8133 TLO.CombineTo(Op, Result);
8134 return true;
8135 }
8136 break;
8137 default:
8138 break;
8139 }
8140
8141 computeKnownBitsForTargetNode(Op, Known, DemandedElts, TLO.DAG, Depth);
8142 return false;
8143}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
constexpr LLT S1
constexpr LLT F32
static cl::list< std::string > UseNative("amdgpu-use-native", cl::desc("Comma separated list of functions to replace with native, or all"), cl::CommaSeparated, cl::ValueOptional, cl::Hidden)
AMDGPU Register Bank Select
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1, TargetLowering::DAGCombinerInfo &DCI, const ARMSubtarget *Subtarget)
PerformADDCombineWithOperands - Try DAG combinations for an ADD with operands N0 and N1.
static SDValue PerformADDCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, const ARMSubtarget *Subtarget)
PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
static SDValue PerformVSELECTCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, const ARMSubtarget *Subtarget)
static SDValue PerformMULCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, const ARMSubtarget *Subtarget)
static SDValue PerformBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, const ARMSubtarget *Subtarget)
PerformBUILD_VECTORCombine - Target-specific dag combine xforms for ISD::BUILD_VECTOR.
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis Results
Atomic ordering constants.
This file contains the simple types necessary to represent the attributes associated with functions a...
#define X(NUM, ENUM, NAME)
Definition ELF.h:857
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
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")
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file contains the declarations of entities that describe floating point environment and related ...
static bool IsIndirectCall(const MachineInstr *MI)
Module.h This file contains the declarations for the Module class.
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Register const TargetRegisterInfo * TRI
#define T
NVPTX address space definition.
static SDValue reportInvalidTensormapReplaceUsage(SDValue Op, SelectionDAG &DAG, unsigned Val)
static SDValue combineShiftOfLogicOp(SDNode *N, TargetLowering::DAGCombinerInfo &DCI)
Commute SHL with a bitwise logic operation when doing so exposes a common shifted operand.
static SDValue combineADDRSPACECAST(SDNode *N, TargetLowering::DAGCombinerInfo &DCI)
static cl::opt< bool > sched4reg("nvptx-sched4reg", cl::desc("NVPTX Specific: schedule for register pressue"), cl::init(false))
static SDValue lowerTcgen05St(SDValue Op, SelectionDAG &DAG, bool hasOffset=false)
static SDValue PerformEXTRACTCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI)
static cl::opt< NVPTX::DivPrecisionLevel > UsePrecDivF32("nvptx-prec-divf32", cl::Hidden, cl::desc("NVPTX Specific: Override the precision of the lowering for f32 fdiv"), cl::values(clEnumValN(NVPTX::DivPrecisionLevel::Approx, "0", "Use div.approx"), clEnumValN(NVPTX::DivPrecisionLevel::Full, "1", "Use div.full"), clEnumValN(NVPTX::DivPrecisionLevel::IEEE754, "2", "Use IEEE Compliant F32 div.rnd if available (default)"), clEnumValN(NVPTX::DivPrecisionLevel::IEEE754_NoFTZ, "3", "Use IEEE Compliant F32 div.rnd if available, no FTZ")), cl::init(NVPTX::DivPrecisionLevel::IEEE754))
static bool isConstOne(const SDValue &Operand)
static cl::opt< unsigned > FMAContractLevelOpt("nvptx-fma-level", cl::Hidden, cl::desc("NVPTX Specific: FMA contraction (0: don't do it" " 1: do it 2: do it aggressively"), cl::init(2))
static bool IsPTXVectorType(MVT VT)
static SDValue PerformSELECTShiftCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI)
Transform patterns like: (select (ugt shift_amt, BitWidth-1), 0, (srl/shl x, shift_amt)) (select (ult...
static SDValue lowerLOADi1(LoadSDNode *LD, SelectionDAG &DAG)
static SDValue lowerIntrinsicVoid(SDValue Op, SelectionDAG &DAG)
static SDValue lowerROT(SDValue Op, SelectionDAG &DAG)
static SDValue PerformFMinMaxCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, const NVPTXSubtarget &STI)
PerformFMinMaxCombine - Combine (fmaxnum (fmaxnum a, b), c) into (fmaxnum3 a, b, c).
static void ComputePTXValueVTs(const TargetLowering &TLI, const DataLayout &DL, LLVMContext &Ctx, CallingConv::ID CallConv, Type *Ty, SmallVectorImpl< EVT > &ValueVTs, SmallVectorImpl< uint64_t > &Offsets, uint64_t StartingOffset=0)
ComputePTXValueVTs - For the given Type Ty, returns the set of primitive legal-ish MVTs that compose ...
static void ReplaceBITCAST(SDNode *Node, SelectionDAG &DAG, SmallVectorImpl< SDValue > &Results)
static void replaceAtomicSwap128(SDNode *N, SelectionDAG &DAG, const NVPTXSubtarget &STI, SmallVectorImpl< SDValue > &Results)
static unsigned getMinMax3Opcode(unsigned MinMax2Opcode)
Get 3-input version of a 2-input min/max opcode.
static SDValue lowerStAsyncWithMbarrier(SDValue Op, SelectionDAG &DAG)
static SDValue lowerSTOREVector(SDValue Op, SelectionDAG &DAG, const NVPTXSubtarget &STI)
static SDValue lowerLoadVector(SDNode *N, SelectionDAG &DAG, const NVPTXSubtarget &STI)
static void replaceProxyReg(SDNode *N, SelectionDAG &DAG, const TargetLowering &TLI, SmallVectorImpl< SDValue > &Results)
static SDValue lowerStAsyncRelease(SDValue Op, SelectionDAG &DAG)
static void ReplaceCopyFromReg_128(SDNode *N, SelectionDAG &DAG, SmallVectorImpl< SDValue > &Results)
#define TCGEN05_LD_RED_INST(SHAPE, NUM, TYPE)
static SDValue getSymbolNode(SelectionDAG &DAG, MCSymbol *Sym, EVT T)
static SDValue lowerCTLZCTPOP(SDValue Op, SelectionDAG &DAG)
static SDValue combineMADConstOne(SDValue X, SDValue Add, EVT VT, SDLoc DL, TargetLowering::DAGCombinerInfo &DCI)
static unsigned getTcgen05LdRedID(Intrinsic::ID IID)
static SDValue combinePRMT(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, CodeGenOptLevel OptLevel)
static SDValue combinePackingMovIntoStore(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, unsigned Front, unsigned Back)
Fold packing movs into a store.
static void ReplaceINTRINSIC_W_CHAIN(SDNode *N, SelectionDAG &DAG, SmallVectorImpl< SDValue > &Results)
static SDValue getBuildVectorizedValue(unsigned N, const SDLoc &dl, SelectionDAG &DAG, T GetElement)
static SDValue getExtractVectorizedValue(SDValue V, unsigned I, EVT VT, const SDLoc &dl, SelectionDAG &DAG)
static SDValue combineSZExtToMulWide(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, CodeGenOptLevel OptLevel)
static unsigned canMergeParamLoadStoresStartingAt(unsigned Idx, uint32_t AccessSize, const SmallVectorImpl< EVT > &ValueVTs, const SmallVectorImpl< T > &Offsets, Align ParamAlignment)
static EVT getVectorizedVT(EVT VT, unsigned N, LLVMContext &C)
static SDValue lowerIntrinsicWOChain(SDValue Op, SelectionDAG &DAG)
static std::optional< unsigned > getScalar3OpcodeForReduction(unsigned ReductionOpcode)
Get 3-input scalar reduction opcode.
static SDValue lowerIntrinsicWChain(SDValue Op, SelectionDAG &DAG)
static bool isNonCoalescableBuildVector(const SDValue &BV)
Check if a v2f32 BUILD_VECTOR provably packs values from non-adjacent register pairs (non-coalescable...
static bool isConstZero(const SDValue &Operand)
static SDValue LowerVectorArith(SDValue Op, SelectionDAG &DAG)
static SDValue LowerTcgen05MMADisableOutputLane(SDValue Op, SelectionDAG &DAG)
static bool IsMulWideOperandDemotable(SDValue Op, unsigned OptSize, OperandSignedness &S)
IsMulWideOperandDemotable - Checks if the provided DAG node is an operand that can be demoted to OptS...
static unsigned getTcgen05MMADisableOutputLane(unsigned IID)
static std::pair< APInt, APInt > getPRMTDemandedBits(const APInt &SelectorVal, const APInt &DemandedBits)
static APInt computePRMT(APInt A, APInt B, APInt Selector, unsigned Mode)
static ISD::NodeType getScalarOpcodeForReduction(unsigned ReductionOpcode)
static SDValue lowerBSWAP(SDValue Op, SelectionDAG &DAG)
static SDValue lowerMSTORE(SDValue Op, SelectionDAG &DAG)
static SDValue PerformMULCombineWithOperands(SDNode *N, SDValue N0, SDValue N1, TargetLowering::DAGCombinerInfo &DCI)
static void computeKnownBitsForPRMT(const SDValue Op, KnownBits &Known, const SelectionDAG &DAG, unsigned Depth)
static SDValue combineUnpackingMovIntoLoad(SDNode *N, TargetLowering::DAGCombinerInfo &DCI)
Fold unpacking movs into a load by increasing the number of return values.
#define TCGEN05_LD_RED_INTR(SHAPE, NUM, TYPE)
static SDValue lowerTensormapReplaceElemtype(SDValue Op, SelectionDAG &DAG)
static SDValue LowerClusterLaunchControlQueryCancel(SDValue Op, SelectionDAG &DAG)
static SDValue PerformSETCCCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, const NVPTXSubtarget &STI)
static std::optional< std::pair< SDValue, SDValue > > lowerTcgen05Ld(SDNode *N, SelectionDAG &DAG, bool HasOffset=false)
static SDValue lowerCvtRSIntrinsics(SDValue Op, SelectionDAG &DAG)
static std::optional< std::pair< SDValue, SDValue > > replaceLoadVector(SDNode *N, SelectionDAG &DAG, const NVPTXSubtarget &STI)
replaceLoadVector - Convert vector loads into multi-output scalar loads.
static SDValue expandFSH64(SDValue A, SDValue B, SDValue ShiftAmount, SDLoc DL, unsigned Opcode, SelectionDAG &DAG)
static cl::opt< bool > AllowFTZAtomics("nvptx-allow-ftz-atomics", cl::Hidden, cl::desc("NVPTX Specific: Lower atomicrmw fadd to atom.add even when its " "FTZ behavior does not match the function's denormal mode."), cl::init(true))
static bool AreMulWideOperandsDemotable(SDValue LHS, SDValue RHS, unsigned OptSize, bool &IsSigned)
AreMulWideOperandsDemotable - Checks if the given LHS and RHS operands can be demoted to OptSize bits...
static std::pair< MemSDNode *, uint32_t > convertMLOADToLoadWithUsedBytesMask(MemSDNode *N, SelectionDAG &DAG, const NVPTXSubtarget &STI)
static SDValue TryMULWIDECombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI)
TryMULWIDECombine - Attempt to replace a multiply of M bits with a multiply of M/2 bits that produces...
static SDValue lowerPrmtIntrinsic(SDValue Op, SelectionDAG &DAG)
static SDValue diagnoseUnsupportedFAdd(SDNode *N, SelectionDAG &DAG, Intrinsic::ID IID, APFloat::roundingMode RoundingMode)
static SDValue combineMulSelectConstOne(SDValue X, SDValue Select, EVT VT, SDLoc DL, TargetLowering::DAGCombinerInfo &DCI)
static SDValue buildTreeReduction(const SmallVector< SDValue > &Elements, EVT EltTy, ArrayRef< std::pair< unsigned, unsigned > > Ops, const SDLoc &DL, const SDNodeFlags Flags, SelectionDAG &DAG)
Reduces the elements using the scalar operations provided.
static SDValue combineProxyReg(SDNode *N, TargetLowering::DAGCombinerInfo &DCI)
static SmallVector< unsigned, 16 > VectorizePTXValueVTs(const SmallVectorImpl< EVT > &ValueVTs, const SmallVectorImpl< T > &Offsets, Align ParamAlignment, bool IsVAArg=false)
static SDValue combineFAddWithNeg(SDNode *N, SelectionDAG &DAG, Intrinsic::ID AddIntrinsicID, APFloat::roundingMode RoundingMode)
static SDValue getPRMT(SDValue A, SDValue B, SDValue Selector, SDLoc DL, SelectionDAG &DAG, unsigned Mode=NVPTX::PTXPrmtMode::NONE)
static SDValue matchMADConstOnePattern(SDValue Add)
static SDValue correctParamType(SDValue V, EVT ExpectedVT, ISD::ArgFlagsTy Flags, SelectionDAG &DAG, SDLoc dl)
static ISD::NodeType getExtOpcode(const ISD::ArgFlagsTy &Flags)
static cl::opt< bool > UsePrecSqrtF32("nvptx-prec-sqrtf32", cl::Hidden, cl::desc("NVPTX Specific: 0 use sqrt.approx, 1 use sqrt.rn."), cl::init(true))
static MachinePointerInfo refinePtrAS(SDValue &Ptr, SelectionDAG &DAG)
static void computeKnownBitsForLoadV(const SDValue Op, KnownBits &Known)
static APInt getPRMTSelector(const APInt &Selector, unsigned Mode)
static EVT promoteScalarIntegerPTX(const EVT VT)
PromoteScalarIntegerPTX Used to make sure the arguments/returns are suitable for passing and promote ...
static std::optional< std::tuple< SDValue, SDValue, SDValue > > lowerTcgen05LdRed(SDNode *N, SelectionDAG &DAG)
static SDValue simplifyDemandedBitsForPRMT(SDValue PRMT, const APInt &DemandedBits, SelectionDAG &DAG, const TargetLowering &TLI, unsigned Depth)
static SDValue lowerFREM(SDValue Op, SelectionDAG &DAG)
static SDValue canonicalizePRMTInput(SDValue Op, SelectionDAG &DAG)
static SDValue sinkProxyReg(SDValue R, SDValue Chain, TargetLowering::DAGCombinerInfo &DCI)
static SDValue lowerFSH(SDValue Op, SelectionDAG &DAG)
static SDValue lowerTensormapReplaceSwizzleMode(SDValue Op, SelectionDAG &DAG)
static SDValue combineIntrinsicWOChain(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, const NVPTXSubtarget &STI)
static SDValue PromoteBinOpToF32(SDNode *N, SelectionDAG &DAG)
static unsigned getFAddWithNegOpcode(EVT VT, Intrinsic::ID IID, APFloat::roundingMode RoundingMode)
static std::optional< std::pair< unsigned int, MVT > > getVectorLoweringShape(EVT VectorEVT, const NVPTXSubtarget &STI, unsigned AddressSpace)
static cl::opt< bool > UseApproxLog2F32("nvptx-approx-log2f32", cl::desc("NVPTX Specific: whether to use lg2.approx for log2"), cl::init(false))
Whereas CUDA's implementation (see libdevice) uses ex2.approx for exp2(), it does NOT use lg2....
static SDValue lowerSELECT(SDValue Op, SelectionDAG &DAG)
static bool isSupportedFAdd(EVT VT, const NVPTXSubtarget &STI, Intrinsic::ID IID, APFloat::roundingMode RoundingMode)
static SDValue combineLOAD(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, const NVPTXSubtarget &STI)
static SDValue combineSTORE(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, const NVPTXSubtarget &STI)
static SDValue PerformSHLCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI, CodeGenOptLevel OptLevel)
PerformSHLCombine - Runs PTX-specific DAG combine patterns on SHL nodes.
uint64_t High
This file contains the definitions of the enumerations and flags associated with NVVM Intrinsics,...
#define P(N)
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.
SI Fold Operands
Func MI getDebugLoc()))
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallVector class.
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
This file describes how to lower LLVM code to machine code.
Value * RHS
Value * LHS
BinaryOperator * Mul
static const fltSemantics & IEEEsingle()
Definition APFloat.h:304
llvm::RoundingMode roundingMode
IEEE-754R 4.3: Rounding-direction attributes.
Definition APFloat.h:359
static constexpr roundingMode rmNearestTiesToEven
Definition APFloat.h:361
static const fltSemantics & IEEEhalf()
Definition APFloat.h:302
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition APFloat.h:1202
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt getLoBits(unsigned numBits) const
Compute an APInt containing numBits lowbits from this APInt.
Definition APInt.cpp:641
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1560
LLVM_ABI APInt getHiBits(unsigned numBits) const
Compute an APInt containing numBits highbits from this APInt.
Definition APInt.cpp:636
LLVM_ABI APInt trunc(unsigned width) const
Truncate to new width.
Definition APInt.cpp:970
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition APInt.h:1350
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1508
bool isSignedIntN(unsigned N) const
Check if this APInt has an N-bits signed integer value.
Definition APInt.h:431
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition APInt.h:1134
LLVM_ABI APInt extractBits(unsigned numBits, unsigned bitPosition) const
Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
Definition APInt.cpp:478
bool isIntN(unsigned N) const
Check if this APInt has an N-bits unsigned integer value.
Definition APInt.h:428
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition APInt.h:1241
an instruction to allocate memory on the stack
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Definition ArrayRef.h:185
an instruction that atomically reads a memory location, combines it with another value,...
@ Add
*p = old + v
@ FAdd
*p = old + v
@ Min
*p = old <signed v ? old : v
@ Sub
*p = old - v
@ And
*p = old & v
@ Xor
*p = old ^ v
@ UIncWrap
Increment one up to a maximum value.
@ Max
*p = old >signed v ? old : v
@ UMin
*p = old <unsigned v ? old : v
@ UMax
*p = old >unsigned v ? old : v
@ UDecWrap
Decrement one until a minimum value or zero.
bool isFloatingPointOperation() const
BinOp getOperation() const
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
This is an SDNode representing atomic operations.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
MaybeAlign getParamAlign(unsigned ArgNo) const
Extract the alignment for a call or parameter (0=unknown).
FunctionType * getFunctionType() const
const APInt & getAPIntValue() const
This is an important base class in LLVM.
Definition Constant.h:43
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
LLVM_ABI TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
LLVM_ABI Align getPrefTypeAlign(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
Diagnostic information for unsupported feature in backend.
static constexpr ElementCount getFixed(ScalarTy MinVal)
Definition TypeSize.h:305
void addFnAttr(Attribute::AttrKind Kind)
Add function attributes to this function.
Definition Function.cpp:640
Module * getParent()
Get the module that this global value is contained inside of...
Common base class shared among various IRBuilders.
Definition IRBuilder.h:114
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
LLVM_ABI void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
This class is used to represent ISD::LOAD nodes.
Context object for machine code objects.
Definition MCContext.h:83
MCSection * getDataSection() const
static constexpr unsigned NoRegister
Definition MCRegister.h:60
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition MCSection.h:580
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
StringRef getName() const
getName - Get the symbol name.
Definition MCSymbol.h:188
Machine Value Type.
static auto integer_fixedlen_vector_valuetypes()
SimpleValueType SimpleTy
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.
static auto integer_valuetypes()
TypeSize getSizeInBits() const
Returns the size of the specified MVT in bits.
static auto fixedlen_vector_valuetypes()
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
static MVT getVectorVT(MVT VT, unsigned NumElements)
MVT getVectorElementType() const
static MVT getIntegerVT(unsigned BitWidth)
static auto fp_valuetypes()
MVT getScalarType() const
If this is a vector, return the element type, otherwise return this.
static auto fp_fixedlen_vector_valuetypes()
const AllocaInst * getObjectAllocation(int ObjectIdx) const
Return the underlying Alloca of the specified stack object if it exists.
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.
MCContext & getContext() const
Function & getFunction()
Return the LLVM function that this machine code represents.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
@ EK_Inline
EK_Inline - Jump table entries are emitted inline at their point of use.
@ MODereferenceable
The memory access is dereferenceable (i.e., doesn't trap).
@ MOLoad
The memory access reads data.
@ MOInvariant
The memory access always returns the same value (or traps).
@ MOStore
The memory access writes data.
This SDNode is used for target intrinsics that touch memory and need an associated MachineMemOperand.
This is an abstract virtual class for memory operations.
Align getAlign() const
MachineMemOperand * getMemOperand() const
Return the unique MachineMemOperand object describing the memory reference performed by operation.
EVT getMemoryVT() const
Return the type of the in-memory value.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
bool hasTensormapReplaceElemtypeSupport(unsigned ElemType) const
bool hasTensormapReplaceSwizzleModeSupport(unsigned SwizzleMode) const
bool hasNativeBF16Support(unsigned Opcode) const
bool hasUsedBytesMaskPragma() const
bool hasAtomSwap128() const
bool hasF32x2Instructions() const
bool has256BitVectorLoadStore(unsigned AS) const
AtomicOrdering atomicOperationOrderAfterFenceSplit(const Instruction *I) const override
ConstraintType getConstraintType(StringRef Constraint) const override
getConstraintType - Given a constraint letter, return the type of constraint it is for this target.
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 SimplifyDemandedBitsForTargetNode(SDValue Op, const APInt &DemandedBits, const APInt &DemandedElts, KnownBits &Known, TargetLoweringOpt &TLO, unsigned Depth=0) const override
Attempt to simplify any target nodes based on the demanded bits/elts, returning true on success.
AtomicExpansionKind shouldExpandAtomicRMWInIR(const AtomicRMWInst *AI) const override
Returns how the IR-level AtomicExpand pass should expand the given AtomicRMW, if at all.
NVPTXTargetLowering(const NVPTXTargetMachine &TM, const NVPTXSubtarget &STI)
unsigned getPreferredFPToIntOpcode(unsigned Op, EVT FromVT, EVT ToVT) const override
bool useF32FTZ(const MachineFunction &MF) const
SDValue LowerSTACKSAVE(SDValue Op, SelectionDAG &DAG) const
SDValue getSqrtEstimate(SDValue Operand, SelectionDAG &DAG, int Enabled, int &ExtraSteps, bool &UseOneConst, bool Reciprocal) const override
Hooks for building estimates in place of slower divisions and square roots.
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,...
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,...
MCSymbol * getParamSymbol(MCContext &Ctx, const Function *F, int Idx) const
void LowerAsmOperandForConstraint(SDValue Op, StringRef Constraint, std::vector< SDValue > &Ops, SelectionDAG &DAG) const override
Lower the specified operand into the Ops vector.
SDValue LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG) const
Instruction * emitTrailingFence(IRBuilderBase &Builder, Instruction *Inst, AtomicOrdering Ord) const override
TargetLoweringBase::LegalizeTypeAction getPreferredVectorAction(MVT VT) const override
Return the preferred vector type legalization action.
NVPTX::DivPrecisionLevel getDivF32Level(const MachineFunction &MF, const SDNode &N) const
bool shouldInsertFencesForAtomic(const Instruction *) const override
Whether AtomicExpandPass should automatically insert fences and reduce ordering for this atomic.
SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Ctx, EVT VT) const override
Return the ValueType of the result of SETCC operations.
std::pair< unsigned, const TargetRegisterClass * > getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const override
Given a physical register constraint (e.g.
bool isLegalAddressingMode(const DataLayout &DL, const AddrMode &AM, Type *Ty, unsigned AS, Instruction *I=nullptr) const override
isLegalAddressingMode - Return true if the addressing mode represented by AM is legal for this target...
Instruction * emitLeadingFence(IRBuilderBase &Builder, Instruction *Inst, AtomicOrdering Ord) const override
Inserts in the IR a target-specific intrinsic specifying a fence.
void getTgtMemIntrinsic(SmallVectorImpl< IntrinsicInfo > &Infos, const CallBase &I, MachineFunction &MF, unsigned Intrinsic) const override
Given an intrinsic, checks if on the target the intrinsic will need to map to a MemIntrinsicNode (tou...
bool allowFMA(MachineFunction &MF, CodeGenOptLevel OptLevel) const
bool usePrecSqrtF32(const SDNode *N=nullptr) const
unsigned getJumpTableEncoding() const override
Return the entry encoding for a jump table in the current function.
SDValue LowerCall(CallLoweringInfo &CLI, SmallVectorImpl< SDValue > &InVals) const override
This hook must be implemented to lower calls into the specified DAG.
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...
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
static LLVM_ABI PointerType * get(LLVMContext &C, unsigned AddressSpace)
This constructs an opaque pointer to an object in a numbered address space.
Definition Type.cpp:887
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
const DebugLoc & getDebugLoc() const
Represents one node in the SelectionDAG.
ArrayRef< SDUse > ops() const
const APInt & getAsAPIntVal() const
Helper method returns the APInt value of a ConstantSDNode.
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
bool hasOneUse() const
Return true if there is exactly one use of this node.
unsigned getIROrder() const
Return the node ordering.
SDNodeFlags getFlags() const
uint64_t getAsZExtVal() const
Helper method returns the zero-extended integer value of a ConstantSDNode.
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
SDVTList getVTList() const
const SDValue & getOperand(unsigned Num) const
bool isUndef() const
Returns true if the node type is UNDEF or POISON.
iterator_range< user_iterator > users()
void setFlags(SDNodeFlags NewFlags)
Represents a use of a SDNode.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
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, in exactly one operand.
SDValue getValue(unsigned R) const
EVT getValueType() const
Return the ValueType of the referenced return value.
TypeSize getValueSizeInBits() const
Returns the size of the value in bits.
const SDValue & getOperand(unsigned i) const
uint64_t getScalarValueSizeInBits() const
uint64_t getConstantOperandVal(unsigned i) const
unsigned getOpcode() const
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition SectionKind.h:22
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
const SDValue & getRoot() const
Return the root tag of the SelectionDAG.
const TargetSubtargetInfo & getSubtarget() 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 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 getFreeze(SDValue V)
Return a freeze using the SDLoc of the value operand.
LLVM_ABI SDValue getSymbolFunctionGlobalAddress(SDValue Op, Function **TargetFunction=nullptr)
Return a GlobalAddress of the function from the current module with name matching the given ExternalS...
LLVM_ABI SDValue getConstantFP(double Val, const SDLoc &DL, EVT VT, bool isTarget=false)
Create a ConstantFPSDNode wrapping a constant value.
LLVM_ABI SDValue getAddrSpaceCast(const SDLoc &dl, EVT VT, SDValue Ptr, unsigned SrcAS, unsigned DestAS, const SDNodeFlags Flags=SDNodeFlags())
Return an AddrSpaceCastSDNode.
LLVM_ABI SDValue getRegister(Register Reg, EVT VT)
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.
SDValue getSetCC(const SDLoc &DL, EVT VT, SDValue LHS, SDValue RHS, ISD::CondCode Cond, SDValue Chain=SDValue(), bool IsSignaling=false, SDNodeFlags Flags={})
Helper function to make it easier to build SetCC's if you just have an ISD::CondCode instead of an SD...
LLVM_ABI Align getEVTAlign(EVT MemoryVT) const
Compute the default alignment value for the given type.
LLVM_ABI SDValue getNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a bitwise NOT operation as (XOR Val, -1).
LLVM_ABI SDNode * MorphNodeTo(SDNode *N, unsigned Opc, SDVTList VTs, ArrayRef< SDValue > Ops)
This mutates the specified node to have the specified return type, opcode, and operands.
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 getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, SDValue Offset, MachinePointerInfo PtrInfo, EVT SVT, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const MMOMetadata &Metadata=MMOMetadata())
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 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...
const DataLayout & getDataLayout() const
LLVM_ABI SDValue getTokenFactor(const SDLoc &DL, SmallVectorImpl< SDValue > &Vals)
Creates a new TokenFactor containing Vals.
LLVM_ABI SDValue getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const MMOMetadata &Metadata=MMOMetadata())
Helper function to build ISD::STORE nodes.
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 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 MMOMetadata &Metadata=MMOMetadata())
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 ...
SDValue getSelectCC(const SDLoc &DL, SDValue LHS, SDValue RHS, SDValue True, SDValue False, ISD::CondCode Cond, SDNodeFlags Flags=SDNodeFlags())
Helper function to make it easier to build SelectCC's if you just have an ISD::CondCode instead of an...
LLVM_ABI SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const MMOMetadata &Metadata=MMOMetadata())
Loads are not normal binary operators: their result type is not determined by their operands,...
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 getIntPtrConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
LLVM_ABI SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
LLVM_ABI SDValue getFPExtendOrRound(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of float type, to the float type VT, by either extending or rounding (by tr...
SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
LLVM_ABI SDValue getVectorIdxConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
MachineFunction & getMachineFunction() const
SDValue getPOISON(EVT VT)
Return a POISON node. POISON does not have a useful SDLoc.
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 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...
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 SDValue getMCSymbol(MCSymbol *Sym, EVT VT)
ArrayRef< int > getMask() const
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 push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This class is used to represent ISD::STORE nodes.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr size_t size() const
Get the string size.
Definition StringRef.h:144
Align getStackAlign() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
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...
void setMaxDivRemBitWidthSupported(unsigned SizeInBits)
Set the size in bits of the maximum div/rem the backend supports.
EVT getValueType(const DataLayout &DL, Type *Ty, bool AllowUnknown=false) const
Return the EVT corresponding to this LLVM type.
unsigned MaxStoresPerMemcpyOptSize
Likewise for functions with the OptSize attribute.
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...
void setOperationPromotedToType(unsigned Opc, MVT OrigVT, MVT DestVT)
Convenience method to set an operation to Promote and specify the type in a single call.
LegalizeTypeAction
This enum indicates whether a types are legal for a target, and if not, what action should be used to...
void addBypassSlowDiv(unsigned int SlowBitWidth, unsigned int FastBitWidth)
Tells the code generator which bitwidths to bypass.
MVT getRegisterType(LLVMContext &Context, EVT VT) const
Return the type of registers that this ValueType will eventually require.
void setMaxLargeFPConvertBitWidthSupported(unsigned SizeInBits)
Set the size in bits of the maximum fp to/from int conversion the backend supports.
virtual unsigned getNumRegisters(LLVMContext &Context, EVT VT, std::optional< MVT > RegisterVT=std::nullopt) const
Return the number of registers that this ValueType will eventually require.
void setMaxAtomicSizeInBitsSupported(unsigned SizeInBits)
Set the maximum atomic operation size supported by the backend.
virtual TargetLoweringBase::LegalizeTypeAction getPreferredVectorAction(MVT VT) const
Return the preferred vector type legalization action.
unsigned MaxStoresPerMemsetOptSize
Likewise for functions with the OptSize attribute.
void setBooleanContents(BooleanContent Ty)
Specify how the target extends the result of integer and floating point boolean values from i1 to a w...
unsigned MaxStoresPerMemmove
Specify maximum number of store instructions per memmove call.
void computeRegisterProperties(const TargetRegisterInfo *TRI)
Once all of the register classes are added, this allows us to compute derived properties we expose.
unsigned MaxStoresPerMemmoveOptSize
Likewise for functions with the OptSize attribute.
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.
unsigned MaxStoresPerMemset
Specify maximum number of store instructions per memset call.
void setTruncStoreAction(MVT ValVT, MVT MemVT, LegalizeAction Action)
Indicate that the specified truncating store does not work with the specified type and indicate what ...
void setMinCmpXchgSizeInBits(unsigned SizeInBits)
Sets the minimum cmpxchg or ll/sc size supported by the backend.
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 setCondCodeAction(ArrayRef< ISD::CondCode > CCs, MVT VT, LegalizeAction Action)
Indicate that the specified condition code is or isn't supported on the target and indicate what to d...
void setTargetDAGCombine(ArrayRef< ISD::NodeType > NTs)
Targets should invoke this method for each target independent node that they want to provide a custom...
Align getMinStackArgumentAlignment() const
Return the minimum stack alignment of an argument.
void setLoadExtAction(unsigned ExtType, MVT ValVT, MVT MemVT, LegalizeAction Action)
Indicate that the specified load with extension does not work with the specified type and indicate wh...
std::vector< ArgListEntry > ArgListTy
virtual Instruction * emitTrailingFence(IRBuilderBase &Builder, Instruction *Inst, AtomicOrdering Ord) const
virtual Instruction * emitLeadingFence(IRBuilderBase &Builder, Instruction *Inst, AtomicOrdering Ord) const
Inserts in the IR a target-specific intrinsic specifying a fence.
unsigned MaxStoresPerMemcpy
Specify maximum number of store instructions per memcpy call.
void setSchedulingPreference(Sched::Preference Pref)
Specify the target scheduling preference.
void setJumpIsExpensive(bool isExpensive=true)
Tells the code generator not to expand logic operations on comparison predicates into separate sequen...
LegalizeAction getOperationAction(unsigned Op, EVT VT) const
Return how this operation should be treated: either it is legal, needs to be promoted to a larger siz...
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
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...
virtual ConstraintType getConstraintType(StringRef Constraint) const
Given a constraint, return the type of constraint it is for this target.
virtual std::pair< unsigned, const TargetRegisterClass * > getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const
Given a physical register constraint (e.g.
TargetLowering(const TargetLowering &)=delete
SDValue expandRoundInexactToOdd(EVT ResultVT, SDValue Op, const SDLoc &DL, SelectionDAG &DAG) const
Truncate Op to ResultVT.
SDValue expandFP_ROUND(SDNode *Node, SelectionDAG &DAG) const
Expand round(fp) to fp conversion.
virtual void LowerAsmOperandForConstraint(SDValue Op, StringRef Constraint, std::vector< SDValue > &Ops, SelectionDAG &DAG) const
Lower the specified operand into the Ops vector.
Primary interface to the complete machine description for the target machine.
CodeGenOptLevel getOptLevel() const
Returns the optimization level: None, Less, Default, or Aggressive.
MCSymbol * getSymbol(const GlobalValue *GV) const
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetFrameLowering * getFrameLowering() const
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:339
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:252
bool isVoidTy() const
Return true if this is 'void'.
Definition Type.h:141
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:257
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:319
CallInst * Call
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI APInt pow(const APInt &X, int64_t N)
Compute X^N for N>=0.
Definition APInt.cpp:3189
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
Definition ISDOpcodes.h:41
@ SETCC
SetCC operator - This evaluates to a true value iff the condition is true.
Definition ISDOpcodes.h:830
@ STACKRESTORE
STACKRESTORE has two operands, an input chain and a pointer to restore to it returns an output chain.
@ STACKSAVE
STACKSAVE - STACKSAVE has one operand, an input chain.
@ POISON
POISON - A poison node.
Definition ISDOpcodes.h:236
@ MLOAD
Masked load and store - consecutive vector load and store operations with additional mask operand tha...
@ SMUL_LOHI
SMUL_LOHI/UMUL_LOHI - Multiply two integers of type iN, producing a signed/unsigned value of type i[2...
Definition ISDOpcodes.h:275
@ BSWAP
Byte Swap and Counting operators.
Definition ISDOpcodes.h:790
@ VAEND
VAEND, VASTART - VAEND and VASTART have three operands: an input chain, pointer, and a SRCVALUE.
@ ADDC
Carry-setting nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:294
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:264
@ 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:864
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition ISDOpcodes.h:521
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition ISDOpcodes.h:220
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition ISDOpcodes.h:891
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition ISDOpcodes.h:587
@ VECREDUCE_FMAX
FMIN/FMAX nodes can have flags, for NaN/NoNaN variants.
@ FADD
Simple binary floating point operators.
Definition ISDOpcodes.h:418
@ VECREDUCE_FMAXIMUM
FMINIMUM/FMAXIMUM nodes propatate NaNs and signed zeroes using the llvm.minimum and llvm....
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition ISDOpcodes.h:750
@ SDIVREM
SDIVREM/UDIVREM - Divide two integers and produce both a quotient and remainder result.
Definition ISDOpcodes.h:280
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition ISDOpcodes.h:254
@ CLMUL
Carry-less multiplication operations.
Definition ISDOpcodes.h:781
@ CTLZ_ZERO_POISON
Definition ISDOpcodes.h:799
@ SIGN_EXTEND
Conversion operators.
Definition ISDOpcodes.h:855
@ READSTEADYCOUNTER
READSTEADYCOUNTER - This corresponds to the readfixedcounter intrinsic.
@ FNEG
Perform various unary floating-point operations inspired by libm.
@ BR_CC
BR_CC - Conditional branch.
@ SSUBO
Same for subtraction.
Definition ISDOpcodes.h:353
@ BRIND
BRIND - Indirect branch.
@ BR_JT
BR_JT - Jumptable branch.
@ SSUBSAT
RESULT = [US]SUBSAT(LHS, RHS) - Perform saturation subtraction on 2 integers with the same bit width ...
Definition ISDOpcodes.h:375
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition ISDOpcodes.h:807
@ UNDEF
UNDEF - An undefined node.
Definition ISDOpcodes.h:233
@ EXTRACT_ELEMENT
EXTRACT_ELEMENT - This is used to get the lower or upper (determined by a Constant,...
Definition ISDOpcodes.h:247
@ VACOPY
VACOPY - VACOPY has 5 operands: an input chain, a destination pointer, a source pointer,...
@ CopyFromReg
CopyFromReg - This node indicates that the input value is a virtual or physical register that is defi...
Definition ISDOpcodes.h:230
@ SADDO
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition ISDOpcodes.h:349
@ MULHU
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition ISDOpcodes.h:707
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:772
@ VECTOR_SHUFFLE
VECTOR_SHUFFLE(VEC1, VEC2) - Returns a vector, of the same type as VEC1/VEC2.
Definition ISDOpcodes.h:652
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition ISDOpcodes.h:617
@ 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:579
@ CopyToReg
CopyToReg - This node has three operands: a chain, a register number to set to this value,...
Definition ISDOpcodes.h:224
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition ISDOpcodes.h:861
@ 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:822
@ 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...
@ SSHLSAT
RESULT = [US]SHLSAT(LHS, RHS) - Perform saturation left shift.
Definition ISDOpcodes.h:387
@ SMULO
Same for multiplication.
Definition ISDOpcodes.h:357
@ 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:899
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition ISDOpcodes.h:730
@ FP_EXTEND
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition ISDOpcodes.h:989
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition ISDOpcodes.h:816
@ UADDO_CARRY
Carry-using nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:329
@ BF16_TO_FP
BF16_TO_FP, FP_TO_BF16 - These operators are used to perform promotions and truncation for bfloat16.
@ FRAMEADDR
FRAMEADDR, RETURNADDR - These nodes represent llvm.frameaddress and llvm.returnaddress on the DAG.
Definition ISDOpcodes.h:110
@ STRICT_FP_TO_UINT
Definition ISDOpcodes.h:481
@ STRICT_FP_TO_SINT
STRICT_FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:480
@ 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:937
@ READCYCLECOUNTER
READCYCLECOUNTER - This corresponds to the readcyclecounter intrinsic.
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:742
@ TRAP
TRAP - Trapping instruction.
@ INTRINSIC_WO_CHAIN
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition ISDOpcodes.h:205
@ ADDE
Carry-using nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:304
@ INSERT_VECTOR_ELT
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL.
Definition ISDOpcodes.h:568
@ ATOMIC_SWAP
Val, OUTCHAIN = ATOMIC_SWAP(INCHAIN, ptr, amt) Val, OUTCHAIN = ATOMIC_LOAD_[OpName](INCHAIN,...
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition ISDOpcodes.h:970
@ ADDRSPACECAST
ADDRSPACECAST - This operator converts between pointers of different address spaces.
@ VECREDUCE_FMINIMUM
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:867
@ VAARG
VAARG - VAARG has four operands: an input chain, a pointer, a SRCVALUE, and the alignment.
@ SHL_PARTS
SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded integer shift operations.
Definition ISDOpcodes.h:844
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition ISDOpcodes.h:537
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition ISDOpcodes.h:366
@ FMINIMUMNUM
FMINIMUMNUM/FMAXIMUMNUM - minimumnum/maximumnum that is same with FMINNUM_IEEE and FMAXNUM_IEEE besid...
@ SADDO_CARRY
Carry-using overflow-aware nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:339
@ INTRINSIC_W_CHAIN
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition ISDOpcodes.h:213
@ ABS_MIN_POISON
ABS with a poison result for INT_MIN.
Definition ISDOpcodes.h:754
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition ISDOpcodes.h:559
bool isExtOpcode(unsigned Opcode)
LLVM_ABI bool allOperandsUndef(const SDNode *N)
Return true if the node has at least one operand and all operands of the specified node are ISD::UNDE...
This namespace contains an enum with a value for every intrinsic/builtin function known by LLVM.
LLVM_ABI StringRef getName(ID id)
Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
LLVM_ABI StringRef getBaseName(ID id)
Return the LLVM name for an intrinsic, without encoded types for overloading, such as "llvm....
@ Bitcast
Perform the operation on a different, but equivalently sized type.
OneUse_match< SubPat > m_OneUse(const SubPat &SP)
@ ATOMIC_CMP_SWAP_B128
These nodes are used to lower atomic instructions with i128 type.
@ DeviceParam
Definition NVPTX.h:333
@ EntryParam
Definition NVPTX.h:327
bool isPackedVectorTy(EVT VT)
DivPrecisionLevel
Definition NVPTX.h:464
match_combine_or< CastInst_match< OpTy, TruncInst >, OpTy > m_TruncOrSelf(const OpTy &Op)
specific_intval< false > m_SpecificInt(const APInt &V)
Match a specific integer value or vector with all elements equal to the value.
match_deferred< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
auto m_Value()
Match an arbitrary value and ignore it.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
BinOpPred_match< LHS, RHS, is_bitwiselogic_op > m_BitwiseLogic(const LHS &L, const RHS &R)
Matches bitwise logic operations.
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
initializer< Ty > init(const Ty &Val)
StringRef GetRoundingModeName(APFloat::roundingMode RM)
@ User
could "use" a pointer
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
This is an optimization pass for GlobalISel generic memory operations.
@ Low
Lower the current thread's priority such that it does not affect foreground tasks significantly.
Definition Threading.h:280
@ Offset
Definition DWP.cpp:577
detail::zippy< detail::zip_shortest, T, U, Args... > zip(T &&t, U &&u, Args &&...args)
zip iterator for two or more iteratable types.
Definition STLExtras.h:846
Align getDeviceByValParamAlign(const Function *F, Type *ArgTy, unsigned AttrIdx, const DataLayout &DL)
The .param-space alignment for a byval parameter or call argument: the (possibly promoted) parameter ...
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:1755
SDValue peekThroughFreeze(SDValue V)
Return the non-frozen source operand of V if it exists.
RelativeUniformCounterPtr Values
Definition InstrProf.h:91
@ Known
Known to have no common set bits.
LLVM_ABI void ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, Type *Ty, SmallVectorImpl< EVT > &ValueVTs, SmallVectorImpl< EVT > *MemVTs=nullptr, SmallVectorImpl< TypeSize > *Offsets=nullptr, TypeSize StartingOffset=TypeSize::getZero())
ComputeValueVTs - Given an LLVM IR type, compute a sequence of EVTs that represent all the individual...
Definition Analysis.cpp:119
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2570
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
@ Store
The extracted value is stored (ExtractElement only).
Align getPTXParamTypeAlign(Type *ArgTy, const DataLayout &DL)
ABI alignment of ArgTy in .param space, capped at the PTX maximum of 128.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
uint64_t PowerOf2Ceil(uint64_t A)
Returns the power of two which is greater than or equal to the given value.
Definition MathExtras.h:380
bool isReleaseOrStronger(AtomicOrdering AO)
OutputIt transform(R &&Range, OutputIt d_first, UnaryFunction F)
Wrapper function around std::transform to apply a function to a range and store the result elsewhere.
Definition STLExtras.h:2042
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
std::optional< SyncScope::ID > getAtomicSyncScopeID(const Instruction *I)
A helper function that returns an atomic operation's sync scope; returns std::nullopt if it is not an...
unsigned promoteScalarArgumentSize(unsigned size)
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1769
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
bool shouldPassAsArray(Type *Ty)
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
iterator_range< filter_iterator< detail::IterOfRange< RangeT >, PredicateT > > make_filter_range(RangeT &&Range, PredicateT Pred)
Convenience function that takes a range of elements and a predicate, and return a new filter_iterator...
Definition STLExtras.h:552
CodeGenOptLevel
Code generation optimization level.
Definition CodeGen.h:227
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
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
AtomicOrdering
Atomic ordering for LLVM's memory model.
@ Sub
Subtraction of integers.
@ Add
Sum of integers.
@ FAdd
Sum of floats.
DWARFExpression::Operation Op
RoundingMode
Rounding mode.
Align getPTXParamAlign(const Function *F, Type *Ty, unsigned AttrIdx, const DataLayout &DL)
Alignment for a function parameter or return value at AttributeList index AttrIdx (FirstArgIndex + ar...
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.
bool isAcquireOrStronger(AtomicOrdering AO)
constexpr unsigned BitWidth
bool isKernelFunction(const Function &F)
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
constexpr auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
Definition Sequence.h:341
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition Alignment.h:201
unsigned getFromTypeWidthForLoad(const MemSDNode *Mem)
The bit-width of a single element loaded by Mem, i.e.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:880
#define N
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
@ PreserveSign
The sign of a flushed-to-zero number is preserved in the sign of 0.
DenormalModeKind Output
Denormal flushing mode for floating point instruction results in the default floating point environme...
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:418
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition ValueTypes.h:145
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:70
EVT changeTypeToInteger() const
Return the type converted to an equivalently sized integer or vector with integer element type.
Definition ValueTypes.h:129
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
Definition ValueTypes.h:307
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition ValueTypes.h:323
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition ValueTypes.h:155
ElementCount getVectorElementCount() const
Definition ValueTypes.h:373
bool is32BitVector() const
Return true if this is a 32-bit vector type.
Definition ValueTypes.h:220
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:396
uint64_t getScalarSizeInBits() const
Definition ValueTypes.h:408
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:339
uint64_t getFixedSizeInBits() const
Return the size of the specified fixed width value type in bits.
Definition ValueTypes.h:404
LLVM_ABI std::string getEVTString() const
This function returns value type as a string, e.g. "i32".
bool isVector() const
Return true if this is a vector value type.
Definition ValueTypes.h:176
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition ValueTypes.h:346
bool bitsEq(EVT VT) const
Return true if this has the same number of bits as VT.
Definition ValueTypes.h:279
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:351
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:121
bool isScalarInteger() const
Return true if this is an integer, but not a vector.
Definition ValueTypes.h:165
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition ValueTypes.h:359
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition ValueTypes.h:160
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition KnownBits.h:315
static LLVM_ABI KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
KnownBits concat(const KnownBits &Lo) const
Concatenate the bits from Lo onto the bottom of *this.
Definition KnownBits.h:247
unsigned getBitWidth() const
Get the bit width of this value.
Definition KnownBits.h:44
unsigned countMaxActiveBits() const
Returns the maximum number of bits needed to represent all possible unsigned values with these known ...
Definition KnownBits.h:310
This class contains a discriminated union of information about pointers in memory operands,...
MachinePointerInfo getWithOffset(int64_t O) const
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 hasAllowContract() const
This represents a list of ValueType's that has been intern'd by a SelectionDAG.
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
Type * RetTy
Same as OrigRetTy, or partially legalized for soft float libcalls.
LLVM_ABI SDValue CombineTo(SDNode *N, ArrayRef< SDValue > To, bool AddTo=true)
A convenience struct that encapsulates a DAG, and two SDValues for returning information from TargetL...