[llvm] [LVI] Generalize mask not equal conditions handling (PR #92946)

Antonio Frighetto via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 6 00:28:25 PDT 2024


https://github.com/antoniofrighetto updated https://github.com/llvm/llvm-project/pull/92946

>From c4593db2824e4941edc7f5c47d25353b3bf41270 Mon Sep 17 00:00:00 2001
From: Antonio Frighetto <me at antoniofrighetto.com>
Date: Thu, 6 Jun 2024 08:25:14 +0200
Subject: [PATCH 1/2] [LVI] Introduce test for PR92946 (NFC)

---
 .../LazyValueAnalysis/lvi-icmp-ne-of-and.ll   | 76 +++++++++++++++++++
 .../CorrelatedValuePropagation/icmp.ll        | 40 +++++++++-
 2 files changed, 115 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/Analysis/LazyValueAnalysis/lvi-icmp-ne-of-and.ll

diff --git a/llvm/test/Analysis/LazyValueAnalysis/lvi-icmp-ne-of-and.ll b/llvm/test/Analysis/LazyValueAnalysis/lvi-icmp-ne-of-and.ll
new file mode 100644
index 0000000000000..ce53c8059f1ae
--- /dev/null
+++ b/llvm/test/Analysis/LazyValueAnalysis/lvi-icmp-ne-of-and.ll
@@ -0,0 +1,76 @@
+; RUN: opt < %s -passes="jump-threading,print<lazy-value-info>" -disable-output 2>&1 | FileCheck %s
+
+; (Val & 4) != 4, ctr_z(M) <= ctr_z(C), sat
+define i1 @test(i4 noundef %arg) {
+entry:
+  %mask = and i4 %arg, 4
+  %cmp = icmp ne i4 %mask, 4
+  br i1 %cmp, label %l_true, label %l_false
+
+; CHECK-LABEL: l_true:
+; CHECK-NEXT:  ; LatticeVal for: 'i4 %arg' is: overdefined
+l_true:
+  ret i1 1
+l_false:
+  ret i1 0
+}
+
+; (Val & 5) != 4, ctr_z(M) <= ctr_z(C), sat
+define i1 @test_2(i4 noundef %arg) {
+entry:
+  %mask = and i4 %arg, 5
+  %cmp = icmp ne i4 %mask, 4
+  br i1 %cmp, label %l_true, label %l_false
+
+; CHECK-LABEL: l_true:
+; CHECK-NEXT:  ; LatticeVal for: 'i4 %arg' is: overdefined
+l_true:
+  ret i1 1
+l_false:
+  ret i1 0
+}
+
+; (Val & 6) != 4, ctr_z(M) <= ctr_z(C), sat
+define i1 @test_3(i4 noundef %arg) {
+entry:
+  %mask = and i4 %arg, 6
+  %cmp = icmp ne i4 %mask, 4
+  br i1 %cmp, label %l_true, label %l_false
+
+; CHECK-LABEL: l_true:
+; CHECK-NEXT:  ; LatticeVal for: 'i4 %arg' is: overdefined
+l_true:
+  ret i1 1
+l_false:
+  ret i1 0
+}
+
+; (Val & 8) != 4, !ctr_z(M) <= ctr_z(C), unsat
+define i1 @test_4(i4 noundef %arg) {
+entry:
+  %mask = and i4 %arg, 8
+  %cmp = icmp ne i4 %mask, 4
+  br i1 %cmp, label %l_true, label %l_false
+
+; CHECK-LABEL: l_true:
+; CHECK-NEXT:  ; LatticeVal for: 'i4 %arg' is: overdefined
+l_true:
+  ret i1 1
+l_false:
+  ret i1 0
+}
+
+; (Val & 8) != 16, ctr_z(M) <= ctr_z(C), !(M >u C), unsat
+define i1 @test_5(i8 noundef %arg) {
+entry:
+  %mask = and i8 %arg, 8
+  %cmp = icmp ne i8 %mask, 16
+  br i1 %cmp, label %l_true, label %l_false
+
+; CHECK-LABEL: l_true:
+; CHECK-NEXT:  ; LatticeVal for: 'i8 %arg' is: overdefined
+l_true:
+  ret i1 1
+l_false:
+  ret i1 0
+}
diff --git a/llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll b/llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll
index b5337b9ddc248..475ebbb923b42 100644
--- a/llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll
+++ b/llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll
@@ -595,7 +595,7 @@ define i1 @test_assume_cmp_with_offset_or(i64 %idx, i1 %other) {
 ; CHECK:       T:
 ; CHECK-NEXT:    ret i1 true
 ; CHECK:       F:
-; CHECK-NEXT:    ret i1 [[CMP2:%.*]]
+; CHECK-NEXT:    ret i1 [[OTHER:%.*]]
 ;
   %idx.off1 = or disjoint i64 %idx, 5
   %cmp1 = icmp ugt i64 %idx.off1, 10
@@ -1475,3 +1475,41 @@ entry:
   %select = select i1 %cmp1, i1 %cmp2, i1 false
   ret i1 %select
 }
+
+declare void @opaque()
+
+define void @test_icmp_ne_from_implied_range(i32 noundef %arg) {
+; CHECK-LABEL: @test_icmp_ne_from_implied_range(
+; CHECK-NEXT:    [[AND_MASK:%.*]] = and i32 [[ARG:%.*]], -8
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[AND_MASK]], -16
+; CHECK-NEXT:    br i1 [[CMP]], label [[END:%.*]], label [[ELSE:%.*]]
+; CHECK:       else:
+; CHECK-NEXT:    switch i32 [[ARG]], label [[END]] [
+; CHECK-NEXT:      i32 -16, label [[SW_CASE:%.*]]
+; CHECK-NEXT:      i32 -12, label [[SW_CASE]]
+; CHECK-NEXT:      i32 -9, label [[SW_CASE]]
+; CHECK-NEXT:    ]
+; CHECK:       sw.case:
+; CHECK-NEXT:    call void @opaque()
+; CHECK-NEXT:    br label [[END]]
+; CHECK:       end:
+; CHECK-NEXT:    ret void
+;
+  %and.mask = and i32 %arg, -8
+  %cmp = icmp eq i32 %and.mask, -16
+  br i1 %cmp, label %end, label %else
+
+else:
+  switch i32 %arg, label %end [
+  i32 -16, label %sw.case
+  i32 -12, label %sw.case
+  i32 -9, label %sw.case
+  ]
+
+sw.case:
+  call void @opaque()
+  br label %end
+
+end:
+  ret void
+}

>From ae24e1f6ee56565d7d960e97ca0b17485113bae2 Mon Sep 17 00:00:00 2001
From: Antonio Frighetto <me at antoniofrighetto.com>
Date: Thu, 6 Jun 2024 08:26:40 +0200
Subject: [PATCH 2/2] [LVI] Generalize mask not equal conditions handling

Extend `V & Mask != 0` for non-zero constants, when retrieving
constraint value information from a non-equality comparison.

Proofs: https://alive2.llvm.org/ce/z/PNDN3K.

Motivating example: https://github.com/gcc-mirror/gcc/blob/master/gcc/testsuite/gcc.dg/tree-ssa/vrp76.c.
---
 llvm/lib/Analysis/LazyValueInfo.cpp           | 25 ++++++++++++++-----
 .../LazyValueAnalysis/lvi-icmp-ne-of-and.ll   |  6 ++---
 .../CorrelatedValuePropagation/icmp.ll        |  6 +----
 3 files changed, 23 insertions(+), 14 deletions(-)

diff --git a/llvm/lib/Analysis/LazyValueInfo.cpp b/llvm/lib/Analysis/LazyValueInfo.cpp
index 6cded828c25f4..5e1b48900c327 100644
--- a/llvm/lib/Analysis/LazyValueInfo.cpp
+++ b/llvm/lib/Analysis/LazyValueInfo.cpp
@@ -1191,12 +1191,25 @@ std::optional<ValueLatticeElement> LazyValueInfoImpl::getValueFromICmpCondition(
       return ValueLatticeElement::getRange(
           ConstantRange::fromKnownBits(Known, /*IsSigned*/ false));
     }
-    // If (Val & Mask) != 0 then the value must be larger than the lowest set
-    // bit of Mask.
-    if (EdgePred == ICmpInst::ICMP_NE && !Mask->isZero() && C->isZero()) {
-      return ValueLatticeElement::getRange(ConstantRange::getNonEmpty(
-          APInt::getOneBitSet(BitWidth, Mask->countr_zero()),
-          APInt::getZero(BitWidth)));
+
+    if (EdgePred == ICmpInst::ICMP_NE && !Mask->isZero()) {
+      // If (Val & Mask) != 0 then the value must be larger than the lowest set
+      // bit of Mask.
+      unsigned TrailingZeroesMask = Mask->countr_zero();
+      if (C->isZero())
+        return ValueLatticeElement::getRange(ConstantRange::getNonEmpty(
+            APInt::getOneBitSet(BitWidth, TrailingZeroesMask),
+            APInt::getZero(BitWidth)));
+
+      unsigned TrailingZeroesC = C->countr_zero();
+      // If (Val & Mask) != C, where Mask >=u C and the number of trailing zeroes
+      // of Mask is at most equal to the number of trailing zeroes of C, then
+      // the value must be larger than the lowest set bit of Mask, offset by
+      // constant C.
+      if (Mask->uge(*C) && TrailingZeroesMask <= TrailingZeroesC) {
+        return ValueLatticeElement::getRange(ConstantRange::getNonEmpty(
+            APInt::getOneBitSet(BitWidth, TrailingZeroesMask) + *C, *C));
+      }
     }
   }
 
diff --git a/llvm/test/Analysis/LazyValueAnalysis/lvi-icmp-ne-of-and.ll b/llvm/test/Analysis/LazyValueAnalysis/lvi-icmp-ne-of-and.ll
index ce53c8059f1ae..7f73715961b44 100644
--- a/llvm/test/Analysis/LazyValueAnalysis/lvi-icmp-ne-of-and.ll
+++ b/llvm/test/Analysis/LazyValueAnalysis/lvi-icmp-ne-of-and.ll
@@ -8,7 +8,7 @@ entry:
   br i1 %cmp, label %l_true, label %l_false
 
 ; CHECK-LABEL: l_true:
-; CHECK-NEXT:  ; LatticeVal for: 'i4 %arg' is: overdefined
+; CHECK-NEXT:  ; LatticeVal for: 'i4 %arg' is: constantrange<-8, 4>
 l_true:
   ret i1 1
 l_false:
@@ -23,7 +23,7 @@ entry:
   br i1 %cmp, label %l_true, label %l_false
 
 ; CHECK-LABEL: l_true:
-; CHECK-NEXT:  ; LatticeVal for: 'i4 %arg' is: overdefined
+; CHECK-NEXT:  ; LatticeVal for: 'i4 %arg' is: constantrange<5, 4>
 l_true:
   ret i1 1
 l_false:
@@ -38,7 +38,7 @@ entry:
   br i1 %cmp, label %l_true, label %l_false
 
 ; CHECK-LABEL: l_true:
-; CHECK-NEXT:  ; LatticeVal for: 'i4 %arg' is: overdefined
+; CHECK-NEXT:  ; LatticeVal for: 'i4 %arg' is: constantrange<6, 4>
 l_true:
   ret i1 1
 l_false:
diff --git a/llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll b/llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll
index 475ebbb923b42..8733979dc339f 100644
--- a/llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll
+++ b/llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll
@@ -1484,11 +1484,7 @@ define void @test_icmp_ne_from_implied_range(i32 noundef %arg) {
 ; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[AND_MASK]], -16
 ; CHECK-NEXT:    br i1 [[CMP]], label [[END:%.*]], label [[ELSE:%.*]]
 ; CHECK:       else:
-; CHECK-NEXT:    switch i32 [[ARG]], label [[END]] [
-; CHECK-NEXT:      i32 -16, label [[SW_CASE:%.*]]
-; CHECK-NEXT:      i32 -12, label [[SW_CASE]]
-; CHECK-NEXT:      i32 -9, label [[SW_CASE]]
-; CHECK-NEXT:    ]
+; CHECK-NEXT:    br label [[END]]
 ; CHECK:       sw.case:
 ; CHECK-NEXT:    call void @opaque()
 ; CHECK-NEXT:    br label [[END]]



More information about the llvm-commits mailing list