[PATCH] D149409: [ValueTracking] Handle bitcasts between vec-int-ptr in `isKnownNonZero`

Noah Goldstein via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Apr 29 09:12:44 PDT 2023


goldstein.w.n updated this revision to Diff 518173.
goldstein.w.n added a comment.

Fix correctness issue with casting for larger element type to smaller element type


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D149409/new/

https://reviews.llvm.org/D149409

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


Index: llvm/test/Analysis/ValueTracking/known-non-zero.ll
===================================================================
--- llvm/test/Analysis/ValueTracking/known-non-zero.ll
+++ llvm/test/Analysis/ValueTracking/known-non-zero.ll
@@ -596,11 +596,7 @@
 
 define i1 @bitcast_nonzero(<2 x i8> %xx, i16 %ind) {
 ; CHECK-LABEL: @bitcast_nonzero(
-; CHECK-NEXT:    [[XA:%.*]] = add nuw nsw <2 x i8> [[XX:%.*]], <i8 1, i8 1>
-; CHECK-NEXT:    [[X:%.*]] = bitcast <2 x i8> [[XA]] to i16
-; CHECK-NEXT:    [[Z:%.*]] = or i16 [[X]], [[IND:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = icmp eq i16 [[Z]], 0
-; CHECK-NEXT:    ret i1 [[R]]
+; CHECK-NEXT:    ret i1 false
 ;
   %xa = add nuw nsw <2 x i8> %xx, <i8 1, i8 1>
   %x = bitcast <2 x i8> %xa to i16
@@ -641,11 +637,7 @@
 
 define <2 x i1> @bitcast_veci8_to_veci16(<4 x i8> %xx, <2 x i16> %ind) {
 ; CHECK-LABEL: @bitcast_veci8_to_veci16(
-; CHECK-NEXT:    [[XA:%.*]] = add nuw nsw <4 x i8> [[XX:%.*]], <i8 1, i8 1, i8 1, i8 1>
-; CHECK-NEXT:    [[X:%.*]] = bitcast <4 x i8> [[XA]] to <2 x i16>
-; CHECK-NEXT:    [[Z:%.*]] = or <2 x i16> [[X]], [[IND:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = icmp eq <2 x i16> [[Z]], zeroinitializer
-; CHECK-NEXT:    ret <2 x i1> [[R]]
+; CHECK-NEXT:    ret <2 x i1> zeroinitializer
 ;
   %xa = add nuw nsw <4 x i8> %xx, <i8 1, i8 1, i8 1, i8 1>
   %x = bitcast <4 x i8> %xa to <2 x i16>
Index: llvm/lib/Analysis/ValueTracking.cpp
===================================================================
--- llvm/lib/Analysis/ValueTracking.cpp
+++ llvm/lib/Analysis/ValueTracking.cpp
@@ -2741,10 +2741,31 @@
     if (I->getType()->isPointerTy())
       return isGEPKnownNonNull(cast<GEPOperator>(I), Depth, Q);
     break;
-  case Instruction::BitCast:
+  case Instruction::BitCast: {
     if (I->getType()->isPointerTy())
       return isKnownNonZero(I->getOperand(0), Depth, Q);
-    break;
+
+    // We need to be a bit careful here. We can only peek through the bitcast
+    // if the scalar size of elements in the operand are smaller than the size
+    // they are casting two. Take two cases:
+    //
+    // 1) Unsafe:
+    //        bitcast <2 x i16> %NonZero to <4 x i8>
+
+    //    %NonZero can have 2 non-zero i16 elements, but isKnownNonZero on a
+    //    <4 x i8> requires that all 4 i8 elements be non-zero which isn't
+    //    guranteed (imagine just sign bit set in the 2 i16 elements).
+
+    // 2) Safe:
+    //        bitcast <4 x i8> %NonZero to <2 x i16>
+    //    This is always safe as non-zero in the 4 i8 elements implies
+    //    non-zero in the combination of any two adjacent ones which in turn
+    //    implies the 2 i16 elements are non-zero.
+    Type *FromTy = I->getOperand(0)->getType();
+    if ((FromTy->isIntOrIntVectorTy() || FromTy->isPtrOrPtrVectorTy()) &&
+        getBitWidth(FromTy->getScalarType(), Q.DL) <= BitWidth)
+      return isKnownNonZero(I->getOperand(0), Depth, Q);
+  } break;
   case Instruction::IntToPtr:
     // Note that we have to take special care to avoid looking through
     // truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149409.518173.patch
Type: text/x-patch
Size: 3091 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230429/d7badac0/attachment.bin>


More information about the llvm-commits mailing list