23 #define DEBUG_TYPE "scalarize-masked-mem-intrin"
32 explicit ScalarizeMaskedMemIntrin() :
FunctionPass(
ID), TTI(
nullptr) {
38 return "Scalarize Masked Memory Intrinsics";
46 bool optimizeBlock(
BasicBlock &BB,
bool &ModifiedDT);
47 bool optimizeCallInst(
CallInst *CI,
bool &ModifiedDT);
53 "Scalarize unsupported masked memory intrinsics",
false,
false)
56 return new ScalarizeMaskedMemIntrin();
100 unsigned AlignVal = cast<ConstantInt>(Alignment)->getZExtValue();
102 assert(VecType &&
"Unexpected return type of masked load intrinsic");
112 Builder.SetInsertPoint(InsertPt);
113 Builder.SetCurrentDebugLocation(CI->
getDebugLoc());
117 isa<Constant>(
Mask) && cast<Constant>(Mask)->isAllOnesValue();
120 Value *NewI = Builder.CreateAlignedLoad(Ptr, AlignVal);
131 Value *FirstEltPtr = Builder.CreateBitCast(Ptr, NewPtrType);
137 Value *VResult = UndefVal;
139 if (isa<ConstantVector>(Mask)) {
140 for (
unsigned Idx = 0; Idx < VectorWidth; ++Idx) {
141 if (cast<ConstantVector>(Mask)->getOperand(Idx)->isNullValue())
144 Builder.CreateInBoundsGEP(EltTy, FirstEltPtr, Builder.getInt32(Idx));
145 LoadInst *
Load = Builder.CreateAlignedLoad(Gep, AlignVal);
147 Builder.CreateInsertElement(VResult, Load, Builder.getInt32(Idx));
149 Value *NewI = Builder.CreateSelect(Mask, VResult, Src0);
156 Value *PrevPhi = UndefVal;
158 for (
unsigned Idx = 0; Idx < VectorWidth; ++Idx) {
168 Phi = Builder.CreatePHI(VecType, 2,
"res.phi.else");
176 Builder.CreateExtractElement(Mask, Builder.getInt32(Idx));
186 CondBlock = IfBlock->
splitBasicBlock(InsertPt->getIterator(),
"cond.load");
187 Builder.SetInsertPoint(InsertPt);
190 Builder.CreateInBoundsGEP(EltTy, FirstEltPtr, Builder.getInt32(Idx));
191 LoadInst *
Load = Builder.CreateAlignedLoad(Gep, AlignVal);
192 VResult = Builder.CreateInsertElement(VResult, Load, Builder.getInt32(Idx));
197 Builder.SetInsertPoint(InsertPt);
201 PrevIfBlock = IfBlock;
202 IfBlock = NewIfBlock;
205 Phi = Builder.CreatePHI(VecType, 2,
"res.phi.select");
208 Value *NewI = Builder.CreateSelect(Mask, Phi, Src0);
247 unsigned AlignVal = cast<ConstantInt>(Alignment)->getZExtValue();
249 assert(VecType &&
"Unexpected data type in masked store intrinsic");
256 Builder.SetInsertPoint(InsertPt);
257 Builder.SetCurrentDebugLocation(CI->
getDebugLoc());
261 isa<Constant>(
Mask) && cast<Constant>(Mask)->isAllOnesValue();
264 Builder.CreateAlignedStore(Src, Ptr, AlignVal);
274 Value *FirstEltPtr = Builder.CreateBitCast(Ptr, NewPtrType);
277 if (isa<ConstantVector>(Mask)) {
278 for (
unsigned Idx = 0; Idx < VectorWidth; ++Idx) {
279 if (cast<ConstantVector>(Mask)->getOperand(Idx)->isNullValue())
281 Value *OneElt = Builder.CreateExtractElement(Src, Builder.getInt32(Idx));
283 Builder.CreateInBoundsGEP(EltTy, FirstEltPtr, Builder.getInt32(Idx));
284 Builder.CreateAlignedStore(OneElt, Gep, AlignVal);
290 for (
unsigned Idx = 0; Idx < VectorWidth; ++Idx) {
299 Builder.CreateExtractElement(Mask, Builder.getInt32(Idx));
311 Builder.SetInsertPoint(InsertPt);
313 Value *OneElt = Builder.CreateExtractElement(Src, Builder.getInt32(Idx));
315 Builder.CreateInBoundsGEP(EltTy, FirstEltPtr, Builder.getInt32(Idx));
316 Builder.CreateAlignedStore(OneElt, Gep, AlignVal);
321 Builder.SetInsertPoint(InsertPt);
325 IfBlock = NewIfBlock;
369 assert(VecType &&
"Unexpected return type of masked load intrinsic");
376 Builder.SetInsertPoint(InsertPt);
377 unsigned AlignVal = cast<ConstantInt>(Alignment)->getZExtValue();
379 Builder.SetCurrentDebugLocation(CI->
getDebugLoc());
384 Value *VResult = UndefVal;
388 bool IsConstMask = isa<ConstantVector>(
Mask);
391 for (
unsigned Idx = 0; Idx < VectorWidth; ++Idx) {
392 if (cast<ConstantVector>(Mask)->getOperand(Idx)->isNullValue())
394 Value *
Ptr = Builder.CreateExtractElement(Ptrs, Builder.getInt32(Idx),
397 Builder.CreateAlignedLoad(Ptr, AlignVal,
"Load" +
Twine(Idx));
398 VResult = Builder.CreateInsertElement(
399 VResult, Load, Builder.getInt32(Idx),
"Res" +
Twine(Idx));
401 Value *NewI = Builder.CreateSelect(Mask, VResult, Src0);
408 Value *PrevPhi = UndefVal;
410 for (
unsigned Idx = 0; Idx < VectorWidth; ++Idx) {
419 Phi = Builder.CreatePHI(VecType, 2,
"res.phi.else");
426 Value *
Predicate = Builder.CreateExtractElement(Mask, Builder.getInt32(Idx),
427 "Mask" +
Twine(Idx));
430 "ToLoad" +
Twine(Idx));
439 Builder.SetInsertPoint(InsertPt);
441 Value *
Ptr = Builder.CreateExtractElement(Ptrs, Builder.getInt32(Idx),
444 Builder.CreateAlignedLoad(Ptr, AlignVal,
"Load" +
Twine(Idx));
445 VResult = Builder.CreateInsertElement(VResult, Load, Builder.getInt32(Idx),
450 Builder.SetInsertPoint(InsertPt);
454 PrevIfBlock = IfBlock;
455 IfBlock = NewIfBlock;
458 Phi = Builder.CreatePHI(VecType, 2,
"res.phi.select");
461 Value *NewI = Builder.CreateSelect(Mask, Phi, Src0);
501 "Unexpected data type in masked scatter intrinsic");
504 "Vector of pointers is expected in masked scatter intrinsic");
509 Builder.SetInsertPoint(InsertPt);
510 Builder.SetCurrentDebugLocation(CI->
getDebugLoc());
512 unsigned AlignVal = cast<ConstantInt>(Alignment)->getZExtValue();
516 bool IsConstMask = isa<ConstantVector>(
Mask);
519 for (
unsigned Idx = 0; Idx < VectorWidth; ++Idx) {
520 if (cast<ConstantVector>(Mask)->getOperand(Idx)->isNullValue())
522 Value *OneElt = Builder.CreateExtractElement(Src, Builder.getInt32(Idx),
524 Value *
Ptr = Builder.CreateExtractElement(Ptrs, Builder.getInt32(Idx),
526 Builder.CreateAlignedStore(OneElt, Ptr, AlignVal);
531 for (
unsigned Idx = 0; Idx < VectorWidth; ++Idx) {
538 Value *
Predicate = Builder.CreateExtractElement(Mask, Builder.getInt32(Idx),
539 "Mask" +
Twine(Idx));
542 "ToStore" +
Twine(Idx));
551 Builder.SetInsertPoint(InsertPt);
553 Value *OneElt = Builder.CreateExtractElement(Src, Builder.getInt32(Idx),
555 Value *
Ptr = Builder.CreateExtractElement(Ptrs, Builder.getInt32(Idx),
557 Builder.CreateAlignedStore(OneElt, Ptr, AlignVal);
561 Builder.SetInsertPoint(InsertPt);
565 IfBlock = NewIfBlock;
570 bool ScalarizeMaskedMemIntrin::runOnFunction(
Function &
F) {
574 bool EverMadeChange =
false;
576 TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
578 bool MadeChange =
true;
583 bool ModifiedDTOnIteration =
false;
584 MadeChange |= optimizeBlock(*BB, ModifiedDTOnIteration);
587 if (ModifiedDTOnIteration)
591 EverMadeChange |= MadeChange;
594 return EverMadeChange;
597 bool ScalarizeMaskedMemIntrin::optimizeBlock(
BasicBlock &BB,
bool &ModifiedDT) {
598 bool MadeChange =
false;
601 while (CurInstIterator != BB.
end()) {
602 if (
CallInst *CI = dyn_cast<CallInst>(&*CurInstIterator++))
603 MadeChange |= optimizeCallInst(CI, ModifiedDT);
611 bool ScalarizeMaskedMemIntrin::optimizeCallInst(
CallInst *CI,
619 case Intrinsic::masked_load: {
621 if (!TTI->isLegalMaskedLoad(CI->
getType())) {
628 case Intrinsic::masked_store: {
636 case Intrinsic::masked_gather: {
637 if (!TTI->isLegalMaskedGather(CI->
getType())) {
644 case Intrinsic::masked_scatter: {
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)
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Maximum length of the test input libFuzzer tries to guess a good value based on the corpus and reports it always prefer smaller inputs during the corpus shuffle When libFuzzer itself reports a bug this exit code will be used If indicates the maximal total time in seconds to run the fuzzer minimizes the provided crash input Use with tries to cleanse the provided crash input to make it contain fewer original bytes Use with exact_artifact_path to specify the output Experimental Use value profile to guide fuzzing Number of jobs to run If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
This class represents a function call, abstracting a target machine's calling convention.
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...
static void scalarizeMaskedGather(CallInst *CI)
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Type * getVectorElementType() const
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Type * getElementType() const
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Represent the analysis usage information of a pass.
uint64_t getNumElements() const
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.
LLVMContext & getContext() const
All values hold a context through their type.
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...
Type * getType() const
All values are typed, get the type of this value.
Predicate
Predicate - These are "(BI << 5) | BO" for various predicates.
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)
Value * getArgOperand(unsigned i) const
getArgOperand/setArgOperand - Return/set the i-th call argument.
Class to represent vector types.
unsigned getVectorNumElements() const
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())
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.
PointerType * getPointerTo(unsigned AddrSpace=0) const
Return a pointer to the current type.
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)
const BasicBlock * getParent() const
A wrapper class for inspecting calls to intrinsic functions.