-
Notifications
You must be signed in to change notification settings - Fork 13.6k
DAG: Preserve range metadata when load is narrowed #128144
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 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8a5fdc1
Preserve range information when load is narrowed
LU-JOHN c9b533d
Reduce 64-bit shl to 32-bit based on range metadata
LU-JOHN 3a9f8de
Add negative test when bounds can't be truncated to i32
LU-JOHN d9b8042
Update testcase comments
LU-JOHN 32bd7e5
Simplify bitwidth checking logic with ConstantRange utility function
LU-JOHN c933174
Make one new range to cover all sub-ranges
LU-JOHN 179869c
Cleanly implement using ConstantRange utility functions
LU-JOHN a95676c
Use C++ comments, don
LU-JOHN 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 |
---|---|---|
|
@@ -14903,12 +14903,47 @@ SDValue DAGCombiner::reduceLoadWidth(SDNode *N) { | |
AddToWorklist(NewPtr.getNode()); | ||
|
||
SDValue Load; | ||
if (ExtType == ISD::NON_EXTLOAD) | ||
Load = DAG.getLoad(VT, DL, LN0->getChain(), NewPtr, | ||
LN0->getPointerInfo().getWithOffset(PtrOff), | ||
LN0->getOriginalAlign(), | ||
LN0->getMemOperand()->getFlags(), LN0->getAAInfo()); | ||
else | ||
if (ExtType == ISD::NON_EXTLOAD) { | ||
const MDNode *OldRanges = LN0->getRanges(); | ||
const MDNode *NewRanges = nullptr; | ||
/* If LSBs are loaded and all bounds in the OldRanges metadata fit in | ||
the narrower size, preserve the range information by translating | ||
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. Do we need new negative tests if it's not in range? 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. Added negative test where bounds cannot fit in 32-bits. |
||
to the the new narrower type, NewTy */ | ||
LU-JOHN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (ShAmt == 0 && OldRanges) { | ||
Type *NewTy = VT.getTypeForEVT(*DAG.getContext()); | ||
const unsigned NumOperands = OldRanges->getNumOperands(); | ||
unsigned NumRanges = NumOperands / 2; | ||
const unsigned NewWidth = NewTy->getIntegerBitWidth(); | ||
LU-JOHN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
bool InRange = true; | ||
SmallVector<Metadata *, 4> Bounds; | ||
Bounds.reserve(NumOperands); | ||
|
||
for (unsigned i = 0; i < NumRanges; ++i) { | ||
const APInt &LowValue = | ||
LU-JOHN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
mdconst::extract<ConstantInt>(OldRanges->getOperand(2 * i)) | ||
->getValue(); | ||
const APInt &HighValue = | ||
LU-JOHN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
mdconst::extract<ConstantInt>(OldRanges->getOperand(2 * i + 1)) | ||
->getValue(); | ||
LU-JOHN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ConstantRange CurRange(LowValue, HighValue); | ||
|
||
if (CurRange.getMinSignedBits() > NewWidth) { | ||
InRange = false; | ||
break; | ||
} | ||
Bounds.push_back(ConstantAsMetadata::get( | ||
ConstantInt::get(NewTy, LowValue.trunc(NewWidth)))); | ||
Bounds.push_back(ConstantAsMetadata::get( | ||
ConstantInt::get(NewTy, HighValue.trunc(NewWidth)))); | ||
} | ||
if (InRange) | ||
NewRanges = MDNode::get(*DAG.getContext(), Bounds); | ||
} | ||
Load = DAG.getLoad( | ||
VT, DL, LN0->getChain(), NewPtr, | ||
LN0->getPointerInfo().getWithOffset(PtrOff), LN0->getOriginalAlign(), | ||
LN0->getMemOperand()->getFlags(), LN0->getAAInfo(), NewRanges); | ||
} else | ||
Load = DAG.getExtLoad(ExtType, DL, VT, LN0->getChain(), NewPtr, | ||
LN0->getPointerInfo().getWithOffset(PtrOff), ExtVT, | ||
LN0->getOriginalAlign(), | ||
|
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.
The core logic ideally would go in a utility function on ConstantRanges, not directly inline here. We will eventually want to share the same logic with globalisel
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.
Used ConstantRange::getMinSignedBits() to implement core logic.