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

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 20 00:59:41 PDT 2024


================
@@ -1040,6 +1045,42 @@ CanRedirectPredsOfEmptyBBToSucc(BasicBlock *BB, BasicBlock *Succ,
   return true;
 }
 
+// Check whether removing BB will make the phis in its Succ will 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()) {
+    auto BlockIdx = Phi.getBasicBlockIndex(BB);
----------------
nikic wrote:

```suggestion
    int BlockIdx = Phi.getBasicBlockIndex(BB);
```

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


More information about the llvm-commits mailing list