[PATCH] D97064: [SCEV] Infer known bits from known sign bits
Philip Reames via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Feb 19 09:45:57 PST 2021
reames created this revision.
reames added reviewers: lebedev.ri, mkazantsev.
Herald added subscribers: dantrushin, hiraditya, mcrosier.
Herald added a reviewer: bollu.
reames requested review of this revision.
Herald added a project: LLVM.
This was suggested by lebedev.ri over on D96534 <https://reviews.llvm.org/D96534>.
This seems like a reasonable change, but I haven't been able to find a test case which actually changes output. All of the obvious ones are caught through the existing ashr constant range trick for signed ranges. Despite this, I think this is reasonable to land if reviewers agree.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D97064
Files:
llvm/lib/Analysis/ScalarEvolution.cpp
Index: llvm/lib/Analysis/ScalarEvolution.cpp
===================================================================
--- llvm/lib/Analysis/ScalarEvolution.cpp
+++ llvm/lib/Analysis/ScalarEvolution.cpp
@@ -5848,24 +5848,31 @@
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),
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D97064.325019.patch
Type: text/x-patch
Size: 2086 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210219/e9b85a7c/attachment.bin>
More information about the llvm-commits
mailing list