[llvm-branch-commits] [llvm] [SelectionDAG] Fold EXTRACT_SUBVECTOR(EXTRACT_SUBVECTOR(X, C1), C0) with nonzero indices (PR #200935)
Krzysztof Drewniak via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue Jun 2 23:14:35 PDT 2026
https://github.com/krzysz00 updated https://github.com/llvm/llvm-project/pull/200935
>From a739b772a2e7ad504a2cae267e6ac4e818599a69 Mon Sep 17 00:00:00 2001
From: Krzysztof Drewniak <Krzysztof.Drewniak at amd.com>
Date: Fri, 29 May 2026 22:40:54 +0000
Subject: [PATCH] [SelectionDAG] Fold nonzero extract-of-extract indices
Generalize the extract_subvector-of-extract_subvector fold to compose
nonzero indices instead of only handling an outer index of zero.
AI note: an LLM generated the code and the test, I've read them
Co-Authored-By: OpenAI Codex <codex at openai.com>
---
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 0c9820fb64de9..889ea53079a9c 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -27559,18 +27559,18 @@ SDValue DAGCombiner::visitEXTRACT_SUBVECTOR(SDNode *N) {
return NarrowLoad;
// Combine an extract of an extract into a single extract_subvector.
- // ext (ext X, C), 0 --> ext X, C
- if (ExtIdx == 0 && V.getOpcode() == ISD::EXTRACT_SUBVECTOR && V.hasOneUse()) {
+ // ext (ext X, C1), C2 --> ext X, C1 + C2
+ if (V.getOpcode() == ISD::EXTRACT_SUBVECTOR && V.hasOneUse()) {
// Both indices must have the same scaling factor and C has to be a
// multiple of the new result type's known minimum vector length.
+ uint64_t InnerExtIdx = V.getConstantOperandVal(1);
+ uint64_t NewExtIdx = InnerExtIdx + ExtIdx;
if (V.getValueType().isScalableVector() == NVT.isScalableVector() &&
- V.getConstantOperandVal(1) % NVT.getVectorMinNumElements() == 0 &&
+ NewExtIdx % NVT.getVectorMinNumElements() == 0 &&
TLI.isExtractSubvectorCheap(NVT, V.getOperand(0).getValueType(),
- V.getConstantOperandVal(1)) &&
- TLI.isOperationLegalOrCustom(ISD::EXTRACT_SUBVECTOR, NVT)) {
- return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, NVT, V.getOperand(0),
- V.getOperand(1));
- }
+ NewExtIdx) &&
+ TLI.isOperationLegalOrCustom(ISD::EXTRACT_SUBVECTOR, NVT))
+ return DAG.getExtractSubvector(DL, NVT, V.getOperand(0), NewExtIdx);
}
// ty1 extract_vector(ty2 splat(V))) -> ty1 splat(V)
More information about the llvm-branch-commits
mailing list