@@ -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 ;
@@ -1825,7 +1843,7 @@ sinkRecurrenceUsersAfterPrevious(VPFirstOrderRecurrencePHIRecipe *FOR,
18251843 VPDT.properlyDominates (Previous, SinkCandidate))
18261844 return true ;
18271845
1828- if (SinkCandidate-> mayHaveSideEffects ( ))
1846+ if (cannotHoistOrSinkRecipe (*SinkCandidate ))
18291847 return false ;
18301848
18311849 WorkList.push_back (SinkCandidate);
@@ -1865,7 +1883,7 @@ sinkRecurrenceUsersAfterPrevious(VPFirstOrderRecurrencePHIRecipe *FOR,
18651883static bool hoistPreviousBeforeFORUsers (VPFirstOrderRecurrencePHIRecipe *FOR,
18661884 VPRecipeBase *Previous,
18671885 VPDominatorTree &VPDT) {
1868- if (Previous-> mayHaveSideEffects () || Previous-> mayReadFromMemory ( ))
1886+ if (cannotHoistOrSinkRecipe (* Previous))
18691887 return false ;
18701888
18711889 // Collect recipes that need hoisting.
@@ -1912,11 +1930,6 @@ static bool hoistPreviousBeforeFORUsers(VPFirstOrderRecurrencePHIRecipe *FOR,
19121930 return nullptr ;
19131931 return HoistCandidate;
19141932 };
1915- auto CanHoist = [&](VPRecipeBase *HoistCandidate) {
1916- // Avoid hoisting candidates with side-effects, as we do not yet analyze
1917- // associated dependencies.
1918- return !HoistCandidate->mayHaveSideEffects ();
1919- };
19201933
19211934 if (!NeedsHoisting (Previous->getVPSingleValue ()))
19221935 return true ;
@@ -1928,7 +1941,7 @@ static bool hoistPreviousBeforeFORUsers(VPFirstOrderRecurrencePHIRecipe *FOR,
19281941 VPRecipeBase *Current = HoistCandidates[I];
19291942 assert (Current->getNumDefinedValues () == 1 &&
19301943 " only recipes with a single defined value expected" );
1931- if (! CanHoist ( Current))
1944+ if (cannotHoistOrSinkRecipe (* Current))
19321945 return false ;
19331946
19341947 for (VPValue *Op : Current->operands ()) {
@@ -2143,24 +2156,6 @@ void VPlanTransforms::cse(VPlan &Plan) {
21432156static void licm (VPlan &Plan) {
21442157 VPBasicBlock *Preheader = Plan.getVectorPreheader ();
21452158
2146- // Return true if we do not know how to (mechanically) hoist a given recipe
2147- // out of a loop region.
2148- auto CannotHoistRecipe = [](VPRecipeBase &R) {
2149- // Assumes don't alias anything or throw; as long as they're guaranteed to
2150- // execute, they're safe to hoist.
2151- if (match (&R, m_Intrinsic<Intrinsic::assume>()))
2152- return false ;
2153-
2154- // TODO: Relax checks in the future, e.g. we could also hoist reads, if
2155- // their memory location is not modified in the vector loop.
2156- if (R.mayHaveSideEffects () || R.mayReadFromMemory () || R.isPhi ())
2157- return true ;
2158-
2159- // Allocas cannot be hoisted.
2160- auto *RepR = dyn_cast<VPReplicateRecipe>(&R);
2161- return RepR && RepR->getOpcode () == Instruction::Alloca;
2162- };
2163-
21642159 // Hoist any loop invariant recipes from the vector loop region to the
21652160 // preheader. Preform a shallow traversal of the vector loop region, to
21662161 // exclude recipes in replicate regions. Since the top-level blocks in the
@@ -2172,7 +2167,7 @@ static void licm(VPlan &Plan) {
21722167 for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(
21732168 vp_depth_first_shallow (LoopRegion->getEntry ()))) {
21742169 for (VPRecipeBase &R : make_early_inc_range (*VPBB)) {
2175- if (CannotHoistRecipe (R))
2170+ if (cannotHoistOrSinkRecipe (R))
21762171 continue ;
21772172 if (any_of (R.operands (), [](VPValue *Op) {
21782173 return !Op->isDefinedOutsideLoopRegions ();
0 commit comments