[llvm] [SLP]Initial compatibility support for shl v, 1 and add v, v (PR #181168)

Alexey Bataev via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 13 15:48:12 PDT 2026


================
@@ -26231,6 +26337,35 @@ class HorizontalReduction {
         ReducedVals.erase(std::next(ReducedVals.begin(), SelectIdx));
       }
     }
+    // Check if shl %x, 1 can be merged with adds.
+    auto ShlIt = UsedReductionOpIds.find(Instruction::Shl);
+    auto AddIt = UsedReductionOpIds.find(Instruction::Add);
+    if (ShlIt != UsedReductionOpIds.end() &&
+        AddIt != UsedReductionOpIds.end()) {
+      unsigned ShlIdx = ShlIt->second;
+      unsigned AddIdx = AddIt->second;
+      if (ReducedVals[ShlIdx].size() < ReductionLimit) {
+        SmallVector<Value *> Shls;
+        SmallVector<Value *> Remaining;
+        for (Value *V : ReducedVals[ShlIdx]) {
+          if (match(V, m_Shl(m_Value(), m_One())))
+            Shls.push_back(V);
+          else
+            Remaining.push_back(V);
+        }
+        // Have compatible shls? Merge them to adds, if so.
+        if (!Shls.empty()) {
+          Shls.append(ReducedVals[AddIdx]);
+          ReducedVals[AddIdx].swap(Shls);
+          if (Remaining.empty())
+            ReducedVals.erase(std::next(ReducedVals.begin(), ShlIdx));
+          else
+            ReducedVals[ShlIdx].swap(Remaining);
+          // Clear UsedReductionOpIds since it is not valid anymore.
+          UsedReductionOpIds.clear();
+        }
+      }
+    }
----------------
alexey-bataev wrote:

[test/Transforms/SLPVectorizer/X86/deleted-instructions-clear.ll](https://github.com/llvm/llvm-project/pull/181168/changes#diff-3084c91cda0d6c881cc53dabd24eab9b6e927cab98665d5547831e4ad5997a21)

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


More information about the llvm-commits mailing list