[llvm] [CVP] Propagate constant range on icmp at use level (PR #73767)

via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 13 06:23:29 PST 2023


https://github.com/XChy updated https://github.com/llvm/llvm-project/pull/73767

>From 68c5409f2d0dd76b134c7e891075cee0064f4a8c 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 ae0b96a59ed1823ed3c5172ea257aaec6f2bce2e 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           | 26 +++++++++++++++++++
 .../Scalar/CorrelatedValuePropagation.cpp     | 21 ++++++++-------
 .../CorrelatedValuePropagation/basic.ll       |  1 +
 .../CorrelatedValuePropagation/icmp.ll        |  5 ----
 .../CorrelatedValuePropagation/sub.ll         |  2 +-
 6 files changed, 44 insertions(+), 16 deletions(-)

diff --git a/llvm/include/llvm/Analysis/LazyValueInfo.h b/llvm/include/llvm/Analysis/LazyValueInfo.h
index cb5fa35d299510..756d80caaff698 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 P, 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..f0003efefafe91 100644
--- a/llvm/lib/Analysis/LazyValueInfo.cpp
+++ b/llvm/lib/Analysis/LazyValueInfo.cpp
@@ -1932,6 +1932,32 @@ LazyValueInfo::Tristate LazyValueInfo::getPredicateAt(unsigned P, Value *LHS,
   return LazyValueInfo::Unknown;
 }
 
+LazyValueInfo::Tristate
+LazyValueInfo::getPredicateAtUse(unsigned P, const Use &LHS, const Use &RHS) {
+  const Instruction *CxtI = dyn_cast<Instruction>(LHS.getUser());
+
+  if (!CxtI)
+    return LazyValueInfo::Unknown;
+
+  const Module *M = CxtI->getModule();
+
+  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((CmpInst::Predicate)P, 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/basic.ll b/llvm/test/Transforms/CorrelatedValuePropagation/basic.ll
index 9fcf7c320f62dd..3a3f503f376ade 100644
--- a/llvm/test/Transforms/CorrelatedValuePropagation/basic.ll
+++ b/llvm/test/Transforms/CorrelatedValuePropagation/basic.ll
@@ -534,6 +534,7 @@ exit:
 
 define i1 @arg_attribute(ptr nonnull %a) {
 ; CHECK-LABEL: @arg_attribute(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq ptr [[A:%.*]], null
 ; CHECK-NEXT:    ret i1 false
 ;
   %cmp = icmp eq i8* %a, null
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