[llvm] [SimplifyCFG]: Switch on umin replaces default (PR #164097)

Hongyu Chen via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 20 02:22:49 PDT 2025


================
@@ -7540,6 +7540,78 @@ static bool reduceSwitchRange(SwitchInst *SI, IRBuilder<> &Builder,
   return true;
 }
 
+/// Tries to transform the switch when the condition is umin and a constant.
+/// In that case, the default branch can be replaced by the constant's branch.
+/// For example:
+/// switch(umin(a, 3)) {
+/// case 0:
+/// case 1:
+/// case 2:
+/// case 3:
+///   // ...
+/// default:
+///   unreachable
+/// }
+///
+/// Transforms into:
+///
+/// switch(a) {
+/// case 0:
+/// case 1:
+/// case 2:
+/// default:
+///   // This is case 3
+/// }
+static bool simplifySwitchWhenUMin(SwitchInst *SI, DomTreeUpdater *DTU) {
+  Value *A;
+  ConstantInt *Constant;
+
+  if (!match(SI->getCondition(), m_UMin(m_Value(A), m_ConstantInt(Constant))))
+    return false;
+
+  if (!SI->defaultDestUnreachable())
+    return false;
+
+  auto Case = SI->findCaseValue(Constant);
+
+  // When the case value cannot be found then `findCaseValue` returns the
+  // default cause. This means that there is no `case 3:` and this
+  // simplification fails.
+  if (Case == SI->case_default())
+    return false;
+
+  SwitchInstProfUpdateWrapper SIW(*SI);
+
+  BasicBlock *Unreachable = SI->getDefaultDest();
+  SIW->setDefaultDest(Case->getCaseSuccessor());
+  SIW.removeCase(Case);
+  SIW->setCondition(A);
+
+  BasicBlock *BB = SIW->getParent();
+  SmallVector<DominatorTree::UpdateType> Updates;
+  Updates.push_back({DominatorTree::Delete, BB, Unreachable});
+
+  // A case is dead when its value is higher than the Constant.
+  SmallVector<ConstantInt *, 4> DeadCases;
+  for (auto Case = SI->case_begin(), e = SI->case_end(); Case != e; Case++) {
+    if (Case->getCaseValue()->getValue().ugt(Constant->getValue())) {
+      DeadCases.push_back(Case->getCaseValue());
+    }
+  }
+
+  for (ConstantInt *DeadCase : DeadCases) {
+    SwitchInst::CaseIt CaseI = SIW->findCaseValue(DeadCase);
+    CaseI->getCaseSuccessor()->removePredecessor(SIW->getParent());
+    SIW.removeCase(Case);
+    Updates.push_back({DominatorTree::Delete, BB, Case->getCaseSuccessor()});
+  }
----------------
XChy wrote:

```suggestion
  for (ConstantInt *DeadCaseVal : DeadCases) {
    SwitchInst::CaseIt DeadCase = SIW->findCaseValue(DeadCaseVal);
    BasicBlock* DeadCaseBB = DeadCase->getCaseSuccessor();
    DeadCaseBB->removePredecessor(SIW->getParent());
    SIW.removeCase(DeadCase);
    Updates.push_back({DominatorTree::Delete, BB, DeadCaseBB});
  }
```

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


More information about the llvm-commits mailing list