Skip to content

[SelectionDAG][RISCV] Teach computeKnownBits to use range metadata for atomic_load. #137119

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 3 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 39 additions & 10 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4382,6 +4382,43 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
Known.Zero |= APInt::getBitsSetFrom(BitWidth, VT.getScalarSizeInBits());
break;
}
case ISD::ATOMIC_LOAD: {
// If we are looking at the loaded value.
if (Op.getResNo() == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(minor) Possibly could be an assertion, chain result is not a part of data flow

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was keeping consistency with ISD::LOAD. Maybe we should change all of them at once?

auto *AT = cast<AtomicSDNode>(Op);
unsigned ScalarMemorySize = AT->getMemoryVT().getScalarSizeInBits();
KnownBits KnownScalarMemory(ScalarMemorySize);
if (const MDNode *MD = AT->getRanges())
computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);

switch (AT->getExtensionType()) {
case ISD::ZEXTLOAD:
Known = KnownScalarMemory.zext(BitWidth);
break;
case ISD::SEXTLOAD:
Known = KnownScalarMemory.sext(BitWidth);
break;
case ISD::EXTLOAD:
switch (TLI->getExtendForAtomicOps()) {
case ISD::ZERO_EXTEND:
Known = KnownScalarMemory.zext(BitWidth);
break;
case ISD::SIGN_EXTEND:
Known = KnownScalarMemory.sext(BitWidth);
break;
default:
Known = KnownScalarMemory.anyext(BitWidth);
break;
}
break;
case ISD::NON_EXTLOAD:
Known = KnownScalarMemory;
break;
}
assert(Known.getBitWidth() == BitWidth);
}
break;
}
case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
if (Op.getResNo() == 1) {
// The boolean result conforms to getBooleanContents.
Expand All @@ -4407,21 +4444,13 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
case ISD::ATOMIC_LOAD_MIN:
case ISD::ATOMIC_LOAD_MAX:
case ISD::ATOMIC_LOAD_UMIN:
case ISD::ATOMIC_LOAD_UMAX:
case ISD::ATOMIC_LOAD: {
case ISD::ATOMIC_LOAD_UMAX: {
// If we are looking at the loaded value.
if (Op.getResNo() == 0) {
auto *AT = cast<AtomicSDNode>(Op);
unsigned MemBits = AT->getMemoryVT().getScalarSizeInBits();

// For atomic_load, prefer to use the extension type.
if (Op->getOpcode() == ISD::ATOMIC_LOAD) {
if (AT->getExtensionType() == ISD::ZEXTLOAD)
Known.Zero.setBitsFrom(MemBits);
else if (AT->getExtensionType() != ISD::SEXTLOAD &&
TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
Known.Zero.setBitsFrom(MemBits);
} else if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
Known.Zero.setBitsFrom(MemBits);
}
break;
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5150,9 +5150,10 @@ void SelectionDAGBuilder::visitAtomicLoad(const LoadInst &I) {

auto Flags = TLI.getLoadMemOperandFlags(I, DAG.getDataLayout(), AC, LibInfo);

const MDNode *Ranges = getRangeMetadata(I);
MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
I.getAlign(), AAMDNodes(), nullptr, SSID, Order);
I.getAlign(), AAMDNodes(), Ranges, SSID, Order);

InChain = TLI.prepareVolatileOrAtomicLoad(InChain, dl, DAG);

Expand Down
Loading
Loading