[PATCH] D151541: [ValueTracking] Implied conditions for lshr

Joshua Cao via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri May 26 01:53:08 PDT 2023


caojoshua created this revision.
Herald added subscribers: JDevlieghere, hiraditya.
Herald added a project: All.
caojoshua added reviewers: nikic, arsenm, goldstein.w.n.
Herald added a subscriber: StephenFan.
caojoshua added a reviewer: floatshadow.
caojoshua added a comment.
caojoshua published this revision for review.
Herald added subscribers: llvm-commits, wdng.
Herald added a project: LLVM.

I've confirmed this fixes the clang issue. Not sure how to test for rustc, but it should fix based on what I'm seeing in the IR.


`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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D151541

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


Index: llvm/test/Transforms/InstSimplify/implies.ll
===================================================================
--- llvm/test/Transforms/InstSimplify/implies.ll
+++ llvm/test/Transforms/InstSimplify/implies.ll
@@ -449,9 +449,8 @@
 ; 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 @@
 ; 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 @@
 ; 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
Index: llvm/lib/Analysis/ValueTracking.cpp
===================================================================
--- llvm/lib/Analysis/ValueTracking.cpp
+++ llvm/lib/Analysis/ValueTracking.cpp
@@ -8024,12 +8024,17 @@
   }
 
   case CmpInst::ICMP_ULE: {
+    const Value *V;
     const APInt *C;
 
     // LHS u<= LHS +_{nuw} C   for any C
     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(V))))
+      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,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D151541.525972.patch
Type: text/x-patch
Size: 2420 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230526/4e6dece4/attachment.bin>


More information about the llvm-commits mailing list