[llvm] [SimplifyCFG] Cache unique predecessors in `simplifyDuplicateSwitchArms` (PR #147384)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 8 06:08:53 PDT 2025


================
@@ -7505,27 +7505,29 @@ bool SimplifyCFGOpt::simplifyDuplicateSwitchArms(SwitchInst *SI,
     if (BB->size() != 1)
       continue;
 
-    // FIXME: This case needs some extra care because the terminators other than
-    // SI need to be updated. For now, consider only backedges to the SI.
-    if (BB->hasNPredecessorsOrMore(4) ||
-        BB->getUniquePredecessor() != SI->getParent())
-      continue;
-
     // FIXME: Relax that the terminator is a BranchInst by checking for equality
     // on other kinds of terminators. We decide to only support unconditional
     // branches for now for compile time reasons.
     auto *BI = dyn_cast<BranchInst>(BB->getTerminator());
     if (!BI || BI->isConditional())
       continue;
 
-    if (Seen.insert(BB).second) {
-      // Keep track of which PHIs we need as keys in PhiPredIVs below.
-      for (BasicBlock *Succ : BI->successors())
-        Phis.insert_range(llvm::make_pointer_range(Succ->phis()));
-      // Add the successor only if not previously visited.
-      Cases.emplace_back(SwitchSuccWrapper{BB, &PhiPredIVs});
+    if (!Seen.insert(BB).second) {
+      BBToSuccessorIndexes[BB].emplace_back(I);
----------------
dtcxzyw wrote:

```suggestion
      auto It = BBToSuccessorIndexes.find(BB);
      if (It != BBToSuccessorIndexes.end())
        It->second.emplace_back(I);
```
If we don't add the index the first time we meet the BB, don't add the index in later iterations.

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


More information about the llvm-commits mailing list