[llvm] [SimplifyCFG] Fold the contiguous wrapping cases into ICmp. (PR #161000)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Sep 27 07:48:53 PDT 2025
https://github.com/dianqk updated https://github.com/llvm/llvm-project/pull/161000
>From 56519730fea18832ca14ad4d164e7c4549eb90a9 Mon Sep 17 00:00:00 2001
From: dianqk <dianqk at dianqk.net>
Date: Sat, 27 Sep 2025 15:56:24 +0800
Subject: [PATCH 1/3] Pre-commit test cases
---
.../SimplifyCFG/switch-range-to-icmp.ll | 98 +++++++++++++++++++
1 file changed, 98 insertions(+)
diff --git a/llvm/test/Transforms/SimplifyCFG/switch-range-to-icmp.ll b/llvm/test/Transforms/SimplifyCFG/switch-range-to-icmp.ll
index 8f2ae2d054f1e..73771b48e21bc 100644
--- a/llvm/test/Transforms/SimplifyCFG/switch-range-to-icmp.ll
+++ b/llvm/test/Transforms/SimplifyCFG/switch-range-to-icmp.ll
@@ -188,4 +188,102 @@ exit:
ret void
}
+define i32 @wrapping_known_range(i8 range(i8 0, 6) %arg) {
+; CHECK-LABEL: @wrapping_known_range(
+; CHECK-NEXT: switch i8 [[ARG:%.*]], label [[ELSE:%.*]] [
+; CHECK-NEXT: i8 0, label [[IF:%.*]]
+; CHECK-NEXT: i8 4, label [[IF]]
+; CHECK-NEXT: i8 5, label [[IF]]
+; CHECK-NEXT: ]
+; CHECK: common.ret:
+; CHECK-NEXT: [[COMMON_RET_OP:%.*]] = phi i32 [ [[I0:%.*]], [[IF]] ], [ [[I1:%.*]], [[ELSE]] ]
+; CHECK-NEXT: ret i32 [[COMMON_RET_OP]]
+; CHECK: if:
+; CHECK-NEXT: [[I0]] = call i32 @f(i32 0)
+; CHECK-NEXT: br label [[COMMON_RET:%.*]]
+; CHECK: else:
+; CHECK-NEXT: [[I1]] = call i32 @f(i32 1)
+; CHECK-NEXT: br label [[COMMON_RET]]
+;
+ switch i8 %arg, label %else [
+ i8 0, label %if
+ i8 4, label %if
+ i8 5, label %if
+ ]
+
+if:
+ %i0 = call i32 @f(i32 0)
+ ret i32 %i0
+
+else:
+ %i1 = call i32 @f(i32 1)
+ ret i32 %i1
+}
+
+define i32 @wrapping_range(i8 %arg) {
+; CHECK-LABEL: @wrapping_range(
+; CHECK-NEXT: switch i8 [[ARG:%.*]], label [[ELSE:%.*]] [
+; CHECK-NEXT: i8 0, label [[IF:%.*]]
+; CHECK-NEXT: i8 -3, label [[IF]]
+; CHECK-NEXT: i8 -2, label [[IF]]
+; CHECK-NEXT: i8 -1, label [[IF]]
+; CHECK-NEXT: ]
+; CHECK: common.ret:
+; CHECK-NEXT: [[COMMON_RET_OP:%.*]] = phi i32 [ [[I0:%.*]], [[IF]] ], [ [[I1:%.*]], [[ELSE]] ]
+; CHECK-NEXT: ret i32 [[COMMON_RET_OP]]
+; CHECK: if:
+; CHECK-NEXT: [[I0]] = call i32 @f(i32 0)
+; CHECK-NEXT: br label [[COMMON_RET:%.*]]
+; CHECK: else:
+; CHECK-NEXT: [[I1]] = call i32 @f(i32 1)
+; CHECK-NEXT: br label [[COMMON_RET]]
+;
+ switch i8 %arg, label %else [
+ i8 0, label %if
+ i8 -3, label %if
+ i8 -2, label %if
+ i8 -1, label %if
+ ]
+
+if:
+ %i0 = call i32 @f(i32 0)
+ ret i32 %i0
+
+else:
+ %i1 = call i32 @f(i32 1)
+ ret i32 %i1
+}
+
+define i32 @no_continuous_wrapping_range(i8 %arg) {
+; CHECK-LABEL: @no_continuous_wrapping_range(
+; CHECK-NEXT: switch i8 [[ARG:%.*]], label [[ELSE:%.*]] [
+; CHECK-NEXT: i8 0, label [[IF:%.*]]
+; CHECK-NEXT: i8 -3, label [[IF]]
+; CHECK-NEXT: i8 -1, label [[IF]]
+; CHECK-NEXT: ]
+; CHECK: common.ret:
+; CHECK-NEXT: [[COMMON_RET_OP:%.*]] = phi i32 [ [[I0:%.*]], [[IF]] ], [ [[I1:%.*]], [[ELSE]] ]
+; CHECK-NEXT: ret i32 [[COMMON_RET_OP]]
+; CHECK: if:
+; CHECK-NEXT: [[I0]] = call i32 @f(i32 0)
+; CHECK-NEXT: br label [[COMMON_RET:%.*]]
+; CHECK: else:
+; CHECK-NEXT: [[I1]] = call i32 @f(i32 1)
+; CHECK-NEXT: br label [[COMMON_RET]]
+;
+ switch i8 %arg, label %else [
+ i8 0, label %if
+ i8 -3, label %if
+ i8 -1, label %if
+ ]
+
+if:
+ %i0 = call i32 @f(i32 0)
+ ret i32 %i0
+
+else:
+ %i1 = call i32 @f(i32 1)
+ ret i32 %i1
+}
+
declare void @bar(ptr nonnull dereferenceable(4))
>From 9b25b14018ed2584681450ed2dbe4809228518fd Mon Sep 17 00:00:00 2001
From: dianqk <dianqk at dianqk.net>
Date: Fri, 26 Sep 2025 23:12:09 +0800
Subject: [PATCH 2/3] [SimplifyCFG] Fold the contiguous wrapping cases into
ICmp.
---
llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 73 +++++++++++++++----
.../SimplifyCFG/switch-range-to-icmp.ll | 17 ++---
2 files changed, 64 insertions(+), 26 deletions(-)
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 5e719c6c8cbb7..b02aed3f8f5ea 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -5761,15 +5761,49 @@ bool SimplifyCFGOpt::simplifyUnreachable(UnreachableInst *UI) {
return Changed;
}
-static bool casesAreContiguous(SmallVectorImpl<ConstantInt *> &Cases) {
+static bool casesAreContiguous(Value *Condition,
+ SmallVectorImpl<ConstantInt *> &Cases,
+ ConstantInt *&ContiguousCasesMin,
+ ConstantInt *&ContiguousCasesMax,
+ bool &IsWrapping) {
assert(Cases.size() >= 1);
array_pod_sort(Cases.begin(), Cases.end(), constantIntSortPredicate);
- for (size_t I = 1, E = Cases.size(); I != E; ++I) {
- if (Cases[I - 1]->getValue() != Cases[I]->getValue() + 1)
+ auto Min = Cases.back()->getValue();
+ auto Max = Cases.front()->getValue();
+ auto Offset = Max - Min;
+ auto ContiguousOffset = Cases.size() - 1;
+ if (Offset == ContiguousOffset) {
+ ContiguousCasesMin = Cases.back();
+ ContiguousCasesMax = Cases.front();
+ return true;
+ }
+ ConstantRange CR = computeConstantRange(Condition, /*ForSigned=*/false);
+ // If this is a wrapping contiguous range, that is, [Min, OtherMin] +
+ // [OtherMax, Max] (also [OtherMax, OtherMin]), [OtherMin+1, OtherMax-1] is a
+ // contiguous range for the other destination. N.B. If CR is not a full range,
+ // Max+1 is not equal to Min. It's not continuous in arithmetic.
+ if (Max == CR.getUnsignedMax() && Min == CR.getUnsignedMin()) {
+ assert(Cases.size() >= 2);
+ auto *It =
+ std::adjacent_find(Cases.begin(), Cases.end(), [](auto L, auto R) {
+ return L->getValue() != R->getValue() + 1;
+ });
+ if (It == Cases.end())
return false;
+ auto *OtherMax = *It;
+ auto *OtherMin = *(It + 1);
+ if ((Max - OtherMax->getValue()) + (OtherMin->getValue() - Min) ==
+ Cases.size() - 2) {
+ ContiguousCasesMin = cast<ConstantInt>(
+ ConstantInt::get(OtherMin->getType(), OtherMin->getValue() + 1));
+ ContiguousCasesMax = cast<ConstantInt>(
+ ConstantInt::get(OtherMax->getType(), OtherMax->getValue() - 1));
+ IsWrapping = true;
+ return true;
+ }
}
- return true;
+ return false;
}
static void createUnreachableSwitchDefault(SwitchInst *Switch,
@@ -5840,25 +5874,34 @@ bool SimplifyCFGOpt::turnSwitchRangeIntoICmp(SwitchInst *SI,
assert(!CasesA.empty() || HasDefault);
// Figure out if one of the sets of cases form a contiguous range.
- SmallVectorImpl<ConstantInt *> *ContiguousCases = nullptr;
+ ConstantInt *ContiguousCasesMin = nullptr;
+ ConstantInt *ContiguousCasesMax = nullptr;
BasicBlock *ContiguousDest = nullptr;
BasicBlock *OtherDest = nullptr;
- if (!CasesA.empty() && casesAreContiguous(CasesA)) {
- ContiguousCases = &CasesA;
+ bool IsWrapping = false;
+ if (!CasesA.empty() &&
+ casesAreContiguous(SI->getCondition(), CasesA, ContiguousCasesMin,
+ ContiguousCasesMax, IsWrapping)) {
ContiguousDest = DestA;
OtherDest = DestB;
- } else if (casesAreContiguous(CasesB)) {
- ContiguousCases = &CasesB;
+ } else if (casesAreContiguous(SI->getCondition(), CasesB, ContiguousCasesMin,
+ ContiguousCasesMax, IsWrapping)) {
ContiguousDest = DestB;
OtherDest = DestA;
} else
return false;
+ if (IsWrapping)
+ std::swap(ContiguousDest, OtherDest);
+
// Start building the compare and branch.
- Constant *Offset = ConstantExpr::getNeg(ContiguousCases->back());
- Constant *NumCases =
- ConstantInt::get(Offset->getType(), ContiguousCases->size());
+ auto ContiguousCasesSize =
+ (ContiguousCasesMax->getValue() - ContiguousCasesMin->getValue())
+ .getZExtValue() +
+ 1;
+ Constant *Offset = ConstantExpr::getNeg(ContiguousCasesMin);
+ Constant *NumCases = ConstantInt::get(Offset->getType(), ContiguousCasesSize);
Value *Sub = SI->getCondition();
if (!Offset->isNullValue())
@@ -5866,7 +5909,7 @@ bool SimplifyCFGOpt::turnSwitchRangeIntoICmp(SwitchInst *SI,
Value *Cmp;
// If NumCases overflowed, then all possible values jump to the successor.
- if (NumCases->isNullValue() && !ContiguousCases->empty())
+ if (NumCases->isNullValue() && ContiguousCasesSize != 0)
Cmp = ConstantInt::getTrue(SI->getContext());
else
Cmp = Builder.CreateICmpULT(Sub, NumCases, "switch");
@@ -5895,14 +5938,14 @@ bool SimplifyCFGOpt::turnSwitchRangeIntoICmp(SwitchInst *SI,
// Prune obsolete incoming values off the successors' PHI nodes.
for (auto BBI = ContiguousDest->begin(); isa<PHINode>(BBI); ++BBI) {
- unsigned PreviousEdges = ContiguousCases->size();
+ unsigned PreviousEdges = ContiguousCasesSize;
if (ContiguousDest == SI->getDefaultDest())
++PreviousEdges;
for (unsigned I = 0, E = PreviousEdges - 1; I != E; ++I)
cast<PHINode>(BBI)->removeIncomingValue(SI->getParent());
}
for (auto BBI = OtherDest->begin(); isa<PHINode>(BBI); ++BBI) {
- unsigned PreviousEdges = SI->getNumCases() - ContiguousCases->size();
+ unsigned PreviousEdges = SI->getNumCases() - ContiguousCasesSize;
if (OtherDest == SI->getDefaultDest())
++PreviousEdges;
for (unsigned I = 0, E = PreviousEdges - 1; I != E; ++I)
diff --git a/llvm/test/Transforms/SimplifyCFG/switch-range-to-icmp.ll b/llvm/test/Transforms/SimplifyCFG/switch-range-to-icmp.ll
index 73771b48e21bc..c6904fe273f09 100644
--- a/llvm/test/Transforms/SimplifyCFG/switch-range-to-icmp.ll
+++ b/llvm/test/Transforms/SimplifyCFG/switch-range-to-icmp.ll
@@ -190,11 +190,9 @@ exit:
define i32 @wrapping_known_range(i8 range(i8 0, 6) %arg) {
; CHECK-LABEL: @wrapping_known_range(
-; CHECK-NEXT: switch i8 [[ARG:%.*]], label [[ELSE:%.*]] [
-; CHECK-NEXT: i8 0, label [[IF:%.*]]
-; CHECK-NEXT: i8 4, label [[IF]]
-; CHECK-NEXT: i8 5, label [[IF]]
-; CHECK-NEXT: ]
+; CHECK-NEXT: [[ARG_OFF:%.*]] = add i8 [[ARG:%.*]], -1
+; CHECK-NEXT: [[SWITCH:%.*]] = icmp ult i8 [[ARG_OFF]], 3
+; CHECK-NEXT: br i1 [[SWITCH]], label [[ELSE:%.*]], label [[IF:%.*]]
; CHECK: common.ret:
; CHECK-NEXT: [[COMMON_RET_OP:%.*]] = phi i32 [ [[I0:%.*]], [[IF]] ], [ [[I1:%.*]], [[ELSE]] ]
; CHECK-NEXT: ret i32 [[COMMON_RET_OP]]
@@ -222,12 +220,9 @@ else:
define i32 @wrapping_range(i8 %arg) {
; CHECK-LABEL: @wrapping_range(
-; CHECK-NEXT: switch i8 [[ARG:%.*]], label [[ELSE:%.*]] [
-; CHECK-NEXT: i8 0, label [[IF:%.*]]
-; CHECK-NEXT: i8 -3, label [[IF]]
-; CHECK-NEXT: i8 -2, label [[IF]]
-; CHECK-NEXT: i8 -1, label [[IF]]
-; CHECK-NEXT: ]
+; CHECK-NEXT: [[ARG_OFF:%.*]] = add i8 [[ARG:%.*]], -1
+; CHECK-NEXT: [[SWITCH:%.*]] = icmp ult i8 [[ARG_OFF]], -4
+; CHECK-NEXT: br i1 [[SWITCH]], label [[ELSE:%.*]], label [[IF:%.*]]
; CHECK: common.ret:
; CHECK-NEXT: [[COMMON_RET_OP:%.*]] = phi i32 [ [[I0:%.*]], [[IF]] ], [ [[I1:%.*]], [[ELSE]] ]
; CHECK-NEXT: ret i32 [[COMMON_RET_OP]]
>From 40a8372e5a4984b68a8e1ef5419ea67452a8eb31 Mon Sep 17 00:00:00 2001
From: dianqk <dianqk at dianqk.net>
Date: Sat, 27 Sep 2025 22:35:46 +0800
Subject: [PATCH 3/3] Fix the count of pruning incoming values
---
llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 21 ++++++++++--------
.../SimplifyCFG/switch-range-to-icmp.ll | 22 +++++++++++++++++++
2 files changed, 34 insertions(+), 9 deletions(-)
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index b02aed3f8f5ea..21fe3f29c4ff2 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -5879,6 +5879,8 @@ bool SimplifyCFGOpt::turnSwitchRangeIntoICmp(SwitchInst *SI,
BasicBlock *ContiguousDest = nullptr;
BasicBlock *OtherDest = nullptr;
bool IsWrapping = false;
+ SmallVectorImpl<ConstantInt *> *ContiguousCases = &CasesA;
+ SmallVectorImpl<ConstantInt *> *OtherCases = &CasesB;
if (!CasesA.empty() &&
casesAreContiguous(SI->getCondition(), CasesA, ContiguousCasesMin,
ContiguousCasesMax, IsWrapping)) {
@@ -5888,20 +5890,21 @@ bool SimplifyCFGOpt::turnSwitchRangeIntoICmp(SwitchInst *SI,
ContiguousCasesMax, IsWrapping)) {
ContiguousDest = DestB;
OtherDest = DestA;
+ std::swap(ContiguousCases, OtherCases);
} else
return false;
- if (IsWrapping)
+ if (IsWrapping) {
std::swap(ContiguousDest, OtherDest);
+ std::swap(ContiguousCases, OtherCases);
+ }
// Start building the compare and branch.
- auto ContiguousCasesSize =
- (ContiguousCasesMax->getValue() - ContiguousCasesMin->getValue())
- .getZExtValue() +
- 1;
Constant *Offset = ConstantExpr::getNeg(ContiguousCasesMin);
- Constant *NumCases = ConstantInt::get(Offset->getType(), ContiguousCasesSize);
+ Constant *NumCases = ConstantInt::get(Offset->getType(),
+ ContiguousCasesMax->getValue() -
+ ContiguousCasesMin->getValue() + 1);
Value *Sub = SI->getCondition();
if (!Offset->isNullValue())
@@ -5909,7 +5912,7 @@ bool SimplifyCFGOpt::turnSwitchRangeIntoICmp(SwitchInst *SI,
Value *Cmp;
// If NumCases overflowed, then all possible values jump to the successor.
- if (NumCases->isNullValue() && ContiguousCasesSize != 0)
+ if (NumCases->isNullValue() && !ContiguousCases->empty())
Cmp = ConstantInt::getTrue(SI->getContext());
else
Cmp = Builder.CreateICmpULT(Sub, NumCases, "switch");
@@ -5938,14 +5941,14 @@ bool SimplifyCFGOpt::turnSwitchRangeIntoICmp(SwitchInst *SI,
// Prune obsolete incoming values off the successors' PHI nodes.
for (auto BBI = ContiguousDest->begin(); isa<PHINode>(BBI); ++BBI) {
- unsigned PreviousEdges = ContiguousCasesSize;
+ unsigned PreviousEdges = ContiguousCases->size();
if (ContiguousDest == SI->getDefaultDest())
++PreviousEdges;
for (unsigned I = 0, E = PreviousEdges - 1; I != E; ++I)
cast<PHINode>(BBI)->removeIncomingValue(SI->getParent());
}
for (auto BBI = OtherDest->begin(); isa<PHINode>(BBI); ++BBI) {
- unsigned PreviousEdges = SI->getNumCases() - ContiguousCasesSize;
+ unsigned PreviousEdges = OtherCases->size();
if (OtherDest == SI->getDefaultDest())
++PreviousEdges;
for (unsigned I = 0, E = PreviousEdges - 1; I != E; ++I)
diff --git a/llvm/test/Transforms/SimplifyCFG/switch-range-to-icmp.ll b/llvm/test/Transforms/SimplifyCFG/switch-range-to-icmp.ll
index c6904fe273f09..c9d9e9abdcba9 100644
--- a/llvm/test/Transforms/SimplifyCFG/switch-range-to-icmp.ll
+++ b/llvm/test/Transforms/SimplifyCFG/switch-range-to-icmp.ll
@@ -249,6 +249,28 @@ else:
ret i32 %i1
}
+define i8 @wrapping_range_phi(i8 %arg) {
+; CHECK-LABEL: @wrapping_range_phi(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[ARG_OFF:%.*]] = add i8 [[ARG:%.*]], -1
+; CHECK-NEXT: [[SWITCH:%.*]] = icmp ult i8 [[ARG_OFF]], -2
+; CHECK-NEXT: [[SPEC_SELECT:%.*]] = select i1 [[SWITCH]], i8 0, i8 1
+; CHECK-NEXT: ret i8 [[SPEC_SELECT]]
+;
+entry:
+ switch i8 %arg, label %else [
+ i8 0, label %if
+ i8 -1, label %if
+ ]
+
+if:
+ %i = phi i8 [ 0, %else ], [ 1, %entry ], [ 1, %entry ]
+ ret i8 %i
+
+else:
+ br label %if
+}
+
define i32 @no_continuous_wrapping_range(i8 %arg) {
; CHECK-LABEL: @no_continuous_wrapping_range(
; CHECK-NEXT: switch i8 [[ARG:%.*]], label [[ELSE:%.*]] [
More information about the llvm-commits
mailing list