[llvm] [SCEV] Extend isBasicBlockEntryGuardedByCond to handle merge block (PR #190602)

Eli Friedman via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 6 09:35:28 PDT 2026


================
@@ -12028,15 +12028,37 @@ bool ScalarEvolution::isBasicBlockEntryGuardedByCond(const BasicBlock *BB,
     PredBB = ContainingLoop->getLoopPredecessor();
   else
     PredBB = BB->getSinglePredecessor();
-  for (std::pair<const BasicBlock *, const BasicBlock *> Pair(PredBB, BB);
-       Pair.first; Pair = getPredecessorWithUniqueSuccessorForBB(Pair.first)) {
-    const CondBrInst *BlockEntryPredicate =
-        dyn_cast<CondBrInst>(Pair.first->getTerminator());
-    if (!BlockEntryPredicate)
-      continue;
-
-    if (ProveViaCond(BlockEntryPredicate->getCondition(),
-                     BlockEntryPredicate->getSuccessor(0) != Pair.second))
+  // For simplificity, only one merge block is handled.
+  const BasicBlock *MergeBlock = nullptr;
+  unsigned NumVisits = 0;
+  auto ProveViaPredecessorChain = [&](const BasicBlock *Pred,
+                                      const BasicBlock *Succ,
+                                      unsigned MaxNumVisits) {
+    for (std::pair<const BasicBlock *, const BasicBlock *> Pair(Pred, Succ);
+         Pair.first;
+         Pair = getPredecessorWithUniqueSuccessorForBB(Pair.first)) {
+      if (NumVisits++ > MaxNumVisits)
+        return false;
+      const CondBrInst *BlockEntryPredicate =
+          dyn_cast<CondBrInst>(Pair.first->getTerminator());
+      if (BlockEntryPredicate &&
+          ProveViaCond(BlockEntryPredicate->getCondition(),
+                       BlockEntryPredicate->getSuccessor(0) != Pair.second))
+        return true;
+      if (!MergeBlock && Pair.first->hasNPredecessorsOrMore(2))
+        MergeBlock = Pair.first;
+    }
+    return false;
+  };
+  unsigned MaxChainVisits = ~0u; // No limit for the first predecessor chain.
+  if (ProveViaPredecessorChain(PredBB, BB, MaxChainVisits))
+    return true;
+  if (MergeBlock) {
----------------
efriedma-quic wrote:

This assumes if MergeBlock is non-null, all the other predecessors are guarded by the condition.  But I'm not seeing any code to actually check for that.

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


More information about the llvm-commits mailing list