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