@@ -892,8 +892,9 @@ void VPlanTransforms::clearReductionWrapFlags(VPlan &Plan) {
892892 }
893893}
894894
895- // / Try to simplify recipe \p R.
896- static void simplifyRecipe (VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
895+ // / Try to simplify recipe \p R. Returns any new recipes introduced during
896+ // / simplification, as a candidate for further simplification.
897+ static VPRecipeBase *simplifyRecipe (VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
897898 using namespace llvm ::VPlanPatternMatch;
898899
899900 if (auto *Blend = dyn_cast<VPBlendRecipe>(&R)) {
@@ -908,11 +909,11 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
908909 if (UniqueValues.size () == 1 ) {
909910 Blend->replaceAllUsesWith (*UniqueValues.begin ());
910911 Blend->eraseFromParent ();
911- return ;
912+ return nullptr ;
912913 }
913914
914915 if (Blend->isNormalized ())
915- return ;
916+ return nullptr ;
916917
917918 // Normalize the blend so its first incomming value is used as the initial
918919 // value with the others blended into it.
@@ -936,26 +937,27 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
936937 Blend->replaceAllUsesWith (NewBlend);
937938 Blend->eraseFromParent ();
938939 recursivelyDeleteDeadRecipes (DeadMask);
939- return ;
940+ return nullptr ;
940941 }
941942
942943 VPValue *A;
943944 if (match (&R, m_Trunc (m_ZExtOrSExt (m_VPValue (A))))) {
944945 VPValue *Trunc = R.getVPSingleValue ();
945946 Type *TruncTy = TypeInfo.inferScalarType (Trunc);
946947 Type *ATy = TypeInfo.inferScalarType (A);
948+ VPWidenCastRecipe *VPC = nullptr ;
947949 if (TruncTy == ATy) {
948950 Trunc->replaceAllUsesWith (A);
949951 } else {
950952 // Don't replace a scalarizing recipe with a widened cast.
951953 if (isa<VPReplicateRecipe>(&R))
952- return ;
954+ return {} ;
953955 if (ATy->getScalarSizeInBits () < TruncTy->getScalarSizeInBits ()) {
954956
955957 unsigned ExtOpcode = match (R.getOperand (0 ), m_SExt (m_VPValue ()))
956958 ? Instruction::SExt
957959 : Instruction::ZExt;
958- auto * VPC =
960+ VPC =
959961 new VPWidenCastRecipe (Instruction::CastOps (ExtOpcode), A, TruncTy);
960962 if (auto *UnderlyingExt = R.getOperand (0 )->getUnderlyingValue ()) {
961963 // UnderlyingExt has distinct return type, used to retain legacy cost.
@@ -964,7 +966,7 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
964966 VPC->insertBefore (&R);
965967 Trunc->replaceAllUsesWith (VPC);
966968 } else if (ATy->getScalarSizeInBits () > TruncTy->getScalarSizeInBits ()) {
967- auto * VPC = new VPWidenCastRecipe (Instruction::Trunc, A, TruncTy);
969+ VPC = new VPWidenCastRecipe (Instruction::Trunc, A, TruncTy);
968970 VPC->insertBefore (&R);
969971 Trunc->replaceAllUsesWith (VPC);
970972 }
@@ -984,6 +986,7 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
984986 assert (TypeInfo.inferScalarType (VPV) == TypeInfo2.inferScalarType (VPV));
985987 }
986988#endif
989+ return VPC;
987990 }
988991
989992 // Simplify (X && Y) || (X && !Y) -> X.
@@ -996,11 +999,12 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
996999 m_LogicalAnd (m_VPValue (X1), m_Not (m_VPValue (Y1))))) &&
9971000 X == X1 && Y == Y1) {
9981001 R.getVPSingleValue ()->replaceAllUsesWith (X);
999- return ;
1002+ return nullptr ;
10001003 }
10011004
10021005 if (match (&R, m_c_Mul (m_VPValue (A), m_SpecificInt (1 ))))
1003- return R.getVPSingleValue ()->replaceAllUsesWith (A);
1006+ R.getVPSingleValue ()->replaceAllUsesWith (A);
1007+ return nullptr ;
10041008}
10051009
10061010// / Try to simplify the recipes in \p Plan.
@@ -1009,8 +1013,10 @@ static void simplifyRecipes(VPlan &Plan, LLVMContext &Ctx) {
10091013 Plan.getEntry ());
10101014 VPTypeAnalysis TypeInfo (Plan.getCanonicalIV ()->getScalarType (), Ctx);
10111015 for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(RPOT)) {
1012- for (VPRecipeBase &R : make_early_inc_range (*VPBB)) {
1013- simplifyRecipe (R, TypeInfo);
1016+ for (auto &R : make_early_inc_range (*VPBB)) {
1017+ VPRecipeBase *NewR = simplifyRecipe (R, TypeInfo);
1018+ while (NewR)
1019+ NewR = simplifyRecipe (*NewR, TypeInfo);
10141020 }
10151021 }
10161022}
0 commit comments