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

Matthew Devereau via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 29 09:51:20 PDT 2025


================
@@ -2287,6 +2287,36 @@ 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) {
+  if (!MD)
+    return false;
+  MemDepResult Dep = MD->getDependency(I);
+  Instruction *DepInst = Dep.getInst();
+  if (!DepInst || !Dep.isLocal() || !Dep.isDef())
+    return false;
+
+  Value *Mask = I->getOperand(2);
+  Value *Passthrough = I->getOperand(3);
+  Value *StoreVal;
+  if (!match(DepInst, m_MaskedStore(m_Value(StoreVal), m_Value(), m_Value(),
+                                    m_Specific(Mask))))
+    return false;
+
+  // Remove the load but generate a select for the passthrough
+  Value *OpToForward = llvm::SelectInst::Create(Mask, StoreVal, Passthrough, "",
+                                                I->getIterator());
+
+  ICF->removeUsersOf(I);
+  I->replaceAllUsesWith(OpToForward);
+  salvageAndRemoveInstruction(I);
+  if (OpToForward->getType()->isPtrOrPtrVectorTy())
----------------
MDevereau wrote:

After looking into it `invalidateCachedPointerInfo` only handles scalar pointers, and just bails on pointer vectors. Therefore I don't think we need to call it here, since masked loads can only be vector types. I've removed the check for a pointer/vector pointer type.

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


More information about the llvm-commits mailing list