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