@@ -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 ;
@@ -1792,7 +1810,7 @@ sinkRecurrenceUsersAfterPrevious(VPFirstOrderRecurrencePHIRecipe *FOR,
17921810 VPDT.properlyDominates (Previous, SinkCandidate))
17931811 return true ;
17941812
1795- if (SinkCandidate-> mayHaveSideEffects ( ))
1813+ if (cannotHoistOrSinkRecipe (*SinkCandidate ))
17961814 return false ;
17971815
17981816 WorkList.push_back (SinkCandidate);
@@ -1832,7 +1850,7 @@ sinkRecurrenceUsersAfterPrevious(VPFirstOrderRecurrencePHIRecipe *FOR,
18321850static bool hoistPreviousBeforeFORUsers (VPFirstOrderRecurrencePHIRecipe *FOR,
18331851 VPRecipeBase *Previous,
18341852 VPDominatorTree &VPDT) {
1835- if (Previous-> mayHaveSideEffects () || Previous-> mayReadFromMemory ( ))
1853+ if (cannotHoistOrSinkRecipe (* Previous))
18361854 return false ;
18371855
18381856 // Collect recipes that need hoisting.
@@ -1879,11 +1897,6 @@ static bool hoistPreviousBeforeFORUsers(VPFirstOrderRecurrencePHIRecipe *FOR,
18791897 return nullptr ;
18801898 return HoistCandidate;
18811899 };
1882- auto CanHoist = [&](VPRecipeBase *HoistCandidate) {
1883- // Avoid hoisting candidates with side-effects, as we do not yet analyze
1884- // associated dependencies.
1885- return !HoistCandidate->mayHaveSideEffects ();
1886- };
18871900
18881901 if (!NeedsHoisting (Previous->getVPSingleValue ()))
18891902 return true ;
@@ -1895,7 +1908,7 @@ static bool hoistPreviousBeforeFORUsers(VPFirstOrderRecurrencePHIRecipe *FOR,
18951908 VPRecipeBase *Current = HoistCandidates[I];
18961909 assert (Current->getNumDefinedValues () == 1 &&
18971910 " only recipes with a single defined value expected" );
1898- if (! CanHoist ( Current))
1911+ if (cannotHoistOrSinkRecipe (* Current))
18991912 return false ;
19001913
19011914 for (VPValue *Op : Current->operands ()) {
@@ -2133,24 +2146,6 @@ void VPlanTransforms::cse(VPlan &Plan) {
21332146static void licm (VPlan &Plan) {
21342147 VPBasicBlock *Preheader = Plan.getVectorPreheader ();
21352148
2136- // Return true if we do not know how to (mechanically) hoist a given recipe
2137- // out of a loop region.
2138- auto CannotHoistRecipe = [](VPRecipeBase &R) {
2139- // Assumes don't alias anything or throw; as long as they're guaranteed to
2140- // execute, they're safe to hoist.
2141- if (match (&R, m_Intrinsic<Intrinsic::assume>()))
2142- return false ;
2143-
2144- // TODO: Relax checks in the future, e.g. we could also hoist reads, if
2145- // their memory location is not modified in the vector loop.
2146- if (R.mayHaveSideEffects () || R.mayReadFromMemory () || R.isPhi ())
2147- return true ;
2148-
2149- // Allocas cannot be hoisted.
2150- auto *RepR = dyn_cast<VPReplicateRecipe>(&R);
2151- return RepR && RepR->getOpcode () == Instruction::Alloca;
2152- };
2153-
21542149 // Hoist any loop invariant recipes from the vector loop region to the
21552150 // preheader. Preform a shallow traversal of the vector loop region, to
21562151 // exclude recipes in replicate regions. Since the top-level blocks in the
@@ -2162,7 +2157,7 @@ static void licm(VPlan &Plan) {
21622157 for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(
21632158 vp_depth_first_shallow (LoopRegion->getEntry ()))) {
21642159 for (VPRecipeBase &R : make_early_inc_range (*VPBB)) {
2165- if (CannotHoistRecipe (R))
2160+ if (cannotHoistOrSinkRecipe (R))
21662161 continue ;
21672162 if (any_of (R.operands (), [](VPValue *Op) {
21682163 return !Op->isDefinedOutsideLoopRegions ();
0 commit comments