[llvm] MemCpyOpt: replace an AA query with MSSA query (NFC) (PR #108535)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 23 02:53:46 PDT 2024


================
@@ -647,19 +647,18 @@ bool MemCpyOptPass::processStoreOfLoad(StoreInst *SI, LoadInst *LI,
       (EnableMemCpyOptWithoutLibcalls ||
        (TLI->has(LibFunc_memcpy) && TLI->has(LibFunc_memmove)))) {
     MemoryLocation LoadLoc = MemoryLocation::get(LI);
-
-    // We use alias analysis to check if an instruction may store to
-    // the memory we load from in between the load and the store. If
-    // such an instruction is found, we try to promote there instead
-    // of at the store position.
-    // TODO: Can use MSSA for this.
-    Instruction *P = SI;
-    for (auto &I : make_range(++LI->getIterator(), SI->getIterator())) {
-      if (isModSet(AA->getModRefInfo(&I, LoadLoc))) {
-        P = &I;
-        break;
-      }
-    }
+    MemoryUseOrDef *LoadAccess = MSSA->getMemoryAccess(LI),
+                   *StoreAccess = MSSA->getMemoryAccess(SI);
+
+    // We use MSSA to check if an instruction may store to the memory we load
+    // from in between the load and the store. If such an instruction is found,
+    // we try to promote there instead of at the store position.
+    BatchAAResults BAA(*AA);
+    auto *Clobber =
+        cast<MemoryUseOrDef>(MSSA->getWalker()->getClobberingMemoryAccess(
----------------
nikic wrote:

Why is this cast safe? Can't this return a MemoryPhi? I think you'd have to move this cast beneath the dominates() check, because in that case we know it must be between two MemoryUseOrDef in the same block and thus can't be a MemoryPhi.

It would be good to add a test where we get a MemoryPhi result.

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


More information about the llvm-commits mailing list