LLVM 24.0.0git
DXILIntrinsicExpansion.cpp
Go to the documentation of this file.
1//===- DXILIntrinsicExpansion.cpp - Prepare LLVM Module for DXIL encoding--===//
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/// \file This file contains DXIL intrinsic expansions for those that don't have
10// opcodes in DirectX Intermediate Language (DXIL).
11//===----------------------------------------------------------------------===//
12
13#include "DirectX.h"
14#include "llvm/ADT/APInt.h"
15#include "llvm/ADT/STLExtras.h"
17#include "llvm/CodeGen/Passes.h"
18#include "llvm/IR/Constants.h"
19#include "llvm/IR/IRBuilder.h"
20#include "llvm/IR/InstrTypes.h"
21#include "llvm/IR/Instruction.h"
23#include "llvm/IR/Intrinsics.h"
24#include "llvm/IR/IntrinsicsDirectX.h"
26#include "llvm/IR/Module.h"
27#include "llvm/IR/PassManager.h"
28#include "llvm/IR/Type.h"
29#include "llvm/Pass.h"
33
34#define DEBUG_TYPE "dxil-intrinsic-expansion"
35
36using namespace llvm;
37
39
40public:
41 bool runOnModule(Module &M) override;
43
44 static char ID; // Pass identification.
45};
46
47static bool resourceAccessNeeds64BitExpansion(Module *M, Type *OverloadTy,
48 bool IsRaw) {
49 if (IsRaw && M->getTargetTriple().getDXILVersion() > VersionTuple(1, 2))
50 return false;
51
52 Type *ScalarTy = OverloadTy->getScalarType();
53 return ScalarTy->isDoubleTy() || ScalarTy->isIntegerTy(64);
54}
55
57 Module *M = Orig->getModule();
58 if (M->getTargetTriple().getDXILVersion() >= VersionTuple(1, 9))
59 return nullptr;
60
61 Value *Val = Orig->getOperand(0);
62 Type *ValTy = Val->getType();
63 if (!ValTy->getScalarType()->isHalfTy())
64 return nullptr;
65
66 IRBuilder<> Builder(Orig);
67 Type *IType = Type::getInt16Ty(M->getContext());
68 Constant *PosInf =
69 ValTy->isVectorTy()
72 cast<FixedVectorType>(ValTy)->getNumElements()),
73 ConstantInt::get(IType, 0x7c00))
74 : ConstantInt::get(IType, 0x7c00);
75
76 Constant *NegInf =
77 ValTy->isVectorTy()
80 cast<FixedVectorType>(ValTy)->getNumElements()),
81 ConstantInt::get(IType, 0xfc00))
82 : ConstantInt::get(IType, 0xfc00);
83
84 Value *IVal = Builder.CreateBitCast(Val, PosInf->getType());
85 Value *B1 = Builder.CreateICmpEQ(IVal, PosInf);
86 Value *B2 = Builder.CreateICmpEQ(IVal, NegInf);
87 Value *B3 = Builder.CreateOr(B1, B2);
88 return B3;
89}
90
92 Module *M = Orig->getModule();
93 if (M->getTargetTriple().getDXILVersion() >= VersionTuple(1, 9))
94 return nullptr;
95
96 Value *Val = Orig->getOperand(0);
97 Type *ValTy = Val->getType();
98 if (!ValTy->getScalarType()->isHalfTy())
99 return nullptr;
100
101 IRBuilder<> Builder(Orig);
102 Type *IType = Type::getInt16Ty(M->getContext());
103
104 Constant *ExpBitMask =
105 ValTy->isVectorTy()
108 cast<FixedVectorType>(ValTy)->getNumElements()),
109 ConstantInt::get(IType, 0x7c00))
110 : ConstantInt::get(IType, 0x7c00);
111 Constant *SigBitMask =
112 ValTy->isVectorTy()
115 cast<FixedVectorType>(ValTy)->getNumElements()),
116 ConstantInt::get(IType, 0x3ff))
117 : ConstantInt::get(IType, 0x3ff);
118
119 Constant *Zero =
120 ValTy->isVectorTy()
123 cast<FixedVectorType>(ValTy)->getNumElements()),
124 ConstantInt::get(IType, 0))
125 : ConstantInt::get(IType, 0);
126
127 Value *IVal = Builder.CreateBitCast(Val, ExpBitMask->getType());
128 Value *Exp = Builder.CreateAnd(IVal, ExpBitMask);
129 Value *B1 = Builder.CreateICmpEQ(Exp, ExpBitMask);
130
131 Value *Sig = Builder.CreateAnd(IVal, SigBitMask);
132 Value *B2 = Builder.CreateICmpNE(Sig, Zero);
133 Value *B3 = Builder.CreateAnd(B1, B2);
134 return B3;
135}
136
138 Module *M = Orig->getModule();
139 if (M->getTargetTriple().getDXILVersion() >= VersionTuple(1, 9))
140 return nullptr;
141
142 Value *Val = Orig->getOperand(0);
143 Type *ValTy = Val->getType();
144 if (!ValTy->getScalarType()->isHalfTy())
145 return nullptr;
146
147 IRBuilder<> Builder(Orig);
148 Type *IType = Type::getInt16Ty(M->getContext());
149
150 Constant *ExpBitMask =
151 ValTy->isVectorTy()
154 cast<FixedVectorType>(ValTy)->getNumElements()),
155 ConstantInt::get(IType, 0x7c00))
156 : ConstantInt::get(IType, 0x7c00);
157
158 Value *IVal = Builder.CreateBitCast(Val, ExpBitMask->getType());
159 Value *Exp = Builder.CreateAnd(IVal, ExpBitMask);
160 Value *B1 = Builder.CreateICmpNE(Exp, ExpBitMask);
161 return B1;
162}
163
165 Module *M = Orig->getModule();
166 if (M->getTargetTriple().getDXILVersion() >= VersionTuple(1, 9))
167 return nullptr;
168
169 Value *Val = Orig->getOperand(0);
170 Type *ValTy = Val->getType();
171 if (!ValTy->getScalarType()->isHalfTy())
172 return nullptr;
173
174 IRBuilder<> Builder(Orig);
175 Type *IType = Type::getInt16Ty(M->getContext());
176
177 Constant *ExpBitMask =
178 ValTy->isVectorTy()
181 cast<FixedVectorType>(ValTy)->getNumElements()),
182 ConstantInt::get(IType, 0x7c00))
183 : ConstantInt::get(IType, 0x7c00);
184 Constant *Zero =
185 ValTy->isVectorTy()
188 cast<FixedVectorType>(ValTy)->getNumElements()),
189 ConstantInt::get(IType, 0))
190 : ConstantInt::get(IType, 0);
191
192 Value *IVal = Builder.CreateBitCast(Val, ExpBitMask->getType());
193 Value *Exp = Builder.CreateAnd(IVal, ExpBitMask);
194 Value *NotAllZeroes = Builder.CreateICmpNE(Exp, Zero);
195 Value *NotAllOnes = Builder.CreateICmpNE(Exp, ExpBitMask);
196 Value *B1 = Builder.CreateAnd(NotAllZeroes, NotAllOnes);
197 return B1;
198}
199
201 assert(F.getIntrinsicID() == Intrinsic::dx_fdot &&
202 "Function is not a dx.fdot intrinsic");
203 auto *ParamTy = cast<FixedVectorType>(F.getFunctionType()->getParamType(0));
204 return ParamTy->getNumElements() <= 4 ||
205 F.getParent()->getTargetTriple().getOSVersion() < VersionTuple(6, 9);
206}
207
209 switch (F.getIntrinsicID()) {
210 case Intrinsic::assume:
211 case Intrinsic::abs:
212 case Intrinsic::atan2:
213 case Intrinsic::copysign:
214 case Intrinsic::fshl:
215 case Intrinsic::fshr:
216 case Intrinsic::exp:
217 case Intrinsic::is_fpclass:
218 case Intrinsic::log:
219 case Intrinsic::log10:
220 case Intrinsic::pow:
221 case Intrinsic::powi:
222 case Intrinsic::dx_uclamp:
223 case Intrinsic::dx_sclamp:
224 case Intrinsic::dx_nclamp:
225 case Intrinsic::dx_isfinite:
226 case Intrinsic::dx_isinf:
227 case Intrinsic::dx_isnan:
228 case Intrinsic::dx_sdot:
229 case Intrinsic::dx_udot:
230 case Intrinsic::dx_sign:
231 case Intrinsic::usub_sat:
232 case Intrinsic::vector_reduce_add:
233 case Intrinsic::vector_reduce_fadd:
234 case Intrinsic::matrix_multiply:
235 case Intrinsic::matrix_transpose:
236 case Intrinsic::umul_with_overflow:
237 case Intrinsic::smul_with_overflow:
238 case Intrinsic::dx_load_input:
239 case Intrinsic::dx_store_output:
240 return true;
241 case Intrinsic::vector_reduce_and:
242 case Intrinsic::vector_reduce_or:
243 return F.getParent()->getTargetTriple().getDXILVersion() <
244 VersionTuple(1, 9);
245 case Intrinsic::dx_quad_all:
246 return F.getParent()->getTargetTriple().getDXILVersion() <
247 VersionTuple(1, 7);
248 case Intrinsic::dx_fdot:
250 case Intrinsic::dx_resource_load_rawbuffer:
252 F.getParent(), F.getReturnType()->getStructElementType(0),
253 /*IsRaw*/ true);
254 case Intrinsic::dx_resource_load_typedbuffer:
256 F.getParent(), F.getReturnType()->getStructElementType(0),
257 /*IsRaw*/ false);
258 case Intrinsic::dx_resource_store_rawbuffer:
260 F.getParent(), F.getFunctionType()->getParamType(3), /*IsRaw*/ true);
261 case Intrinsic::dx_resource_store_typedbuffer:
263 F.getParent(), F.getFunctionType()->getParamType(2), /*IsRaw*/ false);
264 case Intrinsic::dx_resource_load_level:
265 return resourceAccessNeeds64BitExpansion(F.getParent(), F.getReturnType(),
266 /*IsRaw*/ false);
267 case Intrinsic::dx_resource_store_texture:
269 F.getParent(), F.getFunctionType()->getParamType(2), /*IsRaw*/ false);
270 }
271 return false;
272}
273
275 Value *A = Orig->getArgOperand(0);
276 Value *B = Orig->getArgOperand(1);
277 Type *Ty = A->getType();
278
279 IRBuilder<> Builder(Orig);
280
281 Value *Cmp = Builder.CreateICmpULT(A, B, "usub.cmp");
282 Value *Sub = Builder.CreateSub(A, B, "usub.sub");
283 Value *Zero = ConstantInt::get(Ty, 0);
284 return Builder.CreateSelect(Cmp, Zero, Sub, "usub.sat");
285}
286
287// Compute the high N bits of the 2N-bit unsigned product of two N-bit values
288// using only N-bit arithmetic, so we don't introduce a wider integer type that
289// may be unsupported in DXIL.
291 Type *Ty, unsigned BW) {
292 assert(BW % 2 == 0 && "high-half split needs symmetric halves");
293 unsigned Half = BW / 2;
294 Value *HalfShift = ConstantInt::get(Ty, Half);
295 Value *LoMask = ConstantInt::get(Ty, APInt::getLowBitsSet(BW, Half));
296
297 Value *U0 = Builder.CreateAnd(A, LoMask);
298 Value *U1 = Builder.CreateLShr(A, HalfShift);
299 Value *V0 = Builder.CreateAnd(B, LoMask);
300 Value *V1 = Builder.CreateLShr(B, HalfShift);
301
302 Value *W0 = Builder.CreateMul(U0, V0);
303 Value *T = Builder.CreateAdd(Builder.CreateMul(U1, V0),
304 Builder.CreateLShr(W0, HalfShift));
305 Value *W1 = Builder.CreateAnd(T, LoMask);
306 Value *W2 = Builder.CreateLShr(T, HalfShift);
307 W1 = Builder.CreateAdd(Builder.CreateMul(U0, V1), W1);
308 return Builder.CreateAdd(Builder.CreateAdd(Builder.CreateMul(U1, V1), W2),
309 Builder.CreateLShr(W1, HalfShift));
310}
311
312// Expand a {u,s}mul.with.overflow intrinsic. The low half of the result is a
313// plain multiply; overflow is derived from the high half of the double-width
314// product.
316 IRBuilder<> Builder(Orig);
317 Value *A = Orig->getArgOperand(0);
318 Value *B = Orig->getArgOperand(1);
319 Type *Ty = A->getType();
320 unsigned BW = Ty->getScalarSizeInBits();
321
322 Value *Lo;
323 Value *Ov;
324
325 // A plain double-width multiply is simplest, but we avoid it once it would
326 // introduce a 64-bit (or wider) integer, which DXIL does not always support.
327 // For i32 we use the native DXIL IMul/UMul ops, which return the full product
328 // as two i32s; wider types fall back to a same-width high-half computation.
329 if (2 * BW <= 32) {
330 Lo = Builder.CreateMul(A, B);
331 Type *WideTy = Ty->getWithNewBitWidth(2 * BW);
332 Value *WideA =
333 Signed ? Builder.CreateSExt(A, WideTy) : Builder.CreateZExt(A, WideTy);
334 Value *WideB =
335 Signed ? Builder.CreateSExt(B, WideTy) : Builder.CreateZExt(B, WideTy);
336 Value *Wide = Builder.CreateMul(WideA, WideB);
337 if (Signed) {
338 // Overflow when the full product doesn't fit back into BW signed bits.
339 Ov = Builder.CreateICmpNE(Wide, Builder.CreateSExt(Lo, WideTy));
340 } else {
341 Value *Hi = Builder.CreateLShr(Wide, ConstantInt::get(WideTy, BW));
342 Ov = Builder.CreateICmpNE(Hi, ConstantInt::get(WideTy, 0));
343 }
344 } else if (BW == 32) {
345 // IMul/UMul return {high, low}; index 0 is the high 32 bits.
346 Type *ResTy = StructType::get(Ty, Ty);
347 Intrinsic::ID IntrinsicID =
348 Signed ? Intrinsic::dx_imul : Intrinsic::dx_umul;
349 Value *Mul = Builder.CreateIntrinsic(ResTy, IntrinsicID, {A, B});
350 Value *Hi = Builder.CreateExtractValue(Mul, 0);
351 Lo = Builder.CreateExtractValue(Mul, 1);
352 if (Signed)
353 Ov = Builder.CreateICmpNE(
354 Hi, Builder.CreateAShr(Lo, ConstantInt::get(Ty, BW - 1)));
355 else
356 Ov = Builder.CreateICmpNE(Hi, ConstantInt::get(Ty, 0));
357 } else {
358 Lo = Builder.CreateMul(A, B);
359 Value *Hi = createMulHighUnsigned(Builder, A, B, Ty, BW);
360 if (Signed) {
361 // Turn the unsigned high half into the signed one, then overflow means it
362 // isn't the sign extension of the low half.
363 Value *SignShift = ConstantInt::get(Ty, BW - 1);
364 Value *ASign = Builder.CreateAShr(A, SignShift);
365 Value *BSign = Builder.CreateAShr(B, SignShift);
366 Hi = Builder.CreateSub(Hi, Builder.CreateAnd(ASign, B));
367 Hi = Builder.CreateSub(Hi, Builder.CreateAnd(BSign, A));
368 Ov = Builder.CreateICmpNE(Hi, Builder.CreateAShr(Lo, SignShift));
369 } else {
370 Ov = Builder.CreateICmpNE(Hi, ConstantInt::get(Ty, 0));
371 }
372 }
373
374 Value *Agg = PoisonValue::get(Orig->getType());
375 Agg = Builder.CreateInsertValue(Agg, Lo, 0);
376 return Builder.CreateInsertValue(Agg, Ov, 1);
377}
378
379static Value *expandVecReduceAdd(CallInst *Orig, Intrinsic::ID IntrinsicId) {
380 assert(IntrinsicId == Intrinsic::vector_reduce_add ||
381 IntrinsicId == Intrinsic::vector_reduce_fadd);
382
383 IRBuilder<> Builder(Orig);
384 bool IsFAdd = (IntrinsicId == Intrinsic::vector_reduce_fadd);
385
386 Value *X = Orig->getOperand(IsFAdd ? 1 : 0);
387 Type *Ty = X->getType();
388 auto *XVec = dyn_cast<FixedVectorType>(Ty);
389 unsigned XVecSize = XVec->getNumElements();
390 Value *Sum = Builder.CreateExtractElement(X, static_cast<uint64_t>(0));
391
392 // Handle the initial start value for floating-point addition.
393 if (IsFAdd) {
394 Constant *StartValue = dyn_cast<Constant>(Orig->getOperand(0));
395 if (StartValue && !StartValue->isNullValue())
396 Sum = Builder.CreateFAdd(Sum, StartValue);
397 }
398
399 // Accumulate the remaining vector elements.
400 for (unsigned I = 1; I < XVecSize; I++) {
401 Value *Elt = Builder.CreateExtractElement(X, I);
402 if (IsFAdd)
403 Sum = Builder.CreateFAdd(Sum, Elt);
404 else
405 Sum = Builder.CreateAdd(Sum, Elt);
406 }
407
408 return Sum;
409}
410
412 assert(IntrinsicId == Intrinsic::vector_reduce_and ||
413 IntrinsicId == Intrinsic::vector_reduce_or);
414
415 IRBuilder<> Builder(Orig);
416 Value *X = Orig->getArgOperand(0);
417 auto *XVec = cast<FixedVectorType>(X->getType());
418 Value *Result = Builder.CreateExtractElement(X, static_cast<uint64_t>(0));
419 for (unsigned I = 1; I < XVec->getNumElements(); ++I) {
420 Value *Elt = Builder.CreateExtractElement(X, I);
421 Result = IntrinsicId == Intrinsic::vector_reduce_or
422 ? Builder.CreateOr(Result, Elt)
423 : Builder.CreateAnd(Result, Elt);
424 }
425 return Result;
426}
427
428static Value *expandAbs(CallInst *Orig) {
429 Value *X = Orig->getOperand(0);
430 IRBuilder<> Builder(Orig);
431 Type *Ty = X->getType();
432 Type *EltTy = Ty->getScalarType();
433 Constant *Zero = Ty->isVectorTy()
436 cast<FixedVectorType>(Ty)->getNumElements()),
437 ConstantInt::get(EltTy, 0))
438 : ConstantInt::get(EltTy, 0);
439 auto *V = Builder.CreateSub(Zero, X);
440 return Builder.CreateIntrinsic(Ty, Intrinsic::smax, {X, V}, nullptr,
441 "dx.max");
442}
443
444// Create a DXIL dot2, dot3, or dot4 for the given operands.
446 Type *ATy = A->getType();
447 [[maybe_unused]] Type *BTy = B->getType();
448 assert(ATy->isVectorTy() && BTy->isVectorTy());
449
450 IRBuilder<> Builder(Orig);
451
452 auto *AVec = dyn_cast<FixedVectorType>(ATy);
453
455
456 unsigned NumElts = AVec->getNumElements();
457 Intrinsic::ID DotIntrinsic;
458 switch (NumElts) {
459 case 2:
460 DotIntrinsic = Intrinsic::dx_dot2;
461 break;
462 case 3:
463 DotIntrinsic = Intrinsic::dx_dot3;
464 break;
465 case 4:
466 DotIntrinsic = Intrinsic::dx_dot4;
467 break;
468 default:
470 "Invalid dot product input vector: length is outside 2-4");
471 }
472
474 for (unsigned I = 0; I < NumElts; ++I)
475 Args.push_back(Builder.CreateExtractElement(A, Builder.getInt32(I)));
476 for (unsigned I = 0; I < NumElts; ++I)
477 Args.push_back(Builder.CreateExtractElement(B, Builder.getInt32(I)));
478 return Builder.CreateIntrinsic(ATy->getScalarType(), DotIntrinsic, Args,
479 nullptr, "dot");
480}
481
482// Expand an arbitrary-width float dot into the minimum number of legal DXIL
483// dot2, dot3, and dot4 operations.
485 Value *A = Orig->getOperand(0);
486 Value *B = Orig->getOperand(1);
487 unsigned NumElts = cast<FixedVectorType>(A->getType())->getNumElements();
488
489 // We return early here to avoid constructing unnecessary identity shuffles.
490 if (NumElts <= 4)
491 return expandFloatDotChunk(Orig, A, B);
492
494 VersionTuple(6, 9) &&
495 "long fdot must not be expanded for shader model 6.9 or later");
496
497 IRBuilder<> Builder(Orig);
498 Value *Result = nullptr;
499 for (unsigned Offset = 0; Offset < NumElts;) {
500 unsigned Remaining = NumElts - Offset;
501 // Taking four is optimal unless it would leave an illegal one-element
502 // tail. In that case, take three and finish with dot2.
503 unsigned ChunkSize = Remaining == 5 ? 3 : std::min(Remaining, 4u);
505 for (unsigned I = 0; I < ChunkSize; ++I)
506 Mask.push_back(Offset + I);
507 Value *AChunk = Builder.CreateShuffleVector(A, Mask);
508 Value *BChunk = Builder.CreateShuffleVector(B, Mask);
509 Value *Chunk = expandFloatDotChunk(Orig, AChunk, BChunk);
510 Result = Result ? Builder.CreateFAdd(Result, Chunk, "dot.add") : Chunk;
511 Offset += ChunkSize;
512 }
513 return Result;
514}
515
516// Expand integer dot product to multiply and add ops
518 Intrinsic::ID DotIntrinsic) {
519 assert(DotIntrinsic == Intrinsic::dx_sdot ||
520 DotIntrinsic == Intrinsic::dx_udot);
521 Value *A = Orig->getOperand(0);
522 Value *B = Orig->getOperand(1);
523 Type *ATy = A->getType();
524 [[maybe_unused]] Type *BTy = B->getType();
525 assert(ATy->isVectorTy() && BTy->isVectorTy());
526
527 IRBuilder<> Builder(Orig);
528
529 auto *AVec = dyn_cast<FixedVectorType>(ATy);
530
532
533 Value *Result;
534 Intrinsic::ID MadIntrinsic = DotIntrinsic == Intrinsic::dx_sdot
535 ? Intrinsic::dx_imad
536 : Intrinsic::dx_umad;
537 Value *Elt0 = Builder.CreateExtractElement(A, (uint64_t)0);
538 Value *Elt1 = Builder.CreateExtractElement(B, (uint64_t)0);
539 Result = Builder.CreateMul(Elt0, Elt1);
540 for (unsigned I = 1; I < AVec->getNumElements(); I++) {
541 Elt0 = Builder.CreateExtractElement(A, I);
542 Elt1 = Builder.CreateExtractElement(B, I);
543 Result = Builder.CreateIntrinsic(Result->getType(), MadIntrinsic,
544 ArrayRef<Value *>{Elt0, Elt1, Result},
545 nullptr, "dx.mad");
546 }
547 return Result;
548}
549
551 Value *X = Orig->getOperand(0);
552 IRBuilder<> Builder(Orig);
553 Type *Ty = X->getType();
554 Type *EltTy = Ty->getScalarType();
555 Constant *Log2eConst =
556 Ty->isVectorTy() ? ConstantVector::getSplat(
558 cast<FixedVectorType>(Ty)->getNumElements()),
559 ConstantFP::get(EltTy, numbers::log2ef))
560 : ConstantFP::get(EltTy, numbers::log2ef);
561 Value *NewX = Builder.CreateFMul(Log2eConst, X);
562 CallInst *Exp2Call = Builder.CreateIntrinsicWithoutFolding(
563 Ty, Intrinsic::exp2, {NewX}, nullptr, "dx.exp2");
564 Exp2Call->setTailCall(Orig->isTailCall());
565 Exp2Call->setAttributes(Orig->getAttributes());
566 return Exp2Call;
567}
568
570 Value *T = Orig->getArgOperand(1);
571 auto *TCI = dyn_cast<ConstantInt>(T);
572
573 // These FPClassTest cases have DXIL opcodes, so they will be handled in
574 // DXIL Op Lowering instead for all non f16 cases.
575 switch (TCI->getZExtValue()) {
577 return expand16BitIsInf(Orig);
579 return expand16BitIsNaN(Orig);
581 return expand16BitIsNormal(Orig);
583 return expand16BitIsFinite(Orig);
584 }
585
586 IRBuilder<> Builder(Orig);
587
588 Value *F = Orig->getArgOperand(0);
589 Type *FTy = F->getType();
590 unsigned FNumElem = 0; // 0 => F is not a vector
591
592 unsigned BitWidth; // Bit width of F or the ElemTy of F
593 Type *BitCastTy; // An IntNTy of the same bitwidth as F or ElemTy of F
594
595 if (auto *FVecTy = dyn_cast<FixedVectorType>(FTy)) {
596 Type *ElemTy = FVecTy->getElementType();
597 FNumElem = FVecTy->getNumElements();
598 BitWidth = ElemTy->getPrimitiveSizeInBits();
599 BitCastTy = FixedVectorType::get(Builder.getIntNTy(BitWidth), FNumElem);
600 } else {
602 BitCastTy = Builder.getIntNTy(BitWidth);
603 }
604
605 Value *FBitCast = Builder.CreateBitCast(F, BitCastTy);
606 switch (TCI->getZExtValue()) {
608 Value *NegZero = ConstantInt::get(Builder.getIntNTy(BitWidth),
610 Value *RetVal;
611 if (FNumElem) {
612 Value *NegZeroSplat = Builder.CreateVectorSplat(FNumElem, NegZero);
613 RetVal =
614 Builder.CreateICmpEQ(FBitCast, NegZeroSplat, "is.fpclass.negzero");
615 } else
616 RetVal = Builder.CreateICmpEQ(FBitCast, NegZero, "is.fpclass.negzero");
617 return RetVal;
618 }
619 default:
620 reportFatalUsageError("Unsupported FPClassTest");
621 }
622}
623
625 float LogConstVal = numbers::ln2f) {
626 Value *X = Orig->getOperand(0);
627 IRBuilder<> Builder(Orig);
628 Type *Ty = X->getType();
629 Type *EltTy = Ty->getScalarType();
630 Constant *Ln2Const =
631 Ty->isVectorTy() ? ConstantVector::getSplat(
633 cast<FixedVectorType>(Ty)->getNumElements()),
634 ConstantFP::get(EltTy, LogConstVal))
635 : ConstantFP::get(EltTy, LogConstVal);
636 CallInst *Log2Call = Builder.CreateIntrinsicWithoutFolding(
637 Ty, Intrinsic::log2, {X}, nullptr, "elt.log2");
638 Log2Call->setTailCall(Orig->isTailCall());
639 Log2Call->setAttributes(Orig->getAttributes());
640 return Builder.CreateFMul(Ln2Const, Log2Call);
641}
645
647 Value *Y = Orig->getOperand(0);
648 Value *X = Orig->getOperand(1);
649 Type *Ty = X->getType();
650 IRBuilder<> Builder(Orig);
651 Builder.setFastMathFlags(Orig->getFastMathFlags());
652
653 Value *Tan = Builder.CreateFDiv(Y, X);
654
655 CallInst *Atan = Builder.CreateIntrinsicWithoutFolding(
656 Ty, Intrinsic::atan, {Tan}, nullptr, "Elt.Atan");
657 Atan->setTailCall(Orig->isTailCall());
658 Atan->setAttributes(Orig->getAttributes());
659
660 // Modify atan result based on https://en.wikipedia.org/wiki/Atan2.
661 Constant *Pi = ConstantFP::get(Ty, llvm::numbers::pi);
662 Constant *HalfPi = ConstantFP::get(Ty, llvm::numbers::pi / 2);
663 Constant *NegHalfPi = ConstantFP::get(Ty, -llvm::numbers::pi / 2);
664 Constant *Zero = ConstantFP::get(Ty, 0);
665 Value *AtanAddPi = Builder.CreateFAdd(Atan, Pi);
666 Value *AtanSubPi = Builder.CreateFSub(Atan, Pi);
667
668 // x > 0 -> atan.
669 Value *Result = Atan;
670 Value *XLt0 = Builder.CreateFCmpOLT(X, Zero);
671 Value *XEq0 = Builder.CreateFCmpOEQ(X, Zero);
672 Value *YGe0 = Builder.CreateFCmpOGE(Y, Zero);
673 Value *YLt0 = Builder.CreateFCmpOLT(Y, Zero);
674
675 // x < 0, y >= 0 -> atan + pi.
676 Value *XLt0AndYGe0 = Builder.CreateAnd(XLt0, YGe0);
677 Result = Builder.CreateSelect(XLt0AndYGe0, AtanAddPi, Result);
678
679 // x < 0, y < 0 -> atan - pi.
680 Value *XLt0AndYLt0 = Builder.CreateAnd(XLt0, YLt0);
681 Result = Builder.CreateSelect(XLt0AndYLt0, AtanSubPi, Result);
682
683 // x == 0, y < 0 -> -pi/2
684 Value *XEq0AndYLt0 = Builder.CreateAnd(XEq0, YLt0);
685 Result = Builder.CreateSelect(XEq0AndYLt0, NegHalfPi, Result);
686
687 // x == 0, y > 0 -> pi/2
688 Value *XEq0AndYGe0 = Builder.CreateAnd(XEq0, YGe0);
689 Result = Builder.CreateSelect(XEq0AndYGe0, HalfPi, Result);
690
691 return Result;
692}
693
694template <bool LeftFunnel>
696 Type *Ty = Orig->getType();
697 Value *A = Orig->getOperand(0);
698 Value *B = Orig->getOperand(1);
699 Value *Shift = Orig->getOperand(2);
700
701 IRBuilder<> Builder(Orig);
702
703 assert(llvm::isPowerOf2_32(Ty->getScalarSizeInBits()) &&
704 "Can't use Mask to compute modulo and inverse");
705
706 // Note: if (Shift % BitWidth) == 0 then (BitWidth - Shift) == BitWidth,
707 // shifting by the bitwidth for shl/lshr returns a poisoned result. As such,
708 // we implement the same formula as LegalizerHelper::lowerFunnelShiftAsShifts.
709 //
710 // The funnel shift is expanded like so:
711 // fshl
712 // -> msb_extract((concat(A, B) << (Shift % BitWidth)), BitWidth)
713 // -> A << (Shift % BitWidth) | B >> 1 >> (BitWidth - 1 - (Shift % BitWidth))
714 // fshr
715 // -> lsb_extract((concat(A, B) >> (Shift % BitWidth), BitWidth))
716 // -> A << 1 << (BitWidth - 1 - (Shift % BitWidth)) | B >> (Shift % BitWidth)
717
718 // (BitWidth - 1) -> Mask
719 Constant *Mask = ConstantInt::get(Ty, Ty->getScalarSizeInBits() - 1);
720
721 // Shift % BitWidth
722 // -> Shift & (BitWidth - 1)
723 // -> Shift & Mask
724 Value *MaskedShift = Builder.CreateAnd(Shift, Mask);
725
726 // (BitWidth - 1) - (Shift % BitWidth)
727 // -> ~Shift & (BitWidth - 1)
728 // -> ~Shift & Mask
729 Value *NotShift = Builder.CreateNot(Shift);
730 Value *InverseShift = Builder.CreateAnd(NotShift, Mask);
731
732 Constant *One = ConstantInt::get(Ty, 1);
733 Value *ShiftedA;
734 Value *ShiftedB;
735
736 if (LeftFunnel) {
737 ShiftedA = Builder.CreateShl(A, MaskedShift);
738 Value *ShiftB1 = Builder.CreateLShr(B, One);
739 ShiftedB = Builder.CreateLShr(ShiftB1, InverseShift);
740 } else {
741 Value *ShiftA1 = Builder.CreateShl(A, One);
742 ShiftedA = Builder.CreateShl(ShiftA1, InverseShift);
743 ShiftedB = Builder.CreateLShr(B, MaskedShift);
744 }
745
746 Value *Result = Builder.CreateOr(ShiftedA, ShiftedB);
747 return Result;
748}
749
750static Value *expandPowIntrinsic(CallInst *Orig, Intrinsic::ID IntrinsicId) {
751
752 Value *X = Orig->getOperand(0);
753 Value *Y = Orig->getOperand(1);
754 Type *Ty = X->getType();
755 IRBuilder<> Builder(Orig);
756
757 if (IntrinsicId == Intrinsic::powi)
758 Y = Builder.CreateSIToFP(Y, Ty);
759
760 Value *Log2Call =
761 Builder.CreateIntrinsic(Ty, Intrinsic::log2, {X}, nullptr, "elt.log2");
762 auto *Mul = Builder.CreateFMul(Log2Call, Y);
763 CallInst *Exp2Call = Builder.CreateIntrinsicWithoutFolding(
764 Ty, Intrinsic::exp2, {Mul}, nullptr, "elt.exp2");
765 Exp2Call->setTailCall(Orig->isTailCall());
766 Exp2Call->setAttributes(Orig->getAttributes());
767 return Exp2Call;
768}
769
770/// Recombine the lo/hi i32 pairs in `I32Vec` into double or i64 values.
771/// If `Result` is a vector, they are inserted into it from element `Base` on
772/// and the updated vector is returned; otherwise the single combined scalar
773/// is returned.
774static Value *combineI32Pairs(IRBuilder<> &Builder, Value *I32Vec,
775 Value *Result, unsigned Base) {
776 unsigned NumI32 = cast<FixedVectorType>(I32Vec->getType())->getNumElements();
777 bool IsDouble = Result->getType()->getScalarType()->isDoubleTy();
778 bool IsVector = Result->getType()->isVectorTy();
779
781 for (unsigned I = 0; I < NumI32; ++I)
782 Elements.push_back(
783 Builder.CreateExtractElement(I32Vec, Builder.getInt32(I)));
784
785 for (unsigned I = 0; I < NumI32; I += 2) {
786 Value *Combined = nullptr;
787 if (IsDouble)
788 // For doubles, use dx_asdouble intrinsic
789 Combined =
790 Builder.CreateIntrinsic(Builder.getDoubleTy(), Intrinsic::dx_asdouble,
791 {Elements[I], Elements[I + 1]});
792 else {
793 // For int64, manually combine two int32s
794 // First, zero-extend both values to i64
795 Value *Lo = Builder.CreateZExt(Elements[I], Builder.getInt64Ty());
796 Value *Hi = Builder.CreateZExt(Elements[I + 1], Builder.getInt64Ty());
797 // Shift the high bits left by 32 bits
798 Value *ShiftedHi = Builder.CreateShl(Hi, Builder.getInt64(32));
799 // OR the high and low bits together
800 Combined = Builder.CreateOr(Lo, ShiftedHi);
801 }
802
803 if (IsVector)
804 Result = Builder.CreateInsertElement(Result, Combined,
805 Builder.getInt32((I / 2) + Base));
806 else
807 Result = Combined;
808 }
809 return Result;
810}
811
812/// Split the double or i64 scalar or vector `Val` into a vector of i32 that
813/// holds the low and high halves of each element in turn.
814static Value *splitIntoI32Pairs(IRBuilder<> &Builder, Value *Val) {
815 Type *ValTy = Val->getType();
816 Type *Int32Ty = Builder.getInt32Ty();
817 auto *VT = dyn_cast<FixedVectorType>(ValTy);
818 unsigned VecLen = VT ? VT->getNumElements() : 0;
819 Type *SplitElementTy =
820 VT ? static_cast<Type *>(FixedVectorType::get(Int32Ty, VecLen)) : Int32Ty;
821
822 Value *LowBits = nullptr;
823 Value *HighBits = nullptr;
824 // Split the 64-bit values into 32-bit components
825 if (ValTy->getScalarType()->isDoubleTy()) {
826 auto *SplitTy = llvm::StructType::get(SplitElementTy, SplitElementTy);
827 Value *Split =
828 Builder.CreateIntrinsic(SplitTy, Intrinsic::dx_splitdouble, {Val});
829 LowBits = Builder.CreateExtractValue(Split, 0);
830 HighBits = Builder.CreateExtractValue(Split, 1);
831 } else {
832 // Handle int64 type(s)
833 Constant *ShiftAmt = Builder.getInt64(32);
834 if (VT)
835 ShiftAmt =
837
838 // Split into low and high 32-bit parts
839 LowBits = Builder.CreateTrunc(Val, SplitElementTy);
840 Value *ShiftedVal = Builder.CreateLShr(Val, ShiftAmt);
841 HighBits = Builder.CreateTrunc(ShiftedVal, SplitElementTy);
842 }
843
844 if (VT) {
846 for (unsigned I = 0; I < VecLen; ++I) {
847 Mask.push_back(I);
848 Mask.push_back(I + VecLen);
849 }
850 return Builder.CreateShuffleVector(LowBits, HighBits, Mask);
851 }
852
853 Value *Result = PoisonValue::get(FixedVectorType::get(Int32Ty, 2));
854 Result = Builder.CreateInsertElement(Result, LowBits, Builder.getInt32(0));
855 return Builder.CreateInsertElement(Result, HighBits, Builder.getInt32(1));
856}
857
858static bool expandBufferLoadIntrinsic(CallInst *Orig, bool IsRaw) {
859 IRBuilder<> Builder(Orig);
860
861 Type *BufferTy = Orig->getType()->getStructElementType(0);
862 assert((BufferTy->getScalarType()->isDoubleTy() ||
863 BufferTy->getScalarType()->isIntegerTy(64)) &&
864 "Only expand double or int64 scalars or vectors");
865 unsigned ExtractNum = 2;
866 if (auto *VT = dyn_cast<FixedVectorType>(BufferTy)) {
867 ExtractNum = 2 * VT->getNumElements();
868 assert(IsRaw || ExtractNum == 4 && "TypedBufferLoad vector must be size 2");
869 }
870
872 Value *Result = PoisonValue::get(BufferTy);
873 unsigned Base = 0;
874 // If we need to extract more than 4 i32; we need to break it up into
875 // more than one load. LoadNum tells us how many i32s we are loading in
876 // each load
877 while (ExtractNum > 0) {
878 unsigned LoadNum = std::min(ExtractNum, 4u);
879 Type *Ty = VectorType::get(Builder.getInt32Ty(), LoadNum, false);
880
881 Type *LoadType = StructType::get(Ty, Builder.getInt1Ty());
882 Intrinsic::ID LoadIntrinsic = Intrinsic::dx_resource_load_typedbuffer;
883 SmallVector<Value *, 3> Args = {Orig->getOperand(0), Orig->getOperand(1)};
884 if (IsRaw) {
885 LoadIntrinsic = Intrinsic::dx_resource_load_rawbuffer;
886 Value *Tmp = Builder.getInt32(4 * Base * 2);
887 Value *Offset = Orig->getOperand(2);
888 Args.push_back(Offset);
889 unsigned AddressArg = isa<PoisonValue>(Offset) ? 1 : 2;
890 if (Base != 0)
891 Args[AddressArg] = Builder.CreateAdd(Args[AddressArg], Tmp);
892 }
893
894 Value *Load = Builder.CreateIntrinsic(LoadType, LoadIntrinsic, Args);
895 Loads.push_back(Load);
896
897 // Extract the buffer load's result and combine it into double(s) or
898 // int64(s).
899 Value *Extract = Builder.CreateExtractValue(Load, {0});
900 Result = combineI32Pairs(Builder, Extract, Result, Base);
901
902 ExtractNum -= LoadNum;
903 Base += LoadNum / 2;
904 }
905
906 Value *CheckBit = nullptr;
907 for (User *U : make_early_inc_range(Orig->users())) {
908 // If it's not a ExtractValueInst, we don't know how to
909 // handle it
910 auto *EVI = dyn_cast<ExtractValueInst>(U);
911 if (!EVI)
912 llvm_unreachable("Unexpected user of typedbufferload");
913
914 ArrayRef<unsigned> Indices = EVI->getIndices();
915 assert(Indices.size() == 1);
916
917 if (Indices[0] == 0) {
918 // Use of the value(s)
919 EVI->replaceAllUsesWith(Result);
920 } else {
921 // Use of the check bit
922 assert(Indices[0] == 1 && "Unexpected type for typedbufferload");
923 // Note: This does not always match the historical behaviour of DXC.
924 // See https://github.com/microsoft/DirectXShaderCompiler/issues/7622
925 if (!CheckBit) {
926 SmallVector<Value *, 2> CheckBits;
927 for (Value *L : Loads)
928 CheckBits.push_back(Builder.CreateExtractValue(L, {1}));
929 CheckBit = Builder.CreateAnd(CheckBits);
930 }
931 EVI->replaceAllUsesWith(CheckBit);
932 }
933 EVI->eraseFromParent();
934 }
935 Orig->eraseFromParent();
936 return true;
937}
938
939/// Split a 64-bit `llvm.dx.resource.load.level` into a load of lo/hi i32 pairs
940/// on the same texture, forwarding the coordinates, mip level and offsets.
942 IRBuilder<> Builder(Orig);
943
944 Type *ElementTy = Orig->getType();
945 auto *VT = dyn_cast<FixedVectorType>(ElementTy);
946 unsigned NumI32 = 2 * (VT ? VT->getNumElements() : 1);
947 assert(NumI32 <= 4 && "64-bit texture elements have at most 2 components");
948
949 SmallVector<Value *, 4> Args(Orig->args());
950 Value *Load = Builder.CreateIntrinsic(
951 FixedVectorType::get(Builder.getInt32Ty(), NumI32),
952 Intrinsic::dx_resource_load_level, Args);
953 Value *Result = combineI32Pairs(Builder, Load, PoisonValue::get(ElementTy),
954 /*Base=*/0);
955
956 Orig->replaceAllUsesWith(Result);
957 Orig->eraseFromParent();
958 return true;
959}
960
961/// Split a 64-bit `llvm.dx.resource.store.texture` into a store of lo/hi i32
962/// pairs to the same texture, forwarding the coordinates.
964 IRBuilder<> Builder(Orig);
965
966 Value *Data = splitIntoI32Pairs(Builder, Orig->getArgOperand(2));
967 assert(cast<FixedVectorType>(Data->getType())->getNumElements() <= 4 &&
968 "64-bit texture elements have at most 2 components");
969 Builder.CreateIntrinsic(
970 Builder.getVoidTy(), Intrinsic::dx_resource_store_texture,
971 {Orig->getArgOperand(0), Orig->getArgOperand(1), Data});
972 Orig->eraseFromParent();
973 return true;
974}
975
976static bool expandBufferStoreIntrinsic(CallInst *Orig, bool IsRaw) {
977 IRBuilder<> Builder(Orig);
978
979 unsigned ValIndex = IsRaw ? 3 : 2;
980 Type *BufferTy = Orig->getFunctionType()->getParamType(ValIndex);
981 assert((BufferTy->getScalarType()->isDoubleTy() ||
982 BufferTy->getScalarType()->isIntegerTy(64)) &&
983 "Only expand double or int64 scalars or vectors");
984
985 // Determine if we're dealing with a vector or scalar
986 unsigned ExtractNum = 2;
987 unsigned VecLen = 0;
988 if (auto *VT = dyn_cast<FixedVectorType>(BufferTy)) {
989 VecLen = VT->getNumElements();
990 assert(IsRaw || VecLen == 2 && "TypedBufferStore vector must be size 2");
991 ExtractNum = VecLen * 2;
992 }
993
994 Value *Val = splitIntoI32Pairs(Builder, Orig->getOperand(ValIndex));
995
996 // If we need to extract more than 4 i32; we need to break it up into
997 // more than one store. StoreNum tells us how many i32s we are storing in
998 // each store
999 unsigned Base = 0;
1000 while (ExtractNum > 0) {
1001 unsigned StoreNum = std::min(ExtractNum, 4u);
1002
1003 Intrinsic::ID StoreIntrinsic = Intrinsic::dx_resource_store_typedbuffer;
1004 SmallVector<Value *, 4> Args = {Orig->getOperand(0), Orig->getOperand(1)};
1005 if (IsRaw) {
1006 StoreIntrinsic = Intrinsic::dx_resource_store_rawbuffer;
1007 Value *Tmp = Builder.getInt32(4 * Base);
1008 Value *Offset = Orig->getOperand(2);
1009 Args.push_back(Offset);
1010 unsigned AddressArg = isa<PoisonValue>(Offset) ? 1 : 2;
1011 if (Base != 0)
1012 Args[AddressArg] = Builder.CreateAdd(Args[AddressArg], Tmp);
1013 }
1014
1016 for (unsigned I = 0; I < StoreNum; ++I) {
1017 Mask.push_back(Base + I);
1018 }
1019
1020 Value *SubVal = Val;
1021 if (VecLen > 2)
1022 SubVal = Builder.CreateShuffleVector(Val, Mask);
1023
1024 Args.push_back(SubVal);
1025 // Create the final intrinsic call
1026 Builder.CreateIntrinsic(Builder.getVoidTy(), StoreIntrinsic, Args);
1027
1028 ExtractNum -= StoreNum;
1029 Base += StoreNum;
1030 }
1031 Orig->eraseFromParent();
1032 return true;
1033}
1034
1036 if (ClampIntrinsic == Intrinsic::dx_uclamp)
1037 return Intrinsic::umax;
1038 if (ClampIntrinsic == Intrinsic::dx_sclamp)
1039 return Intrinsic::smax;
1040 assert(ClampIntrinsic == Intrinsic::dx_nclamp);
1041 return Intrinsic::maxnum;
1042}
1043
1045 if (ClampIntrinsic == Intrinsic::dx_uclamp)
1046 return Intrinsic::umin;
1047 if (ClampIntrinsic == Intrinsic::dx_sclamp)
1048 return Intrinsic::smin;
1049 assert(ClampIntrinsic == Intrinsic::dx_nclamp);
1050 return Intrinsic::minnum;
1051}
1052
1054 Intrinsic::ID ClampIntrinsic) {
1055 Value *X = Orig->getOperand(0);
1056 Value *Min = Orig->getOperand(1);
1057 Value *Max = Orig->getOperand(2);
1058 Type *Ty = X->getType();
1059 IRBuilder<> Builder(Orig);
1060 auto *MaxCall = Builder.CreateIntrinsic(Ty, getMaxForClamp(ClampIntrinsic),
1061 {X, Min}, nullptr, "dx.max");
1062 return Builder.CreateIntrinsic(Ty, getMinForClamp(ClampIntrinsic),
1063 {MaxCall, Max}, nullptr, "dx.min");
1064}
1065
1067 Value *X = Orig->getOperand(0);
1068 Type *Ty = X->getType();
1069 Type *ScalarTy = Ty->getScalarType();
1070 Type *RetTy = Orig->getType();
1072
1073 IRBuilder<> Builder(Orig);
1074
1075 Value *GT;
1076 Value *LT;
1077 if (ScalarTy->isFloatingPointTy()) {
1078 GT = Builder.CreateFCmpOLT(Zero, X);
1079 LT = Builder.CreateFCmpOLT(X, Zero);
1080 } else {
1081 assert(ScalarTy->isIntegerTy());
1082 GT = Builder.CreateICmpSLT(Zero, X);
1083 LT = Builder.CreateICmpSLT(X, Zero);
1084 }
1085
1086 Value *ZextGT = Builder.CreateZExt(GT, RetTy);
1087 Value *ZextLT = Builder.CreateZExt(LT, RetTy);
1088
1089 return Builder.CreateSub(ZextGT, ZextLT);
1090}
1091
1092// Expand llvm.copysign by combining the sign bit with the magnitude bits using
1093// bitwise operations.
1095 Value *Magnitude = Orig->getOperand(0);
1096 Value *Sign = Orig->getOperand(1);
1097 Type *Ty = Orig->getType();
1098
1099 IRBuilder<> Builder(Orig);
1100
1101 bool IsDouble = Ty->getScalarType()->isDoubleTy();
1102 unsigned BitWidth = IsDouble ? 32 : Ty->getScalarSizeInBits();
1103 Type *IntTy = Ty->getWithNewType(Builder.getIntNTy(BitWidth));
1104
1105 auto CopySignBit = [&](Value *MagnitudeInt, Value *SignInt) {
1106 APInt SignMaskVal = APInt::getSignMask(BitWidth);
1107 // `ConstantInt::get` broadcasts to a splat when `IntTy` is a vector.
1108 Constant *SignMask = ConstantInt::get(IntTy, SignMaskVal);
1109 Constant *NotSignMask = ConstantInt::get(IntTy, ~SignMaskVal);
1110
1111 Value *MagnitudeBits = Builder.CreateAnd(MagnitudeInt, NotSignMask);
1112 Value *SignBits = Builder.CreateAnd(SignInt, SignMask);
1113 return Builder.CreateOr(MagnitudeBits, SignBits);
1114 };
1115
1116 // Avoid i64 bitwise ops, which require the Int64Ops shader feature.
1117 if (IsDouble) {
1118 auto *SplitTy = StructType::get(IntTy, IntTy);
1119 Value *MagnitudeHalves = Builder.CreateIntrinsic(
1120 SplitTy, Intrinsic::dx_splitdouble, {Magnitude});
1121 Value *SignHalves =
1122 Builder.CreateIntrinsic(SplitTy, Intrinsic::dx_splitdouble, {Sign});
1123 Value *MagnitudeLow = Builder.CreateExtractValue(MagnitudeHalves, 0);
1124 Value *MagnitudeHigh = Builder.CreateExtractValue(MagnitudeHalves, 1);
1125 Value *SignHigh = Builder.CreateExtractValue(SignHalves, 1);
1126
1127 Value *CombinedHigh = CopySignBit(MagnitudeHigh, SignHigh);
1128 return Builder.CreateIntrinsic(Ty, Intrinsic::dx_asdouble,
1129 {MagnitudeLow, CombinedHigh});
1130 }
1131
1132 Value *MagnitudeInt = Builder.CreateBitCast(Magnitude, IntTy);
1133 Value *SignInt = Builder.CreateBitCast(Sign, IntTy);
1134 Value *CombinedInt = CopySignBit(MagnitudeInt, SignInt);
1135 return Builder.CreateBitCast(CombinedInt, Ty);
1136}
1137
1138// Expand llvm.matrix.multiply by extracting row/column vectors and computing
1139// dot products.
1140// Result[r,c] = dot(row_r(LHS), col_c(RHS))
1141// Element (r,c) is at index c*NumRows + r (column-major).
1143 Value *LHS = Orig->getArgOperand(0);
1144 Value *RHS = Orig->getArgOperand(1);
1145 unsigned LHSRows = cast<ConstantInt>(Orig->getArgOperand(2))->getZExtValue();
1146 unsigned LHSCols = cast<ConstantInt>(Orig->getArgOperand(3))->getZExtValue();
1147 unsigned RHSCols = cast<ConstantInt>(Orig->getArgOperand(4))->getZExtValue();
1148
1149 auto *RetTy = cast<FixedVectorType>(Orig->getType());
1150 Type *EltTy = RetTy->getElementType();
1151 bool IsFP = EltTy->isFloatingPointTy();
1152
1153 IRBuilder<> Builder(Orig);
1154
1155 // Column-major indexing:
1156 // LHS row R, element K: index = K * LHSRows + R
1157 // RHS col C, element K: index = C * LHSCols + K
1158 Value *Result = PoisonValue::get(RetTy);
1159
1160 // Extract all scalar elements from LHS and RHS once, then reuse them.
1161 unsigned LHSSize = LHSRows * LHSCols;
1162 unsigned RHSSize = LHSCols * RHSCols;
1163 SmallVector<Value *, 16> LHSElts(LHSSize);
1164 SmallVector<Value *, 16> RHSElts(RHSSize);
1165 for (unsigned I = 0; I < LHSSize; ++I)
1166 LHSElts[I] = Builder.CreateExtractElement(LHS, I);
1167 for (unsigned I = 0; I < RHSSize; ++I)
1168 RHSElts[I] = Builder.CreateExtractElement(RHS, I);
1169
1170 // Choose the appropriate scalar-arg dot intrinsic for floats.
1171 // K=1 and double types use scalar expansion instead.
1173 bool UseScalarFP = IsFP && (EltTy->isDoubleTy() || LHSCols == 1);
1174 if (IsFP && !UseScalarFP) {
1175 switch (LHSCols) {
1176 case 2:
1177 FloatDotID = Intrinsic::dx_dot2;
1178 break;
1179 case 3:
1180 FloatDotID = Intrinsic::dx_dot3;
1181 break;
1182 case 4:
1183 FloatDotID = Intrinsic::dx_dot4;
1184 break;
1185 default:
1187 "Invalid matrix inner dimension for dot product: must be 2-4");
1188 return nullptr;
1189 }
1190 }
1191
1192 for (unsigned C = 0; C < RHSCols; ++C) {
1193 for (unsigned R = 0; R < LHSRows; ++R) {
1194 // Gather row R from LHS and column C from RHS.
1195 SmallVector<Value *, 4> RowElts, ColElts;
1196 for (unsigned K = 0; K < LHSCols; ++K) {
1197 RowElts.push_back(LHSElts[K * LHSRows + R]);
1198 ColElts.push_back(RHSElts[C * LHSCols + K]);
1199 }
1200
1201 Value *Dot;
1202 if (UseScalarFP) {
1203 // Scalar fmul+fmuladd expansion for double types and K=1.
1204 Dot = Builder.CreateFMul(RowElts[0], ColElts[0]);
1205 for (unsigned K = 1; K < LHSCols; ++K)
1206 Dot = Builder.CreateIntrinsic(EltTy, Intrinsic::fmuladd,
1207 {RowElts[K], ColElts[K], Dot});
1208 } else if (IsFP) {
1209 // Emit scalar-arg DXIL dot directly (dx.dot2/dx.dot3/dx.dot4).
1211 Args.append(RowElts.begin(), RowElts.end());
1212 Args.append(ColElts.begin(), ColElts.end());
1213 Dot = Builder.CreateIntrinsic(EltTy, FloatDotID, Args);
1214 } else {
1215 // Integer: emit multiply + imad chain.
1216 Dot = Builder.CreateMul(RowElts[0], ColElts[0]);
1217 for (unsigned K = 1; K < LHSCols; ++K)
1218 Dot = Builder.CreateIntrinsic(EltTy, Intrinsic::dx_imad,
1219 {RowElts[K], ColElts[K], Dot});
1220 }
1221 unsigned ResIdx = C * LHSRows + R;
1222 Result = Builder.CreateInsertElement(Result, Dot, ResIdx);
1223 }
1224 }
1225 return Result;
1226}
1227
1228// Expand llvm.matrix.transpose as a shufflevector that permutes elements
1229// from column-major source to column-major transposed layout.
1230// Element (r,c) at index c*Rows + r moves to index r*Cols + c.
1232 Value *Mat = Orig->getArgOperand(0);
1233 unsigned Rows = cast<ConstantInt>(Orig->getArgOperand(1))->getZExtValue();
1234 unsigned Cols = cast<ConstantInt>(Orig->getArgOperand(2))->getZExtValue();
1235
1236 unsigned NumElts = Rows * Cols;
1237 SmallVector<int, 16> Mask(NumElts);
1238 for (unsigned I = 0; I < NumElts; ++I)
1239 Mask[I] = (I % Cols) * Rows + (I / Cols);
1240
1241 IRBuilder<> Builder(Orig);
1242 return Builder.CreateShuffleVector(Mat, Mask);
1243}
1244
1245// Scalarize a vector int_dx_store_output call into per-component scalar calls.
1246// The DXIL StoreOutput op is per-component; vector intrinsics are split here
1247// so that DXILOpLowering sees only scalar variants.
1248static bool expandStoreOutput(CallInst *Orig) {
1249 auto *VT = dyn_cast<FixedVectorType>(Orig->getArgOperand(3)->getType());
1250 if (!VT)
1251 return false; // already scalar, nothing to expand
1252
1253 IRBuilder<> Builder(Orig);
1254 Module *M = Orig->getModule();
1255 Type *Int8Ty = Builder.getInt8Ty();
1256 Type *Int32Ty = Builder.getInt32Ty();
1257 Type *ScalarTy = VT->getElementType();
1258 unsigned NumElems = VT->getNumElements();
1259
1260 Value *SigElementId = Orig->getArgOperand(0);
1261 Value *RowIndex = Orig->getArgOperand(1);
1262 Value *StartCol = Orig->getArgOperand(2); // i8
1263 Value *Data = Orig->getArgOperand(3);
1264 Value *StartColI32 = Builder.CreateZExt(StartCol, Int32Ty);
1265
1267 M, Intrinsic::dx_store_output, {ScalarTy});
1268
1269 for (unsigned I = 0; I < NumElems; ++I) {
1270 Value *Scalar =
1271 Builder.CreateExtractElement(Data, ConstantInt::get(Int32Ty, I));
1272 Value *ColIdx =
1273 Builder.CreateAdd(StartColI32, ConstantInt::get(Int32Ty, I));
1274 Value *ColI8 = Builder.CreateTrunc(ColIdx, Int8Ty);
1275 Builder.CreateCall(ScalarFn, {SigElementId, RowIndex, ColI8, Scalar});
1276 }
1277
1278 Orig->eraseFromParent();
1279 return true;
1280}
1281
1282// Scalarize a vector int_dx_load_input call into per-component scalar calls
1283// and reassemble the vector. The DXIL LoadInput op is per-component.
1285 auto *VT = dyn_cast<FixedVectorType>(Orig->getType());
1286 if (!VT)
1287 return nullptr; // already scalar, nothing to expand
1288
1289 IRBuilder<> Builder(Orig);
1290 Module *M = Orig->getModule();
1291 Type *Int8Ty = Builder.getInt8Ty();
1292 Type *Int32Ty = Builder.getInt32Ty();
1293 Type *ScalarTy = VT->getElementType();
1294 unsigned NumElems = VT->getNumElements();
1295
1296 Value *SigElementId = Orig->getArgOperand(0);
1297 Value *RowIndex = Orig->getArgOperand(1);
1298 Value *StartCol = Orig->getArgOperand(2); // i8
1299 Value *GsVertexOrPrimIndex = Orig->getArgOperand(3);
1300 Value *StartColI32 = Builder.CreateZExt(StartCol, Int32Ty);
1301
1303 M, Intrinsic::dx_load_input, {ScalarTy});
1304
1305 Value *Vec = PoisonValue::get(VT);
1306 for (unsigned I = 0; I < NumElems; ++I) {
1307 Value *ColIdx =
1308 Builder.CreateAdd(StartColI32, ConstantInt::get(Int32Ty, I));
1309 Value *ColI8 = Builder.CreateTrunc(ColIdx, Int8Ty);
1310 Value *Scalar = Builder.CreateCall(
1311 ScalarFn, {SigElementId, RowIndex, ColI8, GsVertexOrPrimIndex});
1312 Vec =
1313 Builder.CreateInsertElement(Vec, Scalar, ConstantInt::get(Int32Ty, I));
1314 }
1315
1316 return Vec;
1317}
1318
1320 IRBuilder<> Builder(Orig);
1322 Orig->getOperandBundlesAsDefs(Bundles);
1323
1324 Value *Cond =
1325 Builder.CreateSExt(Orig->getArgOperand(0), Builder.getInt32Ty());
1326 Value *Result = Cond;
1327 for (Intrinsic::ID ID :
1328 {Intrinsic::dx_quad_read_across_x, Intrinsic::dx_quad_read_across_y,
1329 Intrinsic::dx_quad_read_across_diagonal}) {
1330 Value *QuadValue = Builder.CreateIntrinsic(ID, {Builder.getInt32Ty()}, Cond,
1331 nullptr, "", Bundles);
1332 Result = Builder.CreateAnd(Result, QuadValue);
1333 }
1334 return Builder.CreateTrunc(Result, Builder.getInt1Ty());
1335}
1336
1337static bool expandIntrinsic(Function &F, CallInst *Orig) {
1338 Value *Result = nullptr;
1339 Intrinsic::ID IntrinsicId = F.getIntrinsicID();
1340 switch (IntrinsicId) {
1341 case Intrinsic::abs:
1342 Result = expandAbs(Orig);
1343 break;
1344 case Intrinsic::assume:
1345 Orig->eraseFromParent();
1346 return true;
1347 case Intrinsic::atan2:
1348 Result = expandAtan2Intrinsic(Orig);
1349 break;
1350 case Intrinsic::copysign:
1351 Result = expandCopySignIntrinsic(Orig);
1352 break;
1353 case Intrinsic::fshl:
1354 Result = expandFunnelShiftIntrinsic<true>(Orig);
1355 break;
1356 case Intrinsic::fshr:
1357 Result = expandFunnelShiftIntrinsic<false>(Orig);
1358 break;
1359 case Intrinsic::exp:
1360 Result = expandExpIntrinsic(Orig);
1361 break;
1362 case Intrinsic::is_fpclass:
1363 Result = expandIsFPClass(Orig);
1364 break;
1365 case Intrinsic::log:
1366 Result = expandLogIntrinsic(Orig);
1367 break;
1368 case Intrinsic::log10:
1369 Result = expandLog10Intrinsic(Orig);
1370 break;
1371 case Intrinsic::pow:
1372 case Intrinsic::powi:
1373 Result = expandPowIntrinsic(Orig, IntrinsicId);
1374 break;
1375 case Intrinsic::vector_reduce_and:
1376 case Intrinsic::vector_reduce_or:
1377 Result = expandVecReduceAndOr(Orig, IntrinsicId);
1378 break;
1379 case Intrinsic::dx_uclamp:
1380 case Intrinsic::dx_sclamp:
1381 case Intrinsic::dx_nclamp:
1382 Result = expandClampIntrinsic(Orig, IntrinsicId);
1383 break;
1384 case Intrinsic::dx_isfinite:
1385 Result = expand16BitIsFinite(Orig);
1386 break;
1387 case Intrinsic::dx_isinf:
1388 Result = expand16BitIsInf(Orig);
1389 break;
1390 case Intrinsic::dx_isnan:
1391 Result = expand16BitIsNaN(Orig);
1392 break;
1393 case Intrinsic::dx_fdot:
1394 Result = expandFloatDotIntrinsic(Orig);
1395 break;
1396 case Intrinsic::dx_sdot:
1397 case Intrinsic::dx_udot:
1398 Result = expandIntegerDotIntrinsic(Orig, IntrinsicId);
1399 break;
1400 case Intrinsic::dx_sign:
1401 Result = expandSignIntrinsic(Orig);
1402 break;
1403 case Intrinsic::dx_quad_all:
1404 Result = expandQuadAll(Orig);
1405 break;
1406 case Intrinsic::dx_load_input:
1407 Result = expandLoadInput(Orig);
1408 break;
1409 case Intrinsic::dx_store_output:
1410 if (expandStoreOutput(Orig))
1411 return true;
1412 break;
1413 case Intrinsic::dx_resource_load_rawbuffer:
1414 if (expandBufferLoadIntrinsic(Orig, /*IsRaw*/ true))
1415 return true;
1416 break;
1417 case Intrinsic::dx_resource_store_rawbuffer:
1418 if (expandBufferStoreIntrinsic(Orig, /*IsRaw*/ true))
1419 return true;
1420 break;
1421 case Intrinsic::dx_resource_load_typedbuffer:
1422 if (expandBufferLoadIntrinsic(Orig, /*IsRaw*/ false))
1423 return true;
1424 break;
1425 case Intrinsic::dx_resource_store_typedbuffer:
1426 if (expandBufferStoreIntrinsic(Orig, /*IsRaw*/ false))
1427 return true;
1428 break;
1429 case Intrinsic::dx_resource_load_level:
1431 return true;
1432 break;
1433 case Intrinsic::dx_resource_store_texture:
1435 return true;
1436 break;
1437 case Intrinsic::usub_sat:
1438 Result = expandUsubSat(Orig);
1439 break;
1440 case Intrinsic::umul_with_overflow:
1441 case Intrinsic::smul_with_overflow:
1442 Result = expandMulWithOverflow(Orig, /*Signed=*/IntrinsicId ==
1443 Intrinsic::smul_with_overflow);
1444 break;
1445 case Intrinsic::vector_reduce_add:
1446 case Intrinsic::vector_reduce_fadd:
1447 Result = expandVecReduceAdd(Orig, IntrinsicId);
1448 break;
1449 case Intrinsic::matrix_multiply:
1450 Result = expandMatrixMultiply(Orig);
1451 break;
1452 case Intrinsic::matrix_transpose:
1453 Result = expandMatrixTranspose(Orig);
1454 break;
1455 }
1456 if (Result) {
1457 Orig->replaceAllUsesWith(Result);
1458 Orig->eraseFromParent();
1459 return true;
1460 }
1461 return false;
1462}
1463
1465 for (auto &F : make_early_inc_range(M.functions())) {
1466 if (!isIntrinsicExpansion(F))
1467 continue;
1468 bool IntrinsicExpanded = false;
1469 for (User *U : make_early_inc_range(F.users())) {
1470 auto *IntrinsicCall = dyn_cast<CallInst>(U);
1471 if (!IntrinsicCall)
1472 continue;
1473 IntrinsicExpanded = expandIntrinsic(F, IntrinsicCall);
1474 }
1475 if (F.user_empty() && IntrinsicExpanded)
1476 F.eraseFromParent();
1477 }
1478 return true;
1479}
1480
1487
1491
1493
1495 "DXIL Intrinsic Expansion", false, false)
1497 "DXIL Intrinsic Expansion", false, false)
1498
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
This file implements a class to represent arbitrary precision integral constant values and operations...
#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< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static Value * expand16BitIsNormal(CallInst *Orig)
static Value * createMulHighUnsigned(IRBuilder<> &Builder, Value *A, Value *B, Type *Ty, unsigned BW)
static bool expandIntrinsic(Function &F, CallInst *Orig)
static Value * expandClampIntrinsic(CallInst *Orig, Intrinsic::ID ClampIntrinsic)
static Value * expand16BitIsInf(CallInst *Orig)
static bool expandTextureStoreIntrinsic(CallInst *Orig)
Split a 64-bit llvm.dx.resource.store.texture into a store of lo/hi i32 pairs to the same texture,...
static bool expansionIntrinsics(Module &M)
static Value * expandCopySignIntrinsic(CallInst *Orig)
static Value * expand16BitIsFinite(CallInst *Orig)
static Value * expandLoadInput(CallInst *Orig)
static Value * expandUsubSat(CallInst *Orig)
static Value * expandFloatDotIntrinsic(CallInst *Orig)
static bool expandStoreOutput(CallInst *Orig)
static Value * expandMatrixTranspose(CallInst *Orig)
static Value * combineI32Pairs(IRBuilder<> &Builder, Value *I32Vec, Value *Result, unsigned Base)
Recombine the lo/hi i32 pairs in I32Vec into double or i64 values.
static Value * expandVecReduceAdd(CallInst *Orig, Intrinsic::ID IntrinsicId)
static Value * expandAtan2Intrinsic(CallInst *Orig)
static Value * expandQuadAll(CallInst *Orig)
static Value * expandLog10Intrinsic(CallInst *Orig)
static Intrinsic::ID getMinForClamp(Intrinsic::ID ClampIntrinsic)
static Value * expandIntegerDotIntrinsic(CallInst *Orig, Intrinsic::ID DotIntrinsic)
static Value * expandVecReduceAndOr(CallInst *Orig, Intrinsic::ID IntrinsicId)
static bool expandBufferStoreIntrinsic(CallInst *Orig, bool IsRaw)
static Value * expandLogIntrinsic(CallInst *Orig, float LogConstVal=numbers::ln2f)
static bool expandTextureLoadIntrinsic(CallInst *Orig)
Split a 64-bit llvm.dx.resource.load.level into a load of lo/hi i32 pairs on the same texture,...
static Value * expandMulWithOverflow(CallInst *Orig, bool Signed)
static Value * expandPowIntrinsic(CallInst *Orig, Intrinsic::ID IntrinsicId)
static Value * splitIntoI32Pairs(IRBuilder<> &Builder, Value *Val)
Split the double or i64 scalar or vector Val into a vector of i32 that holds the low and high halves ...
static bool resourceAccessNeeds64BitExpansion(Module *M, Type *OverloadTy, bool IsRaw)
static Value * expandExpIntrinsic(CallInst *Orig)
static Value * expand16BitIsNaN(CallInst *Orig)
static Value * expandSignIntrinsic(CallInst *Orig)
static Intrinsic::ID getMaxForClamp(Intrinsic::ID ClampIntrinsic)
static bool shouldExpandFloatDotIntrinsic(Function &F)
static Value * expandFloatDotChunk(CallInst *Orig, Value *A, Value *B)
static Value * expandAbs(CallInst *Orig)
static bool isIntrinsicExpansion(Function &F)
static bool expandBufferLoadIntrinsic(CallInst *Orig, bool IsRaw)
static Value * expandMatrixMultiply(CallInst *Orig)
static Value * expandIsFPClass(CallInst *Orig)
static Value * expandFunnelShiftIntrinsic(CallInst *Orig)
#define DEBUG_TYPE
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
SmallVector< MachineOperand, 4 > Cond
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define T
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
MachineInstr *& Lo
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")
Value * RHS
Value * LHS
BinaryOperator * Mul
bool runOnModule(Module &M) override
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
Class for arbitrary precision integers.
Definition APInt.h:78
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition APInt.h:225
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition APInt.h:302
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
LLVM_ABI void getOperandBundlesAsDefs(SmallVectorImpl< OperandBundleDef > &Defs) const
Return the list of operand bundles attached to this instruction as a vector of OperandBundleDefs.
void setAttributes(AttributeList A)
Set the attributes for this call.
Value * getArgOperand(unsigned i) const
FunctionType * getFunctionType() const
iterator_range< User::op_iterator > args()
Iteration adapter for range-for loops.
AttributeList getAttributes() const
Return the attributes for this call.
This class represents a function call, abstracting a target machine's calling convention.
bool isTailCall() const
void setTailCall(bool IsTc=true)
static LLVM_ABI Constant * getSplat(ElementCount EC, Constant *Elt)
Return a ConstantVector with the specified constant in each element.
This is an important base class in LLVM.
Definition Constant.h:44
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition Constant.h:65
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &)
static constexpr ElementCount getFixed(ScalarTy MinVal)
Definition TypeSize.h:306
static LLVM_ABI FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition Type.cpp:838
Type * getParamType(unsigned i) const
Parameter type accessors.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2990
LLVM_ABI const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
iterator_range< user_iterator > users()
LLVM_ABI FastMathFlags getFastMathFlags() const LLVM_READONLY
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition Pass.h:253
ModulePass(char &pid)
Definition Pass.h:255
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
const Triple & getTargetTriple() const
Get the target triple which is a string describing the target host.
Definition Module.h:328
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
void push_back(const T &Elt)
LLVM_NO_SANITIZE_CFI_UNRELATED_CAST iterator begin()
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
static LLVM_ABI StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
Definition Type.cpp:462
LLVM_ABI VersionTuple getOSVersion() const
Parse the version number from the OS name component of the triple, if present.
Definition Triple.cpp:1493
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM_ABI Type * getStructElementType(unsigned N) const
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:283
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:294
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:292
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:370
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:187
LLVM_ABI Type * getWithNewBitWidth(unsigned NewBitWidth) const
Given an integer or vector type, change the lane bitwidth to NewBitwidth, whilst keeping the old numb...
static LLVM_ABI IntegerType * getInt16Ty(LLVMContext &C)
Definition Type.cpp:293
bool isHalfTy() const
Return true if this is 'half', a 16-bit IEEE fp type.
Definition Type.h:144
LLVM_ABI Type * getWithNewType(Type *EltTy) const
Given vector type, change the element type, whilst keeping the old number of elements.
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
Definition Type.h:158
bool isFloatingPointTy() const
Return true if this is one of the floating-point types.
Definition Type.h:186
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:252
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition Type.cpp:298
Value * getOperand(unsigned i) const
Definition User.h:207
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 void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:548
static LLVM_ABI VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
Represents a version number in the form major[.minor[.subminor[.build]]].
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > OverloadTys={})
Look up the Function declaration of the intrinsic id in the Module M.
constexpr float ln10f
Definition MathExtras.h:51
constexpr float log2ef
Definition MathExtras.h:52
constexpr double pi
constexpr float ln2f
Definition MathExtras.h:50
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
@ Load
The value being inserted comes from a load (InsertElement only).
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:649
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:280
ModulePass * createDXILIntrinsicExpansionLegacyPass()
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
@ Sub
Subtraction of integers.
constexpr unsigned BitWidth
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:177