[llvm] [VectorCombine] Avoid push_back in range-for (PR #196741)

via llvm-commits llvm-commits at lists.llvm.org
Sat May 9 11:01:35 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Hongyu Chen (XChy)

<details>
<summary>Changes</summary>

Fixes https://github.com/llvm/llvm-project/issues/196709

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


2 Files Affected:

- (modified) llvm/lib/Transforms/Vectorize/VectorCombine.cpp (+6-2) 
- (modified) llvm/test/Transforms/VectorCombine/AArch64/select-shuffle.ll (+19-1) 


``````````diff
diff --git a/llvm/lib/Transforms/Vectorize/VectorCombine.cpp b/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
index 5ba344ea9a808..de0c3722d2a2f 100644
--- a/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
+++ b/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
@@ -4980,11 +4980,15 @@ bool VectorCombine::foldSelectShuffle(Instruction &I, bool FromReduction) {
   // Add any shuffle uses for the shuffles we have found, to include them in our
   // cost calculations.
   if (!FromReduction) {
-    for (ShuffleVectorInst *SV : Shuffles) {
+    size_t OldShuffleSize = Shuffles.size();
+    for (size_t i = 0; i < OldShuffleSize; ++i) {
+      ShuffleVectorInst *SV = Shuffles[i];
       for (auto *U : SV->users()) {
         ShuffleVectorInst *SSV = dyn_cast<ShuffleVectorInst>(U);
-        if (SSV && isa<UndefValue>(SSV->getOperand(1)) && SSV->getType() == VT)
+        if (SSV && isa<UndefValue>(SSV->getOperand(1)) &&
+            SSV->getType() == VT) {
           Shuffles.push_back(SSV);
+        }
       }
     }
   }
diff --git a/llvm/test/Transforms/VectorCombine/AArch64/select-shuffle.ll b/llvm/test/Transforms/VectorCombine/AArch64/select-shuffle.ll
index 5328975f3b00d..f2d1490220526 100644
--- a/llvm/test/Transforms/VectorCombine/AArch64/select-shuffle.ll
+++ b/llvm/test/Transforms/VectorCombine/AArch64/select-shuffle.ll
@@ -984,6 +984,24 @@ entry:
   ret <64 x i32> %6
 }
 
-
+define <4 x i32> @output_undefs(<4 x i32> %a, <4 x i32> %b, <4 x i32> %c, <4 x i32> %d) {
+; CHECK-LABEL: @output_undefs(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[SHUF0:%.*]] = shufflevector <4 x i32> [[A:%.*]], <4 x i32> [[B:%.*]], <4 x i32> <i32 0, i32 1, i32 4, i32 5>
+; CHECK-NEXT:    [[SHUF1:%.*]] = shufflevector <4 x i32> [[C:%.*]], <4 x i32> [[D:%.*]], <4 x i32> <i32 0, i32 1, i32 4, i32 5>
+; CHECK-NEXT:    [[ADD:%.*]] = add <4 x i32> [[SHUF0]], [[SHUF1]]
+; CHECK-NEXT:    [[SUB:%.*]] = sub <4 x i32> [[SHUF0]], [[SHUF1]]
+; CHECK-NEXT:    [[U00:%.*]] = shufflevector <4 x i32> [[ADD]], <4 x i32> [[SUB]], <4 x i32> <i32 0, i32 1, i32 4, i32 5>
+; CHECK-NEXT:    ret <4 x i32> [[U00]]
+;
+entry:
+  %shuf0 = shufflevector <4 x i32> %a, <4 x i32> %b, <4 x i32> <i32 0, i32 1, i32 4, i32 5>
+  %shuf1 = shufflevector <4 x i32> %c, <4 x i32> %d, <4 x i32> <i32 0, i32 1, i32 4, i32 5>
+  %add = add <4 x i32> %shuf0, %shuf1
+  %sub = sub <4 x i32> %shuf0, %shuf1
+  %s0 = shufflevector <4 x i32> %add, <4 x i32> %sub, <4 x i32> <i32 0, i32 1, i32 4, i32 5>
+  %u00 = shufflevector <4 x i32> %s0, <4 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  ret <4 x i32> %u00
+}
 
 declare i32 @llvm.vector.reduce.add.v16i32(<16 x i32>)

``````````

</details>


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


More information about the llvm-commits mailing list