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 @@ -7194,8 +7194,9 @@ // --------------------------------------------------------------------------- // Create a dummy pre-entry VPBasicBlock to start building the VPlan. + auto Plan = std::make_unique(); VPBasicBlock *VPBB = new VPBasicBlock("Pre-Entry"); - auto Plan = std::make_unique(VPBB); + Plan->setEntry(VPBB); // Represent values that will have defs inside VPlan. for (Value *V : NeedDef) 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 @@ -365,6 +365,10 @@ /// Current block predicate - null if the block does not need a predicate. VPValue *Predicate = nullptr; + /// VPlan containing the block. Can only be set on the entry block of the + /// plan. + VPlan *Plan = nullptr; + /// Add \p Successor as the last successor to this block. void appendSuccessor(VPBlockBase *Successor) { assert(Successor && "Cannot add nullptr successor!"); @@ -418,6 +422,14 @@ VPRegionBlock *getParent() { return Parent; } const VPRegionBlock *getParent() const { return Parent; } + /// \return A pointer to the plan containing the current block. + VPlan *getPlan(); + const VPlan *getPlan() const; + + /// Sets the pointer of the plan containing the block. The block must be the + /// entry block into the VPlan. + void setPlan(VPlan *ParentPlan); + void setParent(VPRegionBlock *P) { Parent = P; } /// \return the VPBasicBlock that is the entry of this VPBlockBase, @@ -1402,7 +1414,11 @@ VPBlockBase *getEntry() { return Entry; } const VPBlockBase *getEntry() const { return Entry; } - VPBlockBase *setEntry(VPBlockBase *Block) { return Entry = Block; } + VPBlockBase *setEntry(VPBlockBase *Block) { + Entry = Block; + Block->setPlan(this); + return Entry; + } /// The backedge taken count of the original loop. VPValue *getOrCreateBackedgeTakenCount() { diff --git a/llvm/lib/Transforms/Vectorize/VPlan.cpp b/llvm/lib/Transforms/Vectorize/VPlan.cpp --- a/llvm/lib/Transforms/Vectorize/VPlan.cpp +++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp @@ -56,6 +56,24 @@ return OS; } +// Get the top-most entry block of \p Start. This is the entry block of the +// containing VPlan. +template static T *getPlanEntry(T *Start) { + T *Next = Start; + T *Current = Start; + while ((Next = Next->getParent())) + Current = Next; + + Next = Current; + while ((Next = Next->getSinglePredecessor())) + Current = Next; + return Current; +} + +VPlan *VPBlockBase::getPlan() { return getPlanEntry(this)->Plan; } + +const VPlan *VPBlockBase::getPlan() const { return getPlanEntry(this)->Plan; } + /// \return the VPBasicBlock that is the entry of Block, possibly indirectly. const VPBasicBlock *VPBlockBase::getEntryBasicBlock() const { const VPBlockBase *Block = this; @@ -71,6 +89,12 @@ return cast(Block); } +void VPBlockBase::setPlan(VPlan *ParentPlan) { + assert(ParentPlan->getEntry() == this && + "Can only set plan on its entry block."); + Plan = ParentPlan; +} + /// \return the VPBasicBlock that is the exit of Block, possibly indirectly. const VPBasicBlock *VPBlockBase::getExitBasicBlock() const { const VPBlockBase *Block = this; diff --git a/llvm/unittests/Transforms/Vectorize/VPlanHCFGTest.cpp b/llvm/unittests/Transforms/Vectorize/VPlanHCFGTest.cpp --- a/llvm/unittests/Transforms/Vectorize/VPlanHCFGTest.cpp +++ b/llvm/unittests/Transforms/Vectorize/VPlanHCFGTest.cpp @@ -50,6 +50,7 @@ EXPECT_EQ(7u, VecBB->size()); EXPECT_EQ(2u, VecBB->getNumPredecessors()); EXPECT_EQ(2u, VecBB->getNumSuccessors()); + EXPECT_EQ(&*Plan, VecBB->getPlan()); auto Iter = VecBB->begin(); VPInstruction *Phi = dyn_cast(&*Iter++); diff --git a/llvm/unittests/Transforms/Vectorize/VPlanPredicatorTest.cpp b/llvm/unittests/Transforms/Vectorize/VPlanPredicatorTest.cpp --- a/llvm/unittests/Transforms/Vectorize/VPlanPredicatorTest.cpp +++ b/llvm/unittests/Transforms/Vectorize/VPlanPredicatorTest.cpp @@ -102,6 +102,13 @@ EXPECT_EQ(InnerLoopLinSucc, OuterIf); EXPECT_EQ(OuterIfLinSucc, InnerIf); EXPECT_EQ(InnerIfLinSucc, InnerLoopLatch); + + // Check that the containing VPlan is set correctly. + EXPECT_EQ(&*Plan, InnerLoopLinSucc->getPlan()); + EXPECT_EQ(&*Plan, OuterIfLinSucc->getPlan()); + EXPECT_EQ(&*Plan, InnerIfLinSucc->getPlan()); + EXPECT_EQ(&*Plan, InnerIf->getPlan()); + EXPECT_EQ(&*Plan, InnerLoopLatch->getPlan()); } // Test generation of Not and Or during predication.