Skip to content

[AArch64][SVE] Fold integer lane extract and store to FPR store #129756

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 11 commits into from
Mar 18, 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
67 changes: 62 additions & 5 deletions llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23938,6 +23938,20 @@ static SDValue combineI8TruncStore(StoreSDNode *ST, SelectionDAG &DAG,
return Chain;
}

static unsigned getFPSubregForVT(EVT VT) {
assert(VT.isSimple() && "Expected simple VT");
switch (VT.getSimpleVT().SimpleTy) {
case MVT::f16:
return AArch64::hsub;
case MVT::f32:
return AArch64::ssub;
case MVT::f64:
return AArch64::dsub;
default:
llvm_unreachable("Unexpected VT!");
}
}

static SDValue performSTORECombine(SDNode *N,
TargetLowering::DAGCombinerInfo &DCI,
SelectionDAG &DAG,
Expand Down Expand Up @@ -23998,15 +24012,58 @@ static SDValue performSTORECombine(SDNode *N,
if (SDValue Store = combineBoolVectorAndTruncateStore(DAG, ST))
return Store;

if (ST->isTruncatingStore()) {
EVT StoreVT = ST->getMemoryVT();
if (!isHalvingTruncateOfLegalScalableType(ValueVT, StoreVT))
return SDValue();
if (ST->isTruncatingStore() &&
isHalvingTruncateOfLegalScalableType(ValueVT, MemVT)) {
if (SDValue Rshrnb =
trySimplifySrlAddToRshrnb(ST->getOperand(1), DAG, Subtarget)) {
return DAG.getTruncStore(ST->getChain(), ST, Rshrnb, ST->getBasePtr(),
StoreVT, ST->getMemOperand());
MemVT, ST->getMemOperand());
}
}

// This is an integer vector_extract_elt followed by a (possibly truncating)
// store. We may be able to replace this with a store of an FP subregister.
if (DCI.isAfterLegalizeDAG() && ST->isUnindexed() &&
Value.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {

SDValue Vector = Value.getOperand(0);
SDValue ExtIdx = Value.getOperand(1);
EVT VectorVT = Vector.getValueType();
EVT ElemVT = VectorVT.getVectorElementType();
if (!ValueVT.isInteger() || ElemVT == MVT::i8 || MemVT == MVT::i8)
return SDValue();
if (ValueVT != MemVT && !ST->isTruncatingStore())
return SDValue();

// Heuristic: If there are other users of integer scalars extracted from
// this vector that won't fold into the store -- abandon folding. Applying
// this fold may extend the vector lifetime and disrupt paired stores.
for (const auto &Use : Vector->uses()) {
if (Use.getResNo() != Vector.getResNo())
continue;
const SDNode *User = Use.getUser();
if (User->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
(!User->hasOneUse() ||
(*User->user_begin())->getOpcode() != ISD::STORE))
return SDValue();
}

EVT FPElemVT = EVT::getFloatingPointVT(ElemVT.getSizeInBits());
EVT FPVectorVT = VectorVT.changeVectorElementType(FPElemVT);
SDValue Cast = DAG.getNode(ISD::BITCAST, DL, FPVectorVT, Vector);
SDValue Ext =
DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, FPElemVT, Cast, ExtIdx);

EVT FPMemVT = EVT::getFloatingPointVT(MemVT.getSizeInBits());
if (ST->isTruncatingStore() && FPMemVT != FPElemVT) {
SDValue Trunc = DAG.getTargetExtractSubreg(getFPSubregForVT(FPMemVT), DL,
FPMemVT, Ext);
return DAG.getStore(ST->getChain(), DL, Trunc, ST->getBasePtr(),
ST->getMemOperand());
}

return DAG.getStore(ST->getChain(), DL, Ext, ST->getBasePtr(),
ST->getMemOperand());
}

return SDValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ define void @insert_vec_v23i32_uaddlv_from_v8i16(ptr %0) {
; CHECK-NEXT: movi.2d v0, #0000000000000000
; CHECK-NEXT: movi.2d v2, #0000000000000000
; CHECK-NEXT: str wzr, [x0, #88]
; CHECK-NEXT: str xzr, [x0, #80]
; CHECK-NEXT: uaddlv.8h s1, v0
; CHECK-NEXT: stp q0, q0, [x0, #16]
; CHECK-NEXT: stp q0, q0, [x0, #48]
; CHECK-NEXT: str d0, [x0, #80]
; CHECK-NEXT: mov.s v2[0], v1[0]
; CHECK-NEXT: ucvtf.4s v1, v2
; CHECK-NEXT: str q1, [x0]
Expand Down Expand Up @@ -146,11 +146,10 @@ define void @insert_vec_v6i64_uaddlv_from_v4i32(ptr %0) {
; CHECK-LABEL: insert_vec_v6i64_uaddlv_from_v4i32:
; CHECK: ; %bb.0: ; %entry
; CHECK-NEXT: movi.2d v0, #0000000000000000
; CHECK-NEXT: str xzr, [x0, #16]
; CHECK-NEXT: uaddlv.4s d1, v0
; CHECK-NEXT: mov.d v0[0], v1[0]
; CHECK-NEXT: movi.2d v1, #0000000000000000
; CHECK-NEXT: ucvtf.2d v0, v0
; CHECK-NEXT: str d1, [x0, #16]
; CHECK-NEXT: fcvtn v0.2s, v0.2d
; CHECK-NEXT: str q0, [x0]
; CHECK-NEXT: ret
Expand Down
Loading
Loading