[llvm] [MemCpyOpt] Drop dead `memmove` calls on `memset`'d source data (PR #101930)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 29 08:56:00 PST 2024


================
@@ -1841,12 +1842,74 @@ bool MemCpyOptPass::processMemCpy(MemCpyInst *M, BasicBlock::iterator &BBI) {
   return false;
 }
 
+/// Memmove calls with overlapping src/dest buffers that come after a memset may
+/// be removed.
+bool MemCpyOptPass::isMemMoveMemSetDependency(MemMoveInst *M) {
+  const auto &DL = M->getDataLayout();
+  MemoryUseOrDef *MemMoveAccess = MSSA->getMemoryAccess(M);
+  if (!MemMoveAccess)
+    return false;
+
+  // The memmove is of form memmove(x, x + A, B).
+  MemoryLocation SourceLoc = MemoryLocation::getForSource(M);
+  auto *MemMoveSourceOp = M->getSource();
+  auto *Source = dyn_cast<GEPOperator>(MemMoveSourceOp);
+  if (!Source)
+    return false;
+
+  APInt Offset(DL.getIndexTypeSizeInBits(Source->getType()), 0);
+  LocationSize MemMoveLocSize = SourceLoc.Size;
+  if (Source->getPointerOperand() != M->getDest() ||
+      !MemMoveLocSize.hasValue() || Offset.isNegative() ||
+      !Source->accumulateConstantOffset(DL, Offset)) {
+    return false;
+  }
+
+  const uint64_t MemMoveSize = MemMoveLocSize.getValue();
----------------
nikic wrote:

```suggestion
  uint64_t MemMoveSize = MemMoveLocSize.getValue();
```

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


More information about the llvm-commits mailing list