@@ -2371,7 +2371,7 @@ void InnerLoopVectorizer::scalarizeInstruction(const Instruction *Instr,
23712371 InputLane = VPLane::getFirstLane ();
23722372 Cloned->setOperand (I.index (), State.get (Operand, InputLane));
23732373 }
2374- State. addNewMetadata (Cloned, Instr );
2374+ RepRecipe-> applyMetadata (Cloned);
23752375
23762376 // Place the cloned scalar in the new loop.
23772377 State.Builder .Insert (Cloned);
@@ -7989,24 +7989,6 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
79897989 if (VectorizingEpilogue)
79907990 VPlanTransforms::removeDeadRecipes (BestVPlan);
79917991
7992- // Only use noalias metadata when using memory checks guaranteeing no overlap
7993- // across all iterations.
7994- const LoopAccessInfo *LAI = ILV.Legal ->getLAI ();
7995- std::unique_ptr<LoopVersioning> LVer = nullptr ;
7996- if (LAI && !LAI->getRuntimePointerChecking ()->getChecks ().empty () &&
7997- !LAI->getRuntimePointerChecking ()->getDiffChecks ()) {
7998-
7999- // We currently don't use LoopVersioning for the actual loop cloning but we
8000- // still use it to add the noalias metadata.
8001- // TODO: Find a better way to re-use LoopVersioning functionality to add
8002- // metadata.
8003- LVer = std::make_unique<LoopVersioning>(
8004- *LAI, LAI->getRuntimePointerChecking ()->getChecks (), OrigLoop, LI, DT,
8005- PSE.getSE ());
8006- State.LVer = &*LVer;
8007- State.LVer ->prepareNoAliasMetadata ();
8008- }
8009-
80107992 ILV.printDebugTracesAtStart ();
80117993
80127994 // ===------------------------------------------------===//
@@ -8597,15 +8579,14 @@ VPRecipeBuilder::tryToWidenMemory(Instruction *I, ArrayRef<VPValue *> Operands,
85978579 Builder.insert (VectorPtr);
85988580 Ptr = VectorPtr;
85998581 }
8582+ auto Metadata = getMetadataToPropagate (I);
86008583 if (LoadInst *Load = dyn_cast<LoadInst>(I))
86018584 return new VPWidenLoadRecipe (*Load, Ptr, Mask, Consecutive, Reverse,
8602- getMetadataToPropagate (Load),
8603- I->getDebugLoc ());
8585+ Metadata, I->getDebugLoc ());
86048586
86058587 StoreInst *Store = cast<StoreInst>(I);
86068588 return new VPWidenStoreRecipe (*Store, Ptr, Operands[0 ], Mask, Consecutive,
8607- Reverse, getMetadataToPropagate (Store),
8608- I->getDebugLoc ());
8589+ Reverse, Metadata, I->getDebugLoc ());
86098590}
86108591
86118592// / Creates a VPWidenIntOrFpInductionRecpipe for \p Phi. If needed, it will also
@@ -8985,8 +8966,9 @@ VPRecipeBuilder::handleReplication(Instruction *I, ArrayRef<VPValue *> Operands,
89858966 assert ((Range.Start .isScalar () || !IsUniform || !IsPredicated ||
89868967 (Range.Start .isScalable () && isa<IntrinsicInst>(I))) &&
89878968 " Should not predicate a uniform recipe" );
8988- auto *Recipe = new VPReplicateRecipe (
8989- I, make_range (Operands.begin (), Operands.end ()), IsUniform, BlockInMask);
8969+ auto *Recipe =
8970+ new VPReplicateRecipe (I, make_range (Operands.begin (), Operands.end ()),
8971+ IsUniform, BlockInMask, getMetadataToPropagate (I));
89908972 return Recipe;
89918973}
89928974
@@ -9104,9 +9086,16 @@ bool VPRecipeBuilder::getScaledReductions(
91049086}
91059087
91069088SmallVector<std::pair<unsigned , MDNode *>>
9107- VPRecipeBuilder::getMetadataToPropagate (Instruction *I) {
9089+ VPRecipeBuilder::getMetadataToPropagate (Instruction *I) const {
91089090 SmallVector<std::pair<unsigned , MDNode *>> Metadata;
91099091 ::getMetadataToPropagate (I, Metadata);
9092+ if (LVer && isa<LoadInst, StoreInst>(I)) {
9093+ const auto &[AliasScopeMD, NoAliasMD] = LVer->getNoAliasMetadataFor (I);
9094+ if (AliasScopeMD)
9095+ Metadata.emplace_back (LLVMContext::MD_alias_scope, AliasScopeMD);
9096+ if (NoAliasMD)
9097+ Metadata.emplace_back (LLVMContext::MD_noalias, NoAliasMD);
9098+ }
91109099 return Metadata;
91119100}
91129101
@@ -9239,10 +9228,22 @@ void LoopVectorizationPlanner::buildVPlansWithVPRecipes(ElementCount MinVF,
92399228 ElementCount MaxVF) {
92409229 assert (OrigLoop->isInnermost () && " Inner loop expected." );
92419230
9231+ // Only use noalias metadata when using memory checks guaranteeing no overlap
9232+ // across all iterations.
9233+ const LoopAccessInfo *LAI = Legal->getLAI ();
9234+ std::unique_ptr<LoopVersioning> LVer = nullptr ;
9235+ if (LAI && !LAI->getRuntimePointerChecking ()->getChecks ().empty () &&
9236+ !LAI->getRuntimePointerChecking ()->getDiffChecks ()) {
9237+ LVer = std::make_unique<LoopVersioning>(
9238+ *LAI, LAI->getRuntimePointerChecking ()->getChecks (), OrigLoop, LI, DT,
9239+ PSE.getSE ());
9240+ LVer->prepareNoAliasMetadata ();
9241+ }
9242+
92429243 auto MaxVFTimes2 = MaxVF * 2 ;
92439244 for (ElementCount VF = MinVF; ElementCount::isKnownLT (VF, MaxVFTimes2);) {
92449245 VFRange SubRange = {VF, MaxVFTimes2};
9245- if (auto Plan = tryToBuildVPlanWithVPRecipes (SubRange)) {
9246+ if (auto Plan = tryToBuildVPlanWithVPRecipes (SubRange, LVer. get () )) {
92469247 bool HasScalarVF = Plan->hasScalarVFOnly ();
92479248 // Now optimize the initial VPlan.
92489249 if (!HasScalarVF)
@@ -9550,7 +9551,8 @@ static void addExitUsersForFirstOrderRecurrences(
95509551}
95519552
95529553VPlanPtr
9553- LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes (VFRange &Range) {
9554+ LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes (VFRange &Range,
9555+ LoopVersioning *LVer) {
95549556
95559557 using namespace llvm ::VPlanPatternMatch;
95569558 SmallPtrSet<const InterleaveGroup<Instruction> *, 1 > InterleaveGroups;
@@ -9596,7 +9598,7 @@ LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range) {
95969598 addCanonicalIVRecipes (*Plan, Legal->getWidestInductionType (), HasNUW, DL);
95979599
95989600 VPRecipeBuilder RecipeBuilder (*Plan, OrigLoop, TLI, &TTI, Legal, CM, PSE,
9599- Builder);
9601+ Builder, LVer );
96009602
96019603 // ---------------------------------------------------------------------------
96029604 // Pre-construction: record ingredients whose recipes we'll need to further
@@ -9710,8 +9712,9 @@ LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range) {
97109712 Legal->isInvariantAddressOfReduction (SI->getPointerOperand ())) {
97119713 // Only create recipe for the final invariant store of the reduction.
97129714 if (Legal->isInvariantStoreOfReduction (SI)) {
9713- auto *Recipe =
9714- new VPReplicateRecipe (SI, R.operands (), true /* IsUniform */ );
9715+ auto *Recipe = new VPReplicateRecipe (
9716+ SI, R.operands (), true /* IsUniform */ , /* Mask*/ nullptr ,
9717+ RecipeBuilder.getMetadataToPropagate (SI));
97159718 Recipe->insertBefore (*MiddleVPBB, MBIP);
97169719 }
97179720 R.eraseFromParent ();
@@ -9897,7 +9900,7 @@ VPlanPtr LoopVectorizationPlanner::tryToBuildVPlan(VFRange &Range) {
98979900 // Collect mapping of IR header phis to header phi recipes, to be used in
98989901 // addScalarResumePhis.
98999902 VPRecipeBuilder RecipeBuilder (*Plan, OrigLoop, TLI, &TTI, Legal, CM, PSE,
9900- Builder);
9903+ Builder, nullptr );
99019904 for (auto &R : Plan->getVectorLoopRegion ()->getEntryBasicBlock ()->phis ()) {
99029905 if (isa<VPCanonicalIVPHIRecipe>(&R))
99039906 continue ;
0 commit comments