@@ -235,22 +235,26 @@ bool llvm::MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU,
235
235
// These dominator edges will be redirected from Pred.
236
236
std::vector<DominatorTree::UpdateType> Updates;
237
237
if (DTU) {
238
- SmallPtrSet<BasicBlock *, 2 > SuccsOfBB (succ_begin (BB), succ_end (BB));
238
+ // To avoid processing the same predecessor more than once.
239
+ SmallPtrSet<BasicBlock *, 8 > SeenSuccs;
239
240
SmallPtrSet<BasicBlock *, 2 > SuccsOfPredBB (succ_begin (PredBB),
240
241
succ_end (PredBB));
241
- Updates.reserve (Updates.size () + 2 * SuccsOfBB. size ( ) + 1 );
242
+ Updates.reserve (Updates.size () + 2 * succ_size (BB ) + 1 );
242
243
// Add insert edges first. Experimentally, for the particular case of two
243
244
// blocks that can be merged, with a single successor and single predecessor
244
245
// respectively, it is beneficial to have all insert updates first. Deleting
245
246
// edges first may lead to unreachable blocks, followed by inserting edges
246
247
// making the blocks reachable again. Such DT updates lead to high compile
247
248
// times. We add inserts before deletes here to reduce compile time.
248
- for (BasicBlock *SuccOfBB : SuccsOfBB )
249
+ for (BasicBlock *SuccOfBB : successors (BB) )
249
250
// This successor of BB may already be a PredBB's successor.
250
251
if (!SuccsOfPredBB.contains (SuccOfBB))
251
- Updates.push_back ({DominatorTree::Insert, PredBB, SuccOfBB});
252
- for (BasicBlock *SuccOfBB : SuccsOfBB)
253
- Updates.push_back ({DominatorTree::Delete, BB, SuccOfBB});
252
+ if (SeenSuccs.insert (SuccOfBB).second )
253
+ Updates.push_back ({DominatorTree::Insert, PredBB, SuccOfBB});
254
+ SeenSuccs.clear ();
255
+ for (BasicBlock *SuccOfBB : successors (BB))
256
+ if (SeenSuccs.insert (SuccOfBB).second )
257
+ Updates.push_back ({DominatorTree::Delete, BB, SuccOfBB});
254
258
Updates.push_back ({DominatorTree::Delete, PredBB, BB});
255
259
}
256
260
@@ -804,14 +808,14 @@ static BasicBlock *SplitBlockImpl(BasicBlock *Old, Instruction *SplitPt,
804
808
if (DTU) {
805
809
SmallVector<DominatorTree::UpdateType, 8 > Updates;
806
810
// Old dominates New. New node dominates all other nodes dominated by Old.
807
- SmallPtrSet<BasicBlock *, 8 > UniqueSuccessorsOfOld (succ_begin (New),
808
- succ_end (New));
811
+ SmallPtrSet<BasicBlock *, 8 > UniqueSuccessorsOfOld;
809
812
Updates.push_back ({DominatorTree::Insert, Old, New});
810
- Updates.reserve (Updates.size () + 2 * UniqueSuccessorsOfOld.size ());
811
- for (BasicBlock *UniqueSuccessorOfOld : UniqueSuccessorsOfOld) {
812
- Updates.push_back ({DominatorTree::Insert, New, UniqueSuccessorOfOld});
813
- Updates.push_back ({DominatorTree::Delete, Old, UniqueSuccessorOfOld});
814
- }
813
+ Updates.reserve (Updates.size () + 2 * succ_size (New));
814
+ for (BasicBlock *SuccessorOfOld : successors (New))
815
+ if (UniqueSuccessorsOfOld.insert (SuccessorOfOld).second ) {
816
+ Updates.push_back ({DominatorTree::Insert, New, SuccessorOfOld});
817
+ Updates.push_back ({DominatorTree::Delete, Old, SuccessorOfOld});
818
+ }
815
819
816
820
DTU->applyUpdates (Updates);
817
821
} else if (DT)
@@ -870,14 +874,14 @@ BasicBlock *llvm::splitBlockBefore(BasicBlock *Old, Instruction *SplitPt,
870
874
SmallVector<DominatorTree::UpdateType, 8 > DTUpdates;
871
875
// New dominates Old. The predecessor nodes of the Old node dominate
872
876
// New node.
873
- SmallPtrSet<BasicBlock *, 8 > UniquePredecessorsOfOld (pred_begin (New),
874
- pred_end (New));
877
+ SmallPtrSet<BasicBlock *, 8 > UniquePredecessorsOfOld;
875
878
DTUpdates.push_back ({DominatorTree::Insert, New, Old});
876
- DTUpdates.reserve (DTUpdates.size () + 2 * UniquePredecessorsOfOld.size ());
877
- for (BasicBlock *UniquePredecessorOfOld : UniquePredecessorsOfOld) {
878
- DTUpdates.push_back ({DominatorTree::Insert, UniquePredecessorOfOld, New});
879
- DTUpdates.push_back ({DominatorTree::Delete, UniquePredecessorOfOld, Old});
880
- }
879
+ DTUpdates.reserve (DTUpdates.size () + 2 * pred_size (New));
880
+ for (BasicBlock *PredecessorOfOld : predecessors (New))
881
+ if (UniquePredecessorsOfOld.insert (PredecessorOfOld).second ) {
882
+ DTUpdates.push_back ({DominatorTree::Insert, PredecessorOfOld, New});
883
+ DTUpdates.push_back ({DominatorTree::Delete, PredecessorOfOld, Old});
884
+ }
881
885
882
886
DTU->applyUpdates (DTUpdates);
883
887
@@ -910,13 +914,14 @@ static void UpdateAnalysisInformation(BasicBlock *OldBB, BasicBlock *NewBB,
910
914
} else {
911
915
// Split block expects NewBB to have a non-empty set of predecessors.
912
916
SmallVector<DominatorTree::UpdateType, 8 > Updates;
913
- SmallPtrSet<BasicBlock *, 8 > UniquePreds (Preds. begin (), Preds. end ()) ;
917
+ SmallPtrSet<BasicBlock *, 8 > UniquePreds;
914
918
Updates.push_back ({DominatorTree::Insert, NewBB, OldBB});
915
- Updates.reserve (Updates.size () + 2 * UniquePreds.size ());
916
- for (auto *UniquePred : UniquePreds) {
917
- Updates.push_back ({DominatorTree::Insert, UniquePred, NewBB});
918
- Updates.push_back ({DominatorTree::Delete, UniquePred, OldBB});
919
- }
919
+ Updates.reserve (Updates.size () + 2 * Preds.size ());
920
+ for (auto *Pred : Preds)
921
+ if (UniquePreds.insert (Pred).second ) {
922
+ Updates.push_back ({DominatorTree::Insert, Pred, NewBB});
923
+ Updates.push_back ({DominatorTree::Delete, Pred, OldBB});
924
+ }
920
925
DTU->applyUpdates (Updates);
921
926
}
922
927
} else if (DT) {
@@ -1376,14 +1381,14 @@ SplitBlockAndInsertIfThenImpl(Value *Cond, Instruction *SplitBefore,
1376
1381
BasicBlock *Head = SplitBefore->getParent ();
1377
1382
BasicBlock *Tail = Head->splitBasicBlock (SplitBefore->getIterator ());
1378
1383
if (DTU) {
1379
- SmallPtrSet<BasicBlock *, 8 > UniqueSuccessorsOfHead (succ_begin (Tail),
1380
- succ_end (Tail));
1384
+ SmallPtrSet<BasicBlock *, 8 > UniqueSuccessorsOfHead;
1381
1385
Updates.push_back ({DominatorTree::Insert, Head, Tail});
1382
- Updates.reserve (Updates.size () + 2 * UniqueSuccessorsOfHead.size ());
1383
- for (BasicBlock *UniqueSuccessorOfHead : UniqueSuccessorsOfHead) {
1384
- Updates.push_back ({DominatorTree::Insert, Tail, UniqueSuccessorOfHead});
1385
- Updates.push_back ({DominatorTree::Delete, Head, UniqueSuccessorOfHead});
1386
- }
1386
+ Updates.reserve (Updates.size () + 2 * succ_size (Tail));
1387
+ for (BasicBlock *SuccessorOfHead : successors (Tail))
1388
+ if (UniqueSuccessorsOfHead.insert (SuccessorOfHead).second ) {
1389
+ Updates.push_back ({DominatorTree::Insert, Tail, SuccessorOfHead});
1390
+ Updates.push_back ({DominatorTree::Delete, Head, SuccessorOfHead});
1391
+ }
1387
1392
}
1388
1393
Instruction *HeadOldTerm = Head->getTerminator ();
1389
1394
LLVMContext &C = Head->getContext ();
0 commit comments