[ValueTracking] Implied conditions for lshr
authorJoshua Cao <cao.joshua@yahoo.com>
Wed, 7 Jun 2023 03:27:24 +0000 (20:27 -0700)
committerJoshua Cao <cao.joshua@yahoo.com>
Wed, 7 Jun 2023 04:06:22 +0000 (21:06 -0700)
`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

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

index c1c38c6..5393be0 100644 (file)
@@ -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,
index c8e3d10..41f1995 100644 (file)
@@ -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