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