diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp --- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp +++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp @@ -4068,13 +4068,10 @@ // original loop is widened to a vector form so we can use them to construct // the incoming edges. VPBasicBlock *Header = State.Plan->getEntry()->getEntryBasicBlock(); - for (VPRecipeBase &R : *Header) { - if (isa(&R)) - continue; + for (VPRecipeBase &R : Header->phis()) { VPWidenPHIRecipe *PhiR = dyn_cast(&R); if (!PhiR) - break; - + continue; auto *OrigPhi = cast(PhiR->getUnderlyingValue()); if (PhiR->getRecurrenceDescriptor()) { fixReduction(PhiR, State); diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h b/llvm/lib/Transforms/Vectorize/VPlan.h --- a/llvm/lib/Transforms/Vectorize/VPlan.h +++ b/llvm/lib/Transforms/Vectorize/VPlan.h @@ -1475,6 +1475,18 @@ inline const VPRecipeBase &back() const { return Recipes.back(); } inline VPRecipeBase &back() { return Recipes.back(); } + /// Returns an iterator range over the PHI-like recipes in the block. + iterator_range phis() { + auto FirstNonPhi = begin(); + for (auto E = end(); FirstNonPhi != E; ++FirstNonPhi) { + VPRecipeBase *R = &*FirstNonPhi; + if (!isa(R) && !isa(R)) + break; + } + + return make_range(begin(), FirstNonPhi); + } + /// Returns a reference to the list of recipes. RecipeListTy &getRecipeList() { return Recipes; }