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