[PATCH] D79202: [SVE] Fix invalid usages of getNumElements() in ValueTracking

Christopher Tetreault via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 30 14:00:24 PDT 2020


ctetreau created this revision.
Herald added subscribers: llvm-commits, psnobl, rkruppe, hiraditya, tschuett.
Herald added a reviewer: rengolin.
Herald added a reviewer: efriedma.
Herald added a project: LLVM.
ctetreau updated this revision to Diff 261341.
ctetreau added a comment.
ctetreau added reviewers: kmclaughlin, sdesmalen, spatel.
ctetreau added a child revision: D79053: [SVE] Fix invalid uses of VectorType::getNumElements().

minor stylistic changes


Fix invalid usage of getNumElements in ComputeNumSignBits.
getNumElements was being used to compute the DemandedElts. Since we
can't know the length of a scalable vector, we will instead ignore the
value of DemandedElts, and bail out in any codepath that needs it.

Fix invalid usages of getNumElements in
computeNumSignBitsVectorConstant. Bail out early if the vector is
scalable.

Identified by test case LLVM.Transforms/InstCombine::nsw.ll


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D79202

Files:
  llvm/lib/Analysis/ValueTracking.cpp


Index: llvm/lib/Analysis/ValueTracking.cpp
===================================================================
--- llvm/lib/Analysis/ValueTracking.cpp
+++ llvm/lib/Analysis/ValueTracking.cpp
@@ -373,10 +373,9 @@
 static unsigned ComputeNumSignBits(const Value *V, unsigned Depth,
                                    const Query &Q) {
   Type *Ty = V->getType();
+  auto *FVTy = dyn_cast<FixedVectorType>(Ty);
   APInt DemandedElts =
-      Ty->isVectorTy()
-          ? APInt::getAllOnesValue(cast<VectorType>(Ty)->getNumElements())
-          : APInt(1, 1);
+      FVTy ? APInt::getAllOnesValue(FVTy->getNumElements()) : APInt(1, 1);
   return ComputeNumSignBits(V, DemandedElts, Depth, Q);
 }
 
@@ -2638,19 +2637,19 @@
   return CLow->sle(*CHigh);
 }
 
-/// For vector constants, loop over the elements and find the constant with the
-/// minimum number of sign bits. Return 0 if the value is not a vector constant
-/// or if any element was not analyzed; otherwise, return the count for the
-/// element with the minimum number of sign bits.
-static unsigned computeNumSignBitsVectorConstant(const Value *V,
+/// For fixed width vector constants, loop over the elements and find the
+/// constant with the minimum number of sign bits. Return 0 if the value is
+/// not a vector constant or if any element was not analyzed; otherwise,
+/// return the count for the element with the minimum number of sign bits.
+static unsigned ComputeNumSignBitsVectorConstant(const Value *V,
                                                  const APInt &DemandedElts,
                                                  unsigned TyBits) {
   const auto *CV = dyn_cast<Constant>(V);
-  if (!CV || !CV->getType()->isVectorTy())
+  if (!CV || !isa<FixedVectorType>(CV->getType()))
     return 0;
 
   unsigned MinSignBits = TyBits;
-  unsigned NumElts = cast<VectorType>(CV->getType())->getNumElements();
+  unsigned NumElts = cast<FixedVectorType>(CV->getType())->getNumElements();
   for (unsigned i = 0; i != NumElts; ++i) {
     if (!DemandedElts[i])
       continue;
@@ -2680,9 +2679,9 @@
 /// the other bits. We know that at least 1 bit is always equal to the sign bit
 /// (itself), but other cases can give us information. For example, immediately
 /// after an "ashr X, 2", we know that the top 3 bits are all equal to each
-/// other, so we return 3. For vectors, return the number of sign bits for the
-/// vector element with the minimum number of known sign bits of the demanded
-/// elements in the vector specified by DemandedElts.
+/// other, so we return 3. For fixed width vectors, return the number of sign
+/// bits for the vector element with the minimum number of known sign bits of
+/// the demanded elements in the vector specified by DemandedElts.
 static unsigned ComputeNumSignBitsImpl(const Value *V,
                                        const APInt &DemandedElts,
                                        unsigned Depth, const Query &Q) {
@@ -2693,9 +2692,10 @@
   // same behavior for poison though -- that's a FIXME today.
 
   Type *Ty = V->getType();
-  assert(((Ty->isVectorTy() && cast<VectorType>(Ty)->getNumElements() ==
-                                   DemandedElts.getBitWidth()) ||
-          (!Ty->isVectorTy() && DemandedElts == APInt(1, 1))) &&
+  assert(((isa<FixedVectorType>(Ty) &&
+           cast<FixedVectorType>(Ty)->getNumElements() ==
+               DemandedElts.getBitWidth()) ||
+          (!isa<FixedVectorType>(Ty) && DemandedElts == APInt(1, 1))) &&
          "Unexpected vector size");
 
   Type *ScalarTy = Ty->getScalarType();
@@ -2967,7 +2967,7 @@
   // If we can examine all elements of a vector constant successfully, we're
   // done (we can't do any better than that). If not, keep trying.
   if (unsigned VecSignBits =
-          computeNumSignBitsVectorConstant(V, DemandedElts, TyBits))
+          ComputeNumSignBitsVectorConstant(V, DemandedElts, TyBits))
     return VecSignBits;
 
   KnownBits Known(TyBits);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D79202.261341.patch
Type: text/x-patch
Size: 3969 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200430/556c44f2/attachment.bin>


More information about the llvm-commits mailing list