@@ -205,11 +205,6 @@ VPBlockBase *VPBlockBase::getEnclosingBlockWithPredecessors() {
205
205
return Parent->getEnclosingBlockWithPredecessors ();
206
206
}
207
207
208
- void VPBlockBase::deleteCFG (VPBlockBase *Entry) {
209
- for (VPBlockBase *Block : to_vector (vp_depth_first_shallow (Entry)))
210
- delete Block;
211
- }
212
-
213
208
VPBasicBlock::iterator VPBasicBlock::getFirstNonPhi () {
214
209
iterator It = begin ();
215
210
while (It != end () && It->isPhi ())
@@ -474,6 +469,13 @@ void VPIRBasicBlock::execute(VPTransformState *State) {
474
469
connectToPredecessors (State->CFG );
475
470
}
476
471
472
+ VPIRBasicBlock *VPIRBasicBlock::clone () {
473
+ auto *NewBlock = getPlan ()->createEmptyVPIRBasicBlock (IRBB);
474
+ for (VPRecipeBase &R : Recipes)
475
+ NewBlock->appendRecipe (R.clone ());
476
+ return NewBlock;
477
+ }
478
+
477
479
void VPBasicBlock::execute (VPTransformState *State) {
478
480
bool Replica = bool (State->Lane );
479
481
BasicBlock *NewBB = State->CFG .PrevBB ; // Reuse it if possible.
@@ -513,14 +515,11 @@ void VPBasicBlock::execute(VPTransformState *State) {
513
515
executeRecipes (State, NewBB);
514
516
}
515
517
516
- void VPBasicBlock::dropAllReferences (VPValue *NewValue) {
517
- for (VPRecipeBase &R : Recipes) {
518
- for (auto *Def : R.definedValues ())
519
- Def->replaceAllUsesWith (NewValue);
520
-
521
- for (unsigned I = 0 , E = R.getNumOperands (); I != E; I++)
522
- R.setOperand (I, NewValue);
523
- }
518
+ VPBasicBlock *VPBasicBlock::clone () {
519
+ auto *NewBlock = getPlan ()->createVPBasicBlock (getName ());
520
+ for (VPRecipeBase &R : *this )
521
+ NewBlock->appendRecipe (R.clone ());
522
+ return NewBlock;
524
523
}
525
524
526
525
void VPBasicBlock::executeRecipes (VPTransformState *State, BasicBlock *BB) {
@@ -541,7 +540,7 @@ VPBasicBlock *VPBasicBlock::splitAt(iterator SplitAt) {
541
540
542
541
SmallVector<VPBlockBase *, 2 > Succs (successors ());
543
542
// Create new empty block after the block to split.
544
- auto *SplitBlock = new VPBasicBlock (getName () + " .split" );
543
+ auto *SplitBlock = getPlan ()-> createVPBasicBlock (getName () + " .split" );
545
544
VPBlockUtils::insertBlockAfter (SplitBlock, this );
546
545
547
546
// Finally, move the recipes starting at SplitAt to new block.
@@ -701,20 +700,13 @@ static std::pair<VPBlockBase *, VPBlockBase *> cloneFrom(VPBlockBase *Entry) {
701
700
702
701
VPRegionBlock *VPRegionBlock::clone () {
703
702
const auto &[NewEntry, NewExiting] = cloneFrom (getEntry ());
704
- auto *NewRegion =
705
- new VPRegionBlock (NewEntry, NewExiting, getName (), isReplicator ());
703
+ auto *NewRegion = getPlan ()-> createVPRegionBlock (NewEntry, NewExiting,
704
+ getName (), isReplicator ());
706
705
for (VPBlockBase *Block : vp_depth_first_shallow (NewEntry))
707
706
Block->setParent (NewRegion);
708
707
return NewRegion;
709
708
}
710
709
711
- void VPRegionBlock::dropAllReferences (VPValue *NewValue) {
712
- for (VPBlockBase *Block : vp_depth_first_shallow (Entry))
713
- // Drop all references in VPBasicBlocks and replace all uses with
714
- // DummyValue.
715
- Block->dropAllReferences (NewValue);
716
- }
717
-
718
710
void VPRegionBlock::execute (VPTransformState *State) {
719
711
ReversePostOrderTraversal<VPBlockShallowTraversalWrapper<VPBlockBase *>>
720
712
RPOT (Entry);
@@ -822,32 +814,33 @@ void VPRegionBlock::print(raw_ostream &O, const Twine &Indent,
822
814
#endif
823
815
824
816
VPlan::VPlan (Loop *L) {
825
- setEntry (VPIRBasicBlock::fromBasicBlock (L->getLoopPreheader ()));
826
- ScalarHeader = VPIRBasicBlock::fromBasicBlock (L->getHeader ());
817
+ setEntry (createVPIRBasicBlock (L->getLoopPreheader ()));
818
+ ScalarHeader = createVPIRBasicBlock (L->getHeader ());
827
819
}
828
820
829
821
VPlan::~VPlan () {
830
- if (Entry) {
831
- VPValue DummyValue;
832
- for (VPBlockBase *Block : vp_depth_first_shallow (Entry))
833
- Block->dropAllReferences (&DummyValue);
834
-
835
- VPBlockBase::deleteCFG (Entry);
822
+ VPValue DummyValue;
823
+
824
+ for (auto *VPB : CreatedBlocks) {
825
+ if (auto *VPBB = dyn_cast<VPBasicBlock>(VPB)) {
826
+ // Replace all operands of recipes and all VPValues defined in VPBB with
827
+ // DummyValue so the block can be deleted.
828
+ for (VPRecipeBase &R : *VPBB) {
829
+ for (auto *Def : R.definedValues ())
830
+ Def->replaceAllUsesWith (&DummyValue);
831
+
832
+ for (unsigned I = 0 , E = R.getNumOperands (); I != E; I++)
833
+ R.setOperand (I, &DummyValue);
834
+ }
835
+ }
836
+ delete VPB;
836
837
}
837
838
for (VPValue *VPV : VPLiveInsToFree)
838
839
delete VPV;
839
840
if (BackedgeTakenCount)
840
841
delete BackedgeTakenCount;
841
842
}
842
843
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
-
851
844
VPlanPtr VPlan::createInitialVPlan (Type *InductionTy,
852
845
PredicatedScalarEvolution &PSE,
853
846
bool RequiresScalarEpilogueCheck,
@@ -861,7 +854,7 @@ VPlanPtr VPlan::createInitialVPlan(Type *InductionTy,
861
854
// an epilogue vector loop, the original entry block here will be replaced by
862
855
// a new VPIRBasicBlock wrapping the entry to the epilogue vector loop after
863
856
// generating code for the main vector loop.
864
- VPBasicBlock *VecPreheader = new VPBasicBlock (" vector.ph" );
857
+ VPBasicBlock *VecPreheader = Plan-> createVPBasicBlock (" vector.ph" );
865
858
VPBlockUtils::connectBlocks (Plan->getEntry (), VecPreheader);
866
859
867
860
// Create SCEV and VPValue for the trip count.
@@ -878,17 +871,17 @@ VPlanPtr VPlan::createInitialVPlan(Type *InductionTy,
878
871
879
872
// Create VPRegionBlock, with empty header and latch blocks, to be filled
880
873
// during processing later.
881
- VPBasicBlock *HeaderVPBB = new VPBasicBlock (" vector.body" );
882
- VPBasicBlock *LatchVPBB = new VPBasicBlock (" vector.latch" );
874
+ VPBasicBlock *HeaderVPBB = Plan-> createVPBasicBlock (" vector.body" );
875
+ VPBasicBlock *LatchVPBB = Plan-> createVPBasicBlock (" vector.latch" );
883
876
VPBlockUtils::insertBlockAfter (LatchVPBB, HeaderVPBB);
884
- auto *TopRegion = new VPRegionBlock (HeaderVPBB, LatchVPBB, " vector loop " ,
885
- false /* isReplicator*/ );
877
+ auto *TopRegion = Plan-> createVPRegionBlock (
878
+ HeaderVPBB, LatchVPBB, " vector loop " , false /* isReplicator*/ );
886
879
887
880
VPBlockUtils::insertBlockAfter (TopRegion, VecPreheader);
888
- VPBasicBlock *MiddleVPBB = new VPBasicBlock (" middle.block" );
881
+ VPBasicBlock *MiddleVPBB = Plan-> createVPBasicBlock (" middle.block" );
889
882
VPBlockUtils::insertBlockAfter (MiddleVPBB, TopRegion);
890
883
891
- VPBasicBlock *ScalarPH = new VPBasicBlock (" scalar.ph" );
884
+ VPBasicBlock *ScalarPH = Plan-> createVPBasicBlock (" scalar.ph" );
892
885
VPBlockUtils::connectBlocks (ScalarPH, ScalarHeader);
893
886
if (!RequiresScalarEpilogueCheck) {
894
887
VPBlockUtils::connectBlocks (MiddleVPBB, ScalarPH);
@@ -904,7 +897,7 @@ VPlanPtr VPlan::createInitialVPlan(Type *InductionTy,
904
897
// we unconditionally branch to the scalar preheader. Do nothing.
905
898
// 3) Otherwise, construct a runtime check.
906
899
BasicBlock *IRExitBlock = TheLoop->getUniqueLatchExitBlock ();
907
- auto *VPExitBlock = VPIRBasicBlock::fromBasicBlock (IRExitBlock);
900
+ auto *VPExitBlock = Plan-> createVPIRBasicBlock (IRExitBlock);
908
901
// The connection order corresponds to the operands of the conditional branch.
909
902
VPBlockUtils::insertBlockAfter (VPExitBlock, MiddleVPBB);
910
903
VPBlockUtils::connectBlocks (MiddleVPBB, ScalarPH);
@@ -960,15 +953,14 @@ void VPlan::prepareToExecute(Value *TripCountV, Value *VectorTripCountV,
960
953
// / have a single predecessor, which is rewired to the new VPIRBasicBlock. All
961
954
// / successors of VPBB, if any, are rewired to the new VPIRBasicBlock.
962
955
static void replaceVPBBWithIRVPBB (VPBasicBlock *VPBB, BasicBlock *IRBB) {
963
- VPIRBasicBlock *IRVPBB = VPIRBasicBlock::fromBasicBlock (IRBB);
956
+ VPIRBasicBlock *IRVPBB = VPBB-> getPlan ()-> createVPIRBasicBlock (IRBB);
964
957
for (auto &R : make_early_inc_range (*VPBB)) {
965
958
assert (!R.isPhi () && " Tried to move phi recipe to end of block" );
966
959
R.moveBefore (*IRVPBB, IRVPBB->end ());
967
960
}
968
961
969
962
VPBlockUtils::reassociateBlocks (VPBB, IRVPBB);
970
-
971
- delete VPBB;
963
+ // VPBB is now dead and will be cleaned up when the plan gets destroyed.
972
964
}
973
965
974
966
// / Generate the code inside the preheader and body of the vectorized loop.
@@ -1217,6 +1209,7 @@ static void remapOperands(VPBlockBase *Entry, VPBlockBase *NewEntry,
1217
1209
}
1218
1210
1219
1211
VPlan *VPlan::duplicate () {
1212
+ unsigned NumBlocksBeforeCloning = CreatedBlocks.size ();
1220
1213
// Clone blocks.
1221
1214
const auto &[NewEntry, __] = cloneFrom (Entry);
1222
1215
@@ -1257,9 +1250,32 @@ VPlan *VPlan::duplicate() {
1257
1250
assert (Old2NewVPValues.contains (TripCount) &&
1258
1251
" TripCount must have been added to Old2NewVPValues" );
1259
1252
NewPlan->TripCount = Old2NewVPValues[TripCount];
1253
+
1254
+ // Transfer all cloned blocks (the second half of all current blocks) from
1255
+ // current to new VPlan.
1256
+ unsigned NumBlocksAfterCloning = CreatedBlocks.size ();
1257
+ for (unsigned I :
1258
+ seq<unsigned >(NumBlocksBeforeCloning, NumBlocksAfterCloning))
1259
+ NewPlan->CreatedBlocks .push_back (this ->CreatedBlocks [I]);
1260
+ CreatedBlocks.truncate (NumBlocksBeforeCloning);
1261
+
1260
1262
return NewPlan;
1261
1263
}
1262
1264
1265
+ VPIRBasicBlock *VPlan::createEmptyVPIRBasicBlock (BasicBlock *IRBB) {
1266
+ auto *VPIRBB = new VPIRBasicBlock (IRBB);
1267
+ CreatedBlocks.push_back (VPIRBB);
1268
+ return VPIRBB;
1269
+ }
1270
+
1271
+ VPIRBasicBlock *VPlan::createVPIRBasicBlock (BasicBlock *IRBB) {
1272
+ auto *VPIRBB = createEmptyVPIRBasicBlock (IRBB);
1273
+ for (Instruction &I :
1274
+ make_range (IRBB->begin (), IRBB->getTerminator ()->getIterator ()))
1275
+ VPIRBB->appendRecipe (new VPIRInstruction (I));
1276
+ return VPIRBB;
1277
+ }
1278
+
1263
1279
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1264
1280
1265
1281
Twine VPlanPrinter::getUID (const VPBlockBase *Block) {
0 commit comments