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