[llvm] [VectorCombine] Add binop(concat(x,y),concat(a,b)) -> concat(binop(x,a),binop(y,b)) fold (PR #215518)

via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 11 04:03:53 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-vectorizers

Author: Wenju He (wenju-he)

<details>
<summary>Changes</summary>

commit 95b1b6d removed this fold from InstCombine, claiming VectorCombine's cost-driven fold already covers it. It doesn't: foldShuffleOfBinops only handles the reverse direction (shuffle(binop,binop) -> binop(shuffle,shuffle)), and generic binops on fixed vectors never reach the binop-fold switch at all.

This PR adds the folding to address a downstream test regression.

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


2 Files Affected:

- (modified) llvm/lib/Transforms/Vectorize/VectorCombine.cpp (+62) 
- (added) llvm/test/Transforms/VectorCombine/X86/fold-binop-of-concats.ll (+178) 


``````````diff
diff --git a/llvm/lib/Transforms/Vectorize/VectorCombine.cpp b/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
index 9eeeef16c7bd3..c526d84c411a5 100644
--- a/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
+++ b/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
@@ -130,6 +130,7 @@ class VectorCombine {
   bool scalarizeVPIntrinsic(Instruction &I);
   bool foldExtractedCmps(Instruction &I);
   bool foldSelectsFromBitcast(Instruction &I);
+  bool foldBinopOfConcats(Instruction &I);
   bool foldBinopOfReductions(Instruction &I);
   bool foldSingleElementStore(Instruction &I);
   bool scalarizeLoad(Instruction &I);
@@ -1765,6 +1766,65 @@ static void analyzeCostOfVecReduction(const IntrinsicInst &II,
                                                       std::nullopt, CostKind);
 }
 
+/// Try to fold "binop(concat(L0,L1), concat(R0,R1))" into
+/// "concat(binop(L0,R0), binop(L1,R1))", the inverse of foldShuffleOfBinops.
+bool VectorCombine::foldBinopOfConcats(Instruction &I) {
+  auto *BinOp = dyn_cast<BinaryOperator>(&I);
+  if (!BinOp)
+    return false;
+
+  Value *L0, *L1, *R0, *R1;
+  ArrayRef<int> Mask;
+  if (!match(BinOp->getOperand(0),
+             m_OneUse(m_Shuffle(m_Value(L0), m_Value(L1), m_Mask(Mask)))) ||
+      !match(BinOp->getOperand(1), m_OneUse(m_Shuffle(m_Value(R0), m_Value(R1),
+                                                      m_SpecificMask(Mask)))))
+    return false;
+
+  if (!cast<ShuffleVectorInst>(BinOp->getOperand(0))->isConcat() ||
+      !cast<ShuffleVectorInst>(BinOp->getOperand(1))->isConcat())
+    return false;
+
+  // Don't introduce poison into div/rem.
+  if (BinOp->isIntDivRem() && llvm::is_contained(Mask, PoisonMaskElem))
+    return false;
+
+  // isConcat() guarantees fixed-vector operands of matching type.
+  auto *NarrowTy = cast<FixedVectorType>(L0->getType());
+  auto *WideTy = cast<FixedVectorType>(BinOp->getType());
+
+  Instruction::BinaryOps Opcode = BinOp->getOpcode();
+  InstructionCost ConcatCost =
+      TTI.getShuffleCost(TargetTransformInfo::SK_PermuteTwoSrc, WideTy,
+                         NarrowTy, Mask, CostKind, 0, nullptr, {L0, L1}, &I);
+  InstructionCost OldCost =
+      TTI.getArithmeticInstrCost(Opcode, WideTy, CostKind) + 2 * ConcatCost;
+  InstructionCost NewCost =
+      2 * TTI.getArithmeticInstrCost(Opcode, NarrowTy, CostKind) + ConcatCost;
+
+  LLVM_DEBUG(dbgs() << "Found a binop feeding two concat shuffles: " << I
+                    << "\n  OldCost: " << OldCost << " vs NewCost: " << NewCost
+                    << "\n");
+
+  // Strict comparison: a tie must not fold, or this could flip-flop forever
+  // with foldShuffleOfBinops, which treats some ties as profitable to merge.
+  if (NewCost >= OldCost)
+    return false;
+
+  Value *NewBO0 = Builder.CreateBinOp(Opcode, L0, R0);
+  if (auto *NewInst0 = dyn_cast<Instruction>(NewBO0))
+    NewInst0->copyIRFlags(BinOp);
+  Value *NewBO1 = Builder.CreateBinOp(Opcode, L1, R1);
+  if (auto *NewInst1 = dyn_cast<Instruction>(NewBO1))
+    NewInst1->copyIRFlags(BinOp);
+
+  Worklist.pushValue(NewBO0);
+  Worklist.pushValue(NewBO1);
+  Value *NewShuf = Builder.CreateShuffleVector(NewBO0, NewBO1, Mask);
+  replaceValue(I, *NewShuf);
+  return true;
+}
+
 bool VectorCombine::foldBinopOfReductions(Instruction &I) {
   Instruction::BinaryOps BinOpOpc = cast<BinaryOperator>(&I)->getOpcode();
   Intrinsic::ID ReductionIID = getReductionForBinop(BinOpOpc);
@@ -6580,6 +6640,8 @@ bool VectorCombine::run() {
     // The type checking is for run-time efficiency. We can avoid wasting time
     // dispatching to folding functions if there's no chance of matching.
     if (IsFixedVectorType) {
+      if (Instruction::isBinaryOp(Opcode) && foldBinopOfConcats(I))
+        return true;
       switch (Opcode) {
       case Instruction::InsertElement:
         if (foldInsExtFNeg(I))
diff --git a/llvm/test/Transforms/VectorCombine/X86/fold-binop-of-concats.ll b/llvm/test/Transforms/VectorCombine/X86/fold-binop-of-concats.ll
new file mode 100644
index 0000000000000..464b45b7606f5
--- /dev/null
+++ b/llvm/test/Transforms/VectorCombine/X86/fold-binop-of-concats.ll
@@ -0,0 +1,178 @@
+; 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=avx512vl | FileCheck %s --check-prefixes=CHECK
+; RUN: opt < %s -passes=vector-combine -S -mtriple=x86_64-- -mattr=avx2 | FileCheck %s --check-prefixes=AVX2
+
+define <4 x i8> @add_concat(<2 x i8> %a, <2 x i8> %b, <2 x i8> %c, <2 x i8> %d) {
+; CHECK-LABEL: define <4 x i8> @add_concat(
+; CHECK-SAME: <2 x i8> [[A:%.*]], <2 x i8> [[B:%.*]], <2 x i8> [[C:%.*]], <2 x i8> [[D:%.*]]) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT:    [[TMP1:%.*]] = add <2 x i8> [[A]], [[C]]
+; CHECK-NEXT:    [[TMP2:%.*]] = add <2 x i8> [[B]], [[D]]
+; CHECK-NEXT:    [[R:%.*]] = shufflevector <2 x i8> [[TMP1]], <2 x i8> [[TMP2]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+; CHECK-NEXT:    ret <4 x i8> [[R]]
+;
+; AVX2-LABEL: define <4 x i8> @add_concat(
+; AVX2-SAME: <2 x i8> [[A:%.*]], <2 x i8> [[B:%.*]], <2 x i8> [[C:%.*]], <2 x i8> [[D:%.*]]) #[[ATTR0:[0-9]+]] {
+; AVX2-NEXT:    [[TMP1:%.*]] = add <2 x i8> [[A]], [[C]]
+; AVX2-NEXT:    [[TMP2:%.*]] = add <2 x i8> [[B]], [[D]]
+; AVX2-NEXT:    [[R:%.*]] = shufflevector <2 x i8> [[TMP1]], <2 x i8> [[TMP2]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+; AVX2-NEXT:    ret <4 x i8> [[R]]
+;
+  %concat1 = shufflevector <2 x i8> %a, <2 x i8> %b, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %concat2 = shufflevector <2 x i8> %c, <2 x i8> %d, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %r = add <4 x i8> %concat1, %concat2
+  ret <4 x i8> %r
+}
+
+define <16 x i16> @or_concat(<8 x i16> %add, <8 x i16> %zext9) {
+; CHECK-LABEL: define <16 x i16> @or_concat(
+; CHECK-SAME: <8 x i16> [[ADD:%.*]], <8 x i16> [[ZEXT9:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[SHUFFLEVECTOR6:%.*]] = shufflevector <8 x i16> [[ADD]], <8 x i16> zeroinitializer, <16 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15>
+; CHECK-NEXT:    [[SHUFFLEVECTOR10:%.*]] = shufflevector <8 x i16> [[ZEXT9]], <8 x i16> zeroinitializer, <16 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15>
+; CHECK-NEXT:    [[OR:%.*]] = or <16 x i16> [[SHUFFLEVECTOR6]], [[SHUFFLEVECTOR10]]
+; CHECK-NEXT:    ret <16 x i16> [[OR]]
+;
+; AVX2-LABEL: define <16 x i16> @or_concat(
+; AVX2-SAME: <8 x i16> [[ADD:%.*]], <8 x i16> [[ZEXT9:%.*]]) #[[ATTR0]] {
+; AVX2-NEXT:  [[ENTRY:.*:]]
+; AVX2-NEXT:    [[SHUFFLEVECTOR6:%.*]] = shufflevector <8 x i16> [[ADD]], <8 x i16> zeroinitializer, <16 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15>
+; AVX2-NEXT:    [[SHUFFLEVECTOR10:%.*]] = shufflevector <8 x i16> [[ZEXT9]], <8 x i16> zeroinitializer, <16 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15>
+; AVX2-NEXT:    [[OR:%.*]] = or <16 x i16> [[SHUFFLEVECTOR6]], [[SHUFFLEVECTOR10]]
+; AVX2-NEXT:    ret <16 x i16> [[OR]]
+;
+entry:
+  %shufflevector6 = shufflevector <8 x i16> %add, <8 x i16> zeroinitializer, <16 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15>
+  %shufflevector10 = shufflevector <8 x i16> %zext9, <8 x i16> zeroinitializer, <16 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15>
+  %or = or <16 x i16> %shufflevector6, %shufflevector10
+  ret <16 x i16> %or
+}
+
+; Negative test: on AVX2 the cost is an exact tie, so the fold must not fire.
+define <8 x i32> @add_concat_cost_tie(<4 x i32> %a, <4 x i32> %b, <4 x i32> %c, <4 x i32> %d) {
+; CHECK-LABEL: define <8 x i32> @add_concat_cost_tie(
+; CHECK-SAME: <4 x i32> [[A:%.*]], <4 x i32> [[B:%.*]], <4 x i32> [[C:%.*]], <4 x i32> [[D:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT:    [[CONCAT1:%.*]] = shufflevector <4 x i32> [[A]], <4 x i32> [[B]], <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
+; CHECK-NEXT:    [[CONCAT2:%.*]] = shufflevector <4 x i32> [[C]], <4 x i32> [[D]], <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
+; CHECK-NEXT:    [[R:%.*]] = add <8 x i32> [[CONCAT1]], [[CONCAT2]]
+; CHECK-NEXT:    ret <8 x i32> [[R]]
+;
+; AVX2-LABEL: define <8 x i32> @add_concat_cost_tie(
+; AVX2-SAME: <4 x i32> [[A:%.*]], <4 x i32> [[B:%.*]], <4 x i32> [[C:%.*]], <4 x i32> [[D:%.*]]) #[[ATTR0]] {
+; AVX2-NEXT:    [[CONCAT1:%.*]] = shufflevector <4 x i32> [[A]], <4 x i32> [[B]], <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
+; AVX2-NEXT:    [[CONCAT2:%.*]] = shufflevector <4 x i32> [[C]], <4 x i32> [[D]], <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
+; AVX2-NEXT:    [[R:%.*]] = add <8 x i32> [[CONCAT1]], [[CONCAT2]]
+; AVX2-NEXT:    ret <8 x i32> [[R]]
+;
+  %concat1 = shufflevector <4 x i32> %a, <4 x i32> %b, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
+  %concat2 = shufflevector <4 x i32> %c, <4 x i32> %d, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
+  %r = add <8 x i32> %concat1, %concat2
+  ret <8 x i32> %r
+}
+
+; Negative test: on AVX2, splitting the i8 mul is strictly more expensive.
+define <32 x i8> @mul_concat_no_profit(<16 x i8> %a, <16 x i8> %b, <16 x i8> %c, <16 x i8> %d) {
+; CHECK-LABEL: define <32 x i8> @mul_concat_no_profit(
+; CHECK-SAME: <16 x i8> [[A:%.*]], <16 x i8> [[B:%.*]], <16 x i8> [[C:%.*]], <16 x i8> [[D:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT:    [[CONCAT1:%.*]] = shufflevector <16 x i8> [[A]], <16 x i8> [[B]], <32 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15, i32 16, i32 17, i32 18, i32 19, i32 20, i32 21, i32 22, i32 23, i32 24, i32 25, i32 26, i32 27, i32 28, i32 29, i32 30, i32 31>
+; CHECK-NEXT:    [[CONCAT2:%.*]] = shufflevector <16 x i8> [[C]], <16 x i8> [[D]], <32 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15, i32 16, i32 17, i32 18, i32 19, i32 20, i32 21, i32 22, i32 23, i32 24, i32 25, i32 26, i32 27, i32 28, i32 29, i32 30, i32 31>
+; CHECK-NEXT:    [[R:%.*]] = mul <32 x i8> [[CONCAT1]], [[CONCAT2]]
+; CHECK-NEXT:    ret <32 x i8> [[R]]
+;
+; AVX2-LABEL: define <32 x i8> @mul_concat_no_profit(
+; AVX2-SAME: <16 x i8> [[A:%.*]], <16 x i8> [[B:%.*]], <16 x i8> [[C:%.*]], <16 x i8> [[D:%.*]]) #[[ATTR0]] {
+; AVX2-NEXT:    [[CONCAT1:%.*]] = shufflevector <16 x i8> [[A]], <16 x i8> [[B]], <32 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15, i32 16, i32 17, i32 18, i32 19, i32 20, i32 21, i32 22, i32 23, i32 24, i32 25, i32 26, i32 27, i32 28, i32 29, i32 30, i32 31>
+; AVX2-NEXT:    [[CONCAT2:%.*]] = shufflevector <16 x i8> [[C]], <16 x i8> [[D]], <32 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15, i32 16, i32 17, i32 18, i32 19, i32 20, i32 21, i32 22, i32 23, i32 24, i32 25, i32 26, i32 27, i32 28, i32 29, i32 30, i32 31>
+; AVX2-NEXT:    [[R:%.*]] = mul <32 x i8> [[CONCAT1]], [[CONCAT2]]
+; AVX2-NEXT:    ret <32 x i8> [[R]]
+;
+  %concat1 = shufflevector <16 x i8> %a, <16 x i8> %b, <32 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15, i32 16, i32 17, i32 18, i32 19, i32 20, i32 21, i32 22, i32 23, i32 24, i32 25, i32 26, i32 27, i32 28, i32 29, i32 30, i32 31>
+  %concat2 = shufflevector <16 x i8> %c, <16 x i8> %d, <32 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15, i32 16, i32 17, i32 18, i32 19, i32 20, i32 21, i32 22, i32 23, i32 24, i32 25, i32 26, i32 27, i32 28, i32 29, i32 30, i32 31>
+  %r = mul <32 x i8> %concat1, %concat2
+  ret <32 x i8> %r
+}
+
+; Negative test: one operand of the add is not a shuffle at all.
+define <4 x i8> @add_lhs_not_shuffle(<4 x i8> %a, <2 x i8> %c, <2 x i8> %d) {
+; CHECK-LABEL: define <4 x i8> @add_lhs_not_shuffle(
+; CHECK-SAME: <4 x i8> [[A:%.*]], <2 x i8> [[C:%.*]], <2 x i8> [[D:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT:    [[CONCAT2:%.*]] = shufflevector <2 x i8> [[C]], <2 x i8> [[D]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+; CHECK-NEXT:    [[R:%.*]] = add <4 x i8> [[A]], [[CONCAT2]]
+; CHECK-NEXT:    ret <4 x i8> [[R]]
+;
+; AVX2-LABEL: define <4 x i8> @add_lhs_not_shuffle(
+; AVX2-SAME: <4 x i8> [[A:%.*]], <2 x i8> [[C:%.*]], <2 x i8> [[D:%.*]]) #[[ATTR0]] {
+; AVX2-NEXT:    [[CONCAT2:%.*]] = shufflevector <2 x i8> [[C]], <2 x i8> [[D]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+; AVX2-NEXT:    [[R:%.*]] = add <4 x i8> [[A]], [[CONCAT2]]
+; AVX2-NEXT:    ret <4 x i8> [[R]]
+;
+  %concat2 = shufflevector <2 x i8> %c, <2 x i8> %d, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %r = add <4 x i8> %a, %concat2
+  ret <4 x i8> %r
+}
+
+; Negative test: the two shuffles feeding the add use different masks.
+define <4 x i8> @add_mask_mismatch(<2 x i8> %a, <2 x i8> %b, <2 x i8> %c, <2 x i8> %d) {
+; CHECK-LABEL: define <4 x i8> @add_mask_mismatch(
+; CHECK-SAME: <2 x i8> [[A:%.*]], <2 x i8> [[B:%.*]], <2 x i8> [[C:%.*]], <2 x i8> [[D:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT:    [[CONCAT1:%.*]] = shufflevector <2 x i8> [[A]], <2 x i8> [[B]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+; CHECK-NEXT:    [[CONCAT2:%.*]] = shufflevector <2 x i8> [[C]], <2 x i8> [[D]], <4 x i32> <i32 1, i32 0, i32 3, i32 2>
+; CHECK-NEXT:    [[R:%.*]] = add <4 x i8> [[CONCAT1]], [[CONCAT2]]
+; CHECK-NEXT:    ret <4 x i8> [[R]]
+;
+; AVX2-LABEL: define <4 x i8> @add_mask_mismatch(
+; AVX2-SAME: <2 x i8> [[A:%.*]], <2 x i8> [[B:%.*]], <2 x i8> [[C:%.*]], <2 x i8> [[D:%.*]]) #[[ATTR0]] {
+; AVX2-NEXT:    [[CONCAT1:%.*]] = shufflevector <2 x i8> [[A]], <2 x i8> [[B]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+; AVX2-NEXT:    [[CONCAT2:%.*]] = shufflevector <2 x i8> [[C]], <2 x i8> [[D]], <4 x i32> <i32 1, i32 0, i32 3, i32 2>
+; AVX2-NEXT:    [[R:%.*]] = add <4 x i8> [[CONCAT1]], [[CONCAT2]]
+; AVX2-NEXT:    ret <4 x i8> [[R]]
+;
+  %concat1 = shufflevector <2 x i8> %a, <2 x i8> %b, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %concat2 = shufflevector <2 x i8> %c, <2 x i8> %d, <4 x i32> <i32 1, i32 0, i32 3, i32 2>
+  %r = add <4 x i8> %concat1, %concat2
+  ret <4 x i8> %r
+}
+
+; Negative test: same mask on both shuffles, but it swaps halves, not a concat.
+define <4 x i8> @add_mask_not_concat(<2 x i8> %a, <2 x i8> %b, <2 x i8> %c, <2 x i8> %d) {
+; CHECK-LABEL: define <4 x i8> @add_mask_not_concat(
+; CHECK-SAME: <2 x i8> [[A:%.*]], <2 x i8> [[B:%.*]], <2 x i8> [[C:%.*]], <2 x i8> [[D:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT:    [[CONCAT1:%.*]] = shufflevector <2 x i8> [[A]], <2 x i8> [[B]], <4 x i32> <i32 2, i32 3, i32 0, i32 1>
+; CHECK-NEXT:    [[CONCAT2:%.*]] = shufflevector <2 x i8> [[C]], <2 x i8> [[D]], <4 x i32> <i32 2, i32 3, i32 0, i32 1>
+; CHECK-NEXT:    [[R:%.*]] = add <4 x i8> [[CONCAT1]], [[CONCAT2]]
+; CHECK-NEXT:    ret <4 x i8> [[R]]
+;
+; AVX2-LABEL: define <4 x i8> @add_mask_not_concat(
+; AVX2-SAME: <2 x i8> [[A:%.*]], <2 x i8> [[B:%.*]], <2 x i8> [[C:%.*]], <2 x i8> [[D:%.*]]) #[[ATTR0]] {
+; AVX2-NEXT:    [[CONCAT1:%.*]] = shufflevector <2 x i8> [[A]], <2 x i8> [[B]], <4 x i32> <i32 2, i32 3, i32 0, i32 1>
+; AVX2-NEXT:    [[CONCAT2:%.*]] = shufflevector <2 x i8> [[C]], <2 x i8> [[D]], <4 x i32> <i32 2, i32 3, i32 0, i32 1>
+; AVX2-NEXT:    [[R:%.*]] = add <4 x i8> [[CONCAT1]], [[CONCAT2]]
+; AVX2-NEXT:    ret <4 x i8> [[R]]
+;
+  %concat1 = shufflevector <2 x i8> %a, <2 x i8> %b, <4 x i32> <i32 2, i32 3, i32 0, i32 1>
+  %concat2 = shufflevector <2 x i8> %c, <2 x i8> %d, <4 x i32> <i32 2, i32 3, i32 0, i32 1>
+  %r = add <4 x i8> %concat1, %concat2
+  ret <4 x i8> %r
+}
+
+; Negative test: a poison mask element must block the fold for div/rem.
+define <4 x i32> @udiv_concat_poison_mask(<2 x i32> %a, <2 x i32> %b, <2 x i32> %c, <2 x i32> %d) {
+; CHECK-LABEL: define <4 x i32> @udiv_concat_poison_mask(
+; CHECK-SAME: <2 x i32> [[A:%.*]], <2 x i32> [[B:%.*]], <2 x i32> [[C:%.*]], <2 x i32> [[D:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT:    [[CONCAT1:%.*]] = shufflevector <2 x i32> [[A]], <2 x i32> [[B]], <4 x i32> <i32 0, i32 poison, i32 2, i32 3>
+; CHECK-NEXT:    [[CONCAT2:%.*]] = shufflevector <2 x i32> [[C]], <2 x i32> [[D]], <4 x i32> <i32 0, i32 poison, i32 2, i32 3>
+; CHECK-NEXT:    [[R:%.*]] = udiv <4 x i32> [[CONCAT1]], [[CONCAT2]]
+; CHECK-NEXT:    ret <4 x i32> [[R]]
+;
+; AVX2-LABEL: define <4 x i32> @udiv_concat_poison_mask(
+; AVX2-SAME: <2 x i32> [[A:%.*]], <2 x i32> [[B:%.*]], <2 x i32> [[C:%.*]], <2 x i32> [[D:%.*]]) #[[ATTR0]] {
+; AVX2-NEXT:    [[CONCAT1:%.*]] = shufflevector <2 x i32> [[A]], <2 x i32> [[B]], <4 x i32> <i32 0, i32 poison, i32 2, i32 3>
+; AVX2-NEXT:    [[CONCAT2:%.*]] = shufflevector <2 x i32> [[C]], <2 x i32> [[D]], <4 x i32> <i32 0, i32 poison, i32 2, i32 3>
+; AVX2-NEXT:    [[R:%.*]] = udiv <4 x i32> [[CONCAT1]], [[CONCAT2]]
+; AVX2-NEXT:    ret <4 x i32> [[R]]
+;
+  %concat1 = shufflevector <2 x i32> %a, <2 x i32> %b, <4 x i32> <i32 0, i32 poison, i32 2, i32 3>
+  %concat2 = shufflevector <2 x i32> %c, <2 x i32> %d, <4 x i32> <i32 0, i32 poison, i32 2, i32 3>
+  %r = udiv <4 x i32> %concat1, %concat2
+  ret <4 x i32> %r
+}
+

``````````

</details>


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


More information about the llvm-commits mailing list