[llvm] [VPlan] Use subpattern-m_VPValue in simplifyRecipe (NFC) (PR #213268)
Ramkumar Ramachandra via llvm-commits
llvm-commits at lists.llvm.org
Fri Jul 31 08:28:53 PDT 2026
https://github.com/artagnon updated https://github.com/llvm/llvm-project/pull/213268
>From ab9d7f7729eeec19e8ac42464f74f7de07431acd Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Fri, 31 Jul 2026 13:54:47 +0100
Subject: [PATCH 1/2] [VPlan] Use subpattern-m_VPValue in simplifyRecipe (NFC)
Use the subpattern variant of m_VPValue introduced in 8db13de ([VPlan]
Remove redundant x && (y && x) -> x && y combine, #213219) to improve
code in simplifyRecipe.
---
.../Transforms/Vectorize/VPlanTransforms.cpp | 53 +++++++++----------
1 file changed, 26 insertions(+), 27 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index 4d6f40ec32a62..abae6efd6df1e 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -1298,8 +1298,8 @@ static void simplifyRecipe(VPSingleDefRecipe *Def) {
bool CanCreateNewRecipe =
!isa<VPInstruction>(Def) || !Def->getUnderlyingValue();
- VPValue *A;
- if (match(Def, m_Trunc(m_ZExtOrSExt(m_VPValue(A))))) {
+ VPValue *A, *Z;
+ if (match(Def, m_Trunc(m_VPValue(Z, m_ZExtOrSExt(m_VPValue(A)))))) {
Type *TruncTy = Def->getScalarType();
Type *ATy = A->getScalarType();
if (TruncTy == ATy) {
@@ -1310,12 +1310,11 @@ static void simplifyRecipe(VPSingleDefRecipe *Def) {
return;
if (ATy->getScalarSizeInBits() < TruncTy->getScalarSizeInBits()) {
- unsigned ExtOpcode = match(Def->getOperand(0), m_SExt(m_VPValue()))
- ? Instruction::SExt
- : Instruction::ZExt;
+ unsigned ExtOpcode = match(Z, m_SExt(m_VPValue())) ? Instruction::SExt
+ : Instruction::ZExt;
auto *Ext = Builder.createWidenCast(Instruction::CastOps(ExtOpcode), A,
TruncTy);
- if (auto *UnderlyingExt = Def->getOperand(0)->getUnderlyingValue()) {
+ if (auto *UnderlyingExt = Z->getUnderlyingValue()) {
// UnderlyingExt has distinct return type, used to retain legacy cost.
Ext->setUnderlyingValue(UnderlyingExt);
}
@@ -1330,7 +1329,7 @@ static void simplifyRecipe(VPSingleDefRecipe *Def) {
if (simplifyLogicalRecipe(Def, Builder, CanCreateNewRecipe))
return;
- VPValue *X, *Y, *C;
+ VPValue *X, *Y;
if (match(Def, m_c_Add(m_VPValue(A), m_ZeroInt())))
return Def->replaceAllUsesWith(A);
@@ -1349,14 +1348,13 @@ static void simplifyRecipe(VPSingleDefRecipe *Def) {
}
if (CanCreateNewRecipe &&
- match(Def, m_c_Add(m_VPValue(X), m_Sub(m_ZeroInt(), m_VPValue(Y))))) {
+ match(Def, m_c_Add(m_VPValue(X),
+ m_VPValue(Z, m_Sub(m_ZeroInt(), m_VPValue(Y)))))) {
// Preserve nsw from the Add and the Sub, if it's present on both, on the
// new Sub.
VPIRFlags::WrapFlagsTy NW = {
- false,
- cast<VPRecipeWithIRFlags>(Def)->hasNoSignedWrap() &&
- cast<VPRecipeWithIRFlags>(Def->getOperand(Def->getOperand(0) == X))
- ->hasNoSignedWrap()};
+ false, cast<VPRecipeWithIRFlags>(Def)->hasNoSignedWrap() &&
+ cast<VPRecipeWithIRFlags>(Z)->hasNoSignedWrap()};
return Def->replaceAllUsesWith(
Builder.createSub(X, Y, Def->getDebugLoc(), "", NW));
}
@@ -1458,9 +1456,10 @@ static void simplifyRecipe(VPSingleDefRecipe *Def) {
// Remove redundant DerviedIVs, that is 0 + A * 1 -> A and 0 + 0 * x -> 0.
if ((match(Def, m_DerivedIV(m_ZeroInt(), m_VPValue(A), m_One())) ||
- match(Def, m_DerivedIV(m_ZeroInt(), m_ZeroInt(), m_VPValue()))) &&
- Def->getOperand(1)->getScalarType() == Def->getScalarType())
- return Def->replaceAllUsesWith(Def->getOperand(1));
+ match(Def, m_DerivedIV(m_ZeroInt(), m_VPValue(A, m_ZeroInt()),
+ m_VPValue()))) &&
+ A->getScalarType() == Def->getScalarType())
+ return Def->replaceAllUsesWith(A);
if (match(Def, m_VPInstruction<VPInstruction::WideIVStep>(m_VPValue(X),
m_One()))) {
@@ -1473,11 +1472,12 @@ static void simplifyRecipe(VPSingleDefRecipe *Def) {
// For i1 vp.merges produced by AnyOf reductions:
// vp.merge true, (or x, y), x, evl -> vp.merge y, true, x, evl
- if (match(Def, m_Intrinsic<Intrinsic::vp_merge>(m_True(), m_VPValue(A),
- m_VPValue(X), m_VPValue())) &&
+ if (match(Def, m_Intrinsic<Intrinsic::vp_merge>(m_VPValue(Z, m_True()),
+ m_VPValue(A), m_VPValue(X),
+ m_VPValue())) &&
match(A, m_c_BinaryOr(m_Specific(X), m_VPValue(Y))) &&
Def->getScalarType()->isIntegerTy(1)) {
- Def->setOperand(1, Def->getOperand(0));
+ Def->setOperand(1, Z);
Def->setOperand(0, Y);
return;
}
@@ -1538,10 +1538,10 @@ static void simplifyRecipe(VPSingleDefRecipe *Def) {
// Look through broadcast of single-scalar when used as select conditions; in
// that case the scalar condition can be used directly.
if (match(Def,
- m_Select(m_Broadcast(m_VPValue(C)), m_VPValue(), m_VPValue()))) {
- assert(vputils::isSingleScalar(C) &&
+ m_Select(m_Broadcast(m_VPValue(Z)), m_VPValue(), m_VPValue()))) {
+ assert(vputils::isSingleScalar(Z) &&
"broadcast operand must be single-scalar");
- Def->setOperand(0, C);
+ Def->setOperand(0, Z);
return;
}
@@ -1598,18 +1598,17 @@ static void simplifyRecipe(VPSingleDefRecipe *Def) {
// Def = IVInc + Y
// Fold the increment Y into the phi's start value, replace Def with IVInc,
// and if Inc exists, replace it with X.
- if (match(Def, m_Add(m_Add(m_VPValue(X), m_VPValue()), m_VPValue(Y))) &&
- isa<VPIRValue>(Y) &&
- match(X, m_VPPhi(m_ZeroInt(), m_Specific(Def->getOperand(0))))) {
+ if (match(Def, m_Add(m_VPValue(Z, m_Add(m_VPValue(X), m_VPValue())),
+ m_VPValue(Y))) &&
+ isa<VPIRValue>(Y) && match(X, m_VPPhi(m_ZeroInt(), m_Specific(Z)))) {
auto *Phi = cast<VPPhi>(X);
- auto *IVInc = Def->getOperand(0);
- if (IVInc->getNumUsers() == 2) {
+ if (Z->getNumUsers() == 2) {
// If Phi has a second user (besides IVInc's defining recipe), it must
// be Inc = Phi + Y for the fold to apply.
auto *Inc = dyn_cast_or_null<VPSingleDefRecipe>(
findUserOf(Phi, m_Add(m_Specific(Phi), m_Specific(Y))));
if (Phi->getNumUsers() == 1 || (Phi->getNumUsers() == 2 && Inc)) {
- Def->replaceAllUsesWith(IVInc);
+ Def->replaceAllUsesWith(Z);
if (Inc)
Inc->replaceAllUsesWith(Phi);
Phi->setOperand(0, Y);
>From ab80610920cfdee6fbcbafaef31ec06d12efde82 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Fri, 31 Jul 2026 16:27:15 +0100
Subject: [PATCH 2/2] [VPlan] Some readability improvements
---
.../lib/Transforms/Vectorize/VPlanTransforms.cpp | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index abae6efd6df1e..19dd5550c83ed 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -1472,12 +1472,11 @@ static void simplifyRecipe(VPSingleDefRecipe *Def) {
// For i1 vp.merges produced by AnyOf reductions:
// vp.merge true, (or x, y), x, evl -> vp.merge y, true, x, evl
- if (match(Def, m_Intrinsic<Intrinsic::vp_merge>(m_VPValue(Z, m_True()),
- m_VPValue(A), m_VPValue(X),
- m_VPValue())) &&
+ if (match(Def, m_Intrinsic<Intrinsic::vp_merge>(m_True(), m_VPValue(A),
+ m_VPValue(X), m_VPValue())) &&
match(A, m_c_BinaryOr(m_Specific(X), m_VPValue(Y))) &&
Def->getScalarType()->isIntegerTy(1)) {
- Def->setOperand(1, Z);
+ Def->setOperand(1, Plan->getTrue());
Def->setOperand(0, Y);
return;
}
@@ -1598,17 +1597,18 @@ static void simplifyRecipe(VPSingleDefRecipe *Def) {
// Def = IVInc + Y
// Fold the increment Y into the phi's start value, replace Def with IVInc,
// and if Inc exists, replace it with X.
- if (match(Def, m_Add(m_VPValue(Z, m_Add(m_VPValue(X), m_VPValue())),
+ VPValue *IVInc;
+ if (match(Def, m_Add(m_VPValue(IVInc, m_Add(m_VPValue(X), m_VPValue())),
m_VPValue(Y))) &&
- isa<VPIRValue>(Y) && match(X, m_VPPhi(m_ZeroInt(), m_Specific(Z)))) {
+ isa<VPIRValue>(Y) && match(X, m_VPPhi(m_ZeroInt(), m_Specific(IVInc)))) {
auto *Phi = cast<VPPhi>(X);
- if (Z->getNumUsers() == 2) {
+ if (IVInc->getNumUsers() == 2) {
// If Phi has a second user (besides IVInc's defining recipe), it must
// be Inc = Phi + Y for the fold to apply.
auto *Inc = dyn_cast_or_null<VPSingleDefRecipe>(
findUserOf(Phi, m_Add(m_Specific(Phi), m_Specific(Y))));
if (Phi->getNumUsers() == 1 || (Phi->getNumUsers() == 2 && Inc)) {
- Def->replaceAllUsesWith(Z);
+ Def->replaceAllUsesWith(IVInc);
if (Inc)
Inc->replaceAllUsesWith(Phi);
Phi->setOperand(0, Y);
More information about the llvm-commits
mailing list