[llvm] [ConstantFolding] Fix bitcasting vectors with non-integer ratios (PR #179640)

Kacper Doga via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 7 06:07:37 PDT 2026


https://github.com/varev-dev updated https://github.com/llvm/llvm-project/pull/179640

>From 42b4962256549224b524d15add0c4c049ff07d7e Mon Sep 17 00:00:00 2001
From: "Doga, Kacper" <kacper.doga at intel.com>
Date: Wed, 4 Feb 2026 09:37:02 +0000
Subject: [PATCH 1/5] Support bitcasting vectors with non-integer ratios.

---
 llvm/lib/Analysis/ConstantFolding.cpp         | 149 ++++++++++++------
 llvm/test/Transforms/InstCombine/cast.ll      |   4 +-
 .../InstSimplify/bitcast-vector-fold.ll       |  65 +++++++-
 .../SCCP/bitcast-vector-refinement.l.ll       |   3 +-
 4 files changed, 168 insertions(+), 53 deletions(-)

diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 3c64ad6f6b369..8eae8db1b8c2a 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -217,81 +217,138 @@ Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) {
   // conversion here, which depends on whether the input or output has
   // more elements.
   bool isLittleEndian = DL.isLittleEndian();
-
+  unsigned SrcBitSize = SrcEltTy->getPrimitiveSizeInBits();
+  unsigned DstBitSize = DL.getTypeSizeInBits(DstEltTy);
   SmallVector<Constant*, 32> Result;
+  unsigned SrcElt = 0;
+  APInt Rest(std::max(SrcBitSize, DstBitSize), 0);
+  unsigned RestBitSize = 0;
+  bool HasUndef = true;
+  bool HasPoison = false;
+
   if (NumDstElt < NumSrcElt) {
     // Handle: bitcast (<4 x i32> <i32 0, i32 1, i32 2, i32 3> to <2 x i64>)
-    Constant *Zero = Constant::getNullValue(DstEltTy);
-    unsigned Ratio = NumSrcElt/NumDstElt;
-    unsigned SrcBitSize = SrcEltTy->getPrimitiveSizeInBits();
-    unsigned SrcElt = 0;
-    for (unsigned i = 0; i != NumDstElt; ++i) {
-      // Build each element of the result.
-      Constant *Elt = Zero;
-      unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize*(Ratio-1);
-      for (unsigned j = 0; j != Ratio; ++j) {
-        Constant *Src = C->getAggregateElement(SrcElt++);
-        if (isa_and_nonnull<UndefValue>(Src))
-          Src = Constant::getNullValue(
-              cast<VectorType>(C->getType())->getElementType());
-        else
-          Src = dyn_cast_or_null<ConstantInt>(Src);
-        if (!Src)  // Reject constantexpr elements.
+    //         bitcast (<4 x i24> <i24 0, i24 1, i24 2, i24 3> to <3 x i32>)
+    APInt Zero = APInt::getZero(DstBitSize);
+    while (Result.size() != NumDstElt) {
+      APInt NextVecElem;
+      if (!HasUndef)
+        NextVecElem = Zero;
+      while (RestBitSize < DstBitSize) {
+        assert(SrcElt < NumSrcElt && "Source vector overflow.");
+        auto *Element = C->getAggregateElement(SrcElt++);
+        if (!Element) // Reject constantexpr elements.
           return ConstantExpr::getBitCast(C, DestTy);
 
-        // Zero extend the element to the right size.
-        Src = ConstantFoldCastOperand(Instruction::ZExt, Src, Elt->getType(),
-                                      DL);
-        assert(Src && "Constant folding cannot fail on plain integers");
+        RestBitSize += SrcBitSize;
+        HasUndef = isa<UndefValue>(Element);
+        if (HasUndef) {
+          HasPoison |= isa<PoisonValue>(Element);
+          continue;
+        }
 
-        // Shift it to the right place, depending on endianness.
-        Src = ConstantFoldBinaryOpOperands(
-            Instruction::Shl, Src, ConstantInt::get(Src->getType(), ShiftAmt),
-            DL);
-        assert(Src && "Constant folding cannot fail on plain integers");
+        auto *Src = dyn_cast<ConstantInt>(Element);
+        if (!Src)
+          return ConstantExpr::getBitCast(C, DestTy);
+        NextVecElem = Src->getValue();
+        NextVecElem = NextVecElem.zext(DstBitSize);
 
-        ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize;
+        // Shift next SrcElt to right place, depending on endianness.
+        if (isLittleEndian) {
+          Rest |= NextVecElem << RestBitSize - SrcBitSize;
+        } else {
+          if (RestBitSize <= DstBitSize)
+            Rest |= NextVecElem << (DstBitSize - RestBitSize);
+          else
+            Rest |= NextVecElem.lshr(RestBitSize - DstBitSize);
+        }
+      }
 
-        // Mix it in.
-        Elt = ConstantFoldBinaryOpOperands(Instruction::Or, Elt, Src, DL);
-        assert(Elt && "Constant folding cannot fail on plain integers");
+      RestBitSize -= DstBitSize;
+      if (NextVecElem.getBitWidth() != DstBitSize) {
+        if (HasPoison)
+          Result.push_back(PoisonValue::get(DstEltTy));
+        else
+          Result.push_back(UndefValue::get(DstEltTy));
+        continue;
       }
-      Result.push_back(Elt);
+      Result.push_back(ConstantInt::get(DstEltTy, Rest));
+
+      // Shift unused bits from last SrcElt to next DstElt right place.
+      if (isLittleEndian)
+        Rest = NextVecElem.lshr(SrcBitSize - RestBitSize);
+      else
+        Rest = NextVecElem << (DstBitSize - RestBitSize);
     }
     return ConstantVector::get(Result);
   }
 
   // Handle: bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>)
+  //         bitcast (<3 x i64> <i64 0, i64 1, i64 2> to <8 x i24>)
   unsigned Ratio = NumDstElt/NumSrcElt;
-  unsigned DstBitSize = DL.getTypeSizeInBits(DstEltTy);
-
-  // Loop over each source value, expanding into multiple results.
-  for (unsigned i = 0; i != NumSrcElt; ++i) {
-    auto *Element = C->getAggregateElement(i);
-
+  while (Result.size() != NumDstElt) {
+    unsigned UnusedBits = SrcBitSize - RestBitSize;
+    if (RestBitSize >= DstBitSize) {
+      if (!HasUndef) {
+        APInt Elt =
+            Rest.lshr(isLittleEndian ? UnusedBits : (RestBitSize - DstBitSize));
+        Result.push_back(ConstantInt::get(DstEltTy, Elt.trunc(DstBitSize)));
+      } else if (HasPoison) {
+        Result.push_back(PoisonValue::get(DstEltTy));
+        HasPoison = RestBitSize > DstBitSize;
+      } else {
+        Result.push_back(UndefValue::get(DstEltTy));
+        HasUndef = RestBitSize > DstBitSize;
+      }
+      RestBitSize -= DstBitSize;
+      continue;
+    }
+    auto *Element = C->getAggregateElement(SrcElt++);
     if (!Element) // Reject constantexpr elements.
       return ConstantExpr::getBitCast(C, DestTy);
 
+    APInt NextVecElem;
     if (isa<UndefValue>(Element)) {
       // Correctly Propagate undef values.
-      Result.append(Ratio, UndefValue::get(DstEltTy));
-      continue;
+      HasUndef = true;
+      HasPoison = isa<PoisonValue>(Element);
+      if (RestBitSize == 0) {
+        RestBitSize += SrcBitSize;
+        continue;
+      }
+      NextVecElem = APInt::getZero(SrcBitSize);
+    } else {
+      auto *Src = dyn_cast<ConstantInt>(Element);
+      if (!Src)
+        return ConstantExpr::getBitCast(C, DestTy);
+      NextVecElem = Src->getValue();
+      HasUndef = false;
     }
-
-    auto *Src = dyn_cast<ConstantInt>(Element);
-    if (!Src)
-      return ConstantExpr::getBitCast(C, DestTy);
-
-    unsigned ShiftAmt = isLittleEndian ? 0 : DstBitSize*(Ratio-1);
+    APInt Value = Rest;
+    if (SrcBitSize % DstBitSize)
+      Rest = NextVecElem;
+    if (RestBitSize != 0) {
+      // Shift the Rest into the right place, shift NextVecElem to fit Rest.
+      if (isLittleEndian) {
+        Value.lshrInPlace(UnusedBits);
+        NextVecElem <<= RestBitSize;
+      } else {
+        Value <<= UnusedBits;
+        NextVecElem.lshrInPlace(RestBitSize);
+      }
+    }
+    Value |= NextVecElem;
+    unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize - DstBitSize;
     for (unsigned j = 0; j != Ratio; ++j) {
       // Shift the piece of the value into the right place, depending on
       // endianness.
-      APInt Elt = Src->getValue().lshr(ShiftAmt);
+      APInt Elt = Value.lshr(ShiftAmt);
       ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize;
 
       // Truncate and remember this piece.
       Result.push_back(ConstantInt::get(DstEltTy, Elt.trunc(DstBitSize)));
     }
+    RestBitSize += SrcBitSize - Ratio * DstBitSize;
   }
 
   return ConstantVector::get(Result);
diff --git a/llvm/test/Transforms/InstCombine/cast.ll b/llvm/test/Transforms/InstCombine/cast.ll
index 6047d61e8dbff..b82cb23acc29b 100644
--- a/llvm/test/Transforms/InstCombine/cast.ll
+++ b/llvm/test/Transforms/InstCombine/cast.ll
@@ -1438,10 +1438,10 @@ define i32 @test89() {
 
 define <2 x i32> @test90() {
 ; BE-LABEL: @test90(
-; BE-NEXT:    ret <2 x i32> <i32 0, i32 15360>
+; BE-NEXT:    ret <2 x i32> <i32 poison, i32 15360>
 ;
 ; LE-LABEL: @test90(
-; LE-NEXT:    ret <2 x i32> <i32 0, i32 1006632960>
+; LE-NEXT:    ret <2 x i32> <i32 poison, i32 1006632960>
 ;
   %t6 = bitcast <4 x half> <half poison, half poison, half poison, half 0xH3C00> to <2 x i32>
   ret <2 x i32> %t6
diff --git a/llvm/test/Transforms/InstSimplify/bitcast-vector-fold.ll b/llvm/test/Transforms/InstSimplify/bitcast-vector-fold.ll
index d2656e291547c..ede6db4dbc3e0 100644
--- a/llvm/test/Transforms/InstSimplify/bitcast-vector-fold.ll
+++ b/llvm/test/Transforms/InstSimplify/bitcast-vector-fold.ll
@@ -1,7 +1,7 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt < %s -passes=instsimplify -S | FileCheck %s
-; RUN: opt < %s -passes=instsimplify -use-constant-fp-for-fixed-length-splat -use-constant-int-for-fixed-length-splat -S | FileCheck %s
-target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-f64:32:64-v64:64:64-v128:128:128"
+; RUN: opt < %s -passes=instsimplify -S -data-layout="e-p:32:32:32-i1:8:8-i8:8:8-f64:32:64-v64:64:64-v128:128:128" | FileCheck %s --check-prefixes=CHECK
+; RUN: opt < %s -passes=instsimplify -use-constant-fp-for-fixed-length-splat -use-constant-int-for-fixed-length-splat -S -data-layout="e-p:32:32:32-i1:8:8-i8:8:8-f64:32:64-v64:64:64-v128:128:128" | FileCheck %s --check-prefixes=CHECK
+; RUN: opt < %s -passes=instsimplify -S -data-layout="E-p:32:32:32-i1:8:8-i8:8:8-f64:32:64-v64:64:64-v128:128:128" | FileCheck %s --check-prefixes=BE
 
 define <2 x i64> @test1() {
 ; CHECK-LABEL: @test1(
@@ -83,6 +83,49 @@ define <1 x i1> @test10() {
   ret <1 x i1> %ret
 }
 
+define <4 x i24> @test11() {
+; CHECK-LABEL: @test11(
+; CHECK-NEXT:    ret <4 x i24> <i24 1, i24 256, i24 65536, i24 0>
+;
+; BE-LABEL: @test11(
+; BE-NEXT:    ret <4 x i24> <i24 0, i24 65536, i24 256, i24 1>
+;
+  %c = bitcast <3 x i32> <i32 1, i32 1, i32 1> to <4 x i24>
+  ret <4 x i24> %c
+}
+
+define <8 x i24> @test12() {
+; CHECK-LABEL: @test12(
+; CHECK-NEXT:    ret <8 x i24> splat (i24 1)
+;
+; BE-LABEL: @test12(
+; BE-NEXT:    ret <8 x i24> <i24 256, i24 256, i24 257, i24 1, i24 1, i24 0, i24 65536, i24 65536>
+  %c = bitcast <3 x i64> <i64 281474993487873, i64 72057598332895488, i64 1099511693312> to <8 x i24>
+  ret <8 x i24> %c
+}
+
+define <3 x i32> @test13() {
+; CHECK-LABEL: @test13(
+; CHECK-NEXT:    ret <3 x i32> splat (i32 1)
+;
+; BE-LABEL: @test13(
+; BE-NEXT:    ret <3 x i32> <i32 256, i32 16777472, i32 0>
+;
+  %c = bitcast <4 x i24> <i24 1, i24 256, i24 65536, i24 0> to <3 x i32>
+  ret <3 x i32> %c
+}
+
+define <3 x i64> @test14() {
+; CHECK-LABEL: @test14(
+; CHECK-NEXT:    ret <3 x i64> <i64 281474993487873, i64 72057598332895488, i64 1099511693312>
+;
+; BE-LABEL: @test14(
+; BE-NEXT:    ret <3 x i64> <i64 1099511693312, i64 72057598332895488, i64 281474993487873>
+;
+  %c = bitcast <8 x i24> splat (i24 1) to <3 x i64>
+  ret <3 x i64> %c
+}
+
 ; from MultiSource/Benchmarks/Bullet
 define <2 x float> @foo() {
 ; CHECK-LABEL: @foo(
@@ -277,6 +320,22 @@ define <16 x i8> @bitcast_constexpr_16i8_8i16_u256uuu256uu() {
   ret <16 x i8> %cast
 }
 
+define <4 x i24> @bitcast_constexpr_4i24_3i32_u1u() {
+; CHECK-LABEL: @bitcast_constexpr_4i24_3i32_u1u(
+; CHECK-NEXT:    ret <4 x i24> <i24 undef, i24 256, i24 0, i24 undef>
+;
+  %cast = bitcast <3 x i32><i32 undef, i32 1, i32 undef> to <4 x i24>
+  ret <4 x i24> %cast
+}
+
+define <3 x i32> @bitcast_constexpr_3i32_4i24_n1255uu() {
+; CHECK-LABEL: @bitcast_constexpr_3i32_4i24_n1255uu(
+; CHECK-NEXT:    ret <3 x i32> <i32 -1, i32 0, i32 undef>
+;
+  %cast = bitcast <4 x i24><i24 -1, i24 255, i24 undef, i24 undef> to <3 x i32>
+  ret <3 x i32> %cast
+}
+
 define <1 x i32> @bitcast_constexpr_scalar_fp_to_vector_int() {
 ; CHECK-LABEL: @bitcast_constexpr_scalar_fp_to_vector_int(
 ; CHECK-NEXT:    ret <1 x i32> splat (i32 1065353216)
diff --git a/llvm/test/Transforms/SCCP/bitcast-vector-refinement.l.ll b/llvm/test/Transforms/SCCP/bitcast-vector-refinement.l.ll
index 94df835e317b7..754f749128869 100644
--- a/llvm/test/Transforms/SCCP/bitcast-vector-refinement.l.ll
+++ b/llvm/test/Transforms/SCCP/bitcast-vector-refinement.l.ll
@@ -10,8 +10,7 @@ define <32 x i8> @test(i1 %cond) {
 ; CHECK-NEXT:    [[TMP0:%.*]] = phi <4 x i64> [ zeroinitializer, %[[ENTRY]] ], [ splat (i64 1), %[[FOR_COND2]] ]
 ; CHECK-NEXT:    br i1 [[COND]], label %[[FOR_COND2]], label %[[IF_THEN:.*]]
 ; CHECK:       [[IF_THEN]]:
-; CHECK-NEXT:    [[TMP1:%.*]] = bitcast <4 x i64> zeroinitializer to <32 x i8>
-; CHECK-NEXT:    ret <32 x i8> [[TMP1]]
+; CHECK-NEXT:    ret <32 x i8> zeroinitializer
 ;
 entry:
   br label %for.cond2

>From 9a1d450bd47cf5ee267a08eded2e3dbee218f2a4 Mon Sep 17 00:00:00 2001
From: "Doga, Kacper" <kacper.doga at intel.com>
Date: Fri, 27 Mar 2026 16:42:22 +0000
Subject: [PATCH 2/5] refactor: implement buffer dependent bitcasting merging
 both paths

---
 llvm/lib/Analysis/ConstantFolding.cpp | 168 ++++++++------------------
 1 file changed, 52 insertions(+), 116 deletions(-)

diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 8eae8db1b8c2a..62361ca7e6774 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -213,142 +213,78 @@ Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) {
   }
 
   // Now we know that the input and output vectors are both integer vectors
-  // of the same size, and that their #elements is not the same.  Do the
-  // conversion here, which depends on whether the input or output has
-  // more elements.
+  // of the same size, and that their #elements is not the same. 
+  // Use data buffer for easy non-integer element ratio vectors handling,
+  // For example: <4 x i24> to <3 x i32>.
   bool isLittleEndian = DL.isLittleEndian();
   unsigned SrcBitSize = SrcEltTy->getPrimitiveSizeInBits();
   unsigned DstBitSize = DL.getTypeSizeInBits(DstEltTy);
   SmallVector<Constant*, 32> Result;
   unsigned SrcElt = 0;
-  APInt Rest(std::max(SrcBitSize, DstBitSize), 0);
-  unsigned RestBitSize = 0;
-  bool HasUndef = true;
-  bool HasPoison = false;
-
-  if (NumDstElt < NumSrcElt) {
-    // Handle: bitcast (<4 x i32> <i32 0, i32 1, i32 2, i32 3> to <2 x i64>)
-    //         bitcast (<4 x i24> <i24 0, i24 1, i24 2, i24 3> to <3 x i32>)
-    APInt Zero = APInt::getZero(DstBitSize);
-    while (Result.size() != NumDstElt) {
-      APInt NextVecElem;
-      if (!HasUndef)
-        NextVecElem = Zero;
-      while (RestBitSize < DstBitSize) {
-        assert(SrcElt < NumSrcElt && "Source vector overflow.");
-        auto *Element = C->getAggregateElement(SrcElt++);
-        if (!Element) // Reject constantexpr elements.
-          return ConstantExpr::getBitCast(C, DestTy);
 
-        RestBitSize += SrcBitSize;
-        HasUndef = isa<UndefValue>(Element);
-        if (HasUndef) {
-          HasPoison |= isa<PoisonValue>(Element);
-          continue;
-        }
+  APInt Buffer(2 * std::max(SrcBitSize, DstBitSize), 0);
+  APInt UndefMask(Buffer.getBitWidth(), 0);
+  APInt PoisonMask(Buffer.getBitWidth(), 0);
+  unsigned BufferBitSize = 0;
+  
+  while (Result.size() != NumDstElt) {
+    // Load SrcElts into Buffer.
+    while (BufferBitSize < DstBitSize) {
+      Constant *Element = C->getAggregateElement(SrcElt++);
+      if (!Element) // Reject constantexpr elements
+        return ConstantExpr::getBitCast(C, DestTy);
 
+      // Shift Buffer & Masks to fit next SrcElt.
+      unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize;
+      Buffer = Buffer.shl(ShiftAmt);
+      UndefMask = UndefMask.shl(ShiftAmt);
+      PoisonMask = PoisonMask.shl(ShiftAmt);
+
+      APInt SrcValue;
+      unsigned BitPosition = isLittleEndian ? BufferBitSize : 0;
+      if (isa<UndefValue>(Element)) {
+        // Set masks fragments bits.
+        UndefMask.setBits(BitPosition, BitPosition+SrcBitSize);
+        if (isa<PoisonValue>(Element))
+          PoisonMask.setBits(BitPosition, BitPosition+SrcBitSize);
+        SrcValue = APInt::getZero(DstBitSize);
+      } else {
         auto *Src = dyn_cast<ConstantInt>(Element);
         if (!Src)
           return ConstantExpr::getBitCast(C, DestTy);
-        NextVecElem = Src->getValue();
-        NextVecElem = NextVecElem.zext(DstBitSize);
+        SrcValue = Src->getValue();
+      } 
 
-        // Shift next SrcElt to right place, depending on endianness.
-        if (isLittleEndian) {
-          Rest |= NextVecElem << RestBitSize - SrcBitSize;
-        } else {
-          if (RestBitSize <= DstBitSize)
-            Rest |= NextVecElem << (DstBitSize - RestBitSize);
-          else
-            Rest |= NextVecElem.lshr(RestBitSize - DstBitSize);
-        }
-      }
+      // Insert src element bits into Buffer on correct position.
+      Buffer.insertBits(SrcValue, BitPosition);
+      BufferBitSize += SrcBitSize;
+    }
 
-      RestBitSize -= DstBitSize;
-      if (NextVecElem.getBitWidth() != DstBitSize) {
-        if (HasPoison)
+    // Create DstElts from Buffer.
+    while (BufferBitSize >= DstBitSize) {
+      unsigned ShiftAmt = isLittleEndian ? 0 : BufferBitSize - DstBitSize;
+      // Emit undef/poison, if all undef mask fragment bits are set.
+      if (UndefMask.lshr(ShiftAmt).trunc(DstBitSize).isAllOnes()) {
+        // Push poison, if any bit in poison mask fragment is set.
+        if (!PoisonMask.lshr(ShiftAmt).trunc(DstBitSize).isZero()) {
           Result.push_back(PoisonValue::get(DstEltTy));
-        else
+        } else {
           Result.push_back(UndefValue::get(DstEltTy));
-        continue;
-      }
-      Result.push_back(ConstantInt::get(DstEltTy, Rest));
-
-      // Shift unused bits from last SrcElt to next DstElt right place.
-      if (isLittleEndian)
-        Rest = NextVecElem.lshr(SrcBitSize - RestBitSize);
-      else
-        Rest = NextVecElem << (DstBitSize - RestBitSize);
-    }
-    return ConstantVector::get(Result);
-  }
-
-  // Handle: bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>)
-  //         bitcast (<3 x i64> <i64 0, i64 1, i64 2> to <8 x i24>)
-  unsigned Ratio = NumDstElt/NumSrcElt;
-  while (Result.size() != NumDstElt) {
-    unsigned UnusedBits = SrcBitSize - RestBitSize;
-    if (RestBitSize >= DstBitSize) {
-      if (!HasUndef) {
-        APInt Elt =
-            Rest.lshr(isLittleEndian ? UnusedBits : (RestBitSize - DstBitSize));
-        Result.push_back(ConstantInt::get(DstEltTy, Elt.trunc(DstBitSize)));
-      } else if (HasPoison) {
-        Result.push_back(PoisonValue::get(DstEltTy));
-        HasPoison = RestBitSize > DstBitSize;
+        }
       } else {
-        Result.push_back(UndefValue::get(DstEltTy));
-        HasUndef = RestBitSize > DstBitSize;
+        // Create and push DstElt.
+        APInt Elt = Buffer.lshr(ShiftAmt).trunc(DstBitSize);
+        Result.push_back(ConstantInt::get(DstEltTy, Elt));
       }
-      RestBitSize -= DstBitSize;
-      continue;
-    }
-    auto *Element = C->getAggregateElement(SrcElt++);
-    if (!Element) // Reject constantexpr elements.
-      return ConstantExpr::getBitCast(C, DestTy);
 
-    APInt NextVecElem;
-    if (isa<UndefValue>(Element)) {
-      // Correctly Propagate undef values.
-      HasUndef = true;
-      HasPoison = isa<PoisonValue>(Element);
-      if (RestBitSize == 0) {
-        RestBitSize += SrcBitSize;
-        continue;
-      }
-      NextVecElem = APInt::getZero(SrcBitSize);
-    } else {
-      auto *Src = dyn_cast<ConstantInt>(Element);
-      if (!Src)
-        return ConstantExpr::getBitCast(C, DestTy);
-      NextVecElem = Src->getValue();
-      HasUndef = false;
-    }
-    APInt Value = Rest;
-    if (SrcBitSize % DstBitSize)
-      Rest = NextVecElem;
-    if (RestBitSize != 0) {
-      // Shift the Rest into the right place, shift NextVecElem to fit Rest.
+      // Shift unused Buffer fragment to lower bits.
       if (isLittleEndian) {
-        Value.lshrInPlace(UnusedBits);
-        NextVecElem <<= RestBitSize;
-      } else {
-        Value <<= UnusedBits;
-        NextVecElem.lshrInPlace(RestBitSize);
+        Buffer.lshrInPlace(DstBitSize);
+        UndefMask.lshrInPlace(DstBitSize);
+        PoisonMask.lshrInPlace(DstBitSize); 
       }
+      BufferBitSize -= DstBitSize;
     }
-    Value |= NextVecElem;
-    unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize - DstBitSize;
-    for (unsigned j = 0; j != Ratio; ++j) {
-      // Shift the piece of the value into the right place, depending on
-      // endianness.
-      APInt Elt = Value.lshr(ShiftAmt);
-      ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize;
-
-      // Truncate and remember this piece.
-      Result.push_back(ConstantInt::get(DstEltTy, Elt.trunc(DstBitSize)));
-    }
-    RestBitSize += SrcBitSize - Ratio * DstBitSize;
   }
 
   return ConstantVector::get(Result);

>From 17bdd3938fc68258cf38cb29999d9ea391d1cb84 Mon Sep 17 00:00:00 2001
From: "Doga, Kacper" <kacper.doga at intel.com>
Date: Fri, 27 Mar 2026 16:46:03 +0000
Subject: [PATCH 3/5] update lit tests: expect undef for upper lane in extrqi
 results

---
 llvm/lib/Analysis/ConstantFolding.cpp                | 12 ++++++------
 .../InstCombine/X86/x86-sse4a-inseltpoison.ll        |  2 +-
 llvm/test/Transforms/InstCombine/X86/x86-sse4a.ll    |  2 +-
 3 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 62361ca7e6774..3188ab1cedbcd 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -213,7 +213,7 @@ Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) {
   }
 
   // Now we know that the input and output vectors are both integer vectors
-  // of the same size, and that their #elements is not the same. 
+  // of the same size, and that their #elements is not the same.
   // Use data buffer for easy non-integer element ratio vectors handling,
   // For example: <4 x i24> to <3 x i32>.
   bool isLittleEndian = DL.isLittleEndian();
@@ -226,7 +226,7 @@ Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) {
   APInt UndefMask(Buffer.getBitWidth(), 0);
   APInt PoisonMask(Buffer.getBitWidth(), 0);
   unsigned BufferBitSize = 0;
-  
+
   while (Result.size() != NumDstElt) {
     // Load SrcElts into Buffer.
     while (BufferBitSize < DstBitSize) {
@@ -244,16 +244,16 @@ Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) {
       unsigned BitPosition = isLittleEndian ? BufferBitSize : 0;
       if (isa<UndefValue>(Element)) {
         // Set masks fragments bits.
-        UndefMask.setBits(BitPosition, BitPosition+SrcBitSize);
+        UndefMask.setBits(BitPosition, BitPosition + SrcBitSize);
         if (isa<PoisonValue>(Element))
-          PoisonMask.setBits(BitPosition, BitPosition+SrcBitSize);
+          PoisonMask.setBits(BitPosition, BitPosition + SrcBitSize);
         SrcValue = APInt::getZero(DstBitSize);
       } else {
         auto *Src = dyn_cast<ConstantInt>(Element);
         if (!Src)
           return ConstantExpr::getBitCast(C, DestTy);
         SrcValue = Src->getValue();
-      } 
+      }
 
       // Insert src element bits into Buffer on correct position.
       Buffer.insertBits(SrcValue, BitPosition);
@@ -281,7 +281,7 @@ Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) {
       if (isLittleEndian) {
         Buffer.lshrInPlace(DstBitSize);
         UndefMask.lshrInPlace(DstBitSize);
-        PoisonMask.lshrInPlace(DstBitSize); 
+        PoisonMask.lshrInPlace(DstBitSize);
       }
       BufferBitSize -= DstBitSize;
     }
diff --git a/llvm/test/Transforms/InstCombine/X86/x86-sse4a-inseltpoison.ll b/llvm/test/Transforms/InstCombine/X86/x86-sse4a-inseltpoison.ll
index 7462e311b02ad..8b92eb4c59cc5 100644
--- a/llvm/test/Transforms/InstCombine/X86/x86-sse4a-inseltpoison.ll
+++ b/llvm/test/Transforms/InstCombine/X86/x86-sse4a-inseltpoison.ll
@@ -138,7 +138,7 @@ define <2 x i64> @test_extrqi_constant_poison(<2 x i64> %x) {
 
 define <2 x i64> @test_extrqi_call_constexpr() {
 ; CHECK-LABEL: @test_extrqi_call_constexpr(
-; CHECK-NEXT:    ret <2 x i64> zeroinitializer
+; CHECK-NEXT:    ret <2 x i64> <i64 0, i64 undef>
 ;
   %1 = tail call <2 x i64> @llvm.x86.sse4a.extrqi(<2 x i64> bitcast (<16 x i8> trunc (<16 x i16> bitcast (<4 x i64> <i64 0, i64 poison, i64 2, i64 poison> to <16 x i16>) to <16 x i8>) to <2 x i64>), i8 8, i8 16)
   ret <2 x i64> %1
diff --git a/llvm/test/Transforms/InstCombine/X86/x86-sse4a.ll b/llvm/test/Transforms/InstCombine/X86/x86-sse4a.ll
index 6277986288a8c..237deac09dece 100644
--- a/llvm/test/Transforms/InstCombine/X86/x86-sse4a.ll
+++ b/llvm/test/Transforms/InstCombine/X86/x86-sse4a.ll
@@ -138,7 +138,7 @@ define <2 x i64> @test_extrqi_constant_undef(<2 x i64> %x) {
 
 define <2 x i64> @test_extrqi_call_constexpr() {
 ; CHECK-LABEL: @test_extrqi_call_constexpr(
-; CHECK-NEXT:    ret <2 x i64> zeroinitializer
+; CHECK-NEXT:    ret <2 x i64> <i64 0, i64 undef>
 ;
   %1 = tail call <2 x i64> @llvm.x86.sse4a.extrqi(<2 x i64> bitcast (<16 x i8> trunc (<16 x i16> bitcast (<4 x i64> <i64 0, i64 undef, i64 2, i64 undef> to <16 x i16>) to <16 x i8>) to <2 x i64>), i8 8, i8 16)
   ret <2 x i64> %1

>From 0d08f8af5300c005f69b915ff56452890cb74bf0 Mon Sep 17 00:00:00 2001
From: "Doga, Kacper" <kacper.doga at intel.com>
Date: Tue, 7 Apr 2026 11:02:33 +0200
Subject: [PATCH 4/5] Address review feedback

- use getPrimitiveSizeInBits for both source and destination types
- simplify endianness handling - no dummy shifting by zero
- replace shl with << operator
- use extractBits instead of lshr+trunc
- update lits with CHECK,{LE, BE} prefixes
---
 llvm/lib/Analysis/ConstantFolding.cpp         |  17 +-
 .../InstSimplify/bitcast-vector-fold.ll       | 212 ++++++++++++------
 2 files changed, 156 insertions(+), 73 deletions(-)

diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 3188ab1cedbcd..81ee02300af8a 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -218,7 +218,7 @@ Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) {
   // For example: <4 x i24> to <3 x i32>.
   bool isLittleEndian = DL.isLittleEndian();
   unsigned SrcBitSize = SrcEltTy->getPrimitiveSizeInBits();
-  unsigned DstBitSize = DL.getTypeSizeInBits(DstEltTy);
+  unsigned DstBitSize = DstEltTy->getPrimitiveSizeInBits();
   SmallVector<Constant*, 32> Result;
   unsigned SrcElt = 0;
 
@@ -235,10 +235,11 @@ Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) {
         return ConstantExpr::getBitCast(C, DestTy);
 
       // Shift Buffer & Masks to fit next SrcElt.
-      unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize;
-      Buffer = Buffer.shl(ShiftAmt);
-      UndefMask = UndefMask.shl(ShiftAmt);
-      PoisonMask = PoisonMask.shl(ShiftAmt);
+      if (!isLittleEndian) {
+        Buffer <<= SrcBitSize; 
+        UndefMask <<= SrcBitSize;
+        PoisonMask <<= SrcBitSize;
+      }
 
       APInt SrcValue;
       unsigned BitPosition = isLittleEndian ? BufferBitSize : 0;
@@ -264,16 +265,16 @@ Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) {
     while (BufferBitSize >= DstBitSize) {
       unsigned ShiftAmt = isLittleEndian ? 0 : BufferBitSize - DstBitSize;
       // Emit undef/poison, if all undef mask fragment bits are set.
-      if (UndefMask.lshr(ShiftAmt).trunc(DstBitSize).isAllOnes()) {
+      if (UndefMask.extractBits(DstBitSize, ShiftAmt).isAllOnes()) {
         // Push poison, if any bit in poison mask fragment is set.
-        if (!PoisonMask.lshr(ShiftAmt).trunc(DstBitSize).isZero()) {
+        if (!PoisonMask.extractBits(DstBitSize, ShiftAmt).isZero()) {
           Result.push_back(PoisonValue::get(DstEltTy));
         } else {
           Result.push_back(UndefValue::get(DstEltTy));
         }
       } else {
         // Create and push DstElt.
-        APInt Elt = Buffer.lshr(ShiftAmt).trunc(DstBitSize);
+        APInt Elt = Buffer.extractBits(DstBitSize, ShiftAmt);
         Result.push_back(ConstantInt::get(DstEltTy, Elt));
       }
 
diff --git a/llvm/test/Transforms/InstSimplify/bitcast-vector-fold.ll b/llvm/test/Transforms/InstSimplify/bitcast-vector-fold.ll
index ede6db4dbc3e0..be00d78fc88d5 100644
--- a/llvm/test/Transforms/InstSimplify/bitcast-vector-fold.ll
+++ b/llvm/test/Transforms/InstSimplify/bitcast-vector-fold.ll
@@ -1,59 +1,80 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt < %s -passes=instsimplify -S -data-layout="e-p:32:32:32-i1:8:8-i8:8:8-f64:32:64-v64:64:64-v128:128:128" | FileCheck %s --check-prefixes=CHECK
-; RUN: opt < %s -passes=instsimplify -use-constant-fp-for-fixed-length-splat -use-constant-int-for-fixed-length-splat -S -data-layout="e-p:32:32:32-i1:8:8-i8:8:8-f64:32:64-v64:64:64-v128:128:128" | FileCheck %s --check-prefixes=CHECK
-; RUN: opt < %s -passes=instsimplify -S -data-layout="E-p:32:32:32-i1:8:8-i8:8:8-f64:32:64-v64:64:64-v128:128:128" | FileCheck %s --check-prefixes=BE
+; RUN: opt < %s -passes=instsimplify -S -data-layout="e-p:32:32:32-i1:8:8-i8:8:8-f64:32:64-v64:64:64-v128:128:128" | FileCheck %s --check-prefixes=CHECK,LE
+; RUN: opt < %s -passes=instsimplify -use-constant-fp-for-fixed-length-splat -use-constant-int-for-fixed-length-splat -S -data-layout="e-p:32:32:32-i1:8:8-i8:8:8-f64:32:64-v64:64:64-v128:128:128" | FileCheck %s --check-prefixes=CHECK,LE
+; RUN: opt < %s -passes=instsimplify -S -data-layout="E-p:32:32:32-i1:8:8-i8:8:8-f64:32:64-v64:64:64-v128:128:128" | FileCheck %s --check-prefixes=CHECK,BE
 
 define <2 x i64> @test1() {
-; CHECK-LABEL: @test1(
-; CHECK-NEXT:    ret <2 x i64> <i64 4294967296, i64 12884901890>
+; LE-LABEL: @test1(
+; LE-NEXT:    ret <2 x i64> <i64 4294967296, i64 12884901890>
+;
+; BE-LABEL: @test1(
+; BE-NEXT:    ret <2 x i64> <i64 1, i64 8589934595>
 ;
   %tmp3 = bitcast <4 x i32> < i32 0, i32 1, i32 2, i32 3 > to <2 x i64>
   ret <2 x i64> %tmp3
 }
 
 define <4 x i32> @test2() {
-; CHECK-LABEL: @test2(
-; CHECK-NEXT:    ret <4 x i32> <i32 0, i32 0, i32 1, i32 0>
+; LE-LABEL: @test2(
+; LE-NEXT:    ret <4 x i32> <i32 0, i32 0, i32 1, i32 0>
+;
+; BE-LABEL: @test2(
+; BE-NEXT:    ret <4 x i32> <i32 0, i32 0, i32 0, i32 1>
 ;
   %tmp3 = bitcast <2 x i64> < i64 0, i64 1 > to <4 x i32>
   ret <4 x i32> %tmp3
 }
 
 define <2 x double> @test3() {
-; CHECK-LABEL: @test3(
-; CHECK-NEXT:    ret <2 x double> <double 0x100000000, double 0x300000002>
+; LE-LABEL: @test3(
+; LE-NEXT:    ret <2 x double> <double 0x100000000, double 0x300000002>
+;
+; BE-LABEL: @test3(
+; BE-NEXT:    ret <2 x double> <double 4.940660e-324, double 0x200000003>
 ;
   %tmp3 = bitcast <4 x i32> < i32 0, i32 1, i32 2, i32 3 > to <2 x double>
   ret <2 x double> %tmp3
 }
 
 define <4 x float> @test4() {
-; CHECK-LABEL: @test4(
-; CHECK-NEXT:    ret <4 x float> <float 0.000000e+00, float 0.000000e+00, float 0x36A0000000000000, float 0.000000e+00>
+; LE-LABEL: @test4(
+; LE-NEXT:    ret <4 x float> <float 0.000000e+00, float 0.000000e+00, float 0x36A0000000000000, float 0.000000e+00>
+;
+; BE-LABEL: @test4(
+; BE-NEXT:    ret <4 x float> <float 0.000000e+00, float 0.000000e+00, float 0.000000e+00, float 0x36A0000000000000>
 ;
   %tmp3 = bitcast <2 x i64> < i64 0, i64 1 > to <4 x float>
   ret <4 x float> %tmp3
 }
 
 define <2 x i64> @test5() {
-; CHECK-LABEL: @test5(
-; CHECK-NEXT:    ret <2 x i64> <i64 4575657221408423936, i64 4629700418010611712>
+; LE-LABEL: @test5(
+; LE-NEXT:    ret <2 x i64> <i64 4575657221408423936, i64 4629700418010611712>
+;
+; BE-LABEL: @test5(
+; BE-NEXT:    ret <2 x i64> <i64 1065353216, i64 4611686019505324032>
 ;
   %tmp3 = bitcast <4 x float> <float 0.0, float 1.0, float 2.0, float 3.0> to <2 x i64>
   ret <2 x i64> %tmp3
 }
 
 define <4 x i32> @test6() {
-; CHECK-LABEL: @test6(
-; CHECK-NEXT:    ret <4 x i32> <i32 0, i32 1071644672, i32 0, i32 1072693248>
+; LE-LABEL: @test6(
+; LE-NEXT:    ret <4 x i32> <i32 0, i32 1071644672, i32 0, i32 1072693248>
+;
+; BE-LABEL: @test6(
+; BE-NEXT:    ret <4 x i32> <i32 1071644672, i32 0, i32 1072693248, i32 0>
 ;
   %tmp3 = bitcast <2 x double> <double 0.5, double 1.0> to <4 x i32>
   ret <4 x i32> %tmp3
 }
 
 define i32 @test7() {
-; CHECK-LABEL: @test7(
-; CHECK-NEXT:    ret i32 1118464
+; LE-LABEL: @test7(
+; LE-NEXT:    ret i32 1118464
+;
+; BE-LABEL: @test7(
+; BE-NEXT:    ret i32 285212689
 ;
   %tmp3 = bitcast <2 x half> <half 0xH1100, half 0xH0011> to i32
   ret i32 %tmp3
@@ -84,8 +105,8 @@ define <1 x i1> @test10() {
 }
 
 define <4 x i24> @test11() {
-; CHECK-LABEL: @test11(
-; CHECK-NEXT:    ret <4 x i24> <i24 1, i24 256, i24 65536, i24 0>
+; LE-LABEL: @test11(
+; LE-NEXT:    ret <4 x i24> <i24 1, i24 256, i24 65536, i24 0>
 ;
 ; BE-LABEL: @test11(
 ; BE-NEXT:    ret <4 x i24> <i24 0, i24 65536, i24 256, i24 1>
@@ -95,18 +116,19 @@ define <4 x i24> @test11() {
 }
 
 define <8 x i24> @test12() {
-; CHECK-LABEL: @test12(
-; CHECK-NEXT:    ret <8 x i24> splat (i24 1)
+; LE-LABEL: @test12(
+; LE-NEXT:    ret <8 x i24> splat (i24 1)
 ;
 ; BE-LABEL: @test12(
 ; BE-NEXT:    ret <8 x i24> <i24 256, i24 256, i24 257, i24 1, i24 1, i24 0, i24 65536, i24 65536>
+;
   %c = bitcast <3 x i64> <i64 281474993487873, i64 72057598332895488, i64 1099511693312> to <8 x i24>
   ret <8 x i24> %c
 }
 
 define <3 x i32> @test13() {
-; CHECK-LABEL: @test13(
-; CHECK-NEXT:    ret <3 x i32> splat (i32 1)
+; LE-LABEL: @test13(
+; LE-NEXT:    ret <3 x i32> splat (i32 1)
 ;
 ; BE-LABEL: @test13(
 ; BE-NEXT:    ret <3 x i32> <i32 256, i32 16777472, i32 0>
@@ -116,8 +138,8 @@ define <3 x i32> @test13() {
 }
 
 define <3 x i64> @test14() {
-; CHECK-LABEL: @test14(
-; CHECK-NEXT:    ret <3 x i64> <i64 281474993487873, i64 72057598332895488, i64 1099511693312>
+; LE-LABEL: @test14(
+; LE-NEXT:    ret <3 x i64> <i64 281474993487873, i64 72057598332895488, i64 1099511693312>
 ;
 ; BE-LABEL: @test14(
 ; BE-NEXT:    ret <3 x i64> <i64 1099511693312, i64 72057598332895488, i64 281474993487873>
@@ -177,160 +199,220 @@ define <2 x double> @foo6() {
 }
 
 define <4 x i32> @bitcast_constexpr_4i32_2i64_u2() {
-; CHECK-LABEL: @bitcast_constexpr_4i32_2i64_u2(
-; CHECK-NEXT:    ret <4 x i32> <i32 undef, i32 undef, i32 2, i32 0>
+; LE-LABEL: @bitcast_constexpr_4i32_2i64_u2(
+; LE-NEXT:    ret <4 x i32> <i32 undef, i32 undef, i32 2, i32 0>
+;
+; BE-LABEL: @bitcast_constexpr_4i32_2i64_u2(
+; BE-NEXT:    ret <4 x i32> <i32 undef, i32 undef, i32 0, i32 2>
 ;
   %cast = bitcast <2 x i64><i64 undef, i64 2> to <4 x i32>
   ret <4 x i32> %cast
 }
 
 define <4 x i32> @bitcast_constexpr_4i32_2i64_1u() {
-; CHECK-LABEL: @bitcast_constexpr_4i32_2i64_1u(
-; CHECK-NEXT:    ret <4 x i32> <i32 1, i32 0, i32 undef, i32 undef>
+; LE-LABEL: @bitcast_constexpr_4i32_2i64_1u(
+; LE-NEXT:    ret <4 x i32> <i32 1, i32 0, i32 undef, i32 undef>
+;
+; BE-LABEL: @bitcast_constexpr_4i32_2i64_1u(
+; BE-NEXT:    ret <4 x i32> <i32 0, i32 1, i32 undef, i32 undef>
 ;
   %cast = bitcast <2 x i64><i64 1, i64 undef> to <4 x i32>
   ret <4 x i32> %cast
 }
 
 define <4 x i32> @bitcast_constexpr_4i32_2i64() {
-; CHECK-LABEL: @bitcast_constexpr_4i32_2i64(
-; CHECK-NEXT:    ret <4 x i32> <i32 undef, i32 undef, i32 2, i32 0>
+; LE-LABEL: @bitcast_constexpr_4i32_2i64(
+; LE-NEXT:    ret <4 x i32> <i32 undef, i32 undef, i32 2, i32 0>
+;
+; BE-LABEL: @bitcast_constexpr_4i32_2i64(
+; BE-NEXT:    ret <4 x i32> <i32 undef, i32 undef, i32 0, i32 2>
 ;
   %cast = bitcast <2 x i64><i64 undef, i64 2> to <4 x i32>
   ret <4 x i32> %cast
 }
 
 define <8 x i16> @bitcast_constexpr_8i16_2i64_u2() {
-; CHECK-LABEL: @bitcast_constexpr_8i16_2i64_u2(
-; CHECK-NEXT:    ret <8 x i16> <i16 undef, i16 undef, i16 undef, i16 undef, i16 2, i16 0, i16 0, i16 0>
+; LE-LABEL: @bitcast_constexpr_8i16_2i64_u2(
+; LE-NEXT:    ret <8 x i16> <i16 undef, i16 undef, i16 undef, i16 undef, i16 2, i16 0, i16 0, i16 0>
+;
+; BE-LABEL: @bitcast_constexpr_8i16_2i64_u2(
+; BE-NEXT:    ret <8 x i16> <i16 undef, i16 undef, i16 undef, i16 undef, i16 0, i16 0, i16 0, i16 2>
 ;
   %cast = bitcast <2 x i64><i64 undef, i64 2> to <8 x i16>
   ret <8 x i16> %cast
 }
 
 define <8 x i16> @bitcast_constexpr_8i16_2i64_1u() {
-; CHECK-LABEL: @bitcast_constexpr_8i16_2i64_1u(
-; CHECK-NEXT:    ret <8 x i16> <i16 1, i16 0, i16 0, i16 0, i16 undef, i16 undef, i16 undef, i16 undef>
+; LE-LABEL: @bitcast_constexpr_8i16_2i64_1u(
+; LE-NEXT:    ret <8 x i16> <i16 1, i16 0, i16 0, i16 0, i16 undef, i16 undef, i16 undef, i16 undef>
+;
+; BE-LABEL: @bitcast_constexpr_8i16_2i64_1u(
+; BE-NEXT:    ret <8 x i16> <i16 0, i16 0, i16 0, i16 1, i16 undef, i16 undef, i16 undef, i16 undef>
 ;
   %cast = bitcast <2 x i64><i64 1, i64 undef> to <8 x i16>
   ret <8 x i16> %cast
 }
 
 define <8 x i16> @bitcast_constexpr_8i16_2i64_u65536() {
-; CHECK-LABEL: @bitcast_constexpr_8i16_2i64_u65536(
-; CHECK-NEXT:    ret <8 x i16> <i16 undef, i16 undef, i16 undef, i16 undef, i16 0, i16 1, i16 0, i16 0>
+; LE-LABEL: @bitcast_constexpr_8i16_2i64_u65536(
+; LE-NEXT:    ret <8 x i16> <i16 undef, i16 undef, i16 undef, i16 undef, i16 0, i16 1, i16 0, i16 0>
+;
+; BE-LABEL: @bitcast_constexpr_8i16_2i64_u65536(
+; BE-NEXT:    ret <8 x i16> <i16 undef, i16 undef, i16 undef, i16 undef, i16 0, i16 0, i16 1, i16 0>
 ;
   %cast = bitcast <2 x i64><i64 undef, i64 65536> to <8 x i16>
   ret <8 x i16> %cast
 }
 
 define <16 x i8> @bitcast_constexpr_16i8_2i64_u2() {
-; CHECK-LABEL: @bitcast_constexpr_16i8_2i64_u2(
-; CHECK-NEXT:    ret <16 x i8> <i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 2, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0>
+; LE-LABEL: @bitcast_constexpr_16i8_2i64_u2(
+; LE-NEXT:    ret <16 x i8> <i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 2, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0>
+;
+; BE-LABEL: @bitcast_constexpr_16i8_2i64_u2(
+; BE-NEXT:    ret <16 x i8> <i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 2>
 ;
   %cast = bitcast <2 x i64><i64 undef, i64 2> to <16 x i8>
   ret <16 x i8> %cast
 }
 
 define <16 x i8> @bitcast_constexpr_16i8_2i64_256u() {
-; CHECK-LABEL: @bitcast_constexpr_16i8_2i64_256u(
-; CHECK-NEXT:    ret <16 x i8> <i8 0, i8 1, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef>
+; LE-LABEL: @bitcast_constexpr_16i8_2i64_256u(
+; LE-NEXT:    ret <16 x i8> <i8 0, i8 1, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef>
+;
+; BE-LABEL: @bitcast_constexpr_16i8_2i64_256u(
+; BE-NEXT:    ret <16 x i8> <i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 1, i8 0, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef>
 ;
   %cast = bitcast <2 x i64><i64 256, i64 undef> to <16 x i8>
   ret <16 x i8> %cast
 }
 
 define <16 x i8> @bitcast_constexpr_16i8_2i64_u256() {
-; CHECK-LABEL: @bitcast_constexpr_16i8_2i64_u256(
-; CHECK-NEXT:    ret <16 x i8> <i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 0, i8 1, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0>
+; LE-LABEL: @bitcast_constexpr_16i8_2i64_u256(
+; LE-NEXT:    ret <16 x i8> <i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 0, i8 1, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0>
+;
+; BE-LABEL: @bitcast_constexpr_16i8_2i64_u256(
+; BE-NEXT:    ret <16 x i8> <i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 1, i8 0>
 ;
   %cast = bitcast <2 x i64><i64 undef, i64 256> to <16 x i8>
   ret <16 x i8> %cast
 }
 
 define <8 x i16> @bitcast_constexpr_8i16_4i32_uu22() {
-; CHECK-LABEL: @bitcast_constexpr_8i16_4i32_uu22(
-; CHECK-NEXT:    ret <8 x i16> <i16 undef, i16 undef, i16 undef, i16 undef, i16 2, i16 0, i16 2, i16 0>
+; LE-LABEL: @bitcast_constexpr_8i16_4i32_uu22(
+; LE-NEXT:    ret <8 x i16> <i16 undef, i16 undef, i16 undef, i16 undef, i16 2, i16 0, i16 2, i16 0>
+;
+; BE-LABEL: @bitcast_constexpr_8i16_4i32_uu22(
+; BE-NEXT:    ret <8 x i16> <i16 undef, i16 undef, i16 undef, i16 undef, i16 0, i16 2, i16 0, i16 2>
 ;
   %cast = bitcast <4 x i32><i32 undef, i32 undef, i32 2, i32 2> to <8 x i16>
   ret <8 x i16> %cast
 }
 
 define <8 x i16> @bitcast_constexpr_8i16_4i32_10uu() {
-; CHECK-LABEL: @bitcast_constexpr_8i16_4i32_10uu(
-; CHECK-NEXT:    ret <8 x i16> <i16 1, i16 0, i16 0, i16 0, i16 undef, i16 undef, i16 undef, i16 undef>
+; LE-LABEL: @bitcast_constexpr_8i16_4i32_10uu(
+; LE-NEXT:    ret <8 x i16> <i16 1, i16 0, i16 0, i16 0, i16 undef, i16 undef, i16 undef, i16 undef>
+;
+; BE-LABEL: @bitcast_constexpr_8i16_4i32_10uu(
+; BE-NEXT:    ret <8 x i16> <i16 0, i16 1, i16 0, i16 0, i16 undef, i16 undef, i16 undef, i16 undef>
 ;
   %cast = bitcast <4 x i32><i32 1, i32 0, i32 undef, i32 undef> to <8 x i16>
   ret <8 x i16> %cast
 }
 
 define <8 x i16> @bitcast_constexpr_8i16_4i32_u257u256() {
-; CHECK-LABEL: @bitcast_constexpr_8i16_4i32_u257u256(
-; CHECK-NEXT:    ret <8 x i16> <i16 undef, i16 undef, i16 0, i16 1, i16 undef, i16 undef, i16 0, i16 1>
+; LE-LABEL: @bitcast_constexpr_8i16_4i32_u257u256(
+; LE-NEXT:    ret <8 x i16> <i16 undef, i16 undef, i16 0, i16 1, i16 undef, i16 undef, i16 0, i16 1>
+;
+; BE-LABEL: @bitcast_constexpr_8i16_4i32_u257u256(
+; BE-NEXT:    ret <8 x i16> <i16 undef, i16 undef, i16 1, i16 0, i16 undef, i16 undef, i16 1, i16 0>
 ;
   %cast = bitcast <4 x i32><i32 undef, i32 65536, i32 undef, i32 65536> to <8 x i16>
   ret <8 x i16> %cast
 }
 
 define <16 x i8> @bitcast_constexpr_16i8_4i32_u2u2() {
-; CHECK-LABEL: @bitcast_constexpr_16i8_4i32_u2u2(
-; CHECK-NEXT:    ret <16 x i8> <i8 undef, i8 undef, i8 undef, i8 undef, i8 2, i8 0, i8 0, i8 0, i8 undef, i8 undef, i8 undef, i8 undef, i8 2, i8 0, i8 0, i8 0>
+; LE-LABEL: @bitcast_constexpr_16i8_4i32_u2u2(
+; LE-NEXT:    ret <16 x i8> <i8 undef, i8 undef, i8 undef, i8 undef, i8 2, i8 0, i8 0, i8 0, i8 undef, i8 undef, i8 undef, i8 undef, i8 2, i8 0, i8 0, i8 0>
+;
+; BE-LABEL: @bitcast_constexpr_16i8_4i32_u2u2(
+; BE-NEXT:    ret <16 x i8> <i8 undef, i8 undef, i8 undef, i8 undef, i8 0, i8 0, i8 0, i8 2, i8 undef, i8 undef, i8 undef, i8 undef, i8 0, i8 0, i8 0, i8 2>
 ;
   %cast = bitcast <4 x i32><i32 undef, i32 2, i32 undef, i32 2> to <16 x i8>
   ret <16 x i8> %cast
 }
 
 define <16 x i8> @bitcast_constexpr_16i8_4i32_1u1u() {
-; CHECK-LABEL: @bitcast_constexpr_16i8_4i32_1u1u(
-; CHECK-NEXT:    ret <16 x i8> <i8 1, i8 0, i8 0, i8 0, i8 undef, i8 undef, i8 undef, i8 undef, i8 1, i8 0, i8 0, i8 0, i8 undef, i8 undef, i8 undef, i8 undef>
+; LE-LABEL: @bitcast_constexpr_16i8_4i32_1u1u(
+; LE-NEXT:    ret <16 x i8> <i8 1, i8 0, i8 0, i8 0, i8 undef, i8 undef, i8 undef, i8 undef, i8 1, i8 0, i8 0, i8 0, i8 undef, i8 undef, i8 undef, i8 undef>
+;
+; BE-LABEL: @bitcast_constexpr_16i8_4i32_1u1u(
+; BE-NEXT:    ret <16 x i8> <i8 0, i8 0, i8 0, i8 1, i8 undef, i8 undef, i8 undef, i8 undef, i8 0, i8 0, i8 0, i8 1, i8 undef, i8 undef, i8 undef, i8 undef>
 ;
   %cast = bitcast <4 x i32><i32 1, i32 undef, i32 1, i32 undef> to <16 x i8>
   ret <16 x i8> %cast
 }
 
 define <16 x i8> @bitcast_constexpr_16i8_4i32_u256uu() {
-; CHECK-LABEL: @bitcast_constexpr_16i8_4i32_u256uu(
-; CHECK-NEXT:    ret <16 x i8> <i8 undef, i8 undef, i8 undef, i8 undef, i8 0, i8 1, i8 0, i8 0, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef>
+; LE-LABEL: @bitcast_constexpr_16i8_4i32_u256uu(
+; LE-NEXT:    ret <16 x i8> <i8 undef, i8 undef, i8 undef, i8 undef, i8 0, i8 1, i8 0, i8 0, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef>
+;
+; BE-LABEL: @bitcast_constexpr_16i8_4i32_u256uu(
+; BE-NEXT:    ret <16 x i8> <i8 undef, i8 undef, i8 undef, i8 undef, i8 0, i8 0, i8 1, i8 0, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef>
 ;
   %cast = bitcast <4 x i32><i32 undef, i32 256, i32 undef, i32 undef> to <16 x i8>
   ret <16 x i8> %cast
 }
 
 define <16 x i8> @bitcast_constexpr_16i8_8i16_u2u2u2u2() {
-; CHECK-LABEL: @bitcast_constexpr_16i8_8i16_u2u2u2u2(
-; CHECK-NEXT:    ret <16 x i8> <i8 undef, i8 undef, i8 2, i8 0, i8 undef, i8 undef, i8 2, i8 0, i8 undef, i8 undef, i8 2, i8 0, i8 undef, i8 undef, i8 2, i8 0>
+; LE-LABEL: @bitcast_constexpr_16i8_8i16_u2u2u2u2(
+; LE-NEXT:    ret <16 x i8> <i8 undef, i8 undef, i8 2, i8 0, i8 undef, i8 undef, i8 2, i8 0, i8 undef, i8 undef, i8 2, i8 0, i8 undef, i8 undef, i8 2, i8 0>
+;
+; BE-LABEL: @bitcast_constexpr_16i8_8i16_u2u2u2u2(
+; BE-NEXT:    ret <16 x i8> <i8 undef, i8 undef, i8 0, i8 2, i8 undef, i8 undef, i8 0, i8 2, i8 undef, i8 undef, i8 0, i8 2, i8 undef, i8 undef, i8 0, i8 2>
 ;
   %cast = bitcast <8 x i16><i16 undef, i16 2, i16 undef, i16 2, i16 undef, i16 2, i16 undef, i16 2> to <16 x i8>
   ret <16 x i8> %cast
 }
 
 define <16 x i8> @bitcast_constexpr_16i8_8i16_1u1u1u1u() {
-; CHECK-LABEL: @bitcast_constexpr_16i8_8i16_1u1u1u1u(
-; CHECK-NEXT:    ret <16 x i8> <i8 1, i8 0, i8 undef, i8 undef, i8 1, i8 0, i8 undef, i8 undef, i8 1, i8 0, i8 undef, i8 undef, i8 1, i8 0, i8 undef, i8 undef>
+; LE-LABEL: @bitcast_constexpr_16i8_8i16_1u1u1u1u(
+; LE-NEXT:    ret <16 x i8> <i8 1, i8 0, i8 undef, i8 undef, i8 1, i8 0, i8 undef, i8 undef, i8 1, i8 0, i8 undef, i8 undef, i8 1, i8 0, i8 undef, i8 undef>
+;
+; BE-LABEL: @bitcast_constexpr_16i8_8i16_1u1u1u1u(
+; BE-NEXT:    ret <16 x i8> <i8 0, i8 1, i8 undef, i8 undef, i8 0, i8 1, i8 undef, i8 undef, i8 0, i8 1, i8 undef, i8 undef, i8 0, i8 1, i8 undef, i8 undef>
 ;
   %cast = bitcast <8 x i16><i16 1, i16 undef, i16 1, i16 undef, i16 1, i16 undef, i16 1, i16 undef> to <16 x i8>
   ret <16 x i8> %cast
 }
 
 define <16 x i8> @bitcast_constexpr_16i8_8i16_u256uuu256uu() {
-; CHECK-LABEL: @bitcast_constexpr_16i8_8i16_u256uuu256uu(
-; CHECK-NEXT:    ret <16 x i8> <i8 undef, i8 undef, i8 0, i8 1, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 0, i8 1, i8 undef, i8 undef, i8 undef, i8 undef>
+; LE-LABEL: @bitcast_constexpr_16i8_8i16_u256uuu256uu(
+; LE-NEXT:    ret <16 x i8> <i8 undef, i8 undef, i8 0, i8 1, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 0, i8 1, i8 undef, i8 undef, i8 undef, i8 undef>
+;
+; BE-LABEL: @bitcast_constexpr_16i8_8i16_u256uuu256uu(
+; BE-NEXT:    ret <16 x i8> <i8 undef, i8 undef, i8 1, i8 0, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 1, i8 0, i8 undef, i8 undef, i8 undef, i8 undef>
 ;
   %cast = bitcast <8 x i16><i16 undef, i16 256, i16 undef, i16 undef, i16 undef, i16 256, i16 undef, i16 undef> to <16 x i8>
   ret <16 x i8> %cast
 }
 
 define <4 x i24> @bitcast_constexpr_4i24_3i32_u1u() {
-; CHECK-LABEL: @bitcast_constexpr_4i24_3i32_u1u(
-; CHECK-NEXT:    ret <4 x i24> <i24 undef, i24 256, i24 0, i24 undef>
+; LE-LABEL: @bitcast_constexpr_4i24_3i32_u1u(
+; LE-NEXT:    ret <4 x i24> <i24 undef, i24 256, i24 0, i24 undef>
+;
+; BE-LABEL: @bitcast_constexpr_4i24_3i32_u1u(
+; BE-NEXT:    ret <4 x i24> <i24 undef, i24 0, i24 256, i24 undef>
 ;
   %cast = bitcast <3 x i32><i32 undef, i32 1, i32 undef> to <4 x i24>
   ret <4 x i24> %cast
 }
 
 define <3 x i32> @bitcast_constexpr_3i32_4i24_n1255uu() {
-; CHECK-LABEL: @bitcast_constexpr_3i32_4i24_n1255uu(
-; CHECK-NEXT:    ret <3 x i32> <i32 -1, i32 0, i32 undef>
+; LE-LABEL: @bitcast_constexpr_3i32_4i24_n1255uu(
+; LE-NEXT:    ret <3 x i32> <i32 -1, i32 0, i32 undef>
+;
+; BE-LABEL: @bitcast_constexpr_3i32_4i24_n1255uu(
+; BE-NEXT:    ret <3 x i32> <i32 -256, i32 0, i32 undef>
 ;
   %cast = bitcast <4 x i24><i24 -1, i24 255, i24 undef, i24 undef> to <3 x i32>
   ret <3 x i32> %cast

>From b1ab2e9aa7b4a2a789f7ea41edc3574dcd753561 Mon Sep 17 00:00:00 2001
From: "Doga, Kacper" <kacper.doga at intel.com>
Date: Tue, 7 Apr 2026 15:03:43 +0200
Subject: [PATCH 5/5] fix formatting

---
 llvm/lib/Analysis/ConstantFolding.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 81ee02300af8a..589d8e334b88d 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -236,7 +236,7 @@ Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) {
 
       // Shift Buffer & Masks to fit next SrcElt.
       if (!isLittleEndian) {
-        Buffer <<= SrcBitSize; 
+        Buffer <<= SrcBitSize;
         UndefMask <<= SrcBitSize;
         PoisonMask <<= SrcBitSize;
       }



More information about the llvm-commits mailing list