Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
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
44 changes: 44 additions & 0 deletions llvm/lib/Target/RISCV/RISCVISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15267,6 +15267,48 @@ static SDValue reverseZExtICmpCombine(SDNode *N, SelectionDAG &DAG,
return DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Res);
}

static SDValue reduceANDOfAtomicLoad(SDNode *N,
TargetLowering::DAGCombinerInfo &DCI) {
SelectionDAG &DAG = DCI.DAG;
if (N->getOpcode() != ISD::AND)
return SDValue();

SDValue N0 = N->getOperand(0);
if (N0.getOpcode() != ISD::ATOMIC_LOAD)
return SDValue();

AtomicSDNode *ALoad = cast<AtomicSDNode>(N0.getNode());
if (isStrongerThanMonotonic(ALoad->getSuccessOrdering()))
return SDValue();

EVT LoadedVT = ALoad->getMemoryVT();
EVT ResultVT = N->getValueType(0);

SDValue MaskVal = N->getOperand(1);
ConstantSDNode *MaskConst = dyn_cast<ConstantSDNode>(MaskVal);
if (!MaskConst)
return SDValue();
uint64_t Mask = MaskConst->getZExtValue();
uint64_t ExpectedMask = LoadedVT.getSizeInBits() == 8 ? 0xFF
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we use maskTrailingOnes<uint64_t>(LoadedVT.getSizeInBits())?

: LoadedVT.getSizeInBits() == 16 ? 0xFFFF
: LoadedVT.getSizeInBits() == 32 ? 0xFFFFFFFF
: 0xFFFFFFFFFFFFFFFF;
if (Mask != ExpectedMask)
return SDValue();

SDLoc DL(N);
SDValue Chain = ALoad->getChain();
SDValue Ptr = ALoad->getBasePtr();
MachineMemOperand *MemOp = ALoad->getMemOperand();
SDValue ZextLoad = DAG.getExtLoad(ISD::ZEXTLOAD, DL, ResultVT, Chain, Ptr,
MemOp->getPointerInfo(), LoadedVT,
MemOp->getAlign(), MemOp->getFlags());
DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), ZextLoad);
Copy link
Collaborator

Choose a reason for hiding this comment

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

We need to make sure that the AND is the only user of the atomic_load data result. If another user depends on the sign extension behavior this will break.

Copy link
Collaborator

@topperc topperc Apr 24, 2025

Choose a reason for hiding this comment

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

We should use DCI.CombineTo(N, ZextLoad) instead of DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), ZextLoad)

DAG.ReplaceAllUsesOfValueWith(SDValue(N0.getNode(), 1), ZextLoad.getValue(1));
DCI.recursivelyDeleteUnusedNodes(N0.getNode());
return SDValue(N, 0);
}

// Combines two comparison operation and logic operation to one selection
// operation(min, max) and logic operation. Returns new constructed Node if
// conditions for optimization are satisfied.
Expand Down Expand Up @@ -15301,6 +15343,8 @@ static SDValue performANDCombine(SDNode *N,
return V;
if (SDValue V = combineBinOpOfExtractToReduceTree(N, DAG, Subtarget))
return V;
if (SDValue V = reduceANDOfAtomicLoad(N, DCI))
return V;

if (DCI.isAfterLegalizeDAG())
if (SDValue V = combineDeMorganOfBoolean(N, DAG))
Expand Down
Loading
Loading