Skip to content

Commit a9d36ec

Browse files
committed
[PatternMatch][VPlan] Add std::function match overload. NFCI
A relatively common use case for PatternMatch is to use match inside all_of/any_of/none_of. This patch adds an overload for match that returns a lambda so callers don't need to create a lambda themselves for both the LLVM and VPlan pattern matchers.
1 parent 003145d commit a9d36ec

File tree

13 files changed

+55
-57
lines changed

13 files changed

+55
-57
lines changed

llvm/include/llvm/IR/PatternMatch.h

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

53+
template <typename Val = const Value, typename Pattern>
54+
std::function<bool(Val *)> match(const Pattern &P) {
55+
return [&P](Val *V) { return P.match(V); };
56+
}
57+
5358
template <typename Pattern> bool match(ArrayRef<int> Mask, const Pattern &P) {
5459
return P.match(Mask);
5560
}

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(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(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(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(m_Constant())))
51285124
return nullptr;
51295125

51305126
if (!ConstantExpr::isSupportedGetElementPtr(SrcTy))
@@ -5649,7 +5645,7 @@ static Constant *simplifyFPOp(ArrayRef<Value *> Ops, FastMathFlags FMF,
56495645
RoundingMode Rounding) {
56505646
// Poison is independent of anything else. It always propagates from an
56515647
// operand to a math result.
5652-
if (any_of(Ops, [](Value *V) { return match(V, m_Poison()); }))
5648+
if (any_of(Ops, match(m_Poison())))
56535649
return PoisonValue::get(Ops[0]->getType());
56545650

56555651
for (Value *V : Ops) {
@@ -7116,7 +7112,7 @@ static Value *simplifyInstructionWithOperands(Instruction *I,
71167112

71177113
switch (I->getOpcode()) {
71187114
default:
7119-
if (llvm::all_of(NewOps, [](Value *V) { return isa<Constant>(V); })) {
7115+
if (all_of(NewOps, match(m_Constant()))) {
71207116
SmallVector<Constant *, 8> NewConstOps(NewOps.size());
71217117
transform(NewOps, NewConstOps.begin(),
71227118
[](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
@@ -251,9 +251,8 @@ bool llvm::haveNoCommonBitsSet(const WithCache<const Value *> &LHSCache,
251251
}
252252

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

259258
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
@@ -294,10 +294,9 @@ bool InterleavedAccessImpl::lowerInterleavedLoad(
294294
continue;
295295
}
296296
if (auto *BI = dyn_cast<BinaryOperator>(User)) {
297-
if (!BI->user_empty() && all_of(BI->users(), [](auto *U) {
298-
auto *SVI = dyn_cast<ShuffleVectorInst>(U);
299-
return SVI && isa<UndefValue>(SVI->getOperand(1));
300-
})) {
297+
using namespace PatternMatch;
298+
if (!BI->user_empty() &&
299+
all_of(BI->users(), match(m_Shuffle(m_Value(), m_Undef())))) {
301300
for (auto *SVI : BI->users())
302301
BinOpShuffles.insert(cast<ShuffleVectorInst>(SVI));
303302
continue;

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2307,12 +2307,8 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
23072307
// and let's try to sink `(sub 0, b)` into `b` itself. But only if this isn't
23082308
// a pure negation used by a select that looks like abs/nabs.
23092309
bool IsNegation = match(Op0, m_ZeroInt());
2310-
if (!IsNegation || none_of(I.users(), [&I, Op1](const User *U) {
2311-
const Instruction *UI = dyn_cast<Instruction>(U);
2312-
if (!UI)
2313-
return false;
2314-
return match(UI, m_c_Select(m_Specific(Op1), m_Specific(&I)));
2315-
})) {
2310+
if (!IsNegation ||
2311+
none_of(I.users(), match(m_c_Select(m_Specific(Op1), m_Specific(&I))))) {
23162312
if (Value *NegOp1 = Negator::Negate(IsNegation, /* IsNSW */ IsNegation &&
23172313
I.hasNoSignedWrap(),
23182314
Op1, *this))

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

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

14191419
// At least 1 operand must be a shuffle with 1 use because we are creating 2
14201420
// instructions.
1421-
if (none_of(II->args(), [](Value *V) {
1422-
return isa<ShuffleVectorInst>(V) && V->hasOneUse();
1423-
}))
1421+
if (none_of(II->args(), match(m_OneUse(m_Shuffle(m_Value(), m_Value())))))
14241422
return nullptr;
14251423

14261424
// 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
@@ -1341,7 +1341,7 @@ Instruction *InstCombinerImpl::foldICmpWithConstant(ICmpInst &Cmp) {
13411341
return nullptr;
13421342

13431343
if (auto *Phi = dyn_cast<PHINode>(Op0))
1344-
if (all_of(Phi->operands(), [](Value *V) { return isa<Constant>(V); })) {
1344+
if (all_of(Phi->operands(), match(m_Constant()))) {
13451345
SmallVector<Constant *> Ops;
13461346
for (Value *V : Phi->incoming_values()) {
13471347
Constant *Res =

llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp

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

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

13041304
BasicBlock *BB = PN.getParent();

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

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

31433143
// Profitability check - avoid increasing instruction count.
31443144
if (none_of(ArrayRef<Value *>({OuterSelVal.getCondition(), InnerSelVal}),
3145-
[](Value *V) { return V->hasOneUse(); }))
3145+
match(m_OneUse(m_Value()))))
31463146
return nullptr;
31473147

31483148
// 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
@@ -435,10 +435,9 @@ bool LoopInvariantCodeMotion::runOnLoop(Loop *L, AAResults *AA, LoopInfo *LI,
435435
// potentially happen in other passes where instructions are being moved
436436
// across that edge.
437437
bool HasCoroSuspendInst = llvm::any_of(L->getBlocks(), [](BasicBlock *BB) {
438-
return llvm::any_of(*BB, [](Instruction &I) {
439-
IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I);
440-
return II && II->getIntrinsicID() == Intrinsic::coro_suspend;
441-
});
438+
using namespace PatternMatch;
439+
return any_of(make_pointer_range(*BB),
440+
match(m_Intrinsic<Intrinsic::coro_suspend>()));
442441
});
443442

444443
MemorySSAUpdater MSSAU(MSSA);

0 commit comments

Comments
 (0)