[llvm] [VectorCombine] Fold contiguous loads into a single vector load (PR #185736)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Mar 12 08:23:16 PDT 2026
https://github.com/ParkHanbum updated https://github.com/llvm/llvm-project/pull/185736
>From 48cba51f07a10d4b8bea576b352349d2e282efb3 Mon Sep 17 00:00:00 2001
From: Hanbum Park <kese111 at gmail.com>
Date: Wed, 11 Mar 2026 04:17:15 +0900
Subject: [PATCH 1/6] add testcases for upcoming pathc
---
.../X86/fold-contiguous-loads.ll | 126 ++++++++++++++++++
1 file changed, 126 insertions(+)
create mode 100644 llvm/test/Transforms/VectorCombine/X86/fold-contiguous-loads.ll
diff --git a/llvm/test/Transforms/VectorCombine/X86/fold-contiguous-loads.ll b/llvm/test/Transforms/VectorCombine/X86/fold-contiguous-loads.ll
new file mode 100644
index 0000000000000..7f9128f0d8d4f
--- /dev/null
+++ b/llvm/test/Transforms/VectorCombine/X86/fold-contiguous-loads.ll
@@ -0,0 +1,126 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -passes=vector-combine -S -mtriple=x86_64-- -mattr=SSE2 | FileCheck %s --check-prefixes=CHECK,SSE
+; RUN: opt < %s -passes=vector-combine -S -mtriple=x86_64-- -mattr=AVX2 | FileCheck %s --check-prefixes=CHECK,AVX
+
+; 1. Basic behavior: Extract subvector from a wide load with offset (Success case)
+define <2 x float> @extract_subvector_with_offset(ptr %arg0) {
+; CHECK-LABEL: define <2 x float> @extract_subvector_with_offset(
+; CHECK-SAME: ptr [[ARG0:%.*]]) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT: [[V0:%.*]] = getelementptr inbounds i8, ptr [[ARG0]], i64 40
+; CHECK-NEXT: [[V1:%.*]] = load <4 x float>, ptr [[V0]], align 8
+; CHECK-NEXT: [[TMP2:%.*]] = shufflevector <4 x float> [[V1]], <4 x float> poison, <2 x i32> <i32 2, i32 3>
+; CHECK-NEXT: ret <2 x float> [[TMP2]]
+;
+ %v0 = getelementptr inbounds i8, ptr %arg0, i64 40
+ %v1 = load <4 x float>, ptr %v0, align 8
+ %v2 = shufflevector <4 x float> %v1, <4 x float> poison, <2 x i32> <i32 2, i32 3>
+ ret <2 x float> %v2
+}
+
+; 2. Basic behavior: Extract middle subvector with different type (Success case)
+define <2 x double> @extract_subvector_middle(ptr %arg0) {
+; SSE-LABEL: define <2 x double> @extract_subvector_middle(
+; SSE-SAME: ptr [[ARG0:%.*]]) #[[ATTR0]] {
+; SSE-NEXT: [[V0:%.*]] = getelementptr inbounds i8, ptr [[ARG0]], i64 40
+; SSE-NEXT: [[TMP1:%.*]] = load <3 x double>, ptr [[V0]], align 16
+; SSE-NEXT: [[V2:%.*]] = shufflevector <3 x double> [[TMP1]], <3 x double> poison, <2 x i32> <i32 1, i32 2>
+; SSE-NEXT: ret <2 x double> [[V2]]
+;
+; AVX-LABEL: define <2 x double> @extract_subvector_middle(
+; AVX-SAME: ptr [[ARG0:%.*]]) #[[ATTR0]] {
+; AVX-NEXT: [[V0:%.*]] = getelementptr inbounds i8, ptr [[ARG0]], i64 40
+; AVX-NEXT: [[V1:%.*]] = load <4 x double>, ptr [[V0]], align 16
+; AVX-NEXT: [[V2:%.*]] = shufflevector <4 x double> [[V1]], <4 x double> poison, <2 x i32> <i32 1, i32 2>
+; AVX-NEXT: ret <2 x double> [[V2]]
+;
+ %v0 = getelementptr inbounds i8, ptr %arg0, i64 40
+ %v1 = load <4 x double>, ptr %v0, align 16
+ %v2 = shufflevector <4 x double> %v1, <4 x double> poison, <2 x i32> <i32 1, i32 2>
+ ret <2 x double> %v2
+}
+
+; 3. Negative test: Load instruction has multiple uses (!hasOneUse)
+; Added a store instruction using the load result to the success case.
+define <2 x float> @negative_multi_use(ptr %arg0, ptr %out) {
+; CHECK-LABEL: define <2 x float> @negative_multi_use(
+; CHECK-SAME: ptr [[ARG0:%.*]], ptr [[OUT:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[V0:%.*]] = getelementptr inbounds i8, ptr [[ARG0]], i64 40
+; CHECK-NEXT: [[V1:%.*]] = load <4 x float>, ptr [[V0]], align 8
+; CHECK-NEXT: store <4 x float> [[V1]], ptr [[OUT]], align 8
+; CHECK-NEXT: [[V2:%.*]] = shufflevector <4 x float> [[V1]], <4 x float> poison, <2 x i32> <i32 2, i32 3>
+; CHECK-NEXT: ret <2 x float> [[V2]]
+;
+ %v0 = getelementptr inbounds i8, ptr %arg0, i64 40
+ %v1 = load <4 x float>, ptr %v0, align 8
+ store <4 x float> %v1, ptr %out, align 8
+ %v2 = shufflevector <4 x float> %v1, <4 x float> poison, <2 x i32> <i32 2, i32 3>
+ ret <2 x float> %v2
+}
+
+; 4. Negative test: Non-contiguous memory access (Reverse mask)
+; Tweaked the mask to <3, 2> from the success case.
+define <2 x float> @negative_non_contiguous(ptr %arg0) {
+; CHECK-LABEL: define <2 x float> @negative_non_contiguous(
+; CHECK-SAME: ptr [[ARG0:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[V0:%.*]] = getelementptr inbounds i8, ptr [[ARG0]], i64 40
+; CHECK-NEXT: [[V1:%.*]] = load <4 x float>, ptr [[V0]], align 8
+; CHECK-NEXT: [[V2:%.*]] = shufflevector <4 x float> [[V1]], <4 x float> poison, <2 x i32> <i32 3, i32 2>
+; CHECK-NEXT: ret <2 x float> [[V2]]
+;
+ %v0 = getelementptr inbounds i8, ptr %arg0, i64 40
+ %v1 = load <4 x float>, ptr %v0, align 8
+ %v2 = shufflevector <4 x float> %v1, <4 x float> poison, <2 x i32> <i32 3, i32 2>
+ ret <2 x float> %v2
+}
+
+; 5. Negative test: Volatile load (!isSimple)
+; Added the volatile attribute to the load from the success case.
+define <2 x float> @negative_volatile_load(ptr %arg0) {
+; CHECK-LABEL: define <2 x float> @negative_volatile_load(
+; CHECK-SAME: ptr [[ARG0:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[V0:%.*]] = getelementptr inbounds i8, ptr [[ARG0]], i64 40
+; CHECK-NEXT: [[V1:%.*]] = load volatile <4 x float>, ptr [[V0]], align 8
+; CHECK-NEXT: [[V2:%.*]] = shufflevector <4 x float> [[V1]], <4 x float> poison, <2 x i32> <i32 2, i32 3>
+; CHECK-NEXT: ret <2 x float> [[V2]]
+;
+ %v0 = getelementptr inbounds i8, ptr %arg0, i64 40
+ %v1 = load volatile <4 x float>, ptr %v0, align 8
+ %v2 = shufflevector <4 x float> %v1, <4 x float> poison, <2 x i32> <i32 2, i32 3>
+ ret <2 x float> %v2
+}
+
+; 6. Negative test: Different pointer bases (Base != CommonBase)
+; Kept the success case structure but mixed in a second pointer base to induce failure.
+define <2 x float> @negative_different_bases(ptr %arg0, ptr %arg1) {
+; CHECK-LABEL: define <2 x float> @negative_different_bases(
+; CHECK-SAME: ptr [[ARG0:%.*]], ptr [[ARG1:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[V0:%.*]] = getelementptr inbounds i8, ptr [[ARG0]], i64 40
+; CHECK-NEXT: [[V1:%.*]] = load <4 x float>, ptr [[V0]], align 8
+; CHECK-NEXT: [[V0_OTHER:%.*]] = getelementptr inbounds i8, ptr [[ARG1]], i64 40
+; CHECK-NEXT: [[V1_OTHER:%.*]] = load <4 x float>, ptr [[V0_OTHER]], align 8
+; CHECK-NEXT: [[V2:%.*]] = shufflevector <4 x float> [[V1]], <4 x float> [[V1_OTHER]], <2 x i32> <i32 2, i32 7>
+; CHECK-NEXT: ret <2 x float> [[V2]]
+;
+ %v0 = getelementptr inbounds i8, ptr %arg0, i64 40
+ %v1 = load <4 x float>, ptr %v0, align 8
+ %v0_other = getelementptr inbounds i8, ptr %arg1, i64 40
+ %v1_other = load <4 x float>, ptr %v0_other, align 8
+ %v2 = shufflevector <4 x float> %v1, <4 x float> %v1_other, <2 x i32> <i32 2, i32 7>
+ ret <2 x float> %v2
+}
+
+; 7. Negative test: Offset continuity failure (Memory gap)
+; Tweaked the mask to <1, 3> from the success case to create a gap.
+define <2 x float> @negative_memory_gap(ptr %arg0) {
+; CHECK-LABEL: define <2 x float> @negative_memory_gap(
+; CHECK-SAME: ptr [[ARG0:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[V0:%.*]] = getelementptr inbounds i8, ptr [[ARG0]], i64 40
+; CHECK-NEXT: [[V1:%.*]] = load <4 x float>, ptr [[V0]], align 8
+; CHECK-NEXT: [[V2:%.*]] = shufflevector <4 x float> [[V1]], <4 x float> poison, <2 x i32> <i32 1, i32 3>
+; CHECK-NEXT: ret <2 x float> [[V2]]
+;
+ %v0 = getelementptr inbounds i8, ptr %arg0, i64 40
+ %v1 = load <4 x float>, ptr %v0, align 8
+ %v2 = shufflevector <4 x float> %v1, <4 x float> poison, <2 x i32> <i32 1, i32 3>
+ ret <2 x float> %v2
+}
>From fce5376eda04116282b2e486e65de940693014b4 Mon Sep 17 00:00:00 2001
From: Hanbum Park <kese111 at gmail.com>
Date: Wed, 11 Mar 2026 02:14:11 +0900
Subject: [PATCH 2/6] [VectorCombine] Fold contiguous loads into a single
vector load
VectorCombine currently lacks the capability to recognize and
optimize contiguous lane extractions from wide vector loads.
The IR retains redundant wide loads and shufflevector operations
without narrowing.
(Before):
%v1 = load <4 x float>, ptr %p, align 8
%v2 = shufflevector <4 x float> %v1, poison, <2 x i32> <2, 3>
The new contiguous memory validation logic calculates the absolute
bit offset of each lane to verify strict sequential access. It
replaces the validated shuffle mask and original loads with a single
vector load combined with a constant pointer offset.
(After):
%ptr = getelementptr inbounds i8, ptr %p, i64 8
%v2 = load <2 x float>, ptr %ptr, align 8
Proof: https://alive2.llvm.org/ce/z/HduzCE
Fixed: #185556
---
.../Transforms/Vectorize/VectorCombine.cpp | 88 +++++++++++++++++++
.../X86/fold-contiguous-loads.ll | 26 +++---
2 files changed, 98 insertions(+), 16 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/VectorCombine.cpp b/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
index 3d95fdaa9eb6d..1d3d15b69e845 100644
--- a/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
+++ b/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
@@ -156,6 +156,7 @@ class VectorCombine {
bool shrinkType(Instruction &I);
bool shrinkLoadForShuffles(Instruction &I);
bool shrinkPhiOfShuffles(Instruction &I);
+ bool foldContiguousLoads(Instruction &I);
void replaceValue(Instruction &Old, Value &New, bool Erase = true) {
LLVM_DEBUG(dbgs() << "VC: Replacing: " << Old << '\n');
@@ -5747,6 +5748,91 @@ bool VectorCombine::shrinkPhiOfShuffles(Instruction &I) {
return true;
}
+/// Check if a vector instruction's lanes originate from contiguous memory
+/// accesses. Fold the original loads and shuffles into a single vector load
+/// if it is profitable. For example:
+/// shufflevector(load <4 x float> ptr), poison, <2, 3>
+/// -> load <2 x float> (ptradd ptr, 8)
+/// Cost model calculations take into account the cost of the original
+/// unique load(s) and the target instruction versus the cost of the new
+/// aligned vector load.
+bool VectorCombine::foldContiguousLoads(Instruction &I) {
+ auto *VT = dyn_cast<FixedVectorType>(I.getType());
+ if (!VT || I.use_empty())
+ return false;
+
+ unsigned ElementSize = VT->getElementType()->getScalarSizeInBits();
+ unsigned NumElts = VT->getNumElements();
+ Type *EltTy = VT->getElementType();
+ Value *CommonBase = nullptr;
+ int64_t ExpectedBaseBitOffset = 0, FirstLoadOffset = 0;
+ LoadInst *FirstLI = nullptr;
+ SmallPtrSet<LoadInst *, 4> Loads;
+ for (unsigned Lane = 0; Lane < NumElts; ++Lane) {
+ InstLane IL = lookThroughShuffles(&*I.use_begin(), Lane);
+ if (!IL.first)
+ return false;
+
+ auto *LI = dyn_cast<LoadInst>(IL.first->get());
+ if (!LI)
+ return false;
+
+ if (!LI->isSimple() || !LI->hasOneUse())
+ return false;
+
+ auto *LIVTy = dyn_cast<FixedVectorType>(LI->getType());
+ if (!LIVTy || LIVTy->getElementType() != EltTy)
+ return false;
+
+ int64_t ConstantOffset = 0;
+ Value *Base = GetPointerBaseWithConstantOffset(LI->getPointerOperand(),
+ ConstantOffset, *DL);
+ int64_t AbsoluteBitOffset =
+ (ConstantOffset * 8) + (IL.second * ElementSize);
+ if (Lane == 0) {
+ if (AbsoluteBitOffset % 8 != 0)
+ return false;
+
+ CommonBase = Base;
+ ExpectedBaseBitOffset = AbsoluteBitOffset;
+ FirstLI = LI;
+ FirstLoadOffset = ConstantOffset;
+ } else {
+ if (Base != CommonBase)
+ return false;
+
+ if (AbsoluteBitOffset != ExpectedBaseBitOffset + (Lane * ElementSize))
+ return false;
+ }
+
+ Loads.insert(LI);
+ }
+
+ InstructionCost OldCost = TTI.getInstructionCost(&I, CostKind);
+ for (LoadInst *LI : Loads)
+ OldCost += TTI.getInstructionCost(LI, CostKind);
+
+ int64_t StartByteOffset = ExpectedBaseBitOffset / 8;
+ int64_t OffsetFromFirstLoad = StartByteOffset - FirstLoadOffset;
+ Align NewAlign = commonAlignment(FirstLI->getAlign(), OffsetFromFirstLoad);
+ InstructionCost NewCost =
+ TTI.getMemoryOpCost(Instruction::Load, VT, NewAlign, CostKind);
+ LLVM_DEBUG(dbgs() << "Found contiguous loads to fold: " << I
+ << "\n OldCost: " << OldCost << " vs NewCost: " << NewCost
+ << "\n");
+
+ if (OldCost < NewCost)
+ return false;
+
+ Value *NewBasePtr =
+ Builder.CreatePtrAdd(CommonBase, Builder.getInt64(StartByteOffset));
+ LoadInst *NewLoad = Builder.CreateAlignedLoad(VT, NewBasePtr, NewAlign);
+ NewLoad->copyMetadata(*FirstLI);
+ replaceValue(I, *NewLoad);
+
+ return true;
+}
+
/// This is the entry point for all transforms. Pass manager differences are
/// handled in the callers of this function.
bool VectorCombine::run() {
@@ -5844,6 +5930,8 @@ bool VectorCombine::run() {
return true;
if (foldShuffleToIdentity(I))
return true;
+ if (foldContiguousLoads(I))
+ return true;
break;
case Instruction::Load:
if (shrinkLoadForShuffles(I))
diff --git a/llvm/test/Transforms/VectorCombine/X86/fold-contiguous-loads.ll b/llvm/test/Transforms/VectorCombine/X86/fold-contiguous-loads.ll
index 7f9128f0d8d4f..e513935a7887a 100644
--- a/llvm/test/Transforms/VectorCombine/X86/fold-contiguous-loads.ll
+++ b/llvm/test/Transforms/VectorCombine/X86/fold-contiguous-loads.ll
@@ -6,9 +6,8 @@
define <2 x float> @extract_subvector_with_offset(ptr %arg0) {
; CHECK-LABEL: define <2 x float> @extract_subvector_with_offset(
; CHECK-SAME: ptr [[ARG0:%.*]]) #[[ATTR0:[0-9]+]] {
-; CHECK-NEXT: [[V0:%.*]] = getelementptr inbounds i8, ptr [[ARG0]], i64 40
-; CHECK-NEXT: [[V1:%.*]] = load <4 x float>, ptr [[V0]], align 8
-; CHECK-NEXT: [[TMP2:%.*]] = shufflevector <4 x float> [[V1]], <4 x float> poison, <2 x i32> <i32 2, i32 3>
+; CHECK-NEXT: [[TMP1:%.*]] = getelementptr i8, ptr [[ARG0]], i64 48
+; CHECK-NEXT: [[TMP2:%.*]] = load <2 x float>, ptr [[TMP1]], align 8
; CHECK-NEXT: ret <2 x float> [[TMP2]]
;
%v0 = getelementptr inbounds i8, ptr %arg0, i64 40
@@ -19,19 +18,11 @@ define <2 x float> @extract_subvector_with_offset(ptr %arg0) {
; 2. Basic behavior: Extract middle subvector with different type (Success case)
define <2 x double> @extract_subvector_middle(ptr %arg0) {
-; SSE-LABEL: define <2 x double> @extract_subvector_middle(
-; SSE-SAME: ptr [[ARG0:%.*]]) #[[ATTR0]] {
-; SSE-NEXT: [[V0:%.*]] = getelementptr inbounds i8, ptr [[ARG0]], i64 40
-; SSE-NEXT: [[TMP1:%.*]] = load <3 x double>, ptr [[V0]], align 16
-; SSE-NEXT: [[V2:%.*]] = shufflevector <3 x double> [[TMP1]], <3 x double> poison, <2 x i32> <i32 1, i32 2>
-; SSE-NEXT: ret <2 x double> [[V2]]
-;
-; AVX-LABEL: define <2 x double> @extract_subvector_middle(
-; AVX-SAME: ptr [[ARG0:%.*]]) #[[ATTR0]] {
-; AVX-NEXT: [[V0:%.*]] = getelementptr inbounds i8, ptr [[ARG0]], i64 40
-; AVX-NEXT: [[V1:%.*]] = load <4 x double>, ptr [[V0]], align 16
-; AVX-NEXT: [[V2:%.*]] = shufflevector <4 x double> [[V1]], <4 x double> poison, <2 x i32> <i32 1, i32 2>
-; AVX-NEXT: ret <2 x double> [[V2]]
+; CHECK-LABEL: define <2 x double> @extract_subvector_middle(
+; CHECK-SAME: ptr [[ARG0:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[TMP1:%.*]] = getelementptr i8, ptr [[ARG0]], i64 48
+; CHECK-NEXT: [[V2:%.*]] = load <2 x double>, ptr [[TMP1]], align 8
+; CHECK-NEXT: ret <2 x double> [[V2]]
;
%v0 = getelementptr inbounds i8, ptr %arg0, i64 40
%v1 = load <4 x double>, ptr %v0, align 16
@@ -124,3 +115,6 @@ define <2 x float> @negative_memory_gap(ptr %arg0) {
%v2 = shufflevector <4 x float> %v1, <4 x float> poison, <2 x i32> <i32 1, i32 3>
ret <2 x float> %v2
}
+;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
+; AVX: {{.*}}
+; SSE: {{.*}}
>From a8217a1dda480d5935a2dafdb346328f1c4029fe Mon Sep 17 00:00:00 2001
From: Hanbum Park <kese111 at gmail.com>
Date: Thu, 12 Mar 2026 23:18:33 +0900
Subject: [PATCH 3/6] use compyMetadataForLoad for copy metadata for FirstLI
---
llvm/lib/Transforms/Vectorize/VectorCombine.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/lib/Transforms/Vectorize/VectorCombine.cpp b/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
index 1d3d15b69e845..0e4ac32e47ef9 100644
--- a/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
+++ b/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
@@ -5827,7 +5827,7 @@ bool VectorCombine::foldContiguousLoads(Instruction &I) {
Value *NewBasePtr =
Builder.CreatePtrAdd(CommonBase, Builder.getInt64(StartByteOffset));
LoadInst *NewLoad = Builder.CreateAlignedLoad(VT, NewBasePtr, NewAlign);
- NewLoad->copyMetadata(*FirstLI);
+ NewLoad->copyMetadataForLoad(*FirstLI);
replaceValue(I, *NewLoad);
return true;
>From dbdbdb1a1142618d6a96605fd9c0e5e877660b40 Mon Sep 17 00:00:00 2001
From: Hanbum Park <kese111 at gmail.com>
Date: Thu, 12 Mar 2026 23:56:43 +0900
Subject: [PATCH 4/6] Add testcasess to verify bailout on element type mismatch
---
.../X86/fold-contiguous-loads.ll | 73 +++++++++++++++++++
1 file changed, 73 insertions(+)
diff --git a/llvm/test/Transforms/VectorCombine/X86/fold-contiguous-loads.ll b/llvm/test/Transforms/VectorCombine/X86/fold-contiguous-loads.ll
index e513935a7887a..e5ae0aabb309c 100644
--- a/llvm/test/Transforms/VectorCombine/X86/fold-contiguous-loads.ll
+++ b/llvm/test/Transforms/VectorCombine/X86/fold-contiguous-loads.ll
@@ -115,6 +115,79 @@ define <2 x float> @negative_memory_gap(ptr %arg0) {
%v2 = shufflevector <4 x float> %v1, <4 x float> poison, <2 x i32> <i32 1, i32 3>
ret <2 x float> %v2
}
+
+define <4 x i32> @test_element_mismatch(ptr dereferenceable(16) align 16 %p) {
+; CHECK-LABEL: define <4 x i32> @test_element_mismatch(
+; CHECK-SAME: ptr align 16 dereferenceable(16) [[P:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[L0:%.*]] = load <2 x i32>, ptr [[P]], align 8
+; CHECK-NEXT: [[P1:%.*]] = getelementptr i8, ptr [[P]], i64 8
+; CHECK-NEXT: [[L1:%.*]] = load <2 x float>, ptr [[P1]], align 8
+; CHECK-NEXT: [[V1:%.*]] = bitcast <2 x float> [[L1]] to <2 x i32>
+; CHECK-NEXT: [[RES:%.*]] = shufflevector <2 x i32> [[L0]], <2 x i32> [[V1]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+; CHECK-NEXT: ret <4 x i32> [[RES]]
+;
+ %L0 = load <2 x i32>, ptr %p, align 8
+ %p1 = getelementptr i8, ptr %p, i64 8
+ %L1 = load <2 x float>, ptr %p1, align 8
+ %v1 = bitcast <2 x float> %L1 to <2 x i32>
+ %res = shufflevector <2 x i32> %L0, <2 x i32> %v1, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+ ret <4 x i32> %res
+}
+
+define <4 x i64> @test_element_mismatch_double(ptr dereferenceable(32) align 32 %p) {
+; CHECK-LABEL: define <4 x i64> @test_element_mismatch_double(
+; CHECK-SAME: ptr align 32 dereferenceable(32) [[P:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[L0:%.*]] = load <2 x i64>, ptr [[P]], align 16
+; CHECK-NEXT: [[P1:%.*]] = getelementptr i8, ptr [[P]], i64 16
+; CHECK-NEXT: [[L1:%.*]] = load <2 x double>, ptr [[P1]], align 16
+; CHECK-NEXT: [[V1:%.*]] = bitcast <2 x double> [[L1]] to <2 x i64>
+; CHECK-NEXT: [[RES:%.*]] = shufflevector <2 x i64> [[L0]], <2 x i64> [[V1]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+; CHECK-NEXT: ret <4 x i64> [[RES]]
+;
+ %L0 = load <2 x i64>, ptr %p, align 16
+ %p1 = getelementptr i8, ptr %p, i64 16
+ %L1 = load <2 x double>, ptr %p1, align 16
+ %v1 = bitcast <2 x double> %L1 to <2 x i64>
+ %res = shufflevector <2 x i64> %L0, <2 x i64> %v1, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+ ret <4 x i64> %res
+}
+
+define <4 x i32> @test_element_mismatch_i16_i32(ptr dereferenceable(16) align 16 %p) {
+; CHECK-LABEL: define <4 x i32> @test_element_mismatch_i16_i32(
+; CHECK-SAME: ptr align 16 dereferenceable(16) [[P:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[L0:%.*]] = load <4 x i16>, ptr [[P]], align 8
+; CHECK-NEXT: [[P1:%.*]] = getelementptr i8, ptr [[P]], i64 8
+; CHECK-NEXT: [[L1:%.*]] = load <2 x i32>, ptr [[P1]], align 8
+; CHECK-NEXT: [[V0:%.*]] = bitcast <4 x i16> [[L0]] to <2 x i32>
+; CHECK-NEXT: [[RES:%.*]] = shufflevector <2 x i32> [[V0]], <2 x i32> [[L1]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+; CHECK-NEXT: ret <4 x i32> [[RES]]
+;
+ %L0 = load <4 x i16>, ptr %p, align 8
+ %p1 = getelementptr i8, ptr %p, i64 8
+ %L1 = load <2 x i32>, ptr %p1, align 8
+ %v0 = bitcast <4 x i16> %L0 to <2 x i32>
+ %res = shufflevector <2 x i32> %v0, <2 x i32> %L1, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+ ret <4 x i32> %res
+}
+
+define <4 x i64> @test_element_mismatch_i32_i64(ptr dereferenceable(32) align 32 %p) {
+; CHECK-LABEL: define <4 x i64> @test_element_mismatch_i32_i64(
+; CHECK-SAME: ptr align 32 dereferenceable(32) [[P:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[L0:%.*]] = load <4 x i32>, ptr [[P]], align 16
+; CHECK-NEXT: [[P1:%.*]] = getelementptr i8, ptr [[P]], i64 16
+; CHECK-NEXT: [[L1:%.*]] = load <2 x i64>, ptr [[P1]], align 16
+; CHECK-NEXT: [[V0:%.*]] = bitcast <4 x i32> [[L0]] to <2 x i64>
+; CHECK-NEXT: [[RES:%.*]] = shufflevector <2 x i64> [[V0]], <2 x i64> [[L1]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+; CHECK-NEXT: ret <4 x i64> [[RES]]
+;
+ %L0 = load <4 x i32>, ptr %p, align 16
+ %p1 = getelementptr i8, ptr %p, i64 16
+ %L1 = load <2 x i64>, ptr %p1, align 16
+ %v0 = bitcast <4 x i32> %L0 to <2 x i64>
+ %res = shufflevector <2 x i64> %v0, <2 x i64> %L1, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+ ret <4 x i64> %res
+}
+
;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
; AVX: {{.*}}
; SSE: {{.*}}
>From e42605983453b6d865c573cb0f30c8f9c23bbb11 Mon Sep 17 00:00:00 2001
From: Hanbum Park <kese111 at gmail.com>
Date: Fri, 13 Mar 2026 00:03:06 +0900
Subject: [PATCH 5/6] add testcase which has poison its mask
---
.../VectorCombine/X86/fold-contiguous-loads.ll | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/llvm/test/Transforms/VectorCombine/X86/fold-contiguous-loads.ll b/llvm/test/Transforms/VectorCombine/X86/fold-contiguous-loads.ll
index e5ae0aabb309c..3dd3adb4559eb 100644
--- a/llvm/test/Transforms/VectorCombine/X86/fold-contiguous-loads.ll
+++ b/llvm/test/Transforms/VectorCombine/X86/fold-contiguous-loads.ll
@@ -188,6 +188,22 @@ define <4 x i64> @test_element_mismatch_i32_i64(ptr dereferenceable(32) align 32
ret <4 x i64> %res
}
+define <4 x float> @test_shuffle_poison(ptr align 16 dereferenceable(16) %p) {
+; CHECK-LABEL: define <4 x float> @test_shuffle_poison(
+; CHECK-SAME: ptr align 16 dereferenceable(16) [[P:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[L0:%.*]] = load <2 x float>, ptr [[P]], align 4
+; CHECK-NEXT: [[P1:%.*]] = getelementptr i8, ptr [[P]], i64 8
+; CHECK-NEXT: [[L1:%.*]] = load <2 x float>, ptr [[P1]], align 4
+; CHECK-NEXT: [[RES:%.*]] = shufflevector <2 x float> [[L0]], <2 x float> [[L1]], <4 x i32> <i32 0, i32 poison, i32 2, i32 3>
+; CHECK-NEXT: ret <4 x float> [[RES]]
+;
+ %L0 = load <2 x float>, ptr %p, align 4
+ %p1 = getelementptr i8, ptr %p, i64 8
+ %L1 = load <2 x float>, ptr %p1, align 4
+ %res = shufflevector <2 x float> %L0, <2 x float> %L1, <4 x i32> <i32 0, i32 poison, i32 2, i32 3>
+ ret <4 x float> %res
+}
+
;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
; AVX: {{.*}}
; SSE: {{.*}}
>From 17b65fc7c4ecca93a576237af82625ef243c7363 Mon Sep 17 00:00:00 2001
From: Hanbum Park <kese111 at gmail.com>
Date: Fri, 13 Mar 2026 00:22:43 +0900
Subject: [PATCH 6/6] Add testcasess for odd-sized vectors and cross-boundary
offsets in load widening
---
.../X86/fold-contiguous-loads.ll | 53 +++++++++++++++++++
1 file changed, 53 insertions(+)
diff --git a/llvm/test/Transforms/VectorCombine/X86/fold-contiguous-loads.ll b/llvm/test/Transforms/VectorCombine/X86/fold-contiguous-loads.ll
index 3dd3adb4559eb..209f0704a31fa 100644
--- a/llvm/test/Transforms/VectorCombine/X86/fold-contiguous-loads.ll
+++ b/llvm/test/Transforms/VectorCombine/X86/fold-contiguous-loads.ll
@@ -30,6 +30,59 @@ define <2 x double> @extract_subvector_middle(ptr %arg0) {
ret <2 x double> %v2
}
+define <3 x i32> @test_odd_number_elements(ptr align 16 dereferenceable(16) %p) {
+; CHECK-LABEL: define <3 x i32> @test_odd_number_elements(
+; CHECK-SAME: ptr align 16 dereferenceable(16) [[P:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[RES:%.*]] = load <3 x i32>, ptr [[P]], align 8
+; CHECK-NEXT: ret <3 x i32> [[RES]]
+;
+ %L0 = load <2 x i32>, ptr %p, align 8
+ %p1 = getelementptr i8, ptr %p, i64 8
+ %L1 = load <2 x i32>, ptr %p1, align 8
+ %res = shufflevector <2 x i32> %L0, <2 x i32> %L1, <3 x i32> <i32 0, i32 1, i32 2>
+ ret <3 x i32> %res
+}
+
+define <4 x i32> @test_odd_number_load_to_even_svi(ptr align 16 dereferenceable(32) %p) {
+; CHECK-LABEL: define <4 x i32> @test_odd_number_load_to_even_svi(
+; CHECK-SAME: ptr align 16 dereferenceable(32) [[P:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[RES:%.*]] = load <4 x i32>, ptr [[P]], align 16
+; CHECK-NEXT: ret <4 x i32> [[RES]]
+;
+ %L0 = load <3 x i32>, ptr %p, align 16
+ %p1 = getelementptr i8, ptr %p, i64 12
+ %L1 = load <3 x i32>, ptr %p1, align 4
+ %res = shufflevector <3 x i32> %L0, <3 x i32> %L1, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+ ret <4 x i32> %res
+}
+
+define <11 x i32> @test_odd_to_odd_large_mask(ptr align 64 dereferenceable(64) %p) {
+; CHECK-LABEL: define <11 x i32> @test_odd_to_odd_large_mask(
+; CHECK-SAME: ptr align 64 dereferenceable(64) [[P:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[RES:%.*]] = load <11 x i32>, ptr [[P]], align 16
+; CHECK-NEXT: ret <11 x i32> [[RES]]
+;
+ %L0 = load <7 x i32>, ptr %p, align 16
+ %p1 = getelementptr i8, ptr %p, i64 28
+ %L1 = load <7 x i32>, ptr %p1, align 4
+ %res = shufflevector <7 x i32> %L0, <7 x i32> %L1, <11 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10>
+ ret <11 x i32> %res
+}
+
+define <5 x i16> @test_load_17_mask_5_mid_offset_i16(ptr align 16 dereferenceable(68) %p) {
+; CHECK-LABEL: define <5 x i16> @test_load_17_mask_5_mid_offset_i16(
+; CHECK-SAME: ptr align 16 dereferenceable(68) [[P:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[TMP1:%.*]] = getelementptr i8, ptr [[P]], i64 30
+; CHECK-NEXT: [[RES:%.*]] = load <5 x i16>, ptr [[TMP1]], align 2
+; CHECK-NEXT: ret <5 x i16> [[RES]]
+;
+ %L0 = load <17 x i16>, ptr %p, align 16
+ %p1 = getelementptr i8, ptr %p, i64 34
+ %L1 = load <17 x i16>, ptr %p1, align 2
+ %res = shufflevector <17 x i16> %L0, <17 x i16> %L1, <5 x i32> <i32 15, i32 16, i32 17, i32 18, i32 19>
+ ret <5 x i16> %res
+}
+
; 3. Negative test: Load instruction has multiple uses (!hasOneUse)
; Added a store instruction using the load result to the success case.
define <2 x float> @negative_multi_use(ptr %arg0, ptr %out) {
More information about the llvm-commits
mailing list