[llvm] [SimplifyCFG][NFC] Improve compile time for TryToSimplifyUncondBranchFromEmptyBlock optimization. (PR #110715)
Amara Emerson via llvm-commits
llvm-commits at lists.llvm.org
Mon Oct 7 13:46:32 PDT 2024
https://github.com/aemerson updated https://github.com/llvm/llvm-project/pull/110715
>From 03d9ec6be394ad82cd2840e5c199df4dfe43c2a5 Mon Sep 17 00:00:00 2001
From: Amara Emerson <amara at apple.com>
Date: Wed, 2 Oct 2024 12:29:10 -0700
Subject: [PATCH 1/2] [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
---
llvm/lib/Transforms/Utils/Local.cpp | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp
index 7659fc69196151..6c2e332ef3c590 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -1020,10 +1020,11 @@ static void replaceUndefValuesInPhi(PHINode *PN,
// Only when they shares a single common predecessor, return true.
// Only handles cases when BB can't be merged while its predecessors can be
// redirected.
+// \p SuccPredsOut may be partially populated with the predecessors of Succ.
static bool
CanRedirectPredsOfEmptyBBToSucc(BasicBlock *BB, BasicBlock *Succ,
const SmallPtrSetImpl<BasicBlock *> &BBPreds,
- const SmallPtrSetImpl<BasicBlock *> &SuccPreds,
+ SmallPtrSetImpl<BasicBlock *> &SuccPredsOut,
BasicBlock *&CommonPred) {
// There must be phis in BB, otherwise BB will be merged into Succ directly
@@ -1042,7 +1043,8 @@ CanRedirectPredsOfEmptyBBToSucc(BasicBlock *BB, BasicBlock *Succ,
// Get the single common predecessor of both BB and Succ. Return false
// when there are more than one common predecessors.
- for (BasicBlock *SuccPred : SuccPreds) {
+ for (BasicBlock *SuccPred : predecessors(Succ)) {
+ SuccPredsOut.insert(SuccPred);
if (BBPreds.count(SuccPred)) {
if (CommonPred)
return false;
@@ -1166,7 +1168,7 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
return false;
SmallPtrSet<BasicBlock *, 16> BBPreds(pred_begin(BB), pred_end(BB));
- SmallPtrSet<BasicBlock *, 16> SuccPreds(pred_begin(Succ), pred_end(Succ));
+ SmallPtrSet<BasicBlock *, 16> SuccPreds;
// The single common predecessor of BB and Succ when BB cannot be killed
BasicBlock *CommonPred = nullptr;
@@ -1182,6 +1184,10 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
if ((!BBKillable && !BBPhisMergeable) || introduceTooManyPhiEntries(BB, Succ))
return false;
+ // SuccPreds may not have been fully filled by CanRedirectPredsOfEmptyBBToSucc
+ // so finish it here.
+ SuccPreds.insert(pred_begin(Succ), pred_end(Succ));
+
// Check to see if merging these blocks/phis would cause conflicts for any of
// the phi nodes in BB or Succ. If not, we can safely merge.
>From 89a64fa04753d910467990659148a1a6e163446c Mon Sep 17 00:00:00 2001
From: Amara Emerson <amara at apple.com>
Date: Mon, 7 Oct 2024 13:03:30 -0700
Subject: [PATCH 2/2] Don't unnecessarily cache.
---
llvm/lib/Transforms/Utils/Local.cpp | 1 -
1 file changed, 1 deletion(-)
diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp
index 6c2e332ef3c590..f1faebb84934d2 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -1044,7 +1044,6 @@ CanRedirectPredsOfEmptyBBToSucc(BasicBlock *BB, BasicBlock *Succ,
// Get the single common predecessor of both BB and Succ. Return false
// when there are more than one common predecessors.
for (BasicBlock *SuccPred : predecessors(Succ)) {
- SuccPredsOut.insert(SuccPred);
if (BBPreds.count(SuccPred)) {
if (CommonPred)
return false;
More information about the llvm-commits
mailing list