[llvm] Replace mask{Is,Contains}All{Zero,One}OrUndef with pattern matching on poison (PR #200040)
via llvm-commits
llvm-commits at lists.llvm.org
Wed May 27 12:56:47 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-analysis
Author: Sean Clarke (xarkenz)
<details>
<summary>Changes</summary>
As part of the ongoing effort to deprecate UndefValue, and also to take better advantage of pattern matching, eliminate the VectorUtils functions `maskIsAllZeroOrUndef`, `maskIsAllOneOrUndef`, and `maskContainsAllOneOrUndef`; and replace their uses with equivalent pattern matching, but matching poison instead of undef.
This affects InstCombine/InstSimplify with a mask containing `undef` for the intrinsics `llvm.masked.load`, `llvm.masked.store`, `llvm.masked.gather`, and `llvm.masked.scatter`.
The lone use of `maskContainsAllOneOrUndef` (in `InstCombinerImpl::simplifyMaskedScatter`) was simply removed. There is a prior match for the mask being all zero or poison, the negation of which I think would be sufficient to imply that at least one lane of the mask is active.
Some relevant tests were altered to use poison instead of undef. I may need some guidance on whether the updated test checks in Transforms/InstCombine/pr83947.ll are correct, since they deviate from previous behavior.
---
Full diff: https://github.com/llvm/llvm-project/pull/200040.diff
8 Files Affected:
- (modified) llvm/include/llvm/Analysis/VectorUtils.h (-15)
- (modified) llvm/lib/Analysis/InstructionSimplify.cpp (+3-2)
- (modified) llvm/lib/Analysis/VectorUtils.cpp (-77)
- (modified) llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp (+15-15)
- (modified) llvm/test/Transforms/InstCombine/masked_intrinsics-inseltpoison.ll (-9)
- (modified) llvm/test/Transforms/InstCombine/masked_intrinsics.ll (+6-6)
- (modified) llvm/test/Transforms/InstCombine/pr83947.ll (+4-4)
- (modified) llvm/test/Transforms/InstSimplify/call.ll (+3-3)
``````````diff
diff --git a/llvm/include/llvm/Analysis/VectorUtils.h b/llvm/include/llvm/Analysis/VectorUtils.h
index 3fd4de926683c..d640f21304d5b 100644
--- a/llvm/include/llvm/Analysis/VectorUtils.h
+++ b/llvm/include/llvm/Analysis/VectorUtils.h
@@ -477,21 +477,6 @@ LLVM_ABI llvm::SmallVector<int, 16> createUnaryMask(ArrayRef<int> Mask,
LLVM_ABI Value *concatenateVectors(IRBuilderBase &Builder,
ArrayRef<Value *> Vecs);
-/// Given a mask vector of i1, Return true if all of the elements of this
-/// predicate mask are known to be false or undef. That is, return true if all
-/// lanes can be assumed inactive.
-LLVM_ABI bool maskIsAllZeroOrUndef(Value *Mask);
-
-/// Given a mask vector of i1, Return true if all of the elements of this
-/// predicate mask are known to be true or undef. That is, return true if all
-/// lanes can be assumed active.
-LLVM_ABI bool maskIsAllOneOrUndef(Value *Mask);
-
-/// Given a mask vector of i1, Return true if any of the elements of this
-/// predicate mask are known to be true or undef. That is, return true if at
-/// least one lane can be assumed active.
-LLVM_ABI bool maskContainsAllOneOrUndef(Value *Mask);
-
/// Given a mask vector of the form <Y x i1>, return an APInt (of bitwidth Y)
/// for each lane which may be active.
LLVM_ABI APInt possiblyDemandedEltsInMask(Value *Mask);
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index dbbb5f2eee6a1..406997157c5a7 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -7266,8 +7266,9 @@ static Value *simplifyIntrinsic(CallBase *Call, Value *Callee,
case Intrinsic::masked_gather: {
Value *MaskArg = Args[1];
Value *PassthruArg = Args[2];
- // If the mask is all zeros or undef, the "passthru" argument is the result.
- if (maskIsAllZeroOrUndef(MaskArg))
+ // If the mask is all zeros or poisons, the "passthru" argument is the
+ // result.
+ if (match(MaskArg, m_Zero()) || match(MaskArg, m_Poison()))
return PassthruArg;
return nullptr;
}
diff --git a/llvm/lib/Analysis/VectorUtils.cpp b/llvm/lib/Analysis/VectorUtils.cpp
index 402bef65c2f13..e6e49f0b7bfb2 100644
--- a/llvm/lib/Analysis/VectorUtils.cpp
+++ b/llvm/lib/Analysis/VectorUtils.cpp
@@ -1256,83 +1256,6 @@ Value *llvm::concatenateVectors(IRBuilderBase &Builder,
return ResList[0];
}
-bool llvm::maskIsAllZeroOrUndef(Value *Mask) {
- assert(isa<VectorType>(Mask->getType()) &&
- isa<IntegerType>(Mask->getType()->getScalarType()) &&
- cast<IntegerType>(Mask->getType()->getScalarType())->getBitWidth() ==
- 1 &&
- "Mask must be a vector of i1");
-
- auto *ConstMask = dyn_cast<Constant>(Mask);
- if (!ConstMask)
- return false;
- if (ConstMask->isNullValue() || isa<UndefValue>(ConstMask))
- return true;
- if (isa<ScalableVectorType>(ConstMask->getType()))
- return false;
- for (unsigned
- I = 0,
- E = cast<FixedVectorType>(ConstMask->getType())->getNumElements();
- I != E; ++I) {
- if (auto *MaskElt = ConstMask->getAggregateElement(I))
- if (MaskElt->isNullValue() || isa<UndefValue>(MaskElt))
- continue;
- return false;
- }
- return true;
-}
-
-bool llvm::maskIsAllOneOrUndef(Value *Mask) {
- assert(isa<VectorType>(Mask->getType()) &&
- isa<IntegerType>(Mask->getType()->getScalarType()) &&
- cast<IntegerType>(Mask->getType()->getScalarType())->getBitWidth() ==
- 1 &&
- "Mask must be a vector of i1");
-
- auto *ConstMask = dyn_cast<Constant>(Mask);
- if (!ConstMask)
- return false;
- if (ConstMask->isAllOnesValue() || isa<UndefValue>(ConstMask))
- return true;
- if (isa<ScalableVectorType>(ConstMask->getType()))
- return false;
- for (unsigned
- I = 0,
- E = cast<FixedVectorType>(ConstMask->getType())->getNumElements();
- I != E; ++I) {
- if (auto *MaskElt = ConstMask->getAggregateElement(I))
- if (MaskElt->isAllOnesValue() || isa<UndefValue>(MaskElt))
- continue;
- return false;
- }
- return true;
-}
-
-bool llvm::maskContainsAllOneOrUndef(Value *Mask) {
- assert(isa<VectorType>(Mask->getType()) &&
- isa<IntegerType>(Mask->getType()->getScalarType()) &&
- cast<IntegerType>(Mask->getType()->getScalarType())->getBitWidth() ==
- 1 &&
- "Mask must be a vector of i1");
-
- auto *ConstMask = dyn_cast<Constant>(Mask);
- if (!ConstMask)
- return false;
- if (ConstMask->isAllOnesValue() || isa<UndefValue>(ConstMask))
- return true;
- if (isa<ScalableVectorType>(ConstMask->getType()))
- return false;
- for (unsigned
- I = 0,
- E = cast<FixedVectorType>(ConstMask->getType())->getNumElements();
- I != E; ++I) {
- if (auto *MaskElt = ConstMask->getAggregateElement(I))
- if (MaskElt->isAllOnesValue() || isa<UndefValue>(MaskElt))
- return true;
- }
- return false;
-}
-
/// TODO: This is a lot like known bits, but for
/// vectors. Is there something we can common this with?
APInt llvm::possiblyDemandedEltsInMask(Value *Mask) {
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index 8d1984d4e1463..0167fc653d4e0 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -292,10 +292,11 @@ Instruction *InstCombinerImpl::SimplifyAnyMemSet(AnyMemSetInst *MI) {
Value *InstCombinerImpl::simplifyMaskedLoad(IntrinsicInst &II) {
Value *LoadPtr = II.getArgOperand(0);
const Align Alignment = II.getParamAlign(0).valueOrOne();
+ Value *Mask = II.getArgOperand(1);
- // If the mask is all ones or undefs, this is a plain vector load of the 1st
+ // If the mask is all ones or poisons, this is a plain vector load of the 1st
// argument.
- if (maskIsAllOneOrUndef(II.getArgOperand(1))) {
+ if (match(Mask, m_AllOnes()) || match(Mask, m_Poison())) {
LoadInst *L = Builder.CreateAlignedLoad(II.getType(), LoadPtr, Alignment,
"unmaskedload");
L->copyMetadata(II);
@@ -325,12 +326,13 @@ Instruction *InstCombinerImpl::simplifyMaskedStore(IntrinsicInst &II) {
if (!ConstMask)
return nullptr;
- // If the mask is all zeros, this instruction does nothing.
- if (maskIsAllZeroOrUndef(ConstMask))
+ // If the mask is all zeros or poisons, this instruction does nothing.
+ if (match(ConstMask, m_Zero()) || match(ConstMask, m_Poison()))
return eraseInstFromFunction(II);
- // If the mask is all ones, this is a plain vector store of the 1st argument.
- if (maskIsAllOneOrUndef(ConstMask)) {
+ // If the mask is all ones or poisons, this is a plain vector store of the 1st
+ // argument.
+ if (match(ConstMask, m_AllOnes())) {
StoreInst *S =
new StoreInst(II.getArgOperand(0), StorePtr, false, Alignment);
S->copyMetadata(II);
@@ -388,21 +390,19 @@ Instruction *InstCombinerImpl::simplifyMaskedScatter(IntrinsicInst &II) {
if (!ConstMask)
return nullptr;
- // If the mask is all zeros, a scatter does nothing.
- if (maskIsAllZeroOrUndef(ConstMask))
+ // If the mask is all zeros or poisons, a scatter does nothing.
+ if (match(ConstMask, m_Zero()) || match(ConstMask, m_Poison()))
return eraseInstFromFunction(II);
// Vector splat address -> scalar store
if (auto *SplatPtr = getSplatValue(II.getArgOperand(1))) {
// scatter(splat(value), splat(ptr), non-zero-mask) -> store value, ptr
if (auto *SplatValue = getSplatValue(II.getArgOperand(0))) {
- if (maskContainsAllOneOrUndef(ConstMask)) {
- Align Alignment = II.getParamAlign(1).valueOrOne();
- StoreInst *S = new StoreInst(SplatValue, SplatPtr, /*IsVolatile=*/false,
- Alignment);
- S->copyMetadata(II);
- return S;
- }
+ Align Alignment = II.getParamAlign(1).valueOrOne();
+ StoreInst *S =
+ new StoreInst(SplatValue, SplatPtr, /*IsVolatile=*/false, Alignment);
+ S->copyMetadata(II);
+ return S;
}
// scatter(vector, splat(ptr), splat(true)) -> store extract(vector,
// lastlane), ptr
diff --git a/llvm/test/Transforms/InstCombine/masked_intrinsics-inseltpoison.ll b/llvm/test/Transforms/InstCombine/masked_intrinsics-inseltpoison.ll
index 10a80cc89dbb9..03ed3740ca3da 100644
--- a/llvm/test/Transforms/InstCombine/masked_intrinsics-inseltpoison.ll
+++ b/llvm/test/Transforms/InstCombine/masked_intrinsics-inseltpoison.ll
@@ -24,15 +24,6 @@ define <2 x double> @load_onemask(ptr %ptr, <2 x double> %passthru) {
ret <2 x double> %res
}
-define <2 x double> @load_undefmask(ptr %ptr, <2 x double> %passthru) {
-; CHECK-LABEL: @load_undefmask(
-; CHECK-NEXT: [[UNMASKEDLOAD:%.*]] = load <2 x double>, ptr [[PTR:%.*]], align 2
-; CHECK-NEXT: ret <2 x double> [[UNMASKEDLOAD]]
-;
- %res = call <2 x double> @llvm.masked.load.v2f64.p0(ptr %ptr, i32 2, <2 x i1> <i1 1, i1 undef>, <2 x double> %passthru)
- ret <2 x double> %res
-}
-
@G = external global i8
define <2 x double> @load_cemask(ptr %ptr, <2 x double> %passthru) {
diff --git a/llvm/test/Transforms/InstCombine/masked_intrinsics.ll b/llvm/test/Transforms/InstCombine/masked_intrinsics.ll
index 625c979cf794d..22ef1f6cdbf3f 100644
--- a/llvm/test/Transforms/InstCombine/masked_intrinsics.ll
+++ b/llvm/test/Transforms/InstCombine/masked_intrinsics.ll
@@ -16,11 +16,11 @@ define <2 x double> @load_zeromask(ptr %ptr, <2 x double> %passthru) {
ret <2 x double> %res
}
-define <2 x double> @load_zero_withpoison_mask(ptr %ptr, <2 x double> %passthru) {
-; CHECK-LABEL: @load_zero_withpoison_mask(
+define <2 x double> @load_poisonmask(ptr %ptr, <2 x double> %passthru) {
+; CHECK-LABEL: @load_poisonmask(
; CHECK-NEXT: ret <2 x double> [[PASSTHRU:%.*]]
;
- %res = call <2 x double> @llvm.masked.load.v2f64.p0(ptr %ptr, i32 1, <2 x i1> <i1 0, i1 poison>, <2 x double> %passthru)
+ %res = call <2 x double> @llvm.masked.load.v2f64.p0(ptr %ptr, i32 1, <2 x i1> splat(i1 poison), <2 x double> %passthru)
ret <2 x double> %res
}
@@ -33,12 +33,12 @@ define <2 x double> @load_onemask(ptr %ptr, <2 x double> %passthru) {
ret <2 x double> %res
}
-define <2 x double> @load_undefmask(ptr %ptr, <2 x double> %passthru) {
-; CHECK-LABEL: @load_undefmask(
+define <2 x double> @load_one_withpoison_mask(ptr %ptr, <2 x double> %passthru) {
+; CHECK-LABEL: @load_one_withpoison_mask(
; CHECK-NEXT: [[UNMASKEDLOAD:%.*]] = load <2 x double>, ptr [[PTR:%.*]], align 2
; CHECK-NEXT: ret <2 x double> [[UNMASKEDLOAD]]
;
- %res = call <2 x double> @llvm.masked.load.v2f64.p0(ptr %ptr, i32 2, <2 x i1> <i1 1, i1 undef>, <2 x double> %passthru)
+ %res = call <2 x double> @llvm.masked.load.v2f64.p0(ptr %ptr, i32 2, <2 x i1> <i1 1, i1 poison>, <2 x double> %passthru)
ret <2 x double> %res
}
diff --git a/llvm/test/Transforms/InstCombine/pr83947.ll b/llvm/test/Transforms/InstCombine/pr83947.ll
index 18468d10ba6fe..36893b733ce5e 100644
--- a/llvm/test/Transforms/InstCombine/pr83947.ll
+++ b/llvm/test/Transforms/InstCombine/pr83947.ll
@@ -6,7 +6,7 @@
define void @masked_scatter1() {
; CHECK-LABEL: define void @masked_scatter1() {
-; CHECK-NEXT: call void @llvm.masked.scatter.nxv4i32.nxv4p0(<vscale x 4 x i32> zeroinitializer, <vscale x 4 x ptr> align 4 shufflevector (<vscale x 4 x ptr> insertelement (<vscale x 4 x ptr> poison, ptr @c, i64 0), <vscale x 4 x ptr> poison, <vscale x 4 x i32> zeroinitializer), <vscale x 4 x i1> shufflevector (<vscale x 4 x i1> insertelement (<vscale x 4 x i1> poison, i1 ptrtoint (ptr @b to i1), i64 0), <vscale x 4 x i1> poison, <vscale x 4 x i32> zeroinitializer))
+; CHECK-NEXT: store i32 0, ptr @c, align 4
; CHECK-NEXT: ret void
;
call void @llvm.masked.scatter.nxv4i32.nxv4p0(<vscale x 4 x i32> zeroinitializer, <vscale x 4 x ptr> splat (ptr @c), i32 4, <vscale x 4 x i1> splat (i1 ptrtoint (ptr @b to i1)))
@@ -26,7 +26,7 @@ define void @masked_scatter3() {
; CHECK-LABEL: define void @masked_scatter3() {
; CHECK-NEXT: ret void
;
- call void @llvm.masked.scatter.v2i32.v2p0(<2 x i32> zeroinitializer, <2 x ptr> splat (ptr @c), i32 4, <2 x i1> undef)
+ call void @llvm.masked.scatter.v2i32.v2p0(<2 x i32> zeroinitializer, <2 x ptr> splat (ptr @c), i32 4, <2 x i1> poison)
ret void
}
@@ -51,13 +51,13 @@ define void @masked_scatter6() {
; CHECK-LABEL: define void @masked_scatter6() {
; CHECK-NEXT: ret void
;
- call void @llvm.masked.scatter.v2i32.v2p0(<2 x i32> zeroinitializer, <2 x ptr> splat (ptr @c), i32 4, <2 x i1> <i1 undef, i1 false>)
+ call void @llvm.masked.scatter.v2i32.v2p0(<2 x i32> zeroinitializer, <2 x ptr> splat (ptr @c), i32 4, <2 x i1> <i1 poison, i1 false>)
ret void
}
define void @masked_scatter7() {
; CHECK-LABEL: define void @masked_scatter7() {
-; CHECK-NEXT: call void @llvm.masked.scatter.v2i32.v2p0(<2 x i32> zeroinitializer, <2 x ptr> align 4 <ptr @c, ptr @c>, <2 x i1> <i1 ptrtoint (ptr @b to i1), i1 ptrtoint (ptr @b to i1)>)
+; CHECK-NEXT: store i32 0, ptr @c, align 4
; CHECK-NEXT: ret void
;
call void @llvm.masked.scatter.v2i32.v2p0(<2 x i32> zeroinitializer, <2 x ptr> splat (ptr @c), i32 4, <2 x i1> splat (i1 ptrtoint (ptr @b to i1)))
diff --git a/llvm/test/Transforms/InstSimplify/call.ll b/llvm/test/Transforms/InstSimplify/call.ll
index 10ccb082d9b6d..300c27c98ef99 100644
--- a/llvm/test/Transforms/InstSimplify/call.ll
+++ b/llvm/test/Transforms/InstSimplify/call.ll
@@ -488,11 +488,11 @@ define <8 x i32> @partial_masked_load() {
ret <8 x i32> %masked.load
}
-define <8 x i32> @masked_load_undef_mask(ptr %V) {
-; CHECK-LABEL: @masked_load_undef_mask(
+define <8 x i32> @masked_load_poison_mask(ptr %V) {
+; CHECK-LABEL: @masked_load_poison_mask(
; CHECK-NEXT: ret <8 x i32> <i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0>
;
- %masked.load = call <8 x i32> @llvm.masked.load.v8i32.p0(ptr %V, i32 4, <8 x i1> undef, <8 x i32> <i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0>)
+ %masked.load = call <8 x i32> @llvm.masked.load.v8i32.p0(ptr %V, i32 4, <8 x i1> poison, <8 x i32> <i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0>)
ret <8 x i32> %masked.load
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/200040
More information about the llvm-commits
mailing list