40 #define DEBUG_TYPE "scalarize-masked-mem-intrin" 57 return "Scalarize Masked Memory Intrinsics";
65 bool optimizeBlock(
BasicBlock &BB,
bool &ModifiedDT);
66 bool optimizeCallInst(
CallInst *CI,
bool &ModifiedDT);
74 "Scalarize unsupported masked memory intrinsics",
false,
false)
77 return new ScalarizeMaskedMemIntrin();
121 unsigned AlignVal = cast<ConstantInt>(Alignment)->getZExtValue();
123 assert(VecType &&
"Unexpected return type of masked load intrinsic");
133 Builder.SetInsertPoint(InsertPt);
134 Builder.SetCurrentDebugLocation(CI->
getDebugLoc());
138 isa<Constant>(
Mask) && cast<Constant>(Mask)->isAllOnesValue();
141 Value *NewI = Builder.CreateAlignedLoad(Ptr, AlignVal);
152 Value *FirstEltPtr = Builder.CreateBitCast(Ptr, NewPtrType);
158 Value *VResult = UndefVal;
160 if (isa<ConstantVector>(Mask)) {
161 for (
unsigned Idx = 0; Idx < VectorWidth; ++Idx) {
162 if (cast<ConstantVector>(Mask)->getOperand(Idx)->isNullValue())
165 Builder.CreateInBoundsGEP(EltTy, FirstEltPtr, Builder.getInt32(Idx));
166 LoadInst *
Load = Builder.CreateAlignedLoad(Gep, AlignVal);
168 Builder.CreateInsertElement(VResult, Load, Builder.getInt32(Idx));
170 Value *NewI = Builder.CreateSelect(Mask, VResult, Src0);
177 Value *PrevPhi = UndefVal;
179 for (
unsigned Idx = 0; Idx < VectorWidth; ++Idx) {
188 Phi = Builder.CreatePHI(VecType, 2,
"res.phi.else");
196 Builder.CreateExtractElement(Mask, Builder.getInt32(Idx));
206 CondBlock = IfBlock->
splitBasicBlock(InsertPt->getIterator(),
"cond.load");
207 Builder.SetInsertPoint(InsertPt);
210 Builder.CreateInBoundsGEP(EltTy, FirstEltPtr, Builder.getInt32(Idx));
211 LoadInst *
Load = Builder.CreateAlignedLoad(Gep, AlignVal);
212 VResult = Builder.CreateInsertElement(VResult, Load, Builder.getInt32(Idx));
217 Builder.SetInsertPoint(InsertPt);
221 PrevIfBlock = IfBlock;
222 IfBlock = NewIfBlock;
225 Phi = Builder.CreatePHI(VecType, 2,
"res.phi.select");
228 Value *NewI = Builder.CreateSelect(Mask, Phi, Src0);
267 unsigned AlignVal = cast<ConstantInt>(Alignment)->getZExtValue();
269 assert(VecType &&
"Unexpected data type in masked store intrinsic");
276 Builder.SetInsertPoint(InsertPt);
277 Builder.SetCurrentDebugLocation(CI->
getDebugLoc());
281 isa<Constant>(
Mask) && cast<Constant>(Mask)->isAllOnesValue();
284 Builder.CreateAlignedStore(Src, Ptr, AlignVal);
294 Value *FirstEltPtr = Builder.CreateBitCast(Ptr, NewPtrType);
297 if (isa<ConstantVector>(Mask)) {
298 for (
unsigned Idx = 0; Idx < VectorWidth; ++Idx) {
299 if (cast<ConstantVector>(Mask)->getOperand(Idx)->isNullValue())
301 Value *OneElt = Builder.CreateExtractElement(Src, Builder.getInt32(Idx));
303 Builder.CreateInBoundsGEP(EltTy, FirstEltPtr, Builder.getInt32(Idx));
304 Builder.CreateAlignedStore(OneElt, Gep, AlignVal);
310 for (
unsigned Idx = 0; Idx < VectorWidth; ++Idx) {
318 Builder.CreateExtractElement(Mask, Builder.getInt32(Idx));
330 Builder.SetInsertPoint(InsertPt);
332 Value *OneElt = Builder.CreateExtractElement(Src, Builder.getInt32(Idx));
334 Builder.CreateInBoundsGEP(EltTy, FirstEltPtr, Builder.getInt32(Idx));
335 Builder.CreateAlignedStore(OneElt, Gep, AlignVal);
340 Builder.SetInsertPoint(InsertPt);
344 IfBlock = NewIfBlock;
388 assert(VecType &&
"Unexpected return type of masked load intrinsic");
395 Builder.SetInsertPoint(InsertPt);
396 unsigned AlignVal = cast<ConstantInt>(Alignment)->getZExtValue();
398 Builder.SetCurrentDebugLocation(CI->
getDebugLoc());
403 Value *VResult = UndefVal;
407 bool IsConstMask = isa<ConstantVector>(
Mask);
410 for (
unsigned Idx = 0; Idx < VectorWidth; ++Idx) {
411 if (cast<ConstantVector>(Mask)->getOperand(Idx)->isNullValue())
413 Value *
Ptr = Builder.CreateExtractElement(Ptrs, Builder.getInt32(Idx),
416 Builder.CreateAlignedLoad(Ptr, AlignVal,
"Load" +
Twine(Idx));
417 VResult = Builder.CreateInsertElement(
418 VResult, Load, Builder.getInt32(Idx),
"Res" +
Twine(Idx));
420 Value *NewI = Builder.CreateSelect(Mask, VResult, Src0);
427 Value *PrevPhi = UndefVal;
429 for (
unsigned Idx = 0; Idx < VectorWidth; ++Idx) {
437 Phi = Builder.CreatePHI(VecType, 2,
"res.phi.else");
444 Value *
Predicate = Builder.CreateExtractElement(Mask, Builder.getInt32(Idx),
445 "Mask" +
Twine(Idx));
448 "ToLoad" +
Twine(Idx));
457 Builder.SetInsertPoint(InsertPt);
459 Value *
Ptr = Builder.CreateExtractElement(Ptrs, Builder.getInt32(Idx),
462 Builder.CreateAlignedLoad(Ptr, AlignVal,
"Load" +
Twine(Idx));
463 VResult = Builder.CreateInsertElement(VResult, Load, Builder.getInt32(Idx),
468 Builder.SetInsertPoint(InsertPt);
472 PrevIfBlock = IfBlock;
473 IfBlock = NewIfBlock;
476 Phi = Builder.CreatePHI(VecType, 2,
"res.phi.select");
479 Value *NewI = Builder.CreateSelect(Mask, Phi, Src0);
519 "Unexpected data type in masked scatter intrinsic");
522 "Vector of pointers is expected in masked scatter intrinsic");
527 Builder.SetInsertPoint(InsertPt);
528 Builder.SetCurrentDebugLocation(CI->
getDebugLoc());
530 unsigned AlignVal = cast<ConstantInt>(Alignment)->getZExtValue();
534 bool IsConstMask = isa<ConstantVector>(
Mask);
537 for (
unsigned Idx = 0; Idx < VectorWidth; ++Idx) {
538 if (cast<ConstantVector>(Mask)->getOperand(Idx)->isNullValue())
540 Value *OneElt = Builder.CreateExtractElement(Src, Builder.getInt32(Idx),
542 Value *
Ptr = Builder.CreateExtractElement(Ptrs, Builder.getInt32(Idx),
544 Builder.CreateAlignedStore(OneElt, Ptr, AlignVal);
549 for (
unsigned Idx = 0; Idx < VectorWidth; ++Idx) {
556 Value *
Predicate = Builder.CreateExtractElement(Mask, Builder.getInt32(Idx),
557 "Mask" +
Twine(Idx));
560 "ToStore" +
Twine(Idx));
569 Builder.SetInsertPoint(InsertPt);
571 Value *OneElt = Builder.CreateExtractElement(Src, Builder.getInt32(Idx),
573 Value *
Ptr = Builder.CreateExtractElement(Ptrs, Builder.getInt32(Idx),
575 Builder.CreateAlignedStore(OneElt, Ptr, AlignVal);
579 Builder.SetInsertPoint(InsertPt);
583 IfBlock = NewIfBlock;
589 bool EverMadeChange =
false;
591 TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
593 bool MadeChange =
true;
598 bool ModifiedDTOnIteration =
false;
599 MadeChange |= optimizeBlock(*BB, ModifiedDTOnIteration);
602 if (ModifiedDTOnIteration)
606 EverMadeChange |= MadeChange;
609 return EverMadeChange;
612 bool ScalarizeMaskedMemIntrin::optimizeBlock(
BasicBlock &BB,
bool &ModifiedDT) {
613 bool MadeChange =
false;
616 while (CurInstIterator != BB.
end()) {
617 if (
CallInst *CI = dyn_cast<CallInst>(&*CurInstIterator++))
618 MadeChange |= optimizeCallInst(CI, ModifiedDT);
626 bool ScalarizeMaskedMemIntrin::optimizeCallInst(
CallInst *CI,
633 case Intrinsic::masked_load:
635 if (!TTI->isLegalMaskedLoad(CI->
getType())) {
641 case Intrinsic::masked_store:
648 case Intrinsic::masked_gather:
649 if (!TTI->isLegalMaskedGather(CI->
getType())) {
655 case Intrinsic::masked_scatter:
Type * getVectorElementType() const
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
static void scalarizeMaskedScatter(CallInst *CI)
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
Compute iterated dominance frontiers using a linear time algorithm.
This class represents a function call, abstracting a target machine's calling convention.
LLVMContext & getContext() const
All values hold a context through their type.
An instruction for reading from memory.
void initializeScalarizeMaskedMemIntrinPass(PassRegistry &)
iterator begin()
Instruction iterator methods.
FunctionPass * createScalarizeMaskedMemIntrinPass()
createScalarizeMaskedMemIntrinPass - Replace masked load, store, gather and scatter intrinsics with s...
AnalysisUsage & addRequired()
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
PointerType * getPointerTo(unsigned AddrSpace=0) const
Return a pointer to the current type.
static void scalarizeMaskedGather(CallInst *CI)
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
uint64_t getNumElements() const
Type * getType() const
All values are typed, get the type of this value.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
static bool runOnFunction(Function &F, bool PostInlining)
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Iterator for intrusive lists based on ilist_node.
INITIALIZE_PASS(ScalarizeMaskedMemIntrin, DEBUG_TYPE, "Scalarize unsupported masked memory intrinsics", false, false) FunctionPass *llvm
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type...
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
unsigned getVectorNumElements() const
Class to represent vector types.
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
static void scalarizeMaskedLoad(CallInst *CI)
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="")
Split the basic block into two basic blocks at the specified instruction.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Value * getArgOperand(unsigned i) const
getArgOperand/setArgOperand - Return/set the i-th call argument.
LLVM Value Representation.
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Type * getElementType() const
StringRef - Represent a constant reference to a string, i.e.
const TerminatorInst * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
static void scalarizeMaskedStore(CallInst *CI)
A wrapper class for inspecting calls to intrinsic functions.
const BasicBlock * getParent() const