[llvm] [SimplifyCFG] Simplify switch with implicit default (PR #95665)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Sat Jun 15 20:36:51 PDT 2024
================
@@ -7104,6 +7104,47 @@ static bool simplifySwitchOfPowersOfTwo(SwitchInst *SI, IRBuilder<> &Builder,
return true;
}
+// Try to fold switch with manually selected default branch.
+// For example, for the switch
+// switch (v) { case 0: case 1: case 2: ... case MaxC: default: }
+// (continuous cases value from 0 to MaxC)
+// and, v = select (x u<= MaxC), x, AnotherC.
+// so "AnotherC" is the de-facto default branch, and it will be folded to
+// switch (x) { case 0: case 1: case 2: ... case MaxC: default -> AnotherC: }
+static bool simplifySwitchWithImplicitDefault(SwitchInst *SI) {
+ Value *Cond;
+ ConstantInt *Bound, *DefaultCase;
+ ICmpInst::Predicate Pred;
+ if (!match(SI->getCondition(),
+ m_Select(m_ICmp(Pred, m_Value(Cond), m_ConstantInt(Bound)),
+ m_Deferred(Cond), m_ConstantInt(DefaultCase))) ||
+ Pred != CmpInst::ICMP_ULT)
----------------
dtcxzyw wrote:
`icmp ugt + select + switch` doesn't exist.
I think it is a rustc-specific pattern. https://github.com/rust-lang/rust/blob/81117ff930fbf3792b4f9504e3c6bccc87b10823/compiler/rustc_codegen_ssa/src/mir/place.rs#L206-L309
https://github.com/llvm/llvm-project/pull/95665
More information about the llvm-commits
mailing list