aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/Utils/Local.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib/Transforms/Utils/Local.cpp')
-rw-r--r--llvm/lib/Transforms/Utils/Local.cpp35
1 files changed, 34 insertions, 1 deletions
diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp
index 725b512..7659fc6 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -112,6 +112,12 @@ static cl::opt<unsigned> PHICSENumPHISmallSize(
"When the basic block contains not more than this number of PHI nodes, "
"perform a (faster!) exhaustive search instead of set-driven one."));
+static cl::opt<unsigned> MaxPhiEntriesIncreaseAfterRemovingEmptyBlock(
+ "max-phi-entries-increase-after-removing-empty-block", cl::init(1000),
+ cl::Hidden,
+ cl::desc("Stop removing an empty block if removing it will introduce more "
+ "than this number of phi entries in its successor"));
+
// Max recursion depth for collectBitParts used when detecting bswap and
// bitreverse idioms.
static const unsigned BitPartRecursionMaxDepth = 48;
@@ -1047,6 +1053,33 @@ CanRedirectPredsOfEmptyBBToSucc(BasicBlock *BB, BasicBlock *Succ,
return true;
}
+/// Check whether removing \p BB will make the phis in its \p Succ have too
+/// many incoming entries. This function does not check whether \p BB is
+/// foldable or not.
+static bool introduceTooManyPhiEntries(BasicBlock *BB, BasicBlock *Succ) {
+ // If BB only has one predecessor, then removing it will not introduce more
+ // incoming edges for phis.
+ if (BB->hasNPredecessors(1))
+ return false;
+ unsigned NumPreds = pred_size(BB);
+ unsigned NumChangedPhi = 0;
+ for (auto &Phi : Succ->phis()) {
+ // If the incoming value is a phi and the phi is defined in BB,
+ // then removing BB will not increase the total phi entries of the ir.
+ if (auto *IncomingPhi = dyn_cast<PHINode>(Phi.getIncomingValueForBlock(BB)))
+ if (IncomingPhi->getParent() == BB)
+ continue;
+ // Otherwise, we need to add entries to the phi
+ NumChangedPhi++;
+ }
+ // For every phi that needs to be changed, (NumPreds - 1) new entries will be
+ // added. If the total increase in phi entries exceeds
+ // MaxPhiEntriesIncreaseAfterRemovingEmptyBlock, it will be considered as
+ // introducing too many new phi entries.
+ return (NumPreds - 1) * NumChangedPhi >
+ MaxPhiEntriesIncreaseAfterRemovingEmptyBlock;
+}
+
/// Replace a value flowing from a block to a phi with
/// potentially multiple instances of that value flowing from the
/// block's predecessors to the phi.
@@ -1146,7 +1179,7 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
BBKillable ||
CanRedirectPredsOfEmptyBBToSucc(BB, Succ, BBPreds, SuccPreds, CommonPred);
- if (!BBKillable && !BBPhisMergeable)
+ if ((!BBKillable && !BBPhisMergeable) || introduceTooManyPhiEntries(BB, Succ))
return false;
// Check to see if merging these blocks/phis would cause conflicts for any of