1212// ===----------------------------------------------------------------------===//
1313
1414#include " LoopVectorizationPlanner.h"
15+ #include " VPRecipeBuilder.h"
1516#include " VPlan.h"
1617#include " VPlanCFG.h"
1718#include " VPlanDominatorTree.h"
@@ -1002,10 +1003,10 @@ bool VPlanTransforms::handleMaxMinNumReductions(VPlan &Plan) {
10021003 return true ;
10031004}
10041005
1005- void VPlanTransforms::convertFindLastRecurrences (
1006- VPlan &Plan, VPRecipeBuilder &RecipeBuilder) {
1006+ bool VPlanTransforms::handleFindLastReductions (VPlan &Plan,
1007+ VPRecipeBuilder &RecipeBuilder) {
10071008 if (Plan.hasScalarVFOnly ())
1008- return ;
1009+ return false ;
10091010
10101011 // We want to create the following nodes:
10111012 // vec.body:
@@ -1029,8 +1030,20 @@ void VPlanTransforms::convertFindLastRecurrences(
10291030 continue ;
10301031
10311032 // Find the condition for the select
1032- auto *SR = cast<VPWidenSelectRecipe>(&PhiR->getBackedgeRecipe ());
1033- VPValue *Cond = SR->getCond ();
1033+ auto *SelectR = cast<VPSingleDefRecipe>(&PhiR->getBackedgeRecipe ());
1034+ VPValue *Cond = nullptr ;
1035+ if (auto *WidenR = dyn_cast<VPWidenSelectRecipe>(SelectR))
1036+ Cond = WidenR->getCond ();
1037+ else if (auto *RepR = dyn_cast<VPReplicateRecipe>(SelectR)) {
1038+ auto *SI = dyn_cast<SelectInst>(RepR->getUnderlyingInstr ());
1039+ if (!SI)
1040+ return false ;
1041+ auto *CmpI = dyn_cast<Instruction>(SI->getCondition ());
1042+ if (!CmpI)
1043+ return false ;
1044+ Cond = RecipeBuilder.getRecipe (CmpI)->getVPSingleValue ();
1045+ } else
1046+ return false ;
10341047
10351048 // Add mask phi
10361049 VPBuilder Builder = VPBuilder::getToInsertAfter (PhiR);
@@ -1040,21 +1053,23 @@ void VPlanTransforms::convertFindLastRecurrences(
10401053 Builder.insert (MaskPHI);
10411054
10421055 // Add select for mask
1043- Builder.setInsertPoint (SR );
1056+ Builder.setInsertPoint (SelectR );
10441057 VPValue *AnyOf = Builder.createNaryOp (VPInstruction::AnyOf, {Cond});
10451058 VPValue *MaskSelect = Builder.createSelect (AnyOf, Cond, MaskPHI);
10461059 MaskPHI->addOperand (MaskSelect);
10471060
10481061 // Replace select for data
1049- VPValue *DataSelect = Builder.createSelect (
1050- AnyOf, SR->getOperand (1 ), SR->getOperand (2 ), SR->getDebugLoc ());
1051- SR->replaceAllUsesWith (DataSelect);
1052- SR->eraseFromParent ();
1062+ VPValue *DataSelect =
1063+ Builder.createSelect (AnyOf, SelectR->getOperand (1 ),
1064+ SelectR->getOperand (2 ), SelectR->getDebugLoc ());
1065+ SelectR->replaceAllUsesWith (DataSelect);
1066+ SelectR->eraseFromParent ();
10531067
10541068 // Find final reduction computation and replace it with an
10551069 // extract.last.active intrinsic.
10561070 VPInstruction *RdxResult = findComputeReductionResult (PhiR);
1057- assert (RdxResult && " Unable to find Reduction Result Recipe" );
1071+ if (!RdxResult)
1072+ return false ;
10581073 Builder.setInsertPoint (RdxResult);
10591074 auto *ExtractLastActive =
10601075 Builder.createNaryOp (VPInstruction::ExtractLastActive,
@@ -1063,4 +1078,6 @@ void VPlanTransforms::convertFindLastRecurrences(
10631078 RdxResult->replaceAllUsesWith (ExtractLastActive);
10641079 RdxResult->eraseFromParent ();
10651080 }
1081+
1082+ return true ;
10661083}
0 commit comments