@@ -2122,27 +2122,32 @@ static void licm(VPlan &Plan) {
21222122 VPBasicBlock *Preheader = Plan.getVectorPreheader ();
21232123
21242124 // Return true if we do not know how to (mechanically) hoist a given recipe
2125- // out of a loop region. Does not address legality concerns such as aliasing
2126- // or speculation safety.
2125+ // out of a loop region.
21272126 auto CannotHoistRecipe = [](VPRecipeBase &R) {
2127+ // TODO: Relax checks in the future, e.g. we could also hoist reads, if
2128+ // their memory location is not modified in the vector loop.
2129+ if (R.mayHaveSideEffects () || R.mayReadFromMemory () || R.isPhi ())
2130+ return true ;
2131+
21282132 // Allocas cannot be hoisted.
21292133 auto *RepR = dyn_cast<VPReplicateRecipe>(&R);
21302134 return RepR && RepR->getOpcode () == Instruction::Alloca;
21312135 };
21322136
21332137 // Hoist any loop invariant recipes from the vector loop region to the
21342138 // preheader. Preform a shallow traversal of the vector loop region, to
2135- // exclude recipes in replicate regions.
2139+ // exclude recipes in replicate regions. Since the top-level blocks in the
2140+ // vector loop region are guaranteed to execute if the vector pre-header is,
2141+ // we don't need to check speculation safety.
21362142 VPRegionBlock *LoopRegion = Plan.getVectorLoopRegion ();
2143+ assert (Preheader->getSingleSuccessor () == LoopRegion &&
2144+ " Expected vector prehader's successor to be the vector loop region" );
21372145 for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(
21382146 vp_depth_first_shallow (LoopRegion->getEntry ()))) {
21392147 for (VPRecipeBase &R : make_early_inc_range (*VPBB)) {
21402148 if (CannotHoistRecipe (R))
21412149 continue ;
2142- // TODO: Relax checks in the future, e.g. we could also hoist reads, if
2143- // their memory location is not modified in the vector loop.
2144- if (R.mayHaveSideEffects () || R.mayReadFromMemory () || R.isPhi () ||
2145- any_of (R.operands (), [](VPValue *Op) {
2150+ if (any_of (R.operands (), [](VPValue *Op) {
21462151 return !Op->isDefinedOutsideLoopRegions ();
21472152 }))
21482153 continue ;
0 commit comments