[llvm] [SimplifyCFG] Avoid sinking loads/stores that impact vectorization (PR #222587)

Ashutosh Nema via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 11 01:17:38 PDT 2026


================
@@ -2523,10 +2619,29 @@ static bool sinkCommonCodeFromPredecessors(BasicBlock *BB,
       return false;
     };
 
+    // Check whether the memory operations in \p Insts all access the same
+    // address, so that commoning them needs no PHI for the pointer operand.
+    auto HaveSameMemAddress = [](ArrayRef<Instruction *> Insts) {
+      Value *Ptr = getLoadStorePointerOperand(Insts.front());
+      auto *PtrI = dyn_cast<Instruction>(Ptr);
+      return all_of(drop_begin(Insts), [&](Instruction *I) {
+        Value *OtherPtr = getLoadStorePointerOperand(I);
+        if (OtherPtr == Ptr)
+          return true;
+        auto *OtherPtrI = dyn_cast<Instruction>(OtherPtr);
+        return PtrI && OtherPtrI && PtrI->isIdenticalTo(OtherPtrI);
----------------
nema-ashutosh wrote:

I don’t think we can drop this just yet. InstCombine doesn’t CSE GEPs in sibling blocks, so a[i] on both sides is still two GEPs, isIdenticalTo is what treats them as the same address (same_store_address_expression).

A plain if/else usually doesn’t hit this at -O2 because MergedLoadStoreMotion sinks it first. A 3-way switch storing to a[i] does: MergedLoadStoreMotion skips it, and sink-common SimplifyCFG merges it.

Let me know if you’d still rather just compare the pointer values ?

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


More information about the llvm-commits mailing list