@@ -205,11 +205,6 @@ VPBlockBase *VPBlockBase::getEnclosingBlockWithPredecessors() {
205205 return Parent->getEnclosingBlockWithPredecessors ();
206206}
207207
208- void VPBlockBase::deleteCFG (VPBlockBase *Entry) {
209- for (VPBlockBase *Block : to_vector (vp_depth_first_shallow (Entry)))
210- delete Block;
211- }
212-
213208VPBasicBlock::iterator VPBasicBlock::getFirstNonPhi () {
214209 iterator It = begin ();
215210 while (It != end () && It->isPhi ())
@@ -474,6 +469,16 @@ void VPIRBasicBlock::execute(VPTransformState *State) {
474469 connectToPredecessors (State->CFG );
475470}
476471
472+ VPIRBasicBlock *VPIRBasicBlock::clone () {
473+ auto *NewBlock = getPlan ()->createVPIRBasicBlock (IRBB);
474+ for (VPRecipeBase &R : make_early_inc_range (*NewBlock))
475+ R.eraseFromParent ();
476+
477+ for (VPRecipeBase &R : Recipes)
478+ NewBlock->appendRecipe (R.clone ());
479+ return NewBlock;
480+ }
481+
477482void VPBasicBlock::execute (VPTransformState *State) {
478483 bool Replica = bool (State->Lane );
479484 BasicBlock *NewBB = State->CFG .PrevBB ; // Reuse it if possible.
@@ -523,6 +528,13 @@ void VPBasicBlock::dropAllReferences(VPValue *NewValue) {
523528 }
524529}
525530
531+ VPBasicBlock *VPBasicBlock::clone () {
532+ auto *NewBlock = getPlan ()->createVPBasicBlock (getName ());
533+ for (VPRecipeBase &R : *this )
534+ NewBlock->appendRecipe (R.clone ());
535+ return NewBlock;
536+ }
537+
526538void VPBasicBlock::executeRecipes (VPTransformState *State, BasicBlock *BB) {
527539 LLVM_DEBUG (dbgs () << " LV: vectorizing VPBB:" << getName ()
528540 << " in BB:" << BB->getName () << ' \n ' );
@@ -541,7 +553,7 @@ VPBasicBlock *VPBasicBlock::splitAt(iterator SplitAt) {
541553
542554 SmallVector<VPBlockBase *, 2 > Succs (successors ());
543555 // Create new empty block after the block to split.
544- auto *SplitBlock = new VPBasicBlock (getName () + " .split" );
556+ auto *SplitBlock = getPlan ()-> createVPBasicBlock (getName () + " .split" );
545557 VPBlockUtils::insertBlockAfter (SplitBlock, this );
546558
547559 // Finally, move the recipes starting at SplitAt to new block.
@@ -701,8 +713,8 @@ static std::pair<VPBlockBase *, VPBlockBase *> cloneFrom(VPBlockBase *Entry) {
701713
702714VPRegionBlock *VPRegionBlock::clone () {
703715 const auto &[NewEntry, NewExiting] = cloneFrom (getEntry ());
704- auto *NewRegion =
705- new VPRegionBlock (NewEntry, NewExiting, getName (), isReplicator ());
716+ auto *NewRegion = getPlan ()-> createVPRegionBlock (NewEntry, NewExiting,
717+ getName (), isReplicator ());
706718 for (VPBlockBase *Block : vp_depth_first_shallow (NewEntry))
707719 Block->setParent (NewRegion);
708720 return NewRegion;
@@ -822,32 +834,27 @@ void VPRegionBlock::print(raw_ostream &O, const Twine &Indent,
822834#endif
823835
824836VPlan::VPlan (Loop *L) {
825- setEntry (VPIRBasicBlock::fromBasicBlock (L->getLoopPreheader ()));
826- ScalarHeader = VPIRBasicBlock::fromBasicBlock (L->getHeader ());
837+ setEntry (createVPIRBasicBlock (L->getLoopPreheader ()));
838+ ScalarHeader = createVPIRBasicBlock (L->getHeader ());
827839}
828840
829841VPlan::~VPlan () {
830842 if (Entry) {
831843 VPValue DummyValue;
832- for (VPBlockBase *Block : vp_depth_first_shallow (Entry))
833- Block->dropAllReferences (&DummyValue);
834844
835- VPBlockBase::deleteCFG (Entry);
845+ for (auto *VPB : reverse (CreatedBlocks))
846+ VPB->dropAllReferences (&DummyValue);
847+
848+ for (auto *VPB : reverse (CreatedBlocks)) {
849+ delete VPB;
850+ }
836851 }
837852 for (VPValue *VPV : VPLiveInsToFree)
838853 delete VPV;
839854 if (BackedgeTakenCount)
840855 delete BackedgeTakenCount;
841856}
842857
843- VPIRBasicBlock *VPIRBasicBlock::fromBasicBlock (BasicBlock *IRBB) {
844- auto *VPIRBB = new VPIRBasicBlock (IRBB);
845- for (Instruction &I :
846- make_range (IRBB->begin (), IRBB->getTerminator ()->getIterator ()))
847- VPIRBB->appendRecipe (new VPIRInstruction (I));
848- return VPIRBB;
849- }
850-
851858VPlanPtr VPlan::createInitialVPlan (Type *InductionTy,
852859 PredicatedScalarEvolution &PSE,
853860 bool RequiresScalarEpilogueCheck,
@@ -861,7 +868,7 @@ VPlanPtr VPlan::createInitialVPlan(Type *InductionTy,
861868 // an epilogue vector loop, the original entry block here will be replaced by
862869 // a new VPIRBasicBlock wrapping the entry to the epilogue vector loop after
863870 // generating code for the main vector loop.
864- VPBasicBlock *VecPreheader = new VPBasicBlock (" vector.ph" );
871+ VPBasicBlock *VecPreheader = Plan-> createVPBasicBlock (" vector.ph" );
865872 VPBlockUtils::connectBlocks (Plan->getEntry (), VecPreheader);
866873
867874 // Create SCEV and VPValue for the trip count.
@@ -878,17 +885,17 @@ VPlanPtr VPlan::createInitialVPlan(Type *InductionTy,
878885
879886 // Create VPRegionBlock, with empty header and latch blocks, to be filled
880887 // during processing later.
881- VPBasicBlock *HeaderVPBB = new VPBasicBlock (" vector.body" );
882- VPBasicBlock *LatchVPBB = new VPBasicBlock (" vector.latch" );
888+ VPBasicBlock *HeaderVPBB = Plan-> createVPBasicBlock (" vector.body" );
889+ VPBasicBlock *LatchVPBB = Plan-> createVPBasicBlock (" vector.latch" );
883890 VPBlockUtils::insertBlockAfter (LatchVPBB, HeaderVPBB);
884- auto *TopRegion = new VPRegionBlock (HeaderVPBB, LatchVPBB, " vector loop " ,
885- false /* isReplicator*/ );
891+ auto *TopRegion = Plan-> createVPRegionBlock (
892+ HeaderVPBB, LatchVPBB, " vector loop " , false /* isReplicator*/ );
886893
887894 VPBlockUtils::insertBlockAfter (TopRegion, VecPreheader);
888- VPBasicBlock *MiddleVPBB = new VPBasicBlock (" middle.block" );
895+ VPBasicBlock *MiddleVPBB = Plan-> createVPBasicBlock (" middle.block" );
889896 VPBlockUtils::insertBlockAfter (MiddleVPBB, TopRegion);
890897
891- VPBasicBlock *ScalarPH = new VPBasicBlock (" scalar.ph" );
898+ VPBasicBlock *ScalarPH = Plan-> createVPBasicBlock (" scalar.ph" );
892899 VPBlockUtils::connectBlocks (ScalarPH, ScalarHeader);
893900 if (!RequiresScalarEpilogueCheck) {
894901 VPBlockUtils::connectBlocks (MiddleVPBB, ScalarPH);
@@ -904,7 +911,7 @@ VPlanPtr VPlan::createInitialVPlan(Type *InductionTy,
904911 // we unconditionally branch to the scalar preheader. Do nothing.
905912 // 3) Otherwise, construct a runtime check.
906913 BasicBlock *IRExitBlock = TheLoop->getUniqueLatchExitBlock ();
907- auto *VPExitBlock = VPIRBasicBlock::fromBasicBlock (IRExitBlock);
914+ auto *VPExitBlock = Plan-> createVPIRBasicBlock (IRExitBlock);
908915 // The connection order corresponds to the operands of the conditional branch.
909916 VPBlockUtils::insertBlockAfter (VPExitBlock, MiddleVPBB);
910917 VPBlockUtils::connectBlocks (MiddleVPBB, ScalarPH);
@@ -960,15 +967,13 @@ void VPlan::prepareToExecute(Value *TripCountV, Value *VectorTripCountV,
960967// / have a single predecessor, which is rewired to the new VPIRBasicBlock. All
961968// / successors of VPBB, if any, are rewired to the new VPIRBasicBlock.
962969static void replaceVPBBWithIRVPBB (VPBasicBlock *VPBB, BasicBlock *IRBB) {
963- VPIRBasicBlock *IRVPBB = VPIRBasicBlock::fromBasicBlock (IRBB);
970+ VPIRBasicBlock *IRVPBB = VPBB-> getPlan ()-> createVPIRBasicBlock (IRBB);
964971 for (auto &R : make_early_inc_range (*VPBB)) {
965972 assert (!R.isPhi () && " Tried to move phi recipe to end of block" );
966973 R.moveBefore (*IRVPBB, IRVPBB->end ());
967974 }
968975
969976 VPBlockUtils::reassociateBlocks (VPBB, IRVPBB);
970-
971- delete VPBB;
972977}
973978
974979// / Generate the code inside the preheader and body of the vectorized loop.
@@ -1217,6 +1222,7 @@ static void remapOperands(VPBlockBase *Entry, VPBlockBase *NewEntry,
12171222}
12181223
12191224VPlan *VPlan::duplicate () {
1225+ unsigned CreatedBlockSize = CreatedBlocks.size ();
12201226 // Clone blocks.
12211227 const auto &[NewEntry, __] = cloneFrom (Entry);
12221228
@@ -1257,9 +1263,23 @@ VPlan *VPlan::duplicate() {
12571263 assert (Old2NewVPValues.contains (TripCount) &&
12581264 " TripCount must have been added to Old2NewVPValues" );
12591265 NewPlan->TripCount = Old2NewVPValues[TripCount];
1266+
1267+ for (unsigned I = CreatedBlockSize; I != CreatedBlocks.size (); ++I)
1268+ NewPlan->CreatedBlocks .push_back (CreatedBlocks[I]);
1269+ CreatedBlocks.truncate (CreatedBlockSize);
1270+
12601271 return NewPlan;
12611272}
12621273
1274+ VPIRBasicBlock *VPlan::createVPIRBasicBlock (BasicBlock *IRBB) {
1275+ auto *VPIRBB = new VPIRBasicBlock (IRBB);
1276+ for (Instruction &I :
1277+ make_range (IRBB->begin (), IRBB->getTerminator ()->getIterator ()))
1278+ VPIRBB->appendRecipe (new VPIRInstruction (I));
1279+ CreatedBlocks.push_back (VPIRBB);
1280+ return VPIRBB;
1281+ }
1282+
12631283#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
12641284
12651285Twine VPlanPrinter::getUID (const VPBlockBase *Block) {
0 commit comments