[llvm] [SimplifyCFG] Transform switch to select when common bits uniquely identify one case (PR #145233)
Antonio Frighetto via llvm-commits
llvm-commits at lists.llvm.org
Tue Jul 1 05:25:13 PDT 2025
================
@@ -6234,10 +6234,35 @@ static Value *foldSwitchToSelect(const SwitchCaseResultVectorTy &ResultVector,
// case 0,2,8,10 -> Cond & 0b1..0101 == 0 ? result : default
if (isPowerOf2_32(CaseCount)) {
ConstantInt *MinCaseVal = CaseValues[0];
- // Find mininal value.
- for (auto *Case : CaseValues)
+ // In case, there are bits, that can only be present in the CaseValues we
+ // can transform the switch into a select if the conjunction of
+ // all the values uniquely identify the CaseValues.
+ APInt AndMask = APInt::getAllOnes(MinCaseVal->getBitWidth());
+
+ // Find mininal value and compute the conjuction of the values.
----------------
antoniofrighetto wrote:
```suggestion
// Find the minimum value and compute the and of all the case values.
```
https://github.com/llvm/llvm-project/pull/145233
More information about the llvm-commits
mailing list