[llvm] cb9f1aa - [ValueTracking] Implied conditions for lshr

Joshua Cao via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 6 21:08:35 PDT 2023


Author: Joshua Cao
Date: 2023-06-06T21:06:22-07:00
New Revision: cb9f1aaddac0ce6bf9386309e33e104ba3f9359c

URL: https://github.com/llvm/llvm-project/commit/cb9f1aaddac0ce6bf9386309e33e104ba3f9359c
DIFF: https://github.com/llvm/llvm-project/commit/cb9f1aaddac0ce6bf9386309e33e104ba3f9359c.diff

LOG: [ValueTracking] Implied conditions for lshr

`V1 >> V2 u<= V1` for any V1, V2

This works for lshr and any div's that are changed to lshr's

This fixes issues in clang and rustc:
https://github.com/llvm/llvm-project/issues/62441
https://github.com/rust-lang/rust/issues/110971

Reviewed By: goldstein.w.n

Differential Revision: https://reviews.llvm.org/D151541

Added: 
    

Modified: 
    llvm/lib/Analysis/ValueTracking.cpp
    llvm/test/Transforms/InstSimplify/implies.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index c1c38c60d6897..5393be0e247b4 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -7913,6 +7913,10 @@ static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS,
     if (match(RHS, m_NUWAdd(m_Specific(LHS), m_APInt(C))))
       return true;
 
+    // RHS >> V u<= RHS for any V
+    if (match(LHS, m_LShr(m_Specific(RHS), m_Value())))
+      return true;
+
     // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
     auto MatchNUWAddsToSameValue = [&](const Value *A, const Value *B,
                                        const Value *&X,

diff  --git a/llvm/test/Transforms/InstSimplify/implies.ll b/llvm/test/Transforms/InstSimplify/implies.ll
index c8e3d10a40659..41f1995428473 100644
--- a/llvm/test/Transforms/InstSimplify/implies.ll
+++ b/llvm/test/Transforms/InstSimplify/implies.ll
@@ -449,9 +449,8 @@ define i1 @test_shift(i64 %x, i64 %y, i64 %shift) {
 ; CHECK-LABEL: @test_shift(
 ; CHECK-NEXT:    [[LSHR:%.*]] = lshr i64 [[X:%.*]], [[SHIFT:%.*]]
 ; CHECK-NEXT:    [[ICMP1:%.*]] = icmp ugt i64 [[LSHR]], [[Y:%.*]]
-; CHECK-NEXT:    [[ICMP2:%.*]] = icmp ugt i64 [[X]], [[Y]]
 ; CHECK-NEXT:    call void @llvm.assume(i1 [[ICMP1]])
-; CHECK-NEXT:    ret i1 [[ICMP2]]
+; CHECK-NEXT:    ret i1 true
 ;
   %lshr = lshr i64 %x, %shift
   %icmp1 = icmp ugt i64 %lshr, %y
@@ -479,11 +478,7 @@ define i1 @assume_x_ugt_y_plus_1(i64  %x, i64  %y)  {
 ; i <u L ==> i >> C <u L
 define i1 @lshr_constant(i32 %length.i, i32 %i) {
 ; CHECK-LABEL: @lshr_constant(
-; CHECK-NEXT:    [[SHL:%.*]] = lshr i32 [[I:%.*]], 1
-; CHECK-NEXT:    [[VAR29:%.*]] = icmp ult i32 [[I]], [[LENGTH_I:%.*]]
-; CHECK-NEXT:    [[VAR30:%.*]] = icmp ult i32 [[SHL]], [[LENGTH_I]]
-; CHECK-NEXT:    [[RES:%.*]] = icmp ule i1 [[VAR29]], [[VAR30]]
-; CHECK-NEXT:    ret i1 [[RES]]
+; CHECK-NEXT:    ret i1 true
 ;
   %shl = lshr i32 %i, 1
   %var29 = icmp ult i32 %i, %length.i
@@ -495,11 +490,7 @@ define i1 @lshr_constant(i32 %length.i, i32 %i) {
 ; i <u L ==> i >> V <u L
 define i1 @lshr_value(i32 %length.i, i32 %i, i32 %v) {
 ; CHECK-LABEL: @lshr_value(
-; CHECK-NEXT:    [[SHL:%.*]] = lshr i32 [[I:%.*]], [[V:%.*]]
-; CHECK-NEXT:    [[VAR29:%.*]] = icmp ult i32 [[I]], [[LENGTH_I:%.*]]
-; CHECK-NEXT:    [[VAR30:%.*]] = icmp ult i32 [[SHL]], [[LENGTH_I]]
-; CHECK-NEXT:    [[RES:%.*]] = icmp ule i1 [[VAR29]], [[VAR30]]
-; CHECK-NEXT:    ret i1 [[RES]]
+; CHECK-NEXT:    ret i1 true
 ;
   %shl = lshr i32 %i, %v
   %var29 = icmp ult i32 %i, %length.i


        


More information about the llvm-commits mailing list