[llvm] [SimplifyCFG] Avoid introducing complex phi when removing empty blocks (PR #104887)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 21 01:03:22 PDT 2024


================
@@ -1040,6 +1045,42 @@ CanRedirectPredsOfEmptyBBToSucc(BasicBlock *BB, BasicBlock *Succ,
   return true;
 }
 
+// Check whether removing BB will make the phis in its Succ have too
+// many incoming entries. This function does not check whether BB is foldable
+// or not.
+static bool introduceTooComplexPhi(BasicBlock *BB) {
+  // Check BB only has phi and an unconditional branch
+  BranchInst *Branch = dyn_cast<BranchInst>(BB->getFirstNonPHIOrDbg(true));
+  assert(Branch && Branch->isUnconditional() && "BB is not an empty block");
+
+  // If BB only has one predecessor, then removing it will not introduce more
+  // incoming edges for phis.
+  if (BB->hasNPredecessors(1))
+    return false;
+  int NumPreds = pred_size(BB);
+  auto *Succ = BB->getTerminator()->getSuccessor(0);
+  for (auto &Phi : Succ->phis()) {
+    int BlockIdx = Phi.getBasicBlockIndex(BB);
+    if (BlockIdx >= 0) {
----------------
dtcxzyw wrote:

`BlockIdx >= 0` is a loop invariant.

https://github.com/llvm/llvm-project/pull/104887


More information about the llvm-commits mailing list