Skip to content

Commit 03d9ec6

Browse files
committed
[SimplifyCFG][NFC] Improve compile time for TryToSimplifyUncondBranchFromEmptyBlock optimization.
In some pathological cases this optimization can spend an unreasonable amount of time populating the set for predecessors of the successor block. This change sinks some of that initializing to the point where it's actually necessary so we can take advantage of the existing early-exits. rdar://137063034
1 parent 78ccffc commit 03d9ec6

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

llvm/lib/Transforms/Utils/Local.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,10 +1020,11 @@ static void replaceUndefValuesInPhi(PHINode *PN,
10201020
// Only when they shares a single common predecessor, return true.
10211021
// Only handles cases when BB can't be merged while its predecessors can be
10221022
// redirected.
1023+
// \p SuccPredsOut may be partially populated with the predecessors of Succ.
10231024
static bool
10241025
CanRedirectPredsOfEmptyBBToSucc(BasicBlock *BB, BasicBlock *Succ,
10251026
const SmallPtrSetImpl<BasicBlock *> &BBPreds,
1026-
const SmallPtrSetImpl<BasicBlock *> &SuccPreds,
1027+
SmallPtrSetImpl<BasicBlock *> &SuccPredsOut,
10271028
BasicBlock *&CommonPred) {
10281029

10291030
// There must be phis in BB, otherwise BB will be merged into Succ directly
@@ -1042,7 +1043,8 @@ CanRedirectPredsOfEmptyBBToSucc(BasicBlock *BB, BasicBlock *Succ,
10421043

10431044
// Get the single common predecessor of both BB and Succ. Return false
10441045
// when there are more than one common predecessors.
1045-
for (BasicBlock *SuccPred : SuccPreds) {
1046+
for (BasicBlock *SuccPred : predecessors(Succ)) {
1047+
SuccPredsOut.insert(SuccPred);
10461048
if (BBPreds.count(SuccPred)) {
10471049
if (CommonPred)
10481050
return false;
@@ -1166,7 +1168,7 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
11661168
return false;
11671169

11681170
SmallPtrSet<BasicBlock *, 16> BBPreds(pred_begin(BB), pred_end(BB));
1169-
SmallPtrSet<BasicBlock *, 16> SuccPreds(pred_begin(Succ), pred_end(Succ));
1171+
SmallPtrSet<BasicBlock *, 16> SuccPreds;
11701172

11711173
// The single common predecessor of BB and Succ when BB cannot be killed
11721174
BasicBlock *CommonPred = nullptr;
@@ -1182,6 +1184,10 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
11821184
if ((!BBKillable && !BBPhisMergeable) || introduceTooManyPhiEntries(BB, Succ))
11831185
return false;
11841186

1187+
// SuccPreds may not have been fully filled by CanRedirectPredsOfEmptyBBToSucc
1188+
// so finish it here.
1189+
SuccPreds.insert(pred_begin(Succ), pred_end(Succ));
1190+
11851191
// Check to see if merging these blocks/phis would cause conflicts for any of
11861192
// the phi nodes in BB or Succ. If not, we can safely merge.
11871193

0 commit comments

Comments
 (0)