@@ -892,8 +892,9 @@ void VPlanTransforms::clearReductionWrapFlags(VPlan &Plan) {
892
892
}
893
893
}
894
894
895
- // / Try to simplify recipe \p R.
896
- static void simplifyRecipe (VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
895
+ // / Try to simplify recipe \p R. Returns candidates for further simplification.
896
+ static SmallVector<VPRecipeBase *> simplifyRecipe (VPRecipeBase &R,
897
+ VPTypeAnalysis &TypeInfo) {
897
898
using namespace llvm ::VPlanPatternMatch;
898
899
899
900
if (auto *Blend = dyn_cast<VPBlendRecipe>(&R)) {
@@ -908,11 +909,11 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
908
909
if (UniqueValues.size () == 1 ) {
909
910
Blend->replaceAllUsesWith (*UniqueValues.begin ());
910
911
Blend->eraseFromParent ();
911
- return ;
912
+ return {} ;
912
913
}
913
914
914
915
if (Blend->isNormalized ())
915
- return ;
916
+ return {} ;
916
917
917
918
// Normalize the blend so its first incomming value is used as the initial
918
919
// value with the others blended into it.
@@ -936,26 +937,27 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
936
937
Blend->replaceAllUsesWith (NewBlend);
937
938
Blend->eraseFromParent ();
938
939
recursivelyDeleteDeadRecipes (DeadMask);
939
- return ;
940
+ return {} ;
940
941
}
941
942
942
943
VPValue *A;
943
944
if (match (&R, m_Trunc (m_ZExtOrSExt (m_VPValue (A))))) {
944
945
VPValue *Trunc = R.getVPSingleValue ();
945
946
Type *TruncTy = TypeInfo.inferScalarType (Trunc);
946
947
Type *ATy = TypeInfo.inferScalarType (A);
948
+ VPWidenCastRecipe *VPC = nullptr ;
947
949
if (TruncTy == ATy) {
948
950
Trunc->replaceAllUsesWith (A);
949
951
} else {
950
952
// Don't replace a scalarizing recipe with a widened cast.
951
953
if (isa<VPReplicateRecipe>(&R))
952
- return ;
954
+ return {} ;
953
955
if (ATy->getScalarSizeInBits () < TruncTy->getScalarSizeInBits ()) {
954
956
955
957
unsigned ExtOpcode = match (R.getOperand (0 ), m_SExt (m_VPValue ()))
956
958
? Instruction::SExt
957
959
: Instruction::ZExt;
958
- auto * VPC =
960
+ VPC =
959
961
new VPWidenCastRecipe (Instruction::CastOps (ExtOpcode), A, TruncTy);
960
962
if (auto *UnderlyingExt = R.getOperand (0 )->getUnderlyingValue ()) {
961
963
// UnderlyingExt has distinct return type, used to retain legacy cost.
@@ -964,7 +966,7 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
964
966
VPC->insertBefore (&R);
965
967
Trunc->replaceAllUsesWith (VPC);
966
968
} else if (ATy->getScalarSizeInBits () > TruncTy->getScalarSizeInBits ()) {
967
- auto * VPC = new VPWidenCastRecipe (Instruction::Trunc, A, TruncTy);
969
+ VPC = new VPWidenCastRecipe (Instruction::Trunc, A, TruncTy);
968
970
VPC->insertBefore (&R);
969
971
Trunc->replaceAllUsesWith (VPC);
970
972
}
@@ -984,6 +986,9 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
984
986
assert (TypeInfo.inferScalarType (VPV) == TypeInfo2.inferScalarType (VPV));
985
987
}
986
988
#endif
989
+ if (VPC)
990
+ return {VPC};
991
+ return {};
987
992
}
988
993
989
994
// Simplify (X && Y) || (X && !Y) -> X.
@@ -996,11 +1001,12 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
996
1001
m_LogicalAnd (m_VPValue (X1), m_Not (m_VPValue (Y1))))) &&
997
1002
X == X1 && Y == Y1) {
998
1003
R.getVPSingleValue ()->replaceAllUsesWith (X);
999
- return ;
1004
+ return {} ;
1000
1005
}
1001
1006
1002
1007
if (match (&R, m_c_Mul (m_VPValue (A), m_SpecificInt (1 ))))
1003
- return R.getVPSingleValue ()->replaceAllUsesWith (A);
1008
+ R.getVPSingleValue ()->replaceAllUsesWith (A);
1009
+ return {};
1004
1010
}
1005
1011
1006
1012
// / Try to simplify the recipes in \p Plan.
@@ -1009,8 +1015,16 @@ static void simplifyRecipes(VPlan &Plan, LLVMContext &Ctx) {
1009
1015
Plan.getEntry ());
1010
1016
VPTypeAnalysis TypeInfo (Plan.getCanonicalIV ()->getScalarType (), Ctx);
1011
1017
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(RPOT)) {
1012
- for (VPRecipeBase &R : make_early_inc_range (*VPBB)) {
1013
- simplifyRecipe (R, TypeInfo);
1018
+ // Populate a Worklist, as simplifyRecipe might return a new recipe that we
1019
+ // need to re-process.
1020
+ SmallVector<VPRecipeBase *> Worklist;
1021
+ for (auto &R : VPBB->getRecipeList ())
1022
+ Worklist.push_back (&R);
1023
+
1024
+ while (!Worklist.empty ()) {
1025
+ VPRecipeBase *R = Worklist.pop_back_val ();
1026
+ for (VPRecipeBase *Cand : simplifyRecipe (*R, TypeInfo))
1027
+ Worklist.push_back (Cand);
1014
1028
}
1015
1029
}
1016
1030
}
0 commit comments