Skip to content

Commit 18d655f

Browse files
authored
[SimplifyCFG][NFC] Improve compile time for TryToSimplifyUncondBranchFromEmptyBlock optimization. (llvm#110715)
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 ec450b1 commit 18d655f

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

llvm/lib/Transforms/Utils/Local.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,6 @@ static void replaceUndefValuesInPhi(PHINode *PN,
10231023
static bool
10241024
CanRedirectPredsOfEmptyBBToSucc(BasicBlock *BB, BasicBlock *Succ,
10251025
const SmallPtrSetImpl<BasicBlock *> &BBPreds,
1026-
const SmallPtrSetImpl<BasicBlock *> &SuccPreds,
10271026
BasicBlock *&CommonPred) {
10281027

10291028
// There must be phis in BB, otherwise BB will be merged into Succ directly
@@ -1042,7 +1041,7 @@ CanRedirectPredsOfEmptyBBToSucc(BasicBlock *BB, BasicBlock *Succ,
10421041

10431042
// Get the single common predecessor of both BB and Succ. Return false
10441043
// when there are more than one common predecessors.
1045-
for (BasicBlock *SuccPred : SuccPreds) {
1044+
for (BasicBlock *SuccPred : predecessors(Succ)) {
10461045
if (BBPreds.count(SuccPred)) {
10471046
if (CommonPred)
10481047
return false;
@@ -1166,7 +1165,6 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
11661165
return false;
11671166

11681167
SmallPtrSet<BasicBlock *, 16> BBPreds(pred_begin(BB), pred_end(BB));
1169-
SmallPtrSet<BasicBlock *, 16> SuccPreds(pred_begin(Succ), pred_end(Succ));
11701168

11711169
// The single common predecessor of BB and Succ when BB cannot be killed
11721170
BasicBlock *CommonPred = nullptr;
@@ -1175,9 +1173,8 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
11751173

11761174
// Even if we can not fold BB into Succ, we may be able to redirect the
11771175
// predecessors of BB to Succ.
1178-
bool BBPhisMergeable =
1179-
BBKillable ||
1180-
CanRedirectPredsOfEmptyBBToSucc(BB, Succ, BBPreds, SuccPreds, CommonPred);
1176+
bool BBPhisMergeable = BBKillable || CanRedirectPredsOfEmptyBBToSucc(
1177+
BB, Succ, BBPreds, CommonPred);
11811178

11821179
if ((!BBKillable && !BBPhisMergeable) || introduceTooManyPhiEntries(BB, Succ))
11831180
return false;
@@ -1302,7 +1299,7 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
13021299
// All predecessors of BB (except the common predecessor) will be moved to
13031300
// Succ.
13041301
Updates.reserve(Updates.size() + 2 * pred_size(BB) + 1);
1305-
1302+
SmallPtrSet<BasicBlock *, 16> SuccPreds(pred_begin(Succ), pred_end(Succ));
13061303
for (auto *PredOfBB : predecessors(BB)) {
13071304
// Do not modify those common predecessors of BB and Succ
13081305
if (!SuccPreds.contains(PredOfBB))

0 commit comments

Comments
 (0)