Skip to content

[X86] shouldReduceLoadWidth - don't split loads if we can freely reuse full width legal binop #129695

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 4 commits into from
Apr 29, 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
52 changes: 44 additions & 8 deletions llvm/lib/Target/X86/X86ISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3264,6 +3264,12 @@ bool X86TargetLowering::shouldReduceLoadWidth(
std::optional<unsigned> ByteOffset) const {
assert(cast<LoadSDNode>(Load)->isSimple() && "illegal to narrow");

auto PeekThroughOneUserBitcasts = [](const SDNode *N) {
while (N->getOpcode() == ISD::BITCAST && N->hasOneUse())
N = *N->user_begin();
return N;
};

// "ELF Handling for Thread-Local Storage" specifies that R_X86_64_GOTTPOFF
// relocation target a movq or addq instruction: don't let the load shrink.
SDValue BasePtr = cast<LoadSDNode>(Load)->getBasePtr();
Expand All @@ -3273,24 +3279,46 @@ bool X86TargetLowering::shouldReduceLoadWidth(

// If this is an (1) AVX vector load with (2) multiple uses and (3) all of
// those uses are extracted directly into a store, then the extract + store
// can be store-folded. Therefore, it's probably not worth splitting the load.
// can be store-folded, or (4) any use will be used by legal full width
// instruction. Then, it's probably not worth splitting the load.
EVT VT = Load->getValueType(0);
if ((VT.is256BitVector() || VT.is512BitVector()) &&
!SDValue(Load, 0).hasOneUse()) {
bool FullWidthUse = false;
bool AllExtractStores = true;
for (SDUse &Use : Load->uses()) {
// Skip uses of the chain value. Result 0 of the node is the load value.
if (Use.getResNo() != 0)
continue;

SDNode *User = Use.getUser();
const SDNode *User = PeekThroughOneUserBitcasts(Use.getUser());

// If this use is not an extract + store, it's probably worth splitting.
if (User->getOpcode() != ISD::EXTRACT_SUBVECTOR || !User->hasOneUse() ||
User->user_begin()->getOpcode() != ISD::STORE)
return true;
// If this use is an extract + store, it's probably not worth splitting.
if (User->getOpcode() == ISD::EXTRACT_SUBVECTOR &&
all_of(User->uses(), [&](const SDUse &U) {
const SDNode *Inner = PeekThroughOneUserBitcasts(U.getUser());
return Inner->getOpcode() == ISD::STORE;
}))
continue;

AllExtractStores = false;

// If any use is a full width legal/target bin op, then assume its legal
// and won't split.
if (isBinOp(User->getOpcode()) &&
(isOperationLegal(User->getOpcode(), User->getValueType(0)) ||
User->getOpcode() > ISD::BUILTIN_OP_END))
FullWidthUse = true;
}
// All non-chain uses are extract + store.
return false;

if (AllExtractStores)
return false;

// If we have an user that uses the full vector width, then this use is
// only worth splitting if the offset isn't 0 (to avoid an
// EXTRACT_SUBVECTOR) or we're loading a scalar integer.
if (FullWidthUse)
return (ByteOffset.value_or(0) > 0) || NewVT.isScalarInteger();
}

return true;
Expand Down Expand Up @@ -59154,6 +59182,14 @@ static SDValue combineINSERT_SUBVECTOR(SDNode *N, SelectionDAG &DAG,
return Res;
}

// Match insertion of subvector load that perfectly aliases a base load.
if ((IdxVal % SubVecNumElts) == 0 && ISD::isNormalLoad(Vec.getNode()) &&
ISD::isNormalLoad(SubVec.getNode()) &&
DAG.areNonVolatileConsecutiveLoads(
cast<LoadSDNode>(SubVec), cast<LoadSDNode>(Vec),
SubVec.getValueSizeInBits() / 8, IdxVal / SubVecNumElts))
return Vec;

return SDValue();
}

Expand Down
7 changes: 5 additions & 2 deletions llvm/lib/Target/X86/X86ISelLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -1337,15 +1337,18 @@ namespace llvm {
unsigned Depth) const override;

bool isTargetCanonicalConstantNode(SDValue Op) const override {
// Peek through bitcasts/extracts/inserts to see if we have a broadcast
// vector from memory.
// Peek through bitcasts/extracts/inserts to see if we have a vector
// load/broadcast from memory.
while (Op.getOpcode() == ISD::BITCAST ||
Op.getOpcode() == ISD::EXTRACT_SUBVECTOR ||
(Op.getOpcode() == ISD::INSERT_SUBVECTOR &&
Op.getOperand(0).isUndef()))
Op = Op.getOperand(Op.getOpcode() == ISD::INSERT_SUBVECTOR ? 1 : 0);

return Op.getOpcode() == X86ISD::VBROADCAST_LOAD ||
Op.getOpcode() == X86ISD::SUBV_BROADCAST_LOAD ||
(Op.getOpcode() == ISD::LOAD &&
getTargetConstantFromLoad(cast<LoadSDNode>(Op))) ||
TargetLowering::isTargetCanonicalConstantNode(Op);
}

Expand Down
8 changes: 4 additions & 4 deletions llvm/test/CodeGen/X86/oddsubvector.ll
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,9 @@ define void @PR42833() {
;
; AVX2-LABEL: PR42833:
; AVX2: # %bb.0:
; AVX2-NEXT: movl b(%rip), %eax
; AVX2-NEXT: vmovdqu c+128(%rip), %ymm0
; AVX2-NEXT: addl c+128(%rip), %eax
; AVX2-NEXT: vmovd %xmm0, %eax
; AVX2-NEXT: addl b(%rip), %eax
; AVX2-NEXT: vmovd %eax, %xmm1
; AVX2-NEXT: vpaddd %ymm1, %ymm0, %ymm2
; AVX2-NEXT: vpaddd %ymm0, %ymm0, %ymm3
Expand All @@ -284,10 +284,10 @@ define void @PR42833() {
;
; AVX512-LABEL: PR42833:
; AVX512: # %bb.0:
; AVX512-NEXT: movl b(%rip), %eax
; AVX512-NEXT: vmovdqu c+128(%rip), %ymm0
; AVX512-NEXT: vmovdqu64 c+128(%rip), %zmm1
; AVX512-NEXT: addl c+128(%rip), %eax
; AVX512-NEXT: vmovd %xmm0, %eax
; AVX512-NEXT: addl b(%rip), %eax
; AVX512-NEXT: vmovd %eax, %xmm2
; AVX512-NEXT: vpaddd %ymm2, %ymm0, %ymm2
; AVX512-NEXT: vpaddd %ymm0, %ymm0, %ymm0
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/X86/setcc-lowering.ll
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ define <8 x i16> @pr25080(<8 x i32> %a) nounwind {
; AVX1-LABEL: pr25080:
; AVX1: # %bb.0: # %entry
; AVX1-NEXT: vextractf128 $1, %ymm0, %xmm0
; AVX1-NEXT: vpand {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0, %xmm0
; AVX1-NEXT: vpand {{\.?LCPI[0-9]+_[0-9]+}}+16(%rip), %xmm0, %xmm0
; AVX1-NEXT: vpxor %xmm1, %xmm1, %xmm1
; AVX1-NEXT: vpcmpeqd %xmm1, %xmm0, %xmm0
; AVX1-NEXT: vpackssdw %xmm0, %xmm0, %xmm0
Expand Down
Loading