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 @@ -7020,7 +7020,7 @@ PredInst2Recipe[I] = Recipe; VPBlockBase *Region = createReplicateRegion(I, Recipe, Plan); VPBlockUtils::insertBlockAfter(Region, VPBB); - auto *RegSucc = new VPBasicBlock(); + auto *RegSucc = new VPBasicBlock(&*Plan); VPBlockUtils::insertBlockAfter(RegSucc, Region); return RegSucc; } @@ -7038,12 +7038,15 @@ std::string RegionName = (Twine("pred.") + Instr->getOpcodeName()).str(); assert(Instr->getParent() && "Predicated instruction not in any basic block"); auto *BOMRecipe = new VPBranchOnMaskRecipe(BlockInMask); - auto *Entry = new VPBasicBlock(Twine(RegionName) + ".entry", BOMRecipe); + auto *Entry = + new VPBasicBlock(&*Plan, Twine(RegionName) + ".entry", BOMRecipe); auto *PHIRecipe = Instr->getType()->isVoidTy() ? nullptr : new VPPredInstPHIRecipe(Instr); - auto *Exit = new VPBasicBlock(Twine(RegionName) + ".continue", PHIRecipe); - auto *Pred = new VPBasicBlock(Twine(RegionName) + ".if", PredRecipe); - VPRegionBlock *Region = new VPRegionBlock(Entry, Exit, RegionName, true); + auto *Exit = + new VPBasicBlock(&*Plan, Twine(RegionName) + ".continue", PHIRecipe); + auto *Pred = new VPBasicBlock(&*Plan, Twine(RegionName) + ".if", PredRecipe); + VPRegionBlock *Region = + new VPRegionBlock(&*Plan, Entry, Exit, RegionName, true); // Note: first set Entry as region entry and then connect successors starting // from it in order, to propagate the "parent" of each VPBasicBlock. @@ -7200,8 +7203,9 @@ // --------------------------------------------------------------------------- // Create a dummy pre-entry VPBasicBlock to start building the VPlan. - VPBasicBlock *VPBB = new VPBasicBlock("Pre-Entry"); - auto Plan = std::make_unique(VPBB); + auto Plan = std::make_unique(); + VPBasicBlock *VPBB = new VPBasicBlock(&*Plan, "Pre-Entry"); + Plan->setEntry(VPBB); // Represent values that will have defs inside VPlan. for (Value *V : NeedDef) @@ -7216,7 +7220,7 @@ // Relevant instructions from basic block BB will be grouped into VPRecipe // ingredients and fill a new VPBasicBlock. unsigned VPBBsForBB = 0; - auto *FirstVPBBForBB = new VPBasicBlock(BB->getName()); + auto *FirstVPBBForBB = new VPBasicBlock(&*Plan, BB->getName()); VPBlockUtils::insertBlockAfter(FirstVPBBForBB, VPBB); VPBB = FirstVPBBForBB; Builder.setInsertPoint(VPBB); 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,8 @@ /// Current block predicate - null if the block does not need a predicate. VPValue *Predicate = nullptr; + VPlan *ParentPlan; + /// Add \p Successor as the last successor to this block. void appendSuccessor(VPBlockBase *Successor) { assert(Successor && "Cannot add nullptr successor!"); @@ -392,8 +394,8 @@ } protected: - VPBlockBase(const unsigned char SC, const std::string &N) - : SubclassID(SC), Name(N) {} + VPBlockBase(const unsigned char SC, VPlan *ParentPlan, const std::string &N) + : SubclassID(SC), Name(N), ParentPlan(ParentPlan) {} public: /// An enumeration for keeping track of the concrete subclass of VPBlockBase @@ -418,6 +420,9 @@ VPRegionBlock *getParent() { return Parent; } const VPRegionBlock *getParent() const { return Parent; } + VPlan *getParentPlan() { return ParentPlan; } + const VPlan *getParentPlan() const { return ParentPlan; } + void setParent(VPRegionBlock *P) { Parent = P; } /// \return the VPBasicBlock that is the entry of this VPBlockBase, @@ -1067,8 +1072,9 @@ RecipeListTy Recipes; public: - VPBasicBlock(const Twine &Name = "", VPRecipeBase *Recipe = nullptr) - : VPBlockBase(VPBasicBlockSC, Name.str()) { + VPBasicBlock(VPlan *ParentPlan = nullptr, const Twine &Name = "", + VPRecipeBase *Recipe = nullptr) + : VPBlockBase(VPBasicBlockSC, ParentPlan, Name.str()) { if (Recipe) appendRecipe(Recipe); } @@ -1156,18 +1162,19 @@ bool IsReplicator; public: - VPRegionBlock(VPBlockBase *Entry, VPBlockBase *Exit, + VPRegionBlock(VPlan *ParentPlan, VPBlockBase *Entry, VPBlockBase *Exit, const std::string &Name = "", bool IsReplicator = false) - : VPBlockBase(VPRegionBlockSC, Name), Entry(Entry), Exit(Exit), - IsReplicator(IsReplicator) { + : VPBlockBase(VPRegionBlockSC, ParentPlan, Name), Entry(Entry), + Exit(Exit), IsReplicator(IsReplicator) { assert(Entry->getPredecessors().empty() && "Entry block has predecessors."); assert(Exit->getSuccessors().empty() && "Exit block has successors."); Entry->setParent(this); Exit->setParent(this); } - VPRegionBlock(const std::string &Name = "", bool IsReplicator = false) - : VPBlockBase(VPRegionBlockSC, Name), Entry(nullptr), Exit(nullptr), - IsReplicator(IsReplicator) {} + VPRegionBlock(VPlan *ParentPlan = nullptr, const std::string &Name = "", + bool IsReplicator = false) + : VPBlockBase(VPRegionBlockSC, ParentPlan, Name), Entry(nullptr), + Exit(nullptr), IsReplicator(IsReplicator) {} ~VPRegionBlock() override { if (Entry) diff --git a/llvm/lib/Transforms/Vectorize/VPlanHCFGBuilder.cpp b/llvm/lib/Transforms/Vectorize/VPlanHCFGBuilder.cpp --- a/llvm/lib/Transforms/Vectorize/VPlanHCFGBuilder.cpp +++ b/llvm/lib/Transforms/Vectorize/VPlanHCFGBuilder.cpp @@ -114,7 +114,7 @@ // Create new VPBB. LLVM_DEBUG(dbgs() << "Creating VPBasicBlock for " << BB->getName() << "\n"); - VPBasicBlock *VPBB = new VPBasicBlock(BB->getName()); + VPBasicBlock *VPBB = new VPBasicBlock(&Plan, BB->getName()); BB2VPBB[BB] = VPBB; VPBB->setParent(TopRegion); return VPBB; @@ -238,7 +238,7 @@ // Main interface to build the plain CFG. VPRegionBlock *PlainCFGBuilder::buildPlainCFG() { // 1. Create the Top Region. It will be the parent of all VPBBs. - TopRegion = new VPRegionBlock("TopRegion", false /*isReplicator*/); + TopRegion = new VPRegionBlock(&Plan, "TopRegion", false /*isReplicator*/); // 2. Scan the body of the loop in a topological order to visit each basic // block after having visited its predecessor basic blocks. Create a VPBB for