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

Hongyu Chen via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 21 03:58:21 PDT 2025


================
@@ -7540,6 +7540,79 @@ 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);
+  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->cases()) {
+    if (Case.getCaseValue()->getValue().ugt(Constant->getValue())) {
+      DeadCases.push_back(Case.getCaseValue());
+    }
+  }
----------------
XChy wrote:

```suggestion
  for (auto Case : SI->cases())
    if (Case.getCaseValue()->getValue().ugt(Constant->getValue()))
      DeadCases.push_back(Case.getCaseValue());
```

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


More information about the llvm-commits mailing list