@@ -923,7 +923,7 @@ static void recursivelyDeleteDeadRecipes(VPValue *V) {
923
923
}
924
924
925
925
// / Try to simplify recipe \p R.
926
- static void simplifyRecipe (VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
926
+ static VPValue * simplifyRecipe (VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
927
927
using namespace llvm ::VPlanPatternMatch;
928
928
929
929
// VPScalarIVSteps can only be simplified after unrolling. VPScalarIVSteps for
@@ -932,8 +932,7 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
932
932
if (auto *Steps = dyn_cast<VPScalarIVStepsRecipe>(&R)) {
933
933
if (Steps->getParent ()->getPlan ()->isUnrolled () && Steps->isPart0 () &&
934
934
vputils::onlyFirstLaneUsed (Steps)) {
935
- Steps->replaceAllUsesWith (Steps->getOperand (0 ));
936
- return ;
935
+ return Steps->getOperand (0 );
937
936
}
938
937
}
939
938
@@ -943,11 +942,11 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
943
942
Type *TruncTy = TypeInfo.inferScalarType (Trunc);
944
943
Type *ATy = TypeInfo.inferScalarType (A);
945
944
if (TruncTy == ATy) {
946
- Trunc-> replaceAllUsesWith (A) ;
945
+ return A ;
947
946
} else {
948
947
// Don't replace a scalarizing recipe with a widened cast.
949
948
if (isa<VPReplicateRecipe>(&R))
950
- return ;
949
+ return nullptr ;
951
950
if (ATy->getScalarSizeInBits () < TruncTy->getScalarSizeInBits ()) {
952
951
953
952
unsigned ExtOpcode = match (R.getOperand (0 ), m_SExt (m_VPValue ()))
@@ -960,11 +959,11 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
960
959
VPC->setUnderlyingValue (UnderlyingExt);
961
960
}
962
961
VPC->insertBefore (&R);
963
- Trunc-> replaceAllUsesWith ( VPC) ;
962
+ return VPC;
964
963
} else if (ATy->getScalarSizeInBits () > TruncTy->getScalarSizeInBits ()) {
965
964
auto *VPC = new VPWidenCastRecipe (Instruction::Trunc, A, TruncTy);
966
965
VPC->insertBefore (&R);
967
- Trunc-> replaceAllUsesWith ( VPC) ;
966
+ return VPC;
968
967
}
969
968
}
970
969
#ifndef NDEBUG
@@ -988,20 +987,17 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
988
987
VPValue *X, *Y;
989
988
if (match (&R,
990
989
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;
996
992
997
993
if (match (&R, m_Select (m_VPValue (), m_VPValue (X), m_Deferred (X))))
998
- return R. getVPSingleValue ()-> replaceAllUsesWith (X) ;
994
+ return X ;
999
995
1000
996
if (match (&R, m_c_Mul (m_VPValue (A), m_SpecificInt (1 ))))
1001
- return R. getVPSingleValue ()-> replaceAllUsesWith (A) ;
997
+ return A ;
1002
998
1003
999
if (match (&R, m_Not (m_Not (m_VPValue (A)))))
1004
- return R. getVPSingleValue ()-> replaceAllUsesWith (A) ;
1000
+ return A ;
1005
1001
1006
1002
// Remove redundant DerviedIVs, that is 0 + A * 1 -> A and 0 + 0 * x -> 0.
1007
1003
if ((match (&R,
@@ -1010,16 +1006,31 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
1010
1006
m_DerivedIV (m_SpecificInt (0 ), m_SpecificInt (0 ), m_VPValue ()))) &&
1011
1007
TypeInfo.inferScalarType (R.getOperand (1 )) ==
1012
1008
TypeInfo.inferScalarType (R.getVPSingleValue ()))
1013
- return R.getVPSingleValue ()->replaceAllUsesWith (R.getOperand (1 ));
1009
+ return R.getOperand (1 );
1010
+
1011
+ return nullptr ;
1014
1012
}
1015
1013
1016
1014
void VPlanTransforms::simplifyRecipes (VPlan &Plan, Type &CanonicalIVTy) {
1017
1015
ReversePostOrderTraversal<VPBlockDeepTraversalWrapper<VPBlockBase *>> RPOT (
1018
1016
Plan.getEntry ());
1019
1017
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);
1023
1034
}
1024
1035
}
1025
1036
}
0 commit comments