[llvm] a25b537 - [SCEV] Infer known bits from known sign bits

Philip Reames via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 9 12:37:23 PST 2021


Author: Philip Reames
Date: 2021-03-09T12:37:17-08:00
New Revision: a25b537bf437864232cb5826539eb4cee7c47b74

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

LOG: [SCEV] Infer known bits from known sign bits

This was suggested by lebedev.ri over on D96534.  You'll note lack of tests.  During review, we weren't actually able to find a case which exercises it, but both I and lebedev.ri feel it's a reasonable change, straight forward, and near free.

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

Added: 
    

Modified: 
    llvm/lib/Analysis/ScalarEvolution.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index b8d55e6eb68a3..7ffb384673869 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -5848,24 +5848,31 @@ ScalarEvolution::getRangeRef(const SCEV *S,
     KnownBits Known = computeKnownBits(U->getValue(), DL, 0, &AC, nullptr, &DT);
     if (Known.getBitWidth() != BitWidth)
       Known = Known.zextOrTrunc(BitWidth);
-    // If Known does not result in full-set, intersect with it.
-    if (Known.getMinValue() != Known.getMaxValue() + 1)
-      ConservativeResult = ConservativeResult.intersectWith(
-          ConstantRange(Known.getMinValue(), Known.getMaxValue() + 1),
-          RangeType);
 
     // ValueTracking may be able to compute a tighter result for the number of
     // sign bits than for the value of those sign bits.
     unsigned NS = ComputeNumSignBits(U->getValue(), DL, 0, &AC, nullptr, &DT);
-    // If the pointer size is larger than the index size type, this can cause
-    // NS to be larger than BitWidth. So compensate for this.
     if (U->getType()->isPointerTy()) {
+      // If the pointer size is larger than the index size type, this can cause
+      // NS to be larger than BitWidth. So compensate for this.
       unsigned ptrSize = DL.getPointerTypeSizeInBits(U->getType());
       int ptrIdxDiff = ptrSize - BitWidth;
       if (ptrIdxDiff > 0 && ptrSize > BitWidth && NS > (unsigned)ptrIdxDiff)
         NS -= ptrIdxDiff;
     }
 
+    if (NS > 1) {
+      // If we know any of the sign bits, we know all of the sign bits.
+      if (!Known.Zero.getHiBits(NS).isNullValue())
+        Known.Zero.setHighBits(NS);
+      if (!Known.One.getHiBits(NS).isNullValue())
+        Known.One.setHighBits(NS);
+    }
+
+    if (Known.getMinValue() != Known.getMaxValue() + 1)
+      ConservativeResult = ConservativeResult.intersectWith(
+          ConstantRange(Known.getMinValue(), Known.getMaxValue() + 1),
+          RangeType);
     if (NS > 1)
       ConservativeResult = ConservativeResult.intersectWith(
           ConstantRange(APInt::getSignedMinValue(BitWidth).ashr(NS - 1),


        


More information about the llvm-commits mailing list