[llvm] f688d21 - [ValueTracking] Add `shl nsw %val, %cnt != 0` if `%val != 0`.

Noah Goldstein via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 14 16:24:01 PDT 2023


Author: Noah Goldstein
Date: 2023-04-14T18:23:47-05:00
New Revision: f688d215e53cdeaaa90b317bb476acd6cebabf2c

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

LOG: [ValueTracking] Add `shl nsw %val, %cnt != 0` if `%val != 0`.

Alive2 Link: https://alive2.llvm.org/ce/z/mxZLJn

Reviewed By: nikic

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

Added: 
    

Modified: 
    llvm/lib/Analysis/ValueTracking.cpp
    llvm/test/Analysis/ValueTracking/known-non-zero.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 3a9e80c0e5b98..eae6261cfe131 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -2670,9 +2670,9 @@ bool isKnownNonZero(const Value *V, const APInt &DemandedElts, unsigned Depth,
     return isKnownNonZero(I->getOperand(0), Depth, Q);
 
   case Instruction::Shl: {
-    // shl nuw can't remove any non-zero bits.
+    // shl nsw/nuw can't remove any non-zero bits.
     const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V);
-    if (Q.IIQ.hasNoUnsignedWrap(BO))
+    if (Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO))
       return isKnownNonZero(I->getOperand(0), Depth, Q);
 
     // shl X, Y != 0 if X is odd.  Note that the value of the shift is undefined

diff  --git a/llvm/test/Analysis/ValueTracking/known-non-zero.ll b/llvm/test/Analysis/ValueTracking/known-non-zero.ll
index 68ec1ef107aa9..f6a04403cfb42 100644
--- a/llvm/test/Analysis/ValueTracking/known-non-zero.ll
+++ b/llvm/test/Analysis/ValueTracking/known-non-zero.ll
@@ -227,9 +227,7 @@ define i1 @shl_non_zero_nsw(i8 %s, i8 %cnt) {
 ; CHECK-LABEL: @shl_non_zero_nsw(
 ; CHECK-NEXT:    [[NZ:%.*]] = icmp ne i8 [[S:%.*]], 0
 ; CHECK-NEXT:    call void @llvm.assume(i1 [[NZ]])
-; CHECK-NEXT:    [[V:%.*]] = shl nsw i8 [[S]], [[CNT:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = icmp eq i8 [[V]], 0
-; CHECK-NEXT:    ret i1 [[R]]
+; CHECK-NEXT:    ret i1 false
 ;
   %nz = icmp ne i8 %s, 0
   call void @llvm.assume(i1 %nz)


        


More information about the llvm-commits mailing list