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