[llvm] [SimplifyCFG]: Switch on umin replaces default (PR #164097)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Sat Oct 18 11:28:15 PDT 2025
================
@@ -7540,6 +7540,58 @@ 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;
+
+ BasicBlock *Unreachable = SI->getDefaultDest();
+ SI->setDefaultDest(Case->getCaseSuccessor());
+ SI->removeCase(Case);
----------------
dtcxzyw wrote:
Branch weights need to be updated.
https://github.com/llvm/llvm-project/pull/164097
More information about the llvm-commits
mailing list