[llvm] [CVP] Propagate constant range on icmp at use level (PR #73767)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Dec 15 01:18:36 PST 2023
https://github.com/XChy updated https://github.com/llvm/llvm-project/pull/73767
>From d963c03c9adb99223ef345a275a067be9b94f634 Mon Sep 17 00:00:00 2001
From: XChy <xxs_chy at outlook.com>
Date: Tue, 28 Nov 2023 19:55:00 +0800
Subject: [PATCH 1/2] [CVP] Precommit test for constant propagation of icmp
---
.../CorrelatedValuePropagation/icmp.ll | 102 ++++++++++++++++++
1 file changed, 102 insertions(+)
diff --git a/llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll b/llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll
index 101820a4c65f23..ff9b5df666860c 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
+}
>From 8210eb07ed31b2c30c8d1cfab013d72fa1c99a85 Mon Sep 17 00:00:00 2001
From: XChy <xxs_chy at outlook.com>
Date: Sun, 26 Nov 2023 16:22:29 +0800
Subject: [PATCH 2/2] [CVP] Propagate constant range on icmp at use level
---
llvm/include/llvm/Analysis/LazyValueInfo.h | 5 +++
llvm/lib/Analysis/LazyValueInfo.cpp | 33 +++++++++++++++++++
.../Scalar/CorrelatedValuePropagation.cpp | 21 ++++++------
.../CorrelatedValuePropagation/icmp.ll | 5 ---
.../CorrelatedValuePropagation/sub.ll | 2 +-
5 files changed, 50 insertions(+), 16 deletions(-)
diff --git a/llvm/include/llvm/Analysis/LazyValueInfo.h b/llvm/include/llvm/Analysis/LazyValueInfo.h
index cb5fa35d299510..25a943f21c6fbd 100644
--- a/llvm/include/llvm/Analysis/LazyValueInfo.h
+++ b/llvm/include/llvm/Analysis/LazyValueInfo.h
@@ -87,6 +87,11 @@ namespace llvm {
Tristate getPredicateAt(unsigned Pred, Value *LHS, Value *RHS,
Instruction *CxtI, bool UseBlockValue);
+ /// Determine whether the specified value comparison is known to be true
+ /// or false at the specified use-site.
+ /// \p Pred is a CmpInst predicate.
+ Tristate getPredicateAtUse(unsigned Pred, const Use &LHS, const Use &RHS);
+
/// Determine whether the specified value is known to be a constant at the
/// specified instruction. Return null if not.
Constant *getConstant(Value *V, Instruction *CxtI);
diff --git a/llvm/lib/Analysis/LazyValueInfo.cpp b/llvm/lib/Analysis/LazyValueInfo.cpp
index 910f6b72afefe2..76d5fd9b6024d1 100644
--- a/llvm/lib/Analysis/LazyValueInfo.cpp
+++ b/llvm/lib/Analysis/LazyValueInfo.cpp
@@ -1932,6 +1932,39 @@ LazyValueInfo::Tristate LazyValueInfo::getPredicateAt(unsigned P, Value *LHS,
return LazyValueInfo::Unknown;
}
+LazyValueInfo::Tristate
+LazyValueInfo::getPredicateAtUse(unsigned P, const Use &LHS, const Use &RHS) {
+ CmpInst::Predicate Pred = (CmpInst::Predicate)P;
+ Instruction *CxtI = cast<Instruction>(LHS.getUser());
+ Module *M = CxtI->getModule();
+
+ LazyValueInfo::Tristate Ret = LazyValueInfo::Unknown;
+ if (auto *C = dyn_cast<Constant>(RHS))
+ Ret = getPredicateAt(Pred, LHS, C, CxtI, true);
+ else if (auto *C = dyn_cast<Constant>(LHS))
+ Ret =
+ getPredicateAt(CmpInst::getSwappedPredicate(Pred), RHS, C, CxtI, true);
+
+ if (Ret != LazyValueInfo::Unknown)
+ return Ret;
+
+ ValueLatticeElement L = getOrCreateImpl(M).getValueAtUse(LHS);
+ if (L.isOverdefined())
+ return LazyValueInfo::Unknown;
+
+ ValueLatticeElement R = getOrCreateImpl(M).getValueAtUse(RHS);
+
+ Type *Ty = CmpInst::makeCmpResultType(LHS->getType());
+ if (Constant *Res = L.getCompare(Pred, Ty, R, M->getDataLayout())) {
+ if (Res->isNullValue())
+ return LazyValueInfo::False;
+ if (Res->isOneValue())
+ return LazyValueInfo::True;
+ }
+
+ return LazyValueInfo::Unknown;
+}
+
void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,
BasicBlock *NewSucc) {
if (auto *Impl = getImpl())
diff --git a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
index d2dfc764d042bb..5c407ff05c87eb 100644
--- a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
+++ b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
@@ -275,13 +275,14 @@ static bool processICmp(ICmpInst *Cmp, LazyValueInfo *LVI) {
if (!Cmp->isSigned())
return false;
+ ConstantRange LHSRange =
+ LVI->getConstantRangeAtUse(Cmp->getOperandUse(0), /*UndefAllowed*/ true);
+ ConstantRange RHSRange =
+ LVI->getConstantRangeAtUse(Cmp->getOperandUse(1), /*UndefAllowed*/ true);
+
ICmpInst::Predicate UnsignedPred =
- ConstantRange::getEquivalentPredWithFlippedSignedness(
- Cmp->getPredicate(),
- LVI->getConstantRangeAtUse(Cmp->getOperandUse(0),
- /*UndefAllowed*/ true),
- LVI->getConstantRangeAtUse(Cmp->getOperandUse(1),
- /*UndefAllowed*/ true));
+ ConstantRange::getEquivalentPredWithFlippedSignedness(Cmp->getPredicate(),
+ LHSRange, RHSRange);
if (UnsignedPred == ICmpInst::Predicate::BAD_ICMP_PREDICATE)
return false;
@@ -297,11 +298,11 @@ static bool processICmp(ICmpInst *Cmp, LazyValueInfo *LVI) {
/// conditions, this can sometimes prove conditions instcombine can't by
/// exploiting range information.
static bool constantFoldCmp(CmpInst *Cmp, LazyValueInfo *LVI) {
- Value *Op0 = Cmp->getOperand(0);
- Value *Op1 = Cmp->getOperand(1);
+ Use &Op0 = Cmp->getOperandUse(0);
+ Use &Op1 = Cmp->getOperandUse(1);
+
LazyValueInfo::Tristate Result =
- LVI->getPredicateAt(Cmp->getPredicate(), Op0, Op1, Cmp,
- /*UseBlockValue=*/true);
+ LVI->getPredicateAtUse(Cmp->getPredicate(), Op0, Op1);
if (Result == LazyValueInfo::Unknown)
return false;
diff --git a/llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll b/llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll
index ff9b5df666860c..cf1e4aa4313fd2 100644
--- a/llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll
+++ b/llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll
@@ -1460,7 +1460,6 @@ 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]]
@@ -1477,8 +1476,6 @@ 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]]
;
@@ -1495,7 +1492,6 @@ define i1 @select_ternary_icmp3(i32 noundef %a) {
; 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]]
;
@@ -1511,7 +1507,6 @@ 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]]
diff --git a/llvm/test/Transforms/CorrelatedValuePropagation/sub.ll b/llvm/test/Transforms/CorrelatedValuePropagation/sub.ll
index 8b67574c06cd95..b03d7368238127 100644
--- a/llvm/test/Transforms/CorrelatedValuePropagation/sub.ll
+++ b/llvm/test/Transforms/CorrelatedValuePropagation/sub.ll
@@ -300,7 +300,7 @@ exit:
@limit = external global i32
define i32 @test11(ptr %p, i32 %i) {
; CHECK-LABEL: @test11(
-; CHECK-NEXT: [[LIMIT:%.*]] = load i32, ptr [[P:%.*]], !range !0
+; CHECK-NEXT: [[LIMIT:%.*]] = load i32, ptr [[P:%.*]], align 4, !range [[RNG0:![0-9]+]]
; CHECK-NEXT: [[WITHIN_1:%.*]] = icmp slt i32 [[LIMIT]], [[I:%.*]]
; CHECK-NEXT: [[I_MINUS_7:%.*]] = add i32 [[I]], -7
; CHECK-NEXT: [[WITHIN_2:%.*]] = icmp slt i32 [[LIMIT]], [[I_MINUS_7]]
More information about the llvm-commits
mailing list