[llvm] [GVN] Teach GVN simple masked load/store forwarding (PR #157689)

Matthew Devereau via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 23 06:28:52 PDT 2025


================
@@ -2287,6 +2288,46 @@ bool GVNPass::processLoad(LoadInst *L) {
   return true;
 }
 
+// Attempt to process masked loads which have loaded from
+// masked stores with the same mask
+bool GVNPass::processMaskedLoad(IntrinsicInst *I) {
+  Value *Mask = I->getOperand(2);
+  Value *Passthrough = I->getOperand(3);
+
+  MemDepResult Dep = MD->getDependency(I);
+  Instruction *DepInst = Dep.getInst();
+  if (!DepInst || !Dep.isLocal())
+    return false;
+
+  Value *StoreVal;
+  if (!match(DepInst,
+             m_Intrinsic<Intrinsic::masked_store>(m_Value(StoreVal), m_Value(),
+                                                  m_Value(), m_Specific(Mask))))
+    return false;
+
+  Value *OpToForward = nullptr;
+  if (match(StoreVal, m_MaskedLoad(m_Value(), m_Value(), m_Specific(Mask),
+                                   m_Specific(Passthrough))))
+    // For MaskedLoad->MaskedStore->MaskedLoad, the mask must be the same for
+    // all three instructions. The Passthrough on the two loads must also be the
+    // same.
+    OpToForward = AvailableValue::get(StoreVal).getSimpleValue();
+  else if (match(StoreVal, m_Intrinsic<Intrinsic::masked_load>()))
----------------
MDevereau wrote:

The test `@bail_on_different_passthrough` I added to `masked-load-store.ll` removes a load when excluding clobbers. I think it's OK since it falls to the else case where the inactive select lanes are the passthrough value, so i've renamed the test to `generate_sel_with_passthrough`

That being said, removing the matches for StoreVal which you suggested [here](https://github.com/llvm/llvm-project/pull/157689/files#r2359352866) and always generating a select removes the need for this else if clause anyway.

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


More information about the llvm-commits mailing list