[llvm] [SLP] More OOP to simplify vectorizeStores() (NFC) (PR #134605)

Gaëtan Bossu via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 14 02:29:51 PDT 2025


================
@@ -20358,12 +20364,54 @@ struct RelatedStoreInsts {
     return Inserted ? std::nullopt : std::optional<unsigned>(It->second);
   }
 
+  StoreInst &getBaseStore() const { return *AllStores[BaseInstrIdx]; }
+  using DistToInstMap = std::map<int, unsigned>;
+  const DistToInstMap &getStores() const { return Instrs; }
+
+  /// Recompute the pointer distances to be based on \p NewBaseInstIdx.
+  /// Stores whose index is less than \p MinSafeIdx will be dropped.
+  void rebase(unsigned MinSafeIdx, unsigned NewBaseInstIdx,
+              int DistFromCurBase) {
+    DistToInstMap PrevSet = std::move(Instrs);
+    reset(NewBaseInstIdx);
+
+    // Re-insert stores that come after MinSafeIdx to try and vectorize them
+    // again. Their distance will be "rebased" to use NewBaseInstIdx as
+    // reference.
+    for (auto [Dist, InstIdx] : PrevSet) {
+      if (InstIdx >= MinSafeIdx) {
+        insertOrLookup(InstIdx, Dist - DistFromCurBase);
+      }
+    }
+  }
+
+  /// Remove all stores that have been vectorized from this group.
+  void clearVectorizedStores(const BoUpSLP::ValueSet &VectorizedStores) {
+    const auto Begin = Instrs.begin();
+    auto NonVectorizedStore = Instrs.end();
+
+    while (NonVectorizedStore != Begin) {
----------------
gbossu wrote:

Okay I'm not entirely happy with calling `.base()` on an iterator, but in the end, the code is not that bad and I have no particular as to which implementation I prefer. So if you're happier with what's below, I'd be happy to oblige.

```
  /// Remove all stores that have been vectorized from this group.
  void clearVectorizedStores(const BoUpSLP::ValueSet &VectorizedStores) {
    DistToInstMap::reverse_iterator LastVectorizedStore = find_if(
        reverse(Instrs), [&](const std::pair<int, unsigned> &DistAndIdx) {
          return VectorizedStores.contains(AllStores[DistAndIdx.second]);
        });

    // Get a forward iterator to the last scalar store and erase all stores
    // before it so we don't try to vectorize them again.
    DistToInstMap::iterator NonVectorizedStore = LastVectorizedStore.base();
    Instrs.erase(Instrs.begin(), NonVectorizedStore);
  }
```

Please let me know if you have any other concerns with the rest of the PR, then I can push all the changes in one go 🙂 

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


More information about the llvm-commits mailing list