Skip to content

Commit 32ddc99

Browse files
committed
[VPlan] Process simplifyRecipes as a worklist. NFCI
1 parent a4c2c3d commit 32ddc99

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ static void recursivelyDeleteDeadRecipes(VPValue *V) {
923923
}
924924

925925
/// Try to simplify recipe \p R.
926-
static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
926+
static VPValue *simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
927927
using namespace llvm::VPlanPatternMatch;
928928

929929
// VPScalarIVSteps can only be simplified after unrolling. VPScalarIVSteps for
@@ -932,8 +932,7 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
932932
if (auto *Steps = dyn_cast<VPScalarIVStepsRecipe>(&R)) {
933933
if (Steps->getParent()->getPlan()->isUnrolled() && Steps->isPart0() &&
934934
vputils::onlyFirstLaneUsed(Steps)) {
935-
Steps->replaceAllUsesWith(Steps->getOperand(0));
936-
return;
935+
return Steps->getOperand(0);
937936
}
938937
}
939938

@@ -943,11 +942,11 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
943942
Type *TruncTy = TypeInfo.inferScalarType(Trunc);
944943
Type *ATy = TypeInfo.inferScalarType(A);
945944
if (TruncTy == ATy) {
946-
Trunc->replaceAllUsesWith(A);
945+
return A;
947946
} else {
948947
// Don't replace a scalarizing recipe with a widened cast.
949948
if (isa<VPReplicateRecipe>(&R))
950-
return;
949+
return nullptr;
951950
if (ATy->getScalarSizeInBits() < TruncTy->getScalarSizeInBits()) {
952951

953952
unsigned ExtOpcode = match(R.getOperand(0), m_SExt(m_VPValue()))
@@ -960,11 +959,11 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
960959
VPC->setUnderlyingValue(UnderlyingExt);
961960
}
962961
VPC->insertBefore(&R);
963-
Trunc->replaceAllUsesWith(VPC);
962+
return VPC;
964963
} else if (ATy->getScalarSizeInBits() > TruncTy->getScalarSizeInBits()) {
965964
auto *VPC = new VPWidenCastRecipe(Instruction::Trunc, A, TruncTy);
966965
VPC->insertBefore(&R);
967-
Trunc->replaceAllUsesWith(VPC);
966+
return VPC;
968967
}
969968
}
970969
#ifndef NDEBUG
@@ -988,20 +987,17 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
988987
VPValue *X, *Y;
989988
if (match(&R,
990989
m_c_BinaryOr(m_LogicalAnd(m_VPValue(X), m_VPValue(Y)),
991-
m_LogicalAnd(m_Deferred(X), m_Not(m_Deferred(Y)))))) {
992-
R.getVPSingleValue()->replaceAllUsesWith(X);
993-
R.eraseFromParent();
994-
return;
995-
}
990+
m_LogicalAnd(m_Deferred(X), m_Not(m_Deferred(Y))))))
991+
return X;
996992

997993
if (match(&R, m_Select(m_VPValue(), m_VPValue(X), m_Deferred(X))))
998-
return R.getVPSingleValue()->replaceAllUsesWith(X);
994+
return X;
999995

1000996
if (match(&R, m_c_Mul(m_VPValue(A), m_SpecificInt(1))))
1001-
return R.getVPSingleValue()->replaceAllUsesWith(A);
997+
return A;
1002998

1003999
if (match(&R, m_Not(m_Not(m_VPValue(A)))))
1004-
return R.getVPSingleValue()->replaceAllUsesWith(A);
1000+
return A;
10051001

10061002
// Remove redundant DerviedIVs, that is 0 + A * 1 -> A and 0 + 0 * x -> 0.
10071003
if ((match(&R,
@@ -1010,16 +1006,31 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
10101006
m_DerivedIV(m_SpecificInt(0), m_SpecificInt(0), m_VPValue()))) &&
10111007
TypeInfo.inferScalarType(R.getOperand(1)) ==
10121008
TypeInfo.inferScalarType(R.getVPSingleValue()))
1013-
return R.getVPSingleValue()->replaceAllUsesWith(R.getOperand(1));
1009+
return R.getOperand(1);
1010+
1011+
return nullptr;
10141012
}
10151013

10161014
void VPlanTransforms::simplifyRecipes(VPlan &Plan, Type &CanonicalIVTy) {
10171015
ReversePostOrderTraversal<VPBlockDeepTraversalWrapper<VPBlockBase *>> RPOT(
10181016
Plan.getEntry());
10191017
VPTypeAnalysis TypeInfo(&CanonicalIVTy);
1020-
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(RPOT)) {
1021-
for (VPRecipeBase &R : make_early_inc_range(*VPBB)) {
1022-
simplifyRecipe(R, TypeInfo);
1018+
SetVector<VPRecipeBase *> Worklist;
1019+
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(RPOT))
1020+
for (VPRecipeBase &R : make_early_inc_range(*VPBB))
1021+
Worklist.insert(&R);
1022+
1023+
while (!Worklist.empty()) {
1024+
VPRecipeBase *R = Worklist.pop_back_val();
1025+
if (VPValue *Result = simplifyRecipe(*R, TypeInfo)) {
1026+
R->getVPSingleValue()->replaceAllUsesWith(Result);
1027+
R->eraseFromParent();
1028+
if (VPRecipeBase *ResultR = Result->getDefiningRecipe())
1029+
Worklist.insert(ResultR);
1030+
for (VPUser *U : Result->users())
1031+
if (auto *UR = dyn_cast<VPRecipeBase>(U))
1032+
if (UR != R)
1033+
Worklist.insert(UR);
10231034
}
10241035
}
10251036
}

0 commit comments

Comments
 (0)