@@ -130,6 +130,24 @@ bool VPlanTransforms::tryToConvertVPInstructionsToVPRecipes(
130130 return true ;
131131}
132132
133+ // / Return true if we do not know how to (mechanically) hoist or sink \p R out
134+ // / of a loop region.
135+ static bool cannotHoistOrSinkRecipe (const VPRecipeBase &R) {
136+ // Assumes don't alias anything or throw; as long as they're guaranteed to
137+ // execute, they're safe to hoist.
138+ if (match (&R, m_Intrinsic<Intrinsic::assume>()))
139+ return false ;
140+
141+ // TODO: Relax checks in the future, e.g. we could also hoist reads, if their
142+ // memory location is not modified in the vector loop.
143+ if (R.mayHaveSideEffects () || R.mayReadFromMemory () || R.isPhi ())
144+ return true ;
145+
146+ // Allocas cannot be hoisted.
147+ auto *RepR = dyn_cast<VPReplicateRecipe>(&R);
148+ return RepR && RepR->getOpcode () == Instruction::Alloca;
149+ }
150+
133151static bool sinkScalarOperands (VPlan &Plan) {
134152 auto Iter = vp_depth_first_deep (Plan.getEntry ());
135153 bool Changed = false ;
@@ -1795,7 +1813,7 @@ sinkRecurrenceUsersAfterPrevious(VPFirstOrderRecurrencePHIRecipe *FOR,
17951813 VPDT.properlyDominates (Previous, SinkCandidate))
17961814 return true ;
17971815
1798- if (SinkCandidate-> mayHaveSideEffects ( ))
1816+ if (cannotHoistOrSinkRecipe (*SinkCandidate ))
17991817 return false ;
18001818
18011819 WorkList.push_back (SinkCandidate);
@@ -1835,7 +1853,7 @@ sinkRecurrenceUsersAfterPrevious(VPFirstOrderRecurrencePHIRecipe *FOR,
18351853static bool hoistPreviousBeforeFORUsers (VPFirstOrderRecurrencePHIRecipe *FOR,
18361854 VPRecipeBase *Previous,
18371855 VPDominatorTree &VPDT) {
1838- if (Previous-> mayHaveSideEffects () || Previous-> mayReadFromMemory ( ))
1856+ if (cannotHoistOrSinkRecipe (* Previous))
18391857 return false ;
18401858
18411859 // Collect recipes that need hoisting.
@@ -1882,11 +1900,6 @@ static bool hoistPreviousBeforeFORUsers(VPFirstOrderRecurrencePHIRecipe *FOR,
18821900 return nullptr ;
18831901 return HoistCandidate;
18841902 };
1885- auto CanHoist = [&](VPRecipeBase *HoistCandidate) {
1886- // Avoid hoisting candidates with side-effects, as we do not yet analyze
1887- // associated dependencies.
1888- return !HoistCandidate->mayHaveSideEffects ();
1889- };
18901903
18911904 if (!NeedsHoisting (Previous->getVPSingleValue ()))
18921905 return true ;
@@ -1898,7 +1911,7 @@ static bool hoistPreviousBeforeFORUsers(VPFirstOrderRecurrencePHIRecipe *FOR,
18981911 VPRecipeBase *Current = HoistCandidates[I];
18991912 assert (Current->getNumDefinedValues () == 1 &&
19001913 " only recipes with a single defined value expected" );
1901- if (! CanHoist ( Current))
1914+ if (cannotHoistOrSinkRecipe (* Current))
19021915 return false ;
19031916
19041917 for (VPValue *Op : Current->operands ()) {
@@ -2127,24 +2140,6 @@ void VPlanTransforms::cse(VPlan &Plan) {
21272140static void licm (VPlan &Plan) {
21282141 VPBasicBlock *Preheader = Plan.getVectorPreheader ();
21292142
2130- // Return true if we do not know how to (mechanically) hoist a given recipe
2131- // out of a loop region.
2132- auto CannotHoistRecipe = [](VPRecipeBase &R) {
2133- // Assumes don't alias anything or throw; as long as they're guaranteed to
2134- // execute, they're safe to hoist.
2135- if (match (&R, m_Intrinsic<Intrinsic::assume>()))
2136- return false ;
2137-
2138- // TODO: Relax checks in the future, e.g. we could also hoist reads, if
2139- // their memory location is not modified in the vector loop.
2140- if (R.mayHaveSideEffects () || R.mayReadFromMemory () || R.isPhi ())
2141- return true ;
2142-
2143- // Allocas cannot be hoisted.
2144- auto *RepR = dyn_cast<VPReplicateRecipe>(&R);
2145- return RepR && RepR->getOpcode () == Instruction::Alloca;
2146- };
2147-
21482143 // Hoist any loop invariant recipes from the vector loop region to the
21492144 // preheader. Preform a shallow traversal of the vector loop region, to
21502145 // exclude recipes in replicate regions. Since the top-level blocks in the
@@ -2156,7 +2151,7 @@ static void licm(VPlan &Plan) {
21562151 for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(
21572152 vp_depth_first_shallow (LoopRegion->getEntry ()))) {
21582153 for (VPRecipeBase &R : make_early_inc_range (*VPBB)) {
2159- if (CannotHoistRecipe (R))
2154+ if (cannotHoistOrSinkRecipe (R))
21602155 continue ;
21612156 if (any_of (R.operands (), [](VPValue *Op) {
21622157 return !Op->isDefinedOutsideLoopRegions ();
0 commit comments