[llvm] [ValueTracking] Infer `X u<= X +nuw Y` for any Y (PR #75524)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 14 12:06:45 PST 2023


https://github.com/dtcxzyw created https://github.com/llvm/llvm-project/pull/75524

Alive2: https://alive2.llvm.org/ce/z/kiGxCf
Fixes #70374.


>From 41b120e4b8a5762c4a411d214d9ba0ccff1fc7bc Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Fri, 15 Dec 2023 04:00:39 +0800
Subject: [PATCH 1/2] [ValueTracking] Add pre-commit tests from PR70374. NFC.

---
 llvm/test/Transforms/InstSimplify/implies.ll | 31 ++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/llvm/test/Transforms/InstSimplify/implies.ll b/llvm/test/Transforms/InstSimplify/implies.ll
index d72dad95bfbd09..1ed2140a4de8f8 100644
--- a/llvm/test/Transforms/InstSimplify/implies.ll
+++ b/llvm/test/Transforms/InstSimplify/implies.ll
@@ -316,6 +316,37 @@ define i1 @test_uge_icmp(i32 %length.i, i32 %i) {
   ret i1 %res
 }
 
+; Test from PR70374
+define i1 @pr70374(i32 %x, i32 %y, i32 %z) {
+; CHECK-LABEL: @pr70374(
+; CHECK-NEXT:    [[ADD:%.*]] = add nuw i32 [[Y:%.*]], [[Z:%.*]]
+; CHECK-NEXT:    [[CMP1:%.*]] = icmp ule i32 [[ADD]], [[X:%.*]]
+; CHECK-NEXT:    [[CMP2:%.*]] = icmp uge i32 [[X]], [[Y]]
+; CHECK-NEXT:    [[RES:%.*]] = and i1 [[CMP2]], [[CMP1]]
+; CHECK-NEXT:    ret i1 [[RES]]
+;
+  %add = add nuw i32 %y, %z
+  %cmp1 = icmp ule i32 %add, %x
+  %cmp2 = icmp uge i32 %x, %y
+  %res = and i1 %cmp2, %cmp1
+  ret i1 %res
+}
+
+define i1 @test_uge_icmp_value(i32 %length.i, i32 %i, i32 %j) {
+; CHECK-LABEL: @test_uge_icmp_value(
+; CHECK-NEXT:    [[IPLUSJ:%.*]] = add nuw i32 [[I:%.*]], [[J:%.*]]
+; CHECK-NEXT:    [[VAR29:%.*]] = icmp uge i32 [[LENGTH_I:%.*]], [[I]]
+; CHECK-NEXT:    [[VAR30:%.*]] = icmp uge i32 [[LENGTH_I]], [[IPLUSJ]]
+; CHECK-NEXT:    [[RES:%.*]] = icmp ule i1 [[VAR30]], [[VAR29]]
+; CHECK-NEXT:    ret i1 [[RES]]
+;
+  %iplusj = add nuw i32 %i, %j
+  %var29 = icmp uge i32 %length.i, %i
+  %var30 = icmp uge i32 %length.i, %iplusj
+  %res = icmp ule i1 %var30, %var29
+  ret i1 %res
+}
+
 ; negative case, X + 1 <(s) Y !==> X <(s) Y (X = 0x7fffffff, Y = 0x7fbfffff)
 define i1 @test_sgt_icmp_no_nsw(i32 %length.i, i32 %i) {
 ; CHECK-LABEL: @test_sgt_icmp_no_nsw(

>From c29fa5abc3fbf76c8eee8c94982d118c619d2153 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Fri, 15 Dec 2023 04:03:07 +0800
Subject: [PATCH 2/2] [ValueTracking] Infer `X u<= X +nuw Y` for any Y

---
 llvm/lib/Analysis/ValueTracking.cpp          |  7 +++----
 llvm/test/Transforms/InstSimplify/implies.ll | 10 ++--------
 2 files changed, 5 insertions(+), 12 deletions(-)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 5445746ab2a1bc..8f32a1bad98ceb 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -8102,10 +8102,9 @@ static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS,
   }
 
   case CmpInst::ICMP_ULE: {
-    const APInt *C;
-
-    // LHS u<= LHS +_{nuw} C   for any C
-    if (match(RHS, m_NUWAdd(m_Specific(LHS), m_APInt(C))))
+    // LHS u<= LHS +_{nuw} V for any V
+    if (match(RHS, m_c_Add(m_Specific(LHS), m_Value())) &&
+        cast<OverflowingBinaryOperator>(RHS)->hasNoUnsignedWrap())
       return true;
 
     // RHS >> V u<= RHS for any V
diff --git a/llvm/test/Transforms/InstSimplify/implies.ll b/llvm/test/Transforms/InstSimplify/implies.ll
index 1ed2140a4de8f8..c31f772b184fc4 100644
--- a/llvm/test/Transforms/InstSimplify/implies.ll
+++ b/llvm/test/Transforms/InstSimplify/implies.ll
@@ -321,9 +321,7 @@ define i1 @pr70374(i32 %x, i32 %y, i32 %z) {
 ; CHECK-LABEL: @pr70374(
 ; CHECK-NEXT:    [[ADD:%.*]] = add nuw i32 [[Y:%.*]], [[Z:%.*]]
 ; CHECK-NEXT:    [[CMP1:%.*]] = icmp ule i32 [[ADD]], [[X:%.*]]
-; CHECK-NEXT:    [[CMP2:%.*]] = icmp uge i32 [[X]], [[Y]]
-; CHECK-NEXT:    [[RES:%.*]] = and i1 [[CMP2]], [[CMP1]]
-; CHECK-NEXT:    ret i1 [[RES]]
+; CHECK-NEXT:    ret i1 [[CMP1]]
 ;
   %add = add nuw i32 %y, %z
   %cmp1 = icmp ule i32 %add, %x
@@ -334,11 +332,7 @@ define i1 @pr70374(i32 %x, i32 %y, i32 %z) {
 
 define i1 @test_uge_icmp_value(i32 %length.i, i32 %i, i32 %j) {
 ; CHECK-LABEL: @test_uge_icmp_value(
-; CHECK-NEXT:    [[IPLUSJ:%.*]] = add nuw i32 [[I:%.*]], [[J:%.*]]
-; CHECK-NEXT:    [[VAR29:%.*]] = icmp uge i32 [[LENGTH_I:%.*]], [[I]]
-; CHECK-NEXT:    [[VAR30:%.*]] = icmp uge i32 [[LENGTH_I]], [[IPLUSJ]]
-; CHECK-NEXT:    [[RES:%.*]] = icmp ule i1 [[VAR30]], [[VAR29]]
-; CHECK-NEXT:    ret i1 [[RES]]
+; CHECK-NEXT:    ret i1 true
 ;
   %iplusj = add nuw i32 %i, %j
   %var29 = icmp uge i32 %length.i, %i



More information about the llvm-commits mailing list