[llvm] [CVP] Propagate constant range on icmp at use level (PR #73767)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Nov 29 00:57:58 PST 2023
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: XChy (XChy)
<details>
<summary>Changes</summary>
Solve a shallow version of #<!-- -->71383.
`ProcessICmp` in CVP is used to flip the signedness of `icmp` predicate. This patch reuses the constant range to directly fold `icmp` at use level.
However, this patch cannot solve #<!-- -->71383 now, for InstCombine canonicalizes `select cond, binop(a, C1), op(a, C2)` to `binop(a, select cond, C1, C2)`.
---
Full diff: https://github.com/llvm/llvm-project/pull/73767.diff
2 Files Affected:
- (modified) llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp (+19-4)
- (modified) llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll (+102)
``````````diff
diff --git a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
index 1bdc441d18ea65e..9e947a7e6d39c83 100644
--- a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
+++ b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
@@ -275,11 +275,26 @@ static bool processICmp(ICmpInst *Cmp, LazyValueInfo *LVI) {
if (!Cmp->isSigned())
return false;
+ auto LHSRange = LVI->getConstantRangeAtUse(Cmp->getOperandUse(0));
+ auto RHSRange = LVI->getConstantRangeAtUse(Cmp->getOperandUse(1));
+
+ // NOTE: It's not the original purpose.
+ // Do simple constant propagation at use level,
+ if (Cmp->hasOneUse()) {
+ if (LHSRange.icmp(Cmp->getPredicate(), RHSRange)) {
+ Cmp->replaceAllUsesWith(Constant::getAllOnesValue(Cmp->getType()));
+ return true;
+ }
+
+ if (LHSRange.icmp(Cmp->getInversePredicate(), RHSRange)) {
+ Cmp->replaceAllUsesWith(Constant::getNullValue(Cmp->getType()));
+ return true;
+ }
+ }
+
ICmpInst::Predicate UnsignedPred =
- ConstantRange::getEquivalentPredWithFlippedSignedness(
- Cmp->getPredicate(),
- LVI->getConstantRangeAtUse(Cmp->getOperandUse(0)),
- LVI->getConstantRangeAtUse(Cmp->getOperandUse(1)));
+ ConstantRange::getEquivalentPredWithFlippedSignedness(Cmp->getPredicate(),
+ LHSRange, RHSRange);
if (UnsignedPred == ICmpInst::Predicate::BAD_ICMP_PREDICATE)
return false;
diff --git a/llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll b/llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll
index 101820a4c65f23b..ff9b5df666860c4 100644
--- a/llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll
+++ b/llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll
@@ -1455,3 +1455,105 @@ entry:
%select = select i1 %cmp1, i1 %cmp2, i1 false
ret i1 %select
}
+
+define i1 @select_ternary_icmp1(i32 noundef %a) {
+; CHECK-LABEL: @select_ternary_icmp1(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[A:%.*]], 3
+; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 [[A]], 5
+; CHECK-NEXT: [[CMP2:%.*]] = icmp ult i32 [[A]], 7
+; CHECK-NEXT: [[COND_V:%.*]] = select i1 [[CMP]], i1 true, i1 [[CMP2]]
+; CHECK-NEXT: ret i1 [[COND_V]]
+;
+entry:
+ %cmp = icmp slt i32 %a, 3
+ %cmp1 = icmp slt i32 %a, 5
+ %cmp2 = icmp slt i32 %a, 7
+ %cond.v = select i1 %cmp, i1 %cmp1, i1 %cmp2
+ ret i1 %cond.v
+}
+
+define i1 @select_ternary_icmp2(i32 noundef %a) {
+; CHECK-LABEL: @select_ternary_icmp2(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[A:%.*]], 5
+; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 [[A]], 7
+; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i32 [[A]], 3
+; CHECK-NEXT: [[COND_V:%.*]] = select i1 [[CMP]], i1 true, i1 false
+; CHECK-NEXT: ret i1 [[COND_V]]
+;
+entry:
+ %cmp = icmp slt i32 %a, 5
+ %cmp1 = icmp slt i32 %a, 7
+ %cmp2 = icmp slt i32 %a, 3
+ %cond.v = select i1 %cmp, i1 %cmp1, i1 %cmp2
+ ret i1 %cond.v
+}
+
+define i1 @select_ternary_icmp3(i32 noundef %a) {
+; CHECK-LABEL: @select_ternary_icmp3(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[A:%.*]], 7
+; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 [[A]], 3
+; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i32 [[A]], 5
+; CHECK-NEXT: [[COND_V:%.*]] = select i1 [[CMP]], i1 [[CMP1]], i1 false
+; CHECK-NEXT: ret i1 [[COND_V]]
+;
+entry:
+ %cmp = icmp slt i32 %a, 7
+ %cmp1 = icmp slt i32 %a, 3
+ %cmp2 = icmp slt i32 %a, 5
+ %cond.v = select i1 %cmp, i1 %cmp1, i1 %cmp2
+ ret i1 %cond.v
+}
+
+define i1 @select_ternary_icmp3_reverse(i32 noundef %a) {
+; CHECK-LABEL: @select_ternary_icmp3_reverse(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[A:%.*]], 3
+; CHECK-NEXT: [[CMP1:%.*]] = icmp sge i32 [[A]], 5
+; CHECK-NEXT: [[CMP2:%.*]] = icmp uge i32 [[A]], 7
+; CHECK-NEXT: [[COND_V:%.*]] = select i1 [[CMP]], i1 false, i1 [[CMP2]]
+; CHECK-NEXT: ret i1 [[COND_V]]
+;
+entry:
+ %cmp = icmp slt i32 %a, 3
+ %cmp1 = icmp sge i32 %a, 5
+ %cmp2 = icmp sge i32 %a, 7
+ %cond.v = select i1 %cmp, i1 %cmp1, i1 %cmp2
+ ret i1 %cond.v
+}
+
+define i1 @select_ternary_icmp_fail1(i32 noundef %a, i32 noundef %b) {
+; CHECK-LABEL: @select_ternary_icmp_fail1(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[A:%.*]], 3
+; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 [[B:%.*]], 5
+; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i32 [[B]], 7
+; CHECK-NEXT: [[COND_V:%.*]] = select i1 [[CMP]], i1 [[CMP1]], i1 [[CMP2]]
+; CHECK-NEXT: ret i1 [[COND_V]]
+;
+entry:
+ %cmp = icmp slt i32 %a, 3
+ %cmp1 = icmp slt i32 %b, 5
+ %cmp2 = icmp slt i32 %b, 7
+ %cond.v = select i1 %cmp, i1 %cmp1, i1 %cmp2
+ ret i1 %cond.v
+}
+
+define i1 @select_ternary_icmp_fail2(i32 noundef %a, i32 noundef %b) {
+; CHECK-LABEL: @select_ternary_icmp_fail2(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[A:%.*]], 3
+; CHECK-NEXT: [[CMP1:%.*]] = icmp sge i32 5, [[B:%.*]]
+; CHECK-NEXT: [[CMP2:%.*]] = icmp sge i32 7, [[B]]
+; CHECK-NEXT: [[COND_V:%.*]] = select i1 [[CMP]], i1 [[CMP1]], i1 [[CMP2]]
+; CHECK-NEXT: ret i1 [[COND_V]]
+;
+entry:
+ %cmp = icmp slt i32 %a, 3
+ %cmp1 = icmp sge i32 5, %b
+ %cmp2 = icmp sge i32 7, %b
+ %cond.v = select i1 %cmp, i1 %cmp1, i1 %cmp2
+ ret i1 %cond.v
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/73767
More information about the llvm-commits
mailing list