Skip to content

Commit 7fb3a91

Browse files
artagnonlukel97
andauthored
[PatternMatch] Introduce match functor (NFC) (llvm#159386)
A common idiom is the usage of the PatternMatch match function within a functional algorithm like all_of. Introduce a match functor to shorten this idiom. Co-authored-by: Luke Lau <[email protected]>
1 parent 835d6b3 commit 7fb3a91

File tree

11 files changed

+38
-38
lines changed

11 files changed

+38
-38
lines changed

llvm/include/llvm/IR/PatternMatch.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ template <typename Val, typename Pattern> bool match(Val *V, const Pattern &P) {
5050
return P.match(V);
5151
}
5252

53+
template <typename Val, typename Pattern> struct MatchFunctor {
54+
const Pattern &P;
55+
MatchFunctor(const Pattern &P) : P(P) {}
56+
bool operator()(Val *V) const { return P.match(V); }
57+
};
58+
59+
/// A match functor that can be used as a UnaryPredicate in functional
60+
/// algorithms like all_of.
61+
template <typename Val = const Value, typename Pattern>
62+
MatchFunctor<Val, Pattern> match_fn(const Pattern &P) {
63+
return P;
64+
}
65+
5366
template <typename Pattern> bool match(ArrayRef<int> Mask, const Pattern &P) {
5467
return P.match(Mask);
5568
}

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5057,14 +5057,12 @@ static Value *simplifyGEPInst(Type *SrcTy, Value *Ptr,
50575057
}
50585058

50595059
// All-zero GEP is a no-op, unless it performs a vector splat.
5060-
if (Ptr->getType() == GEPTy &&
5061-
all_of(Indices, [](const auto *V) { return match(V, m_Zero()); }))
5060+
if (Ptr->getType() == GEPTy && all_of(Indices, match_fn(m_Zero())))
50625061
return Ptr;
50635062

50645063
// getelementptr poison, idx -> poison
50655064
// getelementptr baseptr, poison -> poison
5066-
if (isa<PoisonValue>(Ptr) ||
5067-
any_of(Indices, [](const auto *V) { return isa<PoisonValue>(V); }))
5065+
if (isa<PoisonValue>(Ptr) || any_of(Indices, IsaPred<PoisonValue>))
50685066
return PoisonValue::get(GEPTy);
50695067

50705068
// getelementptr undef, idx -> undef
@@ -5121,8 +5119,7 @@ static Value *simplifyGEPInst(Type *SrcTy, Value *Ptr,
51215119
}
51225120

51235121
if (!IsScalableVec && Q.DL.getTypeAllocSize(LastType) == 1 &&
5124-
all_of(Indices.drop_back(1),
5125-
[](Value *Idx) { return match(Idx, m_Zero()); })) {
5122+
all_of(Indices.drop_back(1), match_fn(m_Zero()))) {
51265123
unsigned IdxWidth =
51275124
Q.DL.getIndexSizeInBits(Ptr->getType()->getPointerAddressSpace());
51285125
if (Q.DL.getTypeSizeInBits(Indices.back()->getType()) == IdxWidth) {
@@ -5152,8 +5149,7 @@ static Value *simplifyGEPInst(Type *SrcTy, Value *Ptr,
51525149
}
51535150

51545151
// Check to see if this is constant foldable.
5155-
if (!isa<Constant>(Ptr) ||
5156-
!all_of(Indices, [](Value *V) { return isa<Constant>(V); }))
5152+
if (!isa<Constant>(Ptr) || !all_of(Indices, IsaPred<Constant>))
51575153
return nullptr;
51585154

51595155
if (!ConstantExpr::isSupportedGetElementPtr(SrcTy))
@@ -5691,7 +5687,7 @@ static Constant *simplifyFPOp(ArrayRef<Value *> Ops, FastMathFlags FMF,
56915687
RoundingMode Rounding) {
56925688
// Poison is independent of anything else. It always propagates from an
56935689
// operand to a math result.
5694-
if (any_of(Ops, [](Value *V) { return match(V, m_Poison()); }))
5690+
if (any_of(Ops, IsaPred<PoisonValue>))
56955691
return PoisonValue::get(Ops[0]->getType());
56965692

56975693
for (Value *V : Ops) {
@@ -7155,7 +7151,7 @@ static Value *simplifyInstructionWithOperands(Instruction *I,
71557151

71567152
switch (I->getOpcode()) {
71577153
default:
7158-
if (llvm::all_of(NewOps, [](Value *V) { return isa<Constant>(V); })) {
7154+
if (all_of(NewOps, IsaPred<Constant>)) {
71597155
SmallVector<Constant *, 8> NewConstOps(NewOps.size());
71607156
transform(NewOps, NewConstOps.begin(),
71617157
[](Value *V) { return cast<Constant>(V); });

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,8 @@ bool llvm::haveNoCommonBitsSet(const WithCache<const Value *> &LHSCache,
250250
}
251251

252252
bool llvm::isOnlyUsedInZeroComparison(const Instruction *I) {
253-
return !I->user_empty() && all_of(I->users(), [](const User *U) {
254-
return match(U, m_ICmp(m_Value(), m_Zero()));
255-
});
253+
return !I->user_empty() &&
254+
all_of(I->users(), match_fn(m_ICmp(m_Value(), m_Zero())));
256255
}
257256

258257
bool llvm::isOnlyUsedInZeroEqualityComparison(const Instruction *I) {

llvm/lib/CodeGen/InterleavedAccessPass.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,9 @@ bool InterleavedAccessImpl::lowerInterleavedLoad(
312312
continue;
313313
}
314314
if (auto *BI = dyn_cast<BinaryOperator>(User)) {
315-
if (!BI->user_empty() && all_of(BI->users(), [](auto *U) {
316-
auto *SVI = dyn_cast<ShuffleVectorInst>(U);
317-
return SVI && isa<UndefValue>(SVI->getOperand(1));
318-
})) {
315+
using namespace PatternMatch;
316+
if (!BI->user_empty() &&
317+
all_of(BI->users(), match_fn(m_Shuffle(m_Value(), m_Undef())))) {
319318
for (auto *SVI : BI->users())
320319
BinOpShuffles.insert(cast<ShuffleVectorInst>(SVI));
321320
continue;

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2354,12 +2354,8 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
23542354
// and let's try to sink `(sub 0, b)` into `b` itself. But only if this isn't
23552355
// a pure negation used by a select that looks like abs/nabs.
23562356
bool IsNegation = match(Op0, m_ZeroInt());
2357-
if (!IsNegation || none_of(I.users(), [&I, Op1](const User *U) {
2358-
const Instruction *UI = dyn_cast<Instruction>(U);
2359-
if (!UI)
2360-
return false;
2361-
return match(UI, m_c_Select(m_Specific(Op1), m_Specific(&I)));
2362-
})) {
2357+
if (!IsNegation || none_of(I.users(), match_fn(m_c_Select(m_Specific(Op1),
2358+
m_Specific(&I))))) {
23632359
if (Value *NegOp1 = Negator::Negate(IsNegation, /* IsNSW */ IsNegation &&
23642360
I.hasNoSignedWrap(),
23652361
Op1, *this))

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,9 +1416,7 @@ InstCombinerImpl::foldShuffledIntrinsicOperands(IntrinsicInst *II) {
14161416

14171417
// At least 1 operand must be a shuffle with 1 use because we are creating 2
14181418
// instructions.
1419-
if (none_of(II->args(), [](Value *V) {
1420-
return isa<ShuffleVectorInst>(V) && V->hasOneUse();
1421-
}))
1419+
if (none_of(II->args(), match_fn(m_OneUse(m_Shuffle(m_Value(), m_Value())))))
14221420
return nullptr;
14231421

14241422
// See if all arguments are shuffled with the same mask.

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1340,7 +1340,7 @@ Instruction *InstCombinerImpl::foldICmpWithConstant(ICmpInst &Cmp) {
13401340
return nullptr;
13411341

13421342
if (auto *Phi = dyn_cast<PHINode>(Op0))
1343-
if (all_of(Phi->operands(), [](Value *V) { return isa<Constant>(V); })) {
1343+
if (all_of(Phi->operands(), IsaPred<Constant>)) {
13441344
SmallVector<Constant *> Ops;
13451345
for (Value *V : Phi->incoming_values()) {
13461346
Constant *Res =

llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ bool InstCombinerImpl::foldIntegerTypedPHI(PHINode &PN) {
340340
Instruction *InstCombinerImpl::foldPHIArgIntToPtrToPHI(PHINode &PN) {
341341
// convert ptr2int ( phi[ int2ptr(ptr2int(x))] ) --> ptr2int ( phi [ x ] )
342342
// Make sure all uses of phi are ptr2int.
343-
if (!all_of(PN.users(), [](User *U) { return isa<PtrToIntInst>(U); }))
343+
if (!all_of(PN.users(), IsaPred<PtrToIntInst>))
344344
return nullptr;
345345

346346
// Iterating over all operands to check presence of target pointers for
@@ -1299,7 +1299,7 @@ static Value *simplifyUsingControlFlow(InstCombiner &Self, PHINode &PN,
12991299
// \ /
13001300
// phi [v1] [v2]
13011301
// Make sure all inputs are constants.
1302-
if (!all_of(PN.operands(), [](Value *V) { return isa<ConstantInt>(V); }))
1302+
if (!all_of(PN.operands(), IsaPred<ConstantInt>))
13031303
return nullptr;
13041304

13051305
BasicBlock *BB = PN.getParent();

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3257,7 +3257,7 @@ static Instruction *foldNestedSelects(SelectInst &OuterSelVal,
32573257

32583258
// Profitability check - avoid increasing instruction count.
32593259
if (none_of(ArrayRef<Value *>({OuterSelVal.getCondition(), InnerSelVal}),
3260-
[](Value *V) { return V->hasOneUse(); }))
3260+
match_fn(m_OneUse(m_Value()))))
32613261
return nullptr;
32623262

32633263
// The appropriate hand of the outermost `select` must be a select itself.

llvm/lib/Transforms/Scalar/LICM.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,10 +437,9 @@ bool LoopInvariantCodeMotion::runOnLoop(Loop *L, AAResults *AA, LoopInfo *LI,
437437
// potentially happen in other passes where instructions are being moved
438438
// across that edge.
439439
bool HasCoroSuspendInst = llvm::any_of(L->getBlocks(), [](BasicBlock *BB) {
440-
return llvm::any_of(*BB, [](Instruction &I) {
441-
IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I);
442-
return II && II->getIntrinsicID() == Intrinsic::coro_suspend;
443-
});
440+
using namespace PatternMatch;
441+
return any_of(make_pointer_range(*BB),
442+
match_fn(m_Intrinsic<Intrinsic::coro_suspend>()));
444443
});
445444

446445
MemorySSAUpdater MSSAU(MSSA);

0 commit comments

Comments
 (0)