From 0f287610c7271773f707d12697f93d2c09a1b953 Mon Sep 17 00:00:00 2001 From: Ramkumar Ramachandra Date: Thu, 22 Aug 2024 17:56:46 +0100 Subject: [PATCH] VPlan: introduce worklist in simplifyRecipes In order to break up patterns in simplifyRecipes, and increase its simplification power, introudce a worklist keeping a running list of candidates for simplification, as a prelude to breaking up patterns in simplifyRecipe. --- .../Transforms/Vectorize/VPlanTransforms.cpp | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp index 9796ee64f6ef9..aad017265fc2f 100644 --- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp +++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp @@ -852,8 +852,9 @@ void VPlanTransforms::clearReductionWrapFlags(VPlan &Plan) { } } -/// Try to simplify recipe \p R. -static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) { +/// Try to simplify recipe \p R. Returns any new recipes introduced during +/// simplification, as a candidate for further simplification. +static VPRecipeBase *simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) { using namespace llvm::VPlanPatternMatch; if (auto *Blend = dyn_cast(&R)) { @@ -868,11 +869,11 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) { if (UniqueValues.size() == 1) { Blend->replaceAllUsesWith(*UniqueValues.begin()); Blend->eraseFromParent(); - return; + return nullptr; } if (Blend->isNormalized()) - return; + return nullptr; // Normalize the blend so its first incoming value is used as the initial // value with the others blended into it. @@ -907,7 +908,7 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) { Blend->replaceAllUsesWith(NewBlend); Blend->eraseFromParent(); recursivelyDeleteDeadRecipes(DeadMask); - return; + return nullptr; } VPValue *A; @@ -920,7 +921,7 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) { } else { // Don't replace a scalarizing recipe with a widened cast. if (isa(&R)) - return; + return nullptr; if (ATy->getScalarSizeInBits() < TruncTy->getScalarSizeInBits()) { unsigned ExtOpcode = match(R.getOperand(0), m_SExt(m_VPValue())) @@ -955,6 +956,7 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) { assert(TypeInfo.inferScalarType(VPV) == TypeInfo2.inferScalarType(VPV)); } #endif + return nullptr; } // Simplify (X && Y) || (X && !Y) -> X. @@ -968,11 +970,12 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) { X == X1 && Y == Y1) { R.getVPSingleValue()->replaceAllUsesWith(X); R.eraseFromParent(); - return; + return nullptr; } if (match(&R, m_c_Mul(m_VPValue(A), m_SpecificInt(1)))) - return R.getVPSingleValue()->replaceAllUsesWith(A); + R.getVPSingleValue()->replaceAllUsesWith(A); + return nullptr; } /// Try to simplify the recipes in \p Plan. @@ -981,8 +984,10 @@ static void simplifyRecipes(VPlan &Plan, LLVMContext &Ctx) { Plan.getEntry()); VPTypeAnalysis TypeInfo(Plan.getCanonicalIV()->getScalarType(), Ctx); for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly(RPOT)) { - for (VPRecipeBase &R : make_early_inc_range(*VPBB)) { - simplifyRecipe(R, TypeInfo); + for (auto &R : make_early_inc_range(*VPBB)) { + VPRecipeBase *NewR = simplifyRecipe(R, TypeInfo); + while (NewR) + NewR = simplifyRecipe(*NewR, TypeInfo); } } }