Skip to content

Commit 619fb52

Browse files
artagnonlukel97
andcommitted
[PatternMatch] Introduce match functor (NFC)
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 aa1a694 commit 619fb52

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
@@ -5028,14 +5028,12 @@ static Value *simplifyGEPInst(Type *SrcTy, Value *Ptr,
50285028
}
50295029

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

50355034
// getelementptr poison, idx -> poison
50365035
// getelementptr baseptr, poison -> poison
5037-
if (isa<PoisonValue>(Ptr) ||
5038-
any_of(Indices, [](const auto *V) { return isa<PoisonValue>(V); }))
5036+
if (isa<PoisonValue>(Ptr) || any_of(Indices, match_fn(m_Poison())))
50395037
return PoisonValue::get(GEPTy);
50405038

50415039
// getelementptr undef, idx -> undef
@@ -5092,8 +5090,7 @@ static Value *simplifyGEPInst(Type *SrcTy, Value *Ptr,
50925090
}
50935091

50945092
if (!IsScalableVec && Q.DL.getTypeAllocSize(LastType) == 1 &&
5095-
all_of(Indices.drop_back(1),
5096-
[](Value *Idx) { return match(Idx, m_Zero()); })) {
5093+
all_of(Indices.drop_back(1), match_fn(m_Zero()))) {
50975094
unsigned IdxWidth =
50985095
Q.DL.getIndexSizeInBits(Ptr->getType()->getPointerAddressSpace());
50995096
if (Q.DL.getTypeSizeInBits(Indices.back()->getType()) == IdxWidth) {
@@ -5123,8 +5120,7 @@ static Value *simplifyGEPInst(Type *SrcTy, Value *Ptr,
51235120
}
51245121

51255122
// Check to see if this is constant foldable.
5126-
if (!isa<Constant>(Ptr) ||
5127-
!all_of(Indices, [](Value *V) { return isa<Constant>(V); }))
5123+
if (!isa<Constant>(Ptr) || !all_of(Indices, match_fn(m_Constant())))
51285124
return nullptr;
51295125

51305126
if (!ConstantExpr::isSupportedGetElementPtr(SrcTy))
@@ -5662,7 +5658,7 @@ static Constant *simplifyFPOp(ArrayRef<Value *> Ops, FastMathFlags FMF,
56625658
RoundingMode Rounding) {
56635659
// Poison is independent of anything else. It always propagates from an
56645660
// operand to a math result.
5665-
if (any_of(Ops, [](Value *V) { return match(V, m_Poison()); }))
5661+
if (any_of(Ops, match_fn(m_Poison())))
56665662
return PoisonValue::get(Ops[0]->getType());
56675663

56685664
for (Value *V : Ops) {
@@ -7126,7 +7122,7 @@ static Value *simplifyInstructionWithOperands(Instruction *I,
71267122

71277123
switch (I->getOpcode()) {
71287124
default:
7129-
if (llvm::all_of(NewOps, [](Value *V) { return isa<Constant>(V); })) {
7125+
if (all_of(NewOps, match_fn(m_Constant()))) {
71307126
SmallVector<Constant *, 8> NewConstOps(NewOps.size());
71317127
transform(NewOps, NewConstOps.begin(),
71327128
[](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(), match_fn(m_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(), match_fn(m_PtrToInt(m_Value()))))
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(), match_fn(m_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)