-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[ValueTracking] Fix bit width handling in computeKnownBits() for GEPs #125532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1426,7 +1426,22 @@ static void computeKnownBitsFromOperator(const Operator *I, | |
computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); | ||
// Accumulate the constant indices in a separate variable | ||
// to minimize the number of calls to computeForAddSub. | ||
APInt AccConstIndices(BitWidth, 0, /*IsSigned*/ true); | ||
unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(I->getType()); | ||
APInt AccConstIndices(IndexWidth, 0); | ||
|
||
auto AddIndexToKnown = [&](KnownBits IndexBits) { | ||
if (IndexWidth == BitWidth) { | ||
// Note that inbounds does *not* guarantee nsw for the addition, as only | ||
// the offset is signed, while the base address is unsigned. | ||
Known = KnownBits::add(Known, IndexBits); | ||
} else { | ||
// If the index width is smaller than the pointer width, only add the | ||
// value to the low bits. | ||
assert(IndexWidth < BitWidth && | ||
"Index width can't be larger than pointer width"); | ||
Known.insertBits(KnownBits::add(Known.trunc(IndexWidth), IndexBits), 0); | ||
} | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Maybe rename to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
||
gep_type_iterator GTI = gep_type_begin(I); | ||
for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) { | ||
|
@@ -1464,43 +1479,34 @@ static void computeKnownBitsFromOperator(const Operator *I, | |
break; | ||
} | ||
|
||
unsigned IndexBitWidth = Index->getType()->getScalarSizeInBits(); | ||
KnownBits IndexBits(IndexBitWidth); | ||
computeKnownBits(Index, IndexBits, Depth + 1, Q); | ||
TypeSize IndexTypeSize = GTI.getSequentialElementStride(Q.DL); | ||
uint64_t TypeSizeInBytes = IndexTypeSize.getKnownMinValue(); | ||
KnownBits ScalingFactor(IndexBitWidth); | ||
TypeSize Stride = GTI.getSequentialElementStride(Q.DL); | ||
uint64_t StrideInBytes = Stride.getKnownMinValue(); | ||
if (!Stride.isScalable()) { | ||
// Fast path for constant offset. | ||
if (auto *CI = dyn_cast<ConstantInt>(Index)) { | ||
AccConstIndices += | ||
CI->getValue().sextOrTrunc(IndexWidth) * StrideInBytes; | ||
continue; | ||
} | ||
} | ||
|
||
KnownBits IndexBits = | ||
computeKnownBits(Index, Depth + 1, Q).sextOrTrunc(IndexWidth); | ||
KnownBits ScalingFactor(IndexWidth); | ||
// Multiply by current sizeof type. | ||
// &A[i] == A + i * sizeof(*A[i]). | ||
if (IndexTypeSize.isScalable()) { | ||
if (Stride.isScalable()) { | ||
// For scalable types the only thing we know about sizeof is | ||
// that this is a multiple of the minimum size. | ||
ScalingFactor.Zero.setLowBits(llvm::countr_zero(TypeSizeInBytes)); | ||
} else if (IndexBits.isConstant()) { | ||
APInt IndexConst = IndexBits.getConstant(); | ||
APInt ScalingFactor(IndexBitWidth, TypeSizeInBytes); | ||
IndexConst *= ScalingFactor; | ||
AccConstIndices += IndexConst.sextOrTrunc(BitWidth); | ||
continue; | ||
ScalingFactor.Zero.setLowBits(llvm::countr_zero(StrideInBytes)); | ||
nikic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
ScalingFactor = | ||
KnownBits::makeConstant(APInt(IndexBitWidth, TypeSizeInBytes)); | ||
KnownBits::makeConstant(APInt(IndexWidth, StrideInBytes)); | ||
} | ||
IndexBits = KnownBits::mul(IndexBits, ScalingFactor); | ||
|
||
// If the offsets have a different width from the pointer, according | ||
// to the language reference we need to sign-extend or truncate them | ||
// to the width of the pointer. | ||
IndexBits = IndexBits.sextOrTrunc(BitWidth); | ||
|
||
// Note that inbounds does *not* guarantee nsw for the addition, as only | ||
// the offset is signed, while the base address is unsigned. | ||
Known = KnownBits::add(Known, IndexBits); | ||
} | ||
if (!Known.isUnknown() && !AccConstIndices.isZero()) { | ||
KnownBits Index = KnownBits::makeConstant(AccConstIndices); | ||
Known = KnownBits::add(Known, Index); | ||
AddIndexToKnown(KnownBits::mul(IndexBits, ScalingFactor)); | ||
} | ||
if (!Known.isUnknown() && !AccConstIndices.isZero()) | ||
AddIndexToKnown(KnownBits::makeConstant(AccConstIndices)); | ||
break; | ||
} | ||
case Instruction::PHI: { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Beyond the scope of this PR, but would it be slightly more optimal to start
Known
as zero so we could includensw
here (andnuw
on the final addition) if we haveinbounds
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We cannot preserve
nsw
here because we changed the order of pointer addition (line 1485).