[llvm] [SimplifyCFG] Fold the contiguous wrapping cases into ICmp. (PR #161000)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 5 20:32:28 PDT 2025


================
@@ -5718,22 +5718,35 @@ bool SimplifyCFGOpt::simplifyUnreachable(UnreachableInst *UI) {
   return Changed;
 }
 
-static bool casesAreContiguous(Value *Condition,
-                               SmallVectorImpl<ConstantInt *> &Cases,
-                               ConstantInt *&ContiguousCasesMin,
-                               ConstantInt *&ContiguousCasesMax,
-                               bool &IsWrapping) {
+struct ContiguousCasesResult {
+  ConstantInt *Min;
+  ConstantInt *Max;
+  BasicBlock *Dest;
+  BasicBlock *OtherDest;
+  SmallVectorImpl<ConstantInt *> *Cases;
+  SmallVectorImpl<ConstantInt *> *OtherCases;
+};
+
+static std::optional<ContiguousCasesResult>
+findContiguousCases(Value *Condition, SmallVectorImpl<ConstantInt *> &Cases,
+                    SmallVectorImpl<ConstantInt *> &OtherCases,
+                    BasicBlock *Dest, BasicBlock *OtherDest) {
   assert(Cases.size() >= 1);
 
   array_pod_sort(Cases.begin(), Cases.end(), constantIntSortPredicate);
-  auto Min = Cases.back()->getValue();
-  auto Max = Cases.front()->getValue();
-  auto Offset = Max - Min;
-  auto ContiguousOffset = Cases.size() - 1;
+  APInt Min = Cases.back()->getValue();
+  APInt Max = Cases.front()->getValue();
----------------
arsenm wrote:

```suggestion
  const APInt &Min = Cases.back()->getValue();
  const APInt &Max = Cases.front()->getValue();
```

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


More information about the llvm-commits mailing list