Skip to content

Conversation

@artagnon
Copy link
Contributor

Use PatternMatch to improve code.

@llvmbot llvmbot added the llvm:analysis Includes value tracking, cost tables and constant folding label Nov 17, 2025
@llvmbot
Copy link
Member

llvmbot commented Nov 17, 2025

@llvm/pr-subscribers-llvm-analysis

Author: Ramkumar Ramachandra (artagnon)

Changes

Use PatternMatch to improve code.


Full diff: https://github.com/llvm/llvm-project/pull/168388.diff

1 Files Affected:

  • (modified) llvm/lib/Analysis/VectorUtils.cpp (+6-6)
diff --git a/llvm/lib/Analysis/VectorUtils.cpp b/llvm/lib/Analysis/VectorUtils.cpp
index 530fa9518f40e..b011dcafa1aec 100644
--- a/llvm/lib/Analysis/VectorUtils.cpp
+++ b/llvm/lib/Analysis/VectorUtils.cpp
@@ -1263,7 +1263,7 @@ bool llvm::maskIsAllZeroOrUndef(Value *Mask) {
   auto *ConstMask = dyn_cast<Constant>(Mask);
   if (!ConstMask)
     return false;
-  if (ConstMask->isNullValue() || isa<UndefValue>(ConstMask))
+  if (match(ConstMask, m_CombineOr(m_Zero(), m_Undef())))
     return true;
   if (isa<ScalableVectorType>(ConstMask->getType()))
     return false;
@@ -1272,7 +1272,7 @@ bool llvm::maskIsAllZeroOrUndef(Value *Mask) {
            E = cast<FixedVectorType>(ConstMask->getType())->getNumElements();
        I != E; ++I) {
     if (auto *MaskElt = ConstMask->getAggregateElement(I))
-      if (MaskElt->isNullValue() || isa<UndefValue>(MaskElt))
+      if (match(MaskElt, m_CombineOr(m_Zero(), m_Undef())))
         continue;
     return false;
   }
@@ -1289,7 +1289,7 @@ bool llvm::maskIsAllOneOrUndef(Value *Mask) {
   auto *ConstMask = dyn_cast<Constant>(Mask);
   if (!ConstMask)
     return false;
-  if (ConstMask->isAllOnesValue() || isa<UndefValue>(ConstMask))
+  if (match(ConstMask, m_CombineOr(m_AllOnes(), m_Undef())))
     return true;
   if (isa<ScalableVectorType>(ConstMask->getType()))
     return false;
@@ -1298,7 +1298,7 @@ bool llvm::maskIsAllOneOrUndef(Value *Mask) {
            E = cast<FixedVectorType>(ConstMask->getType())->getNumElements();
        I != E; ++I) {
     if (auto *MaskElt = ConstMask->getAggregateElement(I))
-      if (MaskElt->isAllOnesValue() || isa<UndefValue>(MaskElt))
+      if (match(MaskElt, m_CombineOr(m_AllOnes(), m_Undef())))
         continue;
     return false;
   }
@@ -1315,7 +1315,7 @@ bool llvm::maskContainsAllOneOrUndef(Value *Mask) {
   auto *ConstMask = dyn_cast<Constant>(Mask);
   if (!ConstMask)
     return false;
-  if (ConstMask->isAllOnesValue() || isa<UndefValue>(ConstMask))
+  if (match(ConstMask, m_CombineOr(m_AllOnes(), m_Undef())))
     return true;
   if (isa<ScalableVectorType>(ConstMask->getType()))
     return false;
@@ -1324,7 +1324,7 @@ bool llvm::maskContainsAllOneOrUndef(Value *Mask) {
            E = cast<FixedVectorType>(ConstMask->getType())->getNumElements();
        I != E; ++I) {
     if (auto *MaskElt = ConstMask->getAggregateElement(I))
-      if (MaskElt->isAllOnesValue() || isa<UndefValue>(MaskElt))
+      if (match(MaskElt, m_CombineOr(m_AllOnes(), m_Undef())))
         return true;
   }
   return false;

if (!ConstMask)
return false;
if (ConstMask->isAllOnesValue() || isa<UndefValue>(ConstMask))
if (match(ConstMask, m_CombineOr(m_AllOnes(), m_Undef())))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isAllOnesValue can see through ConstantFP - can m_AllOnes()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! It can't; there are other issues with this patch including the fact that we're matching undef instead of just poison -- abandoning.

@artagnon artagnon changed the title [VectorUtils] Improve mask*All(Ones|Zero)OrUndef (NFC) [VectorUtils] Allow just poison in mask*All(Ones|Zero)OrUndef Nov 17, 2025
@artagnon artagnon closed this Nov 17, 2025
Copy link
Contributor

@fhahn fhahn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like there were no test changes with the patch. Are we missing some test coverage?

@artagnon
Copy link
Contributor Author

It looks like there were no test changes with the patch. Are we missing some test coverage?

We sure are, but I think a more comprehensive patch would strip the touched functions, update callers to directly use PM, and s/undef/poison/ in tests. Not sure if it's really worth the effort for the moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:analysis Includes value tracking, cost tables and constant folding

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants