[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 13:38:23 PST 2024
================
@@ -1841,12 +1842,77 @@ 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() ||
+ !Source->accumulateConstantOffset(DL, Offset) || Offset.isNegative()) {
+ return false;
+ }
+
+ uint64_t MemMoveSize = MemMoveLocSize.getValue();
+ LocationSize TotalSize =
+ LocationSize::precise(Offset.getZExtValue() + MemMoveSize);
+ MemoryLocation CombinedSourceLoc(MemMoveSourceOp, TotalSize);
+ MemoryLocation CombinedDestLoc(M->getDest(), TotalSize);
+
+ // The first dominating clobbering MemoryAccess for the combined location
+ // needs to be a memset.
+ BatchAAResults BAA(*AA);
+ MemoryAccess *FirstDef = MemMoveAccess->getDefiningAccess();
+ auto *DestClobber =
+ dyn_cast<MemoryDef>(MSSA->getWalker()->getClobberingMemoryAccess(
+ FirstDef, CombinedDestLoc, BAA));
+ if (!DestClobber)
+ return false;
+
+ auto *MS = dyn_cast_or_null<MemSetInst>(DestClobber->getMemoryInst());
+ if (!MS)
+ return false;
+
+ // Memset length must be sufficiently large.
+ auto *MemSetLength = dyn_cast<ConstantInt>(MS->getLength());
+ if (!MemSetLength || MemSetLength->getZExtValue() < MemMoveSize)
+ return false;
+
+ // The destination buffer must have been memset'd.
+ if (!BAA.isMustAlias(MS->getDest(), M->getDest()))
----------------
nikic wrote:
Can you please add a negative test where this check fails?
https://github.com/llvm/llvm-project/pull/101930
More information about the llvm-commits
mailing list