[llvm] [DAGCombiner] Fix subvector extraction index for big-endian STLF (PR #180795)

via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 10 09:57:20 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-arm

Author: 陈子昂 (Michael-Chen-NJU)

<details>
<summary>Changes</summary>

This PR fixes a big-endian regression in `ForwardStoreValueToDirectLoad` where the wrong subvector was being extracted. In big-endian, memory offset 0 corresponds to the high bits, so the extraction index needs to be adjusted.

As suggested by @<!-- -->KennethHilmersson, calculate the extraction index as the difference between the number of elements in the intermediate vector and the load vector when in big-endian mode.

Special thanks to Kenneth Hilmersson for providing the fix logic and the ARM regression test.

Co-authored-by: Kenneth Hilmersson

---
Full diff: https://github.com/llvm/llvm-project/pull/180795.diff


2 Files Affected:

- (modified) llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp (+9-1) 
- (added) llvm/test/CodeGen/ARM/stlf-vector-extract-be.ll (+17) 


``````````diff
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index b05157289892b..0f5147ccd92af 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -20831,8 +20831,16 @@ SDValue DAGCombiner::ForwardStoreValueToDirectLoad(LoadSDNode *LD) {
         if (!TLI.isOperationLegalOrCustom(ISD::EXTRACT_SUBVECTOR, InterVT))
           break;
 
+        // In case of big-endian the offset is normalized to zero, denoting
+        // the last bit. For big-endian we need to transform the extraction
+        // to the last sub-vector.
+        unsigned ExtIdx = 0;
+        if (DAG.getDataLayout().isBigEndian()) {
+          ExtIdx =
+              InterVT.getVectorNumElements() - LDMemType.getVectorNumElements();
+        }
         Val = DAG.getExtractSubvector(SDLoc(LD), LDMemType,
-                                      DAG.getBitcast(InterVT, Val), 0);
+                                      DAG.getBitcast(InterVT, Val), ExtIdx);
       } else if (!STMemType.isVector() && !LDMemType.isVector() &&
                  STMemType.isInteger() && LDMemType.isInteger())
         Val = DAG.getNode(ISD::TRUNCATE, SDLoc(LD), LDMemType, Val);
diff --git a/llvm/test/CodeGen/ARM/stlf-vector-extract-be.ll b/llvm/test/CodeGen/ARM/stlf-vector-extract-be.ll
new file mode 100644
index 0000000000000..75de1ae3ac6d4
--- /dev/null
+++ b/llvm/test/CodeGen/ARM/stlf-vector-extract-be.ll
@@ -0,0 +1,17 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc < %s -mtriple=armeb-eabi -mattr=+v7,+neon | FileCheck %s
+
+define <2 x i32> @test_offset_load(ptr %p, <4 x i32> %v) {
+; CHECK-LABEL: test_offset_load:
+; CHECK:       @ %bb.0:
+; CHECK-NEXT:    vldr d17, [sp]
+; CHECK-NEXT:    vmov r1, r12, d17
+; CHECK-NEXT:    vmov d16, r3, r2
+; CHECK-NEXT:    vst1.64 {d16, d17}, [r0:128]
+; CHECK-NEXT:    mov r0, r12
+; CHECK-NEXT:    bx lr
+  store <4 x i32> %v, ptr %p, align 16
+  %p2 = getelementptr i8, ptr %p, i64 8
+  %res = load <2 x i32>, ptr %p2, align 8
+  ret <2 x i32> %res
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/180795


More information about the llvm-commits mailing list