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

David Sherwood via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 18 06:37:41 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),
----------------
david-arm wrote:

It feels a bit wrong to be testing the stored value here - it's an extra level of complexity that I don't think you need. I'd expect that if you always create a select like you do below, that instcombine would fold the select into the masked load anyway if the passthru matches. If instcombine doesn't do that, then perhaps worth adding that pattern?

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


More information about the llvm-commits mailing list