[llvm] [SimplifyCFG] Handle switch-to-select remapping correctly (PR #213302)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 16 01:42:32 PDT 2026


================
@@ -4999,8 +4999,70 @@ bool SimplifyCFGOpt::simplifyTerminatorOnSelect(Instruction *OldTerm,
 //   (switch (select cond, X, Y)) on constant X, Y
 // with a branch - conditional if X and Y lead to distinct BBs,
 // unconditional otherwise.
+//
+// Also folds switch(select(icmp eq X, C, K, X)) into switch(X), retargeting
+// (or adding) the case for C to wherever K currently dispatches to:
+//   %cmp = icmp eq T %x, C
+//   %key = select i1 %cmp, T K, T %x
+//   switch T %key, label %default [ T K, label %case_k ... ]
+// becomes
+//   switch T %x, label %default [ T C, label %case_k
+//                                  T K, label %case_k ... ]
 bool SimplifyCFGOpt::simplifySwitchOnSelect(SwitchInst *SI,
                                             SelectInst *Select) {
+  CmpPredicate Pred;
+  Value *X;
+  ConstantInt *C;
+  if (Select->hasOneUse() &&
+      match(Select->getCondition(),
+            m_OneUse(m_c_ICmp(Pred, m_Value(X), m_ConstantInt(C)))) &&
----------------
dtcxzyw wrote:

Use `m_ICmp`. The constant operand is always placed in the RHS.
One-use constraint on icmp is unnecessary.


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


More information about the llvm-commits mailing list