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