[llvm] [VectorCombine] Consolidate fragmented loads into wide loads (PR #175311)

via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 10 07:04:07 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-vectorizers

Author: hanbeom (ParkHanbum)

<details>
<summary>Changes</summary>

LLVM currently maintains multiple narrow loads (e.g., <2 x double>
or scalars) from adjacent memory addresses when they are combined
into a single larger vector (e.g., <4 x double>) through a sequence
of nested shufflevector instructions. These fragmented loads and the
associated shuffle tree remain as separate operations in the IR.

This patch introduces foldFragmentedLoads to identify these
fragmented load patterns within shuffle chains and consolidate them
into one or two wide loads that fit within the target's maximum
vector register width. The entire shuffle tree is folded into a
single shufflevector operation using the newly created wide load(s)
as operands.

Proof: https://alive2.llvm.org/ce/z/eZXjhM
Fixed: https://github.com/llvm/llvm-project/issues/140234


---

Patch is 24.14 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/175311.diff


2 Files Affected:

- (modified) llvm/lib/Transforms/Vectorize/VectorCombine.cpp (+94) 
- (added) llvm/test/Transforms/VectorCombine/X86/shuffle-of-fragment-loads.ll (+373) 


``````````diff
diff --git a/llvm/lib/Transforms/Vectorize/VectorCombine.cpp b/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
index 3e06f74fa5c65..c1adc2b44a023 100644
--- a/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
+++ b/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
@@ -143,6 +143,7 @@ class VectorCombine {
   bool foldShufflesOfLengthChangingShuffles(Instruction &I);
   bool foldShuffleOfIntrinsics(Instruction &I);
   bool foldShuffleToIdentity(Instruction &I);
+  bool foldFragmentedLoads(Instruction &I);
   bool foldShuffleFromReductions(Instruction &I);
   bool foldShuffleChainsToReduce(Instruction &I);
   bool foldCastFromReductions(Instruction &I);
@@ -3606,6 +3607,97 @@ bool VectorCombine::foldShuffleToIdentity(Instruction &I) {
   return true;
 }
 
+struct LaneOrigin {
+  LoadInst *LI = nullptr;
+  int64_t Offset = 0;
+  Value *BasePtr = nullptr;
+};
+
+bool VectorCombine::foldFragmentedLoads(Instruction &I) {
+  auto *VT = dyn_cast<FixedVectorType>(I.getType());
+  if (!VT || I.use_empty())
+    return false;
+
+  unsigned ElementSize = VT->getElementType()->getPrimitiveSizeInBits();
+  if (!ElementSize)
+    return false;
+
+  unsigned NumElts = VT->getNumElements();
+  unsigned VTySize = ElementSize * NumElts;
+  unsigned MaxVectorSize =
+      TTI.getRegisterBitWidth(TargetTransformInfo::RGK_FixedWidthVector);
+  unsigned MaxVectorEltNum = MaxVectorSize / ElementSize;
+  if (NumElts > MaxVectorEltNum)
+    return false;
+
+  Type *EltTy = VT->getElementType();
+  SmallVector<LaneOrigin, 4> Origins(NumElts);
+  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 (!canWidenLoad(LI, TTI))
+      return false;
+
+    if (auto *LIVTy = dyn_cast<FixedVectorType>(LI->getType());
+        !LIVTy || LIVTy->getElementType() != EltTy ||
+        LIVTy->getNumElements() >= MaxVectorEltNum)
+      return false;
+
+    int64_t ConstantOffset = 0;
+    Value *Base = GetPointerBaseWithConstantOffset(LI->getPointerOperand(),
+                                                   ConstantOffset, *DL);
+
+    Origins[Lane].LI = LI;
+    Origins[Lane].BasePtr = Base;
+    int64_t Offset = ConstantOffset * 8 + (IL.second * ElementSize);
+    if (Offset < 0 || Offset >= VTySize || Offset % ElementSize)
+      return false;
+
+    Origins[Lane].Offset = Offset;
+  }
+
+  SmallPtrSet<LoadInst *, 4> Loads;
+  SmallVector<Value *, 2> Bases;
+  Align Align = Origins[0].LI->getAlign();
+  for (const auto &O : Origins) {
+    Align = commonAlignment(O.LI->getAlign(), Align.value());
+    Loads.insert(O.LI);
+    if (!is_contained(Bases, O.BasePtr))
+      Bases.push_back(O.BasePtr);
+  }
+
+  if (Bases.size() > 2 || Loads.size() <= Bases.size()) {
+    LLVM_DEBUG(dbgs() << "No load reduction: " << Loads.size() << " loads -> "
+                      << Bases.size() << " wide loads..\n");
+    return false;
+  }
+
+  Type *WideVecTy = VT;
+  Value *WideL0 = Builder.CreateAlignedLoad(WideVecTy, Bases[0], Align);
+  Value *WideL1 = Bases.size() > 1
+                      ? Builder.CreateAlignedLoad(WideVecTy, Bases[1], Align)
+                      : WideL0;
+
+  SmallVector<int, 4> NewMask;
+  for (const auto &O : Origins) {
+    int WideIdx = O.Offset / ElementSize;
+    if (O.BasePtr == Bases[0])
+      NewMask.push_back(WideIdx);
+    else
+      NewMask.push_back(WideIdx + NumElts);
+  }
+
+  LLVM_DEBUG(dbgs() << "VC: Folding " << Loads.size() << " loads with "
+                    << Bases.size() << " widening bases.." << "\n");
+
+  Value *NewSVI = Builder.CreateShuffleVector(WideL0, WideL1, NewMask);
+  replaceValue(I, *NewSVI);
+  return true;
+}
+
 /// Given a commutative reduction, the order of the input lanes does not alter
 /// the results. We can use this to remove certain shuffles feeding the
 /// reduction, removing the need to shuffle at all.
@@ -5032,6 +5124,8 @@ bool VectorCombine::run() {
           return true;
         if (foldSelectShuffle(I))
           return true;
+        if (foldFragmentedLoads(I))
+          return true;
         if (foldShuffleToIdentity(I))
           return true;
         break;
diff --git a/llvm/test/Transforms/VectorCombine/X86/shuffle-of-fragment-loads.ll b/llvm/test/Transforms/VectorCombine/X86/shuffle-of-fragment-loads.ll
new file mode 100644
index 0000000000000..8e56a4e001687
--- /dev/null
+++ b/llvm/test/Transforms/VectorCombine/X86/shuffle-of-fragment-loads.ll
@@ -0,0 +1,373 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
+; 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
+
+declare void @use(<2 x i32>)
+
+; ====================================================================================
+; Positive : Consolidate multiple fragmented memory loads and subsequent shuffles into
+; one or two wide loads and a single shuffle operation.
+; ====================================================================================
+
+define <4 x double> @test_double_4_to_2(ptr %x, ptr %y) {
+; SSE-LABEL: define <4 x double> @test_double_4_to_2(
+; SSE-SAME: ptr [[X:%.*]], ptr [[Y:%.*]]) #[[ATTR0:[0-9]+]] {
+; SSE-NEXT:    [[X0:%.*]] = load <2 x double>, ptr [[X]], align 16
+; SSE-NEXT:    [[X_OFF:%.*]] = getelementptr i8, ptr [[X]], i64 16
+; SSE-NEXT:    [[X1:%.*]] = load <2 x double>, ptr [[X_OFF]], align 16
+; SSE-NEXT:    [[Y0:%.*]] = load <2 x double>, ptr [[Y]], align 16
+; SSE-NEXT:    [[Y_OFF:%.*]] = getelementptr i8, ptr [[Y]], i64 16
+; SSE-NEXT:    [[Y1:%.*]] = load <2 x double>, ptr [[Y_OFF]], align 16
+; SSE-NEXT:    [[V1:%.*]] = shufflevector <2 x double> [[X0]], <2 x double> [[X1]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+; SSE-NEXT:    [[V2:%.*]] = shufflevector <2 x double> [[Y0]], <2 x double> [[Y1]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+; SSE-NEXT:    [[RES:%.*]] = shufflevector <4 x double> [[V1]], <4 x double> [[V2]], <4 x i32> <i32 0, i32 2, i32 4, i32 6>
+; SSE-NEXT:    ret <4 x double> [[RES]]
+;
+; AVX-LABEL: define <4 x double> @test_double_4_to_2(
+; AVX-SAME: ptr [[X:%.*]], ptr [[Y:%.*]]) #[[ATTR0:[0-9]+]] {
+; AVX-NEXT:    [[V1:%.*]] = load <4 x double>, ptr [[X]], align 16
+; AVX-NEXT:    [[V2:%.*]] = load <4 x double>, ptr [[Y]], align 16
+; AVX-NEXT:    [[RES:%.*]] = shufflevector <4 x double> [[V1]], <4 x double> [[V2]], <4 x i32> <i32 0, i32 2, i32 4, i32 6>
+; AVX-NEXT:    ret <4 x double> [[RES]]
+;
+  %x0 = load <2 x double>, ptr %x
+  %x_off = getelementptr i8, ptr %x, i64 16
+  %x1 = load <2 x double>, ptr %x_off
+  %y0 = load <2 x double>, ptr %y
+  %y_off = getelementptr i8, ptr %y, i64 16
+  %y1 = load <2 x double>, ptr %y_off
+  %v1 = shufflevector <2 x double> %x0, <2 x double> %x1, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %v2 = shufflevector <2 x double> %y0, <2 x double> %y1, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %res = shufflevector <4 x double> %v1, <4 x double> %v2, <4 x i32> <i32 0, i32 2, i32 4, i32 6>
+  ret <4 x double> %res
+}
+
+define <4 x double> @test_double_3_to_1(ptr %p) {
+; SSE-LABEL: define <4 x double> @test_double_3_to_1(
+; SSE-SAME: ptr [[P:%.*]]) #[[ATTR0]] {
+; SSE-NEXT:    [[L1:%.*]] = load <2 x double>, ptr [[P]], align 16
+; SSE-NEXT:    [[P_PLUS_8:%.*]] = getelementptr i8, ptr [[P]], i64 8
+; SSE-NEXT:    [[L2:%.*]] = load <2 x double>, ptr [[P_PLUS_8]], align 8
+; SSE-NEXT:    [[P_PLUS_16:%.*]] = getelementptr i8, ptr [[P]], i64 16
+; SSE-NEXT:    [[L3:%.*]] = load <2 x double>, ptr [[P_PLUS_16]], align 16
+; SSE-NEXT:    [[V1:%.*]] = shufflevector <2 x double> [[L1]], <2 x double> [[L2]], <2 x i32> <i32 0, i32 3>
+; SSE-NEXT:    [[RES:%.*]] = shufflevector <2 x double> [[V1]], <2 x double> [[L3]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+; SSE-NEXT:    ret <4 x double> [[RES]]
+;
+; AVX-LABEL: define <4 x double> @test_double_3_to_1(
+; AVX-SAME: ptr [[P:%.*]]) #[[ATTR0]] {
+; AVX-NEXT:    [[TMP1:%.*]] = load <4 x double>, ptr [[P]], align 8
+; AVX-NEXT:    [[RES:%.*]] = shufflevector <4 x double> [[TMP1]], <4 x double> [[TMP1]], <4 x i32> <i32 0, i32 2, i32 2, i32 3>
+; AVX-NEXT:    ret <4 x double> [[RES]]
+;
+  %L1 = load <2 x double>, ptr %p, align 16
+  %p_plus_8 = getelementptr i8, ptr %p, i64 8
+  %L2 = load <2 x double>, ptr %p_plus_8, align 8
+  %p_plus_16 = getelementptr i8, ptr %p, i64 16
+  %L3 = load <2 x double>, ptr %p_plus_16, align 16
+  %v1 = shufflevector <2 x double> %L1, <2 x double> %L2, <2 x i32> <i32 0, i32 3>
+  %res = shufflevector <2 x double> %v1, <2 x double> %L3, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  ret <4 x double> %res
+}
+
+; Float
+define <8 x float> @test_float_4_to_2(ptr %x, ptr %y) {
+; CHECK-LABEL: define <8 x float> @test_float_4_to_2(
+; CHECK-SAME: ptr [[X:%.*]], ptr [[Y:%.*]]) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT:    [[V1:%.*]] = load <4 x float>, ptr [[X]], align 8
+; CHECK-NEXT:    [[V2:%.*]] = load <4 x float>, ptr [[Y]], align 8
+; CHECK-NEXT:    [[RES:%.*]] = shufflevector <4 x float> [[V1]], <4 x float> [[V2]], <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
+; CHECK-NEXT:    ret <8 x float> [[RES]]
+;
+  %x0 = load <2 x float>, ptr %x
+  %x_off = getelementptr i8, ptr %x, i64 8
+  %x1 = load <2 x float>, ptr %x_off
+  %y0 = load <2 x float>, ptr %y
+  %y_off = getelementptr i8, ptr %y, i64 8
+  %y1 = load <2 x float>, ptr %y_off
+  %v1 = shufflevector <2 x float> %x0, <2 x float> %x1, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %v2 = shufflevector <2 x float> %y0, <2 x float> %y1, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %v1w = shufflevector <4 x float> %v1, <4 x float> poison, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 poison, i32 poison, i32 poison, i32 poison>
+  %v2w = shufflevector <4 x float> %v2, <4 x float> poison, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 poison, i32 poison, i32 poison, i32 poison>
+  %res = shufflevector <8 x float> %v1w, <8 x float> %v2w, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 8, i32 9, i32 10, i32 11>
+  ret <8 x float> %res
+}
+
+define <4 x float> @test_float_3_to_1(ptr %p) {
+; CHECK-LABEL: define <4 x float> @test_float_3_to_1(
+; CHECK-SAME: ptr [[P:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT:    [[TMP1:%.*]] = load <4 x float>, ptr [[P]], align 4
+; CHECK-NEXT:    [[RES:%.*]] = shufflevector <4 x float> [[TMP1]], <4 x float> [[TMP1]], <4 x i32> <i32 0, i32 2, i32 2, i32 3>
+; CHECK-NEXT:    ret <4 x float> [[RES]]
+;
+  %L0 = load <2 x float>, ptr %p, align 8
+  %p1 = getelementptr i8, ptr %p, i64 4
+  %L1 = load <2 x float>, ptr %p1, align 4
+  %p2 = getelementptr i8, ptr %p, i64 8
+  %L2 = load <2 x float>, ptr %p2, align 4
+  %v1 = shufflevector <2 x float> %L0, <2 x float> %L1, <2 x i32> <i32 0, i32 3>
+  %res = shufflevector <2 x float> %v1, <2 x float> %L2, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  ret <4 x float> %res
+}
+
+; i64
+define <4 x i64> @test_i64_4_to_2(ptr %x, ptr %y) {
+; SSE-LABEL: define <4 x i64> @test_i64_4_to_2(
+; SSE-SAME: ptr [[X:%.*]], ptr [[Y:%.*]]) #[[ATTR0]] {
+; SSE-NEXT:    [[X0:%.*]] = load <2 x i64>, ptr [[X]], align 16
+; SSE-NEXT:    [[XA:%.*]] = getelementptr i8, ptr [[X]], i64 16
+; SSE-NEXT:    [[X1:%.*]] = load <2 x i64>, ptr [[XA]], align 16
+; SSE-NEXT:    [[Y0:%.*]] = load <2 x i64>, ptr [[Y]], align 16
+; SSE-NEXT:    [[YA:%.*]] = getelementptr i8, ptr [[Y]], i64 16
+; SSE-NEXT:    [[Y1:%.*]] = load <2 x i64>, ptr [[YA]], align 16
+; SSE-NEXT:    [[VX:%.*]] = shufflevector <2 x i64> [[X0]], <2 x i64> [[X1]], <4 x i32> <i32 1, i32 0, i32 3, i32 2>
+; SSE-NEXT:    [[VY:%.*]] = shufflevector <2 x i64> [[Y0]], <2 x i64> [[Y1]], <4 x i32> <i32 1, i32 0, i32 3, i32 2>
+; SSE-NEXT:    [[RES:%.*]] = shufflevector <4 x i64> [[VX]], <4 x i64> [[VY]], <4 x i32> <i32 1, i32 3, i32 5, i32 7>
+; SSE-NEXT:    ret <4 x i64> [[RES]]
+;
+; AVX-LABEL: define <4 x i64> @test_i64_4_to_2(
+; AVX-SAME: ptr [[X:%.*]], ptr [[Y:%.*]]) #[[ATTR0]] {
+; AVX-NEXT:    [[TMP1:%.*]] = load <4 x i64>, ptr [[X]], align 16
+; AVX-NEXT:    [[TMP2:%.*]] = load <4 x i64>, ptr [[Y]], align 16
+; AVX-NEXT:    [[RES:%.*]] = shufflevector <4 x i64> [[TMP1]], <4 x i64> [[TMP2]], <4 x i32> <i32 0, i32 2, i32 4, i32 6>
+; AVX-NEXT:    ret <4 x i64> [[RES]]
+;
+  %x0 = load <2 x i64>, ptr %x, align 16
+  %xa = getelementptr i8, ptr %x, i64 16
+  %x1 = load <2 x i64>, ptr %xa, align 16
+  %y0 = load <2 x i64>, ptr %y, align 16
+  %ya = getelementptr i8, ptr %y, i64 16
+  %y1 = load <2 x i64>, ptr %ya, align 16
+  %vx = shufflevector <2 x i64> %x0, <2 x i64> %x1, <4 x i32> <i32 1, i32 0, i32 3, i32 2>
+  %vy = shufflevector <2 x i64> %y0, <2 x i64> %y1, <4 x i32> <i32 1, i32 0, i32 3, i32 2>
+  %res = shufflevector <4 x i64> %vx, <4 x i64> %vy, <4 x i32> <i32 1, i32 3, i32 5, i32 7>
+  ret <4 x i64> %res
+}
+
+define <4 x i64> @test_i64_3_to_1_success(ptr %p) {
+; SSE-LABEL: define <4 x i64> @test_i64_3_to_1_success(
+; SSE-SAME: ptr [[P:%.*]]) #[[ATTR0]] {
+; SSE-NEXT:    [[L0:%.*]] = load <2 x i64>, ptr [[P]], align 16
+; SSE-NEXT:    [[PA:%.*]] = getelementptr i8, ptr [[P]], i64 8
+; SSE-NEXT:    [[L1:%.*]] = load <2 x i64>, ptr [[PA]], align 8
+; SSE-NEXT:    [[PB:%.*]] = getelementptr i8, ptr [[P]], i64 16
+; SSE-NEXT:    [[L2:%.*]] = load <2 x i64>, ptr [[PB]], align 16
+; SSE-NEXT:    [[V1:%.*]] = shufflevector <2 x i64> [[L0]], <2 x i64> [[L1]], <2 x i32> <i32 1, i32 2>
+; SSE-NEXT:    [[RES:%.*]] = shufflevector <2 x i64> [[V1]], <2 x i64> [[L2]], <4 x i32> <i32 1, i32 0, i32 3, i32 2>
+; SSE-NEXT:    ret <4 x i64> [[RES]]
+;
+; AVX-LABEL: define <4 x i64> @test_i64_3_to_1_success(
+; AVX-SAME: ptr [[P:%.*]]) #[[ATTR0]] {
+; AVX-NEXT:    [[TMP1:%.*]] = load <4 x i64>, ptr [[P]], align 8
+; AVX-NEXT:    [[RES:%.*]] = shufflevector <4 x i64> [[TMP1]], <4 x i64> [[TMP1]], <4 x i32> <i32 1, i32 1, i32 3, i32 2>
+; AVX-NEXT:    ret <4 x i64> [[RES]]
+;
+  %L0 = load <2 x i64>, ptr %p, align 16
+  %pa = getelementptr i8, ptr %p, i64 8
+  %L1 = load <2 x i64>, ptr %pa, align 8
+  %pb = getelementptr i8, ptr %p, i64 16
+  %L2 = load <2 x i64>, ptr %pb, align 16
+  %v1 = shufflevector <2 x i64> %L0, <2 x i64> %L1, <2 x i32> <i32 1, i32 2>
+  %res = shufflevector <2 x i64> %v1, <2 x i64> %L2, <4 x i32> <i32 1, i32 0, i32 3, i32 2>
+  ret <4 x i64> %res
+}
+
+; i32
+define <4 x i32> @test_i32_4_to_2_success(ptr %x, ptr %y) {
+; CHECK-LABEL: define <4 x i32> @test_i32_4_to_2_success(
+; CHECK-SAME: ptr [[X:%.*]], ptr [[Y:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT:    [[VX:%.*]] = load <4 x i32>, ptr [[X]], align 8
+; CHECK-NEXT:    [[VY:%.*]] = load <4 x i32>, ptr [[Y]], align 8
+; CHECK-NEXT:    [[RES:%.*]] = shufflevector <4 x i32> [[VX]], <4 x i32> [[VY]], <4 x i32> <i32 1, i32 3, i32 5, i32 7>
+; CHECK-NEXT:    ret <4 x i32> [[RES]]
+;
+  %x0 = load <2 x i32>, ptr %x, align 8
+  %xa = getelementptr i8, ptr %x, i64 8
+  %x1 = load <2 x i32>, ptr %xa, align 8
+  %y0 = load <2 x i32>, ptr %y, align 8
+  %ya = getelementptr i8, ptr %y, i64 8
+  %y1 = load <2 x i32>, ptr %ya, align 8
+  %vx = shufflevector <2 x i32> %x0, <2 x i32> %x1, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %vy = shufflevector <2 x i32> %y0, <2 x i32> %y1, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %res = shufflevector <4 x i32> %vx, <4 x i32> %vy, <4 x i32> <i32 1, i32 3, i32 5, i32 7>
+  ret <4 x i32> %res
+}
+
+define <4 x i32> @test_i32_3_to_1(ptr %p) {
+; CHECK-LABEL: define <4 x i32> @test_i32_3_to_1(
+; CHECK-SAME: ptr [[P:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT:    [[TMP1:%.*]] = load <4 x i32>, ptr [[P]], align 4
+; CHECK-NEXT:    [[RES:%.*]] = shufflevector <4 x i32> [[TMP1]], <4 x i32> [[TMP1]], <4 x i32> <i32 0, i32 2, 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 4
+  %L1 = load <2 x i32>, ptr %p1, align 4
+  %p2 = getelementptr i8, ptr %p, i64 8
+  %L2 = load <2 x i32>, ptr %p2, align 8
+  %v1 = shufflevector <2 x i32> %L0, <2 x i32> %L1, <2 x i32> <i32 0, i32 3>
+  %res = shufflevector <2 x i32> %v1, <2 x i32> %L2, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  ret <4 x i32> %res
+}
+
+; i16
+define <8 x i16> @test_i16_4_to_2(ptr %x, ptr %y) {
+; CHECK-LABEL: define <8 x i16> @test_i16_4_to_2(
+; CHECK-SAME: ptr [[X:%.*]], ptr [[Y:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT:    [[VX:%.*]] = load <4 x i16>, ptr [[X]], align 4
+; CHECK-NEXT:    [[VY:%.*]] = load <4 x i16>, ptr [[Y]], align 4
+; CHECK-NEXT:    [[RES:%.*]] = shufflevector <4 x i16> [[VX]], <4 x i16> [[VY]], <8 x i32> <i32 0, i32 4, i32 1, i32 5, i32 2, i32 6, i32 3, i32 7>
+; CHECK-NEXT:    ret <8 x i16> [[RES]]
+;
+  %x0 = load <2 x i16>, ptr %x, align 4
+  %xa = getelementptr i8, ptr %x, i64 4
+  %x1 = load <2 x i16>, ptr %xa, align 4
+  %y0 = load <2 x i16>, ptr %y, align 4
+  %ya = getelementptr i8, ptr %y, i64 4
+  %y1 = load <2 x i16>, ptr %ya, align 4
+  %vx = shufflevector <2 x i16> %x0, <2 x i16> %x1, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %vy = shufflevector <2 x i16> %y0, <2 x i16> %y1, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %res = shufflevector <4 x i16> %vx, <4 x i16> %vy, <8 x i32> <i32 0, i32 4, i32 1, i32 5, i32 2, i32 6, i32 3, i32 7>
+  ret <8 x i16> %res
+}
+
+define <8 x i16> @test_i16_3_to_1(ptr %p) {
+; CHECK-LABEL: define <8 x i16> @test_i16_3_to_1(
+; CHECK-SAME: ptr [[P:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT:    [[TMP1:%.*]] = load <8 x i16>, ptr [[P]], align 4
+; CHECK-NEXT:    [[RES:%.*]] = shufflevector <8 x i16> [[TMP1]], <8 x i16> [[TMP1]], <8 x i32> <i32 1, i32 0, i32 3, i32 2, i32 5, i32 4, i32 7, i32 6>
+; CHECK-NEXT:    ret <8 x i16> [[RES]]
+;
+  %L0 = load <2 x i16>, ptr %p, align 4
+  %p1 = getelementptr i8, ptr %p, i64 4
+  %L1 = load <2 x i16>, ptr %p1, align 4
+  %p2 = getelementptr i8, ptr %p, i64 8
+  %L2 = load <4 x i16>, ptr %p2, align 8
+  %v1 = shufflevector <2 x i16> %L0, <2 x i16> %L1, <4 x i32> <i32 1, i32 0, i32 3, i32 2>
+  %res = shufflevector <4 x i16> %v1, <4 x i16> %L2, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 5, i32 4, i32 7, i32 6>
+  ret <8 x i16> %res
+}
+
+; ====================================================================================
+; Negative
+; ====================================================================================
+
+; Negative Case 1: Scalar Type Mismatch
+define <4 x i32> @test_neg_trunc_mismatch(ptr %p) {
+; CHECK-LABEL: define <4 x i32> @test_neg_trunc_mismatch(
+; CHECK-SAME: ptr [[P:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT:    [[L0:%.*]] = load <2 x i64>, ptr [[P]], align 16
+; CHECK-NEXT:    [[T0:%.*]] = trunc <2 x i64> [[L0]] to <2 x i32>
+; CHECK-NEXT:    [[P1:%.*]] = getelementptr i8, ptr [[P]], i64 16
+; CHECK-NEXT:    [[L1:%.*]] = load <2 x i32>, ptr [[P1]], align 8
+; CHECK-NEXT:    [[V1:%.*]] = shufflevector <2 x i32> [[T0]], <2 x i32> [[L1]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+; CHECK-NEXT:    ret <4 x i32> [[V1]]
+;
+  %L0 = load <2 x i64>, ptr %p, align 16
+  %T0 = trunc <2 x i64> %L0 to <2 x i32>
+  %p1 = getelementptr i8, ptr %p, i64 16
+  %L1 = load <2 x i32>, ptr %p1, align 8
+  %v1 = shufflevector <2 x i32> %T0, <2 x i32> %L1, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  ret <4 x i32> %v1
+}
+
+; Negative Case 2: Multi-use (hasOneUse() == false)
+define <4 x i32> @test_neg_multi_use(ptr %x, ptr %y) {
+; CHECK-LABEL: define <4 x i32> @test_neg_multi_use(
+; CHECK-SAME: ptr [[X:%.*]], ptr [[Y:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT:    [[X0:%.*]] = load <2 x i32>, ptr [[X]], align 8
+; CHECK-NEXT:    call void @use(<2 x i32> [[X0]])
+; CHECK-NEXT:    [[XA:%.*]] = getelementptr i8, ptr [[X]], i64 8
+; CHECK-NEXT:    [[X1:%.*]] = load <2 x i32>, ptr [[XA]], align 8
+; CHECK-NEXT:    [[VX:%.*]] = shufflevector <2 x i32> [[X0]], <2 x i32> [[X1]], <4 x i32> <i32 0, i32 2, i32 1, i32 3>
+; CHECK-NEXT:    [[TMP1:%.*]] = load <4 x i32>, ptr [[Y]], align 8
+; CHECK-NEXT:    [[VY:%.*]] = shufflevector <4 x i32> [[TMP1]], <4 x i32> [[TMP1]], <4 x i32> <i32 0, i32 2, i32 1, i32 3>
+; CHECK-NEXT:    [[RES:%.*]] = shufflevector <4 x i32> [[VX]], <4 x i32> [[VY]], <4 x i32> <i32 0, i32 1, i32 4, i32 5>
+; CHECK-NEXT:    ret <4 x i32> [[RES]]
+;
+  %x0 = load <2 x i32>, ptr %x, align 8
+  call void @use(<2 x i32> %x0)
+  %xa = getelementptr i8, ptr %x, i64 8
+  %x1 = load <2 x i32>, ptr %xa, align 8
+  %y0 = load <2 x i32>, ptr %y, align 8
+  %ya = getelementptr i8, ptr %y, i64 8
+  %y1 = load <2 x i32>, ptr %ya, align 8
+ ...
[truncated]

``````````

</details>


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


More information about the llvm-commits mailing list